Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Use functions instead of preprocessor abuse.



details:   https://anonhg.NetBSD.org/src/rev/6a5dff552c0d
branches:  trunk
changeset: 814241:6a5dff552c0d
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Mar 13 00:32:09 2016 +0000

description:
Use functions instead of preprocessor abuse.

diffstat:

 libexec/atrun/Makefile |   6 ++-
 libexec/atrun/atrun.c  |  51 ++++++++++++++-----------
 usr.bin/at/Makefile    |   4 +-
 usr.bin/at/at.c        |  39 +++++++++----------
 usr.bin/at/panic.c     |  19 ++++++---
 usr.bin/at/perm.c      |  12 +++---
 usr.bin/at/privs.c     |  99 ++++++++++++++++++++++++++++++++++++++++++++++++++
 usr.bin/at/privs.h     |  51 +++++--------------------
 8 files changed, 183 insertions(+), 98 deletions(-)

diffs (truncated from 668 to 300 lines):

diff -r 3ab5d9a08860 -r 6a5dff552c0d libexec/atrun/Makefile
--- a/libexec/atrun/Makefile    Sun Mar 13 00:09:46 2016 +0000
+++ b/libexec/atrun/Makefile    Sun Mar 13 00:32:09 2016 +0000
@@ -1,11 +1,15 @@
-#      $NetBSD: Makefile,v 1.14 2011/08/16 10:35:03 christos Exp $
+#      $NetBSD: Makefile,v 1.15 2016/03/13 00:32:09 dholland Exp $
 
 .include <bsd.own.mk>
 
 PROG=  atrun
+SRCS=  atrun.c
 BINDIR=        /usr/libexec
 MAN=   atrun.8
 
+.PATH.c: ${NETBSDSRCDIR}/usr.bin/at
+SRCS+= privs.c
+
 CPPFLAGS+= -I${NETBSDSRCDIR}/usr.bin/at
 COPTS.atrun.c = -Wno-format-nonliteral
 
diff -r 3ab5d9a08860 -r 6a5dff552c0d libexec/atrun/atrun.c
--- a/libexec/atrun/atrun.c     Sun Mar 13 00:09:46 2016 +0000
+++ b/libexec/atrun/atrun.c     Sun Mar 13 00:32:09 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: atrun.c,v 1.21 2011/09/16 16:13:17 plunky Exp $        */
+/*     $NetBSD: atrun.c,v 1.22 2016/03/13 00:32:09 dholland Exp $      */
 
 /*
  *  atrun.c - run jobs queued by at; run with root privileges.
@@ -49,7 +49,6 @@
 
 /* Local headers */
 
-#define MAIN
 #include "privs.h"
 #include "pathnames.h"
 #include "atrun.h"
@@ -59,7 +58,7 @@
 #if 0
 static char rcsid[] = "$OpenBSD: atrun.c,v 1.7 1997/09/08 22:12:10 millert Exp $";
 #else
-__RCSID("$NetBSD: atrun.c,v 1.21 2011/09/16 16:13:17 plunky Exp $");
+__RCSID("$NetBSD: atrun.c,v 1.22 2016/03/13 00:32:09 dholland Exp $");
 #endif
 
 static int debug = 0;
@@ -108,6 +107,12 @@
        exit(EXIT_FAILURE);
 }
 
+__dead void
+privs_fail(const char *msg)
+{
+       perr("%s", msg);
+}
+
 static int
 write_string(int fd, const char *a)
 {
@@ -160,12 +165,12 @@
        gid_t ngid;
        int serrno;
 
-       PRIV_START;
+       privs_enter();
 
        if (chmod(filename, S_IRUSR) == -1)
                perr("Cannot change file permissions to `%s'", filename);
 
-       PRIV_END;
+       privs_exit();
 
        pid = fork();
        if (pid == -1)
@@ -184,12 +189,12 @@
                perrx("Userid %lu not found - aborting job `%s'",
                    (unsigned long)uid, filename);
 
-       PRIV_START;
+       privs_enter();
 
        stream = fopen(filename, "r");
        serrno = errno;
 
-       PRIV_END;
+       privs_exit();
 
        if (stream == NULL) {
                errno = serrno;
@@ -206,12 +211,12 @@
        if (fstat(fd_in, &buf) == -1)
                perr("Error in fstat of input file descriptor");
 
-       PRIV_START;
+       privs_enter();
 
        if (lstat(filename, &lbuf) == -1)
                perr("Error in lstat of `%s'", filename);
 
-       PRIV_END;
+       privs_exit();
 
        if (S_ISLNK(lbuf.st_mode))
                perrx("Symbolic link encountered in job `%s' - aborting",
@@ -253,7 +258,7 @@
 
        (void)fclose(stream);
 
-       PRIV_START;
+       privs_enter();
 
        if (chdir(_PATH_ATSPOOL) == -1)
                perr("Cannot chdir to `%s'", _PATH_ATSPOOL);
@@ -267,7 +272,7 @@
                    O_WRONLY | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR)) == -1)
                perr("Cannot create output file `%s'", filename);
 
-       PRIV_END;
+       privs_exit();
 
        write_string(fd_out, "To: ");
        write_string(fd_out, mailname);
@@ -309,7 +314,7 @@
                (void)close(fd_in);
                (void)close(fd_out);
 
-               PRIV_START;
+               privs_enter();
 
                if (chdir(_PATH_ATJOBS) == -1)
                        perr("Cannot chdir to `%s'", _PATH_ATJOBS);
@@ -333,7 +338,7 @@
         * Send mail.  Unlink the output file first, so it is deleted
         * after the run.
         */
-       PRIV_START;
+       privs_enter();
 
        if (stat(filename, &buf) == -1)
                perr("Error in stat of output file `%s'", filename);
@@ -342,12 +347,12 @@
 
        (void)unlink(filename);
 
-       PRIV_END;
+       privs_exit();
 
        if ((buf.st_size != size) || send_mail) {
                /* Fork off a child for sending mail */
 
-               PRIV_START;
+               privs_enter();
 
                become_user(pentry, uid);
 
@@ -355,7 +360,7 @@
                    "-odi", "-oem", "-t", (char *) NULL);
                perr("Exec failed for mail command `%s'", _PATH_SENDMAIL);
 
-               PRIV_END;
+               privs_exit();
        }
        exit(EXIT_SUCCESS);
 }
@@ -406,7 +411,7 @@
         * We don't need root privileges all the time; running under uid
         * and gid nobody is fine except for privileged operations.
         */
-       RELINQUISH_PRIVS_ROOT(pwd->pw_uid, grp->gr_gid);
+       privs_relinquish_root(pwd->pw_uid, grp->gr_gid);
 
        opterr = 0;
        errno = 0;
@@ -433,7 +438,7 @@
                }
        }
 
-       PRIV_START;
+       privs_enter();
 
        if (chdir(_PATH_ATJOBS) == -1)
                perr("Cannot change directory to `%s'", _PATH_ATJOBS);
@@ -453,7 +458,7 @@
        if ((spool = opendir(".")) == NULL)
                perr("Cannot open `%s'", _PATH_ATJOBS);
 
-       PRIV_END;
+       privs_exit();
 
        now = time(NULL);
        run_batch = 0;
@@ -461,13 +466,13 @@
        batch_gid = (gid_t) -1;
 
        while ((dirent = readdir(spool)) != NULL) {
-               PRIV_START;
+               privs_enter();
 
                if (stat(dirent->d_name, &buf) == -1)
                        perr("Cannot stat `%s' in `%s'", dirent->d_name,
                            _PATH_ATJOBS);
 
-               PRIV_END;
+               privs_exit();
 
                /* We don't want directories */
                if (!S_ISREG(buf.st_mode))
@@ -498,11 +503,11 @@
                /* Delete older files */
                if ((run_time < now) && !(S_IXUSR & buf.st_mode) &&
                    (S_IRUSR & buf.st_mode)) {
-                       PRIV_START;
+                       privs_enter();
 
                        (void)unlink(dirent->d_name);
 
-                       PRIV_END;
+                       privs_exit();
                }
        }
 
diff -r 3ab5d9a08860 -r 6a5dff552c0d usr.bin/at/Makefile
--- a/usr.bin/at/Makefile       Sun Mar 13 00:09:46 2016 +0000
+++ b/usr.bin/at/Makefile       Sun Mar 13 00:32:09 2016 +0000
@@ -1,9 +1,9 @@
-#      $NetBSD: Makefile,v 1.11 2009/04/14 22:15:17 lukem Exp $
+#      $NetBSD: Makefile,v 1.12 2016/03/13 00:32:09 dholland Exp $
 
 USE_FORT?=     yes     # setuid
 
 PROG=          at
-SRCS=          at.c panic.c parsetime.c perm.c stime.c
+SRCS=          at.c panic.c parsetime.c perm.c privs.c stime.c
 LINKS=         ${BINDIR}/at ${BINDIR}/atq \
                ${BINDIR}/at ${BINDIR}/atrm \
                ${BINDIR}/at ${BINDIR}/batch
diff -r 3ab5d9a08860 -r 6a5dff552c0d usr.bin/at/at.c
--- a/usr.bin/at/at.c   Sun Mar 13 00:09:46 2016 +0000
+++ b/usr.bin/at/at.c   Sun Mar 13 00:32:09 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: at.c,v 1.30 2011/08/29 14:24:03 joerg Exp $    */
+/*     $NetBSD: at.c,v 1.31 2016/03/13 00:32:09 dholland Exp $ */
 
 /*
  *  at.c : Put file into atrun queue
@@ -57,7 +57,6 @@
 #include "perm.h"
 #include "pathnames.h"
 #include "stime.h"
-#define MAIN
 #include "privs.h"
 
 /* Macros */
@@ -72,7 +71,7 @@
 #if 0
 static char rcsid[] = "$OpenBSD: at.c,v 1.15 1998/06/03 16:20:26 deraadt Exp $";
 #else
-__RCSID("$NetBSD: at.c,v 1.30 2011/08/29 14:24:03 joerg Exp $");
+__RCSID("$NetBSD: at.c,v 1.31 2016/03/13 00:32:09 dholland Exp $");
 #endif
 #endif
 
@@ -108,9 +107,9 @@
 
        /* If a signal interrupts us, remove the spool file and exit. */
        if (fcreated) {
-               PRIV_START;
+               privs_enter();
                (void)unlink(atfile);
-               PRIV_END;
+               privs_exit();
        }
        (void)raise_default_signal(signo);
        exit(EXIT_FAILURE);
@@ -207,7 +206,7 @@
         * to make sure we're alone when doing this.
         */
 
-       PRIV_START;
+       privs_enter();
 
        if ((lockdes = open(_PATH_LOCKFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR)) < 0)
                perr("Cannot open lockfile " _PATH_LOCKFILE);
@@ -260,7 +259,7 @@
        if (fchown(fd2, real_uid, real_gid) == -1)
                perr("Cannot give away file");
 
-       PRIV_END;
+       privs_exit();
 
        /*
         * We've successfully created the file; let's set the flag so it
@@ -402,7 +401,7 @@
 
        (void)fclose(fp);
 
-       PRIV_START;
+       privs_enter();
 
        /*



Home | Main Index | Thread Index | Old Index