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
12 #include <sys/types.h>
19 #include <moira_site.h>
21 #include <afs/param.h>
22 #include <afs/cellconfig.h>
23 #include <afs/venus.h>
24 #include <afs/ptclient.h>
25 #include <afs/pterror.h>
27 #define STOP_FILE "/moira/afs/noafs"
28 #define file_exists(file) (access((file), F_OK) == 0)
32 /* Main stub routines */
39 /* Support stub routines */
42 int add_list_members();
48 /* libprot.a routines */
49 extern long pr_Initialize();
50 extern long pr_CreateUser();
51 extern long pr_CreateGroup();
52 extern long pr_DeleteByID();
53 extern long pr_ChangeEntry();
54 extern long pr_SetFieldsEntry();
55 extern long pr_AddToGroup();
56 extern long pr_RemoveUserFromGroup();
57 extern long pr_SIdToName();
59 static char tbl_buf[1024];
60 static struct member {
66 } *member_head = NULL;
68 static int mr_connections = 0;
70 int main(int argc, char **argv)
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++)
94 strcat(tbl_buf, before[i]);
96 strcat(tbl_buf, ")->(");
97 for (i = 0; i < afterc; i++)
100 strcat(tbl_buf, ",");
101 strcat(tbl_buf, after[i]);
103 strcat(tbl_buf, ")");
105 initialize_sms_error_table();
106 initialize_krb_error_table();
108 if (!strcmp(table, "users"))
109 do_user(before, beforec, after, afterc);
110 else if (!strcmp(table, "list"))
111 do_list(before, beforec, after, afterc);
112 else if (!strcmp(table, "imembers"))
113 do_member(before, beforec, after, afterc);
114 else if (!strcmp(table, "filesys"))
115 do_filesys(before, beforec, after, afterc);
116 else if (!strcmp(table, "quota"))
117 do_quota(before, beforec, after, afterc);
123 do_user(char **before, int beforec, char **after, int afterc)
125 int astate, bstate, auid, buid, code;
128 auid = buid = astate = bstate = 0;
129 if (afterc > U_STATE)
130 astate = atoi(after[U_STATE]);
131 if (beforec > U_STATE)
132 bstate = atoi(before[U_STATE]);
134 auid = atoi(after[U_UID]);
136 buid = atoi(before[U_UID]);
138 /* We consider "half-registered" users to be active */
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 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, "");
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));
170 com_err(whoami, 0, "Deleting user %s (uid %d)",
171 before[U_NAME], buid);
173 code = pr_try(pr_DeleteByID, buid);
174 if (code && code != PRNOENT)
176 critical_alert("incremental", "Couldn't delete user %s (id %d): %s",
177 before[U_NAME], buid, error_message(code));
183 com_err(whoami, 0, "%s user %s (uid %d)",
184 ((bstate != 0) ? "Reactivating" : "Creating"),
185 after[U_NAME], auid);
187 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
188 /* if we get PRIDEXIST, it's only an error if the username
189 doesn't match (otherwise it just means the user was deleted
190 from Moira but not AFS */
191 if (code == PRIDEXIST)
195 if (pr_try(pr_SIdToName, auid, ename) == 0 &&
196 !strcmp(after[U_NAME], ename))
201 critical_alert("incremental", "Couldn't create user %s (id %d): %s",
202 after[U_NAME], auid, error_message(code));
208 /* Reactivating a user; get his group list */
209 code = moira_connect();
212 critical_alert("incremental", "Error contacting Moira server "
213 "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, add_user_lists,
221 if (code && code != MR_NO_MATCH)
223 critical_alert("incremental",
224 "Couldn't retrieve membership of user %s: %s",
225 after[U_NAME], error_message(code));
234 do_list(char **before, int beforec, char **after, int afterc)
239 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
243 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP]))
245 bgid = atoi(before[L_GID]);
246 bhide = atoi(before[L_HIDDEN]);
248 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP]))
250 agid = atoi(after[L_GID]);
251 ahide = atoi(after[L_HIDDEN]);
254 if (agid == 0 && bgid == 0) /* Not active groups */
259 if (strcmp(after[L_NAME], before[L_NAME]))
261 /* Only a modify is required */
262 strcpy(g1, "system:");
263 strcpy(g2, "system:");
264 strcat(g1, before[L_NAME]);
265 strcat(g2, after[L_NAME]);
267 com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)",
268 before[L_NAME], bgid, after[L_NAME], agid);
270 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
273 critical_alert("incremental", "Couldn't change group %s (id %d) "
274 "to %s (id %d): %s", before[L_NAME], -bgid,
275 after[L_NAME], -agid, error_message(code));
280 com_err(whoami, 0, "Making group %s (gid %d) %s", after[L_NAME],
281 agid, (ahide ? "hidden" : "visible"));
282 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
283 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
284 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
287 critical_alert("incremental",
288 "Couldn't set flags of group %s: %s",
289 after[L_NAME], error_message(code));
296 com_err(whoami, 0, "Deleting group %s (gid %d)", before[L_NAME], bgid);
297 code = pr_try(pr_DeleteByID, -bgid);
298 if (code && code != PRNOENT)
300 critical_alert("incremental",
301 "Couldn't delete group %s (id %d): %s",
302 before[L_NAME], -bgid, error_message(code));
308 strcpy(g1, "system:");
309 strcat(g1, after[L_NAME]);
310 strcpy(g2, "system:administrators");
312 com_err(whoami, 0, "Creating %s group %s (gid %d)",
313 (ahide ? "hidden" : "visible"), after[L_NAME], agid);
314 code = pr_try(pr_CreateGroup, g1, g2, &id);
315 if (code == PRIDEXIST)
319 if (pr_try(pr_SIdToName, -agid, ename) == 0 && !strcmp(g1, ename))
324 critical_alert("incremental", "Couldn't create group %s (id %d): %s",
325 after[L_NAME], id, error_message(code));
330 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
331 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
332 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
335 critical_alert("incremental",
336 "Couldn't set flags of group %s: %s",
337 after[L_NAME], error_message(code));
341 /* We need to make sure the group is properly populated */
342 if (beforec < L_ACTIVE)
345 code = moira_connect();
348 critical_alert("incremental",
349 "Error contacting Moira server to resolve %s: %s",
350 after[L_NAME], error_message(code));
353 av[0] = after[L_NAME];
354 code = mr_query("get_end_members_of_list", 1, av,
355 add_list_members, after[L_NAME]);
358 critical_alert("incremental",
359 "Couldn't retrieve full membership of list %s: %s",
360 after[L_NAME], error_message(code));
369 #define LM_EXTRA_ACTIVE (LM_END)
370 #define LM_EXTRA_PUBLIC (LM_END+1)
371 #define LM_EXTRA_HIDDEN (LM_END+2)
372 #define LM_EXTRA_MAILLIST (LM_END+3)
373 #define LM_EXTRA_GROUP (LM_END+4)
374 #define LM_EXTRA_GID (LM_END+5)
375 #define LM_EXTRA_END (LM_END+6)
377 do_member(char **before, int beforec, char **after, int afterc)
384 if (afterc < LM_EXTRA_END)
388 if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP]))
392 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
396 if (beforec < LM_EXTRA_END)
400 if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP]))
403 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
408 do_filesys(char **before, int beforec, char **after, int afterc)
411 int acreate, atype, bcreate, btype;
413 if (afterc < FS_CREATE)
417 atype = !strcmp(after[FS_TYPE], "AFS");
418 acreate = atoi(after[FS_CREATE]);
421 if (beforec < FS_CREATE)
423 if (acreate == 0 || atype == 0)
426 /* new locker creation */
427 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
428 BIN_DIR, BIN_DIR, BIN_DIR,
429 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
430 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
435 btype = !strcmp(before[FS_TYPE], "AFS");
436 bcreate = atoi(before[FS_CREATE]);
437 if (afterc < FS_CREATE)
439 if (btype && bcreate)
440 critical_alert("incremental", "Cannot delete AFS filesystem %s: "
441 "Operation not supported", before[FS_NAME]);
448 /* Are we dealing with AFS lockers (could be type ERR lockers) */
449 if (!atype && !btype)
451 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
455 /* By now, we know we are simply changing AFS filesystem attributes.
456 * Operations supported:
457 * Name change: rename/remount
458 * Path change: remount
459 * Type change: ERR<-->AFS
463 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
464 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
466 critical_alert("incremental",
467 "Cannot change ownership of filesystem %s: Operation not yet supported",
472 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
473 BIN_DIR, BIN_DIR, BIN_DIR,
474 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
475 before[FS_L_TYPE], before[FS_PACK],
476 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
477 after[FS_L_TYPE], after[FS_PACK]);
482 do_quota(char **before, int beforec, char **after, int afterc)
486 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
487 strncmp("/afs/", after[Q_DIRECTORY], 5))
490 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
491 BIN_DIR, BIN_DIR, BIN_DIR,
492 after[Q_DIRECTORY], after[Q_QUOTA]);
500 int success=0, tries=0;
504 while (success == 0 && tries < 2)
508 com_err(whoami, 0, "Executing command: %s", cmd);
509 if (system(cmd) == 0)
513 critical_alert("incremental", "failed command: %s", cmd);
517 int add_user_lists(int ac, char *av[], char *user)
519 if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */
520 edit_group(1, av[L_NAME], "USER", user);
525 int add_list_members(int ac, char *av[], char *group)
527 edit_group(1, group, av[0], av[1]);
531 int check_user(int ac, char *av[], int *ustate)
533 *ustate = atoi(av[U_STATE]);
538 edit_group(int op, char *group, char *type, char *member)
541 char buf[PR_MAXNAMELEN];
543 static char local_realm[REALM_SZ+1] = "";
546 /* The following KERBEROS code allows for the use of entities
550 krb_get_lrealm(local_realm, 1);
551 if (!strcmp(type, "KERBEROS"))
553 p = strchr(member, '@');
554 if (p && !strcasecmp(p+1, local_realm))
557 else if (strcmp(type, "USER"))
558 return; /* invalid type */
560 /* Cannot risk doing another query during a callback */
561 /* We could do this simply for type USER, but eventually this may also
562 * dynamically add KERBEROS types to the prdb, and we will need to do
563 * a query to look up the uid of the null-instance user */
566 m = malloc(sizeof(struct member));
569 critical_alert("incremental", "Out of memory");
573 strcpy(m->list, group);
574 strcpy(m->type, type);
575 strcpy(m->member, member);
576 m->next = member_head;
581 strcpy(buf, "system:");
583 com_err(whoami, 0, "%s %s %s group %s", (op ? "Adding" : "Removing"), member,
584 (op ? "to" : "from"), group);
585 code = pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
588 if (op==1 && code == PRIDEXIST)
589 return; /* Already added */
592 { /* Something is missing */
594 return; /* Already deleted */
595 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
598 /* Check whether the member being added is an active user */
599 code = moira_connect();
602 code = mr_query("get_user_by_login", 1, &member,
603 check_user, (char *) &ustate);
607 critical_alert("incremental", "Error contacting Moira server "
608 "to lookup user %s: %s", member,
609 error_message(code));
612 /* We don't use moira_disconnect()
613 * because we may already be in the routine.
618 if (!code && ustate!=1 && ustate!=2)
619 return; /* inactive user */
623 critical_alert("incremental", "Couldn't %s %s %s %s: %s",
624 op ? "add" : "remove", member,
625 op ? "to" : "from", buf,
626 error_message(code));
631 long pr_try(long (*fn)(), char *a1, char *a2, char *a3, char *a4, char *a5,
632 char *a6, char *a7, char *a8)
634 static int initd = 0;
641 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
648 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
651 critical_alert("incremental", "Couldn't initialize libprot: %s",
652 error_message(code));
656 sleep(1); /* give ptserver room */
658 while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8))
663 if (code == UNOQUORUM)
668 /* Re-initialize the prdb connection */
669 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
671 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
674 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
675 error_message(code));
676 initd = 0; /* we lost */
688 for (i = 0; file_exists(STOP_FILE); i++)
692 critical_alert("incremental",
693 "AFS incremental failed (%s exists): %s",
702 int moira_connect(void)
704 static char hostname[64];
707 if (!mr_connections++)
709 gethostname(hostname, sizeof(hostname));
710 code = mr_connect(hostname);
712 code = mr_auth("afs.incr");
718 int moira_disconnect(void)
722 if (!--mr_connections)
725 while (m = member_head)
727 edit_group(m->op, m->list, m->type, m->member);
728 member_head = m->next;