xref: /freebsd/usr.sbin/pw/pw_vpw.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1996
5  *	David L. Nugent.  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  *
16  * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <pwd.h>
36 #include <grp.h>
37 #include <libutil.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <err.h>
42 
43 #include "pwupd.h"
44 
45 static FILE * pwd_fp = NULL;
46 
47 void
48 vendpwent(void)
49 {
50 	if (pwd_fp != NULL) {
51 		fclose(pwd_fp);
52 		pwd_fp = NULL;
53 	}
54 }
55 
56 void
57 vsetpwent(void)
58 {
59 	vendpwent();
60 }
61 
62 static struct passwd *
63 vnextpwent(char const *nam, uid_t uid, int doclose)
64 {
65 	struct passwd *pw;
66 	char *line;
67 	size_t linecap;
68 	ssize_t linelen;
69 
70 	pw = NULL;
71 	line = NULL;
72 	linecap = 0;
73 
74 	if (pwd_fp != NULL || (pwd_fp = fopen(getpwpath(_MASTERPASSWD), "r")) != NULL) {
75 		while ((linelen = getline(&line, &linecap, pwd_fp)) > 0) {
76 			/* Skip comments and empty lines */
77 			if (*line == '\n' || *line == '#')
78 				continue;
79 			/* trim latest \n */
80 			if (line[linelen - 1 ] == '\n')
81 				line[linelen - 1] = '\0';
82 			pw = pw_scan(line, PWSCAN_MASTER);
83 			if (pw == NULL)
84 				errx(EXIT_FAILURE, "Invalid user entry in '%s':"
85 				    " '%s'", getpwpath(_MASTERPASSWD), line);
86 			if (uid != (uid_t)-1) {
87 				if (uid == pw->pw_uid)
88 					break;
89 			} else if (nam != NULL) {
90 				if (strcmp(nam, pw->pw_name) == 0)
91 					break;
92 			} else
93 				break;
94 			free(pw);
95 			pw = NULL;
96 		}
97 		if (doclose)
98 			vendpwent();
99 	}
100 	free(line);
101 
102 	return (pw);
103 }
104 
105 struct passwd *
106 vgetpwent(void)
107 {
108   return vnextpwent(NULL, -1, 0);
109 }
110 
111 struct passwd *
112 vgetpwuid(uid_t uid)
113 {
114   return vnextpwent(NULL, uid, 1);
115 }
116 
117 struct passwd *
118 vgetpwnam(const char * nam)
119 {
120   return vnextpwent(nam, -1, 1);
121 }
122 
123 
124 static FILE * grp_fp = NULL;
125 
126 void
127 vendgrent(void)
128 {
129 	if (grp_fp != NULL) {
130 		fclose(grp_fp);
131 		grp_fp = NULL;
132 	}
133 }
134 
135 void
136 vsetgrent(void)
137 {
138 	vendgrent();
139 }
140 
141 static struct group *
142 vnextgrent(char const *nam, gid_t gid, int doclose)
143 {
144 	struct group *gr;
145 	char *line;
146 	size_t linecap;
147 	ssize_t linelen;
148 
149 	gr = NULL;
150 	line = NULL;
151 	linecap = 0;
152 
153 	if (grp_fp != NULL || (grp_fp = fopen(getgrpath(_GROUP), "r")) != NULL) {
154 		while ((linelen = getline(&line, &linecap, grp_fp)) > 0) {
155 			/* Skip comments and empty lines */
156 			if (*line == '\n' || *line == '#')
157 				continue;
158 			/* trim latest \n */
159 			if (line[linelen - 1 ] == '\n')
160 				line[linelen - 1] = '\0';
161 			gr = gr_scan(line);
162 			if (gr == NULL)
163 				errx(EXIT_FAILURE, "Invalid group entry in '%s':"
164 				    " '%s'", getgrpath(_GROUP), line);
165 			if (gid != (gid_t)-1) {
166 				if (gid == gr->gr_gid)
167 					break;
168 			} else if (nam != NULL) {
169 				if (strcmp(nam, gr->gr_name) == 0)
170 					break;
171 			} else
172 				break;
173 			free(gr);
174 			gr = NULL;
175 		}
176 		if (doclose)
177 			vendgrent();
178 	}
179 	free(line);
180 
181 	return (gr);
182 }
183 
184 struct group *
185 vgetgrent(void)
186 {
187   return vnextgrent(NULL, -1, 0);
188 }
189 
190 
191 struct group *
192 vgetgrgid(gid_t gid)
193 {
194   return vnextgrent(NULL, gid, 1);
195 }
196 
197 struct group *
198 vgetgrnam(const char * nam)
199 {
200   return vnextgrent(nam, -1, 1);
201 }
202 
203