]>
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> |
2ce085d2 | 12 | #include <moira.h> |
13 | #include <moira_site.h> | |
6efe4fbf | 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]); | |
2ce085d2 | 37 | exit(MR_ARGS); |
6efe4fbf | 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) | |
2ce085d2 | 54 | exit(MR_NO_CHANGE); |
3548eee6 | 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)) |
2ce085d2 | 61 | exit(MR_TAR_FAIL); |
6efe4fbf | 62 | } |
63 | ||
2ce085d2 | 64 | exit(MR_SUCCESS); |
6efe4fbf | 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: | |
2ce085d2 | 83 | ingres_errno = MR_DEADLOCK; |
969cfb2b | 84 | break; |
85 | default: | |
2ce085d2 | 86 | ingres_errno = MR_INGRES_ERR; |
969cfb2b | 87 | } |
2ce085d2 | 88 | com_err(whoami, MR_INGRES_ERR, " code %d\n", *num); |
969cfb2b | 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 | |
64e7c8c7 | 105 | ## retrieve (machname = machine.name, listname = s.value3) |
106 | ## where machine.mach_id = s.mach_id and s.service = "NFS" | |
107 | ## and s.enable != 0 { | |
108 | sq_save_unique_string(machs, strsave(strtrim(machname))); | |
109 | sq_save_unique_string(lists, strsave(strtrim(listname))); | |
6efe4fbf | 110 | ## } |
111 | ||
3548eee6 | 112 | changed = do_lists(lists); |
113 | changed += do_machs(machs); | |
114 | return(changed); | |
6efe4fbf | 115 | ##} |
116 | ||
117 | ||
3548eee6 | 118 | /* Make all of the credentials lists that will be needed. Returns 0 if |
119 | * no files were actually changed */ | |
120 | ||
121 | int do_lists(lists) | |
6efe4fbf | 122 | struct save_queue *lists; |
123 | ##{ | |
3548eee6 | 124 | char file[64], *u; |
125 | struct hash *users, *do_everyone(); | |
126 | struct stat sb; | |
6efe4fbf | 127 | FILE *fd; |
3548eee6 | 128 | ## char *listname, *lsname, lname[33], uname[9], *filetime; |
129 | ## int uid, id, flag1, flag2, flag3, flag4; | |
6efe4fbf | 130 | |
185f76ce | 131 | sprintf(file, "%s/list-", nfs_dir); |
3548eee6 | 132 | if (stat(file, &sb) == 0) { |
133 | filetime = ingres_date_and_time(sb.st_mtime); | |
134 | ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime))) | |
135 | ## where tblstats.table = "users" | |
136 | ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime))) | |
137 | ## where tblstats.table = "list" | |
138 | ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime))) | |
330c8868 | 139 | ## where tblstats.table = "imembers" |
3548eee6 | 140 | ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime))) |
141 | ## where tblstats.table = "serverhosts" | |
142 | if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) { | |
143 | fprintf(stderr, "The lists do not need to be rebuilt.\n"); | |
144 | return(0); | |
145 | } | |
146 | } | |
147 | ||
1daee9f8 | 148 | ## begin transaction |
149 | /* get locks */ | |
150 | ## retrieve (lname = list.modtime) where list.list_id = 0 | |
151 | ## retrieve (lname = users.modtime) where users.users_id = 0 | |
152 | ||
3548eee6 | 153 | /* build the list of everyone, and store it in a file whose name |
154 | * corresponds to the empty list. | |
155 | */ | |
6efe4fbf | 156 | users = do_everyone(); |
157 | ||
3548eee6 | 158 | fprintf(stderr, "Building specific lists\n"); |
159 | /* now do each of the lists used by an NFS server */ | |
6efe4fbf | 160 | ## range of l is list |
161 | ## range of l1 is list | |
330c8868 | 162 | ## range of m is imembers |
6efe4fbf | 163 | ## range of u is users |
164 | while (sq_get_data(lists, &listname)) { | |
165 | if (strlen(listname) == 0) | |
166 | continue; | |
185f76ce | 167 | sprintf(file, "%s/list-%s", nfs_dir, listname); |
6efe4fbf | 168 | fd = fopen(file, "w"); |
169 | if (!fd) { | |
170 | fprintf(stderr, "cannot open %s for output\n", file); | |
2ce085d2 | 171 | exit(MR_OCONFIG); |
6efe4fbf | 172 | } |
330c8868 | 173 | ## repeat retrieve (id = m.member_id) |
44a0936c | 174 | ## where m.list_id = l1.list_id and l1.name = @listname and |
330c8868 | 175 | ## m.member_type = "USER" { |
176 | if (u = hash_lookup(users, id)) | |
177 | fprintf(fd, "%s\n", u); | |
178 | ## } | |
179 | if (fclose(fd)) { | |
6efe4fbf | 180 | fprintf(stderr, "error closing %s\n", file); |
2ce085d2 | 181 | exit(MR_CCONFIG); |
6efe4fbf | 182 | } |
183 | } | |
1daee9f8 | 184 | /* don't free here either |
6efe4fbf | 185 | sq_destroy(lists); |
1daee9f8 | 186 | */ |
187 | ## end transaction | |
3548eee6 | 188 | return(1); |
6efe4fbf | 189 | ##} |
190 | ||
191 | ||
3548eee6 | 192 | /* Build the list of everybody. */ |
193 | struct grp { | |
194 | struct grp *next; | |
64e7c8c7 | 195 | char *lid; |
3548eee6 | 196 | }; |
197 | struct user { | |
198 | char name[9]; | |
d3c7c2f9 | 199 | int uid; |
3548eee6 | 200 | struct grp *lists; |
201 | }; | |
202 | ||
203 | struct hash *do_everyone() | |
6efe4fbf | 204 | ##{ |
3548eee6 | 205 | char buf[BUFSIZ], *l; |
206 | struct hash *groups, *users; | |
207 | struct user *u; | |
208 | struct grp *g; | |
209 | struct bucket *b, **p; | |
6efe4fbf | 210 | ## char name[33]; |
211 | ## int gid, id, lid, maxid, uid; | |
212 | FILE *fd; | |
969cfb2b | 213 | int i; |
6efe4fbf | 214 | struct save_queue *sq; |
215 | ||
3548eee6 | 216 | fprintf(stderr, "Building the list of everybody\n"); |
185f76ce | 217 | sprintf(buf, "%s/list-", nfs_dir); |
6efe4fbf | 218 | fd = fopen(buf, "w"); |
219 | if (!fd) { | |
220 | fprintf(stderr, "cannot open %s for output\n", buf); | |
2ce085d2 | 221 | exit(MR_OCONFIG); |
6efe4fbf | 222 | } |
223 | ||
224 | /* make space for group list */ | |
3548eee6 | 225 | groups = create_hash(15000); |
6efe4fbf | 226 | |
227 | /* retrieve simple groups */ | |
3548eee6 | 228 | ## range of l is list |
6efe4fbf | 229 | ## retrieve (gid = l.#gid, lid = l.list_id) |
3548eee6 | 230 | ## where l.group != 0 and l.active != 0 { |
231 | sprintf(buf, ":%d", gid); | |
232 | hash_store(groups, lid, strsave(buf)); | |
6efe4fbf | 233 | ## } |
234 | ||
3548eee6 | 235 | /* now do grplists */ |
236 | users = create_hash(10000); | |
6efe4fbf | 237 | ## range of u is users |
d3c7c2f9 | 238 | ## retrieve (id = u.users_id, name = u.login, uid = u.#uid) |
239 | ## where u.status = 1 { | |
3548eee6 | 240 | u = (struct user *) malloc(sizeof(struct user)); |
241 | strcpy(u->name, strtrim(name)); | |
d3c7c2f9 | 242 | u->uid = uid; |
3548eee6 | 243 | u->lists = NULL; |
244 | hash_store(users, id, u); | |
245 | ## } | |
6efe4fbf | 246 | |
330c8868 | 247 | ## range of m is imembers |
248 | ## retrieve (lid = m.list_id, id = m.member_id) where m.member_type = "USER" { | |
969cfb2b | 249 | if ((u = (struct user *) hash_lookup(users, id)) && |
64e7c8c7 | 250 | ((l = hash_lookup(groups, lid)) != NULL)) { |
3548eee6 | 251 | g = (struct grp *) malloc(sizeof(struct grp)); |
252 | g->next = u->lists; | |
253 | u->lists = g; | |
64e7c8c7 | 254 | g->lid = l; |
3548eee6 | 255 | } |
6efe4fbf | 256 | ## } |
3548eee6 | 257 | |
258 | for (p = &(users->data[users->size - 1]); p >= users->data; p--) { | |
259 | for (b = *p; b; b = b->next) { | |
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) | |
64e7c8c7 | 263 | strcat(buf, g->lid); |
3548eee6 | 264 | b->data = strsave(buf); |
265 | fprintf(fd, "%s\n", buf); | |
266 | } | |
6efe4fbf | 267 | } |
3548eee6 | 268 | |
6efe4fbf | 269 | fclose(fd); |
6efe4fbf | 270 | free(groups); |
271 | return(users); | |
272 | ##} | |
273 | ||
274 | ||
3548eee6 | 275 | /* Now do each of the servers, linking the credentials list file and |
276 | * compiling the quota and dirs files. | |
277 | */ | |
278 | ||
279 | int do_machs(machs) | |
6efe4fbf | 280 | struct save_queue *machs; |
281 | ##{ | |
64e7c8c7 | 282 | ## char *machname, listname[33], dev[33], dir[81], fstype[9]; |
3548eee6 | 283 | ## int uid, quota, id, gid, flag1, flag2, flag3, flag4; |
6efe4fbf | 284 | char file[64], f1[64], f2[64], *cp, *index(); |
64e7c8c7 | 285 | int prevuid, quotasum, olddev, oldmach; |
6efe4fbf | 286 | FILE *fd; |
64e7c8c7 | 287 | struct hash *machines; |
3548eee6 | 288 | |
289 | fprintf(stderr, "Building machine files\n"); | |
290 | ||
6efe4fbf | 291 | ## range of s is serverhosts |
292 | ## range of m is machine | |
293 | ## range of n is nfsphys | |
79a8f3a4 | 294 | ## range of q is #quota |
6efe4fbf | 295 | ## range of f is filesys |
296 | ## range of u is users | |
297 | ## range of l is list | |
140e3035 | 298 | |
64e7c8c7 | 299 | /* acquire locks on machines & filesystems */ |
140e3035 | 300 | ## begin transaction |
301 | ## retrieve (listname = u.modtime) where u.users_id = 0 | |
140e3035 | 302 | ## retrieve (listname = m.modtime) where m.mach_id = 0 |
303 | ## retrieve (listname = f.modtime) where f.filsys_id = 0 | |
304 | ||
64e7c8c7 | 305 | machines = create_hash(100); |
6efe4fbf | 306 | while (sq_get_data(machs, &machname)) { |
64e7c8c7 | 307 | ## repeat retrieve (listname = s.value3, id = m.mach_id) |
6efe4fbf | 308 | ## where s.mach_id = m.mach_id and m.name = @machname |
3548eee6 | 309 | strtrim(machname); |
64e7c8c7 | 310 | sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname)); |
185f76ce | 311 | sprintf(f2, "%s/%s.cred", nfs_dir, machname); |
6efe4fbf | 312 | unlink(f2); /* ignore errors on this unlink */ |
313 | if (link(f1, f2)) { | |
314 | fprintf(stderr, "Cannot link %s to %s\n", f1, f2); | |
2ce085d2 | 315 | exit(MR_OCONFIG); |
6efe4fbf | 316 | } |
64e7c8c7 | 317 | hash_store(machines, id, machname); |
318 | } | |
319 | ||
320 | olddev = oldmach = -1; | |
321 | fd = stdin; | |
322 | ## retrieve (quota = q.#quota, uid = u.#uid, flag1 = q.phys_id, | |
323 | ## dev = n.device, flag2 = n.mach_id) | |
79a8f3a4 | 324 | ## where u.users_id = q.entity_id and n.nfsphys_id = q.phys_id and |
325 | ## q.phys_id != 0 and n.status < 16 and q.type = "USER" | |
64e7c8c7 | 326 | ## sort by #flag2, #flag1, #uid { |
327 | if (flag1 != olddev || flag2 != oldmach) { | |
328 | fclose(fd); | |
329 | olddev = flag1; | |
330 | oldmach = flag2; | |
331 | while (cp = index(dev, '/')) *cp = '@'; | |
332 | sprintf(file, "%s/%s.%s.quotas", nfs_dir, | |
333 | hash_lookup(machines, flag2), strtrim(dev)); | |
334 | fd = fopen(file, "w"); | |
335 | if (!fd) { | |
336 | fprintf(stderr, "cannot open %s for output\n", file); | |
2ce085d2 | 337 | exit(MR_OCONFIG); |
64e7c8c7 | 338 | } |
339 | prevuid = -1; | |
340 | quotasum = 0; | |
341 | } | |
342 | if (uid != prevuid) { | |
343 | if (quotasum) | |
344 | fprintf(fd, "%d %d\n", prevuid, quotasum); | |
345 | prevuid = uid; | |
346 | quotasum = quota; | |
347 | } else { | |
348 | quotasum += quota; | |
349 | } | |
350 | ## } | |
351 | if (quotasum) | |
352 | fprintf(fd, "%d %d\n", prevuid, quotasum); | |
79a8f3a4 | 353 | if (fd != stdin && fclose(fd)) { |
354 | fprintf(stderr, "error closing %s", file); | |
355 | exit(MR_CCONFIG); | |
356 | } | |
357 | ||
358 | olddev = oldmach = -1; | |
359 | fd = stdin; | |
360 | ## retrieve (quota = q.#quota, gid = l.#gid, flag1 = q.phys_id, | |
361 | ## dev = n.device, flag2 = n.mach_id, flag3 = n.status) | |
362 | ## where l.list_id = q.entity_id and n.nfsphys_id = q.phys_id and | |
363 | ## q.phys_id != 0 and n.status > 15 and q.type = "GROUP" | |
364 | ## sort by #flag2, #flag1, #gid { | |
365 | if (flag1 != olddev || flag2 != oldmach) { | |
366 | fclose(fd); | |
367 | olddev = flag1; | |
368 | oldmach = flag2; | |
369 | while (cp = index(dev, '/')) *cp = '@'; | |
370 | sprintf(file, "%s/%s.%s.quotas", nfs_dir, | |
371 | hash_lookup(machines, flag2), strtrim(dev)); | |
372 | fd = fopen(file, "w"); | |
373 | if (!fd) { | |
374 | fprintf(stderr, "cannot open %s for output\n", file); | |
375 | exit(MR_OCONFIG); | |
376 | } | |
377 | prevuid = -1; | |
378 | quotasum = 0; | |
379 | } | |
380 | if (gid != prevuid) { | |
381 | if (quotasum) | |
382 | fprintf(fd, "%d %d\n", prevuid, quotasum); | |
383 | prevuid = gid; | |
384 | quotasum = quota; | |
385 | } else { | |
386 | quotasum += quota; | |
387 | } | |
388 | ## } | |
389 | if (quotasum) | |
390 | fprintf(fd, "%d %d\n", prevuid, quotasum); | |
391 | if (fd != stdin && fclose(fd)) { | |
64e7c8c7 | 392 | fprintf(stderr, "error closing %s", file); |
2ce085d2 | 393 | exit(MR_CCONFIG); |
64e7c8c7 | 394 | } |
395 | ||
396 | olddev = oldmach = -1; | |
397 | ## retrieve (dir = f.#name, fstype = f.lockertype, uid = u.#uid, | |
398 | ## gid = l.#gid, flag1 = f.phys_id, flag2 = f.mach_id, | |
399 | ## dev = n.device) | |
400 | ## where u.users_id = f.owner and l.list_id = f.owners and | |
401 | ## f.createflg != 0 and f.phys_id != 0 and f.type = "NFS" and | |
402 | ## f.phys_id = n.nfsphys_id | |
403 | ## sort by #flag2, #flag1 { | |
404 | if (flag1 != olddev || flag2 != oldmach) { | |
405 | fclose(fd); | |
406 | olddev = flag1; | |
407 | oldmach = flag2; | |
408 | while (cp = index(dev, '/')) *cp = '@'; | |
409 | sprintf(file, "%s/%s.%s.dirs", nfs_dir, | |
410 | hash_lookup(machines, flag2), strtrim(dev)); | |
411 | fd = fopen(file, "w"); | |
412 | if (!fd) { | |
413 | fprintf(stderr, "cannot open %s for output\n", file); | |
2ce085d2 | 414 | exit(MR_OCONFIG); |
64e7c8c7 | 415 | } |
416 | } | |
417 | fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype)); | |
418 | ## } | |
419 | if (fclose(fd)) { | |
420 | fprintf(stderr, "error closing %s", file); | |
2ce085d2 | 421 | exit(MR_CCONFIG); |
6efe4fbf | 422 | } |
1daee9f8 | 423 | ## end transaction |
3548eee6 | 424 | return(1); |
6efe4fbf | 425 | ##} |