📄 pwd_grp.c
字号:
{ LOCK; if (spf) { fclose(spf); spf = NULL; } UNLOCK;}int getspent_r(struct spwd *resultbuf, char *buffer, size_t buflen, struct spwd **result){ int rv; LOCK; *result = NULL; /* In case of error... */ if (!spf) { if (!(spf = fopen(_PATH_SHADOW, "r"))) { rv = errno; goto ERR; } __STDIO_SET_USER_LOCKING(spf); } if (!(rv = __pgsreader(__parsespent, resultbuf, buffer, buflen, spf))) { *result = resultbuf; } ERR: UNLOCK; return rv;}#endif/**********************************************************************/#ifdef L_getpwentstruct passwd *getpwent(void){ static char line_buff[__UCLIBC_PWD_BUFFER_SIZE__]; static struct passwd pwd; struct passwd *result; getpwent_r(&pwd, line_buff, sizeof(line_buff), &result); return result;}#endif/**********************************************************************/#ifdef L_getgrentstruct group *getgrent(void){ static char line_buff[__UCLIBC_GRP_BUFFER_SIZE__]; static struct group gr; struct group *result; getgrent_r(&gr, line_buff, sizeof(line_buff), &result); return result;}#endif/**********************************************************************/#ifdef L_getspentstruct spwd *getspent(void){ static char line_buff[__UCLIBC_PWD_BUFFER_SIZE__]; static struct spwd spwd; struct spwd *result; getspent_r(&spwd, line_buff, sizeof(line_buff), &result); return result;}#endif/**********************************************************************/#ifdef L_sgetspentstruct spwd *sgetspent(const char *string){ static char line_buff[__UCLIBC_PWD_BUFFER_SIZE__]; static struct spwd spwd; struct spwd *result; sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result); return result;}#endif/**********************************************************************/#ifdef L_initgroupsint initgroups(const char *user, gid_t gid){ FILE *grf; gid_t *group_list; int num_groups, rv; char **m; struct group group; char buff[__UCLIBC_PWD_BUFFER_SIZE__]; rv = -1; /* We alloc space for 8 gids at a time. */ if (((group_list = (gid_t *) malloc(8*sizeof(gid_t *))) != NULL) && ((grf = fopen(_PATH_GROUP, "r")) != NULL) ) { __STDIO_SET_USER_LOCKING(grf); *group_list = gid; num_groups = 1; while (!__pgsreader(__parsegrent, &group, buff, sizeof(buff), grf)) { assert(group.gr_mem); /* Must have at least a NULL terminator. */ if (group.gr_gid != gid) { for (m=group.gr_mem ; *m ; m++) { if (!strcmp(*m, user)) { if (!(num_groups & 7)) { gid_t *tmp = (gid_t *) realloc(group_list, (num_groups+8) * sizeof(gid_t *)); if (!tmp) { rv = -1; goto DO_CLOSE; } group_list = tmp; } group_list[num_groups++] = group.gr_gid; break; } } } } rv = setgroups(num_groups, group_list); DO_CLOSE: fclose(grf); } /* group_list will be NULL if initial malloc failed, which may trigger * warnings from various malloc debuggers. */ free(group_list); return rv;}#endif/**********************************************************************/#ifdef L_putpwentint putpwent(const struct passwd *__restrict p, FILE *__restrict f){ int rv = -1; if (!p || !f) { __set_errno(EINVAL); } else { /* No extra thread locking is needed above what fprintf does. */ if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n", p->pw_name, p->pw_passwd, (unsigned long)(p->pw_uid), (unsigned long)(p->pw_gid), p->pw_gecos, p->pw_dir, p->pw_shell) >= 0 ) { rv = 0; } } return rv;}#endif/**********************************************************************/#ifdef L_putgrentint putgrent(const struct group *__restrict p, FILE *__restrict f){ static const char format[] = ",%s"; char **m; const char *fmt; int rv = -1; __STDIO_AUTO_THREADLOCK_VAR; if (!p || !f) { /* Sigh... glibc checks. */ __set_errno(EINVAL); } else { __STDIO_AUTO_THREADLOCK(f); if (fprintf(f, "%s:%s:%lu:", p->gr_name, p->gr_passwd, (unsigned long)(p->gr_gid)) >= 0 ) { fmt = format + 1; assert(p->gr_mem); m = p->gr_mem; do { if (!*m) { if (fputc_unlocked('\n', f) >= 0) { rv = 0; } break; } if (fprintf(f, fmt, *m) < 0) { break; } ++m; fmt = format; } while (1); } __STDIO_AUTO_THREADUNLOCK(f); } return rv;}#endif/**********************************************************************/#ifdef L_putspentstatic const unsigned char sp_off[] = { offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */};int putspent(const struct spwd *p, FILE *stream){ static const char ld_format[] = "%ld:"; const char *f; long int x; int i; int rv = -1; __STDIO_AUTO_THREADLOCK_VAR; /* Unlike putpwent and putgrent, glibc does not check the args. */ __STDIO_AUTO_THREADLOCK(stream); if (fprintf(stream, "%s:%s:", p->sp_namp, (p->sp_pwdp ? p->sp_pwdp : "")) < 0 ) { goto DO_UNLOCK; } for (i=0 ; i < sizeof(sp_off) ; i++) { f = ld_format; if ((x = *(const long int *)(((const char *) p) + sp_off[i])) == -1) { f += 3; } if (fprintf(stream, f, x) < 0) { goto DO_UNLOCK; } } if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) { goto DO_UNLOCK; } if (fputc_unlocked('\n', stream) > 0) { rv = 0; } DO_UNLOCK: __STDIO_AUTO_THREADUNLOCK(stream); return rv;}#endif/**********************************************************************//* Internal uClibc functions. *//**********************************************************************/#ifdef L___parsepwentstatic const unsigned char pw_off[] = { offsetof(struct passwd, pw_name), /* 0 */ offsetof(struct passwd, pw_passwd), /* 1 */ offsetof(struct passwd, pw_uid), /* 2 - not a char ptr */ offsetof(struct passwd, pw_gid), /* 3 - not a char ptr */ offsetof(struct passwd, pw_gecos), /* 4 */ offsetof(struct passwd, pw_dir), /* 5 */ offsetof(struct passwd, pw_shell) /* 6 */};int __parsepwent(void *data, char *line){ char *endptr; char *p; int i; i = 0; do { p = ((char *) ((struct passwd *) data)) + pw_off[i]; if ((i & 6) ^ 2) { /* i!=2 and i!=3 */ *((char **) p) = line; if (i==6) { return 0; } /* NOTE: glibc difference - glibc allows omission of * ':' seperators after the gid field if all remaining * entries are empty. We require all separators. */ if (!(line = strchr(line, ':'))) { break; } } else { unsigned long t = strtoul(line, &endptr, 10); /* Make sure we had at least one digit, and that the * failing char is the next field seperator ':'. See * glibc difference note above. */ /* TODO: Also check for leading whitespace? */ if ((endptr == line) || (*endptr != ':')) { break; } line = endptr; if (i & 1) { /* i == 3 -- gid */ *((gid_t *) p) = t; } else { /* i == 2 -- uid */ *((uid_t *) p) = t; } } *line++ = 0; ++i; } while (1); return -1;}#endif/**********************************************************************/#ifdef L___parsegrentstatic const unsigned char gr_off[] = { offsetof(struct group, gr_name), /* 0 */ offsetof(struct group, gr_passwd), /* 1 */ offsetof(struct group, gr_gid) /* 2 - not a char ptr */};int __parsegrent(void *data, char *line){ char *endptr; char *p; int i; char **members; char *end_of_buf; end_of_buf = ((struct group *) data)->gr_name; /* Evil hack! */ i = 0; do { p = ((char *) ((struct group *) data)) + gr_off[i]; if (i < 2) { *((char **) p) = line; if (!(line = strchr(line, ':'))) { break; } *line++ = 0; ++i; } else { *((gid_t *) p) = strtoul(line, &endptr, 10); /* NOTE: glibc difference - glibc allows omission of the * trailing colon when there is no member list. We treat * this as an error. */ /* Make sure we had at least one digit, and that the * failing char is the next field seperator ':'. See * glibc difference note above. */ if ((endptr == line) || (*endptr != ':')) { break; } i = 1; /* Count terminating NULL ptr. */ p = endptr; if (p[1]) { /* We have a member list to process. */ /* Overwrite the last ':' with a ',' before counting. * This allows us to test for initial ',' and adds * one ',' so that the ',' count equals the member * count. */ *p = ','; do { /* NOTE: glibc difference - glibc allows and trims leading * (but not trailing) space. We treat this as an error. */ /* NOTE: glibc difference - glibc allows consecutive and * trailing commas, and ignores "empty string" users. We * treat this as an error. */ if (*p == ',') { ++i; *p = 0; /* nul-terminate each member string. */ if (!*++p || (*p == ',') || isspace(*p)) { goto ERR; } } } while (*++p); } /* Now align (p+1), rounding up. */ /* Assumes sizeof(char **) is a power of 2. */ members = (char **)( (((intptr_t) p) + sizeof(char **)) & ~((intptr_t)(sizeof(char **) - 1)) ); if (((char *)(members + i)) > end_of_buf) { /* No space. */ break; } ((struct group *) data)->gr_mem = members; if (--i) { p = endptr; /* Pointing to char prior to first member. */ do { *members++ = ++p; if (!--i) break; while (*++p) {} } while (1); } *members = NULL; return 0; } } while (1); ERR: return -1;}#endif/**********************************************************************/#ifdef L___parsespentstatic const unsigned char sp_off[] = { offsetof(struct spwd, sp_namp), /* 0 */ offsetof(struct spwd, sp_pwdp), /* 1 */ offsetof(struct spwd, sp_lstchg), /* 2 - not a char ptr */ offsetof(struct spwd, sp_min), /* 3 - not a char ptr */ offsetof(struct spwd, sp_max), /* 4 - not a char ptr */ offsetof(struct spwd, sp_warn), /* 5 - not a char ptr */ offsetof(struct spwd, sp_inact), /* 6 - not a char ptr */ offsetof(struct spwd, sp_expire), /* 7 - not a char ptr */ offsetof(struct spwd, sp_flag) /* 8 - not a char ptr */};int __parsespent(void *data, char * line){ char *endptr; char *p; int i; i = 0; do { p = ((char *) ((struct spwd *) data)) + sp_off[i]; if (i < 2) { *((char **) p) = line; if (!(line = strchr(line, ':'))) { break; } } else {#if 0 if (i==5) { /* Support for old format. */ while (isspace(*line)) ++line; /* glibc eats space here. */ if (!*line) { ((struct spwd *) data)->sp_warn = -1; ((struct spwd *) data)->sp_inact = -1; ((struct spwd *) data)->sp_expire = -1; ((struct spwd *) data)->sp_flag = ~0UL; return 0; } }#endif *((long *) p) = (long) strtoul(line, &endptr, 10); if (endptr == line) { *((long *) p) = ((i != 8) ? -1L : ((long)(~0UL))); } line = endptr; if (i == 8) { if (!*endptr) { return 0; } break; } if (*endptr != ':') { break; } } *line++ = 0; ++i; } while (1); return EINVAL;}#endif/**********************************************************************/#ifdef L___pgsreader/* Reads until if EOF, or until if finds a line which fits in the buffer * and for which the parser function succeeds. * * Returns 0 on success and ENOENT for end-of-file (glibc concession). */int __pgsreader(int (*__parserfunc)(void *d, char *line), void *data, char *__restrict line_buff, size_t buflen, FILE *f){ int line_len; int skip; int rv = ERANGE; __STDIO_AUTO_THREADLOCK_VAR; if (buflen < __UCLIBC_PWD_BUFFER_SIZE__) { __set_errno(rv); } else { __STDIO_AUTO_THREADLOCK(f); skip = 0; do { if (!fgets_unlocked(line_buff, buflen, f)) { if (feof_unlocked(f)) { rv = ENOENT; } break; } line_len = strlen(line_buff) - 1; /* strlen() must be > 0. */ if (line_buff[line_len] == '\n') { line_buff[line_len] = 0; } else if (line_len + 2 == buflen) { /* line too long */ ++skip; continue; } if (skip) { --skip; continue; } /* NOTE: glibc difference - glibc strips leading whitespace from * records. We do not allow leading whitespace. */ /* Skip empty lines, comment lines, and lines with leading * whitespace. */ if (*line_buff && (*line_buff != '#') && !isspace(*line_buff)) { if (__parserfunc == __parsegrent) { /* Do evil group hack. */ /* The group entry parsing function needs to know where * the end of the buffer is so that it can construct the * group member ptr table. */ ((struct group *) data)->gr_name = line_buff + buflen; } if (!__parserfunc(data, line_buff)) { rv = 0; break; } } } while (1); __STDIO_AUTO_THREADUNLOCK(f); } return rv;}#endif/**********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -