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