Merge branch 'hs/taintwarn'
[exim.git] / src / src / directory.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* Copyright (c) The Exim Maintainers 2010 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9 #include "exim.h"
10
11
12 /*************************************************
13 *           Attempt to create a directory        *
14 *************************************************/
15
16 /* All the directories that Exim ever creates for itself are within the spool
17 directory as defined by spool_directory. We are prepared to create as many as
18 necessary from that directory downwards, inclusive. However, directory creation
19 can also be required in appendfile and sieve filters. The making function
20 therefore has a parent argument, below which the new directories are to go. It
21 can be NULL if the name is absolute.
22
23 If a non-root uid has been specified for exim, and we are currently running as
24 root, ensure the directory is owned by the non-root id if the parent is the
25 spool directory.
26
27 Arguments:
28   parent    parent directory name; if NULL the name must be absolute
29   name      directory name within the parent that we want
30   mode      mode for the new directory
31   panic     if TRUE, panic on failure
32
33 Returns:    panic on failure if panic is set; otherwise return FALSE;
34             TRUE on success.
35 */
36
37 BOOL
38 directory_make(const uschar *parent, const uschar *name,
39                int mode, BOOL panic)
40 {
41 BOOL use_chown = parent == spool_directory && geteuid() == root_uid;
42 uschar * p;
43 uschar c = 1;
44 struct stat statbuf;
45 uschar * path;
46
47 if (is_tainted2(name, LOG_MAIN|LOG_PANIC, "Tainted path '%s' for new directory", name))
48   { p = US"create"; path = US name; errno = EACCES; goto bad; }
49
50 if (parent)
51   {
52   path = string_sprintf("%s%s%s", parent, US"/", name);
53   p = path + Ustrlen(parent);
54   }
55 else
56   {
57   path = string_copy(name);
58   p = path + 1;
59   }
60
61 /* Walk the path creating any missing directories */
62
63 while (c && *p)
64   {
65   while (*p && *p != '/') p++;
66   c = *p;
67   *p = '\0';
68   if (Ustat(path, &statbuf) != 0)
69     {
70     if (mkdir(CS path, mode) < 0 && errno != EEXIST)
71       { p = US"create"; goto bad; }
72
73     /* Set the ownership if necessary. */
74
75     if (use_chown && exim_chown(path, exim_uid, exim_gid))
76       { p = US"set owner on"; goto bad; }
77
78     /* It appears that any mode bits greater than 0777 are ignored by
79     mkdir(), at least on some operating systems. Therefore, if the mode
80     contains any such bits, do an explicit mode setting. */
81
82     if (mode & 0777000) (void) Uchmod(path, mode);
83     }
84   *p++ = c;
85   }
86
87 return TRUE;
88
89 bad:
90   if (panic) log_write(0, LOG_MAIN|LOG_PANIC_DIE,
91     "Failed to %s directory \"%s\": %s\n", p, path, exim_errstr(errno));
92   return FALSE;
93 }
94
95 /* End of directory.c */