"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" |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [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 | ee5dce3 | 2008-09-26 10:35:55 +0000 | [diff] [blame] | 399 | #ifdef THIS_ONE_IS_UNUSED |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 400 | /* This one doesn't use static buffers */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 401 | int getpw(uid_t uid, char *buf) |
| 402 | { |
| 403 | struct passwd resultbuf; |
| 404 | struct passwd *result; |
| 405 | char buffer[PWD_BUFFER_SIZE]; |
| 406 | |
| 407 | if (!buf) { |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 408 | errno = EINVAL; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 409 | } else if (!getpwuid_r(uid, &resultbuf, buffer, sizeof(buffer), &result)) { |
| 410 | if (sprintf(buf, "%s:%s:%lu:%lu:%s:%s:%s\n", |
| 411 | resultbuf.pw_name, resultbuf.pw_passwd, |
| 412 | (unsigned long)(resultbuf.pw_uid), |
| 413 | (unsigned long)(resultbuf.pw_gid), |
| 414 | resultbuf.pw_gecos, resultbuf.pw_dir, |
| 415 | resultbuf.pw_shell) >= 0 |
| 416 | ) { |
| 417 | return 0; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return -1; |
| 422 | } |
Denis Vlasenko | ee5dce3 | 2008-09-26 10:35:55 +0000 | [diff] [blame] | 423 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 424 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 425 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 426 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 427 | /* FIXME: we don't have such CONFIG_xx - ?! */ |
| 428 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 429 | #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER |
| 430 | static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; |
| 431 | # define LOCK pthread_mutex_lock(&mylock) |
| 432 | # define UNLOCK pthread_mutex_unlock(&mylock); |
| 433 | #else |
| 434 | # define LOCK ((void) 0) |
| 435 | # define UNLOCK ((void) 0) |
| 436 | #endif |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 437 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 438 | static FILE *pwf /*= NULL*/; |
| 439 | void setpwent(void) |
| 440 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 441 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 442 | if (pwf) { |
| 443 | rewind(pwf); |
| 444 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 445 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | void endpwent(void) |
| 449 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 450 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 451 | if (pwf) { |
| 452 | fclose(pwf); |
| 453 | pwf = NULL; |
| 454 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 455 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 459 | int getpwent_r(struct passwd *__restrict resultbuf, |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 460 | char *__restrict buffer, size_t buflen, |
| 461 | struct passwd **__restrict result) |
| 462 | { |
| 463 | int rv; |
| 464 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 465 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 466 | *result = NULL; /* In case of error... */ |
| 467 | |
| 468 | if (!pwf) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 469 | pwf = fopen_for_read(_PATH_PASSWD); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 470 | if (!pwf) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 471 | rv = errno; |
| 472 | goto ERR; |
| 473 | } |
| 474 | } |
| 475 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 476 | rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 477 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 478 | *result = resultbuf; |
| 479 | } |
| 480 | |
| 481 | ERR: |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 482 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 483 | return rv; |
| 484 | } |
| 485 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 486 | static FILE *grf /*= NULL*/; |
| 487 | void setgrent(void) |
| 488 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 489 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 490 | if (grf) { |
| 491 | rewind(grf); |
| 492 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 493 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void endgrent(void) |
| 497 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 498 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 499 | if (grf) { |
| 500 | fclose(grf); |
| 501 | grf = NULL; |
| 502 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 503 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | int getgrent_r(struct group *__restrict resultbuf, |
| 507 | char *__restrict buffer, size_t buflen, |
| 508 | struct group **__restrict result) |
| 509 | { |
| 510 | int rv; |
| 511 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 512 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 513 | *result = NULL; /* In case of error... */ |
| 514 | |
| 515 | if (!grf) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 516 | grf = fopen_for_read(_PATH_GROUP); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 517 | if (!grf) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 518 | rv = errno; |
| 519 | goto ERR; |
| 520 | } |
| 521 | } |
| 522 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 523 | rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 524 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 525 | *result = resultbuf; |
| 526 | } |
| 527 | |
| 528 | ERR: |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 529 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 530 | return rv; |
| 531 | } |
| 532 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 533 | #if ENABLE_USE_BB_SHADOW |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 534 | static FILE *spf /*= NULL*/; |
| 535 | void setspent(void) |
| 536 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 537 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 538 | if (spf) { |
| 539 | rewind(spf); |
| 540 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 541 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void endspent(void) |
| 545 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 546 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 547 | if (spf) { |
| 548 | fclose(spf); |
| 549 | spf = NULL; |
| 550 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 551 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 554 | int getspent_r(struct spwd *resultbuf, char *buffer, |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 555 | size_t buflen, struct spwd **result) |
| 556 | { |
| 557 | int rv; |
| 558 | |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 559 | LOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 560 | *result = NULL; /* In case of error... */ |
| 561 | |
| 562 | if (!spf) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 563 | spf = fopen_for_read(_PATH_SHADOW); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 564 | if (!spf) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 565 | rv = errno; |
| 566 | goto ERR; |
| 567 | } |
| 568 | } |
| 569 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 570 | rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 571 | if (!rv) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 572 | *result = resultbuf; |
| 573 | } |
| 574 | |
| 575 | ERR: |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 576 | UNLOCK; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 577 | return rv; |
| 578 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 579 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 580 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 581 | #if 0 |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 582 | struct passwd *getpwent(void) |
| 583 | { |
| 584 | static char line_buff[PWD_BUFFER_SIZE]; |
| 585 | static struct passwd pwd; |
| 586 | struct passwd *result; |
| 587 | |
| 588 | getpwent_r(&pwd, line_buff, sizeof(line_buff), &result); |
| 589 | return result; |
| 590 | } |
| 591 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 592 | struct group *getgrent(void) |
| 593 | { |
| 594 | static char line_buff[GRP_BUFFER_SIZE]; |
| 595 | static struct group gr; |
| 596 | struct group *result; |
| 597 | |
| 598 | getgrent_r(&gr, line_buff, sizeof(line_buff), &result); |
| 599 | return result; |
| 600 | } |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 601 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 602 | |
Denis Vlasenko | 5df955f | 2007-03-13 13:01:14 +0000 | [diff] [blame] | 603 | #if 0 //ENABLE_USE_BB_SHADOW |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 604 | struct spwd *getspent(void) |
| 605 | { |
| 606 | static char line_buff[PWD_BUFFER_SIZE]; |
| 607 | static struct spwd spwd; |
| 608 | struct spwd *result; |
| 609 | |
| 610 | getspent_r(&spwd, line_buff, sizeof(line_buff), &result); |
| 611 | return result; |
| 612 | } |
| 613 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 614 | struct spwd *sgetspent(const char *string) |
| 615 | { |
| 616 | static char line_buff[PWD_BUFFER_SIZE]; |
| 617 | static struct spwd spwd; |
| 618 | struct spwd *result; |
| 619 | |
| 620 | sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result); |
| 621 | return result; |
| 622 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 623 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 624 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 625 | static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 626 | { |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 627 | FILE *grfile; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 628 | gid_t *group_list; |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 629 | int ngroups; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 630 | struct group group; |
| 631 | char buff[PWD_BUFFER_SIZE]; |
| 632 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 633 | /* We alloc space for 8 gids at a time. */ |
| 634 | group_list = xmalloc(8 * sizeof(group_list[0])); |
| 635 | group_list[0] = gid; |
| 636 | ngroups = 1; |
| 637 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 638 | grfile = fopen_for_read(_PATH_GROUP); |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 639 | if (grfile) { |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 640 | while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) { |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 641 | char **m; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 642 | assert(group.gr_mem); /* Must have at least a NULL terminator. */ |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 643 | if (group.gr_gid == gid) |
| 644 | continue; |
| 645 | for (m = group.gr_mem; *m; m++) { |
| 646 | if (strcmp(*m, user) != 0) |
| 647 | continue; |
| 648 | group_list = xrealloc_vector(group_list, 3, ngroups); |
| 649 | group_list[ngroups++] = group.gr_gid; |
| 650 | break; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
Bernhard Reutner-Fischer | 30c7de0 | 2005-10-28 11:21:40 +0000 | [diff] [blame] | 653 | fclose(grfile); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 654 | } |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 655 | *ngroups_ptr = ngroups; |
| 656 | return group_list; |
| 657 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 658 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 659 | int initgroups(const char *user, gid_t gid) |
| 660 | { |
| 661 | int ngroups; |
| 662 | gid_t *group_list = getgrouplist_internal(&ngroups, user, gid); |
| 663 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 664 | ngroups = setgroups(ngroups, group_list); |
| 665 | free(group_list); |
| 666 | return ngroups; |
| 667 | } |
| 668 | |
Denis Vlasenko | 2228426 | 2008-09-18 00:56:24 +0000 | [diff] [blame] | 669 | int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups) |
| 670 | { |
| 671 | int ngroups_old = *ngroups; |
| 672 | gid_t *group_list = getgrouplist_internal(ngroups, user, gid); |
| 673 | |
| 674 | if (*ngroups <= ngroups_old) { |
| 675 | ngroups_old = *ngroups; |
| 676 | memcpy(groups, group_list, ngroups_old * sizeof(groups[0])); |
| 677 | } else { |
| 678 | ngroups_old = -1; |
| 679 | } |
| 680 | free(group_list); |
| 681 | return ngroups_old; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 684 | int putpwent(const struct passwd *__restrict p, FILE *__restrict f) |
| 685 | { |
| 686 | int rv = -1; |
| 687 | |
| 688 | if (!p || !f) { |
Denis Vlasenko | 9230582 | 2008-03-20 15:12:58 +0000 | [diff] [blame] | 689 | errno = EINVAL; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 690 | } else { |
| 691 | /* No extra thread locking is needed above what fprintf does. */ |
| 692 | if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n", |
| 693 | p->pw_name, p->pw_passwd, |
| 694 | (unsigned long)(p->pw_uid), |
| 695 | (unsigned long)(p->pw_gid), |
| 696 | p->pw_gecos, p->pw_dir, p->pw_shell) >= 0 |
| 697 | ) { |
| 698 | rv = 0; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return rv; |
| 703 | } |
| 704 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 705 | int putgrent(const struct group *__restrict p, FILE *__restrict f) |
| 706 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 707 | static const char format[] ALIGN1 = ",%s"; |
| 708 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 709 | char **m; |
| 710 | const char *fmt; |
| 711 | int rv = -1; |
| 712 | |
| 713 | if (!p || !f) { /* Sigh... glibc checks. */ |
Denis Vlasenko | 9230582 | 2008-03-20 15:12:58 +0000 | [diff] [blame] | 714 | errno = EINVAL; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 715 | } else { |
| 716 | if (fprintf(f, "%s:%s:%lu:", |
| 717 | p->gr_name, p->gr_passwd, |
| 718 | (unsigned long)(p->gr_gid)) >= 0 |
| 719 | ) { |
| 720 | |
| 721 | fmt = format + 1; |
| 722 | |
| 723 | assert(p->gr_mem); |
| 724 | m = p->gr_mem; |
| 725 | |
| 726 | do { |
| 727 | if (!*m) { |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 728 | if (fputc('\n', f) >= 0) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 729 | rv = 0; |
| 730 | } |
| 731 | break; |
| 732 | } |
| 733 | if (fprintf(f, fmt, *m) < 0) { |
| 734 | break; |
| 735 | } |
| 736 | ++m; |
| 737 | fmt = format; |
| 738 | } while (1); |
| 739 | |
| 740 | } |
| 741 | |
| 742 | } |
| 743 | |
| 744 | return rv; |
| 745 | } |
| 746 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 747 | #if ENABLE_USE_BB_SHADOW |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 748 | static const unsigned char _sp_off[] ALIGN1 = { |
| 749 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ |
| 750 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ |
| 751 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ |
| 752 | offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ |
| 753 | offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ |
| 754 | offsetof(struct spwd, sp_expire) /* 7 - not a char ptr */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 755 | }; |
| 756 | |
| 757 | int putspent(const struct spwd *p, FILE *stream) |
| 758 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 759 | static const char ld_format[] ALIGN1 = "%ld:"; |
| 760 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 761 | const char *f; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 762 | long x; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 763 | int i; |
| 764 | int rv = -1; |
| 765 | |
| 766 | /* Unlike putpwent and putgrent, glibc does not check the args. */ |
| 767 | if (fprintf(stream, "%s:%s:", p->sp_namp, |
| 768 | (p->sp_pwdp ? p->sp_pwdp : "")) < 0 |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 769 | ) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 770 | goto DO_UNLOCK; |
| 771 | } |
| 772 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 773 | for (i = 0; i < sizeof(_sp_off); i++) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 774 | f = ld_format; |
Denis Vlasenko | 8746885 | 2007-04-13 23:22:00 +0000 | [diff] [blame] | 775 | x = *(const long *)(((const char *) p) + _sp_off[i]); |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 776 | if (x == -1) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 777 | f += 3; |
| 778 | } |
| 779 | if (fprintf(stream, f, x) < 0) { |
| 780 | goto DO_UNLOCK; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) { |
| 785 | goto DO_UNLOCK; |
| 786 | } |
| 787 | |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 788 | if (fputc('\n', stream) > 0) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 789 | rv = 0; |
| 790 | } |
| 791 | |
| 792 | DO_UNLOCK: |
| 793 | return rv; |
| 794 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 795 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 796 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 797 | /**********************************************************************/ |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 798 | /* Internal uClibc functions. */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 799 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 800 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 801 | static const unsigned char pw_off[] ALIGN1 = { |
| 802 | offsetof(struct passwd, pw_name), /* 0 */ |
| 803 | offsetof(struct passwd, pw_passwd), /* 1 */ |
| 804 | offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */ |
| 805 | offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ |
| 806 | offsetof(struct passwd, pw_gecos), /* 4 */ |
| 807 | offsetof(struct passwd, pw_dir), /* 5 */ |
| 808 | offsetof(struct passwd, pw_shell) /* 6 */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 809 | }; |
| 810 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 811 | static int bb__parsepwent(void *data, char *line) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 812 | { |
| 813 | char *endptr; |
| 814 | char *p; |
| 815 | int i; |
| 816 | |
| 817 | i = 0; |
| 818 | do { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 819 | p = (char *) data + pw_off[i]; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 820 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 821 | if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 822 | *((char **) p) = line; |
| 823 | if (i==6) { |
| 824 | return 0; |
| 825 | } |
| 826 | /* NOTE: glibc difference - glibc allows omission of |
| 827 | * ':' seperators after the gid field if all remaining |
| 828 | * entries are empty. We require all separators. */ |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 829 | line = strchr(line, ':'); |
| 830 | if (!line) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 831 | break; |
| 832 | } |
| 833 | } else { |
| 834 | unsigned long t = strtoul(line, &endptr, 10); |
| 835 | /* Make sure we had at least one digit, and that the |
| 836 | * failing char is the next field seperator ':'. See |
| 837 | * glibc difference note above. */ |
| 838 | /* TODO: Also check for leading whitespace? */ |
| 839 | if ((endptr == line) || (*endptr != ':')) { |
| 840 | break; |
| 841 | } |
| 842 | line = endptr; |
| 843 | if (i & 1) { /* i == 3 -- gid */ |
| 844 | *((gid_t *) p) = t; |
| 845 | } else { /* i == 2 -- uid */ |
| 846 | *((uid_t *) p) = t; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | *line++ = 0; |
| 851 | ++i; |
| 852 | } while (1); |
| 853 | |
| 854 | return -1; |
| 855 | } |
| 856 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 857 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 858 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 859 | static const unsigned char gr_off[] ALIGN1 = { |
| 860 | offsetof(struct group, gr_name), /* 0 */ |
| 861 | offsetof(struct group, gr_passwd), /* 1 */ |
| 862 | offsetof(struct group, gr_gid) /* 2 - not a char ptr */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 863 | }; |
| 864 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 865 | static int bb__parsegrent(void *data, char *line) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 866 | { |
| 867 | char *endptr; |
| 868 | char *p; |
| 869 | int i; |
| 870 | char **members; |
| 871 | char *end_of_buf; |
| 872 | |
| 873 | end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */ |
| 874 | i = 0; |
| 875 | do { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 876 | p = (char *) data + gr_off[i]; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 877 | |
| 878 | if (i < 2) { |
| 879 | *((char **) p) = line; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 880 | line = strchr(line, ':'); |
| 881 | if (!line) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 882 | break; |
| 883 | } |
| 884 | *line++ = 0; |
| 885 | ++i; |
| 886 | } else { |
| 887 | *((gid_t *) p) = strtoul(line, &endptr, 10); |
| 888 | |
| 889 | /* NOTE: glibc difference - glibc allows omission of the |
| 890 | * trailing colon when there is no member list. We treat |
| 891 | * this as an error. */ |
| 892 | |
| 893 | /* Make sure we had at least one digit, and that the |
| 894 | * failing char is the next field seperator ':'. See |
| 895 | * glibc difference note above. */ |
| 896 | if ((endptr == line) || (*endptr != ':')) { |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | i = 1; /* Count terminating NULL ptr. */ |
| 901 | p = endptr; |
| 902 | |
| 903 | if (p[1]) { /* We have a member list to process. */ |
| 904 | /* Overwrite the last ':' with a ',' before counting. |
| 905 | * This allows us to test for initial ',' and adds |
| 906 | * one ',' so that the ',' count equals the member |
| 907 | * count. */ |
| 908 | *p = ','; |
| 909 | do { |
| 910 | /* NOTE: glibc difference - glibc allows and trims leading |
| 911 | * (but not trailing) space. We treat this as an error. */ |
| 912 | /* NOTE: glibc difference - glibc allows consecutive and |
| 913 | * trailing commas, and ignores "empty string" users. We |
| 914 | * treat this as an error. */ |
| 915 | if (*p == ',') { |
| 916 | ++i; |
| 917 | *p = 0; /* nul-terminate each member string. */ |
| 918 | if (!*++p || (*p == ',') || isspace(*p)) { |
| 919 | goto ERR; |
| 920 | } |
| 921 | } |
| 922 | } while (*++p); |
| 923 | } |
| 924 | |
| 925 | /* Now align (p+1), rounding up. */ |
| 926 | /* Assumes sizeof(char **) is a power of 2. */ |
| 927 | members = (char **)( (((intptr_t) p) + sizeof(char **)) |
| 928 | & ~((intptr_t)(sizeof(char **) - 1)) ); |
| 929 | |
| 930 | if (((char *)(members + i)) > end_of_buf) { /* No space. */ |
| 931 | break; |
| 932 | } |
| 933 | |
| 934 | ((struct group *) data)->gr_mem = members; |
| 935 | |
| 936 | if (--i) { |
| 937 | p = endptr; /* Pointing to char prior to first member. */ |
| 938 | do { |
| 939 | *members++ = ++p; |
| 940 | if (!--i) break; |
| 941 | while (*++p) {} |
| 942 | } while (1); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 943 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 944 | *members = NULL; |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | } while (1); |
| 949 | |
| 950 | ERR: |
| 951 | return -1; |
| 952 | } |
| 953 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 954 | /**********************************************************************/ |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 955 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 956 | #if ENABLE_USE_BB_SHADOW |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 957 | static const unsigned char sp_off[] ALIGN1 = { |
| 958 | offsetof(struct spwd, sp_namp), /* 0 */ |
| 959 | offsetof(struct spwd, sp_pwdp), /* 1 */ |
| 960 | offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ |
| 961 | offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ |
| 962 | offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ |
| 963 | offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ |
| 964 | offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ |
| 965 | offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ |
| 966 | offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 967 | }; |
| 968 | |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 969 | static int bb__parsespent(void *data, char *line) |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 970 | { |
| 971 | char *endptr; |
| 972 | char *p; |
| 973 | int i; |
| 974 | |
| 975 | i = 0; |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 976 | while (1) { |
| 977 | p = (char *) data + sp_off[i]; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 978 | if (i < 2) { |
| 979 | *((char **) p) = line; |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 980 | line = strchr(line, ':'); |
| 981 | if (!line) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 982 | break; |
| 983 | } |
| 984 | } else { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 985 | *((long *) p) = strtoul(line, &endptr, 10); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 986 | |
| 987 | if (endptr == line) { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 988 | *((long *) p) = (i != 8) ? -1L : (long)(~0UL); |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | line = endptr; |
| 992 | |
| 993 | if (i == 8) { |
| 994 | if (!*endptr) { |
| 995 | return 0; |
| 996 | } |
| 997 | break; |
| 998 | } |
| 999 | |
| 1000 | if (*endptr != ':') { |
| 1001 | break; |
| 1002 | } |
| 1003 | |
| 1004 | } |
| 1005 | |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 1006 | *line++ = '\0'; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1007 | ++i; |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 1008 | } |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1009 | |
| 1010 | return EINVAL; |
| 1011 | } |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 1012 | #endif |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1013 | |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1014 | /**********************************************************************/ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1015 | |
| 1016 | /* Reads until if EOF, or until if finds a line which fits in the buffer |
| 1017 | * and for which the parser function succeeds. |
| 1018 | * |
| 1019 | * Returns 0 on success and ENOENT for end-of-file (glibc concession). |
| 1020 | */ |
| 1021 | |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 1022 | static int bb__pgsreader(int (*parserfunc)(void *d, char *line), void *data, |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1023 | char *__restrict line_buff, size_t buflen, FILE *f) |
| 1024 | { |
| 1025 | int line_len; |
| 1026 | int skip; |
| 1027 | int rv = ERANGE; |
| 1028 | |
| 1029 | if (buflen < PWD_BUFFER_SIZE) { |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 1030 | errno = rv; |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1031 | } else { |
| 1032 | skip = 0; |
| 1033 | do { |
Rob Landley | 25413bf | 2005-10-08 02:23:22 +0000 | [diff] [blame] | 1034 | if (!fgets(line_buff, buflen, f)) { |
| 1035 | if (feof(f)) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1036 | rv = ENOENT; |
| 1037 | } |
| 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */ |
| 1042 | if (line_buff[line_len] == '\n') { |
| 1043 | line_buff[line_len] = 0; |
| 1044 | } else if (line_len + 2 == buflen) { /* line too long */ |
| 1045 | ++skip; |
| 1046 | continue; |
| 1047 | } |
| 1048 | |
| 1049 | if (skip) { |
| 1050 | --skip; |
| 1051 | continue; |
| 1052 | } |
| 1053 | |
| 1054 | /* NOTE: glibc difference - glibc strips leading whitespace from |
| 1055 | * records. We do not allow leading whitespace. */ |
| 1056 | |
| 1057 | /* Skip empty lines, comment lines, and lines with leading |
| 1058 | * whitespace. */ |
| 1059 | if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) { |
Denis Vlasenko | cb04ff5 | 2006-12-30 21:11:57 +0000 | [diff] [blame] | 1060 | if (parserfunc == bb__parsegrent) { /* Do evil group hack. */ |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1061 | /* The group entry parsing function needs to know where |
| 1062 | * the end of the buffer is so that it can construct the |
| 1063 | * group member ptr table. */ |
| 1064 | ((struct group *) data)->gr_name = line_buff + buflen; |
| 1065 | } |
| 1066 | |
Denis Vlasenko | 7fa0fca | 2006-12-28 21:33:30 +0000 | [diff] [blame] | 1067 | if (!parserfunc(data, line_buff)) { |
Eric Andersen | 9615a08 | 2004-07-15 12:53:49 +0000 | [diff] [blame] | 1068 | rv = 0; |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | } while (1); |
| 1073 | |
| 1074 | } |
| 1075 | |
| 1076 | return rv; |
| 1077 | } |