3 * $Id: tmpdir.c,v 1.1 1999/04/06 20:12:07 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.1 1999/04/06 20:12:07 mdw
37 /*----- Header files ------------------------------------------------------*/
44 #include <sys/types.h>
49 #include <mLib/alloc.h>
50 #include <mLib/dstr.h>
51 #include <mLib/mdwopt.h>
52 #include <mLib/quis.h>
53 #include <mLib/report.h>
57 /*----- Static variables --------------------------------------------------*/
60 static struct chkpath cp
;
61 static struct passwd
*pw
;
63 /*----- Main code ---------------------------------------------------------*/
67 * Arguments: @const char *p@ = pathname to check
68 * @int *f@ = try-to-create flag
70 * Returns: Nonzero if the directory is OK
72 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
73 * and @*f@ is set, then try to create the directory.
76 static int ok(const char *p
, int *f
)
80 /* --- Read the directory status --- */
84 /* --- Maybe create it if it doesn't exist --- */
86 if (errno
!= ENOENT
|| !f
|| !*f
)
93 /* --- Now read the new status --- *
95 * This fixes a race condition between the previous @lstat@ call and
103 /* --- Make sure the directory is good --- *
105 * It must be a genuine directory (not a link); it must be readable
106 * and writable only by its owner, and that owner must be me.
109 if (S_ISDIR(st
.st_mode
) && (st
.st_mode
& 0777) == 0700 && st
.st_uid
== me
)
114 /* --- @trytmp@ --- *
116 * Arguments: @const char *parent@ = parent directory name
117 * @const char *base@ = subdirectory base name
119 * Returns: Pointer to directory name, or null. (String needs freeing.)
121 * Use: Tries to find or create an appropriate temporary directory.
124 static char *trytmp(const char *parent
, const char *base
)
126 static char c
[] = { "0123456789"
127 "abcdefghijklmnopqrstuvwxyz"
128 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
134 /* --- Make sure the parent directory is sane --- *
136 * Must make sure that all the lead-up to the temporary directory is safe.
137 * Otherwise other people can play with symlinks or rename directories
138 * after I've done all this careful work to make the endpoint directory
142 if (path_check(parent
, &cp
))
145 /* --- See whether the trivial version will work --- */
148 dstr_putf(&d
, "%s/%s", parent
, base
);
149 if (ok(d
.buf
, &createflag
))
152 /* --- Now try with various suffixes --- */
158 for (p
= c
; *p
; p
++) {
161 if (ok(d
.buf
, &createflag
))
163 for (q
= c
; *q
; q
++) {
166 if (ok(d
.buf
, &createflag
))
182 /* --- @fullcheck@ --- *
184 * Arguments: @const char *p@ = pointer to path to check
186 * Returns: Zero if it's a bad directory.
188 * Use: Runs a thorough check on a directory.
191 static int fullcheck(const char *p
)
193 return (path_check(p
, &cp
) == 0 && ok(p
, 0));
196 /* --- @goodtmp@ --- *
200 * Returns: Pointer to a known-good secure temporary directory, or
203 * Use: Finds a good place to store temporary files.
206 static char *goodtmp(void)
210 /* --- First of all, try the user's current choice --- */
212 if ((p
= getenv("TMPDIR")) != 0 && fullcheck(p
))
215 /* --- Try making a directory in `/tmp' --- */
217 if (!(q
= getenv("USER")) && !(q
= getenv("LOGNAME")))
219 if ((q
= trytmp("/tmp", q
)) != 0)
222 /* --- That failed: try a directory in the user's home --- */
224 if (!(q
= getenv("HOME")))
226 if ((q
= trytmp(q
, "tmp")) != 0)
229 /* --- Still no joy: give up --- *
231 * To be fair, if the user can't find a safe place in his own home
232 * directory then he's pretty stuffed.
238 /* --- @usage@ --- */
240 static void usage(FILE *fp
)
242 fprintf(fp
, "Usage: %s [-bc] [-v PATH]\n", QUIS
);
245 /* --- @version@ --- */
247 static void version(FILE *fp
)
249 fprintf(fp
, "%s version %s\n", QUIS
, VERSION
);
254 static void help(FILE *fp
)
260 Sets a suitable and secure temporary directory, or checks that a given\n\
261 directory is suitable for use with temporary files. A directory is\n\
262 considered good for a particular user if it's readable and writable only\n\
263 by that user, and if all its parents are modifiable only by the user or\n\
266 Options supported:\n\
268 -h, --help Display this help text.\n\
269 -V, --version Display the program's version number.\n\
270 -u, --usage Display a terse usage summary.\n\
272 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
273 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
274 -v, --verify PATH Check whether PATH is good, setting exit status.\n\
276 The default action is to examine the caller's shell and output a suitable\n\
277 setting for that shell type.\n\
284 * Arguments: @int argc@ = number of command line arguments
285 * @char *argv[]@ = the actual argument strings
287 * Returns: Zero if all went well, else nonzero.
289 * Use: Performs helpful operations on temporary directories.
292 int main(int argc
, char *argv
[])
303 /* --- Initialize variables --- */
307 cp
.cp_what
= CP_WRWORLD
| CP_WRGRP
| CP_WROTHUSR
| CP_STICKYOK
;
313 die(1, "you don't exist");
315 /* --- Parse arguments --- */
318 static struct option opts
[] = {
319 { "help", 0, 0, 'h' },
320 { "version", 0, 0, 'V' },
321 { "usage", 0, 0, 'u' },
322 { "bourne", 0, 0, 'b' },
323 { "cshell", 0, 0, 'c' },
324 { "verify", gFlag_argReq
, 0, 'v' },
327 int i
= mdwopt(argc
, argv
, "hVu bcv:", opts
, 0, 0, 0);
348 return (!fullcheck(optarg
));
356 if (duff
|| optind
!= argc
) {
361 /* --- Choose a shell --- */
365 if (!(p
= getenv("SHELL")))
367 if (strstr(p
, "csh"))
373 /* --- Start the checking --- */
378 die(1, "no good tmp directory");
381 printf("TMPDIR=\"%s\"; export TMPDIR\n", p
);
384 printf("setenv TMPDIR \"%s\"\n", p
);
392 /*----- That's all, folks -------------------------------------------------*/