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