xref: /openbsd-src/bin/mkdir/mkdir.c (revision 3aaa63eb46949490a39db9c6d82aacc8ee5d8551)
1 /*	$OpenBSD: mkdir.c,v 1.31 2019/06/28 13:34:59 deraadt Exp $	*/
2 /*	$NetBSD: mkdir.c,v 1.14 1995/06/25 21:59:21 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 extern char *__progname;
44 
45 int	mkpath(char *, mode_t, mode_t);
46 static void __dead usage(void);
47 
48 int
main(int argc,char * argv[])49 main(int argc, char *argv[])
50 {
51 	int ch, rv, exitval, pflag;
52 	void *set;
53 	mode_t mode, dir_mode;
54 
55 	/*
56 	 * The default file mode is a=rwx (0777) with selected permissions
57 	 * removed in accordance with the file mode creation mask.  For
58 	 * intermediate path name components, the mode is the default modified
59 	 * by u+wx so that the subdirectories can always be created.
60 	 */
61 	mode = 0777 & ~umask(0);
62 	dir_mode = mode | S_IWUSR | S_IXUSR;
63 
64 	pflag = 0;
65 	while ((ch = getopt(argc, argv, "m:p")) != -1)
66 		switch(ch) {
67 		case 'p':
68 			pflag = 1;
69 			break;
70 		case 'm':
71 			if ((set = setmode(optarg)) == NULL)
72 				errx(1, "invalid file mode: %s", optarg);
73 			mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
74 			free(set);
75 			break;
76 		default:
77 			usage();
78 		}
79 	argc -= optind;
80 	argv += optind;
81 
82 	if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) == 0) {
83 		if (pledge("stdio rpath cpath fattr", NULL) == -1)
84 			err(1, "pledge");
85 	}
86 
87 	if (*argv == NULL)
88 		usage();
89 
90 	for (exitval = 0; *argv != NULL; ++argv) {
91 		char *slash;
92 
93 		/* Remove trailing slashes, per POSIX. */
94 		slash = strrchr(*argv, '\0');
95 		while (--slash > *argv && *slash == '/')
96 			*slash = '\0';
97 
98 		if (pflag) {
99 			rv = mkpath(*argv, mode, dir_mode);
100 		} else {
101 			rv = mkdir(*argv, mode);
102 			/*
103 			 * The mkdir() and umask() calls both honor only the
104 			 * low nine bits, so if you try to set a mode including
105 			 * the sticky, setuid, setgid bits you lose them. Don't
106 			 * do this unless the user has specifically requested
107 			 * a mode as chmod will (obviously) ignore the umask.
108 			 */
109 			if (rv == 0 && mode > 0777)
110 				rv = chmod(*argv, mode);
111 		}
112 		if (rv == -1) {
113 			warn("%s", *argv);
114 			exitval = 1;
115 		}
116 	}
117 	return exitval;
118 }
119 
120 /*
121  * mkpath -- create directories.
122  *	path     - path
123  *	mode     - file mode of terminal directory
124  *	dir_mode - file mode of intermediate directories
125  */
126 int
mkpath(char * path,mode_t mode,mode_t dir_mode)127 mkpath(char *path, mode_t mode, mode_t dir_mode)
128 {
129 	struct stat sb;
130 	char *slash;
131 	int done;
132 
133 	slash = path;
134 
135 	for (;;) {
136 		slash += strspn(slash, "/");
137 		slash += strcspn(slash, "/");
138 
139 		done = (*slash == '\0');
140 		*slash = '\0';
141 
142 		if (mkdir(path, done ? mode : dir_mode) == 0) {
143 			if (mode > 0777 && chmod(path, mode) == -1)
144 				return (-1);
145 		} else {
146 			int mkdir_errno = errno;
147 
148 			if (stat(path, &sb) == -1) {
149 				/* Not there; use mkdir()s errno */
150 				errno = mkdir_errno;
151 				return (-1);
152 			}
153 			if (!S_ISDIR(sb.st_mode)) {
154 				/* Is there, but isn't a directory */
155 				errno = ENOTDIR;
156 				return (-1);
157 			}
158 		}
159 
160 		if (done)
161 			break;
162 
163 		*slash = '/';
164 	}
165 
166 	return (0);
167 }
168 
169 static void __dead
usage(void)170 usage(void)
171 {
172 	(void)fprintf(stderr, "usage: %s [-p] [-m mode] directory ...\n",
173 	    __progname);
174 	exit(1);
175 }
176