]> granicus.if.org Git - sudo/commitdiff
Pass in output function to lbuf_init() instead of writing to stdout.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 18 Mar 2010 10:42:17 +0000 (06:42 -0400)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 18 Mar 2010 10:42:17 +0000 (06:42 -0400)
A side effect is that the usage info can now go to stderr as it should.

include/lbuf.h
plugins/sudoers/auth/sudo_auth.c
plugins/sudoers/check.c
plugins/sudoers/sudo_nss.c
plugins/sudoers/sudoers.c
plugins/sudoers/sudoers.h
src/lbuf.c
src/parse_args.c

index 302918aad94359924b3ebeaa8782286907c752d4..1a8e6dfdae6862e7ea147cc970a508ef1aa4fb7b 100644 (file)
  * Line buffer struct.
  */
 struct lbuf {
+    int (*output)(const char *);
     char *buf;
-    int continuation;
+    const char *continuation;
     int indent;
     int len;
     int size;
     int cols;
 };
 
-void lbuf_init         (struct lbuf *, char *, int, int, int);
-void lbuf_destroy      (struct lbuf *);
-void lbuf_append       (struct lbuf *, ...);
-void lbuf_append_quoted        (struct lbuf *, const char *, ...);
-void lbuf_print                (struct lbuf *);
+void lbuf_init(struct lbuf *, int (*)(const char *), int, const char *, int);
+void lbuf_destroy(struct lbuf *);
+void lbuf_append(struct lbuf *, ...);
+void lbuf_append_quoted(struct lbuf *, const char *, ...);
+void lbuf_print(struct lbuf *);
 
 #endif /* _SUDO_LBUF_H */
index 707c5cc87b011b81d8e1b0cc1802fa809537eec8..94ca3bb351f9bee1795f4bcff6012268a28e0cd8 100644 (file)
@@ -52,8 +52,6 @@
 #include "sudo_auth.h"
 #include "insults.h"
 
-sudo_conv_t sudo_conv;
-
 sudo_auth auth_switch[] = {
 #ifdef AUTH_STANDALONE
     AUTH_STANDALONE
index f913ff667837c7531b794bfb1afd4b54a342846a..161d86e5d859059ec413b53fea6d7cf03f49bef0 100644 (file)
@@ -75,8 +75,6 @@ static char *expand_prompt(char *, char *, char *);
 static void  lecture(int);
 static void  update_timestamp(char *, char *);
 
-extern sudo_conv_t sudo_conv;
-
 /*
  * This function only returns if the user can successfully
  * verify who he/she is.
index c6e905a4e040b0c375461405f3f35b6378fa1992..df8509c1ae8ba396f64b7daeda1963bf273b0429 100644 (file)
@@ -224,10 +224,27 @@ reset_groups(pw)
 #endif
 }
 
+static int
+output(const char *buf)
+{
+    struct sudo_conv_message msg;
+    struct sudo_conv_reply repl;
+
+    /* Call conversation function */
+    memset(&msg, 0, sizeof(msg));
+    msg.msg_type = SUDO_CONV_INFO_MSG;
+    msg.msg = buf;
+    memset(&repl, 0, sizeof(repl));
+    if (sudo_conv(1, &msg, &repl) == -1)
+       return 0;
+    return (int)strlen(buf);
+}
+
 /*
  * Print out privileges for the specified user.
  * We only get here if the user is allowed to run something on this host.
  */
+/* XXX - conversation function or newlines in lbuf */
 void
 display_privs(snl, pw)
     struct sudo_nss_list *snl;
@@ -240,7 +257,7 @@ display_privs(snl, pw)
     /* Reset group vector so group matching works correctly. */
     reset_groups(pw);
 
-    lbuf_init(&lbuf, NULL, 4, 0, sudo_user.cols);
+    lbuf_init(&lbuf, output, 4, NULL, sudo_user.cols);
 
     /* Display defaults from all sources. */
     count = 0;
index 7820135a2ab7d85940ce7b3a8fac8a945f6d628c..986cef6cd97c432db6b361c1cf20ecffec12306a 100644 (file)
@@ -152,6 +152,8 @@ login_cap_t *lc;
 char *login_style;
 #endif /* HAVE_BSD_AUTH_H */
 sigaction_t saved_sa_int, saved_sa_quit, saved_sa_tstp;
+sudo_conv_t sudo_conv;
+
 static char *runas_user;
 static char *runas_group;
 static struct sudo_nss_list *snl;
index 46a3c78473d0190eca566f069212984bbe3ca76d..19ac90131070a1747e20873af2449683bbed409e 100644 (file)
@@ -307,6 +307,7 @@ extern struct passwd *auth_pw, *list_pw;
 extern int tgetpass_flags; /* XXX */
 extern int long_list;
 extern uid_t timestamp_uid;
+extern sudo_conv_t sudo_conv;
 #endif
 #ifndef errno
 extern int errno;
index 148278ded66ddc3114928aa947b3f25d27588982..4a7437ebe8cb50b192755e40ac9614dd2e563425 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007-2009 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2007-2010 Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
  */
 
 void
-lbuf_init(struct lbuf *lbuf, char *buf, int indent, int continuation, int cols)
+lbuf_init(struct lbuf *lbuf, int (*output)(const char *),
+    int indent, const char *continuation, int cols)
 {
+    lbuf->output = output;
     lbuf->continuation = continuation;
     lbuf->indent = indent;
     lbuf->cols = cols;
@@ -163,10 +165,10 @@ lbuf_append(struct lbuf *lbuf, ...)
 void
 lbuf_print(struct lbuf *lbuf)
 {
-    char *cp;
+    char *cp, save;
     int i, have, contlen;
 
-    contlen = lbuf->continuation ? 2 : 0;
+    contlen = lbuf->continuation ? strlen(lbuf->continuation) : 0;
 
     /* For very small widths just give up... */
     if (lbuf->cols <= lbuf->indent + contlen + 20) {
@@ -198,9 +200,13 @@ lbuf_print(struct lbuf *lbuf)
        if (cp != lbuf->buf) {
            /* indent continued lines */
            for (i = 0; i < lbuf->indent; i++)
-               putchar(' ');
+               lbuf->output(" ");
        }
-       fwrite(cp, need, 1, stdout);
+       /* NUL-terminate cp for the output function and restore afterwards */
+       save = cp[need];
+       cp[need] = '\0';
+       lbuf->output(cp);
+       cp[need] = save;
        cp = ep;
 
        /*
@@ -212,12 +218,10 @@ lbuf_print(struct lbuf *lbuf)
            do {
                cp++;
            } while (isspace((unsigned char)*cp));
-           if (lbuf->continuation) {
-               putchar(' ');
-               putchar(lbuf->continuation);
-           }
+           if (contlen)
+               lbuf->output(lbuf->continuation);
        }
-       putchar('\n');
+       lbuf->output("\n");
     }
 
 done:
index 41d0ed5cd31e34a26aeb76c1948e38ec6a5eba78..e970a9f041bdc88ccf7a1fed31e071819fa31e29 100644 (file)
@@ -373,6 +373,12 @@ parse_args(int argc, char **argv, int *nargc, char ***nargv, char ***settingsp,
     return(mode | flags);
 }
 
+static int
+usage_out(const char *buf)
+{
+    return fputs(buf, stderr);
+}
+
 /*
  * Give usage message and exit.
  * The actual usage strings are in sudo_usage.h for configure substitution.
@@ -404,7 +410,7 @@ usage(int exit_val)
      * tty width.
      */
     ulen = (int)strlen(getprogname()) + 8;
-    lbuf_init(&lbuf, NULL, ulen, 0, user_details.ts_cols);
+    lbuf_init(&lbuf, usage_out, ulen, NULL, user_details.ts_cols);
     for (i = 0; uvec[i] != NULL; i++) {
        lbuf_append(&lbuf, "usage: ", getprogname(), uvec[i], NULL);
        lbuf_print(&lbuf);