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--)
81 whoami = ((whoami = rindex(argv[0], '/')) ? whoami++ : argv[0]);
84 beforec = atoi(argv[2]);
86 afterc = atoi(argv[3]);
87 after = &argv[4 + beforec];
91 strcpy(tbl_buf, table);
92 strcat(tbl_buf, " (");
93 for (i = 0; i < beforec; i++) {
96 strcat(tbl_buf, before[i]);
98 strcat(tbl_buf, ")->(");
99 for (i = 0; i < afterc; i++) {
101 strcat(tbl_buf, ",");
102 strcat(tbl_buf, after[i]);
104 strcat(tbl_buf, ")");
106 com_err(whoami, 0, "%s", tbl_buf);
109 initialize_sms_error_table();
110 initialize_krb_error_table();
112 if (!strcmp(table, "users")) {
113 do_user(before, beforec, after, afterc);
114 } else if (!strcmp(table, "list")) {
115 do_list(before, beforec, after, afterc);
116 } else if (!strcmp(table, "members")) {
117 do_member(before, beforec, after, afterc);
118 } else if (!strcmp(table, "filesys")) {
119 do_filesys(before, beforec, after, afterc);
120 } else if (!strcmp(table, "quota")) {
121 do_quota(before, beforec, after, afterc);
127 do_user(before, beforec, after, afterc)
133 int astate, bstate, auid, buid, code;
136 auid = buid = astate = bstate = 0;
137 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
138 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
139 if (afterc > U_UID) auid = atoi(after[U_UID]);
140 if (beforec > U_UID) buid = atoi(before[U_UID]);
142 /* We consider "half-registered" users to be active */
143 if (astate == 2) astate = 1;
144 if (bstate == 2) bstate = 1;
146 if (astate != 1 && bstate != 1) /* inactive user */
149 if (astate == bstate && auid == buid &&
150 !strcmp(before[U_NAME], after[U_NAME]))
151 /* No AFS related attributes have changed */
154 if (astate == bstate) {
155 /* Only a modify has to be done */
156 com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)",
157 before[U_NAME], buid, after[U_NAME], auid);
159 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
161 critical_alert("incremental",
162 "Couldn't change user %s (id %d) to %s (id %d): %s",
163 before[U_NAME], buid, after[U_NAME], auid,
164 error_message(code));
169 com_err(whoami, 0, "Deleting user %s (uid %d)",
170 before[U_NAME], buid);
172 code = pr_try(pr_DeleteByID, buid);
173 if (code && code != PRNOENT) {
174 critical_alert("incremental",
175 "Couldn't delete user %s (id %d): %s",
176 before[U_NAME], buid, error_message(code));
181 com_err(whoami, 0, "%s user %s (uid %d)",
182 ((bstate != 0) ? "Reactivating" : "Creating"),
183 after[U_NAME], auid);
185 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
187 critical_alert("incremental",
188 "Couldn't create user %s (id %d): %s",
189 after[U_NAME], auid, error_message(code));
194 /* Reactivating a user; get his group list */
195 code = moira_connect();
197 critical_alert("incremental",
198 "Error contacting Moira server to retrieve grouplist of user %s: %s",
199 after[U_NAME], error_message(code));
203 av[1] = after[U_NAME];
204 code = mr_query("get_lists_of_member", 2, av,
205 add_user_lists, after[U_NAME]);
206 if (code && code != MR_NO_MATCH)
207 critical_alert("incremental",
208 "Couldn't retrieve membership of user %s: %s",
209 after[U_NAME], error_message(code));
217 do_list(before, beforec, after, afterc)
223 register int agid, bgid;
226 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
230 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
231 bgid = atoi(before[L_GID]);
232 bhide = atoi(before[L_HIDDEN]);
234 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
235 agid = atoi(after[L_GID]);
236 ahide = atoi(after[L_HIDDEN]);
239 if (agid == 0 && bgid == 0) /* Not active groups */
243 if (strcmp(after[L_NAME], before[L_NAME])) {
244 /* Only a modify is required */
245 strcpy(g1, "system:");
246 strcpy(g2, "system:");
247 strcat(g1, before[L_NAME]);
248 strcat(g2, after[L_NAME]);
250 com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)",
251 before[L_NAME], bgid, after[L_NAME], agid);
253 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
255 critical_alert("incremental",
256 "Couldn't change group %s (id %d) to %s (id %d): %s",
257 before[L_NAME], -bgid, after[L_NAME], -agid,
258 error_message(code));
261 if (ahide != bhide) {
262 com_err(whoami, 0, "Making group %s (gid %d) %s",
264 (ahide ? "hidden" : "visible"));
265 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
266 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
267 0 /*ngroups*/, 0 /*nusers*/);
269 critical_alert("incremental",
270 "Couldn't set flags of group %s: %s",
271 after[L_NAME], error_message(code));
277 com_err(whoami, 0, "Deleting group %s (gid %d)",
278 before[L_NAME], bgid);
279 code = pr_try(pr_DeleteByID, -bgid);
280 if (code && code != PRNOENT) {
281 critical_alert("incremental",
282 "Couldn't delete group %s (id %d): %s",
283 before[L_NAME], -bgid, error_message(code));
288 strcpy(g1, "system:");
289 strcat(g1, after[L_NAME]);
290 strcpy(g2, "system:administrators");
292 com_err(whoami, 0, "Creating %s group %s (gid %d)",
293 (ahide ? "hidden" : "visible"), after[L_NAME], agid);
294 code = pr_try(pr_CreateGroup, g1, g2, &id);
296 critical_alert("incremental",
297 "Couldn't create group %s (id %d): %s",
298 after[L_NAME], id, error_message(code));
302 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
303 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
304 0 /*ngroups*/, 0 /*nusers*/);
306 critical_alert("incremental",
307 "Couldn't set flags of group %s: %s",
308 after[L_NAME], error_message(code));
312 /* We need to make sure the group is properly populated */
313 if (beforec < L_ACTIVE) return;
315 code = moira_connect();
317 critical_alert("incremental",
318 "Error contacting Moira server to resolve %s: %s",
319 after[L_NAME], error_message(code));
322 av[0] = after[L_NAME];
323 code = mr_query("get_end_members_of_list", 1, av,
324 add_list_members, after[L_NAME]);
326 critical_alert("incremental",
327 "Couldn't retrieve full membership of list %s: %s",
328 after[L_NAME], error_message(code));
336 #define LM_EXTRA_ACTIVE (LM_END)
337 #define LM_EXTRA_PUBLIC (LM_END+1)
338 #define LM_EXTRA_HIDDEN (LM_END+2)
339 #define LM_EXTRA_MAILLIST (LM_END+3)
340 #define LM_EXTRA_GROUP (LM_END+4)
341 #define LM_EXTRA_GID (LM_END+5)
342 #define LM_EXTRA_END (LM_END+6)
344 #define LM_EXTRA_OLD_GROUP (LM_END)
345 #define LM_EXTRA_OLD_END (LM_END+1)
347 do_member(before, beforec, after, afterc)
357 if (afterc < LM_EXTRA_OLD_END) {
359 } else if (afterc < LM_EXTRA_END) {
360 if (!atoi(after[LM_EXTRA_OLD_GROUP])) return;
362 if (!atoi(after[LM_EXTRA_ACTIVE]) && !atoi(after[LM_EXTRA_GROUP]))
365 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
366 } else if (beforec) {
367 if (beforec < LM_EXTRA_OLD_END) {
369 } else if (beforec < LM_EXTRA_END) {
370 if (!atoi(before[LM_EXTRA_OLD_GROUP])) return;
372 if (!atoi(before[LM_EXTRA_ACTIVE]) && !atoi(before[LM_EXTRA_GROUP]))
374 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
379 do_filesys(before, beforec, after, afterc)
386 int acreate, atype, btype;
388 if (afterc < FS_CREATE) {
391 atype = !strcmp(after[FS_TYPE], "AFS");
392 acreate = atoi(after[FS_CREATE]);
395 if (beforec < FS_CREATE) {
396 if (acreate == 0 || atype == 0) return;
398 /* new locker creation */
399 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
400 BIN_DIR, BIN_DIR, BIN_DIR,
401 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
402 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
407 btype = !strcmp(before[FS_TYPE], "AFS");
408 if (afterc < FS_CREATE) {
410 critical_alert("incremental",
411 "Cannot delete AFS filesystem %s: Operation not supported",
419 /* Are we dealing with AFS lockers (could be type ERR lockers) */
420 if (!atype && !btype)
421 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
424 /* By now, we know we are simply changing AFS filesystem attributes.
425 * Operations supported:
426 * Name change: rename/remount
427 * Path change: remount
428 * Type change: ERR<-->AFS
432 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
433 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
435 critical_alert("incremental",
436 "Cannot change ownership of filesystem %s: Operation not yet supported",
441 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
442 BIN_DIR, BIN_DIR, BIN_DIR,
443 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
444 before[FS_L_TYPE], before[FS_PACK],
445 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
446 after[FS_L_TYPE], after[FS_PACK]);
451 do_quota(before, beforec, after, afterc)
459 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
460 strncmp("/afs/", after[Q_DIRECTORY], 5))
463 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
464 BIN_DIR, BIN_DIR, BIN_DIR,
465 after[Q_DIRECTORY], after[Q_QUOTA]);
474 int success=0, tries=0;
478 while (success == 0 && tries < 2) {
481 com_err(whoami, 0, "Executing command: %s", cmd);
482 if (system(cmd) == 0)
486 critical_alert("incremental", "failed command: %s", cmd);
490 add_user_lists(ac, av, user)
495 if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */
496 edit_group(1, av[L_NAME], "USER", user);
501 add_list_members(ac, av, group)
506 edit_group(1, group, av[0], av[1]);
511 check_user(ac, av, ustate)
516 *ustate = atoi(av[U_STATE]);
521 edit_group(op, group, type, member)
528 char buf[PR_MAXNAMELEN];
530 static char local_realm[REALM_SZ+1] = "";
533 /* The following KERBEROS code allows for the use of entities
537 krb_get_lrealm(local_realm, 1);
538 if (!strcmp(type, "KERBEROS")) {
539 p = index(member, '@');
540 if (p && !strcasecmp(p+1, local_realm))
542 } else if (strcmp(type, "USER"))
543 return; /* invalid type */
545 /* Cannot risk doing another query during a callback */
546 /* We could do this simply for type USER, but eventually this may also
547 * dynamically add KERBEROS types to the prdb, and we will need to do
548 * a query to look up the uid of the null-instance user */
549 if (mr_connections) {
550 m = (struct member *)malloc(sizeof(struct member));
552 critical_alert("incremental", "Out of memory");
556 strcpy(m->list, group);
557 strcpy(m->type, type);
558 strcpy(m->member, member);
559 m->next = member_head;
564 strcpy(buf, "system:");
566 com_err(whoami, 0, "%s %s %s group %s",
567 (op ? "Adding" : "Removing"), member,
568 (op ? "to" : "from"), group);
570 code=pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
572 if (op==1 && code == PRIDEXIST) return; /* Already added */
574 if (code == PRNOENT) { /* Something is missing */
575 if (op==0) return; /* Already deleted */
576 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
579 /* Check whether the member being added is an active user */
580 code = moira_connect();
581 if (!code) code = mr_query("get_user_by_login", 1, &member,
582 check_user, &ustate);
584 critical_alert("incremental",
585 "Error contacting Moira server to lookup user %s: %s",
586 member, error_message(code));
589 /* We don't use moira_disconnect()
590 * because we may already be in the routine.
595 if (!code && ustate!=1 && ustate!=2) return; /* inactive user */
599 critical_alert("incremental",
600 "Couldn't %s %s %s %s: %s",
601 op ? "add" : "remove", member,
602 op ? "to" : "from", buf,
603 error_message(code));
608 long pr_try(fn, a1, a2, a3, a4, a5, a6, a7, a8)
610 char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
613 volatile register long code;
614 register int tries = 0;
622 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
627 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
629 critical_alert("incremental", "Couldn't initialize libprot: %s",
630 error_message(code));
634 sleep(1); /* give ptserver room */
636 while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)) {
640 if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
641 else if (fn == pr_RemoveUserFromGroup)
642 strcpy(fname, "pr_RemoveUserFromGroup");
643 else if (fn == pr_CreateUser) strcpy(fname, "pr_CreateUser");
644 else if (fn == pr_CreateGroup) strcpy(fname, "pr_CreateGroup");
645 else if (fn == pr_DeleteByID) strcpy(fname, "pr_DeleteByID");
646 else if (fn == pr_ChangeEntry) strcpy(fname, "pr_ChangeEntry");
647 else if (fn == pr_SetFieldsEntry) strcpy(fname, "pr_SetFieldsEntry");
648 else if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
650 sprintf(fname, "pr_??? (0x%08x)", (long)fn);
652 com_err(whoami, code, "- %s failed (try %d @%u)", fname, tries+1, t);
654 if (++tries > 2) break; /* 3 tries */
656 if (code == UNOQUORUM) sleep(90);
659 /* Re-initialize the prdb connection */
660 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
661 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
663 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
664 error_message(code));
665 initd = 0; /* we lost */
677 for (i=0; file_exists(STOP_FILE); i++) {
679 critical_alert("incremental",
680 "AFS incremental failed (%s exists): %s",
691 static char hostname[64];
694 if (!mr_connections++) {
696 code = mr_connect("moira");
698 gethostname(hostname, sizeof(hostname));
699 code = mr_connect(hostname);
701 if (!code) code = mr_auth("afs.incr");
711 if (!--mr_connections) {
713 while(m = member_head) {
714 edit_group(m->op, m->list, m->type, m->member);
715 member_head = m->next;