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
10 #include <sys/types.h>
16 #include <moira_site.h>
18 #include <afs/param.h>
19 #include <afs/cellconfig.h>
20 #include <afs/venus.h>
21 #include <afs/ptclient.h>
22 #include <afs/pterror.h>
24 #define STOP_FILE "/moira/afs/noafs"
26 #define file_exists(file) (access((file), F_OK) == 0)
34 int beforec, afterc, i;
35 char *table, **before, **after;
38 for (i = getdtablesize() - 1; i > 2; i--)
42 beforec = atoi(argv[2]);
44 afterc = atoi(argv[3]);
45 after = &argv[4 + beforec];
50 for (i = 0; i < beforec; i++) {
53 strcat(buf, before[i]);
56 for (i = 0; i < afterc; i++) {
59 strcat(buf, after[i]);
65 initialize_sms_error_table();
66 initialize_krb_error_table();
68 for (i=0; file_exists(STOP_FILE); i++) {
70 critical_alert("incremental",
71 "AFS incremental failed (%s exists): %s",
78 if (!strcmp(table, "users")) {
79 do_user(before, beforec, after, afterc);
80 } else if (!strcmp(table, "list")) {
81 do_list(before, beforec, after, afterc);
82 } else if (!strcmp(table, "members")) {
83 do_member(before, beforec, after, afterc);
84 } else if (!strcmp(table, "filesys")) {
85 do_filesys(before, beforec, after, afterc);
86 } else if (!strcmp(table, "quota")) {
87 do_quota(before, beforec, after, afterc);
96 int success = 0, tries = 0;
98 while (success == 0 && tries < 1) {
101 com_err(whoami, 0, "Executing command: %s", cmd);
102 if (system(cmd) == 0)
106 critical_alert("incremental", "failed command: %s", cmd);
110 do_user(before, beforec, after, afterc)
116 int astate, bstate, auid, buid, code;
118 auid = buid = astate = bstate = 0;
119 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
120 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
121 if (afterc > U_UID) auid = atoi(after[U_UID]);
122 if (beforec > U_UID) buid = atoi(before[U_UID]);
124 /* We consider "half-registered" users to be active */
125 if (astate == 2) astate = 1;
126 if (bstate == 2) bstate = 1;
128 if (astate != 1 && bstate != 1) /* inactive user */
131 if (astate == bstate && auid == buid &&
132 !strcmp(before[U_NAME], after[U_NAME]))
133 /* No AFS related attributes have changed */
136 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
138 critical_alert("incremental", "Couldn't initialize libprot: %s",
139 error_message(code));
143 if (astate == bstate) {
144 /* Only a modify has to be done */
145 code = pr_ChangeEntry(before[U_NAME], after[U_NAME],
146 (auid==buid) ? 0 : 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_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_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));
177 do_list(before, beforec, after, afterc)
183 register int agid, bgid;
187 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
191 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) {
192 bgid = atoi(before[L_GID]);
193 bhide = atoi(before[L_HIDDEN]);
195 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) {
196 agid = atoi(after[L_GID]);
197 ahide = atoi(after[L_HIDDEN]);
200 if (agid == 0 && bgid == 0) /* Not active groups */
203 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
205 critical_alert("incremental", "Couldn't initialize libprot: %s",
206 error_message(code));
211 if (strcmp(after[L_NAME], before[L_NAME])) {
212 /* Only a modify is required */
213 strcpy(g1, "system:");
214 strcpy(g2, "system:");
215 strcat(g1, before[L_NAME]);
216 strcat(g2, after[L_NAME]);
217 code = pr_ChangeEntry(g1, g2, (agid==bgid) ? 0 : -agid, "");
219 critical_alert("incremental",
220 "Couldn't change group %s (id %d) to %s (id %d): %s",
221 before[L_NAME], -bgid, after[L_NAME], -agid,
222 error_message(code));
225 if (ahide != bhide) {
226 code = pr_SetFieldsEntry
227 (-agid, PR_SF_ALLBITS,
228 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >> PRIVATE_SHIFT,
229 0 /*ngroups*/, 0 /*nusers*/);
231 critical_alert("incremental",
232 "Couldn't set flags of group %s: %s",
233 after[L_NAME], error_message(code));
239 code = pr_DeleteByID(-bgid);
240 if (code && code != PRNOENT) {
241 critical_alert("incremental",
242 "Couldn't delete group %s (id %d): %s",
243 before[L_NAME], -bgid, error_message(code));
248 strcpy(g1, "system:");
249 strcat(g1, after[L_NAME]);
250 strcpy(g2, "system:administrators");
252 code = pr_CreateGroup(g1, g2, &id);
254 critical_alert("incremental",
255 "Couldn't create group %s (id %d): %s",
256 after[L_NAME], id, error_message(code));
260 code = pr_SetFieldsEntry
261 (-agid, PR_SF_ALLBITS,
262 (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >> PRIVATE_SHIFT,
263 0 /*ngroups*/, 0 /*nusers*/);
265 critical_alert("incremental",
266 "Couldn't set flags of group %s: %s",
267 after[L_NAME], error_message(code));
271 /* We need to make sure the group is properly populated */
272 if (beforec < L_ACTIVE || atoi(before[L_ACTIVE]) == 0) return;
274 gethostname(hostname, sizeof(hostname));
275 code = mr_connect(hostname);
276 if (!code) code = mr_auth("afs.incr");
278 critical_alert("incremental",
279 "Error contacting Moira server to resolve %s: %s",
280 after[L_NAME], error_message(code));
284 av[1] = after[L_NAME];
285 get_members(2, av, after[L_NAME]);
293 do_member(before, beforec, after, afterc)
302 if ((beforec < 4 || !atoi(before[LM_END])) &&
303 (afterc < 4 || !atoi(after[LM_END])))
306 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
308 critical_alert("incremental", "Couldn't initialize libprot: %s",
309 error_message(code));
314 edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]);
316 edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]);
320 get_members(ac, av, group)
327 if (strcmp(av[0], "LIST")) {
328 sleep(1); /* give the ptserver room */
329 edit_group(1, group, av[0], av[1]);
331 code = mr_query("get_end_members_of_list", 1, &av[1],
334 critical_alert("incremental",
335 "Couldn't retrieve full membership of %s: %s",
336 group, error_message(code));
342 edit_group(op, group, type, member)
349 char buf[PR_MAXNAMELEN];
352 static char local_realm[REALM_SZ+1] = "";
353 extern long pr_AddToGroup(), pr_RemoveUserFromGroup();
355 fn = op ? pr_AddToGroup : pr_RemoveUserFromGroup;
357 /* The following KERBEROS code allows for the use of entities
361 krb_get_lrealm(local_realm, 1);
362 if (!strcmp(type, "KERBEROS")) {
363 p = index(member, '@');
364 if (p && !strcasecmp(p+1, local_realm))
366 } else if (strcmp(type, "USER"))
367 return; /* invalid type */
369 strcpy(buf, "system:");
371 code = (*fn)(member, buf);
373 if (op==0 && code == PRNOENT) return;
374 if (op==1 && code == PRIDEXIST) return;
375 if (strcmp(type, "KERBEROS") || code != PRNOENT) {
376 critical_alert("incremental",
377 "Couldn't %s %s %s %s: %s",
378 op ? "add" : "remove", member,
379 op ? "to" : "from", buf,
380 error_message(code));
387 do_filesys(before, beforec, after, afterc)
395 if (beforec < FS_CREATE) {
396 if (afterc < FS_CREATE || atoi(after[FS_CREATE])==0 ||
397 strcmp(after[FS_TYPE], "AFS"))
400 /* new locker creation */
401 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
402 BIN_DIR, BIN_DIR, BIN_DIR,
403 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
404 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
409 /* What do we do? When do we use FS_CREATE?
411 * TYPE change: AFS->ERR, ERR->AFS: rename/unmount/remount
412 * LOCKERTYPE change: rename/remount
413 * PACK change: remount
414 * LABEL change: rename/remount
415 * Deletion: rename/unmount
417 if (afterc < FS_CREATE) {
418 if (!strcmp(before[FS_TYPE], "AFS"))
419 critical_alert("incremental",
420 "Could not delete AFS filesystem %s: Operation not supported\n",
425 if (!strcmp(after[FS_TYPE], "AFS")) {
426 if (strcmp(before[FS_TYPE], "AFS")) {
427 critical_alert("incremental",
428 "Cannot convert %s to an AFS filesystem: Operation not supported\n",
431 critical_alert("incremental",
432 "Cannot change attributes of AFS filesystem %s: Operation not supported\n",
440 do_quota(before, beforec, after, afterc)
448 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
449 strncmp("/afs/", after[Q_DIRECTORY], 5))
452 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
453 BIN_DIR, BIN_DIR, BIN_DIR,
454 after[Q_DIRECTORY], after[Q_QUOTA]);