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