]> andersk Git - moira.git/blob - gen/hosts.pc
do "mach_id > 0" not "mach_id != 0" so it doesn't complain about the host
[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   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[MACHINE_NAME_SIZE], vendor[MACHINE_VENDOR_SIZE];
43   char model[MACHINE_MODEL_SIZE], os[MACHINE_OS_SIZE];
44   char addr[MACHINE_ADDRESS_SIZE];
45   EXEC SQL END DECLARE SECTION;
46
47   EXEC SQL CONNECT :db;
48
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);
61             }
62         }
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);
69         }
70     }
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;
80
81   gettimeofday(&now, NULL);
82
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));
89
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;
102       if ((i = strlen(name)) < 9 || !strchr(name, '.') ||
103           strcmp(strchr(name, '.'), ".MIT.EDU"))
104         {
105           fprintf(stderr, "Ignoring alias %s\n", name);
106           continue;
107         }
108       else
109         name[i - 8] = 0;
110       if ((p = hash_lookup(aliases, id)))
111         {
112           p = realloc(p, strlen(p) + strlen(name) + 2);
113           sprintf(strchr(p, '\0'), ",%s", name);
114           hash_update(aliases, id, p);
115         }
116       else
117         hash_store(aliases, id, strdup(name));
118     }
119
120   EXEC SQL DECLARE x CURSOR FOR SELECT
121     name, mach_id, vendor, model, os, address
122     FROM machine WHERE status = 1 AND mach_id > 0
123     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 (!*strtrim(name))
131         continue;
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         }
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;
146       fprintf(out, "HOST : %s : %s", addr, name);
147       if ((p = hash_lookup(aliases, id)))
148         fprintf(out, ",%s", p);
149       if ((*vendor || *model) && *os)
150         {
151           if (*vendor && *model)
152             fprintf(out, " : %s/%s : %s :\n", vendor, model, os);
153           else
154             fprintf(out, " : %s%s : %s :\n", vendor, model, os);
155         }
156       else
157         fputs(" : \n", out);
158     }
159
160   EXEC SQL CLOSE x;
161
162   EXEC SQL COMMIT;
163
164   fprintf(out, "; End of automatically generated host table\n");
165   if (fclose(out))
166     {
167       perror("close failed");
168       exit(MR_CCONFIG);
169     }
170   if (outf)
171     fix_file(outf);
172   exit(MR_SUCCESS);
173
174 sqlerr:
175   db_error(sqlca.sqlcode);
176   exit(MR_DBMS_ERR);
177 }
This page took 0.056195 seconds and 5 git commands to generate.