2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid[] = "$Id: lcl_ng.c,v 1.3 2005/04/27 04:56:31 sra Exp $";
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
37 #include <isc/memcluster.h>
39 #include "port_after.h"
46 #define NG_HOST 0 /*%< Host name */
47 #define NG_USER 1 /*%< User name */
48 #define NG_DOM 2 /*%< and Domain name */
49 #define LINSIZ 1024 /*%< Length of netgroup file line */
52 * This code is a hack-and-slash special. It realy needs to be
53 * rewritten with things like strdup, and realloc in mind.
54 * More reasonable data structures would not be a bad thing.
58 * Static Variables and functions used by setnetgrent(), getnetgrent() and
61 * There are two linked lists:
62 * \li linelist is just used by setnetgrent() to parse the net group file via.
64 * \li netgrp is the list of entries for the current netgroup
67 struct linelist *l_next; /*%< Chain ptr. */
68 int l_parsed; /*%< Flag for cycles */
69 char * l_groupname; /*%< Name of netgroup */
70 char * l_line; /*%< Netgroup entrie(s) to be parsed */
73 struct ng_old_struct {
74 struct ng_old_struct *ng_next; /*%< Chain ptr */
75 char * ng_str[3]; /*%< Field pointers, see below */
80 struct linelist *linehead;
81 struct ng_old_struct *nextgrp;
83 struct ng_old_struct *gr;
90 static void ng_rewind(struct irs_ng *, const char*);
91 static void ng_close(struct irs_ng *);
92 static int ng_next(struct irs_ng *, const char **,
93 const char **, const char **);
94 static int ng_test(struct irs_ng *, const char *,
95 const char *, const char *,
97 static void ng_minimize(struct irs_ng *);
99 static int parse_netgrp(struct irs_ng *, const char*);
100 static struct linelist *read_for_group(struct irs_ng *, const char *);
101 static void freelists(struct irs_ng *);
106 irs_lcl_ng(struct irs_acc *this) {
112 if (!(ng = memget(sizeof *ng))) {
116 memset(ng, 0x5e, sizeof *ng);
117 if (!(pvt = memget(sizeof *pvt))) {
118 memput(ng, sizeof *ng);
122 memset(pvt, 0, sizeof *pvt);
124 ng->close = ng_close;
127 ng->rewind = ng_rewind;
128 ng->minimize = ng_minimize;
135 ng_close(struct irs_ng *this) {
136 struct pvt *pvt = (struct pvt *)this->private;
141 memput(pvt, sizeof *pvt);
142 memput(this, sizeof *this);
146 * Parse the netgroup file looking for the netgroup and build the list
147 * of netgrp structures. Let parse_netgrp() and read_for_group() do
151 ng_rewind(struct irs_ng *this, const char *group) {
152 struct pvt *pvt = (struct pvt *)this->private;
154 if (pvt->fp != NULL && fseek(pvt->fp, SEEK_CUR, 0L) == -1) {
159 if (pvt->fp == NULL || pvt->grouphead.gr == NULL ||
160 strcmp(group, pvt->grouphead.grname)) {
164 pvt->fp = fopen(_PATH_NETGROUP, "r");
165 if (pvt->fp != NULL) {
166 if (parse_netgrp(this, group))
168 if (!(pvt->grouphead.grname = strdup(group)))
174 pvt->nextgrp = pvt->grouphead.gr;
178 * Get the next netgroup off the list.
181 ng_next(struct irs_ng *this, const char **host, const char **user,
184 struct pvt *pvt = (struct pvt *)this->private;
187 *host = pvt->nextgrp->ng_str[NG_HOST];
188 *user = pvt->nextgrp->ng_str[NG_USER];
189 *domain = pvt->nextgrp->ng_str[NG_DOM];
190 pvt->nextgrp = pvt->nextgrp->ng_next;
197 * Search for a match in a netgroup.
200 ng_test(struct irs_ng *this, const char *name,
201 const char *host, const char *user, const char *domain)
203 const char *ng_host, *ng_user, *ng_domain;
205 ng_rewind(this, name);
206 while (ng_next(this, &ng_host, &ng_user, &ng_domain))
207 if ((host == NULL || ng_host == NULL ||
208 !strcmp(host, ng_host)) &&
209 (user == NULL || ng_user == NULL ||
210 !strcmp(user, ng_user)) &&
211 (domain == NULL || ng_domain == NULL ||
212 !strcmp(domain, ng_domain))) {
221 ng_minimize(struct irs_ng *this) {
222 struct pvt *pvt = (struct pvt *)this->private;
224 if (pvt->fp != NULL) {
225 (void)fclose(pvt->fp);
233 * endnetgrent() - cleanup
236 freelists(struct irs_ng *this) {
237 struct pvt *pvt = (struct pvt *)this->private;
238 struct linelist *lp, *olp;
239 struct ng_old_struct *gp, *ogp;
245 free(olp->l_groupname);
249 pvt->linehead = NULL;
250 if (pvt->grouphead.grname) {
251 free(pvt->grouphead.grname);
252 pvt->grouphead.grname = NULL;
254 gp = pvt->grouphead.gr;
258 if (ogp->ng_str[NG_HOST])
259 free(ogp->ng_str[NG_HOST]);
260 if (ogp->ng_str[NG_USER])
261 free(ogp->ng_str[NG_USER]);
262 if (ogp->ng_str[NG_DOM])
263 free(ogp->ng_str[NG_DOM]);
266 pvt->grouphead.gr = NULL;
270 * Parse the netgroup file setting up the linked lists.
273 parse_netgrp(struct irs_ng *this, const char *group) {
274 struct pvt *pvt = (struct pvt *)this->private;
278 struct ng_old_struct *grp;
279 struct linelist *lp = pvt->linehead;
282 * First, see if the line has already been read in.
285 if (!strcmp(group, lp->l_groupname))
290 (lp = read_for_group(this, group)) == NULL)
293 /*fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);*/
298 while (*pos != '\0') {
300 if (!(grp = malloc(sizeof (struct ng_old_struct)))) {
305 memset(grp, 0, sizeof (struct ng_old_struct));
306 grp->ng_next = pvt->grouphead.gr;
307 pvt->grouphead.gr = grp;
309 gpos = strsep(&pos, ")");
310 for (strpos = 0; strpos < 3; strpos++) {
311 if ((spos = strsep(&gpos, ","))) {
312 while (*spos == ' ' || *spos == '\t')
314 if ((epos = strpbrk(spos, " \t"))) {
320 if(!(grp->ng_str[strpos]
326 memcpy(grp->ng_str[strpos],
334 spos = strsep(&pos, ", \t");
335 if (spos != NULL && parse_netgrp(this, spos)) {
342 while (*pos == ' ' || *pos == ',' || *pos == '\t')
347 /*fprintf(stderr, "Bad netgroup %s at ..%s\n", lp->l_groupname,
353 * Read the netgroup file and save lines until the line for the netgroup
354 * is found. Return 1 if eof is encountered.
356 static struct linelist *
357 read_for_group(struct irs_ng *this, const char *group) {
358 struct pvt *pvt = (struct pvt *)this->private;
359 char *pos, *spos, *linep = NULL, *olinep;
362 char line[LINSIZ + 1];
364 while (fgets(line, LINSIZ, pvt->fp) != NULL) {
368 while (*pos == ' ' || *pos == '\t')
371 while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
375 while (*pos == ' ' || *pos == '\t')
377 if (*pos != '\n' && *pos != '\0') {
378 if (!(lp = malloc(sizeof (*lp)))) {
383 if (!(lp->l_groupname = malloc(len + 1))) {
388 memcpy(lp->l_groupname, spos, len);
389 *(lp->l_groupname + len) = '\0';
395 * Loop around handling line continuations.
398 if (*(pos + len - 1) == '\n')
400 if (*(pos + len - 1) == '\\') {
406 if (!(linep = malloc(olen + len + 1))){
409 free(lp->l_groupname);
416 memcpy(linep, olinep, olen);
419 memcpy(linep + olen, pos, len);
421 *(linep + olen) = '\0';
425 if (fgets(line, LINSIZ, pvt->fp)) {
433 lp->l_next = pvt->linehead;
437 * If this is the one we wanted, we are done.
439 if (!strcmp(lp->l_groupname, group))