Update copyright notices with scripts/update-copyrights
[glibc.git] / nss / nss_db / db-netgrp.c
blob8c8fbd8180e1f160afc3250e143d6138fdab41c8
1 /* Netgroup file parser in nss_db modules.
2 Copyright (C) 1996-2014 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 <stdint.h>
27 #include <bits/libc-lock.h>
28 #include <paths.h>
29 #include <stdlib.h>
31 #include "nsswitch.h"
32 #include "nss_db.h"
34 /* The hashing function we use. */
35 #include "../intl/hash-string.h"
38 #define DBFILE _PATH_VARDB "netgroup.db"
40 /* Maintenance of the shared handle open on the database. */
41 enum nss_status
42 _nss_db_setnetgrent (const char *group, struct __netgrent *result)
44 struct nss_db_map state;
45 enum nss_status status = internal_setent (DBFILE, &state);
47 if (status == NSS_STATUS_SUCCESS)
49 const struct nss_db_header *header = state.header;
50 const stridx_t *hashtable
51 = (const stridx_t *) ((const char *) header
52 + header->dbs[0].hashoffset);
53 const char *valstrtab = (const char *) header + header->valstroffset;
54 uint32_t hashval = __hash_string (group);
55 size_t grouplen = strlen (group);
56 size_t hidx = hashval % header->dbs[0].hashsize;
57 size_t hval2 = 1 + hashval % (header->dbs[0].hashsize - 2);
59 status = NSS_STATUS_NOTFOUND;
60 while (hashtable[hidx] != ~((stridx_t) 0))
62 const char *valstr = valstrtab + hashtable[hidx];
64 if (strncmp (valstr, group, grouplen) == 0
65 && isblank (valstr[grouplen]))
67 const char *cp = &valstr[grouplen + 1];
68 while (isblank (*cp))
69 ++cp;
70 if (*cp != '\0')
72 result->data = strdup (cp);
73 if (result->data == NULL)
74 status = NSS_STATUS_TRYAGAIN;
75 else
77 status = NSS_STATUS_SUCCESS;
78 result->cursor = result->data;
80 break;
84 if ((hidx += hval2) >= header->dbs[0].hashsize)
85 hidx -= header->dbs[0].hashsize;
88 internal_endent (&state);
91 return status;
96 enum nss_status
97 _nss_db_endnetgrent (struct __netgrent *result)
99 free (result->data);
100 result->data = NULL;
101 result->data_size = 0;
102 result->cursor = NULL;
103 return NSS_STATUS_SUCCESS;
107 extern enum nss_status _nss_netgroup_parseline (char **cursor,
108 struct __netgrent *result,
109 char *buffer, size_t buflen,
110 int *errnop);
112 enum nss_status
113 _nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
114 int *errnop)
116 enum nss_status status;
118 status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
119 errnop);
121 return status;