]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id$ |
2289e07e | 2 | * |
3 | * This generates the hstath.txt hosttable. | |
4 | * | |
7ac48069 | 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>. | |
2289e07e | 8 | */ |
9 | ||
10 | #include <mit-copyright.h> | |
2289e07e | 11 | #include <moira.h> |
7ac48069 | 12 | |
2289e07e | 13 | #include <sys/stat.h> |
7ac48069 | 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 | ||
2289e07e | 25 | EXEC SQL INCLUDE sqlca; |
26 | ||
7ac48069 | 27 | RCSID("$Header$"); |
28 | ||
2289e07e | 29 | char *whoami = "hosts.gen"; |
9c04b191 | 30 | char *db = "moira/moira"; |
2289e07e | 31 | |
5eaef520 | 32 | int main(int argc, char **argv) |
2289e07e | 33 | { |
5eaef520 | 34 | FILE *out = stdout; |
dfaf9b68 | 35 | char *outf = NULL, outft[MAXPATHLEN], *p; |
5eaef520 | 36 | struct stat sb; |
37 | struct timeval now; | |
38 | int flag1, flag2, i; | |
39 | struct hash *aliases; | |
40 | EXEC SQL BEGIN DECLARE SECTION; | |
41 | int id; | |
dfaf9b68 | 42 | char name[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE]; |
43 | char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE]; | |
44 | char addr[MACHINE_ADDRESS_SIZE]; | |
5eaef520 | 45 | EXEC SQL END DECLARE SECTION; |
2289e07e | 46 | |
5eaef520 | 47 | EXEC SQL CONNECT :db; |
2289e07e | 48 | |
5eaef520 | 49 | if (argc == 2) |
50 | { | |
51 | if (stat(argv[1], &sb) == 0) | |
52 | { | |
53 | if (ModDiff(&flag1, "machine", sb.st_mtime) || | |
54 | ModDiff(&flag2, "subnet", sb.st_mtime)) | |
55 | exit(MR_DATE); | |
56 | if (flag1 < 0 && flag2 < 0) | |
57 | { | |
58 | fprintf(stderr, "File %s does not need to be rebuilt.\n", | |
59 | argv[1]); | |
60 | exit(MR_NO_CHANGE); | |
2289e07e | 61 | } |
62 | } | |
5eaef520 | 63 | outf = argv[1]; |
64 | sprintf(outft, "%s~", outf); | |
65 | if (!(out = fopen(outft, "w"))) | |
66 | { | |
67 | fprintf(stderr, "unable to open %s for output\n", outf); | |
68 | exit(MR_OCONFIG); | |
2289e07e | 69 | } |
2289e07e | 70 | } |
5eaef520 | 71 | else if (argc != 1) |
72 | { | |
73 | fprintf(stderr, "usage: %s [outfile]\n", argv[0]); | |
74 | exit(MR_ARGS); | |
75 | } | |
76 | else | |
77 | outf = NULL; | |
78 | ||
79 | EXEC SQL WHENEVER SQLERROR GOTO sqlerr; | |
2289e07e | 80 | |
5eaef520 | 81 | gettimeofday(&now, NULL); |
2289e07e | 82 | |
5eaef520 | 83 | fprintf(out, "; MIT Network Host Table\n;\n"); |
84 | fprintf(out, "; \t%cAuthor: $\n", '$'); | |
85 | fprintf(out, "; \t%cDate: $\n", '$'); | |
86 | fprintf(out, "; \t%cRevision: $\n;\n", '$'); | |
87 | fprintf(out, "; Host table generated by Moira at %s;\n", | |
88 | ctime(&now.tv_sec)); | |
2289e07e | 89 | |
5eaef520 | 90 | EXEC SQL DECLARE y CURSOR FOR SELECT mach_id, name FROM hostalias; |
91 | EXEC SQL OPEN y; | |
92 | aliases = create_hash(1001); | |
93 | while (1) | |
94 | { | |
95 | EXEC SQL FETCH y INTO :id, :name; | |
96 | if (sqlca.sqlcode) | |
97 | break; | |
98 | if (id == 0) | |
99 | continue; | |
100 | if (!*strtrim(name)) | |
101 | continue; | |
ed7faf49 | 102 | if ((i = strlen(name)) < 9 || !strchr(name, '.') || |
103 | strcmp(strchr(name, '.'), ".MIT.EDU")) | |
5eaef520 | 104 | { |
ed7faf49 | 105 | fprintf(stderr, "Ignoring alias %s\n", name); |
5eaef520 | 106 | continue; |
2289e07e | 107 | } |
5eaef520 | 108 | else |
109 | name[i - 8] = 0; | |
7ac48069 | 110 | if ((p = hash_lookup(aliases, id))) |
5eaef520 | 111 | { |
3283335f | 112 | p = realloc(p, strlen(p) + strlen(name) + 2); |
113 | sprintf(strchr(p, '\0'), ",%s", name); | |
114 | hash_update(aliases, id, p); | |
5eaef520 | 115 | } |
116 | else | |
7ac48069 | 117 | hash_store(aliases, id, strdup(name)); |
2289e07e | 118 | } |
119 | ||
5eaef520 | 120 | EXEC SQL DECLARE x CURSOR FOR SELECT |
121 | name, mach_id, vendor, model, os, address | |
c203b667 | 122 | FROM machine WHERE status = 1 AND mach_id > 0 |
123 | ORDER BY address; | |
5eaef520 | 124 | EXEC SQL OPEN x; |
125 | while (1) | |
126 | { | |
127 | EXEC SQL FETCH x INTO :name, :id, :vendor, :model, :os, :addr; | |
128 | if (sqlca.sqlcode) | |
129 | break; | |
5eaef520 | 130 | if (!*strtrim(name)) |
131 | continue; | |
ed7faf49 | 132 | if ((i = strlen(name)) < 9 || !strchr(name, '.') || |
133 | strcmp(strchr(name, '.'), ".MIT.EDU")) | |
134 | { | |
135 | fprintf(stderr, "Ignoring machine %s\n", name); | |
136 | continue; | |
137 | } | |
5eaef520 | 138 | else |
139 | name[i - 8] = 0; | |
140 | strtrim(vendor); | |
141 | strtrim(model); | |
142 | strtrim(os); | |
143 | strtrim(addr); | |
144 | if (*addr == 0 || inet_addr(addr) == -1) | |
145 | continue; | |
3283335f | 146 | fprintf(out, "HOST : %s : %s", addr, name); |
5eaef520 | 147 | if ((p = hash_lookup(aliases, id))) |
3283335f | 148 | fprintf(out, ",%s", p); |
5eaef520 | 149 | if ((*vendor || *model) && *os) |
150 | { | |
151 | if (*vendor && *model) | |
3283335f | 152 | fprintf(out, " : %s/%s : %s :\n", vendor, model, os); |
5eaef520 | 153 | else |
3283335f | 154 | fprintf(out, " : %s%s : %s :\n", vendor, model, os); |
2289e07e | 155 | } |
5eaef520 | 156 | else |
3283335f | 157 | fputs(" : \n", out); |
2289e07e | 158 | } |
159 | ||
5eaef520 | 160 | EXEC SQL CLOSE x; |
9c04b191 | 161 | |
5eaef520 | 162 | EXEC SQL COMMIT; |
2289e07e | 163 | |
5eaef520 | 164 | fprintf(out, "; End of automatically generated host table\n"); |
165 | if (fclose(out)) | |
166 | { | |
167 | perror("close failed"); | |
168 | exit(MR_CCONFIG); | |
2289e07e | 169 | } |
5eaef520 | 170 | if (outf) |
171 | fix_file(outf); | |
172 | exit(MR_SUCCESS); | |
2289e07e | 173 | |
5eaef520 | 174 | sqlerr: |
175 | db_error(sqlca.sqlcode); | |
176 | exit(MR_DBMS_ERR); | |
2289e07e | 177 | } |