1 | /* $Id: hosts.pc,v 1.8 2000-11-30 23:27:40 zacheiss Exp $ |
---|
2 | * |
---|
3 | * This generates the hstath.txt hosttable. |
---|
4 | * |
---|
5 | * (c) Copyright 1993-1998 by the Massachusetts Institute of Technology. |
---|
6 | * For copying and distribution information, please see the file |
---|
7 | * <mit-copyright.h>. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <mit-copyright.h> |
---|
11 | #include <moira.h> |
---|
12 | |
---|
13 | #include <sys/stat.h> |
---|
14 | |
---|
15 | #include <netinet/in.h> |
---|
16 | #include <arpa/inet.h> |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <string.h> |
---|
21 | #include <time.h> |
---|
22 | |
---|
23 | #include "util.h" |
---|
24 | |
---|
25 | EXEC SQL INCLUDE sqlca; |
---|
26 | |
---|
27 | RCSID("$Header: /afs/.athena.mit.edu/astaff/project/moiradev/repository/moira/gen/hosts.pc,v 1.8 2000-11-30 23:27:40 zacheiss Exp $"); |
---|
28 | |
---|
29 | char *whoami = "hosts.gen"; |
---|
30 | char *db = "moira/moira"; |
---|
31 | |
---|
32 | int main(int argc, char **argv) |
---|
33 | { |
---|
34 | FILE *out = stdout; |
---|
35 | char *outf = NULL, outft[MAXPATHLEN], *p; |
---|
36 | struct timeval now; |
---|
37 | int i; |
---|
38 | struct hash *aliases; |
---|
39 | EXEC SQL BEGIN DECLARE SECTION; |
---|
40 | int id; |
---|
41 | char name[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE]; |
---|
42 | char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE]; |
---|
43 | char addr[MACHINE_ADDRESS_SIZE]; |
---|
44 | EXEC SQL END DECLARE SECTION; |
---|
45 | |
---|
46 | EXEC SQL CONNECT :db; |
---|
47 | |
---|
48 | if (argc == 2) |
---|
49 | { |
---|
50 | outf = argv[1]; |
---|
51 | sprintf(outft, "%s~", outf); |
---|
52 | if (!(out = fopen(outft, "w"))) |
---|
53 | { |
---|
54 | fprintf(stderr, "unable to open %s for output\n", outf); |
---|
55 | exit(MR_OCONFIG); |
---|
56 | } |
---|
57 | } |
---|
58 | else if (argc != 1) |
---|
59 | { |
---|
60 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); |
---|
61 | exit(MR_ARGS); |
---|
62 | } |
---|
63 | else |
---|
64 | outf = NULL; |
---|
65 | |
---|
66 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; |
---|
67 | |
---|
68 | gettimeofday(&now, NULL); |
---|
69 | |
---|
70 | fprintf(out, "; MIT Network Host Table\n;\n"); |
---|
71 | fprintf(out, "; \t%cAuthor: $\n", '$'); |
---|
72 | fprintf(out, "; \t%cDate: $\n", '$'); |
---|
73 | fprintf(out, "; \t%cRevision: $\n;\n", '$'); |
---|
74 | fprintf(out, "; Host table generated by Moira at %s;\n", |
---|
75 | ctime(&now.tv_sec)); |
---|
76 | |
---|
77 | EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias; |
---|
78 | EXEC SQL OPEN y; |
---|
79 | aliases = create_hash(1001); |
---|
80 | while (1) |
---|
81 | { |
---|
82 | EXEC SQL FETCH y INTO :id, :name; |
---|
83 | if (sqlca.sqlcode) |
---|
84 | break; |
---|
85 | if (id == 0) |
---|
86 | continue; |
---|
87 | if (!*strtrim(name)) |
---|
88 | continue; |
---|
89 | if ((i = strlen(name)) < 9 || !strchr(name, '.') || |
---|
90 | strcmp(strchr(name, '.'), ".MIT.EDU")) |
---|
91 | { |
---|
92 | fprintf(stderr, "Ignoring alias %s\n", name); |
---|
93 | continue; |
---|
94 | } |
---|
95 | else |
---|
96 | name[i - 8] = 0; |
---|
97 | if ((p = hash_lookup(aliases, id))) |
---|
98 | { |
---|
99 | p = realloc(p, strlen(p) + strlen(name) + 2); |
---|
100 | sprintf(strchr(p, '\0'), ",%s", name); |
---|
101 | hash_update(aliases, id, p); |
---|
102 | } |
---|
103 | else |
---|
104 | hash_store(aliases, id, strdup(name)); |
---|
105 | } |
---|
106 | |
---|
107 | EXEC SQL DECLARE x CURSOR FOR SELECT |
---|
108 | name, mach_id, vendor, model, os, address |
---|
109 | FROM machine WHERE status = 1 AND mach_id > 0 |
---|
110 | ORDER BY address; |
---|
111 | EXEC SQL OPEN x; |
---|
112 | while (1) |
---|
113 | { |
---|
114 | EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr; |
---|
115 | if (sqlca.sqlcode) |
---|
116 | break; |
---|
117 | if (!*strtrim(name)) |
---|
118 | continue; |
---|
119 | if ((i = strlen(name)) < 9 || !strchr(name, '.') || |
---|
120 | strcmp(strchr(name, '.'), ".MIT.EDU")) |
---|
121 | { |
---|
122 | fprintf(stderr, "Ignoring machine %s\n", name); |
---|
123 | continue; |
---|
124 | } |
---|
125 | else |
---|
126 | name[i - 8] = 0; |
---|
127 | strtrim(vendor); |
---|
128 | strtrim(model); |
---|
129 | strtrim(os); |
---|
130 | strtrim(addr); |
---|
131 | if (*addr == 0 || inet_addr(addr) == -1) |
---|
132 | continue; |
---|
133 | fprintf(out, "HOST : %s : %s", addr, name); |
---|
134 | if ((p = hash_lookup(aliases, id))) |
---|
135 | fprintf(out, ",%s", p); |
---|
136 | if ((*vendor || *model) && *os) |
---|
137 | { |
---|
138 | if (*vendor && *model) |
---|
139 | fprintf(out, " : %s/%s : %s :\n", vendor, model, os); |
---|
140 | else |
---|
141 | fprintf(out, " : %s%s : %s :\n", vendor, model, os); |
---|
142 | } |
---|
143 | else |
---|
144 | fputs(" : \n", out); |
---|
145 | } |
---|
146 | |
---|
147 | EXEC SQL CLOSE x; |
---|
148 | |
---|
149 | EXEC SQL COMMIT; |
---|
150 | |
---|
151 | fprintf(out, "; End of automatically generated host table\n"); |
---|
152 | if (fclose(out)) |
---|
153 | { |
---|
154 | perror("close failed"); |
---|
155 | exit(MR_CCONFIG); |
---|
156 | } |
---|
157 | if (outf) |
---|
158 | fix_file(outf); |
---|
159 | exit(MR_SUCCESS); |
---|
160 | |
---|
161 | sqlerr: |
---|
162 | db_error(sqlca.sqlcode); |
---|
163 | exit(MR_DBMS_ERR); |
---|
164 | } |
---|