/*1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1989, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Kevin Fall.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/types.h>35#include <sys/stat.h>3637#include <err.h>38#include <errno.h>39#include <stdio.h>40#include <stdlib.h>41#include <unistd.h>42#include <grp.h>43#include <pwd.h>44#include <string.h>4546static void47usage(void)48{4950(void)fprintf(stderr,51"usage: mknod name\n"52" mknod name [b | c] major minor [owner:group]\n");53exit(1);54}5556static u_long57id(const char *name, const char *type)58{59u_long val;60char *ep;6162/*63* XXX64* We know that uid_t's and gid_t's are unsigned longs.65*/66errno = 0;67val = strtoul(name, &ep, 10);68if (errno)69err(1, "%s", name);70if (*ep != '\0')71errx(1, "%s: illegal %s name", name, type);72return (val);73}7475static gid_t76a_gid(const char *s)77{78struct group *gr;7980if (*s == '\0') /* Argument was "uid[:.]". */81errx(1, "group must be specified when the owner is");82return ((gr = getgrnam(s)) == NULL) ? id(s, "group") : gr->gr_gid;83}8485static uid_t86a_uid(const char *s)87{88struct passwd *pw;8990if (*s == '\0') /* Argument was "[:.]gid". */91errx(1, "owner must be specified when the group is");92return ((pw = getpwnam(s)) == NULL) ? id(s, "user") : pw->pw_uid;93}9495int96main(int argc, char **argv)97{98int range_error;99uid_t uid;100gid_t gid;101mode_t mode;102dev_t dev;103char *cp, *endp;104long mymajor, myminor;105106if (argc != 2 && argc != 5 && argc != 6)107usage();108109if (argc >= 5) {110mode = 0666;111if (argv[2][0] == 'c')112mode |= S_IFCHR;113else if (argv[2][0] == 'b')114mode |= S_IFBLK;115else116errx(1, "node must be type 'b' or 'c'");117118errno = 0;119mymajor = (long)strtoul(argv[3], &endp, 0);120if (endp == argv[3] || *endp != '\0')121errx(1, "%s: non-numeric major number", argv[3]);122range_error = errno;123errno = 0;124myminor = (long)strtoul(argv[4], &endp, 0);125if (endp == argv[4] || *endp != '\0')126errx(1, "%s: non-numeric minor number", argv[4]);127range_error |= errno;128dev = makedev(mymajor, myminor);129if (range_error || major(dev) != mymajor ||130(long)(u_int)minor(dev) != myminor)131errx(1, "major or minor number too large");132} else {133mode = 0666 | S_IFCHR;134dev = 0;135}136137uid = gid = -1;138if (6 == argc) {139/* have owner:group */140if ((cp = strchr(argv[5], ':')) != NULL) {141*cp++ = '\0';142gid = a_gid(cp);143} else144usage();145uid = a_uid(argv[5]);146}147148if (mknod(argv[1], mode, dev) != 0)149err(1, "%s", argv[1]);150if (6 == argc)151if (chown(argv[1], uid, gid))152err(1, "setting ownership on %s", argv[1]);153exit(0);154}155156157