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