1 /* Netgroup file parser in nss_db modules.
2 Copyright (C) 1996-2017 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/>. */
27 #include <libc-lock.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. */
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];
72 result
->data
= strdup (cp
);
73 if (result
->data
== NULL
)
74 status
= NSS_STATUS_TRYAGAIN
;
77 status
= NSS_STATUS_SUCCESS
;
78 result
->cursor
= result
->data
;
84 if ((hidx
+= hval2
) >= header
->dbs
[0].hashsize
)
85 hidx
-= header
->dbs
[0].hashsize
;
88 internal_endent (&state
);
97 _nss_db_endnetgrent (struct __netgrent
*result
)
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
,
113 _nss_db_getnetgrent_r (struct __netgrent
*result
, char *buffer
, size_t buflen
,
116 enum nss_status status
;
118 status
= _nss_netgroup_parseline (&result
->cursor
, result
, buffer
, buflen
,