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
11 #include <sys/types.h>
17 #include <moira_site.h>
19 #include <afs/param.h>
20 #include <afs/cellconfig.h>
21 #include <afs/venus.h>
22 #include <afs/ptclient.h>
23 #include <afs/pterror.h>
25 #define STOP_FILE "/moira/afs/noafs"
26 #define file_exists(file) (access((file), F_OK) == 0)
28 #if defined(vax) && !defined(__STDC__)
34 /* Main stub routines */
41 /* Support stub routines */
49 /* libprot.a routines */
50 extern long pr_Initialize();
51 extern long pr_CreateUser();
52 extern long pr_CreateGroup();
53 extern long pr_DeleteByID();
54 extern long pr_ChangeEntry();
55 extern long pr_SetFieldsEntry();
56 extern long pr_AddToGroup();
57 extern long pr_RemoveUserFromGroup();
59 static char tbl_buf[1024];
65 int beforec, afterc, i;
66 char *table, **before, **after;
68 for (i = getdtablesize() - 1; i > 2; i--)
72 beforec = atoi(argv[2]);
74 afterc = atoi(argv[3]);
75 after = &argv[4 + beforec];
80 strcpy(tbl_buf, table);
81 strcat(tbl_buf, " (");
82 for (i = 0; i < beforec; i++) {
85 strcat(tbl_buf, before[i]);
87 strcat(tbl_buf, ")->(");
88 for (i = 0; i < afterc; i++) {
91 strcat(tbl_buf, after[i]);
95 printf("%s\n", tbl_buf);
98 initialize_sms_error_table();
99 initialize_krb_error_table();
101 if (!strcmp(table, "users")) {
102 do_user(before, beforec, after, afterc);
103 } else if (!strcmp(table, "list")) {
104 do_list(before, beforec, after, afterc);
105 } else if (!strcmp(table, "members")) {
106 do_member(before, beforec, after, afterc);
107 } else if (!strcmp(table, "filesys")) {
108 do_filesys(before, beforec, after, afterc);
109 } else if (!strcmp(table, "quota")) {
110 do_quota(before, beforec, after, afterc);
116 do_user(before, beforec, after, afterc)
122 int astate, bstate, auid, buid, code;
126 auid = buid = astate = bstate = 0;
127 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
128 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
129 if (afterc > U_UID) auid = atoi(after[U_UID]);
130 if (beforec > U_UID) buid = atoi(before[U_UID]);
132 /* We consider "half-registered" users to be active */
133 if (astate == 2) astate = 1;
134 if (bstate == 2) bstate = 1;
136 if (astate != 1 && bstate != 1) /* inactive user */
139 if (astate == bstate && auid == buid &&
140 !strcmp(before[U_NAME], after[U_NAME]))
141 /* No AFS related attributes have changed */
144 if (astate == bstate) {
145 /* Only a modify has to be done */
146 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
148 critical_alert("incremental",
149 "Couldn't change user %s (id %d) to %s (id %d): %s",
150 before[U_NAME], buid, after[U_NAME], auid,
151 error_message(code));
156 code = pr_try(pr_DeleteByID, buid);
157 if (code && code != PRNOENT) {
158 critical_alert("incremental",
159 "Couldn't delete user %s (id %d): %s",
160 before[U_NAME], buid, error_message(code));
165 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
167 critical_alert("incremental",
168 "Couldn't create user %s (id %d): %s",
169 after[U_NAME], auid, error_message(code));
174 /* Reactivating a user; get his group list */
175 gethostname(hostname, sizeof(hostname));
176 code = mr_connect(hostname);
177 if (!code) code = mr_auth("afs.incr");
179 critical_alert("incremental",
180 "Error contacting Moira server to retrieve grouplist of user %s: %s",
181 after[U_NAME], error_message(code));
185 av[1] = after[U_NAME];
186 code = mr_query("get_lists_of_member", 2, av,
187 add_user_lists, after[U_NAME]);
188 if (code && code != MR_NO_MATCH)
189 critical_alert("incremental",
190 "Couldn't retrieve membership of user %s: %s",
191 after[U_NAME], error_message(code));
199 do_list(before, beforec, after, afterc)
205 register int agid, bgid;
209 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
213 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
214 bgid = atoi(before[L_GID]);
215 bhide = atoi(before[L_HIDDEN]);
217 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
218 agid = atoi(after[L_GID]);
219 ahide = atoi(after[L_HIDDEN]);
222 if (agid == 0 && bgid == 0) /* Not active groups */
226 if (strcmp(after[L_NAME], before[L_NAME])) {
227 /* Only a modify is required */
228 strcpy(g1, "system:");
229 strcpy(g2, "system:");
230 strcat(g1, before[L_NAME]);
231 strcat(g2, after[L_NAME]);
232 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
234 critical_alert("incremental",
235 "Couldn't change group %s (id %d) to %s (id %d): %s",
236 before[L_NAME], -bgid, after[L_NAME], -agid,
237 error_message(code));
240 if (ahide != bhide) {
241 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
242 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
243 0 /*ngroups*/, 0 /*nusers*/);
245 critical_alert("incremental",
246 "Couldn't set flags of group %s: %s",
247 after[L_NAME], error_message(code));
253 code = pr_try(pr_DeleteByID, -bgid);
254 if (code && code != PRNOENT) {
255 critical_alert("incremental",
256 "Couldn't delete group %s (id %d): %s",
257 before[L_NAME], -bgid, error_message(code));
262 strcpy(g1, "system:");
263 strcat(g1, after[L_NAME]);
264 strcpy(g2, "system:administrators");
266 code = pr_try(pr_CreateGroup, g1, g2, &id);
268 critical_alert("incremental",
269 "Couldn't create group %s (id %d): %s",
270 after[L_NAME], id, error_message(code));
274 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
275 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
276 0 /*ngroups*/, 0 /*nusers*/);
278 critical_alert("incremental",
279 "Couldn't set flags of group %s: %s",
280 after[L_NAME], error_message(code));
284 /* We need to make sure the group is properly populated */
285 if (beforec < L_ACTIVE || atoi(before[L_ACTIVE]) == 0) return;
287 gethostname(hostname, sizeof(hostname));
288 code = mr_connect(hostname);
289 if (!code) code = mr_auth("afs.incr");
291 critical_alert("incremental",
292 "Error contacting Moira server to resolve %s: %s",
293 after[L_NAME], error_message(code));
297 av[1] = after[L_NAME];
298 get_members(2, av, after[L_NAME]);
306 do_member(before, beforec, after, afterc)
315 if ((beforec < 4 || !atoi(before[LM_END])) &&
316 (afterc < 4 || !atoi(after[LM_END])))
320 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
322 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
326 do_filesys(before, beforec, after, afterc)
334 if (beforec < FS_CREATE) {
335 if (afterc < FS_CREATE || atoi(after[FS_CREATE])==0 ||
336 strcmp(after[FS_TYPE], "AFS"))
339 /* new locker creation */
340 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
341 BIN_DIR, BIN_DIR, BIN_DIR,
342 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
343 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
348 /* What do we do? When do we use FS_CREATE?
350 * TYPE change: AFS->ERR, ERR->AFS: rename/unmount/remount
351 * LOCKERTYPE change: rename/remount
352 * PACK change: remount
353 * LABEL change: rename/remount
354 * Deletion: rename/unmount
356 if (afterc < FS_CREATE) {
357 if (!strcmp(before[FS_TYPE], "AFS"))
358 critical_alert("incremental",
359 "Could not delete AFS filesystem %s: Operation not supported",
364 if (!strcmp(after[FS_TYPE], "AFS")) {
365 if (strcmp(before[FS_TYPE], "AFS")) {
366 critical_alert("incremental",
367 "Cannot convert %s to an AFS filesystem: Operation not supported",
370 critical_alert("incremental",
371 "Cannot change attributes of AFS filesystem %s: Operation not supported",
379 do_quota(before, beforec, after, afterc)
387 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
388 strncmp("/afs/", after[Q_DIRECTORY], 5))
391 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
392 BIN_DIR, BIN_DIR, BIN_DIR,
393 after[Q_DIRECTORY], after[Q_QUOTA]);
402 int success=0, tries=0;
406 while (success == 0 && tries < 2) {
409 com_err(whoami, 0, "Executing command: %s", cmd);
410 if (system(cmd) == 0)
414 critical_alert("incremental", "failed command: %s", cmd);
418 add_user_lists(ac, av, user)
424 edit_group(1, av[0], "USER", user);
428 get_members(ac, av, group)
435 if (strcmp(av[0], "LIST")) {
436 edit_group(1, group, av[0], av[1]);
438 code = mr_query("get_end_members_of_list", 1, &av[1],
441 critical_alert("incremental",
442 "Couldn't retrieve full membership of %s: %s",
443 group, error_message(code));
449 edit_group(op, group, type, member)
456 char buf[PR_MAXNAMELEN];
458 static char local_realm[REALM_SZ+1] = "";
460 /* The following KERBEROS code allows for the use of entities
464 krb_get_lrealm(local_realm, 1);
465 if (!strcmp(type, "KERBEROS")) {
466 p = index(member, '@');
467 if (p && !strcasecmp(p+1, local_realm))
469 } else if (strcmp(type, "USER"))
470 return; /* invalid type */
472 strcpy(buf, "system:");
474 code=pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
476 if (op==0 && code == PRNOENT) return;
477 if (op==1 && code == PRIDEXIST) return;
478 if (strcmp(type, "KERBEROS") || code != PRNOENT) {
479 critical_alert("incremental",
480 "Couldn't %s %s %s %s: %s",
481 op ? "add" : "remove", member,
482 op ? "to" : "from", buf,
483 error_message(code));
489 long pr_try(fn, a1, a2, a3, a4, a5, a6, a7, a8)
491 char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
494 volatile register long code;
495 register int tries = 0;
503 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
505 critical_alert("incremental", "Couldn't initialize libprot: %s",
506 error_message(code));
511 sleep(1); /* give ptserver room */
513 while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)) {
517 if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
518 else if (fn == pr_RemoveUserFromGroup)
519 strcpy(fname, "pr_RemoveUserFromGroup");
520 else if (fn == pr_CreateUser) strcpy(fname, "pr_CreateUser");
521 else if (fn == pr_CreateGroup) strcpy(fname, "pr_CreateGroup");
522 else if (fn == pr_DeleteByID) strcpy(fname, "pr_DeleteByID");
523 else if (fn == pr_ChangeEntry) strcpy(fname, "pr_ChangeEntry");
524 else if (fn == pr_SetFieldsEntry) strcpy(fname, "pr_SetFieldsEntry");
525 else if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
527 sprintf(fname, "pr_??? (0x%08x)", (long)fn);
529 com_err(whoami, code, "- %s failed (try %d @%u)", fname, tries+1, t);
531 if (++tries > 2) break; /* 3 tries */
533 if (code == UNOQUORUM) sleep(90);
536 /* Re-initialize the prdb connection */
537 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
538 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
540 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
541 error_message(code));
542 initd = 0; /* we lost */
554 for (i=0; file_exists(STOP_FILE); i++) {
556 critical_alert("incremental",
557 "AFS incremental failed (%s exists): %s",