mknod.c revision 1.31 1 /* $NetBSD: mknod.c,v 1.31 2004/05/11 17:09:58 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #if HAVE_NBTOOL_CONFIG_H
40 #include "nbtool_config.h"
41 #endif
42
43 #include <sys/cdefs.h>
44 #ifndef lint
45 __COPYRIGHT("@(#) Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.\n");
46 __RCSID("$NetBSD: mknod.c,v 1.31 2004/05/11 17:09:58 christos Exp $");
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/param.h>
52
53 #include <err.h>
54 #include <errno.h>
55 #include <limits.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <pwd.h>
60 #include <grp.h>
61 #include <string.h>
62 #include <ctype.h>
63
64 #include "pack_dev.h"
65
66 static int gid_name(const char *, gid_t *);
67 static portdev_t callPack(pack_t *, int, u_long *);
68
69 int main(int, char *[]);
70 static void usage(void);
71
72 #define MAXARGS 3 /* 3 for bsdos, 2 for rest */
73
74 int
75 main(int argc, char **argv)
76 {
77 char *name, *p;
78 mode_t mode;
79 portdev_t dev;
80 pack_t *pack;
81 u_long numbers[MAXARGS];
82 int n, ch, fifo, hasformat;
83 int r_flag = 0; /* force: delete existing entry */
84 void *modes = 0;
85 uid_t uid = -1;
86 gid_t gid = -1;
87 int rval;
88
89 dev = 0;
90 fifo = hasformat = 0;
91 pack = pack_native;
92
93 while ((ch = getopt(argc, argv, "rRF:g:m:u:")) != -1) {
94 switch (ch) {
95
96 case 'r':
97 r_flag = 1;
98 break;
99
100 case 'R':
101 r_flag = 2;
102 break;
103
104 case 'F':
105 pack = pack_find(optarg);
106 if (pack == NULL)
107 errx(1, "invalid format: %s", optarg);
108 hasformat++;
109 break;
110
111 case 'g':
112 if (optarg[0] == '#') {
113 gid = strtol(optarg + 1, &p, 10);
114 if (*p == 0)
115 break;
116 }
117 if (gid_name(optarg, &gid) == 0)
118 break;
119 gid = strtol(optarg, &p, 10);
120 if (*p == 0)
121 break;
122 errx(1, "%s: invalid group name", optarg);
123
124 case 'm':
125 modes = setmode(optarg);
126 if (modes == NULL)
127 errx(1, "invalid mode: %s", optarg);
128 break;
129
130 case 'u':
131 if (optarg[0] == '#') {
132 uid = strtol(optarg + 1, &p, 10);
133 if (*p == 0)
134 break;
135 }
136 if (uid_from_user(optarg, &uid) == 0)
137 break;
138 uid = strtol(optarg, &p, 10);
139 if (*p == 0)
140 break;
141 errx(1, "%s: invalid user name", optarg);
142
143 default:
144 case '?':
145 usage();
146 }
147 }
148 argc -= optind;
149 argv += optind;
150
151 if (argc < 2 || argc > 10)
152 usage();
153
154 name = *argv;
155 argc--;
156 argv++;
157
158 umask(mode = umask(0));
159 mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mode;
160
161 if (argv[0][1] != '\0')
162 goto badtype;
163 switch (*argv[0]) {
164 case 'c':
165 mode |= S_IFCHR;
166 break;
167
168 case 'b':
169 mode |= S_IFBLK;
170 break;
171
172 case 'p':
173 if (hasformat)
174 errx(1, "format is meaningless for fifos");
175 mode |= S_IFIFO;
176 fifo = 1;
177 break;
178
179 default:
180 badtype:
181 errx(1, "node type must be 'b', 'c' or 'p'.");
182 }
183 argc--;
184 argv++;
185
186 if (fifo) {
187 if (argc != 0)
188 usage();
189 } else {
190 if (argc < 1 || argc > MAXARGS)
191 usage();
192 }
193
194 for (n = 0; n < argc; n++) {
195 errno = 0;
196 numbers[n] = strtoul(argv[n], &p, 0);
197 if (*p == 0 && errno == 0)
198 continue;
199 errx(1, "invalid number: %s", argv[n]);
200 }
201
202 switch (argc) {
203 case 0:
204 dev = 0;
205 break;
206
207 case 1:
208 dev = numbers[0];
209 break;
210
211 default:
212 dev = callPack(pack, argc, numbers);
213 break;
214 }
215
216 if (modes != NULL)
217 mode = getmode(modes, mode);
218 #if 0
219 printf("name: %s\nmode: %05o\ndev: %08x\nuid: %5d\ngid: %5d\n",
220 name, mode, dev, uid, gid);
221 #endif
222 umask(0);
223 rval = fifo ? mkfifo(name, mode) : mknod(name, mode, dev);
224 if (rval < 0 && errno == EEXIST && r_flag) {
225 struct stat sb;
226 if (lstat(name, &sb) != 0 || (!fifo && sb.st_rdev != dev))
227 sb.st_mode = 0;
228
229 if ((sb.st_mode & S_IFMT) == (mode & S_IFMT)) {
230 if (r_flag == 1)
231 /* Ignore permissions and user/group */
232 return 0;
233 if (sb.st_mode != mode)
234 rval = chmod(name, mode);
235 else
236 rval = 0;
237 } else {
238 unlink(name);
239 rval = fifo ? mkfifo(name, mode)
240 : mknod(name, mode, dev);
241 }
242 }
243 if (rval < 0)
244 err(1, "%s", name);
245 if ((uid != -1 || gid != -1) && chown(name, uid, gid) == -1)
246 /* XXX Should we unlink the files here? */
247 warn("%s: uid/gid not changed", name);
248
249 return 0;
250 }
251
252 static void
253 usage(void)
254 {
255 const char *progname = getprogname();
256
257 (void)fprintf(stderr,
258 "usage: %s [-rR] [-F format] [-m mode] [-u user] [-g group]\n",
259 progname);
260 (void)fprintf(stderr,
261 " [ name [b | c] major minor\n"
262 " | name [b | c] major unit subunit\n"
263 " | name [b | c] number\n"
264 " | name p ]\n");
265 exit(1);
266 }
267
268 static int
269 gid_name(const char *name, gid_t *gid)
270 {
271 struct group *g;
272
273 g = getgrnam(name);
274 if (!g)
275 return -1;
276 *gid = g->gr_gid;
277 return 0;
278 }
279
280 static portdev_t
281 callPack(pack_t *f, int n, u_long *numbers)
282 {
283 portdev_t d;
284 const char *error = NULL;
285
286 d = (*f)(n, numbers, &error);
287 if (error != NULL)
288 errx(1, "%s", error);
289 return d;
290 }
291