diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-26 16:18:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-26 16:18:36 +0000 |
commit | 6c3ea4f47ea280811a7fe53a22f7832e4533c9ec (patch) | |
tree | 3d7ed5da23b5dbf6f9e450dfb61642832249c31e /lib/getdef.c | |
parent | Adding upstream version 1:4.13+dfsg1. (diff) | |
download | shadow-upstream.tar.xz shadow-upstream.zip |
Adding upstream version 1:4.15.2.upstream/1%4.15.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/getdef.c')
-rw-r--r-- | lib/getdef.c | 119 |
1 files changed, 65 insertions, 54 deletions
diff --git a/lib/getdef.c b/lib/getdef.c index dcd1fe7..30f54ba 100644 --- a/lib/getdef.c +++ b/lib/getdef.c @@ -13,6 +13,7 @@ #include "prototypes.h" #include "defines.h" +#include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> @@ -20,8 +21,14 @@ #ifdef USE_ECONF #include <libeconf.h> #endif + +#include "alloc.h" +#include "atoi/str2i.h" #include "getdef.h" #include "shadowlog_internal.h" +#include "string/sprintf.h" + + /* * A configuration item definition. */ @@ -33,7 +40,6 @@ struct itemdef { #define PAMDEFS \ {"CHFN_AUTH", NULL}, \ {"CHSH_AUTH", NULL}, \ - {"CRACKLIB_DICTPATH", NULL}, \ {"ENV_HZ", NULL}, \ {"ENVIRON_FILE", NULL}, \ {"ENV_TZ", NULL}, \ @@ -132,10 +138,8 @@ static struct itemdef def_table[] = { #ifndef USE_PAM PAMDEFS #endif -#ifdef USE_SYSLOG {"SYSLOG_SG_ENAB", NULL}, {"SYSLOG_SU_ENAB", NULL}, -#endif #ifdef WITH_TCB {"TCB_AUTH_GROUP", NULL}, {"TCB_SYMLINKS", NULL}, @@ -173,7 +177,7 @@ static const char* def_fname = LOGINDEFS; /* login config defs file */ static bool def_loaded = false; /* are defs already loaded? */ /* local function prototypes */ -static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *); +static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *, const char *); static void def_load (void); @@ -192,8 +196,8 @@ static void def_load (void); def_load (); } - d = def_find (item); - return ((NULL == d)? (const char *) NULL : d->value); + d = def_find (item, NULL); + return (NULL == d) ? NULL : d->value; } @@ -211,7 +215,7 @@ bool getdef_bool (const char *item) def_load (); } - d = def_find (item); + d = def_find (item, NULL); if ((NULL == d) || (NULL == d->value)) { return false; } @@ -237,21 +241,21 @@ int getdef_num (const char *item, int dflt) def_load (); } - d = def_find (item); + d = def_find (item, NULL); if ((NULL == d) || (NULL == d->value)) { return dflt; } - if ( (getlong (d->value, &val) == 0) + if ( (str2sl(&val, d->value) == -1) || (val > INT_MAX) - || (val < INT_MIN)) { + || (val < -1)) { fprintf (shadow_logfd, _("configuration error - cannot parse %s value: '%s'"), item, d->value); return dflt; } - return (int) val; + return val; } @@ -272,12 +276,12 @@ unsigned int getdef_unum (const char *item, unsigned int dflt) def_load (); } - d = def_find (item); + d = def_find (item, NULL); if ((NULL == d) || (NULL == d->value)) { return dflt; } - if ( (getlong (d->value, &val) == 0) + if ( (str2sl(&val, d->value) == -1) || (val < 0) || (val > INT_MAX)) { fprintf (shadow_logfd, @@ -286,7 +290,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt) return dflt; } - return (unsigned int) val; + return val; } @@ -307,12 +311,12 @@ long getdef_long (const char *item, long dflt) def_load (); } - d = def_find (item); + d = def_find (item, NULL); if ((NULL == d) || (NULL == d->value)) { return dflt; } - if (getlong (d->value, &val) == 0) { + if (str2sl(&val, d->value) == -1 || val < -1) { fprintf (shadow_logfd, _("configuration error - cannot parse %s value: '%s'"), item, d->value); @@ -339,12 +343,12 @@ unsigned long getdef_ulong (const char *item, unsigned long dflt) def_load (); } - d = def_find (item); + d = def_find (item, NULL); if ((NULL == d) || (NULL == d->value)) { return dflt; } - if (getulong (d->value, &val) == 0) { + if (str2ul(&val, d->value) == -1) { fprintf (shadow_logfd, _("configuration error - cannot parse %s value: '%s'"), item, d->value); @@ -359,7 +363,7 @@ unsigned long getdef_ulong (const char *item, unsigned long dflt) * (also used when loading the initial defaults) */ -int putdef_str (const char *name, const char *value) +int putdef_str (const char *name, const char *value, const char *srcfile) { struct itemdef *d; char *cp; @@ -372,10 +376,9 @@ int putdef_str (const char *name, const char *value) * Locate the slot to save the value. If this parameter * is unknown then "def_find" will print an err message. */ - d = def_find (name); - if (NULL == d) { + d = def_find (name, srcfile); + if (NULL == d) return -1; - } /* * Save off the value. @@ -399,9 +402,12 @@ int putdef_str (const char *name, const char *value) * * Search through a table of configurable items to locate the * specified configuration option. + * + * If srcfile is not NULL, and the item is not found, then report an error saying + * the unknown item was used in this file. */ -static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name) +static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name, const char *srcfile) { struct itemdef *ptr; @@ -427,10 +433,11 @@ static /*@observer@*/ /*@null@*/struct itemdef *def_find (const char *name) fprintf (shadow_logfd, _("configuration error - unknown item '%s' (notify administrator)\n"), name); - SYSLOG ((LOG_CRIT, "unknown configuration item `%s'", name)); + if (srcfile != NULL) + SYSLOG ((LOG_CRIT, "shadow: unknown configuration item '%s' in '%s'", name, srcfile)); out: - return (struct itemdef *) NULL; + return NULL; } /* @@ -442,21 +449,12 @@ out: void setdef_config_file (const char* file) { #ifdef USE_ECONF - size_t len; - char* cp; - - len = strlen(file) + strlen(sysconfdir) + 2; - cp = malloc(len); - if (cp == NULL) - exit (13); - snprintf(cp, len, "%s/%s", file, sysconfdir); + char *cp; + + xasprintf(&cp, "%s/%s", file, sysconfdir); sysconfdir = cp; #ifdef VENDORDIR - len = strlen(file) + strlen(vendordir) + 2; - cp = malloc(len); - if (cp == NULL) - exit (13); - snprintf(cp, len, "%s/%s", file, vendordir); + xasprintf(&cp, "%s/%s", file, vendordir); vendordir = cp; #endif #else @@ -470,18 +468,13 @@ void setdef_config_file (const char* file) * Loads the user-configured options from the default configuration file */ +#ifdef USE_ECONF static void def_load (void) { -#ifdef USE_ECONF econf_file *defs_file = NULL; econf_err error; char **keys; size_t key_number; -#else - int i; - FILE *fp; - char buf[1024], *name, *value, *s; -#endif /* * Set the initialized flag. @@ -489,8 +482,6 @@ static void def_load (void) */ def_loaded = true; -#ifdef USE_ECONF - error = econf_readDirs (&defs_file, vendordir, sysconfdir, "login", "defs", " \t", "#"); if (error) { if (error == ECONF_NOFILE) @@ -510,7 +501,12 @@ static void def_load (void) for (size_t i = 0; i < key_number; i++) { char *value; - econf_getStringValue(defs_file, NULL, keys[i], &value); + error = econf_getStringValue(defs_file, NULL, keys[i], &value); + if (error) { + SYSLOG ((LOG_CRIT, "failed reading key %zu from econf [%s]", + i, econf_errString(error))); + exit (EXIT_FAILURE); + } /* * Store the value in def_table. @@ -519,12 +515,27 @@ static void def_load (void) * The error was already reported to the user and to * syslog. The tools will just use their default values. */ - (void)putdef_str (keys[i], value); + (void)putdef_str (keys[i], value, econf_getPath(defs_file)); + + free(value); } econf_free (keys); econf_free (defs_file); -#else +} +#else /* USE_ECONF */ +static void def_load (void) +{ + int i; + FILE *fp; + char buf[1024], *name, *value, *s; + + /* + * Set the initialized flag. + * (do it early to prevent recursion in putdef_str()) + */ + def_loaded = true; + /* * Open the configuration definitions file. */ @@ -542,12 +553,12 @@ static void def_load (void) /* * Go through all of the lines in the file. */ - while (fgets (buf, (int) sizeof (buf), fp) != NULL) { + while (fgets (buf, sizeof (buf), fp) != NULL) { /* * Trim trailing whitespace. */ - for (i = (int) strlen (buf) - 1; i >= 0; --i) { + for (i = (ptrdiff_t) strlen (buf) - 1; i >= 0; --i) { if (!isspace (buf[i])) { break; } @@ -577,7 +588,7 @@ static void def_load (void) * The error was already reported to the user and to * syslog. The tools will just use their default values. */ - (void)putdef_str (name, value); + (void)putdef_str (name, value, def_fname); } if (ferror (fp) != 0) { @@ -588,8 +599,8 @@ static void def_load (void) } (void) fclose (fp); -#endif } +#endif /* USE_ECONF */ #ifdef CKDEFS @@ -602,7 +613,7 @@ int main (int argc, char **argv) def_load (); for (i = 0; i < NUMDEFS; ++i) { - d = def_find (def_table[i].name); + d = def_find (def_table[i].name, NULL); if (NULL == d) { printf ("error - lookup '%s' failed\n", def_table[i].name); |