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