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 */
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];
66 int beforec, afterc, i;
67 char *table, **before, **after;
69 for (i = getdtablesize() - 1; i > 2; i--)
73 beforec = atoi(argv[2]);
75 afterc = atoi(argv[3]);
76 after = &argv[4 + beforec];
81 strcpy(tbl_buf, table);
82 strcat(tbl_buf, " (");
83 for (i = 0; i < beforec; i++) {
86 strcat(tbl_buf, before[i]);
88 strcat(tbl_buf, ")->(");
89 for (i = 0; i < afterc; i++) {
92 strcat(tbl_buf, after[i]);
96 printf("%s\n", tbl_buf);
99 initialize_sms_error_table();
100 initialize_krb_error_table();
102 if (!strcmp(table, "users")) {
103 do_user(before, beforec, after, afterc);
104 } else if (!strcmp(table, "list")) {
105 do_list(before, beforec, after, afterc);
106 } else if (!strcmp(table, "members")) {
107 do_member(before, beforec, after, afterc);
108 } else if (!strcmp(table, "filesys")) {
109 do_filesys(before, beforec, after, afterc);
110 } else if (!strcmp(table, "quota")) {
111 do_quota(before, beforec, after, afterc);
117 do_user(before, beforec, after, afterc)
123 int astate, bstate, auid, buid, code;
126 auid = buid = astate = bstate = 0;
127 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
128 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
129 if (afterc > U_UID) auid = atoi(after[U_UID]);
130 if (beforec > U_UID) buid = atoi(before[U_UID]);
132 /* We consider "half-registered" users to be active */
133 if (astate == 2) astate = 1;
134 if (bstate == 2) bstate = 1;
136 if (astate != 1 && bstate != 1) /* inactive user */
139 if (astate == bstate && auid == buid &&
140 !strcmp(before[U_NAME], after[U_NAME]))
141 /* No AFS related attributes have changed */
144 if (astate == bstate) {
145 /* Only a modify has to be done */
146 code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, "");
148 critical_alert("incremental",
149 "Couldn't change user %s (id %d) to %s (id %d): %s",
150 before[U_NAME], buid, after[U_NAME], auid,
151 error_message(code));
156 code = pr_try(pr_DeleteByID, buid);
157 if (code && code != PRNOENT) {
158 critical_alert("incremental",
159 "Couldn't delete user %s (id %d): %s",
160 before[U_NAME], buid, error_message(code));
165 code = pr_try(pr_CreateUser, after[U_NAME], &auid);
167 critical_alert("incremental",
168 "Couldn't create user %s (id %d): %s",
169 after[U_NAME], auid, error_message(code));
174 /* Reactivating a user; get his group list */
175 code = moira_connect();
177 critical_alert("incremental",
178 "Error contacting Moira server to retrieve grouplist of user %s: %s",
179 after[U_NAME], error_message(code));
183 av[1] = after[U_NAME];
184 code = mr_query("get_lists_of_member", 2, av,
185 add_user_lists, after[U_NAME]);
186 if (code && code != MR_NO_MATCH)
187 critical_alert("incremental",
188 "Couldn't retrieve membership of user %s: %s",
189 after[U_NAME], error_message(code));
197 do_list(before, beforec, after, afterc)
203 register int agid, bgid;
206 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
210 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
211 bgid = atoi(before[L_GID]);
212 bhide = atoi(before[L_HIDDEN]);
214 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
215 agid = atoi(after[L_GID]);
216 ahide = atoi(after[L_HIDDEN]);
219 if (agid == 0 && bgid == 0) /* Not active groups */
223 if (strcmp(after[L_NAME], before[L_NAME])) {
224 /* Only a modify is required */
225 strcpy(g1, "system:");
226 strcpy(g2, "system:");
227 strcat(g1, before[L_NAME]);
228 strcat(g2, after[L_NAME]);
229 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
231 critical_alert("incremental",
232 "Couldn't change group %s (id %d) to %s (id %d): %s",
233 before[L_NAME], -bgid, after[L_NAME], -agid,
234 error_message(code));
237 if (ahide != bhide) {
238 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
239 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
240 0 /*ngroups*/, 0 /*nusers*/);
242 critical_alert("incremental",
243 "Couldn't set flags of group %s: %s",
244 after[L_NAME], error_message(code));
250 code = pr_try(pr_DeleteByID, -bgid);
251 if (code && code != PRNOENT) {
252 critical_alert("incremental",
253 "Couldn't delete group %s (id %d): %s",
254 before[L_NAME], -bgid, error_message(code));
259 strcpy(g1, "system:");
260 strcat(g1, after[L_NAME]);
261 strcpy(g2, "system:administrators");
263 code = pr_try(pr_CreateGroup, g1, g2, &id);
265 critical_alert("incremental",
266 "Couldn't create group %s (id %d): %s",
267 after[L_NAME], id, error_message(code));
271 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
272 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
273 0 /*ngroups*/, 0 /*nusers*/);
275 critical_alert("incremental",
276 "Couldn't set flags of group %s: %s",
277 after[L_NAME], error_message(code));
281 /* We need to make sure the group is properly populated */
282 if (beforec < L_ACTIVE) return;
284 code = moira_connect();
286 critical_alert("incremental",
287 "Error contacting Moira server to resolve %s: %s",
288 after[L_NAME], error_message(code));
292 av[1] = after[L_NAME];
293 get_members(2, av, after[L_NAME]);
301 do_member(before, beforec, after, afterc)
310 if ((beforec < 4 || !atoi(before[LM_END])) &&
311 (afterc < 4 || !atoi(after[LM_END])))
315 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
317 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
321 do_filesys(before, beforec, after, afterc)
328 int acreate, atype, btype;
330 if (afterc < FS_CREATE) {
333 atype = !strcmp(after[FS_TYPE], "AFS");
334 acreate = atoi(after[FS_CREATE]);
337 if (beforec < FS_CREATE) {
338 if (acreate == 0 || atype == 0) return;
340 /* new locker creation */
341 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
342 BIN_DIR, BIN_DIR, BIN_DIR,
343 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
344 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
349 btype = !strcmp(before[FS_TYPE], "AFS");
350 if (afterc < FS_CREATE) {
352 critical_alert("incremental",
353 "Cannot delete AFS filesystem %s: Operation not supported",
361 /* Are we dealing with AFS lockers (could be type ERR lockers) */
362 if (!atype && !btype)
363 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
366 /* By now, we know we are simply changing AFS filesystem attributes.
367 * Operations supported:
368 * Name change: rename/remount
369 * Path change: remount
370 * Type change: ERR<-->AFS
374 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
375 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
377 critical_alert("incremental",
378 "Cannot change ownership of filesystem %s: Operation not yet supported",
383 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
384 BIN_DIR, BIN_DIR, BIN_DIR,
385 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
386 before[FS_L_TYPE], before[FS_PACK],
387 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
388 after[FS_L_TYPE], after[FS_PACK]);
393 do_quota(before, beforec, after, afterc)
401 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
402 strncmp("/afs/", after[Q_DIRECTORY], 5))
405 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
406 BIN_DIR, BIN_DIR, BIN_DIR,
407 after[Q_DIRECTORY], after[Q_QUOTA]);
416 int success=0, tries=0;
420 while (success == 0 && tries < 2) {
423 com_err(whoami, 0, "Executing command: %s", cmd);
424 if (system(cmd) == 0)
428 critical_alert("incremental", "failed command: %s", cmd);
432 add_user_lists(ac, av, user)
438 edit_group(1, av[0], "USER", user);
442 get_members(ac, av, group)
449 if (strcmp(av[0], "LIST")) {
450 edit_group(1, group, av[0], av[1]);
452 code = mr_query("get_end_members_of_list", 1, &av[1],
455 critical_alert("incremental",
456 "Couldn't retrieve full membership of %s: %s",
457 group, error_message(code));
463 check_user(ac, av, ustate)
468 *ustate = atoi(av[6]);
472 edit_group(op, group, type, member)
479 char buf[PR_MAXNAMELEN];
481 static char local_realm[REALM_SZ+1] = "";
483 /* The following KERBEROS code allows for the use of entities
487 krb_get_lrealm(local_realm, 1);
488 if (!strcmp(type, "KERBEROS")) {
489 p = index(member, '@');
490 if (p && !strcasecmp(p+1, local_realm))
492 } else if (strcmp(type, "USER"))
493 return; /* invalid type */
495 strcpy(buf, "system:");
497 code=pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
499 if (op==1 && code == PRIDEXIST) return; /* Already added */
501 if (code == PRNOENT) { /* Something is missing */
502 if (op==0) return; /* Already deleted */
503 if (!strcmp(type, "KERBEROS")) /* Special instances; ok */
506 /* Check whether the member being added is an active user */
507 code = moira_connect();
508 if (!code) code = mr_query("get_user_by_login", 1, &member,
509 check_user, &ustate);
511 critical_alert("incremental",
512 "Error contacting Moira server to lookup user %s: %s",
513 member, error_message(code));
516 if (!code && ustate!=1 && ustate!=2) return; /* inactive user */
519 critical_alert("incremental",
520 "Couldn't %s %s %s %s: %s",
521 op ? "add" : "remove", member,
522 op ? "to" : "from", buf,
523 error_message(code));
528 long pr_try(fn, a1, a2, a3, a4, a5, a6, a7, a8)
530 char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
533 volatile register long code;
534 register int tries = 0;
542 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
544 critical_alert("incremental", "Couldn't initialize libprot: %s",
545 error_message(code));
550 sleep(1); /* give ptserver room */
552 while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)) {
556 if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
557 else if (fn == pr_RemoveUserFromGroup)
558 strcpy(fname, "pr_RemoveUserFromGroup");
559 else if (fn == pr_CreateUser) strcpy(fname, "pr_CreateUser");
560 else if (fn == pr_CreateGroup) strcpy(fname, "pr_CreateGroup");
561 else if (fn == pr_DeleteByID) strcpy(fname, "pr_DeleteByID");
562 else if (fn == pr_ChangeEntry) strcpy(fname, "pr_ChangeEntry");
563 else if (fn == pr_SetFieldsEntry) strcpy(fname, "pr_SetFieldsEntry");
564 else if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
566 sprintf(fname, "pr_??? (0x%08x)", (long)fn);
568 com_err(whoami, code, "- %s failed (try %d @%u)", fname, tries+1, t);
570 if (++tries > 2) break; /* 3 tries */
572 if (code == UNOQUORUM) sleep(90);
575 /* Re-initialize the prdb connection */
576 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
577 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
579 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
580 error_message(code));
581 initd = 0; /* we lost */
593 for (i=0; file_exists(STOP_FILE); i++) {
595 critical_alert("incremental",
596 "AFS incremental failed (%s exists): %s",
605 static int mr_connections=0;
609 static char hostname[64];
612 if (!mr_connections++) {
613 gethostname(hostname, sizeof(hostname));
614 code = mr_connect(hostname);
615 if (!code) code = mr_auth("afs.incr");
623 if (!--mr_connections) {