xref: /openbsd/bin/mkdir/mkdir.c (revision 07222127)
1 /*	$OpenBSD: mkdir.c,v 1.26 2015/10/07 14:17:18 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 <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 extern char *__progname;
45 
46 int	mkpath(char *, mode_t, mode_t);
47 void	usage(void);
48 
49 int
50 main(int argc, char *argv[])
51 {
52 	int ch, rv, exitval, pflag;
53 	void *set;
54 	mode_t mode, dir_mode;
55 
56 	setlocale(LC_ALL, "");
57 
58 	/*
59 	 * The default file mode is a=rwx (0777) with selected permissions
60 	 * removed in accordance with the file mode creation mask.  For
61 	 * intermediate path name components, the mode is the default modified
62 	 * by u+wx so that the subdirectories can always be created.
63 	 */
64 	mode = 0777 & ~umask(0);
65 	dir_mode = mode | S_IWUSR | S_IXUSR;
66 
67 	pflag = 0;
68 	while ((ch = getopt(argc, argv, "m:p")) != -1)
69 		switch(ch) {
70 		case 'p':
71 			pflag = 1;
72 			break;
73 		case 'm':
74 			if ((set = setmode(optarg)) == NULL)
75 				errx(1, "invalid file mode: %s", optarg);
76 			mode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
77 			free(set);
78 			break;
79 		default:
80 			usage();
81 		}
82 	argc -= optind;
83 	argv += optind;
84 
85 	if (mode & (S_ISUID | S_ISGID | S_ISTXT) == 0) {
86 		if (tame("stdio cpath rpath fattr", NULL) == -1)
87 			err(1, "tame");
88 	}
89 
90 	if (*argv == NULL)
91 		usage();
92 
93 	for (exitval = 0; *argv != NULL; ++argv) {
94 		char *slash;
95 
96 		/* Remove trailing slashes, per POSIX. */
97 		slash = strrchr(*argv, '\0');
98 		while (--slash > *argv && *slash == '/')
99 			*slash = '\0';
100 
101 		if (pflag) {
102 			rv = mkpath(*argv, mode, dir_mode);
103 		} else {
104 			rv = mkdir(*argv, mode);
105 			/*
106 			 * The mkdir() and umask() calls both honor only the
107 			 * low nine bits, so if you try to set a mode including
108 			 * the sticky, setuid, setgid bits you lose them. Don't
109 			 * do this unless the user has specifically requested
110 			 * a mode as chmod will (obviously) ignore the umask.
111 			 */
112 			if (rv == 0 && mode > 0777)
113 				rv = chmod(*argv, mode);
114 		}
115 		if (rv < 0) {
116 			warn("%s", *argv);
117 			exitval = 1;
118 		}
119 	}
120 	exit(exitval);
121 }
122 
123 /*
124  * mkpath -- create directories.
125  *	path     - path
126  *	mode     - file mode of terminal directory
127  *	dir_mode - file mode of intermediate directories
128  */
129 int
130 mkpath(char *path, mode_t mode, mode_t dir_mode)
131 {
132 	struct stat sb;
133 	char *slash;
134 	int done;
135 
136 	slash = path;
137 
138 	for (;;) {
139 		slash += strspn(slash, "/");
140 		slash += strcspn(slash, "/");
141 
142 		done = (*slash == '\0');
143 		*slash = '\0';
144 
145 		if (mkdir(path, done ? mode : dir_mode) == 0) {
146 			if (mode > 0777 && chmod(path, mode) < 0)
147 				return (-1);
148 		} else {
149 			int mkdir_errno = errno;
150 
151 			if (stat(path, &sb)) {
152 				/* Not there; use mkdir()s errno */
153 				errno = mkdir_errno;
154 				return (-1);
155 			}
156 			if (!S_ISDIR(sb.st_mode)) {
157 				/* Is there, but isn't a directory */
158 				errno = ENOTDIR;
159 				return (-1);
160 			}
161 		}
162 
163 		if (done)
164 			break;
165 
166 		*slash = '/';
167 	}
168 
169 	return (0);
170 }
171 
172 void
173 usage(void)
174 {
175 	(void)fprintf(stderr, "usage: %s [-p] [-m mode] directory ...\n",
176 	    __progname);
177 	exit(1);
178 }
179