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