source: trunk/third/moira/gen/ndb.pc @ 24319

Revision 24319, 4.1 KB checked in by broder, 14 years ago (diff)
New Moira snapshot from SVN.
Line 
1/* $Id: ndb.pc 3956 2010-01-05 20:56:56Z zacheiss $
2 *
3 * This generates the msql database for the network tables.
4 *
5 * Copyright 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 <stdio.h>
16#include <string.h>
17#include <time.h>
18
19#include "util.h"
20
21EXEC SQL INCLUDE sqlca;
22
23RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/gen/ndb.pc $ $Id: ndb.pc 3956 2010-01-05 20:56:56Z zacheiss $");
24
25char *whoami = "ndb.gen";
26char *db = "moira/moira";
27
28void users(FILE *out);
29void hosts(FILE *out);
30
31int main(int argc, char **argv)
32{
33  FILE *out = stdout;
34  char *outf = NULL, outft[MAXPATHLEN];
35  struct stat sb;
36  int flag;
37
38  EXEC SQL CONNECT :db;
39
40  if (argc == 2)
41    {
42      outf = argv[1];
43      sprintf(outft, "%s~", outf);
44      if (!(out = fopen(outft, "w")))
45        {
46          fprintf(stderr, "unable to open %s for output\n", outf);
47          exit(MR_OCONFIG);
48        }
49    }
50  else if (argc != 1)
51    {
52      fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
53      exit(MR_ARGS);
54    }
55  else
56    outf = NULL;
57
58  EXEC SQL COMMIT;
59
60  fprintf(stderr, "users...\n");
61  users(out);
62  fprintf(stderr, "hosts...\n");
63  hosts(out);
64
65  if (fclose(out) < 0)
66    {
67      perror("close failed");
68      exit(MR_CCONFIG);
69    }
70
71  if (outf)
72    fix_file(outf);
73  exit(MR_SUCCESS);
74}
75
76void users(FILE *out)
77{
78  char *c;
79  EXEC SQL BEGIN DECLARE SECTION;
80  char login[USERS_LOGIN_SIZE], id[USERS_CLEARID_SIZE];
81  char first[USERS_FIRST_SIZE], middle[USERS_MIDDLE_SIZE];
82  char last[USERS_LAST_SIZE], type[USERS_TYPE_SIZE];
83  int status, users_id;
84  EXEC SQL END DECLARE SECTION;
85
86  EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
87
88  EXEC SQL DECLARE users1 CURSOR FOR
89    SELECT login, clearid, users_id, type, status, first, middle, last
90    FROM users WHERE clearid != '0'
91    AND login NOT LIKE '#%';
92  EXEC SQL OPEN users1;
93  while (1)
94    {
95      EXEC SQL FETCH users1 INTO :login, :id, :users_id, :type, :status,
96        :first, :middle, :last;
97
98      if (sqlca.sqlcode)
99        break;
100      strtrim(login);
101      strtrim(id);
102      strtrim(type);
103      strtrim(first);
104      strtrim(middle);
105      strtrim(last);
106      if (!*id)
107        continue;
108      fprintf(out, "user,%d,%s,%s,%s,%d,%s,%s,%s\n", users_id, id, login,
109              type, status, first, middle, last);
110    }
111
112  EXEC SQL CLOSE users1;
113
114  EXEC SQL COMMIT;
115
116  return;
117
118sqlerr:
119  db_error(sqlca.sqlcode);
120  exit(MR_DBMS_ERR);
121}
122
123void hosts(FILE *out)
124{
125  struct hash *users;
126  char *p;
127  int i;
128  EXEC SQL BEGIN DECLARE SECTION;
129  char name[MACHINE_NAME_SIZE], owner_type[MACHINE_OWNER_TYPE_SIZE];
130  char addr[MACHINE_ADDRESS_SIZE];
131  int id, use, status, owner;
132  EXEC SQL END DECLARE SECTION;
133
134  EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
135
136  fprintf(stderr, "aliases...\n");
137
138  EXEC SQL DECLARE hosts1 CURSOR FOR SELECT
139    mach_id, name FROM hostalias;
140  EXEC SQL OPEN hosts1;
141
142  while (1)
143    {
144      EXEC SQL FETCH hosts1 INTO :id, :name;
145      if (sqlca.sqlcode)
146        break;
147      if (id == 0)
148        continue;
149      if (!*strtrim(name))
150        continue;
151      if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU"))
152        {
153          fprintf(stderr, "Name %s not in MIT domain\n", name);
154          continue;
155        }
156      fprintf(out, "host_alias,%d,%s\n", id, name);
157    }
158
159  EXEC SQL CLOSE hosts1;
160
161  EXEC SQL COMMIT;
162
163  fprintf(stderr, "hosts (for real)...\n");
164
165  EXEC SQL DECLARE hosts3 CURSOR FOR SELECT
166    name, mach_id, address, use, status, owner_type, owner_id
167    FROM machine;
168  EXEC SQL OPEN hosts3;
169  while (1)
170    {
171      EXEC SQL FETCH hosts3 INTO :name, :id, :addr, :use, :status,
172        :owner_type, :owner;
173      if (sqlca.sqlcode)
174        break;
175      if (id == 0)
176        continue;
177      if (!*strtrim(name))
178        continue;
179      if ((i = strlen(name)) < 9 || strcmp(&name[i - 8], ".MIT.EDU"))
180        continue;
181      strtrim(addr);
182      strtrim(owner_type);
183      fprintf(out, "host,%d,%s,%s,%d,0,%d", id, name, addr, use, status);
184      if (!strcmp(owner_type, "USER"))
185        fprintf(out, ",USER,%d\n", owner);
186      else
187        fprintf(out, ",NONE,0\n");
188    }
189
190  EXEC SQL CLOSE hosts3;
191
192  EXEC SQL COMMIT;
193
194  return;
195sqlerr:
196  db_error(sqlca.sqlcode);
197  exit(MR_DBMS_ERR);
198}
199
200
Note: See TracBrowser for help on using the repository browser.