3 * Do AFS incremental updates
5 * Copyright (C) 1989,1992 by the Massachusetts Institute of Technology
6 * for copying and distribution information, please see the file
10 #include <sys/types.h>
16 #include <moira_site.h>
18 #include <afs/param.h>
19 #include <afs/cellconfig.h>
20 #include <afs/venus.h>
21 #include <afs/ptclient.h>
22 #include <afs/pterror.h>
24 #define STOP_FILE "/moira/afs/noafs"
28 #define file_exists(file) (access((file), F_OK) == 0)
36 int beforec, afterc, i;
37 char *table, **before, **after;
40 for (i = getdtablesize() - 1; i > 2; i--)
44 beforec = atoi(argv[2]);
46 afterc = atoi(argv[3]);
47 after = &argv[4 + beforec];
52 for (i = 0; i < beforec; i++) {
55 strcat(buf, before[i]);
58 for (i = 0; i < afterc; i++) {
61 strcat(buf, after[i]);
68 initialize_sms_error_table();
69 initialize_krb_error_table();
71 for (i=0; file_exists(STOP_FILE); i++) {
73 critical_alert("incremental",
74 "AFS incremental failed (%s exists): %s",
81 if (!strcmp(table, "users")) {
82 do_user(before, beforec, after, afterc);
83 } else if (!strcmp(table, "list")) {
84 do_list(before, beforec, after, afterc);
85 } else if (!strcmp(table, "members")) {
86 do_member(before, beforec, after, afterc);
87 } else if (!strcmp(table, "filesys")) {
88 do_filesys(before, beforec, after, afterc);
89 } else if (!strcmp(table, "quota")) {
90 do_quota(before, beforec, after, afterc);
99 int success = 0, tries = 0;
101 while (success == 0 && tries < 2) {
104 com_err(whoami, 0, "Executing command: %s", cmd);
105 if (system(cmd) == 0)
109 critical_alert("incremental", "failed command: %s", cmd);
113 add_user_lists(ac, av, user)
119 sleep(1); /* give ptserver some time */
120 edit_group(1, av[0], "USER", user);
125 do_user(before, beforec, after, afterc)
131 int astate, bstate, auid, buid, code, tries;
135 auid = buid = astate = bstate = 0;
136 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
137 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
138 if (afterc > U_UID) auid = atoi(after[U_UID]);
139 if (beforec > U_UID) buid = atoi(before[U_UID]);
141 /* We consider "half-registered" users to be active */
142 if (astate == 2) astate = 1;
143 if (bstate == 2) bstate = 1;
145 if (astate != 1 && bstate != 1) /* inactive user */
148 if (astate == bstate && auid == buid &&
149 !strcmp(before[U_NAME], after[U_NAME]))
150 /* No AFS related attributes have changed */
153 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
155 critical_alert("incremental", "Couldn't initialize libprot: %s",
156 error_message(code));
160 if (astate == bstate) {
161 /* Only a modify has to be done */
163 while (code=pr_ChangeEntry(before[U_NAME], after[U_NAME], auid, "")) {
164 if (++tries > PR_TRIES)
166 if (code == UNOQUORUM) { sleep(90); continue; }
170 critical_alert("incremental",
171 "Couldn't change user %s (id %d) to %s (id %d): %s",
172 before[U_NAME], buid, after[U_NAME], auid,
173 error_message(code));
179 while (code = pr_DeleteByID(buid)) {
180 if (++tries > PR_TRIES)
182 if (code == UNOQUORUM) { sleep(90); continue; }
185 if (code && code != PRNOENT) {
186 critical_alert("incremental",
187 "Couldn't delete user %s (id %d): %s",
188 before[U_NAME], buid, error_message(code));
194 while (code = pr_CreateUser(after[U_NAME], &auid)) {
195 if (++tries > PR_TRIES)
197 if (code == UNOQUORUM) { sleep(90); continue; }
201 critical_alert("incremental",
202 "Couldn't create user %s (id %d): %s",
203 after[U_NAME], auid, error_message(code));
208 /* Reactivating a user; get his group list */
209 gethostname(hostname, sizeof(hostname));
210 code = mr_connect(hostname);
211 if (!code) code = mr_auth("afs.incr");
213 critical_alert("incremental",
214 "Error contacting Moira server to retrieve grouplist of user %s: %s",
215 after[U_NAME], error_message(code));
219 av[1] = after[U_NAME];
220 code = mr_query("get_lists_of_member", 2, av,
221 add_user_lists, after[U_NAME]);
223 critical_alert("incremental",
224 "Couldn't retrieve membership of user %s: %s",
225 after[U_NAME], error_message(code));
234 do_list(before, beforec, after, afterc)
240 register int agid, bgid;
241 int ahide, bhide, tries;
244 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
248 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
249 bgid = atoi(before[L_GID]);
250 bhide = atoi(before[L_HIDDEN]);
252 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
253 agid = atoi(after[L_GID]);
254 ahide = atoi(after[L_HIDDEN]);
257 if (agid == 0 && bgid == 0) /* Not active groups */
260 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
262 critical_alert("incremental", "Couldn't initialize libprot: %s",
263 error_message(code));
268 if (strcmp(after[L_NAME], before[L_NAME])) {
269 /* Only a modify is required */
270 strcpy(g1, "system:");
271 strcpy(g2, "system:");
272 strcat(g1, before[L_NAME]);
273 strcat(g2, after[L_NAME]);
275 while (code = pr_ChangeEntry(g1, g2, -agid, "")) {
276 if (++tries > PR_TRIES)
278 if (code == UNOQUORUM) { sleep(90); continue; }
282 critical_alert("incremental",
283 "Couldn't change group %s (id %d) to %s (id %d): %s",
284 before[L_NAME], -bgid, after[L_NAME], -agid,
285 error_message(code));
288 if (ahide != bhide) {
290 while (code = pr_SetFieldsEntry
291 (-agid, PR_SF_ALLBITS,
292 (ahide ?PRP_STATUS_ANY :PRP_GROUP_DEFAULT)>>PRIVATE_SHIFT,
293 0 /*ngroups*/, 0 /*nusers*/)) {
294 if (++tries > PR_TRIES)
296 if (code == UNOQUORUM) { sleep(90); continue; }
300 critical_alert("incremental",
301 "Couldn't set flags of group %s: %s",
302 after[L_NAME], error_message(code));
309 while (code = pr_DeleteByID(-bgid)) {
310 if (++tries > PR_TRIES)
312 if (code == UNOQUORUM) { sleep(90); continue; }
315 if (code && code != PRNOENT) {
316 critical_alert("incremental",
317 "Couldn't delete group %s (id %d): %s",
318 before[L_NAME], -bgid, error_message(code));
323 strcpy(g1, "system:");
324 strcat(g1, after[L_NAME]);
325 strcpy(g2, "system:administrators");
328 while (code = pr_CreateGroup(g1, g2, &id)) {
329 if (++tries > PR_TRIES)
331 if (code == UNOQUORUM) { sleep(90); continue; }
335 critical_alert("incremental",
336 "Couldn't create group %s (id %d): %s",
337 after[L_NAME], id, error_message(code));
342 while (code = pr_SetFieldsEntry
343 (-agid, PR_SF_ALLBITS,
344 (ahide ?PRP_STATUS_ANY :PRP_GROUP_DEFAULT)>>PRIVATE_SHIFT,
345 0 /*ngroups*/, 0 /*nusers*/)) {
346 if (++tries > PR_TRIES)
348 if (code == UNOQUORUM) { sleep(90); continue; }
352 critical_alert("incremental",
353 "Couldn't set flags of group %s: %s",
354 after[L_NAME], error_message(code));
358 sleep(1); /* give ptserver some time */
360 /* We need to make sure the group is properly populated */
361 if (beforec < L_ACTIVE || atoi(before[L_ACTIVE]) == 0) return;
363 gethostname(hostname, sizeof(hostname));
364 code = mr_connect(hostname);
365 if (!code) code = mr_auth("afs.incr");
367 critical_alert("incremental",
368 "Error contacting Moira server to resolve %s: %s",
369 after[L_NAME], error_message(code));
373 av[1] = after[L_NAME];
374 get_members(2, av, after[L_NAME]);
382 do_member(before, beforec, after, afterc)
391 if ((beforec < 4 || !atoi(before[LM_END])) &&
392 (afterc < 4 || !atoi(after[LM_END])))
395 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
397 critical_alert("incremental", "Couldn't initialize libprot: %s",
398 error_message(code));
403 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
405 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
409 get_members(ac, av, group)
416 if (strcmp(av[0], "LIST")) {
417 sleep(1); /* give ptserver some time */
418 edit_group(1, group, av[0], av[1]);
420 code = mr_query("get_end_members_of_list", 1, &av[1],
423 critical_alert("incremental",
424 "Couldn't retrieve full membership of %s: %s",
425 group, error_message(code));
431 edit_group(op, group, type, member)
438 char buf[PR_MAXNAMELEN];
442 static char local_realm[REALM_SZ+1] = "";
443 extern long pr_AddToGroup(), pr_RemoveUserFromGroup();
445 fn = op ? pr_AddToGroup : pr_RemoveUserFromGroup;
447 /* The following KERBEROS code allows for the use of entities
451 krb_get_lrealm(local_realm, 1);
452 if (!strcmp(type, "KERBEROS")) {
453 p = index(member, '@');
454 if (p && !strcasecmp(p+1, local_realm))
456 } else if (strcmp(type, "USER"))
457 return; /* invalid type */
459 strcpy(buf, "system:");
461 sleep(1); /* give ptserver some time */
462 while (code = (*fn)(member, buf)) {
463 if (++tries > PR_TRIES)
465 if (code == UNOQUORUM) { sleep(90); continue; }
469 if (op==0 && code == PRNOENT) return;
470 if (op==1 && code == PRIDEXIST) return;
471 if (strcmp(type, "KERBEROS") || code != PRNOENT) {
472 critical_alert("incremental",
473 "Couldn't %s %s %s %s: %s",
474 op ? "add" : "remove", member,
475 op ? "to" : "from", buf,
476 error_message(code));
483 do_filesys(before, beforec, after, afterc)
491 if (beforec < FS_CREATE) {
492 if (afterc < FS_CREATE || atoi(after[FS_CREATE])==0 ||
493 strcmp(after[FS_TYPE], "AFS"))
496 /* new locker creation */
497 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
498 BIN_DIR, BIN_DIR, BIN_DIR,
499 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
500 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
505 /* What do we do? When do we use FS_CREATE?
507 * TYPE change: AFS->ERR, ERR->AFS: rename/unmount/remount
508 * LOCKERTYPE change: rename/remount
509 * PACK change: remount
510 * LABEL change: rename/remount
511 * Deletion: rename/unmount
513 if (afterc < FS_CREATE) {
514 if (!strcmp(before[FS_TYPE], "AFS"))
515 critical_alert("incremental",
516 "Could not delete AFS filesystem %s: Operation not supported",
521 if (!strcmp(after[FS_TYPE], "AFS")) {
522 if (strcmp(before[FS_TYPE], "AFS")) {
523 critical_alert("incremental",
524 "Cannot convert %s to an AFS filesystem: Operation not supported",
527 critical_alert("incremental",
528 "Cannot change attributes of AFS filesystem %s: Operation not supported",
536 do_quota(before, beforec, after, afterc)
544 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
545 strncmp("/afs/", after[Q_DIRECTORY], 5))
548 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
549 BIN_DIR, BIN_DIR, BIN_DIR,
550 after[Q_DIRECTORY], after[Q_QUOTA]);