Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libc / gen / getgrent.c
1 /*      $NetBSD: getgrent.c,v 1.34.2.1 1999/04/27 14:10:58 perry Exp $  */
2 /*
3  * Copyright (c) 1989, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  * Portions Copyright (c) 1994, Jason Downs. All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * @(#)getgrent.c       8.2 (Berkeley) 3/21/94
36  * $FreeBSD: src/lib/libc/gen/getgrent.c,v 1.17.6.1 2001/03/05 08:56:02 obrien Exp $
37  * $DragonFly: src/lib/libc/gen/getgrent.c,v 1.2 2003/06/17 04:26:42 dillon Exp $
38  */
39
40 #include <errno.h>
41 #include <limits.h>
42 #include <sys/types.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <grp.h>
47 #include <syslog.h>
48
49 static FILE *_gr_fp;
50 static struct group _gr_group;
51 static int _gr_stayopen;
52 static int grscan(), start_gr();
53 #ifdef YP
54 #include <rpc/rpc.h>
55 #include <rpcsvc/yp_prot.h>
56 #include <rpcsvc/ypclnt.h>
57 static int _gr_stepping_yp;
58 static int _gr_yp_enabled;
59 static int _getypgroup(struct group *, const char *, char *);
60 static int _nextypgroup(struct group *);
61 #endif
62
63 /* initial size for malloc and increase steps for realloc */
64 #define MAXGRP          64
65 #define MAXLINELENGTH   256
66
67 static char **members;          /* list of group members */
68 static int maxgrp;              /* current length of **mebers */
69 static char *line;              /* temp buffer for group line */
70 static int maxlinelength;       /* current length of *line */
71
72 /* 
73  * Lines longer than MAXLINELENGTHLIMIT will be counted as an error.
74  * <= 0 disable check for maximum line length
75  * 256K is enough for 64,000 uids
76  */
77 #define MAXLINELENGTHLIMIT      (256 * 1024)
78 #define GROUP_IGNORE_COMMENTS   1       /* allow comments in /etc/group */
79
80 struct group *
81 getgrent()
82 {
83         if (!_gr_fp && !start_gr()) {
84                 return NULL;
85         }
86
87 #ifdef YP
88         if (_gr_stepping_yp) {
89                 if (_nextypgroup(&_gr_group))
90                         return(&_gr_group);
91         }
92 tryagain:
93 #endif
94
95         if (!grscan(0, 0, NULL))
96                 return(NULL);
97 #ifdef YP
98         if(_gr_group.gr_name[0] == '+' && _gr_group.gr_name[1]) {
99                 _getypgroup(&_gr_group, &_gr_group.gr_name[1],
100                             "group.byname");
101         } else if(_gr_group.gr_name[0] == '+') {
102                 if (!_nextypgroup(&_gr_group))
103                         goto tryagain;
104                 else
105                         return(&_gr_group);
106         }
107 #endif
108         return(&_gr_group);
109 }
110
111 struct group *
112 getgrnam(name)
113         const char *name;
114 {
115         int rval;
116
117         if (!start_gr())
118                 return(NULL);
119 #ifdef YP
120         tryagain:
121 #endif
122         rval = grscan(1, 0, name);
123 #ifdef YP
124         if(rval == -1 && (_gr_yp_enabled < 0 || (_gr_yp_enabled &&
125                                         _gr_group.gr_name[0] == '+'))) {
126                 if (!(rval = _getypgroup(&_gr_group, name, "group.byname")))
127                         goto tryagain;
128         }
129 #endif
130         if (!_gr_stayopen)
131                 endgrent();
132         return (rval) ? &_gr_group : NULL;
133 }
134
135 struct group *
136 getgrgid(gid)
137         gid_t gid;
138 {
139         int rval;
140
141         if (!start_gr())
142                 return(NULL);
143 #ifdef YP
144         tryagain:
145 #endif
146         rval = grscan(1, gid, NULL);
147 #ifdef YP
148         if(rval == -1 && _gr_yp_enabled) {
149                 char buf[16];
150                 snprintf(buf, sizeof buf, "%d", (unsigned)gid);
151                 if (!(rval = _getypgroup(&_gr_group, buf, "group.bygid")))
152                         goto tryagain;
153         }
154 #endif
155         if (!_gr_stayopen)
156                 endgrent();
157         return (rval) ? &_gr_group : NULL;
158 }
159
160 static int
161 start_gr()
162 {
163         if (_gr_fp) {
164                 rewind(_gr_fp);
165                 return(1);
166         }
167         _gr_fp = fopen(_PATH_GROUP, "r");
168         if(!_gr_fp) return 0;
169 #ifdef YP
170         /*
171          * This is a disgusting hack, used to determine when YP is enabled.
172          * This would be easier if we had a group database to go along with
173          * the password database.
174          */
175         {
176                 char *line;
177                 size_t linelen;
178                 _gr_yp_enabled = 0;
179                 while((line = fgetln(_gr_fp, &linelen)) != NULL) {
180                         if(line[0] == '+') {
181                                 if(line[1] && line[1] != ':' && !_gr_yp_enabled) {
182                                         _gr_yp_enabled = 1;
183                                 } else {
184                                         _gr_yp_enabled = -1;
185                                         break;
186                                 }
187                         }
188                 }
189                 rewind(_gr_fp);
190         }
191 #endif
192
193         if (maxlinelength == 0) {
194                 if ((line = (char *)malloc(MAXLINELENGTH)) == NULL)
195                         return 0;
196                 maxlinelength += MAXLINELENGTH;
197         }
198
199         if (maxgrp == 0) {
200                 if ((members = (char **) malloc(sizeof(char **) * 
201                                                MAXGRP)) == NULL)
202                         return 0;
203                 maxgrp += MAXGRP;
204         }
205
206         return 1;
207 }
208
209 int
210 setgrent(void)
211 {
212         return setgroupent(0);
213 }
214
215 int
216 setgroupent(stayopen)
217         int stayopen;
218 {
219         if (!start_gr())
220                 return 0;
221         _gr_stayopen = stayopen;
222 #ifdef YP
223         _gr_stepping_yp = 0;
224 #endif
225         return 1;
226 }
227
228 void
229 endgrent()
230 {
231 #ifdef YP
232         _gr_stepping_yp = 0;
233 #endif
234         if (_gr_fp) {
235                 (void)fclose(_gr_fp);
236                 _gr_fp = NULL;
237         }
238 }
239
240 static int
241 grscan(search, gid, name)
242         register int search, gid;
243         register char *name;
244 {
245         register char *cp, **m;
246         char *bp;
247
248
249 #ifdef YP
250         int _ypfound;
251 #endif
252         for (;;) {
253 #ifdef YP
254                 _ypfound = 0;
255 #endif
256                 if (fgets(line, maxlinelength, _gr_fp) == NULL)
257                         return(0);
258
259                 if (!index(line, '\n')) {
260                         do {
261                                 if (feof(_gr_fp))
262                                         return(0);
263                         
264                                 /* don't allocate infinite memory */
265                                 if (MAXLINELENGTHLIMIT > 0 && 
266                                     maxlinelength >= MAXLINELENGTHLIMIT)
267                                         return(0);
268
269                                 if ((line = (char *)reallocf(line, 
270                                      sizeof(char) * 
271                                      (maxlinelength + MAXLINELENGTH))) == NULL)
272                                         return(0);
273                         
274                                 if (fgets(line + maxlinelength - 1, 
275                                           MAXLINELENGTH + 1, _gr_fp) == NULL)
276                                         return(0);
277
278                                 maxlinelength += MAXLINELENGTH;
279                         } while (!index(line + maxlinelength - 
280                                        MAXLINELENGTH - 1, '\n'));
281                 }
282
283 #ifdef GROUP_IGNORE_COMMENTS
284                 /* 
285                  * Ignore comments: ^[ \t]*#
286                  */
287                 for (cp = line; *cp != '\0'; cp++)
288                         if (*cp != ' ' && *cp != '\t')
289                                 break;
290                 if (*cp == '#' || *cp == '\0')
291                         continue;
292 #endif
293
294                 bp = line;
295
296                 if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)
297                         break;
298 #ifdef YP
299                 /*
300                  * XXX   We need to be careful to avoid proceeding
301                  * past this point under certain circumstances or
302                  * we risk dereferencing null pointers down below.
303                  */
304                 if (_gr_group.gr_name[0] == '+') {
305                         if (strlen(_gr_group.gr_name) == 1) {
306                                 switch(search) {
307                                 case 0:
308                                         return(1);
309                                 case 1:
310                                         return(-1);
311                                 default:
312                                         return(0);
313                                 }
314                         } else {
315                                 cp = &_gr_group.gr_name[1];
316                                 if (search && name != NULL)
317                                         if (strcmp(cp, name))
318                                                 continue;
319                                 if (!_getypgroup(&_gr_group, cp,
320                                                 "group.byname"))
321                                         continue;
322                                 if (search && name == NULL)
323                                         if (gid != _gr_group.gr_gid)
324                                                 continue;
325                         /* We're going to override -- tell the world. */
326                                 _ypfound++;
327                         }
328                 }
329 #else
330                 if (_gr_group.gr_name[0] == '+')
331                         continue;
332 #endif /* YP */
333                 if (search && name) {
334                         if(strcmp(_gr_group.gr_name, name)) {
335                                 continue;
336                         }
337                 }
338 #ifdef YP
339                 if ((cp = strsep(&bp, ":\n")) == NULL)
340                         if (_ypfound)
341                                 return(1);
342                         else
343                                 break;
344                 if (strlen(cp) || !_ypfound)
345                         _gr_group.gr_passwd = cp;
346 #else
347                 if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL)
348                         break;
349 #endif
350                 if (!(cp = strsep(&bp, ":\n")))
351 #ifdef YP
352                         if (_ypfound)
353                                 return(1);
354                         else
355 #endif
356                                 continue;
357 #ifdef YP
358                 /*
359                  * Hurm. Should we be doing this? We allow UIDs to
360                  * be overridden -- what about GIDs?
361                  */
362                 if (!_ypfound)
363 #endif
364                 _gr_group.gr_gid = atoi(cp);
365                 if (search && name == NULL && _gr_group.gr_gid != gid)
366                         continue;
367                 cp = NULL;
368                 if (bp == NULL) /* !!! Must check for this! */
369                         break;
370 #ifdef YP
371                 if ((cp = strsep(&bp, ":\n")) == NULL)
372                         break;
373
374                 if (!strlen(cp) && _ypfound)
375                         return(1);
376                 else
377                         members[0] = NULL;
378                 bp = cp;
379                 cp = NULL;
380 #endif
381                 for (m = members; ; bp++) {
382                         if (m == (members + maxgrp - 1)) {
383                                 if ((members = (char **)
384                                      reallocf(members, 
385                                              sizeof(char **) * 
386                                              (maxgrp + MAXGRP))) == NULL)
387                                         return(0);
388                                 m = members + maxgrp - 1;
389                                 maxgrp += MAXGRP;
390                         }
391                         if (*bp == ',') {
392                                 if (cp) {
393                                         *bp = '\0';
394                                         *m++ = cp;
395                                         cp = NULL;
396                                 }
397                         } else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
398                                 if (cp) {
399                                         *bp = '\0';
400                                         *m++ = cp;
401                                 }
402                                 break;
403                         } else if (cp == NULL)
404                                 cp = bp;
405                         
406                 }
407                 _gr_group.gr_mem = members;
408                 *m = NULL;
409                 return(1);
410         }
411         /* NOTREACHED */
412         return (0);
413 }
414
415 #ifdef YP
416
417 static int
418 _gr_breakout_yp(struct group *gr, char *result)
419 {
420         char *s, *cp;
421         char **m;
422
423         /*
424          * XXX If 's' ends up being a NULL pointer, punt on this group.
425          * It means the NIS group entry is badly formatted and should
426          * be skipped.
427          */
428         if ((s = strsep(&result, ":")) == NULL) return 0; /* name */
429         gr->gr_name = s;
430
431         if ((s = strsep(&result, ":")) == NULL) return 0; /* password */
432         gr->gr_passwd = s;
433
434         if ((s = strsep(&result, ":")) == NULL) return 0; /* gid */
435         gr->gr_gid = atoi(s);
436
437         if ((s = result) == NULL) return 0;
438         cp = 0;
439
440         for (m = members; ; s++) {
441                 if (m == members + maxgrp - 1) {
442                         if ((members = (char **)reallocf(members, 
443                              sizeof(char **) * (maxgrp + MAXGRP))) == NULL)
444                                 return(0);
445                         m = members + maxgrp - 1;
446                         maxgrp += MAXGRP;
447                 }
448                 if (*s == ',') {
449                         if (cp) {
450                                 *s = '\0';
451                                 *m++ = cp;
452                                 cp = NULL;
453                         }
454                 } else if (*s == '\0' || *s == '\n' || *s == ' ') {
455                         if (cp) {
456                                 *s = '\0';
457                                 *m++ = cp;
458                         }
459                         break;
460                 } else if (cp == NULL) {
461                         cp = s;
462                 }
463         }
464         _gr_group.gr_mem = members;
465         *m = NULL;
466
467         return 1;
468 }
469
470 static char *_gr_yp_domain;
471
472 static int
473 _getypgroup(struct group *gr, const char *name, char *map)
474 {
475         char *result, *s;
476         static char resultbuf[YPMAXRECORD + 2];
477         int resultlen;
478
479         if(!_gr_yp_domain) {
480                 if(yp_get_default_domain(&_gr_yp_domain))
481                   return 0;
482         }
483
484         if(yp_match(_gr_yp_domain, map, name, strlen(name),
485                     &result, &resultlen))
486                 return 0;
487
488         s = strchr(result, '\n');
489         if(s) *s = '\0';
490
491         if(resultlen >= sizeof resultbuf) return 0;
492         strncpy(resultbuf, result, resultlen);
493         resultbuf[resultlen] = '\0';
494         free(result);
495         return(_gr_breakout_yp(gr, resultbuf));
496
497 }
498
499
500 static int
501 _nextypgroup(struct group *gr)
502 {
503         static char *key;
504         static int keylen;
505         char *lastkey, *result;
506         static char resultbuf[YPMAXRECORD + 2];
507         int resultlen;
508         int rv;
509
510         if(!_gr_yp_domain) {
511                 if(yp_get_default_domain(&_gr_yp_domain))
512                   return 0;
513         }
514
515         if(!_gr_stepping_yp) {
516                 if(key) free(key);
517                 rv = yp_first(_gr_yp_domain, "group.byname",
518                               &key, &keylen, &result, &resultlen);
519                 if(rv) {
520                         return 0;
521                 }
522                 _gr_stepping_yp = 1;
523                 goto unpack;
524         } else {
525 tryagain:
526                 lastkey = key;
527                 rv = yp_next(_gr_yp_domain, "group.byname", key, keylen,
528                              &key, &keylen, &result, &resultlen);
529                 free(lastkey);
530 unpack:
531                 if(rv) {
532                         _gr_stepping_yp = 0;
533                         return 0;
534                 }
535
536                 if(resultlen > sizeof(resultbuf)) {
537                         free(result);
538                         goto tryagain;
539                 }
540
541                 strncpy(resultbuf, result, resultlen);
542                 resultbuf[resultlen] = '\0';
543                 free(result);
544                 if((result = strchr(resultbuf, '\n')) != NULL)
545                         *result = '\0';
546                 if (_gr_breakout_yp(gr, resultbuf))
547                         return(1);
548                 else
549                         goto tryagain;
550         }
551 }
552
553 #endif /* YP */