]> andersk Git - moira.git/blame - gen/hesiod.qc
improve locking; don't free some stuff in the interests of speed
[moira.git] / gen / hesiod.qc
CommitLineData
dfb56d6b 1/* $Header$
2 *
3 * This generates the zone files necessary to load a hesiod server.
f575c498 4 * The following zones are generated: passwd, uid, pobox, group,
5 * grplist, gid, filsys, cluster, pcap, sloc, service.
0a5ff702 6 *
7 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
8 * For copying and distribution information, please see the file
9 * <mit-copyright.h>.
dfb56d6b 10 */
11
0a5ff702 12#include <mit-copyright.h>
dfb56d6b 13#include <stdio.h>
14#include <sms.h>
15#include <sms_app.h>
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <sys/time.h>
abeed590 19#include <ctype.h>
dfb56d6b 20
bd8a14a5 21
dfb56d6b 22#define HESIOD_DIR "/u1/sms/dcm/hesiod"
23
24#define min(x,y) ((x) < (y) ? (x) : (y))
bd8a14a5 25struct hash *machines = NULL;
26struct hash *users = NULL;
1991abf7 27char *whoami = "hesiod.gen";
bd8a14a5 28
29struct grp {
30 struct grp *next;
31 int id;
32};
33struct user {
34 char name[9];
35 struct grp *lists;
36};
dfb56d6b 37
38char *malloc(), *strsave();
39char *ingres_date_and_time(), *ingres_time(), *ingres_date();
40
41main(argc, argv)
42int argc;
43char **argv;
44{
45 char cmd[64];
46 struct stat sb;
47 int changed = 0;
1991abf7 48 int ingerr();
dfb56d6b 49
50 if (argc > 2) {
51 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
f575c498 52 exit(SMS_ARGS);
dfb56d6b 53 }
54
1991abf7 55 IIseterr(ingerr);
56
dfb56d6b 57## ingres sms
dfb56d6b 58
59 changed = do_passwd();
60 changed += do_pobox();
dfb56d6b 61 changed += do_filsys();
62 changed += do_cluster();
63 changed += do_printers();
f575c498 64 changed += do_sloc();
65 changed += do_service();
66 changed += do_groups();
dfb56d6b 67
68## exit
69
70 if (!changed) {
71 fprintf(stderr, "No files updated.\n");
72 if (argc == 2 && stat(argv[1], &sb) == 0)
f575c498 73 exit(SMS_NO_CHANGE);
dfb56d6b 74 }
75
76 if (argc == 2) {
bd8a14a5 77 fprintf(stderr, "Building tar file.\n");
dfb56d6b 78 sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
f575c498 79 if (system(cmd))
80 exit(SMS_TAR_FAIL);
dfb56d6b 81 }
82
f575c498 83 exit(SMS_SUCCESS);
dfb56d6b 84}
85
86
1991abf7 87/*
88 * ingerr: (supposedly) called when Ingres indicates an error.
89 * I have not yet been able to get this to work to intercept a
90 * database open error.
91 */
92#define INGRES_DEADLOCK 4700
93
94static int ingerr(num)
95 int *num;
96{
97 char buf[256];
98 int ingres_errno;
99
100 switch (*num) {
101 case INGRES_DEADLOCK:
102 ingres_errno = SMS_DEADLOCK;
103 break;
104 default:
105 ingres_errno = SMS_INGRES_ERR;
106 }
107 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
108 critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
109 exit(ingres_errno);
110}
111
112
bd8a14a5 113get_mach()
114##{
115## int id;
116## char name[33];
117
118 if (machines)
119 return;
120
121 machines = create_hash(1000);
122## retrieve (name = machine.#name, id = machine.mach_id) {
123 hash_store(machines, id, strsave(strtrim(name)));
124## }
125##}
126
127
dfb56d6b 128do_passwd()
129##{
130 FILE *pout, *uout;
f575c498 131 char poutf[64], uoutf[64], poutft[64], uoutft[64];
dfb56d6b 132 struct stat psb, usb;
133 time_t ftime;
bd8a14a5 134 struct user *u;
dfb56d6b 135## char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
0a48a760 136## char nn[17];
bd8a14a5 137## int uid, flag, id;
dfb56d6b 138
139 sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
140 sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
141
142 if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0) {
143 ftime = min(psb.st_mtime, usb.st_mtime);
144 filetime = ingres_date_and_time(ftime);
145## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
146## where tblstats.table = "users"
147 if (flag < 0) {
148 fprintf(stderr, "Files passwd.db and uid.db do not need to be rebuilt.\n");
149 return(0);
150 }
151 }
152
f575c498 153 sprintf(poutft, "%s~", poutf);
154 pout = fopen(poutft, "w");
dfb56d6b 155 if (!pout) {
f575c498 156 perror("cannot open passwd.db~ for write");
157 exit(SMS_OCONFIG);
dfb56d6b 158 }
f575c498 159 sprintf(uoutft, "%s~", uoutf);
160 uout = fopen(uoutft, "w");
dfb56d6b 161 if (!uout) {
f575c498 162 perror("cannot open uid.db~ for write");
163 exit(SMS_OCONFIG);
dfb56d6b 164 }
165
166 fprintf(stderr, "Building passwd.db and uid.db\n");
167
bd8a14a5 168 users = create_hash(10000);
dfb56d6b 169## range of u is users
170## retrieve (login = u.#login, uid = u.#uid, shell = u.#shell,
0a48a760 171## fullname = u.#fullname, nn = u.nickname, oa = u.office_addr,
bd8a14a5 172## op = u.office_phone, hp = u.home_phone, id = u.users_id)
173## where u.status = 1 {
174 strtrim(login);
29904371 175 dequote(fullname);
0a48a760 176 dequote(nn);
29904371 177 dequote(oa);
178 dequote(op);
179 dequote(hp);
180 dequote(shell);
bd8a14a5 181 u = (struct user *) malloc(sizeof(struct user));
182 strcpy(u->name, login);
183 u->lists = NULL;
184 hash_store(users, id, u);
0a48a760 185 fprintf(pout, "%s.passwd\tHS UNSPECA \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
186 login, login, uid, fullname, nn, oa, op, hp, login, shell);
dfb56d6b 187 fprintf(uout, "%d.uid\tHS CNAME %s.passwd\n", uid, login);
188## }
189
190 if (fclose(pout) || fclose(uout)) {
191 fprintf(stderr, "Unsuccessful file close of passwd.db or uid.db\n");
f575c498 192 exit(SMS_CCONFIG);
dfb56d6b 193 }
f575c498 194 fix_file(poutf);
195 fix_file(uoutf);
dfb56d6b 196 return(1);
197##}
198
199
200do_pobox()
201##{
202 FILE *out;
bd8a14a5 203 char outf[64], outft[64], *mach;
dfb56d6b 204 struct stat sb;
205 time_t ftime;
bd8a14a5 206## char login[9], *filetime;
207## int flag1, flag2, id;
dfb56d6b 208
209 sprintf(outf, "%s/pobox.db", HESIOD_DIR);
210
211 if (stat(outf, &sb) == 0) {
212 ftime = sb.st_mtime;
213 filetime = ingres_date_and_time(ftime);
214## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
215## where tblstats.table = "users"
216## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
217## where tblstats.table = "machine"
218 if (flag1 < 0 && flag2 < 0) {
219 fprintf(stderr,"File pobox.db does not need to be rebuilt.\n");
220 return(0);
221 }
222 }
223
f575c498 224 sprintf(outft, "%s~", outf);
225 out = fopen(outft, "w");
dfb56d6b 226 if (!out) {
227 perror("cannot open pobox.db for write");
f575c498 228 exit(SMS_OCONFIG);
dfb56d6b 229 }
230
231 fprintf(stderr, "Building pobox.db\n");
bd8a14a5 232 get_mach();
dfb56d6b 233
234## range of u is users
bd8a14a5 235## retrieve (login = u.#login, id = u.pop_id)
236## where u.status = 1 and u.potype = "POP" {
237 strtrim(login);
238 if (mach = hash_lookup(machines, id))
239 fprintf(out, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
240 login, mach, login);
dfb56d6b 241## }
242
243 if (fclose(out)) {
244 fprintf(stderr, "Unsuccessful close of pobox.db\n");
f575c498 245 exit(SMS_CCONFIG);
dfb56d6b 246 }
f575c498 247 fix_file(outf);
dfb56d6b 248 return(1);
249##}
250
251
f575c498 252/************************************************************************
253 * WARNING: this routine mallocs a large amount of memory which it never frees.
254 * This is considered OK only because main has been arranged to call this
255 * last, so the process will exit shortly after we return.
256 */
257
dfb56d6b 258do_groups()
259##{
260 FILE *iout, *gout, *lout;
1991abf7 261 char ioutf[64], goutf[64], loutf[64], buf[512], *l;
bd8a14a5 262 struct hash *groups;
263 register struct bucket *b, **p;
264 struct grp *g;
265 struct user *u;
dfb56d6b 266 struct stat isb, gsb, lsb;
267 time_t ftime;
f575c498 268 register struct save_queue *sq;
269 struct save_queue *sq_create();
bd8a14a5 270 register int i;
dfb56d6b 271## char name[33], *filetime;
272## int gid, id, lid, flag1, flag2, flag3, maxid;
273
274 /* open files */
275 sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
276 sprintf(goutf, "%s/group.db", HESIOD_DIR);
277 sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
278
279 if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
280 ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
281 filetime = ingres_date_and_time(ftime);
282## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
283## where tblstats.table = "users"
284## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
285## where tblstats.table = "list"
286## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
287## where tblstats.table = "members"
288 if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
289 fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
290 return(0);
291 }
292 }
293
f575c498 294 sprintf(buf, "%s~", ioutf);
295 iout = fopen(buf, "w");
dfb56d6b 296 if (!iout) {
297 perror("cannot open gid.db for write");
f575c498 298 exit(SMS_OCONFIG);
dfb56d6b 299 }
f575c498 300 sprintf(buf, "%s~", goutf);
301 gout = fopen(buf, "w");
dfb56d6b 302 if (!gout) {
303 perror("cannot open group.db for write");
f575c498 304 exit(SMS_OCONFIG);
dfb56d6b 305 }
f575c498 306 sprintf(buf, "%s~", loutf);
307 lout = fopen(buf, "w");
dfb56d6b 308 if (!lout) {
309 perror("cannot open grplist.db for write");
f575c498 310 exit(SMS_OCONFIG);
dfb56d6b 311 }
312
313 fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
314
315 /* make space for group list */
bd8a14a5 316 groups = create_hash(15000);
dfb56d6b 317
318 /* retrieve simple groups */
bd8a14a5 319## range of l is list
29904371 320## begin transaction
dfb56d6b 321## retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
f575c498 322## where l.group != 0 and l.active != 0 {
bd8a14a5 323 strtrim(name);
dfb56d6b 324 sprintf(buf, "%s:%d", name, gid);
bd8a14a5 325 hash_store(groups, lid, strsave(buf));
dfb56d6b 326 fprintf(iout, "%d.gid\tHS CNAME %s.group\n", gid, name);
327 fprintf(gout, "%s.group\tHS UNSPECA \"%s:*:%d:\"\n",
328 name, name, gid);
329## }
330
f575c498 331 fflush(iout);
332 fflush(gout);
333
dfb56d6b 334 /* get special cases: lists that aren't groups themselves but are
335 * members of groups. */
336 sq = sq_create();
337## range of m is members
f575c498 338## retrieve (name = list.#name, gid = list.#gid, lid = l.list_id)
1991abf7 339## where l.active != 0 and list.active != 0 and m.member_type = "LIST" and
dfb56d6b 340## m.member_id = l.list_id and m.list_id = list.list_id and
341## list.group != 0 {
bd8a14a5 342 strtrim(name);
343 if (l = hash_lookup(groups, lid)) {
344 sprintf(buf, "%s:%s:%d", l, name, gid);
345 free(l);
f575c498 346 } else {
347 sprintf(buf, "%s:%d", name, gid);
348 }
bd8a14a5 349 hash_store(groups, lid, strsave(buf));
1991abf7 350 sq_save_unique_data(sq, lid);
dfb56d6b 351## }
f575c498 352
dfb56d6b 353 while (sq_get_data(sq, &id)) {
1991abf7 354## repeat retrieve (lid = l.list_id) where m.member_type = "LIST" and
355## m.list_id = @id and m.member_id = l.list_id and l.active != 0 {
bd8a14a5 356 if (l = hash_lookup(groups, lid)) {
1991abf7 357 sprintf(buf, "%s:%s", l, hash_lookup(groups, id));
bd8a14a5 358 free(l);
1991abf7 359 l = strsave(buf);
bd8a14a5 360 } else {
1991abf7 361 l = hash_lookup(groups, id);
bd8a14a5 362 }
1991abf7 363 hash_store(groups, lid, l);
dfb56d6b 364 sq_save_unique_data(sq, lid);
365## }
366 }
367 sq_destroy(sq);
368
369 /* now do grplists */
bd8a14a5 370 if (users == NULL) {
371 users = create_hash(10000);
372## range of u is users
373## retrieve (id = u.users_id, name = u.login) where u.status = 1 {
374 u = (struct user *) malloc(sizeof(struct user));
375 strcpy(u->name, strtrim(name));
376 u->lists = NULL;
377 hash_store(users, id, u);
dfb56d6b 378## }
dfb56d6b 379 }
bd8a14a5 380
381## repeat retrieve (lid = m.list_id, id = m.member_id)
382## where m.member_type = "USER" {
1991abf7 383 if ((u = (struct user *) hash_lookup(users, id)) &&
384 (hash_lookup(groups, lid) != NULL)) {
bd8a14a5 385 g = (struct grp *) malloc(sizeof(struct grp));
386 g->next = u->lists;
387 u->lists = g;
388 g->id = lid;
389 }
390## }
29904371 391## end transaction
bd8a14a5 392
393 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
394 for (b = *p; b; b = b->next) {
1991abf7 395 i = 0;
396 sprintf(buf, "%s.grplist\tHS UNSPECA \"",
bd8a14a5 397 ((struct user *)b->data)->name);
398 for (g = ((struct user *)b->data)->lists; g; g = g->next)
1991abf7 399 if (l = hash_lookup(groups, g->id)) {
400 i++;
401 strcat(buf, l);
402 if (g->next)
403 strcat(buf, ":");
404 }
405 strcat(buf, "\"\n");
406 /* Do duplicate detection here */
407 if (i > 1) {
408 remove_duplicate_groups(buf);
409 }
410 fputs(buf, lout);
bd8a14a5 411 }
412 }
413
f575c498 414#ifdef notdef
415/* This is commented out because it takes on the order of 30 minutes to
416 * run. Instead, we never free the memory, but the program will exit
417 * shortly anyway.
418 */
dfb56d6b 419 sq_destroy(sq);
bd8a14a5 420 {
421 register char **p;
422 for (p = &groups[maxid-1]; p >= groups; p--)
423 if (*p)
424 free(*p);
425 }
dfb56d6b 426 free(groups);
f575c498 427#endif
dfb56d6b 428
429 if (fclose(iout) || fclose(gout) || fclose(lout)) {
430 fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
f575c498 431 exit(SMS_CCONFIG);
dfb56d6b 432 }
f575c498 433 fix_file(ioutf);
434 fix_file(goutf);
435 fix_file(loutf);
dfb56d6b 436 return(1);
437##}
438
439
1991abf7 440#define MAXGROUPS 256
441
442remove_duplicate_groups(p)
443register char *p;
444{
445 register char *pp;
446 char *index();
447 register int *mp;
448 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
449
450 bzero(memory, MAXGROUPS * sizeof(int));
451 while (p = index(p, ':')) {
452 i = atoi(++p);
453 for (mp = memory; *mp && mp < mg; mp++)
454 if (*mp == i) {
455 if (p = index(p, ':'))
456 strcpy(pp, ++p);
457 else {
458 strcpy(--pp, "\"\n");
459 }
460 p = pp;
461 break;
462 }
463 if (*mp == i)
464 continue;
465 *mp = i;
466 if (!(p = index(p, ':')))
467 return;
468 pp = ++p;
469 }
470}
471
472
dfb56d6b 473do_filsys()
474##{
475 FILE *out;
90647001 476 char outf[64], outft[64], *mach, machbuf[128], *group;
abeed590 477 register char *p;
dfb56d6b 478 struct stat sb;
479 time_t ftime;
90647001 480 struct save_queue *sq, *sq_create();
bd8a14a5 481## char name[33], type[9], loc[33], access[2], mount[33], trans[257];
f575c498 482## char *filetime, comments[65];
90647001 483## int flag1, flag2, flag3, flag4, id, fid;
dfb56d6b 484
485 sprintf(outf, "%s/filsys.db", HESIOD_DIR);
486
487 if (stat(outf, &sb) == 0) {
488 ftime = sb.st_mtime;
489 filetime = ingres_date_and_time(ftime);
490## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
491## where tblstats.table = "filesys"
492## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
493## where tblstats.table = "machine"
494## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
495## where tblstats.table = "alias"
90647001 496## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
497## where tblstats.table = "fsgroup"
498 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
dfb56d6b 499 fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
500 return(0);
501 }
502 }
503
f575c498 504 sprintf(outft, "%s~", outf);
505 out = fopen(outft, "w");
dfb56d6b 506 if (!out) {
507 perror("cannot open filsys.db for write");
f575c498 508 exit(SMS_OCONFIG);
dfb56d6b 509 }
510
511 fprintf(stderr, "Building filsys.db\n");
bd8a14a5 512 get_mach();
90647001 513 sq = sq_create();
dfb56d6b 514
515## range of f is filesys
bd8a14a5 516## retrieve (name = f.label, type = f.#type, loc = f.#name, id = f.mach_id,
90647001 517## access = f.#access, mount = f.#mount, comments = f.#comments,
518## fid = f.filsys_id) {
bd8a14a5 519 strtrim(name);
520 strtrim(type);
90647001 521 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
bd8a14a5 522 strtrim(loc);
523 strtrim(access);
524 strtrim(mount);
abeed590 525 if (mach = hash_lookup(machines, id)) {
526 for (p = machbuf; *mach && *mach != '.'; mach++) {
527 if (isupper(*mach))
528 *p++ = tolower(*mach);
529 else
530 *p++ = *mach;
531 *p = 0;
532 }
533 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
534 name, type, loc, machbuf, access, mount);
535 }
90647001 536 } else if (!strcmp(type, "ERR")) {
bd8a14a5 537 strtrim(comments);
538 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
539 name, comments);
90647001 540 } else if (!strcmp(type, "FSGROUP")) {
541 sprintf(trans, "%s:%d", name, fid);
542 sq_save_data(sq, strsave(trans));
f575c498 543 }
dfb56d6b 544## }
545
90647001 546## range of g is fsgroup
547 while (sq_get_data(sq, &group)) {
548 fid = atoi(index(group, ':')+1);
549 *index(group, ':') = 0;
550## retrieve (type = f.#type, loc = f.#name, id = f.mach_id,
551## access = f.#access, mount = f.#mount, comments = f.#comments,
552## name = f.#label, trans = g.#key)
553## where f.filsys_id = g.filsys_id and g.group_id = fid
554## sort by #trans, #name {
555 strtrim(type);
556 if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
557 strtrim(loc);
558 strtrim(access);
559 strtrim(mount);
560 if (mach = hash_lookup(machines, id)) {
561 for (p = machbuf; *mach && *mach != '.'; mach++) {
562 if (isupper(*mach))
563 *p++ = tolower(*mach);
564 else
565 *p++ = *mach;
566 *p = 0;
567 }
568 fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
569 group, type, loc, machbuf, access, mount);
570 }
571 } else if (!strcmp(type, "ERR")) {
572 strtrim(comments);
573 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
574 group, comments);
575 }
576## }
577 free(group);
578 }
579 sq_destroy(sq);
29904371 580
dfb56d6b 581## range of a is alias
582## retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
bd8a14a5 583 strtrim(name);
584 strtrim(trans);
dfb56d6b 585 fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
586## }
587
588 if (fclose(out)) {
589 fprintf(stderr, "Unsuccessful close of filsys.db\n");
f575c498 590 exit(SMS_CCONFIG);
dfb56d6b 591 }
f575c498 592 fix_file(outf);
dfb56d6b 593 return(1);
594##}
595
596
597/*
598 * Modified from sys/types.h:
599 */
600int setsize; /* = howmany(setbits, NSETBITS) */
601
602typedef long set_mask;
603#define NSETBITS (sizeof(set_mask) * NBBY) /* bits per mask */
604#ifndef howmany
605#define howmany(x, y) (((x)+((y)-1))/(y))
606#endif
607
608#define SET_SET(n, p) ((p)[(n)/NSETBITS] |= (1 << ((n) % NSETBITS)))
609#define SET_CLR(n, p) ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
610#define SET_ISSET(n, p) ((p)[(n)/NSETBITS] & (1 << ((n) % NSETBITS)))
611#define SET_CREATE() ((set_mask *)malloc(setsize * sizeof(set_mask)))
612#define SET_ZERO(p) bzero((char *)(p), setsize * sizeof(set_mask))
613#define SET_CMP(p1, p2) (bcmp((p1), (p2), setsize * sizeof(set_mask)))
614
615
616do_cluster()
617##{
618 FILE *out;
29904371 619 char outf[64], outft[64], *mach, machbuf[33], *p;
dfb56d6b 620 struct stat sb;
621 time_t ftime;
622## int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
bd8a14a5 623## char name[33], label[17], data[33], *filetime;
dfb56d6b 624 set_mask **machs, *ms, *ps;
625
626 sprintf(outf, "%s/cluster.db", HESIOD_DIR);
627
628 if (stat(outf, &sb) == 0) {
629 ftime = sb.st_mtime;
630 filetime = ingres_date_and_time(ftime);
631## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
632## where tblstats.table = "cluster"
633## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
634## where tblstats.table = "machine"
635## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
636## where tblstats.table = "mcmap"
637## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
638## where tblstats.table = "svc"
639 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
640 fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
641 return(0);
642 }
643 }
644
f575c498 645 sprintf(outft, "%s~", outf);
646 out = fopen(outft, "w");
dfb56d6b 647 if (!out) {
648 perror("cannot open cluster.db for write");
f575c498 649 exit(SMS_OCONFIG);
dfb56d6b 650 }
651
652 fprintf(stderr, "Building cluster.db\n");
bd8a14a5 653 get_mach();
dfb56d6b 654
655## range of c is cluster
29904371 656## begin transaction
dfb56d6b 657## retrieve (maxclu = max(c.clu_id))
3604fe12 658 maxclu++;
dfb56d6b 659 setsize = howmany(maxclu, NSETBITS);
660## range of m is machine
661## retrieve (maxmach = max(m.mach_id))
3604fe12 662 maxmach++;
dfb56d6b 663 machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
664 bzero(machs, (maxmach + 1) * sizeof(int));
665
666## range of p is mcmap
667## retrieve (mid = p.mach_id, cid = p.clu_id) {
668 if (!(ms = machs[mid])) {
669 ms = machs[mid] = SET_CREATE();
670 SET_ZERO(ms);
671 }
672 SET_SET(cid, ms);
673## }
674
675## range of d is svc
676 for (mid = 1; mid < maxmach; mid++) {
677 if (!machs[mid])
678 continue;
679 ms = machs[mid];
dfb56d6b 680 for (cid = 1; cid < maxclu; cid++) {
681 if (SET_ISSET(cid, ms)) {
682## repeat retrieve (label = d.serv_label, data = d.serv_cluster)
683## where d.clu_id = @cid {
bd8a14a5 684 strtrim(label);
685 strtrim(data);
f575c498 686 fprintf(out,
687 "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
688 mid, label, data);
dfb56d6b 689## }
690 }
691 }
692
29904371 693 if (mach = hash_lookup(machines, mid)) {
694 for (p = machbuf; *mach && *mach != '.'; mach++)
695 *p++ = *mach;
696 *p = 0;
697 fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
698 machbuf, mid);
699 }
dfb56d6b 700 for (id = mid + 1; id < maxmach; id++) {
701 if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
702 free(ps);
703 machs[id] = NULL;
29904371 704 if (mach = hash_lookup(machines, id)) {
705 for (p = machbuf; *mach && *mach != '.'; mach++)
706 *p++ = *mach;
707 *p = 0;
708 fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
709 machbuf, mid);
710 }
dfb56d6b 711 }
712 }
713 free(ms);
714 machs[mid] = NULL;
715 }
dfb56d6b 716
717## retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
718## where c.clu_id = d.clu_id {
bd8a14a5 719 strtrim(name);
720 strtrim(label);
721 strtrim(data);
dfb56d6b 722 fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
723 name, label, data);
724## }
29904371 725## end transaction
726 free(machs);
dfb56d6b 727
728 if (fclose(out)) {
729 fprintf(stderr, "Unsuccessful close of cluster.db\n");
f575c498 730 exit(SMS_CCONFIG);
dfb56d6b 731 }
f575c498 732 fix_file(outf);
dfb56d6b 733 return(1);
734##}
735
736
737do_printers()
738##{
739 FILE *out;
f575c498 740 char outf[64], outft[64];
dfb56d6b 741 struct stat sb;
742 time_t ftime;
29904371 743## char name[17], rp[17], sd[33], rm[33], *filetime;
dfb56d6b 744## int flag;
745
746 sprintf(outf, "%s/printcap.db", HESIOD_DIR);
747
748 if (stat(outf, &sb) == 0) {
749 ftime = sb.st_mtime;
750 filetime = ingres_date_and_time(ftime);
751## retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
752## where tblstats.table = "printcap"
753 if (flag < 0) {
754 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
755 return(0);
756 }
757 }
758
f575c498 759 sprintf(outft, "%s~", outf);
760 out = fopen(outft, "w");
dfb56d6b 761 if (!out) {
762 perror("cannot open printcap.db for write");
f575c498 763 exit(SMS_OCONFIG);
dfb56d6b 764 }
765
766 fprintf(stderr, "Building printcap.db\n");
767
768## range of p is printcap
29904371 769## retrieve (name = p.#name, rp = p.#rp, sd = p.dir, rm = machine.#name)
770## where machine.mach_id = p.mach_id {
bd8a14a5 771 strtrim(name);
29904371 772 strtrim(rp);
773 strtrim(sd);
774 strtrim(rm);
775 fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
776 name, name, rp, rm, sd);
dfb56d6b 777## }
778
779 if (fclose(out)) {
780 fprintf(stderr, "Unsuccessful close of pcap.db\n");
f575c498 781 exit(SMS_CCONFIG);
dfb56d6b 782 }
f575c498 783 fix_file(outf);
dfb56d6b 784 return(1);
785##}
786
787
f575c498 788do_sloc()
dfb56d6b 789##{
f575c498 790 FILE *out, *old, *new;
bd8a14a5 791 char outf[64], outft[64], *mach;
dfb56d6b 792 struct stat sb;
793 time_t ftime;
f575c498 794 register int c;
bd8a14a5 795## char service[17], *filetime;
796## int port, flag1, flag2, id;
dfb56d6b 797
798 sprintf(outf, "%s/sloc.db", HESIOD_DIR);
799
800 if (stat(outf, &sb) == 0) {
801 ftime = sb.st_mtime;
802 filetime = ingres_date_and_time(ftime);
803## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
804## where tblstats.table = "serverhosts"
805## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
806## where tblstats.table = "machine"
807 if (flag1 < 0 && flag2 < 0) {
808 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
f575c498 809 return(0);
dfb56d6b 810 }
811 }
812
f575c498 813 sprintf(outft, "%s~", outf);
814 out = fopen(outft, "w");
dfb56d6b 815 if (!out) {
816 perror("cannot open sloc.db for write");
f575c498 817 exit(SMS_OCONFIG);
dfb56d6b 818 }
819
820 fprintf(stderr, "Building sloc.db\n");
bd8a14a5 821 get_mach();
dfb56d6b 822
823## range of s is serverhosts
bd8a14a5 824## retrieve (service = s.#service, id = s.mach_id)
825## sort by #service {
826 strtrim(service);
827 if (mach = hash_lookup(machines, id))
828 fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
dfb56d6b 829## }
830
831 if (fclose(out)) {
832 fprintf(stderr, "Unsuccessful close of sloc.db\n");
f575c498 833 exit(SMS_CCONFIG);
834 }
835
bd8a14a5 836 fix_file(outf);
837 return(1);
f575c498 838##}
839
840do_service()
841##{
842 FILE *out;
843 char outf[64], outft[64];
844 struct stat sb;
845 time_t ftime;
846## char mach[33], service[33], protocol[9], altserv[129], *filetime;
847## int port, flag1, flag2;
dfb56d6b 848
dfb56d6b 849 sprintf(outf, "%s/service.db", HESIOD_DIR);
850
851 if (stat(outf, &sb) == 0) {
852 ftime = sb.st_mtime;
853 filetime = ingres_date_and_time(ftime);
854## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
855## where tblstats.table = "services"
856 if (flag1 < 0) {
857 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
858 return(0);
859 }
860 }
861
f575c498 862 sprintf(outft, "%s~", outf);
863 out = fopen(outft, "w");
dfb56d6b 864 if (!out) {
865 perror("cannot open service.db for write");
f575c498 866 exit(SMS_OCONFIG);
dfb56d6b 867 }
868
869 fprintf(stderr, "Building service.db\n");
870
871## range of s is services
872## retrieve (service = s.name, protocol = lowercase(s.#protocol),
873## port = s.#port) {
bd8a14a5 874 strtrim(service);
875 strtrim(protocol);
dfb56d6b 876 fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
877 service, service, protocol, port);
878## }
879
f575c498 880## range of a is alias
881## retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
bd8a14a5 882 strtrim(service);
883 strtrim(altserv);
f575c498 884 fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
885## }
886
dfb56d6b 887 if (fclose(out)) {
888 fprintf(stderr, "Unsuccessful close of service.db\n");
f575c498 889 exit(SMS_CCONFIG);
dfb56d6b 890 }
f575c498 891 fix_file(outf);
dfb56d6b 892 return(1);
893##}
This page took 0.211642 seconds and 5 git commands to generate.