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