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/resource.h>
13 #include <sys/types.h>
14 #include <sys/utsname.h>
22 #include <moira_site.h>
24 #include <afs/param.h>
25 #include <afs/cellconfig.h>
26 #include <afs/venus.h>
27 #include <afs/ptclient.h>
28 #include <afs/pterror.h>
30 #define STOP_FILE "/moira/afs/noafs"
31 #define file_exists(file) (access((file), F_OK) == 0)
35 /* Main stub routines */
36 void do_user(char **before, int beforec, char **after, int afterc);
37 void do_list(char **before, int beforec, char **after, int afterc);
38 void do_member(char **before, int beforec, char **after, int afterc);
39 void do_filesys(char **before, int beforec, char **after, int afterc);
40 void do_quota(char **before, int beforec, char **after, int afterc);
42 /* Support stub routines */
43 void run_cmd(char *cmd);
44 int add_user_lists(int ac, char **av, void *user);
45 int add_list_members(int ac, char **av, void *group);
46 int check_user(int ac, char **av, void *ustate);
47 void edit_group(int op, char *group, char *type, char *member);
50 int moira_connect(void);
51 int moira_disconnect(void);
53 /* libprot.a routines */
54 extern long pr_Initialize();
55 extern long pr_CreateUser();
56 extern long pr_CreateGroup();
57 extern long pr_DeleteByID();
58 extern long pr_ChangeEntry();
59 extern long pr_SetFieldsEntry();
60 extern long pr_AddToGroup();
61 extern long pr_RemoveUserFromGroup();
62 extern long pr_SIdToName();
64 static char tbl_buf[1024];
65 static struct member {
71 } *member_head = NULL;
73 static int mr_connections = 0;
75 int main(int argc, char **argv)
77 int beforec, afterc, i;
78 char *table, **before, **after;
81 getrlimit(RLIMIT_NOFILE, &rl);
82 for (i = rl.rlim_cur; i > 2; i--)
85 whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]);
88 beforec = atoi(argv[2]);
90 afterc = atoi(argv[3]);
91 after = &argv[4 + beforec];
93 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
95 strcpy(tbl_buf, table);
96 strcat(tbl_buf, " (");
97 for (i = 0; i < beforec; i++)
100 strcat(tbl_buf, ",");
101 strcat(tbl_buf, before[i]);
103 strcat(tbl_buf, ")->(");
104 for (i = 0; i < afterc; i++)
107 strcat(tbl_buf, ",");
108 strcat(tbl_buf, after[i]);
110 strcat(tbl_buf, ")");
112 initialize_sms_error_table();
113 initialize_krb_error_table();
115 if (!strcmp(table, "users"))
116 do_user(before, beforec, after, afterc);
117 else if (!strcmp(table, "list"))
118 do_list(before, beforec, after, afterc);
119 else if (!strcmp(table, "imembers"))
120 do_member(before, beforec, after, afterc);
121 else if (!strcmp(table, "filesys"))
122 do_filesys(before, beforec, after, afterc);
123 else if (!strcmp(table, "quota"))
124 do_quota(before, beforec, after, afterc);
130 void do_user(char **before, int beforec, char **after, int afterc)
132 int astate, bstate, auid, buid, code;
135 auid = buid = astate = bstate = 0;
136 if (afterc > U_STATE)
137 astate = atoi(after[U_STATE]);
138 if (beforec > U_STATE)
139 bstate = atoi(before[U_STATE]);
141 auid = atoi(after[U_UID]);
143 buid = atoi(before[U_UID]);
145 /* We consider "half-registered" users to be active */
151 if (astate != 1 && bstate != 1) /* inactive user */
154 if (astate == bstate && auid == buid &&
155 !strcmp(before[U_NAME], after[U_NAME]))
156 /* No AFS related attributes have changed */
159 if (astate == bstate)
161 /* Only a modify has to be done */
162 com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)",
163 before[U_NAME], buid, after[U_NAME], auid);
165 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
168 critical_alert("incremental",
169 "Couldn't change user %s (id %d) to %s (id %d): %s",
170 before[U_NAME], buid, after[U_NAME], auid,
171 error_message(code));
177 com_err(whoami, 0, "Deleting user %s (uid %d)",
178 before[U_NAME], buid);
180 code = pr_try(pr_DeleteByID, buid);
181 if (code && code != PRNOENT)
183 critical_alert("incremental", "Couldn't delete user %s (id %d): %s",
184 before[U_NAME], buid, error_message(code));
190 com_err(whoami, 0, "%s user %s (uid %d)",
191 ((bstate != 0) ? "Reactivating" : "Creating"),
192 after[U_NAME], auid);
194 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
195 /* if we get PRIDEXIST, it's only an error if the username
196 doesn't match (otherwise it just means the user was deleted
197 from Moira but not AFS */
198 if (code == PRIDEXIST)
202 if (pr_try(pr_SIdToName, auid, ename) == 0 &&
203 !strcmp(after[U_NAME], ename))
208 critical_alert("incremental", "Couldn't create user %s (id %d): %s",
209 after[U_NAME], auid, error_message(code));
215 /* Reactivating a user; get his group list */
216 code = moira_connect();
219 critical_alert("incremental", "Error contacting Moira server "
220 "to retrieve grouplist of user %s: %s",
221 after[U_NAME], error_message(code));
225 av[1] = after[U_NAME];
226 code = mr_query("get_lists_of_member", 2, av, add_user_lists,
228 if (code && code != MR_NO_MATCH)
230 critical_alert("incremental",
231 "Couldn't retrieve membership of user %s: %s",
232 after[U_NAME], error_message(code));
241 void do_list(char **before, int beforec, char **after, int afterc)
246 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
250 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP]))
252 bgid = atoi(before[L_GID]);
253 bhide = atoi(before[L_HIDDEN]);
255 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP]))
257 agid = atoi(after[L_GID]);
258 ahide = atoi(after[L_HIDDEN]);
261 if (agid == 0 && bgid == 0) /* Not active groups */
266 if (strcmp(after[L_NAME], before[L_NAME]))
268 /* Only a modify is required */
269 strcpy(g1, "system:");
270 strcpy(g2, "system:");
271 strcat(g1, before[L_NAME]);
272 strcat(g2, after[L_NAME]);
274 com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)",
275 before[L_NAME], bgid, after[L_NAME], agid);
277 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
280 critical_alert("incremental", "Couldn't change group %s (id %d) "
281 "to %s (id %d): %s", before[L_NAME], -bgid,
282 after[L_NAME], -agid, error_message(code));
287 com_err(whoami, 0, "Making group %s (gid %d) %s", after[L_NAME],
288 agid, (ahide ? "hidden" : "visible"));
289 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
290 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
291 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
294 critical_alert("incremental",
295 "Couldn't set flags of group %s: %s",
296 after[L_NAME], error_message(code));
303 com_err(whoami, 0, "Deleting group %s (gid %d)", before[L_NAME], bgid);
304 code = pr_try(pr_DeleteByID, -bgid);
305 if (code && code != PRNOENT)
307 critical_alert("incremental",
308 "Couldn't delete group %s (id %d): %s",
309 before[L_NAME], -bgid, error_message(code));
315 strcpy(g1, "system:");
316 strcat(g1, after[L_NAME]);
317 strcpy(g2, "system:administrators");
319 com_err(whoami, 0, "Creating %s group %s (gid %d)",
320 (ahide ? "hidden" : "visible"), after[L_NAME], agid);
321 code = pr_try(pr_CreateGroup, g1, g2, &id);
322 if (code == PRIDEXIST)
326 if (pr_try(pr_SIdToName, -agid, ename) == 0 && !strcmp(g1, ename))
331 critical_alert("incremental", "Couldn't create group %s (id %d): %s",
332 after[L_NAME], id, error_message(code));
337 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
338 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>
339 PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/);
342 critical_alert("incremental",
343 "Couldn't set flags of group %s: %s",
344 after[L_NAME], error_message(code));
348 /* We need to make sure the group is properly populated */
349 if (beforec < L_ACTIVE)
352 code = moira_connect();
355 critical_alert("incremental",
356 "Error contacting Moira server to resolve %s: %s",
357 after[L_NAME], error_message(code));
360 av[0] = after[L_NAME];
361 code = mr_query("get_end_members_of_list", 1, av,
362 add_list_members, after[L_NAME]);
365 critical_alert("incremental",
366 "Couldn't retrieve full membership of list %s: %s",
367 after[L_NAME], error_message(code));
376 #define LM_EXTRA_ACTIVE (LM_END)
377 #define LM_EXTRA_PUBLIC (LM_END+1)
378 #define LM_EXTRA_HIDDEN (LM_END+2)
379 #define LM_EXTRA_MAILLIST (LM_END+3)
380 #define LM_EXTRA_GROUP (LM_END+4)
381 #define LM_EXTRA_GID (LM_END+5)
382 #define LM_EXTRA_END (LM_END+6)
384 void do_member(char **before, int beforec, char **after, int afterc)
388 if (afterc < LM_EXTRA_END)
392 if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP]))
396 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
400 if (beforec < LM_EXTRA_END)
404 if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP]))
407 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
412 void do_filesys(char **before, int beforec, char **after, int afterc)
415 int acreate, atype, bcreate, btype;
417 if (afterc < FS_CREATE)
421 atype = !strcmp(after[FS_TYPE], "AFS");
422 acreate = atoi(after[FS_CREATE]);
425 if (beforec < FS_CREATE)
427 if (acreate == 0 || atype == 0)
430 /* new locker creation */
431 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
432 BIN_DIR, BIN_DIR, BIN_DIR,
433 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
434 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
439 btype = !strcmp(before[FS_TYPE], "AFS");
440 bcreate = atoi(before[FS_CREATE]);
441 if (afterc < FS_CREATE)
443 if (btype && bcreate)
444 critical_alert("incremental", "Cannot delete AFS filesystem %s: "
445 "Operation not supported", before[FS_NAME]);
452 /* Are we dealing with AFS lockers (could be type ERR lockers) */
453 if (!atype && !btype)
455 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
459 /* By now, we know we are simply changing AFS filesystem attributes.
460 * Operations supported:
461 * Name change: rename/remount
462 * Path change: remount
463 * Type change: ERR<-->AFS
467 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
468 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
470 critical_alert("incremental",
471 "Cannot change ownership of filesystem %s: Operation not yet supported",
476 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
477 BIN_DIR, BIN_DIR, BIN_DIR,
478 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
479 before[FS_L_TYPE], before[FS_PACK],
480 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
481 after[FS_L_TYPE], after[FS_PACK]);
486 void do_quota(char **before, int beforec, char **after, int afterc)
490 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
491 strncmp("/afs/", after[Q_DIRECTORY], 5))
494 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
495 BIN_DIR, BIN_DIR, BIN_DIR,
496 after[Q_DIRECTORY], after[Q_QUOTA]);
502 void run_cmd(char *cmd)
504 int success=0, tries=0;
508 while (success == 0 && tries < 2)
512 com_err(whoami, 0, "Executing command: %s", cmd);
513 if (system(cmd) == 0)
517 critical_alert("incremental", "failed command: %s", cmd);
521 int add_user_lists(int ac, char **av, void *user)
523 if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */
524 edit_group(1, av[L_NAME], "USER", user);
529 int add_list_members(int ac, char **av, void *group)
531 edit_group(1, group, av[0], av[1]);
535 int check_user(int ac, char **av, void *ustate)
537 *(int *)ustate = atoi(av[U_STATE]);
542 void edit_group(int op, char *group, char *type, char *member)
545 char buf[PR_MAXNAMELEN];
547 static char local_realm[REALM_SZ+1] = "";
550 /* The following KERBEROS code allows for the use of entities
554 krb_get_lrealm(local_realm, 1);
555 if (!strcmp(type, "KERBEROS"))
557 p = strchr(member, '@');
558 if (p && !strcasecmp(p+1, local_realm))
561 else if (strcmp(type, "USER"))
562 return; /* invalid type */
564 /* Cannot risk doing another query during a callback */
565 /* We could do this simply for type USER, but eventually this may also
566 * dynamically add KERBEROS types to the prdb, and we will need to do
567 * a query to look up the uid of the null-instance user */
570 m = malloc(sizeof(struct member));
573 critical_alert("incremental", "Out of memory");
577 strcpy(m->list, group);
578 strcpy(m->type, type);
579 strcpy(m->member, member);
580 m->next = member_head;
585 strcpy(buf, "system:");
587 com_err(whoami, 0, "%s %s %s group %s", (op ? "Adding" : "Removing"), member,
588 (op ? "to" : "from"), group);
589 code = pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
592 if (op==1 && code == PRIDEXIST)
593 return; /* Already added */
596 { /* Something is missing */
598 return; /* Already deleted */
599 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
602 /* Check whether the member being added is an active user */
603 code = moira_connect();
606 code = mr_query("get_user_by_login", 1, &member,
607 check_user, (char *) &ustate);
611 critical_alert("incremental", "Error contacting Moira server "
612 "to lookup user %s: %s", member,
613 error_message(code));
616 /* We don't use moira_disconnect()
617 * because we may already be in the routine.
622 if (!code && ustate!=1 && ustate!=2)
623 return; /* inactive user */
627 critical_alert("incremental", "Couldn't %s %s %s %s: %s",
628 op ? "add" : "remove", member,
629 op ? "to" : "from", buf,
630 error_message(code));
635 long pr_try(long (*fn)(), char *a1, char *a2, char *a3, char *a4, char *a5,
636 char *a6, char *a7, char *a8)
638 static int initd = 0;
645 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
652 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
655 critical_alert("incremental", "Couldn't initialize libprot: %s",
656 error_message(code));
660 sleep(1); /* give ptserver room */
662 while ((code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)))
667 if (code == UNOQUORUM)
672 /* Re-initialize the prdb connection */
673 code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
675 code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
678 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
679 error_message(code));
680 initd = 0; /* we lost */
692 for (i = 0; file_exists(STOP_FILE); i++)
696 critical_alert("incremental",
697 "AFS incremental failed (%s exists): %s",
706 int moira_connect(void)
710 if (!mr_connections++)
714 code = mr_connect(uts.nodename);
716 code = mr_auth("afs.incr");
722 int moira_disconnect(void)
726 if (!--mr_connections)
729 while ((m = member_head))
731 edit_group(m->op, m->list, m->type, m->member);
732 member_head = m->next;