]> git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_vpw.c
4517a743c73ea46592746556991ee539878ede14
[pw-darwin.git] / pw / pw_vpw.c
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 #include <unistd.h>
43
44 #include "pwupd.h"
45
46 static FILE * pwd_fp = NULL;
47 static int pwd_scanflag;
48 static const char *pwd_filename;
49
50 void
51 vendpwent(void)
52 {
53 if (pwd_fp != NULL) {
54 fclose(pwd_fp);
55 pwd_fp = NULL;
56 }
57 }
58
59 void
60 vsetpwent(void)
61 {
62 vendpwent();
63 }
64
65 static struct passwd *
66 vnextpwent(char const *nam, uid_t uid, int doclose)
67 {
68 struct passwd *pw;
69 char *line;
70 size_t linecap;
71 ssize_t linelen;
72
73 pw = NULL;
74 line = NULL;
75 linecap = 0;
76
77 if (pwd_fp == NULL) {
78 if (geteuid() == 0) {
79 pwd_filename = _MASTERPASSWD;
80 pwd_scanflag = PWSCAN_MASTER;
81 } else {
82 pwd_filename = _PASSWD;
83 pwd_scanflag = 0;
84 }
85 pwd_fp = fopen(getpwpath(pwd_filename), "r");
86 }
87
88 if (pwd_fp != NULL) {
89 while ((linelen = getline(&line, &linecap, pwd_fp)) > 0) {
90 /* Skip comments and empty lines */
91 if (*line == '\n' || *line == '#')
92 continue;
93 /* trim latest \n */
94 if (line[linelen - 1 ] == '\n')
95 line[linelen - 1] = '\0';
96 pw = pw_scan(line, pwd_scanflag);
97 if (pw == NULL)
98 errx(EXIT_FAILURE, "Invalid user entry in '%s':"
99 " '%s'", getpwpath(pwd_filename), line);
100 if (uid != (uid_t)-1) {
101 if (uid == pw->pw_uid)
102 break;
103 } else if (nam != NULL) {
104 if (strcmp(nam, pw->pw_name) == 0)
105 break;
106 } else
107 break;
108 free(pw);
109 pw = NULL;
110 }
111 if (doclose)
112 vendpwent();
113 }
114 free(line);
115
116 /*
117 * If we read the non-master passwd, some fields may not have been
118 * populated. Clean them up so that the output looks the same as that
119 * generated using getpwnam() which also inits them to these values.
120 */
121 if (!(pw->pw_fields & _PWF_CLASS))
122 pw->pw_class = "";
123 if (!(pw->pw_fields & _PWF_CHANGE))
124 pw->pw_change = 0;
125 if (!(pw->pw_fields & _PWF_EXPIRE))
126 pw->pw_expire = 0;
127
128 return (pw);
129 }
130
131 struct passwd *
132 vgetpwent(void)
133 {
134 return vnextpwent(NULL, -1, 0);
135 }
136
137 struct passwd *
138 vgetpwuid(uid_t uid)
139 {
140 return vnextpwent(NULL, uid, 1);
141 }
142
143 struct passwd *
144 vgetpwnam(const char * nam)
145 {
146 return vnextpwent(nam, -1, 1);
147 }
148
149
150 static FILE * grp_fp = NULL;
151
152 void
153 vendgrent(void)
154 {
155 if (grp_fp != NULL) {
156 fclose(grp_fp);
157 grp_fp = NULL;
158 }
159 }
160
161 void
162 vsetgrent(void)
163 {
164 vendgrent();
165 }
166
167 static struct group *
168 vnextgrent(char const *nam, gid_t gid, int doclose)
169 {
170 struct group *gr;
171 char *line;
172 size_t linecap;
173 ssize_t linelen;
174
175 gr = NULL;
176 line = NULL;
177 linecap = 0;
178
179 if (grp_fp != NULL || (grp_fp = fopen(getgrpath(_GROUP), "r")) != NULL) {
180 while ((linelen = getline(&line, &linecap, grp_fp)) > 0) {
181 /* Skip comments and empty lines */
182 if (*line == '\n' || *line == '#')
183 continue;
184 /* trim latest \n */
185 if (line[linelen - 1 ] == '\n')
186 line[linelen - 1] = '\0';
187 gr = gr_scan(line);
188 if (gr == NULL)
189 errx(EXIT_FAILURE, "Invalid group entry in '%s':"
190 " '%s'", getgrpath(_GROUP), line);
191 if (gid != (gid_t)-1) {
192 if (gid == gr->gr_gid)
193 break;
194 } else if (nam != NULL) {
195 if (strcmp(nam, gr->gr_name) == 0)
196 break;
197 } else
198 break;
199 free(gr);
200 gr = NULL;
201 }
202 if (doclose)
203 vendgrent();
204 }
205 free(line);
206
207 return (gr);
208 }
209
210 struct group *
211 vgetgrent(void)
212 {
213 return vnextgrent(NULL, -1, 0);
214 }
215
216
217 struct group *
218 vgetgrgid(gid_t gid)
219 {
220 return vnextgrent(NULL, gid, 1);
221 }
222
223 struct group *
224 vgetgrnam(const char * nam)
225 {
226 return vnextgrent(nam, -1, 1);
227 }
228