|
|
1.1 root 1: #
2:
3:
4: /*
5: * Getname / getuserid for those with no
6: * hashed passwd data base).
7: * Do not compile this module in if you DO have hashed
8: * passwd's -- this is slower.
9: *
10: * Also provided here is a getpw routine which can share
11: * the open file. This is used for the Version 6 getenv
12: * implementation.
13: */
14:
15: #include "rcv.h"
16:
17: static FILE *pwfile = NULL; /* Pw file held open */
18: static char *pwname = "/etc/passwd"; /* Name of passwd file */
19:
20: /*
21: * Search the passwd file for a uid. Return name through ref parameter
22: * if found, indicating success with 0 return. Return -1 on error.
23: * If -1 is passed as the user id, close the passwd file.
24: */
25:
26: getname(uid, namebuf)
27: char namebuf[];
28: {
29: register char *cp, *cp2;
30: char linebuf[BUFSIZ];
31:
32: if (uid == -1) {
33: if (pwfile != NULL)
34: fclose(pwfile);
35: pwfile = NULL;
36: return(0);
37: }
38: if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
39: return(-1);
40: rewind(pwfile);
41: while (fgets(linebuf, BUFSIZ, pwfile) != NULL)
42: if (pweval(linebuf) == uid) {
43: for (cp = linebuf, cp2 = namebuf; *cp != ':';
44: *cp2++ = *cp++)
45: ;
46: *cp2 = '\0';
47: return(0);
48: }
49: return(-1);
50: }
51:
52: /*
53: * Read the users password file line into the passed line
54: * buffer.
55: */
56:
57: getpw(uid, linebuf)
58: char linebuf[];
59: {
60: register char *cp, *cp2;
61:
62: if (uid == -1) {
63: if (pwfile != NULL)
64: fclose(pwfile);
65: pwfile = NULL;
66: return(0);
67: }
68: if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
69: return(-1);
70: rewind(pwfile);
71: while (fgets(linebuf, BUFSIZ, pwfile) != NULL)
72: if (pweval(linebuf) == uid) {
73: if (linebuf[0] != '\0')
74: linebuf[strlen(linebuf)-1] = '\0';
75: return(0);
76: }
77: return(-1);
78: }
79:
80: /*
81: * Look for passwd line belonging to 'name'
82: */
83:
84: getpwnam(name, linebuf)
85: char name[], linebuf[];
86: {
87: register char *cp, *cp2;
88:
89: if (name == NOSTR) {
90: if (pwfile != NULL)
91: fclose(pwfile);
92: pwfile = NULL;
93: return(0);
94: }
95: if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL) {
96: perror(pwname);
97: return(-1);
98: }
99: rewind(pwfile);
100: while (fgets(linebuf, BUFSIZ, pwfile) != NULL) {
101: cp = linebuf;
102: cp2 = name;
103: while (*cp2++ == *cp++)
104: ;
105: if (*--cp == ':' && *--cp2 == 0)
106: return(0);
107: }
108: return(-1);
109: }
110:
111: /*
112: * Convert the passed name to a user id and return it. Return -1
113: * on error. Iff the name passed is -1 (yech) close the pwfile.
114: */
115:
116: getuserid(name)
117: char name[];
118: {
119: register char *cp, *cp2;
120: char linebuf[BUFSIZ];
121:
122: if (name == (char *) -1) {
123: if (pwfile != NULL)
124: fclose(pwfile);
125: pwfile = NULL;
126: return(0);
127: }
128: if (pwfile == NULL && (pwfile = fopen(pwname, "r")) == NULL)
129: return(-1);
130: rewind(pwfile);
131: while (fgets(linebuf, BUFSIZ, pwfile) != NULL) {
132: for (cp = name, cp2 = linebuf; *cp++ == *cp2++;)
133: ;
134: if (*--cp == '\0' && *--cp2 == ':')
135: return(pweval(linebuf));
136: }
137: return(-1);
138: }
139:
140: /*
141: * Evaluate the user id of the passed passwd line and return it.
142: */
143:
144: static
145: pweval(line)
146: char line[];
147: {
148: register char *cp;
149: register int i;
150: register int uid;
151:
152: for (cp = line, i = 0; i < 2; i += (*cp++ == ':'))
153: ;
154: uid = atoi(cp);
155:
156: #ifdef UIDGID
157: while (*cp && *cp != ':')
158: cp++;
159: cp++;
160: uid |= atoi(cp) << 8;
161: #endif
162:
163: return(uid);
164: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.