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