]> andersk Git - moira.git/blob - gen/hesiod.qc
3458d3ef500705bd6b32b22feb58fca70a8fa1e2
[moira.git] / gen / hesiod.qc
1 /* $Header$
2  *
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.
6  */
7
8 #include <stdio.h>
9 #include <sms.h>
10 #include <sms_app.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <ctype.h>
15
16
17 #define HESIOD_DIR      "/u1/sms/dcm/hesiod"
18
19 #define min(x,y)        ((x) < (y) ? (x) : (y))
20 struct hash *machines = NULL;
21 struct hash *users = NULL;
22 char *whoami = "hesiod.gen";
23
24 struct grp {
25     struct grp *next;
26     int id;
27 };
28 struct user {
29     char name[9];
30     struct grp *lists;
31 };
32
33 char *malloc(), *strsave();
34 char *ingres_date_and_time(), *ingres_time(), *ingres_date();
35
36 main(argc, argv)
37 int argc;
38 char **argv;
39 {
40     char cmd[64];
41     struct stat sb;
42     int changed = 0;
43     int ingerr();
44
45     if (argc > 2) {
46         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
47         exit(SMS_ARGS);
48     }
49
50     IIseterr(ingerr);
51
52 ##  ingres sms
53
54     changed = do_passwd();
55     changed += do_pobox();
56     changed += do_filsys();
57     changed += do_cluster();
58     changed += do_printers();
59     changed += do_sloc();
60     changed += do_service();
61     changed += do_groups();
62
63 ##  exit
64
65     if (!changed) {
66         fprintf(stderr, "No files updated.\n");
67         if (argc == 2 && stat(argv[1], &sb) == 0)
68           exit(SMS_NO_CHANGE);
69     }
70
71     if (argc == 2) {
72         fprintf(stderr, "Building tar file.\n");
73         sprintf(cmd, "cd %s; tar cf %s .", HESIOD_DIR, argv[1]);
74         if (system(cmd))
75           exit(SMS_TAR_FAIL);
76     }
77
78     exit(SMS_SUCCESS);
79 }
80
81
82 /*
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.
86  */
87 #define INGRES_DEADLOCK 4700
88
89 static int ingerr(num)
90     int *num;
91 {
92     char buf[256];
93     int ingres_errno;
94
95     switch (*num) {
96     case INGRES_DEADLOCK:
97         ingres_errno = SMS_DEADLOCK;
98         break;
99     default:
100         ingres_errno = SMS_INGRES_ERR;
101     }
102     com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
103     critical_alert("DCM", "Hesiod build encountered INGRES ERROR %d", *num);
104     exit(ingres_errno);
105 }
106
107
108 get_mach()
109 ##{
110 ##  int id;
111 ##  char name[33];
112
113     if (machines)
114       return;
115
116     machines = create_hash(1000);
117 ##  retrieve (name = machine.#name, id = machine.mach_id) {
118         hash_store(machines, id, strsave(strtrim(name)));
119 ##  }
120 ##}
121
122
123 do_passwd()
124 ##{
125     FILE *pout, *uout;
126     char poutf[64], uoutf[64], poutft[64], uoutft[64];
127     struct stat psb, usb;
128     time_t ftime;
129     struct user *u;
130 ##  char login[9], shell[33], fullname[33], oa[17], op[13], hp[17], *filetime;
131 ##  char nn[17];
132 ##  int uid, flag, id;
133
134     sprintf(poutf, "%s/passwd.db", HESIOD_DIR);
135     sprintf(uoutf, "%s/uid.db", HESIOD_DIR);
136
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"
142         if (flag < 0) {
143             fprintf(stderr, "Files passwd.db and uid.db do not need to be rebuilt.\n");
144             return(0);
145       }
146     }
147
148     sprintf(poutft, "%s~", poutf);
149     pout = fopen(poutft, "w");
150     if (!pout) {
151         perror("cannot open passwd.db~ for write");
152         exit(SMS_OCONFIG);
153     }
154     sprintf(uoutft, "%s~", uoutf);
155     uout = fopen(uoutft, "w");
156     if (!uout) {
157         perror("cannot open uid.db~ for write");
158         exit(SMS_OCONFIG);
159     }
160
161     fprintf(stderr, "Building passwd.db and uid.db\n");
162
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 {
169             strtrim(login);
170             dequote(fullname);
171             dequote(nn);
172             dequote(oa);
173             dequote(op);
174             dequote(hp);
175             dequote(shell);
176             u = (struct user *) malloc(sizeof(struct user));
177             strcpy(u->name, login);
178             u->lists = NULL;
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);
183 ##  }
184
185     if (fclose(pout) || fclose(uout)) {
186         fprintf(stderr, "Unsuccessful file close of passwd.db or uid.db\n");
187         exit(SMS_CCONFIG);
188     }
189     fix_file(poutf);
190     fix_file(uoutf);
191     return(1);
192 ##}
193
194
195 do_pobox()
196 ##{
197     FILE *out;
198     char outf[64], outft[64], *mach;
199     struct stat sb;
200     time_t ftime;
201 ##  char login[9], *filetime;
202 ##  int flag1, flag2, id;
203
204     sprintf(outf, "%s/pobox.db", HESIOD_DIR);
205
206     if (stat(outf, &sb) == 0) {
207         ftime = sb.st_mtime;
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");
215             return(0);
216       }
217     }
218
219     sprintf(outft, "%s~", outf);
220     out = fopen(outft, "w");
221     if (!out) {
222         perror("cannot open pobox.db for write");
223         exit(SMS_OCONFIG);
224     }
225
226     fprintf(stderr, "Building pobox.db\n");
227     get_mach();
228
229 ##  range of u is users
230 ##  retrieve (login = u.#login, id = u.pop_id)
231 ##      where u.status = 1 and u.potype = "POP" {
232             strtrim(login);
233             if (mach = hash_lookup(machines, id))
234               fprintf(out, "%s.pobox\tHS UNSPECA \"POP %s %s\"\n",
235                       login, mach, login);
236 ##  }
237
238     if (fclose(out)) {
239         fprintf(stderr, "Unsuccessful close of pobox.db\n");
240         exit(SMS_CCONFIG);
241     }
242     fix_file(outf);
243     return(1);
244 ##}
245
246
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.
251  */
252
253 do_groups()
254 ##{
255     FILE *iout, *gout, *lout;
256     char ioutf[64], goutf[64], loutf[64], buf[512], *l;
257     struct hash *groups;
258     register struct bucket *b, **p;
259     struct grp *g;
260     struct user *u;
261     struct stat isb, gsb, lsb;
262     time_t ftime;
263     register struct save_queue *sq;
264     struct save_queue *sq_create();
265     register int i;
266 ##  char name[33], *filetime;
267 ##  int gid, id, lid, flag1, flag2, flag3, maxid;
268
269     /* open files */
270     sprintf(ioutf, "%s/gid.db", HESIOD_DIR);
271     sprintf(goutf, "%s/group.db", HESIOD_DIR);
272     sprintf(loutf, "%s/grplist.db", HESIOD_DIR);
273
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");
285             return(0);
286       }
287     }
288
289     sprintf(buf, "%s~", ioutf);
290     iout = fopen(buf, "w");
291     if (!iout) {
292         perror("cannot open gid.db for write");
293         exit(SMS_OCONFIG);
294     }
295     sprintf(buf, "%s~", goutf);
296     gout = fopen(buf, "w");
297     if (!gout) {
298         perror("cannot open group.db for write");
299         exit(SMS_OCONFIG);
300     }
301     sprintf(buf, "%s~", loutf);
302     lout = fopen(buf, "w");
303     if (!lout) {
304         perror("cannot open grplist.db for write");
305         exit(SMS_OCONFIG);
306     }
307
308     fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
309
310     /* make space for group list */
311     groups = create_hash(15000);
312
313     /* retrieve simple groups */
314 ##  range of l is list
315 ##  begin transaction
316 ##  retrieve (name = l.#name, gid = l.#gid, lid = l.list_id)
317 ##      where l.group != 0 and l.active != 0 {
318             strtrim(name);
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",
323                     name, name, gid);
324 ##  }
325
326     fflush(iout);
327     fflush(gout);
328
329     /* get special cases: lists that aren't groups themselves but are
330      * members of groups. */
331     sq = sq_create();
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
336 ##              list.group != 0 {
337         strtrim(name);
338         if (l = hash_lookup(groups, lid)) {
339             sprintf(buf, "%s:%s:%d", l, name, gid);
340             free(l);
341         } else {
342             sprintf(buf, "%s:%d", name, gid);
343         }
344         hash_store(groups, lid, strsave(buf));
345         sq_save_unique_data(sq, lid);
346 ##  }
347
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));
353                 free(l);
354                 l = strsave(buf);
355             } else {
356                 l = hash_lookup(groups, id);
357             }
358             hash_store(groups, lid, l);
359             sq_save_unique_data(sq, lid);
360 ##      }
361     }
362     sq_destroy(sq);
363
364     /* now do grplists */
365     if (users == NULL) {
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));
371           u->lists = NULL;
372           hash_store(users, id, u);
373 ##      }
374     }
375
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));
381           g->next = u->lists;
382           u->lists = g;
383           g->id = lid;
384       }
385 ##  }
386 ##  end transaction
387
388     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
389         for (b = *p; b; b = b->next) {
390             i = 0;
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)) {
395                   i++;
396                   strcat(buf, l);
397                   if (g->next)
398                     strcat(buf, ":");
399               }
400             strcat(buf, "\"\n");
401             /* Do duplicate detection here */
402             if (i > 1) {
403                 remove_duplicate_groups(buf);
404             }
405             fputs(buf, lout);
406         }
407     }
408
409 #ifdef notdef
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
412  * shortly anyway.
413  */
414     sq_destroy(sq);
415     {
416         register char **p;
417         for (p = &groups[maxid-1]; p >= groups; p--)
418           if (*p)
419             free(*p);
420     }
421     free(groups);
422 #endif
423
424     if (fclose(iout) || fclose(gout) || fclose(lout)) {
425         fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
426         exit(SMS_CCONFIG);
427     }
428     fix_file(ioutf);
429     fix_file(goutf);
430     fix_file(loutf);
431     return(1);
432 ##}
433
434
435 #define MAXGROUPS 256
436
437 remove_duplicate_groups(p)
438 register char *p;
439 {
440     register char *pp;
441     char *index();
442     register int *mp;
443     int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
444
445     bzero(memory, MAXGROUPS * sizeof(int));
446     while (p = index(p, ':')) {
447         i = atoi(++p);
448         for (mp = memory; *mp && mp < mg; mp++)
449           if (*mp == i) {
450               if (p = index(p, ':'))
451                 strcpy(pp, ++p);
452               else {
453                   strcpy(--pp, "\"\n");
454               }
455               p = pp;
456               break;
457           }
458         if (*mp == i)
459           continue;
460         *mp = i;
461         if (!(p = index(p, ':')))
462           return;
463         pp = ++p;
464     }
465 }
466
467
468 do_filsys()
469 ##{
470     FILE *out;
471     char outf[64], outft[64], *mach, machbuf[128];
472     register char *p;
473     struct stat sb;
474     time_t ftime;
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;
478
479     sprintf(outf, "%s/filsys.db", HESIOD_DIR);
480
481     if (stat(outf, &sb) == 0) {
482         ftime = sb.st_mtime;
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");
492             return(0);
493       }
494     }
495
496     sprintf(outft, "%s~", outf);
497     out = fopen(outft, "w");
498     if (!out) {
499         perror("cannot open filsys.db for write");
500         exit(SMS_OCONFIG);
501     }
502
503     fprintf(stderr, "Building filsys.db\n");
504     get_mach();
505
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) {
509             strtrim(name);
510             strtrim(type);
511             if (strcmp(type, "ERR")) {
512                 strtrim(loc);
513                 strtrim(access);
514                 strtrim(mount);
515                 if (mach = hash_lookup(machines, id)) {
516                     for (p = machbuf; *mach && *mach != '.'; mach++) {
517                         if (isupper(*mach))
518                           *p++ = tolower(*mach);
519                         else
520                           *p++ = *mach;
521                         *p = 0;
522                     }
523                     fprintf(out, "%s.filsys\tHS UNSPECA \"%s %s %s %s %s\"\n",
524                             name, type, loc, machbuf, access, mount);
525                 }
526             } else {
527                 strtrim(comments);
528                 fprintf(out, "%s.filsys\tHS UNSPECA \"ERR %s\"\n",
529                         name, comments);
530             }
531 ##  }
532
533
534
535 ##  range of a is alias
536 ##  retrieve (name = a.#name, trans = a.#trans) where a.#type = "FILESYS" {
537         strtrim(name);
538         strtrim(trans);
539         fprintf(out, "%s.filsys\tHS CNAME %s.filsys\n", name, trans);
540 ##  }
541
542     if (fclose(out)) {
543         fprintf(stderr, "Unsuccessful close of filsys.db\n");
544         exit(SMS_CCONFIG);
545     }
546     fix_file(outf);
547     return(1);
548 ##}
549
550
551 /*
552  * Modified from sys/types.h:
553  */
554 int setsize;    /* = howmany(setbits, NSETBITS) */
555
556 typedef long    set_mask;
557 #define NSETBITS        (sizeof(set_mask) * NBBY)       /* bits per mask */
558 #ifndef howmany
559 #define howmany(x, y)   (((x)+((y)-1))/(y))
560 #endif
561
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)))
568
569
570 do_cluster()
571 ##{
572     FILE *out;
573     char outf[64], outft[64], *mach, machbuf[33], *p;
574     struct stat sb;
575     time_t ftime;
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;
579
580     sprintf(outf, "%s/cluster.db", HESIOD_DIR);
581
582     if (stat(outf, &sb) == 0) {
583         ftime = sb.st_mtime;
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");
595             return(0);
596       }
597     }
598
599     sprintf(outft, "%s~", outf);
600     out = fopen(outft, "w");
601     if (!out) {
602         perror("cannot open cluster.db for write");
603         exit(SMS_OCONFIG);
604     }
605
606     fprintf(stderr, "Building cluster.db\n");
607     get_mach();
608
609 ##  range of c is cluster
610 ##  begin transaction
611 ##  retrieve (maxclu = max(c.clu_id))
612     maxclu++;
613     setsize = howmany(maxclu, NSETBITS);
614 ##  range of m is machine
615 ##  retrieve (maxmach = max(m.mach_id))
616     maxmach++;
617     machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
618     bzero(machs, (maxmach + 1) * sizeof(int));
619
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();
624             SET_ZERO(ms);
625         }
626         SET_SET(cid, ms);
627 ##  }
628
629 ##  range of d is svc
630     for (mid = 1; mid < maxmach; mid++) {
631         if (!machs[mid])
632           continue;
633         ms = machs[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 {
638                     strtrim(label);
639                     strtrim(data);
640                     fprintf(out,
641                             "smsinternal-%d.cluster\tHS UNSPECA \"%s %s\"\n",
642                             mid, label, data);
643 ##              }
644             }
645         }
646
647         if (mach = hash_lookup(machines, mid)) {
648             for (p = machbuf; *mach && *mach != '.'; mach++)
649               *p++ = *mach;
650             *p = 0;
651             fprintf(out, "%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
652                     machbuf, mid);
653         }
654         for (id = mid + 1; id < maxmach; id++) {
655             if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
656                 free(ps);
657                 machs[id] = NULL;
658                 if (mach = hash_lookup(machines, id)) {
659                     for (p = machbuf; *mach && *mach != '.'; mach++)
660                       *p++ = *mach;
661                     *p = 0;
662                     fprintf(out,"%s.cluster\tHS CNAME smsinternal-%d.cluster\n",
663                             machbuf, mid);
664                 }
665             }
666         }
667         free(ms);
668         machs[mid] = NULL;
669     }
670
671 ##  retrieve (name = c.#name, label = d.serv_label, data = d.serv_cluster)
672 ##      where c.clu_id = d.clu_id {
673             strtrim(name);
674             strtrim(label);
675             strtrim(data);
676             fprintf(out, "%s.cluster\tHS UNSPECA \"%s %s\"\n",
677                     name, label, data);
678 ##  }
679 ##  end transaction
680     free(machs);
681
682     if (fclose(out)) {
683         fprintf(stderr, "Unsuccessful close of cluster.db\n");
684         exit(SMS_CCONFIG);
685     }
686     fix_file(outf);
687     return(1);
688 ##}
689
690
691 do_printers()
692 ##{
693     FILE *out;
694     char outf[64], outft[64];
695     struct stat sb;
696     time_t ftime;
697 ##  char name[17], rp[17], sd[33], rm[33], *filetime;
698 ##  int flag;
699
700     sprintf(outf, "%s/printcap.db", HESIOD_DIR);
701
702     if (stat(outf, &sb) == 0) {
703         ftime = sb.st_mtime;
704         filetime = ingres_date_and_time(ftime);
705 ##      retrieve (flag = int4(interval("min", tblstats.modtime - filetime)))
706 ##          where tblstats.table = "printcap"
707         if (flag < 0) {
708             fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
709             return(0);
710       }
711     }
712
713     sprintf(outft, "%s~", outf);
714     out = fopen(outft, "w");
715     if (!out) {
716         perror("cannot open printcap.db for write");
717         exit(SMS_OCONFIG);
718     }
719
720     fprintf(stderr, "Building printcap.db\n");
721
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 {
725         strtrim(name);
726         strtrim(rp);
727         strtrim(sd);
728         strtrim(rm);
729         fprintf(out, "%s.pcap\tHS UNSPECA \"%s:rp=%s:rm=%s:sd=%s\"\n",
730                 name, name, rp, rm, sd);
731 ##  }
732
733     if (fclose(out)) {
734         fprintf(stderr, "Unsuccessful close of pcap.db\n");
735         exit(SMS_CCONFIG);
736     }
737     fix_file(outf);
738     return(1);
739 ##}
740
741
742 do_sloc()
743 ##{
744     FILE *out, *old, *new;
745     char outf[64], outft[64], *mach;
746     struct stat sb;
747     time_t ftime;
748     register int c;
749 ##  char service[17], *filetime;
750 ##  int port, flag1, flag2, id;
751
752     sprintf(outf, "%s/sloc.db", HESIOD_DIR);
753
754     if (stat(outf, &sb) == 0) {
755         ftime = sb.st_mtime;
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");
763             return(0);
764       }
765     }
766
767     sprintf(outft, "%s~", outf);
768     out = fopen(outft, "w");
769     if (!out) {
770         perror("cannot open sloc.db for write");
771         exit(SMS_OCONFIG);
772     }
773
774     fprintf(stderr, "Building sloc.db\n");
775     get_mach();
776
777 ##  range of s is serverhosts
778 ##  retrieve (service = s.#service, id = s.mach_id)
779 ##      sort by #service {
780             strtrim(service);
781             if (mach = hash_lookup(machines, id))
782               fprintf(out, "%s.sloc\tHS UNSPECA %s\n", service, mach);
783 ##  }
784
785     if (fclose(out)) {
786         fprintf(stderr, "Unsuccessful close of sloc.db\n");
787         exit(SMS_CCONFIG);
788     }
789
790     fix_file(outf);
791     return(1);
792 ##}
793
794 do_service()
795 ##{
796     FILE *out;
797     char outf[64], outft[64];
798     struct stat sb;
799     time_t ftime;
800 ##  char mach[33], service[33], protocol[9], altserv[129], *filetime;
801 ##  int port, flag1, flag2;
802
803     sprintf(outf, "%s/service.db", HESIOD_DIR);
804
805     if (stat(outf, &sb) == 0) {
806         ftime = sb.st_mtime;
807         filetime = ingres_date_and_time(ftime);
808 ##      retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
809 ##          where tblstats.table = "services"
810         if (flag1 < 0) {
811             fprintf(stderr, "File service.db does not need to be rebuilt.\n");
812             return(0);
813       }
814     }
815
816     sprintf(outft, "%s~", outf);
817     out = fopen(outft, "w");
818     if (!out) {
819         perror("cannot open service.db for write");
820         exit(SMS_OCONFIG);
821     }
822
823     fprintf(stderr, "Building service.db\n");
824
825 ##  range of s is services
826 ##  retrieve (service = s.name, protocol = lowercase(s.#protocol),
827 ##            port = s.#port) {
828         strtrim(service);
829         strtrim(protocol);
830         fprintf(out, "%s.service\tHS UNSPECA \"%s %s %d\"\n",
831                 service, service, protocol, port);
832 ##  }
833
834 ##  range of a is alias
835 ##  retrieve (service = a.name, altserv = a.trans) where a.type = "SERVICE" {
836         strtrim(service);
837         strtrim(altserv);
838         fprintf(out, "%s.service\tHS CNAME %s.service\n", service, altserv);
839 ##    }
840
841     if (fclose(out)) {
842         fprintf(stderr, "Unsuccessful close of service.db\n");
843         exit(SMS_CCONFIG);
844     }
845     fix_file(outf);
846     return(1);
847 ##}
This page took 1.490019 seconds and 3 git commands to generate.