X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Fmakedir.c;h=e87ae5741321870adf2ad74c73eb30e65162b4a4;hp=6cf9e03c29303f90aab20eb9612228cbd78c5760;hb=fc9279e818dfc96c63a5d75a89080cc68cfe1170;hpb=1691e80890e5d8ba258c51c214a3e91880e1db2b diff --git a/sbr/makedir.c b/sbr/makedir.c index 6cf9e03..e87ae57 100644 --- a/sbr/makedir.c +++ b/sbr/makedir.c @@ -1,79 +1,66 @@ - /* - * makedir.c -- make a directory - * - * $Id$ - */ +** makedir.c -- make a directory +** +** This code is Copyright (c) 2002, by the authors of nmh. See the +** COPYRIGHT file in the root directory of the nmh distribution for +** complete copyright information. +*/ /* - * Modified to try recursive create. - */ +** Modified to try recursive create. +*/ #include #include #include #include +#include -extern int errno; - int -makedir (char *dir) +makedir(char *dir) { - pid_t pid; - register char *cp; - register char *c; - char path[PATH_MAX]; + char path[PATH_MAX]; + char *cp; + int had_an_error = 0; + mode_t folder_perms, saved_umask; + char* c; - context_save(); /* save the context file */ - fflush(stdout); + context_save(); /* save the context file */ + fflush(stdout); - if (getuid () == geteuid ()) { - c = strncpy(path, dir, sizeof(path)); + if (!(cp = context_find("folder-protect")) || !*cp) { + cp = foldprot; + } + folder_perms = strtoul(cp, NULL, 8); - while ((c = strchr((c + 1), '/')) != NULL) { - *c = (char)0; - if (access(path, X_OK)) { - if (errno != ENOENT){ - advise (dir, "unable to create directory"); - return 0; - } - if (mkdir(path, 0775)) { - advise (dir, "unable to create directory"); - return 0; - } - } - *c = '/'; - } - - if (mkdir (dir, 0755) == -1) { - advise (dir, "unable to create directory"); - return 0; - } - } else { - switch (pid = vfork()) { - case -1: - advise ("fork", "unable to"); - return 0; + /* + ** Folders have definite desired permissions that are set -- we + ** don't want to interact with the umask. Clear it temporarily. + */ + saved_umask = umask(0); - case 0: - setgid (getgid ()); - setuid (getuid ()); + c = strncpy(path, dir, sizeof(path)); - execl ("/bin/mkdir", "mkdir", dir, NULL); - execl ("/usr/bin/mkdir", "mkdir", dir, NULL); - fprintf (stderr, "unable to exec "); - perror ("mkdir"); - _exit (-1); + while (!had_an_error && (c = strchr((c + 1), '/')) != NULL) { + *c = '\0'; + /* Create an outer directory. */ + if (mkdir(path, folder_perms) == -1 && + errno != EEXIST) { + advise(dir, "unable to create directory"); + had_an_error = 1; + } + *c = '/'; + } - default: - if (pidXwait(pid, "mkdir")) - return 0; - break; + /* + ** Create the innermost nested subdirectory of the + ** path we're being asked to create. + */ + if (!had_an_error && mkdir(dir, folder_perms)==-1) { + advise(dir, "unable to create directory"); + had_an_error = 1; } - } + umask(saved_umask); /* put the user's umask back */ - if (!(cp = context_find ("folder-protect"))) - cp = foldprot; - chmod (dir, atooi (cp)); - return 1; + return (had_an_error) ? 0 : 1; }