]>
Commit | Line | Data |
---|---|---|
2d7360ca | 1 | /* $Header$ |
2 | * | |
d44cee72 | 3 | * Command line oriented Moira List tool. |
2d7360ca | 4 | * |
5 | * by Mark Rosenstein, September 1988. | |
6 | * | |
7 | * Copyright 1989 by the Massachusetts Institute of Technology. | |
1b6b0a57 | 8 | * |
9 | * (c) Copyright 1988 by the Massachusetts Institute of Technology. | |
10 | * For copying and distribution information, please see the file | |
11 | * <mit-copyright.h>. | |
2d7360ca | 12 | */ |
13 | ||
1b6b0a57 | 14 | /* ### Aren't there a lot of sq abstraction barrier violations here? |
15 | Do we need to improve the support for queue operations? */ | |
16 | ||
2d7360ca | 17 | #include <mit-copyright.h> |
9eb9cfb9 | 18 | #include <errno.h> |
2d7360ca | 19 | #include <stdio.h> |
20 | #include <ctype.h> | |
f071d8a7 | 21 | #include <string.h> |
8defc06b | 22 | #include <moira.h> |
23 | #include <moira_site.h> | |
2d7360ca | 24 | |
25 | #ifndef LINT | |
d44cee72 | 26 | static char blanche_rcsid[] = "$Header$"; |
2d7360ca | 27 | #endif |
28 | ||
29 | ||
30 | struct member { | |
31 | int type; | |
32 | char *name; | |
33 | }; | |
34 | ||
b5b167d6 | 35 | /* It is important to membercmp that M_USER < M_LIST < M_STRING */ |
2d7360ca | 36 | #define M_ANY 0 |
37 | #define M_USER 1 | |
38 | #define M_LIST 2 | |
39 | #define M_STRING 3 | |
8c3b414d | 40 | #define M_KERBEROS 4 |
2d7360ca | 41 | |
1b6b0a57 | 42 | /* argument parsing macro */ |
43 | #define argis(a,b) ((strcmp(*arg+1, a) == 0) || (strcmp(*arg+1, b) == 0)) | |
44 | ||
2d7360ca | 45 | /* flags from command line */ |
8b494222 | 46 | int infoflg, verbose, syncflg, memberflg, recursflg, debugflg, noauth; |
8c3b414d | 47 | int showusers, showstrings, showkerberos, showlists; |
2d7360ca | 48 | |
49 | /* various member lists */ | |
50 | struct save_queue *addlist, *dellist, *memberlist, *synclist; | |
51 | ||
52 | char *listname, *whoami; | |
53 | ||
f071d8a7 | 54 | #ifndef POSIX |
2d7360ca | 55 | extern int errno; |
f071d8a7 | 56 | #endif |
2d7360ca | 57 | |
58 | int show_list_info(), show_list_count(), get_list_members(), scream(); | |
135cd06a | 59 | int show_list_members(), membercmp(); |
2d7360ca | 60 | struct member *parse_member(); |
61 | ||
62 | ||
63 | ||
64 | main(argc, argv) | |
65 | int argc; | |
66 | char **argv; | |
67 | { | |
68 | int status; | |
69 | char **arg = argv; | |
37e3ac2d | 70 | char *membervec[3], *motd; |
2d7360ca | 71 | struct member *memberstruct; |
99bdafb8 | 72 | char *server = NULL, *p; |
2d7360ca | 73 | |
74 | /* clear all flags & lists */ | |
1b6b0a57 | 75 | infoflg = verbose = syncflg = memberflg = debugflg = recursflg = 0; |
8c3b414d | 76 | noauth = showusers = showstrings = showkerberos = showlists = 0; |
2d7360ca | 77 | listname = NULL; |
78 | addlist = sq_create(); | |
79 | dellist = sq_create(); | |
80 | memberlist = sq_create(); | |
81 | synclist = sq_create(); | |
82 | whoami = argv[0]; | |
83 | ||
84 | /* parse args, building addlist, dellist, & synclist */ | |
85 | while (++arg - argv < argc) { | |
86 | if (**arg == '-') | |
1b6b0a57 | 87 | { |
88 | if (argis("m", "members")) | |
89 | memberflg++; | |
8b494222 | 90 | else if (argis("u", "users")) |
91 | showusers++; | |
92 | else if (argis("s", "strings")) | |
93 | showstrings++; | |
94 | else if (argis("l", "lists")) | |
95 | showlists++; | |
8c3b414d | 96 | else if (argis("k", "kerberos")) |
97 | showkerberos++; | |
1b6b0a57 | 98 | else if (argis("D", "debug")) |
99 | debugflg++; | |
62a6860a | 100 | else if (argis("i","info")) |
2d7360ca | 101 | infoflg++; |
8b494222 | 102 | else if (argis("n","noauth")) |
103 | noauth++; | |
1b6b0a57 | 104 | else if (argis("v","verbose")) |
2d7360ca | 105 | verbose++; |
1b6b0a57 | 106 | else if (argis("r","recursive")) |
107 | recursflg++; | |
8c3b414d | 108 | else if (argis("S","server")) |
109 | if (arg - argv < argc - 1) { | |
110 | ++arg; | |
111 | server = *arg; | |
112 | } else | |
113 | usage(argv); | |
1b6b0a57 | 114 | else if (argis("a","add")) |
115 | if (arg - argv < argc - 1) { | |
116 | ++arg; | |
117 | if (memberstruct = parse_member(*arg)) | |
118 | sq_save_data(addlist, memberstruct); | |
119 | } else | |
120 | usage(argv); | |
7c02cbdb | 121 | else if (argis("al","addlist")) |
122 | if (arg - argv < argc - 1) { | |
123 | ++arg; | |
124 | get_members_from_file(*arg, addlist); | |
125 | } else | |
126 | usage(argv); | |
1b6b0a57 | 127 | else if (argis("d","delete")) |
128 | if (arg - argv < argc - 1) { | |
129 | ++arg; | |
130 | if (memberstruct = parse_member(*arg)) | |
131 | sq_save_data(dellist, memberstruct); | |
132 | } else | |
133 | usage(argv); | |
7c02cbdb | 134 | else if (argis("dl","deletelist")) |
135 | if (arg - argv < argc - 1) { | |
136 | ++arg; | |
137 | get_members_from_file(*arg, dellist); | |
138 | } else | |
139 | usage(argv); | |
1b6b0a57 | 140 | else if (argis("f","file")) |
141 | if (arg - argv < argc - 1) { | |
1b6b0a57 | 142 | syncflg++; |
143 | ++arg; | |
7c02cbdb | 144 | get_members_from_file(*arg, synclist); |
1b6b0a57 | 145 | } else |
b5b167d6 | 146 | usage(argv); |
1b6b0a57 | 147 | else |
2d7360ca | 148 | usage(argv); |
1b6b0a57 | 149 | } |
2d7360ca | 150 | else if (listname == NULL) |
151 | listname = *arg; | |
152 | else | |
153 | usage(argv); | |
154 | } | |
155 | if (listname == NULL) | |
156 | usage(argv); | |
157 | ||
158 | /* if no other options specified, turn on list members flag */ | |
159 | if (!(infoflg || syncflg || | |
160 | addlist->q_next != addlist || dellist->q_next != dellist)) | |
161 | memberflg++; | |
162 | ||
8c3b414d | 163 | /* If none of {users,strings,lists,kerberos} specified, turn them all on */ |
164 | if (!(showusers || showstrings || showlists || showkerberos)) | |
165 | showusers = showstrings = showlists = showkerberos = 1; | |
8b494222 | 166 | |
d44cee72 | 167 | /* fire up Moira */ |
8defc06b | 168 | if (status = mr_connect(server)) { |
d44cee72 | 169 | com_err(whoami, status, "unable to connect to the Moira server"); |
2d7360ca | 170 | exit(2); |
171 | } | |
8defc06b | 172 | if ( status = mr_motd(&motd) ) { |
d44cee72 | 173 | com_err(whoami, status, "unable to check server status"); |
37e3ac2d | 174 | exit(2); |
175 | } | |
176 | if (motd) { | |
95cd286e | 177 | fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd); |
8defc06b | 178 | mr_disconnect(); |
37e3ac2d | 179 | exit(2); |
180 | } | |
181 | ||
8defc06b | 182 | if (!noauth && (status = mr_auth("blanche"))) { |
ece49f32 | 183 | if (status == MR_USER_AUTH) |
184 | com_err(whoami, status, ""); | |
185 | else { | |
186 | com_err(whoami, status, "unable to authenticate to Moira"); | |
187 | com_err(whoami, 0, | |
188 | " Try the -noauth flag if you don't need authentication"); | |
189 | exit(2); | |
190 | } | |
2d7360ca | 191 | } |
192 | ||
193 | /* display list info if requested to */ | |
194 | if (infoflg) { | |
8defc06b | 195 | status = mr_query("get_list_info", 1, &listname, show_list_info,NULL); |
2d7360ca | 196 | if (status) |
d44cee72 | 197 | com_err(whoami, status, "while getting list information"); |
2d7360ca | 198 | if (verbose && !memberflg) { |
8defc06b | 199 | status = mr_query("count_members_of_list", 1, &listname, |
2d7360ca | 200 | show_list_count, NULL); |
201 | if (status) | |
d44cee72 | 202 | com_err(whoami, status, "while getting list count"); |
2d7360ca | 203 | } |
204 | } | |
205 | ||
206 | /* if we're synchronizing to a file, we need to: | |
207 | * get the current members of the list | |
208 | * for each member of the sync file | |
209 | * if they are on the list, remove them from the in-memory copy | |
210 | * if they're not on the list, add them to add-list | |
211 | * if anyone is left on the in-memory copy, put them on the delete-list | |
212 | * lastly, reset memberlist so we can use it again later | |
213 | */ | |
214 | if (syncflg) { | |
8defc06b | 215 | status = mr_query("get_members_of_list", 1, &listname, |
1b6b0a57 | 216 | get_list_members, (char *)memberlist); |
2d7360ca | 217 | if (status) |
d44cee72 | 218 | com_err(whoami, status, "getting members of list %s", listname); |
2d7360ca | 219 | while (sq_get_data(synclist, &memberstruct)) { |
220 | struct save_queue *q; | |
221 | int removed = 0; | |
222 | ||
223 | for (q = memberlist->q_next; q != memberlist; q = q->q_next) { | |
1b6b0a57 | 224 | if (membercmp(q->q_data, memberstruct) == 0) { |
2d7360ca | 225 | q->q_prev->q_next = q->q_next; |
226 | q->q_next->q_prev = q->q_prev; | |
227 | removed++; | |
228 | break; | |
229 | } | |
230 | } | |
231 | if (!removed) | |
232 | sq_save_data(addlist, memberstruct); | |
233 | } | |
234 | while (sq_get_data(memberlist, &memberstruct)) | |
235 | sq_save_data(dellist, memberstruct); | |
236 | sq_destroy(memberlist); | |
237 | memberlist = sq_create(); | |
238 | } | |
239 | ||
240 | /* Process the add list */ | |
241 | while (sq_get_data(addlist, &memberstruct)) { | |
99bdafb8 | 242 | /* canonicalize string if necessary */ |
243 | if (memberstruct->type == M_STRING && | |
f071d8a7 | 244 | (p = strchr(memberstruct->name, '@'))) { |
99bdafb8 | 245 | char *host = canonicalize_hostname(strsave(++p)); |
246 | static char **mailhubs = NULL; | |
247 | char *argv[4]; | |
248 | int i, collect(); | |
249 | ||
250 | if (!mailhubs) { | |
251 | argv[0] = "mailhub"; | |
252 | argv[1] = "TYPE"; | |
253 | argv[2] = "*"; | |
254 | mailhubs = (char **)malloc(sizeof(char *)); | |
255 | mailhubs[0] = NULL; | |
f416d13d | 256 | status = mr_query("get_alias", 3, argv, collect, |
257 | (char *)&mailhubs); | |
99bdafb8 | 258 | if (status != MR_SUCCESS && status != MR_NO_MATCH) { |
259 | com_err(whoami, status, | |
260 | " while reading list of MAILHUB servers"); | |
261 | mailhubs[0] = NULL; | |
262 | } | |
263 | } | |
264 | for (i = 0; p = mailhubs[i]; i++) { | |
265 | if (!strcasecmp(p, host)) { | |
266 | host = strsave(memberstruct->name); | |
f071d8a7 | 267 | *(strchr(memberstruct->name, '@')) = 0; |
99bdafb8 | 268 | memberstruct->type = M_ANY; |
269 | fprintf(stderr, "Warning: \"STRING:%s\" converted to \"%s\" because it is a local name.\n", | |
270 | host, memberstruct->name); | |
271 | break; | |
272 | } | |
273 | } | |
274 | free(host); | |
275 | } | |
276 | /* now continue adding member */ | |
2d7360ca | 277 | membervec[0] = listname; |
278 | membervec[2] = memberstruct->name; | |
97f4e1fa | 279 | if (verbose) { |
b5b167d6 | 280 | printf("Adding member "); |
281 | show_list_member(memberstruct); | |
282 | } | |
2d7360ca | 283 | switch (memberstruct->type) { |
284 | case M_ANY: | |
285 | case M_USER: | |
286 | membervec[1] = "USER"; | |
8defc06b | 287 | status = mr_query("add_member_to_list", 3, membervec, scream, NULL); |
288 | if (status == MR_SUCCESS) | |
2d7360ca | 289 | break; |
8defc06b | 290 | else if (status != MR_USER || memberstruct->type != M_ANY) { |
d44cee72 | 291 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 292 | memberstruct->name, listname); |
293 | break; | |
294 | } | |
295 | case M_LIST: | |
296 | membervec[1] = "LIST"; | |
8defc06b | 297 | status = mr_query("add_member_to_list", 3, membervec, |
2d7360ca | 298 | scream, NULL); |
8defc06b | 299 | if (status == MR_SUCCESS) |
2d7360ca | 300 | break; |
8defc06b | 301 | else if (status != MR_LIST || memberstruct->type != M_ANY) { |
d44cee72 | 302 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 303 | memberstruct->name, listname); |
304 | break; | |
305 | } | |
306 | case M_STRING: | |
307 | membervec[1] = "STRING"; | |
8defc06b | 308 | status = mr_query("add_member_to_list", 3, membervec, |
2d7360ca | 309 | scream, NULL); |
8defc06b | 310 | if (status != MR_SUCCESS) |
d44cee72 | 311 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 312 | memberstruct->name, listname); |
8c3b414d | 313 | break; |
314 | case M_KERBEROS: | |
315 | membervec[1] = "KERBEROS"; | |
8defc06b | 316 | status = mr_query("add_member_to_list", 3, membervec, |
8c3b414d | 317 | scream, NULL); |
8defc06b | 318 | if (status != MR_SUCCESS) |
d44cee72 | 319 | com_err(whoami, status, "while adding member %s to %s", |
8c3b414d | 320 | memberstruct->name, listname); |
2d7360ca | 321 | } |
322 | } | |
323 | ||
324 | /* Process the delete list */ | |
325 | while (sq_get_data(dellist, &memberstruct)) { | |
326 | membervec[0] = listname; | |
327 | membervec[2] = memberstruct->name; | |
97f4e1fa | 328 | if (verbose) { |
b5b167d6 | 329 | printf("Deleting member "); |
330 | show_list_member(memberstruct); | |
331 | } | |
2d7360ca | 332 | switch (memberstruct->type) { |
333 | case M_ANY: | |
334 | case M_USER: | |
335 | membervec[1] = "USER"; | |
8defc06b | 336 | status = mr_query("delete_member_from_list", 3, membervec, |
2d7360ca | 337 | scream, NULL); |
8defc06b | 338 | if (status == MR_SUCCESS) |
2d7360ca | 339 | break; |
8defc06b | 340 | else if ((status != MR_USER && status != MR_NO_MATCH) || |
2d7360ca | 341 | memberstruct->type != M_ANY) { |
d44cee72 | 342 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 343 | memberstruct->name, listname); |
344 | break; | |
345 | } | |
346 | case M_LIST: | |
347 | membervec[1] = "LIST"; | |
8defc06b | 348 | status = mr_query("delete_member_from_list", 3, membervec, |
2d7360ca | 349 | scream, NULL); |
8defc06b | 350 | if (status == MR_SUCCESS) |
2d7360ca | 351 | break; |
8defc06b | 352 | else if ((status != MR_LIST && status != MR_NO_MATCH) || |
2d7360ca | 353 | memberstruct->type != M_ANY) { |
40637dba | 354 | if (status == MR_PERM && memberstruct->type == M_ANY) { |
355 | /* M_ANY means we've fallen through from the user case | |
356 | * The fact that we didn't get MR_PERM there indicates | |
357 | * that we had permission to remove the specified member | |
358 | * from the list if it is a user, but not a list. This is | |
359 | * if we are the member in question. Since we exist as a user | |
360 | * we must have gotten the MR_NO_MATCH error, so we will | |
361 | * return that, since it will be less confusing. However, | |
362 | * This will generate the wrongerror if the user was trying | |
363 | * to remove the list with his/her username from a list they | |
364 | * don't administrate, without explicitly specifying "list:". | |
365 | */ | |
366 | status = MR_NO_MATCH; | |
367 | } | |
d44cee72 | 368 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 369 | memberstruct->name, listname); |
370 | break; | |
371 | } | |
372 | case M_STRING: | |
373 | membervec[1] = "STRING"; | |
8defc06b | 374 | status = mr_query("delete_member_from_list", 3, membervec, |
2d7360ca | 375 | scream, NULL); |
8defc06b | 376 | if (status == MR_STRING && memberstruct->type == M_ANY) |
d44cee72 | 377 | com_err(whoami, 0, " Unable to find member %s to delete from %s", |
2d7360ca | 378 | memberstruct->name, listname); |
8defc06b | 379 | else if (status != MR_SUCCESS) |
d44cee72 | 380 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 381 | memberstruct->name, listname); |
8c3b414d | 382 | break; |
383 | case M_KERBEROS: | |
384 | membervec[1] = "KERBEROS"; | |
8defc06b | 385 | status = mr_query("delete_member_from_list", 3, membervec, |
8c3b414d | 386 | scream, NULL); |
8defc06b | 387 | if (status != MR_SUCCESS) |
d44cee72 | 388 | com_err(whoami, status, "while deleting member %s from %s", |
8c3b414d | 389 | memberstruct->name, listname); |
2d7360ca | 390 | } |
391 | } | |
392 | ||
393 | /* Display the members of the list now, if requested */ | |
b5b167d6 | 394 | if (memberflg) { |
395 | if (recursflg) | |
396 | recursive_display_list_members(); | |
397 | else { | |
8defc06b | 398 | status = mr_query("get_members_of_list", 1, &listname, |
b5b167d6 | 399 | get_list_members, (char *)memberlist); |
400 | if (status) | |
d44cee72 | 401 | com_err(whoami, status, "while getting members of list %s", |
b5b167d6 | 402 | listname); |
403 | while (sq_get_data(memberlist, &memberstruct)) | |
404 | show_list_member(memberstruct); | |
405 | } | |
406 | } | |
2d7360ca | 407 | |
408 | /* We're done! */ | |
8defc06b | 409 | mr_disconnect(); |
2d7360ca | 410 | exit(0); |
411 | } | |
412 | ||
413 | usage(argv) | |
414 | char **argv; | |
415 | { | |
d465790c | 416 | fprintf(stderr, "Usage: %s listname [options]\n",argv[0]); |
1b6b0a57 | 417 | fprintf(stderr, "Options are\n"); |
418 | fprintf(stderr, " -v | -verbose\n"); | |
419 | fprintf(stderr, " -m | -members\n"); | |
8b494222 | 420 | fprintf(stderr, " -u | -users\n"); |
421 | fprintf(stderr, " -l | -lists\n"); | |
422 | fprintf(stderr, " -s | -strings\n"); | |
8c3b414d | 423 | fprintf(stderr, " -k | -kerberos\n"); |
1b6b0a57 | 424 | fprintf(stderr, " -i | -info\n"); |
425 | fprintf(stderr, " -r | -recursive\n"); | |
426 | fprintf(stderr, " -a | -add member\n"); | |
427 | fprintf(stderr, " -d | -delete member\n"); | |
7c02cbdb | 428 | fprintf(stderr, " -al | -addlist filename\n"); |
429 | fprintf(stderr, " -dl | -deletelist filename\n"); | |
1b6b0a57 | 430 | fprintf(stderr, " -f | -file filename\n"); |
8b494222 | 431 | fprintf(stderr, " -n | -noauth\n"); |
7c02cbdb | 432 | fprintf(stderr, " -S | -server host[:port]\n"); |
1b6b0a57 | 433 | fprintf(stderr, " -D | -debug\n"); |
2d7360ca | 434 | exit(1); |
435 | } | |
436 | ||
437 | ||
b5b167d6 | 438 | /* Display the members stored in the queue */ |
1b6b0a57 | 439 | |
b5b167d6 | 440 | show_list_member(memberstruct) |
441 | struct member *memberstruct; | |
442 | { | |
24582af9 | 443 | char *s = ""; |
8b494222 | 444 | |
445 | switch (memberstruct->type) { | |
446 | case M_USER: | |
447 | if (!showusers) | |
448 | return; | |
449 | s = "USER"; | |
450 | break; | |
451 | case M_LIST: | |
452 | if (!showlists) | |
453 | return; | |
454 | s = "LIST"; | |
455 | break; | |
456 | case M_STRING: | |
457 | if (!showstrings) | |
458 | return; | |
459 | s = "STRING"; | |
460 | break; | |
8c3b414d | 461 | case M_KERBEROS: |
462 | if (!showkerberos) | |
463 | return; | |
464 | s = "KERBEROS"; | |
465 | break; | |
8b494222 | 466 | case M_ANY: |
467 | printf("%s\n", memberstruct->name); | |
468 | return; | |
469 | } | |
470 | ||
471 | if (verbose) | |
472 | printf("%s:%s\n", s, memberstruct->name); | |
473 | else { | |
b5b167d6 | 474 | if (memberstruct->type == M_LIST) |
475 | printf("LIST:%s\n", memberstruct->name); | |
8c3b414d | 476 | else if (memberstruct->type == M_KERBEROS) |
477 | printf("KERBEROS:%s\n", memberstruct->name); | |
b5b167d6 | 478 | else if (memberstruct->type == M_STRING && |
f071d8a7 | 479 | !strchr(memberstruct->name, '@')) |
b5b167d6 | 480 | printf("STRING:%s\n", memberstruct->name); |
481 | else | |
482 | printf("%s\n", memberstruct->name); | |
1b6b0a57 | 483 | } |
484 | } | |
485 | ||
b5b167d6 | 486 | |
487 | /* Show the retrieved information about a list */ | |
488 | ||
2d7360ca | 489 | show_list_info(argc, argv, hint) |
490 | int argc; | |
491 | char **argv; | |
492 | int hint; | |
493 | { | |
494 | printf("List: %s\n", argv[0]); | |
495 | printf("Description: %s\n", argv[9]); | |
496 | printf("Flags: %s, %s, and %s\n", | |
497 | atoi(argv[1]) ? "active" : "inactive", | |
498 | atoi(argv[2]) ? "public" : "private", | |
499 | atoi(argv[3]) ? "hidden" : "visible"); | |
1b6b0a57 | 500 | printf("%s is %sa maillist and is %sa group", argv[0], |
2d7360ca | 501 | atoi(argv[4]) ? "" : "not ", |
502 | atoi(argv[5]) ? "" : "not "); | |
503 | if (atoi(argv[5])) | |
504 | printf(" with GID %d\n", atoi(argv[6])); | |
505 | else | |
506 | printf("\n"); | |
507 | printf("Owner: %s %s\n", argv[7], argv[8]); | |
508 | printf("Last modified by %s with %s on %s\n", argv[11], argv[12], argv[10]); | |
8defc06b | 509 | return(MR_CONT); |
2d7360ca | 510 | } |
511 | ||
512 | ||
b5b167d6 | 513 | /* Show the retrieve list member count */ |
514 | ||
2d7360ca | 515 | show_list_count(argc, argv, hint) |
516 | int argc; | |
517 | char **argv; | |
518 | int hint; | |
519 | { | |
520 | printf("Members: %s\n", argv[0]); | |
521 | } | |
522 | ||
523 | ||
b5b167d6 | 524 | /* Recursively find all of the members of listname, and then display them */ |
525 | ||
526 | recursive_display_list_members() | |
1b6b0a57 | 527 | { |
b5b167d6 | 528 | int status, count, savecount; |
529 | struct save_queue *lists, *members; | |
530 | struct member *m, *m1, *data; | |
531 | ||
532 | lists = sq_create(); | |
533 | members = sq_create(); | |
534 | m = (struct member *) malloc(sizeof(struct member)); | |
535 | m->type = M_LIST; | |
536 | m->name = listname; | |
537 | sq_save_data(lists, m); | |
538 | ||
539 | while (sq_get_data(lists, &m)) { | |
540 | sq_destroy(memberlist); | |
541 | memberlist = sq_create(); | |
542 | if (debugflg) | |
543 | fprintf(stderr, "Fetching members of %s\n", m->name); | |
8defc06b | 544 | status = mr_query("get_members_of_list", 1, &(m->name), |
b5b167d6 | 545 | get_list_members, (char *)memberlist); |
546 | if (status) | |
d44cee72 | 547 | com_err(whoami, status, "while getting members of list %s", m->name); |
b5b167d6 | 548 | while (sq_get_data(memberlist, &m1)) { |
549 | if (m1->type == M_LIST) | |
550 | unique_add_member(lists, m1); | |
551 | else | |
552 | unique_add_member(members, m1); | |
553 | } | |
554 | } | |
555 | savecount = count = sq_count_elts(members); | |
556 | data = (struct member *) malloc(count * sizeof(struct member)); | |
557 | count = 0; | |
558 | while (sq_get_data(members, &m)) | |
f071d8a7 | 559 | memcpy(&data[count++], m, sizeof(struct member)); |
b5b167d6 | 560 | qsort(data, count, sizeof(struct member), membercmp); |
561 | for (count = 0; count < savecount; count++) { | |
562 | show_list_member(&data[count]); | |
563 | } | |
564 | } | |
565 | ||
566 | ||
567 | /* add a struct member to a queue if that member isn't already there. */ | |
568 | ||
569 | unique_add_member(q, m) | |
570 | struct save_queue *q; | |
571 | struct member *m; | |
572 | { | |
573 | struct save_queue *qp; | |
1b6b0a57 | 574 | |
b5b167d6 | 575 | for (qp = q->q_next; qp != q; qp = qp->q_next) { |
576 | if (!membercmp(qp->q_data, m)) | |
577 | return; | |
578 | } | |
579 | sq_save_data(q, m); | |
1b6b0a57 | 580 | } |
581 | ||
b5b167d6 | 582 | |
583 | /* Collect the retrieved members of the list */ | |
584 | ||
2d7360ca | 585 | get_list_members(argc, argv, q) |
586 | int argc; | |
587 | char **argv; | |
588 | struct save_queue *q; | |
589 | { | |
590 | struct member *m; | |
591 | ||
592 | m = (struct member *) malloc(sizeof(struct member)); | |
593 | switch (argv[0][0]) { | |
594 | case 'U': | |
595 | m->type = M_USER; | |
596 | break; | |
597 | case 'L': | |
598 | m->type = M_LIST; | |
599 | break; | |
600 | case 'S': | |
601 | m->type = M_STRING; | |
602 | break; | |
8c3b414d | 603 | case 'K': |
604 | m->type = M_KERBEROS; | |
605 | break; | |
2d7360ca | 606 | } |
607 | m->name = strsave(argv[1]); | |
608 | sq_save_data(q, m); | |
8defc06b | 609 | return(MR_CONT); |
2d7360ca | 610 | } |
611 | ||
612 | ||
b5b167d6 | 613 | /* Called only if a query returns a value that we weren't expecting */ |
614 | ||
2d7360ca | 615 | scream() |
616 | { | |
617 | fprintf(stderr, "Programmer botch\n"); | |
618 | exit(3); | |
619 | } | |
620 | ||
621 | ||
7c02cbdb | 622 | /* Open file, parse members from file, and put them on the specified queue */ |
623 | get_members_from_file(filename, queue) | |
624 | char *filename; | |
625 | struct save_queue *queue; | |
626 | { | |
627 | FILE *in; | |
628 | char buf[BUFSIZ]; | |
629 | struct member *memberstruct; | |
630 | ||
631 | if (!strcmp(filename, "-")) | |
632 | in = stdin; | |
633 | else { | |
634 | in = fopen(filename, "r"); | |
635 | if (!in) { | |
636 | com_err(whoami, errno, "while opening %s for input", filename); | |
637 | exit(2); | |
638 | } | |
639 | } | |
640 | ||
641 | while (fgets(buf, BUFSIZ, in)) | |
642 | if (memberstruct = parse_member(buf)) | |
643 | sq_save_data(queue, memberstruct); | |
644 | if (!feof(in)) | |
645 | com_err(whoami, errno, "while reading from %s", filename); | |
646 | } | |
647 | ||
648 | ||
99bdafb8 | 649 | /* Collect the possible expansions of the alias MAILHUB */ |
650 | ||
651 | int collect(argc, argv, list) | |
652 | int argc; | |
653 | char **argv; | |
654 | char ***list; | |
655 | { | |
656 | int i; | |
657 | ||
658 | for (i = 0; (*list)[i]; i++); | |
659 | *list = (char **)realloc(*list, (i + 2) * sizeof(char *)); | |
660 | (*list)[i] = strsave(argv[2]); | |
661 | (*list)[i+1] = NULL; | |
662 | return(MR_CONT); | |
663 | } | |
664 | ||
665 | ||
b5b167d6 | 666 | /* Parse a line of input, fetching a member. NULL is returned if a member |
7c02cbdb | 667 | * is not found. ';' is a comment character. |
b5b167d6 | 668 | */ |
669 | ||
2d7360ca | 670 | struct member *parse_member(s) |
671 | register char *s; | |
672 | { | |
673 | register struct member *m; | |
7c02cbdb | 674 | char *p, *lastchar; |
2d7360ca | 675 | |
676 | while (*s && isspace(*s)) | |
677 | s++; | |
7c02cbdb | 678 | lastchar = p = s; |
679 | while (*p && *p != '\n' && *p != ';') | |
680 | if (isprint(*p) && !isspace(*p)) | |
681 | lastchar = p++; | |
682 | else | |
683 | p++; | |
684 | lastchar++; | |
685 | *lastchar = 0; | |
2d7360ca | 686 | if (p == s || strlen(s) == 0) |
687 | return(NULL); | |
688 | ||
689 | if ((m = (struct member *) malloc(sizeof(struct member))) == NULL) | |
690 | return(NULL); | |
691 | ||
f071d8a7 | 692 | if (p = strchr(s, ':')) { |
2d7360ca | 693 | *p = 0; |
694 | m->name = ++p; | |
695 | if (!strcasecmp("user", s)) | |
696 | m->type = M_USER; | |
697 | else if (!strcasecmp("list", s)) | |
698 | m->type = M_LIST; | |
699 | else if (!strcasecmp("string", s)) | |
700 | m->type = M_STRING; | |
8c3b414d | 701 | else if (!strcasecmp("kerberos", s)) |
702 | m->type = M_KERBEROS; | |
2d7360ca | 703 | else { |
704 | m->type = M_STRING; | |
705 | *(--p) = ':'; | |
706 | m->name = s; | |
707 | } | |
708 | m->name = strsave(m->name); | |
99bdafb8 | 709 | } else { |
710 | m->name = strsave(s); | |
f071d8a7 | 711 | if (strchr(s, '@') || strchr(s, '!') || strchr(s, '%') || strchr(s, ' ')) |
99bdafb8 | 712 | m->type = M_STRING; |
713 | else | |
714 | m->type = M_ANY; | |
2d7360ca | 715 | } |
2d7360ca | 716 | return(m); |
717 | } | |
718 | ||
719 | ||
1b6b0a57 | 720 | /* |
721 | * This routine two compares members by the following rules: | |
722 | * 1. A USER is less than a LIST | |
723 | * 2. A LIST is less than a STRING | |
724 | * 3. If two members are of the same type, the one alphabetically first | |
725 | * is less than the other | |
726 | * It returs < 0 if the first member is less, 0 if they are identical, and | |
727 | * > 0 if the second member is less (the first member is greater). | |
728 | */ | |
b5b167d6 | 729 | |
730 | int membercmp(m1, m2) | |
731 | struct member *m1, *m2; | |
2d7360ca | 732 | { |
1b6b0a57 | 733 | if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type)) |
734 | return(strcmp(m1->name, m2->name)); | |
2d7360ca | 735 | else |
1b6b0a57 | 736 | return(m1->type - m2->type); |
2d7360ca | 737 | } |
b5b167d6 | 738 | |
739 | ||
740 | sq_count_elts(q) | |
741 | struct save_queue *q; | |
742 | { | |
743 | char *foo; | |
744 | int count; | |
745 | ||
746 | count = 0; | |
747 | while (sq_get_data(q, &foo)) | |
748 | count++; | |
749 | return(count); | |
750 | } |