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