]> granicus.if.org Git - sudo/commitdiff
Add notbefore and notafter support to the backends.
authorTodd C. Miller <Todd.Miller@sudo.ws>
Sun, 28 Jan 2018 14:09:22 +0000 (07:09 -0700)
committerTodd C. Miller <Todd.Miller@sudo.ws>
Sun, 28 Jan 2018 14:09:22 +0000 (07:09 -0700)
plugins/sudoers/cvtsudoers_json.c
plugins/sudoers/cvtsudoers_ldif.c
plugins/sudoers/regress/sudoers/test19.json.ok

index 92ead63295d8538cc3fbcbf7df4108ea6505d667..e1556f32090b07281da129b60cee5243ffa59a0a 100644 (file)
@@ -27,6 +27,7 @@
 #endif /* HAVE_STRINGS_H */
 #include <unistd.h>
 #include <stdarg.h>
+#include <time.h>
 #include <ctype.h>
 
 #include "sudoers.h"
@@ -733,7 +734,9 @@ print_cmndspec_json(FILE *fp, struct cmndspec *cs, struct cmndspec **nextp,
     struct cmndspec *next = *nextp;
     struct json_value value;
     struct member *m;
+    struct tm *tp;
     bool last_one;
+    char timebuf[sizeof("20120727121554Z")];
     debug_decl(print_cmndspec_json, SUDOERS_DEBUG_UTIL)
 
     /* Open Cmnd_Spec object. */
@@ -765,7 +768,8 @@ print_cmndspec_json(FILE *fp, struct cmndspec *cs, struct cmndspec **nextp,
     }
 
     /* Print tags */
-    if (cs->timeout > 0 || TAGS_SET(cs->tags)) {
+    if (cs->timeout > 0 || cs->notbefore != UNSPEC || cs->notafter != UNSPEC ||
+       TAGS_SET(cs->tags)) {
        struct cmndtag tag = cs->tags;
 
        fprintf(fp, "%*s\"Options\": [\n", indent, "");
@@ -776,6 +780,35 @@ print_cmndspec_json(FILE *fp, struct cmndspec *cs, struct cmndspec **nextp,
            print_pair_json(fp, "{ ", "command_timeout", &value,
                TAGS_SET(tag) ? " },\n" : " }\n", indent);
        }
+       if (cs->notbefore != UNSPEC) {
+           if ((tp = gmtime(&cs->notbefore)) == NULL) {
+               sudo_warn(U_("unable to get GMT time"));
+           } else {
+               if (strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", tp) == 0) {
+                   sudo_warnx(U_("unable to format timestamp"));
+               } else {
+                   value.type = JSON_STRING;
+                   value.u.string = timebuf;
+                   print_pair_json(fp, "{ ", "notbefore", &value,
+                       (TAGS_SET(tag) || cs->notafter != UNSPEC) ?
+                       " },\n" : " }\n", indent);
+               }
+           }
+       }
+       if (cs->notafter != UNSPEC) {
+           if ((tp = gmtime(&cs->notafter)) == NULL) {
+               sudo_warn(U_("unable to get GMT time"));
+           } else {
+               if (strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", tp) == 0) {
+                   sudo_warnx(U_("unable to format timestamp"));
+               } else {
+                   value.type = JSON_STRING;
+                   value.u.string = timebuf;
+                   print_pair_json(fp, "{ ", "notafter", &value,
+                       TAGS_SET(tag) ?  " },\n" : " }\n", indent);
+               }
+           }
+       }
        if (tag.nopasswd != UNSPEC) {
            value.type = JSON_BOOL;
            value.u.boolean = !tag.nopasswd;
index 7282978cc2d817f88549d4b8f3de2339135dc1af..ef576e586c271634db6e3b33a034ee51c13f1d1e 100644 (file)
@@ -165,7 +165,9 @@ print_cmndspec_ldif(FILE *fp, struct cmndspec *cs, struct cmndspec **nextp)
 {
     struct cmndspec *next = *nextp;
     struct member *m;
+    struct tm *tp;
     bool last_one;
+    char timebuf[sizeof("20120727121554Z")];
     debug_decl(print_cmndspec_ldif, SUDOERS_DEBUG_UTIL)
 
     /* Print runasuserlist as sudoRunAsUser attributes */
@@ -184,6 +186,30 @@ print_cmndspec_ldif(FILE *fp, struct cmndspec *cs, struct cmndspec **nextp)
        }
     }
 
+    /* Print sudoNotBefore and sudoNotAfter attributes */
+    if (cs->notbefore != UNSPEC) {
+       if ((tp = gmtime(&cs->notbefore)) == NULL) {
+           sudo_warn(U_("unable to get GMT time"));
+       } else {
+           if (strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", tp) == 0) {
+               sudo_warnx(U_("unable to format timestamp"));
+           } else {
+               fprintf(fp, "sudoNotBefore: %s\n", timebuf);
+           }
+       }
+    }
+    if (cs->notafter != UNSPEC) {
+       if ((tp = gmtime(&cs->notafter)) == NULL) {
+           sudo_warn(U_("unable to get GMT time"));
+       } else {
+           if (strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", tp) == 0) {
+               sudo_warnx(U_("unable to format timestamp"));
+           } else {
+               fprintf(fp, "sudoNotAfter: %s\n", timebuf);
+           }
+       }
+    }
+
     /* Print tags as sudoOption attributes */
     if (cs->timeout > 0 || TAGS_SET(cs->tags)) {
        struct cmndtag tag = cs->tags;
@@ -378,7 +404,7 @@ convert_sudoers_ldif(const char *output_file, const char *base)
            sudo_fatalx(U_("The SUDOERS_BASE environment variable is not set"));
     }
 
-    if (strcmp(output_file, "-") != 0) {                     
+    if (strcmp(output_file, "-") != 0) {
        if ((output_fp = fopen(output_file, "w")) == NULL)
            sudo_fatal(U_("unable to open %s"), output_file);
     }
index e9cc9dd2f108946dfbe424a98bc59a7948e6e49a..1da8df4b0cec60258bedb15458ab4142711d73f8 100644 (file)
@@ -9,6 +9,10 @@
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083000Z" },
+                        { "notafter": "20170301083000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" },
                         { "command": "/bin/ls" }
@@ -25,6 +29,9 @@
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" },
                         { "command": "/bin/ls" }
@@ -41,6 +48,9 @@
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083018Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]
@@ -56,6 +66,9 @@
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214080000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]
@@ -71,6 +84,9 @@
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214082400Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]
             ],
             "Cmnd_Specs": [
                 {
+                    "Options": [
+                        { "notbefore": "20170214083000Z" }
+                    ],
                     "Commands": [
                         { "command": "/usr/bin/id" }
                     ]