8e704e67e90b6cc283ae82d0df0f4ca2039cb960
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 ------------------------------------------------------*/
34 #include <sys/types.h>
39 #include <mLib/alloc.h>
40 #include <mLib/dstr.h>
41 #include <mLib/macros.h>
42 #include <mLib/mdwopt.h>
43 #include <mLib/quis.h>
44 #include <mLib/report.h>
46 #include "checkpath.h"
48 /*----- Static variables --------------------------------------------------*/
51 static struct checkpath cp
;
52 static struct passwd
*pw
;
54 /*----- Main code ---------------------------------------------------------*/
58 * Arguments: @const char *p@ = pathname to check
59 * @int *f@ = try-to-create flag
61 * Returns: Nonzero if the directory is OK
63 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
64 * and @*f@ is set, then try to create the directory.
67 static int ok(const char *p
, int *f
)
71 /* --- Read the directory status --- */
75 /* --- Maybe create it if it doesn't exist --- */
77 if (errno
!= ENOENT
|| !f
|| !*f
)
84 /* --- Now read the new status --- *
86 * This fixes a race condition between the previous @lstat@ call and
94 /* --- Make sure the directory is good --- *
96 * It must be a genuine directory (not a link); it must be readable
97 * and writable only by its owner, and that owner must be me.
100 if (S_ISDIR(st
.st_mode
) && (st
.st_mode
& 0777) == 0700 && st
.st_uid
== me
)
105 /* --- @trytmp@ --- *
107 * Arguments: @const char *parent@ = parent directory name
108 * @const char *base@ = subdirectory base name
110 * Returns: Pointer to directory name, or null. (String needs freeing.)
112 * Use: Tries to find or create an appropriate temporary directory.
115 static char *trytmp(const char *parent
, const char *base
)
117 static char c
[] = { "0123456789"
118 "abcdefghijklmnopqrstuvwxyz"
119 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
125 /* --- Make sure the parent directory is sane --- *
127 * Must make sure that all the lead-up to the temporary directory is safe.
128 * Otherwise other people can play with symlinks or rename directories
129 * after I've done all this careful work to make the endpoint directory
133 if (checkpath(parent
, &cp
))
136 /* --- See whether the trivial version will work --- */
138 dstr_putf(&d
, "%s/%s", parent
, base
);
139 if (ok(d
.buf
, &createflag
))
142 /* --- Now try with various suffixes --- */
148 for (p
= c
; *p
; p
++) {
151 if (ok(d
.buf
, &createflag
))
153 for (q
= c
; *q
; q
++) {
156 if (ok(d
.buf
, &createflag
))
172 /* --- @fullcheck@ --- *
174 * Arguments: @const char *p@ = pointer to path to check
176 * Returns: Zero if it's a bad directory.
178 * Use: Runs a thorough check on a directory.
181 static int fullcheck(const char *p
)
183 return (checkpath(p
, &cp
) == 0 && ok(p
, 0));
186 /* --- @goodtmp@ --- *
190 * Returns: Pointer to a known-good secure temporary directory, or
193 * Use: Finds a good place to store temporary files.
196 static char *goodtmp(void)
200 /* --- First of all, try the user's current choice --- */
202 if ((p
= getenv("TMPDIR")) != 0 && fullcheck(p
))
205 /* --- Try making a directory in `/tmp' --- */
207 if (!(q
= getenv("USER")) && !(q
= getenv("LOGNAME")))
209 if ((q
= trytmp("/tmp", q
)) != 0)
212 /* --- That failed: try a directory in the user's home --- */
214 if (!(q
= getenv("HOME")))
216 if ((q
= trytmp(q
, "tmp")) != 0)
219 /* --- Still no joy: give up --- *
221 * To be fair, if the user can't find a safe place in his own home
222 * directory then he's pretty stuffed.
228 /* --- @report@ --- */
230 static void report(unsigned what
, int verbose
,
231 const char *p
, const char *msg
,
235 /* --- @usage@ --- */
237 static void usage(FILE *fp
)
238 { fprintf(fp
, "Usage: %s [-bc] [-v PATH]\n", QUIS
); }
240 /* --- @version@ --- */
242 static void version(FILE *fp
)
243 { fprintf(fp
, "%s version %s\n", QUIS
, VERSION
); }
247 static void help(FILE *fp
)
253 Sets a suitable and secure temporary directory, or checks that a given\n\
254 directory is suitable for use with temporary files. A directory is\n\
255 considered good for a particular user if it's readable and writable only\n\
256 by that user, and if all its parents are modifiable only by the user or\n\
259 Options supported:\n\
261 -h, --help Display this help text.\n\
262 -V, --version Display the program's version number.\n\
263 -u, --usage Display a terse usage summary.\n\
265 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
266 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
267 -v, --verbose Report problems to standard error.\n\
268 -C, --check PATH Check whether PATH is good, setting exit status.\n\
270 The default action is to examine the caller's shell and output a suitable\n\
271 setting for that shell type.\n\
278 * Arguments: @int argc@ = number of command line arguments
279 * @char *argv[]@ = the actual argument strings
281 * Returns: Zero if all went well, else nonzero.
283 * Use: Performs helpful operations on temporary directories.
286 int main(int argc
, char *argv
[])
298 /* --- Initialize variables --- */
302 cp
.cp_what
= CP_WRWORLD
| CP_WRGRP
| CP_WROTHUSR
| CP_STICKYOK
| CP_REPORT
;
304 cp
.cp_report
= report
;
305 checkpath_setids(&cp
);
308 die(1, "you don't exist");
310 /* --- Parse arguments --- */
313 static struct option opts
[] = {
314 { "help", 0, 0, 'h' },
315 { "version", 0, 0, 'V' },
316 { "usage", 0, 0, 'u' },
317 { "bourne", 0, 0, 'b' },
318 { "cshell", 0, 0, 'c' },
319 { "check", OPTF_ARGREQ
, 0, 'C' },
320 { "verify", OPTF_ARGREQ
, 0, 'C' },
321 { "verbose", 0, 0, 'v' },
324 int i
= mdwopt(argc
, argv
, "hVu bcvc:", opts
, 0, 0, 0);
345 return (!fullcheck(optarg
));
356 if (duff
|| optind
!= argc
) {
361 /* --- Choose a shell --- */
364 if (!(p
= getenv("SHELL")))
366 if (strstr(p
, "csh"))
372 /* --- Start the checking --- */
374 if ((p
= goodtmp()) == 0)
375 die(1, "no good tmp directory");
378 printf("TMPDIR=\"%s\"; export TMPDIR\n", p
);
381 printf("setenv TMPDIR \"%s\"\n", p
);
388 /*----- That's all, folks -------------------------------------------------*/