Replace FSF snail mail address with URLs.
[glibc.git] / nss / nss_db / db-netgrp.c
blob37705b05a141e9a188eef800ed0ca0a27aa61b05
1 /* Netgroup file parser in nss_db modules.
2 Copyright (C) 1996, 1997, 1999, 2000, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <ctype.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <netgroup.h>
25 #include <string.h>
26 #include <bits/libc-lock.h>
27 #include <paths.h>
29 #include "nsswitch.h"
30 #include "nss_db.h"
32 /* The hashing function we use. */
33 #include "../intl/hash-string.h"
36 #define DBFILE _PATH_VARDB "netgroup.db"
38 /* Maintenance of the shared handle open on the database. */
39 enum nss_status
40 _nss_db_setnetgrent (const char *group, struct __netgrent *result)
42 struct nss_db_map state;
43 enum nss_status status = internal_setent (DBFILE, &state);
45 if (status == NSS_STATUS_SUCCESS)
47 const struct nss_db_header *header = state.header;
48 const stridx_t *hashtable
49 = (const stridx_t *) ((const char *) header
50 + header->dbs[0].hashoffset);
51 const char *valstrtab = (const char *) header + header->valstroffset;
52 uint32_t hashval = __hash_string (group);
53 size_t grouplen = strlen (group);
54 size_t hidx = hashval % header->dbs[0].hashsize;
55 size_t hval2 = 1 + hashval % (header->dbs[0].hashsize - 2);
57 status = NSS_STATUS_NOTFOUND;
58 while (hashtable[hidx] != ~((stridx_t) 0))
60 const char *valstr = valstrtab + hashtable[hidx];
62 if (strncmp (valstr, group, grouplen) == 0
63 && isblank (valstr[grouplen]))
65 const char *cp = &valstr[grouplen + 1];
66 while (isblank (*cp))
67 ++cp;
68 if (*cp != '\0')
70 result->data = strdup (cp);
71 if (result->data == NULL)
72 status = NSS_STATUS_TRYAGAIN;
73 else
75 status = NSS_STATUS_SUCCESS;
76 result->cursor = result->data;
78 break;
82 if ((hidx += hval2) >= header->dbs[0].hashsize)
83 hidx -= header->dbs[0].hashsize;
86 internal_endent (&state);
89 return status;
94 enum nss_status
95 _nss_db_endnetgrent (struct __netgrent *result)
97 free (result->data);
98 result->data = NULL;
99 result->data_size = 0;
100 result->cursor = NULL;
101 return NSS_STATUS_SUCCESS;
105 extern enum nss_status _nss_netgroup_parseline (char **cursor,
106 struct __netgrent *result,
107 char *buffer, size_t buflen,
108 int *errnop);
110 enum nss_status
111 _nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
112 int *errnop)
114 enum nss_status status;
116 status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
117 errnop);
119 return status;