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