3 * Choose and check temporary directories
5 * (c) 1999 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of chkpath.
12 * chkpath is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * chkpath is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with chkpath; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
43 #include <mLib/alloc.h>
44 #include <mLib/dstr.h>
45 #include <mLib/macros.h>
46 #include <mLib/mdwopt.h>
47 #include <mLib/quis.h>
48 #include <mLib/report.h>
50 #include "checkpath.h"
53 /*----- Static variables --------------------------------------------------*/
56 static struct checkpath cp;
57 static struct passwd *pw;
59 /*----- Main code ---------------------------------------------------------*/
63 * Arguments: @const char *p@ = pathname to check
64 * @int *f@ = try-to-create flag
66 * Returns: Nonzero if the directory is OK
68 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
69 * and @*f@ is set, then try to create the directory.
72 static void complain(const char *p, const char *msg, int err)
76 if (!cp.cp_verbose) return;
77 dstr_putf(&d, "Path: %s: %s", p, msg);
78 if (err) dstr_putf(&d, ": %s", strerror(err));
83 static int ok(const char *p, int *f)
87 /* --- Read the directory status --- */
91 /* --- Maybe create it if it doesn't exist --- */
93 if (errno != ENOENT || !f || !*f) {
94 complain(p, "can't stat", errno);
98 complain(p, "can't create", errno);
103 /* --- Now read the new status --- *
105 * This fixes a race condition between the previous @lstat@ call and
110 complain(p, "can't stat after creating", errno);
115 /* --- Make sure the directory is good --- *
117 * It must be a genuine directory (not a link); it must be readable
118 * and writable only by its owner, and that owner must be me.
121 if (!S_ISDIR(st.st_mode))
122 complain(p, "not a directory", 0);
123 else if (st.st_uid != me)
124 complain(p, "not owner", 0);
125 else if ((st.st_mode & 0777) != 0700)
126 complain(p, "non-owner access permitted", 0);
132 /* --- @trytmp@ --- *
134 * Arguments: @const char *parent@ = parent directory name
135 * @const char *base@ = subdirectory base name
137 * Returns: Pointer to directory name, or null. (String needs freeing.)
139 * Use: Tries to find or create an appropriate temporary directory.
142 static char *trytmp(const char *parent, const char *base)
144 static char c[] = { "0123456789"
145 "abcdefghijklmnopqrstuvwxyz"
146 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
152 /* --- Make sure the parent directory is sane --- *
154 * Must make sure that all the lead-up to the temporary directory is safe.
155 * Otherwise other people can play with symlinks or rename directories
156 * after I've done all this careful work to make the endpoint directory
160 if (checkpath(parent, &cp))
163 /* --- See whether the trivial version will work --- */
165 dstr_putf(&d, "%s/%s", parent, base);
166 if (ok(d.buf, &createflag))
169 /* --- Now try with various suffixes --- */
175 for (p = c; *p; p++) {
178 if (ok(d.buf, &createflag))
180 for (q = c; *q; q++) {
183 if (ok(d.buf, &createflag))
199 /* --- @fullcheck@ --- *
201 * Arguments: @const char *p@ = pointer to path to check
203 * Returns: Zero if it's a bad directory.
205 * Use: Runs a thorough check on a directory.
208 static int fullcheck(const char *p)
209 { return (checkpath(p, &cp) == 0 && ok(p, 0)); }
211 /* --- @goodtmp@ --- *
215 * Returns: Pointer to a known-good secure temporary directory, or
218 * Use: Finds a good place to store temporary files.
221 static char *goodtmp(void)
225 /* --- First of all, try the user's current choice --- */
227 if ((p = getenv("TMPDIR")) != 0 && fullcheck(p))
230 /* --- Try making a directory in `/tmp' --- */
232 if ((q = trytmp("/tmp", pw->pw_name)) != 0)
235 /* --- That failed: try a directory in the user's home --- */
237 if ((q = trytmp(pw->pw_dir, "tmp")) != 0)
240 /* --- Still no joy: give up --- *
242 * To be fair, if the user can't find a safe place in his own home
243 * directory then he's pretty stuffed.
249 /* --- @report@ --- */
251 static void report(unsigned what, int verbose,
252 const char *p, const char *msg,
256 /* --- @usage@ --- */
258 static void usage(FILE *fp)
259 { fprintf(fp, "Usage: %s [-bcv] [-g NAME] [-C PATH]\n", QUIS); }
261 /* --- @version@ --- */
263 static void version(FILE *fp)
264 { fprintf(fp, "%s version %s\n", QUIS, VERSION); }
268 static void help(FILE *fp)
274 Sets a suitable and secure temporary directory, or checks that a given\n\
275 directory is suitable for use with temporary files. A directory is\n\
276 considered good for a particular user if it's readable and writable only\n\
277 by that user, and if all its parents are modifiable only by the user or\n\
280 Options supported:\n\
282 -h, --help Display this help text.\n\
283 -V, --version Display the program's version number.\n\
284 -u, --usage Display a terse usage summary.\n\
286 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
287 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
288 -v, --verbose Report problems to standard error.\n\
289 -g, --group NAME Trust group NAME to be honest and true.\n\
290 -C, --check PATH Check whether PATH is good, setting exit status.\n\
292 The default action is to examine the caller's shell and output a suitable\n\
293 setting for that shell type.\n\
300 * Arguments: @int argc@ = number of command line arguments
301 * @char *argv[]@ = the actual argument strings
303 * Returns: Zero if all went well, else nonzero.
305 * Use: Performs helpful operations on temporary directories.
308 int main(int argc, char *argv[])
320 /* --- Initialize variables --- */
323 me = cp.cp_uid = geteuid();
324 cp.cp_what = (CP_WRWORLD | CP_WROTHGRP | CP_WROTHUSR |
325 CP_STICKYOK | CP_REPORT | CP_ERROR);
327 cp.cp_report = report;
328 cp.cp_gids = 0; /* ignore group membership */
331 die(1, "you don't exist");
333 /* --- Parse arguments --- */
336 static struct option opts[] = {
337 { "help", 0, 0, 'h' },
338 { "version", 0, 0, 'V' },
339 { "usage", 0, 0, 'u' },
340 { "bourne", 0, 0, 'b' },
341 { "cshell", 0, 0, 'c' },
342 { "check", OPTF_ARGREQ, 0, 'C' },
343 { "verify", OPTF_ARGREQ, 0, 'C' },
344 { "verbose", 0, 0, 'v' },
345 { "trust-groups", 0, 0, 't' },
346 { "group", OPTF_ARGREQ, 0, 'g' },
349 int i = mdwopt(argc, argv, "hVu" "bcvtg:C:", opts, 0, 0, 0);
370 return (!fullcheck(optarg));
373 allowgroup(&cp, optarg);
384 if (duff || optind != argc) {
389 /* --- Choose a shell --- */
392 if (!(p = getenv("SHELL")))
394 if (strstr(p, "csh"))
400 /* --- Start the checking --- */
402 if ((p = goodtmp()) == 0)
403 die(1, "no good tmp directory");
406 printf("TMPDIR=\"%s\"; export TMPDIR\n", p);
409 printf("setenv TMPDIR \"%s\"\n", p);
416 /*----- That's all, folks -------------------------------------------------*/