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>
18 #include <moira_site.h>
20 #include <afs/param.h>
21 #include <afs/cellconfig.h>
22 #include <afs/venus.h>
23 #include <afs/ptclient.h>
24 #include <afs/pterror.h>
26 #define STOP_FILE "/moira/afs/noafs"
27 #define file_exists(file) (access((file), F_OK) == 0)
31 /* Main stub routines */
38 /* Support stub routines */
41 int add_list_members();
47 /* libprot.a routines */
48 extern long pr_Initialize();
49 extern long pr_CreateUser();
50 extern long pr_CreateGroup();
51 extern long pr_DeleteByID();
52 extern long pr_ChangeEntry();
53 extern long pr_SetFieldsEntry();
54 extern long pr_AddToGroup();
55 extern long pr_RemoveUserFromGroup();
57 static char tbl_buf[1024];
58 static struct member {
66 static int mr_connections=0;
72 int beforec, afterc, i;
73 char *table, **before, **after;
75 for (i = getdtablesize() - 1; i > 2; i--)
78 whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]);
81 beforec = atoi(argv[2]);
83 afterc = atoi(argv[3]);
84 after = &argv[4 + beforec];
86 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
88 strcpy(tbl_buf, table);
89 strcat(tbl_buf, " (");
90 for (i = 0; i < beforec; i++) {
93 strcat(tbl_buf, before[i]);
95 strcat(tbl_buf, ")->(");
96 for (i = 0; i < afterc; i++) {
99 strcat(tbl_buf, after[i]);
101 strcat(tbl_buf, ")");
103 com_err(whoami, 0, "%s", tbl_buf);
106 initialize_sms_error_table();
107 initialize_krb_error_table();
109 if (!strcmp(table, "users")) {
110 do_user(before, beforec, after, afterc);
111 } else if (!strcmp(table, "list")) {
112 do_list(before, beforec, after, afterc);
113 } else if (!strcmp(table, "imembers")) {
114 do_member(before, beforec, after, afterc);
115 } else if (!strcmp(table, "filesys")) {
116 do_filesys(before, beforec, after, afterc);
117 } else if (!strcmp(table, "quota")) {
118 do_quota(before, beforec, after, afterc);
124 do_user(before, beforec, after, afterc)
130 int astate, bstate, auid, buid, code;
133 auid = buid = astate = bstate = 0;
134 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
135 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
136 if (afterc > U_UID) auid = atoi(after[U_UID]);
137 if (beforec > U_UID) buid = atoi(before[U_UID]);
139 /* We consider "half-registered" users to be active */
140 if (astate == 2) astate = 1;
141 if (bstate == 2) bstate = 1;
143 if (astate != 1 && bstate != 1) /* inactive user */
146 if (astate == bstate && auid == buid &&
147 !strcmp(before[U_NAME], after[U_NAME]))
148 /* No AFS related attributes have changed */
151 if (astate == bstate) {
152 /* Only a modify has to be done */
153 com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)",
154 before[U_NAME], buid, after[U_NAME], auid);
156 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
158 critical_alert("incremental",
159 "Couldn't change user %s (id %d) to %s (id %d): %s",
160 before[U_NAME], buid, after[U_NAME], auid,
161 error_message(code));
166 com_err(whoami, 0, "Deleting user %s (uid %d)",
167 before[U_NAME], buid);
169 code = pr_try(pr_DeleteByID, buid);
170 if (code && code != PRNOENT) {
171 critical_alert("incremental",
172 "Couldn't delete user %s (id %d): %s",
173 before[U_NAME], buid, error_message(code));
178 com_err(whoami, 0, "%s user %s (uid %d)",
179 ((bstate != 0) ? "Reactivating" : "Creating"),
180 after[U_NAME], auid);
182 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
184 critical_alert("incremental",
185 "Couldn't create user %s (id %d): %s",
186 after[U_NAME], auid, error_message(code));
191 /* Reactivating a user; get his group list */
192 code = moira_connect();
194 critical_alert("incremental",
195 "Error contacting Moira server to retrieve grouplist of user %s: %s",
196 after[U_NAME], error_message(code));
200 av[1] = after[U_NAME];
201 code = mr_query("get_lists_of_member", 2, av,
202 add_user_lists, after[U_NAME]);
203 if (code && code != MR_NO_MATCH)
204 critical_alert("incremental",
205 "Couldn't retrieve membership of user %s: %s",
206 after[U_NAME], error_message(code));
214 do_list(before, beforec, after, afterc)
220 register int agid, bgid;
223 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
227 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
228 bgid = atoi(before[L_GID]);
229 bhide = atoi(before[L_HIDDEN]);
231 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
232 agid = atoi(after[L_GID]);
233 ahide = atoi(after[L_HIDDEN]);
236 if (agid == 0 && bgid == 0) /* Not active groups */
240 if (strcmp(after[L_NAME], before[L_NAME])) {
241 /* Only a modify is required */
242 strcpy(g1, "system:");
243 strcpy(g2, "system:");
244 strcat(g1, before[L_NAME]);
245 strcat(g2, after[L_NAME]);
247 com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)",
248 before[L_NAME], bgid, after[L_NAME], agid);
250 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
252 critical_alert("incremental",
253 "Couldn't change group %s (id %d) to %s (id %d): %s",
254 before[L_NAME], -bgid, after[L_NAME], -agid,
255 error_message(code));
258 if (ahide != bhide) {
259 com_err(whoami, 0, "Making group %s (gid %d) %s",
261 (ahide ? "hidden" : "visible"));
262 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
263 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
264 0 /*ngroups*/, 0 /*nusers*/);
266 critical_alert("incremental",
267 "Couldn't set flags of group %s: %s",
268 after[L_NAME], error_message(code));
274 com_err(whoami, 0, "Deleting group %s (gid %d)",
275 before[L_NAME], bgid);
276 code = pr_try(pr_DeleteByID, -bgid);
277 if (code && code != PRNOENT) {
278 critical_alert("incremental",
279 "Couldn't delete group %s (id %d): %s",
280 before[L_NAME], -bgid, error_message(code));
285 strcpy(g1, "system:");
286 strcat(g1, after[L_NAME]);
287 strcpy(g2, "system:administrators");
289 com_err(whoami, 0, "Creating %s group %s (gid %d)",
290 (ahide ? "hidden" : "visible"), after[L_NAME], agid);
291 code = pr_try(pr_CreateGroup, g1, g2, &id);
293 critical_alert("incremental",
294 "Couldn't create group %s (id %d): %s",
295 after[L_NAME], id, error_message(code));
299 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
300 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
301 0 /*ngroups*/, 0 /*nusers*/);
303 critical_alert("incremental",
304 "Couldn't set flags of group %s: %s",
305 after[L_NAME], error_message(code));
309 /* We need to make sure the group is properly populated */
310 if (beforec < L_ACTIVE) return;
312 code = moira_connect();
314 critical_alert("incremental",
315 "Error contacting Moira server to resolve %s: %s",
316 after[L_NAME], error_message(code));
319 av[0] = after[L_NAME];
320 code = mr_query("get_end_members_of_list", 1, av,
321 add_list_members, after[L_NAME]);
323 critical_alert("incremental",
324 "Couldn't retrieve full membership of list %s: %s",
325 after[L_NAME], error_message(code));
333 #define LM_EXTRA_ACTIVE (LM_END)
334 #define LM_EXTRA_PUBLIC (LM_END+1)
335 #define LM_EXTRA_HIDDEN (LM_END+2)
336 #define LM_EXTRA_MAILLIST (LM_END+3)
337 #define LM_EXTRA_GROUP (LM_END+4)
338 #define LM_EXTRA_GID (LM_END+5)
339 #define LM_EXTRA_END (LM_END+6)
341 do_member(before, beforec, after, afterc)
351 if (afterc < LM_EXTRA_END) {
354 if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP]))
357 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
358 } else if (beforec) {
359 if (beforec < LM_EXTRA_END) {
362 if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP]))
364 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
369 do_filesys(before, beforec, after, afterc)
376 int acreate, atype, bcreate, btype;
378 if (afterc < FS_CREATE) {
381 atype = !strcmp(after[FS_TYPE], "AFS");
382 acreate = atoi(after[FS_CREATE]);
385 if (beforec < FS_CREATE) {
386 if (acreate == 0 || atype == 0) return;
388 /* new locker creation */
389 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
390 BIN_DIR, BIN_DIR, BIN_DIR,
391 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
392 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
397 btype = !strcmp(before[FS_TYPE], "AFS");
398 bcreate = atoi(before[FS_CREATE]);
399 if (afterc < FS_CREATE) {
400 if (btype && bcreate)
401 critical_alert("incremental",
402 "Cannot delete AFS filesystem %s: Operation not supported",
410 /* Are we dealing with AFS lockers (could be type ERR lockers) */
411 if (!atype && !btype)
412 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
415 /* By now, we know we are simply changing AFS filesystem attributes.
416 * Operations supported:
417 * Name change: rename/remount
418 * Path change: remount
419 * Type change: ERR<-->AFS
423 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
424 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
426 critical_alert("incremental",
427 "Cannot change ownership of filesystem %s: Operation not yet supported",
432 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
433 BIN_DIR, BIN_DIR, BIN_DIR,
434 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
435 before[FS_L_TYPE], before[FS_PACK],
436 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
437 after[FS_L_TYPE], after[FS_PACK]);
442 do_quota(before, beforec, after, afterc)
450 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
451 strncmp("/afs/", after[Q_DIRECTORY], 5))
454 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
455 BIN_DIR, BIN_DIR, BIN_DIR,
456 after[Q_DIRECTORY], after[Q_QUOTA]);
465 int success=0, tries=0;
469 while (success == 0 && tries < 2) {
472 com_err(whoami, 0, "Executing command: %s", cmd);
473 if (system(cmd) == 0)
477 critical_alert("incremental", "failed command: %s", cmd);
481 add_user_lists(ac, av, user)
486 if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */
487 edit_group(1, av[L_NAME], "USER", user);
492 add_list_members(ac, av, group)
497 edit_group(1, group, av[0], av[1]);
502 check_user(ac, av, ustate)
507 *ustate = atoi(av[U_STATE]);
512 edit_group(op, group, type, member)
519 char buf[PR_MAXNAMELEN];
521 static char local_realm[REALM_SZ+1] = "";
524 /* The following KERBEROS code allows for the use of entities
528 krb_get_lrealm(local_realm, 1);
529 if (!strcmp(type, "KERBEROS")) {
530 p = strchr(member, '@');
531 if (p && !strcasecmp(p+1, local_realm))
533 } else if (strcmp(type, "USER"))
534 return; /* invalid type */
536 /* Cannot risk doing another query during a callback */
537 /* We could do this simply for type USER, but eventually this may also
538 * dynamically add KERBEROS types to the prdb, and we will need to do
539 * a query to look up the uid of the null-instance user */
540 if (mr_connections) {
541 m = (struct member *)malloc(sizeof(struct member));
543 critical_alert("incremental", "Out of memory");
547 strcpy(m->list, group);
548 strcpy(m->type, type);
549 strcpy(m->member, member);
550 m->next = member_head;
555 strcpy(buf, "system:");
557 com_err(whoami, 0, "%s %s %s group %s",
558 (op ? "Adding" : "Removing"), member,
559 (op ? "to" : "from"), group);
561 code=pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
563 if (op==1 && code == PRIDEXIST) return; /* Already added */
565 if (code == PRNOENT) { /* Something is missing */
566 if (op==0) return; /* Already deleted */
567 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
570 /* Check whether the member being added is an active user */
571 code = moira_connect();
572 if (!code) code = mr_query("get_user_by_login", 1, &member,
573 check_user, &ustate);
575 critical_alert("incremental",
576 "Error contacting Moira server to lookup user %s: %s",
577 member, error_message(code));
580 /* We don't use moira_disconnect()
581 * because we may already be in the routine.
586 if (!code && ustate!=1 && ustate!=2) return; /* inactive user */
590 critical_alert("incremental",
591 "Couldn't %s %s %s %s: %s",
592 op ? "add" : "remove", member,
593 op ? "to" : "from", buf,
594 error_message(code));
599 long pr_try(fn, a1, a2, a3, a4, a5, a6, a7, a8)
601 char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
605 register int tries = 0;
613 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
618 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
620 critical_alert("incremental", "Couldn't initialize libprot: %s",
621 error_message(code));
625 sleep(1); /* give ptserver room */
627 while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)) {
631 if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
632 else if (fn == pr_RemoveUserFromGroup)
633 strcpy(fname, "pr_RemoveUserFromGroup");
634 else if (fn == pr_CreateUser) strcpy(fname, "pr_CreateUser");
635 else if (fn == pr_CreateGroup) strcpy(fname, "pr_CreateGroup");
636 else if (fn == pr_DeleteByID) strcpy(fname, "pr_DeleteByID");
637 else if (fn == pr_ChangeEntry) strcpy(fname, "pr_ChangeEntry");
638 else if (fn == pr_SetFieldsEntry) strcpy(fname, "pr_SetFieldsEntry");
639 else if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
641 sprintf(fname, "pr_??? (0x%08x)", (long)fn);
643 com_err(whoami, code, "- %s failed (try %d @%u)", fname, tries+1, t);
645 if (++tries > 2) break; /* 3 tries */
647 if (code == UNOQUORUM) sleep(90);
650 /* Re-initialize the prdb connection */
651 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
652 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
654 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
655 error_message(code));
656 initd = 0; /* we lost */
668 for (i=0; file_exists(STOP_FILE); i++) {
670 critical_alert("incremental",
671 "AFS incremental failed (%s exists): %s",
682 static char hostname[64];
685 if (!mr_connections++) {
686 gethostname(hostname, sizeof(hostname));
687 code = mr_connect(hostname);
688 if (!code) code = mr_auth("afs.incr");
698 if (!--mr_connections) {
700 while(m = member_head) {
701 edit_group(m->op, m->list, m->type, m->member);
702 member_head = m->next;