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