diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2016-03-05 10:47:09 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2016-03-05 10:47:09 +0000 |
commit | ef291430e2204dceca880cbb9640762e2d49bceb (patch) | |
tree | bbaf03b22d934494dcce53e6e13b7ddb4a8fbf40 | |
parent | 59bb47308294ab8725ab6852025bd94718120a9c (diff) |
Almost complete rewrite of mknod.
With this, none of the original code survives, so the Copyright notice
can be changed accordingly.
This does pledge upfront, because having fifos or devices with
setuid/gid/sticky makes no sense anyway.
The reorganization was done to allow a "create multiple devices" at once
mode, in order to speed up MAKEDEV (eventually) now that the ksh builtin
is gone. The code is picky and checks all parameters before doing anything.
Inputs from natano, deraadt, rpe, millert, tb, jmc...
okay tb@, deraadt@
-rw-r--r-- | sbin/mknod/mknod.8 | 19 | ||||
-rw-r--r-- | sbin/mknod/mknod.c | 286 |
2 files changed, 182 insertions, 123 deletions
diff --git a/sbin/mknod/mknod.8 b/sbin/mknod/mknod.8 index 37a0e20d768..d861e9afbd1 100644 --- a/sbin/mknod/mknod.8 +++ b/sbin/mknod/mknod.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: mknod.8,v 1.20 2016/03/04 16:49:17 tb Exp $ +.\" $OpenBSD: mknod.8,v 1.21 2016/03/05 10:47:07 espie Exp $ .\" $NetBSD: mknod.8,v 1.9 1995/08/10 23:47:32 jtc Exp $ .\" .\" Copyright (c) 1980, 1991, 1993 @@ -30,7 +30,7 @@ .\" .\" @(#)mknod.8 8.2 (Berkeley) 12/11/93 .\" -.Dd $Mdocdate: March 4 2016 $ +.Dd $Mdocdate: March 5 2016 $ .Dt MKNOD 8 .Os .Sh NAME @@ -121,6 +121,21 @@ will cause the number to be interpreted as octal. .Xr mkfifo 2 , .Xr mknod 2 , .Xr MAKEDEV 8 +.Sh STANDARDS +As an extension, +.Nm +can also take multiple lists of parameters in one go. +Note that +.Fl m +is not reset from one list to the next so, for example, in +.Pp +.Dl mknod -m 700 name b 12 5 name2 b 12 6 +.Pp +both +.Ar name +and +.Ar name2 +will be mode 700. .Sh HISTORY A .Nm diff --git a/sbin/mknod/mknod.c b/sbin/mknod/mknod.c index 525e095751e..ea55587cbed 100644 --- a/sbin/mknod/mknod.c +++ b/sbin/mknod/mknod.c @@ -1,36 +1,22 @@ -/* $OpenBSD: mknod.c,v 1.20 2016/03/04 16:48:13 natano Exp $ */ +/* $OpenBSD: mknod.c,v 1.21 2016/03/05 10:47:08 espie Exp $ */ /* $NetBSD: mknod.c,v 1.8 1995/08/11 00:08:18 jtc Exp $ */ /* - * Copyright (c) 1989, 1990, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 1997-2016 Theo de Raadt <deraadt@openbsd.org>, + * Marc Espie <espie@openbsd.org>, Todd Miller <millert@openbsd.org>, + * Martin Natano <natano@openbsd.org> * - * This code is derived from software contributed to Berkeley by - * Kevin Fall. + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <sys/types.h> @@ -44,131 +30,189 @@ extern char *__progname; -int domknod(char **, mode_t); -int domkfifo(char **, mode_t); -void usage(int); + +struct node { + const char *name; + mode_t mode; + dev_t dev; + char type; + char mflag; +}; + +static int domakenodes(struct node *, int); +static dev_t compute_device(int, char **); +__dead static void usage(int); int main(int argc, char *argv[]) { - int ch, ismkfifo = 0; - void *set = NULL; - mode_t mode = DEFFILEMODE; - - setlocale (LC_ALL, ""); - - if (strcmp(__progname, "mkfifo") == 0) - ismkfifo = 1; - - while ((ch = getopt(argc, argv, "m:")) != -1) - switch (ch) { - case 'm': - if (!(set = setmode(optarg))) { - errx(1, "invalid file mode."); - /* NOTREACHED */ + struct node *node; + int ismkfifo; + int n = 0; + int mode = DEFFILEMODE; + int mflag = 0; + void *set; + int ch; + + if (pledge("stdio rpath wpath cpath dpath fattr", NULL) == -1) + err(1, "pledge"); + + setlocale(LC_ALL, ""); + + node = reallocarray(NULL, sizeof(struct node), argc); + if (!node) + err(1, NULL); + + + ismkfifo = strcmp(__progname, "mkfifo") == 0; + + /* we parse all arguments upfront */ + while (argc > 1) { + while ((ch = getopt(argc, argv, "m:")) != -1) { + switch (ch) { + case 'm': + if (!(set = setmode(optarg))) { + errx(1, "invalid file mode."); + /* NOTREACHED */ + } + + /* + * In symbolic mode strings, the + and - + * operators are interpreted relative to + * an assumed initial mode of a=rw. + */ + mode = getmode(set, DEFFILEMODE); + if ((mode & ACCESSPERMS) != mode) + errx(1, "forbidden mode: %o", mode); + mflag = 1; + free(set); + break; + default: + usage(ismkfifo); } - - /* - * In symbolic mode strings, the + and - operators are - * interpreted relative to an assumed initial mode of - * a=rw. - */ - mode = getmode(set, DEFFILEMODE); - free(set); + } + argc -= optind; + argv += optind; + + if (ismkfifo) { + while (*argv) { + node[n].mode = mode; + node[n].mflag = mflag; + node[n].type = 'p'; + node[n].name = *argv; + node[n].dev = 0; + n++; + argv++; + } + /* XXX no multiple getopt */ break; - case '?': - default: - usage(ismkfifo); + } else { + if (argc < 2) + usage(ismkfifo); + node[n].mode = mode; + node[n].mflag = mflag; + node[n].name = argv[0]; + if (strlen(argv[1]) != 1) + errx(1, "node must be type 'b|c|p' %s", + argv[1]); + node[n].type = argv[1][0]; + + /* XXX computation offset by one for next getopt */ + switch(node[n].type) { + case 'p': + node[n].dev = 0; + argv++; + argc--; + break; + case 'b': + node[n].mode |= S_IFBLK; + goto common; + case 'c': + node[n].mode |= S_IFCHR; +common: + node[n].dev = compute_device(argc, argv); + argv+=3; + argc-=3; + break; + default: + errx(1, "node must be type 'b|c|p' %c", + node[n].type); + } + n++; } - argc -= optind; - argv += optind; - - if ((mode & ACCESSPERMS) == mode) - if (pledge("stdio rpath wpath cpath dpath fattr", NULL) == -1) - err(1, "pledge"); + optind = 1; + optreset = 1; + } - if (argv[0] == NULL) + if (n == 0) usage(ismkfifo); - if (!ismkfifo) { - if (argc == 2 && argv[1][0] == 'p') { - ismkfifo = 2; - argc--; - argv[1] = NULL; - } else if (argc != 4) { - usage(ismkfifo); - /* NOTREACHED */ - } - } - /* - * If the user specified a mode via `-m', don't allow the umask - * to modify it. If no `-m' flag was specified, the default - * mode is the value of the bitwise inclusive or of S_IRUSR, - * S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH as modified by - * the umask. - */ - if (set) - (void)umask(0); - - if (ismkfifo) - exit(domkfifo(argv, mode)); - else - exit(domknod(argv, mode)); + return (domakenodes(node, n)); } -int -domknod(char **argv, mode_t mode) +static dev_t +compute_device(int argc, char **argv) { dev_t dev; char *endp; u_int major, minor; - if (argv[1][0] == 'c') - mode |= S_IFCHR; - else if (argv[1][0] == 'b') - mode |= S_IFBLK; - else { - errx(1, "node must be type 'b' or 'c'."); - /* NOTREACHED */ - } - + if (argc < 4) + usage(0); major = (long)strtoul(argv[2], &endp, 0); - if (endp == argv[2] || *endp != '\0') { + if (endp == argv[2] || *endp != '\0') errx(1, "non-numeric major number."); - /* NOTREACHED */ - } minor = (long)strtoul(argv[3], &endp, 0); - if (endp == argv[3] || *endp != '\0') { + if (endp == argv[3] || *endp != '\0') errx(1, "non-numeric minor number."); - /* NOTREACHED */ - } dev = makedev(major, minor); - if (major(dev) != major || minor(dev) != minor) { + if (major(dev) != major || minor(dev) != minor) errx(1, "major or minor number too large"); - /* NOTREACHED */ - } - if (mknod(argv[0], mode, dev) < 0) { - err(1, "%s", argv[0]); - /* NOTREACHED */ - } - return(0); + return dev; } -int -domkfifo(char **argv, mode_t mode) +static int +domakenodes(struct node *node, int n) { - int rv; + int done_umask = 0; + int rv = 0; + int i; + +#if !defined(CHECK_PARSING_ONLY) + for (i = 0; i != n; i++) { + int r; + /* + * If the user specified a mode via `-m', don't allow the umask + * to modify it. If no `-m' flag was specified, the default + * mode is the value of the bitwise inclusive or of S_IRUSR, + * S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH as + * modified by the umask. + */ + if (node[i].mflag && !done_umask) { + (void)umask(0); + done_umask = 1; + } - for (rv = 0; *argv; ++argv) { - if (mkfifo(*argv, mode) < 0) { - warn("%s", *argv); + if (node[i].type == 'p') + r = mkfifo(node[i].name, node[i].mode); + else + r = mknod(node[i].name, node[i].mode, node[i].dev); + if (r < 0) { + warn("%s", node[i].name); rv = 1; } } - return(rv); +#else + for (i = 0; i != n; i++) + printf("%s %c (mode %o) dev=%d\n", node[i].name, + node[i].type, node[i].mode, node[i].dev); + +#endif + free(node); + return rv; } -void +__dead static void usage(int ismkfifo) { |