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 <moira_site.h>
12 #include <moira_schema.h>
16 #include <sys/resource.h>
17 #include <sys/types.h>
18 #include <sys/utsname.h>
29 #include <afs/param.h>
30 #include <afs/cellconfig.h>
31 #include <afs/venus.h>
32 #include <afs/ptclient.h>
33 #include <afs/pterror.h>
35 /* Cheesy test for determining AFS more recent than 3.4a */
36 #ifndef AFSCONF_CLIENTNAME
37 #include <afs/dirpath.h>
38 #define AFSCONF_CLIENTNAME AFSDIR_CLIENT_ETC_DIRPATH
41 #define STOP_FILE "/moira/afs/noafs"
42 #define file_exists(file) (access((file), F_OK) == 0)
46 /* Main stub routines */
47 void do_user(char **before, int beforec, char **after, int afterc);
48 void do_list(char **before, int beforec, char **after, int afterc);
49 void do_member(char **before, int beforec, char **after, int afterc);
50 void do_filesys(char **before, int beforec, char **after, int afterc);
51 void do_quota(char **before, int beforec, char **after, int afterc);
53 /* Support stub routines */
54 void run_cmd(char *cmd);
55 int add_user_lists(int ac, char **av, void *user);
56 int add_list_members(int ac, char **av, void *group);
57 int check_user(int ac, char **av, void *ustate);
58 void edit_group(int op, char *group, char *type, char *member);
61 int moira_connect(void);
62 int moira_disconnect(void);
64 /* libprot.a routines */
65 extern long pr_Initialize();
66 extern long pr_CreateUser();
67 extern long pr_CreateGroup();
68 extern long pr_DeleteByID();
69 extern long pr_ChangeEntry();
70 extern long pr_SetFieldsEntry();
71 extern long pr_AddToGroup();
72 extern long pr_RemoveUserFromGroup();
73 extern long pr_SIdToName();
75 static char tbl_buf[1024];
76 static struct member {
78 char list[LIST_NAME_SIZE];
79 char type[IMEMBERS_MEMBER_TYPE_SIZE];
80 char member[MAX_FIELD_WIDTH];
82 } *member_head = NULL;
84 static int mr_connections = 0;
86 int main(int argc, char **argv)
88 int beforec, afterc, i;
89 char *table, **before, **after;
92 getrlimit(RLIMIT_NOFILE, &rl);
93 for (i = rl.rlim_cur; i > 2; i--)
96 whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]);
99 beforec = atoi(argv[2]);
101 afterc = atoi(argv[3]);
102 after = &argv[4 + beforec];
104 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
106 strcpy(tbl_buf, table);
107 strcat(tbl_buf, " (");
108 for (i = 0; i < beforec; i++)
111 strcat(tbl_buf, ",");
112 strcat(tbl_buf, before[i]);
114 strcat(tbl_buf, ")->(");
115 for (i = 0; i < afterc; i++)
118 strcat(tbl_buf, ",");
119 strcat(tbl_buf, after[i]);
121 strcat(tbl_buf, ")");
123 initialize_sms_error_table();
124 initialize_krb_error_table();
126 if (!strcmp(table, "users"))
127 do_user(before, beforec, after, afterc);
128 else if (!strcmp(table, "list"))
129 do_list(before, beforec, after, afterc);
130 else if (!strcmp(table, "imembers"))
131 do_member(before, beforec, after, afterc);
132 else if (!strcmp(table, "filesys"))
133 do_filesys(before, beforec, after, afterc);
134 else if (!strcmp(table, "quota"))
135 do_quota(before, beforec, after, afterc);
141 void do_user(char **before, int beforec, char **after, int afterc)
143 int astate, bstate, auid, buid, code;
146 auid = buid = astate = bstate = 0;
147 if (afterc > U_STATE)
148 astate = atoi(after[U_STATE]);
149 if (beforec > U_STATE)
150 bstate = atoi(before[U_STATE]);
152 auid = atoi(after[U_UID]);
154 buid = atoi(before[U_UID]);
156 /* We consider "half-registered" users to be active */
162 if (astate != 1 && bstate != 1) /* inactive user */
165 if (astate == bstate && auid == buid &&
166 !strcmp(before[U_NAME], after[U_NAME]))
167 /* No AFS related attributes have changed */
170 if (astate == bstate)
172 /* Only a modify has to be done */
173 com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)",
174 before[U_NAME], buid, after[U_NAME], auid);
176 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
179 critical_alert("incremental",
180 "Couldn't change user %s (id %d) to %s (id %d): %s",
181 before[U_NAME], buid, after[U_NAME], auid,
182 error_message(code));
188 com_err(whoami, 0, "Deleting user %s (uid %d)",
189 before[U_NAME], buid);
191 code = pr_try(pr_DeleteByID, buid);
192 if (code && code != PRNOENT)
194 critical_alert("incremental", "Couldn't delete user %s (id %d): %s",
195 before[U_NAME], buid, error_message(code));
201 com_err(whoami, 0, "%s user %s (uid %d)",
202 ((bstate != 0) ? "Reactivating" : "Creating"),
203 after[U_NAME], auid);
205 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
206 /* if we get PRIDEXIST, it's only an error if the username
207 doesn't match (otherwise it just means the user was deleted
208 from Moira but not AFS */
209 if (code == PRIDEXIST)
211 char ename[PR_MAXNAMELEN];
213 if (pr_try(pr_SIdToName, auid, ename) == 0 &&
214 !strcmp(after[U_NAME], ename))
219 critical_alert("incremental", "Couldn't create user %s (id %d): %s",
220 after[U_NAME], auid, error_message(code));
226 /* Reactivating a user; get his group list */
227 code = moira_connect();
230 critical_alert("incremental", "Error contacting Moira server "
231 "to retrieve grouplist of user %s: %s",
232 after[U_NAME], error_message(code));
236 av[1] = after[U_NAME];
237 code = mr_query("get_lists_of_member", 2, av, add_user_lists,
239 if (code && code != MR_NO_MATCH)
241 critical_alert("incremental",
242 "Couldn't retrieve membership of user %s: %s",
243 after[U_NAME], error_message(code));
252 void do_list(char **before, int beforec, char **after, int afterc)
257 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
261 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP]))
263 bgid = atoi(before[L_GID]);
264 bhide = atoi(before[L_HIDDEN]);
266 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP]))
268 agid = atoi(after[L_GID]);
269 ahide = atoi(after[L_HIDDEN]);
272 if (agid == 0 && bgid == 0) /* Not active groups */
277 if (strcmp(after[L_NAME], before[L_NAME]))
279 /* Only a modify is required */
280 strcpy(g1, "system:");
281 strcpy(g2, "system:");
282 strcat(g1, before[L_NAME]);
283 strcat(g2, after[L_NAME]);
285 com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)",
286 before[L_NAME], bgid, after[L_NAME], agid);
288 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
291 critical_alert("incremental", "Couldn't change group %s (id %d) "
292 "to %s (id %d): %s", before[L_NAME], -bgid,
293 after[L_NAME], -agid, error_message(code));
298 com_err(whoami, 0, "Making group %s (gid %d) %s", after[L_NAME],
299 agid, (ahide ? "hidden" : "visible"));
300 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
301 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
302 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
305 critical_alert("incremental",
306 "Couldn't set flags of group %s: %s",
307 after[L_NAME], error_message(code));
314 com_err(whoami, 0, "Deleting group %s (gid %d)", before[L_NAME], bgid);
315 code = pr_try(pr_DeleteByID, -bgid);
316 if (code && code != PRNOENT)
318 critical_alert("incremental",
319 "Couldn't delete group %s (id %d): %s",
320 before[L_NAME], -bgid, error_message(code));
326 strcpy(g1, "system:");
327 strcat(g1, after[L_NAME]);
328 strcpy(g2, "system:administrators");
330 com_err(whoami, 0, "Creating %s group %s (gid %d)",
331 (ahide ? "hidden" : "visible"), after[L_NAME], agid);
332 code = pr_try(pr_CreateGroup, g1, g2, &id);
333 if (code == PRIDEXIST)
335 char ename[PR_MAXNAMELEN];
337 if (pr_try(pr_SIdToName, -agid, ename) == 0 && !strcmp(g1, ename))
342 critical_alert("incremental", "Couldn't create group %s (id %d): %s",
343 after[L_NAME], id, error_message(code));
348 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
349 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
350 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
353 critical_alert("incremental",
354 "Couldn't set flags of group %s: %s",
355 after[L_NAME], error_message(code));
359 /* We need to make sure the group is properly populated */
360 if (beforec < L_ACTIVE)
363 code = moira_connect();
366 critical_alert("incremental",
367 "Error contacting Moira server to resolve %s: %s",
368 after[L_NAME], error_message(code));
371 av[0] = after[L_NAME];
372 code = mr_query("get_end_members_of_list", 1, av,
373 add_list_members, after[L_NAME]);
376 critical_alert("incremental",
377 "Couldn't retrieve full membership of list %s: %s",
378 after[L_NAME], error_message(code));
387 #define LM_EXTRA_ACTIVE (LM_END)
388 #define LM_EXTRA_PUBLIC (LM_END+1)
389 #define LM_EXTRA_HIDDEN (LM_END+2)
390 #define LM_EXTRA_MAILLIST (LM_END+3)
391 #define LM_EXTRA_GROUP (LM_END+4)
392 #define LM_EXTRA_GID (LM_END+5)
393 #define LM_EXTRA_END (LM_END+6)
395 void do_member(char **before, int beforec, char **after, int afterc)
399 if (afterc < LM_EXTRA_END)
403 if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP]))
407 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
411 if (beforec < LM_EXTRA_END)
415 if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP]))
418 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
423 void do_filesys(char **before, int beforec, char **after, int afterc)
426 int acreate, atype, bcreate, btype;
428 if (afterc < FS_CREATE)
432 atype = !strcmp(after[FS_TYPE], "AFS");
433 acreate = atoi(after[FS_CREATE]);
436 if (beforec < FS_CREATE)
438 if (acreate == 0 || atype == 0)
441 /* new locker creation */
442 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
443 BIN_DIR, BIN_DIR, BIN_DIR,
444 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
445 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
450 btype = !strcmp(before[FS_TYPE], "AFS");
451 bcreate = atoi(before[FS_CREATE]);
452 if (afterc < FS_CREATE)
454 if (btype && bcreate)
455 critical_alert("incremental", "Cannot delete AFS filesystem %s: "
456 "Operation not supported", before[FS_NAME]);
463 /* Are we dealing with AFS lockers (could be type ERR lockers) */
464 if (!atype && !btype)
466 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
470 /* By now, we know we are simply changing AFS filesystem attributes.
471 * Operations supported:
472 * Name change: rename/remount
473 * Path change: remount
474 * Type change: ERR<-->AFS
478 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
479 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
481 critical_alert("incremental",
482 "Cannot change ownership of filesystem %s: Operation not yet supported",
487 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
488 BIN_DIR, BIN_DIR, BIN_DIR,
489 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
490 before[FS_L_TYPE], before[FS_PACK],
491 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
492 after[FS_L_TYPE], after[FS_PACK]);
497 void do_quota(char **before, int beforec, char **after, int afterc)
501 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
502 strncmp("/afs/", after[Q_DIRECTORY], 5))
505 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
506 BIN_DIR, BIN_DIR, BIN_DIR,
507 after[Q_DIRECTORY], after[Q_QUOTA]);
513 void run_cmd(char *cmd)
515 int success=0, tries=0;
519 while (success == 0 && tries < 2)
523 com_err(whoami, 0, "Executing command: %s", cmd);
524 if (system(cmd) == 0)
528 critical_alert("incremental", "failed command: %s", cmd);
532 int add_user_lists(int ac, char **av, void *user)
534 if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */
535 edit_group(1, av[L_NAME], "USER", user);
540 int add_list_members(int ac, char **av, void *group)
542 edit_group(1, group, av[0], av[1]);
546 int check_user(int ac, char **av, void *ustate)
548 *(int *)ustate = atoi(av[U_STATE]);
553 void edit_group(int op, char *group, char *type, char *member)
556 char buf[PR_MAXNAMELEN];
558 static char *local_realm = NULL;
560 krb5_context context = NULL;
562 /* The following KERBEROS code allows for the use of entities
567 code = krb5_init_context(&context);
571 code = krb5_get_default_realm(context, &local_realm);
576 if (!strcmp(type, "KERBEROS"))
578 p = strchr(member, '@');
579 if (p && !strcasecmp(p+1, local_realm))
582 else if (strcmp(type, "USER"))
583 return; /* invalid type */
585 /* Cannot risk doing another query during a callback */
586 /* We could do this simply for type USER, but eventually this may also
587 * dynamically add KERBEROS types to the prdb, and we will need to do
588 * a query to look up the uid of the null-instance user */
591 m = malloc(sizeof(struct member));
594 critical_alert("incremental", "Out of memory");
598 strcpy(m->list, group);
599 strcpy(m->type, type);
600 strcpy(m->member, member);
601 m->next = member_head;
606 strcpy(buf, "system:");
608 com_err(whoami, 0, "%s %s %s group %s", (op ? "Adding" : "Removing"), member,
609 (op ? "to" : "from"), group);
610 code = pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
613 if (op==1 && code == PRIDEXIST)
614 return; /* Already added */
617 { /* Something is missing */
619 return; /* Already deleted */
620 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
623 /* Check whether the member being added is an active user */
624 code = moira_connect();
627 code = mr_query("get_user_by_login", 1, &member,
628 check_user, (char *) &ustate);
632 critical_alert("incremental", "Error contacting Moira server "
633 "to lookup user %s: %s", member,
634 error_message(code));
637 /* We don't use moira_disconnect()
638 * because we may already be in the routine.
643 if (!code && ustate!=1 && ustate!=2)
644 return; /* inactive user */
650 krb5_free_context(context);
654 critical_alert("incremental", "Couldn't %s %s %s %s: %s",
655 op ? "add" : "remove", member,
656 op ? "to" : "from", buf,
657 error_message(code));
662 long pr_try(long (*fn)(), char *a1, char *a2, char *a3, char *a4, char *a5,
663 char *a6, char *a7, char *a8)
665 static int initd = 0;
672 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
679 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
682 critical_alert("incremental", "Couldn't initialize libprot: %s",
683 error_message(code));
687 sleep(1); /* give ptserver room */
689 while ((code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)))
694 if (code == UNOQUORUM)
696 else if (code == PRPERM)
697 system("/bin/athena/aklog");
701 /* Re-initialize the prdb connection */
702 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
704 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
707 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
708 error_message(code));
709 initd = 0; /* we lost */
721 for (i = 0; file_exists(STOP_FILE); i++)
725 critical_alert("incremental",
726 "AFS incremental failed (%s exists): %s",
735 int moira_connect(void)
739 if (!mr_connections++)
743 code = mr_connect(uts.nodename);
745 code = mr_krb5_auth("afs.incr");
751 int moira_disconnect(void)
755 if (!--mr_connections)
758 while ((m = member_head))
760 edit_group(m->op, m->list, m->type, m->member);
761 member_head = m->next;