]> andersk Git - moira.git/blame - gen/hesiod.pc
cast result of strlen to (int) so that "COLS - strlen(buf)" will be an
[moira.git] / gen / hesiod.pc
CommitLineData
7ac48069 1/* $Id$
bac4ceaa 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 *
7ac48069 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>.
bac4ceaa 10 */
11
12#include <mit-copyright.h>
bac4ceaa 13#include <moira.h>
14#include <moira_site.h>
7ac48069 15
bac4ceaa 16#include <sys/stat.h>
7ac48069 17
18#include <stdio.h>
19#include <stdlib.h>
9c04b191 20#include <string.h>
7ac48069 21
22#include "util.h"
23
bac4ceaa 24EXEC SQL INCLUDE sqlca;
25
7ac48069 26RCSID("$Header$");
bac4ceaa 27
9c04b191 28#ifndef HTYPE
e14de0ca 29#define HTYPE "TXT"
9c04b191 30#endif
31#ifndef HCLASS
e14de0ca 32#define HCLASS "IN"
9c04b191 33#endif
34
35#ifndef HESIOD_SUBDIR
36#define HESIOD_SUBDIR "hesiod"
bac4ceaa 37#endif
38
5eaef520 39/* max number of bytes of a data record that can be returned in a hesiod
4821a398 40 * query. This is 512 - overhead (~66) [derived empirically]
41 */
42#define MAXHESSIZE 446
43
dfaf9b68 44char hesiod_dir[MAXPATHLEN];
bac4ceaa 45
5eaef520 46#define min(x, y) ((x) < (y) ? (x) : (y))
bac4ceaa 47struct hash *machines = NULL;
48struct hash *users = NULL;
49char *whoami = "hesiod.gen";
9c04b191 50char *db = "moira/moira";
bac4ceaa 51
52struct grp {
5eaef520 53 struct grp *next;
54 char *lid;
bac4ceaa 55};
56struct user {
dfaf9b68 57 char name[USERS_LOGIN_SIZE];
5eaef520 58 struct grp *lists;
bac4ceaa 59};
60
7ac48069 61/*
62 * Modified from sys/types.h:
63 */
64int setsize; /* = howmany(setbits, NSETBITS) */
65
66typedef 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
80void get_mach(void);
81int nbitsset(set_mask *set);
2203d8f9 82int valid(char *name);
7ac48069 83
84int do_passwd(void);
85int do_groups(void);
86int do_filsys(void);
87int do_cluster(void);
88int do_printcap(void);
7ac48069 89int do_sloc(void);
90int do_service(void);
bac4ceaa 91
5eaef520 92int main(int argc, char **argv)
bac4ceaa 93{
5eaef520 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);
bac4ceaa 102 }
103
5eaef520 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();
5eaef520 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);
bac4ceaa 122 }
123
5eaef520 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);
bac4ceaa 130 }
131
5eaef520 132 exit(MR_SUCCESS);
bac4ceaa 133}
134
135
7ac48069 136void get_mach(void)
bac4ceaa 137{
5eaef520 138 EXEC SQL BEGIN DECLARE SECTION;
139 int id;
dfaf9b68 140 char name[MACHINE_NAME_SIZE];
5eaef520 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;
2203d8f9 158 strtrim(name);
159 if (!valid(name))
160 continue;
161 hash_store(machines, id, strdup(name));
5eaef520 162 }
163 if (sqlca.sqlcode < 0)
164 db_error(sqlca.sqlcode);
165 EXEC SQL CLOSE m_cursor;
166 EXEC SQL COMMIT;
bac4ceaa 167}
168
169
2203d8f9 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) */
172int 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
5eaef520 192int do_passwd(void)
bac4ceaa 193{
5eaef520 194 FILE *pout, *uout, *bout;
dfaf9b68 195 char poutf[MAXPATHLEN], uoutf[MAXPATHLEN], poutft[MAXPATHLEN];
196 char uoutft[MAXPATHLEN], boutf[MAXPATHLEN], boutft[MAXPATHLEN];
5eaef520 197 struct stat psb, usb, bsb;
198 time_t ftime;
199 struct user *u;
200 char *mach;
201 EXEC SQL BEGIN DECLARE SECTION;
dfaf9b68 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];
5eaef520 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);
bac4ceaa 248 }
bac4ceaa 249
5eaef520 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
3fe4e44d 258 WHERE status = 1 OR status = 2 OR status = 5 OR status = 6
5eaef520 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)
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 }
bac4ceaa 290 }
5eaef520 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;
bac4ceaa 305}
306
307
5eaef520 308int do_groups(void)
bac4ceaa 309{
5eaef520 310 FILE *iout, *gout, *lout;
dfaf9b68 311 char ioutf[MAXPATHLEN], goutf[MAXPATHLEN], loutf[MAXPATHLEN];
312 char buf[MAXPATHLEN], *l;
5eaef520 313 struct hash *groups;
44d12d58 314 struct bucket *b, **p;
5eaef520 315 struct grp *g;
316 struct user *u;
317 struct stat isb, gsb, lsb;
318 time_t ftime;
319 EXEC SQL BEGIN DECLARE SECTION;
dfaf9b68 320 char name[LIST_NAME_SIZE];
5eaef520 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 {
dfaf9b68 385 char buf[LIST_NAME_SIZE + 10];
386
5eaef520 387 EXEC SQL FETCH l_cursor INTO :name, :gid, :lid;
388 if (sqlca.sqlcode)
389 break;
390 strtrim(name);
2203d8f9 391 if (!valid(name))
392 continue;
5eaef520 393 sprintf(buf, "%s:%d", name, gid);
7ac48069 394 hash_store(groups, lid, strdup(buf));
5eaef520 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
3fe4e44d 411 WHERE status = 1 OR status = 2
5eaef520 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);
bac4ceaa 423 }
5eaef520 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;
7ac48069 438 if ((l = hash_lookup(groups, lid)) && (u = hash_lookup(users, id)))
5eaef520 439 {
440 g = malloc(sizeof(struct grp));
441 g->next = u->lists;
442 u->lists = g;
443 g->lid = l;
bac4ceaa 444 }
445 }
5eaef520 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;
4821a398 473 }
bac4ceaa 474 }
5eaef520 475 fputs("\"\n", lout);
bac4ceaa 476 }
477 }
478
5eaef520 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);
bac4ceaa 483 }
5eaef520 484 fix_file(ioutf);
485 fix_file(goutf);
486 fix_file(loutf);
487 return 1;
488sqlerr:
489 db_error(sqlca.sqlcode);
490 return 0;
bac4ceaa 491}
492
493
5eaef520 494int do_filsys(void)
bac4ceaa 495{
5eaef520 496 FILE *out;
dfaf9b68 497 char outf[MAXPATHLEN], outft[MAXPATHLEN], *mach, *group;
5eaef520 498 struct stat sb;
499 time_t ftime;
7ac48069 500 struct save_queue *sq, *sq2;
5eaef520 501 EXEC SQL BEGIN DECLARE SECTION;
dfaf9b68 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];
5eaef520 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;
2203d8f9 556 strtrim(name);
557 if (!valid(name))
558 continue;
5eaef520 559 strtrim(type);
560 if (!strcmp(type, "NFS") || !strcmp(type, "RVD"))
561 {
7ac48069 562 if ((mach = hash_lookup(machines, id)))
5eaef520 563 {
564 fprintf(out, "%s.filsys\t%s %s \"%s %s %s %s %s\"\n",
2203d8f9 565 name, HCLASS, HTYPE, type, strtrim(loc),
5eaef520 566 mach, strtrim(access), strtrim(mount));
bac4ceaa 567 }
5eaef520 568 }
569 else if (!strcmp(type, "AFS"))
570 {
571 fprintf(out, "%s.filsys\t%s %s \"AFS %s %s %s\"\n",
2203d8f9 572 name, HCLASS, HTYPE, strtrim(loc),
5eaef520 573 strtrim(access), strtrim(mount));
574 }
575 else if (!strcmp(type, "ERR"))
576 {
577 fprintf(out, "%s.filsys\t%s %s \"ERR %s\"\n",
2203d8f9 578 name, HCLASS, HTYPE, strtrim(comments));
5eaef520 579 }
580 else if (!strcmp(type, "FSGROUP"))
581 {
dfaf9b68 582 char buf[FILESYS_NAME_SIZE + 10];
2203d8f9 583 sprintf(buf, "%s:%d", name, fid);
dfaf9b68 584 sq_save_data(sq, strdup(buf));
5eaef520 585 }
586 else if (!strcmp(type, "MUL"))
587 {
dfaf9b68 588 char buf[FILESYS_NAME_SIZE + 10];
2203d8f9 589 sprintf(buf, "%s:%d", name, fid);
dfaf9b68 590 sq_save_data(sq2, strdup(buf));
bac4ceaa 591 }
592 }
5eaef520 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,
dfaf9b68 610 :comments, :name, :key;
5eaef520 611 if (sqlca.sqlcode)
612 break;
613 strtrim(type);
614 if (!strcmp(type, "NFS") || !strcmp(type, "RVD"))
615 {
7ac48069 616 if ((mach = hash_lookup(machines, id)))
5eaef520 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);
bac4ceaa 621 }
5eaef520 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));
bac4ceaa 633 }
634 }
5eaef520 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 {
dfaf9b68 653 EXEC SQL FETCH f_cursor3 INTO :name, :key;
5eaef520 654 if (sqlca.sqlcode)
655 break;
656 fprintf(out, " %s", strtrim(name));
542bdacb 657 }
5eaef520 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 {
dfaf9b68 671 EXEC SQL FETCH a_cursor INTO :aname, :trans;
5eaef520 672 if (sqlca.sqlcode)
673 break;
2203d8f9 674 strtrim(aname);
675 strtrim(trans);
676 if (!valid(aname) || !valid(trans))
677 continue;
5eaef520 678 fprintf(out, "%s.filsys\t%s CNAME %s.filsys\n",
2203d8f9 679 aname, HCLASS, trans);
5eaef520 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;
692sqlerr:
693 db_error(sqlca.sqlcode);
694 return 0;
bac4ceaa 695}
696
dfaf9b68 697int nbitsset(set_mask *set)
bac4ceaa 698{
5eaef520 699 int i, ret;
700 ret = 0;
701 for (i = 0; i < setsize * NSETBITS; i++)
702 {
bac4ceaa 703 if (SET_ISSET(i, set))
704 ret++;
5eaef520 705 }
706 return ret;
bac4ceaa 707}
708
709
5eaef520 710int do_cluster(void)
bac4ceaa 711{
5eaef520 712 FILE *out;
dfaf9b68 713 char outf[MAXPATHLEN], outft[MAXPATHLEN], *mach;
714 char machbuf[MACHINE_NAME_SIZE], clubuf[CLUSTERS_NAME_SIZE], *p;
5eaef520 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;
dfaf9b68 719 char name[CLUSTERS_NAME_SIZE];
720 char label[SVC_SERV_LABEL_SIZE], data[SVC_SERV_CLUSTER_SIZE];
5eaef520 721 EXEC SQL END DECLARE SECTION;
722 set_mask **machs, *ms, *ps;
5eaef520 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;
bac4ceaa 738 }
5eaef520 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 {
ed7faf49 787 sprintf(clubuf, "mrinternal-%d", mid);
5eaef520 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 {
dfaf9b68 799 EXEC SQL FETCH d_cursor INTO :label, :data;
5eaef520 800 if (sqlca.sqlcode)
801 break;
dfaf9b68 802 strtrim(label);
5eaef520 803 strtrim(data);
ed7faf49 804 fprintf(out, "%s.cluster\t%s %s \"%s %s\"\n",
dfaf9b68 805 clubuf, HCLASS, HTYPE, label, data);
bac4ceaa 806 }
5eaef520 807 EXEC SQL CLOSE d_cursor;
bac4ceaa 808 }
809 }
5eaef520 810 }
811 else
812 {
5eaef520 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);
2203d8f9 819 if (!valid(name))
820 continue;
ed7faf49 821 strcpy(clubuf, name);
bac4ceaa 822 }
823
5eaef520 824 if ((mach = hash_lookup(machines, mid)))
825 {
ed7faf49 826 fprintf(out, "%s.cluster\t%s CNAME %s.cluster\n",
827 mach, HCLASS, clubuf);
5eaef520 828 for (p = machbuf; *mach && *mach != '.'; mach++)
829 *p++ = *mach;
ed7faf49 830 if (!strcasecmp(mach, ".mit.edu"))
5eaef520 831 {
ed7faf49 832 *p = '\0';
9c04b191 833 fprintf(out, "%s.cluster\t%s CNAME %s.cluster\n",
ed7faf49 834 machbuf, HCLASS, clubuf);
5eaef520 835 }
bac4ceaa 836 }
5eaef520 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 {
ed7faf49 845 fprintf(out, "%s.cluster\t%s CNAME %s.cluster\n",
846 mach, HCLASS, clubuf);
5eaef520 847 for (p = machbuf; *mach && *mach != '.'; mach++)
848 *p++ = *mach;
ed7faf49 849 if (!strcasecmp(mach, ".mit.edu"))
5eaef520 850 {
ed7faf49 851 *p = '\0';
9c04b191 852 fprintf(out, "%s.cluster\t%s CNAME %s.cluster\n",
ed7faf49 853 machbuf, HCLASS, clubuf);
5eaef520 854 }
bac4ceaa 855 }
856 }
857 }
5eaef520 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 {
dfaf9b68 869 EXEC SQL FETCH d_cursor2 INTO :name, :label, :data;
5eaef520 870 if (sqlca.sqlcode)
871 break;
872 strtrim(name);
2203d8f9 873 if (!valid(name))
874 continue;
dfaf9b68 875 strtrim(label);
5eaef520 876 strtrim(data);
877 fprintf(out, "%s.cluster\t%s %s \"%s %s\"\n",
dfaf9b68 878 name, HCLASS, HTYPE, label, data);
5eaef520 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;
890sqlerr:
891 db_error(sqlca.sqlcode);
892 return 0;
bac4ceaa 893}
894
895
5eaef520 896int do_printcap(void)
bac4ceaa 897{
5eaef520 898 FILE *out;
dfaf9b68 899 char outf[MAXPATHLEN], outft[MAXPATHLEN];
5eaef520 900 struct stat sb;
901 time_t ftime;
902 EXEC SQL BEGIN DECLARE SECTION;
ed9a436f 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];
5eaef520 906 int flag1, flag2, ka, pc, rm, rq;
907 EXEC SQL END DECLARE SECTION;
ed9a436f 908 char *rmname, *rqname;
5eaef520 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, "printcap", ftime)
916 || ModDiff (&flag2, "machine", ftime))
917 exit(MR_DATE);
918 if (flag1 < 0 && flag2 < 0)
919 {
920 fprintf(stderr, "File printcap.db does not need to be rebuilt.\n");
921 return 0;
922 }
923 }
924
925 sprintf(outft, "%s~", outf);
926 out = fopen(outft, "w");
927 if (!out)
928 {
929 perror("cannot open printcap.db for write");
930 exit(MR_OCONFIG);
931 }
932
933 fprintf(stderr, "Building printcap.db\n");
934 get_mach();
935
936 EXEC SQL DECLARE p_cursor2 CURSOR FOR
ed9a436f 937 SELECT name, duplexname, type, rp, rm, ka, pc, rq
938 FROM printers;
5eaef520 939 EXEC SQL OPEN p_cursor2;
940 while (1)
941 {
ed9a436f 942 EXEC SQL FETCH p_cursor2 INTO :name, :duplexname, :type,
943 :rp, :rm, :ka, :pc, :rq;
5eaef520 944 if (sqlca.sqlcode)
945 break;
ed9a436f 946 if (!(rmname = hash_lookup(machines, rm)))
5eaef520 947 continue;
ed9a436f 948 rqname = rq ? hash_lookup(machines, rq) : NULL;
5eaef520 949 strtrim(name);
2203d8f9 950 if (!valid(name))
951 continue;
5eaef520 952 strtrim(rp);
ed9a436f 953 fprintf(out, "%s.pcap\t%s %s \"%s:rp=%s:rm=%s:ka#%d:pc#%d",
954 name, HCLASS, HTYPE, name, rp, rmname, ka, pc);
955 if (rqname)
956 fprintf(out, ":rq=%s\"\n", rqname);
5eaef520 957 else
958 fputs("\"\n", out);
bac4ceaa 959
ed9a436f 960 strtrim(duplexname);
961 if (!valid(duplexname))
962 continue;
963 if (!strcmp(strtrim(type), "ALIAS"))
5eaef520 964 {
ed9a436f 965 EXEC SQL SELECT duplexname INTO :duplexrp
966 FROM printers WHERE name = :rp;
967 strtrim(duplexrp);
5eaef520 968 }
ed9a436f 969 else
970 strcpy(duplexrp, duplexname);
971 fprintf(out, "%s.pcap\t%s %s \"%s:rp=%s:rm=%s:ka#%d:pc#%d",
972 duplexname, HCLASS, HTYPE, duplexname, duplexrp,
973 rmname, ka, pc);
974 if (rqname)
975 fprintf(out, ":rq=%s\"\n", rqname);
976 else
977 fputs("\"\n", out);
5eaef520 978 }
ed9a436f 979 EXEC SQL CLOSE p_cursor2;
5eaef520 980
981 EXEC SQL COMMIT;
982
983 if (fclose(out))
984 {
ed9a436f 985 fprintf(stderr, "Unsuccessful close of pcap.db\n");
5eaef520 986 exit(MR_CCONFIG);
987 }
988 fix_file(outf);
989 return 1;
990sqlerr:
991 db_error(sqlca.sqlcode);
992 return 0;
bac4ceaa 993}
994
995
5eaef520 996int do_sloc(void)
bac4ceaa 997{
5eaef520 998 FILE *out;
dfaf9b68 999 char outf[MAXPATHLEN], outft[MAXPATHLEN], *mach;
5eaef520 1000 struct stat sb;
1001 time_t ftime;
1002 EXEC SQL BEGIN DECLARE SECTION;
dfaf9b68 1003 char service[SERVERHOSTS_SERVICE_SIZE];
5eaef520 1004 int flag1, flag2, id;
1005 EXEC SQL END DECLARE SECTION;
1006
1007 sprintf(outf, "%s/sloc.db", hesiod_dir);
1008
1009 if (stat(outf, &sb) == 0)
1010 {
1011 ftime = sb.st_mtime;
1012 if ((ModDiff (&flag1, "serverhosts", ftime)) ||
1013 (ModDiff (&flag2, "machine", ftime)))
1014 exit(MR_DATE);
1015 if (flag1 < 0 && flag2 < 0)
1016 {
1017 fprintf(stderr, "File sloc.db does not need to be rebuilt.\n");
1018 return 0;
1019 }
1020 }
1021
1022 sprintf(outft, "%s~", outf);
1023 out = fopen(outft, "w");
1024 if (!out)
1025 {
1026 perror("cannot open sloc.db for write");
1027 exit(MR_OCONFIG);
1028 }
1029
1030 fprintf(stderr, "Building sloc.db\n");
1031 get_mach();
1032
1033 EXEC SQL DECLARE s_cursor CURSOR FOR
1034 SELECT DISTINCT service, mach_id
1035 FROM serverhosts
1036 ORDER BY service;
1037 EXEC SQL OPEN s_cursor;
1038 while (1)
1039 {
1040 EXEC SQL FETCH s_cursor INTO :service, :id;
1041 if (sqlca.sqlcode)
1042 break;
1043 strtrim(service);
2203d8f9 1044 if (valid(service) && (mach = hash_lookup(machines, id)))
5eaef520 1045 fprintf(out, "%s.sloc\t%s %s %s\n", service, HCLASS, HTYPE, mach);
1046 }
1047 EXEC SQL CLOSE s_cursor;
1048
1049 EXEC SQL COMMIT;
1050
1051 if (fclose(out))
1052 {
1053 fprintf(stderr, "Unsuccessful close of sloc.db\n");
1054 exit(MR_CCONFIG);
1055 }
1056
1057 fix_file(outf);
1058 return 1;
1059sqlerr:
1060 db_error(sqlca.sqlcode);
1061 return 0;
bac4ceaa 1062}
1063
5eaef520 1064int do_service(void)
bac4ceaa 1065{
5eaef520 1066 FILE *out;
dfaf9b68 1067 char outf[MAXPATHLEN], outft[MAXPATHLEN];
5eaef520 1068 struct stat sb;
1069 time_t ftime;
1070 EXEC SQL BEGIN DECLARE SECTION;
dfaf9b68 1071 char service[SERVICES_NAME_SIZE], protocol[SERVICES_PROTOCOL_SIZE];
1072 char aname[ALIAS_NAME_SIZE], trans[ALIAS_TRANS_SIZE];
5eaef520 1073 int port, flag1;
1074 EXEC SQL END DECLARE SECTION;
1075
1076 sprintf(outf, "%s/service.db", hesiod_dir);
1077
1078 if (stat(outf, &sb) == 0)
1079 {
1080 ftime = sb.st_mtime;
1081 if (ModDiff (&flag1, "services", ftime))
1082 exit(MR_DATE);
1083 if (flag1 < 0)
1084 {
1085 fprintf(stderr, "File service.db does not need to be rebuilt.\n");
1086 return 0;
1087 }
1088 }
1089
1090 sprintf(outft, "%s~", outf);
1091 out = fopen(outft, "w");
1092 if (!out)
1093 {
1094 perror("cannot open service.db for write");
1095 exit(MR_OCONFIG);
1096 }
1097
1098 fprintf(stderr, "Building service.db\n");
1099
1100 EXEC SQL DECLARE s_cursor2 CURSOR FOR
1101 SELECT name, protocol, port
1102 FROM services;
1103 EXEC SQL OPEN s_cursor2;
1104 while (1)
1105 {
1106 EXEC SQL FETCH s_cursor2 INTO :service, :protocol, :port;
1107 if (sqlca.sqlcode)
1108 break;
1109 lowercase(protocol); /* Convert protocol to lowercase */
1110 strtrim(service);
2203d8f9 1111 if (!valid(service))
1112 continue;
5eaef520 1113 strtrim(protocol);
1114 fprintf(out, "%s.service\t%s %s \"%s %s %d\"\n",
1115 service, HCLASS, HTYPE, service, protocol, port);
1116 }
1117 EXEC SQL CLOSE s_cursor2;
1118
1119 EXEC SQL DECLARE a_cursor3 CURSOR FOR
1120 SELECT name, trans
1121 FROM alias
1122 WHERE type = 'SERVICE';
1123 EXEC SQL OPEN a_cursor3;
1124 while (1)
1125 {
dfaf9b68 1126 EXEC SQL FETCH a_cursor3 INTO :aname, :trans;
5eaef520 1127 if (sqlca.sqlcode)
1128 break;
dfaf9b68 1129 strtrim(aname);
1130 strtrim(trans);
2203d8f9 1131 if (!valid(aname) || !valid(trans))
1132 continue;
dfaf9b68 1133 fprintf(out, "%s.service\t%s CNAME %s.service\n", aname, HCLASS,
1134 trans);
5eaef520 1135 }
1136 EXEC SQL CLOSE a_cursor3;
1137
1138 EXEC SQL COMMIT;
1139
1140 if (fclose(out))
1141 {
1142 fprintf(stderr, "Unsuccessful close of service.db\n");
1143 exit(MR_CCONFIG);
1144 }
1145 fix_file(outf);
1146 return 1;
1147sqlerr:
1148 db_error(sqlca.sqlcode);
1149 return 0;
bac4ceaa 1150}
This page took 0.240023 seconds and 5 git commands to generate.