Unleashed v1.4
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / lcl_ng.c
blob07cb0218ba238ab0f1703821fbdd8d42a572bf51
1 /*
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 /* Imports */
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/nameser.h>
25 #include <resolv.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include <irs.h>
33 #include <isc/memcluster.h>
35 #include "port_after.h"
37 #include "irs_p.h"
38 #include "lcl_p.h"
40 /* Definitions */
42 #define NG_HOST 0 /*%< Host name */
43 #define NG_USER 1 /*%< User name */
44 #define NG_DOM 2 /*%< and Domain name */
45 #define LINSIZ 1024 /*%< Length of netgroup file line */
47 * XXX Warning XXX
48 * This code is a hack-and-slash special. It realy needs to be
49 * rewritten with things like strdup, and realloc in mind.
50 * More reasonable data structures would not be a bad thing.
53 /*%
54 * Static Variables and functions used by setnetgrent(), getnetgrent() and
55 * endnetgrent().
57 * There are two linked lists:
58 * \li linelist is just used by setnetgrent() to parse the net group file via.
59 * parse_netgrp()
60 * \li netgrp is the list of entries for the current netgroup
62 struct linelist {
63 struct linelist *l_next; /*%< Chain ptr. */
64 int l_parsed; /*%< Flag for cycles */
65 char * l_groupname; /*%< Name of netgroup */
66 char * l_line; /*%< Netgroup entrie(s) to be parsed */
69 struct ng_old_struct {
70 struct ng_old_struct *ng_next; /*%< Chain ptr */
71 char * ng_str[3]; /*%< Field pointers, see below */
74 struct pvt {
75 FILE *fp;
76 struct linelist *linehead;
77 struct ng_old_struct *nextgrp;
78 struct {
79 struct ng_old_struct *gr;
80 char *grname;
81 } grouphead;
84 /* Forward */
86 static void ng_rewind(struct irs_ng *, const char*);
87 static void ng_close(struct irs_ng *);
88 static int ng_next(struct irs_ng *, const char **,
89 const char **, const char **);
90 static int ng_test(struct irs_ng *, const char *,
91 const char *, const char *,
92 const char *);
93 static void ng_minimize(struct irs_ng *);
95 static int parse_netgrp(struct irs_ng *, const char*);
96 static struct linelist *read_for_group(struct irs_ng *, const char *);
97 static void freelists(struct irs_ng *);
99 /* Public */
101 struct irs_ng *
102 irs_lcl_ng(struct irs_acc *this) {
103 struct irs_ng *ng;
104 struct pvt *pvt;
106 UNUSED(this);
108 if (!(ng = memget(sizeof *ng))) {
109 errno = ENOMEM;
110 return (NULL);
112 memset(ng, 0x5e, sizeof *ng);
113 if (!(pvt = memget(sizeof *pvt))) {
114 memput(ng, sizeof *ng);
115 errno = ENOMEM;
116 return (NULL);
118 memset(pvt, 0, sizeof *pvt);
119 ng->private = pvt;
120 ng->close = ng_close;
121 ng->next = ng_next;
122 ng->test = ng_test;
123 ng->rewind = ng_rewind;
124 ng->minimize = ng_minimize;
125 return (ng);
128 /* Methods */
130 static void
131 ng_close(struct irs_ng *this) {
132 struct pvt *pvt = (struct pvt *)this->private;
134 if (pvt->fp != NULL)
135 fclose(pvt->fp);
136 freelists(this);
137 memput(pvt, sizeof *pvt);
138 memput(this, sizeof *this);
142 * Parse the netgroup file looking for the netgroup and build the list
143 * of netgrp structures. Let parse_netgrp() and read_for_group() do
144 * most of the work.
146 static void
147 ng_rewind(struct irs_ng *this, const char *group) {
148 struct pvt *pvt = (struct pvt *)this->private;
150 if (pvt->fp != NULL && fseek(pvt->fp, SEEK_CUR, 0L) == -1) {
151 fclose(pvt->fp);
152 pvt->fp = NULL;
155 if (pvt->fp == NULL || pvt->grouphead.gr == NULL ||
156 strcmp(group, pvt->grouphead.grname)) {
157 freelists(this);
158 if (pvt->fp != NULL)
159 fclose(pvt->fp);
160 pvt->fp = fopen(_PATH_NETGROUP, "r");
161 if (pvt->fp != NULL) {
162 if (parse_netgrp(this, group))
163 freelists(this);
164 if (!(pvt->grouphead.grname = strdup(group)))
165 freelists(this);
166 fclose(pvt->fp);
167 pvt->fp = NULL;
170 pvt->nextgrp = pvt->grouphead.gr;
174 * Get the next netgroup off the list.
176 static int
177 ng_next(struct irs_ng *this, const char **host, const char **user,
178 const char **domain)
180 struct pvt *pvt = (struct pvt *)this->private;
182 if (pvt->nextgrp) {
183 *host = pvt->nextgrp->ng_str[NG_HOST];
184 *user = pvt->nextgrp->ng_str[NG_USER];
185 *domain = pvt->nextgrp->ng_str[NG_DOM];
186 pvt->nextgrp = pvt->nextgrp->ng_next;
187 return (1);
189 return (0);
193 * Search for a match in a netgroup.
195 static int
196 ng_test(struct irs_ng *this, const char *name,
197 const char *host, const char *user, const char *domain)
199 const char *ng_host, *ng_user, *ng_domain;
201 ng_rewind(this, name);
202 while (ng_next(this, &ng_host, &ng_user, &ng_domain))
203 if ((host == NULL || ng_host == NULL ||
204 !strcmp(host, ng_host)) &&
205 (user == NULL || ng_user == NULL ||
206 !strcmp(user, ng_user)) &&
207 (domain == NULL || ng_domain == NULL ||
208 !strcmp(domain, ng_domain))) {
209 freelists(this);
210 return (1);
212 freelists(this);
213 return (0);
216 static void
217 ng_minimize(struct irs_ng *this) {
218 struct pvt *pvt = (struct pvt *)this->private;
220 if (pvt->fp != NULL) {
221 (void)fclose(pvt->fp);
222 pvt->fp = NULL;
226 /* Private */
229 * endnetgrent() - cleanup
231 static void
232 freelists(struct irs_ng *this) {
233 struct pvt *pvt = (struct pvt *)this->private;
234 struct linelist *lp, *olp;
235 struct ng_old_struct *gp, *ogp;
237 lp = pvt->linehead;
238 while (lp) {
239 olp = lp;
240 lp = lp->l_next;
241 free(olp->l_groupname);
242 free(olp->l_line);
243 free((char *)olp);
245 pvt->linehead = NULL;
246 if (pvt->grouphead.grname) {
247 free(pvt->grouphead.grname);
248 pvt->grouphead.grname = NULL;
250 gp = pvt->grouphead.gr;
251 while (gp) {
252 ogp = gp;
253 gp = gp->ng_next;
254 free(ogp->ng_str[NG_HOST]);
255 free(ogp->ng_str[NG_USER]);
256 free(ogp->ng_str[NG_DOM]);
257 free((char *)ogp);
259 pvt->grouphead.gr = NULL;
263 * Parse the netgroup file setting up the linked lists.
265 static int
266 parse_netgrp(struct irs_ng *this, const char *group) {
267 struct pvt *pvt = (struct pvt *)this->private;
268 char *spos, *epos;
269 int len, strpos;
270 char *pos, *gpos;
271 struct ng_old_struct *grp;
272 struct linelist *lp = pvt->linehead;
275 * First, see if the line has already been read in.
277 while (lp) {
278 if (!strcmp(group, lp->l_groupname))
279 break;
280 lp = lp->l_next;
282 if (lp == NULL &&
283 (lp = read_for_group(this, group)) == NULL)
284 return (1);
285 if (lp->l_parsed) {
286 /*fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);*/
287 return (1);
288 } else
289 lp->l_parsed = 1;
290 pos = lp->l_line;
291 while (*pos != '\0') {
292 if (*pos == '(') {
293 if (!(grp = malloc(sizeof (struct ng_old_struct)))) {
294 freelists(this);
295 errno = ENOMEM;
296 return (1);
298 memset(grp, 0, sizeof (struct ng_old_struct));
299 grp->ng_next = pvt->grouphead.gr;
300 pvt->grouphead.gr = grp;
301 pos++;
302 gpos = strsep(&pos, ")");
303 for (strpos = 0; strpos < 3; strpos++) {
304 if ((spos = strsep(&gpos, ","))) {
305 while (*spos == ' ' || *spos == '\t')
306 spos++;
307 if ((epos = strpbrk(spos, " \t"))) {
308 *epos = '\0';
309 len = epos - spos;
310 } else
311 len = strlen(spos);
312 if (len > 0) {
313 if(!(grp->ng_str[strpos]
314 = (char *)
315 malloc(len + 1))) {
316 freelists(this);
317 return (1);
319 memcpy(grp->ng_str[strpos],
320 spos,
321 len + 1);
323 } else
324 goto errout;
326 } else {
327 spos = strsep(&pos, ", \t");
328 if (spos != NULL && parse_netgrp(this, spos)) {
329 freelists(this);
330 return (1);
333 if (pos == NULL)
334 break;
335 while (*pos == ' ' || *pos == ',' || *pos == '\t')
336 pos++;
338 return (0);
339 errout:
340 /*fprintf(stderr, "Bad netgroup %s at ..%s\n", lp->l_groupname,
341 spos);*/
342 return (1);
346 * Read the netgroup file and save lines until the line for the netgroup
347 * is found. Return 1 if eof is encountered.
349 static struct linelist *
350 read_for_group(struct irs_ng *this, const char *group) {
351 struct pvt *pvt = (struct pvt *)this->private;
352 char *pos, *spos, *linep = NULL, *olinep;
353 int len, olen, cont;
354 struct linelist *lp;
355 char line[LINSIZ + 1];
357 while (fgets(line, LINSIZ, pvt->fp) != NULL) {
358 pos = line;
359 if (*pos == '#')
360 continue;
361 while (*pos == ' ' || *pos == '\t')
362 pos++;
363 spos = pos;
364 while (*pos != ' ' && *pos != '\t' && *pos != '\n' &&
365 *pos != '\0')
366 pos++;
367 len = pos - spos;
368 while (*pos == ' ' || *pos == '\t')
369 pos++;
370 if (*pos != '\n' && *pos != '\0') {
371 if (!(lp = malloc(sizeof (*lp)))) {
372 freelists(this);
373 return (NULL);
375 lp->l_parsed = 0;
376 if (!(lp->l_groupname = malloc(len + 1))) {
377 free(lp);
378 freelists(this);
379 return (NULL);
381 memcpy(lp->l_groupname, spos, len);
382 *(lp->l_groupname + len) = '\0';
383 len = strlen(pos);
384 olen = 0;
385 olinep = NULL;
388 * Loop around handling line continuations.
390 do {
391 if (*(pos + len - 1) == '\n')
392 len--;
393 if (*(pos + len - 1) == '\\') {
394 len--;
395 cont = 1;
396 } else
397 cont = 0;
398 if (len > 0) {
399 if (!(linep = malloc(olen + len + 1))){
400 if (olen > 0)
401 free(olinep);
402 free(lp->l_groupname);
403 free(lp);
404 freelists(this);
405 errno = ENOMEM;
406 return (NULL);
408 if (olen > 0) {
409 memcpy(linep, olinep, olen);
410 free(olinep);
412 memcpy(linep + olen, pos, len);
413 olen += len;
414 *(linep + olen) = '\0';
415 olinep = linep;
417 if (cont) {
418 if (fgets(line, LINSIZ, pvt->fp)) {
419 pos = line;
420 len = strlen(pos);
421 } else
422 cont = 0;
424 } while (cont);
425 lp->l_line = linep;
426 lp->l_next = pvt->linehead;
427 pvt->linehead = lp;
430 * If this is the one we wanted, we are done.
432 if (!strcmp(lp->l_groupname, group))
433 return (lp);
436 return (NULL);
439 /*! \file */