]> andersk Git - moira.git/blob - gen/hosts.pc
Use moira_schema.h
[moira.git] / gen / hosts.pc
1 /* $Id$
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$");
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   char buf[MACHINE_NAME_SIZE + ALIAS_NAME_SIZE];
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;
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];
46   EXEC SQL END DECLARE SECTION;
47
48   EXEC SQL CONNECT :db;
49
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);
62             }
63         }
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);
70         }
71     }
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;
81
82   gettimeofday(&now, NULL);
83
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));
90
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;
103       if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
104           strcmp(strchr(name, '.'), ".MIT.EDU"))
105         {
106           fprintf(stderr, "Ignoring alias %s\n", name);
107           continue;
108         }
109       else
110         name[i - 8] = 0;
111       if ((p = hash_lookup(aliases, id)))
112         {
113           sprintf(buf, "%s,%s", p, name);
114           hash_update(aliases, id, strdup(buf));
115           free(p);
116         }
117       else
118         hash_store(aliases, id, strdup(name));
119     }
120
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;
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         }
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);
159         }
160       else
161         fputs("\n", out);
162     }
163
164   EXEC SQL CLOSE x;
165
166   EXEC SQL COMMIT;
167
168   fprintf(out, "; End of automatically generated host table\n");
169   if (fclose(out))
170     {
171       perror("close failed");
172       exit(MR_CCONFIG);
173     }
174   if (outf)
175     fix_file(outf);
176   exit(MR_SUCCESS);
177
178 sqlerr:
179   db_error(sqlca.sqlcode);
180   exit(MR_DBMS_ERR);
181 }
This page took 0.053857 seconds and 5 git commands to generate.