18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
458f0484fSRodney W. Grimes * Copyright (c) 1992, 1993
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes * Rick Macklem at The University of Guelph.
958f0484fSRodney W. Grimes *
1058f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes * are met:
1358f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes * without specific prior written permission.
2158f0484fSRodney W. Grimes *
2258f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes * SUCH DAMAGE.
3358f0484fSRodney W. Grimes */
3458f0484fSRodney W. Grimes
35714ac002SMark Johnston #include "namespace.h"
36714ac002SMark Johnston
37185ec971STim J. Robbins #include <ctype.h>
38714ac002SMark Johnston #include <errno.h>
39714ac002SMark Johnston #include <netdb.h>
40714ac002SMark Johnston #include <nsswitch.h>
41714ac002SMark Johnston #include <pthread.h>
42714ac002SMark Johnston #include <pthread_np.h>
4358f0484fSRodney W. Grimes #include <stdio.h>
44958f4e36SBill Paul #include <stdlib.h>
4573c9eb2fSDag-Erling Smørgrav #include <string.h>
46958f4e36SBill Paul #include <unistd.h>
47958f4e36SBill Paul
48714ac002SMark Johnston #include "nss_tls.h"
49714ac002SMark Johnston
50958f4e36SBill Paul #ifdef YP
511e890b05SBill Paul /*
521e890b05SBill Paul * Notes:
531e890b05SBill Paul * We want to be able to use NIS netgroups properly while retaining
541e890b05SBill Paul * the ability to use a local /etc/netgroup file. Unfortunately, you
551e890b05SBill Paul * can't really do both at the same time - at least, not efficiently.
561e890b05SBill Paul * NetBSD deals with this problem by creating a netgroup database
571e890b05SBill Paul * using Berkeley DB (just like the password database) that allows
581e890b05SBill Paul * for lookups using netgroup, netgroup.byuser or netgroup.byhost
591e890b05SBill Paul * searches. This is a neat idea, but I don't have time to implement
601e890b05SBill Paul * something like that now. (I think ultimately it would be nice
611e890b05SBill Paul * if we DB-fied the group and netgroup stuff all in one shot, but
621e890b05SBill Paul * for now I'm satisfied just to have something that works well
631e890b05SBill Paul * without requiring massive code changes.)
641e890b05SBill Paul *
651e890b05SBill Paul * Therefore, to still permit the use of the local file and maintain
661e890b05SBill Paul * optimum NIS performance, we allow for the following conditions:
671e890b05SBill Paul *
681e890b05SBill Paul * - If /etc/netgroup does not exist and NIS is turned on, we use
691e890b05SBill Paul * NIS netgroups only.
701e890b05SBill Paul *
711e890b05SBill Paul * - If /etc/netgroup exists but is empty, we use NIS netgroups
721e890b05SBill Paul * only.
731e890b05SBill Paul *
741e890b05SBill Paul * - If /etc/netgroup exists and contains _only_ a '+', we use
751e890b05SBill Paul * NIS netgroups only.
761e890b05SBill Paul *
771e890b05SBill Paul * - If /etc/netgroup exists, contains locally defined netgroups
781e890b05SBill Paul * and a '+', we use a mixture of NIS and the local entries.
791e890b05SBill Paul * This method should return the same NIS data as just using
801e890b05SBill Paul * NIS alone, but it will be slower if the NIS netgroup database
811e890b05SBill Paul * is large (innetgr() in particular will suffer since extra
821e890b05SBill Paul * processing has to be done in order to determine memberships
831e890b05SBill Paul * using just the raw netgroup data).
841e890b05SBill Paul *
851e890b05SBill Paul * - If /etc/netgroup exists and contains only locally defined
861e890b05SBill Paul * netgroup entries, we use just those local entries and ignore
871e890b05SBill Paul * NIS (this is the original, pre-NIS behavior).
881e890b05SBill Paul */
89c17942caSBill Paul
90958f4e36SBill Paul #include <rpc/rpc.h>
91958f4e36SBill Paul #include <rpcsvc/yp_prot.h>
92958f4e36SBill Paul #include <rpcsvc/ypclnt.h>
931e890b05SBill Paul #include <sys/param.h>
942cf5e936SAndriy Voskoboinyk #include <sys/stat.h>
951e890b05SBill Paul #include <sys/errno.h>
961e890b05SBill Paul static char *_netgr_yp_domain;
97d454389cSBill Paul int _use_only_yp;
98958f4e36SBill Paul static int _netgr_yp_enabled;
991e890b05SBill Paul static int _yp_innetgr;
100958f4e36SBill Paul #endif
10158f0484fSRodney W. Grimes
1021e890b05SBill Paul #ifndef _PATH_NETGROUP
10358f0484fSRodney W. Grimes #define _PATH_NETGROUP "/etc/netgroup"
1041e890b05SBill Paul #endif
10558f0484fSRodney W. Grimes
106714ac002SMark Johnston enum constants {
107714ac002SMark Johnston NGRP_STORAGE_INITIAL = 1 << 10, /* 1 KByte */
108714ac002SMark Johnston NGRP_STORAGE_MAX = 1 << 20, /* 1 MByte */
109714ac002SMark Johnston };
110714ac002SMark Johnston
111714ac002SMark Johnston static const ns_src defaultsrc[] = {
112714ac002SMark Johnston { NSSRC_COMPAT, NS_SUCCESS },
113714ac002SMark Johnston { NULL, 0 },
114714ac002SMark Johnston };
115714ac002SMark Johnston
11658f0484fSRodney W. Grimes /*
11758f0484fSRodney W. Grimes * Static Variables and functions used by setnetgrent(), getnetgrent() and
11858f0484fSRodney W. Grimes * endnetgrent().
11958f0484fSRodney W. Grimes * There are two linked lists:
12058f0484fSRodney W. Grimes * - linelist is just used by setnetgrent() to parse the net group file via.
12158f0484fSRodney W. Grimes * parse_netgrp()
12258f0484fSRodney W. Grimes * - netgrp is the list of entries for the current netgroup
12358f0484fSRodney W. Grimes */
12458f0484fSRodney W. Grimes struct linelist {
12558f0484fSRodney W. Grimes struct linelist *l_next; /* Chain ptr. */
12658f0484fSRodney W. Grimes int l_parsed; /* Flag for cycles */
12758f0484fSRodney W. Grimes char *l_groupname; /* Name of netgroup */
12858f0484fSRodney W. Grimes char *l_line; /* Netgroup entrie(s) to be parsed */
12958f0484fSRodney W. Grimes };
13058f0484fSRodney W. Grimes
13158f0484fSRodney W. Grimes struct netgrp {
13258f0484fSRodney W. Grimes struct netgrp *ng_next; /* Chain ptr */
13358f0484fSRodney W. Grimes char *ng_str[3]; /* Field pointers, see below */
13458f0484fSRodney W. Grimes };
135714ac002SMark Johnston
136714ac002SMark Johnston struct netgr_state {
137714ac002SMark Johnston FILE *st_netf;
138714ac002SMark Johnston struct linelist *st_linehead;
139714ac002SMark Johnston struct netgrp *st_nextgrp;
140714ac002SMark Johnston struct netgrp *st_gr;
141714ac002SMark Johnston char *st_grname;
142714ac002SMark Johnston };
143714ac002SMark Johnston
14458f0484fSRodney W. Grimes #define NG_HOST 0 /* Host name */
14558f0484fSRodney W. Grimes #define NG_USER 1 /* User name */
14658f0484fSRodney W. Grimes #define NG_DOM 2 /* and Domain name */
14758f0484fSRodney W. Grimes
148714ac002SMark Johnston static void netgr_endstate(void *);
149714ac002SMark Johnston NSS_TLS_HANDLING(netgr);
15076884596SDag-Erling Smørgrav
151714ac002SMark Johnston static int files_endnetgrent(void *, void *, va_list);
152714ac002SMark Johnston static int files_getnetgrent_r(void *, void *, va_list);
153714ac002SMark Johnston static int files_setnetgrent(void *, void *, va_list);
154714ac002SMark Johnston
155714ac002SMark Johnston static int compat_endnetgrent(void *, void *, va_list);
156714ac002SMark Johnston static int compat_innetgr(void *, void *, va_list);
157714ac002SMark Johnston static int compat_getnetgrent_r(void *, void *, va_list);
158714ac002SMark Johnston static int compat_setnetgrent(void *, void *, va_list);
159714ac002SMark Johnston
160714ac002SMark Johnston static void _compat_clearstate(void);
161714ac002SMark Johnston static int _getnetgrent_r(char **, char **, char **, char *, size_t, int *,
162714ac002SMark Johnston struct netgr_state *);
163714ac002SMark Johnston static int _innetgr_fallback(void *, void *, const char *, const char *,
164714ac002SMark Johnston const char *, const char *);
165714ac002SMark Johnston static int innetgr_fallback(void *, void *, va_list);
166714ac002SMark Johnston static int parse_netgrp(const char *, struct netgr_state *, int);
167714ac002SMark Johnston static struct linelist *read_for_group(const char *, struct netgr_state *, int);
16858f0484fSRodney W. Grimes
16958f0484fSRodney W. Grimes #define LINSIZ 1024 /* Length of netgroup file line */
17058f0484fSRodney W. Grimes
171714ac002SMark Johnston static const ns_dtab getnetgrent_dtab[] = {
172714ac002SMark Johnston NS_FILES_CB(files_getnetgrent_r, NULL)
173714ac002SMark Johnston NS_COMPAT_CB(compat_getnetgrent_r, NULL)
174714ac002SMark Johnston { NULL, NULL, NULL },
175714ac002SMark Johnston };
176714ac002SMark Johnston
177714ac002SMark Johnston static const ns_dtab setnetgrent_dtab[] = {
178714ac002SMark Johnston NS_FILES_CB(files_setnetgrent, NULL)
179714ac002SMark Johnston NS_COMPAT_CB(compat_setnetgrent, NULL)
180714ac002SMark Johnston { NULL, NULL, NULL },
181714ac002SMark Johnston };
182714ac002SMark Johnston
183714ac002SMark Johnston static const ns_dtab endnetgrent_dtab[] = {
184714ac002SMark Johnston NS_FILES_CB(files_endnetgrent, NULL)
185714ac002SMark Johnston NS_COMPAT_CB(compat_endnetgrent, NULL)
186714ac002SMark Johnston { NULL, NULL, NULL },
187714ac002SMark Johnston };
188714ac002SMark Johnston
189714ac002SMark Johnston static struct netgr_state compat_state;
190714ac002SMark Johnston
191714ac002SMark Johnston static void
netgr_endstate(void * arg)192714ac002SMark Johnston netgr_endstate(void *arg)
193714ac002SMark Johnston {
194714ac002SMark Johnston struct linelist *lp, *olp;
195714ac002SMark Johnston struct netgrp *gp, *ogp;
196714ac002SMark Johnston struct netgr_state *st;
197714ac002SMark Johnston
198714ac002SMark Johnston st = (struct netgr_state *)arg;
199714ac002SMark Johnston lp = st->st_linehead;
200714ac002SMark Johnston while (lp != NULL) {
201714ac002SMark Johnston olp = lp;
202714ac002SMark Johnston lp = lp->l_next;
203714ac002SMark Johnston free(olp->l_groupname);
204714ac002SMark Johnston free(olp->l_line);
205714ac002SMark Johnston free(olp);
206714ac002SMark Johnston }
207714ac002SMark Johnston st->st_linehead = NULL;
208714ac002SMark Johnston if (st->st_grname != NULL) {
209714ac002SMark Johnston free(st->st_grname);
210714ac002SMark Johnston st->st_grname = NULL;
211714ac002SMark Johnston }
212714ac002SMark Johnston gp = st->st_gr;
213714ac002SMark Johnston while (gp != NULL) {
214714ac002SMark Johnston ogp = gp;
215714ac002SMark Johnston gp = gp->ng_next;
216714ac002SMark Johnston free(ogp->ng_str[NG_HOST]);
217714ac002SMark Johnston free(ogp->ng_str[NG_USER]);
218714ac002SMark Johnston free(ogp->ng_str[NG_DOM]);
219714ac002SMark Johnston free(ogp);
220714ac002SMark Johnston }
221714ac002SMark Johnston st->st_gr = NULL;
222714ac002SMark Johnston st->st_nextgrp = NULL;
223714ac002SMark Johnston }
224714ac002SMark Johnston
225714ac002SMark Johnston static int
files_getnetgrent_r(void * retval,void * mdata,va_list ap)226714ac002SMark Johnston files_getnetgrent_r(void *retval, void *mdata, va_list ap)
227714ac002SMark Johnston {
228714ac002SMark Johnston struct netgr_state *st;
229714ac002SMark Johnston char **hostp, **userp, **domp, *buf;
230714ac002SMark Johnston size_t bufsize;
231714ac002SMark Johnston int *errnop;
232714ac002SMark Johnston
233714ac002SMark Johnston hostp = va_arg(ap, char **);
234714ac002SMark Johnston userp = va_arg(ap, char **);
235714ac002SMark Johnston domp = va_arg(ap, char **);
236714ac002SMark Johnston buf = va_arg(ap, char *);
237714ac002SMark Johnston bufsize = va_arg(ap, size_t);
238714ac002SMark Johnston errnop = va_arg(ap, int *);
239714ac002SMark Johnston
240714ac002SMark Johnston if (netgr_getstate(&st) != 0)
241714ac002SMark Johnston return (NS_UNAVAIL);
242714ac002SMark Johnston
243714ac002SMark Johnston return (_getnetgrent_r(hostp, userp, domp, buf, bufsize, errnop, st));
244714ac002SMark Johnston }
245714ac002SMark Johnston
246714ac002SMark Johnston static int
files_setnetgrent(void * retval,void * mdata,va_list ap)247714ac002SMark Johnston files_setnetgrent(void *retval, void *mdata, va_list ap)
248714ac002SMark Johnston {
249714ac002SMark Johnston const ns_src src[] = {
250714ac002SMark Johnston { NSSRC_FILES, NS_SUCCESS },
251714ac002SMark Johnston { NULL, 0 },
252714ac002SMark Johnston };
253714ac002SMark Johnston struct netgr_state *st;
254714ac002SMark Johnston const char *group;
255714ac002SMark Johnston int rv;
256714ac002SMark Johnston
257714ac002SMark Johnston group = va_arg(ap, const char *);
258714ac002SMark Johnston
259714ac002SMark Johnston if (group == NULL || group[0] == '\0')
260714ac002SMark Johnston return (NS_RETURN);
261714ac002SMark Johnston
262714ac002SMark Johnston rv = netgr_getstate(&st);
263714ac002SMark Johnston if (rv != 0)
264714ac002SMark Johnston return (NS_UNAVAIL);
265714ac002SMark Johnston
266714ac002SMark Johnston if (st->st_gr == NULL || strcmp(group, st->st_grname) != 0) {
267714ac002SMark Johnston (void)_nsdispatch(NULL, endnetgrent_dtab, NSDB_NETGROUP,
268714ac002SMark Johnston "endnetgrent", src);
269714ac002SMark Johnston if ((st->st_netf = fopen(_PATH_NETGROUP, "re")) != NULL) {
270714ac002SMark Johnston if (parse_netgrp(group, st, 0) != 0)
271714ac002SMark Johnston (void)_nsdispatch(NULL, endnetgrent_dtab,
272714ac002SMark Johnston NSDB_NETGROUP, "endnetgrent", src);
273714ac002SMark Johnston else
274714ac002SMark Johnston st->st_grname = strdup(group);
275714ac002SMark Johnston (void)fclose(st->st_netf);
276714ac002SMark Johnston st->st_netf = NULL;
277714ac002SMark Johnston }
278714ac002SMark Johnston }
279714ac002SMark Johnston st->st_nextgrp = st->st_gr;
280714ac002SMark Johnston return (st->st_grname != NULL ? NS_SUCCESS : NS_NOTFOUND);
281714ac002SMark Johnston }
282714ac002SMark Johnston
283714ac002SMark Johnston static int
files_endnetgrent(void * retval,void * mdata,va_list ap)284714ac002SMark Johnston files_endnetgrent(void *retval, void *mdata, va_list ap)
285714ac002SMark Johnston {
286714ac002SMark Johnston struct netgr_state *st;
287714ac002SMark Johnston
288714ac002SMark Johnston if (netgr_getstate(&st) != 0)
289714ac002SMark Johnston return (NS_UNAVAIL);
290714ac002SMark Johnston netgr_endstate(st);
291714ac002SMark Johnston return (NS_SUCCESS);
292714ac002SMark Johnston }
293714ac002SMark Johnston
294714ac002SMark Johnston static int
compat_getnetgrent_r(void * retval,void * mdata,va_list ap)295714ac002SMark Johnston compat_getnetgrent_r(void *retval, void *mdata, va_list ap)
296714ac002SMark Johnston {
297714ac002SMark Johnston char **hostp, **userp, **domp, *buf;
298714ac002SMark Johnston size_t bufsize;
299714ac002SMark Johnston int *errnop;
300714ac002SMark Johnston #ifdef YP
301714ac002SMark Johnston _yp_innetgr = 0;
302714ac002SMark Johnston #endif
303714ac002SMark Johnston
304714ac002SMark Johnston hostp = va_arg(ap, char **);
305714ac002SMark Johnston userp = va_arg(ap, char **);
306714ac002SMark Johnston domp = va_arg(ap, char **);
307714ac002SMark Johnston buf = va_arg(ap, char *);
308714ac002SMark Johnston bufsize = va_arg(ap, size_t);
309714ac002SMark Johnston errnop = va_arg(ap, int *);
310714ac002SMark Johnston
311714ac002SMark Johnston return (_getnetgrent_r(hostp, userp, domp, buf, bufsize, errnop,
312714ac002SMark Johnston &compat_state));
313714ac002SMark Johnston }
314714ac002SMark Johnston
31558f0484fSRodney W. Grimes /*
316714ac002SMark Johnston * compat_setnetgrent()
31758f0484fSRodney W. Grimes * Parse the netgroup file looking for the netgroup and build the list
31858f0484fSRodney W. Grimes * of netgrp structures. Let parse_netgrp() and read_for_group() do
31958f0484fSRodney W. Grimes * most of the work.
32058f0484fSRodney W. Grimes */
321714ac002SMark Johnston static int
compat_setnetgrent(void * retval,void * mdata,va_list ap)322714ac002SMark Johnston compat_setnetgrent(void *retval, void *mdata, va_list ap)
32358f0484fSRodney W. Grimes {
324714ac002SMark Johnston FILE *netf;
325714ac002SMark Johnston const char *group;
3261e890b05SBill Paul #ifdef YP
3271e890b05SBill Paul struct stat _yp_statp;
3281e890b05SBill Paul char _yp_plus;
3291e890b05SBill Paul #endif
3301e890b05SBill Paul
331714ac002SMark Johnston group = va_arg(ap, const char *);
332714ac002SMark Johnston
3330ffe27f5SBill Paul /* Sanity check */
3340ffe27f5SBill Paul if (group == NULL || !strlen(group))
335714ac002SMark Johnston return (NS_RETURN);
3360ffe27f5SBill Paul
337714ac002SMark Johnston if (compat_state.st_gr == NULL ||
338714ac002SMark Johnston strcmp(group, compat_state.st_grname) != 0) {
339714ac002SMark Johnston _compat_clearstate();
340714ac002SMark Johnston
3411e890b05SBill Paul #ifdef YP
342d454389cSBill Paul /* Presumed guilty until proven innocent. */
343d454389cSBill Paul _use_only_yp = 0;
3441e890b05SBill Paul /*
345cbe78b44SBill Paul * If /etc/netgroup doesn't exist or is empty,
3461e890b05SBill Paul * use NIS exclusively.
3471e890b05SBill Paul */
3481e890b05SBill Paul if (((stat(_PATH_NETGROUP, &_yp_statp) < 0) &&
3491e890b05SBill Paul errno == ENOENT) || _yp_statp.st_size == 0)
3501e890b05SBill Paul _use_only_yp = _netgr_yp_enabled = 1;
3511084b38bSJilles Tjoelker if ((netf = fopen(_PATH_NETGROUP,"re")) != NULL ||_use_only_yp){
352714ac002SMark Johnston compat_state.st_netf = netf;
3531e890b05SBill Paul /*
3541e890b05SBill Paul * Icky: grab the first character of the netgroup file
3551e890b05SBill Paul * and turn on NIS if it's a '+'. rewind the stream
3561e890b05SBill Paul * afterwards so we don't goof up read_for_group() later.
3571e890b05SBill Paul */
3581e890b05SBill Paul if (netf) {
3591e890b05SBill Paul fscanf(netf, "%c", &_yp_plus);
3601e890b05SBill Paul rewind(netf);
3611e890b05SBill Paul if (_yp_plus == '+')
3621e890b05SBill Paul _use_only_yp = _netgr_yp_enabled = 1;
3631e890b05SBill Paul }
3641e890b05SBill Paul /*
3651e890b05SBill Paul * If we were called specifically for an innetgr()
3661e890b05SBill Paul * lookup and we're in NIS-only mode, short-circuit
3671e890b05SBill Paul * parse_netgroup() and cut directly to the chase.
3681e890b05SBill Paul */
369d454389cSBill Paul if (_use_only_yp && _yp_innetgr) {
370d454389cSBill Paul /* dohw! */
371de32dbbdSBill Paul if (netf != NULL)
372d454389cSBill Paul fclose(netf);
373714ac002SMark Johnston return (NS_RETURN);
374d454389cSBill Paul }
3751e890b05SBill Paul #else
3761084b38bSJilles Tjoelker if ((netf = fopen(_PATH_NETGROUP, "re"))) {
377714ac002SMark Johnston compat_state.st_netf = netf;
3781e890b05SBill Paul #endif
379714ac002SMark Johnston if (parse_netgrp(group, &compat_state, 1)) {
380714ac002SMark Johnston _compat_clearstate();
381714ac002SMark Johnston } else {
382714ac002SMark Johnston compat_state.st_grname = strdup(group);
38358f0484fSRodney W. Grimes }
3841e890b05SBill Paul if (netf)
38558f0484fSRodney W. Grimes fclose(netf);
38658f0484fSRodney W. Grimes }
38758f0484fSRodney W. Grimes }
388714ac002SMark Johnston compat_state.st_nextgrp = compat_state.st_gr;
389714ac002SMark Johnston return (NS_SUCCESS);
39058f0484fSRodney W. Grimes }
39158f0484fSRodney W. Grimes
392714ac002SMark Johnston static void
393714ac002SMark Johnston _compat_clearstate(void)
39458f0484fSRodney W. Grimes {
39558f0484fSRodney W. Grimes
396409495f6SBill Paul #ifdef YP
3978516cd0fSBill Paul _netgr_yp_enabled = 0;
398409495f6SBill Paul #endif
399714ac002SMark Johnston netgr_endstate(&compat_state);
400714ac002SMark Johnston }
401714ac002SMark Johnston
402714ac002SMark Johnston /*
403714ac002SMark Johnston * compat_endnetgrent() - cleanup
404714ac002SMark Johnston */
405714ac002SMark Johnston static int
406714ac002SMark Johnston compat_endnetgrent(void *retval, void *mdata, va_list ap)
407714ac002SMark Johnston {
408714ac002SMark Johnston
409714ac002SMark Johnston _compat_clearstate();
410714ac002SMark Johnston return (NS_SUCCESS);
411714ac002SMark Johnston }
412714ac002SMark Johnston
413714ac002SMark Johnston int
414714ac002SMark Johnston _getnetgrent_r(char **hostp, char **userp, char **domp, char *buf,
415714ac002SMark Johnston size_t bufsize, int *errnop, struct netgr_state *st)
416714ac002SMark Johnston {
417714ac002SMark Johnston char *p, *src;
418714ac002SMark Johnston size_t len;
419714ac002SMark Johnston int rv;
420714ac002SMark Johnston
421714ac002SMark Johnston #define COPY_NG_ELEM(dstp, i) do { \
422714ac002SMark Johnston src = st->st_nextgrp->ng_str[(i)]; \
423714ac002SMark Johnston if (src == NULL) \
424714ac002SMark Johnston src = ""; \
425714ac002SMark Johnston len = strlcpy(p, src, bufsize); \
426714ac002SMark Johnston if (len >= bufsize) { \
427714ac002SMark Johnston *errnop = ERANGE; \
428714ac002SMark Johnston return (NS_RETURN); \
429714ac002SMark Johnston } \
430714ac002SMark Johnston *(dstp) = p; \
431714ac002SMark Johnston p += len + 1; \
432714ac002SMark Johnston bufsize -= len + 1; \
433714ac002SMark Johnston } while (0)
434714ac002SMark Johnston
435714ac002SMark Johnston p = buf;
436714ac002SMark Johnston if (st->st_nextgrp != NULL) {
437714ac002SMark Johnston COPY_NG_ELEM(hostp, NG_HOST);
438714ac002SMark Johnston COPY_NG_ELEM(userp, NG_USER);
439714ac002SMark Johnston COPY_NG_ELEM(domp, NG_DOM);
440714ac002SMark Johnston st->st_nextgrp = st->st_nextgrp->ng_next;
441714ac002SMark Johnston rv = NS_SUCCESS;
442714ac002SMark Johnston } else {
443714ac002SMark Johnston rv = NS_NOTFOUND;
444714ac002SMark Johnston }
445714ac002SMark Johnston #undef COPY_NG_ELEM
446714ac002SMark Johnston
447714ac002SMark Johnston return (rv);
44858f0484fSRodney W. Grimes }
44958f0484fSRodney W. Grimes
4501e890b05SBill Paul #ifdef YP
45176884596SDag-Erling Smørgrav static int
45276884596SDag-Erling Smørgrav _listmatch(const char *list, const char *group, int len)
4531e890b05SBill Paul {
45476884596SDag-Erling Smørgrav const char *ptr = list;
45576884596SDag-Erling Smørgrav const char *cptr;
456d9cc92f5SBill Paul int glen = strlen(group);
4571e890b05SBill Paul
458d9cc92f5SBill Paul /* skip possible leading whitespace */
45949435560SAndrey A. Chernov while (isspace((unsigned char)*ptr))
460d9cc92f5SBill Paul ptr++;
461cbe78b44SBill Paul
462d9cc92f5SBill Paul while (ptr < list + len) {
463d9cc92f5SBill Paul cptr = ptr;
46449435560SAndrey A. Chernov while(*ptr != ',' && *ptr != '\0' && !isspace((unsigned char)*ptr))
465d9cc92f5SBill Paul ptr++;
466f48bc662SSteve Price if (strncmp(cptr, group, glen) == 0 && glen == (ptr - cptr))
4671e890b05SBill Paul return (1);
46849435560SAndrey A. Chernov while (*ptr == ',' || isspace((unsigned char)*ptr))
469d9cc92f5SBill Paul ptr++;
470dfe8e51cSBill Paul }
471cbe78b44SBill Paul
4721e890b05SBill Paul return (0);
4731e890b05SBill Paul }
4741e890b05SBill Paul
47576884596SDag-Erling Smørgrav static int
476c5a096e0SJonathan Chen _revnetgr_lookup(char* lookupdom, char* map, const char* str,
477c5a096e0SJonathan Chen const char* dom, const char* group)
4781e890b05SBill Paul {
479c5a096e0SJonathan Chen int y, rv, rot;
480c5a096e0SJonathan Chen char key[MAXHOSTNAMELEN];
481c5a096e0SJonathan Chen char *result;
482c5a096e0SJonathan Chen int resultlen;
483c5a096e0SJonathan Chen
484c5a096e0SJonathan Chen for (rot = 0; ; rot++) {
485c5a096e0SJonathan Chen switch (rot) {
4863d1d73c2SGuy Helmer case 0:
487299bafaeSGuy Helmer snprintf(key, MAXHOSTNAMELEN, "%s.%s", str,
488c5a096e0SJonathan Chen dom ? dom : lookupdom);
4891e890b05SBill Paul break;
4903d1d73c2SGuy Helmer case 1:
491299bafaeSGuy Helmer snprintf(key, MAXHOSTNAMELEN, "%s.*", str);
492299bafaeSGuy Helmer break;
4933d1d73c2SGuy Helmer case 2:
494299bafaeSGuy Helmer snprintf(key, MAXHOSTNAMELEN, "*.%s",
495299bafaeSGuy Helmer dom ? dom : lookupdom);
496299bafaeSGuy Helmer break;
4973d1d73c2SGuy Helmer case 3:
498299bafaeSGuy Helmer snprintf(key, MAXHOSTNAMELEN, "*.*");
4991e890b05SBill Paul break;
5003d1d73c2SGuy Helmer default:
5013d1d73c2SGuy Helmer return (0);
5021e890b05SBill Paul }
503c5a096e0SJonathan Chen y = yp_match(lookupdom, map, key, strlen(key), &result,
504c5a096e0SJonathan Chen &resultlen);
505c5a096e0SJonathan Chen if (y == 0) {
506c5a096e0SJonathan Chen rv = _listmatch(result, group, resultlen);
507c5a096e0SJonathan Chen free(result);
5083d1d73c2SGuy Helmer if (rv)
5093d1d73c2SGuy Helmer return (1);
510c5a096e0SJonathan Chen } else if (y != YPERR_KEY) {
511c5a096e0SJonathan Chen /*
512c5a096e0SJonathan Chen * If we get an error other than 'no
513c5a096e0SJonathan Chen * such key in map' then something is
514c5a096e0SJonathan Chen * wrong and we should stop the search.
515c5a096e0SJonathan Chen */
516c5a096e0SJonathan Chen return (-1);
517c5a096e0SJonathan Chen }
518c5a096e0SJonathan Chen }
5191e890b05SBill Paul }
5201e890b05SBill Paul #endif
5211e890b05SBill Paul
52258f0484fSRodney W. Grimes /*
52358f0484fSRodney W. Grimes * Search for a match in a netgroup.
52458f0484fSRodney W. Grimes */
525714ac002SMark Johnston static int
526714ac002SMark Johnston compat_innetgr(void *retval, void *mdata, va_list ap)
52758f0484fSRodney W. Grimes {
528714ac002SMark Johnston #ifdef YP
529714ac002SMark Johnston const ns_src src[] = {
530714ac002SMark Johnston { mdata, NS_SUCCESS },
531714ac002SMark Johnston { NULL, 0 },
532714ac002SMark Johnston };
533714ac002SMark Johnston #endif
534714ac002SMark Johnston const char *group, *host, *user, *dom;
535714ac002SMark Johnston
536714ac002SMark Johnston group = va_arg(ap, const char *);
537714ac002SMark Johnston host = va_arg(ap, const char *);
538714ac002SMark Johnston user = va_arg(ap, const char *);
539714ac002SMark Johnston dom = va_arg(ap, const char *);
5400ffe27f5SBill Paul
5410ffe27f5SBill Paul if (group == NULL || !strlen(group))
542714ac002SMark Johnston return (NS_RETURN);
5430ffe27f5SBill Paul
5441e890b05SBill Paul #ifdef YP
5451e890b05SBill Paul _yp_innetgr = 1;
546714ac002SMark Johnston (void)_nsdispatch(NULL, setnetgrent_dtab, NSDB_NETGROUP, "setnetgrent",
547714ac002SMark Johnston src, group);
548cbe78b44SBill Paul _yp_innetgr = 0;
5491e890b05SBill Paul /*
5501e890b05SBill Paul * If we're in NIS-only mode, do the search using
5511e890b05SBill Paul * NIS 'reverse netgroup' lookups.
552c5a096e0SJonathan Chen *
553c5a096e0SJonathan Chen * What happens with 'reverse netgroup' lookups:
554c5a096e0SJonathan Chen *
555c5a096e0SJonathan Chen * 1) try 'reverse netgroup' lookup
556c5a096e0SJonathan Chen * 1.a) if host is specified and user is null:
557c5a096e0SJonathan Chen * look in netgroup.byhost
558c5a096e0SJonathan Chen * (try host.domain, host.*, *.domain or *.*)
559c5a096e0SJonathan Chen * if found, return yes
560c5a096e0SJonathan Chen * 1.b) if user is specified and host is null:
561c5a096e0SJonathan Chen * look in netgroup.byuser
562c5a096e0SJonathan Chen * (try host.domain, host.*, *.domain or *.*)
563c5a096e0SJonathan Chen * if found, return yes
564c5a096e0SJonathan Chen * 1.c) if both host and user are specified,
565c5a096e0SJonathan Chen * don't do 'reverse netgroup' lookup. It won't work.
566c5a096e0SJonathan Chen * 1.d) if neither host ane user are specified (why?!?)
567c5a096e0SJonathan Chen * don't do 'reverse netgroup' lookup either.
568c5a096e0SJonathan Chen * 2) if domain is specified and 'reverse lookup' is done:
569c5a096e0SJonathan Chen * 'reverse lookup' was authoritative. bye bye.
570c5a096e0SJonathan Chen * 3) otherwise, too bad, try it the slow way.
5711e890b05SBill Paul */
572c5a096e0SJonathan Chen if (_use_only_yp && (host == NULL) != (user == NULL)) {
573c5a096e0SJonathan Chen int ret;
5741e890b05SBill Paul if(yp_get_default_domain(&_netgr_yp_domain))
575714ac002SMark Johnston return (NS_NOTFOUND);
576c5a096e0SJonathan Chen ret = _revnetgr_lookup(_netgr_yp_domain,
577c5a096e0SJonathan Chen host?"netgroup.byhost":"netgroup.byuser",
578c5a096e0SJonathan Chen host?host:user, dom, group);
579714ac002SMark Johnston if (ret == 1) {
580714ac002SMark Johnston *(int *)retval = 1;
581714ac002SMark Johnston return (NS_SUCCESS);
582714ac002SMark Johnston } else if (ret == 0 && dom != NULL) {
583714ac002SMark Johnston *(int *)retval = 0;
584714ac002SMark Johnston return (NS_SUCCESS);
5851e890b05SBill Paul }
586714ac002SMark Johnston }
5871e890b05SBill Paul #endif /* YP */
588e882d43eSBill Paul
589714ac002SMark Johnston return (_innetgr_fallback(retval, mdata, group, host, user, dom));
59058f0484fSRodney W. Grimes }
591714ac002SMark Johnston
592714ac002SMark Johnston static int
593714ac002SMark Johnston _innetgr_fallback(void *retval, void *mdata, const char *group, const char *host,
594714ac002SMark Johnston const char *user, const char *dom)
595714ac002SMark Johnston {
596714ac002SMark Johnston const ns_src src[] = {
597714ac002SMark Johnston { mdata, NS_SUCCESS },
598714ac002SMark Johnston { NULL, 0 },
599714ac002SMark Johnston };
600714ac002SMark Johnston char *h, *u, *d;
601714ac002SMark Johnston char *buf;
602714ac002SMark Johnston size_t bufsize;
603714ac002SMark Johnston int rv, ret_errno;
604714ac002SMark Johnston
605714ac002SMark Johnston if (group == NULL || group[0] == '\0')
606714ac002SMark Johnston return (NS_RETURN);
607714ac002SMark Johnston
608714ac002SMark Johnston bufsize = NGRP_STORAGE_INITIAL;
609714ac002SMark Johnston buf = malloc(bufsize);
610714ac002SMark Johnston if (buf == NULL)
611714ac002SMark Johnston return (NS_UNAVAIL);
612714ac002SMark Johnston
613714ac002SMark Johnston *(int *)retval = 0;
614714ac002SMark Johnston
615714ac002SMark Johnston (void)_nsdispatch(NULL, setnetgrent_dtab, NSDB_NETGROUP, "setnetgrent",
616714ac002SMark Johnston src, group);
617714ac002SMark Johnston
618714ac002SMark Johnston for (;;) {
619714ac002SMark Johnston do {
620714ac002SMark Johnston ret_errno = 0;
621714ac002SMark Johnston rv = _nsdispatch(NULL, getnetgrent_dtab, NSDB_NETGROUP,
622714ac002SMark Johnston "getnetgrent_r", src, &h, &u, &d, buf, bufsize,
623714ac002SMark Johnston &ret_errno);
624714ac002SMark Johnston if (rv != NS_SUCCESS && ret_errno == ERANGE) {
625714ac002SMark Johnston bufsize *= 2;
626714ac002SMark Johnston if (bufsize > NGRP_STORAGE_MAX ||
627714ac002SMark Johnston (buf = reallocf(buf, bufsize)) == NULL)
628714ac002SMark Johnston goto out;
629714ac002SMark Johnston }
630714ac002SMark Johnston } while (rv != NS_SUCCESS && ret_errno == ERANGE);
631714ac002SMark Johnston
632714ac002SMark Johnston if (rv != NS_SUCCESS) {
633714ac002SMark Johnston if (rv == NS_NOTFOUND && ret_errno == 0)
634714ac002SMark Johnston rv = NS_SUCCESS;
635714ac002SMark Johnston break;
636714ac002SMark Johnston }
637714ac002SMark Johnston
638714ac002SMark Johnston if ((host == NULL || h == NULL || strcmp(host, h) == 0) &&
639714ac002SMark Johnston (user == NULL || u == NULL || strcmp(user, u) == 0) &&
640714ac002SMark Johnston (dom == NULL || d == NULL || strcmp(dom, d) == 0)) {
641714ac002SMark Johnston *(int *)retval = 1;
642714ac002SMark Johnston break;
643714ac002SMark Johnston }
644714ac002SMark Johnston }
645714ac002SMark Johnston
646714ac002SMark Johnston out:
647714ac002SMark Johnston free(buf);
648714ac002SMark Johnston (void)_nsdispatch(NULL, endnetgrent_dtab, NSDB_NETGROUP, "endnetgrent",
649714ac002SMark Johnston src);
650714ac002SMark Johnston return (rv);
651714ac002SMark Johnston }
652714ac002SMark Johnston
653714ac002SMark Johnston static int
654714ac002SMark Johnston innetgr_fallback(void *retval, void *mdata, va_list ap)
655714ac002SMark Johnston {
656714ac002SMark Johnston const char *group, *host, *user, *dom;
657714ac002SMark Johnston
658714ac002SMark Johnston group = va_arg(ap, const char *);
659714ac002SMark Johnston host = va_arg(ap, const char *);
660714ac002SMark Johnston user = va_arg(ap, const char *);
661714ac002SMark Johnston dom = va_arg(ap, const char *);
662714ac002SMark Johnston
663714ac002SMark Johnston return (_innetgr_fallback(retval, mdata, group, host, user, dom));
66458f0484fSRodney W. Grimes }
66558f0484fSRodney W. Grimes
66658f0484fSRodney W. Grimes /*
66758f0484fSRodney W. Grimes * Parse the netgroup file setting up the linked lists.
66858f0484fSRodney W. Grimes */
66958f0484fSRodney W. Grimes static int
670714ac002SMark Johnston parse_netgrp(const char *group, struct netgr_state *st, int niscompat)
67158f0484fSRodney W. Grimes {
6723d1d73c2SGuy Helmer struct netgrp *grp;
673714ac002SMark Johnston struct linelist *lp = st->st_linehead;
6743d1d73c2SGuy Helmer char **ng;
6753d1d73c2SGuy Helmer char *epos, *gpos, *pos, *spos;
6763d1d73c2SGuy Helmer int freepos, len, strpos;
677dbf973c0SBill Paul #ifdef DEBUG
67822626efaSDavid E. O'Brien int fields;
679dbf973c0SBill Paul #endif
68058f0484fSRodney W. Grimes
68158f0484fSRodney W. Grimes /*
68258f0484fSRodney W. Grimes * First, see if the line has already been read in.
68358f0484fSRodney W. Grimes */
68458f0484fSRodney W. Grimes while (lp) {
68558f0484fSRodney W. Grimes if (!strcmp(group, lp->l_groupname))
68658f0484fSRodney W. Grimes break;
68758f0484fSRodney W. Grimes lp = lp->l_next;
68858f0484fSRodney W. Grimes }
689714ac002SMark Johnston if (lp == NULL && (lp = read_for_group(group, st, niscompat)) == NULL)
69058f0484fSRodney W. Grimes return (1);
69158f0484fSRodney W. Grimes if (lp->l_parsed) {
692dbf973c0SBill Paul #ifdef DEBUG
693dbf973c0SBill Paul /*
694dbf973c0SBill Paul * This error message is largely superflous since the
695dbf973c0SBill Paul * code handles the error condition sucessfully, and
696dbf973c0SBill Paul * spewing it out from inside libc can actually hose
697dbf973c0SBill Paul * certain programs.
698dbf973c0SBill Paul */
69958f0484fSRodney W. Grimes fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);
700dbf973c0SBill Paul #endif
70158f0484fSRodney W. Grimes return (1);
70258f0484fSRodney W. Grimes } else
70358f0484fSRodney W. Grimes lp->l_parsed = 1;
70458f0484fSRodney W. Grimes pos = lp->l_line;
705409495f6SBill Paul /* Watch for null pointer dereferences, dammit! */
706409495f6SBill Paul while (pos != NULL && *pos != '\0') {
70758f0484fSRodney W. Grimes if (*pos == '(') {
7083d1d73c2SGuy Helmer grp = malloc(sizeof(*grp));
709fed7420cSGuy Helmer if (grp == NULL)
710fed7420cSGuy Helmer return (1);
7113d1d73c2SGuy Helmer ng = grp->ng_str;
7123d1d73c2SGuy Helmer bzero(grp, sizeof(*grp));
71358f0484fSRodney W. Grimes pos++;
71458f0484fSRodney W. Grimes gpos = strsep(&pos, ")");
715dbf973c0SBill Paul #ifdef DEBUG
716dbf973c0SBill Paul fields = 0;
717dbf973c0SBill Paul #endif
71858f0484fSRodney W. Grimes for (strpos = 0; strpos < 3; strpos++) {
7193d1d73c2SGuy Helmer if ((spos = strsep(&gpos, ",")) == NULL) {
7203d1d73c2SGuy Helmer /*
7213d1d73c2SGuy Helmer * All other systems I've tested
7223d1d73c2SGuy Helmer * return NULL for empty netgroup
7233d1d73c2SGuy Helmer * fields. It's up to user programs
7243d1d73c2SGuy Helmer * to handle the NULLs appropriately.
7253d1d73c2SGuy Helmer */
7263d1d73c2SGuy Helmer ng[strpos] = NULL;
7273d1d73c2SGuy Helmer continue;
7283d1d73c2SGuy Helmer }
729dbf973c0SBill Paul #ifdef DEBUG
730dbf973c0SBill Paul fields++;
731dbf973c0SBill Paul #endif
73258f0484fSRodney W. Grimes while (*spos == ' ' || *spos == '\t')
73358f0484fSRodney W. Grimes spos++;
7341e890b05SBill Paul if ((epos = strpbrk(spos, " \t"))) {
73558f0484fSRodney W. Grimes *epos = '\0';
73658f0484fSRodney W. Grimes len = epos - spos;
73758f0484fSRodney W. Grimes } else
73858f0484fSRodney W. Grimes len = strlen(spos);
7393d1d73c2SGuy Helmer if (len <= 0)
7403d1d73c2SGuy Helmer continue;
7413d1d73c2SGuy Helmer ng[strpos] = malloc(len + 1);
7423d1d73c2SGuy Helmer if (ng[strpos] == NULL) {
7433d1d73c2SGuy Helmer for (freepos = 0; freepos < strpos;
7443d1d73c2SGuy Helmer freepos++)
7453d1d73c2SGuy Helmer free(ng[freepos]);
746fed7420cSGuy Helmer free(grp);
747fed7420cSGuy Helmer return (1);
748fed7420cSGuy Helmer }
7493d1d73c2SGuy Helmer bcopy(spos, ng[strpos], len + 1);
750dbf973c0SBill Paul }
751714ac002SMark Johnston grp->ng_next = st->st_gr;
752714ac002SMark Johnston st->st_gr = grp;
753dbf973c0SBill Paul #ifdef DEBUG
754dbf973c0SBill Paul /*
755dbf973c0SBill Paul * Note: on other platforms, malformed netgroup
756dbf973c0SBill Paul * entries are not normally flagged. While we
757dbf973c0SBill Paul * can catch bad entries and report them, we should
758dbf973c0SBill Paul * stay silent by default for compatibility's sake.
759dbf973c0SBill Paul */
7603d1d73c2SGuy Helmer if (fields < 3) {
7613d1d73c2SGuy Helmer fprintf(stderr,
7623d1d73c2SGuy Helmer "Bad entry (%s%s%s%s%s) in netgroup \"%s\"\n",
7633d1d73c2SGuy Helmer ng[NG_HOST] == NULL ? "" : ng[NG_HOST],
7643d1d73c2SGuy Helmer ng[NG_USER] == NULL ? "" : ",",
7653d1d73c2SGuy Helmer ng[NG_USER] == NULL ? "" : ng[NG_USER],
7663d1d73c2SGuy Helmer ng[NG_DOM] == NULL ? "" : ",",
7673d1d73c2SGuy Helmer ng[NG_DOM] == NULL ? "" : ng[NG_DOM],
768dbf973c0SBill Paul lp->l_groupname);
76923e49122SEnji Cooper }
770dbf973c0SBill Paul #endif
77158f0484fSRodney W. Grimes } else {
77258f0484fSRodney W. Grimes spos = strsep(&pos, ", \t");
773714ac002SMark Johnston if (parse_netgrp(spos, st, niscompat))
774e8030794SBill Paul continue;
77558f0484fSRodney W. Grimes }
776adf6ad9eSPeter Wemm if (pos == NULL)
777adf6ad9eSPeter Wemm break;
77858f0484fSRodney W. Grimes while (*pos == ' ' || *pos == ',' || *pos == '\t')
77958f0484fSRodney W. Grimes pos++;
78058f0484fSRodney W. Grimes }
78158f0484fSRodney W. Grimes return (0);
78258f0484fSRodney W. Grimes }
78358f0484fSRodney W. Grimes
78458f0484fSRodney W. Grimes /*
78558f0484fSRodney W. Grimes * Read the netgroup file and save lines until the line for the netgroup
78658f0484fSRodney W. Grimes * is found. Return 1 if eof is encountered.
78758f0484fSRodney W. Grimes */
78858f0484fSRodney W. Grimes static struct linelist *
789714ac002SMark Johnston read_for_group(const char *group, struct netgr_state *st, int niscompat)
79058f0484fSRodney W. Grimes {
7913a3c9121SKonstantin Belousov char *linep, *olinep, *pos, *spos;
79222626efaSDavid E. O'Brien int len, olen;
79358f0484fSRodney W. Grimes int cont;
79458f0484fSRodney W. Grimes struct linelist *lp;
7951d2493ffSBill Paul char line[LINSIZ + 2];
796714ac002SMark Johnston FILE *netf;
797409495f6SBill Paul #ifdef YP
798409495f6SBill Paul char *result;
799409495f6SBill Paul int resultlen;
800fed7420cSGuy Helmer linep = NULL;
80158f0484fSRodney W. Grimes
802714ac002SMark Johnston netf = st->st_netf;
803714ac002SMark Johnston while ((_netgr_yp_enabled && niscompat) ||
804714ac002SMark Johnston fgets(line, LINSIZ, netf) != NULL) {
805409495f6SBill Paul if (_netgr_yp_enabled) {
806409495f6SBill Paul if(!_netgr_yp_domain)
8078516cd0fSBill Paul if(yp_get_default_domain(&_netgr_yp_domain))
808409495f6SBill Paul continue;
8098516cd0fSBill Paul if (yp_match(_netgr_yp_domain, "netgroup", group,
8108516cd0fSBill Paul strlen(group), &result, &resultlen)) {
8118516cd0fSBill Paul free(result);
8121e890b05SBill Paul if (_use_only_yp)
8138516cd0fSBill Paul return ((struct linelist *)0);
8141e890b05SBill Paul else {
8151e890b05SBill Paul _netgr_yp_enabled = 0;
8161e890b05SBill Paul continue;
8171e890b05SBill Paul }
818409495f6SBill Paul }
81903ad7e45SMark Johnston if (strlen(result) == 0) {
82003ad7e45SMark Johnston free(result);
82103ad7e45SMark Johnston return (NULL);
82203ad7e45SMark Johnston }
8231d2493ffSBill Paul snprintf(line, LINSIZ, "%s %s", group, result);
824409495f6SBill Paul free(result);
825409495f6SBill Paul }
826409495f6SBill Paul #else
827fed7420cSGuy Helmer linep = NULL;
82858f0484fSRodney W. Grimes while (fgets(line, LINSIZ, netf) != NULL) {
829409495f6SBill Paul #endif
830409495f6SBill Paul pos = (char *)&line;
831409495f6SBill Paul #ifdef YP
832714ac002SMark Johnston if (niscompat && *pos == '+') {
833409495f6SBill Paul _netgr_yp_enabled = 1;
834409495f6SBill Paul continue;
835409495f6SBill Paul }
836409495f6SBill Paul #endif
83758f0484fSRodney W. Grimes if (*pos == '#')
83858f0484fSRodney W. Grimes continue;
83958f0484fSRodney W. Grimes while (*pos == ' ' || *pos == '\t')
84058f0484fSRodney W. Grimes pos++;
84158f0484fSRodney W. Grimes spos = pos;
84258f0484fSRodney W. Grimes while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
84358f0484fSRodney W. Grimes *pos != '\0')
84458f0484fSRodney W. Grimes pos++;
84558f0484fSRodney W. Grimes len = pos - spos;
84658f0484fSRodney W. Grimes while (*pos == ' ' || *pos == '\t')
84758f0484fSRodney W. Grimes pos++;
84858f0484fSRodney W. Grimes if (*pos != '\n' && *pos != '\0') {
849714ac002SMark Johnston lp = malloc(sizeof (*lp));
850fed7420cSGuy Helmer if (lp == NULL)
851fed7420cSGuy Helmer return (NULL);
85258f0484fSRodney W. Grimes lp->l_parsed = 0;
853714ac002SMark Johnston lp->l_groupname = malloc(len + 1);
854fed7420cSGuy Helmer if (lp->l_groupname == NULL) {
855fed7420cSGuy Helmer free(lp);
856fed7420cSGuy Helmer return (NULL);
857fed7420cSGuy Helmer }
85858f0484fSRodney W. Grimes bcopy(spos, lp->l_groupname, len);
85958f0484fSRodney W. Grimes *(lp->l_groupname + len) = '\0';
86058f0484fSRodney W. Grimes len = strlen(pos);
86158f0484fSRodney W. Grimes olen = 0;
86258f0484fSRodney W. Grimes
86358f0484fSRodney W. Grimes /*
86458f0484fSRodney W. Grimes * Loop around handling line continuations.
86558f0484fSRodney W. Grimes */
86658f0484fSRodney W. Grimes do {
86758f0484fSRodney W. Grimes if (*(pos + len - 1) == '\n')
86858f0484fSRodney W. Grimes len--;
86958f0484fSRodney W. Grimes if (*(pos + len - 1) == '\\') {
87058f0484fSRodney W. Grimes len--;
87158f0484fSRodney W. Grimes cont = 1;
87258f0484fSRodney W. Grimes } else
87358f0484fSRodney W. Grimes cont = 0;
87458f0484fSRodney W. Grimes if (len > 0) {
8753a3c9121SKonstantin Belousov linep = malloc(olen + len + 1);
876fed7420cSGuy Helmer if (linep == NULL) {
877fed7420cSGuy Helmer free(lp->l_groupname);
878fed7420cSGuy Helmer free(lp);
8792b34ca7dSDon Lewis if (olen > 0)
8802b34ca7dSDon Lewis free(olinep);
881fed7420cSGuy Helmer return (NULL);
88258f0484fSRodney W. Grimes }
8833a3c9121SKonstantin Belousov if (olen > 0) {
8843a3c9121SKonstantin Belousov bcopy(olinep, linep, olen);
8853a3c9121SKonstantin Belousov free(olinep);
8863a3c9121SKonstantin Belousov }
88758f0484fSRodney W. Grimes bcopy(pos, linep + olen, len);
88858f0484fSRodney W. Grimes olen += len;
88958f0484fSRodney W. Grimes *(linep + olen) = '\0';
8903a3c9121SKonstantin Belousov olinep = linep;
89158f0484fSRodney W. Grimes }
89258f0484fSRodney W. Grimes if (cont) {
89358f0484fSRodney W. Grimes if (fgets(line, LINSIZ, netf)) {
89458f0484fSRodney W. Grimes pos = line;
89558f0484fSRodney W. Grimes len = strlen(pos);
89658f0484fSRodney W. Grimes } else
89758f0484fSRodney W. Grimes cont = 0;
89858f0484fSRodney W. Grimes }
89958f0484fSRodney W. Grimes } while (cont);
90058f0484fSRodney W. Grimes lp->l_line = linep;
901714ac002SMark Johnston lp->l_next = st->st_linehead;
902714ac002SMark Johnston st->st_linehead = lp;
90358f0484fSRodney W. Grimes
90458f0484fSRodney W. Grimes /*
90558f0484fSRodney W. Grimes * If this is the one we wanted, we are done.
90658f0484fSRodney W. Grimes */
90758f0484fSRodney W. Grimes if (!strcmp(lp->l_groupname, group))
90858f0484fSRodney W. Grimes return (lp);
90958f0484fSRodney W. Grimes }
91058f0484fSRodney W. Grimes }
9111e890b05SBill Paul #ifdef YP
9121e890b05SBill Paul /*
9131e890b05SBill Paul * Yucky. The recursive nature of this whole mess might require
9141e890b05SBill Paul * us to make more than one pass through the netgroup file.
9151e890b05SBill Paul * This might be best left outside the #ifdef YP, but YP is
9161e890b05SBill Paul * defined by default anyway, so I'll leave it like this
9171e890b05SBill Paul * until I know better.
9181e890b05SBill Paul */
9191e890b05SBill Paul rewind(netf);
9201e890b05SBill Paul #endif
921fed7420cSGuy Helmer return (NULL);
92258f0484fSRodney W. Grimes }
923714ac002SMark Johnston
924714ac002SMark Johnston int
925714ac002SMark Johnston getnetgrent_r(char **hostp, char **userp, char **domp, char *buf, size_t bufsize)
926714ac002SMark Johnston {
927714ac002SMark Johnston int rv, ret_errno;
928714ac002SMark Johnston
929714ac002SMark Johnston ret_errno = 0;
930714ac002SMark Johnston rv = _nsdispatch(NULL, getnetgrent_dtab, NSDB_NETGROUP, "getnetgrent_r",
931714ac002SMark Johnston defaultsrc, hostp, userp, domp, buf, bufsize, &ret_errno);
932714ac002SMark Johnston if (rv == NS_SUCCESS) {
933714ac002SMark Johnston return (1);
934714ac002SMark Johnston } else {
935714ac002SMark Johnston errno = ret_errno;
936714ac002SMark Johnston return (0);
937714ac002SMark Johnston }
938714ac002SMark Johnston }
939714ac002SMark Johnston
940714ac002SMark Johnston int
941714ac002SMark Johnston getnetgrent(char **hostp, char **userp, char **domp)
942714ac002SMark Johnston {
943714ac002SMark Johnston static char *ngrp_storage;
944714ac002SMark Johnston static size_t ngrp_storage_size;
945714ac002SMark Johnston int ret_errno, rv;
946714ac002SMark Johnston
947714ac002SMark Johnston if (ngrp_storage == NULL) {
948714ac002SMark Johnston ngrp_storage_size = NGRP_STORAGE_INITIAL;
949714ac002SMark Johnston ngrp_storage = malloc(ngrp_storage_size);
950714ac002SMark Johnston if (ngrp_storage == NULL)
951714ac002SMark Johnston return (0);
952714ac002SMark Johnston }
953714ac002SMark Johnston
954714ac002SMark Johnston do {
955714ac002SMark Johnston ret_errno = 0;
956714ac002SMark Johnston rv = _nsdispatch(NULL, getnetgrent_dtab, NSDB_NETGROUP,
957714ac002SMark Johnston "getnetgrent_r", defaultsrc, hostp, userp, domp,
958714ac002SMark Johnston ngrp_storage, ngrp_storage_size, &ret_errno);
959714ac002SMark Johnston if (rv != NS_SUCCESS && ret_errno == ERANGE) {
960714ac002SMark Johnston ngrp_storage_size *= 2;
961714ac002SMark Johnston if (ngrp_storage_size > NGRP_STORAGE_MAX) {
962714ac002SMark Johnston free(ngrp_storage);
963714ac002SMark Johnston ngrp_storage = NULL;
964714ac002SMark Johnston errno = ERANGE;
965714ac002SMark Johnston return (0);
966714ac002SMark Johnston }
967714ac002SMark Johnston ngrp_storage = reallocf(ngrp_storage,
968714ac002SMark Johnston ngrp_storage_size);
969714ac002SMark Johnston if (ngrp_storage == NULL)
970714ac002SMark Johnston return (0);
971714ac002SMark Johnston }
972714ac002SMark Johnston } while (rv != NS_SUCCESS && ret_errno == ERANGE);
973714ac002SMark Johnston
974714ac002SMark Johnston if (rv == NS_SUCCESS) {
975714ac002SMark Johnston return (1);
976714ac002SMark Johnston } else {
977714ac002SMark Johnston errno = ret_errno;
978714ac002SMark Johnston return (0);
979714ac002SMark Johnston }
980714ac002SMark Johnston }
981714ac002SMark Johnston
982714ac002SMark Johnston void
983714ac002SMark Johnston setnetgrent(const char *netgroup)
984714ac002SMark Johnston {
985714ac002SMark Johnston
986714ac002SMark Johnston (void)_nsdispatch(NULL, setnetgrent_dtab, NSDB_NETGROUP, "setnetgrent",
987714ac002SMark Johnston defaultsrc, netgroup);
988714ac002SMark Johnston }
989714ac002SMark Johnston
990714ac002SMark Johnston void
991714ac002SMark Johnston endnetgrent(void)
992714ac002SMark Johnston {
993714ac002SMark Johnston
994714ac002SMark Johnston (void)_nsdispatch(NULL, endnetgrent_dtab, NSDB_NETGROUP, "endnetgrent",
995714ac002SMark Johnston defaultsrc);
996714ac002SMark Johnston }
997714ac002SMark Johnston
998714ac002SMark Johnston int
999714ac002SMark Johnston innetgr(const char *netgroup, const char *host, const char *user,
1000714ac002SMark Johnston const char *domain)
1001714ac002SMark Johnston {
1002714ac002SMark Johnston static const ns_dtab dtab[] = {
1003714ac002SMark Johnston NS_COMPAT_CB(compat_innetgr, NULL)
1004714ac002SMark Johnston NS_FALLBACK_CB(innetgr_fallback)
1005714ac002SMark Johnston { NULL, NULL, NULL },
1006714ac002SMark Johnston };
1007714ac002SMark Johnston int result, rv;
1008714ac002SMark Johnston
1009714ac002SMark Johnston rv = _nsdispatch(&result, dtab, NSDB_NETGROUP, "innetgr", defaultsrc,
1010714ac002SMark Johnston netgroup, host, user, domain);
1011714ac002SMark Johnston return (rv == NS_SUCCESS ? result : 0);
1012714ac002SMark Johnston }
1013