]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nss_db/db-netgrp.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / nss / nss_db / db-netgrp.c
CommitLineData
a68b0d31 1/* Netgroup file parser in nss_db modules.
2666d441 2 Copyright (C) 1996, 1997, 1999, 2000, 2011 Free Software Foundation, Inc.
19361cb7
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
19361cb7
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
19361cb7 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
a68b0d31 19
2666d441 20#include <ctype.h>
9a6450d5 21#include <dlfcn.h>
a68b0d31
UD
22#include <errno.h>
23#include <fcntl.h>
9a6450d5 24#include <netgroup.h>
46ec036d 25#include <string.h>
5107cf1d 26#include <bits/libc-lock.h>
a68b0d31 27#include <paths.h>
9a6450d5 28
a68b0d31 29#include "nsswitch.h"
9a6450d5 30#include "nss_db.h"
a68b0d31 31
2666d441
UD
32/* The hashing function we use. */
33#include "../intl/hash-string.h"
a68b0d31
UD
34
35
2666d441 36#define DBFILE _PATH_VARDB "netgroup.db"
a68b0d31
UD
37\f
38/* Maintenance of the shared handle open on the database. */
a68b0d31 39enum nss_status
2666d441 40_nss_db_setnetgrent (const char *group, struct __netgrent *result)
a68b0d31 41{
2666d441
UD
42 struct nss_db_map state;
43 enum nss_status status = internal_setent (DBFILE, &state);
a68b0d31
UD
44
45 if (status == NSS_STATUS_SUCCESS)
46 {
2666d441
UD
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);
56
57 status = NSS_STATUS_NOTFOUND;
58 while (hashtable[hidx] != ~((stridx_t) 0))
59 {
60 const char *valstr = valstrtab + hashtable[hidx];
61
62 if (strncmp (valstr, group, grouplen) == 0
63 && isblank (valstr[grouplen]))
64 {
65 const char *cp = &valstr[grouplen + 1];
66 while (isblank (*cp))
67 ++cp;
68 if (*cp != '\0')
69 {
70 result->data = strdup (cp);
71 if (result->data == NULL)
72 status = NSS_STATUS_TRYAGAIN;
73 else
74 {
75 status = NSS_STATUS_SUCCESS;
76 result->cursor = result->data;
77 }
78 break;
79 }
80 }
81
82 if ((hidx += hval2) >= header->dbs[0].hashsize)
83 hidx -= header->dbs[0].hashsize;
84 }
85
86 internal_endent (&state);
a68b0d31
UD
87 }
88
a68b0d31
UD
89 return status;
90
91}
92
93
94enum nss_status
2666d441 95_nss_db_endnetgrent (struct __netgrent *result)
a68b0d31 96{
2666d441
UD
97 free (result->data);
98 result->data = NULL;
99 result->data_size = 0;
100 result->cursor = NULL;
a68b0d31
UD
101 return NSS_STATUS_SUCCESS;
102}
103
104
105extern enum nss_status _nss_netgroup_parseline (char **cursor,
106 struct __netgrent *result,
d71b808a
UD
107 char *buffer, size_t buflen,
108 int *errnop);
a68b0d31
UD
109
110enum nss_status
d71b808a
UD
111_nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
112 int *errnop)
a68b0d31 113{
2666d441 114 enum nss_status status;
a68b0d31 115
2666d441
UD
116 status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
117 errnop);
a68b0d31
UD
118
119 return status;
120}