1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 #include <pwd.h> 31 #include <grp.h> 32 #include <libutil.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <stdlib.h> 36 #include <err.h> 37 #include <unistd.h> 38 39 #include "pwupd.h" 40 41 static FILE * pwd_fp = NULL; 42 static int pwd_scanflag; 43 static const char *pwd_filename; 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) { 73 if (geteuid() == 0) { 74 pwd_filename = _MASTERPASSWD; 75 pwd_scanflag = PWSCAN_MASTER; 76 } else { 77 pwd_filename = _PASSWD; 78 pwd_scanflag = 0; 79 } 80 pwd_fp = fopen(getpwpath(pwd_filename), "r"); 81 } 82 83 if (pwd_fp != NULL) { 84 while ((linelen = getline(&line, &linecap, pwd_fp)) > 0) { 85 /* Skip comments and empty lines */ 86 if (*line == '\n' || *line == '#') 87 continue; 88 /* trim latest \n */ 89 if (line[linelen - 1 ] == '\n') 90 line[linelen - 1] = '\0'; 91 pw = pw_scan(line, pwd_scanflag); 92 if (pw == NULL) 93 errx(EXIT_FAILURE, "Invalid user entry in '%s':" 94 " '%s'", getpwpath(pwd_filename), line); 95 if (uid != (uid_t)-1) { 96 if (uid == pw->pw_uid) 97 break; 98 } else if (nam != NULL) { 99 if (strcmp(nam, pw->pw_name) == 0) 100 break; 101 } else 102 break; 103 free(pw); 104 pw = NULL; 105 } 106 if (doclose) 107 vendpwent(); 108 } 109 free(line); 110 111 return (pw); 112 } 113 114 struct passwd * 115 vgetpwent(void) 116 { 117 return vnextpwent(NULL, -1, 0); 118 } 119 120 struct passwd * 121 vgetpwuid(uid_t uid) 122 { 123 return vnextpwent(NULL, uid, 1); 124 } 125 126 struct passwd * 127 vgetpwnam(const char * nam) 128 { 129 return vnextpwent(nam, -1, 1); 130 } 131 132 133 static FILE * grp_fp = NULL; 134 135 void 136 vendgrent(void) 137 { 138 if (grp_fp != NULL) { 139 fclose(grp_fp); 140 grp_fp = NULL; 141 } 142 } 143 144 void 145 vsetgrent(void) 146 { 147 vendgrent(); 148 } 149 150 static struct group * 151 vnextgrent(char const *nam, gid_t gid, int doclose) 152 { 153 struct group *gr; 154 char *line; 155 size_t linecap; 156 ssize_t linelen; 157 158 gr = NULL; 159 line = NULL; 160 linecap = 0; 161 162 if (grp_fp != NULL || (grp_fp = fopen(getgrpath(_GROUP), "r")) != NULL) { 163 while ((linelen = getline(&line, &linecap, grp_fp)) > 0) { 164 /* Skip comments and empty lines */ 165 if (*line == '\n' || *line == '#') 166 continue; 167 /* trim latest \n */ 168 if (line[linelen - 1 ] == '\n') 169 line[linelen - 1] = '\0'; 170 gr = gr_scan(line); 171 if (gr == NULL) 172 errx(EXIT_FAILURE, "Invalid group entry in '%s':" 173 " '%s'", getgrpath(_GROUP), line); 174 if (gid != (gid_t)-1) { 175 if (gid == gr->gr_gid) 176 break; 177 } else if (nam != NULL) { 178 if (strcmp(nam, gr->gr_name) == 0) 179 break; 180 } else 181 break; 182 free(gr); 183 gr = NULL; 184 } 185 if (doclose) 186 vendgrent(); 187 } 188 free(line); 189 190 return (gr); 191 } 192 193 struct group * 194 vgetgrent(void) 195 { 196 return vnextgrent(NULL, -1, 0); 197 } 198 199 200 struct group * 201 vgetgrgid(gid_t gid) 202 { 203 return vnextgrent(NULL, gid, 1); 204 } 205 206 struct group * 207 vgetgrnam(const char * nam) 208 { 209 return vnextgrent(nam, -1, 1); 210 } 211 212