3 * $Id: tmpdir.c,v 1.4 2003/01/25 23:58:44 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.4 2003/01/25 23:58:44 mdw
33 * Make guts into official library.
35 * Revision 1.3 1999/05/21 22:07:20 mdw
36 * Take advantage of new dynamic string macros.
38 * Revision 1.2 1999/05/19 20:37:28 mdw
39 * Track gratuitous change in mdwopt interface.
41 * Revision 1.1.1.1 1999/04/06 20:12:07 mdw
46 /*----- Header files ------------------------------------------------------*/
53 #include <sys/types.h>
58 #include <mLib/alloc.h>
59 #include <mLib/dstr.h>
60 #include <mLib/mdwopt.h>
61 #include <mLib/quis.h>
62 #include <mLib/report.h>
64 #include "checkpath.h"
66 /*----- Static variables --------------------------------------------------*/
69 static struct checkpath cp
;
70 static struct passwd
*pw
;
72 /*----- Main code ---------------------------------------------------------*/
76 * Arguments: @const char *p@ = pathname to check
77 * @int *f@ = try-to-create flag
79 * Returns: Nonzero if the directory is OK
81 * Use: Ensures that a directory is OK. If @f@ is a real pointer,
82 * and @*f@ is set, then try to create the directory.
85 static int ok(const char *p
, int *f
)
89 /* --- Read the directory status --- */
93 /* --- Maybe create it if it doesn't exist --- */
95 if (errno
!= ENOENT
|| !f
|| !*f
)
102 /* --- Now read the new status --- *
104 * This fixes a race condition between the previous @lstat@ call and
112 /* --- Make sure the directory is good --- *
114 * It must be a genuine directory (not a link); it must be readable
115 * and writable only by its owner, and that owner must be me.
118 if (S_ISDIR(st
.st_mode
) && (st
.st_mode
& 0777) == 0700 && st
.st_uid
== me
)
123 /* --- @trytmp@ --- *
125 * Arguments: @const char *parent@ = parent directory name
126 * @const char *base@ = subdirectory base name
128 * Returns: Pointer to directory name, or null. (String needs freeing.)
130 * Use: Tries to find or create an appropriate temporary directory.
133 static char *trytmp(const char *parent
, const char *base
)
135 static char c
[] = { "0123456789"
136 "abcdefghijklmnopqrstuvwxyz"
137 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
143 /* --- Make sure the parent directory is sane --- *
145 * Must make sure that all the lead-up to the temporary directory is safe.
146 * Otherwise other people can play with symlinks or rename directories
147 * after I've done all this careful work to make the endpoint directory
151 if (checkpath(parent
, &cp
))
154 /* --- See whether the trivial version will work --- */
156 dstr_putf(&d
, "%s/%s", parent
, base
);
157 if (ok(d
.buf
, &createflag
))
160 /* --- Now try with various suffixes --- */
166 for (p
= c
; *p
; p
++) {
169 if (ok(d
.buf
, &createflag
))
171 for (q
= c
; *q
; q
++) {
174 if (ok(d
.buf
, &createflag
))
190 /* --- @fullcheck@ --- *
192 * Arguments: @const char *p@ = pointer to path to check
194 * Returns: Zero if it's a bad directory.
196 * Use: Runs a thorough check on a directory.
199 static int fullcheck(const char *p
)
201 return (checkpath(p
, &cp
) == 0 && ok(p
, 0));
204 /* --- @goodtmp@ --- *
208 * Returns: Pointer to a known-good secure temporary directory, or
211 * Use: Finds a good place to store temporary files.
214 static char *goodtmp(void)
218 /* --- First of all, try the user's current choice --- */
220 if ((p
= getenv("TMPDIR")) != 0 && fullcheck(p
))
223 /* --- Try making a directory in `/tmp' --- */
225 if (!(q
= getenv("USER")) && !(q
= getenv("LOGNAME")))
227 if ((q
= trytmp("/tmp", q
)) != 0)
230 /* --- That failed: try a directory in the user's home --- */
232 if (!(q
= getenv("HOME")))
234 if ((q
= trytmp(q
, "tmp")) != 0)
237 /* --- Still no joy: give up --- *
239 * To be fair, if the user can't find a safe place in his own home
240 * directory then he's pretty stuffed.
246 /* --- @usage@ --- */
248 static void usage(FILE *fp
)
250 fprintf(fp
, "Usage: %s [-bc] [-v PATH]\n", QUIS
);
253 /* --- @version@ --- */
255 static void version(FILE *fp
)
257 fprintf(fp
, "%s version %s\n", QUIS
, VERSION
);
262 static void help(FILE *fp
)
268 Sets a suitable and secure temporary directory, or checks that a given\n\
269 directory is suitable for use with temporary files. A directory is\n\
270 considered good for a particular user if it's readable and writable only\n\
271 by that user, and if all its parents are modifiable only by the user or\n\
274 Options supported:\n\
276 -h, --help Display this help text.\n\
277 -V, --version Display the program's version number.\n\
278 -u, --usage Display a terse usage summary.\n\
280 -b, --bourne Output a `TMPDIR' setting for Bourne shell users.\n\
281 -c, --cshell Output a `TMPDIR' setting for C shell users.\n\
282 -v, --verify PATH Check whether PATH is good, setting exit status.\n\
284 The default action is to examine the caller's shell and output a suitable\n\
285 setting for that shell type.\n\
292 * Arguments: @int argc@ = number of command line arguments
293 * @char *argv[]@ = the actual argument strings
295 * Returns: Zero if all went well, else nonzero.
297 * Use: Performs helpful operations on temporary directories.
300 int main(int argc
, char *argv
[])
311 /* --- Initialize variables --- */
315 cp
.cp_what
= CP_WRWORLD
| CP_WRGRP
| CP_WROTHUSR
| CP_STICKYOK
;
318 checkpath_setids(&cp
);
321 die(1, "you don't exist");
323 /* --- Parse arguments --- */
326 static struct option opts
[] = {
327 { "help", 0, 0, 'h' },
328 { "version", 0, 0, 'V' },
329 { "usage", 0, 0, 'u' },
330 { "bourne", 0, 0, 'b' },
331 { "cshell", 0, 0, 'c' },
332 { "verify", OPTF_ARGREQ
, 0, 'v' },
335 int i
= mdwopt(argc
, argv
, "hVu bcv:", opts
, 0, 0, 0);
356 return (!fullcheck(optarg
));
364 if (duff
|| optind
!= argc
) {
369 /* --- Choose a shell --- */
373 if (!(p
= getenv("SHELL")))
375 if (strstr(p
, "csh"))
381 /* --- Start the checking --- */
386 die(1, "no good tmp directory");
389 printf("TMPDIR=\"%s\"; export TMPDIR\n", p
);
392 printf("setenv TMPDIR \"%s\"\n", p
);
400 /*----- That's all, folks -------------------------------------------------*/