]> andersk Git - moira.git/blame - gen/nfs.qc
made clname & entity buffers instead of pointers;
[moira.git] / gen / nfs.qc
CommitLineData
6efe4fbf 1/* $Header$
2 *
3 * This generates the files necessary to load an nfs server.
0a5ff702 4 *
5 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
6efe4fbf 8 */
9
0a5ff702 10#include <mit-copyright.h>
6efe4fbf 11#include <stdio.h>
12#include <sms.h>
13#include <sms_app.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <sys/time.h>
17
c461aa3e 18#undef NFS_DIR
6efe4fbf 19#define NFS_DIR "/u1/sms/dcm/nfs"
20
21#define min(x,y) ((x) < (y) ? (x) : (y))
22
969cfb2b 23char *whoami = "nfs.gen";
6efe4fbf 24char *malloc(), *strsave();
25char *ingres_date_and_time(), *ingres_time(), *ingres_date();
26
27main(argc, argv)
28int argc;
29char **argv;
30{
31 char cmd[64];
32 struct stat sb;
3548eee6 33 int changed = 0;
969cfb2b 34 int ingerr();
6efe4fbf 35
36 if (argc > 2) {
37 fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
38 exit(SMS_ARGS);
39 }
40
969cfb2b 41 IIseterr(ingerr);
330c8868 42 initialize_sms_error_table();
6efe4fbf 43## ingres sms
1daee9f8 44## set lockmode session where level = table
6efe4fbf 45
3548eee6 46 changed = do_nfs();
6efe4fbf 47
48## exit
49
3548eee6 50 if (!changed) {
51 fprintf(stderr, "No files updated.\n");
52 if (argc == 2 && stat(argv[1], &sb) == 0)
53 exit(SMS_NO_CHANGE);
54 }
55
6efe4fbf 56 if (argc == 2) {
72d4c6c5 57 sprintf(cmd, "cd %s; cp /u1/sms/nfs/* .; tar cf %s .", NFS_DIR, argv[1]);
6efe4fbf 58 if (system(cmd))
59 exit(SMS_TAR_FAIL);
60 }
61
62 exit(SMS_SUCCESS);
63}
64
65
969cfb2b 66/*
67 * ingerr: (supposedly) called when Ingres indicates an error.
68 * I have not yet been able to get this to work to intercept a
69 * database open error.
70 */
71#define INGRES_DEADLOCK 4700
72
73static int ingerr(num)
74 int *num;
75{
76 char buf[256];
77 int ingres_errno;
78
79 switch (*num) {
80 case INGRES_DEADLOCK:
81 ingres_errno = SMS_DEADLOCK;
82 break;
83 default:
84 ingres_errno = SMS_INGRES_ERR;
85 }
86 com_err(whoami, SMS_INGRES_ERR, " code %d\n", *num);
87 critical_alert("DCM", "NFS build encountered INGRES ERROR %d", *num);
88 exit(ingres_errno);
89}
90
91
3548eee6 92/* Generate the files. Returns zero if nothing changed, non-zero otherwise. */
93
94int do_nfs()
6efe4fbf 95##{
96## char machname[33], listname[33];
97 struct save_queue *machs, *lists;
3548eee6 98 int changed;
6efe4fbf 99
100 machs = sq_create();
101 lists = sq_create();
102## range of s is serverhosts
103## retrieve (machname = trim(machine.name), listname = trim(s.value3))
104## where machine.mach_id = s.mach_id and s.enable != 0 {
105 sq_save_unique_string(machs, strsave(machname));
106 sq_save_unique_string(lists, strsave(listname));
107## }
108
3548eee6 109 changed = do_lists(lists);
110 changed += do_machs(machs);
111 return(changed);
6efe4fbf 112##}
113
114
3548eee6 115/* Make all of the credentials lists that will be needed. Returns 0 if
116 * no files were actually changed */
117
118int do_lists(lists)
6efe4fbf 119struct save_queue *lists;
120##{
3548eee6 121 char file[64], *u;
122 struct hash *users, *do_everyone();
123 struct stat sb;
6efe4fbf 124 FILE *fd;
3548eee6 125## char *listname, *lsname, lname[33], uname[9], *filetime;
126## int uid, id, flag1, flag2, flag3, flag4;
6efe4fbf 127
3548eee6 128 sprintf(file, "%s/list-", NFS_DIR);
129 if (stat(file, &sb) == 0) {
130 filetime = ingres_date_and_time(sb.st_mtime);
131## retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
132## where tblstats.table = "users"
133## retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
134## where tblstats.table = "list"
135## retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
330c8868 136## where tblstats.table = "imembers"
3548eee6 137## retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
138## where tblstats.table = "serverhosts"
139 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
140 fprintf(stderr, "The lists do not need to be rebuilt.\n");
141 return(0);
142 }
143 }
144
1daee9f8 145## begin transaction
146 /* get locks */
147## retrieve (lname = list.modtime) where list.list_id = 0
148## retrieve (lname = users.modtime) where users.users_id = 0
149
3548eee6 150 /* build the list of everyone, and store it in a file whose name
151 * corresponds to the empty list.
152 */
6efe4fbf 153 users = do_everyone();
154
3548eee6 155 fprintf(stderr, "Building specific lists\n");
156 /* now do each of the lists used by an NFS server */
6efe4fbf 157## range of l is list
158## range of l1 is list
330c8868 159## range of m is imembers
6efe4fbf 160## range of u is users
161 while (sq_get_data(lists, &listname)) {
162 if (strlen(listname) == 0)
163 continue;
164 sprintf(file, "%s/list-%s", NFS_DIR, listname);
165 fd = fopen(file, "w");
166 if (!fd) {
167 fprintf(stderr, "cannot open %s for output\n", file);
168 exit(SMS_OCONFIG);
169 }
330c8868 170## repeat retrieve (id = m.member_id)
171## where m.list_id = l1.list_id and l1.name = @lsname and
172## m.member_type = "USER" {
173 if (u = hash_lookup(users, id))
174 fprintf(fd, "%s\n", u);
175## }
176 if (fclose(fd)) {
6efe4fbf 177 fprintf(stderr, "error closing %s\n", file);
178 exit(SMS_CCONFIG);
179 }
180 }
1daee9f8 181/* don't free here either
6efe4fbf 182 sq_destroy(lists);
1daee9f8 183 */
184## end transaction
3548eee6 185 return(1);
6efe4fbf 186##}
187
188
3548eee6 189/* Build the list of everybody. */
190struct grp {
191 struct grp *next;
192 int id;
193};
194struct user {
195 char name[9];
d3c7c2f9 196 int uid;
3548eee6 197 struct grp *lists;
198};
199
200struct hash *do_everyone()
6efe4fbf 201##{
3548eee6 202 char buf[BUFSIZ], *l;
203 struct hash *groups, *users;
204 struct user *u;
205 struct grp *g;
206 struct bucket *b, **p;
6efe4fbf 207## char name[33];
208## int gid, id, lid, maxid, uid;
209 FILE *fd;
969cfb2b 210 int i;
6efe4fbf 211 struct save_queue *sq;
212
3548eee6 213 fprintf(stderr, "Building the list of everybody\n");
6efe4fbf 214 sprintf(buf, "%s/list-", NFS_DIR);
215 fd = fopen(buf, "w");
216 if (!fd) {
217 fprintf(stderr, "cannot open %s for output\n", buf);
218 exit(SMS_OCONFIG);
219 }
220
221 /* make space for group list */
3548eee6 222 groups = create_hash(15000);
6efe4fbf 223
224 /* retrieve simple groups */
3548eee6 225## range of l is list
6efe4fbf 226## retrieve (gid = l.#gid, lid = l.list_id)
3548eee6 227## where l.group != 0 and l.active != 0 {
228 sprintf(buf, ":%d", gid);
229 hash_store(groups, lid, strsave(buf));
6efe4fbf 230## }
231
3548eee6 232 /* now do grplists */
233 users = create_hash(10000);
6efe4fbf 234## range of u is users
d3c7c2f9 235## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
236## where u.status = 1 {
3548eee6 237 u = (struct user *) malloc(sizeof(struct user));
238 strcpy(u->name, strtrim(name));
d3c7c2f9 239 u->uid = uid;
3548eee6 240 u->lists = NULL;
241 hash_store(users, id, u);
242## }
6efe4fbf 243
330c8868 244## range of m is imembers
245## retrieve (lid = m.list_id, id = m.member_id) where m.member_type = "USER" {
969cfb2b 246 if ((u = (struct user *) hash_lookup(users, id)) &&
247 (hash_lookup(groups, lid) != NULL)) {
3548eee6 248 g = (struct grp *) malloc(sizeof(struct grp));
249 g->next = u->lists;
250 u->lists = g;
251 g->id = lid;
252 }
6efe4fbf 253## }
3548eee6 254
255 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
256 for (b = *p; b; b = b->next) {
969cfb2b 257 i = 0;
d3c7c2f9 258 u = (struct user *)b->data;
259 sprintf(buf, "%s:%d", u->name, u->uid);
260 for (g = u->lists; g; g = g->next)
969cfb2b 261 if (l = hash_lookup(groups, g->id)) {
262 i++;
263 strcat(buf, l);
264 }
330c8868 265 /* should free stuff here...
969cfb2b 266 if (i > 1) {
267 remove_duplicate_groups(buf);
330c8868 268 } */
3548eee6 269 b->data = strsave(buf);
270 fprintf(fd, "%s\n", buf);
271 }
6efe4fbf 272 }
3548eee6 273
6efe4fbf 274 fclose(fd);
6efe4fbf 275 free(groups);
276 return(users);
277##}
278
969cfb2b 279#define MAXGROUPS 256
280
281remove_duplicate_groups(p)
282register char *p;
283{
284 register char *pp;
285 char *index();
286 register int *mp;
287 int memory[MAXGROUPS], i, *mg = &memory[MAXGROUPS-1];
288
289 bzero(memory, MAXGROUPS * sizeof(int));
290 p = index(p, ':');
291 p++;
292 while (p = index(p, ':')) {
293 i = atoi(++p);
294 pp = p;
295 for (mp = memory; *mp && mp < mg; mp++)
296 if (*mp == i) {
297 if (p = index(p, ':'))
298 strcpy(pp, ++p);
299 else {
300 *(--pp) = 0;
301 }
302 p = pp - 2;
303 break;
304 }
305 *mp = i;
306 }
307}
308
6efe4fbf 309
3548eee6 310/* Now do each of the servers, linking the credentials list file and
311 * compiling the quota and dirs files.
312 */
313
314int do_machs(machs)
6efe4fbf 315struct save_queue *machs;
316##{
3548eee6 317## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
318## char *filetime;
319## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
6efe4fbf 320 char file[64], f1[64], f2[64], *cp, *index();
321 int prevuid, quotasum;
322 FILE *fd;
3548eee6 323 struct stat sb;
6efe4fbf 324 struct save_queue *sq;
325
3548eee6 326/*
327 sprintf(file, "%s/list-", NFS_DIR);
328 if (stat(file, &sb) == 0) {
329 filetime = ingres_date_and_time(sb.st_mtime);
330 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
331 # where tblstats.table = "serverhosts"
332 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
333 # where tblstats.table = "filesys"
334 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
335 # where tblstats.table = "nfsquota"
336 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
337 # where tblstats.table = "nfsphys"
338 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
339 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
340 return(0);
341 }
342 }
343*/
344
345 fprintf(stderr, "Building machine files\n");
346
6efe4fbf 347## range of s is serverhosts
348## range of m is machine
349## range of n is nfsphys
350## range of q is nfsquota
351## range of f is filesys
352## range of u is users
353## range of l is list
140e3035 354
355/* acquire locks on users, lists, machines & filesystems */
356## begin transaction
357## retrieve (listname = u.modtime) where u.users_id = 0
358## retrieve (listname = l.modtime) where l.list_id = 0
359## retrieve (listname = m.modtime) where m.mach_id = 0
360## retrieve (listname = f.modtime) where f.filsys_id = 0
361
6efe4fbf 362 while (sq_get_data(machs, &machname)) {
3548eee6 363## repeat retrieve (listname = trim(s.value3))
6efe4fbf 364## where s.mach_id = m.mach_id and m.name = @machname
3548eee6 365 strtrim(machname);
6efe4fbf 366 sprintf(f1, "%s/list-%s", NFS_DIR, listname);
367 sprintf(f2, "%s/%s.cred", NFS_DIR, machname);
368 unlink(f2); /* ignore errors on this unlink */
369 if (link(f1, f2)) {
370 fprintf(stderr, "Cannot link %s to %s\n", f1, f2);
371 exit(SMS_OCONFIG);
372 }
373 sq = sq_create();
374## repeat retrieve (dev = trim(n.#device))
375## where n.mach_id = m.mach_id and m.name = @machname {
376 sq_save_data(sq, strsave(dev));
377## }
378 while (sq_get_data(sq, &device)) {
379## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
380## m.#name = @machname and n.#device = @device
381 while (cp = index(device, '/')) *cp = '@';
382 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
383 fd = fopen(file, "w");
384 if (!fd) {
385 fprintf(stderr, "cannot open %s for output\n", file);
386 exit(SMS_OCONFIG);
387 }
388 prevuid = -1;
389 quotasum = 0;
390## repeat retrieve (uid = u.#uid, quota = q.#quota)
391## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
392## f.phys_id = @id and u.status != 0 sort by #uid {
393 if (uid != prevuid) {
394 if (quotasum)
395 fprintf(fd, "%d %d\n", prevuid, quotasum);
396 prevuid = uid;
397 quotasum = quota;
398 } else {
399 quotasum += quota;
400 }
401## }
402 if (quotasum)
403 fprintf(fd, "%d %d\n", prevuid, quotasum);
404 if (fclose(fd)) {
405 fprintf(stderr, "error closing %s", file);
406 exit(SMS_CCONFIG);
407 }
408 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
409 fd = fopen(file, "w");
410 if (!fd) {
411 fprintf(stderr, "cannot open %s for output\n", file);
412 exit(SMS_OCONFIG);
413 }
3548eee6 414## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
415## uid = u.#uid, gid = l.#gid)
6efe4fbf 416## where f.phys_id = @id and f.owner = u.users_id and
417## f.owners = l.list_id and f.createflg != 0 {
3548eee6 418 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
6efe4fbf 419## }
420 if (fclose(fd)) {
421 fprintf(stderr, "error closing %s", file);
422 exit(SMS_CCONFIG);
423 }
424 }
425 sq_destroy(sq);
426 }
1daee9f8 427## end transaction
3548eee6 428 return(1);
6efe4fbf 429##}
This page took 0.144318 seconds and 5 git commands to generate.