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 */
44 int add_list_members();
50 /* libprot.a routines */
51 extern long pr_Initialize();
52 extern long pr_CreateUser();
53 extern long pr_CreateGroup();
54 extern long pr_DeleteByID();
55 extern long pr_ChangeEntry();
56 extern long pr_SetFieldsEntry();
57 extern long pr_AddToGroup();
58 extern long pr_RemoveUserFromGroup();
60 static char tbl_buf[1024];
61 static struct member {
69 static int mr_connections=0;
75 int beforec, afterc, i;
76 char *table, **before, **after;
78 for (i = getdtablesize() - 1; i > 2; i--)
82 beforec = atoi(argv[2]);
84 afterc = atoi(argv[3]);
85 after = &argv[4 + beforec];
90 strcpy(tbl_buf, table);
91 strcat(tbl_buf, " (");
92 for (i = 0; i < beforec; i++) {
95 strcat(tbl_buf, before[i]);
97 strcat(tbl_buf, ")->(");
98 for (i = 0; i < afterc; i++) {
100 strcat(tbl_buf, ",");
101 strcat(tbl_buf, after[i]);
103 strcat(tbl_buf, ")");
105 com_err(whoami, 0, "%s", tbl_buf);
108 initialize_sms_error_table();
109 initialize_krb_error_table();
111 if (!strcmp(table, "users")) {
112 do_user(before, beforec, after, afterc);
113 } else if (!strcmp(table, "list")) {
114 do_list(before, beforec, after, afterc);
115 } else if (!strcmp(table, "members")) {
116 do_member(before, beforec, after, afterc);
117 } else if (!strcmp(table, "filesys")) {
118 do_filesys(before, beforec, after, afterc);
119 } else if (!strcmp(table, "quota")) {
120 do_quota(before, beforec, after, afterc);
126 do_user(before, beforec, after, afterc)
132 int astate, bstate, auid, buid, code;
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 if (astate == bstate) {
154 /* Only a modify has to be done */
155 com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)",
156 before[U_NAME], buid, after[U_NAME], auid);
158 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
160 critical_alert("incremental",
161 "Couldn't change user %s (id %d) to %s (id %d): %s",
162 before[U_NAME], buid, after[U_NAME], auid,
163 error_message(code));
168 com_err(whoami, 0, "Deleting user %s (uid %d)",
169 before[U_NAME], buid);
171 code = pr_try(pr_DeleteByID, buid);
172 if (code && code != PRNOENT) {
173 critical_alert("incremental",
174 "Couldn't delete user %s (id %d): %s",
175 before[U_NAME], buid, error_message(code));
180 com_err(whoami, 0, "%s user %s (uid %d)",
181 ((bstate != 0) ? "Reactivating" : "Creating"),
182 after[U_NAME], auid);
184 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
186 critical_alert("incremental",
187 "Couldn't create user %s (id %d): %s",
188 after[U_NAME], auid, error_message(code));
193 /* Reactivating a user; get his group list */
194 code = moira_connect();
196 critical_alert("incremental",
197 "Error contacting Moira server to retrieve grouplist of user %s: %s",
198 after[U_NAME], error_message(code));
202 av[1] = after[U_NAME];
203 code = mr_query("get_lists_of_member", 2, av,
204 add_user_lists, after[U_NAME]);
205 if (code && code != MR_NO_MATCH)
206 critical_alert("incremental",
207 "Couldn't retrieve membership of user %s: %s",
208 after[U_NAME], error_message(code));
216 do_list(before, beforec, after, afterc)
222 register int agid, bgid;
225 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
229 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
230 bgid = atoi(before[L_GID]);
231 bhide = atoi(before[L_HIDDEN]);
233 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
234 agid = atoi(after[L_GID]);
235 ahide = atoi(after[L_HIDDEN]);
238 if (agid == 0 && bgid == 0) /* Not active groups */
242 if (strcmp(after[L_NAME], before[L_NAME])) {
243 /* Only a modify is required */
244 strcpy(g1, "system:");
245 strcpy(g2, "system:");
246 strcat(g1, before[L_NAME]);
247 strcat(g2, after[L_NAME]);
249 com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)",
250 before[L_NAME], bgid, after[L_NAME], agid);
252 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
254 critical_alert("incremental",
255 "Couldn't change group %s (id %d) to %s (id %d): %s",
256 before[L_NAME], -bgid, after[L_NAME], -agid,
257 error_message(code));
260 if (ahide != bhide) {
261 com_err(whoami, 0, "Making group %s (gid %d) %s",
263 (ahide ? "hidden" : "visible"));
264 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
265 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
266 0 /*ngroups*/, 0 /*nusers*/);
268 critical_alert("incremental",
269 "Couldn't set flags of group %s: %s",
270 after[L_NAME], error_message(code));
276 com_err(whoami, 0, "Deleting group %s (gid %d)",
277 before[L_NAME], bgid);
278 code = pr_try(pr_DeleteByID, -bgid);
279 if (code && code != PRNOENT) {
280 critical_alert("incremental",
281 "Couldn't delete group %s (id %d): %s",
282 before[L_NAME], -bgid, error_message(code));
287 strcpy(g1, "system:");
288 strcat(g1, after[L_NAME]);
289 strcpy(g2, "system:administrators");
291 com_err(whoami, 0, "Creating %s group %s (gid %d)",
292 (ahide ? "hidden" : "visible"), after[L_NAME], agid);
293 code = pr_try(pr_CreateGroup, g1, g2, &id);
295 critical_alert("incremental",
296 "Couldn't create group %s (id %d): %s",
297 after[L_NAME], id, error_message(code));
301 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
302 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
303 0 /*ngroups*/, 0 /*nusers*/);
305 critical_alert("incremental",
306 "Couldn't set flags of group %s: %s",
307 after[L_NAME], error_message(code));
311 /* We need to make sure the group is properly populated */
312 if (beforec < L_ACTIVE) return;
314 code = moira_connect();
316 critical_alert("incremental",
317 "Error contacting Moira server to resolve %s: %s",
318 after[L_NAME], error_message(code));
321 av[0] = after[L_NAME];
322 code = mr_query("get_end_members_of_list", 1, av,
323 add_list_members, after[L_NAME]);
325 critical_alert("incremental",
326 "Couldn't retrieve full membership of list %s: %s",
327 after[L_NAME], error_message(code));
334 do_member(before, beforec, after, afterc)
343 if ((beforec < 4 || !atoi(before[LM_END])) &&
344 (afterc < 4 || !atoi(after[LM_END])))
348 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
350 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
354 do_filesys(before, beforec, after, afterc)
361 int acreate, atype, btype;
363 if (afterc < FS_CREATE) {
366 atype = !strcmp(after[FS_TYPE], "AFS");
367 acreate = atoi(after[FS_CREATE]);
370 if (beforec < FS_CREATE) {
371 if (acreate == 0 || atype == 0) return;
373 /* new locker creation */
374 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
375 BIN_DIR, BIN_DIR, BIN_DIR,
376 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
377 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
382 btype = !strcmp(before[FS_TYPE], "AFS");
383 if (afterc < FS_CREATE) {
385 critical_alert("incremental",
386 "Cannot delete AFS filesystem %s: Operation not supported",
394 /* Are we dealing with AFS lockers (could be type ERR lockers) */
395 if (!atype && !btype)
396 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
399 /* By now, we know we are simply changing AFS filesystem attributes.
400 * Operations supported:
401 * Name change: rename/remount
402 * Path change: remount
403 * Type change: ERR<-->AFS
407 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
408 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
410 critical_alert("incremental",
411 "Cannot change ownership of filesystem %s: Operation not yet supported",
416 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
417 BIN_DIR, BIN_DIR, BIN_DIR,
418 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
419 before[FS_L_TYPE], before[FS_PACK],
420 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
421 after[FS_L_TYPE], after[FS_PACK]);
426 do_quota(before, beforec, after, afterc)
434 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
435 strncmp("/afs/", after[Q_DIRECTORY], 5))
438 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
439 BIN_DIR, BIN_DIR, BIN_DIR,
440 after[Q_DIRECTORY], after[Q_QUOTA]);
449 int success=0, tries=0;
453 while (success == 0 && tries < 2) {
456 com_err(whoami, 0, "Executing command: %s", cmd);
457 if (system(cmd) == 0)
461 critical_alert("incremental", "failed command: %s", cmd);
465 add_user_lists(ac, av, user)
470 if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */
471 edit_group(1, av[L_NAME], "USER", user);
475 add_list_members(ac, av, group)
480 edit_group(1, group, av[0], av[1]);
484 check_user(ac, av, ustate)
489 *ustate = atoi(av[U_STATE]);
493 edit_group(op, group, type, member)
500 char buf[PR_MAXNAMELEN];
502 static char local_realm[REALM_SZ+1] = "";
505 /* The following KERBEROS code allows for the use of entities
509 krb_get_lrealm(local_realm, 1);
510 if (!strcmp(type, "KERBEROS")) {
511 p = index(member, '@');
512 if (p && !strcasecmp(p+1, local_realm))
514 } else if (strcmp(type, "USER"))
515 return; /* invalid type */
517 /* Cannot risk doing another query during a callback */
518 /* We could do this simply for type USER, but eventually this may also
519 * dynamically add KERBEROS types to the prdb, and we will need to do
520 * a query to look up the uid of the null-instance user */
521 if (mr_connections) {
522 m = (struct member *)malloc(sizeof(struct member));
524 critical_alert("incremental", "Out of memory");
528 strcpy(m->list, group);
529 strcpy(m->type, type);
530 strcpy(m->member, member);
531 m->next = member_head;
536 strcpy(buf, "system:");
538 com_err(whoami, 0, "%s %s %s group %s",
539 (op ? "Adding" : "Removing"), member,
540 (op ? "to" : "from"), group);
541 code=pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
543 if (op==1 && code == PRIDEXIST) return; /* Already added */
545 if (code == PRNOENT) { /* Something is missing */
546 if (op==0) return; /* Already deleted */
547 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
550 /* Check whether the member being added is an active user */
551 code = moira_connect();
552 if (!code) code = mr_query("get_user_by_login", 1, &member,
553 check_user, &ustate);
555 critical_alert("incremental",
556 "Error contacting Moira server to lookup user %s: %s",
557 member, error_message(code));
560 /* We don't use moira_disconnect()
561 * because we may already be in the routine.
566 if (!code && ustate!=1 && ustate!=2) return; /* inactive user */
570 critical_alert("incremental",
571 "Couldn't %s %s %s %s: %s",
572 op ? "add" : "remove", member,
573 op ? "to" : "from", buf,
574 error_message(code));
579 long pr_try(fn, a1, a2, a3, a4, a5, a6, a7, a8)
581 char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
584 volatile register long code;
585 register int tries = 0;
593 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
598 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
600 critical_alert("incremental", "Couldn't initialize libprot: %s",
601 error_message(code));
605 sleep(1); /* give ptserver room */
607 while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)) {
611 if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
612 else if (fn == pr_RemoveUserFromGroup)
613 strcpy(fname, "pr_RemoveUserFromGroup");
614 else if (fn == pr_CreateUser) strcpy(fname, "pr_CreateUser");
615 else if (fn == pr_CreateGroup) strcpy(fname, "pr_CreateGroup");
616 else if (fn == pr_DeleteByID) strcpy(fname, "pr_DeleteByID");
617 else if (fn == pr_ChangeEntry) strcpy(fname, "pr_ChangeEntry");
618 else if (fn == pr_SetFieldsEntry) strcpy(fname, "pr_SetFieldsEntry");
619 else if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
621 sprintf(fname, "pr_??? (0x%08x)", (long)fn);
623 com_err(whoami, code, "- %s failed (try %d @%u)", fname, tries+1, t);
625 if (++tries > 2) break; /* 3 tries */
627 if (code == UNOQUORUM) sleep(90);
630 /* Re-initialize the prdb connection */
631 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
632 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
634 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
635 error_message(code));
636 initd = 0; /* we lost */
648 for (i=0; file_exists(STOP_FILE); i++) {
650 critical_alert("incremental",
651 "AFS incremental failed (%s exists): %s",
662 static char hostname[64];
665 if (!mr_connections++) {
666 gethostname(hostname, sizeof(hostname));
667 code = mr_connect(hostname);
668 if (!code) code = mr_auth("afs.incr");
678 if (!--mr_connections) {
680 while(m = member_head) {
681 edit_group(m->op, m->list, m->type, m->member);
682 member_head = m->next;