3 * $Id: tmpdir.c,v 1.3 1999/05/21 22:07:20 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.3 1999/05/21 22:07:20 mdw
33 * Take advantage of new dynamic string macros.
35 * Revision 1.2 1999/05/19 20:37:28 mdw
36 * Track gratuitous change in mdwopt interface.
38 * Revision 1.1.1.1 1999/04/06 20:12:07 mdw
43 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
55 #include <mLib/alloc.h>
56 #include <mLib/dstr.h>
57 #include <mLib/mdwopt.h>
58 #include <mLib/quis.h>
59 #include <mLib/report.h>
63 /*----- Static variables --------------------------------------------------*/
66 static struct chkpath cp;
67 static struct passwd *pw;
69 /*----- Main code ---------------------------------------------------------*/
73 * Arguments: @const char *p@ = pathname to check
74 * @int *f@ = try-to-create flag
76 * Returns: Nonzero if the directory is OK
78 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
79 * and @*f@ is set, then try to create the directory.
82 static int ok(const char *p, int *f)
86 /* --- Read the directory status --- */
90 /* --- Maybe create it if it doesn't exist --- */
92 if (errno != ENOENT || !f || !*f)
99 /* --- Now read the new status --- *
101 * This fixes a race condition between the previous @lstat@ call and
109 /* --- Make sure the directory is good --- *
111 * It must be a genuine directory (not a link); it must be readable
112 * and writable only by its owner, and that owner must be me.
115 if (S_ISDIR(st.st_mode) && (st.st_mode & 0777) == 0700 && st.st_uid == me)
120 /* --- @trytmp@ --- *
122 * Arguments: @const char *parent@ = parent directory name
123 * @const char *base@ = subdirectory base name
125 * Returns: Pointer to directory name, or null. (String needs freeing.)
127 * Use: Tries to find or create an appropriate temporary directory.
130 static char *trytmp(const char *parent, const char *base)
132 static char c[] = { "0123456789"
133 "abcdefghijklmnopqrstuvwxyz"
134 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
140 /* --- Make sure the parent directory is sane --- *
142 * Must make sure that all the lead-up to the temporary directory is safe.
143 * Otherwise other people can play with symlinks or rename directories
144 * after I've done all this careful work to make the endpoint directory
148 if (path_check(parent, &cp))
151 /* --- See whether the trivial version will work --- */
153 dstr_putf(&d, "%s/%s", parent, base);
154 if (ok(d.buf, &createflag))
157 /* --- Now try with various suffixes --- */
163 for (p = c; *p; p++) {
166 if (ok(d.buf, &createflag))
168 for (q = c; *q; q++) {
171 if (ok(d.buf, &createflag))
187 /* --- @fullcheck@ --- *
189 * Arguments: @const char *p@ = pointer to path to check
191 * Returns: Zero if it's a bad directory.
193 * Use: Runs a thorough check on a directory.
196 static int fullcheck(const char *p)
198 return (path_check(p, &cp) == 0 && ok(p, 0));
201 /* --- @goodtmp@ --- *
205 * Returns: Pointer to a known-good secure temporary directory, or
208 * Use: Finds a good place to store temporary files.
211 static char *goodtmp(void)
215 /* --- First of all, try the user's current choice --- */
217 if ((p = getenv("TMPDIR")) != 0 && fullcheck(p))
220 /* --- Try making a directory in `/tmp' --- */
222 if (!(q = getenv("USER")) && !(q = getenv("LOGNAME")))
224 if ((q = trytmp("/tmp", q)) != 0)
227 /* --- That failed: try a directory in the user's home --- */
229 if (!(q = getenv("HOME")))
231 if ((q = trytmp(q, "tmp")) != 0)
234 /* --- Still no joy: give up --- *
236 * To be fair, if the user can't find a safe place in his own home
237 * directory then he's pretty stuffed.
243 /* --- @usage@ --- */
245 static void usage(FILE *fp)
247 fprintf(fp, "Usage: %s [-bc] [-v PATH]\n", QUIS);
250 /* --- @version@ --- */
252 static void version(FILE *fp)
254 fprintf(fp, "%s version %s\n", QUIS, VERSION);
259 static void help(FILE *fp)
265 Sets a suitable and secure temporary directory, or checks that a given\n\
266 directory is suitable for use with temporary files. A directory is\n\
267 considered good for a particular user if it's readable and writable only\n\
268 by that user, and if all its parents are modifiable only by the user or\n\
271 Options supported:\n\
273 -h, --help Display this help text.\n\
274 -V, --version Display the program's version number.\n\
275 -u, --usage Display a terse usage summary.\n\
277 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
278 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
279 -v, --verify PATH Check whether PATH is good, setting exit status.\n\
281 The default action is to examine the caller's shell and output a suitable\n\
282 setting for that shell type.\n\
289 * Arguments: @int argc@ = number of command line arguments
290 * @char *argv[]@ = the actual argument strings
292 * Returns: Zero if all went well, else nonzero.
294 * Use: Performs helpful operations on temporary directories.
297 int main(int argc, char *argv[])
308 /* --- Initialize variables --- */
312 cp.cp_what = CP_WRWORLD | CP_WRGRP | CP_WROTHUSR | CP_STICKYOK;
318 die(1, "you don't exist");
320 /* --- Parse arguments --- */
323 static struct option opts[] = {
324 { "help", 0, 0, 'h' },
325 { "version", 0, 0, 'V' },
326 { "usage", 0, 0, 'u' },
327 { "bourne", 0, 0, 'b' },
328 { "cshell", 0, 0, 'c' },
329 { "verify", OPTF_ARGREQ, 0, 'v' },
332 int i = mdwopt(argc, argv, "hVu bcv:", opts, 0, 0, 0);
353 return (!fullcheck(optarg));
361 if (duff || optind != argc) {
366 /* --- Choose a shell --- */
370 if (!(p = getenv("SHELL")))
372 if (strstr(p, "csh"))
378 /* --- Start the checking --- */
383 die(1, "no good tmp directory");
386 printf("TMPDIR=\"%s\"; export TMPDIR\n", p);
389 printf("setenv TMPDIR \"%s\"\n", p);
397 /*----- That's all, folks -------------------------------------------------*/