]>
Commit | Line | Data |
---|---|---|
1 | /* $Header$ | |
2 | * | |
3 | * This generates the files necessary to load an nfs server. | |
4 | * | |
5 | * (c) Copyright 1988 by the Massachusetts Institute of Technology. | |
6 | * For copying and distribution information, please see the file | |
7 | * <mit-copyright.h>. | |
8 | */ | |
9 | ||
10 | #include <mit-copyright.h> | |
11 | #include <stdio.h> | |
12 | #include <sms.h> | |
13 | #include <sms_app.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/stat.h> | |
16 | #include <sys/time.h> | |
17 | ||
18 | #define NFS_DIR "/u1/sms/dcm/nfs" | |
19 | ||
20 | #define min(x,y) ((x) < (y) ? (x) : (y)) | |
21 | ||
22 | char *whoami = "nfs.gen"; | |
23 | char *malloc(), *strsave(); | |
24 | char *ingres_date_and_time(), *ingres_time(), *ingres_date(); | |
25 | ||
26 | main(argc, argv) | |
27 | int argc; | |
28 | char **argv; | |
29 | { | |
30 | char cmd[64]; | |
31 | struct stat sb; | |
32 | int changed = 0; | |
33 | int ingerr(); | |
34 | ||
35 | if (argc > 2) { | |
36 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
37 | exit(SMS_ARGS); | |
38 | } | |
39 | ||
40 | IIseterr(ingerr); | |
41 | ## ingres sms | |
42 | ||
43 | changed = do_nfs(); | |
44 | ||
45 | ## exit | |
46 | ||
47 | if (!changed) { | |
48 | fprintf(stderr, "No files updated.\n"); | |
49 | if (argc == 2 && stat(argv[1], &sb) == 0) | |
50 | exit(SMS_NO_CHANGE); | |
51 | } | |
52 | ||
53 | if (argc == 2) { | |
54 | sprintf(cmd, "cd %s; cp /u1/sms/bin/install_* .; tar cf %s .", NFS_DIR, argv[1]); | |
55 | if (system(cmd)) | |
56 | exit(SMS_TAR_FAIL); | |
57 | } | |
58 | ||
59 | exit(SMS_SUCCESS); | |
60 | } | |
61 | ||
62 | ||
63 | /* | |
64 | * ingerr: (supposedly) called when Ingres indicates an error. | |
65 | * I have not yet been able to get this to work to intercept a | |
66 | * database open error. | |
67 | */ | |
68 | #define INGRES_DEADLOCK 4700 | |
69 | ||
70 | static int ingerr(num) | |
71 | int *num; | |
72 | { | |
73 | char buf[256]; | |
74 | int ingres_errno; | |
75 | ||
76 | switch (*num) { | |
77 | case INGRES_DEADLOCK: | |
78 | ingres_errno = SMS_DEADLOCK; | |
79 | break; | |
80 | default: | |
81 | ingres_errno = SMS_INGRES_ERR; | |
82 | } | |
83 | com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num); | |
84 | critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num); | |
85 | exit(ingres_errno); | |
86 | } | |
87 | ||
88 | ||
89 | /* Generate the files. Returns zero if nothing changed, non-zero otherwise. */ | |
90 | ||
91 | int do_nfs() | |
92 | ##{ | |
93 | ## char machname[33], listname[33]; | |
94 | struct save_queue *machs, *lists; | |
95 | int changed; | |
96 | ||
97 | machs = sq_create(); | |
98 | lists = sq_create(); | |
99 | ## range of s is serverhosts | |
100 | ## retrieve (machname = trim(machine.name), listname = trim(s.value3)) | |
101 | ## where machine.mach_id = s.mach_id and s.enable != 0 { | |
102 | sq_save_unique_string(machs, strsave(machname)); | |
103 | sq_save_unique_string(lists, strsave(listname)); | |
104 | ## } | |
105 | ||
106 | changed = do_lists(lists); | |
107 | changed += do_machs(machs); | |
108 | return(changed); | |
109 | ##} | |
110 | ||
111 | ||
112 | /* Make all of the credentials lists that will be needed. Returns 0 if | |
113 | * no files were actually changed */ | |
114 | ||
115 | int do_lists(lists) | |
116 | struct save_queue *lists; | |
117 | ##{ | |
118 | struct save_queue *sq, *members; | |
119 | char file[64], *u; | |
120 | struct hash *users, *do_everyone(); | |
121 | struct stat sb; | |
122 | FILE *fd; | |
123 | ## char *listname, *lsname, lname[33], uname[9], *filetime; | |
124 | ## int uid, id, flag1, flag2, flag3, flag4; | |
125 | ||
126 | sprintf(file, "%s/list-", NFS_DIR); | |
127 | if (stat(file, &sb) == 0) { | |
128 | filetime = ingres_date_and_time(sb.st_mtime); | |
129 | ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime))) | |
130 | ## where tblstats.table = "users" | |
131 | ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime))) | |
132 | ## where tblstats.table = "list" | |
133 | ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime))) | |
134 | ## where tblstats.table = "members" | |
135 | ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime))) | |
136 | ## where tblstats.table = "serverhosts" | |
137 | if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) { | |
138 | fprintf(stderr, "The lists do not need to be rebuilt.\n"); | |
139 | return(0); | |
140 | } | |
141 | } | |
142 | ||
143 | /* build the list of everyone, and store it in a file whose name | |
144 | * corresponds to the empty list. | |
145 | */ | |
146 | users = do_everyone(); | |
147 | ||
148 | fprintf(stderr, "Building specific lists\n"); | |
149 | /* now do each of the lists used by an NFS server */ | |
150 | ## range of l is list | |
151 | ## range of l1 is list | |
152 | ## range of m is members | |
153 | ## range of u is users | |
154 | while (sq_get_data(lists, &listname)) { | |
155 | if (strlen(listname) == 0) | |
156 | continue; | |
157 | sprintf(file, "%s/list-%s", NFS_DIR, listname); | |
158 | fd = fopen(file, "w"); | |
159 | if (!fd) { | |
160 | fprintf(stderr, "cannot open %s for output\n", file); | |
161 | exit(SMS_OCONFIG); | |
162 | } | |
163 | sq = sq_create(); | |
164 | sq_save_data(sq, listname); | |
165 | members = sq_create(); | |
166 | while (sq_get_data(sq, &lsname)) { | |
167 | ## repeat retrieve (lname = trim(l.name)) | |
168 | ## where m.list_id = l1.list_id and l1.name = @lsname and | |
169 | ## m.member_type = "LIST" and m.member_id = l.list_id { | |
170 | sq_save_unique_string(sq, strsave(lname)); | |
171 | ## } | |
172 | ## repeat retrieve (id = m.member_id) | |
173 | ## where m.list_id = l1.list_id and l1.name = @lsname and | |
174 | ## m.member_type = "USER" { | |
175 | if (u = hash_lookup(users, id)) { | |
176 | sq_save_unique_data(members, u); | |
177 | } | |
178 | ## } | |
179 | } | |
180 | while (sq_get_data(members, &lsname)) | |
181 | fprintf(fd, "%s\n", lsname); | |
182 | sq_destroy(members); | |
183 | sq_destroy(sq); | |
184 | if (fclose(fd)) { | |
185 | fprintf(stderr, "error closing %s\n", file); | |
186 | exit(SMS_CCONFIG); | |
187 | } | |
188 | } | |
189 | sq_destroy(lists); | |
190 | return(1); | |
191 | ##} | |
192 | ||
193 | ||
194 | /* Build the list of everybody. */ | |
195 | struct grp { | |
196 | struct grp *next; | |
197 | int id; | |
198 | }; | |
199 | struct user { | |
200 | char name[9]; | |
201 | int uid; | |
202 | struct grp *lists; | |
203 | }; | |
204 | ||
205 | struct hash *do_everyone() | |
206 | ##{ | |
207 | char buf[BUFSIZ], *l; | |
208 | struct hash *groups, *users; | |
209 | struct user *u; | |
210 | struct grp *g; | |
211 | struct bucket *b, **p; | |
212 | ## char name[33]; | |
213 | ## int gid, id, lid, maxid, uid; | |
214 | FILE *fd; | |
215 | int i; | |
216 | struct save_queue *sq; | |
217 | ||
218 | fprintf(stderr, "Building the list of everybody\n"); | |
219 | sprintf(buf, "%s/list-", NFS_DIR); | |
220 | fd = fopen(buf, "w"); | |
221 | if (!fd) { | |
222 | fprintf(stderr, "cannot open %s for output\n", buf); | |
223 | exit(SMS_OCONFIG); | |
224 | } | |
225 | ||
226 | /* make space for group list */ | |
227 | groups = create_hash(15000); | |
228 | ||
229 | /* retrieve simple groups */ | |
230 | ## range of l is list | |
231 | ## retrieve (gid = l.#gid, lid = l.list_id) | |
232 | ## where l.group != 0 and l.active != 0 { | |
233 | sprintf(buf, ":%d", gid); | |
234 | hash_store(groups, lid, strsave(buf)); | |
235 | ## } | |
236 | ||
237 | /* get special cases: lists that aren't groups themselves but are | |
238 | * members of groups. */ | |
239 | sq = sq_create(); | |
240 | ## range of m is members | |
241 | ## retrieve (gid = list.#gid, lid = l.list_id) | |
242 | ## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and | |
243 | ## m.member_id = l.list_id and m.list_id = list.list_id and | |
244 | ## list.group != 0 { | |
245 | if (l = hash_lookup(groups, lid)) { | |
246 | sprintf(buf, "%s:%d", l, gid); | |
247 | free(l); | |
248 | } else { | |
249 | sprintf(buf, ":%d", gid); | |
250 | } | |
251 | hash_store(groups, lid, strsave(buf)); | |
252 | sq_save_data(sq, lid); | |
253 | ## } | |
254 | ||
255 | while (sq_get_data(sq, &id)) { | |
256 | ## repeat retrieve (lid = l.list_id) where l.active != 0 and | |
257 | ## m.member_type = "LIST" and m.member_id = l.list_id and | |
258 | ## m.list_id = @id { | |
259 | if (l = hash_lookup(groups, lid)) { | |
260 | sprintf(buf, "%s%s", l, hash_lookup(groups, id)); | |
261 | free(l); | |
262 | l = strsave(buf); | |
263 | } else { | |
264 | l = hash_lookup(groups, id); | |
265 | } | |
266 | hash_store(groups, lid, l); | |
267 | sq_save_unique_data(sq, lid); | |
268 | ## } | |
269 | } | |
270 | sq_destroy(sq); | |
271 | ||
272 | /* now do grplists */ | |
273 | users = create_hash(10000); | |
274 | ## range of u is users | |
275 | ## retrieve (id = u.users_id, name = u.login, uid = u.#uid) | |
276 | ## where u.status = 1 { | |
277 | u = (struct user *) malloc(sizeof(struct user)); | |
278 | strcpy(u->name, strtrim(name)); | |
279 | u->uid = uid; | |
280 | u->lists = NULL; | |
281 | hash_store(users, id, u); | |
282 | ## } | |
283 | ||
284 | ## repeat retrieve (lid = m.list_id, id = m.member_id) | |
285 | ## where m.member_type = "USER" { | |
286 | if ((u = (struct user *) hash_lookup(users, id)) && | |
287 | (hash_lookup(groups, lid) != NULL)) { | |
288 | g = (struct grp *) malloc(sizeof(struct grp)); | |
289 | g->next = u->lists; | |
290 | u->lists = g; | |
291 | g->id = lid; | |
292 | } | |
293 | ## } | |
294 | ||
295 | for (p = &(users->data[users->size - 1]); p >= users->data; p--) { | |
296 | for (b = *p; b; b = b->next) { | |
297 | i = 0; | |
298 | u = (struct user *)b->data; | |
299 | sprintf(buf, "%s:%d", u->name, u->uid); | |
300 | for (g = u->lists; g; g = g->next) | |
301 | if (l = hash_lookup(groups, g->id)) { | |
302 | i++; | |
303 | strcat(buf, l); | |
304 | } | |
305 | /* should free stuff here... */ | |
306 | if (i > 1) { | |
307 | remove_duplicate_groups(buf); | |
308 | } | |
309 | b->data = strsave(buf); | |
310 | fprintf(fd, "%s\n", buf); | |
311 | } | |
312 | } | |
313 | ||
314 | fclose(fd); | |
315 | free(groups); | |
316 | return(users); | |
317 | ##} | |
318 | ||
319 | #define MAXGROUPS 256 | |
320 | ||
321 | remove_duplicate_groups(p) | |
322 | register char *p; | |
323 | { | |
324 | register char *pp; | |
325 | char *index(); | |
326 | register int *mp; | |
327 | int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1]; | |
328 | ||
329 | bzero(memory, MAXGROUPS * sizeof(int)); | |
330 | p = index(p, ':'); | |
331 | p++; | |
332 | while (p = index(p, ':')) { | |
333 | i = atoi(++p); | |
334 | pp = p; | |
335 | for (mp = memory; *mp && mp < mg; mp++) | |
336 | if (*mp == i) { | |
337 | if (p = index(p, ':')) | |
338 | strcpy(pp, ++p); | |
339 | else { | |
340 | *(--pp) = 0; | |
341 | } | |
342 | p = pp - 2; | |
343 | break; | |
344 | } | |
345 | *mp = i; | |
346 | } | |
347 | } | |
348 | ||
349 | ||
350 | /* Now do each of the servers, linking the credentials list file and | |
351 | * compiling the quota and dirs files. | |
352 | */ | |
353 | ||
354 | int do_machs(machs) | |
355 | struct save_queue *machs; | |
356 | ##{ | |
357 | ## char *machname, listname[33], dev[33], *device, dir[64], fstype[9]; | |
358 | ## char *filetime; | |
359 | ## int uid, quota, id, gid, flag1, flag2, flag3, flag4; | |
360 | char file[64], f1[64], f2[64], *cp, *index(); | |
361 | int prevuid, quotasum; | |
362 | FILE *fd; | |
363 | struct stat sb; | |
364 | struct save_queue *sq; | |
365 | ||
366 | /* | |
367 | sprintf(file, "%s/list-", NFS_DIR); | |
368 | if (stat(file, &sb) == 0) { | |
369 | filetime = ingres_date_and_time(sb.st_mtime); | |
370 | # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime))) | |
371 | # where tblstats.table = "serverhosts" | |
372 | # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime))) | |
373 | # where tblstats.table = "filesys" | |
374 | # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime))) | |
375 | # where tblstats.table = "nfsquota" | |
376 | # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime))) | |
377 | # where tblstats.table = "nfsphys" | |
378 | if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) { | |
379 | fprintf(stderr, "The machine files do not need to be rebuilt.\n"); | |
380 | return(0); | |
381 | } | |
382 | } | |
383 | */ | |
384 | ||
385 | fprintf(stderr, "Building machine files\n"); | |
386 | ||
387 | ## range of s is serverhosts | |
388 | ## range of m is machine | |
389 | ## range of n is nfsphys | |
390 | ## range of q is nfsquota | |
391 | ## range of f is filesys | |
392 | ## range of u is users | |
393 | ## range of l is list | |
394 | while (sq_get_data(machs, &machname)) { | |
395 | ## repeat retrieve (listname = trim(s.value3)) | |
396 | ## where s.mach_id = m.mach_id and m.name = @machname | |
397 | strtrim(machname); | |
398 | sprintf(f1, "%s/list-%s", NFS_DIR, listname); | |
399 | sprintf(f2, "%s/%s.cred", NFS_DIR, machname); | |
400 | unlink(f2); /* ignore errors on this unlink */ | |
401 | if (link(f1, f2)) { | |
402 | fprintf(stderr, "Cannot link %s to %s\n", f1, f2); | |
403 | exit(SMS_OCONFIG); | |
404 | } | |
405 | sq = sq_create(); | |
406 | ## repeat retrieve (dev = trim(n.#device)) | |
407 | ## where n.mach_id = m.mach_id and m.name = @machname { | |
408 | sq_save_data(sq, strsave(dev)); | |
409 | ## } | |
410 | while (sq_get_data(sq, &device)) { | |
411 | ## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and | |
412 | ## m.#name = @machname and n.#device = @device | |
413 | while (cp = index(device, '/')) *cp = '@'; | |
414 | sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device); | |
415 | fd = fopen(file, "w"); | |
416 | if (!fd) { | |
417 | fprintf(stderr, "cannot open %s for output\n", file); | |
418 | exit(SMS_OCONFIG); | |
419 | } | |
420 | prevuid = -1; | |
421 | quotasum = 0; | |
422 | ## repeat retrieve (uid = u.#uid, quota = q.#quota) | |
423 | ## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and | |
424 | ## f.phys_id = @id and u.status != 0 sort by #uid { | |
425 | if (uid != prevuid) { | |
426 | if (quotasum) | |
427 | fprintf(fd, "%d %d\n", prevuid, quotasum); | |
428 | prevuid = uid; | |
429 | quotasum = quota; | |
430 | } else { | |
431 | quotasum += quota; | |
432 | } | |
433 | ## } | |
434 | if (quotasum) | |
435 | fprintf(fd, "%d %d\n", prevuid, quotasum); | |
436 | if (fclose(fd)) { | |
437 | fprintf(stderr, "error closing %s", file); | |
438 | exit(SMS_CCONFIG); | |
439 | } | |
440 | sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device); | |
441 | fd = fopen(file, "w"); | |
442 | if (!fd) { | |
443 | fprintf(stderr, "cannot open %s for output\n", file); | |
444 | exit(SMS_OCONFIG); | |
445 | } | |
446 | ## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype), | |
447 | ## uid = u.#uid, gid = l.#gid) | |
448 | ## where f.phys_id = @id and f.owner = u.users_id and | |
449 | ## f.owners = l.list_id and f.createflg != 0 { | |
450 | fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype); | |
451 | ## } | |
452 | if (fclose(fd)) { | |
453 | fprintf(stderr, "error closing %s", file); | |
454 | exit(SMS_CCONFIG); | |
455 | } | |
456 | } | |
457 | sq_destroy(sq); | |
458 | } | |
459 | return(1); | |
460 | ##} |