]>
Commit | Line | Data |
---|---|---|
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 | ||
dfaf9b68 | 10 | #include <moira.h> |
11 | #include <moira_site.h> | |
12 | #include <moira_schema.h> | |
13 | ||
16838ad1 | 14 | #include <stdio.h> |
5eaef520 | 15 | #include <stdlib.h> |
7ac48069 | 16 | #include <sys/resource.h> |
60c22e5f | 17 | #include <sys/types.h> |
7ac48069 | 18 | #include <sys/utsname.h> |
60c22e5f | 19 | #include <sys/file.h> |
f19e3bf8 | 20 | #include <string.h> |
e2666aac | 21 | #include <unistd.h> |
60c22e5f | 22 | |
7ac48069 | 23 | #include <com_err.h> |
d1be2e3a | 24 | #include <krb.h> |
f633445d | 25 | |
60c22e5f | 26 | #include <afs/param.h> |
27 | #include <afs/cellconfig.h> | |
28 | #include <afs/venus.h> | |
29 | #include <afs/ptclient.h> | |
30 | #include <afs/pterror.h> | |
fb8809f4 | 31 | |
df2f7fb7 | 32 | /* Cheesy test for determining AFS more recent than 3.4a */ |
33 | #ifndef AFSCONF_CLIENTNAME | |
34 | #include <afs/dirpath.h> | |
35 | #define AFSCONF_CLIENTNAME AFSDIR_CLIENT_ETC_DIRPATH | |
36 | #endif | |
37 | ||
d1be2e3a | 38 | #define STOP_FILE "/moira/afs/noafs" |
60c22e5f | 39 | #define file_exists(file) (access((file), F_OK) == 0) |
fb8809f4 | 40 | |
41 | char *whoami; | |
42 | ||
eb63ae5f | 43 | /* Main stub routines */ |
7ac48069 | 44 | void do_user(char **before, int beforec, char **after, int afterc); |
45 | void do_list(char **before, int beforec, char **after, int afterc); | |
46 | void do_member(char **before, int beforec, char **after, int afterc); | |
47 | void do_filesys(char **before, int beforec, char **after, int afterc); | |
48 | void do_quota(char **before, int beforec, char **after, int afterc); | |
eb63ae5f | 49 | |
50 | /* Support stub routines */ | |
7ac48069 | 51 | void run_cmd(char *cmd); |
52 | int add_user_lists(int ac, char **av, void *user); | |
53 | int add_list_members(int ac, char **av, void *group); | |
54 | int check_user(int ac, char **av, void *ustate); | |
55 | void edit_group(int op, char *group, char *type, char *member); | |
e2666aac | 56 | long pr_try(); |
7ac48069 | 57 | void check_afs(void); |
58 | int moira_connect(void); | |
59 | int moira_disconnect(void); | |
eb63ae5f | 60 | |
61 | /* libprot.a routines */ | |
62 | extern long pr_Initialize(); | |
63 | extern long pr_CreateUser(); | |
64 | extern long pr_CreateGroup(); | |
65 | extern long pr_DeleteByID(); | |
66 | extern long pr_ChangeEntry(); | |
67 | extern long pr_SetFieldsEntry(); | |
68 | extern long pr_AddToGroup(); | |
69 | extern long pr_RemoveUserFromGroup(); | |
0beb8644 | 70 | extern long pr_SIdToName(); |
eb63ae5f | 71 | |
72 | static char tbl_buf[1024]; | |
2cb1f02c | 73 | static struct member { |
5eaef520 | 74 | int op; |
dfaf9b68 | 75 | char list[LIST_NAME_SIZE]; |
76 | char type[IMEMBERS_MEMBER_TYPE_SIZE]; | |
77 | char member[MAX_FIELD_WIDTH]; | |
5eaef520 | 78 | struct member *next; |
79 | } *member_head = NULL; | |
80 | ||
81 | static int mr_connections = 0; | |
82 | ||
83 | int main(int argc, char **argv) | |
fb8809f4 | 84 | { |
5eaef520 | 85 | int beforec, afterc, i; |
86 | char *table, **before, **after; | |
7ac48069 | 87 | struct rlimit rl; |
fb8809f4 | 88 | |
7ac48069 | 89 | getrlimit(RLIMIT_NOFILE, &rl); |
90 | for (i = rl.rlim_cur; i > 2; i--) | |
5eaef520 | 91 | close(i); |
a7fdf2de | 92 | |
5eaef520 | 93 | whoami = ((whoami = strrchr(argv[0], '/')) ? whoami+1 : argv[0]); |
d4ea75d6 | 94 | |
5eaef520 | 95 | table = argv[1]; |
96 | beforec = atoi(argv[2]); | |
97 | before = &argv[4]; | |
98 | afterc = atoi(argv[3]); | |
99 | after = &argv[4 + beforec]; | |
f10e3ca2 | 100 | |
5eaef520 | 101 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ); |
eb63ae5f | 102 | |
5eaef520 | 103 | strcpy(tbl_buf, table); |
104 | strcat(tbl_buf, " ("); | |
105 | for (i = 0; i < beforec; i++) | |
106 | { | |
107 | if (i > 0) | |
108 | strcat(tbl_buf, ","); | |
109 | strcat(tbl_buf, before[i]); | |
810d12a7 | 110 | } |
5eaef520 | 111 | strcat(tbl_buf, ")->("); |
112 | for (i = 0; i < afterc; i++) | |
113 | { | |
114 | if (i > 0) | |
115 | strcat(tbl_buf, ","); | |
116 | strcat(tbl_buf, after[i]); | |
fb8809f4 | 117 | } |
5eaef520 | 118 | strcat(tbl_buf, ")"); |
119 | ||
120 | initialize_sms_error_table(); | |
121 | initialize_krb_error_table(); | |
122 | ||
123 | if (!strcmp(table, "users")) | |
124 | do_user(before, beforec, after, afterc); | |
125 | else if (!strcmp(table, "list")) | |
126 | do_list(before, beforec, after, afterc); | |
127 | else if (!strcmp(table, "imembers")) | |
128 | do_member(before, beforec, after, afterc); | |
129 | else if (!strcmp(table, "filesys")) | |
130 | do_filesys(before, beforec, after, afterc); | |
131 | else if (!strcmp(table, "quota")) | |
132 | do_quota(before, beforec, after, afterc); | |
133 | ||
134 | exit(0); | |
fb8809f4 | 135 | } |
136 | ||
137 | ||
7ac48069 | 138 | void do_user(char **before, int beforec, char **after, int afterc) |
fb8809f4 | 139 | { |
5eaef520 | 140 | int astate, bstate, auid, buid, code; |
141 | char *av[2]; | |
142 | ||
143 | auid = buid = astate = bstate = 0; | |
144 | if (afterc > U_STATE) | |
145 | astate = atoi(after[U_STATE]); | |
146 | if (beforec > U_STATE) | |
147 | bstate = atoi(before[U_STATE]); | |
148 | if (afterc > U_UID) | |
149 | auid = atoi(after[U_UID]); | |
150 | if (beforec > U_UID) | |
151 | buid = atoi(before[U_UID]); | |
152 | ||
153 | /* We consider "half-registered" users to be active */ | |
154 | if (astate == 2) | |
155 | astate = 1; | |
156 | if (bstate == 2) | |
157 | bstate = 1; | |
158 | ||
159 | if (astate != 1 && bstate != 1) /* inactive user */ | |
160 | return; | |
60c22e5f | 161 | |
5eaef520 | 162 | if (astate == bstate && auid == buid && |
163 | !strcmp(before[U_NAME], after[U_NAME])) | |
164 | /* No AFS related attributes have changed */ | |
165 | return; | |
fb8809f4 | 166 | |
5eaef520 | 167 | if (astate == bstate) |
168 | { | |
169 | /* Only a modify has to be done */ | |
170 | com_err(whoami, 0, "Changing user %s (uid %d) to %s (uid %d)", | |
171 | before[U_NAME], buid, after[U_NAME], auid); | |
172 | ||
173 | code = pr_try(pr_ChangeEntry, before[U_NAME], after[U_NAME], auid, ""); | |
174 | if (code) | |
175 | { | |
176 | critical_alert("incremental", | |
177 | "Couldn't change user %s (id %d) to %s (id %d): %s", | |
178 | before[U_NAME], buid, after[U_NAME], auid, | |
179 | error_message(code)); | |
60c22e5f | 180 | } |
5eaef520 | 181 | return; |
60c22e5f | 182 | } |
5eaef520 | 183 | if (bstate == 1) |
184 | { | |
185 | com_err(whoami, 0, "Deleting user %s (uid %d)", | |
186 | before[U_NAME], buid); | |
187 | ||
188 | code = pr_try(pr_DeleteByID, buid); | |
189 | if (code && code != PRNOENT) | |
190 | { | |
191 | critical_alert("incremental", "Couldn't delete user %s (id %d): %s", | |
192 | before[U_NAME], buid, error_message(code)); | |
60c22e5f | 193 | } |
5eaef520 | 194 | return; |
60c22e5f | 195 | } |
5eaef520 | 196 | if (astate == 1) |
197 | { | |
198 | com_err(whoami, 0, "%s user %s (uid %d)", | |
199 | ((bstate != 0) ? "Reactivating" : "Creating"), | |
200 | after[U_NAME], auid); | |
201 | ||
202 | code = pr_try(pr_CreateUser, after[U_NAME], &auid); | |
203 | /* if we get PRIDEXIST, it's only an error if the username | |
204 | doesn't match (otherwise it just means the user was deleted | |
205 | from Moira but not AFS */ | |
206 | if (code == PRIDEXIST) | |
207 | { | |
dfaf9b68 | 208 | char ename[PR_MAXNAMELEN]; |
5eaef520 | 209 | |
210 | if (pr_try(pr_SIdToName, auid, ename) == 0 && | |
211 | !strcmp(after[U_NAME], ename)) | |
96786144 | 212 | return; |
60c22e5f | 213 | } |
5eaef520 | 214 | if (code) |
215 | { | |
216 | critical_alert("incremental", "Couldn't create user %s (id %d): %s", | |
217 | after[U_NAME], auid, error_message(code)); | |
218 | return; | |
219 | } | |
2dbcb756 | 220 | |
5eaef520 | 221 | if (bstate != 0) |
222 | { | |
223 | /* Reactivating a user; get his group list */ | |
224 | code = moira_connect(); | |
225 | if (code) | |
226 | { | |
227 | critical_alert("incremental", "Error contacting Moira server " | |
228 | "to retrieve grouplist of user %s: %s", | |
229 | after[U_NAME], error_message(code)); | |
230 | return; | |
231 | } | |
232 | av[0] = "ruser"; | |
233 | av[1] = after[U_NAME]; | |
234 | code = mr_query("get_lists_of_member", 2, av, add_user_lists, | |
235 | after[U_NAME]); | |
236 | if (code && code != MR_NO_MATCH) | |
237 | { | |
238 | critical_alert("incremental", | |
239 | "Couldn't retrieve membership of user %s: %s", | |
240 | after[U_NAME], error_message(code)); | |
9b273918 | 241 | } |
5eaef520 | 242 | moira_disconnect(); |
9b273918 | 243 | } |
5eaef520 | 244 | return; |
fb8809f4 | 245 | } |
246 | } | |
247 | ||
248 | ||
7ac48069 | 249 | void do_list(char **before, int beforec, char **after, int afterc) |
fb8809f4 | 250 | { |
44d12d58 | 251 | int agid, bgid; |
5eaef520 | 252 | int ahide, bhide; |
253 | long code, id; | |
254 | char g1[PR_MAXNAMELEN], g2[PR_MAXNAMELEN]; | |
255 | char *av[2]; | |
256 | ||
257 | agid = bgid = 0; | |
258 | if (beforec > L_GID && atoi(before[L_ACTIVE]) && atoi(before[L_GROUP])) | |
259 | { | |
260 | bgid = atoi(before[L_GID]); | |
261 | bhide = atoi(before[L_HIDDEN]); | |
8ec23c21 | 262 | } |
5eaef520 | 263 | if (afterc > L_GID && atoi(after[L_ACTIVE]) && atoi(after[L_GROUP])) |
264 | { | |
265 | agid = atoi(after[L_GID]); | |
266 | ahide = atoi(after[L_HIDDEN]); | |
8ec23c21 | 267 | } |
fb8809f4 | 268 | |
5eaef520 | 269 | if (agid == 0 && bgid == 0) /* Not active groups */ |
270 | return; | |
60c22e5f | 271 | |
5eaef520 | 272 | if (agid && bgid) |
273 | { | |
274 | if (strcmp(after[L_NAME], before[L_NAME])) | |
275 | { | |
276 | /* Only a modify is required */ | |
277 | strcpy(g1, "system:"); | |
278 | strcpy(g2, "system:"); | |
279 | strcat(g1, before[L_NAME]); | |
280 | strcat(g2, after[L_NAME]); | |
281 | ||
282 | com_err(whoami, 0, "Changing group %s (gid %d) to %s (gid %d)", | |
283 | before[L_NAME], bgid, after[L_NAME], agid); | |
284 | ||
285 | code = pr_try(pr_ChangeEntry, g1, g2, -agid, ""); | |
286 | if (code) | |
287 | { | |
288 | critical_alert("incremental", "Couldn't change group %s (id %d) " | |
289 | "to %s (id %d): %s", before[L_NAME], -bgid, | |
290 | after[L_NAME], -agid, error_message(code)); | |
8ec23c21 | 291 | } |
292 | } | |
5eaef520 | 293 | if (ahide != bhide) |
294 | { | |
295 | com_err(whoami, 0, "Making group %s (gid %d) %s", after[L_NAME], | |
296 | agid, (ahide ? "hidden" : "visible")); | |
297 | code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS, | |
298 | (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >> | |
299 | PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/); | |
300 | if (code) | |
301 | { | |
302 | critical_alert("incremental", | |
303 | "Couldn't set flags of group %s: %s", | |
304 | after[L_NAME], error_message(code)); | |
8ec23c21 | 305 | } |
60c22e5f | 306 | } |
5eaef520 | 307 | return; |
fb8809f4 | 308 | } |
5eaef520 | 309 | if (bgid) |
310 | { | |
311 | com_err(whoami, 0, "Deleting group %s (gid %d)", before[L_NAME], bgid); | |
312 | code = pr_try(pr_DeleteByID, -bgid); | |
313 | if (code && code != PRNOENT) | |
314 | { | |
315 | critical_alert("incremental", | |
316 | "Couldn't delete group %s (id %d): %s", | |
317 | before[L_NAME], -bgid, error_message(code)); | |
60c22e5f | 318 | } |
5eaef520 | 319 | return; |
fb8809f4 | 320 | } |
5eaef520 | 321 | if (agid) |
322 | { | |
323 | strcpy(g1, "system:"); | |
324 | strcat(g1, after[L_NAME]); | |
325 | strcpy(g2, "system:administrators"); | |
326 | id = -agid; | |
327 | com_err(whoami, 0, "Creating %s group %s (gid %d)", | |
328 | (ahide ? "hidden" : "visible"), after[L_NAME], agid); | |
329 | code = pr_try(pr_CreateGroup, g1, g2, &id); | |
330 | if (code == PRIDEXIST) | |
331 | { | |
dfaf9b68 | 332 | char ename[PR_MAXNAMELEN]; |
5eaef520 | 333 | |
334 | if (pr_try(pr_SIdToName, -agid, ename) == 0 && !strcmp(g1, ename)) | |
60c22e5f | 335 | return; |
336 | } | |
5eaef520 | 337 | if (code) |
338 | { | |
339 | critical_alert("incremental", "Couldn't create group %s (id %d): %s", | |
340 | after[L_NAME], id, error_message(code)); | |
341 | return; | |
342 | } | |
343 | if (ahide) | |
344 | { | |
345 | code = pr_try(pr_SetFieldsEntry, -agid, PR_SF_ALLBITS, | |
346 | (ahide ? PRP_STATUS_ANY : PRP_GROUP_DEFAULT) >> | |
347 | PRIVATE_SHIFT, 0 /*ngroups*/, 0 /*nusers*/); | |
348 | if (code) | |
349 | { | |
350 | critical_alert("incremental", | |
351 | "Couldn't set flags of group %s: %s", | |
352 | after[L_NAME], error_message(code)); | |
8ec23c21 | 353 | } |
354 | } | |
60c22e5f | 355 | |
5eaef520 | 356 | /* We need to make sure the group is properly populated */ |
357 | if (beforec < L_ACTIVE) | |
358 | return; | |
60c22e5f | 359 | |
5eaef520 | 360 | code = moira_connect(); |
361 | if (code) | |
362 | { | |
363 | critical_alert("incremental", | |
364 | "Error contacting Moira server to resolve %s: %s", | |
365 | after[L_NAME], error_message(code)); | |
366 | return; | |
ed9a5300 | 367 | } |
5eaef520 | 368 | av[0] = after[L_NAME]; |
369 | code = mr_query("get_end_members_of_list", 1, av, | |
370 | add_list_members, after[L_NAME]); | |
371 | if (code) | |
372 | { | |
373 | critical_alert("incremental", | |
374 | "Couldn't retrieve full membership of list %s: %s", | |
375 | after[L_NAME], error_message(code)); | |
376 | } | |
377 | moira_disconnect(); | |
378 | return; | |
60c22e5f | 379 | } |
fb8809f4 | 380 | } |
381 | ||
382 | ||
5eaef520 | 383 | |
f2cf09f5 | 384 | #define LM_EXTRA_ACTIVE (LM_END) |
385 | #define LM_EXTRA_PUBLIC (LM_END+1) | |
386 | #define LM_EXTRA_HIDDEN (LM_END+2) | |
387 | #define LM_EXTRA_MAILLIST (LM_END+3) | |
388 | #define LM_EXTRA_GROUP (LM_END+4) | |
389 | #define LM_EXTRA_GID (LM_END+5) | |
390 | #define LM_EXTRA_END (LM_END+6) | |
391 | ||
7ac48069 | 392 | void do_member(char **before, int beforec, char **after, int afterc) |
fb8809f4 | 393 | { |
5eaef520 | 394 | if (afterc) |
395 | { | |
396 | if (afterc < LM_EXTRA_END) | |
397 | return; | |
398 | else | |
399 | { | |
a859d2ed | 400 | if (!atoi(after[LM_EXTRA_ACTIVE]) || !atoi(after[LM_EXTRA_GROUP])) |
f2cf09f5 | 401 | return; |
5eaef520 | 402 | } |
403 | ||
404 | edit_group(1, after[LM_LIST], after[LM_TYPE], after[LM_MEMBER]); | |
405 | } | |
406 | else if (beforec) | |
407 | { | |
408 | if (beforec < LM_EXTRA_END) | |
409 | return; | |
410 | else | |
411 | { | |
a859d2ed | 412 | if (!atoi(before[LM_EXTRA_ACTIVE]) || !atoi(before[LM_EXTRA_GROUP])) |
f2cf09f5 | 413 | return; |
5eaef520 | 414 | } |
415 | edit_group(0, before[LM_LIST], before[LM_TYPE], before[LM_MEMBER]); | |
f2cf09f5 | 416 | } |
ed9a5300 | 417 | } |
418 | ||
419 | ||
7ac48069 | 420 | void do_filesys(char **before, int beforec, char **after, int afterc) |
eb63ae5f | 421 | { |
5eaef520 | 422 | char cmd[1024]; |
423 | int acreate, atype, bcreate, btype; | |
89e513d9 | 424 | |
5eaef520 | 425 | if (afterc < FS_CREATE) |
426 | atype = acreate = 0; | |
427 | else | |
428 | { | |
429 | atype = !strcmp(after[FS_TYPE], "AFS"); | |
430 | acreate = atoi(after[FS_CREATE]); | |
431 | } | |
eb63ae5f | 432 | |
5eaef520 | 433 | if (beforec < FS_CREATE) |
434 | { | |
435 | if (acreate == 0 || atype == 0) | |
eb63ae5f | 436 | return; |
5eaef520 | 437 | |
438 | /* new locker creation */ | |
439 | sprintf(cmd, "%s/perl -I%s %s/afs_create.pl %s %s %s %s %s %s", | |
440 | BIN_DIR, BIN_DIR, BIN_DIR, | |
441 | after[FS_NAME], after[FS_L_TYPE], after[FS_MACHINE], | |
442 | after[FS_PACK], after[FS_OWNER], after[FS_OWNERS]); | |
443 | run_cmd(cmd); | |
444 | return; | |
eb63ae5f | 445 | } |
5eaef520 | 446 | |
447 | btype = !strcmp(before[FS_TYPE], "AFS"); | |
448 | bcreate = atoi(before[FS_CREATE]); | |
449 | if (afterc < FS_CREATE) | |
450 | { | |
451 | if (btype && bcreate) | |
452 | critical_alert("incremental", "Cannot delete AFS filesystem %s: " | |
453 | "Operation not supported", before[FS_NAME]); | |
454 | return; | |
eb63ae5f | 455 | } |
3b1e9c5f | 456 | |
5eaef520 | 457 | if (!acreate) |
458 | return; | |
459 | ||
460 | /* Are we dealing with AFS lockers (could be type ERR lockers) */ | |
461 | if (!atype && !btype) | |
462 | { | |
463 | if (strcmp(before[FS_TYPE], "ERR") || strcmp(after[FS_TYPE], "ERR")) | |
3b1e9c5f | 464 | return; |
5eaef520 | 465 | } |
3b1e9c5f | 466 | |
5eaef520 | 467 | /* By now, we know we are simply changing AFS filesystem attributes. |
468 | * Operations supported: | |
469 | * Name change: rename/remount | |
470 | * Path change: remount | |
471 | * Type change: ERR<-->AFS | |
472 | */ | |
0f82335a | 473 | |
474 | #if 0 | |
5eaef520 | 475 | if (strcmp(before[FS_OWNER], after[FS_OWNER]) || |
476 | strcmp(before[FS_OWNERS], after[FS_OWNERS])) | |
3b1e9c5f | 477 | { |
5eaef520 | 478 | critical_alert("incremental", |
479 | "Cannot change ownership of filesystem %s: Operation not yet supported", | |
480 | after[FS_NAME]); | |
3b1e9c5f | 481 | } |
0f82335a | 482 | #endif |
3b1e9c5f | 483 | |
5eaef520 | 484 | sprintf(cmd, "%s/perl -I%s %s/afs_rename.pl %s %s %s %s %s %s %s %s %s %s", |
485 | BIN_DIR, BIN_DIR, BIN_DIR, | |
486 | before[FS_NAME], before[FS_MACHINE], before[FS_TYPE], | |
487 | before[FS_L_TYPE], before[FS_PACK], | |
488 | after[FS_NAME], after[FS_MACHINE], after[FS_TYPE], | |
489 | after[FS_L_TYPE], after[FS_PACK]); | |
490 | run_cmd(cmd); | |
eb63ae5f | 491 | } |
492 | ||
493 | ||
7ac48069 | 494 | void do_quota(char **before, int beforec, char **after, int afterc) |
eb63ae5f | 495 | { |
5eaef520 | 496 | char cmd[1024]; |
eb63ae5f | 497 | |
5eaef520 | 498 | if (afterc < Q_DIRECTORY || strcmp("ANY", after[Q_TYPE]) || |
499 | strncmp("/afs/", after[Q_DIRECTORY], 5)) | |
eb63ae5f | 500 | return; |
5eaef520 | 501 | |
502 | sprintf(cmd, "%s/perl -I%s %s/afs_quota.pl %s %s", | |
503 | BIN_DIR, BIN_DIR, BIN_DIR, | |
504 | after[Q_DIRECTORY], after[Q_QUOTA]); | |
505 | run_cmd(cmd); | |
506 | return; | |
eb63ae5f | 507 | } |
508 | ||
509 | ||
7ac48069 | 510 | void run_cmd(char *cmd) |
eb63ae5f | 511 | { |
5eaef520 | 512 | int success=0, tries=0; |
513 | ||
514 | check_afs(); | |
515 | ||
516 | while (success == 0 && tries < 2) | |
517 | { | |
518 | if (tries++) | |
519 | sleep(90); | |
520 | com_err(whoami, 0, "Executing command: %s", cmd); | |
521 | if (system(cmd) == 0) | |
522 | success++; | |
eb63ae5f | 523 | } |
5eaef520 | 524 | if (!success) |
525 | critical_alert("incremental", "failed command: %s", cmd); | |
eb63ae5f | 526 | } |
527 | ||
528 | ||
7ac48069 | 529 | int add_user_lists(int ac, char **av, void *user) |
eb63ae5f | 530 | { |
5eaef520 | 531 | if (atoi(av[L_ACTIVE]) && atoi(av[L_GROUP])) /* active group ? */ |
532 | edit_group(1, av[L_NAME], "USER", user); | |
533 | return 0; | |
eb63ae5f | 534 | } |
535 | ||
536 | ||
7ac48069 | 537 | int add_list_members(int ac, char **av, void *group) |
ed9a5300 | 538 | { |
5eaef520 | 539 | edit_group(1, group, av[0], av[1]); |
540 | return 0; | |
ed9a5300 | 541 | } |
542 | ||
7ac48069 | 543 | int check_user(int ac, char **av, void *ustate) |
c69d9f2c | 544 | { |
7ac48069 | 545 | *(int *)ustate = atoi(av[U_STATE]); |
5eaef520 | 546 | return 0; |
c69d9f2c | 547 | } |
548 | ||
549 | ||
7ac48069 | 550 | void edit_group(int op, char *group, char *type, char *member) |
ed9a5300 | 551 | { |
5eaef520 | 552 | char *p = 0; |
553 | char buf[PR_MAXNAMELEN]; | |
554 | int code, ustate; | |
555 | static char local_realm[REALM_SZ+1] = ""; | |
556 | struct member *m; | |
557 | ||
558 | /* The following KERBEROS code allows for the use of entities | |
559 | * user@foreign_cell. | |
560 | */ | |
561 | if (!local_realm[0]) | |
562 | krb_get_lrealm(local_realm, 1); | |
563 | if (!strcmp(type, "KERBEROS")) | |
564 | { | |
565 | p = strchr(member, '@'); | |
566 | if (p && !strcasecmp(p+1, local_realm)) | |
567 | *p = 0; | |
568 | } | |
569 | else if (strcmp(type, "USER")) | |
570 | return; /* invalid type */ | |
571 | ||
572 | /* Cannot risk doing another query during a callback */ | |
573 | /* We could do this simply for type USER, but eventually this may also | |
574 | * dynamically add KERBEROS types to the prdb, and we will need to do | |
575 | * a query to look up the uid of the null-instance user */ | |
576 | if (mr_connections) | |
577 | { | |
578 | m = malloc(sizeof(struct member)); | |
579 | if (!m) | |
580 | { | |
581 | critical_alert("incremental", "Out of memory"); | |
582 | exit(1); | |
2cb1f02c | 583 | } |
5eaef520 | 584 | m->op = op; |
585 | strcpy(m->list, group); | |
586 | strcpy(m->type, type); | |
587 | strcpy(m->member, member); | |
588 | m->next = member_head; | |
589 | member_head = m; | |
590 | return; | |
2cb1f02c | 591 | } |
592 | ||
5eaef520 | 593 | strcpy(buf, "system:"); |
594 | strcat(buf, group); | |
595 | com_err(whoami, 0, "%s %s %s group %s", (op ? "Adding" : "Removing"), member, | |
596 | (op ? "to" : "from"), group); | |
597 | code = pr_try(op ? pr_AddToGroup : pr_RemoveUserFromGroup, member, buf); | |
598 | if (code) | |
599 | { | |
600 | if (op==1 && code == PRIDEXIST) | |
601 | return; /* Already added */ | |
602 | ||
603 | if (code == PRNOENT) | |
604 | { /* Something is missing */ | |
605 | if (op == 0) | |
606 | return; /* Already deleted */ | |
607 | if (!strcmp(type, "KERBEROS")) /* Special instances; ok */ | |
608 | return; | |
609 | ||
610 | /* Check whether the member being added is an active user */ | |
611 | code = moira_connect(); | |
612 | if (!code) | |
613 | { | |
614 | code = mr_query("get_user_by_login", 1, &member, | |
615 | check_user, (char *) &ustate); | |
616 | } | |
617 | if (code) | |
618 | { | |
619 | critical_alert("incremental", "Error contacting Moira server " | |
620 | "to lookup user %s: %s", member, | |
621 | error_message(code)); | |
c69d9f2c | 622 | } |
6250582e | 623 | |
5eaef520 | 624 | /* We don't use moira_disconnect() |
625 | * because we may already be in the routine. | |
626 | */ | |
627 | mr_disconnect(); | |
628 | mr_connections--; | |
6250582e | 629 | |
5eaef520 | 630 | if (!code && ustate!=1 && ustate!=2) |
631 | return; /* inactive user */ | |
632 | code = PRNOENT; | |
60c22e5f | 633 | } |
c69d9f2c | 634 | |
5eaef520 | 635 | critical_alert("incremental", "Couldn't %s %s %s %s: %s", |
636 | op ? "add" : "remove", member, | |
637 | op ? "to" : "from", buf, | |
638 | error_message(code)); | |
57bc9f26 | 639 | } |
fb8809f4 | 640 | } |
641 | ||
642 | ||
5eaef520 | 643 | long pr_try(long (*fn)(), char *a1, char *a2, char *a3, char *a4, char *a5, |
644 | char *a6, char *a7, char *a8) | |
fb8809f4 | 645 | { |
5eaef520 | 646 | static int initd = 0; |
44d12d58 | 647 | long code; |
648 | int tries = 0; | |
eb63ae5f | 649 | |
5eaef520 | 650 | check_afs(); |
d4ea75d6 | 651 | |
5eaef520 | 652 | if (initd) |
653 | code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0); | |
654 | else | |
655 | { | |
656 | code = 0; | |
657 | initd = 1; | |
9b9540a1 | 658 | } |
5eaef520 | 659 | if (!code) |
660 | code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0); | |
661 | if (code) | |
662 | { | |
663 | critical_alert("incremental", "Couldn't initialize libprot: %s", | |
664 | error_message(code)); | |
7ac48069 | 665 | return code; |
7fca28a9 | 666 | } |
667 | ||
5eaef520 | 668 | sleep(1); /* give ptserver room */ |
669 | ||
7ac48069 | 670 | while ((code = (*fn)(a1, a2, a3, a4, a5, a6, a7, a8))) |
5eaef520 | 671 | { |
672 | if (++tries > 2) | |
673 | break; /* 3 tries */ | |
674 | ||
675 | if (code == UNOQUORUM) | |
676 | sleep(90); | |
ca72bfeb | 677 | else if (code == PRPERM) |
678 | system("/bin/athena/aklog"); | |
5eaef520 | 679 | else |
680 | sleep(15); | |
681 | ||
682 | /* Re-initialize the prdb connection */ | |
683 | code = pr_Initialize(0, AFSCONF_CLIENTNAME, 0); | |
684 | if (!code) | |
685 | code = pr_Initialize(1, AFSCONF_CLIENTNAME, 0); | |
686 | if (code) | |
687 | { | |
688 | critical_alert("incremental", "Couldn't re-initialize libprot: %s", | |
689 | error_message(code)); | |
690 | initd = 0; /* we lost */ | |
691 | break; | |
16838ad1 | 692 | } |
9b9540a1 | 693 | } |
5eaef520 | 694 | return code; |
fb8809f4 | 695 | } |
696 | ||
697 | ||
7ac48069 | 698 | void check_afs(void) |
fb8809f4 | 699 | { |
5eaef520 | 700 | int i; |
701 | ||
702 | for (i = 0; file_exists(STOP_FILE); i++) | |
703 | { | |
704 | if (i > 30) | |
705 | { | |
706 | critical_alert("incremental", | |
707 | "AFS incremental failed (%s exists): %s", | |
708 | STOP_FILE, tbl_buf); | |
709 | exit(1); | |
eb63ae5f | 710 | } |
5eaef520 | 711 | sleep(60); |
eb63ae5f | 712 | } |
fb8809f4 | 713 | } |
14d69e25 | 714 | |
715 | ||
5eaef520 | 716 | int moira_connect(void) |
14d69e25 | 717 | { |
5eaef520 | 718 | long code; |
719 | ||
720 | if (!mr_connections++) | |
721 | { | |
7ac48069 | 722 | struct utsname uts; |
723 | uname(&uts); | |
724 | code = mr_connect(uts.nodename); | |
5eaef520 | 725 | if (!code) |
726 | code = mr_auth("afs.incr"); | |
727 | return code; | |
14d69e25 | 728 | } |
5eaef520 | 729 | return 0; |
14d69e25 | 730 | } |
731 | ||
5eaef520 | 732 | int moira_disconnect(void) |
14d69e25 | 733 | { |
5eaef520 | 734 | struct member *m; |
735 | ||
736 | if (!--mr_connections) | |
737 | { | |
738 | mr_disconnect(); | |
7ac48069 | 739 | while ((m = member_head)) |
5eaef520 | 740 | { |
741 | edit_group(m->op, m->list, m->type, m->member); | |
742 | member_head = m->next; | |
743 | free(m); | |
2cb1f02c | 744 | } |
14d69e25 | 745 | } |
5eaef520 | 746 | return 0; |
14d69e25 | 747 | } |