xref: /dragonfly/libexec/mknetid/parse_group.c (revision 2702099d)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)getgrent.c	8.2 (Berkeley) 3/21/94
30  * $FreeBSD: src/libexec/mknetid/parse_group.c,v 1.6 1999/08/28 00:09:42 peter Exp $
31  * $DragonFly: src/libexec/mknetid/parse_group.c,v 1.3 2006/08/03 16:40:46 swildner Exp $
32  */
33 
34 /*
35  * This is a slightly modified chunk of getgrent(3). All the YP support
36  * and unneeded functions have been stripped out.
37  */
38 
39 #include <sys/types.h>
40 #include <grp.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 struct group *_getgrent(void);
46 int _setgrent(void);
47 void _endgrent(void);
48 FILE *_gr_fp;
49 
50 static struct group _gr_group;
51 static int _gr_stayopen;
52 static int grscan(int, gid_t, char *), start_gr(void);
53 
54 #define	MAXGRP		200
55 static char *members[MAXGRP];
56 #define	MAXLINELENGTH	1024
57 static char line[MAXLINELENGTH];
58 
59 struct group *
60 _getgrent(void)
61 {
62 	if (!_gr_fp && !start_gr()) {
63 		return NULL;
64 	}
65 
66 
67 	if (!grscan(0, 0, NULL))
68 		return(NULL);
69 	return(&_gr_group);
70 }
71 
72 static int
73 start_gr(void)
74 {
75 	return 1;
76 }
77 
78 static int
79 _setgroupent(int stayopen)
80 {
81 	if (!start_gr())
82 		return(0);
83 	_gr_stayopen = stayopen;
84 	return(1);
85 }
86 
87 int
88 _setgrent(void)
89 {
90 	return(_setgroupent(0));
91 }
92 
93 void
94 _endgrent(void)
95 {
96 	if (_gr_fp) {
97 		(void)fclose(_gr_fp);
98 		_gr_fp = NULL;
99 	}
100 }
101 
102 static int
103 grscan(int search, gid_t gid, char *name)
104 {
105 	char *cp, **m;
106 	char *bp;
107 	for (;;) {
108 		if (!fgets(line, sizeof(line), _gr_fp))
109 			return(0);
110 		bp = line;
111 		/* skip lines that are too big */
112 		if (!index(line, '\n')) {
113 			int ch;
114 
115 			while ((ch = getc(_gr_fp)) != '\n' && ch != EOF)
116 				;
117 			continue;
118 		}
119 		if ((_gr_group.gr_name = strsep(&bp, ":\n")) == NULL)
120 			break;
121 		if (_gr_group.gr_name[0] == '+')
122 			continue;
123 
124 		if (search && name) {
125 			if(strcmp(_gr_group.gr_name, name)) {
126 				continue;
127 			}
128 		}
129 		if ((_gr_group.gr_passwd = strsep(&bp, ":\n")) == NULL)
130 			break;
131 		if (!(cp = strsep(&bp, ":\n")))
132 			continue;
133 		_gr_group.gr_gid = atoi(cp);
134 		if (search && name == NULL && _gr_group.gr_gid != gid)
135 			continue;
136 		cp = NULL;
137 		if (bp == NULL) /* !! Must check for this! */
138 			break;
139 		for (m = _gr_group.gr_mem = members;; bp++) {
140 			if (m == &members[MAXGRP - 1])
141 				break;
142 			if (*bp == ',') {
143 				if (cp) {
144 					*bp = '\0';
145 					*m++ = cp;
146 					cp = NULL;
147 				}
148 			} else if (*bp == '\0' || *bp == '\n' || *bp == ' ') {
149 				if (cp) {
150 					*bp = '\0';
151 					*m++ = cp;
152 			}
153 				break;
154 			} else if (cp == NULL)
155 				cp = bp;
156 		}
157 		*m = NULL;
158 		return(1);
159 	}
160 	/* NOTREACHED */
161 	return (0);
162 }
163