]> granicus.if.org Git - sudo/commitdiff
Add print_error() function that uses the conversation function to
authorTodd C. Miller <Todd.Miller@courtesan.com>
Mon, 3 May 2010 20:53:05 +0000 (16:53 -0400)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Mon, 3 May 2010 20:53:05 +0000 (16:53 -0400)
print a variable number of error strings and use it in log_error().

plugins/sudoers/logging.c
plugins/sudoers/plugin_error.c
plugins/sudoers/sudoers.h

index e9c6830efefd9249d28fc58c768c4afe1029ccd4..7eaa9de1905f567244b09a476f7121720c501790 100644 (file)
@@ -275,23 +275,24 @@ log_denial(int status, int inform_user)
 
     /* Inform the user if they failed to authenticate.  */
     if (inform_user) {
-       if (ISSET(status, FLAG_NO_USER))
-           (void) fprintf(stderr, "%s is not in the sudoers file.  %s",
-               user_name, "This incident will be reported.\n");
-       else if (ISSET(status, FLAG_NO_HOST))
-           (void) fprintf(stderr, "%s is not allowed to run sudo on %s.  %s",
-               user_name, user_shost, "This incident will be reported.\n");
-       else if (ISSET(status, FLAG_NO_CHECK))
-           (void) fprintf(stderr, "Sorry, user %s may not run sudo on %s.\n",
-               user_name, user_shost);
-       else
-           (void) fprintf(stderr,
-               "Sorry, user %s is not allowed to execute '%s%s%s' as %s%s%s on %s.\n",
-               user_name, user_cmnd, user_args ? " " : "",
-               user_args ? user_args : "",
-               list_pw ? list_pw->pw_name : runas_pw ?
-               runas_pw->pw_name : user_name, runas_gr ? ":" : "",
-               runas_gr ? runas_gr->gr_name : "", user_host);
+       if (ISSET(status, FLAG_NO_USER)) {
+           print_error(2, user_name, " is not in the sudoers file.  "
+               "This incident will be reported.\n");
+       } else if (ISSET(status, FLAG_NO_HOST)) {
+           print_error(4, user_name, " is not allowed to run sudo on ",
+               user_shost, ".  This incident will be reported.\n");
+       } else if (ISSET(status, FLAG_NO_CHECK)) {
+           print_error(5, "Sorry, user ", user_name, " may not run sudo on ",
+               user_shost, ".\n");
+       } else {
+           print_error(13, "Sorry, user ", user_name,
+               " is not allowed to execute '", user_cmnd,
+               user_args ? " " : "", user_args ? user_args : "", "' ",
+               list_pw ? list_pw->pw_name :
+               runas_pw ? runas_pw->pw_name : user_name,
+               runas_gr ? ":" : "", runas_gr ? runas_gr->gr_name : "", " on ",
+               user_shost, ".\n");
+       }
     }
 
     /*
index d6e5af83b4a79d31f7c91901eaf25f38ecc0da14..8730ef904288d8c5598f5c4f027e12e00e7147ae 100644 (file)
@@ -36,41 +36,44 @@ extern sudo_conv_t sudo_conv;
 void
 error(int eval, const char *fmt, ...)
 {
-       va_list ap;
-       va_start(ap, fmt);
-       _warning(1, fmt, ap);
-       va_end(ap);
-       cleanup(0);
-       siglongjmp(error_jmp, eval);
+    va_list ap;
+
+    va_start(ap, fmt);
+    _warning(1, fmt, ap);
+    va_end(ap);
+    cleanup(0);
+    siglongjmp(error_jmp, eval);
 }
 
 void
 errorx(int eval, const char *fmt, ...)
 {
-       va_list ap;
-       va_start(ap, fmt);
-       _warning(0, fmt, ap);
-       va_end(ap);
-       cleanup(0);
-       siglongjmp(error_jmp, eval);
+    va_list ap;
+
+    va_start(ap, fmt);
+    _warning(0, fmt, ap);
+    va_end(ap);
+    cleanup(0);
+    siglongjmp(error_jmp, eval);
 }
 
 void
 warning(const char *fmt, ...)
 {
-       va_list ap;
-       va_start(ap, fmt);
-       _warning(1, fmt, ap);
-       va_end(ap);
+    va_list ap;
+
+    va_start(ap, fmt);
+    _warning(1, fmt, ap);
+    va_end(ap);
 }
 
 void
 warningx(const char *fmt, ...)
 {
-       va_list ap;
-       va_start(ap, fmt);
-       _warning(0, fmt, ap);
-       va_end(ap);
+    va_list ap;
+    va_start(ap, fmt);
+    _warning(0, fmt, ap);
+    va_end(ap);
 }
 
 static void
@@ -103,3 +106,28 @@ _warning(int use_errno, const char *fmt, va_list ap)
     memset(&repl, 0, sizeof(repl));
     sudo_conv(nmsgs, msg, repl);
 }
+
+void
+print_error(int nmsgs, ...)
+{
+    struct sudo_conv_message *msg;
+    struct sudo_conv_reply *repl;
+    va_list ap;
+    int i;
+
+    if (nmsgs <= 0)
+       return;
+
+    msg = emalloc2(nmsgs, sizeof(*msg));
+    repl = emalloc2(nmsgs, sizeof(*repl));
+    memset(repl, 0, nmsgs * sizeof(*repl));
+
+    va_start(ap, nmsgs);
+    for (i = 0; i < nmsgs; i++) {
+       msg[i].msg_type = SUDO_CONV_ERROR_MSG;
+       msg[i].msg = va_arg(ap, char *);
+    }
+    va_end(ap);
+
+    sudo_conv(nmsgs, msg, repl);
+}
index 6e9b93b57094306ba24a1e065838dc657c78f5e1..c2d54ecfc23c4fd43fb30150157cd746c20a4edd 100644 (file)
@@ -311,6 +311,9 @@ void cleanup(int);
 void set_fqdn(void);
 FILE *open_sudoers(const char *, int, int *);
 
+/* plugin_error.c */
+void print_error(int nmsgs, ...);
+
 #ifndef _SUDO_MAIN
 extern struct sudo_user sudo_user;
 extern struct passwd *auth_pw, *list_pw;