diff options
Diffstat (limited to 'nis/nss_nisplus')
-rw-r--r-- | nis/nss_nisplus/nisplus-alias.c | 251 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-ethers.c | 278 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-grp.c | 387 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-hosts.c | 412 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-netgrp.c | 141 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-network.c | 340 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-proto.c | 284 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-publickey.c | 347 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-pwd.c | 293 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-rpc.c | 284 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-service.c | 308 | ||||
-rw-r--r-- | nis/nss_nisplus/nisplus-spwd.c | 262 |
12 files changed, 3587 insertions, 0 deletions
diff --git a/nis/nss_nisplus/nisplus-alias.c b/nis/nss_nisplus/nisplus-alias.c new file mode 100644 index 0000000..b0f0351 --- /dev/null +++ b/nis/nss_nisplus/nisplus-alias.c @@ -0,0 +1,251 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <ctype.h> +#include <string.h> +#include <aliases.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +static int +_nss_nisplus_parse_aliasent (nis_result *result, struct aliasent *alias, + char *buffer, size_t buflen) +{ + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_len != 1 || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "mail_aliases") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 2) + return -1; + else + { + char *first_unused = buffer + NISENTRYLEN(0, 1, result) + 1; + size_t room_left = + buflen - (buflen % __alignof__ (char *)) - + NISENTRYLEN(0, 1, result) - 2; + char *line; + char *cp; + + if (NISENTRYLEN(0, 1, result) >= buflen) + { + /* The line is too long for our buffer. */ + no_more_room: + __set_errno (ERANGE); + return -1; + } + else + { + strncpy (buffer, NISENTRYVAL(0, 1, result), NISENTRYLEN(0, 1, result)); + buffer[NISENTRYLEN(0, 1, result)] = '\0'; + } + + if (NISENTRYLEN(0, 0, result) >= room_left) + goto no_more_room; + + alias->alias_local = 0; + alias->alias_members_len = 0; + *first_unused = '\0'; + ++first_unused; + strcpy (first_unused, NISENTRYVAL(0, 0, result)); + first_unused[NISENTRYLEN(0, 0, result)] = '\0'; + alias->alias_name = first_unused; + + /* Terminate the line for any case. */ + cp = strpbrk (alias->alias_name, "#\n"); + if (cp != NULL) + *cp = '\0'; + + first_unused += strlen (alias->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 *)); + alias->alias_members = (char **) first_unused; + + line = buffer; + + 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 *); + alias->alias_members[alias->alias_members_len] = line; + + while (*line != '\0' && *line != ',') + line++; + + if (line != alias->alias_members[alias->alias_members_len]) + { + *line = '\0'; + line++; + alias->alias_members_len++; + } + } + + return alias->alias_members_len == 0 ? 0 : 1; + } +} + +enum nss_status +_nss_nisplus_setaliasent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endaliasent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getaliasent_r (struct aliasent *alias, + char *buffer, size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames("mail_aliases.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry(names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res2; + + res2 = nis_next_entry(names[0], &result->cookie); + nis_freeresult (result); + result = res2; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_aliasent (result, alias, buffer, buflen); + } while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer, + size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getaliasent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL || strlen(name) > 8) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[strlen (name) + 30]; + + sprintf(buf, "[name=%s],mail_aliases.org_dir", name); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + + parse_res = _nss_nisplus_parse_aliasent (result, alias, buffer, buflen); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} diff --git a/nis/nss_nisplus/nisplus-ethers.c b/nis/nss_nisplus/nisplus-ethers.c new file mode 100644 index 0000000..ac3e069 --- /dev/null +++ b/nis/nss_nisplus/nisplus-ethers.c @@ -0,0 +1,278 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <ctype.h> +#include <string.h> +#include <libc-lock.h> +#include <netdb.h> +#include <netinet/ether.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> +#include <netinet/if_ether.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +/* Because the `ethers' lookup does not fit so well in the scheme so + we define a dummy struct here which helps us to use the available + functions. */ +struct etherent +{ + const char *e_name; + struct ether_addr e_addr; +}; +struct etherent_data {}; + +#define ENTNAME etherent +#define DATABASE "ethers" +#include "../../nss/nss_files/files-parse.c" +LINE_PARSER +("#", + /* Read the ethernet address: 6 x 8bit hexadecimal number. */ + { + size_t cnt; + + for (cnt = 0; cnt < 6; ++cnt) + { + unsigned int number; + + if (cnt < 5) + INT_FIELD (number, ISCOLON , 0, 16, (unsigned int)) + else + INT_FIELD (number, isspace, 0, 16, (unsigned int)) + + if (number > 0xff) + return 0; + result->e_addr.ether_addr_octet[cnt] = number; + } + }; + STRING_FIELD (result->e_name, isspace, 1); + ) + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +static int +_nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether, + char *buffer, size_t buflen) +{ + char *p = buffer; + size_t room_left = buflen; + struct parser_data *data = (void *) buffer; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_len != 1 || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "ethers_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 2) + return -1; + + memset (p, '\0', room_left); + + /* Generate the ether entry format and use the normal parser */ + if (NISENTRYLEN (0, 0, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result)); + room_left -= (NISENTRYLEN (0, 0, result) +1); + + if (NISENTRYLEN (0, 1, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, "\t"); + strncat (p, NISENTRYVAL (0, 1, result), NISENTRYLEN (0, 1, result)); + room_left -= (NISENTRYLEN (0, 1, result) + 1); + + return _nss_files_parse_etherent (p,ether, data, buflen); +} + +enum nss_status +_nss_nisplus_setetherent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endetherent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getetherent_r (struct etherent *ether, char *buffer, + size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames("ethers.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry(names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res2; + + res2 = nis_next_entry(names[0], &result->cookie); + nis_freeresult (result); + result = res2; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_etherent (result, ether, buffer, buflen); + } while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getetherent_r (struct etherent *result, char *buffer, + size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getetherent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_gethostton_r (const char *name, struct etherent *eth, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[strlen (name) + 255]; + + sprintf(buf, "[name=%s],ethers.org_dir", name); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + + parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, buflen); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +enum nss_status +_nss_nisplus_getntohost_r (const struct ether_addr *addr, + struct etherent *eth, + char *buffer, size_t buflen) +{ + int parse_res; + nis_result *result; + char buf[255]; + + if (addr == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + memset (&buf, '\0', sizeof (buf)); + snprintf(buf, sizeof (buf), "[addr=%x:%x:%x:%x:%x:%x],ethers.org_dir", + addr->ether_addr_octet[0], addr->ether_addr_octet[1], + addr->ether_addr_octet[2], addr->ether_addr_octet[3], + addr->ether_addr_octet[4], addr->ether_addr_octet[5]); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + + parse_res = _nss_nisplus_parse_etherent (result, eth, buffer, buflen); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; +} diff --git a/nis/nss_nisplus/nisplus-grp.c b/nis/nss_nisplus/nisplus-grp.c new file mode 100644 index 0000000..2e56afa --- /dev/null +++ b/nis/nss_nisplus/nisplus-grp.c @@ -0,0 +1,387 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <grp.h> +#include <ctype.h> +#include <errno.h> +#include <string.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock); + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define NISENTRYVAL(idx,col,res) \ +((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +#define STRUCTURE group +#define ENTNAME grent +struct grent_data {}; + +#define TRAILING_LIST_MEMBER gr_mem +#define TRAILING_LIST_SEPARATOR_P(c) ((c) == ',') +#include "../../nss/nss_files/files-parse.c" +LINE_PARSER +(, + STRING_FIELD (result->gr_name, ISCOLON, 0); + if (line[0] == '\0' + && (result->gr_name[0] == '+' || result->gr_name[0] == '-')) + { + result->gr_passwd = NULL; + result->gr_gid = 0; + } + else + { + STRING_FIELD (result->gr_passwd, ISCOLON, 0); + if (result->gr_name[0] == '+' || result->gr_name[0] == '-') + INT_FIELD_MAYBE_NULL (result->gr_gid, ISCOLON, 0, 10, , 0) + else + INT_FIELD (result->gr_gid, ISCOLON, 0, 10,) + } + ) + +static int +_nss_nisplus_parse_grent (nis_result * result, struct group *gr, + char *buffer, size_t buflen) +{ +#if 0 + /* XXX here is a bug, sometimes we get some special characters at the + end of a line */ + char *first_unused = buffer; + size_t room_left = buflen; + char *line; + int count; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_len != 1 || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "group_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4) + return -1; + + if (NISENTRYLEN (0, 0, result) >= room_left) + { + /* The line is too long for our buffer. */ + no_more_room: + __set_errno (ERANGE); + return -1; + } + + strncpy (first_unused, NISENTRYVAL (0, 0, result), + NISENTRYLEN (0, 0, result)); + first_unused[NISENTRYLEN (0, 0, result)] = '\0'; + gr->gr_name = first_unused; + room_left -= (strlen (first_unused) + 1); + first_unused += strlen (first_unused) + 1; + + if (NISENTRYLEN (0, 1, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL (0, 1, result), + NISENTRYLEN (0, 1, result)); + first_unused[NISENTRYLEN (0, 1, result)] = '\0'; + gr->gr_passwd = first_unused; + room_left -= (strlen (first_unused) + 1); + first_unused += strlen (first_unused) + 1; + + if (NISENTRYLEN (0, 2, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL (0, 2, result), + NISENTRYLEN (0, 2, result)); + first_unused[NISENTRYLEN (0, 2, result)] = '\0'; + gr->gr_gid = atoi (first_unused); + room_left -= (strlen (first_unused) + 1); + first_unused += strlen (first_unused) + 1; + + if (NISENTRYLEN (0, 3, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL (0, 3, result), + NISENTRYLEN (0, 3, result)); + first_unused[NISENTRYLEN (0, 3, result)] = '\0'; + line = first_unused; + room_left -= (strlen (line) + 1); + first_unused += strlen (line) + 1; + /* Adjust the pointer so it is aligned for + storing pointers. */ + first_unused += __alignof__ (char *) - 1; + first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *)); + gr->gr_mem = (char **) first_unused; + + count = 0; + 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 *); + gr->gr_mem[count] = line; + + while (*line != '\0' && *line != ',' && !isspace(*line)) + ++line; + + if (line != gr->gr_mem[count]) + { + *line = '\0'; + ++line; + ++count; + } + else + gr->gr_mem[count] = NULL; + } + if (room_left < sizeof (char *)) + goto no_more_room; + room_left -= sizeof (char *); + gr->gr_mem[count] = NULL; + + return 1; +#else + char *p = buffer; + size_t room_left = buflen; + struct parser_data *data = (void *) buffer; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_len != 1 || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "group_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4) + return -1; + + memset (p, '\0', room_left); + + if (NISENTRYLEN (0, 0, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result)); + room_left -= (NISENTRYLEN (0, 0, result) + 1); + strcat (p, ":"); + + if (NISENTRYLEN (0, 1, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncat (p, NISENTRYVAL (0, 1, result), NISENTRYLEN (0, 1, result)); + room_left -= (NISENTRYLEN (0, 1, result) + 1); + strcat (p, ":"); + if (NISENTRYLEN (0, 2, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncat (p, NISENTRYVAL (0, 2, result), NISENTRYLEN (0, 2, result)); + room_left -= (NISENTRYLEN (0, 2, result) + 1); + strcat (p, ":"); + if (NISENTRYLEN (0, 3, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncat (p, NISENTRYVAL (0, 3, result), NISENTRYLEN (0, 3, result)); + room_left -= (NISENTRYLEN (0, 3, result) + 1); + + return _nss_files_parse_grent (p, gr, data, buflen); +#endif +} + +enum nss_status +_nss_nisplus_setgrent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endgrent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames ("group.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry (names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res; + + res = nis_next_entry (names[0], &result->cookie); + nis_freeresult (result); + result = res; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen); + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getgrent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getgrnam_r (const char *name, struct group *gr, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL || strlen (name) > 8) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[strlen (name) + 24]; + + sprintf (buf, "[name=%s],group.org_dir", name); + + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +enum nss_status +_nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr, + char *buffer, size_t buflen) +{ + int parse_res; + nis_result *result; + char buf[36]; + + sprintf (buf, "[gid=%d],group.org_dir", gid); + + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_grent (result, gr, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; +} diff --git a/nis/nss_nisplus/nisplus-hosts.c b/nis/nss_nisplus/nisplus-hosts.c new file mode 100644 index 0000000..0a48641 --- /dev/null +++ b/nis/nss_nisplus/nisplus-hosts.c @@ -0,0 +1,412 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <netdb.h> +#include <errno.h> +#include <ctype.h> +#include <string.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +/* 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/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; + map_v4v6_hostent (result, &bufptr, &buflen); + } + + STRING_FIELD (result->h_name, isspace, 1); + } +) + + +static int +_nss_nisplus_parse_hostent (nis_result *result, struct hostent *host, + char *buffer, size_t buflen) +{ + char *p = buffer; + size_t room_left = buflen; + int parse_res, i; + struct parser_data *data = (void *) buffer; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "hosts_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4) + return -1; + + memset (p, '\0', room_left); + + /* Generate the hosts entry format and use the normal parser */ + if (NISENTRYLEN (0, 2, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncpy (p, NISENTRYVAL (0, 2, result), + NISENTRYLEN (0, 2, result)); + room_left -= (NISENTRYLEN (0, 2, result) + 1); + + if (NISENTRYLEN (0, 0, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, "\t"); + strncat (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result)); + room_left -= (NISENTRYLEN (0, 0, result) + 1); + /* + 1: We overwrite the last \0 */ + + for (i = 1; i < result->objects.objects_len; i++) + { + if (NISENTRYLEN (i, 1, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, " "); + strcat (p, NISENTRYVAL (i, 1, result)); + room_left -= (NISENTRYLEN (i, 1, result) + 1); + } + + parse_res = parse_line (p, host, data, buflen); + + return parse_res; +} + +enum nss_status +_nss_nisplus_sethostent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endhostent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_gethostent_r (struct hostent *host, char *buffer, + size_t buflen, int *herrnop) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames("hosts.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry(names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + int retval; + + retval = niserr2nss (result->status); + if (retval == NSS_STATUS_TRYAGAIN) + { + *herrnop = NETDB_INTERNAL; + __set_errno (EAGAIN); + } + return retval; + } + + } + else + { + nis_result *res2; + + res2 = nis_next_entry(names[0], &result->cookie); + nis_freeresult (result); + result = res2; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + int retval; + + retval = niserr2nss (result->status); + if (retval == NSS_STATUS_TRYAGAIN) + { + *herrnop = NETDB_INTERNAL; + __set_errno (EAGAIN); + } + return retval; + } + } + + parse_res = _nss_nisplus_parse_hostent (result, host, buffer, buflen); + if (!parse_res && errno == ERANGE) + { + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + } while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_gethostent_r (struct hostent *result, char *buffer, + size_t buflen, int *herrnop) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_gethostent_r (result, buffer, buflen, herrnop); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host, + char *buffer, size_t buflen, int *herrnop) +{ + int parse_res, retval; + + if (name == NULL) + { + __set_errno (EINVAL); + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_NOTFOUND; + } + else + { + nis_result *result; + char buf[strlen (name) + 255]; + + /* Search at first in the alias list, and use the correct name + for the next search */ + sprintf(buf, "[name=%s],hosts.org_dir", name); + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + /* If we do not find it, try it as original name. But if the + database is correct, we should find it in the first case, too */ + if ((result->status != NIS_SUCCESS && + result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "hosts_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len + < 3) + sprintf(buf, "[cname=%s],hosts.org_dir", name); + else + sprintf(buf, "[cname=%s],hosts.org_dir", NISENTRYVAL(0, 0, result)); + + nis_freeresult (result); + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + retval = niserr2nss (result->status); + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + __set_errno (EAGAIN); + *herrnop = NETDB_INTERNAL; + } + nis_freeresult (result); + return retval; + } + + parse_res = _nss_nisplus_parse_hostent (result, host, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + *herrnop = NETDB_INTERNAL; + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +enum nss_status +_nss_nisplus_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_nisplus_gethostbyname2_r (name, AF_INET6, host, buffer, + buflen, h_errnop); + if (status == NSS_STATUS_SUCCESS) + return status; + } + + return _nss_nisplus_gethostbyname2_r (name, AF_INET, host, buffer, + buflen, h_errnop); +} + +enum nss_status +_nss_nisplus_gethostbyaddr_r (const char *addr, struct hostent *host, + char *buffer, size_t buflen, int *herrnop) +{ + if (addr == NULL) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[24 + strlen (addr)]; + int retval, parse_res; + + sprintf(buf, "[addr=%s],hosts.org_dir", addr); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + retval = niserr2nss (result->status); + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + __set_errno (EAGAIN); + *herrnop = NETDB_INTERNAL; + } + nis_freeresult (result); + return retval; + } + + parse_res = _nss_nisplus_parse_hostent (result, host, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + *herrnop = NETDB_INTERNAL; + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} diff --git a/nis/nss_nisplus/nisplus-netgrp.c b/nis/nss_nisplus/nisplus-netgrp.c new file mode 100644 index 0000000..766d2bc --- /dev/null +++ b/nis/nss_nisplus/nisplus-netgrp.c @@ -0,0 +1,141 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <ctype.h> +#include <netdb.h> +#include <string.h> +#include <netgroup.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__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); + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +enum nss_status +_nss_nisplus_setnetgrent (char *group) + +{ + enum nss_status status; + nis_result *result; + char buf[strlen (group) + 30]; + int i; + size_t len; + + if (group == NULL || group[0] == '\0') + return NSS_STATUS_UNAVAIL; + + status = NSS_STATUS_SUCCESS; + + __libc_lock_lock (lock); + + if (data != NULL) + { + free (data); + data = NULL; + data_size = 0; + cursor = NULL; + } + + sprintf(buf, "[name=%s],netgroup.org_dir", group); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + status = niserr2nss (result->status); + + len = 0; + for (i = 0; i < result->objects.objects_len; i++) + len += 1 + NISENTRYLEN (i, 1, result) + 1 + NISENTRYLEN(i,2,result) + + 1 + NISENTRYLEN(i,3,result) + 1 + NISENTRYLEN(i,4,result) + 2; + + data = malloc (len+1); + memset (data, '\0', len+1); + + for (i = 0; i < result->objects.objects_len; i++) + { + strncat (data, NISENTRYVAL (i, 1, result), NISENTRYLEN (i, 1, result)); + strcat (data," ("); + strncat (data, NISENTRYVAL(i,2,result), NISENTRYLEN (i, 2, result)); + strcat (data, ","); + strncat (data, NISENTRYVAL(i,3,result), NISENTRYLEN (i, 3, result)); + strcat (data, ","); + strncat (data, NISENTRYVAL(i,4,result), NISENTRYLEN (i, 4, result)); + strcat (data, ") "); + } + + nis_freeresult (result); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_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_nisplus_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/nis/nss_nisplus/nisplus-network.c b/nis/nss_nisplus/nisplus-network.c new file mode 100644 index 0000000..28580b6 --- /dev/null +++ b/nis/nss_nisplus/nisplus-network.c @@ -0,0 +1,340 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <netdb.h> +#include <errno.h> +#include <ctype.h> +#include <string.h> +#include <arpa/inet.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +/* Get the declaration of the parser function. */ +#define ENTNAME netent +#define DATABASE "networks" +#define TRAILING_LIST_MEMBER n_aliases +#define TRAILING_LIST_SEPARATOR_P isspace +#include "../nss/nss_files/files-parse.c" +LINE_PARSER +("#", + { + char *addr; + + STRING_FIELD (result->n_name, isspace, 1); + + STRING_FIELD (addr, isspace, 1); + result->n_net = inet_network (addr); + + }) + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + + +static int +_nss_nisplus_parse_netent (nis_result *result, struct netent *network, + char *buffer, size_t buflen) +{ + char *p = buffer; + size_t room_left = buflen; + int i; + struct parser_data *data = (void *) buffer; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "networks_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3) + return -1; + + /* Generate the network entry format and use the normal parser */ + if (NISENTRYLEN (0, 0, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + + memset (p, '\0', room_left); + + strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result)); + room_left -= (NISENTRYLEN (0, 0, result) +1); + + if (NISENTRYLEN (0, 2, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, "\t"); + strncat (p, NISENTRYVAL (0, 2, result), NISENTRYLEN (0, 2, result)); + room_left -= (NISENTRYLEN (0, 2, result) + 1); + /* + 1: We overwrite the last \0 */ + + for (i = 1; i < result->objects.objects_len; i++) + /* XXX should we start with i = 0 or with i = 1 ? */ + { + if (NISENTRYLEN (i, 1, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, " "); + strncat (p, NISENTRYVAL (i, 1, result), NISENTRYLEN (i, 1, result)); + room_left -= (NISENTRYLEN (i, 1, result) + 1); + } + + return _nss_files_parse_netent (p, network, data, buflen); +} + +enum nss_status +_nss_nisplus_setnetent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endnetent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getnetent_r (struct netent *network, char *buffer, + size_t buflen, int *herrnop) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames("networks.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry(names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + int retval; + + retval = niserr2nss (result->status); + if (retval == NSS_STATUS_TRYAGAIN) + { + *herrnop = NETDB_INTERNAL; + __set_errno (EAGAIN); + } + return retval; + } + } + else + { + nis_result *res; + + res = nis_next_entry(names[0], &result->cookie); + nis_freeresult (result); + result = res; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + int retval; + + retval = niserr2nss (result->status); + if (retval == NSS_STATUS_TRYAGAIN) + { + *herrnop = NETDB_INTERNAL; + __set_errno (EAGAIN); + } + return retval; + } + } + + parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen); + if (!parse_res && errno == ERANGE) + { + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_TRYAGAIN; + } + + } while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getnetent_r (struct netent *result, char *buffer, + size_t buflen, int *herrnop) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getnetent_r (result, buffer, buflen, herrnop); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getnetbyname_r (const char *name, struct netent *network, + char *buffer, size_t buflen, int *herrnop) +{ + int parse_res, retval; + + if (name == NULL) + { + __set_errno (EINVAL); + *herrnop = NETDB_INTERNAL; + return NSS_STATUS_UNAVAIL; + } + else + { + nis_result *result; + char buf[strlen (name) + 255]; + + + /* Search at first in the alias list, and use the correct name + for the next search */ + sprintf(buf, "[name=%s],networks.org_dir", name); + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + /* If we do not find it, try it as original name. But if the + database is correct, we should find it in the first case, too */ + if ((result->status != NIS_SUCCESS && + result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "networks_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3) + sprintf(buf, "[cname=%s],networks.org_dir", name); + else + sprintf(buf, "[cname=%s],networks.org_dir", NISENTRYVAL(0, 0, result)); + + nis_freeresult (result); + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + retval = niserr2nss (result->status); + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + __set_errno (EAGAIN); + *herrnop = NETDB_INTERNAL; + } + nis_freeresult (result); + return retval; + } + + parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + *herrnop = NETDB_INTERNAL; + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +/* XXX type is ignored, SUN's NIS+ table doesn't support it */ +enum nss_status +_nss_nisplus_getnetbyaddr_r (const unsigned long addr, const int type, + struct netent *network, + char *buffer, size_t buflen, int *herrnop) +{ + int parse_res, retval; + nis_result *result; + char buf[1024]; + struct in_addr in; + + in = inet_makeaddr (addr, 0); + snprintf(buf, sizeof (buf) - 1, "[addr=%s],networks.org_dir", + inet_ntoa (in)); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + retval = niserr2nss (result->status); + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + { + __set_errno (EAGAIN); + *herrnop = NETDB_INTERNAL; + } + nis_freeresult (result); + return retval; + } + + parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + *herrnop = NETDB_INTERNAL; + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; +} diff --git a/nis/nss_nisplus/nisplus-proto.c b/nis/nss_nisplus/nisplus-proto.c new file mode 100644 index 0000000..b26cb82 --- /dev/null +++ b/nis/nss_nisplus/nisplus-proto.c @@ -0,0 +1,284 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <ctype.h> +#include <netdb.h> +#include <string.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define ENTNAME protoent +#define DATABASE "protocols" +#define TRAILING_LIST_MEMBER p_aliases +#define TRAILING_LIST_SEPARATOR_P isspace +#include "../../nss/nss_files/files-parse.c" +LINE_PARSER +("#", + STRING_FIELD (result->p_name, isspace, 1); + INT_FIELD (result->p_proto, isspace, 1, 10,); +) + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +static int +_nss_nisplus_parse_protoent (nis_result * result, struct protoent *proto, + char *buffer, size_t buflen) +{ + char *p = buffer; + size_t room_left = buflen; + int i; + struct parser_data *data = (void *) buffer; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "protocols_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3) + return -1; + + memset (p, '\0', room_left); + + /* Generate the protocols entry format and use the normal parser */ + if (NISENTRYLEN (0, 0, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result)); + room_left -= (NISENTRYLEN (0, 0, result) + 1); + + if (NISENTRYLEN (0, 2, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, "\t"); + strncat (p, NISENTRYVAL (0, 2, result), NISENTRYLEN (0, 2, result)); + room_left -= (NISENTRYLEN (0, 2, result) + 1); + /* + 1: We overwrite the last \0 */ + + for (i = 1; i < result->objects.objects_len; i++) + { + if (NISENTRYLEN (i, 1, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, " "); + strncat (p, NISENTRYVAL (i, 1, result), NISENTRYLEN (i, 1, result)); + room_left -= (NISENTRYLEN (i, 1, result) + 1); + } + + return _nss_files_parse_protoent (p, proto, data, buflen); +} + +enum nss_status +_nss_nisplus_setprotoent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endprotoent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getprotoent_r (struct protoent *proto, char *buffer, + size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames ("protocols.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry (names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res; + + res = nis_next_entry (names[0], &result->cookie); + nis_freeresult (result); + result = res; + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen); + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getprotoent_r (struct protoent *result, char *buffer, + size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getprotoent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getprotobyname_r (const char *name, struct protoent *proto, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[strlen (name) + 255]; + + /* Search at first in the alias list, and use the correct name + for the next search */ + sprintf (buf, "[name=%s],protocols.org_dir", name); + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + /* If we do not find it, try it as original name. But if the + database is correct, we should find it in the first case, too */ + if ((result->status != NIS_SUCCESS && + result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "protocols_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3) + sprintf (buf, "[cname=%s],protocols.org_dir", name); + else + sprintf (buf, "[cname=%s],protocols.org_dir", NISENTRYVAL (0, 0, result)); + + nis_freeresult (result); + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +enum nss_status +_nss_nisplus_getprotobynumber_r (const int number, struct protoent *proto, + char *buffer, size_t buflen) +{ + int parse_res; + nis_result *result; + char buf[46]; + + snprintf (buf, sizeof (buf), "[number=%d],protocols.org_dir", number); + + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_protoent (result, proto, buffer, buflen); + + nis_freeresult (result); + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; +} diff --git a/nis/nss_nisplus/nisplus-publickey.c b/nis/nss_nisplus/nisplus-publickey.c new file mode 100644 index 0000000..7312c91 --- /dev/null +++ b/nis/nss_nisplus/nisplus-publickey.c @@ -0,0 +1,347 @@ +/* Copyright (c) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <stdio.h> +#include <string.h> +#include <syslog.h> +#include <rpc/key_prot.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include <nss-nisplus.h> + +extern int xdecrypt (char *, char *); + +/* If we found the entry, we give a SUCCESS and an empty key back. */ +enum nss_status +_nss_nisplus_getpublickey (const char *netname, char *pkey) +{ + nis_result *res; + enum nss_status retval; + char buf[NIS_MAXNAMELEN+2]; + char *domain, *cptr; + int len; + + pkey[0] = 0; + + if (netname == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + domain = strchr (netname, '@'); + if (!domain) + return NSS_STATUS_UNAVAIL; + domain++; + + snprintf (buf, NIS_MAXNAMELEN, + "[auth_name=%s,auth_type=DES],cred.org_dir.%s", + netname, domain); + + if (buf[strlen (buf)-1] != '.') + strcat(buf, "."); + + res = nis_list(buf, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH, + NULL, NULL); + + retval = niserr2nss (res->status); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + nis_freeresult (res); + return retval; + } + + if (res->objects.objects_len > 1) + { + /* + * More than one principal with same uid? + * something wrong with cred table. Should be unique + * Warn user and continue. + */ + printf (_("DES entry for netname %s not unique\n"), netname); + nis_freeresult (res); + return NSS_STATUS_SUCCESS; + } + + len = ENTRY_LEN (res->objects.objects_val, 3); + memcpy (pkey, ENTRY_VAL (res->objects.objects_val,3), len); + pkey[len] = 0; + cptr = strchr (pkey, ':'); + if (cptr) + cptr[0] = '\0'; + nis_freeresult (res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getsecretkey (const char *netname, char *skey, char *passwd) +{ + nis_result *res; + enum nss_status retval; + char buf[NIS_MAXNAMELEN+2]; + char *domain, *cptr; + int len; + + skey[0] = 0; + + if (netname == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_UNAVAIL; + } + + domain = strchr (netname, '@'); + if (!domain) + return NSS_STATUS_UNAVAIL; + domain++; + + snprintf (buf, NIS_MAXNAMELEN, + "[auth_name=%s,auth_type=DES],cred.org_dir.%s", + netname, domain); + + if (buf[strlen(buf)-1] != '.') + strcat(buf, "."); + + res = nis_list (buf, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH, + NULL, NULL); + + retval = niserr2nss (res->status); + + if (retval != NSS_STATUS_SUCCESS) + { + if (retval == NSS_STATUS_TRYAGAIN) + __set_errno (EAGAIN); + nis_freeresult (res); + return retval; + } + + if (res->objects.objects_len > 1) + { + /* + * More than one principal with same uid? + * something wrong with cred table. Should be unique + * Warn user and continue. + */ + printf (_("DES entry for netname %s not unique\n"), netname); + nis_freeresult (res); + return NSS_STATUS_SUCCESS; + } + + len = ENTRY_LEN (res->objects.objects_val, 4); + memcpy (buf, ENTRY_VAL (res->objects.objects_val,4), len); + skey[len] = 0; + cptr = strchr (skey, ':'); + if (cptr) + cptr[0] = '\0'; + nis_freeresult (res); + + if (!xdecrypt (buf, passwd)) + return NSS_STATUS_SUCCESS; + + if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0) + return NSS_STATUS_SUCCESS; + + buf[HEXKEYBYTES] = 0; + strcpy (skey, buf); + + return NSS_STATUS_SUCCESS; +} + +/* Parse information from the passed string. + The format of the string passed is gid,grp,grp, ... */ +static enum nss_status +parse_grp_str (const char *s, gid_t *gidp, int *gidlenp, gid_t *gidlist) +{ + int gidlen; + + if (!s || (!isdigit (*s))) + { + syslog (LOG_ERR, "netname2user: missing group id list in '%s'.", s); + return NSS_STATUS_NOTFOUND; + } + + *gidp = (atoi (s)); + + gidlen = 0; + + while ((s = strchr (s, ',')) != NULL) + { + s++; + gidlist[gidlen++] = atoi (s); + } + *gidlenp = gidlen; + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp, + gid_t *gidp, int *gidlenp, gid_t *gidlist) +{ + char *domain; + nis_result *res; + char sname[NIS_MAXNAMELEN+1]; /* search criteria + table name */ + char principal[NIS_MAXNAMELEN+1]; + int len; + + /* 1. Get home domain of user. */ + domain = strchr (netname, '@'); + if (! domain) + return NSS_STATUS_UNAVAIL; + + domain++; /* skip '@' */ + + /* 2. Get user's nisplus principal name. */ + if ((strlen (netname) + strlen (domain)+45) > + (size_t) NIS_MAXNAMELEN) + return NSS_STATUS_UNAVAIL; + + snprintf (sname, NIS_MAXNAMELEN, + "[auth_name=%s,auth_type=DES],cred.org_dir.%s", + netname, domain); + if (sname[strlen (sname) - 1] != '.') + strcat(sname, "."); + + /* must use authenticated call here */ + /* XXX but we cant, for now. XXX */ + res = nis_list (sname, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH, + NULL, NULL); + switch(res->status) + { + case NIS_SUCCESS: + case NIS_S_SUCCESS: + break; /* go and do something useful */ + case NIS_NOTFOUND: + case NIS_PARTIAL: + case NIS_NOSUCHNAME: + case NIS_NOSUCHTABLE: + nis_freeresult (res); + return NSS_STATUS_NOTFOUND; + case NIS_S_NOTFOUND: + case NIS_TRYAGAIN: + syslog (LOG_ERR, "netname2user: (nis+ lookup): %s\n", + nis_sperrno (res->status)); + nis_freeresult (res); + return NSS_STATUS_TRYAGAIN; + default: + syslog (LOG_ERR, "netname2user: (nis+ lookup): %s\n", + nis_sperrno (res->status)); + nis_freeresult (res); + return NSS_STATUS_UNAVAIL; + } + + if (res->objects.objects_len > 1) + { + /* + * A netname belonging to more than one principal? + * Something wrong with cred table. should be unique. + * Warn user and continue. + */ + syslog (LOG_ALERT, + _("netname2user: DES entry for %s in directory %s not unique"), + netname, domain); + } + + len = ENTRY_LEN(res->objects.objects_val, 0); + strncpy(principal, ENTRY_VAL(res->objects.objects_val, 0), len); + principal[len] = '\0'; + nis_freeresult(res); + + if (principal[0] == '\0') + return NSS_STATUS_UNAVAIL; + + /* + * 3. Use principal name to look up uid/gid information in + * LOCAL entry in **local** cred table. + */ + domain = nis_local_directory (); + if ((strlen(principal)+strlen(domain)+45) > + (size_t) NIS_MAXNAMELEN) + { + syslog (LOG_ERR, _("netname2user: principal name '%s' too long"), + principal); + return NSS_STATUS_UNAVAIL; + } + sprintf(sname, "[cname=%s,auth_type=LOCAL],cred.org_dir.%s", + principal, domain); + if (sname[strlen(sname) - 1] != '.') + strcat(sname, "."); + + /* must use authenticated call here */ + /* XXX but we cant, for now. XXX */ + res = nis_list(sname, USE_DGRAM+NO_AUTHINFO+FOLLOW_LINKS+FOLLOW_PATH, + NULL, NULL); + switch(res->status) { + case NIS_NOTFOUND: + case NIS_PARTIAL: + case NIS_NOSUCHNAME: + case NIS_NOSUCHTABLE: + nis_freeresult (res); + return NSS_STATUS_NOTFOUND; + case NIS_S_NOTFOUND: + case NIS_TRYAGAIN: + syslog (LOG_ERR, + "netname2user: (nis+ lookup): %s\n", + nis_sperrno (res->status)); + nis_freeresult (res); + return NSS_STATUS_TRYAGAIN; + case NIS_SUCCESS: + case NIS_S_SUCCESS: + break; /* go and do something useful */ + default: + syslog (LOG_ERR, "netname2user: (nis+ lookup): %s\n", + nis_sperrno (res->status)); + nis_freeresult (res); + return NSS_STATUS_UNAVAIL; + } + + if (res->objects.objects_len > 1) + { + /* + * A principal can have more than one LOCAL entry? + * Something wrong with cred table. + * Warn user and continue. + */ + syslog(LOG_ALERT, + _("netname2user: LOCAL entry for %s in directory %s not unique"), + netname, domain); + } + /* Fetch the uid */ + *uidp = (atoi (ENTRY_VAL (res->objects.objects_val, 2))); + + if (*uidp == 0) + { + syslog (LOG_ERR, _("netname2user: should not have uid 0")); + return NSS_STATUS_NOTFOUND; + } + + parse_grp_str (ENTRY_VAL (res->objects.objects_val, 3), + gidp, gidlenp, gidlist); + + nis_freeresult (res); + return NSS_STATUS_SUCCESS; +} diff --git a/nis/nss_nisplus/nisplus-pwd.c b/nis/nss_nisplus/nisplus-pwd.c new file mode 100644 index 0000000..3717d5e --- /dev/null +++ b/nis/nss_nisplus/nisplus-pwd.c @@ -0,0 +1,293 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <pwd.h> +#include <string.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +static int +_nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw, + char *buffer, size_t buflen) +{ + char *first_unused = buffer; + size_t room_left = buflen; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_len != 1 || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "passwd_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 7) + return -1; + + if (NISENTRYLEN(0, 0, result) >= room_left) + { + /* The line is too long for our buffer. */ + no_more_room: + __set_errno (ERANGE); + return -1; + } + + strncpy (first_unused, NISENTRYVAL(0, 0, result), + NISENTRYLEN (0, 0, result)); + first_unused[NISENTRYLEN(0, 0, result)] = '\0'; + pw->pw_name = first_unused; + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + if (NISENTRYLEN(0, 1, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL(0, 1, result), + NISENTRYLEN (0, 1, result)); + first_unused[NISENTRYLEN(0, 1, result)] = '\0'; + pw->pw_passwd = first_unused; + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + if (NISENTRYLEN(0, 2, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL (0, 2, result), + NISENTRYLEN (0, 2, result)); + first_unused[NISENTRYLEN(0, 2, result)] = '\0'; + pw->pw_uid = atoi (first_unused); + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + if (NISENTRYLEN(0, 3, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL(0, 3, result), + NISENTRYLEN (0, 3, result)); + first_unused[NISENTRYLEN(0, 3, result)] = '\0'; + pw->pw_gid = atoi (first_unused); + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + if (NISENTRYLEN(0, 4, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL(0, 4, result), + NISENTRYLEN (0, 4, result)); + first_unused[NISENTRYLEN(0, 4, result)] = '\0'; + pw->pw_gecos = first_unused; + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + if (NISENTRYLEN(0, 5, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL (0, 5, result), + NISENTRYLEN (0, 5, result)); + first_unused[NISENTRYLEN(0, 5, result)] = '\0'; + pw->pw_dir = first_unused; + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + if (NISENTRYLEN(0, 6, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL (0, 6, result), + NISENTRYLEN (0, 6, result)); + first_unused[NISENTRYLEN (0, 6, result)] = '\0'; + pw->pw_shell = first_unused; + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + return 1; +} + +enum nss_status +_nss_nisplus_setpwent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endpwent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames ("passwd.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry(names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res; + + res = nis_next_entry(names[0], &result->cookie); + nis_freeresult (result); + result = res; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen); + } while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getpwent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getpwnam_r (const char *name, struct passwd *pw, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL || strlen (name) > 8) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[strlen (name) + 24]; + + sprintf(buf, "[name=%s],passwd.org_dir", name); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +enum nss_status +_nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw, + char *buffer, size_t buflen) +{ + int parse_res; + nis_result *result; + char buf[100]; + + sprintf(buf, "[uid=%d],passwd.org_dir", uid); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen); + + nis_freeresult (result); + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; +} diff --git a/nis/nss_nisplus/nisplus-rpc.c b/nis/nss_nisplus/nisplus-rpc.c new file mode 100644 index 0000000..326f262 --- /dev/null +++ b/nis/nss_nisplus/nisplus-rpc.c @@ -0,0 +1,284 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <ctype.h> +#include <string.h> +#include <libc-lock.h> +#include <rpc/netdb.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define ENTNAME rpcent +#define DATABASE "rpc" +#define TRAILING_LIST_MEMBER r_aliases +#define TRAILING_LIST_SEPARATOR_P isspace +#include "../../nss/nss_files/files-parse.c" +LINE_PARSER +("#", + STRING_FIELD (result->r_name, isspace, 1); + INT_FIELD (result->r_number, isspace, 1, 10,); + ) + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +static int +_nss_nisplus_parse_rpcent (nis_result *result, struct rpcent *rpc, + char *buffer, size_t buflen) +{ + char *p = buffer; + size_t room_left = buflen; + int i; + struct parser_data *data = (void *) buffer; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "rpc_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3) + return -1; + + memset (p, '\0', room_left); + + /* Generate the rpc entry format and use the normal parser */ + if (NISENTRYLEN (0, 0, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result)); + room_left -= (NISENTRYLEN (0, 0, result) +1); + + if (NISENTRYLEN (0, 2, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, "\t"); + strncat (p, NISENTRYVAL (0, 2, result), NISENTRYLEN (0, 2, result)); + room_left -= (NISENTRYLEN (0, 2, result) + 1); + /* + 1: We overwrite the last \0 */ + + for (i = 0; i < result->objects.objects_len; i++) + /* XXX should we start with i = 0 or with i = 1 ? */ + { + if (NISENTRYLEN (i, 1, result) +1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, " "); + strncat (p, NISENTRYVAL (i, 1, result), NISENTRYLEN (i, 1, result)); + room_left -= (NISENTRYLEN (i, 1, result) + 1); + } + + return _nss_files_parse_rpcent (p, rpc, data, buflen); +} + +enum nss_status +_nss_nisplus_setrpcent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endrpcent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getrpcent_r (struct rpcent *rpc, char *buffer, + size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames ("rpc.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry(names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res; + + res = nis_next_entry (names[0], &result->cookie); + nis_freeresult (result); + result = res; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen); + } while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getrpcent_r (struct rpcent *result, char *buffer, + size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getrpcent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getrpcbyname_r (const char *name, struct rpcent *rpc, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[strlen (name) + 255]; + + /* Search at first in the alias list, and use the correct name + for the next search */ + sprintf (buf, "[name=%s],rpc.org_dir", name); + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + /* If we do not find it, try it as original name. But if the + database is correct, we should find it in the first case, too */ + if ((result->status != NIS_SUCCESS && + result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "rpc_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 3) + sprintf (buf, "[cname=%s],rpc.org_dir", name); + else + sprintf (buf, "[cname=%s],rpc.org_dir", NISENTRYVAL(0, 0, result)); + + nis_freeresult (result); + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +enum nss_status +_nss_nisplus_getrpcbynumber_r (const int number, struct rpcent *rpc, + char *buffer, size_t buflen) +{ + int parse_res; + nis_result *result; + char buf[100]; + + snprintf (buf, sizeof (buf), "[number=%d],rpc.org_dir", number); + + result = nis_list(buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_rpcent (result, rpc, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; +} diff --git a/nis/nss_nisplus/nisplus-service.c b/nis/nss_nisplus/nisplus-service.c new file mode 100644 index 0000000..54f4f87 --- /dev/null +++ b/nis/nss_nisplus/nisplus-service.c @@ -0,0 +1,308 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <ctype.h> +#include <netdb.h> +#include <string.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock); + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define ENTNAME servent +#define DATABASE "services" +#define TRAILING_LIST_MEMBER s_aliases +#define TRAILING_LIST_SEPARATOR_P isspace +#include "../../nss/nss_files/files-parse.c" +#define ISSLASH(c) ((c) == '/') +LINE_PARSER +("#", + STRING_FIELD (result->s_name, isspace, 1); + INT_FIELD (result->s_port, ISSLASH, 10, 0, htons); + STRING_FIELD (result->s_proto, isspace, 1); + ) + + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +static int +_nss_nisplus_parse_servent (nis_result *result, struct servent *serv, + char *buffer, size_t buflen) +{ + char *p = buffer; + size_t room_left = buflen; + int i; + struct parser_data *data = (void *) buffer; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "services_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4) + return -1; + + memset (p, '\0', room_left); + + /* Generate the services entry format and use the normal parser */ + if (NISENTRYLEN (0, 0, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strncpy (p, NISENTRYVAL (0, 0, result), NISENTRYLEN (0, 0, result)); + room_left -= (NISENTRYLEN (0, 0, result) + 1); + + if (NISENTRYLEN (0, 3, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, "\t"); + strncat (p, NISENTRYVAL (0, 3, result), NISENTRYLEN (0, 3, result)); + room_left -= (NISENTRYLEN (0, 3, result) + 1); + if (NISENTRYLEN (0, 2, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, "/"); + strncat (p, NISENTRYVAL (0, 2, result), NISENTRYLEN (0, 2, result)); + room_left -= (NISENTRYLEN (0, 2, result) + 1); + + for (i = 1; i < result->objects.objects_len; i++) + { + if (NISENTRYLEN (i, 1, result) + 1 > room_left) + { + __set_errno (ERANGE); + return -1; + } + strcat (p, " "); + strcat (p, NISENTRYVAL (i, 1, result)); + room_left -= (NISENTRYLEN (i, 1, result) + 1); + } + + return _nss_files_parse_servent (p, serv, data, buflen); +} + +enum nss_status +_nss_nisplus_setservent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endservent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getservent_r (struct servent *serv, char *buffer, + size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames ("services.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry (names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res; + + res = nis_next_entry (names[0], &result->cookie); + nis_freeresult (result); + result = res; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen); + } + while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getservent_r (struct servent *result, char *buffer, + size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getservent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getservbyname_r (const char *name, const char *protocol, + struct servent *serv, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL || protocol == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_NOTFOUND; + } + else + { + nis_result *result; + char buf[strlen (name) + 255]; + + /* Search at first in the alias list, and use the correct name + for the next search */ + sprintf (buf, "[name=%s,proto=%s],services.org_dir", name, + protocol); + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + /* If we do not find it, try it as original name. But if the + database is correct, we should find it in the first case, too */ + if ((result->status != NIS_SUCCESS && + result->status != NIS_S_SUCCESS) || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "services_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 4) + sprintf (buf, "[cname=%s,proto=%s],services.org_dir", name, protocol); + else + sprintf (buf, "[cname=%s,proto=%s],services.org_dir", + NISENTRYVAL (0, 0, result), protocol); + + nis_freeresult (result); + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} + +enum nss_status +_nss_nisplus_getservbynumber_r (const int number, const char *protocol, + struct servent *serv, + char *buffer, size_t buflen) +{ + int parse_res; + nis_result *result; + char buf[60 + strlen (protocol)]; + + if (protocol == NULL) + { + __set_errno (EINVAL); + return NSS_STATUS_NOTFOUND; + } + + snprintf (buf, sizeof (buf), "[number=%d,proto=%s],services.org_dir", + number, protocol); + + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; +} diff --git a/nis/nss_nisplus/nisplus-spwd.c b/nis/nss_nisplus/nisplus-spwd.c new file mode 100644 index 0000000..ec4b5b3 --- /dev/null +++ b/nis/nss_nisplus/nisplus-spwd.c @@ -0,0 +1,262 @@ +/* Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. + + 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 <errno.h> +#include <shadow.h> +#include <string.h> +#include <libc-lock.h> +#include <rpcsvc/nis.h> +#include <rpcsvc/nislib.h> + +#include "nss-nisplus.h" + +__libc_lock_define_initialized (static, lock) + +static nis_result *result = NULL; +static nis_name *names = NULL; + +#define NISENTRYVAL(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val) + +#define NISENTRYLEN(idx,col,res) \ + ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len) + +static int +_nss_nisplus_parse_spent (nis_result *result, struct spwd *sp, + char *buffer, size_t buflen) +{ + char *first_unused = buffer; + size_t room_left = buflen; + char *line, *cp; + + if (result == NULL) + return -1; + + if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || + result->objects.objects_len != 1 || + result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ || + strcmp (result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type, + "passwd_tbl") != 0 || + result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 8) + return -1; + + if (NISENTRYLEN(0, 0, result) >= room_left) + { + /* The line is too long for our buffer. */ + no_more_room: + __set_errno (ERANGE); + return -1; + } + + strncpy (first_unused, NISENTRYVAL (0, 0, result), + NISENTRYLEN (0, 0, result)); + first_unused[NISENTRYLEN(0, 0, result)] = '\0'; + sp->sp_namp = first_unused; + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + if (NISENTRYLEN(0, 1, result) >= room_left) + goto no_more_room; + + strncpy (first_unused, NISENTRYVAL (0, 1, result), + NISENTRYLEN (0, 1, result)); + first_unused[NISENTRYLEN(0, 1, result)] = '\0'; + sp->sp_pwdp = first_unused; + room_left -= (strlen (first_unused) +1); + first_unused += strlen (first_unused) +1; + + sp->sp_lstchg = sp->sp_min = sp->sp_max = sp->sp_warn = sp->sp_inact = + sp->sp_expire = sp->sp_flag = -1; + + line = NISENTRYVAL (0, 7, result); + cp = strchr (line, ':'); + if (cp == NULL) + return 0; + *cp++ = '\0'; + sp->sp_lstchg = atoi (line); + + line = cp; + cp = strchr (line, ':'); + if (cp == NULL) + return 0; + *cp++ = '\0'; + sp->sp_min = atoi(line); + + line = cp; + cp = strchr (line, ':'); + if (cp == NULL) + return 0; + *cp++ = '\0'; + sp->sp_max = atoi(line); + + line = cp; + cp = strchr (line, ':'); + if (cp == NULL) + return 0; + *cp++ = '\0'; + sp->sp_warn = atoi(line); + + line = cp; + cp = strchr (line, ':'); + if (cp == NULL) + return 0; + *cp++ = '\0'; + sp->sp_inact = atoi(line); + + line = cp; + cp = strchr (line, ':'); + if (cp == NULL) + return 0; + *cp++ = '\0'; + sp->sp_expire = atoi(line); + + line = cp; + if (line == NULL) + return 0; + sp->sp_flag = atoi(line); + + return 1; +} + +enum nss_status +_nss_nisplus_setspent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_endspent (void) +{ + __libc_lock_lock (lock); + + if (result) + nis_freeresult (result); + result = NULL; + if (names) + { + nis_freenames (names); + names = NULL; + } + + __libc_lock_unlock (lock); + + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen) +{ + int parse_res; + + /* Get the next entry until we found a correct one. */ + do + { + if (result == NULL) + { + names = nis_getnames ("passwd.org_dir"); + if (names == NULL || names[0] == NULL) + return NSS_STATUS_UNAVAIL; + + result = nis_first_entry (names[0]); + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + else + { + nis_result *res; + + res = nis_next_entry (names[0], &result->cookie); + nis_freeresult (result); + result = res; + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); + } + + parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen); + } while (!parse_res); + + return NSS_STATUS_SUCCESS; +} + +enum nss_status +_nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen) +{ + int status; + + __libc_lock_lock (lock); + + status = internal_nisplus_getspent_r (result, buffer, buflen); + + __libc_lock_unlock (lock); + + return status; +} + +enum nss_status +_nss_nisplus_getspnam_r (const char *name, struct spwd *sp, + char *buffer, size_t buflen) +{ + int parse_res; + + if (name == NULL || strlen (name) > 8) + return NSS_STATUS_NOTFOUND; + else + { + nis_result *result; + char buf[strlen (name) + 24]; + + sprintf (buf, "[name=%s],passwd.org_dir", name); + + result = nis_list (buf, EXPAND_NAME, NULL, NULL); + + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen); + + nis_freeresult (result); + + if (parse_res) + return NSS_STATUS_SUCCESS; + + if (!parse_res && errno == ERANGE) + return NSS_STATUS_TRYAGAIN; + else + return NSS_STATUS_NOTFOUND; + } +} |