📄 getpwent.c
字号:
#if !defined(lint) && defined(SCCSIDS)static char sccsid[] = "@(#)getpwent.c 1.1 92/07/30 SMI";#endif/* * Copyright (c) 1990 by Sun Microsystems, Inc. */#ifdef S5EMUL#include <sys/param.h>#endif#include <stdio.h>#include <pwd.h>#include <rpcsvc/ypclnt.h>#include <ctype.h>#include <string.h>#include <syslog.h>#define MAXINT 0x7fffffff;extern void rewind();extern long strtol();extern int strcmp();#ifdef S5EMULextern int strncmp();#endifextern int strlen();extern int fclose();extern char *strcpy();extern char *strncpy();extern char *malloc();extern char *pwskip();void setpwent(), endpwent();static char *EMPTY = "";static struct _pwjunk { struct passwd _NULLPW; FILE *_pwf; /* pointer into /etc/passwd */ char *_yp; /* pointer into NIS */ int _yplen; char *_oldyp; int _oldyplen; struct list { char *name; struct list *nxt; } *_minuslist; struct passwd _interppasswd; char _interpline[BUFSIZ+1]; char *_domain; char *_PASSWD; char _errstring[200];} *__pwjunk;#define NULLPW (_pw->_NULLPW)#define pwf (_pw->_pwf)#define yp (_pw->_yp)#define yplen (_pw->_yplen)#define oldyp (_pw->_oldyp)#define oldyplen (_pw->_oldyplen)#define minuslist (_pw->_minuslist)#define interppasswd (_pw->_interppasswd)#define interpline (_pw->_interpline)#define domain (_pw->_domain)#define PASSWD (_pw->_PASSWD)#define errstring (_pw->_errstring)static struct passwd *interpret();static struct passwd *interpretwithsave();static struct passwd *save();static struct passwd *getnamefromyellow();static struct passwd *getuidfromyellow();#ifndef S5EMUL/* * The following is for compatibility with the 4.3BSD version. */int _pw_stayopen;#endifstatic struct _pwjunk *_pwjunk(){ register struct _pwjunk *_pw = __pwjunk; if (_pw == 0) { _pw = (struct _pwjunk *)calloc(1, sizeof (*__pwjunk)); if (_pw == 0) return (0); PASSWD = "/etc/passwd"; __pwjunk = _pw; } return (__pwjunk);}struct passwd *getpwnam(name) register char *name;{ register struct _pwjunk *_pw = _pwjunk(); struct passwd *pw; char line[BUFSIZ+1], namepw[20], *errstr = 0; if (_pw == 0) return (0); setpwent(); if (!pwf) return NULL; while (fgets(line, BUFSIZ, pwf) != NULL) { /* * If line is too long, consider it invalid and skip over. */ if (strchr(line, '\n') == NULL) { strncpy(namepw, line, sizeof (namepw)); namepw[sizeof (namepw) - 1] = '\0'; (void) pwskip(namepw); syslog(LOG_INFO|LOG_AUTH, "getpwnam: passwd entry exceeds %d characters: \"%s\"", BUFSIZ, name); errstr = 0; while (strchr(line, '\n') == NULL) { if (fgets(line, BUFSIZ, pwf) == NULL) { break; } } continue; } if ((pw = interpret(line, strlen(line), &errstr)) == NULL) { syslog(LOG_INFO|LOG_AUTH, "getpwnam: %s", errstr); errstr = 0; continue; } if (matchname(line, &pw, name, &errstr)) {#ifndef S5EMUL if (!_pw_stayopen)#endif endpwent(); return pw; } else if (errstr) { syslog(LOG_INFO|LOG_AUTH, "getpwnam: %s", errstr); errstr = 0; } }#ifndef S5EMUL if (!_pw_stayopen)#endif endpwent(); return NULL;}struct passwd *getpwuid(uid) register uid;{ register struct _pwjunk *_pw = _pwjunk(); struct passwd *pw; char line[BUFSIZ+1], name[20], *errstr = 0; if (_pw == 0) return (0); setpwent(); if (!pwf) return NULL; while (fgets(line, BUFSIZ, pwf) != NULL) { /* * If line is too long, consider it invalid and skip over. */ if (strchr(line, '\n') == NULL) { strncpy(name, line, sizeof(name)); name[sizeof(name) - 1] = '\0'; (void) pwskip(name); syslog(LOG_INFO|LOG_AUTH, "getpwuid: passwd entry exceeds %d characters: \"%s\"", BUFSIZ, name); errstr = 0; while (strchr(line, '\n') == NULL) { if (fgets(line, BUFSIZ, pwf) == NULL) { break; } } continue; } if ((pw = interpret(line, strlen(line), &errstr)) == NULL) { syslog(LOG_INFO|LOG_AUTH, "getpwuid: %s", errstr); errstr = 0; continue; } if (matchuid(line, &pw, uid, &errstr)) {#ifndef S5EMUL if (!_pw_stayopen)#endif endpwent(); return pw; } else if (errstr) { syslog(LOG_INFO|LOG_AUTH, "getpwuid: %s", errstr); errstr = 0; } }#ifndef S5EMUL if (!_pw_stayopen)#endif endpwent(); return NULL;}voidsetpwent(){ register struct _pwjunk *_pw = _pwjunk(); if (_pw == 0) return; if (domain == NULL) { (void) yp_get_default_domain(&domain ); } if (pwf == NULL) pwf = fopen(PASSWD, "r"); else rewind(pwf); if (yp) free(yp); yp = NULL; freeminuslist();}voidendpwent(){ register struct _pwjunk *_pw = _pwjunk(); if (_pw == 0) return; if (pwf != NULL) { (void) fclose(pwf); pwf = NULL; } if (yp) free(yp); yp = NULL; freeminuslist(); endnetgrent();}setpwfile(file) char *file;{ register struct _pwjunk *_pw = _pwjunk(); if (_pw == 0) return (0); PASSWD = file; return (1);}/* fgetpwent removed to separate file fgetpwent.c */static char *pwskip(p) register char *p;{ while(*p && *p != ':' && *p != '\n') ++p; if (*p == ':') *p++ = '\0'; return(p);}struct passwd *getpwent(){ register struct _pwjunk *_pw = _pwjunk(); char line1[BUFSIZ+1], name[20], *errstr = 0; static struct passwd *savepw; struct passwd *pw; char *user; char *mach; char *dom; if (_pw == 0) return (0); if (domain == NULL) { (void) yp_get_default_domain(&domain ); } if (pwf == NULL && (pwf = fopen(PASSWD, "r")) == NULL) { return (NULL); } for (;;) { if (yp) { pw = interpretwithsave(yp, yplen, savepw, &errstr); free(yp); if (pw == NULL) { if (errstr) { syslog(LOG_INFO|LOG_AUTH, "getpwent: %s", errstr); errstr = 0; } continue; } getnextfromyellow(); if (!onminuslist(pw)) { return(pw); } } else if (getnetgrent(&mach,&user,&dom)) { if (user) { pw = getnamefromyellow(user, savepw, &errstr); if (pw != NULL && !onminuslist(pw)) { return(pw); } else if (errstr) { syslog(LOG_INFO|LOG_AUTH, "getpwent: %s", errstr); errstr = 0; } } } else { endnetgrent(); if (fgets(line1, BUFSIZ, pwf) == NULL) { return(NULL); } /* * If line is too long, consider invalid and skip over. */ if (strchr(line1, '\n') == NULL) { strncpy(name, line1, sizeof(name)); name[sizeof(name) - 1] = '\0'; (void) pwskip(name); syslog(LOG_INFO|LOG_AUTH, "getpwent: passwd entry exceeds %d characters: \"%s\"", BUFSIZ, name); errstr = 0; while (strchr(line1, '\n') == NULL) { if (fgets(line1, BUFSIZ, pwf) == NULL) { break; } } continue; } if ((pw = interpret(line1, strlen(line1), &errstr)) == NULL) { syslog(LOG_INFO|LOG_AUTH, "getpwent: %s", errstr); errstr = 0; continue; } switch(line1[0]) { case '+': if (strcmp(pw->pw_name, "+") == 0) { getfirstfromyellow(); savepw = save(pw); } else if (line1[1] == '@') { savepw = save(pw); if (innetgr(pw->pw_name+2,(char *) NULL,"*",domain)) { /* include the whole NIS database */ getfirstfromyellow(); } else { setnetgrent(pw->pw_name+2); } } else { /* * else look up this entry in NIS. */ savepw = save(pw); pw = getnamefromyellow(pw->pw_name+1, savepw, &errstr); if (pw != NULL && !onminuslist(pw)) { return(pw); } else if (pw == NULL && errstr) { syslog(LOG_INFO|LOG_AUTH, "getpwent: %s", errstr); errstr = 0; } } break; case '-': if (line1[1] == '@') { if (innetgr(pw->pw_name+2,(char *) NULL,"*",domain)) { /* everybody was subtracted */ return(NULL); } setnetgrent(pw->pw_name+2); while (getnetgrent(&mach,&user,&dom)) { if (user) { addtominuslist(user); } } endnetgrent(); } else { addtominuslist(pw->pw_name+1); } break; default: if (!onminuslist(pw)) { return(pw); } break; } } }}staticmatchname(line1, pwp, name, errstr) char line1[]; struct passwd **pwp; char *name, **errstr;{ register struct _pwjunk *_pw = _pwjunk(); struct passwd *savepw; struct passwd *pw = *pwp; if (_pw == 0) return (0); switch(line1[0]) { case '+': if (strcmp(pw->pw_name, "+") == 0) { savepw = save(pw); pw = getnamefromyellow(name, savepw, errstr); if (pw) { *pwp = pw; return 1; } else return 0; } if (line1[1] == '@') { if (innetgr(pw->pw_name+2,(char *) NULL,name,domain)) { savepw = save(pw); pw = getnamefromyellow(name, savepw, errstr); if (pw) { *pwp = pw; return 1; } } return 0; }#ifdef S5EMUL if (strncmp(pw->pw_name+1, name, L_cuserid - 1) == 0) {#else if (strcmp(pw->pw_name+1, name) == 0) {#endif savepw = save(pw); pw = getnamefromyellow(pw->pw_name+1, savepw, errstr); if (pw) { *pwp = pw; return 1; } else return 0; } break; case '-': if (line1[1] == '@') { if (innetgr(pw->pw_name+2,(char *) NULL,name,domain)) { *pwp = NULL; return 1; } }#ifdef S5EMUL else if (strncmp(pw->pw_name+1, name, L_cuserid - 1) == 0) {#else else if (strcmp(pw->pw_name+1, name) == 0) {#endif *pwp = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -