|
|
1.1 root 1: /*
2: * make an ID map for the file server library
3: * takes a /etc/passwd-like file and an optional `exception' list
4: * of explicit mappings;
5: * returns a list of local name-ID mappings to be combined
6: * with those from the client to make an ID-ID map.
7: */
8:
9: #include <stdio.h>
10: #include <ctype.h>
11: #include "rf.h"
12:
13: static Idmap *exmerge();
14: static int nextid();
15:
16: extern char *malloc(), *realloc();
17:
18: Idmap *
19: rfmkidmap(file, ex)
20: char *file;
21: Namemap *ex;
22: {
23: Idmap *map;
24: register Idmap *cur;
25: register int size;
26: register Idmap *last;
27: int curoff;
28: FILE *fp;
29:
30: if ((fp = fopen(file, "r")) == NULL)
31: rfpanic("can't open map %s\n", file);
32: size = 100;
33: if ((map = (Idmap *)malloc(size*sizeof(Idmap))) == NULL)
34: rfpanic("out of mem for %s map\n", file);
35: last = &map[size - 1];
36: for (cur = map;; cur++) {
37: if (cur >= last) {
38: curoff = cur - map; /* realloc may move map */
39: size += 100;
40: if ((map = (Idmap *)realloc((char *)map, size*sizeof(Idmap))) == NULL)
41: rfpanic("out of mem for %s map\n", file);
42: last = &map[size - 1];
43: cur = &map[curoff];
44: }
45: if (nextid(cur, fp) == 0)
46: break;
47: }
48: cur->name[0] = 0;
49: fclose(fp);
50: return (exmerge(map, last, ex));
51: }
52:
53: /*
54: * pick out the next name and ID from the file
55: * name:junk:number:junk until newline
56: * this matches /etc/passwd and /etc/group,
57: * and is simple enough to fake up
58: */
59: static
60: nextid(cur, fp)
61: Idmap *cur;
62: register FILE *fp;
63: {
64: register char *p;
65: char buf[150]; /* assume big enough for now */
66:
67: for (;;) {
68: if (fgets(buf, sizeof(buf), fp) == NULL)
69: return (0); /* EOF */
70: p = buf;
71: while (*p && *p != ':')
72: p++;
73: if (*p == 0)
74: continue;
75: *p++ = 0;
76: strncpy(cur->name, buf, sizeof(cur->name)-1);
77: cur->name[sizeof(cur->name)-1] = 0;
78: while (*p && *p != ':')
79: p++;
80: if (*p == 0)
81: continue;
82: p++;
83: if (!isdigit(*p) && *p != '-')
84: continue;
85: cur->id = atoi(p);
86: return (1);
87: }
88: }
89:
90: static Idmap *
91: exmerge(map, last, ex)
92: Idmap *map, *last;
93: Namemap *ex;
94: {
95: register Idmap *lp;
96: register Namemap *ep;
97: int size, curoff;
98: static Namemap nilex;
99:
100: if (ex == NULL)
101: ex = &nilex;
102: for (ep = ex; ep->cname[0]; ep++) {
103: if (ep->sname[0] == 0)
104: ep->sid = RFNOID;
105: else
106: ep->sid = _rflookid(map, ep->sname);
107: }
108: for (lp = map; lp->name[0]; lp++)
109: for (ep = ex; ep->cname[0]; ep++) {
110: if (ep->cname[0] != lp->name[0]) /* hash */
111: continue;
112: if (strcmp(ep->cname, lp->name) == 0) {
113: lp->id = ep->sid;
114: ep->sid = RFNOID;
115: }
116: }
117: /*
118: * add client names not in local file
119: * lp was left at the end of the map
120: */
121: for (ep = ex; ep->cname[0]; ep++) {
122: if (ep->sid == RFNOID) /* no need to enter */
123: continue;
124: if (lp >= last) {
125: curoff = lp - map;
126: size = curoff + 10;
127: if ((map = (Idmap *)realloc((char *)map,
128: size*sizeof(Idmap))) == NULL)
129: rfpanic("out of mem merging except\n");
130: last = &map[size - 1];
131: lp = &map[curoff];
132: }
133: strncpy(lp->name, ep->cname, sizeof(lp->name)-1);
134: lp->id = ep->sid;
135: lp++;
136: }
137: lp->name[0] = 0;
138: return (map);
139: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.