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