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