From 38937dca5e5ec459473ba73a2b39574fc3bc9290 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 19 Mar 2010 07:04:34 -0400 Subject: [PATCH] Use embedded newlines in lbuf instead of multiple calls to lbuf_print. --- plugins/sudoers/ldap.c | 25 +++++++++---------------- plugins/sudoers/parse.c | 24 ++++++++---------------- plugins/sudoers/sudo_nss.c | 37 +++++++++++++++++++++++-------------- 3 files changed, 40 insertions(+), 46 deletions(-) diff --git a/plugins/sudoers/ldap.c b/plugins/sudoers/ldap.c index 18ab135e5..016c83113 100644 --- a/plugins/sudoers/ldap.c +++ b/plugins/sudoers/ldap.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003-2009 Todd C. Miller + * Copyright (c) 2003-2010 Todd C. Miller * * This code is derived from software contributed by Aaron Spangler. * @@ -1150,10 +1150,7 @@ sudo_ldap_display_defaults(nss, pw, lbuf) if (rc == LDAP_SUCCESS && (entry = ldap_first_entry(ld, result))) { bv = ldap_get_values_len(ld, entry, "sudoOption"); if (bv != NULL) { - if (lbuf->len == 0) - prefix = " "; - else - prefix = ", "; + prefix = " "; for (p = bv; *p != NULL; p++) { lbuf_append(lbuf, prefix, (*p)->bv_val, NULL); prefix = ", "; @@ -1257,8 +1254,8 @@ sudo_ldap_display_entry_short(ld, entry, lbuf) } ldap_value_free_len(bv); } + lbuf_append(lbuf, "\n", NULL); - lbuf_print(lbuf); /* forces a newline */ return(count); } @@ -1277,9 +1274,7 @@ sudo_ldap_display_entry_long(ld, entry, lbuf) /* extract the dn, only show the first rdn */ rdn = sudo_ldap_get_first_rdn(ld, entry); - lbuf_print(lbuf); /* force a newline */ - lbuf_append(lbuf, "LDAP Role: ", rdn ? rdn : "UNKNOWN", NULL); - lbuf_print(lbuf); + lbuf_append(lbuf, "\nLDAP Role: ", rdn ? rdn : "UNKNOWN", "\n", NULL); if (rdn) ldap_memfree(rdn); @@ -1297,7 +1292,7 @@ sudo_ldap_display_entry_long(ld, entry, lbuf) ldap_value_free_len(bv); } else lbuf_append(lbuf, def_runas_default, NULL); - lbuf_print(lbuf); + lbuf_append(lbuf, "\n", NULL); /* get the RunAsGroup Values from the entry */ bv = ldap_get_values_len(ld, entry, "sudoRunAsGroup"); @@ -1309,7 +1304,7 @@ sudo_ldap_display_entry_long(ld, entry, lbuf) lbuf_append(lbuf, (*p)->bv_val, NULL); } ldap_value_free_len(bv); - lbuf_print(lbuf); + lbuf_append(lbuf, "\n", NULL); } /* get the Option Values from the entry */ @@ -1322,17 +1317,15 @@ sudo_ldap_display_entry_long(ld, entry, lbuf) lbuf_append(lbuf, (*p)->bv_val, NULL); } ldap_value_free_len(bv); - lbuf_print(lbuf); + lbuf_append(lbuf, "\n", NULL); } /* get the Command Values from the entry */ bv = ldap_get_values_len(ld, entry, "sudoCommand"); if (bv != NULL) { - lbuf_append(lbuf, " Commands:", NULL); - lbuf_print(lbuf); + lbuf_append(lbuf, " Commands:\n", NULL); for (p = bv; *p != NULL; p++) { - lbuf_append(lbuf, "\t", (*p)->bv_val, NULL); - lbuf_print(lbuf); + lbuf_append(lbuf, "\t", (*p)->bv_val, "\n", NULL); count++; } ldap_value_free_len(bv); diff --git a/plugins/sudoers/parse.c b/plugins/sudoers/parse.c index d83ec2e13..3dfe7b846 100644 --- a/plugins/sudoers/parse.c +++ b/plugins/sudoers/parse.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004-2005, 2007-2009 Todd C. Miller + * Copyright (c) 2004-2005, 2007-2010 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -352,7 +352,7 @@ sudo_file_display_priv_short(pw, us, lbuf) sudo_file_append_cmnd(cs, &tags, lbuf); nfound++; } - lbuf_print(lbuf); /* forces a newline */ + lbuf_append(lbuf, "\n", NULL); } return(nfound); } @@ -376,9 +376,7 @@ sudo_file_display_priv_long(pw, us, lbuf) tags.setenv = UNSPEC; tags.nopasswd = UNSPEC; tags.transcript = UNSPEC; - lbuf_print(lbuf); /* force a newline */ - lbuf_append(lbuf, "Sudoers entry:", NULL); - lbuf_print(lbuf); + lbuf_append(lbuf, "\nSudoers entry:\n", NULL); tq_foreach_fwd(&priv->cmndlist, cs) { lbuf_append(lbuf, " RunAsUsers: ", NULL); if (!tq_empty(&cs->runasuserlist)) { @@ -393,7 +391,7 @@ sudo_file_display_priv_long(pw, us, lbuf) } else { lbuf_append(lbuf, pw->pw_name, NULL); } - lbuf_print(lbuf); + lbuf_append(lbuf, "\n", NULL); if (!tq_empty(&cs->runasgrouplist)) { lbuf_append(lbuf, " RunAsGroups: ", NULL); tq_foreach_fwd(&cs->runasgrouplist, m) { @@ -402,13 +400,11 @@ sudo_file_display_priv_long(pw, us, lbuf) print_member(lbuf, m->name, m->type, m->negated, RUNASALIAS); } - lbuf_print(lbuf); + lbuf_append(lbuf, "\n", NULL); } - lbuf_append(lbuf, " Commands: ", NULL); - lbuf_print(lbuf); - lbuf_append(lbuf, "\t", NULL); + lbuf_append(lbuf, " Commands:\n\t", NULL); sudo_file_append_cmnd(cs, &tags, lbuf); - lbuf_print(lbuf); + lbuf_append(lbuf, "\n", NULL); nfound++; } } @@ -455,11 +451,7 @@ sudo_file_display_defaults(nss, pw, lbuf) if (nss->handle == NULL) return(-1); - if (lbuf->len == 0) - prefix = " "; - else - prefix = ", "; - + prefix = " "; tq_foreach_fwd(&defaults, d) { switch (d->type) { case DEFAULTS_HOST: diff --git a/plugins/sudoers/sudo_nss.c b/plugins/sudoers/sudo_nss.c index df8509c1a..8ef352776 100644 --- a/plugins/sudoers/sudo_nss.c +++ b/plugins/sudoers/sudo_nss.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007-2009 Todd C. Miller + * Copyright (c) 2007-2010 Todd C. Miller * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -244,7 +244,6 @@ output(const char *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; @@ -260,32 +259,42 @@ display_privs(snl, pw) lbuf_init(&lbuf, output, 4, NULL, sudo_user.cols); /* Display defaults from all sources. */ + lbuf_append(&lbuf, "Matching Defaults entries for ", pw->pw_name, + " on this host:\n", NULL); count = 0; - tq_foreach_fwd(snl, nss) + tq_foreach_fwd(snl, nss) { count += nss->display_defaults(nss, pw, &lbuf); + } if (count) { - printf("Matching Defaults entries for %s on this host:\n", pw->pw_name); + lbuf_append(&lbuf, "\n\n", NULL); lbuf_print(&lbuf); - putchar('\n'); } /* Display Runas and Cmnd-specific defaults from all sources. */ + lbuf.len = 0; + lbuf_append(&lbuf, "Runas and Command-specific defaults for", pw->pw_name, + ":\n", NULL); count = 0; - tq_foreach_fwd(snl, nss) + tq_foreach_fwd(snl, nss) { count += nss->display_bound_defaults(nss, pw, &lbuf); + } if (count) { - printf("Runas and Command-specific defaults for %s:\n", pw->pw_name); + lbuf_append(&lbuf, "\n\n", NULL); lbuf_print(&lbuf); - putchar('\n'); } /* Display privileges from all sources. */ - printf("User %s may run the following commands on this host:\n", - pw->pw_name); - tq_foreach_fwd(snl, nss) - (void) nss->display_privs(nss, pw, &lbuf); - if (lbuf.len != 0) - lbuf_print(&lbuf); /* print remainder, if any */ + lbuf.len = 0; + lbuf_append(&lbuf, "User ", pw->pw_name, + " may run the following commands on this host:\n", NULL); + count = 0; + tq_foreach_fwd(snl, nss) { + count += nss->display_privs(nss, pw, &lbuf); + } + if (count) { + lbuf_print(&lbuf); + } + lbuf_destroy(&lbuf); } -- 2.40.0