Date: Mon, 19 Jun 2006 16:20:43 GMT From: Michael Bushkov <bushman@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 99617 for review Message-ID: <200606191620.k5JGKh3O079954@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=99617 Change 99617 by bushman@bushman_nss_ldap_cached on 2006/06/19 16:19:46 Nss-modules code deleted from libc sources (only "cache" source is left untouched). All nsdispatch(3) calls modified to use only "cache" source or dynamically linked nss-modules. Affected files ... .. //depot/projects/soc2006/nss_ldap_cached/src/lib/Makefile#4 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/gen/getgrent.c#3 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/gen/getpwent.c#2 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/Makefile.inc#3 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/gethostnamadr.c#4 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/getnetnamadr.c#2 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/getproto.c#2 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/getprotoent.c#2 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/getprotoname.c#2 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/getservent.c#2 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/net/netdb_private.h#4 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/rpc/getrpcent.c#2 edit Differences ... ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/Makefile#4 (text+ko) ==== @@ -14,6 +14,8 @@ # libmd must be built before libatm, libopie, libradius, and libtacplus. # libncurses must be built before libdialog, libedit and libreadline. # libnetgraph must be built before libbsnmp/modules/snmp_netgraph. +# libnssutil must be built before nss_files/nss_dns/nss_nis/nss_compat +# libnss_files must be built before libnss_compat # libopie must be built before libpam. # libradius must be built before libpam. # librpcsvc must be built before libpam. @@ -25,7 +27,7 @@ # Otherwise, the SUBDIR list should be in alphabetical order. SUBDIR= ${_csu} libbsm libcom_err libcrypt libkvm msun libmd libncurses \ - nss_files nss_dns nss_icmp ${_nss_nis}\ + libnssutil nss_files nss_compat nss_dns nss_icmp ${_nss_nis}\ libnetgraph libradius librpcsvc libsbuf libtacplus libutil \ ${_libypclnt} libalias libarchive ${_libatm} \ libbegemot ${_libbluetooth} libbsnmp libbz2 libc ${_libc_r} \ ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/libc/gen/getgrent.c#3 (text+ko) ==== @@ -35,16 +35,8 @@ #include "namespace.h" #include <sys/param.h> -#ifdef YP -#include <rpc/rpc.h> -#include <rpcsvc/yp_prot.h> -#include <rpcsvc/ypclnt.h> -#endif #include <ctype.h> #include <errno.h> -#ifdef HESIOD -#include <hesiod.h> -#endif #include <grp.h> #include <nsswitch.h> #include <pthread.h> @@ -64,9 +56,6 @@ enum constants { GRP_STORAGE_INITIAL = 1 << 10, /* 1 KByte */ GRP_STORAGE_MAX = 1 << 20, /* 1 MByte */ - SETGRENT = 1, - ENDGRENT = 2, - HESIOD_NAME_MAX = 256, }; static const ns_src defaultsrc[] = { @@ -74,13 +63,6 @@ { NULL, 0 } }; -int __gr_match_entry(const char *, size_t, enum nss_lookup_type, - const char *, gid_t); -int __gr_parse_entry(char *, size_t, struct group *, char *, size_t, - int *); - -static int is_comment_line(const char *, size_t); - union key { const char *name; gid_t gid; @@ -94,55 +76,6 @@ static int wrap_getgrent_r(union key, struct group *, char *, size_t, struct group **); -struct files_state { - FILE *fp; - int stayopen; -}; -static void files_endstate(void *); -NSS_TLS_HANDLING(files); -static int files_setgrent(void *, void *, va_list); -static int files_group(void *, void *, va_list); - - -#ifdef HESIOD -struct dns_state { - long counter; -}; -static void dns_endstate(void *); -NSS_TLS_HANDLING(dns); -static int dns_setgrent(void *, void *, va_list); -static int dns_group(void *, void *, va_list); -#endif - - -#ifdef YP -struct nis_state { - char domain[MAXHOSTNAMELEN]; - int done; - char *key; - int keylen; -}; -static void nis_endstate(void *); -NSS_TLS_HANDLING(nis); -static int nis_setgrent(void *, void *, va_list); -static int nis_group(void *, void *, va_list); -#endif - -struct compat_state { - FILE *fp; - int stayopen; - char *name; - enum _compat { - COMPAT_MODE_OFF = 0, - COMPAT_MODE_ALL, - COMPAT_MODE_NAME - } compat; -}; -static void compat_endstate(void *); -NSS_TLS_HANDLING(compat); -static int compat_setgrent(void *, void *, va_list); -static int compat_group(void *, void *, va_list); - #ifdef NS_CACHING static int grp_id_func(char *, size_t *, va_list, void *); static int grp_marshal_func(char *, size_t *, void *, va_list, void *); @@ -373,14 +306,6 @@ #endif static const ns_dtab dtab[] = { - { NSSRC_FILES, files_setgrent, (void *)SETGRENT }, -#ifdef HESIOD - { NSSRC_DNS, dns_setgrent, (void *)SETGRENT }, -#endif -#ifdef YP - { NSSRC_NIS, nis_setgrent, (void *)SETGRENT }, -#endif - { NSSRC_COMPAT, compat_setgrent, (void *)SETGRENT }, #ifdef NS_CACHING NS_CACHE_CB(&cache_info) #endif @@ -401,14 +326,6 @@ #endif static const ns_dtab dtab[] = { - { NSSRC_FILES, files_setgrent, (void *)SETGRENT }, -#ifdef HESIOD - { NSSRC_DNS, dns_setgrent, (void *)SETGRENT }, -#endif -#ifdef YP - { NSSRC_NIS, nis_setgrent, (void *)SETGRENT }, -#endif - { NSSRC_COMPAT, compat_setgrent, (void *)SETGRENT }, #ifdef NS_CACHING NS_CACHE_CB(&cache_info) #endif @@ -430,14 +347,6 @@ #endif static const ns_dtab dtab[] = { - { NSSRC_FILES, files_setgrent, (void *)ENDGRENT }, -#ifdef HESIOD - { NSSRC_DNS, dns_setgrent, (void *)ENDGRENT }, -#endif -#ifdef YP - { NSSRC_NIS, nis_setgrent, (void *)ENDGRENT }, -#endif - { NSSRC_COMPAT, compat_setgrent, (void *)ENDGRENT }, #ifdef NS_CACHING NS_CACHE_CB(&cache_info) #endif @@ -458,14 +367,6 @@ #endif static const ns_dtab dtab[] = { - { NSSRC_FILES, files_group, (void *)nss_lt_all }, -#ifdef HESIOD - { NSSRC_DNS, dns_group, (void *)nss_lt_all }, -#endif -#ifdef YP - { NSSRC_NIS, nis_group, (void *)nss_lt_all }, -#endif - { NSSRC_COMPAT, compat_group, (void *)nss_lt_all }, #ifdef NS_CACHING NS_CACHE_CB(&cache_info) #endif @@ -496,14 +397,6 @@ #endif static const ns_dtab dtab[] = { - { NSSRC_FILES, files_group, (void *)nss_lt_name }, -#ifdef HESIOD - { NSSRC_DNS, dns_group, (void *)nss_lt_name }, -#endif -#ifdef YP - { NSSRC_NIS, nis_group, (void *)nss_lt_name }, -#endif - { NSSRC_COMPAT, compat_group, (void *)nss_lt_name }, #ifdef NS_CACHING NS_CACHE_CB(&cache_info) #endif @@ -534,14 +427,6 @@ #endif static const ns_dtab dtab[] = { - { NSSRC_FILES, files_group, (void *)nss_lt_id }, -#ifdef HESIOD - { NSSRC_DNS, dns_group, (void *)nss_lt_id }, -#endif -#ifdef YP - { NSSRC_NIS, nis_group, (void *)nss_lt_id }, -#endif - { NSSRC_COMPAT, compat_group, (void *)nss_lt_id }, #ifdef NS_CACHING NS_CACHE_CB(&cache_info) #endif @@ -650,787 +535,3 @@ key.gid = 0; /* not used */ return (getgr(wrap_getgrent_r, key)); } - - -static int -is_comment_line(const char *s, size_t n) -{ - const char *eom; - - eom = &s[n]; - - for (; s < eom; s++) - if (*s == '#' || !isspace((unsigned char)*s)) - break; - return (*s == '#' || s == eom); -} - - -/* - * files backend - */ -static void -files_endstate(void *p) -{ - - if (p == NULL) - return; - if (((struct files_state *)p)->fp != NULL) - fclose(((struct files_state *)p)->fp); - free(p); -} - - -static int -files_setgrent(void *retval, void *mdata, va_list ap) -{ - struct files_state *st; - int rv, stayopen; - - rv = files_getstate(&st); - if (rv != 0) - return (NS_UNAVAIL); - switch ((enum constants)mdata) { - case SETGRENT: - stayopen = va_arg(ap, int); - if (st->fp != NULL) - rewind(st->fp); - else if (stayopen) - st->fp = fopen(_PATH_GROUP, "r"); - break; - case ENDGRENT: - if (st->fp != NULL) { - fclose(st->fp); - st->fp = NULL; - } - break; - default: - break; - } - return (NS_UNAVAIL); -} - - -static int -files_group(void *retval, void *mdata, va_list ap) -{ - struct files_state *st; - enum nss_lookup_type how; - const char *name, *line; - struct group *grp; - gid_t gid; - char *buffer; - size_t bufsize, linesize; - off_t pos; - int rv, stayopen, *errnop; - - name = NULL; - gid = (gid_t)-1; - how = (enum nss_lookup_type)mdata; - switch (how) { - case nss_lt_name: - name = va_arg(ap, const char *); - break; - case nss_lt_id: - gid = va_arg(ap, gid_t); - break; - case nss_lt_all: - break; - default: - return (NS_NOTFOUND); - } - grp = va_arg(ap, struct group *); - buffer = va_arg(ap, char *); - bufsize = va_arg(ap, size_t); - errnop = va_arg(ap, int *); - *errnop = files_getstate(&st); - if (*errnop != 0) - return (NS_UNAVAIL); - if (st->fp == NULL && - ((st->fp = fopen(_PATH_GROUP, "r")) == NULL)) { - *errnop = errno; - return (NS_UNAVAIL); - } - if (how == nss_lt_all) - stayopen = 1; - else { - rewind(st->fp); - stayopen = st->stayopen; - } - rv = NS_NOTFOUND; - pos = ftello(st->fp); - while ((line = fgetln(st->fp, &linesize)) != NULL) { - if (line[linesize-1] == '\n') - linesize--; - rv = __gr_match_entry(line, linesize, how, name, gid); - if (rv != NS_SUCCESS) - continue; - /* We need room at least for the line, a string NUL - * terminator, alignment padding, and one (char *) - * pointer for the member list terminator. - */ - if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { - fseeko(st->fp, pos, SEEK_SET); - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - pos = ftello(st->fp); - memcpy(buffer, line, linesize); - buffer[linesize] = '\0'; - rv = __gr_parse_entry(buffer, linesize, grp, - &buffer[linesize + 1], bufsize - linesize - 1, errnop); - if (rv & NS_TERMINATE) - break; - } - if (!stayopen && st->fp != NULL) { - fclose(st->fp); - st->fp = NULL; - } - if (rv == NS_SUCCESS && retval != NULL) - *(struct group **)retval = grp; - return (rv); -} - - -#ifdef HESIOD -/* - * dns backend - */ -static void -dns_endstate(void *p) -{ - - free(p); -} - - -static int -dns_setgrent(void *retval, void *cb_data, va_list ap) -{ - struct dns_state *st; - int rv; - - rv = dns_getstate(&st); - if (rv != 0) - return (NS_UNAVAIL); - st->counter = 0; - return (NS_UNAVAIL); -} - - -static int -dns_group(void *retval, void *mdata, va_list ap) -{ - char buf[HESIOD_NAME_MAX]; - struct dns_state *st; - struct group *grp; - const char *name, *label; - void *ctx; - char *buffer, **hes; - size_t bufsize, adjsize, linesize; - gid_t gid; - enum nss_lookup_type how; - int rv, *errnop; - - ctx = NULL; - hes = NULL; - name = NULL; - gid = (gid_t)-1; - how = (enum nss_lookup_type)mdata; - switch (how) { - case nss_lt_name: - name = va_arg(ap, const char *); - break; - case nss_lt_id: - gid = va_arg(ap, gid_t); - break; - case nss_lt_all: - break; - } - grp = va_arg(ap, struct group *); - buffer = va_arg(ap, char *); - bufsize = va_arg(ap, size_t); - errnop = va_arg(ap, int *); - *errnop = dns_getstate(&st); - if (*errnop != 0) - return (NS_UNAVAIL); - if (hesiod_init(&ctx) != 0) { - *errnop = errno; - rv = NS_UNAVAIL; - goto fin; - } - do { - rv = NS_NOTFOUND; - switch (how) { - case nss_lt_name: - label = name; - break; - case nss_lt_id: - if (snprintf(buf, sizeof(buf), "%lu", - (unsigned long)gid) >= sizeof(buf)) - goto fin; - label = buf; - break; - case nss_lt_all: - if (st->counter < 0) - goto fin; - if (snprintf(buf, sizeof(buf), "group-%ld", - st->counter++) >= sizeof(buf)) - goto fin; - label = buf; - break; - } - hes = hesiod_resolve(ctx, label, - how == nss_lt_id ? "gid" : "group"); - if ((how == nss_lt_id && hes == NULL && - (hes = hesiod_resolve(ctx, buf, "group")) == NULL) || - hes == NULL) { - if (how == nss_lt_all) - st->counter = -1; - if (errno != ENOENT) - *errnop = errno; - goto fin; - } - rv = __gr_match_entry(hes[0], strlen(hes[0]), how, name, gid); - if (rv != NS_SUCCESS) { - hesiod_free_list(ctx, hes); - hes = NULL; - continue; - } - /* We need room at least for the line, a string NUL - * terminator, alignment padding, and one (char *) - * pointer for the member list terminator. - */ - adjsize = bufsize - _ALIGNBYTES - sizeof(char *); - linesize = strlcpy(buffer, hes[0], adjsize); - if (linesize >= adjsize) { - *errnop = ERANGE; - rv = NS_RETURN; - goto fin; - } - hesiod_free_list(ctx, hes); - hes = NULL; - rv = __gr_parse_entry(buffer, linesize, grp, - &buffer[linesize + 1], bufsize - linesize - 1, errnop); - } while (how == nss_lt_all && !(rv & NS_TERMINATE)); -fin: - if (hes != NULL) - hesiod_free_list(ctx, hes); - if (ctx != NULL) - hesiod_end(ctx); - if (rv == NS_SUCCESS && retval != NULL) - *(struct group **)retval = grp; - return (rv); -} -#endif /* HESIOD */ - - -#ifdef YP -/* - * nis backend - */ -static void -nis_endstate(void *p) -{ - - if (p == NULL) - return; - free(((struct nis_state *)p)->key); - free(p); -} - - -static int -nis_setgrent(void *retval, void *cb_data, va_list ap) -{ - struct nis_state *st; - int rv; - - rv = nis_getstate(&st); - if (rv != 0) - return (NS_UNAVAIL); - st->done = 0; - free(st->key); - st->key = NULL; - return (NS_UNAVAIL); -} - - -static int -nis_group(void *retval, void *mdata, va_list ap) -{ - char *map; - struct nis_state *st; - struct group *grp; - const char *name; - char *buffer, *key, *result; - size_t bufsize; - gid_t gid; - enum nss_lookup_type how; - int *errnop, keylen, resultlen, rv; - - name = NULL; - gid = (gid_t)-1; - how = (enum nss_lookup_type)mdata; - switch (how) { - case nss_lt_name: - name = va_arg(ap, const char *); - map = "group.byname"; - break; - case nss_lt_id: - gid = va_arg(ap, gid_t); - map = "group.bygid"; - break; - case nss_lt_all: - map = "group.byname"; - break; - } - grp = va_arg(ap, struct group *); - buffer = va_arg(ap, char *); - bufsize = va_arg(ap, size_t); - errnop = va_arg(ap, int *); - *errnop = nis_getstate(&st); - if (*errnop != 0) - return (NS_UNAVAIL); - if (st->domain[0] == '\0') { - if (getdomainname(st->domain, sizeof(st->domain)) != 0) { - *errnop = errno; - return (NS_UNAVAIL); - } - } - result = NULL; - do { - rv = NS_NOTFOUND; - switch (how) { - case nss_lt_name: - if (strlcpy(buffer, name, bufsize) >= bufsize) - goto erange; - break; - case nss_lt_id: - if (snprintf(buffer, bufsize, "%lu", - (unsigned long)gid) >= bufsize) - goto erange; - break; - case nss_lt_all: - if (st->done) - goto fin; - break; - } - result = NULL; - if (how == nss_lt_all) { - if (st->key == NULL) - rv = yp_first(st->domain, map, &st->key, - &st->keylen, &result, &resultlen); - else { - key = st->key; - keylen = st->keylen; - st->key = NULL; - rv = yp_next(st->domain, map, key, keylen, - &st->key, &st->keylen, &result, - &resultlen); - free(key); - } - if (rv != 0) { - free(result); - free(st->key); - st->key = NULL; - if (rv == YPERR_NOMORE) { - st->done = 1; - rv = NS_NOTFOUND; - } else - rv = NS_UNAVAIL; - goto fin; - } - } else { - rv = yp_match(st->domain, map, buffer, strlen(buffer), - &result, &resultlen); - if (rv == YPERR_KEY) { - rv = NS_NOTFOUND; - continue; - } else if (rv != 0) { - free(result); - rv = NS_UNAVAIL; - continue; - } - } - /* We need room at least for the line, a string NUL - * terminator, alignment padding, and one (char *) - * pointer for the member list terminator. - */ - if (resultlen >= bufsize - _ALIGNBYTES - sizeof(char *)) - goto erange; - memcpy(buffer, result, resultlen); - buffer[resultlen] = '\0'; - free(result); - rv = __gr_match_entry(buffer, resultlen, how, name, gid); - if (rv == NS_SUCCESS) - rv = __gr_parse_entry(buffer, resultlen, grp, - &buffer[resultlen+1], bufsize - resultlen - 1, - errnop); - } while (how == nss_lt_all && !(rv & NS_TERMINATE)); -fin: - if (rv == NS_SUCCESS && retval != NULL) - *(struct group **)retval = grp; - return (rv); -erange: - *errnop = ERANGE; - return (NS_RETURN); -} -#endif /* YP */ - - - -/* - * compat backend - */ -static void -compat_endstate(void *p) -{ - struct compat_state *st; - - if (p == NULL) - return; - st = (struct compat_state *)p; - free(st->name); - if (st->fp != NULL) - fclose(st->fp); - free(p); -} - - -static int -compat_setgrent(void *retval, void *mdata, va_list ap) -{ - static const ns_src compatsrc[] = { -#ifdef YP - { NSSRC_NIS, NS_SUCCESS }, -#endif - { NULL, 0 } - }; - ns_dtab dtab[] = { -#ifdef HESIOD - { NSSRC_DNS, dns_setgrent, NULL }, -#endif -#ifdef YP - { NSSRC_NIS, nis_setgrent, NULL }, -#endif - { NULL, NULL, NULL } - }; - struct compat_state *st; - int rv, stayopen; - -#define set_setent(x, y) do { \ - int i; \ - \ - for (i = 0; i < (sizeof(x)/sizeof(x[0])) - 1; i++) \ - x[i].mdata = (void *)y; \ -} while (0) - - rv = compat_getstate(&st); - if (rv != 0) - return (NS_UNAVAIL); - switch ((enum constants)mdata) { - case SETGRENT: - stayopen = va_arg(ap, int); - if (st->fp != NULL) - rewind(st->fp); - else if (stayopen) - st->fp = fopen(_PATH_GROUP, "r"); - set_setent(dtab, mdata); - (void)_nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "setgrent", - compatsrc, 0); - break; - case ENDGRENT: - if (st->fp != NULL) { - fclose(st->fp); - st->fp = NULL; - } - set_setent(dtab, mdata); - (void)_nsdispatch(NULL, dtab, NSDB_GROUP_COMPAT, "endgrent", - compatsrc, 0); - break; - default: - break; - } - st->compat = COMPAT_MODE_OFF; - free(st->name); - st->name = NULL; - return (NS_UNAVAIL); -#undef set_setent -} - - -static int -compat_group(void *retval, void *mdata, va_list ap) -{ - static const ns_src compatsrc[] = { -#ifdef YP - { NSSRC_NIS, NS_SUCCESS }, -#endif - { NULL, 0 } - }; - ns_dtab dtab[] = { -#ifdef YP - { NSSRC_NIS, nis_group, NULL }, -#endif -#ifdef HESIOD - { NSSRC_DNS, dns_group, NULL }, -#endif - { NULL, NULL, NULL } - }; - struct compat_state *st; - enum nss_lookup_type how; - const char *name, *line; - struct group *grp; - gid_t gid; - char *buffer, *p; - void *discard; - size_t bufsize, linesize; - off_t pos; - int rv, stayopen, *errnop; - -#define set_lookup_type(x, y) do { \ - int i; \ - \ - for (i = 0; i < (sizeof(x)/sizeof(x[0])) - 1; i++) \ - x[i].mdata = (void *)y; \ -} while (0) - - name = NULL; - gid = (gid_t)-1; - how = (enum nss_lookup_type)mdata; - switch (how) { - case nss_lt_name: - name = va_arg(ap, const char *); - break; - case nss_lt_id: - gid = va_arg(ap, gid_t); - break; - case nss_lt_all: - break; - default: - return (NS_NOTFOUND); - } - grp = va_arg(ap, struct group *); - buffer = va_arg(ap, char *); - bufsize = va_arg(ap, size_t); - errnop = va_arg(ap, int *); - *errnop = compat_getstate(&st); - if (*errnop != 0) - return (NS_UNAVAIL); - if (st->fp == NULL && - ((st->fp = fopen(_PATH_GROUP, "r")) == NULL)) { - *errnop = errno; - rv = NS_UNAVAIL; - goto fin; - } - if (how == nss_lt_all) - stayopen = 1; - else { - rewind(st->fp); - stayopen = st->stayopen; - } -docompat: - switch (st->compat) { - case COMPAT_MODE_ALL: - set_lookup_type(dtab, how); - switch (how) { - case nss_lt_all: - rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, - "getgrent_r", compatsrc, grp, buffer, bufsize, - errnop); - break; - case nss_lt_id: - rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, - "getgrgid_r", compatsrc, gid, grp, buffer, bufsize, - errnop); - break; - case nss_lt_name: - rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, - "getgrnam_r", compatsrc, name, grp, buffer, - bufsize, errnop); - break; - } - if (rv & NS_TERMINATE) - goto fin; - st->compat = COMPAT_MODE_OFF; - break; - case COMPAT_MODE_NAME: - set_lookup_type(dtab, nss_lt_name); - rv = _nsdispatch(&discard, dtab, NSDB_GROUP_COMPAT, - "getgrnam_r", compatsrc, st->name, grp, buffer, bufsize, - errnop); - switch (rv) { - case NS_SUCCESS: - switch (how) { - case nss_lt_name: - if (strcmp(name, grp->gr_name) != 0) - rv = NS_NOTFOUND; - break; - case nss_lt_id: - if (gid != grp->gr_gid) - rv = NS_NOTFOUND; - break; - default: - break; - } - break; - case NS_RETURN: - goto fin; - default: - break; - } - free(st->name); - st->name = NULL; - st->compat = COMPAT_MODE_OFF; - if (rv == NS_SUCCESS) - goto fin; - break; - default: - break; - } - rv = NS_NOTFOUND; - pos = ftello(st->fp); - while ((line = fgetln(st->fp, &linesize)) != NULL) { - if (line[linesize-1] == '\n') - linesize--; - if (linesize > 2 && line[0] == '+') { - p = memchr(&line[1], ':', linesize); - if (p == NULL || p == &line[1]) - st->compat = COMPAT_MODE_ALL; - else { - st->name = malloc(p - line); - if (st->name == NULL) { - syslog(LOG_ERR, - "getgrent memory allocation failure"); - *errnop = ENOMEM; - rv = NS_UNAVAIL; - break; - } - memcpy(st->name, &line[1], p - line - 1); - st->name[p - line - 1] = '\0'; - st->compat = COMPAT_MODE_NAME; - } - goto docompat; - } - rv = __gr_match_entry(line, linesize, how, name, gid); - if (rv != NS_SUCCESS) - continue; - /* We need room at least for the line, a string NUL - * terminator, alignment padding, and one (char *) - * pointer for the member list terminator. - */ - if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { - fseeko(st->fp, pos, SEEK_SET); - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - pos = ftello(st->fp); - memcpy(buffer, line, linesize); - buffer[linesize] = '\0'; - rv = __gr_parse_entry(buffer, linesize, grp, - &buffer[linesize + 1], bufsize - linesize - 1, errnop); - if (rv & NS_TERMINATE) - break; - } -fin: - if (!stayopen && st->fp != NULL) { - fclose(st->fp); - st->fp = NULL; - } - if (rv == NS_SUCCESS && retval != NULL) - *(struct group **)retval = grp; - return (rv); -#undef set_lookup_type -} - - -/* - * common group line matching and parsing - */ -int -__gr_match_entry(const char *line, size_t linesize, enum nss_lookup_type how, - const char *name, gid_t gid) -{ - size_t namesize; - const char *p, *eol; - char *q; - unsigned long n; - int i, needed; - - if (linesize == 0 || is_comment_line(line, linesize)) - return (NS_NOTFOUND); - switch (how) { - case nss_lt_name: needed = 1; break; - case nss_lt_id: needed = 2; break; - default: needed = 2; break; - } - eol = &line[linesize]; - for (p = line, i = 0; i < needed && p < eol; p++) - if (*p == ':') - i++; - if (i < needed) - return (NS_NOTFOUND); - switch (how) { - case nss_lt_name: - namesize = strlen(name); - if (namesize + 1 == (size_t)(p - line) && - memcmp(line, name, namesize) == 0) - return (NS_SUCCESS); - break; - case nss_lt_id: - n = strtoul(p, &q, 10); - if (q < eol && *q == ':' && gid == (gid_t)n) - return (NS_SUCCESS); - break; - case nss_lt_all: - return (NS_SUCCESS); - default: - break; - } - return (NS_NOTFOUND); -} - - -int -__gr_parse_entry(char *line, size_t linesize, struct group *grp, char *membuf, - size_t membufsize, int *errnop) -{ - char *s_gid, *s_mem, *p, **members; - unsigned long n; - int maxmembers; - - memset(grp, 0, sizeof(*grp)); - members = (char **)_ALIGN(membuf); - membufsize -= (char *)members - membuf; - maxmembers = membufsize / sizeof(*members); - if (maxmembers <= 0 || - (grp->gr_name = strsep(&line, ":")) == NULL || - grp->gr_name[0] == '\0' || - (grp->gr_passwd = strsep(&line, ":")) == NULL || - (s_gid = strsep(&line, ":")) == NULL || - s_gid[0] == '\0') - return (NS_NOTFOUND); - s_mem = line; - n = strtoul(s_gid, &s_gid, 10); >>> TRUNCATED FOR MAIL (1000 lines) <<<
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200606191620.k5JGKh3O079954>