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_printers();
66 changed += do_service();
67 changed += do_groups();
72 fprintf(stderr, "No files updated.\n");
73 if (argc == 2 && stat(argv[1], &sb) == 0)
78 fprintf(stderr, "Building tar file.\n");
79 sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
89 * ingerr: (supposedly) called when Ingres indicates an error.
90 * I have not yet been able to get this to work to intercept a
91 * database open error.
93 #define INGRES_DEADLOCK 4700
95 static int ingerr(num)
102 case INGRES_DEADLOCK:
103 ingres_errno = SMS_DEADLOCK;
106 ingres_errno = SMS_INGRES_ERR;
108 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
109 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
122 machines = create_hash(1000);
123 ## retrieve (name = machine.#name, id = machine.mach_id) {
124 hash_store(machines, id, strsave(strtrim(name)));
131 FILE *pout, *uout, *bout;
132 char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
133 struct stat psb, usb, bsb;
137 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
138 ## char nn[17], ptype[9];
139 ## int uid, flag1, flag2, id, pid;
141 sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
142 sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
143 sprintf(boutf, "%s/pobox.db", HESIOD_DIR);
145 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
146 stat(boutf, &bsb) == 0) {
147 ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
148 filetime = ingres_date_and_time(ftime);
149 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
150 ## where tblstats.table = "users"
151 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
152 ## where tblstats.table = "machine"
153 if (flag1 < 0 && flag2 < 0) {
154 fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
159 sprintf(poutft, "%s~", poutf);
160 pout = fopen(poutft, "w");
162 perror("cannot open passwd.db~ for write");
165 sprintf(uoutft, "%s~", uoutf);
166 uout = fopen(uoutft, "w");
168 perror("cannot open uid.db~ for write");
171 sprintf(boutft, "%s~", boutf);
172 bout = fopen(boutft, "w");
174 perror("cannot open pobox.db for write");
178 fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
181 users = create_hash(10000);
182 ## range of u is users
183 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
184 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
185 ## op = u.office_phone, hp = u.home_phone, id = u.users_id,
186 ## pid = u.pop_id, ptype = u.potype)
187 ## where u.status = 1 {
195 u = (struct user *) malloc(sizeof(struct user));
196 strcpy(u->name, login);
198 hash_store(users, id, u);
199 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
200 login, login, uid, fullname, nn, oa, op, hp, login, shell);
201 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
202 if (ptype[0] == 'P' && (mach = hash_lookup(machines, pid))) {
203 fprintf(bout, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
208 if (fclose(pout) || fclose(uout) || fclose(bout)) {
209 fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
219 /************************************************************************
220 * WARNING: this routine mallocs a large amount of memory which it never frees.
221 * This is considered OK only because main has been arranged to call this
222 * last, so the process will exit shortly after we return.
227 FILE *iout, *gout, *lout;
228 char ioutf[64], goutf[64], loutf[64], buf[2048], *l;
230 register struct bucket *b, **p;
233 struct stat isb, gsb, lsb;
235 register struct save_queue *sq;
236 struct save_queue *sq_create();
238 ## char name[33], *filetime;
239 ## int gid, id, lid, flag1, flag2, flag3, maxid;
242 sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
243 sprintf(goutf, "%s/group.db", HESIOD_DIR);
244 sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
246 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
247 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
248 filetime = ingres_date_and_time(ftime);
249 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
250 ## where tblstats.table = "users"
251 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
252 ## where tblstats.table = "list"
253 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
254 ## where tblstats.table = "imembers"
255 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
256 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
261 sprintf(buf, "%s~", ioutf);
262 iout = fopen(buf, "w");
264 perror("cannot open gid.db for write");
267 sprintf(buf, "%s~", goutf);
268 gout = fopen(buf, "w");
270 perror("cannot open group.db for write");
273 sprintf(buf, "%s~", loutf);
274 lout = fopen(buf, "w");
276 perror("cannot open grplist.db for write");
280 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
282 /* make space for group list */
283 groups = create_hash(15000);
285 /* retrieve simple groups */
286 ## range of l is list
287 ## range of m is imembers
289 /* get lock records */
290 ## retrieve (name = l.modtime) where l.list_id = 0
291 ## retrieve (name = users.modtime) where users.users_id = 0
293 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
294 ## where l.group != 0 and l.active != 0 {
296 sprintf(buf, "%s:%d", name, gid);
297 hash_store(groups, lid, strsave(buf));
298 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
299 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
306 /* now do grplists */
308 users = create_hash(10000);
309 ## range of u is users
310 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
311 u = (struct user *) malloc(sizeof(struct user));
312 strcpy(u->name, strtrim(name));
314 hash_store(users, id, u);
318 ## repeat retrieve (lid = m.list_id, id = m.member_id)
319 ## where m.member_type = "USER" {
320 if ((u = (struct user *) hash_lookup(users, id)) &&
321 (hash_lookup(groups, lid) != NULL)) {
322 g = (struct grp *) malloc(sizeof(struct grp));
330 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
331 for (b = *p; b; b = b->next) {
333 sprintf(buf, "%s.grplist\tHS UNSPECA \"",
334 ((struct user *)b->data)->name);
335 for (g = ((struct user *)b->data)->lists; g; g = g->next)
336 if (l = hash_lookup(groups, g->id)) {
349 /* This is commented out because it takes on the order of 30 minutes to
350 * run. Instead, we never free the memory, but the program will exit
356 for (p = &groups[maxid-1]; p >= groups; p--)
363 if (fclose(iout) || fclose(gout) || fclose(lout)) {
364 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
377 char outf[64], outft[64], *mach, machbuf[128], *group;
381 struct save_queue *sq, *sq_create();
382 ## char name[33], type[9], loc[81], access[2], mount[33], trans[257];
383 ## char *filetime, comments[65];
384 ## int flag1, flag2, flag3, flag4, id, fid;
386 sprintf(outf, "%s/filsys.db", HESIOD_DIR);
388 if (stat(outf, &sb) == 0) {
390 filetime = ingres_date_and_time(ftime);
391 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
392 ## where tblstats.table = "filesys"
393 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
394 ## where tblstats.table = "machine"
395 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
396 ## where tblstats.table = "alias"
397 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
398 ## where tblstats.table = "fsgroup"
399 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
400 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
405 sprintf(outft, "%s~", outf);
406 out = fopen(outft, "w");
408 perror("cannot open filsys.db for write");
412 fprintf(stderr, "Building filsys.db\n");
416 ## range of f is filesys
417 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
418 ## access = f.#access, mount = f.#mount, comments = f.#comments,
419 ## fid = f.filsys_id) {
422 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
426 if (mach = hash_lookup(machines, id)) {
427 for (p = machbuf; *mach && *mach != '.'; mach++) {
429 *p++ = tolower(*mach);
434 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
435 name, type, loc, machbuf, access, mount);
437 } else if (!strcmp(type, "AFS")) {
441 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
442 name, loc, access, mount);
443 } else if (!strcmp(type, "ERR")) {
445 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
447 } else if (!strcmp(type, "FSGROUP")) {
448 sprintf(trans, "%s:%d", name, fid);
449 sq_save_data(sq, strsave(trans));
453 ## range of g is fsgroup
454 while (sq_get_data(sq, &group)) {
455 fid = atoi(index(group, ':')+1);
456 *index(group, ':') = 0;
457 ## retrieve (type = f.#type, loc = f.#name, id = f.mach_id,
458 ## access = f.#access, mount = f.#mount, comments = f.#comments,
459 ## name = f.#label, trans = g.#key)
460 ## where f.filsys_id = g.filsys_id and g.group_id = fid
461 ## sort by #trans, #name {
463 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
467 if (mach = hash_lookup(machines, id)) {
468 for (p = machbuf; *mach && *mach != '.'; mach++) {
470 *p++ = tolower(*mach);
475 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
476 group, type, loc, machbuf, access, mount);
478 } else if (!strcmp(type, "AFS")) {
482 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
483 group, loc, access, mount);
484 } else if (!strcmp(type, "ERR")) {
486 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
494 ## range of a is alias
495 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
498 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
502 fprintf(stderr, "Unsuccessful close of filsys.db\n");
511 * Modified from sys/types.h:
513 int setsize; /* = howmany(setbits, NSETBITS) */
515 typedef long set_mask;
516 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
518 #define howmany(x, y) (((x)+((y)-1))/(y))
521 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
522 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
523 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
524 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
525 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
526 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
532 char outf[64], outft[64], *mach, machbuf[33], *p;
535 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
536 ## char name[33], label[17], data[33], *filetime;
537 set_mask **machs, *ms, *ps;
539 sprintf(outf, "%s/cluster.db", HESIOD_DIR);
541 if (stat(outf, &sb) == 0) {
543 filetime = ingres_date_and_time(ftime);
544 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
545 ## where tblstats.table = "cluster"
546 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
547 ## where tblstats.table = "machine"
548 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
549 ## where tblstats.table = "mcmap"
550 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
551 ## where tblstats.table = "svc"
552 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
553 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
558 sprintf(outft, "%s~", outf);
559 out = fopen(outft, "w");
561 perror("cannot open cluster.db for write");
565 fprintf(stderr, "Building cluster.db\n");
568 ## range of c is cluster
570 ## retrieve (maxclu = max(c.clu_id))
572 setsize = howmany(maxclu, NSETBITS);
573 ## range of m is machine
574 ## retrieve (maxmach = max(m.mach_id))
576 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
577 bzero(machs, (maxmach + 1) * sizeof(int));
579 ## range of p is mcmap
580 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
581 if (!(ms = machs[mid])) {
582 ms = machs[mid] = SET_CREATE();
589 for (mid = 1; mid < maxmach; mid++) {
593 for (cid = 1; cid < maxclu; cid++) {
594 if (SET_ISSET(cid, ms)) {
595 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
596 ## where d.clu_id = @cid {
600 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
606 if (mach = hash_lookup(machines, mid)) {
607 for (p = machbuf; *mach && *mach != '.'; mach++)
610 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
613 for (id = mid + 1; id < maxmach; id++) {
614 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
617 if (mach = hash_lookup(machines, id)) {
618 for (p = machbuf; *mach && *mach != '.'; mach++)
621 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
630 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
631 ## where c.clu_id = d.clu_id {
635 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
642 fprintf(stderr, "Unsuccessful close of cluster.db\n");
653 char outf[64], outft[64];
656 ## char name[17], rp[17], sd[33], rm[33], *filetime;
659 sprintf(outf, "%s/printcap.db", HESIOD_DIR);
661 if (stat(outf, &sb) == 0) {
663 filetime = ingres_date_and_time(ftime);
664 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
665 ## where tblstats.table = "printcap"
667 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
672 sprintf(outft, "%s~", outf);
673 out = fopen(outft, "w");
675 perror("cannot open printcap.db for write");
679 fprintf(stderr, "Building printcap.db\n");
681 ## range of p is printcap
682 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
683 ## where machine.mach_id = p.mach_id {
688 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
689 name, name, rp, rm, sd);
693 fprintf(stderr, "Unsuccessful close of pcap.db\n");
703 FILE *out, *old, *new;
704 char outf[64], outft[64], *mach;
708 ## char service[17], *filetime;
709 ## int port, flag1, flag2, id;
711 sprintf(outf, "%s/sloc.db", HESIOD_DIR);
713 if (stat(outf, &sb) == 0) {
715 filetime = ingres_date_and_time(ftime);
716 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
717 ## where tblstats.table = "serverhosts"
718 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
719 ## where tblstats.table = "machine"
720 if (flag1 < 0 && flag2 < 0) {
721 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
726 sprintf(outft, "%s~", outf);
727 out = fopen(outft, "w");
729 perror("cannot open sloc.db for write");
733 fprintf(stderr, "Building sloc.db\n");
736 ## range of s is serverhosts
737 ## retrieve (service = s.#service, id = s.mach_id)
738 ## sort by #service {
740 if (mach = hash_lookup(machines, id))
741 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
745 fprintf(stderr, "Unsuccessful close of sloc.db\n");
756 char outf[64], outft[64];
759 ## char mach[33], service[33], protocol[9], altserv[129], *filetime;
760 ## int port, flag1, flag2;
762 sprintf(outf, "%s/service.db", HESIOD_DIR);
764 if (stat(outf, &sb) == 0) {
766 filetime = ingres_date_and_time(ftime);
767 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
768 ## where tblstats.table = "services"
770 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
775 sprintf(outft, "%s~", outf);
776 out = fopen(outft, "w");
778 perror("cannot open service.db for write");
782 fprintf(stderr, "Building service.db\n");
784 ## range of s is services
785 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
789 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
790 service, service, protocol, port);
793 ## range of a is alias
794 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
797 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
801 fprintf(stderr, "Unsuccessful close of service.db\n");