1 /* $NetBSD: mknod.c,v 1.27 2003/05/08 13:29:39 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 #include <sys/cdefs.h> 40 #ifndef lint 41 __COPYRIGHT("@(#) Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.\n"); 42 __RCSID("$NetBSD: mknod.c,v 1.27 2003/05/08 13:29:39 christos Exp $"); 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 #include <sys/stat.h> 47 #include <sys/param.h> 48 49 #include <err.h> 50 #include <errno.h> 51 #include <limits.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <unistd.h> 55 #include <pwd.h> 56 #include <grp.h> 57 #include <string.h> 58 #include <ctype.h> 59 60 #include "pack_dev.h" 61 62 static int gid_name(const char *, gid_t *); 63 64 int main(int, char *[]); 65 static void usage(void); 66 67 #define MAXARGS 3 /* 3 for bsdos, 2 for rest */ 68 69 int 70 main(int argc, char **argv) 71 { 72 char *name, *p; 73 mode_t mode; 74 portdev_t dev; 75 pack_t *pack; 76 u_long numbers[MAXARGS]; 77 int n, ch, fifo, hasformat; 78 int r_flag = 0; /* force: delete existing entry */ 79 void *modes = 0; 80 uid_t uid = -1; 81 gid_t gid = -1; 82 int rval; 83 84 dev = 0; 85 fifo = hasformat = 0; 86 pack = pack_native; 87 88 while ((ch = getopt(argc, argv, "rRF:g:m:u:")) != -1) { 89 switch (ch) { 90 91 case 'r': 92 r_flag = 1; 93 break; 94 95 case 'R': 96 r_flag = 2; 97 break; 98 99 case 'F': 100 pack = pack_find(optarg); 101 if (pack == NULL) 102 errx(1, "invalid format: %s", optarg); 103 hasformat++; 104 break; 105 106 case 'g': 107 if (optarg[0] == '#') { 108 gid = strtol(optarg + 1, &p, 10); 109 if (*p == 0) 110 break; 111 } 112 if (gid_name(optarg, &gid) == 0) 113 break; 114 gid = strtol(optarg, &p, 10); 115 if (*p == 0) 116 break; 117 errx(1, "%s: invalid group name", optarg); 118 119 case 'm': 120 modes = setmode(optarg); 121 if (modes == NULL) 122 errx(1, "invalid mode: %s", optarg); 123 break; 124 125 case 'u': 126 if (optarg[0] == '#') { 127 uid = strtol(optarg + 1, &p, 10); 128 if (*p == 0) 129 break; 130 } 131 if (uid_from_user(optarg, &uid) == 0) 132 break; 133 uid = strtol(optarg, &p, 10); 134 if (*p == 0) 135 break; 136 errx(1, "%s: invalid user name", optarg); 137 138 default: 139 case '?': 140 usage(); 141 } 142 } 143 argc -= optind; 144 argv += optind; 145 146 if (argc < 2 || argc > 10) 147 usage(); 148 149 name = *argv; 150 argc--; 151 argv++; 152 153 umask(mode = umask(0)); 154 mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) & ~mode; 155 156 if (argv[0][1] != '\0') 157 goto badtype; 158 switch (*argv[0]) { 159 case 'c': 160 mode |= S_IFCHR; 161 break; 162 163 case 'b': 164 mode |= S_IFBLK; 165 break; 166 167 case 'p': 168 if (hasformat) 169 errx(1, "format is meaningless for fifos"); 170 mode |= S_IFIFO; 171 fifo = 1; 172 break; 173 174 default: 175 badtype: 176 errx(1, "node type must be 'b', 'c' or 'p'."); 177 } 178 argc--; 179 argv++; 180 181 if (fifo) { 182 if (argc != 0) 183 usage(); 184 } else { 185 if (argc < 1 || argc > MAXARGS) 186 usage(); 187 } 188 189 for (n = 0; n < argc; n++) { 190 errno = 0; 191 numbers[n] = strtoul(argv[n], &p, 0); 192 if (*p == 0 && errno == 0) 193 continue; 194 errx(1, "invalid number: %s", argv[n]); 195 } 196 197 switch (argc) { 198 case 0: 199 dev = 0; 200 break; 201 202 case 1: 203 dev = numbers[0]; 204 break; 205 206 default: 207 dev = (*pack)(argc, numbers); 208 break; 209 } 210 211 if (modes != NULL) 212 mode = getmode(modes, mode); 213 #if 0 214 printf("name: %s\nmode: %05o\ndev: %08x\nuid: %5d\ngid: %5d\n", 215 name, mode, dev, uid, gid); 216 #endif 217 umask(0); 218 rval = fifo ? mkfifo(name, mode) : mknod(name, mode, dev); 219 if (rval < 0 && errno == EEXIST && r_flag) { 220 struct stat sb; 221 if (lstat(name, &sb) != 0 || (!fifo && sb.st_rdev != dev)) 222 sb.st_mode = 0; 223 224 if ((sb.st_mode & S_IFMT) == (mode & S_IFMT)) { 225 if (r_flag == 1) 226 /* Ignore permissions and user/group */ 227 return 0; 228 if (sb.st_mode != mode) 229 rval = chmod(name, mode); 230 else 231 rval = 0; 232 } else { 233 unlink(name); 234 rval = fifo ? mkfifo(name, mode) 235 : mknod(name, mode, dev); 236 } 237 } 238 if (rval < 0) 239 err(1, "%s", name); 240 if ((uid != -1 || gid != -1) && chown(name, uid, gid) == -1) 241 /* XXX Should we unlink the files here? */ 242 warn("%s: uid/gid not changed", name); 243 244 return 0; 245 } 246 247 static void 248 usage(void) 249 { 250 const char *progname = getprogname(); 251 252 (void)fprintf(stderr, 253 "Usage: %s [-rR] [-F format] [-m mode] [-u user] [-g group]\n", 254 progname); 255 (void)fprintf(stderr, 256 " [ name [b | c] major minor\n" 257 " | name [b | c] major unit subunit\n" 258 " | name [b | c] number\n" 259 " | name p ]\n"); 260 exit(1); 261 } 262 263 static int 264 gid_name(const char *name, gid_t *gid) 265 { 266 struct group *g; 267 268 g = getgrnam(name); 269 if (!g) 270 return -1; 271 *gid = g->gr_gid; 272 return 0; 273 } 274