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