]> git.armaanb.net Git - opendoas.git/blobdiff - doas.c
Add version information to doas executable.
[opendoas.git] / doas.c
diff --git a/doas.c b/doas.c
index 7a525e6e9b55432d673c719d7bc145a243813d5d..1457925bb928946910df44c13e984aa2e971e471 100644 (file)
--- a/doas.c
+++ b/doas.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: doas.c,v 1.32 2015/07/29 00:00:31 tedu Exp $ */
+/* $OpenBSD: doas.c,v 1.33 2015/07/30 17:04:33 tedu Exp $ */
 /*
  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
  *
@@ -19,8 +19,6 @@
 #include <sys/stat.h>
 
 #include <limits.h>
-#include <login_cap.h>
-#include <bsd_auth.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <syslog.h>
 #include <errno.h>
 
+#include "openbsd.h"
+
 #include "doas.h"
+#include "version.h"
+
+static void __dead
+version(void)
+{
+       fprintf(stderr, "doas: version %s built %s\n", VERSION, __DATE__);
+       exit(1);
+}
 
 static void __dead
 usage(void)
 {
-       fprintf(stderr, "usage: doas [-ns] [-C config] [-u user] command [args]\n");
+       fprintf(stderr, "usage: doas [-nsv] [-C config] [-u user] command [args]\n");
        exit(1);
 }
 
@@ -45,9 +53,11 @@ arraylen(const char **arr)
 {
        size_t cnt = 0;
 
-       while (*arr) {
-               cnt++;
-               arr++;
+       if (arr) {
+               while (*arr) {
+                       cnt++;
+                       arr++;
+               }
        }
        return cnt;
 }
@@ -162,10 +172,7 @@ parseconfig(const char *filename, int checkperms)
 
        yyfp = fopen(filename, "r");
        if (!yyfp) {
-               if (checkperms)
-                       fprintf(stderr, "doas is not enabled.\n");
-               else
-                       warn("could not open config file");
+               warn("could not open config file");
                exit(1);
        }
 
@@ -188,10 +195,10 @@ parseconfig(const char *filename, int checkperms)
  * Copy the environment variables in safeset from oldenvp to envp.
  */
 static int
-copyenvhelper(const char **oldenvp, const char **safeset, int nsafe,
+copyenvhelper(const char **oldenvp, const char **safeset, size_t nsafe,
     char **envp, int ei)
 {
-       int i;
+       size_t i;
 
        for (i = 0; i < nsafe; i++) {
                const char **oe = oldenvp;
@@ -224,8 +231,8 @@ copyenv(const char **oldenvp, struct rule *rule)
        char **envp;
        const char **extra;
        int ei;
-       int nsafe, nbad;
-       int nextras = 0;
+       size_t nsafe, nbad;
+       size_t nextras = 0;
 
        /* if there was no envvar whitelist, pass all except badset ones */
        nbad = arraylen(badset);
@@ -331,9 +338,11 @@ main(int argc, char **argv, char **envp)
        int i, ch;
        int sflag = 0;
        int nflag = 0;
+       int vflag = 0;
 
        uid = getuid();
-       while ((ch = getopt(argc, argv, "C:nsu:")) != -1) {
+
+       while ((ch = getopt(argc, argv, "C:nsu:v")) != -1) {
                switch (ch) {
                case 'C':
                        confpath = optarg;
@@ -348,6 +357,9 @@ main(int argc, char **argv, char **envp)
                case 's':
                        sflag = 1;
                        break;
+               case 'v':
+                       vflag = 1;
+                       break;
                default:
                        usage();
                        break;
@@ -356,13 +368,15 @@ main(int argc, char **argv, char **envp)
        argv += optind;
        argc -= optind;
 
+       if (vflag)
+               version();
+
        if (confpath) {
                if (sflag)
                        usage();
        } else if ((!sflag && !argc) || (sflag && argc))
                usage();
 
-       uid = getuid();
        pw = getpwuid(uid);
        if (!pw)
                err(1, "getpwuid failed");