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