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