1 /* Netgroup file parser in nss_db modules.
2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
26 #include <libc-lock.h>
33 /* The hashing function we use. */
34 #include "../intl/hash-string.h"
37 #define DBFILE _PATH_VARDB "netgroup.db"
39 /* Maintenance of the shared handle open on the database. */
41 _nss_db_setnetgrent (const char *group
, struct __netgrent
*result
)
43 struct nss_db_map state
;
44 enum nss_status status
= internal_setent (DBFILE
, &state
);
46 if (status
== NSS_STATUS_SUCCESS
)
48 const struct nss_db_header
*header
= state
.header
;
49 const stridx_t
*hashtable
50 = (const stridx_t
*) ((const char *) header
51 + header
->dbs
[0].hashoffset
);
52 const char *valstrtab
= (const char *) header
+ header
->valstroffset
;
53 uint32_t hashval
= __hash_string (group
);
54 size_t grouplen
= strlen (group
);
55 size_t hidx
= hashval
% header
->dbs
[0].hashsize
;
56 size_t hval2
= 1 + hashval
% (header
->dbs
[0].hashsize
- 2);
58 status
= NSS_STATUS_NOTFOUND
;
59 while (hashtable
[hidx
] != ~((stridx_t
) 0))
61 const char *valstr
= valstrtab
+ hashtable
[hidx
];
63 if (strncmp (valstr
, group
, grouplen
) == 0
64 && isblank (valstr
[grouplen
]))
66 const char *cp
= &valstr
[grouplen
+ 1];
71 result
->data
= strdup (cp
);
72 if (result
->data
== NULL
)
73 status
= NSS_STATUS_TRYAGAIN
;
76 status
= NSS_STATUS_SUCCESS
;
77 result
->cursor
= result
->data
;
83 if ((hidx
+= hval2
) >= header
->dbs
[0].hashsize
)
84 hidx
-= header
->dbs
[0].hashsize
;
87 internal_endent (&state
);
96 _nss_db_endnetgrent (struct __netgrent
*result
)
100 result
->data_size
= 0;
101 result
->cursor
= NULL
;
102 return NSS_STATUS_SUCCESS
;
106 extern enum nss_status
_nss_netgroup_parseline (char **cursor
,
107 struct __netgrent
*result
,
108 char *buffer
, size_t buflen
,
112 _nss_db_getnetgrent_r (struct __netgrent
*result
, char *buffer
, size_t buflen
,
115 enum nss_status status
;
117 status
= _nss_netgroup_parseline (&result
->cursor
, result
, buffer
, buflen
,