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"
27 #define file_exists(file) (access((file), F_OK) == 0)
35 int beforec, afterc, i;
36 char *table, **before, **after;
39 for (i = getdtablesize() - 1; i > 2; i--)
43 beforec = atoi(argv[2]);
45 afterc = atoi(argv[3]);
46 after = &argv[4 + beforec];
51 for (i = 0; i < beforec; i++) {
54 strcat(buf, before[i]);
57 for (i = 0; i < afterc; i++) {
60 strcat(buf, after[i]);
67 initialize_sms_error_table();
68 initialize_krb_error_table();
70 for (i=0; file_exists(STOP_FILE); i++) {
72 critical_alert("incremental",
73 "AFS incremental failed (%s exists): %s",
80 if (!strcmp(table, "users")) {
81 do_user(before, beforec, after, afterc);
82 } else if (!strcmp(table, "list")) {
83 do_list(before, beforec, after, afterc);
84 } else if (!strcmp(table, "members")) {
85 do_member(before, beforec, after, afterc);
86 } else if (!strcmp(table, "filesys")) {
87 do_filesys(before, beforec, after, afterc);
88 } else if (!strcmp(table, "quota")) {
89 do_quota(before, beforec, after, afterc);
98 int success = 0, tries = 0;
100 while (success == 0 && tries < 2) {
103 com_err(whoami, 0, "Executing command: %s", cmd);
104 if (system(cmd) == 0)
108 critical_alert("incremental", "failed command: %s", cmd);
112 add_user_lists(ac, av, user)
118 sleep(1); /* give ptserver some time */
119 edit_group(1, av[0], "USER", user);
124 do_user(before, beforec, after, afterc)
130 int astate, bstate, auid, buid, code, tries;
134 auid = buid = astate = bstate = 0;
135 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
136 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
137 if (afterc > U_UID) auid = atoi(after[U_UID]);
138 if (beforec > U_UID) buid = atoi(before[U_UID]);
140 /* We consider "half-registered" users to be active */
141 if (astate == 2) astate = 1;
142 if (bstate == 2) bstate = 1;
144 if (astate != 1 && bstate != 1) /* inactive user */
147 if (astate == bstate && auid == buid &&
148 !strcmp(before[U_NAME], after[U_NAME]))
149 /* No AFS related attributes have changed */
152 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
154 critical_alert("incremental", "Couldn't initialize libprot: %s",
155 error_message(code));
159 if (astate == bstate) {
160 /* Only a modify has to be done */
162 while (code=pr_ChangeEntry(before[U_NAME], after[U_NAME], auid, "")) {
163 if (++tries > TRY_PR)
165 if (code == UNOQUORUM) { sleep(90); continue; }
169 critical_alert("incremental",
170 "Couldn't change user %s (id %d) to %s (id %d): %s",
171 before[U_NAME], buid, after[U_NAME], auid,
172 error_message(code));
178 while (code = pr_DeleteByID(buid)) {
179 if (++tries > TRY_PR)
181 if (code == UNOQUORUM) { sleep(90); continue; }
184 if (code && code != PRNOENT) {
185 critical_alert("incremental",
186 "Couldn't delete user %s (id %d): %s",
187 before[U_NAME], buid, error_message(code));
193 while (code = pr_CreateUser(after[U_NAME], &auid)) {
194 if (++tries > TRY_PR)
196 if (code == UNOQUORUM) { sleep(90); continue; }
200 critical_alert("incremental",
201 "Couldn't create user %s (id %d): %s",
202 after[U_NAME], auid, error_message(code));
207 /* Reactivating a user; get his group list */
208 gethostname(hostname, sizeof(hostname));
209 code = mr_connect(hostname);
210 if (!code) code = mr_auth("afs.incr");
212 critical_alert("incremental",
213 "Error contacting Moira server to retrieve grouplist of user %s: %s",
214 after[U_NAME], error_message(code));
218 av[1] = after[U_NAME];
219 code = mr_query("get_lists_of_member", 2, av,
220 add_user_lists, after[U_NAME]);
222 critical_alert("incremental",
223 "Couldn't retrieve membership of user %s: %s",
224 after[U_NAME], error_message(code));
233 do_list(before, beforec, after, afterc)
239 register int agid, bgid;
240 int ahide, bhide, tries;
243 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
247 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
248 bgid = atoi(before[L_GID]);
249 bhide = atoi(before[L_HIDDEN]);
251 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
252 agid = atoi(after[L_GID]);
253 ahide = atoi(after[L_HIDDEN]);
256 if (agid == 0 && bgid == 0) /* Not active groups */
259 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
261 critical_alert("incremental", "Couldn't initialize libprot: %s",
262 error_message(code));
267 if (strcmp(after[L_NAME], before[L_NAME])) {
268 /* Only a modify is required */
269 strcpy(g1, "system:");
270 strcpy(g2, "system:");
271 strcat(g1, before[L_NAME]);
272 strcat(g2, after[L_NAME]);
274 while (code = pr_ChangeEntry(g1, g2, -agid, "")) {
275 if (++tries > TRY_PR)
277 if (code == UNOQUORUM) { sleep(90); continue; }
281 critical_alert("incremental",
282 "Couldn't change group %s (id %d) to %s (id %d): %s",
283 before[L_NAME], -bgid, after[L_NAME], -agid,
284 error_message(code));
287 if (ahide != bhide) {
289 while (code = pr_SetFieldsEntry
290 (-agid, PR_SF_ALLBITS,
291 (ahide ?PRP_STATUS_ANY :PRP_GROUP_DEFAULT)>>PRIVATE_SHIFT,
292 0 /*ngroups*/, 0 /*nusers*/)) {
293 if (++tries > TRY_PR)
295 if (code == UNOQUORUM) { sleep(90); continue; }
299 critical_alert("incremental",
300 "Couldn't set flags of group %s: %s",
301 after[L_NAME], error_message(code));
308 while (code = pr_DeleteByID(-bgid)) {
309 if (++tries > TRY_PR)
311 if (code == UNOQUORUM) { sleep(90); continue; }
314 if (code && code != PRNOENT) {
315 critical_alert("incremental",
316 "Couldn't delete group %s (id %d): %s",
317 before[L_NAME], -bgid, error_message(code));
322 strcpy(g1, "system:");
323 strcat(g1, after[L_NAME]);
324 strcpy(g2, "system:administrators");
327 while (code = pr_CreateGroup(g1, g2, &id)) {
328 if (++tries > TRY_PR)
330 if (code == UNOQUORUM) { sleep(90); continue; }
334 critical_alert("incremental",
335 "Couldn't create group %s (id %d): %s",
336 after[L_NAME], id, error_message(code));
341 while (code = pr_SetFieldsEntry
342 (-agid, PR_SF_ALLBITS,
343 (ahide ?PRP_STATUS_ANY :PRP_GROUP_DEFAULT)>>PRIVATE_SHIFT,
344 0 /*ngroups*/, 0 /*nusers*/)) {
345 if (++tries > TRY_PR)
347 if (code == UNOQUORUM) { sleep(90); continue; }
351 critical_alert("incremental",
352 "Couldn't set flags of group %s: %s",
353 after[L_NAME], error_message(code));
357 sleep(1); /* give ptserver some time */
359 /* We need to make sure the group is properly populated */
360 if (beforec < L_ACTIVE || atoi(before[L_ACTIVE]) == 0) return;
362 gethostname(hostname, sizeof(hostname));
363 code = mr_connect(hostname);
364 if (!code) code = mr_auth("afs.incr");
366 critical_alert("incremental",
367 "Error contacting Moira server to resolve %s: %s",
368 after[L_NAME], error_message(code));
372 av[1] = after[L_NAME];
373 get_members(2, av, after[L_NAME]);
381 do_member(before, beforec, after, afterc)
390 if ((beforec < 4 || !atoi(before[LM_END])) &&
391 (afterc < 4 || !atoi(after[LM_END])))
394 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
396 critical_alert("incremental", "Couldn't initialize libprot: %s",
397 error_message(code));
402 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
404 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
408 get_members(ac, av, group)
415 if (strcmp(av[0], "LIST")) {
416 sleep(1); /* give ptserver some time */
417 edit_group(1, group, av[0], av[1]);
419 code = mr_query("get_end_members_of_list", 1, &av[1],
422 critical_alert("incremental",
423 "Couldn't retrieve full membership of %s: %s",
424 group, error_message(code));
430 edit_group(op, group, type, member)
437 char buf[PR_MAXNAMELEN];
441 static char local_realm[REALM_SZ+1] = "";
442 extern long pr_AddToGroup(), pr_RemoveUserFromGroup();
444 fn = op ? pr_AddToGroup : pr_RemoveUserFromGroup;
446 /* The following KERBEROS code allows for the use of entities
450 krb_get_lrealm(local_realm, 1);
451 if (!strcmp(type, "KERBEROS")) {
452 p = index(member, '@');
453 if (p && !strcasecmp(p+1, local_realm))
455 } else if (strcmp(type, "USER"))
456 return; /* invalid type */
458 strcpy(buf, "system:");
460 sleep(1); /* give ptserver some time */
461 while (code = (*fn)(member, buf)) {
462 if (++tries > TRY_PR)
464 if (code == UNOQUORUM) { sleep(90); continue; }
468 if (op==0 && code == PRNOENT) return;
469 if (op==1 && code == PRIDEXIST) return;
470 if (strcmp(type, "KERBEROS") || code != PRNOENT) {
471 critical_alert("incremental",
472 "Couldn't %s %s %s %s: %s",
473 op ? "add" : "remove", member,
474 op ? "to" : "from", buf,
475 error_message(code));
482 do_filesys(before, beforec, after, afterc)
490 if (beforec < FS_CREATE) {
491 if (afterc < FS_CREATE || atoi(after[FS_CREATE])==0 ||
492 strcmp(after[FS_TYPE], "AFS"))
495 /* new locker creation */
496 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
497 BIN_DIR, BIN_DIR, BIN_DIR,
498 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
499 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
504 /* What do we do? When do we use FS_CREATE?
506 * TYPE change: AFS->ERR, ERR->AFS: rename/unmount/remount
507 * LOCKERTYPE change: rename/remount
508 * PACK change: remount
509 * LABEL change: rename/remount
510 * Deletion: rename/unmount
512 if (afterc < FS_CREATE) {
513 if (!strcmp(before[FS_TYPE], "AFS"))
514 critical_alert("incremental",
515 "Could not delete AFS filesystem %s: Operation not supported\n",
520 if (!strcmp(after[FS_TYPE], "AFS")) {
521 if (strcmp(before[FS_TYPE], "AFS")) {
522 critical_alert("incremental",
523 "Cannot convert %s to an AFS filesystem: Operation not supported\n",
526 critical_alert("incremental",
527 "Cannot change attributes of AFS filesystem %s: Operation not supported\n",
535 do_quota(before, beforec, after, afterc)
543 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
544 strncmp("/afs/", after[Q_DIRECTORY], 5))
547 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
548 BIN_DIR, BIN_DIR, BIN_DIR,
549 after[Q_DIRECTORY], after[Q_QUOTA]);