diff --git a/lib/gshadow.c b/lib/gshadow.c index d1ca70706..97eae6250 100644 --- a/lib/gshadow.c +++ b/lib/gshadow.c @@ -15,6 +15,7 @@ #include #include +#include #include #include "alloc/malloc.h" @@ -68,34 +69,27 @@ void endsgent (void) } /*@observer@*//*@null@*/struct sgrp * -sgetsgent(const char *string) +sgetsgent(const char *s) { - static char *sgrbuf = NULL; - static size_t sgrbuflen = 0; + static char *dup = NULL; char *fields[FIELDS]; char *cp; int i; - size_t len = strlen (string) + 1; - if (len > sgrbuflen) { - char *buf = REALLOC(sgrbuf, len, char); - if (NULL == buf) - return NULL; - - sgrbuf = buf; - sgrbuflen = len; - } + free(dup); + dup = strdup(s); + if (dup == NULL) + return NULL; - strcpy (sgrbuf, string); - stpsep(sgrbuf, "\n"); + stpsep(dup, "\n"); /* * There should be exactly 4 colon separated fields. Find * all 4 of them and save the starting addresses in fields[]. */ - for (cp = sgrbuf, i = 0; (i < FIELDS) && (NULL != cp); i++) + for (cp = dup, i = 0; (i < FIELDS) && (NULL != cp); i++) fields[i] = strsep(&cp, ":"); /* diff --git a/lib/sgetgrent.c b/lib/sgetgrent.c index eeeed4b6f..da5601ffc 100644 --- a/lib/sgetgrent.c +++ b/lib/sgetgrent.c @@ -11,10 +11,11 @@ #ident "$Id$" -#include -#include #include +#include +#include #include +#include #include "alloc/malloc.h" #include "alloc/reallocf.h" @@ -64,30 +65,23 @@ list(char *s) } -struct group *sgetgrent (const char *buf) +struct group * +sgetgrent(const char *s) { - static char *grpbuf = NULL; - static size_t size = 0; + static char *dup = NULL; static char *grpfields[NFIELDS]; static struct group grent; int i; char *cp; - if (strlen (buf) + 1 > size) { - /* no need to use realloc() here - just free it and - allocate a larger block */ - free (grpbuf); - size = strlen (buf) + 1000; /* at least: strlen(buf) + 1 */ - grpbuf = MALLOC(size, char); - if (grpbuf == NULL) { - size = 0; - return NULL; - } - } - strcpy (grpbuf, buf); - stpsep(grpbuf, "\n"); + free(dup); + dup = strdup(s); + if (dup == NULL) + return NULL; + + stpsep(dup, "\n"); - for (cp = grpbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++) + for (cp = dup, i = 0; (i < NFIELDS) && (NULL != cp); i++) grpfields[i] = strsep(&cp, ":"); if (i < NFIELDS || streq(grpfields[2], "") || cp != NULL) { diff --git a/lib/sgetpwent.c b/lib/sgetpwent.c index b13d5bc54..691979a19 100644 --- a/lib/sgetpwent.c +++ b/lib/sgetpwent.c @@ -11,16 +11,18 @@ #ident "$Id$" -#include -#include #include +#include +#include #include +#include #include "atoi/getnum.h" #include "defines.h" #include "prototypes.h" #include "shadowlog_internal.h" #include "string/strcmp/streq.h" +#include "string/strtok/stpsep.h" #define NFIELDS 7 @@ -38,33 +40,27 @@ * compilation glarp to improve on this in the future. */ struct passwd * -sgetpwent(const char *buf) +sgetpwent(const char *s) { + static char *dup = NULL; static struct passwd pwent; - static char pwdbuf[PASSWD_ENTRY_MAX_LENGTH]; int i; char *cp; char *fields[NFIELDS]; - /* - * Copy the string to a static buffer so the pointers into - * the password structure remain valid. - */ + free(dup); + dup = strdup(s); + if (dup == NULL) + return NULL; - if (strlen (buf) >= sizeof pwdbuf) { - fprintf (shadow_logfd, - "%s: Too long passwd entry encountered, file corruption?\n", - shadow_progname); - return NULL; /* fail if too long */ - } - strcpy (pwdbuf, buf); + stpsep(dup, "\n"); /* * Save a pointer to the start of each colon separated * field. The fields are converted into NUL terminated strings. */ - for (cp = pwdbuf, i = 0; (i < NFIELDS) && (NULL != cp); i++) + for (cp = dup, i = 0; (i < NFIELDS) && (NULL != cp); i++) fields[i] = strsep(&cp, ":"); /* something at the end, columns over shot */ diff --git a/lib/sgetspent.c b/lib/sgetspent.c index f34853ec6..c4f742e0a 100644 --- a/lib/sgetspent.c +++ b/lib/sgetspent.c @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -36,34 +37,27 @@ * sgetspent - convert string in shadow file format to (struct spwd *) */ struct spwd * -sgetspent(const char *string) +sgetspent(const char *s) { - static char spwbuf[PASSWD_ENTRY_MAX_LENGTH]; + static char *dup = NULL; static struct spwd spwd; char *fields[FIELDS]; char *cp; int i; - /* - * Copy string to local buffer. It has to be tokenized and we - * have to do that to our private copy. - */ + free(dup); + dup = strdup(s); + if (dup == NULL) + return NULL; - if (strlen (string) >= sizeof spwbuf) { - fprintf (shadow_logfd, - "%s: Too long passwd entry encountered, file corruption?\n", - shadow_progname); - return NULL; /* fail if too long */ - } - strcpy (spwbuf, string); - stpsep(spwbuf, "\n"); + stpsep(dup, "\n"); /* * Tokenize the string into colon separated fields. Allow up to * FIELDS different fields. */ - for (cp = spwbuf, i = 0; cp != NULL && i < FIELDS; i++) + for (cp = dup, i = 0; cp != NULL && i < FIELDS; i++) fields[i] = strsep(&cp, ":"); if (i == (FIELDS - 1))