1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
11 /*************************************************
12 * Attempt to create a directory *
13 *************************************************/
15 /* All the directories that Exim ever creates for itself are within the spool
16 directory as defined by spool_directory. We are prepared to create as many as
17 necessary from that directory downwards, inclusive. However, directory creation
18 can also be required in appendfile and sieve filters. The making function
19 therefore has a parent argument, below which the new directories are to go. It
20 can be NULL if the name is absolute.
22 If a non-root uid has been specified for exim, and we are currently running as
23 root, ensure the directory is owned by the non-root id if the parent is the
27 parent parent directory name; if NULL the name must be absolute
28 name directory name within the parent that we want
29 mode mode for the new directory
30 panic if TRUE, panic on failure
32 Returns: panic on failure if panic is set; otherwise return FALSE;
37 directory_make(const uschar *parent, const uschar *name,
40 BOOL use_chown = parent == spool_directory && geteuid() == root_uid;
50 slash = parent = CUS"";
54 p = buffer + Ustrlen(parent);
58 if (!string_format(buffer, sizeof(buffer), "%s%s%s", parent, slash, name))
59 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "name too long in directory_make");
61 while (c != 0 && *p != 0)
63 while (*p != 0 && *p != '/') p++;
66 if (Ustat(buffer, &statbuf) != 0)
68 if (mkdir(CS buffer, mode) < 0 && errno != EEXIST)
70 if (!panic) return FALSE;
71 log_write(0, LOG_MAIN|LOG_PANIC_DIE,
72 "Failed to create directory \"%s\": %s\n", buffer, strerror(errno));
75 /* Set the ownership if necessary. */
77 if (use_chown) (void)Uchown(buffer, exim_uid, exim_gid);
79 /* It appears that any mode bits greater than 0777 are ignored by
80 mkdir(), at least on some operating systems. Therefore, if the mode
81 contains any such bits, do an explicit mode setting. */
83 if ((mode & 0777000) != 0) (void)Uchmod(buffer, mode);
91 /* End of directory.c */