3 * $Id: tmpdir.c,v 1.5 2004/04/08 01:36:22 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 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
43 #include <mLib/alloc.h>
44 #include <mLib/dstr.h>
45 #include <mLib/mdwopt.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
49 #include "checkpath.h"
51 /*----- Static variables --------------------------------------------------*/
54 static struct checkpath cp;
55 static struct passwd *pw;
57 /*----- Main code ---------------------------------------------------------*/
61 * Arguments: @const char *p@ = pathname to check
62 * @int *f@ = try-to-create flag
64 * Returns: Nonzero if the directory is OK
66 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
67 * and @*f@ is set, then try to create the directory.
70 static int ok(const char *p, int *f)
74 /* --- Read the directory status --- */
78 /* --- Maybe create it if it doesn't exist --- */
80 if (errno != ENOENT || !f || !*f)
87 /* --- Now read the new status --- *
89 * This fixes a race condition between the previous @lstat@ call and
97 /* --- Make sure the directory is good --- *
99 * It must be a genuine directory (not a link); it must be readable
100 * and writable only by its owner, and that owner must be me.
103 if (S_ISDIR(st.st_mode) && (st.st_mode & 0777) == 0700 && st.st_uid == me)
108 /* --- @trytmp@ --- *
110 * Arguments: @const char *parent@ = parent directory name
111 * @const char *base@ = subdirectory base name
113 * Returns: Pointer to directory name, or null. (String needs freeing.)
115 * Use: Tries to find or create an appropriate temporary directory.
118 static char *trytmp(const char *parent, const char *base)
120 static char c[] = { "0123456789"
121 "abcdefghijklmnopqrstuvwxyz"
122 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
128 /* --- Make sure the parent directory is sane --- *
130 * Must make sure that all the lead-up to the temporary directory is safe.
131 * Otherwise other people can play with symlinks or rename directories
132 * after I've done all this careful work to make the endpoint directory
136 if (checkpath(parent, &cp))
139 /* --- See whether the trivial version will work --- */
141 dstr_putf(&d, "%s/%s", parent, base);
142 if (ok(d.buf, &createflag))
145 /* --- Now try with various suffixes --- */
151 for (p = c; *p; p++) {
154 if (ok(d.buf, &createflag))
156 for (q = c; *q; q++) {
159 if (ok(d.buf, &createflag))
175 /* --- @fullcheck@ --- *
177 * Arguments: @const char *p@ = pointer to path to check
179 * Returns: Zero if it's a bad directory.
181 * Use: Runs a thorough check on a directory.
184 static int fullcheck(const char *p)
185 { return (checkpath(p, &cp) == 0 && ok(p, 0)); }
187 /* --- @goodtmp@ --- *
191 * Returns: Pointer to a known-good secure temporary directory, or
194 * Use: Finds a good place to store temporary files.
197 static char *goodtmp(void)
201 /* --- First of all, try the user's current choice --- */
203 if ((p = getenv("TMPDIR")) != 0 && fullcheck(p))
206 /* --- Try making a directory in `/tmp' --- */
208 if ((q = trytmp("/tmp", pw->pw_name)) != 0)
211 /* --- That failed: try a directory in the user's home --- */
213 if ((q = trytmp(pw->pw_dir, "tmp")) != 0)
216 /* --- Still no joy: give up --- *
218 * To be fair, if the user can't find a safe place in his own home
219 * directory then he's pretty stuffed.
225 /* --- @usage@ --- */
227 static void usage(FILE *fp)
228 { fprintf(fp, "Usage: %s [-bc] [-v PATH]\n", QUIS); }
230 /* --- @version@ --- */
232 static void version(FILE *fp)
233 { fprintf(fp, "%s version %s\n", QUIS, VERSION); }
237 static void help(FILE *fp)
243 Sets a suitable and secure temporary directory, or checks that a given\n\
244 directory is suitable for use with temporary files. A directory is\n\
245 considered good for a particular user if it's readable and writable only\n\
246 by that user, and if all its parents are modifiable only by the user or\n\
249 Options supported:\n\
251 -h, --help Display this help text.\n\
252 -V, --version Display the program's version number.\n\
253 -u, --usage Display a terse usage summary.\n\
255 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
256 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
257 -v, --verify PATH Check whether PATH is good, setting exit status.\n\
259 The default action is to examine the caller's shell and output a suitable\n\
260 setting for that shell type.\n\
267 * Arguments: @int argc@ = number of command line arguments
268 * @char *argv[]@ = the actual argument strings
270 * Returns: Zero if all went well, else nonzero.
272 * Use: Performs helpful operations on temporary directories.
275 int main(int argc, char *argv[])
286 /* --- Initialize variables --- */
290 cp.cp_what = CP_WRWORLD | CP_WRGRP | CP_WROTHUSR | CP_STICKYOK;
293 checkpath_setids(&cp);
296 die(1, "you don't exist");
298 /* --- Parse arguments --- */
301 static struct option opts[] = {
302 { "help", 0, 0, 'h' },
303 { "version", 0, 0, 'V' },
304 { "usage", 0, 0, 'u' },
305 { "bourne", 0, 0, 'b' },
306 { "cshell", 0, 0, 'c' },
307 { "verify", OPTF_ARGREQ, 0, 'v' },
310 int i = mdwopt(argc, argv, "hVu" "bcv:", opts, 0, 0, 0);
331 return (!fullcheck(optarg));
339 if (duff || optind != argc) {
344 /* --- Choose a shell --- */
348 if (!(p = getenv("SHELL")))
350 if (strstr(p, "csh"))
356 /* --- Start the checking --- */
361 die(1, "no good tmp directory");
364 printf("TMPDIR=\"%s\"; export TMPDIR\n", p);
367 printf("setenv TMPDIR \"%s\"\n", p);
375 /*----- That's all, folks -------------------------------------------------*/