📄 nss_wrapper.c
字号:
ofs = PTR_DIFF(src->pw_shell, first); dst->pw_shell = buf + ofs; if (dstp) { *dstp = dst; } return 0;}/* * the caller has to call nwrap_unload() on failure */static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line){ struct nwrap_gr *nwrap_gr; char *c; char *p; char *e; struct group *gr; size_t list_size; unsigned nummem; nwrap_gr = (struct nwrap_gr *)nwrap->private_data; list_size = sizeof(*nwrap_gr->list) * (nwrap_gr->num+1); gr = (struct group *)realloc(nwrap_gr->list, list_size); if (!gr) { NWRAP_ERROR(("%s:realloc failed\n",__location__)); return false; } nwrap_gr->list = gr; gr = &nwrap_gr->list[nwrap_gr->num]; c = line; /* name */ p = strchr(c, ':'); if (!p) { NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n", __location__, line, c)); return false; } *p = '\0'; p++; gr->gr_name = c; c = p; NWRAP_VERBOSE(("name[%s]\n", gr->gr_name)); /* password */ p = strchr(c, ':'); if (!p) { NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n", __location__, line, c)); return false; } *p = '\0'; p++; gr->gr_passwd = c; c = p; NWRAP_VERBOSE(("password[%s]\n", gr->gr_passwd)); /* gid */ p = strchr(c, ':'); if (!p) { NWRAP_ERROR(("%s:invalid line[%s]: '%s'\n", __location__, line, c)); return false; } *p = '\0'; p++; e = NULL; gr->gr_gid = (gid_t)strtoul(c, &e, 10); if (c == e) { NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n", __location__, line, c, strerror(errno))); return false; } if (e == NULL) { NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n", __location__, line, c, strerror(errno))); return false; } if (e[0] != '\0') { NWRAP_ERROR(("%s:invalid line[%s]: '%s' - %s\n", __location__, line, c, strerror(errno))); return false; } c = p; NWRAP_VERBOSE(("gid[%u]\n", gr->gr_gid)); /* members */ gr->gr_mem = (char **)malloc(sizeof(char *)); if (!gr->gr_mem) { NWRAP_ERROR(("%s:calloc failed\n",__location__)); return false; } gr->gr_mem[0] = NULL; for(nummem=0; p; nummem++) { char **m; size_t m_size; c = p; p = strchr(c, ','); if (p) { *p = '\0'; p++; } if (strlen(c) == 0) { break; } m_size = sizeof(char *) * (nummem+2); m = (char **)realloc(gr->gr_mem, m_size); if (!m) { NWRAP_ERROR(("%s:realloc(%u) failed\n", __location__, m_size)); return false; } gr->gr_mem = m; gr->gr_mem[nummem] = c; gr->gr_mem[nummem+1] = NULL; NWRAP_VERBOSE(("member[%u]: '%s'\n", nummem, gr->gr_mem[nummem])); } NWRAP_DEBUG(("add group[%s:%s:%u:] with %u members\n", gr->gr_name, gr->gr_passwd, gr->gr_gid, nummem)); nwrap_gr->num++; return true;}static void nwrap_gr_unload(struct nwrap_cache *nwrap){ int i; struct nwrap_gr *nwrap_gr; nwrap_gr = (struct nwrap_gr *)nwrap->private_data; if (nwrap_gr->list) { for (i=0; i < nwrap_gr->num; i++) { if (nwrap_gr->list[i].gr_mem) { free(nwrap_gr->list[i].gr_mem); } } free(nwrap_gr->list); } nwrap_gr->list = NULL; nwrap_gr->num = 0; nwrap_gr->idx = 0;}static int nwrap_gr_copy_r(const struct group *src, struct group *dst, char *buf, size_t buflen, struct group **dstp){ char *first; char **lastm; char *last; off_t ofsb; off_t ofsm; off_t ofs; unsigned i; first = src->gr_name; lastm = src->gr_mem; while (*lastm) lastm++; last = *lastm; while (*last) last++; ofsb = PTR_DIFF(last + 1, first); ofsm = PTR_DIFF(lastm + 1, src->gr_mem); if ((ofsb + ofsm) > buflen) { return ERANGE; } memcpy(buf, first, ofsb); memcpy(buf + ofsb, src->gr_mem, ofsm); ofs = PTR_DIFF(src->gr_name, first); dst->gr_name = buf + ofs; ofs = PTR_DIFF(src->gr_passwd, first); dst->gr_passwd = buf + ofs; dst->gr_gid = src->gr_gid; dst->gr_mem = (char **)(buf + ofsb); for (i=0; src->gr_mem[i]; i++) { ofs = PTR_DIFF(src->gr_mem[i], first); dst->gr_mem[i] = buf + ofs; } if (dstp) { *dstp = dst; } return 0;}/* user functions */_PUBLIC_ struct passwd *nwrap_getpwnam(const char *name){ int i; if (!nwrap_enabled()) { return real_getpwnam(name); } nwrap_cache_reload(nwrap_pw_global.cache); for (i=0; i<nwrap_pw_global.num; i++) { if (strcmp(nwrap_pw_global.list[i].pw_name, name) == 0) { NWRAP_DEBUG(("%s: user[%s] found\n", __location__, name)); return &nwrap_pw_global.list[i]; } NWRAP_VERBOSE(("%s: user[%s] does not match [%s]\n", __location__, name, nwrap_pw_global.list[i].pw_name)); } NWRAP_DEBUG(("%s: user[%s] not found\n", __location__, name)); errno = ENOENT; return NULL;}_PUBLIC_ int nwrap_getpwnam_r(const char *name, struct passwd *pwdst, char *buf, size_t buflen, struct passwd **pwdstp){ struct passwd *pw; if (!nwrap_enabled()) { return real_getpwnam_r(name, pwdst, buf, buflen, pwdstp); } pw = nwrap_getpwnam(name); if (!pw) { if (errno == 0) { return ENOENT; } return errno; } return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);}_PUBLIC_ struct passwd *nwrap_getpwuid(uid_t uid){ int i; if (!nwrap_enabled()) { return real_getpwuid(uid); } nwrap_cache_reload(nwrap_pw_global.cache); for (i=0; i<nwrap_pw_global.num; i++) { if (nwrap_pw_global.list[i].pw_uid == uid) { NWRAP_DEBUG(("%s: uid[%u] found\n", __location__, uid)); return &nwrap_pw_global.list[i]; } NWRAP_VERBOSE(("%s: uid[%u] does not match [%u]\n", __location__, uid, nwrap_pw_global.list[i].pw_uid)); } NWRAP_DEBUG(("%s: uid[%u] not found\n", __location__, uid)); errno = ENOENT; return NULL;}_PUBLIC_ int nwrap_getpwuid_r(uid_t uid, struct passwd *pwdst, char *buf, size_t buflen, struct passwd **pwdstp){ struct passwd *pw; if (!nwrap_enabled()) { return real_getpwuid_r(uid, pwdst, buf, buflen, pwdstp); } pw = nwrap_getpwuid(uid); if (!pw) { if (errno == 0) { return ENOENT; } return errno; } return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);}/* user enum functions */_PUBLIC_ void nwrap_setpwent(void){ if (!nwrap_enabled()) { real_setpwent(); } nwrap_pw_global.idx = 0;}_PUBLIC_ struct passwd *nwrap_getpwent(void){ struct passwd *pw; if (!nwrap_enabled()) { return real_getpwent(); } if (nwrap_pw_global.idx == 0) { nwrap_cache_reload(nwrap_pw_global.cache); } if (nwrap_pw_global.idx >= nwrap_pw_global.num) { errno = ENOENT; return NULL; } pw = &nwrap_pw_global.list[nwrap_pw_global.idx++]; NWRAP_VERBOSE(("%s: return user[%s] uid[%u]\n", __location__, pw->pw_name, pw->pw_uid)); return pw;}_PUBLIC_ int nwrap_getpwent_r(struct passwd *pwdst, char *buf, size_t buflen, struct passwd **pwdstp){ struct passwd *pw; if (!nwrap_enabled()) {#ifdef SOLARIS_GETPWENT_R pw = real_getpwent_r(pwdst, buf, buflen); if (!pw) { if (errno == 0) { return ENOENT; } return errno; } if (pwdstp) { *pwdstp = pw; } return 0;#else return real_getpwent_r(pwdst, buf, buflen, pwdstp);#endif } pw = nwrap_getpwent(); if (!pw) { if (errno == 0) { return ENOENT; } return errno; } return nwrap_pw_copy_r(pw, pwdst, buf, buflen, pwdstp);}_PUBLIC_ void nwrap_endpwent(void){ if (!nwrap_enabled()) { real_endpwent(); } nwrap_pw_global.idx = 0;}/* misc functions */_PUBLIC_ int nwrap_initgroups(const char *user, gid_t group){ if (!nwrap_enabled()) { return real_initgroups(user, group); } /* TODO: maybe we should also fake this... */ return EPERM;}/* group functions */_PUBLIC_ struct group *nwrap_getgrnam(const char *name){ int i; if (!nwrap_enabled()) { return real_getgrnam(name); } nwrap_cache_reload(nwrap_gr_global.cache); for (i=0; i<nwrap_gr_global.num; i++) { if (strcmp(nwrap_gr_global.list[i].gr_name, name) == 0) { NWRAP_DEBUG(("%s: group[%s] found\n", __location__, name)); return &nwrap_gr_global.list[i]; } NWRAP_VERBOSE(("%s: group[%s] does not match [%s]\n", __location__, name, nwrap_gr_global.list[i].gr_name)); } NWRAP_DEBUG(("%s: group[%s] not found\n", __location__, name)); errno = ENOENT; return NULL;}_PUBLIC_ int nwrap_getgrnam_r(const char *name, struct group *grdst, char *buf, size_t buflen, struct group **grdstp){ struct group *gr; if (!nwrap_enabled()) { return real_getgrnam_r(name, grdst, buf, buflen, grdstp); } gr = nwrap_getgrnam(name); if (!gr) { if (errno == 0) { return ENOENT; } return errno; } return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);}_PUBLIC_ struct group *nwrap_getgrgid(gid_t gid){ int i; if (!nwrap_enabled()) { return real_getgrgid(gid); } nwrap_cache_reload(nwrap_gr_global.cache); for (i=0; i<nwrap_gr_global.num; i++) { if (nwrap_gr_global.list[i].gr_gid == gid) { NWRAP_DEBUG(("%s: gid[%u] found\n", __location__, gid)); return &nwrap_gr_global.list[i]; } NWRAP_VERBOSE(("%s: gid[%u] does not match [%u]\n", __location__, gid, nwrap_gr_global.list[i].gr_gid)); } NWRAP_DEBUG(("%s: gid[%u] not found\n", __location__, gid)); errno = ENOENT; return NULL;}_PUBLIC_ int nwrap_getgrgid_r(gid_t gid, struct group *grdst, char *buf, size_t buflen, struct group **grdstp){ struct group *gr; if (!nwrap_enabled()) { return real_getgrgid_r(gid, grdst, buf, buflen, grdstp); } gr = nwrap_getgrgid(gid); if (!gr) { if (errno == 0) { return ENOENT; } return errno; } return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp); return ENOENT;}/* group enum functions */_PUBLIC_ void nwrap_setgrent(void){ if (!nwrap_enabled()) { real_setgrent(); } nwrap_gr_global.idx = 0;}_PUBLIC_ struct group *nwrap_getgrent(void){ struct group *gr; if (!nwrap_enabled()) { return real_getgrent(); } if (nwrap_gr_global.idx == 0) { nwrap_cache_reload(nwrap_gr_global.cache); } if (nwrap_gr_global.idx >= nwrap_gr_global.num) { errno = ENOENT; return NULL; } gr = &nwrap_gr_global.list[nwrap_gr_global.idx++]; NWRAP_VERBOSE(("%s: return group[%s] gid[%u]\n", __location__, gr->gr_name, gr->gr_gid)); return gr;}_PUBLIC_ int nwrap_getgrent_r(struct group *grdst, char *buf, size_t buflen, struct group **grdstp){ struct group *gr; if (!nwrap_enabled()) {#ifdef SOLARIS_GETGRENT_R gr = real_getgrent_r(grdst, buf, buflen); if (!gr) { if (errno == 0) { return ENOENT; } return errno; } if (grdstp) { *grdstp = gr; } return 0;#else return real_getgrent_r(grdst, buf, buflen, grdstp);#endif } gr = nwrap_getgrent(); if (!gr) { if (errno == 0) { return ENOENT; } return errno; } return nwrap_gr_copy_r(gr, grdst, buf, buflen, grdstp);}_PUBLIC_ void nwrap_endgrent(void){ if (!nwrap_enabled()) { real_endgrent(); } nwrap_gr_global.idx = 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -