]> andersk Git - moira.git/blame - gen/nfs.dc
New database and column names for Moira2.
[moira.git] / gen / nfs.dc
CommitLineData
f852c398 1/* $Header$
2 *
3 * This generates the files necessary to load an nfs server.
4 *
5 * (c) Copyright 1988, 1990 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 <stdio.h>
12#include <moira.h>
13#include <moira_site.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <sys/time.h>
17EXEC SQL INCLUDE sqlca;
18
19
20#define min(x,y) ((x) < (y) ? (x) : (y))
21
22char *whoami = "nfs.gen";
23char *malloc(), *strsave();
24char nfs_dir[64];
25
26main(argc, argv)
27int argc;
28char **argv;
29{
30 char cmd[64];
31 struct stat sb;
32 int changed = 0;
33
34 if (argc > 2) {
35 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
36 exit(MR_ARGS);
37 }
38
39 initialize_sms_error_table();
40 sprintf(nfs_dir, "%s/nfs", DCM_DIR);
41
42#ifsql INGRES
9fd1c2b8 43 EXEC SQL CONNECT moira;
f852c398 44#endsql
45#ifsql INFORMIX
9fd1c2b8 46 EXEC SQL DATABASE moira;
f852c398 47#endsql
48
49 changed = do_nfs();
50
51#ifsql INGRES
52 EXEC SQL DISCONNECT;
53#endsql
54#ifsql INFORMIX
55 EXEC SQL CLOSE DATABASE;
56#endsql
57
58 if (!changed) {
59 fprintf(stderr, "No files updated.\n");
60 if (argc == 2 && stat(argv[1], &sb) == 0)
61 exit(MR_NO_CHANGE);
62 }
63
64 if (argc == 2) {
65 sprintf(cmd, "cd %s; cp %s/nfs/* .; tar cf %s .",
66 nfs_dir, SMS_DIR, argv[1]);
67 if (system(cmd))
68 exit(MR_TAR_FAIL);
69 }
70
71 exit(MR_SUCCESS);
72}
73
74
75/* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
76
77int do_nfs()
78{
79 EXEC SQL BEGIN DECLARE SECTION;
80 char machname[33], listname[33];
81 EXEC SQL END DECLARE SECTION;
82 struct save_queue *machs, *lists;
83 int changed;
84
85 machs = sq_create();
86 lists = sq_create();
87
9fd1c2b8 88 /* The following is declarative, not executed,
89 * and so is dependent on where it is in the file,
90 * not in the order of execution of statements.
91 */
f852c398 92 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
93 EXEC SQL DECLARE s_cursor CURSOR FOR
94 SELECT m.name, s.value3
95 FROM machine m, serverhosts s
96 WHERE m.mach_id = s.mach_id AND s.service = "NFS" AND s.enable != 0;
97 EXEC SQL OPEN s_cursor;
98 while (1) {
99 EXEC SQL FETCH s_cursor INTO :machname, :listname;
100 if (sqlca.sqlcode != 0) break;
101 sq_save_unique_string(machs, strsave(strtrim(machname)));
102 sq_save_unique_string(lists, strsave(strtrim(listname)));
103 }
104 EXEC SQL CLOSE s_cursor;
105
106 changed = do_lists(lists);
107 changed += do_machs(machs);
108 return(changed);
109 sqlerr:
110 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
111 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
112 sqlca.sqlcode);
113 exit(MR_INGRES_ERR);
114}
115
116
117/* Make all of the credentials lists that will be needed. Returns 0 if
118 * no files were actually changed */
119
120int do_lists(lists)
121struct save_queue *lists;
122{
123 char file[64], *u;
124 struct hash *users, *do_everyone();
125 struct stat sb;
126 FILE *fd;
127 EXEC SQL BEGIN DECLARE SECTION;
128 char *listname, *lsname, lname[33], uname[9];
129 int uid, id, flag1, flag2, flag3, flag4;
130 EXEC SQL END DECLARE SECTION;
131
132 sprintf(file, "%s/list-", nfs_dir);
133 if (stat(file, &sb) == 0) {
134 if ((ModDiff (&flag1, "users", sb.st_mtime)) ||
135 (ModDiff (&flag2, "list", sb.st_mtime)) ||
136 (ModDiff (&flag3, "imembers", sb.st_mtime)) ||
137 (ModDiff (&flag4, "serverhosts", sb.st_mtime))) exit (MR_DATE);
138 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
139 fprintf(stderr, "The lists do not need to be rebuilt.\n");
140 return(0);
141 }
142 }
143
144 /* get locks */
145 EXEC SQL SELECT modtime INTO :lname FROM list WHERE list_id = 0;
146 EXEC SQL SELECT modtime INTO :lname FROM users WHERE users_id = 0;
147
148 /* build the list of everyone, and store it in a file whose name
149 * corresponds to the empty list.
150 */
151 users = do_everyone();
152
153 fprintf(stderr, "Building specific lists\n");
154 /* now do each of the lists used by an NFS server */
155
156 while (sq_get_data(lists, &listname)) {
157 if (strlen(listname) == 0)
158 continue;
159 sprintf(file, "%s/list-%s", nfs_dir, listname);
160 fd = fopen(file, "w");
161 if (!fd) {
162 fprintf(stderr, "cannot open %s for output\n", file);
163 exit(MR_OCONFIG);
164 }
165
166 EXEC SQL DECLARE m_cursor CURSOR FOR
167 SELECT m.member_id
168 FROM imembers m, list l
169 WHERE m.list_id=l.list_id AND l.name = :listname AND
170 m.member_type="USER";
171 EXEC SQL OPEN m_cursor;
172 while (1) {
173 EXEC SQL FETCH m_cursor INTO :id;
174 if (sqlca.sqlcode != 0) break;
175 if (u = hash_lookup(users, id))
176 fprintf(fd, "%s\n", u);
177 }
178 EXEC SQL CLOSE m_cursor;
2ab080f0 179 if (fclose(fd) == EOF) {
f852c398 180 fprintf(stderr, "error closing %s\n", file);
181 exit(MR_CCONFIG);
182 }
183 }
184/* don't free here either
185 sq_destroy(lists);
186 */
187 return(1);
188 sqlerr:
189 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
190 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
191 sqlca.sqlcode);
192 exit(MR_INGRES_ERR);
193}
194
195
196/* Build the list of everybody. */
197struct grp {
198 struct grp *next;
199 char *lid;
200};
201struct user {
202 char name[9];
203 int uid;
204 struct grp *lists;
205};
206
207
208struct hash *do_everyone()
209{
210 char buf[BUFSIZ], *l;
211 struct hash *groups, *users;
212 struct user *u;
213 struct grp *g;
214 struct bucket *b, **p;
215 EXEC SQL BEGIN DECLARE SECTION;
216 char name[33];
217 int gid, id, lid, maxid, uid;
218 EXEC SQL END DECLARE SECTION;
219 FILE *fd;
220 int i;
221 struct save_queue *sq;
222
223 fprintf(stderr, "Building the list of everybody\n");
224 sprintf(buf, "%s/list-", nfs_dir);
225 fd = fopen(buf, "w");
226 if (!fd) {
227 fprintf(stderr, "cannot open %s for output\n", buf);
228 exit(MR_OCONFIG);
229 }
230
231 /* make space for group list */
232 groups = create_hash(15000);
233
234 /* retrieve simple groups */
235 EXEC SQL DECLARE l_cursor CURSOR FOR
236 SELECT gid, list_id
237 FROM list
9fd1c2b8 238 WHERE grouplist != 0 AND active != 0;
f852c398 239 EXEC SQL OPEN l_cursor;
240 while (1) {
241 EXEC SQL FETCH l_cursor INTO :gid, :lid;
242 if (sqlca.sqlcode != 0) break;
243 sprintf(buf, ":%d", gid);
244 hash_store(groups, lid, strsave(buf));
245 }
246 EXEC SQL CLOSE l_cursor;
247
248 /* now do grplists */
249 users = create_hash(10000);
250 EXEC SQL DECLARE u_cursor CURSOR FOR
251 SELECT users_id, login, uid
252 FROM users
253 WHERE status = 1;
254 EXEC SQL OPEN u_cursor;
255 while (1) {
256 EXEC SQL FETCH u_cursor INTO :id, :name, :uid;
257 if (sqlca.sqlcode != 0) break;
258 u = (struct user *) malloc(sizeof(struct user));
259 strcpy(u->name, strtrim(name));
260 u->uid = uid;
261 u->lists = NULL;
262 hash_store(users, id, u);
263 }
264 EXEC SQL CLOSE u_cursor;
265
266 EXEC SQL DECLARE m_cursor2 CURSOR FOR
267 SELECT list_id, member_id
268 FROM imembers
269 WHERE member_type = "USER";
270 EXEC SQL OPEN m_cursor2;
271 while (1) {
272 EXEC SQL FETCH m_cursor2 INTO :lid, :id;
273 if (sqlca.sqlcode != 0) break;
274 if ((u = (struct user *) hash_lookup(users, id)) &&
275 ((l = hash_lookup(groups, lid)) != NULL)) {
276 g = (struct grp *) malloc(sizeof(struct grp));
277 g->next = u->lists;
278 u->lists = g;
279 g->lid = l;
280 }
281 }
282 EXEC SQL CLOSE m_cursor2;
283
284 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
285 for (b = *p; b; b = b->next) {
286 u = (struct user *)b->data;
287 sprintf(buf, "%s:%d", u->name, u->uid);
288 for (g = u->lists; g; g = g->next)
289 strcat(buf, g->lid);
290 b->data = strsave(buf);
291 fprintf(fd, "%s\n", buf);
292 }
293 }
294
295 fclose(fd);
296 free(groups);
297 return(users);
298 sqlerr:
299 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
300 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
301 sqlca.sqlcode);
302 exit(MR_INGRES_ERR);
303}
304
305
306/* Now do each of the servers, linking the credentials list file and
307 * compiling the quota and dirs files.
308 */
309
310int do_machs(machs)
311struct save_queue *machs;
312{
313 EXEC SQL BEGIN DECLARE SECTION;
314 char *machname, listname[33], dev[33], dir[81], fstype[9];
315 int uid, quota, id, gid, flag1, flag2, flag3, flag4;
316 EXEC SQL END DECLARE SECTION;
317 char file[64], f1[64], f2[64], *cp, *index();
318 int prevuid, quotasum, olddev, oldmach;
319 FILE *fd;
320 struct hash *machines;
321
322 fprintf(stderr, "Building machine files\n");
323
324 /* acquire locks on machines & filesystems */
325 EXEC SQL SELECT modtime INTO :listname FROM users WHERE users_id = 0;
326 EXEC SQL SELECT modtime INTO :listname FROM machine WHERE users_id = 0;
327 EXEC SQL SELECT modtime INTO :listname FROM filesys WHERE filsys_id = 0;
328
329 machines = create_hash(100);
330 while (sq_get_data(machs, &machname)) {
331 EXEC SQL SELECT s.value3, m.mach_id
332 INTO :listname, :id
333 FROM serverhosts s, machine m
334 WHERE s.mach_id = m.mach_id AND m.name = :machname;
335 strtrim(machname);
336 sprintf(f1, "%s/list-%s", nfs_dir, strtrim(listname));
337 sprintf(f2, "%s/%s.cred", nfs_dir, machname);
338 unlink(f2); /* ignore errors on this unlink */
339 if (link(f1, f2)) {
340 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
341 exit(MR_OCONFIG);
342 }
343 hash_store(machines, id, machname);
344 }
345
346
347 olddev = oldmach = -1;
348 fd = stdin;
349
350 EXEC SQL DECLARE q_cursor CURSOR FOR
351 SELECT DISTINCT q.quota, u.uid, q.phys_id, n.device, n.mach_id
352 FROM quota q, users u, nfsphys n
353 WHERE u.users_id = q.entity_id AND n.nfsphys_id = q.phys_id AND
354 q.phys_id != 0 AND n.status < 16 AND q.type = "USER"
355 ORDER BY mach_id, phys_id, uid;
356 EXEC SQL OPEN q_cursor;
357 while (1) {
358 EXEC SQL FETCH q_cursor INTO :quota, :uid, :flag1, :dev, :flag2;
359 if (sqlca.sqlcode != 0) break;
360 if (flag1 != olddev || flag2 != oldmach) {
361 fclose(fd);
362 olddev = flag1;
363 oldmach = flag2;
364 while (cp = index(dev, '/')) *cp = '@';
365 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
366 hash_lookup(machines, flag2), strtrim(dev));
367 fd = fopen(file, "w");
368 if (!fd) {
369 fprintf(stderr, "cannot open %s for output\n", file);
370 exit(MR_OCONFIG);
371 }
372 prevuid = -1;
373 quotasum = 0;
374 }
375 if (uid != prevuid) {
376 if (quotasum)
377 fprintf(fd, "%d %d\n", prevuid, quotasum);
378 prevuid = uid;
379 quotasum = quota;
380 } else {
381 quotasum += quota;
382 }
383 }
384 EXEC SQL CLOSE q_cursor;
385 if (quotasum)
386 fprintf(fd, "%d %d\n", prevuid, quotasum);
2ab080f0 387 if (fd != stdin && fclose(fd) == EOF) {
f852c398 388 fprintf(stderr, "error closing %s", file);
389 exit(MR_CCONFIG);
390 }
391
392 olddev = oldmach = -1;
393 fd = stdin;
394 EXEC SQL DECLARE q_cursor2 CURSOR FOR
395 SELECT DISTINCT q.quota, l.gid, q.phys_id, n.device, n.mach_id, n.status
396 FROM quota q, list l, nfsphys n
397 WHERE l.list_id = q.entity_id AND n.nfsphys_id = q.phys_id AND
398 q.phys_id != 0 AND n.status > 15 AND q.type = "GROUP"
399 ORDER BY mach_id, phys_id, gid;
400 EXEC SQL OPEN q_cursor2;
401 while (1) {
402 EXEC SQL FETCH q_cursor2 INTO :quota, :gid, :flag1, :dev,
403 :flag2, :flag3;
404 if (sqlca.sqlcode != 0) break;
405 if (flag1 != olddev || flag2 != oldmach) {
406 fclose(fd);
407 olddev = flag1;
408 oldmach = flag2;
409 while (cp = index(dev, '/')) *cp = '@';
410 sprintf(file, "%s/%s.%s.quotas", nfs_dir,
411 hash_lookup(machines, flag2), strtrim(dev));
412 fd = fopen(file, "w");
413 if (!fd) {
414 fprintf(stderr, "cannot open %s for output\n", file);
415 exit(MR_OCONFIG);
416 }
417 prevuid = -1;
418 quotasum = 0;
419 }
420 if (gid != prevuid) {
421 if (quotasum)
422 fprintf(fd, "%d %d\n", prevuid, quotasum);
423 prevuid = gid;
424 quotasum = quota;
425 } else {
426 quotasum += quota;
427 }
428 }
429 EXEC SQL CLOSE q_cursor2;
430 if (quotasum)
431 fprintf(fd, "%d %d\n", prevuid, quotasum);
2ab080f0 432 if (fd != stdin && fclose(fd) == EOF) {
f852c398 433 fprintf(stderr, "error closing %s", file);
434 exit(MR_CCONFIG);
435 }
436
437 olddev = oldmach = -1;
438
439 EXEC SQL DECLARE q_cursor3 CURSOR FOR
440 SELECT DISTINCT f.name, f.lockertype, u.uid, l.gid, f.phys_id,
441 f.mach_id, n.device
442 FROM users u, list l, nfsphys n, filesys f
443 WHERE u.users_id = f.owner AND
444 l.list_id = f.owners AND
445 f.createflg != 0 AND f.phys_id != 0 AND
446 f.type = "NFS" AND
447 f.phys_id = n.nfsphys_id
448 ORDER BY mach_id, phys_id;
449 EXEC SQL OPEN q_cursor3;
450 while (1) {
451 EXEC SQL FETCH q_cursor3 INTO :dir, :fstype, :uid, :gid, :flag1,
452 :flag2, :dev;
453 if (sqlca.sqlcode != 0) break;
454 if (flag1 != olddev || flag2 != oldmach) {
455 fclose(fd);
456 olddev = flag1;
457 oldmach = flag2;
458 while (cp = index(dev, '/')) *cp = '@';
459 sprintf(file, "%s/%s.%s.dirs", nfs_dir,
460 hash_lookup(machines, flag2), strtrim(dev));
461 fd = fopen(file, "w");
462 if (!fd) {
463 fprintf(stderr, "cannot open %s for output\n", file);
464 exit(MR_OCONFIG);
465 }
466 }
467 fprintf(fd, "%s %d %d %s\n", strtrim(dir), uid, gid, strtrim(fstype));
468 }
469 EXEC SQL CLOSE q_cursor3;
2ab080f0 470 if (fclose(fd) == EOF) {
f852c398 471 fprintf(stderr, "error closing %s", file);
472 exit(MR_CCONFIG);
473 }
474 return(1);
475 sqlerr:
476 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
477 critical_alert("DCM", "NFS build encountered DATABASE ERROR %d",
478 sqlca.sqlcode);
479 exit(MR_INGRES_ERR);
480}
This page took 0.119512 seconds and 5 git commands to generate.