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>
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 ();
57 sprintf(hesiod_dir, "%s/hesiod", DCM_DIR);
60 ## set lockmode session where level = table
62 changed = do_passwd();
63 changed += do_filsys();
64 changed += do_cluster();
65 changed += do_printcap();
66 changed += do_palladium();
68 changed += do_service();
69 changed += do_groups();
74 fprintf(stderr, "No files updated.\n");
75 if (argc == 2 && stat(argv[1], &sb) == 0)
80 fprintf(stderr, "Building tar file.\n");
81 sprintf(cmd, "cd %s; tar cf %s .", hesiod_dir, argv[1]);
91 * ingerr: (supposedly) called when Ingres indicates an error.
92 * I have not yet been able to get this to work to intercept a
93 * database open error.
95 #define INGRES_DEADLOCK 4700
97 static int ingerr(num)
104 case INGRES_DEADLOCK:
105 ingres_errno = SMS_DEADLOCK;
108 ingres_errno = SMS_INGRES_ERR;
110 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
111 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
124 machines = create_hash(1000);
125 ## retrieve (name = machine.#name, id = machine.mach_id) {
126 hash_store(machines, id, strsave(strtrim(name)));
133 FILE *pout, *uout, *bout;
134 char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
135 struct stat psb, usb, bsb;
139 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
140 ## char nn[17], ptype[9];
141 ## int uid, flag1, flag2, id, pid;
143 sprintf(poutf, "%s/passwd.db", hesiod_dir);
144 sprintf(uoutf, "%s/uid.db", hesiod_dir);
145 sprintf(boutf, "%s/pobox.db", hesiod_dir);
147 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
148 stat(boutf, &bsb) == 0) {
149 ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
150 filetime = ingres_date_and_time(ftime);
151 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
152 ## where tblstats.table = "users"
153 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
154 ## where tblstats.table = "machine"
155 if (flag1 < 0 && flag2 < 0) {
156 fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
161 sprintf(poutft, "%s~", poutf);
162 pout = fopen(poutft, "w");
164 perror("cannot open passwd.db~ for write");
167 sprintf(uoutft, "%s~", uoutf);
168 uout = fopen(uoutft, "w");
170 perror("cannot open uid.db~ for write");
173 sprintf(boutft, "%s~", boutf);
174 bout = fopen(boutft, "w");
176 perror("cannot open pobox.db for write");
180 fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
183 users = create_hash(10000);
184 ## range of u is users
185 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
186 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
187 ## op = u.office_phone, hp = u.home_phone, id = u.users_id,
188 ## pid = u.pop_id, ptype = u.potype)
189 ## where u.status = 1 {
197 u = (struct user *) malloc(sizeof(struct user));
198 strcpy(u->name, login);
200 hash_store(users, id, u);
201 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
202 login, login, uid, fullname, nn, oa, op, hp, login, shell);
203 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
204 if (ptype[0] == 'P' && (mach = hash_lookup(machines, pid))) {
205 fprintf(bout, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
210 if (fclose(pout) || fclose(uout) || fclose(bout)) {
211 fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
221 /************************************************************************
222 * WARNING: this routine mallocs a large amount of memory which it never frees.
223 * This is considered OK only because main has been arranged to call this
224 * last, so the process will exit shortly after we return.
229 FILE *iout, *gout, *lout;
230 char ioutf[64], goutf[64], loutf[64], buf[2048], *l;
232 register struct bucket *b, **p;
235 struct stat isb, gsb, lsb;
237 register struct save_queue *sq;
238 struct save_queue *sq_create();
240 ## char name[33], *filetime;
241 ## int gid, id, lid, flag1, flag2, flag3, maxid;
244 sprintf(ioutf, "%s/gid.db", hesiod_dir);
245 sprintf(goutf, "%s/group.db", hesiod_dir);
246 sprintf(loutf, "%s/grplist.db", hesiod_dir);
248 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
249 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
250 filetime = ingres_date_and_time(ftime);
251 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
252 ## where tblstats.table = "users"
253 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
254 ## where tblstats.table = "list"
255 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
256 ## where tblstats.table = "imembers"
257 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
258 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
263 sprintf(buf, "%s~", ioutf);
264 iout = fopen(buf, "w");
266 perror("cannot open gid.db for write");
269 sprintf(buf, "%s~", goutf);
270 gout = fopen(buf, "w");
272 perror("cannot open group.db for write");
275 sprintf(buf, "%s~", loutf);
276 lout = fopen(buf, "w");
278 perror("cannot open grplist.db for write");
282 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
284 /* make space for group list */
285 groups = create_hash(15000);
287 /* retrieve simple groups */
288 ## range of l is list
289 ## range of m is imembers
291 /* get lock records */
292 ## retrieve (name = l.modtime) where l.list_id = 0
293 ## retrieve (name = users.modtime) where users.users_id = 0
295 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
296 ## where l.group != 0 and l.active != 0 {
298 sprintf(buf, "%s:%d", name, gid);
299 hash_store(groups, lid, strsave(buf));
300 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
301 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
308 /* now do grplists */
310 users = create_hash(10000);
311 ## range of u is users
312 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
313 u = (struct user *) malloc(sizeof(struct user));
314 strcpy(u->name, strtrim(name));
316 hash_store(users, id, u);
320 ## repeat retrieve (lid = m.list_id, id = m.member_id)
321 ## where m.member_type = "USER" {
322 if ((u = (struct user *) hash_lookup(users, id)) &&
323 (hash_lookup(groups, lid) != NULL)) {
324 g = (struct grp *) malloc(sizeof(struct grp));
332 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
333 for (b = *p; b; b = b->next) {
335 sprintf(buf, "%s.grplist\tHS UNSPECA \"",
336 ((struct user *)b->data)->name);
337 for (g = ((struct user *)b->data)->lists; g; g = g->next)
338 if (l = hash_lookup(groups, g->id)) {
351 /* This is commented out because it takes on the order of 30 minutes to
352 * run. Instead, we never free the memory, but the program will exit
358 for (p = &groups[maxid-1]; p >= groups; p--)
365 if (fclose(iout) || fclose(gout) || fclose(lout)) {
366 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
379 char outf[64], outft[64], *mach, machbuf[128], *group;
383 struct save_queue *sq, *sq_create();
384 ## char name[33], type[9], loc[81], access[2], mount[33], trans[257];
385 ## char *filetime, comments[65];
386 ## int flag1, flag2, flag3, flag4, id, fid;
389 sprintf(outf, "%s/filsys.db", hesiod_dir);
391 if (stat(outf, &sb) == 0) {
393 filetime = ingres_date_and_time(ftime);
394 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
395 ## where tblstats.table = "filesys"
396 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
397 ## where tblstats.table = "machine"
398 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
399 ## where tblstats.table = "alias"
400 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
401 ## where tblstats.table = "fsgroup"
402 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
403 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
408 sprintf(outft, "%s~", outf);
409 out = fopen(outft, "w");
411 perror("cannot open filsys.db for write");
415 fprintf(stderr, "Building filsys.db\n");
419 ## range of f is filesys
420 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
421 ## access = f.#access, mount = f.#mount, comments = f.#comments,
422 ## fid = f.filsys_id) {
425 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
429 if (mach = hash_lookup(machines, id)) {
430 for (p = machbuf; *mach && *mach != '.'; mach++) {
432 *p++ = tolower(*mach);
437 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
438 name, type, loc, machbuf, access, mount);
440 } else if (!strcmp(type, "AFS")) {
444 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
445 name, loc, access, mount);
446 } else if (!strcmp(type, "ERR")) {
448 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
450 } else if (!strcmp(type, "FSGROUP")) {
451 sprintf(trans, "%s:%d", name, fid);
452 sq_save_data(sq, strsave(trans));
456 ## range of g is fsgroup
457 while (sq_get_data(sq, &group)) {
458 fid = atoi(index(group, ':')+1);
459 *index(group, ':') = 0;
460 ## retrieve (type = f.#type, loc = f.#name, id = f.mach_id,
461 ## access = f.#access, mount = f.#mount, comments = f.#comments,
462 ## name = f.#label, trans = g.#key)
463 ## where f.filsys_id = g.filsys_id and g.group_id = fid
464 ## sort by #trans, #name {
466 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
470 if (mach = hash_lookup(machines, id)) {
471 for (p = machbuf; *mach && *mach != '.'; mach++) {
473 *p++ = tolower(*mach);
478 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
479 group, type, loc, machbuf, access, mount);
481 } else if (!strcmp(type, "AFS")) {
485 fprintf(out, "%s.filsys\tHS UNSPECA \"AFS %s %s %s\"\n",
486 group, loc, access, mount);
487 } else if (!strcmp(type, "ERR")) {
489 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
497 ## range of a is alias
498 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
501 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
505 fprintf(stderr, "Unsuccessful close of filsys.db\n");
514 * Modified from sys/types.h:
516 int setsize; /* = howmany(setbits, NSETBITS) */
518 typedef long set_mask;
519 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
521 #define howmany(x, y) (((x)+((y)-1))/(y))
524 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
525 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
526 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
527 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
528 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
529 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
535 char outf[64], outft[64], *mach, machbuf[33], *p;
538 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
539 ## char name[33], label[17], data[33], *filetime;
540 set_mask **machs, *ms, *ps;
542 sprintf(outf, "%s/cluster.db", hesiod_dir);
544 if (stat(outf, &sb) == 0) {
546 filetime = ingres_date_and_time(ftime);
547 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
548 ## where tblstats.table = "cluster"
549 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
550 ## where tblstats.table = "machine"
551 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
552 ## where tblstats.table = "mcmap"
553 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
554 ## where tblstats.table = "svc"
555 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
556 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
561 sprintf(outft, "%s~", outf);
562 out = fopen(outft, "w");
564 perror("cannot open cluster.db for write");
568 fprintf(stderr, "Building cluster.db\n");
571 ## range of c is cluster
573 ## retrieve (maxclu = max(c.clu_id))
575 setsize = howmany(maxclu, NSETBITS);
576 ## range of m is machine
577 ## retrieve (maxmach = max(m.mach_id))
579 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
580 bzero(machs, (maxmach + 1) * sizeof(int));
582 ## range of p is mcmap
583 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
584 if (!(ms = machs[mid])) {
585 ms = machs[mid] = SET_CREATE();
592 for (mid = 1; mid < maxmach; mid++) {
596 for (cid = 1; cid < maxclu; cid++) {
597 if (SET_ISSET(cid, ms)) {
598 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
599 ## where d.clu_id = @cid {
603 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
609 if (mach = hash_lookup(machines, mid)) {
610 for (p = machbuf; *mach && *mach != '.'; mach++)
613 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
616 for (id = mid + 1; id < maxmach; id++) {
617 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
620 if (mach = hash_lookup(machines, id)) {
621 for (p = machbuf; *mach && *mach != '.'; mach++)
624 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
633 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
634 ## where c.clu_id = d.clu_id {
638 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
645 fprintf(stderr, "Unsuccessful close of cluster.db\n");
656 char outf[64], outft[64];
659 ## char name[17], rp[17], sd[33], rm[33], *filetime;
662 sprintf(outf, "%s/printcap.db", hesiod_dir);
664 if (stat(outf, &sb) == 0) {
666 filetime = ingres_date_and_time(ftime);
667 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
668 ## where tblstats.table = "printcap"
670 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
675 sprintf(outft, "%s~", outf);
676 out = fopen(outft, "w");
678 perror("cannot open printcap.db for write");
682 fprintf(stderr, "Building printcap.db\n");
684 ## range of p is printcap
685 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
686 ## where machine.mach_id = p.mach_id {
691 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
692 name, name, rp, rm, sd);
696 fprintf(stderr, "Unsuccessful close of pcap.db\n");
707 char outf[64], outft[64];
710 ## char name[33], rm[33], trans[129], *filetime;
711 ## int flag, flag1, ident;
713 sprintf(outf, "%s/palladium.db", hesiod_dir);
715 if (stat(outf, &sb) == 0) {
717 filetime = ingres_date_and_time(ftime);
718 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
719 ## where tblstats.table = "palladium"
720 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
721 ## where tblstats.table = "alias"
722 if (flag < 0 && flag1 < 0) {
723 fprintf(stderr, "File palladium.db does not need to be rebuilt.\n");
728 sprintf(outft, "%s~", outf);
729 out = fopen(outft, "w");
731 perror("cannot open palladium.db for write");
735 fprintf(stderr, "Building palladium.db\n");
737 ## range of p is palladium
738 ## retrieve (name = p.#name, ident = p.#ident, rm = machine.#name)
739 ## where machine.mach_id = p.mach_id {
743 "%s.palladium\tHS UNSPECA \"%s %d %s interface directory\"\n",
744 name, rm, ident, name);
746 ## range of a is alias
747 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "PALLADIUM" {
750 fprintf(out, "%s.palladium\tHS UNSPECA \"%s\"\n", name, trans);
754 fprintf(stderr, "Unsuccessful close of palladium.db\n");
764 FILE *out, *old, *new;
765 char outf[64], outft[64], *mach;
769 ## char service[17], *filetime;
770 ## int port, flag1, flag2, id;
772 sprintf(outf, "%s/sloc.db", hesiod_dir);
774 if (stat(outf, &sb) == 0) {
776 filetime = ingres_date_and_time(ftime);
777 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
778 ## where tblstats.table = "serverhosts"
779 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
780 ## where tblstats.table = "machine"
781 if (flag1 < 0 && flag2 < 0) {
782 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
787 sprintf(outft, "%s~", outf);
788 out = fopen(outft, "w");
790 perror("cannot open sloc.db for write");
794 fprintf(stderr, "Building sloc.db\n");
797 ## range of s is serverhosts
798 ## retrieve (service = s.#service, id = s.mach_id)
799 ## sort by #service {
801 if (mach = hash_lookup(machines, id))
802 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
806 fprintf(stderr, "Unsuccessful close of sloc.db\n");
817 char outf[64], outft[64];
820 ## char mach[33], service[33], protocol[9], altserv[129], *filetime;
821 ## int port, flag1, flag2;
823 sprintf(outf, "%s/service.db", hesiod_dir);
825 if (stat(outf, &sb) == 0) {
827 filetime = ingres_date_and_time(ftime);
828 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
829 ## where tblstats.table = "services"
831 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
836 sprintf(outft, "%s~", outf);
837 out = fopen(outft, "w");
839 perror("cannot open service.db for write");
843 fprintf(stderr, "Building service.db\n");
845 ## range of s is services
846 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
850 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
851 service, service, protocol, port);
854 ## range of a is alias
855 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
858 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
862 fprintf(stderr, "Unsuccessful close of service.db\n");