]>
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> |
18 | #include <stdio.h> | |
19 | #include <ctype.h> | |
f071d8a7 | 20 | #include <string.h> |
8defc06b | 21 | #include <moira.h> |
22 | #include <moira_site.h> | |
2d7360ca | 23 | |
24 | #ifndef LINT | |
d44cee72 | 25 | static char blanche_rcsid[] = "$Header$"; |
2d7360ca | 26 | #endif |
27 | ||
28 | ||
29 | struct member { | |
30 | int type; | |
31 | char *name; | |
32 | }; | |
33 | ||
b5b167d6 | 34 | /* It is important to membercmp that M_USER < M_LIST < M_STRING */ |
2d7360ca | 35 | #define M_ANY 0 |
36 | #define M_USER 1 | |
37 | #define M_LIST 2 | |
38 | #define M_STRING 3 | |
8c3b414d | 39 | #define M_KERBEROS 4 |
2d7360ca | 40 | |
1b6b0a57 | 41 | /* argument parsing macro */ |
42 | #define argis(a,b) ((strcmp(*arg+1, a) == 0) || (strcmp(*arg+1, b) == 0)) | |
43 | ||
2d7360ca | 44 | /* flags from command line */ |
8b494222 | 45 | int infoflg, verbose, syncflg, memberflg, recursflg, debugflg, noauth; |
8c3b414d | 46 | int showusers, showstrings, showkerberos, showlists; |
2d7360ca | 47 | |
48 | /* various member lists */ | |
49 | struct save_queue *addlist, *dellist, *memberlist, *synclist; | |
50 | ||
51 | char *listname, *whoami; | |
52 | ||
f071d8a7 | 53 | #ifndef POSIX |
2d7360ca | 54 | extern char *index(); |
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) { |
d44cee72 | 354 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 355 | memberstruct->name, listname); |
356 | break; | |
357 | } | |
358 | case M_STRING: | |
359 | membervec[1] = "STRING"; | |
8defc06b | 360 | status = mr_query("delete_member_from_list", 3, membervec, |
2d7360ca | 361 | scream, NULL); |
8defc06b | 362 | if (status == MR_STRING && memberstruct->type == M_ANY) |
d44cee72 | 363 | com_err(whoami, 0, " Unable to find member %s to delete from %s", |
2d7360ca | 364 | memberstruct->name, listname); |
8defc06b | 365 | else if (status != MR_SUCCESS) |
d44cee72 | 366 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 367 | memberstruct->name, listname); |
8c3b414d | 368 | break; |
369 | case M_KERBEROS: | |
370 | membervec[1] = "KERBEROS"; | |
8defc06b | 371 | status = mr_query("delete_member_from_list", 3, membervec, |
8c3b414d | 372 | scream, NULL); |
8defc06b | 373 | if (status != MR_SUCCESS) |
d44cee72 | 374 | com_err(whoami, status, "while deleting member %s from %s", |
8c3b414d | 375 | memberstruct->name, listname); |
2d7360ca | 376 | } |
377 | } | |
378 | ||
379 | /* Display the members of the list now, if requested */ | |
b5b167d6 | 380 | if (memberflg) { |
381 | if (recursflg) | |
382 | recursive_display_list_members(); | |
383 | else { | |
8defc06b | 384 | status = mr_query("get_members_of_list", 1, &listname, |
b5b167d6 | 385 | get_list_members, (char *)memberlist); |
386 | if (status) | |
d44cee72 | 387 | com_err(whoami, status, "while getting members of list %s", |
b5b167d6 | 388 | listname); |
389 | while (sq_get_data(memberlist, &memberstruct)) | |
390 | show_list_member(memberstruct); | |
391 | } | |
392 | } | |
2d7360ca | 393 | |
394 | /* We're done! */ | |
8defc06b | 395 | mr_disconnect(); |
2d7360ca | 396 | exit(0); |
397 | } | |
398 | ||
399 | usage(argv) | |
400 | char **argv; | |
401 | { | |
d465790c | 402 | fprintf(stderr, "Usage: %s listname [options]\n",argv[0]); |
1b6b0a57 | 403 | fprintf(stderr, "Options are\n"); |
404 | fprintf(stderr, " -v | -verbose\n"); | |
405 | fprintf(stderr, " -m | -members\n"); | |
8b494222 | 406 | fprintf(stderr, " -u | -users\n"); |
407 | fprintf(stderr, " -l | -lists\n"); | |
408 | fprintf(stderr, " -s | -strings\n"); | |
8c3b414d | 409 | fprintf(stderr, " -k | -kerberos\n"); |
1b6b0a57 | 410 | fprintf(stderr, " -i | -info\n"); |
411 | fprintf(stderr, " -r | -recursive\n"); | |
412 | fprintf(stderr, " -a | -add member\n"); | |
413 | fprintf(stderr, " -d | -delete member\n"); | |
7c02cbdb | 414 | fprintf(stderr, " -al | -addlist filename\n"); |
415 | fprintf(stderr, " -dl | -deletelist filename\n"); | |
1b6b0a57 | 416 | fprintf(stderr, " -f | -file filename\n"); |
8b494222 | 417 | fprintf(stderr, " -n | -noauth\n"); |
7c02cbdb | 418 | fprintf(stderr, " -S | -server host[:port]\n"); |
1b6b0a57 | 419 | fprintf(stderr, " -D | -debug\n"); |
2d7360ca | 420 | exit(1); |
421 | } | |
422 | ||
423 | ||
b5b167d6 | 424 | /* Display the members stored in the queue */ |
1b6b0a57 | 425 | |
b5b167d6 | 426 | show_list_member(memberstruct) |
427 | struct member *memberstruct; | |
428 | { | |
24582af9 | 429 | char *s = ""; |
8b494222 | 430 | |
431 | switch (memberstruct->type) { | |
432 | case M_USER: | |
433 | if (!showusers) | |
434 | return; | |
435 | s = "USER"; | |
436 | break; | |
437 | case M_LIST: | |
438 | if (!showlists) | |
439 | return; | |
440 | s = "LIST"; | |
441 | break; | |
442 | case M_STRING: | |
443 | if (!showstrings) | |
444 | return; | |
445 | s = "STRING"; | |
446 | break; | |
8c3b414d | 447 | case M_KERBEROS: |
448 | if (!showkerberos) | |
449 | return; | |
450 | s = "KERBEROS"; | |
451 | break; | |
8b494222 | 452 | case M_ANY: |
453 | printf("%s\n", memberstruct->name); | |
454 | return; | |
455 | } | |
456 | ||
457 | if (verbose) | |
458 | printf("%s:%s\n", s, memberstruct->name); | |
459 | else { | |
b5b167d6 | 460 | if (memberstruct->type == M_LIST) |
461 | printf("LIST:%s\n", memberstruct->name); | |
8c3b414d | 462 | else if (memberstruct->type == M_KERBEROS) |
463 | printf("KERBEROS:%s\n", memberstruct->name); | |
b5b167d6 | 464 | else if (memberstruct->type == M_STRING && |
f071d8a7 | 465 | !strchr(memberstruct->name, '@')) |
b5b167d6 | 466 | printf("STRING:%s\n", memberstruct->name); |
467 | else | |
468 | printf("%s\n", memberstruct->name); | |
1b6b0a57 | 469 | } |
470 | } | |
471 | ||
b5b167d6 | 472 | |
473 | /* Show the retrieved information about a list */ | |
474 | ||
2d7360ca | 475 | show_list_info(argc, argv, hint) |
476 | int argc; | |
477 | char **argv; | |
478 | int hint; | |
479 | { | |
480 | printf("List: %s\n", argv[0]); | |
481 | printf("Description: %s\n", argv[9]); | |
482 | printf("Flags: %s, %s, and %s\n", | |
483 | atoi(argv[1]) ? "active" : "inactive", | |
484 | atoi(argv[2]) ? "public" : "private", | |
485 | atoi(argv[3]) ? "hidden" : "visible"); | |
1b6b0a57 | 486 | printf("%s is %sa maillist and is %sa group", argv[0], |
2d7360ca | 487 | atoi(argv[4]) ? "" : "not ", |
488 | atoi(argv[5]) ? "" : "not "); | |
489 | if (atoi(argv[5])) | |
490 | printf(" with GID %d\n", atoi(argv[6])); | |
491 | else | |
492 | printf("\n"); | |
493 | printf("Owner: %s %s\n", argv[7], argv[8]); | |
494 | printf("Last modified by %s with %s on %s\n", argv[11], argv[12], argv[10]); | |
8defc06b | 495 | return(MR_CONT); |
2d7360ca | 496 | } |
497 | ||
498 | ||
b5b167d6 | 499 | /* Show the retrieve list member count */ |
500 | ||
2d7360ca | 501 | show_list_count(argc, argv, hint) |
502 | int argc; | |
503 | char **argv; | |
504 | int hint; | |
505 | { | |
506 | printf("Members: %s\n", argv[0]); | |
507 | } | |
508 | ||
509 | ||
b5b167d6 | 510 | /* Recursively find all of the members of listname, and then display them */ |
511 | ||
512 | recursive_display_list_members() | |
1b6b0a57 | 513 | { |
b5b167d6 | 514 | int status, count, savecount; |
515 | struct save_queue *lists, *members; | |
516 | struct member *m, *m1, *data; | |
517 | ||
518 | lists = sq_create(); | |
519 | members = sq_create(); | |
520 | m = (struct member *) malloc(sizeof(struct member)); | |
521 | m->type = M_LIST; | |
522 | m->name = listname; | |
523 | sq_save_data(lists, m); | |
524 | ||
525 | while (sq_get_data(lists, &m)) { | |
526 | sq_destroy(memberlist); | |
527 | memberlist = sq_create(); | |
528 | if (debugflg) | |
529 | fprintf(stderr, "Fetching members of %s\n", m->name); | |
8defc06b | 530 | status = mr_query("get_members_of_list", 1, &(m->name), |
b5b167d6 | 531 | get_list_members, (char *)memberlist); |
532 | if (status) | |
d44cee72 | 533 | com_err(whoami, status, "while getting members of list %s", m->name); |
b5b167d6 | 534 | while (sq_get_data(memberlist, &m1)) { |
535 | if (m1->type == M_LIST) | |
536 | unique_add_member(lists, m1); | |
537 | else | |
538 | unique_add_member(members, m1); | |
539 | } | |
540 | } | |
541 | savecount = count = sq_count_elts(members); | |
542 | data = (struct member *) malloc(count * sizeof(struct member)); | |
543 | count = 0; | |
544 | while (sq_get_data(members, &m)) | |
f071d8a7 | 545 | memcpy(&data[count++], m, sizeof(struct member)); |
b5b167d6 | 546 | qsort(data, count, sizeof(struct member), membercmp); |
547 | for (count = 0; count < savecount; count++) { | |
548 | show_list_member(&data[count]); | |
549 | } | |
550 | } | |
551 | ||
552 | ||
553 | /* add a struct member to a queue if that member isn't already there. */ | |
554 | ||
555 | unique_add_member(q, m) | |
556 | struct save_queue *q; | |
557 | struct member *m; | |
558 | { | |
559 | struct save_queue *qp; | |
1b6b0a57 | 560 | |
b5b167d6 | 561 | for (qp = q->q_next; qp != q; qp = qp->q_next) { |
562 | if (!membercmp(qp->q_data, m)) | |
563 | return; | |
564 | } | |
565 | sq_save_data(q, m); | |
1b6b0a57 | 566 | } |
567 | ||
b5b167d6 | 568 | |
569 | /* Collect the retrieved members of the list */ | |
570 | ||
2d7360ca | 571 | get_list_members(argc, argv, q) |
572 | int argc; | |
573 | char **argv; | |
574 | struct save_queue *q; | |
575 | { | |
576 | struct member *m; | |
577 | ||
578 | m = (struct member *) malloc(sizeof(struct member)); | |
579 | switch (argv[0][0]) { | |
580 | case 'U': | |
581 | m->type = M_USER; | |
582 | break; | |
583 | case 'L': | |
584 | m->type = M_LIST; | |
585 | break; | |
586 | case 'S': | |
587 | m->type = M_STRING; | |
588 | break; | |
8c3b414d | 589 | case 'K': |
590 | m->type = M_KERBEROS; | |
591 | break; | |
2d7360ca | 592 | } |
593 | m->name = strsave(argv[1]); | |
594 | sq_save_data(q, m); | |
8defc06b | 595 | return(MR_CONT); |
2d7360ca | 596 | } |
597 | ||
598 | ||
b5b167d6 | 599 | /* Called only if a query returns a value that we weren't expecting */ |
600 | ||
2d7360ca | 601 | scream() |
602 | { | |
603 | fprintf(stderr, "Programmer botch\n"); | |
604 | exit(3); | |
605 | } | |
606 | ||
607 | ||
7c02cbdb | 608 | /* Open file, parse members from file, and put them on the specified queue */ |
609 | get_members_from_file(filename, queue) | |
610 | char *filename; | |
611 | struct save_queue *queue; | |
612 | { | |
613 | FILE *in; | |
614 | char buf[BUFSIZ]; | |
615 | struct member *memberstruct; | |
616 | ||
617 | if (!strcmp(filename, "-")) | |
618 | in = stdin; | |
619 | else { | |
620 | in = fopen(filename, "r"); | |
621 | if (!in) { | |
622 | com_err(whoami, errno, "while opening %s for input", filename); | |
623 | exit(2); | |
624 | } | |
625 | } | |
626 | ||
627 | while (fgets(buf, BUFSIZ, in)) | |
628 | if (memberstruct = parse_member(buf)) | |
629 | sq_save_data(queue, memberstruct); | |
630 | if (!feof(in)) | |
631 | com_err(whoami, errno, "while reading from %s", filename); | |
632 | } | |
633 | ||
634 | ||
99bdafb8 | 635 | /* Collect the possible expansions of the alias MAILHUB */ |
636 | ||
637 | int collect(argc, argv, list) | |
638 | int argc; | |
639 | char **argv; | |
640 | char ***list; | |
641 | { | |
642 | int i; | |
643 | ||
644 | for (i = 0; (*list)[i]; i++); | |
645 | *list = (char **)realloc(*list, (i + 2) * sizeof(char *)); | |
646 | (*list)[i] = strsave(argv[2]); | |
647 | (*list)[i+1] = NULL; | |
648 | return(MR_CONT); | |
649 | } | |
650 | ||
651 | ||
b5b167d6 | 652 | /* Parse a line of input, fetching a member. NULL is returned if a member |
7c02cbdb | 653 | * is not found. ';' is a comment character. |
b5b167d6 | 654 | */ |
655 | ||
2d7360ca | 656 | struct member *parse_member(s) |
657 | register char *s; | |
658 | { | |
659 | register struct member *m; | |
7c02cbdb | 660 | char *p, *lastchar; |
2d7360ca | 661 | |
662 | while (*s && isspace(*s)) | |
663 | s++; | |
7c02cbdb | 664 | lastchar = p = s; |
665 | while (*p && *p != '\n' && *p != ';') | |
666 | if (isprint(*p) && !isspace(*p)) | |
667 | lastchar = p++; | |
668 | else | |
669 | p++; | |
670 | lastchar++; | |
671 | *lastchar = 0; | |
2d7360ca | 672 | if (p == s || strlen(s) == 0) |
673 | return(NULL); | |
674 | ||
675 | if ((m = (struct member *) malloc(sizeof(struct member))) == NULL) | |
676 | return(NULL); | |
677 | ||
f071d8a7 | 678 | if (p = strchr(s, ':')) { |
2d7360ca | 679 | *p = 0; |
680 | m->name = ++p; | |
681 | if (!strcasecmp("user", s)) | |
682 | m->type = M_USER; | |
683 | else if (!strcasecmp("list", s)) | |
684 | m->type = M_LIST; | |
685 | else if (!strcasecmp("string", s)) | |
686 | m->type = M_STRING; | |
8c3b414d | 687 | else if (!strcasecmp("kerberos", s)) |
688 | m->type = M_KERBEROS; | |
2d7360ca | 689 | else { |
690 | m->type = M_STRING; | |
691 | *(--p) = ':'; | |
692 | m->name = s; | |
693 | } | |
694 | m->name = strsave(m->name); | |
99bdafb8 | 695 | } else { |
696 | m->name = strsave(s); | |
f071d8a7 | 697 | if (strchr(s, '@') || strchr(s, '!') || strchr(s, '%') || strchr(s, ' ')) |
99bdafb8 | 698 | m->type = M_STRING; |
699 | else | |
700 | m->type = M_ANY; | |
2d7360ca | 701 | } |
2d7360ca | 702 | return(m); |
703 | } | |
704 | ||
705 | ||
1b6b0a57 | 706 | /* |
707 | * This routine two compares members by the following rules: | |
708 | * 1. A USER is less than a LIST | |
709 | * 2. A LIST is less than a STRING | |
710 | * 3. If two members are of the same type, the one alphabetically first | |
711 | * is less than the other | |
712 | * It returs < 0 if the first member is less, 0 if they are identical, and | |
713 | * > 0 if the second member is less (the first member is greater). | |
714 | */ | |
b5b167d6 | 715 | |
716 | int membercmp(m1, m2) | |
717 | struct member *m1, *m2; | |
2d7360ca | 718 | { |
1b6b0a57 | 719 | if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type)) |
720 | return(strcmp(m1->name, m2->name)); | |
2d7360ca | 721 | else |
1b6b0a57 | 722 | return(m1->type - m2->type); |
2d7360ca | 723 | } |
b5b167d6 | 724 | |
725 | ||
726 | sq_count_elts(q) | |
727 | struct save_queue *q; | |
728 | { | |
729 | char *foo; | |
730 | int count; | |
731 | ||
732 | count = 0; | |
733 | while (sq_get_data(q, &foo)) | |
734 | count++; | |
735 | return(count); | |
736 | } |