]> andersk Git - moira.git/blob - gen/hesiod.dc
5344049050930f95318f2eb20efc235d19434e23
[moira.git] / gen / hesiod.dc
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, 1990 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 <moira.h>
15 #include <moira_site.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <ctype.h>
20 #include <string.h>
21 EXEC SQL INCLUDE sqlca;
22
23
24 #ifndef HTYPE
25 #define HTYPE "UNSPECA"
26 #endif
27 #ifndef HCLASS
28 #define HCLASS "HS"
29 #endif
30
31 #ifndef HESIOD_SUBDIR
32 #define HESIOD_SUBDIR "hesiod"
33 #endif
34
35 /* max number of bytes of a data record that can be returned in a hesiod 
36  * query.  This is 512 - overhead (~66) [derived empirically]
37  */
38 #define MAXHESSIZE 446
39
40 char hesiod_dir[64];
41
42 #define min(x,y)        ((x) < (y) ? (x) : (y))
43 struct hash *machines = NULL;
44 struct hash *users = NULL;
45 char *whoami = "hesiod.gen";
46 char *db = "moira/moira";
47
48 struct grp {
49     struct grp *next;
50     char *lid;
51 };
52 struct user {
53     char name[9];
54     struct grp *lists;
55 };
56
57 void lowercase();
58 char *malloc(), *strsave();
59
60
61 main(argc, argv)
62 int argc;
63 char **argv;
64 {
65     char cmd[64];
66     struct stat sb;
67     int changed = 0;
68
69     if (argc > 2) {
70         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
71         exit(MR_ARGS);
72     }
73
74     initialize_sms_error_table ();
75     sprintf(hesiod_dir, "%s/%s", DCM_DIR, HESIOD_SUBDIR);
76
77     EXEC SQL CONNECT :db;
78
79     changed = do_passwd();
80     changed += do_filsys();
81     changed += do_cluster();
82     changed += do_printcap();
83     changed += do_palladium();
84     changed += do_sloc();
85     changed += do_service();
86     changed += do_groups();
87
88     if (!changed) {
89         fprintf(stderr, "No files updated.\n");
90         if (argc == 2 && stat(argv[1], &sb) == 0)
91           exit(MR_NO_CHANGE);
92     }
93
94     if (argc == 2) {
95         fprintf(stderr, "Building tar file.\n");
96         sprintf(cmd, "cd %s; tar cf %s .", hesiod_dir, argv[1]);
97         if (system(cmd))
98           exit(MR_TAR_FAIL);
99     }
100
101     exit(MR_SUCCESS);
102 }
103
104
105 get_mach()
106 {
107     EXEC SQL BEGIN DECLARE SECTION;
108     int id;
109     char name[41];
110     EXEC SQL END DECLARE SECTION;
111
112     if (machines)
113       return;
114
115     machines = create_hash(1000);
116     EXEC SQL DECLARE m_cursor CURSOR FOR
117       SELECT name, mach_id
118       FROM machine
119       WHERE status = 1 and mach_id != 0
120       ORDER BY mach_id;
121     EXEC SQL OPEN m_cursor;
122     while (1) {
123         EXEC SQL FETCH m_cursor INTO :name, :id;
124         if (sqlca.sqlcode != 0) break;
125         hash_store(machines, id, strsave(strtrim(name)));
126     }
127     if (sqlca.sqlcode < 0) db_error(sqlca.sqlcode);
128     EXEC SQL CLOSE m_cursor;
129     EXEC SQL COMMIT;
130 }
131
132
133 do_passwd()
134 {
135     FILE *pout, *uout, *bout;
136     char poutf[64], uoutf[64], poutft[64], uoutft[64], boutf[64], boutft[64];
137     struct stat psb, usb, bsb;
138     time_t ftime;
139     struct user *u;
140     char *mach;
141     EXEC SQL BEGIN DECLARE SECTION;
142     char login[9], shell[33], fullname[33], oa[17], op[13], hp[17];
143     char nn[17], ptype[9];
144     int uid, flag1, flag2, id, pid, status;
145     EXEC SQL END DECLARE SECTION;
146
147     sprintf(poutf, "%s/passwd.db", hesiod_dir);
148     sprintf(uoutf, "%s/uid.db", hesiod_dir);
149     sprintf(boutf, "%s/pobox.db", hesiod_dir);
150
151     if (stat(poutf, &psb) == 0 && stat(uoutf, &usb) == 0 &&
152         stat(boutf, &bsb) == 0) {
153         ftime = min(min(psb.st_mtime, usb.st_mtime), bsb.st_mtime);
154         if (ModDiff (&flag1, "users", ftime) ||
155             ModDiff (&flag2, "machine", ftime))
156             exit (MR_DATE);
157         if (flag1 < 0 && flag2 < 0) {
158             fprintf(stderr, "Files passwd.db, uid.db, and pobox.db do not need to be rebuilt.\n");
159             return(0);
160       }
161     }
162
163     sprintf(poutft, "%s~", poutf);
164     pout = fopen(poutft, "w");
165     if (!pout) {
166         perror("cannot open passwd.db~ for write");
167         exit(MR_OCONFIG);
168     }
169     sprintf(uoutft, "%s~", uoutf);
170     uout = fopen(uoutft, "w");
171     if (!uout) {
172         perror("cannot open uid.db~ for write");
173         exit(MR_OCONFIG);
174     }
175     sprintf(boutft, "%s~", boutf);
176     bout = fopen(boutft, "w");
177     if (!bout) {
178         perror("cannot open pobox.db for write");
179         exit(MR_OCONFIG);
180     }
181
182     fprintf(stderr, "Building passwd.db, uid.db, and pobox.db\n");
183     get_mach();
184
185     users = create_hash(12001);
186     EXEC SQL DECLARE u_cursor CURSOR FOR
187       SELECT login, unix_uid, shell, fullname, nickname, office_addr,
188              office_phone, home_phone, users_id, pop_id, potype, status
189       FROM users
190       WHERE status = 1 or status = 5 or status = 6
191       ORDER BY users_id;
192     EXEC SQL OPEN u_cursor;
193     while (1) {
194         EXEC SQL FETCH u_cursor INTO :login, :uid, :shell, :fullname, :nn,
195                        :oa, :op, :hp, :id, :pid, :ptype, :status;
196             if (sqlca.sqlcode != 0) break;
197             strtrim(login);
198             dequote(fullname);
199             dequote(nn);
200             dequote(oa);
201             dequote(op);
202             dequote(hp);
203             dequote(shell);
204             u = (struct user *) malloc(sizeof(struct user));
205             strcpy(u->name, login);
206             u->lists = NULL;
207             hash_store(users, id, u);
208             if (status == 1) {
209                 fprintf(pout, "%s.passwd\t%s %s \"%s:*:%d:101:%s,%s,%s,%s,%s:/mit/%s:%s\"\n",
210                         login, HCLASS, HTYPE, login, uid, fullname, nn, oa,
211                         op, hp, login, shell);
212                 fprintf(uout, "%d.uid\t%s CNAME %s.passwd\n", uid, HCLASS,
213                         login);
214             }
215             if (pid != 0 && (mach = hash_lookup(machines, pid))) {
216                 fprintf(bout, "%s.pobox\t%s %s \"POP %s %s\"\n",
217                         login, HCLASS, HTYPE, mach, login);
218             }
219     }
220     if (sqlca.sqlcode < 0) db_error(sqlca.sqlcode);
221     EXEC SQL CLOSE u_cursor;
222     EXEC SQL COMMIT;
223
224     if (fclose(pout) || fclose(uout) || fclose(bout)) {
225         fprintf(stderr, "Unsuccessful file close of passwd.db, uid.db, or pobox.db\n");
226         exit(MR_CCONFIG);
227     }
228     fix_file(poutf);
229     fix_file(uoutf);
230     fix_file(boutf);
231     return(1);
232 }
233
234
235 do_groups()
236 {
237     FILE *iout, *gout, *lout;
238     char ioutf[64], goutf[64], loutf[64], buf[2048], *l;
239     struct hash *groups;
240     register struct bucket *b, **p;
241     struct grp *g;
242     struct user *u;
243     struct stat isb, gsb, lsb;
244     time_t ftime;
245     EXEC SQL BEGIN DECLARE SECTION;
246     char name[33];
247     int gid, id, lid, flag1, flag2, flag3, len;
248     EXEC SQL END DECLARE SECTION;
249
250     /* open files */
251     sprintf(ioutf, "%s/gid.db", hesiod_dir);
252     sprintf(goutf, "%s/group.db", hesiod_dir);
253     sprintf(loutf, "%s/grplist.db", hesiod_dir);
254
255     if (stat(ioutf, &isb) == 0 && stat(goutf, &gsb) == 0 && stat(loutf, &lsb) == 0) {
256         ftime = min(isb.st_mtime, min(gsb.st_mtime, lsb.st_mtime));
257         if (ModDiff (&flag1, "users", ftime) ||
258             ModDiff (&flag2, "list", ftime) ||
259             ModDiff (&flag3, "imembers", ftime))
260             exit (MR_DATE);
261         if (flag1 < 0 && flag2 < 0 && flag3 < 0) {
262             fprintf(stderr, "Files gid.db, group.db and grplist.db do not need to be rebuilt.\n");
263             return(0);
264       }
265     }
266
267     sprintf(buf, "%s~", ioutf);
268     iout = fopen(buf, "w");
269     if (!iout) {
270         perror("cannot open gid.db for write");
271         exit(MR_OCONFIG);
272     }
273     sprintf(buf, "%s~", goutf);
274     gout = fopen(buf, "w");
275     if (!gout) {
276         perror("cannot open group.db for write");
277         exit(MR_OCONFIG);
278     }
279     sprintf(buf, "%s~", loutf);
280     lout = fopen(buf, "w");
281     if (!lout) {
282         perror("cannot open grplist.db for write");
283         exit(MR_OCONFIG);
284     }
285
286     fprintf(stderr, "Building gid.db, group.db, and grplist.db\n");
287
288     /* make space for group list */
289     groups = create_hash(15001);
290
291     /* The following WHENEVER is declarative, not executed,
292      * and applies for the remainder of this file only.
293      */
294     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
295
296     EXEC SQL DECLARE l_cursor CURSOR FOR
297       SELECT name, gid, list_id
298       FROM list
299       WHERE grouplist != 0 AND active != 0
300       ORDER BY list_id;
301     EXEC SQL OPEN l_cursor;
302     while (1) {
303         EXEC SQL FETCH l_cursor INTO :name, :gid, :lid;
304         if (sqlca.sqlcode != 0) break;
305         strtrim(name);
306         sprintf(buf, "%s:%d", name, gid);
307         hash_store(groups, lid, strsave(buf));
308         fprintf(iout, "%d.gid\t%s CNAME %s.group\n", gid, HCLASS, name);
309         fprintf(gout, "%s.group\t%s %s \"%s:*:%d:\"\n",
310                 name, HCLASS, HTYPE, name, gid);
311     }
312     EXEC SQL CLOSE l_cursor;
313
314     fflush(iout);
315     fflush(gout);
316
317     /* now do grplists */
318     if (users == NULL) {
319         users = create_hash(12001);
320         EXEC SQL DECLARE u_cursor2 CURSOR FOR
321           SELECT users_id, login
322           FROM users
323           WHERE status = 1
324           ORDER BY users_id;
325         EXEC SQL OPEN u_cursor2;
326         while (1) {
327             EXEC SQL FETCH u_cursor2 INTO :id, :name;
328             if (sqlca.sqlcode != 0) break;
329             u = (struct user *) malloc(sizeof(struct user));
330             strcpy(u->name, strtrim(name));
331             u->lists = NULL;
332             hash_store(users, id, u);
333         }
334         EXEC SQL CLOSE u_cursor2;
335     }
336
337     EXEC SQL DECLARE i_cursor CURSOR FOR
338       SELECT list_id, member_id
339       FROM imembers
340       WHERE member_type = 'USER'
341       ORDER BY list_id;
342     EXEC SQL OPEN i_cursor;
343     while (1) {
344         EXEC SQL FETCH i_cursor INTO :lid, :id;
345         if (sqlca.sqlcode != 0) break;
346         if (((l = hash_lookup(groups, lid)) != NULL) &&
347             (u = (struct user *) hash_lookup(users, id))) {
348             g = (struct grp *) malloc(sizeof(struct grp));
349             g->next = u->lists;
350             u->lists = g;
351             g->lid = l;
352         }
353     }
354     EXEC SQL CLOSE i_cursor;
355
356     EXEC SQL COMMIT;
357
358     for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
359         for (b = *p; b; b = b->next) {
360             if ((g = ((struct user *)b->data)->lists) == NULL)
361               continue;
362             fprintf(lout, "%s.grplist\t%s %s \"",
363                     ((struct user *)b->data)->name, HCLASS, HTYPE);
364             len = 0;
365             for (; g; g = g->next) {
366                 if (len + strlen(g->lid) + 1 < MAXHESSIZE) {
367                     fputs(g->lid, lout);
368                     if (g->next)
369                       putc(':', lout);
370                     len += strlen(g->lid) + 1;
371                 } else {
372                     com_err(whoami, 0, "truncated grp list for user %s",
373                             ((struct user *)b->data)->name);
374                     break;
375                 }
376             }
377             fputs("\"\n", lout);
378         }
379     }
380
381     if (fclose(iout) || fclose(gout) || fclose(lout)) {
382         fprintf(stderr, "Unsuccessful close of gid.db, group.db, or grplist.db\n");
383         exit(MR_CCONFIG);
384     }
385     fix_file(ioutf);
386     fix_file(goutf);
387     fix_file(loutf);
388     return(1);
389  sqlerr:
390     db_error(sqlca.sqlcode);
391     return(0);
392 }
393
394
395 do_filsys()
396 {
397     FILE *out;
398     char outf[64], outft[64], *mach, *group;
399     register char *p;
400     struct stat sb;
401     time_t ftime;
402     struct save_queue *sq, *sq2, *sq_create();
403     EXEC SQL BEGIN DECLARE SECTION;
404     char name[33], type[9], loc[81], access[2], mount[33], trans[257];
405     char comments[65];
406     int flag1, flag2, flag3, flag4, id, fid;
407     EXEC SQL END DECLARE SECTION;
408
409     sprintf(outf, "%s/filsys.db", hesiod_dir);
410
411     if (stat(outf, &sb) == 0) {
412         ftime = sb.st_mtime;
413
414         if (ModDiff(&flag1, "filesys", ftime)) exit (MR_DATE);
415         if (ModDiff(&flag2, "machine", ftime)) exit (MR_DATE);
416         if (ModDiff(&flag3, "alias", ftime)) exit (MR_DATE);
417         if (ModDiff(&flag4, "fsgroup", ftime)) exit (MR_DATE);
418
419         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
420             fprintf(stderr, "File filsys.db does not need to be rebuilt.\n");
421             return(0);
422       }
423     }
424
425     sprintf(outft, "%s~", outf);
426     out = fopen(outft, "w");
427     if (!out) {
428         perror("cannot open filsys.db for write");
429         exit(MR_OCONFIG);
430     }
431
432     fprintf(stderr, "Building filsys.db\n");
433     get_mach();
434     sq = sq_create();
435     sq2 = sq_create();
436
437     EXEC SQL DECLARE f_cursor CURSOR FOR
438       SELECT label, type, name, mach_id, rwaccess, mount, comments, filsys_id
439       FROM filesys
440       ORDER BY filsys_id;
441     EXEC SQL OPEN f_cursor;
442     while (1) {
443         EXEC SQL FETCH f_cursor INTO :name, :type, :loc, :id, :access, 
444                                    :mount, :comments, :fid;
445         if (sqlca.sqlcode != 0) break;
446         strtrim(type);
447         if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
448             if (mach = hash_lookup(machines, id)) {
449                 fprintf(out, "%s.filsys\t%s %s \"%s %s %s %s %s\"\n",
450                         strtrim(name), HCLASS, HTYPE, type, strtrim(loc),
451                         mach, strtrim(access), strtrim(mount));
452             }
453         } else if (!strcmp(type, "AFS")) {
454             fprintf(out, "%s.filsys\t%s %s \"AFS %s %s %s\"\n",
455                     strtrim(name), HCLASS, HTYPE, strtrim(loc),
456                     strtrim(access), strtrim(mount));
457         } else if (!strcmp(type, "ERR")) {
458             fprintf(out, "%s.filsys\t%s %s \"ERR %s\"\n",
459                     strtrim(name), HCLASS, HTYPE, strtrim(comments));
460         } else if (!strcmp(type, "FSGROUP")) {
461             sprintf(trans, "%s:%d", strtrim(name), fid);
462             sq_save_data(sq, strsave(trans));
463         } else if (!strcmp(type, "MUL")) {
464             sprintf(trans, "%s:%d", strtrim(name), fid);
465             sq_save_data(sq2, strsave(trans));
466         }
467     }
468     EXEC SQL CLOSE f_cursor;
469
470     while (sq_get_data(sq, &group)) {
471         fid = atoi(strchr(group, ':')+1);
472         *strchr(group, ':') = 0;
473
474         EXEC SQL DECLARE f_cursor2 CURSOR FOR
475           SELECT DISTINCT f.type, f.name, f.mach_id, f.rwaccess, f.mount,
476                 f.comments, f.label, g.key
477           FROM filesys f, fsgroup g
478           WHERE f.filsys_id = g.filsys_id AND g.group_id = :fid
479           ORDER BY key, label;
480         EXEC SQL OPEN f_cursor2;
481         for (flag1=1;;flag1++) {
482             EXEC SQL FETCH f_cursor2 INTO :type, :loc, :id, :access,:mount,
483                                     :comments, :name, :trans;
484             if (sqlca.sqlcode != 0) break;
485             strtrim(type);
486             if (!strcmp(type, "NFS") || !strcmp(type, "RVD")) {
487                 if (mach = hash_lookup(machines, id)) {
488                     fprintf(out, "%s.filsys\t%s %s \"%s %s %s %s %s %d\"\n",
489                             group, HCLASS, HTYPE, type, strtrim(loc), mach,
490                             strtrim(access), strtrim(mount), flag1);
491                 }
492             } else if (!strcmp(type, "AFS")) {
493                 fprintf(out, "%s.filsys\t%s %s \"AFS %s %s %s %d\"\n",
494                         group, HCLASS, HTYPE, strtrim(loc), strtrim(access),
495                         strtrim(mount), flag1);
496             } else if (!strcmp(type, "ERR")) {
497                 fprintf(out, "%s.filsys\t%s %s \"ERR %s\"\n",
498                         group, HCLASS, HTYPE, strtrim(comments));
499             }
500         }
501         EXEC SQL CLOSE f_cursor2;
502         free(group);
503     }
504     sq_destroy(sq);
505
506     while (sq_get_data(sq2, &group)) {
507         fid = atoi(strchr(group, ':') + 1);
508         *strchr(group, ':') = 0;
509         fprintf(out, "%s.filsys\t%s %s \"MUL", group, HCLASS, HTYPE);
510         EXEC SQL DECLARE f_cursor3 CURSOR FOR
511             SELECT DISTINCT f.label, g.key
512             FROM filesys f, fsgroup g
513             WHERE f.filsys_id = g.filsys_id AND g.group_id = :fid
514             ORDER BY key, label;
515         EXEC SQL OPEN f_cursor3;
516         while (1) {
517             EXEC SQL FETCH f_cursor3 INTO :name, :trans;
518             if (sqlca.sqlcode != 0) break;
519             fprintf(out, " %s", strtrim(name));
520         }
521         EXEC SQL CLOSE f_cursor3;
522         fprintf(out, "\"\n");
523         free(group);
524     }
525     sq_destroy(sq2);
526
527     EXEC SQL DECLARE a_cursor CURSOR FOR
528       SELECT name, trans
529       FROM alias
530       WHERE type = 'FILESYS';
531     EXEC SQL OPEN a_cursor;
532     while (1) {
533         EXEC SQL FETCH a_cursor INTO :name, :trans;
534         if (sqlca.sqlcode != 0) break;
535         fprintf(out, "%s.filsys\t%s CNAME %s.filsys\n",
536                 strtrim(name), HCLASS, strtrim(trans));
537     }
538     EXEC SQL CLOSE a_cursor;
539
540     EXEC SQL COMMIT;
541
542     if (fclose(out)) {
543         fprintf(stderr, "Unsuccessful close of filsys.db\n");
544         exit(MR_CCONFIG);
545     }
546     fix_file(outf);
547     return(1);
548  sqlerr:
549     db_error(sqlca.sqlcode);
550     return(0);
551 }
552
553
554 /*
555  * Modified from sys/types.h:
556  */
557 int setsize;    /* = howmany(setbits, NSETBITS) */
558
559 typedef long    set_mask;
560 #define NSETBITS        (sizeof(set_mask) * NBBY)       /* bits per mask */
561 #ifndef howmany
562 #define howmany(x, y)   (((x)+((y)-1))/(y))
563 #endif
564
565 #define SET_SET(n, p)   ((p)[(n)/NSETBITS] |=  (1 << ((n) % NSETBITS)))
566 #define SET_CLR(n, p)   ((p)[(n)/NSETBITS] &= ~(1 << ((n) % NSETBITS)))
567 #define SET_ISSET(n, p) ((p)[(n)/NSETBITS] &   (1 << ((n) % NSETBITS)))
568 #define SET_CREATE()    ((set_mask *)malloc(setsize * sizeof(set_mask)))
569 #define SET_ZERO(p)     memset((char *)(p), 0, setsize * sizeof(set_mask))
570 #define SET_CMP(p1, p2) (memcmp((p1), (p2), setsize * sizeof(set_mask)))
571
572 int nbitsset(set)
573 set_mask *set;
574 {
575     int i, ret;
576     ret = 0;
577     for (i = 0; i < setsize * NSETBITS; i++)
578       if (SET_ISSET(i, set))
579         ret++;
580     return(ret);
581 }
582
583
584 do_cluster()
585 {
586     FILE *out;
587     char outf[64], outft[64], *mach, machbuf[41], *p;
588     struct stat sb;
589     time_t ftime;
590     EXEC SQL BEGIN DECLARE SECTION;
591     int flag1, flag2, flag3, flag4, maxmach, maxclu, mid, cid, id;
592     char name[41], label2[17], data[33];
593     EXEC SQL END DECLARE SECTION;
594     set_mask **machs, *ms, *ps;
595     int oneclu;
596
597     sprintf(outf, "%s/cluster.db", hesiod_dir);
598
599     if (stat(outf, &sb) == 0) {
600         ftime = sb.st_mtime;
601         if (ModDiff (&flag1, "clusters", ftime)
602             || ModDiff (&flag2, "machine", ftime)
603             || ModDiff (&flag3, "mcmap", ftime)
604             || ModDiff (&flag4, "svc", ftime)) exit (MR_DATE);
605         if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
606             fprintf(stderr, "File cluster.db does not need to be rebuilt.\n");
607             return(0);
608       }
609     }
610
611     sprintf(outft, "%s~", outf);
612     out = fopen(outft, "w");
613     if (!out) {
614         perror("cannot open cluster.db for write");
615         exit(MR_OCONFIG);
616     }
617
618     fprintf(stderr, "Building cluster.db\n");
619     get_mach();
620
621     EXEC SQL SELECT MAX(clu_id) INTO :maxclu FROM clusters;
622     maxclu++;
623     setsize = howmany(maxclu, NSETBITS);
624
625     EXEC SQL SELECT MAX(mach_id) INTO :maxmach FROM machine;
626     maxmach++;
627     machs = (set_mask **)malloc((maxmach + 1) * sizeof(set_mask **));
628     memset(machs, 0, (maxmach + 1) * sizeof(int));
629
630     EXEC SQL DECLARE p_cursor CURSOR FOR
631       SELECT mach_id, clu_id
632       FROM mcmap
633       ORDER BY mach_id;
634     EXEC SQL OPEN p_cursor;
635     while (1) {
636         EXEC SQL FETCH p_cursor INTO :mid, :cid;
637         if (sqlca.sqlcode != 0) break;
638         if (!(ms = machs[mid])) {
639             ms = machs[mid] = SET_CREATE();
640             SET_ZERO(ms);
641         }
642         SET_SET(cid, ms);
643     }
644     EXEC SQL CLOSE p_cursor;
645
646     for (mid = 1; mid < maxmach; mid++) {
647         if (!machs[mid])
648           continue;
649         ms = machs[mid];
650         if (nbitsset(ms) > 1) {
651             oneclu = 0;
652             for (cid = 1; cid < maxclu; cid++) {
653                 if (SET_ISSET(cid, ms)) {
654                     EXEC SQL DECLARE d_cursor CURSOR FOR
655                       SELECT serv_label, serv_cluster
656                       FROM svc
657                       WHERE clu_id = :cid;
658                     EXEC SQL OPEN d_cursor;
659                     while (1) {
660                         EXEC SQL FETCH d_cursor INTO :label2, :data;
661                         if (sqlca.sqlcode != 0) break;
662                         strtrim(label2);
663                         strtrim(data);
664                         fprintf(out,
665                                 "mrinternal-%d.cluster\t%s %s \"%s %s\"\n",
666                                 mid, HCLASS, HTYPE, label2, data);
667                     }
668                     EXEC SQL CLOSE d_cursor;
669                 }
670             }
671         } else {
672             oneclu = 1;
673             for (cid = 1; cid < maxclu; cid++)
674               if (SET_ISSET(cid, ms)) break;
675
676             EXEC SQL SELECT name INTO :name FROM clusters WHERE clu_id = :cid;
677             strtrim(name);
678         }
679
680         if (mach = hash_lookup(machines, mid)) {
681             for (p = machbuf; *mach && *mach != '.'; mach++)
682               *p++ = *mach;
683             *p = 0;
684             if (oneclu)
685               fprintf(out, "%s.cluster\t%s CNAME %s.cluster\n",
686                       machbuf, HCLASS, name);
687             else
688               fprintf(out, "%s.cluster\t%s CNAME mrinternal-%d.cluster\n",
689                       machbuf, HCLASS, mid);
690         }
691         for (id = mid + 1; id < maxmach; id++) {
692             if ((ps = machs[id]) && !SET_CMP(ms, ps)) {
693                 free(ps);
694                 machs[id] = NULL;
695                 if (mach = hash_lookup(machines, id)) {
696                     for (p = machbuf; *mach && *mach != '.'; mach++)
697                       *p++ = *mach;
698                     *p = 0;
699                     if (oneclu)
700                       fprintf(out, "%s.cluster\t%s CNAME %s.cluster\n",
701                               machbuf, HCLASS, name);
702                     else
703                       fprintf(out,
704                               "%s.cluster\t%s CNAME mrinternal-%d.cluster\n",
705                               machbuf, HCLASS, mid);
706                 }
707             }
708         }
709         free(ms);
710         machs[mid] = NULL;
711     }
712
713     EXEC SQL DECLARE d_cursor2 CURSOR FOR
714       SELECT c.name, d.serv_label, d.serv_cluster
715       FROM svc d, clusters c
716       WHERE c.clu_id = d.clu_id; 
717     EXEC SQL OPEN d_cursor2; 
718         while (1) { 
719             EXEC SQL FETCH d_cursor2 INTO :name, :label2, :data; 
720             if (sqlca.sqlcode != 0) break;
721             strtrim(name);
722             strtrim(label2);
723             strtrim(data);
724             fprintf(out, "%s.cluster\t%s %s \"%s %s\"\n",
725                     name, HCLASS, HTYPE, label2, data);
726     }
727     free(machs);
728     EXEC SQL COMMIT;
729
730     if (fclose(out)) {
731         fprintf(stderr, "Unsuccessful close of cluster.db\n");
732         exit(MR_CCONFIG);
733     }
734     fix_file(outf);
735     return(1);
736  sqlerr:
737     db_error(sqlca.sqlcode);
738     return(0);
739 }
740
741
742 do_printcap()
743 {
744     FILE *out;
745     char outf[64], outft[64];
746     struct stat sb;
747     time_t ftime;
748     EXEC SQL BEGIN DECLARE SECTION;
749     char name[17], rp[17], sd[33];
750     int flag1, flag2, ka, pc, rm, rq;
751     EXEC SQL END DECLARE SECTION;
752
753     sprintf(outf, "%s/printcap.db", hesiod_dir);
754
755     if (stat(outf, &sb) == 0) {
756         ftime = sb.st_mtime;
757         if (ModDiff (&flag1, "printcap", ftime)
758             || ModDiff (&flag2, "machine", ftime))
759           exit (MR_DATE);
760         if (flag1 < 0 && flag2 < 0) {
761             fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
762             return(0);
763       }
764     }
765
766     sprintf(outft, "%s~", outf);
767     out = fopen(outft, "w");
768     if (!out) {
769         perror("cannot open printcap.db for write");
770         exit(MR_OCONFIG);
771     }
772
773     fprintf(stderr, "Building printcap.db\n");
774     get_mach();
775
776     EXEC SQL DECLARE p_cursor2 CURSOR FOR
777       SELECT name, rp, dir, mach_id, auth, price, quotaserver
778       FROM printcap;
779     EXEC SQL OPEN p_cursor2; 
780     while (1) { 
781         EXEC SQL FETCH p_cursor2 INTO :name, :rp, :sd, :rm, :ka, :pc, :rq; 
782         if (sqlca.sqlcode != 0) break;
783         if (!hash_lookup(machines, rm)) continue;
784         strtrim(name);
785         strtrim(rp);
786         strtrim(sd);
787         fprintf(out, "%s.pcap\t%s %s \"%s:rp=%s:rm=%s:sd=%s:ka#%d:pc#%d",
788                 name, HCLASS, HTYPE, name, rp, hash_lookup(machines, rm),
789                 sd, ka, pc);
790         if (rq && hash_lookup(machines, rq))
791           fprintf(out, ":rq=%s\"\n", hash_lookup(machines, rq));
792         else
793           fputs("\"\n", out);
794     }
795     EXEC SQL CLOSE p_cursor2;
796
797     EXEC SQL COMMIT;
798
799     if (fclose(out)) {
800         fprintf(stderr, "Unsuccessful close of pcap.db\n");
801         exit(MR_CCONFIG);
802     }
803     fix_file(outf);
804     return(1);
805  sqlerr:
806     db_error(sqlca.sqlcode);
807     return(0);
808   }
809
810
811 do_palladium()
812 {
813     FILE *out;
814     char outf[64], outft[64];
815     struct stat sb;
816     time_t ftime;
817     EXEC SQL BEGIN DECLARE SECTION;
818     char name[33], trans[129];
819     int flag, flag1, identifier, rm;
820     EXEC SQL END DECLARE SECTION;
821
822     sprintf(outf, "%s/palladium.db", hesiod_dir);
823
824     if (stat(outf, &sb) == 0) {
825         ftime = sb.st_mtime;
826         if ((ModDiff (&flag, "palladium", ftime)) ||
827             (ModDiff (&flag1, "alias", ftime))) exit (MR_DATE);
828         if (flag < 0 && flag1 < 0) {
829             fprintf(stderr, "File palladium.db does not need to be rebuilt.\n");
830             return(0);
831       }
832     }
833
834     sprintf(outft, "%s~", outf);
835     out = fopen(outft, "w");
836     if (!out) {
837         perror("cannot open palladium.db for write");
838         exit(MR_OCONFIG);
839     }
840
841     fprintf(stderr, "Building palladium.db\n");
842     get_mach();
843
844     EXEC SQL DECLARE p_cursor3 CURSOR FOR
845       SELECT name, identifier, mach_id
846       FROM palladium;
847     EXEC SQL OPEN p_cursor3; 
848     while (1) { 
849         EXEC SQL FETCH p_cursor3 INTO :name, :identifier, :rm;
850         if (sqlca.sqlcode != 0) break;
851         if (!hash_lookup(machines, rm)) break;
852         strtrim(name);
853         fprintf(out,
854                 "%s.palladium\t%s %s \"%s %d %s interface directory\"\n",
855                 name, HCLASS, HTYPE, hash_lookup(machines, rm), identifier,
856                 name);
857     }
858     EXEC SQL CLOSE p_cursor3;
859
860     EXEC SQL DECLARE a_cursor2 CURSOR FOR
861       SELECT name, trans
862       FROM alias
863       WHERE type = 'PALLADIUM';
864     EXEC SQL OPEN a_cursor2; 
865     while (1) { 
866         EXEC SQL FETCH a_cursor2 INTO :name, :trans;
867         if (sqlca.sqlcode != 0) break;
868         strtrim(name);
869         strtrim(trans);
870         fprintf(out, "%s.palladium\t%s %s \"%s\"\n", name, HCLASS, HTYPE,
871                 trans);
872     }
873     EXEC SQL CLOSE a_cursor2;
874
875     EXEC SQL COMMIT;
876
877     if (fclose(out)) {
878         fprintf(stderr, "Unsuccessful close of palladium.db\n");
879         exit(MR_CCONFIG);
880     }
881     fix_file(outf);
882     return(1);
883  sqlerr:
884     db_error(sqlca.sqlcode);
885     return(0);
886 }
887
888
889 do_sloc()
890 {
891     FILE *out;
892     char outf[64], outft[64], *mach;
893     struct stat sb;
894     time_t ftime;
895     EXEC SQL BEGIN DECLARE SECTION;
896     char service[17];
897     int flag1, flag2, id;
898     EXEC SQL END DECLARE SECTION;
899
900     sprintf(outf, "%s/sloc.db", hesiod_dir);
901
902     if (stat(outf, &sb) == 0) {
903         ftime = sb.st_mtime;
904         if ((ModDiff (&flag1, "serverhosts", ftime)) ||
905             (ModDiff (&flag2, "machine", ftime))) exit (MR_DATE);
906         if (flag1 < 0 && flag2 < 0) {
907             fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
908             return(0);
909       }
910     }
911
912     sprintf(outft, "%s~", outf);
913     out = fopen(outft, "w");
914     if (!out) {
915         perror("cannot open sloc.db for write");
916         exit(MR_OCONFIG);
917     }
918
919     fprintf(stderr, "Building sloc.db\n");
920     get_mach();
921
922     EXEC SQL DECLARE s_cursor CURSOR FOR
923       SELECT DISTINCT service, mach_id
924       FROM serverhosts
925       ORDER BY service;
926     EXEC SQL OPEN s_cursor; 
927     while (1) { 
928         EXEC SQL FETCH s_cursor INTO :service, :id;
929         if (sqlca.sqlcode != 0) break;
930         strtrim(service);
931         if (mach = hash_lookup(machines, id))
932           fprintf(out, "%s.sloc\t%s %s %s\n", service, HCLASS, HTYPE, mach);
933     }
934     EXEC SQL CLOSE s_cursor;
935
936     EXEC SQL COMMIT;
937
938     if (fclose(out)) {
939         fprintf(stderr, "Unsuccessful close of sloc.db\n");
940         exit(MR_CCONFIG);
941     }
942
943     fix_file(outf);
944     return(1);
945  sqlerr:
946     db_error(sqlca.sqlcode);
947     return(0);
948 }
949
950 do_service()
951 {
952     FILE *out;
953     char outf[64], outft[64];
954     struct stat sb;
955     time_t ftime;
956     EXEC SQL BEGIN DECLARE SECTION;
957     char service[33], protocol[9], altserv[129];
958     int port, flag1;
959     EXEC SQL END DECLARE SECTION;
960
961     sprintf(outf, "%s/service.db", hesiod_dir);
962
963     if (stat(outf, &sb) == 0) {
964         ftime = sb.st_mtime;
965         if (ModDiff (&flag1, "services", ftime)) exit (MR_DATE);
966         if (flag1 < 0) {
967             fprintf(stderr, "File service.db does not need to be rebuilt.\n");
968             return(0);
969       }
970     }
971
972     sprintf(outft, "%s~", outf);
973     out = fopen(outft, "w");
974     if (!out) {
975         perror("cannot open service.db for write");
976         exit(MR_OCONFIG);
977     }
978
979     fprintf(stderr, "Building service.db\n");
980
981     EXEC SQL DECLARE s_cursor2 CURSOR FOR
982       SELECT name, protocol, port
983       FROM services;
984     EXEC SQL OPEN s_cursor2;
985     while (1) { 
986         EXEC SQL FETCH s_cursor2 INTO :service, :protocol, :port;
987         if (sqlca.sqlcode != 0) break;
988         lowercase(protocol);      /* Convert protocol to lowercase */
989         strtrim(service);
990         strtrim(protocol);
991         fprintf(out, "%s.service\t%s %s \"%s %s %d\"\n",
992                 service, HCLASS, HTYPE, service, protocol, port);
993     }
994     EXEC SQL CLOSE s_cursor2;
995
996     EXEC SQL DECLARE a_cursor3 CURSOR FOR
997       SELECT name, trans
998       FROM alias
999       WHERE type = 'SERVICE';
1000     EXEC SQL OPEN a_cursor3; 
1001     while (1) { 
1002         EXEC SQL FETCH a_cursor3 INTO :service, :altserv;
1003         if (sqlca.sqlcode != 0) break;
1004         strtrim(service);
1005         strtrim(altserv);
1006         fprintf(out, "%s.service\t%s CNAME %s.service\n", service, HCLASS,
1007                 altserv);
1008     }
1009     EXEC SQL CLOSE a_cursor3;
1010
1011     EXEC SQL COMMIT;
1012
1013     if (fclose(out)) {
1014         fprintf(stderr, "Unsuccessful close of service.db\n");
1015         exit(MR_CCONFIG);
1016     }
1017     fix_file(outf);
1018     return(1);
1019  sqlerr:
1020     db_error(sqlca.sqlcode);
1021     return(0);
1022 }
This page took 0.116898 seconds and 3 git commands to generate.