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