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