/* Netgroup file parser in nss_db modules.
- Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+ Copyright (C) 1996-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
The GNU C Library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public License as
- published by the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 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.
+ Lesser 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. */
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
-#include <db.h>
+#include <ctype.h>
+#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
+#include <netgroup.h>
#include <string.h>
-#include <bits/libc-lock.h>
+#include <stdint.h>
+#include <libc-lock.h>
#include <paths.h>
-#include "nsswitch.h"
-#include "netgroup.h"
+#include <stdlib.h>
+#include "nsswitch.h"
+#include "nss_db.h"
-#define DBFILE _PATH_VARDB "netgroup.db"
+/* The hashing function we use. */
+#include "../intl/hash-string.h"
-/* Locks the static variables in this file. */
-__libc_lock_define_initialized (static, lock)
+#define DBFILE _PATH_VARDB "netgroup.db"
\f
/* Maintenance of the shared handle open on the database. */
-static DB *db;
-static char *entry;
-static char *cursor;
-
enum nss_status
-_nss_db_setnetgrent (const char *group)
+_nss_db_setnetgrent (const char *group, struct __netgrent *result)
{
- enum nss_status status = NSS_STATUS_SUCCESS;
-
- __libc_lock_lock (lock);
+ struct nss_db_map state;
+ enum nss_status status = internal_setent (DBFILE, &state);
- /* Make sure the data base file is open. */
- if (db == NULL)
+ if (status == NSS_STATUS_SUCCESS)
{
- db = __dbopen (DBFILE, O_RDONLY, 0, DB_BTREE, NULL);
-
- if (db == NULL)
- status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
- else
+ const struct nss_db_header *header = state.header;
+ const stridx_t *hashtable
+ = (const stridx_t *) ((const char *) header
+ + header->dbs[0].hashoffset);
+ const char *valstrtab = (const char *) header + header->valstroffset;
+ uint32_t hashval = __hash_string (group);
+ size_t grouplen = strlen (group);
+ size_t hidx = hashval % header->dbs[0].hashsize;
+ size_t hval2 = 1 + hashval % (header->dbs[0].hashsize - 2);
+
+ status = NSS_STATUS_NOTFOUND;
+ while (hashtable[hidx] != ~((stridx_t) 0))
{
- /* We have to make sure the file is `closed on exec'. */
- int result, flags;
+ const char *valstr = valstrtab + hashtable[hidx];
- result = flags = fcntl ((*db->fd) (db), F_GETFD, 0);
- if (result >= 0)
- {
- flags |= FD_CLOEXEC;
- result = fcntl ((*db->fd) (db), F_SETFD, flags);
- }
- if (result < 0)
+ if (strncmp (valstr, group, grouplen) == 0
+ && isblank (valstr[grouplen]))
{
- /* Something went wrong. Close the stream and return a
- failure. */
- (*db->close) (db);
- db = NULL;
- status = NSS_STATUS_UNAVAIL;
+ const char *cp = &valstr[grouplen + 1];
+ while (isblank (*cp))
+ ++cp;
+ if (*cp != '\0')
+ {
+ result->data = strdup (cp);
+ if (result->data == NULL)
+ status = NSS_STATUS_TRYAGAIN;
+ else
+ {
+ status = NSS_STATUS_SUCCESS;
+ result->cursor = result->data;
+ }
+ break;
+ }
}
- }
- }
- if (status == NSS_STATUS_SUCCESS)
- {
- DBT key = { data: (void *) group, size: strlen (group) };
- DBT value;
+ if ((hidx += hval2) >= header->dbs[0].hashsize)
+ hidx -= header->dbs[0].hashsize;
+ }
- if ((*db->get) (db, &key, &value, 0) != 0)
- status = NSS_STATUS_NOTFOUND;
- else
- cursor = entry = value.data;
+ internal_endent (&state);
}
- __libc_lock_unlock (lock);
-
return status;
}
enum nss_status
-_nss_db_endnetgrent (void)
+_nss_db_endnetgrent (struct __netgrent *result)
{
- __libc_lock_lock (lock);
-
- if (db != NULL)
- {
- (*db->close) (db);
- db = NULL;
- }
-
- __libc_lock_unlock (lock);
-
+ free (result->data);
+ result->data = NULL;
+ result->data_size = 0;
+ result->cursor = NULL;
return NSS_STATUS_SUCCESS;
}
extern enum nss_status _nss_netgroup_parseline (char **cursor,
struct __netgrent *result,
- char *buffer, int buflen);
+ char *buffer, size_t buflen,
+ int *errnop);
enum nss_status
-_nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, int buflen)
+_nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
+ int *errnop)
{
- int status;
-
- __libc_lock_lock (lock);
-
- status = _nss_netgroup_parseline (&cursor, result, buffer, buflen);
+ enum nss_status status;
- __libc_lock_unlock (lock);
+ status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
+ errnop);
return status;
}