]> andersk Git - moira.git/blame - gen/nfs.qc
fix problem with strtrim on empty strings
[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];
d3c7c2f9 178 int uid;
3548eee6 179 struct grp *lists;
180};
181
182struct hash *do_everyone()
6efe4fbf 183##{
3548eee6 184 char buf[BUFSIZ], *l;
185 struct hash *groups, *users;
186 struct user *u;
187 struct grp *g;
188 struct bucket *b, **p;
6efe4fbf 189## char name[33];
190## int gid, id, lid, maxid, uid;
191 FILE *fd;
192 int first;
193 struct save_queue *sq;
194
3548eee6 195 fprintf(stderr, "Building the list of everybody\n");
6efe4fbf 196 sprintf(buf, "%s/list-", NFS_DIR);
197 fd = fopen(buf, "w");
198 if (!fd) {
199 fprintf(stderr, "cannot open %s for output\n", buf);
200 exit(SMS_OCONFIG);
201 }
202
203 /* make space for group list */
3548eee6 204 groups = create_hash(15000);
6efe4fbf 205
206 /* retrieve simple groups */
3548eee6 207## range of l is list
6efe4fbf 208## retrieve (gid = l.#gid, lid = l.list_id)
3548eee6 209## where l.group != 0 and l.active != 0 {
210 sprintf(buf, ":%d", gid);
211 hash_store(groups, lid, strsave(buf));
6efe4fbf 212## }
213
214 /* get special cases: lists that aren't groups themselves but are
215 * members of groups. */
216 sq = sq_create();
217## range of m is members
218## retrieve (gid = list.#gid, lid = l.list_id)
3548eee6 219## where l.group = 0 and l.active != 0 and m.member_type = "LIST" and
6efe4fbf 220## m.member_id = l.list_id and m.list_id = list.list_id and
221## list.group != 0 {
3548eee6 222 if (l = hash_lookup(groups, lid)) {
223 sprintf(buf, "%s:%d", l, gid);
224 free(l);
225 } else {
226 sprintf(buf, ":%d", gid);
227 }
228 hash_store(groups, lid, strsave(buf));
229 sq_save_data(sq, lid);
6efe4fbf 230## }
3548eee6 231
6efe4fbf 232 while (sq_get_data(sq, &id)) {
3548eee6 233## repeat retrieve (lid = l.list_id, gid = l.#gid)
234## where l.group = 0 and l.active != 0 and m.member_type = "LIST" and
6efe4fbf 235## m.member_id = l.list_id and m.list_id = @id {
3548eee6 236 if (l = hash_lookup(groups, lid)) {
237 sprintf(buf, "%s%s", l, hash_lookup(groups, id));
238 free(l);
239 } else {
240 strcpy(buf, hash_lookup(groups, id));
241 }
242 sq_save_unique_data(sq, lid);
243## }
6efe4fbf 244 }
245 sq_destroy(sq);
246
3548eee6 247 /* now do grplists */
248 users = create_hash(10000);
6efe4fbf 249## range of u is users
d3c7c2f9 250## retrieve (id = u.users_id, name = u.login, uid = u.#uid)
251## where u.status = 1 {
3548eee6 252 u = (struct user *) malloc(sizeof(struct user));
253 strcpy(u->name, strtrim(name));
d3c7c2f9 254 u->uid = uid;
3548eee6 255 u->lists = NULL;
256 hash_store(users, id, u);
257## }
6efe4fbf 258
3548eee6 259## repeat retrieve (lid = m.list_id, id = m.member_id)
260## where m.member_type = "USER" {
261 if (u = (struct user *) hash_lookup(users, id)) {
262 g = (struct grp *) malloc(sizeof(struct grp));
263 g->next = u->lists;
264 u->lists = g;
265 g->id = lid;
266 }
6efe4fbf 267## }
3548eee6 268
269 for (p = &(users->data[users->size - 1]); p >= users->data; p--) {
270 for (b = *p; b; b = b->next) {
d3c7c2f9 271 u = (struct user *)b->data;
272 sprintf(buf, "%s:%d", u->name, u->uid);
273 for (g = u->lists; g; g = g->next)
3548eee6 274 if (l = hash_lookup(groups, g->id))
275 strcat(buf, l);
276 /* should free stuff here... */
277 b->data = strsave(buf);
278 fprintf(fd, "%s\n", buf);
279 }
6efe4fbf 280 }
3548eee6 281
6efe4fbf 282 fclose(fd);
6efe4fbf 283 free(groups);
284 return(users);
285##}
286
287
3548eee6 288/* Now do each of the servers, linking the credentials list file and
289 * compiling the quota and dirs files.
290 */
291
292int do_machs(machs)
6efe4fbf 293struct save_queue *machs;
294##{
3548eee6 295## char *machname, listname[33], dev[33], *device, dir[64], fstype[9];
296## char *filetime;
297## int uid, quota, id, gid, flag1, flag2, flag3, flag4;
6efe4fbf 298 char file[64], f1[64], f2[64], *cp, *index();
299 int prevuid, quotasum;
300 FILE *fd;
3548eee6 301 struct stat sb;
6efe4fbf 302 struct save_queue *sq;
303
3548eee6 304/*
305 sprintf(file, "%s/list-", NFS_DIR);
306 if (stat(file, &sb) == 0) {
307 filetime = ingres_date_and_time(sb.st_mtime);
308 # retrieve (flag1 = int4(interval("min", tblstats.modtime - filetime)))
309 # where tblstats.table = "serverhosts"
310 # retrieve (flag2 = int4(interval("min", tblstats.modtime - filetime)))
311 # where tblstats.table = "filesys"
312 # retrieve (flag3 = int4(interval("min", tblstats.modtime - filetime)))
313 # where tblstats.table = "nfsquota"
314 # retrieve (flag4 = int4(interval("min", tblstats.modtime - filetime)))
315 # where tblstats.table = "nfsphys"
316 if (flag1 < 0 && flag2 < 0 && flag3 < 0 && flag4 < 0) {
317 fprintf(stderr, "The machine files do not need to be rebuilt.\n");
318 return(0);
319 }
320 }
321*/
322
323 fprintf(stderr, "Building machine files\n");
324
6efe4fbf 325## range of s is serverhosts
326## range of m is machine
327## range of n is nfsphys
328## range of q is nfsquota
329## range of f is filesys
330## range of u is users
331## range of l is list
332 while (sq_get_data(machs, &machname)) {
3548eee6 333## repeat retrieve (listname = trim(s.value3))
6efe4fbf 334## where s.mach_id = m.mach_id and m.name = @machname
3548eee6 335 strtrim(machname);
6efe4fbf 336 sprintf(f1, "%s/list-%s", NFS_DIR, 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(SMS_OCONFIG);
342 }
343 sq = sq_create();
344## repeat retrieve (dev = trim(n.#device))
345## where n.mach_id = m.mach_id and m.name = @machname {
346 sq_save_data(sq, strsave(dev));
347## }
348 while (sq_get_data(sq, &device)) {
349## repeat retrieve (id = n.nfsphys_id) where n.mach_id = m.mach_id and
350## m.#name = @machname and n.#device = @device
351 while (cp = index(device, '/')) *cp = '@';
352 sprintf(file, "%s/%s.%s.quotas", NFS_DIR, machname, device);
353 fd = fopen(file, "w");
354 if (!fd) {
355 fprintf(stderr, "cannot open %s for output\n", file);
356 exit(SMS_OCONFIG);
357 }
358 prevuid = -1;
359 quotasum = 0;
360## repeat retrieve (uid = u.#uid, quota = q.#quota)
361## where q.users_id = u.users_id and q.filsys_id = f.filsys_id and
362## f.phys_id = @id and u.status != 0 sort by #uid {
363 if (uid != prevuid) {
364 if (quotasum)
365 fprintf(fd, "%d %d\n", prevuid, quotasum);
366 prevuid = uid;
367 quotasum = quota;
368 } else {
369 quotasum += quota;
370 }
371## }
372 if (quotasum)
373 fprintf(fd, "%d %d\n", prevuid, quotasum);
374 if (fclose(fd)) {
375 fprintf(stderr, "error closing %s", file);
376 exit(SMS_CCONFIG);
377 }
378 sprintf(file, "%s/%s.%s.dirs", NFS_DIR, machname, device);
379 fd = fopen(file, "w");
380 if (!fd) {
381 fprintf(stderr, "cannot open %s for output\n", file);
382 exit(SMS_OCONFIG);
383 }
3548eee6 384## repeat retrieve (dir = trim(f.#name), fstype = trim(f.lockertype),
385## uid = u.#uid, gid = l.#gid)
6efe4fbf 386## where f.phys_id = @id and f.owner = u.users_id and
387## f.owners = l.list_id and f.createflg != 0 {
3548eee6 388 fprintf(fd, "%s %d %d %s\n", dir, uid, gid, fstype);
6efe4fbf 389## }
390 if (fclose(fd)) {
391 fprintf(stderr, "error closing %s", file);
392 exit(SMS_CCONFIG);
393 }
394 }
395 sq_destroy(sq);
396 }
3548eee6 397 return(1);
6efe4fbf 398##}
This page took 0.107284 seconds and 5 git commands to generate.