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