3 * $Id: tmpdir.c,v 1.2 1999/05/19 20:37:28 mdw Exp $
5 * Choose and check temporary directories
7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of chkpath.
14 * chkpath is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * chkpath is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with chkpath; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/05/19 20:37:28 mdw
33 * Track gratuitous change in mdwopt interface.
35 * Revision 1.1.1.1 1999/04/06 20:12:07 mdw
40 /*----- Header files ------------------------------------------------------*/
47 #include <sys/types.h>
52 #include <mLib/alloc.h>
53 #include <mLib/dstr.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
60 /*----- Static variables --------------------------------------------------*/
63 static struct chkpath cp
;
64 static struct passwd
*pw
;
66 /*----- Main code ---------------------------------------------------------*/
70 * Arguments: @const char *p@ = pathname to check
71 * @int *f@ = try-to-create flag
73 * Returns: Nonzero if the directory is OK
75 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
76 * and @*f@ is set, then try to create the directory.
79 static int ok(const char *p
, int *f
)
83 /* --- Read the directory status --- */
87 /* --- Maybe create it if it doesn't exist --- */
89 if (errno
!= ENOENT
|| !f
|| !*f
)
96 /* --- Now read the new status --- *
98 * This fixes a race condition between the previous @lstat@ call and
106 /* --- Make sure the directory is good --- *
108 * It must be a genuine directory (not a link); it must be readable
109 * and writable only by its owner, and that owner must be me.
112 if (S_ISDIR(st
.st_mode
) && (st
.st_mode
& 0777) == 0700 && st
.st_uid
== me
)
117 /* --- @trytmp@ --- *
119 * Arguments: @const char *parent@ = parent directory name
120 * @const char *base@ = subdirectory base name
122 * Returns: Pointer to directory name, or null. (String needs freeing.)
124 * Use: Tries to find or create an appropriate temporary directory.
127 static char *trytmp(const char *parent
, const char *base
)
129 static char c
[] = { "0123456789"
130 "abcdefghijklmnopqrstuvwxyz"
131 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
137 /* --- Make sure the parent directory is sane --- *
139 * Must make sure that all the lead-up to the temporary directory is safe.
140 * Otherwise other people can play with symlinks or rename directories
141 * after I've done all this careful work to make the endpoint directory
145 if (path_check(parent
, &cp
))
148 /* --- See whether the trivial version will work --- */
151 dstr_putf(&d
, "%s/%s", parent
, base
);
152 if (ok(d
.buf
, &createflag
))
155 /* --- Now try with various suffixes --- */
161 for (p
= c
; *p
; p
++) {
164 if (ok(d
.buf
, &createflag
))
166 for (q
= c
; *q
; q
++) {
169 if (ok(d
.buf
, &createflag
))
185 /* --- @fullcheck@ --- *
187 * Arguments: @const char *p@ = pointer to path to check
189 * Returns: Zero if it's a bad directory.
191 * Use: Runs a thorough check on a directory.
194 static int fullcheck(const char *p
)
196 return (path_check(p
, &cp
) == 0 && ok(p
, 0));
199 /* --- @goodtmp@ --- *
203 * Returns: Pointer to a known-good secure temporary directory, or
206 * Use: Finds a good place to store temporary files.
209 static char *goodtmp(void)
213 /* --- First of all, try the user's current choice --- */
215 if ((p
= getenv("TMPDIR")) != 0 && fullcheck(p
))
218 /* --- Try making a directory in `/tmp' --- */
220 if (!(q
= getenv("USER")) && !(q
= getenv("LOGNAME")))
222 if ((q
= trytmp("/tmp", q
)) != 0)
225 /* --- That failed: try a directory in the user's home --- */
227 if (!(q
= getenv("HOME")))
229 if ((q
= trytmp(q
, "tmp")) != 0)
232 /* --- Still no joy: give up --- *
234 * To be fair, if the user can't find a safe place in his own home
235 * directory then he's pretty stuffed.
241 /* --- @usage@ --- */
243 static void usage(FILE *fp
)
245 fprintf(fp
, "Usage: %s [-bc] [-v PATH]\n", QUIS
);
248 /* --- @version@ --- */
250 static void version(FILE *fp
)
252 fprintf(fp
, "%s version %s\n", QUIS
, VERSION
);
257 static void help(FILE *fp
)
263 Sets a suitable and secure temporary directory, or checks that a given\n\
264 directory is suitable for use with temporary files. A directory is\n\
265 considered good for a particular user if it's readable and writable only\n\
266 by that user, and if all its parents are modifiable only by the user or\n\
269 Options supported:\n\
271 -h, --help Display this help text.\n\
272 -V, --version Display the program's version number.\n\
273 -u, --usage Display a terse usage summary.\n\
275 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
276 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
277 -v, --verify PATH Check whether PATH is good, setting exit status.\n\
279 The default action is to examine the caller's shell and output a suitable\n\
280 setting for that shell type.\n\
287 * Arguments: @int argc@ = number of command line arguments
288 * @char *argv[]@ = the actual argument strings
290 * Returns: Zero if all went well, else nonzero.
292 * Use: Performs helpful operations on temporary directories.
295 int main(int argc
, char *argv
[])
306 /* --- Initialize variables --- */
310 cp
.cp_what
= CP_WRWORLD
| CP_WRGRP
| CP_WROTHUSR
| CP_STICKYOK
;
316 die(1, "you don't exist");
318 /* --- Parse arguments --- */
321 static struct option opts
[] = {
322 { "help", 0, 0, 'h' },
323 { "version", 0, 0, 'V' },
324 { "usage", 0, 0, 'u' },
325 { "bourne", 0, 0, 'b' },
326 { "cshell", 0, 0, 'c' },
327 { "verify", OPTF_ARGREQ
, 0, 'v' },
330 int i
= mdwopt(argc
, argv
, "hVu bcv:", opts
, 0, 0, 0);
351 return (!fullcheck(optarg
));
359 if (duff
|| optind
!= argc
) {
364 /* --- Choose a shell --- */
368 if (!(p
= getenv("SHELL")))
370 if (strstr(p
, "csh"))
376 /* --- Start the checking --- */
381 die(1, "no good tmp directory");
384 printf("TMPDIR=\"%s\"; export TMPDIR\n", p
);
387 printf("setenv TMPDIR \"%s\"\n", p
);
395 /*----- That's all, folks -------------------------------------------------*/