diff options
author | Roland McGrath <roland@gnu.org> | 2004-09-22 21:21:10 +0000 |
---|---|---|
committer | Roland McGrath <roland@gnu.org> | 2004-09-22 21:21:10 +0000 |
commit | b5707b44d25d7af61b0338c2a2206c036eaf7337 (patch) | |
tree | d8b9e865cbc78d64835a63959370865a2a043223 /glibc-compat/nss_nis | |
parent | 4ff389feb39f2eb649530b843d478c80c27ab4cf (diff) |
Changes and additions migrated from cvs.devel.redhat.com:/cvs/devel/glibc to fedora-branch
Diffstat (limited to 'glibc-compat/nss_nis')
-rw-r--r-- | glibc-compat/nss_nis/nis-alias.c | 278 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-ethers.c | 299 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-grp.c | 249 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-hosts.c | 417 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-netgrp.c | 128 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-network.c | 318 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-proto.c | 280 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-pwd.c | 407 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-rpc.c | 295 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-service.c | 280 | ||||
-rw-r--r-- | glibc-compat/nss_nis/nis-spwd.c | 201 |
11 files changed, 3152 insertions, 0 deletions
diff --git a/glibc-compat/nss_nis/nis-alias.c b/glibc-compat/nss_nis/nis-alias.c new file mode 100644 index 0000000000..14149699d3 --- /dev/null +++ b/glibc-compat/nss_nis/nis-alias.c @@ -0,0 +1,278 @@ +/* Copyright (C) 1996, 1998 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <glibc-compat/include/aliases.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +__libc_lock_define_initialized (static, lock) + +static bool_t new_start = 1; +static char *oldkey = NULL; +static int oldkeylen = 0; + +static int +_nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result, + char *buffer, size_t buflen) +{ + char *first_unused = buffer + strlen (alias) + 1; + size_t room_left = + buflen - (buflen % __alignof__ (char *)) - strlen (alias) - 2; + char *line; + char *cp; + + result->alias_members_len = 0; + *first_unused = '\0'; + first_unused++; + strcpy (first_unused, key); + + if (first_unused[room_left - 1] != '\0') + { + /* The line is too long for our buffer. */ + no_more_room: + __set_errno (ERANGE); + return -1; + } + + result->alias_name = first_unused; + + /* Terminate the line for any case. */ + cp = strpbrk (alias, "#\n"); + if (cp != NULL) + *cp = '\0'; + + first_unused += strlen (result->alias_name) + 1; + /* Adjust the pointer so it is aligned for + storing pointers. */ + first_unused += __alignof__ (char *) - 1; + first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *)); + result->alias_members = (char **) first_unused; + + line = alias; + + while (*line != '\0') + { + /* Skip leading blanks. */ + while (isspace (*line)) + line++; + + if (*line == '\0') + break; + + if (room_left < sizeof (char *)) + goto no_more_room; + room_left -= sizeof (char *); + result->alias_members[result->alias_members_len] = line; + + while (*line != '\0' && *line != ',') + line++; + + if (line != result->alias_members[result->alias_members_len]) + { + *line = '\0'; + line++; + result->alias_members_len++; + } + } + return result->alias_members_len == 0 ? 0 : 1; +} + +enum nss_status +_nss_nis_setaliasent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endaliasent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_getaliasent_r (struct aliasent *alias, char *buffer, + size_t buflen) +{ + char *domain; + char *result; + int len; + char *outkey; + int keylen; + char *p; + int parse_res; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + alias->alias_local = 0; + + /* Get the next entry until we found a correct one. */ + do + { + enum nss_status retval; + + if (new_start) + retval = yperr2nss (yp_first (domain, "mail.aliases", + &outkey, &keylen, &result, &len)); + else + retval = yperr2nss ( yp_next (domain, "mail.aliases", oldkey, + oldkeylen, &outkey, &keylen, + &result, &len)); + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer, buflen); + if (parse_res == -1) + { + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + free (oldkey); + oldkey = outkey; + oldkeylen = keylen; + new_start = 0; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_getaliasent_r (alias, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias, + char *buffer, size_t buflen) +{ + enum nss_status retval; + int parse_res; + char *domain; + char *result; + int len; + char *p; + size_t namlen = strlen (name); + char name2[namlen + 1]; + int i; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + /* Convert name to lowercase. */ + for (i = 0; i < namlen; ++i) + name2[i] = tolower (name[i]); + name2[i] = '\0'; + + retval = yperr2nss (yp_match (domain, "mail.aliases", name, namlen, + &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + alias->alias_local = 0; + parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen); + if (parse_res == -1) + return NSS_STATUS_TRYAGAIN; + else + if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + else + return NSS_STATUS_SUCCESS; +} diff --git a/glibc-compat/nss_nis/nis-ethers.c b/glibc-compat/nss_nis/nis-ethers.c new file mode 100644 index 0000000000..54b99dcba9 --- /dev/null +++ b/glibc-compat/nss_nis/nis-ethers.c @@ -0,0 +1,299 @@ +/* Copyright (C) 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> +#include <netinet/if_ether.h> + +#include "nss-nis.h" + +/* Protect global state against multiple changers */ +__libc_lock_define_initialized (static, lock) + +struct ether +{ + const char *e_name; + struct ether_addr e_addr; +}; + +/* Get the declaration of the parser function. */ +#define ENTNAME etherent +#define STRUCTURE ether +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +struct response +{ + char *val; + struct response *next; +}; + +static struct response *start = NULL; +static struct response *next = NULL; + +static int +saveit (int instatus, char *inkey, int inkeylen, char *inval, + int invallen, char *indata) +{ + if (instatus != YP_TRUE) + return instatus; + + if (inkey && inkeylen > 0 && inval && invallen > 0) + { + if (start == NULL) + { + start = malloc (sizeof (struct response)); + next = start; + } + else + { + next->next = malloc (sizeof (struct response)); + next = next->next; + } + next->next = NULL; + next->val = malloc (invallen + 1); + strncpy (next->val, inval, invallen); + next->val[invallen] = '\0'; + } + + return 0; +} + +enum nss_status +internal_nis_setetherent (void) +{ + char *domainname; + struct ypall_callback ypcb; + enum nss_status status; + + yp_get_default_domain (&domainname); + + while (start != NULL) + { + if (start->val != NULL) + free (start->val); + next = start; + start = start->next; + free (next); + } + start = NULL; + + ypcb.foreach = saveit; + ypcb.data = NULL; + status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb)); + next = start; + + return status; +} + +enum nss_status +_nss_nis_setetherent (void) +{ + enum nss_status result; + + __libc_lock_lock (lock); + + result = internal_nis_setetherent (); + + __libc_lock_unlock (lock); + + return result; +} + +enum nss_status +_nss_nis_endetherent (void) +{ + __libc_lock_lock (lock); + + while (start != NULL) + { + if (start->val != NULL) + free (start->val); + next = start; + start = start->next; + free (next); + } + start = NULL; + next = NULL; + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + int parse_res; + + if (start == NULL) + internal_nis_setetherent (); + + /* Get the next entry until we found a correct one. */ + do + { + char *p; + + if (next == NULL) + return NSS_STATUS_NOTFOUND; + p = strncpy (buffer, next->val, buflen); + next = next->next; + + while (isspace (*p)) + ++p; + + parse_res = _nss_files_parse_etherent (p, eth, data, buflen); + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nis_getetherent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_gethostton_r (const char *name, struct ether *eth, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, parse_res; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + retval = yperr2nss (yp_match (domain, "ethers.byname", name, + strlen (name), &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_etherent (p, eth, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, nlen, parse_res; + char buf[33]; + + if (addr == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x", + (int) addr->ether_addr_octet[0], + (int) addr->ether_addr_octet[1], + (int) addr->ether_addr_octet[2], + (int) addr->ether_addr_octet[3], + (int) addr->ether_addr_octet[4], + (int) addr->ether_addr_octet[5]); + + retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf, + nlen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_etherent (p, eth, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} diff --git a/glibc-compat/nss_nis/nis-grp.c b/glibc-compat/nss_nis/nis-grp.c new file mode 100644 index 0000000000..5b8e838bdc --- /dev/null +++ b/glibc-compat/nss_nis/nis-grp.c @@ -0,0 +1,249 @@ +/* Copyright (C) 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <glibc-compat/include/grp.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME grent +#define STRUCTURE group +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +/* Protect global state against multiple changers */ +__libc_lock_define_initialized (static, lock) + +static bool_t new_start = 1; +static char *oldkey = NULL; +static int oldkeylen = 0; + +enum nss_status +_nss_nis_setgrent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endgrent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + char *domain, *result, *outkey; + int len, keylen, parse_res; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + /* Get the next entry until we found a correct one. */ + do + { + enum nss_status retval; + char *p; + + if (new_start) + retval = yperr2nss (yp_first (domain, "group.byname", + &outkey, &keylen, &result, &len)); + else + retval = yperr2nss ( yp_next (domain, "group.byname", + oldkey, oldkeylen, + &outkey, &keylen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_grent (p, grp, data, buflen); + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + + free (oldkey); + oldkey = outkey; + oldkeylen = keylen; + new_start = 0; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nis_getgrent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getgrnam_r (const char *name, struct group *grp, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, parse_res; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + retval = yperr2nss (yp_match (domain, "group.byname", name, + strlen (name), &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_grent (p, grp, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getgrgid_r (gid_t gid, struct group *grp, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, nlen, parse_res; + char buf[32]; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + nlen = sprintf (buf, "%d", gid); + + retval = yperr2nss (yp_match (domain, "group.bygid", buf, + nlen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_grent (p, grp, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} diff --git a/glibc-compat/nss_nis/nis-hosts.c b/glibc-compat/nss_nis/nis-hosts.c new file mode 100644 index 0000000000..c6c413c55d --- /dev/null +++ b/glibc-compat/nss_nis/nis-hosts.c @@ -0,0 +1,417 @@ +/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <ctype.h> +#include <glibc-compat/include/netdb.h> +#include <string.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <resolv.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get implementation for some internal functions. */ +#include "../../resolv/mapv4v6addr.h" +#include "../../resolv/mapv4v6hostent.h" + +#define ENTNAME hostent +#define DATABASE "hosts" +#define NEED_H_ERRNO + +#define ENTDATA hostent_data +struct hostent_data + { + unsigned char host_addr[16]; /* IPv4 or IPv6 address. */ + char *h_addr_ptrs[2]; /* Points to that and null terminator. */ + }; + +#define TRAILING_LIST_MEMBER h_aliases +#define TRAILING_LIST_SEPARATOR_P isspace +#include "../nss_files/files-parse.c" +LINE_PARSER +("#", + { + char *addr; + + STRING_FIELD (addr, isspace, 1); + + /* Parse address. */ + if ((_res.options & RES_USE_INET6) + && inet_pton (AF_INET6, addr, entdata->host_addr) > 0) + { + result->h_addrtype = AF_INET6; + result->h_length = IN6ADDRSZ; + } + else + if (inet_pton (AF_INET, addr, entdata->host_addr) > 0) + { + if (_res.options & RES_USE_INET6) + { + map_v4v6_address ((char *) entdata->host_addr, + (char *) entdata->host_addr); + result->h_addrtype = AF_INET6; + result->h_length = IN6ADDRSZ; + } + else + { + result->h_addrtype = AF_INET; + result->h_length = INADDRSZ; + } + } + else + /* Illegal address: ignore line. */ + return 0; + + /* Store a pointer to the address in the expected form. */ + entdata->h_addr_ptrs[0] = entdata->host_addr; + entdata->h_addr_ptrs[1] = NULL; + result->h_addr_list = entdata->h_addr_ptrs; + + /* If we need the host entry in IPv6 form change it now. */ + if (_res.options & RES_USE_INET6) + { + char *bufptr = data->linebuffer; + size_t buflen = (char *) data + datalen - bufptr; + int ibuflen = buflen; /* Use this for machines with size_t > int. */ + map_v4v6_hostent (result, &bufptr, &ibuflen); + buflen = ibuflen; + } + + STRING_FIELD (result->h_name, isspace, 1); + } +) + +__libc_lock_define_initialized (static, lock) + +static bool_t new_start = 1; +static char *oldkey = NULL; +static int oldkeylen = 0; + +enum nss_status +_nss_nis_sethostent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endhostent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_gethostent_r (struct hostent *host, char *buffer, + size_t buflen, int *h_errnop) +{ + char *domain; + char *result; + int len, parse_res; + char *outkey; + int keylen; + struct parser_data *data = (void *) buffer; + size_t linebuflen = buffer + buflen - data->linebuffer; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + if (buflen < sizeof *data + 1) + { + __set_errno (ERANGE); + *h_errnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + /* Get the next entry until we found a correct one. */ + do + { + enum nss_status retval; + char *p; + + if (new_start) + retval = yperr2nss (yp_first (domain, "hosts.byname", + &outkey, &keylen, &result, &len)); + else + retval = yperr2nss ( yp_next (domain, "hosts.byname", + oldkey, oldkeylen, + &outkey, &keylen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + switch (retval) + { + case NSS_STATUS_TRYAGAIN: + __set_errno (EAGAIN); + *h_errnop = TRY_AGAIN; + break; + case NSS_STATUS_NOTFOUND: + *h_errnop = HOST_NOT_FOUND; + break; + default: + *h_errnop = NO_RECOVERY; + break; + } + return retval; + } + + if ((size_t) (len + 1) > linebuflen) + { + free (result); + *h_errnop = NETDB_INTERNAL; + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (data->linebuffer, result, len); + data->linebuffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = parse_line (p, host, data, buflen); + if (parse_res == -1 && errno == ERANGE) + { + *h_errnop = NETDB_INTERNAL;; + return NSS_STATUS_TRYAGAIN; + } + free (oldkey); + oldkey = outkey; + oldkeylen = keylen; + new_start = 0; + } + while (!parse_res); + + *h_errnop = NETDB_SUCCESS; + return NSS_STATUS_SUCCESS; +} + +int +_nss_nis_gethostent_r (struct hostent *host, char *buffer, size_t buflen, + int *h_errnop) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nis_gethostent_r (host, buffer, buflen, h_errnop); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_gethostbyname2_r (const char *name, int af, struct hostent *host, + char *buffer, size_t buflen, int *h_errnop) +{ + enum nss_status retval; + char *domain, *result, *p; + int len, parse_res; + struct parser_data *data = (void *) buffer; + size_t linebuflen = buffer + buflen - data->linebuffer; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + if (buflen < sizeof *data + 1) + { + *h_errnop = NETDB_INTERNAL; + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + else + { + /* Convert name to lowercase. */ + size_t namelen = strlen (name); + char name2[namelen + 1]; + int i; + + for (i = 0; i < namelen; ++i) + name2[i] = tolower (name[i]); + name2[i] = '\0'; + + retval = yperr2nss (yp_match (domain, "hosts.byname", name2, + namelen, &result, &len)); + + } + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + *h_errnop = TRY_AGAIN; + __set_errno (EAGAIN); + } + if (retval == NSS_STATUS_NOTFOUND) + *h_errnop = HOST_NOT_FOUND; + return retval; + } + + if ((size_t) (len + 1) > linebuflen) + { + free (result); + *h_errnop = NETDB_INTERNAL; + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (data->linebuffer, result, len); + data->linebuffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = parse_line (p, host, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + { + *h_errnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + if (parse_res == 0 || host->h_addrtype != af) + { + *h_errnop = HOST_NOT_FOUND; + return NSS_STATUS_NOTFOUND; + } + + *h_errnop = NETDB_SUCCESS; + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_gethostbyname_r (const char *name, struct hostent *host, + char *buffer, size_t buflen, int *h_errnop) +{ + if (_res.options & RES_USE_INET6) + { + enum nss_status status; + + status = _nss_nis_gethostbyname2_r (name, AF_INET6, host, buffer, buflen, + h_errnop); + if (status == NSS_STATUS_SUCCESS) + return status; + } + + return _nss_nis_gethostbyname2_r (name, AF_INET, host, buffer, buflen, + h_errnop); +} + +enum nss_status +_nss_nis_gethostbyaddr_r (char *addr, int addrlen, int type, + struct hostent *host, char *buffer, size_t buflen, + int *h_errnop) +{ + enum nss_status retval; + char *domain, *result, *p; + int len, parse_res; + char *buf; + struct parser_data *data = (void *) buffer; + size_t linebuflen = buffer + buflen - data->linebuffer; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + if (buflen < sizeof *data + 1) + { + __set_errno (ERANGE); + *h_errnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + buf = inet_ntoa (*(struct in_addr *) addr); + + retval = yperr2nss (yp_match (domain, "hosts.byaddr", buf, + strlen (buf), &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + *h_errnop = TRY_AGAIN; + __set_errno (EAGAIN); + } + if (retval == NSS_STATUS_NOTFOUND) + *h_errnop = HOST_NOT_FOUND; + return retval; + } + + if ((size_t) (len + 1) > linebuflen) + { + free (result); + __set_errno (ERANGE); + *h_errnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (data->linebuffer, result, len); + data->linebuffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = parse_line (p, host, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + { + *h_errnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + else if (parse_res == 0) + { + *h_errnop = HOST_NOT_FOUND; + return NSS_STATUS_NOTFOUND; + } + + *h_errnop = NETDB_SUCCESS; + return NSS_STATUS_SUCCESS; +} diff --git a/glibc-compat/nss_nis/nis-netgrp.c b/glibc-compat/nss_nis/nis-netgrp.c new file mode 100644 index 0000000000..da87f1a605 --- /dev/null +++ b/glibc-compat/nss_nis/nis-netgrp.c @@ -0,0 +1,128 @@ +/* Copyright (C) 1996 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <ctype.h> +#include <errno.h> +#include <bits/libc-lock.h> +#include <glibc-compat/include/netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <netgroup.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Locks the static variables in this file. */ +__libc_lock_define_initialized (static, lock) + +static char *data = NULL; +static size_t data_size = 0; +static char *cursor = NULL;; + +extern enum nss_status +_nss_netgroup_parseline (char **cursor, struct __netgrent *result, + char *buffer, size_t buflen); + +enum nss_status +_nss_nis_setnetgrent (char *group) +{ + char *domain; + char *result; + int len, group_len; + enum nss_status status; + + status = NSS_STATUS_SUCCESS; + + if (group[0] == '\0') + return NSS_STATUS_UNAVAIL; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + __libc_lock_lock (lock); + + if (data != NULL) + { + free (data); + data = NULL; + data_size = 0; + cursor = NULL; + } + + group_len = strlen (group); + + status = yperr2nss (yp_match (domain, "netgroup", group, group_len, + &result, &len)); + if (status == NSS_STATUS_SUCCESS) + { + if (len > 0) + { + data = malloc (len + 1); + data_size = len; + cursor = strncpy (data, result, len + 1); + data[len] = '\0'; + free (result); + } + else + status = NSS_STATUS_NOTFOUND; + } + + __libc_lock_unlock (lock); + + return status; +} + + +enum nss_status +_nss_nis_endnetgrent (void) +{ + __libc_lock_lock (lock); + + if (data != NULL) + { + free (data); + data = NULL; + data_size = 0; + cursor = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen) +{ + enum nss_status status; + + if (cursor == NULL) + return NSS_STATUS_NOTFOUND; + + __libc_lock_lock (lock); + + status = _nss_netgroup_parseline (&cursor, result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} diff --git a/glibc-compat/nss_nis/nis-network.c b/glibc-compat/nss_nis/nis-network.c new file mode 100644 index 0000000000..3accc2be41 --- /dev/null +++ b/glibc-compat/nss_nis/nis-network.c @@ -0,0 +1,318 @@ +/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <glibc-compat/include/netdb.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME netent +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +__libc_lock_define_initialized (static, lock) + +static bool_t new_start = 1; +static char *oldkey = NULL; +static int oldkeylen = 0; + +enum nss_status +_nss_nis_setnetent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endnetent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen, + int *herrnop) +{ + struct parser_data *data = (void *) buffer; + char *domain, *result, *outkey; + int len, keylen, parse_res; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + /* Get the next entry until we found a correct one. */ + do + { + enum nss_status retval; + char *p; + + if (new_start) + retval = yperr2nss (yp_first (domain, "networks.byname", + &outkey, &keylen, &result, &len)); + else + retval = yperr2nss ( yp_next (domain, "networks.byname", + oldkey, oldkeylen, + &outkey, &keylen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + *herrnop = NETDB_INTERNAL; + __set_errno (EAGAIN); + } + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_netent (p, net, data, buflen); + if (parse_res == -1 && errno == ERANGE) + { + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + free (oldkey); + oldkey = outkey; + oldkeylen = keylen; + new_start = 0; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen, + int *herrnop) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_getnetent_r (net, buffer, buflen, herrnop); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getnetbyname_r (const char *name, struct netent *net, + char *buffer, size_t buflen, int *herrnop) +{ + enum nss_status retval; + struct parser_data *data = (void *) buffer; + char *domain, *result, *p; + int len, parse_res; + + if (name == NULL) + { + __set_errno (EINVAL); + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + if (buflen < sizeof *data + 1) + { + *herrnop = NETDB_INTERNAL; + __set_errno(ERANGE); + return NSS_STATUS_TRYAGAIN; + } + else + { + /* Convert name to lowercase. */ + size_t namlen = strlen (name); + char name2[namlen + 1]; + int i; + + for (i = 0; i < namlen; ++i) + name2[i] = tolower (name[i]); + name2[i] = '\0'; + + retval = yperr2nss (yp_match (domain, "networks.byname", name2, + namlen, &result, &len)); + } + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + __set_errno (EAGAIN); + *herrnop = NETDB_INTERNAL; + } + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_netent (p, net, data, buflen); + + if (parse_res <= 0) + { + *herrnop = NETDB_INTERNAL; + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } + else + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net, + char *buffer, size_t buflen, int *herrnop) +{ + struct parser_data *data = (void *) buffer; + char *domain; + char *result; + int len; + char buf[256]; + int blen; + struct in_addr in; + char *p; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + in = inet_makeaddr (addr, 0); + strcpy (buf, inet_ntoa (in)); + blen = strlen (buf); + + while (1) + { + enum nss_status retval; + int parse_res; + + retval = yperr2nss (yp_match (domain, "networks.byaddr", buf, + strlen (buf), &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_NOTFOUND) + { + if (buf[blen - 2] == '.' && buf[blen - 1] == '0') + { + /* Try again, but with trailing dot(s) + removed (one by one) */ + buf[blen - 2] = '\0'; + blen -= 2; + continue; + } + else + return NSS_STATUS_NOTFOUND; + } + else + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_netent (p, net, data, buflen); + + + if (parse_res <= 0) + { + *herrnop = NETDB_INTERNAL; + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } + else + return NSS_STATUS_SUCCESS; + } +} diff --git a/glibc-compat/nss_nis/nis-proto.c b/glibc-compat/nss_nis/nis-proto.c new file mode 100644 index 0000000000..8dff7d3687 --- /dev/null +++ b/glibc-compat/nss_nis/nis-proto.c @@ -0,0 +1,280 @@ +/* Copyright (C) 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <glibc-compat/include/netdb.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME protoent +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +__libc_lock_define_initialized (static, lock) + +struct response +{ + char *val; + struct response *next; +}; + +static struct response *start = NULL; +static struct response *next = NULL; + +static int +saveit (int instatus, char *inkey, int inkeylen, char *inval, + int invallen, char *indata) +{ + if (instatus != YP_TRUE) + return instatus; + + if (inkey && inkeylen > 0 && inval && invallen > 0) + { + if (start == NULL) + { + start = malloc (sizeof (struct response)); + next = start; + } + else + { + next->next = malloc (sizeof (struct response)); + next = next->next; + } + next->next = NULL; + next->val = malloc (invallen + 1); + strncpy (next->val, inval, invallen); + next->val[invallen] = '\0'; + } + + return 0; +} + +enum nss_status +internal_nis_setprotoent (void) +{ + char *domainname; + struct ypall_callback ypcb; + enum nss_status status; + + yp_get_default_domain (&domainname); + + while (start != NULL) + { + if (start->val != NULL) + free (start->val); + next = start; + start = start->next; + free (next); + } + start = NULL; + + ypcb.foreach = saveit; + ypcb.data = NULL; + status = yperr2nss (yp_all (domainname, "protocols.bynumber", &ypcb)); + next = start; + + return status; +} + +enum nss_status +_nss_nis_setprotoent (void) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_setprotoent (); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_endprotoent (void) +{ + __libc_lock_lock (lock); + + while (start != NULL) + { + if (start->val != NULL) + free (start->val); + next = start; + start = start->next; + free (next); + } + start = NULL; + next = NULL; + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_getprotoent_r (struct protoent *proto, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + int parse_res; + + if (start == NULL) + internal_nis_setprotoent (); + + /* Get the next entry until we found a correct one. */ + do + { + char *p; + + if (next == NULL) + return NSS_STATUS_NOTFOUND; + p = strncpy (buffer, next->val, buflen); + next = next->next; + + while (isspace (*p)) + ++p; + + parse_res = _nss_files_parse_protoent (p, proto, data, buflen); + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_getprotoent_r (proto, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getprotobyname_r (const char *name, struct protoent *proto, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, parse_res; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + retval = yperr2nss (yp_match (domain, "protocols.byname", name, + strlen (name), &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_protoent (p, proto, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getprotobynumber_r (int number, struct protoent *proto, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, nlen, parse_res; + char buf[32]; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + nlen = sprintf (buf, "%d", number); + + retval = yperr2nss (yp_match (domain, "protocols.bynumber", buf, + nlen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_protoent (p, proto, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} diff --git a/glibc-compat/nss_nis/nis-pwd.c b/glibc-compat/nss_nis/nis-pwd.c new file mode 100644 index 0000000000..e18c80d8ac --- /dev/null +++ b/glibc-compat/nss_nis/nis-pwd.c @@ -0,0 +1,407 @@ +/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <glibc-compat/include/pwd.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME pwent +#define STRUCTURE passwd +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +/* Protect global state against multiple changers */ +__libc_lock_define_initialized (static, lock) + +static bool_t new_start = 1; +static char *oldkey = NULL; +static int oldkeylen = 0; + +enum nss_status +_nss_nis_setpwent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endpwent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + char *domain; + int parse_res; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + /* Get the next entry until we found a correct one. */ + do + { + enum nss_status retval; + char *result, *outkey, *result2, *p; + int len, keylen, len2; + size_t namelen; + + if (new_start) + retval = yperr2nss (yp_first (domain, "passwd.byname", + &outkey, &keylen, &result, &len)); + else + retval = yperr2nss ( yp_next (domain, "passwd.byname", + oldkey, oldkeylen, + &outkey, &keylen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + /* Check for adjunct style secret passwords. They can be + recognized by a password starting with "##". */ + p = strchr (result, ':'); + if (p != NULL /* This better should be true in all cases. */ + && p[1] == '#' && p[2] == '#' + && (namelen = p - result, + yp_match (domain, "passwd.adjunct.byname", result, namelen, + &result2, &len2)) == YPERR_SUCCESS) + { + /* We found a passwd.adjunct entry. Merge encrypted + password therein into original result. */ + char *encrypted = strchr (result2, ':'); + char *endp, *tmp; + size_t restlen; + + if (encrypted == NULL + || (endp = strchr (++encrypted, ':')) == NULL + || (p = strchr (p + 1, ':')) == NULL) + { + /* Invalid format of the entry. This never should happen + unless the data from which the NIS table is generated is + wrong. We simply ignore it. */ + free (result2); + goto non_adjunct; + } + + restlen = len - (p - result); + if ((size_t) (namelen + (endp - encrypted) + restlen + 2) > buflen) + { + free (result2); + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + memcpy (buffer, result, namelen); + tmp = buffer + namelen; + *tmp++ = ':'; + memcpy (tmp, encrypted, endp - encrypted); + tmp += endp - encrypted; + memcpy (tmp, p, restlen + 1); + p = buffer; + + free (result2); + } + else + { + non_adjunct: + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_pwent (p, pwd, data, buflen); + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + + free (oldkey); + oldkey = outkey; + oldkeylen = keylen; + new_start = 0; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getpwent_r (struct passwd *result, char *buffer, size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nis_getpwent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getpwnam_r (const char *name, struct passwd *pwd, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *result2, *p; + int len, len2, parse_res; + size_t namelen; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + namelen = strlen (name); + + retval = yperr2nss (yp_match (domain, "passwd.byname", name, + namelen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + /* Check for adjunct style secret passwords. They can be recognized + by a password starting with "##". */ + p = strchr (result, ':'); + if (p != NULL /* This better should be true in all cases. */ + && p[1] == '#' && p[2] == '#' + && (namelen = p - result, + yp_match (domain, "passwd.adjunct.byname", name, namelen, + &result2, &len2)) == YPERR_SUCCESS) + { + /* We found a passwd.adjunct entry. Merge encrypted password + therein into original result. */ + char *encrypted = strchr (result2, ':'); + char *endp, *tmp; + size_t restlen; + + if (encrypted == NULL + || (endp = strchr (++encrypted, ':')) == NULL + || (p = strchr (p + 1, ':')) == NULL) + { + /* Invalid format of the entry. This never should happen + unless the data from which the NIS table is generated is + wrong. We simply ignore it. */ + free (result2); + goto non_adjunct; + } + + restlen = len - (p - result); + if ((size_t) (namelen + (endp - encrypted) + restlen + 2) > buflen) + { + free (result2); + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + memcpy (buffer, name, namelen); + tmp = buffer + namelen; + *tmp++ = ':'; + memcpy (tmp, encrypted, endp - encrypted); + tmp += endp - encrypted; + memcpy (tmp, p, restlen + 1); + p = buffer; + + free (result2); + } + else + { + non_adjunct: + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + } + + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_pwent (p, pwd, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p, *result2; + int len, nlen, parse_res, len2; + char buf[32]; + size_t namelen; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + nlen = sprintf (buf, "%d", uid); + + retval = yperr2nss (yp_match (domain, "passwd.byuid", buf, + nlen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + /* Check for adjunct style secret passwords. They can be recognized + by a password starting with "##". */ + p = strchr (result, ':'); + if (p != NULL /* This better should be true in all cases. */ + && p[1] == '#' && p[2] == '#' + && (namelen = p - result, + yp_match (domain, "passwd.adjunct.byname", result, namelen, + &result2, &len2)) == YPERR_SUCCESS) + { + /* We found a passwd.adjunct entry. Merge encrypted password + therein into original result. */ + char *encrypted = strchr (result2, ':'); + char *endp, *tmp; + size_t restlen; + + if (encrypted == NULL + || (endp = strchr (++encrypted, ':')) == NULL + || (p = strchr (p + 1, ':')) == NULL) + { + /* Invalid format of the entry. This never should happen + unless the data from which the NIS table is generated is + wrong. We simply ignore it. */ + free (result2); + goto non_adjunct; + } + + restlen = len - (p - result); + if ((size_t) (namelen + (endp - encrypted) + restlen + 2) > buflen) + { + free (result2); + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + memcpy (buffer, result, namelen); + tmp = buffer + namelen; + *tmp++ = ':'; + memcpy (tmp, encrypted, endp - encrypted); + tmp += endp - encrypted; + memcpy (tmp, p, restlen + 1); + p = buffer; + + free (result2); + } + else + { + non_adjunct: + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + } + + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_pwent (p, pwd, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} diff --git a/glibc-compat/nss_nis/nis-rpc.c b/glibc-compat/nss_nis/nis-rpc.c new file mode 100644 index 0000000000..b265fcdecb --- /dev/null +++ b/glibc-compat/nss_nis/nis-rpc.c @@ -0,0 +1,295 @@ +/* Copyright (C) 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <glibc-compat/include/netdb.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME rpcent +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +__libc_lock_define_initialized (static, lock) + +struct response_t +{ + char *val; + struct response_t *next; +}; + +struct intern_t +{ + struct response_t *start; + struct response_t *next; +}; +typedef struct intern_t intern_t; + +static intern_t intern = {NULL, NULL}; + +static int +saveit (int instatus, char *inkey, int inkeylen, char *inval, + int invallen, char *indata) +{ + intern_t *intern = (intern_t *)indata; + + if (instatus != YP_TRUE) + return instatus; + + if (inkey && inkeylen > 0 && inval && invallen > 0) + { + if (intern->start == NULL) + { + intern->start = malloc (sizeof (struct response_t)); + intern->next = intern->start; + } + else + { + intern->next->next = malloc (sizeof (struct response_t)); + intern->next = intern->next->next; + } + intern->next->next = NULL; + intern->next->val = malloc (invallen + 1); + strncpy (intern->next->val, inval, invallen); + intern->next->val[invallen] = '\0'; + } + + return 0; +} + +static enum nss_status +internal_nis_setrpcent (intern_t *intern) +{ + char *domainname; + struct ypall_callback ypcb; + enum nss_status status; + + if (yp_get_default_domain (&domainname)) + return NSS_STATUS_UNAVAIL; + + while (intern->start != NULL) + { + if (intern->start->val != NULL) + free (intern->start->val); + intern->next = intern->start; + intern->start = intern->start->next; + free (intern->next); + } + intern->start = NULL; + + ypcb.foreach = saveit; + ypcb.data = (char *)intern; + status = yperr2nss (yp_all(domainname, "rpc.bynumber", &ypcb)); + intern->next = intern->start; + + return status; +} + +enum nss_status +_nss_nis_setrpcent (void) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_setrpcent (&intern); + + __libc_lock_unlock (lock); + + return status; +} + +static enum nss_status +internal_nis_endrpcent (intern_t *intern) +{ + while (intern->start != NULL) + { + if (intern->start->val != NULL) + free (intern->start->val); + intern->next = intern->start; + intern->start = intern->start->next; + free (intern->next); + } + intern->start = NULL; + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endrpcent (void) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_endrpcent (&intern); + + __libc_lock_unlock (lock); + + return status; +} + +static enum nss_status +internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen, + intern_t *data) +{ + struct parser_data *pdata = (void *) buffer; + int parse_res; + char *p; + + if (data->start == NULL) + internal_nis_setrpcent (data); + + /* Get the next entry until we found a correct one. */ + do + { + if (data->next == NULL) + return NSS_STATUS_NOTFOUND; + p = strncpy (buffer, data->next->val, buflen); + data->next = data->next->next; + while (isspace (*p)) + ++p; + + parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen); + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_getrpcent_r (rpc, buffer, buflen, &intern); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc, + char *buffer, size_t buflen) +{ + intern_t data = {NULL, NULL}; + enum nss_status status; + int found; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + status = internal_nis_setrpcent (&data); + if (status != NSS_STATUS_SUCCESS) + return status; + + found = 0; + while (!found && + ((status = internal_nis_getrpcent_r (rpc, buffer, buflen, &data)) + == NSS_STATUS_SUCCESS)) + { + if (strcmp (rpc->r_name, name) == 0) + found = 1; + else + { + int i = 0; + + while (rpc->r_aliases[i] != NULL) + { + if (strcmp (rpc->r_aliases[i], name) == 0) + { + found = 1; + break; + } + else + ++i; + } + } + } + + internal_nis_endrpcent (&data); + + if (!found && status == NSS_STATUS_SUCCESS) + return NSS_STATUS_NOTFOUND; + else + return status; +} + +enum nss_status +_nss_nis_getrpcbynumber_r (int number, struct rpcent *rpc, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, nlen, parse_res; + char buf[32]; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + nlen = sprintf (buf, "%d", number); + + retval = yperr2nss (yp_match (domain, "rpc.bynumber", buf, + nlen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_rpcent (p, rpc, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} diff --git a/glibc-compat/nss_nis/nis-service.c b/glibc-compat/nss_nis/nis-service.c new file mode 100644 index 0000000000..75b871e440 --- /dev/null +++ b/glibc-compat/nss_nis/nis-service.c @@ -0,0 +1,280 @@ +/* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <glibc-compat/include/netdb.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME servent +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +__libc_lock_define_initialized (static, lock) + +struct response_t +{ + char *val; + struct response_t *next; +}; + +struct intern_t +{ + struct response_t *start; + struct response_t *next; +}; +typedef struct intern_t intern_t; + +static intern_t intern = { NULL, NULL }; + +static int +saveit (int instatus, char *inkey, int inkeylen, char *inval, + int invallen, char *indata) +{ + intern_t *intern = (intern_t *) indata; + + if (instatus != YP_TRUE) + return instatus; + + if (inkey && inkeylen > 0 && inval && invallen > 0) + { + if (intern->start == NULL) + { + intern->start = malloc (sizeof (struct response_t)); + intern->next = intern->start; + } + else + { + intern->next->next = malloc (sizeof (struct response_t)); + intern->next = intern->next->next; + } + intern->next->next = NULL; + intern->next->val = malloc (invallen + 1); + strncpy (intern->next->val, inval, invallen); + intern->next->val[invallen] = '\0'; + } + + return 0; +} + +static enum nss_status +internal_nis_setservent (intern_t *intern) +{ + char *domainname; + struct ypall_callback ypcb; + enum nss_status status; + + if (yp_get_default_domain (&domainname)) + return NSS_STATUS_UNAVAIL; + + while (intern->start != NULL) + { + if (intern->start->val != NULL) + free (intern->start->val); + intern->next = intern->start; + intern->start = intern->start->next; + free (intern->next); + } + intern->start = NULL; + + ypcb.foreach = saveit; + ypcb.data = (char *) intern; + status = yperr2nss (yp_all (domainname, "services.byname", &ypcb)); + intern->next = intern->start; + + return status; +} +enum nss_status +_nss_nis_setservent (void) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_setservent (&intern); + + __libc_lock_unlock (lock); + + return status; +} + +static enum nss_status +internal_nis_endservent (intern_t * intern) +{ + while (intern->start != NULL) + { + if (intern->start->val != NULL) + free (intern->start->val); + intern->next = intern->start; + intern->start = intern->start->next; + free (intern->next); + } + intern->start = NULL; + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endservent (void) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_endservent (&intern); + + __libc_lock_unlock (lock); + + return status; +} + +static enum nss_status +internal_nis_getservent_r (struct servent *serv, char *buffer, + size_t buflen, intern_t *data) +{ + struct parser_data *pdata = (void *) buffer; + int parse_res; + char *p; + + if (data->start == NULL) + internal_nis_setservent (data); + + /* Get the next entry until we found a correct one. */ + do + { + if (data->next == NULL) + return NSS_STATUS_NOTFOUND; + p = strncpy (buffer, data->next->val, buflen); + data->next = data->next->next; + while (isspace (*p)) + ++p; + + parse_res = _nss_files_parse_servent (p, serv, pdata, buflen); + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getservent_r (struct servent *serv, char *buffer, size_t buflen) +{ + enum nss_status status; + + __libc_lock_lock (lock); + + status = internal_nis_getservent_r (serv, buffer, buflen, &intern); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getservbyname_r (const char *name, char *protocol, + struct servent *serv, char *buffer, size_t buflen) +{ + intern_t data = { NULL, NULL }; + enum nss_status status; + int found; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + status = internal_nis_setservent (&data); + if (status != NSS_STATUS_SUCCESS) + return status; + + found = 0; + while (!found && + ((status = internal_nis_getservent_r (serv, buffer, buflen, &data)) + == NSS_STATUS_SUCCESS)) + { + if (protocol == NULL || strcmp (serv->s_proto, protocol) == 0) + { + char **cp; + + if (strcmp (serv->s_name, name) == 0) + found = 1; + else + for (cp = serv->s_aliases; *cp; cp++) + if (strcmp (name, *cp) == 0) + found = 1; + } + } + + internal_nis_endservent (&data); + + if (!found && status == NSS_STATUS_SUCCESS) + return NSS_STATUS_NOTFOUND; + else + return status; +} + +enum nss_status +_nss_nis_getservbyport_r (int port, char *protocol, struct servent *serv, + char *buffer, size_t buflen) +{ + intern_t data = { NULL, NULL }; + enum nss_status status; + int found; + + if (protocol == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + status = internal_nis_setservent (&data); + if (status != NSS_STATUS_SUCCESS) + return status; + + found = 0; + while (!found && + ((status = internal_nis_getservent_r (serv, buffer, buflen, &data)) + == NSS_STATUS_SUCCESS)) + { + if (htons (serv->s_port) == port) + { + if (strcmp (serv->s_proto, protocol) == 0) + { + found = 1; + } + } + } + + internal_nis_endservent (&data); + + if (!found && status == NSS_STATUS_SUCCESS) + return NSS_STATUS_NOTFOUND; + else + return status; +} diff --git a/glibc-compat/nss_nis/nis-spwd.c b/glibc-compat/nss_nis/nis-spwd.c new file mode 100644 index 0000000000..d7857b2c88 --- /dev/null +++ b/glibc-compat/nss_nis/nis-spwd.c @@ -0,0 +1,201 @@ +/* Copyright (C) 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include <nss.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <glibc-compat/include/shadow.h> +#include <bits/libc-lock.h> +#include <rpcsvc/yp.h> +#include <rpcsvc/ypclnt.h> + +#include "nss-nis.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME spent +#define STRUCTURE spwd +#define EXTERN_PARSER +#include "../nss_files/files-parse.c" + +/* Protect global state against multiple changers */ +__libc_lock_define_initialized (static, lock) + +static bool_t new_start = 1; +static char *oldkey = NULL; +static int oldkeylen = 0; + +enum nss_status +_nss_nis_setspent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_endspent (void) +{ + __libc_lock_lock (lock); + + new_start = 1; + if (oldkey != NULL) + { + free (oldkey); + oldkey = NULL; + oldkeylen = 0; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + char *domain, *result, *outkey; + int len, keylen, parse_res; + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + /* Get the next entry until we found a correct one. */ + do + { + enum nss_status retval; + char *p; + + if (new_start) + retval = yperr2nss (yp_first (domain, "shadow.byname", + &outkey, &keylen, &result, &len)); + else + retval = yperr2nss ( yp_next (domain, "shadow.byname", + oldkey, oldkeylen, + &outkey, &keylen, &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_spent (p, sp, data, buflen); + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + + free (oldkey); + oldkey = outkey; + oldkeylen = keylen; + new_start = 0; + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nis_getspent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nis_getspnam_r (const char *name, struct spwd *sp, + char *buffer, size_t buflen) +{ + struct parser_data *data = (void *) buffer; + enum nss_status retval; + char *domain, *result, *p; + int len, parse_res; + + if (name == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + if (yp_get_default_domain (&domain)) + return NSS_STATUS_UNAVAIL; + + retval = yperr2nss (yp_match (domain, "shadow.byname", name, + strlen (name), &result, &len)); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + return retval; + } + + if ((size_t) (len + 1) > buflen) + { + free (result); + __set_errno (ERANGE); + return NSS_STATUS_TRYAGAIN; + } + + p = strncpy (buffer, result, len); + buffer[len] = '\0'; + while (isspace (*p)) + ++p; + free (result); + + parse_res = _nss_files_parse_spent (p, sp, data, buflen); + + if (parse_res == -1 && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else if (parse_res == 0) + return NSS_STATUS_NOTFOUND; + + return NSS_STATUS_SUCCESS; +} |