Date: Tue, 31 Oct 2006 11:14:30 GMT From: Michael Bushkov <bushman@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 108816 for review Message-ID: <200610311114.k9VBEU4v070716@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=108816 Change 108816 by bushman@bushman_nss_ldap_cached on 2006/10/31 11:14:26 nss_compat and nss_files functionalitites were separated to simplify the build Affected files ... .. //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/Makefile#6 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/compat_serv.c#4 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/compat_serv.h#3 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/nss_compat.c#6 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_files/files_serv.c#5 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_files/files_serv.h#3 edit .. //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_files/nss_files.c#12 edit Differences ... ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/Makefile#6 (text+ko) ==== @@ -8,8 +8,6 @@ SHLIBDIR?= /lib SRCS+= nss_compat.c compat_group.c compat_passwd.c compat_serv.c -# NOTE: hack with nss_files's file_serv.c is used -SRCS+= ${.CURDIR}/../nss_files/files_serv.c CFLAGS+=-I${.CURDIR} -I${.CURDIR}/../libc/gen -I${.CURDIR}/../libc/include\ -I${.CURDIR}/../libc/net -I${.CURDIR}/../libnssutil ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/compat_serv.c#4 (text+ko) ==== @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2006 Michael Bushkov <bushman@rsu.ru> + * Copyright (c) 2006 Michael Bushkov <bushman@freebsd.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,11 +25,237 @@ * */ +#include <sys/param.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <errno.h> +#include <limits.h> +#include <netdb.h> #include <nsswitch.h> +#include "namespace.h" +#include <pthread.h> +#include <pthread_np.h> +#include "un-namespace.h" +#include <stdio.h> +#include <string.h> #include <stdlib.h> -#include "../nss_files/files_serv.h" +#include <unistd.h> +#include "compat_serv.h" #include "nss_compat.h" - +#include "nss_tls.h" +#include "servunpack.h" + +struct compat_state +{ + FILE *fp; + int stayopen; + + int compat_mode_active; +}; +static void compat_endstate(void *); +NSS_TLS_HANDLING(compat); + +static void +compat_endstate(void *p) +{ + FILE * f; + + if (p == NULL) + return; + + f = ((struct compat_state *)p)->fp; + if (f != NULL) + fclose(f); + + free(p); +} + +int +__compat_servent(void *retval, void *mdata, va_list ap) +{ + static const ns_src compat_src[] = { +#ifdef YP + { NSSRC_NIS, NS_SUCCESS }, +#endif + { NULL, 0 } + }; + ns_dtab compat_dtab[] = { + { NULL, NULL, NULL } + }; + + struct compat_state *st; + int rv; + int stayopen; + + char *name; + char *proto; + int port; + + struct servent *serv; + char *buffer; + size_t bufsize; + int *errnop; + + char **aliases; + int aliases_size; + size_t linesize; + char *line; + char **cp; + + name = NULL; + proto = NULL; + switch ((enum nss_lookup_type)mdata) { + case nss_lt_name: + name = va_arg(ap, char *); + proto = va_arg(ap, char *); + break; + case nss_lt_id: + port = va_arg(ap, int); + proto = va_arg(ap, char *); + break; + case nss_lt_all: + break; + default: + return NS_NOTFOUND; + }; + + serv = va_arg(ap, struct servent *); + 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->compat_mode_active = 0; + + if (st->fp == NULL && (st->fp = fopen(_PATH_SERVICES, "r")) == NULL) { + *errnop = errno; + return (NS_UNAVAIL); + } + + if ((enum nss_lookup_type)mdata == nss_lt_all) + stayopen = 1; + else { + rewind(st->fp); + stayopen = st->stayopen; + } + + rv = NS_NOTFOUND; + do { + if (!st->compat_mode_active) { + if ((line = fgetln(st->fp, &linesize)) == NULL) { + *errnop = errno; + rv = NS_RETURN; + break; + } + + if (*line=='+') + st->compat_mode_active = 1; + else { + if (bufsize <= linesize + _ALIGNBYTES + + sizeof(char *)) { + *errnop = ERANGE; + rv = NS_RETURN; + break; + } + aliases = (char **)_ALIGN(&buffer[linesize+1]); + aliases_size = (buffer + bufsize - + (char *)aliases) / sizeof(char *); + if (aliases_size < 1) { + *errnop = ERANGE; + rv = NS_RETURN; + break; + } + + memcpy(buffer, line, linesize); + buffer[linesize] = '\0'; + } + } + + if (st->compat_mode_active != 0) { + switch ((enum nss_lookup_type)mdata) { + case nss_lt_name: + rv = _nsdispatch(retval, compat_dtab, + NSDB_SERVICES_COMPAT, "getservbyname_r", + compat_src, name, proto, serv, buffer, + bufsize, errnop); + break; + case nss_lt_id: + rv = _nsdispatch(retval, compat_dtab, + NSDB_SERVICES_COMPAT, "getservbyport_r", + compat_src, port, proto, serv, buffer, + bufsize, errnop); + break; + case nss_lt_all: + rv = _nsdispatch(retval, compat_dtab, + NSDB_SERVICES_COMPAT, "getservent_r", + compat_src, serv, buffer, bufsize, errnop); + break; + } + + if (!(rv & NS_TERMINATE) || + (enum nss_lookup_type)mdata != nss_lt_all) + st->compat_mode_active = 0; + + continue; + } + + rv = __servent_unpack(buffer, serv, aliases, aliases_size, + errnop); + if (rv != 0) { + if (*errnop == 0) { + rv = NS_NOTFOUND; + continue; + } + else { + rv = NS_RETURN; + break; + } + } + + rv = NS_NOTFOUND; + switch ((enum nss_lookup_type)mdata) { + case nss_lt_name: + if (strcmp(name, serv->s_name) == 0) + goto gotname; + for (cp = serv->s_aliases; *cp; cp++) + if (strcmp(name, *cp) == 0) + goto gotname; + + continue; + gotname: + if (proto == 0 || strcmp(serv->s_proto, proto) == 0) + rv = NS_SUCCESS; + break; + case nss_lt_id: + if (port != serv->s_port) + continue; + + if (proto == 0 || strcmp(serv->s_proto, proto) == 0) + rv = NS_SUCCESS; + break; + case nss_lt_all: + rv = NS_SUCCESS; + break; + } + + } while (!(rv & NS_TERMINATE)); + + if (!stayopen && st->fp != NULL) { + fclose(st->fp); + st->fp = NULL; + } + + if ((rv == NS_SUCCESS) && (retval != NULL)) + *(struct servent **)retval=serv; + + return (rv); +} + int __compat_setservent(void *retval, void *mdata, va_list ap) { @@ -42,23 +268,40 @@ ns_dtab compat_dtab[] = { { NULL, NULL, NULL } }; + + struct compat_state *st; + int rv; int f; - (void)__files_setservent(retval, mdata, ap); + rv = compat_getstate(&st); + if (rv != 0) + return (NS_UNAVAIL); switch ((enum nss_ent_type)mdata) { case nss_set_ent: f = va_arg(ap,int); + if (st->fp == NULL) + st->fp = fopen(_PATH_SERVICES, "r"); + else + rewind(st->fp); + st->stayopen |= f; + (void)nsdispatch(retval, compat_dtab, NSDB_SERVICES_COMPAT, "setservent", compat_src, f); break; case nss_end_ent: + if (st->fp != NULL) { + fclose(st->fp); + st->fp = NULL; + } + st->stayopen = 0; (void)nsdispatch(retval, compat_dtab, NSDB_SERVICES_COMPAT, "endservent", compat_src); break; default: break; - } + }; + st->compat_mode_active = 0; return (NS_UNAVAIL); } ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/compat_serv.h#3 (text+ko) ==== @@ -26,4 +26,5 @@ * $FreeBSD$ */ +extern int __compat_servent(void *, void *, va_list); extern int __compat_setservent(void *, void *, va_list); ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_compat/nss_compat.c#6 (text+ko) ==== @@ -34,13 +34,8 @@ #include "compat_passwd.h" #include "compat_group.h" #include "compat_serv.h" -#include "../nss_files/files_serv.h" #include "nss_compat.h" -static struct servent_mdata getservbyname_mdata = { nss_lt_name, 1 }; -static struct servent_mdata getservbyport_mdata = { nss_lt_id, 1 }; -static struct servent_mdata getservent_mdata = { nss_lt_all, 1 }; - static ns_mtab methods[] = { {NSDB_GROUP, "getgrnam_r", __compat_group, (void *)nss_lt_name}, {NSDB_GROUP, "getgrgid_r", __compat_group, (void *)nss_lt_id}, @@ -54,14 +49,13 @@ {NSDB_PASSWD, "endpwent", __compat_setpwent, (void *)nss_end_ent}, {NSDB_PASSWD, "setpwent", __compat_setpwent, (void *)nss_set_ent}, - {NSDB_SERVICES, "getservbyname_r", __files_servent, - (void *)&getservbyname_mdata}, - {NSDB_SERVICES, "getservbyport_r", __files_servent, - (void *)&getservbyport_mdata}, - {NSDB_SERVICES, "getservent_r", __files_servent, - (void *)&getservent_mdata}, + {NSDB_SERVICES, "getservbyname_r", __compat_servent, + (void *)nss_lt_name}, + {NSDB_SERVICES, "getservbyport_r", __compat_servent, + (void *)nss_lt_id}, + {NSDB_SERVICES, "getservent_r", __compat_servent, (void *)nss_lt_all}, {NSDB_SERVICES, "setservent", __compat_setservent, (void *)nss_set_ent}, - {NSDB_SERVICES, "endservent", __compat_setservent, (void *)nss_end_ent}, + {NSDB_SERVICES, "endservent", __compat_setservent, (void *)nss_end_ent} }; ns_mtab * ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_files/files_serv.c#5 (text+ko) ==== @@ -101,7 +101,6 @@ int rv; int stayopen; - struct servent_mdata *serv_mdata; char *name; char *proto; int port; @@ -119,8 +118,7 @@ name = NULL; proto = NULL; - serv_mdata = (struct servent_mdata *)mdata; - switch (serv_mdata->how) { + switch ((enum nss_lookup_type)mdata) { case nss_lt_name: name = va_arg(ap, char *); proto = va_arg(ap, char *); @@ -152,7 +150,7 @@ return (NS_UNAVAIL); } - if (serv_mdata->how == nss_lt_all) + if ((enum nss_lookup_type)mdata == nss_lt_all) stayopen = 1; else { rewind(st->fp); @@ -161,68 +159,36 @@ rv = NS_NOTFOUND; do { - if (!st->compat_mode_active) { - if ((line = fgetln(st->fp, &linesize)) == NULL) { - *errnop = errno; - rv = NS_RETURN; - break; - } + if ((line = fgetln(st->fp, &linesize)) == NULL) { + *errnop = errno; + rv = NS_RETURN; + break; + } + + if (*line=='+') + continue; - if (*line=='+') { - if (serv_mdata->compat_mode != 0) - st->compat_mode_active = 1; - } else { - if (bufsize <= linesize + _ALIGNBYTES + - sizeof(char *)) { - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - aliases = (char **)_ALIGN(&buffer[linesize+1]); - aliases_size = (buffer + bufsize - - (char *)aliases) / sizeof(char *); - if (aliases_size < 1) { - *errnop = ERANGE; - rv = NS_RETURN; - break; - } - - memcpy(buffer, line, linesize); - buffer[linesize] = '\0'; - } + if (bufsize <= linesize + _ALIGNBYTES + + sizeof(char *)) { + *errnop = ERANGE; + rv = NS_RETURN; + break; } - - if (st->compat_mode_active != 0) { - switch (serv_mdata->how) { - case nss_lt_name: - rv = _nsdispatch(retval, compat_dtab, - NSDB_SERVICES_COMPAT, "getservbyname_r", - compat_src, name, proto, serv, buffer, - bufsize, errnop); - break; - case nss_lt_id: - rv = _nsdispatch(retval, compat_dtab, - NSDB_SERVICES_COMPAT, "getservbyport_r", - compat_src, port, proto, serv, buffer, - bufsize, errnop); - break; - case nss_lt_all: - rv = _nsdispatch(retval, compat_dtab, - NSDB_SERVICES_COMPAT, "getservent_r", - compat_src, serv, buffer, bufsize, errnop); - break; - } - - if (!(rv & NS_TERMINATE) || - serv_mdata->how != nss_lt_all) - st->compat_mode_active = 0; - - continue; + aliases = (char **)_ALIGN(&buffer[linesize+1]); + aliases_size = (buffer + bufsize - + (char *)aliases) / sizeof(char *); + if (aliases_size < 1) { + *errnop = ERANGE; + rv = NS_RETURN; + break; } + memcpy(buffer, line, linesize); + buffer[linesize] = '\0'; + rv = __servent_unpack(buffer, serv, aliases, aliases_size, errnop); - if (rv !=0 ) { + if (rv != 0 ) { if (*errnop == 0) { rv = NS_NOTFOUND; continue; ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_files/files_serv.h#3 (text+ko) ==== @@ -26,11 +26,5 @@ * $FreeBSD$ */ -struct servent_mdata -{ - enum nss_lookup_type how; - int compat_mode; -}; - extern int __files_servent(void *, void *, va_list); extern int __files_setservent(void *, void *, va_list); ==== //depot/projects/soc2006/nss_ldap_cached/src/lib/nss_files/nss_files.c#12 (text+ko) ==== @@ -45,10 +45,6 @@ #include "netdb_private.h" #include "nss_files.h" -static struct servent_mdata getservbyname_mdata = { nss_lt_name, 0 }; -static struct servent_mdata getservbyport_mdata = { nss_lt_id, 0 }; -static struct servent_mdata getservent_mdata = { nss_lt_all, 0 }; - static ns_mtab methods[] = { {NSDB_GROUP, "getgrnam_r", __files_group, (void *)nss_lt_name}, {NSDB_GROUP, "getgrgid_r", __files_group, (void *)nss_lt_id}, @@ -70,11 +66,9 @@ {NSDB_HOSTS_INTERNAL, "endhostent", __files_endhostent, NULL}, {NSDB_SERVICES, "getservbyname_r", __files_servent, - (void *)&getservbyname_mdata}, - {NSDB_SERVICES, "getservbyport_r", __files_servent, - (void *)&getservbyport_mdata}, - {NSDB_SERVICES, "getservent_r", __files_servent, - (void *)&getservent_mdata}, + (void *)nss_lt_name}, + {NSDB_SERVICES, "getservbyport_r", __files_servent, (void *)nss_lt_id}, + {NSDB_SERVICES, "getservent_r", __files_servent, (void *)nss_lt_all}, {NSDB_SERVICES, "setservent", __files_setservent, (void *)nss_set_ent}, {NSDB_SERVICES, "endservent", __files_setservent, (void *)nss_end_ent}, @@ -113,11 +107,11 @@ {NSDB_PASSWD_COMPAT, "setpwent", __files_setpwent, (void *)nss_set_ent}, {NSDB_SERVICES_COMPAT, "getservbyname_r", __files_servent, - (void *)&getservbyname_mdata}, + (void *)nss_lt_name}, {NSDB_SERVICES_COMPAT, "getservbyport_r", __files_servent, - (void *)&getservbyport_mdata}, + (void *)nss_lt_id}, {NSDB_SERVICES_COMPAT, "getservent_r", __files_servent, - (void *)&getservent_mdata}, + (void *)nss_lt_all}, {NSDB_SERVICES_COMPAT, "setservent", __files_setservent, (void *)nss_set_ent}, {NSDB_SERVICES_COMPAT, "endservent", __files_setservent,
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200610311114.k9VBEU4v070716>