1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kevin Fall.
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. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/types.h>
36#include <sys/stat.h>
37
38#include <err.h>
39#include <errno.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <grp.h>
44#include <pwd.h>
45#include <string.h>
46
47static void
48usage(void)
49{
50
51	(void)fprintf(stderr,
52	    "usage: mknod name\n"
53	    "       mknod name [b | c] major minor [owner:group]\n");
54	exit(1);
55}
56
57static u_long
58id(const char *name, const char *type)
59{
60	u_long val;
61	char *ep;
62
63	/*
64	 * XXX
65	 * We know that uid_t's and gid_t's are unsigned longs.
66	 */
67	errno = 0;
68	val = strtoul(name, &ep, 10);
69	if (errno)
70		err(1, "%s", name);
71	if (*ep != '\0')
72		errx(1, "%s: illegal %s name", name, type);
73	return (val);
74}
75
76static gid_t
77a_gid(const char *s)
78{
79	struct group *gr;
80
81	if (*s == '\0')			/* Argument was "uid[:.]". */
82		errx(1, "group must be specified when the owner is");
83	return ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid;
84}
85
86static uid_t
87a_uid(const char *s)
88{
89	struct passwd *pw;
90
91	if (*s == '\0')			/* Argument was "[:.]gid". */
92		errx(1, "owner must be specified when the group is");
93	return ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid;
94}
95
96int
97main(int argc, char **argv)
98{
99	int range_error;
100	uid_t uid;
101	gid_t gid;
102	mode_t mode;
103	dev_t dev;
104	char *cp, *endp;
105	long mymajor, myminor;
106
107	if (argc != 2 && argc != 5 && argc != 6)
108		usage();
109
110	if (argc >= 5) {
111		mode = 0666;
112		if (argv[2][0] == 'c')
113			mode |= S_IFCHR;
114		else if (argv[2][0] == 'b')
115			mode |= S_IFBLK;
116		else
117			errx(1, "node must be type 'b' or 'c'");
118
119		errno = 0;
120		mymajor = (long)strtoul(argv[3], &endp, 0);
121		if (endp == argv[3] || *endp != '\0')
122			errx(1, "%s: non-numeric major number", argv[3]);
123		range_error = errno;
124		errno = 0;
125		myminor = (long)strtoul(argv[4], &endp, 0);
126		if (endp == argv[4] || *endp != '\0')
127			errx(1, "%s: non-numeric minor number", argv[4]);
128		range_error |= errno;
129		dev = makedev(mymajor, myminor);
130		if (range_error || major(dev) != mymajor ||
131		    (long)(u_int)minor(dev) != myminor)
132			errx(1, "major or minor number too large");
133	} else {
134		mode = 0666 | S_IFCHR;
135		dev = 0;
136	}
137
138	uid = gid = -1;
139	if (6 == argc) {
140	    	/* have owner:group */
141		if ((cp = strchr(argv[5], ':')) != NULL) {
142			*cp++ = '\0';
143			gid = a_gid(cp);
144		} else
145		usage();
146		uid = a_uid(argv[5]);
147	}
148
149	if (mknod(argv[1], mode, dev) != 0)
150		err(1, "%s", argv[1]);
151	if (6 == argc)
152		if (chown(argv[1], uid, gid))
153			err(1, "setting ownership on %s", argv[1]);
154	exit(0);
155}
156