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