From: Todd C. Miller Date: Mon, 31 Dec 2007 20:04:46 +0000 (+0000) Subject: Refactor line reading into a separate function, sudo_parseln(), X-Git-Tag: SUDO_1_7_0~283 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=926dcd0bccf2fbcf2a0486a13606b68846e5bb92;p=sudo Refactor line reading into a separate function, sudo_parseln(), which removes comments, leading/trailing whitespace and newlines. May want to rethink the use of sudo_parseln() for /etc/ldap.secret --- diff --git a/env.c b/env.c index b371b710c..baa467493 100644 --- a/env.c +++ b/env.c @@ -42,8 +42,6 @@ #ifdef HAVE_UNISTD_H # include #endif /* HAVE_UNISTD_H */ -#include -#include #include #include "sudo.h" @@ -52,10 +50,6 @@ __unused static const char rcsid[] = "$Sudo$"; #endif /* lint */ -#ifndef LINE_MAX -# define LINE_MAX 2048 -#endif - /* * Flags used in rebuild_env() */ @@ -754,8 +748,7 @@ read_env_file(path) const char *path; { FILE *fp; - char *cp, buf[LINE_MAX]; - size_t len; + char *cp; if ((fp = fopen(path, "r")) == NULL) return; @@ -764,17 +757,9 @@ read_env_file(path) if (env.envp != environ) sync_env(); - while (fgets(buf, sizeof(buf), fp) != NULL) { - len = strlen(buf); - - /* Trim leading and trailing whitespace/newline */ - while (len > 0 && isspace(buf[len - 1])) - buf[--len] = '\0'; - for (cp = buf; isblank(*cp); cp++) - continue; - + while ((cp = sudo_parseln(fp)) != NULL) { /* Skip blank or comment lines */ - if (*cp == '\0' || *cp == '#') + if (*cp == '\0') continue; /* Must be of the form name=value */ diff --git a/fileops.c b/fileops.c index cb1399e23..7b1ee6dff 100644 --- a/fileops.c +++ b/fileops.c @@ -27,6 +27,15 @@ # include #endif /* HAVE_FLOCK */ #include +#ifdef HAVE_STRING_H +# include +#else +# ifdef HAVE_STRINGS_H +# include +# endif +#endif /* HAVE_STRING_H */ +#include +#include #ifdef HAVE_UNISTD_H # include #endif /* HAVE_UNISTD_H */ @@ -40,6 +49,10 @@ #include "sudo.h" +#ifndef LINE_MAX +# define LINE_MAX 2048 +#endif + #ifndef lint __unused static const char rcsid[] = "$Sudo$"; #endif /* lint */ @@ -139,3 +152,30 @@ lock_file(fd, lockit) #endif } #endif + +/* + * Read a line of input, remove comments and strip off leading + * and trailing spaces. Returns static storage that is reused. + */ +char * +sudo_parseln(fp) + FILE *fp; +{ + size_t len; + char *cp = NULL; + static char buf[LINE_MAX]; + + if (fgets(buf, sizeof(buf), fp) != NULL) { + /* Remove comments */ + if ((cp = strchr(buf, '#')) != NULL) + *cp = '\0'; + + /* Trim leading and trailing whitespace/newline */ + len = strlen(buf); + while (len > 0 && isspace(buf[len - 1])) + buf[--len] = '\0'; + for (cp = buf; isblank(*cp); cp++) + continue; + } + return(cp); +} diff --git a/ldap.c b/ldap.c index 2e0d7274c..2434ac8ee 100644 --- a/ldap.c +++ b/ldap.c @@ -45,7 +45,6 @@ # include #endif /* HAVE_UNISTD_H */ #include -#include #include #include #include @@ -80,10 +79,6 @@ __unused static const char rcsid[] = "$Sudo$"; #endif /* lint */ -#ifndef LINE_MAX -# define LINE_MAX 2048 -#endif - #ifndef LDAP_OPT_SUCCESS # define LDAP_OPT_SUCCESS LDAP_SUCCESS #endif @@ -658,8 +653,8 @@ _atobool(s) int sudo_ldap_read_config() { - FILE *f; - char buf[LINE_MAX], *c, *keyword, *value; + FILE *fp; + char *cp, *keyword, *value; struct ldap_config_table *cur; /* defaults */ @@ -671,38 +666,24 @@ sudo_ldap_read_config() ldap_conf.use_sasl = -1; ldap_conf.rootuse_sasl = -1; - if ((f = fopen(_PATH_LDAP_CONF, "r")) == NULL) + if ((fp = fopen(_PATH_LDAP_CONF, "r")) == NULL) return(FALSE); - while (fgets(buf, sizeof(buf), f)) { - /* ignore text after comment character */ - if ((c = strchr(buf, '#')) != NULL) - *c = '\0'; - - /* skip leading whitespace */ - for (c = buf; isspace((unsigned char) *c); c++) - /* nothing */; - - if (*c == '\0' || *c == '\n') + while ((cp = sudo_parseln(fp)) != NULL) { + if (*cp == '\0') continue; /* skip empty line */ - /* properly terminate keyword string */ - keyword = c; - while (*c && !isspace((unsigned char) *c)) - c++; - if (*c) - *c++ = '\0'; /* terminate keyword */ + /* split into keyword and value */ + keyword = cp; + while (*cp && !isblank((unsigned char) *cp)) + cp++; + if (*cp) + *cp++ = '\0'; /* terminate keyword */ /* skip whitespace before value */ - while (isspace((unsigned char) *c)) - c++; - value = c; - - /* trim whitespace after value */ - while (*c) - c++; /* wind to end */ - while (--c > value && isspace((unsigned char) *c)) - *c = '\0'; + while (isblank((unsigned char) *cp)) + cp++; + value = cp; /* Look up keyword in config table. */ for (cur = ldap_conf_table; cur->conf_str != NULL; cur++) { @@ -723,7 +704,7 @@ sudo_ldap_read_config() } } } - fclose(f); + fclose(fp); if (!ldap_conf.host) ldap_conf.host = "localhost"; @@ -810,21 +791,16 @@ sudo_ldap_read_config() /* If rootbinddn set, read in /etc/ldap.secret if it exists. */ if (ldap_conf.rootbinddn) { - if ((f = fopen(_PATH_LDAP_SECRET, "r")) != NULL) { - if (fgets(buf, sizeof(buf), f) != NULL) { - /* removing trailing newlines */ - for (c = buf; *c != '\0'; c++) - continue; - while (--c > buf && *c == '\n') - *c = '\0'; + if ((fp = fopen(_PATH_LDAP_SECRET, "r")) != NULL) { + if ((cp = sudo_parseln(fp)) != NULL) { /* copy to bindpw and binddn */ efree(ldap_conf.bindpw); - ldap_conf.bindpw = estrdup(buf); + ldap_conf.bindpw = estrdup(cp); efree(ldap_conf.binddn); ldap_conf.binddn = ldap_conf.rootbinddn; ldap_conf.rootbinddn = NULL; } - fclose(f); + fclose(fp); } } #ifdef HAVE_LDAP_SASL_INTERACTIVE_BIND_S @@ -836,8 +812,8 @@ sudo_ldap_read_config() strncasecmp(ldap_conf.krb5_ccname, "WRFILE:", 7) == 0) { value = ldap_conf.krb5_ccname + (ldap_conf.krb5_ccname[4] == ':' ? 5 : 7); - if ((f = fopen(value, "r")) != NULL) { - fclose(f); + if ((fp = fopen(value, "r")) != NULL) { + fclose(fp); } else { /* Can't open it, just ignore the entry. */ efree(ldap_conf.krb5_ccname); diff --git a/sudo.h b/sudo.h index 965f98705..c03059de9 100644 --- a/sudo.h +++ b/sudo.h @@ -278,6 +278,7 @@ FILE *open_sudoers __P((const char *, int *)); void display_privs __P((struct sudo_nss_list *, struct passwd *)); int display_cmnd __P((struct sudo_nss_list *, struct passwd *)); int get_ttycols __P((void)); +char *sudo_parseln __P((FILE *)); void sudo_setenv __P((const char *, const char *, int)); void sudo_unsetenv __P((const char *)); void sudo_setgrent __P((void)); diff --git a/sudo_nss.c b/sudo_nss.c index 747ba2b1c..1b014f9fb 100644 --- a/sudo_nss.c +++ b/sudo_nss.c @@ -34,8 +34,6 @@ # include # endif #endif /* HAVE_STRING_H */ -#include -#include #include "sudo.h" @@ -43,10 +41,6 @@ __unused static const char rcsid[] = "$Sudo$"; #endif /* lint */ -#ifndef LINE_MAX -# define LINE_MAX 2048 -#endif - extern struct sudo_nss sudo_nss_file; #ifdef HAVE_LDAP @@ -62,9 +56,8 @@ struct sudo_nss_list * read_nss(path) const char *path; { - size_t len; FILE *fp; - char *cp, buf[LINE_MAX]; + char *cp; int saw_files = FALSE; int saw_ldap = FALSE; int got_match = FALSE; @@ -73,20 +66,9 @@ read_nss(path) if ((fp = fopen(path, "r")) == NULL) goto nomatch; - while (fgets(buf, sizeof(buf), fp) != NULL) { - /* Remove comments */ - if ((cp = strchr(buf, '#')) != NULL) - *cp = '\0'; - - /* Trim leading and trailing whitespace/newline */ - len = strlen(buf); - while (len > 0 && isspace(buf[len - 1])) - buf[--len] = '\0'; - for (cp = buf; isblank(*cp); cp++) - continue; - + while ((cp = sudo_parseln(fp)) != NULL) { /* Skip blank or comment lines */ - if (*cp == '\0' || *cp == '#') + if (*cp == '\0') continue; /* Look for a line starting with "sudoers:" */