3 * This generates the zone files necessary to load a hesiod server.
4 * The following zones are generated: passwd, uid, pobox, group,
5 * grplist, gid, filsys, cluster, pcap, sloc, service.
7 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
8 * For copying and distribution information, please see the file
12 #include <mit-copyright.h>
16 #include <sys/types.h>
22 #define HESIOD_DIR "/u1/sms/dcm/hesiod"
24 #define min(x,y) ((x) < (y) ? (x) : (y))
25 struct hash *machines = NULL;
26 struct hash *users = NULL;
27 char *whoami = "hesiod.gen";
38 char *malloc(), *strsave();
39 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
51 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
56 initialize_sms_error_table ();
59 ## set lockmode session where level = table
61 changed = do_passwd();
62 changed += do_filsys();
63 changed += do_cluster();
64 changed += do_printcap();
65 changed += do_palladium();
67 changed += do_service();
68 changed += do_groups();
73 fprintf(stderr, "No files updated.\n");
74 if (argc == 2 && stat(argv[1], &sb) == 0)
79 fprintf(stderr, "Building tar file.\n");
80 sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
90 * ingerr: (supposedly) called when Ingres indicates an error.
91 * I have not yet been able to get this to work to intercept a
92 * database open error.
94 #define INGRES_DEADLOCK 4700
96 static int ingerr(num)
103 case INGRES_DEADLOCK:
104 ingres_errno = SMS_DEADLOCK;
107 ingres_errno = SMS_INGRES_ERR;
109 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
110 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
123 machines = create_hash(1000);
124 ## retrieve (name = machine.#name, id = machine.mach_id) {
125 hash_store(machines, id, strsave(strtrim(name)));
132 FILE *pout, *uout, *bout;
133 char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
134 struct stat psb, usb, bsb;
138 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
139 ## char nn[17], ptype[9];
140 ## int uid, flag1, flag2, id, pid;
142 sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
143 sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
144 sprintf(boutf, "%s/pobox.db", HESIOD_DIR);
146 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
147 stat(boutf, &bsb) == 0) {
148 ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
149 filetime = ingres_date_and_time(ftime);
150 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
151 ## where tblstats.table = "users"
152 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
153 ## where tblstats.table = "machine"
154 if (flag1 < 0 && flag2 < 0) {
155 fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
160 sprintf(poutft, "%s~", poutf);
161 pout = fopen(poutft, "w");
163 perror("cannot open passwd.db~ for write");
166 sprintf(uoutft, "%s~", uoutf);
167 uout = fopen(uoutft, "w");
169 perror("cannot open uid.db~ for write");
172 sprintf(boutft, "%s~", boutf);
173 bout = fopen(boutft, "w");
175 perror("cannot open pobox.db for write");
179 fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
182 users = create_hash(10000);
183 ## range of u is users
184 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
185 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
186 ## op = u.office_phone, hp = u.home_phone, id = u.users_id,
187 ## pid = u.pop_id, ptype = u.potype)
188 ## where u.status = 1 {
196 u = (struct user *) malloc(sizeof(struct user));
197 strcpy(u->name, login);
199 hash_store(users, id, u);
200 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
201 login, login, uid, fullname, nn, oa, op, hp, login, shell);
202 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
203 if (ptype[0] == 'P' && (mach = hash_lookup(machines, pid))) {
204 fprintf(bout, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
209 if (fclose(pout) || fclose(uout) || fclose(bout)) {
210 fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
220 /************************************************************************
221 * WARNING: this routine mallocs a large amount of memory which it never frees.
222 * This is considered OK only because main has been arranged to call this
223 * last, so the process will exit shortly after we return.
228 FILE *iout, *gout, *lout;
229 char ioutf[64], goutf[64], loutf[64], buf[2048], *l;
231 register struct bucket *b, **p;
234 struct stat isb, gsb, lsb;
236 register struct save_queue *sq;
237 struct save_queue *sq_create();
239 ## char name[33], *filetime;
240 ## int gid, id, lid, flag1, flag2, flag3, maxid;
243 sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
244 sprintf(goutf, "%s/group.db", HESIOD_DIR);
245 sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
247 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
248 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
249 filetime = ingres_date_and_time(ftime);
250 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
251 ## where tblstats.table = "users"
252 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
253 ## where tblstats.table = "list"
254 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
255 ## where tblstats.table = "imembers"
256 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
257 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
262 sprintf(buf, "%s~", ioutf);
263 iout = fopen(buf, "w");
265 perror("cannot open gid.db for write");
268 sprintf(buf, "%s~", goutf);
269 gout = fopen(buf, "w");
271 perror("cannot open group.db for write");
274 sprintf(buf, "%s~", loutf);
275 lout = fopen(buf, "w");
277 perror("cannot open grplist.db for write");
281 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
283 /* make space for group list */
284 groups = create_hash(15000);
286 /* retrieve simple groups */
287 ## range of l is list
288 ## range of m is imembers
290 /* get lock records */
291 ## retrieve (name = l.modtime) where l.list_id = 0
292 ## retrieve (name = users.modtime) where users.users_id = 0
294 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
295 ## where l.group != 0 and l.active != 0 {
297 sprintf(buf, "%s:%d", name, gid);
298 hash_store(groups, lid, strsave(buf));
299 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
300 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
307 /* now do grplists */
309 users = create_hash(10000);
310 ## range of u is users
311 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
312 u = (struct user *) malloc(sizeof(struct user));
313 strcpy(u->name, strtrim(name));
315 hash_store(users, id, u);
319 ## repeat retrieve (lid = m.list_id, id = m.member_id)
320 ## where m.member_type = "USER" {
321 if ((u = (struct user *) hash_lookup(users, id)) &&
322 (hash_lookup(groups, lid) != NULL)) {
323 g = (struct grp *) malloc(sizeof(struct grp));
331 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
332 for (b = *p; b; b = b->next) {
334 sprintf(buf, "%s.grplist\tHS UNSPECA \"",
335 ((struct user *)b->data)->name);
336 for (g = ((struct user *)b->data)->lists; g; g = g->next)
337 if (l = hash_lookup(groups, g->id)) {
350 /* This is commented out because it takes on the order of 30 minutes to
351 * run. Instead, we never free the memory, but the program will exit
357 for (p = &groups[maxid-1]; p >= groups; p--)
364 if (fclose(iout) || fclose(gout) || fclose(lout)) {
365 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
378 char outf[64], outft[64], *mach, machbuf[128], *group;
382 struct save_queue *sq, *sq_create();
383 ## char name[33], type[9], loc[81], access[2], mount[33], trans[257];
384 ## char *filetime, comments[65];
385 ## int flag1, flag2, flag3, flag4, id, fid;
388 sprintf(outf, "%s/filsys.db", HESIOD_DIR);
390 if (stat(outf, &sb) == 0) {
392 filetime = ingres_date_and_time(ftime);
393 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
394 ## where tblstats.table = "filesys"
395 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
396 ## where tblstats.table = "machine"
397 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
398 ## where tblstats.table = "alias"
399 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
400 ## where tblstats.table = "fsgroup"
401 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
402 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
407 sprintf(outft, "%s~", outf);
408 out = fopen(outft, "w");
410 perror("cannot open filsys.db for write");
414 fprintf(stderr, "Building filsys.db\n");
418 ## range of f is filesys
419 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
420 ## access = f.#access, mount = f.#mount, comments = f.#comments,
421 ## fid = f.filsys_id) {
424 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
428 if (mach = hash_lookup(machines, id)) {
429 for (p = machbuf; *mach && *mach != '.'; mach++) {
431 *p++ = tolower(*mach);
436 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
437 name, type, loc, machbuf, access, mount);
439 } else if (!strcmp(type, "AFS")) {
443 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
444 name, loc, access, mount);
445 } else if (!strcmp(type, "ERR")) {
447 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
449 } else if (!strcmp(type, "FSGROUP")) {
450 sprintf(trans, "%s:%d", name, fid);
451 sq_save_data(sq, strsave(trans));
455 ## range of g is fsgroup
456 while (sq_get_data(sq, &group)) {
457 fid = atoi(index(group, ':')+1);
458 *index(group, ':') = 0;
459 ## retrieve (type = f.#type, loc = f.#name, id = f.mach_id,
460 ## access = f.#access, mount = f.#mount, comments = f.#comments,
461 ## name = f.#label, trans = g.#key)
462 ## where f.filsys_id = g.filsys_id and g.group_id = fid
463 ## sort by #trans, #name {
465 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
469 if (mach = hash_lookup(machines, id)) {
470 for (p = machbuf; *mach && *mach != '.'; mach++) {
472 *p++ = tolower(*mach);
477 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
478 group, type, loc, machbuf, access, mount);
480 } else if (!strcmp(type, "AFS")) {
484 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
485 group, loc, access, mount);
486 } else if (!strcmp(type, "ERR")) {
488 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
496 ## range of a is alias
497 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
500 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
504 fprintf(stderr, "Unsuccessful close of filsys.db\n");
513 * Modified from sys/types.h:
515 int setsize; /* = howmany(setbits, NSETBITS) */
517 typedef long set_mask;
518 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
520 #define howmany(x, y) (((x)+((y)-1))/(y))
523 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
524 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
525 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
526 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
527 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
528 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
534 char outf[64], outft[64], *mach, machbuf[33], *p;
537 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
538 ## char name[33], label[17], data[33], *filetime;
539 set_mask **machs, *ms, *ps;
541 sprintf(outf, "%s/cluster.db", HESIOD_DIR);
543 if (stat(outf, &sb) == 0) {
545 filetime = ingres_date_and_time(ftime);
546 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
547 ## where tblstats.table = "cluster"
548 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
549 ## where tblstats.table = "machine"
550 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
551 ## where tblstats.table = "mcmap"
552 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
553 ## where tblstats.table = "svc"
554 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
555 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
560 sprintf(outft, "%s~", outf);
561 out = fopen(outft, "w");
563 perror("cannot open cluster.db for write");
567 fprintf(stderr, "Building cluster.db\n");
570 ## range of c is cluster
572 ## retrieve (maxclu = max(c.clu_id))
574 setsize = howmany(maxclu, NSETBITS);
575 ## range of m is machine
576 ## retrieve (maxmach = max(m.mach_id))
578 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
579 bzero(machs, (maxmach + 1) * sizeof(int));
581 ## range of p is mcmap
582 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
583 if (!(ms = machs[mid])) {
584 ms = machs[mid] = SET_CREATE();
591 for (mid = 1; mid < maxmach; mid++) {
595 for (cid = 1; cid < maxclu; cid++) {
596 if (SET_ISSET(cid, ms)) {
597 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
598 ## where d.clu_id = @cid {
602 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
608 if (mach = hash_lookup(machines, mid)) {
609 for (p = machbuf; *mach && *mach != '.'; mach++)
612 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
615 for (id = mid + 1; id < maxmach; id++) {
616 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
619 if (mach = hash_lookup(machines, id)) {
620 for (p = machbuf; *mach && *mach != '.'; mach++)
623 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
632 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
633 ## where c.clu_id = d.clu_id {
637 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
644 fprintf(stderr, "Unsuccessful close of cluster.db\n");
655 char outf[64], outft[64];
658 ## char name[17], rp[17], sd[33], rm[33], *filetime;
661 sprintf(outf, "%s/printcap.db", HESIOD_DIR);
663 if (stat(outf, &sb) == 0) {
665 filetime = ingres_date_and_time(ftime);
666 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
667 ## where tblstats.table = "printcap"
669 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
674 sprintf(outft, "%s~", outf);
675 out = fopen(outft, "w");
677 perror("cannot open printcap.db for write");
681 fprintf(stderr, "Building printcap.db\n");
683 ## range of p is printcap
684 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
685 ## where machine.mach_id = p.mach_id {
690 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
691 name, name, rp, rm, sd);
695 fprintf(stderr, "Unsuccessful close of pcap.db\n");
706 char outf[64], outft[64];
709 ## char name[33], rm[33], trans[129], *filetime;
710 ## int flag, flag1, ident;
712 sprintf(outf, "%s/palladium.db", HESIOD_DIR);
714 if (stat(outf, &sb) == 0) {
716 filetime = ingres_date_and_time(ftime);
717 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
718 ## where tblstats.table = "palladium"
719 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
720 ## where tblstats.table = "alias"
721 if (flag < 0 && flag1 < 0) {
722 fprintf(stderr, "File palladium.db does not need to be rebuilt.\n");
727 sprintf(outft, "%s~", outf);
728 out = fopen(outft, "w");
730 perror("cannot open palladium.db for write");
734 fprintf(stderr, "Building palladium.db\n");
736 ## range of p is palladium
737 ## retrieve (name = p.#name, ident = p.#ident, rm = machine.#name)
738 ## where machine.mach_id = p.mach_id {
742 "%s.palladium\tHS UNSPECA \"%s %d %s interface directory\"\n",
743 name, rm, ident, name);
745 ## range of a is alias
746 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "PALLADIUM" {
749 fprintf(out, "%s.palladium\tHS UNSPECA \"%s\"\n", name, trans);
753 fprintf(stderr, "Unsuccessful close of palladium.db\n");
763 FILE *out, *old, *new;
764 char outf[64], outft[64], *mach;
768 ## char service[17], *filetime;
769 ## int port, flag1, flag2, id;
771 sprintf(outf, "%s/sloc.db", HESIOD_DIR);
773 if (stat(outf, &sb) == 0) {
775 filetime = ingres_date_and_time(ftime);
776 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
777 ## where tblstats.table = "serverhosts"
778 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
779 ## where tblstats.table = "machine"
780 if (flag1 < 0 && flag2 < 0) {
781 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
786 sprintf(outft, "%s~", outf);
787 out = fopen(outft, "w");
789 perror("cannot open sloc.db for write");
793 fprintf(stderr, "Building sloc.db\n");
796 ## range of s is serverhosts
797 ## retrieve (service = s.#service, id = s.mach_id)
798 ## sort by #service {
800 if (mach = hash_lookup(machines, id))
801 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
805 fprintf(stderr, "Unsuccessful close of sloc.db\n");
816 char outf[64], outft[64];
819 ## char mach[33], service[33], protocol[9], altserv[129], *filetime;
820 ## int port, flag1, flag2;
822 sprintf(outf, "%s/service.db", HESIOD_DIR);
824 if (stat(outf, &sb) == 0) {
826 filetime = ingres_date_and_time(ftime);
827 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
828 ## where tblstats.table = "services"
830 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
835 sprintf(outft, "%s~", outf);
836 out = fopen(outft, "w");
838 perror("cannot open service.db for write");
842 fprintf(stderr, "Building service.db\n");
844 ## range of s is services
845 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
849 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
850 service, service, protocol, port);
853 ## range of a is alias
854 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
857 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
861 fprintf(stderr, "Unsuccessful close of service.db\n");