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>
26 #include <afs/param.h>
27 #include <afs/cellconfig.h>
28 #include <afs/venus.h>
29 #include <afs/ptclient.h>
30 #include <afs/pterror.h>
32 #define STOP_FILE "/moira/afs/noafs"
33 #define file_exists(file) (access((file), F_OK) == 0)
37 /* Main stub routines */
38 void do_user(char **before, int beforec, char **after, int afterc);
39 void do_list(char **before, int beforec, char **after, int afterc);
40 void do_member(char **before, int beforec, char **after, int afterc);
41 void do_filesys(char **before, int beforec, char **after, int afterc);
42 void do_quota(char **before, int beforec, char **after, int afterc);
44 /* Support stub routines */
45 void run_cmd(char *cmd);
46 int add_user_lists(int ac, char **av, void *user);
47 int add_list_members(int ac, char **av, void *group);
48 int check_user(int ac, char **av, void *ustate);
49 void edit_group(int op, char *group, char *type, char *member);
52 int moira_connect(void);
53 int moira_disconnect(void);
55 /* libprot.a routines */
56 extern long pr_Initialize();
57 extern long pr_CreateUser();
58 extern long pr_CreateGroup();
59 extern long pr_DeleteByID();
60 extern long pr_ChangeEntry();
61 extern long pr_SetFieldsEntry();
62 extern long pr_AddToGroup();
63 extern long pr_RemoveUserFromGroup();
64 extern long pr_SIdToName();
66 static char tbl_buf[1024];
67 static struct member {
69 char list[LIST_NAME_SIZE];
70 char type[IMEMBERS_MEMBER_TYPE_SIZE];
71 char member[MAX_FIELD_WIDTH];
73 } *member_head = NULL;
75 static int mr_connections = 0;
77 int main(int argc, char **argv)
79 int beforec, afterc, i;
80 char *table, **before, **after;
83 getrlimit(RLIMIT_NOFILE, &rl);
84 for (i = rl.rlim_cur; i > 2; i--)
87 whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]);
90 beforec = atoi(argv[2]);
92 afterc = atoi(argv[3]);
93 after = &argv[4 + beforec];
95 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
97 strcpy(tbl_buf, table);
98 strcat(tbl_buf, " (");
99 for (i = 0; i < beforec; i++)
102 strcat(tbl_buf, ",");
103 strcat(tbl_buf, before[i]);
105 strcat(tbl_buf, ")->(");
106 for (i = 0; i < afterc; i++)
109 strcat(tbl_buf, ",");
110 strcat(tbl_buf, after[i]);
112 strcat(tbl_buf, ")");
114 initialize_sms_error_table();
115 initialize_krb_error_table();
117 if (!strcmp(table, "users"))
118 do_user(before, beforec, after, afterc);
119 else if (!strcmp(table, "list"))
120 do_list(before, beforec, after, afterc);
121 else if (!strcmp(table, "imembers"))
122 do_member(before, beforec, after, afterc);
123 else if (!strcmp(table, "filesys"))
124 do_filesys(before, beforec, after, afterc);
125 else if (!strcmp(table, "quota"))
126 do_quota(before, beforec, after, afterc);
132 void do_user(char **before, int beforec, char **after, int afterc)
134 int astate, bstate, auid, buid, code;
137 auid = buid = astate = bstate = 0;
138 if (afterc > U_STATE)
139 astate = atoi(after[U_STATE]);
140 if (beforec > U_STATE)
141 bstate = atoi(before[U_STATE]);
143 auid = atoi(after[U_UID]);
145 buid = atoi(before[U_UID]);
147 /* We consider "half-registered" users to be active */
153 if (astate != 1 && bstate != 1) /* inactive user */
156 if (astate == bstate && auid == buid &&
157 !strcmp(before[U_NAME], after[U_NAME]))
158 /* No AFS related attributes have changed */
161 if (astate == bstate)
163 /* Only a modify has to be done */
164 com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)",
165 before[U_NAME], buid, after[U_NAME], auid);
167 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
170 critical_alert("incremental",
171 "Couldn't change user %s (id %d) to %s (id %d): %s",
172 before[U_NAME], buid, after[U_NAME], auid,
173 error_message(code));
179 com_err(whoami, 0, "Deleting user %s (uid %d)",
180 before[U_NAME], buid);
182 code = pr_try(pr_DeleteByID, buid);
183 if (code && code != PRNOENT)
185 critical_alert("incremental", "Couldn't delete user %s (id %d): %s",
186 before[U_NAME], buid, error_message(code));
192 com_err(whoami, 0, "%s user %s (uid %d)",
193 ((bstate != 0) ? "Reactivating" : "Creating"),
194 after[U_NAME], auid);
196 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
197 /* if we get PRIDEXIST, it's only an error if the username
198 doesn't match (otherwise it just means the user was deleted
199 from Moira but not AFS */
200 if (code == PRIDEXIST)
202 char ename[PR_MAXNAMELEN];
204 if (pr_try(pr_SIdToName, auid, ename) == 0 &&
205 !strcmp(after[U_NAME], ename))
210 critical_alert("incremental", "Couldn't create user %s (id %d): %s",
211 after[U_NAME], auid, error_message(code));
217 /* Reactivating a user; get his group list */
218 code = moira_connect();
221 critical_alert("incremental", "Error contacting Moira server "
222 "to retrieve grouplist of user %s: %s",
223 after[U_NAME], error_message(code));
227 av[1] = after[U_NAME];
228 code = mr_query("get_lists_of_member", 2, av, add_user_lists,
230 if (code && code != MR_NO_MATCH)
232 critical_alert("incremental",
233 "Couldn't retrieve membership of user %s: %s",
234 after[U_NAME], error_message(code));
243 void do_list(char **before, int beforec, char **after, int afterc)
248 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
252 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP]))
254 bgid = atoi(before[L_GID]);
255 bhide = atoi(before[L_HIDDEN]);
257 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP]))
259 agid = atoi(after[L_GID]);
260 ahide = atoi(after[L_HIDDEN]);
263 if (agid == 0 && bgid == 0) /* Not active groups */
268 if (strcmp(after[L_NAME], before[L_NAME]))
270 /* Only a modify is required */
271 strcpy(g1, "system:");
272 strcpy(g2, "system:");
273 strcat(g1, before[L_NAME]);
274 strcat(g2, after[L_NAME]);
276 com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)",
277 before[L_NAME], bgid, after[L_NAME], agid);
279 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
282 critical_alert("incremental", "Couldn't change group %s (id %d) "
283 "to %s (id %d): %s", before[L_NAME], -bgid,
284 after[L_NAME], -agid, error_message(code));
289 com_err(whoami, 0, "Making group %s (gid %d) %s", after[L_NAME],
290 agid, (ahide ? "hidden" : "visible"));
291 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
292 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
293 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
296 critical_alert("incremental",
297 "Couldn't set flags of group %s: %s",
298 after[L_NAME], error_message(code));
305 com_err(whoami, 0, "Deleting group %s (gid %d)", before[L_NAME], bgid);
306 code = pr_try(pr_DeleteByID, -bgid);
307 if (code && code != PRNOENT)
309 critical_alert("incremental",
310 "Couldn't delete group %s (id %d): %s",
311 before[L_NAME], -bgid, error_message(code));
317 strcpy(g1, "system:");
318 strcat(g1, after[L_NAME]);
319 strcpy(g2, "system:administrators");
321 com_err(whoami, 0, "Creating %s group %s (gid %d)",
322 (ahide ? "hidden" : "visible"), after[L_NAME], agid);
323 code = pr_try(pr_CreateGroup, g1, g2, &id);
324 if (code == PRIDEXIST)
326 char ename[PR_MAXNAMELEN];
328 if (pr_try(pr_SIdToName, -agid, ename) == 0 && !strcmp(g1, ename))
333 critical_alert("incremental", "Couldn't create group %s (id %d): %s",
334 after[L_NAME], id, error_message(code));
339 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
340 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
341 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
344 critical_alert("incremental",
345 "Couldn't set flags of group %s: %s",
346 after[L_NAME], error_message(code));
350 /* We need to make sure the group is properly populated */
351 if (beforec < L_ACTIVE)
354 code = moira_connect();
357 critical_alert("incremental",
358 "Error contacting Moira server to resolve %s: %s",
359 after[L_NAME], error_message(code));
362 av[0] = after[L_NAME];
363 code = mr_query("get_end_members_of_list", 1, av,
364 add_list_members, after[L_NAME]);
367 critical_alert("incremental",
368 "Couldn't retrieve full membership of list %s: %s",
369 after[L_NAME], error_message(code));
378 #define LM_EXTRA_ACTIVE (LM_END)
379 #define LM_EXTRA_PUBLIC (LM_END+1)
380 #define LM_EXTRA_HIDDEN (LM_END+2)
381 #define LM_EXTRA_MAILLIST (LM_END+3)
382 #define LM_EXTRA_GROUP (LM_END+4)
383 #define LM_EXTRA_GID (LM_END+5)
384 #define LM_EXTRA_END (LM_END+6)
386 void do_member(char **before, int beforec, char **after, int afterc)
390 if (afterc < LM_EXTRA_END)
394 if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP]))
398 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
402 if (beforec < LM_EXTRA_END)
406 if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP]))
409 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
414 void do_filesys(char **before, int beforec, char **after, int afterc)
417 int acreate, atype, bcreate, btype;
419 if (afterc < FS_CREATE)
423 atype = !strcmp(after[FS_TYPE], "AFS");
424 acreate = atoi(after[FS_CREATE]);
427 if (beforec < FS_CREATE)
429 if (acreate == 0 || atype == 0)
432 /* new locker creation */
433 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
434 BIN_DIR, BIN_DIR, BIN_DIR,
435 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
436 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
441 btype = !strcmp(before[FS_TYPE], "AFS");
442 bcreate = atoi(before[FS_CREATE]);
443 if (afterc < FS_CREATE)
445 if (btype && bcreate)
446 critical_alert("incremental", "Cannot delete AFS filesystem %s: "
447 "Operation not supported", before[FS_NAME]);
454 /* Are we dealing with AFS lockers (could be type ERR lockers) */
455 if (!atype && !btype)
457 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
461 /* By now, we know we are simply changing AFS filesystem attributes.
462 * Operations supported:
463 * Name change: rename/remount
464 * Path change: remount
465 * Type change: ERR<-->AFS
469 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
470 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
472 critical_alert("incremental",
473 "Cannot change ownership of filesystem %s: Operation not yet supported",
478 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
479 BIN_DIR, BIN_DIR, BIN_DIR,
480 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
481 before[FS_L_TYPE], before[FS_PACK],
482 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
483 after[FS_L_TYPE], after[FS_PACK]);
488 void do_quota(char **before, int beforec, char **after, int afterc)
492 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
493 strncmp("/afs/", after[Q_DIRECTORY], 5))
496 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
497 BIN_DIR, BIN_DIR, BIN_DIR,
498 after[Q_DIRECTORY], after[Q_QUOTA]);
504 void run_cmd(char *cmd)
506 int success=0, tries=0;
510 while (success == 0 && tries < 2)
514 com_err(whoami, 0, "Executing command: %s", cmd);
515 if (system(cmd) == 0)
519 critical_alert("incremental", "failed command: %s", cmd);
523 int add_user_lists(int ac, char **av, void *user)
525 if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */
526 edit_group(1, av[L_NAME], "USER", user);
531 int add_list_members(int ac, char **av, void *group)
533 edit_group(1, group, av[0], av[1]);
537 int check_user(int ac, char **av, void *ustate)
539 *(int *)ustate = atoi(av[U_STATE]);
544 void edit_group(int op, char *group, char *type, char *member)
547 char buf[PR_MAXNAMELEN];
549 static char local_realm[REALM_SZ+1] = "";
552 /* The following KERBEROS code allows for the use of entities
556 krb_get_lrealm(local_realm, 1);
557 if (!strcmp(type, "KERBEROS"))
559 p = strchr(member, '@');
560 if (p && !strcasecmp(p+1, local_realm))
563 else if (strcmp(type, "USER"))
564 return; /* invalid type */
566 /* Cannot risk doing another query during a callback */
567 /* We could do this simply for type USER, but eventually this may also
568 * dynamically add KERBEROS types to the prdb, and we will need to do
569 * a query to look up the uid of the null-instance user */
572 m = malloc(sizeof(struct member));
575 critical_alert("incremental", "Out of memory");
579 strcpy(m->list, group);
580 strcpy(m->type, type);
581 strcpy(m->member, member);
582 m->next = member_head;
587 strcpy(buf, "system:");
589 com_err(whoami, 0, "%s %s %s group %s", (op ? "Adding" : "Removing"), member,
590 (op ? "to" : "from"), group);
591 code = pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
594 if (op==1 && code == PRIDEXIST)
595 return; /* Already added */
598 { /* Something is missing */
600 return; /* Already deleted */
601 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
604 /* Check whether the member being added is an active user */
605 code = moira_connect();
608 code = mr_query("get_user_by_login", 1, &member,
609 check_user, (char *) &ustate);
613 critical_alert("incremental", "Error contacting Moira server "
614 "to lookup user %s: %s", member,
615 error_message(code));
618 /* We don't use moira_disconnect()
619 * because we may already be in the routine.
624 if (!code && ustate!=1 && ustate!=2)
625 return; /* inactive user */
629 critical_alert("incremental", "Couldn't %s %s %s %s: %s",
630 op ? "add" : "remove", member,
631 op ? "to" : "from", buf,
632 error_message(code));
637 long pr_try(long (*fn)(), char *a1, char *a2, char *a3, char *a4, char *a5,
638 char *a6, char *a7, char *a8)
640 static int initd = 0;
647 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
654 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
657 critical_alert("incremental", "Couldn't initialize libprot: %s",
658 error_message(code));
662 sleep(1); /* give ptserver room */
664 while ((code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)))
669 if (code == UNOQUORUM)
671 else if (code == PRPERM)
672 system("/bin/athena/aklog");
676 /* Re-initialize the prdb connection */
677 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
679 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
682 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
683 error_message(code));
684 initd = 0; /* we lost */
696 for (i = 0; file_exists(STOP_FILE); i++)
700 critical_alert("incremental",
701 "AFS incremental failed (%s exists): %s",
710 int moira_connect(void)
714 if (!mr_connections++)
718 code = mr_connect(uts.nodename);
720 code = mr_auth("afs.incr");
726 int moira_disconnect(void)
730 if (!--mr_connections)
733 while ((m = member_head))
735 edit_group(m->op, m->list, m->type, m->member);
736 member_head = m->next;