]> andersk Git - moira.git/blame - incremental/afs.c
AFS libraries are now used by afs.incr (AFSDIR should be changed!!!)
[moira.git] / incremental / afs.c
CommitLineData
fb8809f4 1/* $Header$
2 *
3 * Do AFS incremental updates
4 *
60c22e5f 5 * Copyright (C) 1989,1992 by the Massachusetts Institute of Technology
fb8809f4 6 * for copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 */
9
60c22e5f 10#include <sys/types.h>
11#include <sys/file.h>
12#include <strings.h>
13
2ce085d2 14#include <moira.h>
15#include <moira_site.h>
f633445d 16
60c22e5f 17#include <afs/param.h>
18#include <afs/cellconfig.h>
19#include <afs/venus.h>
20#include <afs/ptclient.h>
21#include <afs/pterror.h>
fb8809f4 22
60c22e5f 23#define file_exists(file) (access((file), F_OK) == 0)
fb8809f4 24
25char *whoami;
60c22e5f 26char *cellname = "ATHENA.MIT.EDU";
fb8809f4 27
28main(argc, argv)
29char **argv;
30int argc;
31{
a7fdf2de 32 int beforec, afterc, i;
fb8809f4 33 char *table, **before, **after;
810d12a7 34#ifdef DEBUG
35 char buf[1024];
810d12a7 36#endif
fb8809f4 37
a7fdf2de 38 for (i = getdtablesize() - 1; i > 2; i--)
39 close(i);
40
fb8809f4 41 table = argv[1];
42 beforec = atoi(argv[2]);
43 before = &argv[4];
44 afterc = atoi(argv[3]);
45 after = &argv[4 + beforec];
46 whoami = argv[0];
47
810d12a7 48#ifdef DEBUG
49 sprintf(buf, "%s (", table);
50 for (i = 0; i < beforec; i++) {
51 if (i > 0)
52 strcat(buf, ",");
53 strcat(buf, before[i]);
54 }
55 strcat(buf, ")->(");
56 for (i = 0; i < afterc; i++) {
57 if (i > 0)
58 strcat(buf, ",");
59 strcat(buf, after[i]);
60 }
61 strcat(buf, ")\n");
62 write(1,buf,strlen(buf));
63#endif
64
fb8809f4 65 initialize_sms_error_table();
66 initialize_krb_error_table();
67
68 if (!strcmp(table, "users")) {
69 do_user(before, beforec, after, afterc);
70 } else if (!strcmp(table, "list")) {
71 do_list(before, beforec, after, afterc);
72 } else if (!strcmp(table, "members")) {
73 do_member(before, beforec, after, afterc);
74 } else if (!strcmp(table, "filesys")) {
75 do_filesys(before, beforec, after, afterc);
1f377b55 76 } else if (!strcmp(table, "quota")) {
fb8809f4 77 do_quota(before, beforec, after, afterc);
78 }
fb8809f4 79 exit(0);
80}
81
82
83do_cmd(cmd)
84char *cmd;
85{
a7fdf2de 86 int success = 0, tries = 0;
fb8809f4 87
60c22e5f 88 while (success == 0 && tries < 1) {
89 if (tries++)
90 sleep(5*60);
a7fdf2de 91 com_err(whoami, 0, "Executing command: %s", cmd);
92 if (system(cmd) == 0)
60c22e5f 93 success++;
fb8809f4 94 }
95 if (!success)
60c22e5f 96 critical_alert("incremental", "failed command: %s", cmd);
fb8809f4 97}
98
99
100do_user(before, beforec, after, afterc)
101char **before;
102int beforec;
103char **after;
104int afterc;
105{
60c22e5f 106 int astate, bstate, auid, buid, code;
107
108 auid = buid = astate = bstate = 0;
109 if (afterc > U_STATE) astate = atoi(after[U_STATE]);
110 if (beforec > U_STATE) bstate = atoi(before[U_STATE]);
111 if (afterc > U_UID) auid = atoi(after[U_UID]);
112 if (beforec > U_UID) buid = atoi(before[U_UID]);
113
114 /* We consider "half-registered" users to be active */
810d12a7 115 if (astate == 2) astate = 1;
116 if (bstate == 2) bstate = 1;
fb8809f4 117
60c22e5f 118 if (astate != 1 && bstate != 1) /* inactive user */
fb8809f4 119 return;
60c22e5f 120
121 if (astate == bstate && auid == buid &&
122 !strcmp(before[U_NAME], after[U_NAME]))
123 /* No AFS related attributes have changed */
fb8809f4 124 return;
fb8809f4 125
60c22e5f 126 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
127 if (code) {
128 critical_alert("incremental", "Couldn't initialize libprot: %s",
129 error_message(code));
130 return;
fb8809f4 131 }
60c22e5f 132
133 if (astate == bstate) {
134 /* Only a modify has to be done */
135 code = pr_ChangeEntry(before[U_NAME], after[U_NAME], auid, "");
136 if (code) {
137 critical_alert("incremental",
138 "Couldn't change user %s (id %d) to %s (id %d): %s",
139 before[U_NAME], buid, after[U_NAME], auid,
140 error_message(code));
141 }
142 return;
143 }
144 if (bstate == 1) {
145 code = pr_DeleteByID(buid);
146 if (code && code != PRNOENT) {
147 critical_alert("incremental",
148 "Couldn't delete user %s (id %d): %s",
149 before[U_NAME], buid, error_message(code));
150 }
151 return;
152 }
153 if (astate == 1) {
154 code = pr_CreateUser(after[U_NAME], &auid);
155 if (code) {
156 critical_alert("incremental",
157 "Couldn't create user %s (id %d): %s",
158 after[U_NAME], auid, error_message(code));
159 }
160 return;
fb8809f4 161 }
162}
163
164
165
166do_list(before, beforec, after, afterc)
167char **before;
168int beforec;
169char **after;
170int afterc;
171{
fb8809f4 172 int agid, bgid;
60c22e5f 173 long code, id;
174 char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN];
fb8809f4 175
60c22e5f 176 agid = bgid = 0;
fb8809f4 177 if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP]))
60c22e5f 178 bgid = atoi(before[L_GID]);
fb8809f4 179 if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP]))
60c22e5f 180 agid = atoi(after[L_GID]);
fb8809f4 181
60c22e5f 182 if (agid == 0 && bgid == 0) /* Not active groups */
183 return;
184 if (agid == bgid && !strcmp(after[L_NAME], before[L_NAME]))
185 return; /* No change */
186
187 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
188 if (code) {
189 critical_alert("incremental", "Couldn't initialize libprot: %s",
190 error_message(code));
fb8809f4 191 return;
192 }
60c22e5f 193
194 if (agid && bgid) {
195 /* Only a modify is required */
196 strcpy(g1, "system:");
197 strcpy(g2, "system:");
198 strcat(g1, before[L_NAME]);
199 strcat(g2, after[L_NAME]);
200 code = pr_ChangeEntry(g1, g2, -agid, "");
201 if (code) {
202 critical_alert("incremental",
203 "Couldn't change group %s (id %d) to %s (id %d): %s",
204 before[L_NAME], -bgid, after[L_NAME], -agid,
205 error_message(code));
206 }
fb8809f4 207 return;
208 }
60c22e5f 209 if (bgid) {
210 code = pr_DeleteByID(-bgid);
211 if (code && code != PRNOENT) {
212 critical_alert("incremental",
213 "Couldn't delete group %s (id %d): %s",
214 before[L_NAME], -bgid, error_message(code));
215 }
fb8809f4 216 return;
217 }
60c22e5f 218 if (agid) {
219 strcpy(g1, "system:");
220 strcat(g1, after[L_NAME]);
221 strcpy(g2, "system:administrators");
222 id = -agid;
223 code = pr_CreateGroup(g1, g2, &id);
224 if (code) {
225 critical_alert("incremental",
226 "Couldn't create group %s (id %d): %s",
227 after[L_NAME], -agid, error_message(code));
228 return;
229 }
230
231 /* We need to make sure the group is properly populated */
232 if (beforec < L_ACTIVE || atoi(before[L_ACTIVE]) == 0) return;
233
234 /* XXX - To be implemented */
235 critical_alert("incremental",
236 "Status change for list %s; membership may be wrong",
237 after[L_NAME]);
238 }
fb8809f4 239}
240
241
242do_member(before, beforec, after, afterc)
243char **before;
244int beforec;
245char **after;
246int afterc;
247{
60c22e5f 248 int code;
249 char *p;
250
bd70a79d 251 if ((beforec < 4 || !atoi(before[LM_END])) &&
252 (afterc < 4 || !atoi(after[LM_END])))
fb8809f4 253 return;
60c22e5f 254
255 code=pr_Initialize(1, AFSCONF_CLIENTNAME, 0);
256 if (code) {
257 critical_alert("incremental", "Couldn't initialize libprot: %s",
258 error_message(code));
fb8809f4 259 return;
260 }
60c22e5f 261
262 /* The following KERBEROS code allows for the use of entities
263 * user@foreign_cell.
264 */
265 if (afterc && !strcmp(after[LM_TYPE], "KERBEROS")) {
57bc9f26 266 p = index(after[LM_MEMBER], '@');
60c22e5f 267 if (p && !strcasecmp(p+1, cellname))
268 *p = 0;
57bc9f26 269 }
60c22e5f 270 if (beforec && !strcmp(before[LM_TYPE], "KERBEROS")) {
57bc9f26 271 p = index(before[LM_MEMBER], '@');
60c22e5f 272 if (p && !strcasecmp(p+1, cellname))
273 *p = 0;
274 }
275
276 if (afterc) {
277 if (!strcmp(after[LM_TYPE], "KERBEROS")) {
278 p = index(after[LM_MEMBER], '@');
279 if (p && !strcasecmp(p+1, cellname))
280 *p = 0;
281 } else if (strcmp(after[LM_TYPE], "USER"))
282 return; /* invalid type */
283
284 code = pr_AddToGroup(after[LM_MEMBER], after[LM_LIST]);
285 if (code) {
286 if (strcmp(after[LM_TYPE], "KERBEROS") || code != PRNOENT) {
287 critical_alert("incremental",
288 "Couldn't add %s to %s: %s",
289 after[LM_MEMBER], after[LM_LIST],
290 error_message(code));
291 return;
292 }
293 }
294 }
295
296 if (beforec) {
297 if (!strcmp(before[LM_TYPE], "KERBEROS")) {
298 p = index(before[LM_MEMBER], '@');
299 if (p && !strcasecmp(p+1, cellname))
300 *p = 0;
301 } else if (strcmp(before[LM_TYPE], "USER"))
302 return; /* invalid type */
303
304 code = pr_RemoveUserFromGroup(before[LM_MEMBER], before[LM_LIST]);
305 if (code && code != PRNOENT) {
306 critical_alert("incremental",
307 "Couldn't remove %s from %s: %s",
308 before[LM_MEMBER], before[LM_LIST],
309 error_message(code));
310 return;
311 }
57bc9f26 312 }
fb8809f4 313}
314
315
316do_filesys(before, beforec, after, afterc)
317char **before;
318int beforec;
319char **after;
320int afterc;
321{
60c22e5f 322 char cmd[1024];
323
d43ba793 324 if (beforec < FS_CREATE) {
60c22e5f 325 if (afterc < FS_CREATE || atoi(after[FS_CREATE])==0 ||
326 strcmp(after[FS_TYPE], "AFS"))
327 return;
328
d43ba793 329 /* new locker creation */
60c22e5f 330 sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s",
331 BIN_DIR, BIN_DIR, BIN_DIR,
332 after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE],
333 after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]);
d43ba793 334 do_cmd(cmd);
335 return;
f633445d 336 }
60c22e5f 337
338 /* What do we do? When do we use FS_CREATE?
339 *
340 * TYPE change: AFS->ERR, ERR->AFS: rename/unmount/remount
341 * LOCKERTYPE change: rename/remount
342 * PACK change: remount
343 * LABEL change: rename/remount
344 * Deletion: rename/unmount
345 */
fb8809f4 346}
347
348
349do_quota(before, beforec, after, afterc)
350char **before;
351int beforec;
352char **after;
353int afterc;
354{
60c22e5f 355 char cmd[1024];
fb8809f4 356
1f377b55 357 if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) ||
60c22e5f 358 strncmp("/afs/", after[Q_DIRECTORY], 5))
359 return;
1f377b55 360
60c22e5f 361 sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s",
362 BIN_DIR, BIN_DIR, BIN_DIR,
1f377b55 363 after[Q_DIRECTORY], after[Q_QUOTA]);
364 do_cmd(cmd);
365 return;
fb8809f4 366}
This page took 5.244506 seconds and 5 git commands to generate.