]>
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> |
7b58c1b9 | 20 | #include <stdlib.h> |
2d7360ca | 21 | #include <ctype.h> |
f071d8a7 | 22 | #include <string.h> |
8defc06b | 23 | #include <moira.h> |
24 | #include <moira_site.h> | |
2d7360ca | 25 | |
26 | #ifndef LINT | |
d44cee72 | 27 | static char blanche_rcsid[] = "$Header$"; |
2d7360ca | 28 | #endif |
29 | ||
30 | ||
31 | struct member { | |
32 | int type; | |
33 | char *name; | |
34 | }; | |
35 | ||
b5b167d6 | 36 | /* It is important to membercmp that M_USER < M_LIST < M_STRING */ |
2d7360ca | 37 | #define M_ANY 0 |
38 | #define M_USER 1 | |
39 | #define M_LIST 2 | |
40 | #define M_STRING 3 | |
8c3b414d | 41 | #define M_KERBEROS 4 |
2d7360ca | 42 | |
1b6b0a57 | 43 | /* argument parsing macro */ |
44 | #define argis(a,b) ((strcmp(*arg+1, a) == 0) || (strcmp(*arg+1, b) == 0)) | |
45 | ||
2d7360ca | 46 | /* flags from command line */ |
8b494222 | 47 | int infoflg, verbose, syncflg, memberflg, recursflg, debugflg, noauth; |
8c3b414d | 48 | int showusers, showstrings, showkerberos, showlists; |
2d7360ca | 49 | |
50 | /* various member lists */ | |
51 | struct save_queue *addlist, *dellist, *memberlist, *synclist; | |
52 | ||
53 | char *listname, *whoami; | |
54 | ||
f071d8a7 | 55 | #ifndef POSIX |
2d7360ca | 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 | { | |
e681e918 | 69 | int status, success; |
2d7360ca | 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 | ||
e681e918 | 85 | success = 1; |
86 | ||
2d7360ca | 87 | /* parse args, building addlist, dellist, & synclist */ |
88 | while (++arg - argv < argc) { | |
89 | if (**arg == '-') | |
1b6b0a57 | 90 | { |
91 | if (argis("m", "members")) | |
92 | memberflg++; | |
8b494222 | 93 | else if (argis("u", "users")) |
94 | showusers++; | |
95 | else if (argis("s", "strings")) | |
96 | showstrings++; | |
97 | else if (argis("l", "lists")) | |
98 | showlists++; | |
8c3b414d | 99 | else if (argis("k", "kerberos")) |
100 | showkerberos++; | |
1b6b0a57 | 101 | else if (argis("D", "debug")) |
102 | debugflg++; | |
62a6860a | 103 | else if (argis("i","info")) |
2d7360ca | 104 | infoflg++; |
8b494222 | 105 | else if (argis("n","noauth")) |
106 | noauth++; | |
1b6b0a57 | 107 | else if (argis("v","verbose")) |
2d7360ca | 108 | verbose++; |
1b6b0a57 | 109 | else if (argis("r","recursive")) |
110 | recursflg++; | |
e84d51e9 | 111 | else if (argis("S","server") || argis("db","database")) |
8c3b414d | 112 | if (arg - argv < argc - 1) { |
113 | ++arg; | |
114 | server = *arg; | |
115 | } else | |
116 | usage(argv); | |
1b6b0a57 | 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); | |
7c02cbdb | 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); | |
1b6b0a57 | 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); | |
7c02cbdb | 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); | |
1b6b0a57 | 143 | else if (argis("f","file")) |
144 | if (arg - argv < argc - 1) { | |
1b6b0a57 | 145 | syncflg++; |
146 | ++arg; | |
7c02cbdb | 147 | get_members_from_file(*arg, synclist); |
1b6b0a57 | 148 | } else |
b5b167d6 | 149 | usage(argv); |
1b6b0a57 | 150 | else |
2d7360ca | 151 | usage(argv); |
1b6b0a57 | 152 | } |
2d7360ca | 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 | ||
8c3b414d | 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; | |
8b494222 | 169 | |
d44cee72 | 170 | /* fire up Moira */ |
8defc06b | 171 | if (status = mr_connect(server)) { |
d44cee72 | 172 | com_err(whoami, status, "unable to connect to the Moira server"); |
2d7360ca | 173 | exit(2); |
174 | } | |
8defc06b | 175 | if ( status = mr_motd(&motd) ) { |
d44cee72 | 176 | com_err(whoami, status, "unable to check server status"); |
37e3ac2d | 177 | exit(2); |
178 | } | |
179 | if (motd) { | |
95cd286e | 180 | fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd); |
8defc06b | 181 | mr_disconnect(); |
37e3ac2d | 182 | exit(2); |
183 | } | |
184 | ||
8defc06b | 185 | if (!noauth && (status = mr_auth("blanche"))) { |
ece49f32 | 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 | } | |
2d7360ca | 194 | } |
195 | ||
196 | /* display list info if requested to */ | |
197 | if (infoflg) { | |
8defc06b | 198 | status = mr_query("get_list_info", 1, &listname, show_list_info,NULL); |
2d7360ca | 199 | if (status) |
d44cee72 | 200 | com_err(whoami, status, "while getting list information"); |
2d7360ca | 201 | if (verbose && !memberflg) { |
8defc06b | 202 | status = mr_query("count_members_of_list", 1, &listname, |
2d7360ca | 203 | show_list_count, NULL); |
204 | if (status) | |
d44cee72 | 205 | com_err(whoami, status, "while getting list count"); |
2d7360ca | 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) { | |
8defc06b | 218 | status = mr_query("get_members_of_list", 1, &listname, |
1b6b0a57 | 219 | get_list_members, (char *)memberlist); |
26624cbe | 220 | if (status) { |
221 | com_err(whoami, status, "getting members of list %s", listname); | |
222 | exit (2); | |
223 | } | |
2d7360ca | 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) { | |
1b6b0a57 | 229 | if (membercmp(q->q_data, memberstruct) == 0) { |
2d7360ca | 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)) { | |
99bdafb8 | 247 | /* canonicalize string if necessary */ |
248 | if (memberstruct->type == M_STRING && | |
f071d8a7 | 249 | (p = strchr(memberstruct->name, '@'))) { |
99bdafb8 | 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; | |
f416d13d | 261 | status = mr_query("get_alias", 3, argv, collect, |
262 | (char *)&mailhubs); | |
99bdafb8 | 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); | |
f071d8a7 | 272 | *(strchr(memberstruct->name, '@')) = 0; |
99bdafb8 | 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 */ | |
2d7360ca | 282 | membervec[0] = listname; |
283 | membervec[2] = memberstruct->name; | |
97f4e1fa | 284 | if (verbose) { |
b5b167d6 | 285 | printf("Adding member "); |
286 | show_list_member(memberstruct); | |
287 | } | |
2d7360ca | 288 | switch (memberstruct->type) { |
289 | case M_ANY: | |
290 | case M_USER: | |
291 | membervec[1] = "USER"; | |
8defc06b | 292 | status = mr_query("add_member_to_list", 3, membervec, scream, NULL); |
293 | if (status == MR_SUCCESS) | |
2d7360ca | 294 | break; |
8defc06b | 295 | else if (status != MR_USER || memberstruct->type != M_ANY) { |
d44cee72 | 296 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 297 | memberstruct->name, listname); |
e681e918 | 298 | success = 0; |
2d7360ca | 299 | break; |
300 | } | |
301 | case M_LIST: | |
302 | membervec[1] = "LIST"; | |
8defc06b | 303 | status = mr_query("add_member_to_list", 3, membervec, |
2d7360ca | 304 | scream, NULL); |
7b58c1b9 | 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) { | |
d44cee72 | 317 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 318 | memberstruct->name, listname); |
e681e918 | 319 | success = 0; |
2d7360ca | 320 | break; |
321 | } | |
322 | case M_STRING: | |
323 | membervec[1] = "STRING"; | |
8defc06b | 324 | status = mr_query("add_member_to_list", 3, membervec, |
2d7360ca | 325 | scream, NULL); |
e681e918 | 326 | if (status != MR_SUCCESS) { |
d44cee72 | 327 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 328 | memberstruct->name, listname); |
e681e918 | 329 | success = 0; |
330 | } | |
f1f92ff7 | 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 | } | |
8c3b414d | 339 | break; |
340 | case M_KERBEROS: | |
341 | membervec[1] = "KERBEROS"; | |
8defc06b | 342 | status = mr_query("add_member_to_list", 3, membervec, |
8c3b414d | 343 | scream, NULL); |
e681e918 | 344 | if (status != MR_SUCCESS) { |
d44cee72 | 345 | com_err(whoami, status, "while adding member %s to %s", |
8c3b414d | 346 | memberstruct->name, listname); |
e681e918 | 347 | success = 0; |
348 | } | |
2d7360ca | 349 | } |
350 | } | |
351 | ||
352 | /* Process the delete list */ | |
353 | while (sq_get_data(dellist, &memberstruct)) { | |
354 | membervec[0] = listname; | |
355 | membervec[2] = memberstruct->name; | |
97f4e1fa | 356 | if (verbose) { |
b5b167d6 | 357 | printf("Deleting member "); |
358 | show_list_member(memberstruct); | |
359 | } | |
2d7360ca | 360 | switch (memberstruct->type) { |
361 | case M_ANY: | |
362 | case M_USER: | |
363 | membervec[1] = "USER"; | |
8defc06b | 364 | status = mr_query("delete_member_from_list", 3, membervec, |
2d7360ca | 365 | scream, NULL); |
8defc06b | 366 | if (status == MR_SUCCESS) |
2d7360ca | 367 | break; |
8defc06b | 368 | else if ((status != MR_USER && status != MR_NO_MATCH) || |
2d7360ca | 369 | memberstruct->type != M_ANY) { |
d44cee72 | 370 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 371 | memberstruct->name, listname); |
e681e918 | 372 | success = 0; |
2d7360ca | 373 | break; |
374 | } | |
375 | case M_LIST: | |
376 | membervec[1] = "LIST"; | |
8defc06b | 377 | status = mr_query("delete_member_from_list", 3, membervec, |
2d7360ca | 378 | scream, NULL); |
8defc06b | 379 | if (status == MR_SUCCESS) |
2d7360ca | 380 | break; |
8defc06b | 381 | else if ((status != MR_LIST && status != MR_NO_MATCH) || |
2d7360ca | 382 | memberstruct->type != M_ANY) { |
9fa24493 | 383 | if (status == MR_PERM && memberstruct->type == M_ANY && |
384 | !strcmp(membervec[2], getenv("USER"))) { | |
385 | /* M_ANY means we've fallen through from the user | |
386 | * case. The user is trying to remove himself from | |
387 | * a list, but we got MR_USER or MR_NO_MATCH above, | |
388 | * meaning he's not really on it, and we got MR_PERM | |
389 | * when trying to remove LIST:$USER because he's not | |
390 | * on the acl. That error is useless, so return | |
391 | * MR_NO_MATCH instead. However, this will generate the | |
392 | * wrong error if the user was trying to remove the list | |
393 | * with his username from a list he doesn't administrate | |
394 | * without explicitly specifying "list:". | |
395 | */ | |
40637dba | 396 | status = MR_NO_MATCH; |
397 | } | |
d44cee72 | 398 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 399 | memberstruct->name, listname); |
e681e918 | 400 | success = 0; |
2d7360ca | 401 | break; |
402 | } | |
403 | case M_STRING: | |
404 | membervec[1] = "STRING"; | |
8defc06b | 405 | status = mr_query("delete_member_from_list", 3, membervec, |
2d7360ca | 406 | scream, NULL); |
7b58c1b9 | 407 | if (status == MR_STRING && memberstruct->type == M_ANY) { |
d44cee72 | 408 | com_err(whoami, 0, " Unable to find member %s to delete from %s", |
2d7360ca | 409 | memberstruct->name, listname); |
e681e918 | 410 | success = 0; |
7b58c1b9 | 411 | if (!strcmp(membervec[0], getenv("USER"))) { |
412 | fprintf(stderr, "(If you were trying to remove yourself from the list \"%s\",\n", membervec[2]); | |
413 | fprintf(stderr, "the correct command is \"blanche %s -d %s\".)\n", | |
414 | membervec[2], membervec[0]); | |
415 | } | |
e681e918 | 416 | } else if (status != MR_SUCCESS) { |
d44cee72 | 417 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 418 | memberstruct->name, listname); |
e681e918 | 419 | success = 0; |
420 | } | |
8c3b414d | 421 | break; |
422 | case M_KERBEROS: | |
423 | membervec[1] = "KERBEROS"; | |
8defc06b | 424 | status = mr_query("delete_member_from_list", 3, membervec, |
8c3b414d | 425 | scream, NULL); |
e681e918 | 426 | if (status != MR_SUCCESS) { |
d44cee72 | 427 | com_err(whoami, status, "while deleting member %s from %s", |
8c3b414d | 428 | memberstruct->name, listname); |
e681e918 | 429 | success = 0; |
430 | } | |
2d7360ca | 431 | } |
432 | } | |
433 | ||
434 | /* Display the members of the list now, if requested */ | |
b5b167d6 | 435 | if (memberflg) { |
436 | if (recursflg) | |
437 | recursive_display_list_members(); | |
438 | else { | |
8defc06b | 439 | status = mr_query("get_members_of_list", 1, &listname, |
b5b167d6 | 440 | get_list_members, (char *)memberlist); |
441 | if (status) | |
d44cee72 | 442 | com_err(whoami, status, "while getting members of list %s", |
b5b167d6 | 443 | listname); |
444 | while (sq_get_data(memberlist, &memberstruct)) | |
445 | show_list_member(memberstruct); | |
446 | } | |
447 | } | |
2d7360ca | 448 | |
449 | /* We're done! */ | |
8defc06b | 450 | mr_disconnect(); |
e681e918 | 451 | exit(success?0:1); |
2d7360ca | 452 | } |
453 | ||
454 | usage(argv) | |
455 | char **argv; | |
456 | { | |
d465790c | 457 | fprintf(stderr, "Usage: %s listname [options]\n",argv[0]); |
1b6b0a57 | 458 | fprintf(stderr, "Options are\n"); |
459 | fprintf(stderr, " -v | -verbose\n"); | |
460 | fprintf(stderr, " -m | -members\n"); | |
8b494222 | 461 | fprintf(stderr, " -u | -users\n"); |
462 | fprintf(stderr, " -l | -lists\n"); | |
463 | fprintf(stderr, " -s | -strings\n"); | |
8c3b414d | 464 | fprintf(stderr, " -k | -kerberos\n"); |
1b6b0a57 | 465 | fprintf(stderr, " -i | -info\n"); |
466 | fprintf(stderr, " -r | -recursive\n"); | |
467 | fprintf(stderr, " -a | -add member\n"); | |
468 | fprintf(stderr, " -d | -delete member\n"); | |
7c02cbdb | 469 | fprintf(stderr, " -al | -addlist filename\n"); |
470 | fprintf(stderr, " -dl | -deletelist filename\n"); | |
1b6b0a57 | 471 | fprintf(stderr, " -f | -file filename\n"); |
8b494222 | 472 | fprintf(stderr, " -n | -noauth\n"); |
48d8155d | 473 | fprintf(stderr, " -db | -database host[:port]\n"); |
1b6b0a57 | 474 | fprintf(stderr, " -D | -debug\n"); |
2d7360ca | 475 | exit(1); |
476 | } | |
477 | ||
478 | ||
b5b167d6 | 479 | /* Display the members stored in the queue */ |
1b6b0a57 | 480 | |
b5b167d6 | 481 | show_list_member(memberstruct) |
482 | struct member *memberstruct; | |
483 | { | |
24582af9 | 484 | char *s = ""; |
8b494222 | 485 | |
486 | switch (memberstruct->type) { | |
487 | case M_USER: | |
488 | if (!showusers) | |
489 | return; | |
490 | s = "USER"; | |
491 | break; | |
492 | case M_LIST: | |
493 | if (!showlists) | |
494 | return; | |
495 | s = "LIST"; | |
496 | break; | |
497 | case M_STRING: | |
498 | if (!showstrings) | |
499 | return; | |
500 | s = "STRING"; | |
501 | break; | |
8c3b414d | 502 | case M_KERBEROS: |
503 | if (!showkerberos) | |
504 | return; | |
505 | s = "KERBEROS"; | |
506 | break; | |
8b494222 | 507 | case M_ANY: |
508 | printf("%s\n", memberstruct->name); | |
509 | return; | |
510 | } | |
511 | ||
512 | if (verbose) | |
513 | printf("%s:%s\n", s, memberstruct->name); | |
514 | else { | |
b5b167d6 | 515 | if (memberstruct->type == M_LIST) |
516 | printf("LIST:%s\n", memberstruct->name); | |
8c3b414d | 517 | else if (memberstruct->type == M_KERBEROS) |
518 | printf("KERBEROS:%s\n", memberstruct->name); | |
b5b167d6 | 519 | else if (memberstruct->type == M_STRING && |
f071d8a7 | 520 | !strchr(memberstruct->name, '@')) |
b5b167d6 | 521 | printf("STRING:%s\n", memberstruct->name); |
522 | else | |
523 | printf("%s\n", memberstruct->name); | |
1b6b0a57 | 524 | } |
525 | } | |
526 | ||
b5b167d6 | 527 | |
528 | /* Show the retrieved information about a list */ | |
529 | ||
2d7360ca | 530 | show_list_info(argc, argv, hint) |
531 | int argc; | |
532 | char **argv; | |
533 | int hint; | |
534 | { | |
535 | printf("List: %s\n", argv[0]); | |
536 | printf("Description: %s\n", argv[9]); | |
537 | printf("Flags: %s, %s, and %s\n", | |
538 | atoi(argv[1]) ? "active" : "inactive", | |
539 | atoi(argv[2]) ? "public" : "private", | |
540 | atoi(argv[3]) ? "hidden" : "visible"); | |
1b6b0a57 | 541 | printf("%s is %sa maillist and is %sa group", argv[0], |
2d7360ca | 542 | atoi(argv[4]) ? "" : "not ", |
543 | atoi(argv[5]) ? "" : "not "); | |
544 | if (atoi(argv[5])) | |
545 | printf(" with GID %d\n", atoi(argv[6])); | |
546 | else | |
547 | printf("\n"); | |
548 | printf("Owner: %s %s\n", argv[7], argv[8]); | |
549 | printf("Last modified by %s with %s on %s\n", argv[11], argv[12], argv[10]); | |
8defc06b | 550 | return(MR_CONT); |
2d7360ca | 551 | } |
552 | ||
553 | ||
b5b167d6 | 554 | /* Show the retrieve list member count */ |
555 | ||
2d7360ca | 556 | show_list_count(argc, argv, hint) |
557 | int argc; | |
558 | char **argv; | |
559 | int hint; | |
560 | { | |
561 | printf("Members: %s\n", argv[0]); | |
562 | } | |
563 | ||
564 | ||
b5b167d6 | 565 | /* Recursively find all of the members of listname, and then display them */ |
566 | ||
567 | recursive_display_list_members() | |
1b6b0a57 | 568 | { |
b5b167d6 | 569 | int status, count, savecount; |
570 | struct save_queue *lists, *members; | |
571 | struct member *m, *m1, *data; | |
572 | ||
573 | lists = sq_create(); | |
574 | members = sq_create(); | |
575 | m = (struct member *) malloc(sizeof(struct member)); | |
576 | m->type = M_LIST; | |
577 | m->name = listname; | |
578 | sq_save_data(lists, m); | |
579 | ||
580 | while (sq_get_data(lists, &m)) { | |
581 | sq_destroy(memberlist); | |
582 | memberlist = sq_create(); | |
583 | if (debugflg) | |
584 | fprintf(stderr, "Fetching members of %s\n", m->name); | |
8defc06b | 585 | status = mr_query("get_members_of_list", 1, &(m->name), |
b5b167d6 | 586 | get_list_members, (char *)memberlist); |
587 | if (status) | |
d44cee72 | 588 | com_err(whoami, status, "while getting members of list %s", m->name); |
b5b167d6 | 589 | while (sq_get_data(memberlist, &m1)) { |
590 | if (m1->type == M_LIST) | |
591 | unique_add_member(lists, m1); | |
592 | else | |
593 | unique_add_member(members, m1); | |
594 | } | |
595 | } | |
596 | savecount = count = sq_count_elts(members); | |
597 | data = (struct member *) malloc(count * sizeof(struct member)); | |
598 | count = 0; | |
599 | while (sq_get_data(members, &m)) | |
f071d8a7 | 600 | memcpy(&data[count++], m, sizeof(struct member)); |
b5b167d6 | 601 | qsort(data, count, sizeof(struct member), membercmp); |
602 | for (count = 0; count < savecount; count++) { | |
603 | show_list_member(&data[count]); | |
604 | } | |
605 | } | |
606 | ||
607 | ||
608 | /* add a struct member to a queue if that member isn't already there. */ | |
609 | ||
610 | unique_add_member(q, m) | |
611 | struct save_queue *q; | |
612 | struct member *m; | |
613 | { | |
614 | struct save_queue *qp; | |
1b6b0a57 | 615 | |
b5b167d6 | 616 | for (qp = q->q_next; qp != q; qp = qp->q_next) { |
617 | if (!membercmp(qp->q_data, m)) | |
618 | return; | |
619 | } | |
620 | sq_save_data(q, m); | |
1b6b0a57 | 621 | } |
622 | ||
b5b167d6 | 623 | |
624 | /* Collect the retrieved members of the list */ | |
625 | ||
2d7360ca | 626 | get_list_members(argc, argv, q) |
627 | int argc; | |
628 | char **argv; | |
629 | struct save_queue *q; | |
630 | { | |
631 | struct member *m; | |
632 | ||
633 | m = (struct member *) malloc(sizeof(struct member)); | |
634 | switch (argv[0][0]) { | |
635 | case 'U': | |
636 | m->type = M_USER; | |
637 | break; | |
638 | case 'L': | |
639 | m->type = M_LIST; | |
640 | break; | |
641 | case 'S': | |
642 | m->type = M_STRING; | |
643 | break; | |
8c3b414d | 644 | case 'K': |
645 | m->type = M_KERBEROS; | |
646 | break; | |
2d7360ca | 647 | } |
648 | m->name = strsave(argv[1]); | |
649 | sq_save_data(q, m); | |
8defc06b | 650 | return(MR_CONT); |
2d7360ca | 651 | } |
652 | ||
653 | ||
b5b167d6 | 654 | /* Called only if a query returns a value that we weren't expecting */ |
655 | ||
2d7360ca | 656 | scream() |
657 | { | |
658 | fprintf(stderr, "Programmer botch\n"); | |
659 | exit(3); | |
660 | } | |
661 | ||
662 | ||
7c02cbdb | 663 | /* Open file, parse members from file, and put them on the specified queue */ |
664 | get_members_from_file(filename, queue) | |
665 | char *filename; | |
666 | struct save_queue *queue; | |
667 | { | |
668 | FILE *in; | |
669 | char buf[BUFSIZ]; | |
670 | struct member *memberstruct; | |
671 | ||
672 | if (!strcmp(filename, "-")) | |
673 | in = stdin; | |
674 | else { | |
675 | in = fopen(filename, "r"); | |
676 | if (!in) { | |
677 | com_err(whoami, errno, "while opening %s for input", filename); | |
678 | exit(2); | |
679 | } | |
680 | } | |
681 | ||
682 | while (fgets(buf, BUFSIZ, in)) | |
683 | if (memberstruct = parse_member(buf)) | |
684 | sq_save_data(queue, memberstruct); | |
e681e918 | 685 | if (!feof(in)) { |
7c02cbdb | 686 | com_err(whoami, errno, "while reading from %s", filename); |
e681e918 | 687 | exit(2); |
688 | } | |
7c02cbdb | 689 | } |
690 | ||
691 | ||
99bdafb8 | 692 | /* Collect the possible expansions of the alias MAILHUB */ |
693 | ||
694 | int collect(argc, argv, list) | |
695 | int argc; | |
696 | char **argv; | |
697 | char ***list; | |
698 | { | |
699 | int i; | |
700 | ||
701 | for (i = 0; (*list)[i]; i++); | |
702 | *list = (char **)realloc(*list, (i + 2) * sizeof(char *)); | |
703 | (*list)[i] = strsave(argv[2]); | |
704 | (*list)[i+1] = NULL; | |
705 | return(MR_CONT); | |
706 | } | |
707 | ||
708 | ||
b5b167d6 | 709 | /* Parse a line of input, fetching a member. NULL is returned if a member |
7c02cbdb | 710 | * is not found. ';' is a comment character. |
b5b167d6 | 711 | */ |
712 | ||
2d7360ca | 713 | struct member *parse_member(s) |
714 | register char *s; | |
715 | { | |
716 | register struct member *m; | |
7c02cbdb | 717 | char *p, *lastchar; |
2d7360ca | 718 | |
719 | while (*s && isspace(*s)) | |
720 | s++; | |
7c02cbdb | 721 | lastchar = p = s; |
722 | while (*p && *p != '\n' && *p != ';') | |
723 | if (isprint(*p) && !isspace(*p)) | |
724 | lastchar = p++; | |
725 | else | |
726 | p++; | |
727 | lastchar++; | |
728 | *lastchar = 0; | |
2d7360ca | 729 | if (p == s || strlen(s) == 0) |
730 | return(NULL); | |
731 | ||
732 | if ((m = (struct member *) malloc(sizeof(struct member))) == NULL) | |
733 | return(NULL); | |
734 | ||
f071d8a7 | 735 | if (p = strchr(s, ':')) { |
2d7360ca | 736 | *p = 0; |
737 | m->name = ++p; | |
738 | if (!strcasecmp("user", s)) | |
739 | m->type = M_USER; | |
740 | else if (!strcasecmp("list", s)) | |
741 | m->type = M_LIST; | |
742 | else if (!strcasecmp("string", s)) | |
743 | m->type = M_STRING; | |
8c3b414d | 744 | else if (!strcasecmp("kerberos", s)) |
745 | m->type = M_KERBEROS; | |
2d7360ca | 746 | else { |
747 | m->type = M_STRING; | |
748 | *(--p) = ':'; | |
749 | m->name = s; | |
750 | } | |
751 | m->name = strsave(m->name); | |
99bdafb8 | 752 | } else { |
753 | m->name = strsave(s); | |
f071d8a7 | 754 | if (strchr(s, '@') || strchr(s, '!') || strchr(s, '%') || strchr(s, ' ')) |
99bdafb8 | 755 | m->type = M_STRING; |
756 | else | |
757 | m->type = M_ANY; | |
2d7360ca | 758 | } |
2d7360ca | 759 | return(m); |
760 | } | |
761 | ||
762 | ||
1b6b0a57 | 763 | /* |
764 | * This routine two compares members by the following rules: | |
765 | * 1. A USER is less than a LIST | |
766 | * 2. A LIST is less than a STRING | |
767 | * 3. If two members are of the same type, the one alphabetically first | |
768 | * is less than the other | |
769 | * It returs < 0 if the first member is less, 0 if they are identical, and | |
770 | * > 0 if the second member is less (the first member is greater). | |
771 | */ | |
b5b167d6 | 772 | |
773 | int membercmp(m1, m2) | |
774 | struct member *m1, *m2; | |
2d7360ca | 775 | { |
1b6b0a57 | 776 | if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type)) |
777 | return(strcmp(m1->name, m2->name)); | |
2d7360ca | 778 | else |
1b6b0a57 | 779 | return(m1->type - m2->type); |
2d7360ca | 780 | } |
b5b167d6 | 781 | |
782 | ||
783 | sq_count_elts(q) | |
784 | struct save_queue *q; | |
785 | { | |
786 | char *foo; | |
787 | int count; | |
788 | ||
789 | count = 0; | |
790 | while (sq_get_data(q, &foo)) | |
791 | count++; | |
792 | return(count); | |
793 | } |