1/* 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (c) 1998-1999 by Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18#if defined(LIBC_SCCS) && !defined(lint) 19static const char rcsid[] = "$Id: getpwent_r.c,v 1.8 2005/04/27 04:56:26 sra Exp $"; 20#endif /* LIBC_SCCS and not lint */ 21 22#include <port_before.h> 23#if !defined(_REENTRANT) || !defined(DO_PTHREADS) || !defined(WANT_IRS_PW) 24 static int getpwent_r_not_required = 0; 25#else 26#include <errno.h> 27#include <string.h> 28#include <stdio.h> 29#include <sys/types.h> 30#if (defined(POSIX_GETPWNAM_R) || defined(POSIX_GETPWUID_R)) 31#if defined(_POSIX_PTHREAD_SEMANTICS) 32 /* turn off solaris remapping in <grp.h> */ 33#undef _POSIX_PTHREAD_SEMANTICS 34#include <pwd.h> 35#define _POSIX_PTHREAD_SEMANTICS 1 36#else 37#define _UNIX95 1 38#include <pwd.h> 39#endif 40#else 41#include <pwd.h> 42#endif 43#include <port_after.h> 44 45#ifdef PASS_R_RETURN 46 47static int 48copy_passwd(struct passwd *, struct passwd *, char *buf, int buflen); 49 50/* POSIX 1003.1c */ 51#ifdef POSIX_GETPWNAM_R 52int 53__posix_getpwnam_r(const char *login, struct passwd *pwptr, 54 char *buf, size_t buflen, struct passwd **result) { 55#else 56int 57getpwnam_r(const char *login, struct passwd *pwptr, 58 char *buf, size_t buflen, struct passwd **result) { 59#endif 60 struct passwd *pw = getpwnam(login); 61 int res; 62 63 if (pw == NULL) { 64 *result = NULL; 65 return (0); 66 } 67 68 res = copy_passwd(pw, pwptr, buf, buflen); 69 *result = res ? NULL : pwptr; 70 return (res); 71} 72 73#ifdef POSIX_GETPWNAM_R 74struct passwd * 75getpwnam_r(const char *login, struct passwd *pwptr, char *buf, int buflen) { 76 struct passwd *pw = getpwnam(login); 77 int res; 78 79 if (pw == NULL) 80 return (NULL); 81 82 res = copy_passwd(pw, pwptr, buf, buflen); 83 return (res ? NULL : pwptr); 84} 85#endif 86 87/* POSIX 1003.1c */ 88#ifdef POSIX_GETPWUID_R 89int 90__posix_getpwuid_r(uid_t uid, struct passwd *pwptr, 91 char *buf, int buflen, struct passwd **result) { 92#else 93int 94getpwuid_r(uid_t uid, struct passwd *pwptr, 95 char *buf, size_t buflen, struct passwd **result) { 96#endif 97 struct passwd *pw = getpwuid(uid); 98 int res; 99 100 if (pw == NULL) { 101 *result = NULL; 102 return (0); 103 } 104 105 res = copy_passwd(pw, pwptr, buf, buflen); 106 *result = res ? NULL : pwptr; 107 return (res); 108} 109 110#ifdef POSIX_GETPWUID_R 111struct passwd * 112getpwuid_r(uid_t uid, struct passwd *pwptr, char *buf, int buflen) { 113 struct passwd *pw = getpwuid(uid); 114 int res; 115 116 if (pw == NULL) 117 return (NULL); 118 119 res = copy_passwd(pw, pwptr, buf, buflen); 120 return (res ? NULL : pwptr); 121} 122#endif 123 124/*% 125 * These assume a single context is in operation per thread. 126 * If this is not the case we will need to call irs directly 127 * rather than through the base functions. 128 */ 129 130PASS_R_RETURN 131getpwent_r(struct passwd *pwptr, PASS_R_ARGS) { 132 struct passwd *pw = getpwent(); 133 int res = 0; 134 135 if (pw == NULL) 136 return (PASS_R_BAD); 137 138 res = copy_passwd(pw, pwptr, buf, buflen); 139 return (res ? PASS_R_BAD : PASS_R_OK); 140} 141 142PASS_R_SET_RETURN 143#ifdef PASS_R_ENT_ARGS 144setpassent_r(int stayopen, PASS_R_ENT_ARGS) 145#else 146setpassent_r(int stayopen) 147#endif 148{ 149 150 setpassent(stayopen); 151#ifdef PASS_R_SET_RESULT 152 return (PASS_R_SET_RESULT); 153#endif 154} 155 156PASS_R_SET_RETURN 157#ifdef PASS_R_ENT_ARGS 158setpwent_r(PASS_R_ENT_ARGS) 159#else 160setpwent_r(void) 161#endif 162{ 163 164 setpwent(); 165#ifdef PASS_R_SET_RESULT 166 return (PASS_R_SET_RESULT); 167#endif 168} 169 170PASS_R_END_RETURN 171#ifdef PASS_R_ENT_ARGS 172endpwent_r(PASS_R_ENT_ARGS) 173#else 174endpwent_r(void) 175#endif 176{ 177 178 endpwent(); 179 PASS_R_END_RESULT(PASS_R_OK); 180} 181 182 183#ifdef HAS_FGETPWENT 184PASS_R_RETURN 185fgetpwent_r(FILE *f, struct passwd *pwptr, PASS_R_COPY_ARGS) { 186 struct passwd *pw = fgetpwent(f); 187 int res = 0; 188 189 if (pw == NULL) 190 return (PASS_R_BAD); 191 192 res = copy_passwd(pw, pwptr, PASS_R_COPY); 193 return (res ? PASS_R_BAD : PASS_R_OK ); 194} 195#endif 196 197/* Private */ 198 199static int 200copy_passwd(struct passwd *pw, struct passwd *pwptr, char *buf, int buflen) { 201 char *cp; 202 int n; 203 int len; 204 205 /* Find out the amount of space required to store the answer. */ 206 len = strlen(pw->pw_name) + 1; 207 len += strlen(pw->pw_passwd) + 1; 208#ifdef HAVE_PW_CLASS 209 len += strlen(pw->pw_class) + 1; 210#endif 211 len += strlen(pw->pw_gecos) + 1; 212 len += strlen(pw->pw_dir) + 1; 213 len += strlen(pw->pw_shell) + 1; 214 215 if (len > buflen) { 216 errno = ERANGE; 217 return (ERANGE); 218 } 219 220 /* copy fixed atomic values*/ 221 pwptr->pw_uid = pw->pw_uid; 222 pwptr->pw_gid = pw->pw_gid; 223#ifdef HAVE_PW_CHANGE 224 pwptr->pw_change = pw->pw_change; 225#endif 226#ifdef HAVE_PW_EXPIRE 227 pwptr->pw_expire = pw->pw_expire; 228#endif 229 230 cp = buf; 231 232 /* copy official name */ 233 n = strlen(pw->pw_name) + 1; 234 strcpy(cp, pw->pw_name); 235 pwptr->pw_name = cp; 236 cp += n; 237 238 /* copy password */ 239 n = strlen(pw->pw_passwd) + 1; 240 strcpy(cp, pw->pw_passwd); 241 pwptr->pw_passwd = cp; 242 cp += n; 243 244#ifdef HAVE_PW_CLASS 245 /* copy class */ 246 n = strlen(pw->pw_class) + 1; 247 strcpy(cp, pw->pw_class); 248 pwptr->pw_class = cp; 249 cp += n; 250#endif 251 252 /* copy gecos */ 253 n = strlen(pw->pw_gecos) + 1; 254 strcpy(cp, pw->pw_gecos); 255 pwptr->pw_gecos = cp; 256 cp += n; 257 258 /* copy directory */ 259 n = strlen(pw->pw_dir) + 1; 260 strcpy(cp, pw->pw_dir); 261 pwptr->pw_dir = cp; 262 cp += n; 263 264 /* copy login shell */ 265 n = strlen(pw->pw_shell) + 1; 266 strcpy(cp, pw->pw_shell); 267 pwptr->pw_shell = cp; 268 cp += n; 269 270 return (0); 271} 272#else /* PASS_R_RETURN */ 273 static int getpwent_r_unknown_system = 0; 274#endif /* PASS_R_RETURN */ 275#endif /* !def(_REENTRANT) || !def(DO_PTHREADS) || !def(WANT_IRS_PW) */ 276/*! \file */ 277