"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 2 | /* Copyright (C) 2003 Manuel Novoa III |
| 3 | * |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 4 | * Licensed under GPL v2, or later. See file LICENSE in this tarball. |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* Nov 6, 2003 Initial version. |
| 8 | * |
| 9 | * NOTE: This implementation is quite strict about requiring all |
| 10 | * field seperators. It also does not allow leading whitespace |
| 11 | * except when processing the numeric fields. glibc is more |
| 12 | * lenient. See the various glibc difference comments below. |
| 13 | * |
| 14 | * TODO: |
Rob Landley | 06ec8cf | 2006-03-03 19:02:50 +0000 | [diff] [blame] | 15 | * Move to dynamic allocation of (currently statically allocated) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 16 | * buffers; especially for the group-related functions since |
| 17 | * large group member lists will cause error returns. |
| 18 | * |
| 19 | */ |
| 20 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 22 | #include <features.h> |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 23 | #include <assert.h> |
Bernhard Reutner-Fischer | fa939aa | 2006-04-05 16:21:37 +0000 | [diff] [blame] | 24 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 25 | #ifndef _PATH_SHADOW |
| 26 | #define _PATH_SHADOW "/etc/shadow" |
| 27 | #endif |
| 28 | #ifndef _PATH_PASSWD |
| 29 | #define _PATH_PASSWD "/etc/passwd" |
| 30 | #endif |
| 31 | #ifndef _PATH_GROUP |
| 32 | #define _PATH_GROUP "/etc/group" |
| 33 | #endif |
| 34 | |
| 35 | /**********************************************************************/ |
Rob Landley | 06ec8cf | 2006-03-03 19:02:50 +0000 | [diff] [blame] | 36 | /* Sizes for statically allocated buffers. */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 37 | |
| 38 | /* If you change these values, also change _SC_GETPW_R_SIZE_MAX and |
| 39 | * _SC_GETGR_R_SIZE_MAX in libc/unistd/sysconf.c to match */ |
| 40 | #define PWD_BUFFER_SIZE 256 |
| 41 | #define GRP_BUFFER_SIZE 256 |
| 42 | |
| 43 | /**********************************************************************/ |
| 44 | /* Prototypes for internal functions. */ |
| 45 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 46 | static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data, |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 47 | char *__restrict line_buff, size_t buflen, FILE *f); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 49 | static int bb__parsepwent(void *pw, char *line); |
| 50 | static int bb__parsegrent(void *gr, char *line); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 51 | #if ENABLE_USE_BB_SHADOW |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 52 | static int bb__parsespent(void *sp, char *line); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 55 | /**********************************************************************/ |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 56 | /* We avoid having big global data. */ |
| 57 | |
| 58 | struct statics { |
| 59 | /* Smaller things first */ |
| 60 | struct passwd getpwuid_resultbuf; |
| 61 | struct group getgrgid_resultbuf; |
| 62 | struct passwd getpwnam_resultbuf; |
| 63 | struct group getgrnam_resultbuf; |
| 64 | |
| 65 | char getpwuid_buffer[PWD_BUFFER_SIZE]; |
| 66 | char getgrgid_buffer[GRP_BUFFER_SIZE]; |
| 67 | char getpwnam_buffer[PWD_BUFFER_SIZE]; |
| 68 | char getgrnam_buffer[GRP_BUFFER_SIZE]; |
| 69 | #if 0 |
| 70 | struct passwd fgetpwent_resultbuf; |
| 71 | struct group fgetgrent_resultbuf; |
| 72 | struct spwd fgetspent_resultbuf; |
| 73 | char fgetpwent_buffer[PWD_BUFFER_SIZE]; |
| 74 | char fgetgrent_buffer[GRP_BUFFER_SIZE]; |
| 75 | char fgetspent_buffer[PWD_BUFFER_SIZE]; |
| 76 | #endif |
| 77 | #if 0 //ENABLE_USE_BB_SHADOW |
| 78 | struct spwd getspuid_resultbuf; |
| 79 | struct spwd getspnam_resultbuf; |
| 80 | char getspuid_buffer[PWD_BUFFER_SIZE]; |
| 81 | char getspnam_buffer[PWD_BUFFER_SIZE]; |
| 82 | #endif |
| 83 | // Not converted - too small to bother |
| 84 | //pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; |
| 85 | //FILE *pwf /*= NULL*/; |
| 86 | //FILE *grf /*= NULL*/; |
| 87 | //FILE *spf /*= NULL*/; |
| 88 | #if 0 |
| 89 | struct passwd getpwent_pwd; |
| 90 | struct group getgrent_gr; |
| 91 | char getpwent_line_buff[PWD_BUFFER_SIZE]; |
| 92 | char getgrent_line_buff[GRP_BUFFER_SIZE]; |
| 93 | #endif |
| 94 | #if 0 //ENABLE_USE_BB_SHADOW |
| 95 | struct spwd getspent_spwd; |
| 96 | struct spwd sgetspent_spwd; |
| 97 | char getspent_line_buff[PWD_BUFFER_SIZE]; |
| 98 | char sgetspent_line_buff[PWD_BUFFER_SIZE]; |
| 99 | #endif |
| 100 | }; |
| 101 | |
| 102 | static struct statics *ptr_to_statics; |
| 103 | |
| 104 | static struct statics *get_S(void) |
| 105 | { |
| 106 | if (!ptr_to_statics) |
| 107 | ptr_to_statics = xzalloc(sizeof(*ptr_to_statics)); |
| 108 | return ptr_to_statics; |
| 109 | } |
| 110 | |
| 111 | /* Always use in this order, get_S() must be called first */ |
| 112 | #define RESULTBUF(name) &((S = get_S())->name##_resultbuf) |
| 113 | #define BUFFER(name) (S->name##_buffer) |
| 114 | |
| 115 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 116 | /* For the various fget??ent_r funcs, return |
| 117 | * |
| 118 | * 0: success |
| 119 | * ENOENT: end-of-file encountered |
| 120 | * ERANGE: buflen too small |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 121 | * other error values possible. See bb__pgsreader. |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 122 | * |
| 123 | * Also, *result == resultbuf on success and NULL on failure. |
| 124 | * |
| 125 | * NOTE: glibc difference - For the ENOENT case, glibc also sets errno. |
| 126 | * We do not, as it really isn't an error if we reach the end-of-file. |
| 127 | * Doing so is analogous to having fgetc() set errno on EOF. |
| 128 | */ |
| 129 | /**********************************************************************/ |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 130 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 131 | int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf, |
| 132 | char *__restrict buffer, size_t buflen, |
| 133 | struct passwd **__restrict result) |
| 134 | { |
| 135 | int rv; |
| 136 | |
| 137 | *result = NULL; |
| 138 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 139 | rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 140 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 141 | *result = resultbuf; |
| 142 | } |
| 143 | |
| 144 | return rv; |
| 145 | } |
| 146 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 147 | int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf, |
| 148 | char *__restrict buffer, size_t buflen, |
| 149 | struct group **__restrict result) |
| 150 | { |
| 151 | int rv; |
| 152 | |
| 153 | *result = NULL; |
| 154 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 155 | rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 156 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 157 | *result = resultbuf; |
| 158 | } |
| 159 | |
| 160 | return rv; |
| 161 | } |
| 162 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 163 | #if ENABLE_USE_BB_SHADOW |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 164 | int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf, |
| 165 | char *__restrict buffer, size_t buflen, |
| 166 | struct spwd **__restrict result) |
| 167 | { |
| 168 | int rv; |
| 169 | |
| 170 | *result = NULL; |
| 171 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 172 | rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 173 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 174 | *result = resultbuf; |
| 175 | } |
| 176 | |
| 177 | return rv; |
| 178 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 179 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 180 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 181 | /**********************************************************************/ |
| 182 | /* For the various fget??ent funcs, return NULL on failure and a |
Rob Landley | 06ec8cf | 2006-03-03 19:02:50 +0000 | [diff] [blame] | 183 | * pointer to the appropriate struct (statically allocated) on success. |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 184 | * TODO: audit & stop using these in bbox, they pull in static buffers */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 185 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 187 | #if 0 |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 188 | struct passwd *fgetpwent(FILE *stream) |
| 189 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 190 | struct statics *S; |
| 191 | struct passwd *resultbuf = RESULTBUF(fgetpwent); |
| 192 | char *buffer = BUFFER(fgetpwent); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 193 | struct passwd *result; |
| 194 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 195 | fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetpwent)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 196 | return result; |
| 197 | } |
| 198 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 199 | struct group *fgetgrent(FILE *stream) |
| 200 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 201 | struct statics *S; |
| 202 | struct group *resultbuf = RESULTBUF(fgetgrent); |
| 203 | char *buffer = BUFFER(fgetgrent); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 204 | struct group *result; |
| 205 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 206 | fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetgrent)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 207 | return result; |
| 208 | } |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 209 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 210 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 211 | #if ENABLE_USE_BB_SHADOW |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 212 | #if 0 |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 213 | struct spwd *fgetspent(FILE *stream) |
| 214 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 215 | struct statics *S; |
| 216 | struct spwd *resultbuf = RESULTBUF(fgetspent); |
| 217 | char *buffer = BUFFER(fgetspent); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 218 | struct spwd *result; |
| 219 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 220 | fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(fgetspent)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 221 | return result; |
| 222 | } |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 223 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 224 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 225 | int sgetspent_r(const char *string, struct spwd *result_buf, |
| 226 | char *buffer, size_t buflen, struct spwd **result) |
| 227 | { |
| 228 | int rv = ERANGE; |
| 229 | |
| 230 | *result = NULL; |
| 231 | |
| 232 | if (buflen < PWD_BUFFER_SIZE) { |
| 233 | DO_ERANGE: |
| 234 | errno=rv; |
| 235 | goto DONE; |
| 236 | } |
| 237 | |
| 238 | if (string != buffer) { |
| 239 | if (strlen(string) >= buflen) { |
| 240 | goto DO_ERANGE; |
| 241 | } |
| 242 | strcpy(buffer, string); |
| 243 | } |
| 244 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 245 | rv = bb__parsespent(result_buf, buffer); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 246 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 247 | *result = result_buf; |
| 248 | } |
| 249 | |
| 250 | DONE: |
| 251 | return rv; |
| 252 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 253 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 254 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 255 | /**********************************************************************/ |
| 256 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 257 | #define GETXXKEY_R_FUNC getpwnam_r |
| 258 | #define GETXXKEY_R_PARSER bb__parsepwent |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 259 | #define GETXXKEY_R_ENTTYPE struct passwd |
| 260 | #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key)) |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 261 | #define GETXXKEY_R_KEYTYPE const char *__restrict |
| 262 | #define GETXXKEY_R_PATHNAME _PATH_PASSWD |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 263 | #include "pwd_grp_internal.c" |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 265 | #define GETXXKEY_R_FUNC getgrnam_r |
| 266 | #define GETXXKEY_R_PARSER bb__parsegrent |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 267 | #define GETXXKEY_R_ENTTYPE struct group |
| 268 | #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key)) |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 269 | #define GETXXKEY_R_KEYTYPE const char *__restrict |
| 270 | #define GETXXKEY_R_PATHNAME _PATH_GROUP |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 271 | #include "pwd_grp_internal.c" |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 272 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 273 | #if ENABLE_USE_BB_SHADOW |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 274 | #define GETXXKEY_R_FUNC getspnam_r |
| 275 | #define GETXXKEY_R_PARSER bb__parsespent |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 276 | #define GETXXKEY_R_ENTTYPE struct spwd |
| 277 | #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key)) |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 278 | #define GETXXKEY_R_KEYTYPE const char *__restrict |
| 279 | #define GETXXKEY_R_PATHNAME _PATH_SHADOW |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 280 | #include "pwd_grp_internal.c" |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 281 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 282 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 283 | #define GETXXKEY_R_FUNC getpwuid_r |
| 284 | #define GETXXKEY_R_PARSER bb__parsepwent |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 285 | #define GETXXKEY_R_ENTTYPE struct passwd |
| 286 | #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key) |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 287 | #define GETXXKEY_R_KEYTYPE uid_t |
| 288 | #define GETXXKEY_R_PATHNAME _PATH_PASSWD |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 289 | #include "pwd_grp_internal.c" |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 290 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 291 | #define GETXXKEY_R_FUNC getgrgid_r |
| 292 | #define GETXXKEY_R_PARSER bb__parsegrent |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 293 | #define GETXXKEY_R_ENTTYPE struct group |
| 294 | #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key) |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 295 | #define GETXXKEY_R_KEYTYPE gid_t |
| 296 | #define GETXXKEY_R_PATHNAME _PATH_GROUP |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 297 | #include "pwd_grp_internal.c" |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 298 | |
| 299 | /**********************************************************************/ |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 300 | /* TODO: audit & stop using these in bbox, they pull in static buffers */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 301 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 302 | /* This one has many users */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 303 | struct passwd *getpwuid(uid_t uid) |
| 304 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 305 | struct statics *S; |
| 306 | struct passwd *resultbuf = RESULTBUF(getpwuid); |
| 307 | char *buffer = BUFFER(getpwuid); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 308 | struct passwd *result; |
| 309 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 310 | getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpwuid)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 311 | return result; |
| 312 | } |
| 313 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 314 | /* This one has many users */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 315 | struct group *getgrgid(gid_t gid) |
| 316 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 317 | struct statics *S; |
| 318 | struct group *resultbuf = RESULTBUF(getgrgid); |
| 319 | char *buffer = BUFFER(getgrgid); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 320 | struct group *result; |
| 321 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 322 | getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgrgid)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 323 | return result; |
| 324 | } |
| 325 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 326 | #if 0 //ENABLE_USE_BB_SHADOW |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 327 | /* This function is non-standard and is currently not built. It seems |
| 328 | * to have been created as a reentrant version of the non-standard |
| 329 | * functions getspuid. Why getspuid was added, I do not know. */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 330 | int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf, |
| 331 | char *__restrict buffer, size_t buflen, |
| 332 | struct spwd **__restrict result) |
| 333 | { |
| 334 | int rv; |
| 335 | struct passwd *pp; |
| 336 | struct passwd password; |
| 337 | char pwd_buff[PWD_BUFFER_SIZE]; |
| 338 | |
| 339 | *result = NULL; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 340 | rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp); |
| 341 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 342 | rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result); |
| 343 | } |
| 344 | |
| 345 | return rv; |
| 346 | } |
| 347 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 348 | /* This function is non-standard and is currently not built. |
| 349 | * Why it was added, I do not know. */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 350 | struct spwd *getspuid(uid_t uid) |
| 351 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 352 | struct statics *S; |
| 353 | struct spwd *resultbuf = RESULTBUF(getspuid); |
| 354 | char *buffer = BUFFER(getspuid); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 355 | struct spwd *result; |
| 356 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 357 | getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getspuid)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 358 | return result; |
| 359 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 360 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 361 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 362 | /* This one has many users */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 363 | struct passwd *getpwnam(const char *name) |
| 364 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 365 | struct statics *S; |
| 366 | struct passwd *resultbuf = RESULTBUF(getpwnam); |
| 367 | char *buffer = BUFFER(getpwnam); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 368 | struct passwd *result; |
| 369 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 370 | getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpwnam)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 371 | return result; |
| 372 | } |
| 373 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 374 | /* This one has many users */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 375 | struct group *getgrnam(const char *name) |
| 376 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 377 | struct statics *S; |
| 378 | struct group *resultbuf = RESULTBUF(getgrnam); |
| 379 | char *buffer = BUFFER(getgrnam); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 380 | struct group *result; |
| 381 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 382 | getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgrnam)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 383 | return result; |
| 384 | } |
| 385 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 386 | #if 0 //ENABLE_USE_BB_SHADOW |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 387 | struct spwd *getspnam(const char *name) |
| 388 | { |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 389 | struct statics *S; |
| 390 | struct spwd *resultbuf = RESULTBUF(getspnam); |
| 391 | char *buffer = BUFFER(getspnam); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 392 | struct spwd *result; |
| 393 | |
Denis Vlasenko | 2c91efb | 2007-06-18 10:08:27 +0000 | [diff] [blame] | 394 | getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getspnam)), &result); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 395 | return result; |
| 396 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 397 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 398 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 399 | /* This one doesn't use static buffers */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 400 | int getpw(uid_t uid, char *buf) |
| 401 | { |
| 402 | struct passwd resultbuf; |
| 403 | struct passwd *result; |
| 404 | char buffer[PWD_BUFFER_SIZE]; |
| 405 | |
| 406 | if (!buf) { |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 407 | errno = EINVAL; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 408 | } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) { |
| 409 | if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n", |
| 410 | resultbuf.pw_name, resultbuf.pw_passwd, |
| 411 | (unsigned long)(resultbuf.pw_uid), |
| 412 | (unsigned long)(resultbuf.pw_gid), |
| 413 | resultbuf.pw_gecos, resultbuf.pw_dir, |
| 414 | resultbuf.pw_shell) >= 0 |
| 415 | ) { |
| 416 | return 0; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return -1; |
| 421 | } |
| 422 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 423 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 424 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 425 | /* FIXME: we don't have such CONFIG_xx - ?! */ |
| 426 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 427 | #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER |
| 428 | static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; |
| 429 | # define LOCK pthread_mutex_lock(&mylock) |
| 430 | # define UNLOCK pthread_mutex_unlock(&mylock); |
| 431 | #else |
| 432 | # define LOCK ((void) 0) |
| 433 | # define UNLOCK ((void) 0) |
| 434 | #endif |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 435 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 436 | static FILE *pwf /*= NULL*/; |
| 437 | void setpwent(void) |
| 438 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 439 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 440 | if (pwf) { |
| 441 | rewind(pwf); |
| 442 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 443 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void endpwent(void) |
| 447 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 448 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 449 | if (pwf) { |
| 450 | fclose(pwf); |
| 451 | pwf = NULL; |
| 452 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 453 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 457 | int getpwent_r(struct passwd *__restrict resultbuf, |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 458 | char *__restrict buffer, size_t buflen, |
| 459 | struct passwd **__restrict result) |
| 460 | { |
| 461 | int rv; |
| 462 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 463 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 464 | *result = NULL; /* In case of error... */ |
| 465 | |
| 466 | if (!pwf) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 467 | pwf = fopen(_PATH_PASSWD, "r"); |
| 468 | if (!pwf) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 469 | rv = errno; |
| 470 | goto ERR; |
| 471 | } |
| 472 | } |
| 473 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 474 | rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 475 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 476 | *result = resultbuf; |
| 477 | } |
| 478 | |
| 479 | ERR: |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 480 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 481 | return rv; |
| 482 | } |
| 483 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 484 | static FILE *grf /*= NULL*/; |
| 485 | void setgrent(void) |
| 486 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 487 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 488 | if (grf) { |
| 489 | rewind(grf); |
| 490 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 491 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void endgrent(void) |
| 495 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 496 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 497 | if (grf) { |
| 498 | fclose(grf); |
| 499 | grf = NULL; |
| 500 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 501 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | int getgrent_r(struct group *__restrict resultbuf, |
| 505 | char *__restrict buffer, size_t buflen, |
| 506 | struct group **__restrict result) |
| 507 | { |
| 508 | int rv; |
| 509 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 510 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 511 | *result = NULL; /* In case of error... */ |
| 512 | |
| 513 | if (!grf) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 514 | grf = fopen(_PATH_GROUP, "r"); |
| 515 | if (!grf) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 516 | rv = errno; |
| 517 | goto ERR; |
| 518 | } |
| 519 | } |
| 520 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 521 | rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 522 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 523 | *result = resultbuf; |
| 524 | } |
| 525 | |
| 526 | ERR: |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 527 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 528 | return rv; |
| 529 | } |
| 530 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 531 | #if ENABLE_USE_BB_SHADOW |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 532 | static FILE *spf /*= NULL*/; |
| 533 | void setspent(void) |
| 534 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 535 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 536 | if (spf) { |
| 537 | rewind(spf); |
| 538 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 539 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void endspent(void) |
| 543 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 544 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 545 | if (spf) { |
| 546 | fclose(spf); |
| 547 | spf = NULL; |
| 548 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 549 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 552 | int getspent_r(struct spwd *resultbuf, char *buffer, |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 553 | size_t buflen, struct spwd **result) |
| 554 | { |
| 555 | int rv; |
| 556 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 557 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 558 | *result = NULL; /* In case of error... */ |
| 559 | |
| 560 | if (!spf) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 561 | spf = fopen(_PATH_SHADOW, "r"); |
| 562 | if (!spf) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 563 | rv = errno; |
| 564 | goto ERR; |
| 565 | } |
| 566 | } |
| 567 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 568 | rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 569 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 570 | *result = resultbuf; |
| 571 | } |
| 572 | |
| 573 | ERR: |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 574 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 575 | return rv; |
| 576 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 577 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 578 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 579 | #if 0 |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 580 | struct passwd *getpwent(void) |
| 581 | { |
| 582 | static char line_buff[PWD_BUFFER_SIZE]; |
| 583 | static struct passwd pwd; |
| 584 | struct passwd *result; |
| 585 | |
| 586 | getpwent_r(&pwd, line_buff, sizeof(line_buff), &result); |
| 587 | return result; |
| 588 | } |
| 589 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 590 | struct group *getgrent(void) |
| 591 | { |
| 592 | static char line_buff[GRP_BUFFER_SIZE]; |
| 593 | static struct group gr; |
| 594 | struct group *result; |
| 595 | |
| 596 | getgrent_r(&gr, line_buff, sizeof(line_buff), &result); |
| 597 | return result; |
| 598 | } |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 599 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 600 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 601 | #if 0 //ENABLE_USE_BB_SHADOW |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 602 | struct spwd *getspent(void) |
| 603 | { |
| 604 | static char line_buff[PWD_BUFFER_SIZE]; |
| 605 | static struct spwd spwd; |
| 606 | struct spwd *result; |
| 607 | |
| 608 | getspent_r(&spwd, line_buff, sizeof(line_buff), &result); |
| 609 | return result; |
| 610 | } |
| 611 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 612 | struct spwd *sgetspent(const char *string) |
| 613 | { |
| 614 | static char line_buff[PWD_BUFFER_SIZE]; |
| 615 | static struct spwd spwd; |
| 616 | struct spwd *result; |
| 617 | |
| 618 | sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result); |
| 619 | return result; |
| 620 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 621 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 622 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 623 | int initgroups(const char *user, gid_t gid) |
| 624 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 625 | FILE *grfile; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 626 | gid_t *group_list; |
| 627 | int num_groups, rv; |
| 628 | char **m; |
| 629 | struct group group; |
| 630 | char buff[PWD_BUFFER_SIZE]; |
| 631 | |
| 632 | rv = -1; |
| 633 | |
| 634 | /* We alloc space for 8 gids at a time. */ |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 635 | group_list = (gid_t *) malloc(8*sizeof(gid_t *)); |
| 636 | if (group_list |
| 637 | && ((grfile = fopen(_PATH_GROUP, "r")) != NULL) |
| 638 | ) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 639 | *group_list = gid; |
| 640 | num_groups = 1; |
| 641 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 642 | while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 643 | assert(group.gr_mem); /* Must have at least a NULL terminator. */ |
| 644 | if (group.gr_gid != gid) { |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 645 | for (m = group.gr_mem; *m; m++) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 646 | if (!strcmp(*m, user)) { |
| 647 | if (!(num_groups & 7)) { |
| 648 | gid_t *tmp = (gid_t *) |
| 649 | realloc(group_list, |
| 650 | (num_groups+8) * sizeof(gid_t *)); |
| 651 | if (!tmp) { |
| 652 | rv = -1; |
| 653 | goto DO_CLOSE; |
| 654 | } |
| 655 | group_list = tmp; |
| 656 | } |
| 657 | group_list[num_groups++] = group.gr_gid; |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | rv = setgroups(num_groups, group_list); |
| 665 | DO_CLOSE: |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 666 | fclose(grfile); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* group_list will be NULL if initial malloc failed, which may trigger |
| 670 | * warnings from various malloc debuggers. */ |
| 671 | free(group_list); |
| 672 | return rv; |
| 673 | } |
| 674 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 675 | int putpwent(const struct passwd *__restrict p, FILE *__restrict f) |
| 676 | { |
| 677 | int rv = -1; |
| 678 | |
| 679 | if (!p || !f) { |
| 680 | errno=EINVAL; |
| 681 | } else { |
| 682 | /* No extra thread locking is needed above what fprintf does. */ |
| 683 | if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n", |
| 684 | p->pw_name, p->pw_passwd, |
| 685 | (unsigned long)(p->pw_uid), |
| 686 | (unsigned long)(p->pw_gid), |
| 687 | p->pw_gecos, p->pw_dir, p->pw_shell) >= 0 |
| 688 | ) { |
| 689 | rv = 0; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return rv; |
| 694 | } |
| 695 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 696 | int putgrent(const struct group *__restrict p, FILE *__restrict f) |
| 697 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 698 | static const char format[] ALIGN1 = ",%s"; |
| 699 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 700 | char **m; |
| 701 | const char *fmt; |
| 702 | int rv = -1; |
| 703 | |
| 704 | if (!p || !f) { /* Sigh... glibc checks. */ |
| 705 | errno=EINVAL; |
| 706 | } else { |
| 707 | if (fprintf(f, "%s:%s:%lu:", |
| 708 | p->gr_name, p->gr_passwd, |
| 709 | (unsigned long)(p->gr_gid)) >= 0 |
| 710 | ) { |
| 711 | |
| 712 | fmt = format + 1; |
| 713 | |
| 714 | assert(p->gr_mem); |
| 715 | m = p->gr_mem; |
| 716 | |
| 717 | do { |
| 718 | if (!*m) { |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 719 | if (fputc('\n', f) >= 0) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 720 | rv = 0; |
| 721 | } |
| 722 | break; |
| 723 | } |
| 724 | if (fprintf(f, fmt, *m) < 0) { |
| 725 | break; |
| 726 | } |
| 727 | ++m; |
| 728 | fmt = format; |
| 729 | } while (1); |
| 730 | |
| 731 | } |
| 732 | |
| 733 | } |
| 734 | |
| 735 | return rv; |
| 736 | } |
| 737 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 738 | #if ENABLE_USE_BB_SHADOW |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 739 | static const unsigned char _sp_off[] ALIGN1 = { |
| 740 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ |
| 741 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ |
| 742 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ |
| 743 | offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ |
| 744 | offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ |
| 745 | offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 746 | }; |
| 747 | |
| 748 | int putspent(const struct spwd *p, FILE *stream) |
| 749 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 750 | static const char ld_format[] ALIGN1 = "%ld:"; |
| 751 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 752 | const char *f; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 753 | long x; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 754 | int i; |
| 755 | int rv = -1; |
| 756 | |
| 757 | /* Unlike putpwent and putgrent, glibc does not check the args. */ |
| 758 | if (fprintf(stream, "%s:%s:", p->sp_namp, |
| 759 | (p->sp_pwdp ? p->sp_pwdp : "")) < 0 |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 760 | ) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 761 | goto DO_UNLOCK; |
| 762 | } |
| 763 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 764 | for (i = 0; i < sizeof(_sp_off); i++) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 765 | f = ld_format; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 766 | x = *(const long *)(((const char *) p) + _sp_off[i]); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 767 | if (x == -1) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 768 | f += 3; |
| 769 | } |
| 770 | if (fprintf(stream, f, x) < 0) { |
| 771 | goto DO_UNLOCK; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) { |
| 776 | goto DO_UNLOCK; |
| 777 | } |
| 778 | |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 779 | if (fputc('\n', stream) > 0) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 780 | rv = 0; |
| 781 | } |
| 782 | |
| 783 | DO_UNLOCK: |
| 784 | return rv; |
| 785 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 786 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 787 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 788 | /**********************************************************************/ |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 789 | /* Internal uClibc functions. */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 790 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 791 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 792 | static const unsigned char pw_off[] ALIGN1 = { |
| 793 | offsetof(struct passwd, pw_name), /* 0 */ |
| 794 | offsetof(struct passwd, pw_passwd), /* 1 */ |
| 795 | offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */ |
| 796 | offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ |
| 797 | offsetof(struct passwd, pw_gecos), /* 4 */ |
| 798 | offsetof(struct passwd, pw_dir), /* 5 */ |
| 799 | offsetof(struct passwd, pw_shell) /* 6 */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 800 | }; |
| 801 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 802 | static int bb__parsepwent(void *data, char *line) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 803 | { |
| 804 | char *endptr; |
| 805 | char *p; |
| 806 | int i; |
| 807 | |
| 808 | i = 0; |
| 809 | do { |
| 810 | p = ((char *) ((struct passwd *) data)) + pw_off[i]; |
| 811 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 812 | if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 813 | *((char **) p) = line; |
| 814 | if (i==6) { |
| 815 | return 0; |
| 816 | } |
| 817 | /* NOTE: glibc difference - glibc allows omission of |
| 818 | * ':' seperators after the gid field if all remaining |
| 819 | * entries are empty. We require all separators. */ |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 820 | line = strchr(line, ':'); |
| 821 | if (!line) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | } else { |
| 825 | unsigned long t = strtoul(line, &endptr, 10); |
| 826 | /* Make sure we had at least one digit, and that the |
| 827 | * failing char is the next field seperator ':'. See |
| 828 | * glibc difference note above. */ |
| 829 | /* TODO: Also check for leading whitespace? */ |
| 830 | if ((endptr == line) || (*endptr != ':')) { |
| 831 | break; |
| 832 | } |
| 833 | line = endptr; |
| 834 | if (i & 1) { /* i == 3 -- gid */ |
| 835 | *((gid_t *) p) = t; |
| 836 | } else { /* i == 2 -- uid */ |
| 837 | *((uid_t *) p) = t; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | *line++ = 0; |
| 842 | ++i; |
| 843 | } while (1); |
| 844 | |
| 845 | return -1; |
| 846 | } |
| 847 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 848 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 849 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 850 | static const unsigned char gr_off[] ALIGN1 = { |
| 851 | offsetof(struct group, gr_name), /* 0 */ |
| 852 | offsetof(struct group, gr_passwd), /* 1 */ |
| 853 | offsetof(struct group, gr_gid) /* 2 - not a char ptr */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 854 | }; |
| 855 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 856 | static int bb__parsegrent(void *data, char *line) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 857 | { |
| 858 | char *endptr; |
| 859 | char *p; |
| 860 | int i; |
| 861 | char **members; |
| 862 | char *end_of_buf; |
| 863 | |
| 864 | end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */ |
| 865 | i = 0; |
| 866 | do { |
| 867 | p = ((char *) ((struct group *) data)) + gr_off[i]; |
| 868 | |
| 869 | if (i < 2) { |
| 870 | *((char **) p) = line; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 871 | line = strchr(line, ':'); |
| 872 | if (!line) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | *line++ = 0; |
| 876 | ++i; |
| 877 | } else { |
| 878 | *((gid_t *) p) = strtoul(line, &endptr, 10); |
| 879 | |
| 880 | /* NOTE: glibc difference - glibc allows omission of the |
| 881 | * trailing colon when there is no member list. We treat |
| 882 | * this as an error. */ |
| 883 | |
| 884 | /* Make sure we had at least one digit, and that the |
| 885 | * failing char is the next field seperator ':'. See |
| 886 | * glibc difference note above. */ |
| 887 | if ((endptr == line) || (*endptr != ':')) { |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | i = 1; /* Count terminating NULL ptr. */ |
| 892 | p = endptr; |
| 893 | |
| 894 | if (p[1]) { /* We have a member list to process. */ |
| 895 | /* Overwrite the last ':' with a ',' before counting. |
| 896 | * This allows us to test for initial ',' and adds |
| 897 | * one ',' so that the ',' count equals the member |
| 898 | * count. */ |
| 899 | *p = ','; |
| 900 | do { |
| 901 | /* NOTE: glibc difference - glibc allows and trims leading |
| 902 | * (but not trailing) space. We treat this as an error. */ |
| 903 | /* NOTE: glibc difference - glibc allows consecutive and |
| 904 | * trailing commas, and ignores "empty string" users. We |
| 905 | * treat this as an error. */ |
| 906 | if (*p == ',') { |
| 907 | ++i; |
| 908 | *p = 0; /* nul-terminate each member string. */ |
| 909 | if (!*++p || (*p == ',') || isspace(*p)) { |
| 910 | goto ERR; |
| 911 | } |
| 912 | } |
| 913 | } while (*++p); |
| 914 | } |
| 915 | |
| 916 | /* Now align (p+1), rounding up. */ |
| 917 | /* Assumes sizeof(char **) is a power of 2. */ |
| 918 | members = (char **)( (((intptr_t) p) + sizeof(char **)) |
| 919 | & ~((intptr_t)(sizeof(char **) - 1)) ); |
| 920 | |
| 921 | if (((char *)(members + i)) > end_of_buf) { /* No space. */ |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | ((struct group *) data)->gr_mem = members; |
| 926 | |
| 927 | if (--i) { |
| 928 | p = endptr; /* Pointing to char prior to first member. */ |
| 929 | do { |
| 930 | *members++ = ++p; |
| 931 | if (!--i) break; |
| 932 | while (*++p) {} |
| 933 | } while (1); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 934 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 935 | *members = NULL; |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | } while (1); |
| 940 | |
| 941 | ERR: |
| 942 | return -1; |
| 943 | } |
| 944 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 945 | /**********************************************************************/ |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 946 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 947 | #if ENABLE_USE_BB_SHADOW |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame^] | 948 | static const unsigned char sp_off[] ALIGN1 = { |
| 949 | offsetof(struct spwd, sp_namp), /* 0 */ |
| 950 | offsetof(struct spwd, sp_pwdp), /* 1 */ |
| 951 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ |
| 952 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ |
| 953 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ |
| 954 | offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ |
| 955 | offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ |
| 956 | offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ |
| 957 | offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 958 | }; |
| 959 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 960 | static int bb__parsespent(void *data, char * line) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 961 | { |
| 962 | char *endptr; |
| 963 | char *p; |
| 964 | int i; |
| 965 | |
| 966 | i = 0; |
| 967 | do { |
| 968 | p = ((char *) ((struct spwd *) data)) + sp_off[i]; |
| 969 | if (i < 2) { |
| 970 | *((char **) p) = line; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 971 | line = strchr(line, ':'); |
| 972 | if (!line) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | } else { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 976 | *((long *) p) = (long) strtoul(line, &endptr, 10); |
| 977 | |
| 978 | if (endptr == line) { |
| 979 | *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL))); |
| 980 | } |
| 981 | |
| 982 | line = endptr; |
| 983 | |
| 984 | if (i == 8) { |
| 985 | if (!*endptr) { |
| 986 | return 0; |
| 987 | } |
| 988 | break; |
| 989 | } |
| 990 | |
| 991 | if (*endptr != ':') { |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | } |
| 996 | |
| 997 | *line++ = 0; |
| 998 | ++i; |
| 999 | } while (1); |
| 1000 | |
| 1001 | return EINVAL; |
| 1002 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 1003 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1004 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1005 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* Reads until if EOF, or until if finds a line which fits in the buffer |
| 1008 | * and for which the parser function succeeds. |
| 1009 | * |
| 1010 | * Returns 0 on success and ENOENT for end-of-file (glibc concession). |
| 1011 | */ |
| 1012 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 1013 | static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data, |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1014 | char *__restrict line_buff, size_t buflen, FILE *f) |
| 1015 | { |
| 1016 | int line_len; |
| 1017 | int skip; |
| 1018 | int rv = ERANGE; |
| 1019 | |
| 1020 | if (buflen < PWD_BUFFER_SIZE) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 1021 | errno = rv; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1022 | } else { |
| 1023 | skip = 0; |
| 1024 | do { |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 1025 | if (!fgets(line_buff, buflen, f)) { |
| 1026 | if (feof(f)) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1027 | rv = ENOENT; |
| 1028 | } |
| 1029 | break; |
| 1030 | } |
| 1031 | |
| 1032 | line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */ |
| 1033 | if (line_buff[line_len] == '\n') { |
| 1034 | line_buff[line_len] = 0; |
| 1035 | } else if (line_len + 2 == buflen) { /* line too long */ |
| 1036 | ++skip; |
| 1037 | continue; |
| 1038 | } |
| 1039 | |
| 1040 | if (skip) { |
| 1041 | --skip; |
| 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | /* NOTE: glibc difference - glibc strips leading whitespace from |
| 1046 | * records. We do not allow leading whitespace. */ |
| 1047 | |
| 1048 | /* Skip empty lines, comment lines, and lines with leading |
| 1049 | * whitespace. */ |
| 1050 | if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) { |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 1051 | if (parserfunc == bb__parsegrent) { /* Do evil group hack. */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1052 | /* The group entry parsing function needs to know where |
| 1053 | * the end of the buffer is so that it can construct the |
| 1054 | * group member ptr table. */ |
| 1055 | ((struct group *) data)->gr_name = line_buff + buflen; |
| 1056 | } |
| 1057 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 1058 | if (!parserfunc(data, line_buff)) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1059 | rv = 0; |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | } while (1); |
| 1064 | |
| 1065 | } |
| 1066 | |
| 1067 | return rv; |
| 1068 | } |