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 */
49 /* libprot.a routines */
50 extern long pr_Initialize();
51 extern long pr_CreateUser();
52 extern long pr_CreateGroup();
53 extern long pr_DeleteByID();
54 extern long pr_ChangeEntry();
55 extern long pr_SetFieldsEntry();
56 extern long pr_AddToGroup();
57 extern long pr_RemoveUserFromGroup();
59 static char tbl_buf[1024];
65 int beforec, afterc, i;
66 char *table, **before, **after;
68 for (i = getdtablesize() - 1; i > 2; i--)
72 beforec = atoi(argv[2]);
74 afterc = atoi(argv[3]);
75 after = &argv[4 + beforec];
80 strcpy(tbl_buf, table);
81 strcat(tbl_buf, " (");
82 for (i = 0; i < beforec; i++) {
85 strcat(tbl_buf, before[i]);
87 strcat(tbl_buf, ")->(");
88 for (i = 0; i < afterc; i++) {
91 strcat(tbl_buf, after[i]);
95 printf("%s\n", tbl_buf);
98 initialize_sms_error_table();
99 initialize_krb_error_table();
101 if (!strcmp(table, "users")) {
102 do_user(before, beforec, after, afterc);
103 } else if (!strcmp(table, "list")) {
104 do_list(before, beforec, after, afterc);
105 } else if (!strcmp(table, "members")) {
106 do_member(before, beforec, after, afterc);
107 } else if (!strcmp(table, "filesys")) {
108 do_filesys(before, beforec, after, afterc);
109 } else if (!strcmp(table, "quota")) {
110 do_quota(before, beforec, after, afterc);
116 do_user(before, beforec, after, afterc)
122 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 gethostname(hostname, sizeof(hostname));
176 code = mr_connect(hostname);
177 if (!code) code = mr_auth("afs.incr");
179 critical_alert("incremental",
180 "Error contacting Moira server to retrieve grouplist of user %s: %s",
181 after[U_NAME], error_message(code));
185 av[1] = after[U_NAME];
186 code = mr_query("get_lists_of_member", 2, av,
187 add_user_lists, after[U_NAME]);
188 if (code && code != MR_NO_MATCH)
189 critical_alert("incremental",
190 "Couldn't retrieve membership of user %s: %s",
191 after[U_NAME], error_message(code));
199 do_list(before, beforec, after, afterc)
205 register int agid, bgid;
209 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
213 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
214 bgid = atoi(before[L_GID]);
215 bhide = atoi(before[L_HIDDEN]);
217 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
218 agid = atoi(after[L_GID]);
219 ahide = atoi(after[L_HIDDEN]);
222 if (agid == 0 && bgid == 0) /* Not active groups */
226 if (strcmp(after[L_NAME], before[L_NAME])) {
227 /* Only a modify is required */
228 strcpy(g1, "system:");
229 strcpy(g2, "system:");
230 strcat(g1, before[L_NAME]);
231 strcat(g2, after[L_NAME]);
232 code = pr_try(pr_ChangeEntry, g1, g2, -agid, "");
234 critical_alert("incremental",
235 "Couldn't change group %s (id %d) to %s (id %d): %s",
236 before[L_NAME], -bgid, after[L_NAME], -agid,
237 error_message(code));
240 if (ahide != bhide) {
241 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
242 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
243 0 /*ngroups*/, 0 /*nusers*/);
245 critical_alert("incremental",
246 "Couldn't set flags of group %s: %s",
247 after[L_NAME], error_message(code));
253 code = pr_try(pr_DeleteByID, -bgid);
254 if (code && code != PRNOENT) {
255 critical_alert("incremental",
256 "Couldn't delete group %s (id %d): %s",
257 before[L_NAME], -bgid, error_message(code));
262 strcpy(g1, "system:");
263 strcat(g1, after[L_NAME]);
264 strcpy(g2, "system:administrators");
266 code = pr_try(pr_CreateGroup, g1, g2, &id);
268 critical_alert("incremental",
269 "Couldn't create group %s (id %d): %s",
270 after[L_NAME], id, error_message(code));
274 code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS,
275 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >>PRIVATE_SHIFT,
276 0 /*ngroups*/, 0 /*nusers*/);
278 critical_alert("incremental",
279 "Couldn't set flags of group %s: %s",
280 after[L_NAME], error_message(code));
284 /* We need to make sure the group is properly populated */
285 if (beforec < L_ACTIVE || atoi(before[L_ACTIVE]) == 0) return;
287 gethostname(hostname, sizeof(hostname));
288 code = mr_connect(hostname);
289 if (!code) code = mr_auth("afs.incr");
291 critical_alert("incremental",
292 "Error contacting Moira server to resolve %s: %s",
293 after[L_NAME], error_message(code));
297 av[1] = after[L_NAME];
298 get_members(2, av, after[L_NAME]);
306 do_member(before, beforec, after, afterc)
315 if ((beforec < 4 || !atoi(before[LM_END])) &&
316 (afterc < 4 || !atoi(after[LM_END])))
320 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
322 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
326 do_filesys(before, beforec, after, afterc)
333 int acreate, atype, btype;
335 if (afterc < FS_CREATE) {
338 atype = !strcmp(after[FS_TYPE], "AFS");
339 acreate = atoi(after[FS_CREATE]);
342 if (beforec < FS_CREATE) {
343 if (acreate == 0 || atype == 0) return;
345 /* new locker creation */
346 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
347 BIN_DIR, BIN_DIR, BIN_DIR,
348 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
349 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
354 btype = !strcmp(before[FS_TYPE], "AFS");
355 if (afterc < FS_CREATE) {
357 critical_alert("incremental",
358 "Cannot delete AFS filesystem %s: Operation not supported",
366 /* Are we dealing with AFS lockers (could be type ERR lockers) */
367 if (!atype && !btype)
368 if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR"))
371 /* By now, we know we are simply changing AFS filesystem attributes.
372 * Operations supported:
373 * Name change: rename/remount
374 * Path change: remount
375 * Type change: ERR<-->AFS
379 if (strcmp(before[FS_OWNER], after[FS_OWNER]) ||
380 strcmp(before[FS_OWNERS], after[FS_OWNERS]))
382 critical_alert("incremental",
383 "Cannot change ownership of filesystem %s: Operation not yet supported",
388 sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s",
389 BIN_DIR, BIN_DIR, BIN_DIR,
390 before[FS_NAME], before[FS_MACHINE], before[FS_TYPE],
391 before[FS_L_TYPE], before[FS_PACK],
392 after[FS_NAME], after[FS_MACHINE], after[FS_TYPE],
393 after[FS_L_TYPE], after[FS_PACK]);
398 do_quota(before, beforec, after, afterc)
406 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
407 strncmp("/afs/", after[Q_DIRECTORY], 5))
410 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
411 BIN_DIR, BIN_DIR, BIN_DIR,
412 after[Q_DIRECTORY], after[Q_QUOTA]);
421 int success=0, tries=0;
425 while (success == 0 && tries < 2) {
428 com_err(whoami, 0, "Executing command: %s", cmd);
429 if (system(cmd) == 0)
433 critical_alert("incremental", "failed command: %s", cmd);
437 add_user_lists(ac, av, user)
443 edit_group(1, av[0], "USER", user);
447 get_members(ac, av, group)
454 if (strcmp(av[0], "LIST")) {
455 edit_group(1, group, av[0], av[1]);
457 code = mr_query("get_end_members_of_list", 1, &av[1],
460 critical_alert("incremental",
461 "Couldn't retrieve full membership of %s: %s",
462 group, error_message(code));
468 edit_group(op, group, type, member)
475 char buf[PR_MAXNAMELEN];
477 static char local_realm[REALM_SZ+1] = "";
479 /* The following KERBEROS code allows for the use of entities
483 krb_get_lrealm(local_realm, 1);
484 if (!strcmp(type, "KERBEROS")) {
485 p = index(member, '@');
486 if (p && !strcasecmp(p+1, local_realm))
488 } else if (strcmp(type, "USER"))
489 return; /* invalid type */
491 strcpy(buf, "system:");
493 code=pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf);
495 if (op==0 && code == PRNOENT) return;
496 if (op==1 && code == PRIDEXIST) return;
497 if (strcmp(type, "KERBEROS") || code != PRNOENT) {
498 critical_alert("incremental",
499 "Couldn't %s %s %s %s: %s",
500 op ? "add" : "remove", member,
501 op ? "to" : "from", buf,
502 error_message(code));
508 long pr_try(fn, a1, a2, a3, a4, a5, a6, a7, a8)
510 char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
513 volatile register long code;
514 register int tries = 0;
522 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
524 critical_alert("incremental", "Couldn't initialize libprot: %s",
525 error_message(code));
530 sleep(1); /* give ptserver room */
532 while (code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8)) {
536 if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
537 else if (fn == pr_RemoveUserFromGroup)
538 strcpy(fname, "pr_RemoveUserFromGroup");
539 else if (fn == pr_CreateUser) strcpy(fname, "pr_CreateUser");
540 else if (fn == pr_CreateGroup) strcpy(fname, "pr_CreateGroup");
541 else if (fn == pr_DeleteByID) strcpy(fname, "pr_DeleteByID");
542 else if (fn == pr_ChangeEntry) strcpy(fname, "pr_ChangeEntry");
543 else if (fn == pr_SetFieldsEntry) strcpy(fname, "pr_SetFieldsEntry");
544 else if (fn == pr_AddToGroup) strcpy(fname, "pr_AddToGroup");
546 sprintf(fname, "pr_??? (0x%08x)", (long)fn);
548 com_err(whoami, code, "- %s failed (try %d @%u)", fname, tries+1, t);
550 if (++tries > 2) break; /* 3 tries */
552 if (code == UNOQUORUM) sleep(90);
555 /* Re-initialize the prdb connection */
556 code=pr_Initialize(0, AFSCONF_CLIENTNAME, 0);
557 if (!code) code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
559 critical_alert("incremental", "Couldn't re-initialize libprot: %s",
560 error_message(code));
561 initd = 0; /* we lost */
573 for (i=0; file_exists(STOP_FILE); i++) {
575 critical_alert("incremental",
576 "AFS incremental failed (%s exists): %s",