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