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.
11 #include <sys/types.h>
17 #define HESIOD_DIR "/u1/sms/dcm/hesiod"
19 #define min(x,y) ((x) < (y) ? (x) : (y))
20 struct hash *machines = NULL;
21 struct hash *users = NULL;
22 char *whoami = "hesiod.gen";
33 char *malloc(), *strsave();
34 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
46 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
54 changed = do_passwd();
55 changed += do_pobox();
56 changed += do_filsys();
57 changed += do_cluster();
58 changed += do_printers();
60 changed += do_service();
61 changed += do_groups();
66 fprintf(stderr, "No files updated.\n");
67 if (argc == 2 && stat(argv[1], &sb) == 0)
72 fprintf(stderr, "Building tar file.\n");
73 sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
83 * ingerr: (supposedly) called when Ingres indicates an error.
84 * I have not yet been able to get this to work to intercept a
85 * database open error.
87 #define INGRES_DEADLOCK 4700
89 static int ingerr(num)
97 ingres_errno = SMS_DEADLOCK;
100 ingres_errno = SMS_INGRES_ERR;
102 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
103 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
116 machines = create_hash(1000);
117 ## retrieve (name = machine.#name, id = machine.mach_id) {
118 hash_store(machines, id, strsave(strtrim(name)));
126 char poutf[64], uoutf[64], poutft[64], uoutft[64];
127 struct stat psb, usb;
130 ## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
132 ## int uid, flag, id;
134 sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
135 sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
137 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0) {
138 ftime = min(psb.st_mtime, usb.st_mtime);
139 filetime = ingres_date_and_time(ftime);
140 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
141 ## where tblstats.table = "users"
143 fprintf(stderr, "Files passwd.db and uid.db do not need to be rebuilt.\n");
148 sprintf(poutft, "%s~", poutf);
149 pout = fopen(poutft, "w");
151 perror("cannot open passwd.db~ for write");
154 sprintf(uoutft, "%s~", uoutf);
155 uout = fopen(uoutft, "w");
157 perror("cannot open uid.db~ for write");
161 fprintf(stderr, "Building passwd.db and uid.db\n");
163 users = create_hash(10000);
164 ## range of u is users
165 ## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
166 ## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
167 ## op = u.office_phone, hp = u.home_phone, id = u.users_id)
168 ## where u.status = 1 {
176 u = (struct user *) malloc(sizeof(struct user));
177 strcpy(u->name, login);
179 hash_store(users, id, u);
180 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
181 login, login, uid, fullname, nn, oa, op, hp, login, shell);
182 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
185 if (fclose(pout) || fclose(uout)) {
186 fprintf(stderr, "Unsuccessful file close of passwd.db or uid.db\n");
198 char outf[64], outft[64], *mach;
201 ## char login[9], *filetime;
202 ## int flag1, flag2, id;
204 sprintf(outf, "%s/pobox.db", HESIOD_DIR);
206 if (stat(outf, &sb) == 0) {
208 filetime = ingres_date_and_time(ftime);
209 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
210 ## where tblstats.table = "users"
211 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
212 ## where tblstats.table = "machine"
213 if (flag1 < 0 && flag2 < 0) {
214 fprintf(stderr,"File pobox.db does not need to be rebuilt.\n");
219 sprintf(outft, "%s~", outf);
220 out = fopen(outft, "w");
222 perror("cannot open pobox.db for write");
226 fprintf(stderr, "Building pobox.db\n");
229 ## range of u is users
230 ## retrieve (login = u.#login, id = u.pop_id)
231 ## where u.status = 1 and u.potype = "POP" {
233 if (mach = hash_lookup(machines, id))
234 fprintf(out, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
239 fprintf(stderr, "Unsuccessful close of pobox.db\n");
247 /************************************************************************
248 * WARNING: this routine mallocs a large amount of memory which it never frees.
249 * This is considered OK only because main has been arranged to call this
250 * last, so the process will exit shortly after we return.
255 FILE *iout, *gout, *lout;
256 char ioutf[64], goutf[64], loutf[64], buf[512], *l;
258 register struct bucket *b, **p;
261 struct stat isb, gsb, lsb;
263 register struct save_queue *sq;
264 struct save_queue *sq_create();
266 ## char name[33], *filetime;
267 ## int gid, id, lid, flag1, flag2, flag3, maxid;
270 sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
271 sprintf(goutf, "%s/group.db", HESIOD_DIR);
272 sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
274 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
275 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
276 filetime = ingres_date_and_time(ftime);
277 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
278 ## where tblstats.table = "users"
279 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
280 ## where tblstats.table = "list"
281 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
282 ## where tblstats.table = "members"
283 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
284 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
289 sprintf(buf, "%s~", ioutf);
290 iout = fopen(buf, "w");
292 perror("cannot open gid.db for write");
295 sprintf(buf, "%s~", goutf);
296 gout = fopen(buf, "w");
298 perror("cannot open group.db for write");
301 sprintf(buf, "%s~", loutf);
302 lout = fopen(buf, "w");
304 perror("cannot open grplist.db for write");
308 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
310 /* make space for group list */
311 groups = create_hash(15000);
313 /* retrieve simple groups */
314 ## range of l is list
316 ## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
317 ## where l.group != 0 and l.active != 0 {
319 sprintf(buf, "%s:%d", name, gid);
320 hash_store(groups, lid, strsave(buf));
321 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
322 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
329 /* get special cases: lists that aren't groups themselves but are
330 * members of groups. */
332 ## range of m is members
333 ## retrieve (name = list.#name, gid = list.#gid, lid = l.list_id)
334 ## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
335 ## m.member_id = l.list_id and m.list_id = list.list_id and
338 if (l = hash_lookup(groups, lid)) {
339 sprintf(buf, "%s:%s:%d", l, name, gid);
342 sprintf(buf, "%s:%d", name, gid);
344 hash_store(groups, lid, strsave(buf));
345 sq_save_unique_data(sq, lid);
348 while (sq_get_data(sq, &id)) {
349 ## repeat retrieve (lid = l.list_id) where m.member_type = "LIST" and
350 ## m.list_id = @id and m.member_id = l.list_id and l.active != 0 {
351 if (l = hash_lookup(groups, lid)) {
352 sprintf(buf, "%s:%s", l, hash_lookup(groups, id));
356 l = hash_lookup(groups, id);
358 hash_store(groups, lid, l);
359 sq_save_unique_data(sq, lid);
364 /* now do grplists */
366 users = create_hash(10000);
367 ## range of u is users
368 ## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
369 u = (struct user *) malloc(sizeof(struct user));
370 strcpy(u->name, strtrim(name));
372 hash_store(users, id, u);
376 ## repeat retrieve (lid = m.list_id, id = m.member_id)
377 ## where m.member_type = "USER" {
378 if ((u = (struct user *) hash_lookup(users, id)) &&
379 (hash_lookup(groups, lid) != NULL)) {
380 g = (struct grp *) malloc(sizeof(struct grp));
388 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
389 for (b = *p; b; b = b->next) {
391 sprintf(buf, "%s.grplist\tHS UNSPECA \"",
392 ((struct user *)b->data)->name);
393 for (g = ((struct user *)b->data)->lists; g; g = g->next)
394 if (l = hash_lookup(groups, g->id)) {
401 /* Do duplicate detection here */
403 remove_duplicate_groups(buf);
410 /* This is commented out because it takes on the order of 30 minutes to
411 * run. Instead, we never free the memory, but the program will exit
417 for (p = &groups[maxid-1]; p >= groups; p--)
424 if (fclose(iout) || fclose(gout) || fclose(lout)) {
425 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
435 #define MAXGROUPS 256
437 remove_duplicate_groups(p)
443 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
445 bzero(memory, MAXGROUPS * sizeof(int));
446 while (p = index(p, ':')) {
448 for (mp = memory; *mp && mp < mg; mp++)
450 if (p = index(p, ':'))
453 strcpy(--pp, "\"\n");
461 if (!(p = index(p, ':')))
471 char outf[64], outft[64], *mach, machbuf[128];
475 ## char name[33], type[9], loc[33], access[2], mount[33], trans[257];
476 ## char *filetime, comments[65];
477 ## int flag1, flag2, flag3, id;
479 sprintf(outf, "%s/filsys.db", HESIOD_DIR);
481 if (stat(outf, &sb) == 0) {
483 filetime = ingres_date_and_time(ftime);
484 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
485 ## where tblstats.table = "filesys"
486 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
487 ## where tblstats.table = "machine"
488 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
489 ## where tblstats.table = "alias"
490 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
491 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
496 sprintf(outft, "%s~", outf);
497 out = fopen(outft, "w");
499 perror("cannot open filsys.db for write");
503 fprintf(stderr, "Building filsys.db\n");
506 ## range of f is filesys
507 ## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
508 ## access = f.#access, mount = f.#mount, comments = f.#comments) {
511 if (strcmp(type, "ERR")) {
515 if (mach = hash_lookup(machines, id)) {
516 for (p = machbuf; *mach && *mach != '.'; mach++) {
518 *p++ = tolower(*mach);
523 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
524 name, type, loc, machbuf, access, mount);
528 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
535 ## range of a is alias
536 ## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
539 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
543 fprintf(stderr, "Unsuccessful close of filsys.db\n");
552 * Modified from sys/types.h:
554 int setsize; /* = howmany(setbits, NSETBITS) */
556 typedef long set_mask;
557 #define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
559 #define howmany(x, y) (((x)+((y)-1))/(y))
562 #define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
563 #define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
564 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
565 #define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
566 #define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
567 #define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
573 char outf[64], outft[64], *mach, machbuf[33], *p;
576 ## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
577 ## char name[33], label[17], data[33], *filetime;
578 set_mask **machs, *ms, *ps;
580 sprintf(outf, "%s/cluster.db", HESIOD_DIR);
582 if (stat(outf, &sb) == 0) {
584 filetime = ingres_date_and_time(ftime);
585 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
586 ## where tblstats.table = "cluster"
587 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
588 ## where tblstats.table = "machine"
589 ## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
590 ## where tblstats.table = "mcmap"
591 ## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
592 ## where tblstats.table = "svc"
593 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
594 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
599 sprintf(outft, "%s~", outf);
600 out = fopen(outft, "w");
602 perror("cannot open cluster.db for write");
606 fprintf(stderr, "Building cluster.db\n");
609 ## range of c is cluster
611 ## retrieve (maxclu = max(c.clu_id))
613 setsize = howmany(maxclu, NSETBITS);
614 ## range of m is machine
615 ## retrieve (maxmach = max(m.mach_id))
617 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
618 bzero(machs, (maxmach + 1) * sizeof(int));
620 ## range of p is mcmap
621 ## retrieve (mid = p.mach_id, cid = p.clu_id) {
622 if (!(ms = machs[mid])) {
623 ms = machs[mid] = SET_CREATE();
630 for (mid = 1; mid < maxmach; mid++) {
634 for (cid = 1; cid < maxclu; cid++) {
635 if (SET_ISSET(cid, ms)) {
636 ## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
637 ## where d.clu_id = @cid {
641 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
647 if (mach = hash_lookup(machines, mid)) {
648 for (p = machbuf; *mach && *mach != '.'; mach++)
651 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
654 for (id = mid + 1; id < maxmach; id++) {
655 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
658 if (mach = hash_lookup(machines, id)) {
659 for (p = machbuf; *mach && *mach != '.'; mach++)
662 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
671 ## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
672 ## where c.clu_id = d.clu_id {
676 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
683 fprintf(stderr, "Unsuccessful close of cluster.db\n");
694 char outf[64], outft[64];
697 ## char name[17], rp[17], sd[33], rm[33], *filetime;
700 sprintf(outf, "%s/printcap.db", HESIOD_DIR);
702 if (stat(outf, &sb) == 0) {
704 filetime = ingres_date_and_time(ftime);
705 ## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
706 ## where tblstats.table = "printcap"
708 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
713 sprintf(outft, "%s~", outf);
714 out = fopen(outft, "w");
716 perror("cannot open printcap.db for write");
720 fprintf(stderr, "Building printcap.db\n");
722 ## range of p is printcap
723 ## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
724 ## where machine.mach_id = p.mach_id {
729 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
730 name, name, rp, rm, sd);
734 fprintf(stderr, "Unsuccessful close of pcap.db\n");
744 FILE *out, *old, *new;
745 char outf[64], outft[64], *mach;
749 ## char service[17], *filetime;
750 ## int port, flag1, flag2, id;
752 sprintf(outf, "%s/sloc.db", HESIOD_DIR);
754 if (stat(outf, &sb) == 0) {
756 filetime = ingres_date_and_time(ftime);
757 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
758 ## where tblstats.table = "serverhosts"
759 ## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
760 ## where tblstats.table = "machine"
761 if (flag1 < 0 && flag2 < 0) {
762 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
767 sprintf(outft, "%s~", outf);
768 out = fopen(outft, "w");
770 perror("cannot open sloc.db for write");
774 fprintf(stderr, "Building sloc.db\n");
777 ## range of s is serverhosts
778 ## retrieve (service = s.#service, id = s.mach_id)
779 ## sort by #service {
781 if (mach = hash_lookup(machines, id))
782 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
786 fprintf(stderr, "Unsuccessful close of sloc.db\n");
797 char outf[64], outft[64];
800 ## char mach[33], service[33], protocol[9], altserv[129], *filetime;
801 ## int port, flag1, flag2;
803 sprintf(outf, "%s/service.db", HESIOD_DIR);
805 if (stat(outf, &sb) == 0) {
807 filetime = ingres_date_and_time(ftime);
808 ## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
809 ## where tblstats.table = "services"
811 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
816 sprintf(outft, "%s~", outf);
817 out = fopen(outft, "w");
819 perror("cannot open service.db for write");
823 fprintf(stderr, "Building service.db\n");
825 ## range of s is services
826 ## retrieve (service = s.name, protocol = lowercase(s.#protocol),
830 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
831 service, service, protocol, port);
834 ## range of a is alias
835 ## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
838 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
842 fprintf(stderr, "Unsuccessful close of service.db\n");