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 * $FreeBSD: src/usr.sbin/pw/pw_vpw.c,v 1.3 2000/01/15 00:20:21 davidn Exp $ 27 * $DragonFly: src/usr.sbin/pw/pw_vpw.c,v 1.4 2005/12/05 02:40:27 swildner Exp $ 28 */ 29 30 #include <stdio.h> 31 #include <string.h> 32 #include <stdlib.h> 33 #include <sys/param.h> 34 35 #include "pwupd.h" 36 37 static FILE * pwd_fp = NULL; 38 39 void 40 vendpwent(void) 41 { 42 if (pwd_fp != NULL) { 43 fclose(pwd_fp); 44 pwd_fp = NULL; 45 } 46 } 47 48 void 49 vsetpwent(void) 50 { 51 vendpwent(); 52 } 53 54 static struct passwd * 55 vnextpwent(char const * nam, uid_t uid, int doclose) 56 { 57 struct passwd * pw = NULL; 58 static char pwtmp[1024]; 59 60 strncpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof pwtmp); 61 pwtmp[sizeof pwtmp - 1] = '\0'; 62 63 if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) { 64 int done = 0; 65 66 static struct passwd pwd; 67 68 while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL) 69 { 70 int i, quickout = 0; 71 char * q; 72 char * p = strchr(pwtmp, '\n'); 73 74 if (p == NULL) { 75 while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL) 76 ; /* Skip long lines */ 77 continue; 78 } 79 80 /* skip comments & empty lines */ 81 if (*pwtmp =='\n' || *pwtmp == '#') 82 continue; 83 84 i = 0; 85 q = p = pwtmp; 86 bzero(&pwd, sizeof pwd); 87 while (!quickout && (p = strsep(&q, ":\n")) != NULL) { 88 switch (i++) 89 { 90 case 0: /* username */ 91 pwd.pw_name = p; 92 if (nam) { 93 if (strcmp(nam, p) == 0) 94 done = 1; 95 else 96 quickout = 1; 97 } 98 break; 99 case 1: /* password */ 100 pwd.pw_passwd = p; 101 break; 102 case 2: /* uid */ 103 pwd.pw_uid = atoi(p); 104 if (uid != (uid_t)-1) { 105 if (uid == pwd.pw_uid) 106 done = 1; 107 else 108 quickout = 1; 109 } 110 break; 111 case 3: /* gid */ 112 pwd.pw_gid = atoi(p); 113 break; 114 case 4: /* class */ 115 if (nam == NULL && uid == (uid_t)-1) 116 done = 1; 117 pwd.pw_class = p; 118 break; 119 case 5: /* change */ 120 pwd.pw_change = (time_t)atol(p); 121 break; 122 case 6: /* expire */ 123 pwd.pw_expire = (time_t)atol(p); 124 break; 125 case 7: /* gecos */ 126 pwd.pw_gecos = p; 127 break; 128 case 8: /* directory */ 129 pwd.pw_dir = p; 130 break; 131 case 9: /* shell */ 132 pwd.pw_shell = p; 133 break; 134 } 135 } 136 } 137 if (doclose) 138 vendpwent(); 139 if (done && pwd.pw_name) { 140 pw = &pwd; 141 142 #define CKNULL(s) s = s ? s : "" 143 CKNULL(pwd.pw_passwd); 144 CKNULL(pwd.pw_class); 145 CKNULL(pwd.pw_gecos); 146 CKNULL(pwd.pw_dir); 147 CKNULL(pwd.pw_shell); 148 } 149 } 150 return pw; 151 } 152 153 struct passwd * 154 vgetpwent(void) 155 { 156 return vnextpwent(NULL, -1, 0); 157 } 158 159 struct passwd * 160 vgetpwuid(uid_t uid) 161 { 162 return vnextpwent(NULL, uid, 1); 163 } 164 165 struct passwd * 166 vgetpwnam(const char * nam) 167 { 168 return vnextpwent(nam, -1, 1); 169 } 170 171 int 172 vpwdb(char *arg, ...) 173 { 174 arg=arg; 175 return 0; 176 } 177 178 179 180 static FILE * grp_fp = NULL; 181 182 void 183 vendgrent(void) 184 { 185 if (grp_fp != NULL) { 186 fclose(grp_fp); 187 grp_fp = NULL; 188 } 189 } 190 191 RET_SETGRENT 192 vsetgrent(void) 193 { 194 vendgrent(); 195 #if defined(__DragonFly__) 196 return 0; 197 #endif 198 } 199 200 static struct group * 201 vnextgrent(char const * nam, gid_t gid, int doclose) 202 { 203 struct group * gr = NULL; 204 205 static char * grtmp = NULL; 206 static int grlen = 0; 207 static char ** mems = NULL; 208 static int memlen = 0; 209 210 extendline(&grtmp, &grlen, MAXPATHLEN); 211 strncpy(grtmp, getgrpath(_GROUP), MAXPATHLEN); 212 grtmp[MAXPATHLEN - 1] = '\0'; 213 214 if (grp_fp != NULL || (grp_fp = fopen(grtmp, "r")) != NULL) { 215 int done = 0; 216 217 static struct group grp; 218 219 while (!done && fgets(grtmp, grlen, grp_fp) != NULL) 220 { 221 int i, quickout = 0; 222 int mno = 0; 223 char * q, * p; 224 char * sep = ":\n"; 225 226 if ((p = strchr(grtmp, '\n')) == NULL) { 227 int l; 228 extendline(&grtmp, &grlen, grlen + PWBUFSZ); 229 l = strlen(grtmp); 230 if (fgets(grtmp + l, grlen - l, grp_fp) == NULL) 231 break; /* No newline terminator on last line */ 232 } 233 /* Skip comments and empty lines */ 234 if (*grtmp == '\n' || *grtmp == '#') 235 continue; 236 i = 0; 237 q = p = grtmp; 238 bzero(&grp, sizeof grp); 239 extendarray(&mems, &memlen, 200); 240 while (!quickout && (p = strsep(&q, sep)) != NULL) { 241 switch (i++) 242 { 243 case 0: /* groupname */ 244 grp.gr_name = p; 245 if (nam) { 246 if (strcmp(nam, p) == 0) 247 done = 1; 248 else 249 quickout = 1; 250 } 251 break; 252 case 1: /* password */ 253 grp.gr_passwd = p; 254 break; 255 case 2: /* gid */ 256 grp.gr_gid = atoi(p); 257 if (gid != (gid_t)-1) { 258 if (gid == (gid_t)grp.gr_gid) 259 done = 1; 260 else 261 quickout = 1; 262 } else if (nam == NULL) 263 done = 1; 264 break; 265 case 3: 266 q = p; 267 sep = ",\n"; 268 break; 269 default: 270 if (*p) { 271 extendarray(&mems, &memlen, mno + 2); 272 mems[mno++] = p; 273 } 274 break; 275 } 276 } 277 grp.gr_mem = mems; 278 mems[mno] = NULL; 279 } 280 if (doclose) 281 vendgrent(); 282 if (done && grp.gr_name) { 283 gr = &grp; 284 285 CKNULL(grp.gr_passwd); 286 } 287 } 288 return gr; 289 } 290 291 struct group * 292 vgetgrent(void) 293 { 294 return vnextgrent(NULL, -1, 0); 295 } 296 297 298 struct group * 299 vgetgrgid(gid_t gid) 300 { 301 return vnextgrent(NULL, gid, 1); 302 } 303 304 struct group * 305 vgetgrnam(const char * nam) 306 { 307 return vnextgrent(nam, -1, 1); 308 } 309 310 int 311 vgrdb(char *arg, ...) 312 { 313 arg=arg; 314 return 0; 315 } 316 317