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 ------------------------------------------------------*/
36 #include <sys/types.h>
41 #include <mLib/alloc.h>
42 #include <mLib/dstr.h>
43 #include <mLib/mdwopt.h>
44 #include <mLib/quis.h>
45 #include <mLib/report.h>
47 #include "checkpath.h"
49 /*----- Static variables --------------------------------------------------*/
52 static struct checkpath cp
;
53 static struct passwd
*pw
;
55 /*----- Main code ---------------------------------------------------------*/
59 * Arguments: @const char *p@ = pathname to check
60 * @int *f@ = try-to-create flag
62 * Returns: Nonzero if the directory is OK
64 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
65 * and @*f@ is set, then try to create the directory.
68 static int ok(const char *p
, int *f
)
72 /* --- Read the directory status --- */
76 /* --- Maybe create it if it doesn't exist --- */
78 if (errno
!= ENOENT
|| !f
|| !*f
)
85 /* --- Now read the new status --- *
87 * This fixes a race condition between the previous @lstat@ call and
95 /* --- Make sure the directory is good --- *
97 * It must be a genuine directory (not a link); it must be readable
98 * and writable only by its owner, and that owner must be me.
101 if (S_ISDIR(st
.st_mode
) && (st
.st_mode
& 0777) == 0700 && st
.st_uid
== me
)
106 /* --- @trytmp@ --- *
108 * Arguments: @const char *parent@ = parent directory name
109 * @const char *base@ = subdirectory base name
111 * Returns: Pointer to directory name, or null. (String needs freeing.)
113 * Use: Tries to find or create an appropriate temporary directory.
116 static char *trytmp(const char *parent
, const char *base
)
118 static char c
[] = { "0123456789"
119 "abcdefghijklmnopqrstuvwxyz"
120 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
126 /* --- Make sure the parent directory is sane --- *
128 * Must make sure that all the lead-up to the temporary directory is safe.
129 * Otherwise other people can play with symlinks or rename directories
130 * after I've done all this careful work to make the endpoint directory
134 if (checkpath(parent
, &cp
))
137 /* --- See whether the trivial version will work --- */
139 dstr_putf(&d
, "%s/%s", parent
, base
);
140 if (ok(d
.buf
, &createflag
))
143 /* --- Now try with various suffixes --- */
149 for (p
= c
; *p
; p
++) {
152 if (ok(d
.buf
, &createflag
))
154 for (q
= c
; *q
; q
++) {
157 if (ok(d
.buf
, &createflag
))
173 /* --- @fullcheck@ --- *
175 * Arguments: @const char *p@ = pointer to path to check
177 * Returns: Zero if it's a bad directory.
179 * Use: Runs a thorough check on a directory.
182 static int fullcheck(const char *p
)
183 { return (checkpath(p
, &cp
) == 0 && ok(p
, 0)); }
185 /* --- @goodtmp@ --- *
189 * Returns: Pointer to a known-good secure temporary directory, or
192 * Use: Finds a good place to store temporary files.
195 static char *goodtmp(void)
199 /* --- First of all, try the user's current choice --- */
201 if ((p
= getenv("TMPDIR")) != 0 && fullcheck(p
))
204 /* --- Try making a directory in `/tmp' --- */
206 if ((q
= trytmp("/tmp", pw
->pw_name
)) != 0)
209 /* --- That failed: try a directory in the user's home --- */
211 if ((q
= trytmp(pw
->pw_dir
, "tmp")) != 0)
214 /* --- Still no joy: give up --- *
216 * To be fair, if the user can't find a safe place in his own home
217 * directory then he's pretty stuffed.
223 /* --- @usage@ --- */
225 static void usage(FILE *fp
)
226 { fprintf(fp
, "Usage: %s [-bc] [-v PATH]\n", QUIS
); }
228 /* --- @version@ --- */
230 static void version(FILE *fp
)
231 { fprintf(fp
, "%s version %s\n", QUIS
, VERSION
); }
235 static void help(FILE *fp
)
241 Sets a suitable and secure temporary directory, or checks that a given\n\
242 directory is suitable for use with temporary files. A directory is\n\
243 considered good for a particular user if it's readable and writable only\n\
244 by that user, and if all its parents are modifiable only by the user or\n\
247 Options supported:\n\
249 -h, --help Display this help text.\n\
250 -V, --version Display the program's version number.\n\
251 -u, --usage Display a terse usage summary.\n\
253 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
254 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
255 -v, --verify PATH Check whether PATH is good, setting exit status.\n\
257 The default action is to examine the caller's shell and output a suitable\n\
258 setting for that shell type.\n\
265 * Arguments: @int argc@ = number of command line arguments
266 * @char *argv[]@ = the actual argument strings
268 * Returns: Zero if all went well, else nonzero.
270 * Use: Performs helpful operations on temporary directories.
273 int main(int argc
, char *argv
[])
284 /* --- Initialize variables --- */
288 cp
.cp_what
= CP_WRWORLD
| CP_WRGRP
| CP_WROTHUSR
| CP_STICKYOK
;
291 checkpath_setids(&cp
);
294 die(1, "you don't exist");
296 /* --- Parse arguments --- */
299 static struct option opts
[] = {
300 { "help", 0, 0, 'h' },
301 { "version", 0, 0, 'V' },
302 { "usage", 0, 0, 'u' },
303 { "bourne", 0, 0, 'b' },
304 { "cshell", 0, 0, 'c' },
305 { "verify", OPTF_ARGREQ
, 0, 'v' },
308 int i
= mdwopt(argc
, argv
, "hVu" "bcv:", opts
, 0, 0, 0);
329 return (!fullcheck(optarg
));
337 if (duff
|| optind
!= argc
) {
342 /* --- Choose a shell --- */
346 if (!(p
= getenv("SHELL")))
348 if (strstr(p
, "csh"))
354 /* --- Start the checking --- */
359 die(1, "no good tmp directory");
362 printf("TMPDIR=\"%s\"; export TMPDIR\n", p
);
365 printf("setenv TMPDIR \"%s\"\n", p
);
373 /*----- That's all, folks -------------------------------------------------*/