]>
Commit | Line | Data |
---|---|---|
c441a31a | 1 | /* $Id$ |
2d7360ca | 2 | * |
d44cee72 | 3 | * Command line oriented Moira List tool. |
2d7360ca | 4 | * |
5 | * by Mark Rosenstein, September 1988. | |
6 | * | |
7ac48069 | 7 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
1b6b0a57 | 8 | * For copying and distribution information, please see the file |
9 | * <mit-copyright.h>. | |
2d7360ca | 10 | */ |
11 | ||
12 | #include <mit-copyright.h> | |
7ac48069 | 13 | #include <moira.h> |
14 | #include <moira_site.h> | |
2a12a5ec | 15 | #include <mrclient.h> |
7ac48069 | 16 | |
17 | #include <ctype.h> | |
9eb9cfb9 | 18 | #include <errno.h> |
2d7360ca | 19 | #include <stdio.h> |
7b58c1b9 | 20 | #include <stdlib.h> |
f071d8a7 | 21 | #include <string.h> |
2d7360ca | 22 | |
7ac48069 | 23 | RCSID("$Header$"); |
2d7360ca | 24 | |
25 | struct member { | |
5eaef520 | 26 | int type; |
27 | char *name; | |
2d7360ca | 28 | }; |
29 | ||
b5b167d6 | 30 | /* It is important to membercmp that M_USER < M_LIST < M_STRING */ |
2d7360ca | 31 | #define M_ANY 0 |
32 | #define M_USER 1 | |
33 | #define M_LIST 2 | |
34 | #define M_STRING 3 | |
8c3b414d | 35 | #define M_KERBEROS 4 |
2d7360ca | 36 | |
1b6b0a57 | 37 | /* argument parsing macro */ |
5eaef520 | 38 | #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b)) |
1b6b0a57 | 39 | |
2d7360ca | 40 | /* flags from command line */ |
763465b8 | 41 | int infoflg, verbose, syncflg, memberflg, recursflg, noauth; |
8c3b414d | 42 | int showusers, showstrings, showkerberos, showlists; |
763465b8 | 43 | int createflag, setinfo, active, public, hidden, maillist, grouplist; |
44 | struct member *owner; | |
45 | char *desc, *newname; | |
2d7360ca | 46 | |
47 | /* various member lists */ | |
48 | struct save_queue *addlist, *dellist, *memberlist, *synclist; | |
49 | ||
50 | char *listname, *whoami; | |
51 | ||
7ac48069 | 52 | void usage(char **argv); |
53 | void show_list_member(struct member *memberstruct); | |
54 | int show_list_info(int argc, char **argv, void *hint); | |
55 | int save_list_info(int argc, char **argv, void *hint); | |
56 | int show_list_count(int argc, char **argv, void *hint); | |
57 | void recursive_display_list_members(void); | |
58 | void unique_add_member(struct save_queue *q, struct member *m); | |
59 | int get_list_members(int argc, char **argv, void *sq); | |
60 | void get_members_from_file(char *filename, struct save_queue *queue); | |
61 | int collect(int argc, char **argv, void *l); | |
62 | struct member *parse_member(char *s); | |
63 | int membercmp(const void *mem1, const void *mem2); | |
64 | int sq_count_elts(struct save_queue *q); | |
2d7360ca | 65 | |
5eaef520 | 66 | int main(int argc, char **argv) |
2d7360ca | 67 | { |
5eaef520 | 68 | int status, success; |
69 | char **arg = argv; | |
2a12a5ec | 70 | char *membervec[3]; |
5eaef520 | 71 | struct member *memberstruct; |
72 | char *server = NULL, *p; | |
73 | ||
74 | /* clear all flags & lists */ | |
75 | infoflg = verbose = syncflg = memberflg = recursflg = 0; | |
76 | noauth = showusers = showstrings = showkerberos = showlists = 0; | |
77 | createflag = setinfo = 0; | |
78 | active = public = hidden = maillist = grouplist = -1; | |
79 | listname = newname = desc = NULL; | |
80 | owner = NULL; | |
81 | addlist = sq_create(); | |
82 | dellist = sq_create(); | |
83 | memberlist = sq_create(); | |
84 | synclist = sq_create(); | |
85 | whoami = argv[0]; | |
86 | ||
87 | success = 1; | |
88 | ||
89 | /* parse args, building addlist, dellist, & synclist */ | |
90 | while (++arg - argv < argc) | |
91 | { | |
92 | if (**arg == '-') | |
1b6b0a57 | 93 | { |
5eaef520 | 94 | if (argis("m", "members")) |
95 | memberflg++; | |
96 | else if (argis("u", "users")) | |
97 | showusers++; | |
98 | else if (argis("s", "strings")) | |
99 | showstrings++; | |
100 | else if (argis("l", "lists")) | |
101 | showlists++; | |
102 | else if (argis("k", "kerberos")) | |
103 | showkerberos++; | |
104 | else if (argis("i", "info")) | |
105 | infoflg++; | |
106 | else if (argis("n", "noauth")) | |
107 | noauth++; | |
108 | else if (argis("v", "verbose")) | |
109 | verbose++; | |
110 | else if (argis("r", "recursive")) | |
111 | recursflg++; | |
112 | else if (argis("S", "server") || argis("db", "database")) | |
113 | { | |
114 | if (arg - argv < argc - 1) | |
115 | { | |
116 | ++arg; | |
117 | server = *arg; | |
118 | } | |
119 | else | |
120 | usage(argv); | |
121 | } | |
122 | else if (argis("a", "add")) | |
123 | { | |
124 | if (arg - argv < argc - 1) | |
125 | { | |
126 | ++arg; | |
127 | if ((memberstruct = parse_member(*arg))) | |
128 | sq_save_data(addlist, memberstruct); | |
129 | } | |
130 | else | |
131 | usage(argv); | |
132 | } | |
133 | else if (argis("al", "addlist")) | |
134 | { | |
135 | if (arg - argv < argc - 1) | |
136 | { | |
137 | ++arg; | |
138 | get_members_from_file(*arg, addlist); | |
139 | } | |
140 | else | |
141 | usage(argv); | |
142 | } | |
143 | else if (argis("d", "delete")) | |
144 | { | |
145 | if (arg - argv < argc - 1) | |
146 | { | |
147 | ++arg; | |
148 | if ((memberstruct = parse_member(*arg))) | |
149 | sq_save_data(dellist, memberstruct); | |
150 | } | |
151 | else | |
152 | usage(argv); | |
153 | } | |
154 | else if (argis("dl", "deletelist")) | |
155 | { | |
156 | if (arg - argv < argc - 1) | |
157 | { | |
158 | ++arg; | |
159 | get_members_from_file(*arg, dellist); | |
160 | } | |
161 | else | |
162 | usage(argv); | |
163 | } | |
164 | else if (argis("f", "file")) | |
165 | { | |
166 | if (arg - argv < argc - 1) | |
167 | { | |
168 | syncflg++; | |
169 | ++arg; | |
170 | get_members_from_file(*arg, synclist); | |
171 | } | |
172 | else | |
173 | usage(argv); | |
174 | } | |
175 | else if (argis("C", "create")) | |
176 | createflag++; | |
177 | else if (argis("P", "public")) | |
178 | { | |
179 | setinfo++; | |
180 | public = 1; | |
181 | } | |
182 | else if (argis("NP", "private")) | |
183 | { | |
184 | setinfo++; | |
185 | public = 0; | |
186 | } | |
187 | else if (argis("A", "active")) | |
188 | { | |
189 | setinfo++; | |
190 | active = 1; | |
191 | } | |
192 | else if (argis("I", "inactive")) | |
193 | { | |
194 | setinfo++; | |
195 | active = 0; | |
196 | } | |
197 | else if (argis("V", "visible")) | |
198 | { | |
199 | setinfo++; | |
200 | hidden = 0; | |
201 | } | |
202 | else if (argis("H", "hidden")) | |
203 | { | |
204 | setinfo++; | |
205 | hidden = 1; | |
206 | } | |
207 | else if (argis("M", "mail")) | |
208 | { | |
209 | setinfo++; | |
210 | maillist = 1; | |
211 | } | |
212 | else if (argis("NM", "notmail")) | |
213 | { | |
214 | setinfo++; | |
215 | maillist = 0; | |
216 | } | |
217 | else if (argis("G", "group")) | |
218 | { | |
219 | setinfo++; | |
220 | grouplist = 1; | |
221 | } | |
222 | else if (argis("NG", "notgroup")) | |
223 | { | |
224 | setinfo++; | |
225 | grouplist = 0; | |
226 | } | |
227 | else if (argis("D", "desc")) | |
228 | { | |
229 | if (arg - argv < argc - 1) | |
230 | { | |
231 | setinfo++; | |
232 | ++arg; | |
233 | desc = *arg; | |
234 | } | |
235 | else | |
236 | usage(argv); | |
237 | } | |
238 | else if (argis("O", "owner")) | |
239 | { | |
240 | if (arg - argv < argc - 1) | |
241 | { | |
242 | setinfo++; | |
243 | ++arg; | |
244 | owner = parse_member(*arg); | |
245 | } | |
246 | else | |
247 | usage(argv); | |
248 | } | |
249 | else if (argis("R", "rename")) | |
250 | { | |
251 | if (arg - argv < argc - 1) | |
252 | { | |
253 | setinfo++; | |
254 | ++arg; | |
255 | newname = *arg; | |
256 | } | |
257 | else | |
2d7360ca | 258 | usage(argv); |
5eaef520 | 259 | } |
260 | else | |
261 | usage(argv); | |
1b6b0a57 | 262 | } |
5eaef520 | 263 | else if (listname == NULL) |
264 | listname = *arg; | |
265 | else | |
266 | usage(argv); | |
2d7360ca | 267 | } |
5eaef520 | 268 | if (listname == NULL) |
269 | usage(argv); | |
270 | ||
271 | /* if no other options specified, turn on list members flag */ | |
272 | if (!(infoflg || syncflg || createflag || setinfo || | |
273 | addlist->q_next != addlist || dellist->q_next != dellist)) | |
274 | memberflg++; | |
275 | ||
276 | /* If none of {users,strings,lists,kerberos} specified, turn them all on */ | |
277 | if (!(showusers || showstrings || showlists || showkerberos)) | |
278 | showusers = showstrings = showlists = showkerberos = 1; | |
279 | ||
280 | /* fire up Moira */ | |
2a12a5ec | 281 | status = mrcl_connect(server, "blanche", !noauth); |
282 | if (status == MRCL_AUTH_ERROR) | |
5eaef520 | 283 | { |
2a12a5ec | 284 | com_err(whoami, 0, "Try the -noauth flag if you don't " |
285 | "need authentication."); | |
2d7360ca | 286 | } |
2a12a5ec | 287 | if (status) |
288 | exit(2); | |
2d7360ca | 289 | |
84826a5d | 290 | /* check for username/listname clash */ |
291 | if (createflag || (setinfo && newname && strcmp(newname, listname))) | |
292 | { | |
293 | status = mr_query("get_user_account_by_login", 1, | |
294 | createflag ? &listname : &newname, | |
295 | NULL, NULL); | |
296 | if (status != MR_NO_MATCH) | |
297 | fprintf(stderr, "WARNING: A user by that name already exists.\n"); | |
298 | } | |
299 | ||
5eaef520 | 300 | /* create if needed */ |
301 | if (createflag) | |
302 | { | |
303 | char *argv[10]; | |
304 | ||
305 | argv[0] = listname; | |
306 | argv[1] = (active == 0) ? "0" : "1"; | |
307 | argv[2] = (public == 1) ? "1" : "0"; | |
308 | argv[3] = (hidden == 1) ? "1" : "0"; | |
309 | argv[4] = (maillist == 0) ? "0" : "1"; | |
310 | argv[5] = (grouplist == 1) ? "1" : "0"; | |
311 | argv[6] = UNIQUE_GID; | |
312 | argv[9] = desc ? desc : "none"; | |
313 | ||
314 | if (owner) | |
315 | { | |
316 | argv[8] = owner->name; | |
317 | switch (owner->type) | |
318 | { | |
319 | case M_ANY: | |
320 | case M_USER: | |
321 | argv[7] = "USER"; | |
7ac48069 | 322 | status = mr_query("add_list", 10, argv, NULL, NULL); |
5eaef520 | 323 | if (owner->type != M_ANY || status != MR_USER) |
324 | break; | |
325 | ||
326 | case M_LIST: | |
327 | argv[7] = "LIST"; | |
7ac48069 | 328 | status = mr_query("add_list", 10, argv, NULL, NULL); |
5eaef520 | 329 | break; |
330 | ||
331 | case M_KERBEROS: | |
332 | argv[7] = "KERBEROS"; | |
7ac48069 | 333 | status = mr_query("add_list", 10, argv, NULL, NULL); |
5eaef520 | 334 | break; |
335 | } | |
763465b8 | 336 | } |
5eaef520 | 337 | else |
338 | { | |
339 | argv[7] = "USER"; | |
340 | argv[8] = getenv("USER"); | |
341 | ||
7ac48069 | 342 | status = mr_query("add_list", 10, argv, NULL, NULL); |
763465b8 | 343 | } |
344 | ||
5eaef520 | 345 | if (status) |
346 | { | |
347 | com_err(whoami, status, "while creating list."); | |
348 | exit(1); | |
349 | } | |
763465b8 | 350 | } |
5eaef520 | 351 | else if (setinfo) |
352 | { | |
353 | char *argv[11]; | |
763465b8 | 354 | |
5eaef520 | 355 | status = mr_query("get_list_info", 1, &listname, |
7ac48069 | 356 | save_list_info, argv); |
5eaef520 | 357 | if (status) |
358 | { | |
d44cee72 | 359 | com_err(whoami, status, "while getting list information"); |
5eaef520 | 360 | exit(1); |
2d7360ca | 361 | } |
5eaef520 | 362 | |
363 | argv[0] = listname; | |
364 | if (newname) | |
365 | argv[1] = newname; | |
366 | if (active != -1) | |
367 | argv[2] = active ? "1" : "0"; | |
368 | if (public != -1) | |
369 | argv[3] = public ? "1" : "0"; | |
370 | if (hidden != -1) | |
371 | argv[4] = hidden ? "1" : "0"; | |
372 | if (maillist != -1) | |
373 | argv[5] = maillist ? "1" : "0"; | |
374 | if (grouplist != -1) | |
375 | argv[6] = grouplist ? "1" : "0"; | |
376 | if (desc) | |
377 | argv[10] = desc; | |
378 | ||
379 | if (owner) | |
380 | { | |
381 | argv[9] = owner->name; | |
382 | switch (owner->type) | |
383 | { | |
384 | case M_ANY: | |
385 | case M_USER: | |
386 | argv[8] = "USER"; | |
7ac48069 | 387 | status = mr_query("update_list", 11, argv, NULL, NULL); |
5eaef520 | 388 | if (owner->type != M_ANY || status != MR_USER) |
389 | break; | |
390 | ||
391 | case M_LIST: | |
392 | argv[8] = "LIST"; | |
7ac48069 | 393 | status = mr_query("update_list", 11, argv, NULL, NULL); |
5eaef520 | 394 | break; |
395 | ||
396 | case M_KERBEROS: | |
397 | argv[8] = "KERBEROS"; | |
7ac48069 | 398 | status = mr_query("update_list", 11, argv, NULL, NULL); |
5eaef520 | 399 | break; |
400 | } | |
401 | } | |
402 | else | |
7ac48069 | 403 | status = mr_query("update_list", 11, argv, NULL, NULL); |
5eaef520 | 404 | |
405 | if (status) | |
406 | com_err(whoami, status, "while updating list."); | |
407 | else if (newname) | |
408 | listname = newname; | |
2d7360ca | 409 | } |
410 | ||
5eaef520 | 411 | /* display list info if requested to */ |
412 | if (infoflg) | |
413 | { | |
414 | status = mr_query("get_list_info", 1, &listname, show_list_info, NULL); | |
415 | if (status) | |
416 | com_err(whoami, status, "while getting list information"); | |
417 | if (verbose && !memberflg) | |
418 | { | |
419 | status = mr_query("count_members_of_list", 1, &listname, | |
420 | show_list_count, NULL); | |
421 | if (status) | |
422 | com_err(whoami, status, "while getting list count"); | |
26624cbe | 423 | } |
5eaef520 | 424 | } |
425 | ||
426 | /* if we're synchronizing to a file, we need to: | |
427 | * get the current members of the list | |
428 | * for each member of the sync file | |
429 | * if they are on the list, remove them from the in-memory copy | |
430 | * if they're not on the list, add them to add-list | |
431 | * if anyone is left on the in-memory copy, put them on the delete-list | |
432 | * lastly, reset memberlist so we can use it again later | |
433 | */ | |
434 | if (syncflg) | |
435 | { | |
436 | status = mr_query("get_members_of_list", 1, &listname, | |
7ac48069 | 437 | get_list_members, memberlist); |
5eaef520 | 438 | if (status) |
439 | { | |
440 | com_err(whoami, status, "getting members of list %s", listname); | |
441 | exit(2); | |
442 | } | |
443 | while (sq_get_data(synclist, &memberstruct)) | |
444 | { | |
445 | struct save_queue *q; | |
446 | int removed = 0; | |
447 | ||
448 | for (q = memberlist->q_next; q != memberlist; q = q->q_next) | |
449 | { | |
450 | if (membercmp(q->q_data, memberstruct) == 0) | |
451 | { | |
452 | q->q_prev->q_next = q->q_next; | |
453 | q->q_next->q_prev = q->q_prev; | |
454 | removed++; | |
455 | break; | |
2d7360ca | 456 | } |
457 | } | |
5eaef520 | 458 | if (!removed) |
459 | sq_save_data(addlist, memberstruct); | |
2d7360ca | 460 | } |
5eaef520 | 461 | while (sq_get_data(memberlist, &memberstruct)) |
462 | sq_save_data(dellist, memberstruct); | |
463 | sq_destroy(memberlist); | |
464 | memberlist = sq_create(); | |
2d7360ca | 465 | } |
466 | ||
5eaef520 | 467 | /* Process the add list */ |
468 | while (sq_get_data(addlist, &memberstruct)) | |
469 | { | |
470 | /* canonicalize string if necessary */ | |
2b3e2afe | 471 | if (memberstruct->type != M_KERBEROS && |
5eaef520 | 472 | (p = strchr(memberstruct->name, '@'))) |
473 | { | |
7ac48069 | 474 | char *host = canonicalize_hostname(strdup(++p)); |
5eaef520 | 475 | static char **mailhubs = NULL; |
476 | char *argv[4]; | |
7ac48069 | 477 | int i; |
5eaef520 | 478 | |
479 | if (!mailhubs) | |
480 | { | |
481 | argv[0] = "mailhub"; | |
482 | argv[1] = "TYPE"; | |
483 | argv[2] = "*"; | |
484 | mailhubs = malloc(sizeof(char *)); | |
485 | mailhubs[0] = NULL; | |
486 | status = mr_query("get_alias", 3, argv, collect, | |
7ac48069 | 487 | &mailhubs); |
5eaef520 | 488 | if (status != MR_SUCCESS && status != MR_NO_MATCH) |
489 | { | |
490 | com_err(whoami, status, | |
491 | " while reading list of MAILHUB servers"); | |
492 | mailhubs[0] = NULL; | |
99bdafb8 | 493 | } |
494 | } | |
5eaef520 | 495 | for (i = 0; (p = mailhubs[i]); i++) |
496 | { | |
497 | if (!strcasecmp(p, host)) | |
498 | { | |
7ac48069 | 499 | host = strdup(memberstruct->name); |
5eaef520 | 500 | *(strchr(memberstruct->name, '@')) = 0; |
2b3e2afe | 501 | if (memberstruct->type == M_STRING) |
502 | memberstruct->type = M_ANY; | |
503 | fprintf(stderr, "Warning: \"%s\" converted to " | |
5eaef520 | 504 | "\"%s\" because it is a local name.\n", |
505 | host, memberstruct->name); | |
506 | break; | |
99bdafb8 | 507 | } |
508 | } | |
5eaef520 | 509 | free(host); |
99bdafb8 | 510 | } |
5eaef520 | 511 | /* now continue adding member */ |
512 | membervec[0] = listname; | |
513 | membervec[2] = memberstruct->name; | |
514 | if (verbose) | |
515 | { | |
516 | printf("Adding member "); | |
517 | show_list_member(memberstruct); | |
b5b167d6 | 518 | } |
5eaef520 | 519 | switch (memberstruct->type) |
520 | { | |
2d7360ca | 521 | case M_ANY: |
522 | case M_USER: | |
5eaef520 | 523 | membervec[1] = "USER"; |
7ac48069 | 524 | status = mr_query("add_member_to_list", 3, membervec, NULL, NULL); |
5eaef520 | 525 | if (status == MR_SUCCESS) |
526 | break; | |
527 | else if (status != MR_USER || memberstruct->type != M_ANY) | |
528 | { | |
529 | com_err(whoami, status, "while adding member %s to %s", | |
530 | memberstruct->name, listname); | |
531 | success = 0; | |
2d7360ca | 532 | break; |
2d7360ca | 533 | } |
534 | case M_LIST: | |
5eaef520 | 535 | membervec[1] = "LIST"; |
536 | status = mr_query("add_member_to_list", 3, membervec, | |
7ac48069 | 537 | NULL, NULL); |
5eaef520 | 538 | if (status == MR_SUCCESS) |
539 | { | |
540 | if (!strcmp(membervec[0], getenv("USER"))) | |
541 | { | |
542 | fprintf(stderr, "\nWARNING: \"LIST:%s\" was just added " | |
543 | "to list \"%s\".\n", membervec[2], membervec[0]); | |
544 | fprintf(stderr, "If you meant to add yourself to the list " | |
545 | "\"%s\", type:\n", membervec[2]); | |
546 | fprintf(stderr, "\tblanche %s -d %s\t(to undo this)\n", | |
547 | membervec[0], membervec[2]); | |
548 | fprintf(stderr, "\tblanche %s -a %s\t(to add yourself to " | |
549 | "that list)\n", membervec[2], membervec[0]); | |
7b58c1b9 | 550 | } |
5eaef520 | 551 | break; |
552 | } | |
553 | else if (status != MR_LIST || memberstruct->type != M_ANY) | |
554 | { | |
555 | com_err(whoami, status, "while adding member %s to %s", | |
556 | memberstruct->name, listname); | |
557 | success = 0; | |
558 | break; | |
2d7360ca | 559 | } |
560 | case M_STRING: | |
5eaef520 | 561 | if (memberstruct->type == M_ANY && |
562 | !strchr(memberstruct->name, '@') && | |
563 | !strchr(memberstruct->name, '!') && | |
564 | !strchr(memberstruct->name, '%')) | |
565 | { | |
566 | /* if user is trying to add something which isn't a | |
567 | remote string, or a list, or a user, and didn't | |
568 | explicitly specify `STRING:', it's probably a typo */ | |
569 | com_err(whoami, MR_NO_MATCH, "while adding member %s to %s", | |
570 | memberstruct->name, listname); | |
571 | success = 0; | |
572 | break; | |
25978f25 | 573 | } |
5eaef520 | 574 | |
575 | membervec[1] = "STRING"; | |
576 | status = mr_query("add_member_to_list", 3, membervec, | |
7ac48069 | 577 | NULL, NULL); |
5eaef520 | 578 | if (status != MR_SUCCESS) |
579 | { | |
d44cee72 | 580 | com_err(whoami, status, "while adding member %s to %s", |
2d7360ca | 581 | memberstruct->name, listname); |
e681e918 | 582 | success = 0; |
583 | } | |
5eaef520 | 584 | break; |
8c3b414d | 585 | case M_KERBEROS: |
5eaef520 | 586 | membervec[1] = "KERBEROS"; |
587 | status = mr_query("add_member_to_list", 3, membervec, | |
7ac48069 | 588 | NULL, NULL); |
5eaef520 | 589 | if (status != MR_SUCCESS) |
590 | { | |
d44cee72 | 591 | com_err(whoami, status, "while adding member %s to %s", |
8c3b414d | 592 | memberstruct->name, listname); |
e681e918 | 593 | success = 0; |
594 | } | |
2d7360ca | 595 | } |
596 | } | |
597 | ||
5eaef520 | 598 | /* Process the delete list */ |
599 | while (sq_get_data(dellist, &memberstruct)) | |
600 | { | |
601 | membervec[0] = listname; | |
602 | membervec[2] = memberstruct->name; | |
603 | if (verbose) | |
604 | { | |
605 | printf("Deleting member "); | |
606 | show_list_member(memberstruct); | |
b5b167d6 | 607 | } |
5eaef520 | 608 | switch (memberstruct->type) |
609 | { | |
2d7360ca | 610 | case M_ANY: |
611 | case M_USER: | |
5eaef520 | 612 | membervec[1] = "USER"; |
613 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 614 | NULL, NULL); |
5eaef520 | 615 | if (status == MR_SUCCESS) |
616 | break; | |
617 | else if ((status != MR_USER && status != MR_NO_MATCH) || | |
618 | memberstruct->type != M_ANY) | |
619 | { | |
620 | com_err(whoami, status, "while deleting member %s from %s", | |
621 | memberstruct->name, listname); | |
622 | success = 0; | |
2d7360ca | 623 | break; |
2d7360ca | 624 | } |
625 | case M_LIST: | |
5eaef520 | 626 | membervec[1] = "LIST"; |
627 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 628 | NULL, NULL); |
5eaef520 | 629 | if (status == MR_SUCCESS) |
630 | break; | |
631 | else if ((status != MR_LIST && status != MR_NO_MATCH) || | |
632 | memberstruct->type != M_ANY) | |
633 | { | |
634 | if (status == MR_PERM && memberstruct->type == M_ANY && | |
635 | !strcmp(membervec[2], getenv("USER"))) | |
636 | { | |
637 | /* M_ANY means we've fallen through from the user | |
638 | * case. The user is trying to remove himself from | |
639 | * a list, but we got MR_USER or MR_NO_MATCH above, | |
640 | * meaning he's not really on it, and we got MR_PERM | |
641 | * when trying to remove LIST:$USER because he's not | |
642 | * on the acl. That error is useless, so return | |
643 | * MR_NO_MATCH instead. However, this will generate the | |
644 | * wrong error if the user was trying to remove the list | |
645 | * with his username from a list he doesn't administrate | |
646 | * without explicitly specifying "list:". | |
647 | */ | |
40637dba | 648 | status = MR_NO_MATCH; |
649 | } | |
5eaef520 | 650 | com_err(whoami, status, "while deleting member %s from %s", |
651 | memberstruct->name, listname); | |
652 | success = 0; | |
653 | break; | |
2d7360ca | 654 | } |
655 | case M_STRING: | |
5eaef520 | 656 | membervec[1] = "STRING"; |
657 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 658 | NULL, NULL); |
5eaef520 | 659 | if (status == MR_STRING && memberstruct->type == M_ANY) |
660 | { | |
d44cee72 | 661 | com_err(whoami, 0, " Unable to find member %s to delete from %s", |
2d7360ca | 662 | memberstruct->name, listname); |
e681e918 | 663 | success = 0; |
5eaef520 | 664 | if (!strcmp(membervec[0], getenv("USER"))) |
665 | { | |
666 | fprintf(stderr, "(If you were trying to remove yourself " | |
667 | "from the list \"%s\",\n", membervec[2]); | |
668 | fprintf(stderr, "the correct command is \"blanche %s -d " | |
669 | "%s\".)\n", membervec[2], membervec[0]); | |
670 | } | |
671 | } | |
672 | else if (status != MR_SUCCESS) | |
673 | { | |
d44cee72 | 674 | com_err(whoami, status, "while deleting member %s from %s", |
2d7360ca | 675 | memberstruct->name, listname); |
e681e918 | 676 | success = 0; |
677 | } | |
5eaef520 | 678 | break; |
8c3b414d | 679 | case M_KERBEROS: |
5eaef520 | 680 | membervec[1] = "KERBEROS"; |
681 | status = mr_query("delete_member_from_list", 3, membervec, | |
7ac48069 | 682 | NULL, NULL); |
5eaef520 | 683 | if (status != MR_SUCCESS) |
684 | { | |
d44cee72 | 685 | com_err(whoami, status, "while deleting member %s from %s", |
8c3b414d | 686 | memberstruct->name, listname); |
e681e918 | 687 | success = 0; |
688 | } | |
2d7360ca | 689 | } |
690 | } | |
691 | ||
5eaef520 | 692 | /* Display the members of the list now, if requested */ |
693 | if (memberflg) | |
694 | { | |
695 | if (recursflg) | |
696 | recursive_display_list_members(); | |
697 | else | |
698 | { | |
699 | status = mr_query("get_members_of_list", 1, &listname, | |
7ac48069 | 700 | get_list_members, memberlist); |
5eaef520 | 701 | if (status) |
702 | com_err(whoami, status, "while getting members of list %s", | |
703 | listname); | |
704 | while (sq_get_data(memberlist, &memberstruct)) | |
705 | show_list_member(memberstruct); | |
b5b167d6 | 706 | } |
707 | } | |
2d7360ca | 708 | |
5eaef520 | 709 | /* We're done! */ |
710 | mr_disconnect(); | |
711 | exit(success ? 0 : 1); | |
2d7360ca | 712 | } |
713 | ||
7ac48069 | 714 | void usage(char **argv) |
2d7360ca | 715 | { |
5eaef520 | 716 | fprintf(stderr, "Usage: %s listname [options]\n", argv[0]); |
717 | fprintf(stderr, "Options are\n"); | |
718 | fprintf(stderr, " %-39s%-39s\n", "-v | -verbose", | |
719 | "-C | -create"); | |
720 | fprintf(stderr, " %-39s%-39s\n", "-m | -members", | |
721 | "-R | -rename newname"); | |
722 | fprintf(stderr, " %-39s%-39s\n", "-u | -users", | |
723 | "-P | -public"); | |
724 | fprintf(stderr, " %-39s%-39s\n", "-l | -lists", | |
725 | "-NP | -private"); | |
726 | fprintf(stderr, " %-39s%-39s\n", "-s | -strings", | |
727 | "-A | -active"); | |
728 | fprintf(stderr, " %-39s%-39s\n", "-k | -kerberos", | |
729 | "-I | -inactive"); | |
730 | fprintf(stderr, " %-39s%-39s\n", "-i | -info", | |
731 | "-V | -visible"); | |
732 | fprintf(stderr, " %-39s%-39s\n", "-r | -recursive", | |
733 | "-H | -hidden"); | |
734 | fprintf(stderr, " %-39s%-39s\n", "-a | -add member", | |
735 | "-M | -mail"); | |
736 | fprintf(stderr, " %-39s%-39s\n", "-d | -delete member", | |
737 | "-NM | -notmail"); | |
738 | fprintf(stderr, " %-39s%-39s\n", "-al | -addlist filename", | |
739 | "-G | -group"); | |
740 | fprintf(stderr, " %-39s%-39s\n", "-dl | -deletelist filename", | |
741 | "-NG | -notgroup"); | |
742 | fprintf(stderr, " %-39s%-39s\n", "-f | -file filename", | |
743 | "-D | -desc description"); | |
744 | fprintf(stderr, " %-39s%-39s\n", "-n | -noauth", | |
745 | "-O | -owner owner"); | |
746 | fprintf(stderr, " %-39s%-39s\n", "-db | -database host[:port]", | |
747 | ""); | |
748 | exit(1); | |
2d7360ca | 749 | } |
750 | ||
751 | ||
b5b167d6 | 752 | /* Display the members stored in the queue */ |
1b6b0a57 | 753 | |
7ac48069 | 754 | void show_list_member(struct member *memberstruct) |
b5b167d6 | 755 | { |
5eaef520 | 756 | char *s = ""; |
8b494222 | 757 | |
5eaef520 | 758 | switch (memberstruct->type) |
759 | { | |
8b494222 | 760 | case M_USER: |
5eaef520 | 761 | if (!showusers) |
762 | return; | |
763 | s = "USER"; | |
764 | break; | |
8b494222 | 765 | case M_LIST: |
5eaef520 | 766 | if (!showlists) |
767 | return; | |
768 | s = "LIST"; | |
769 | break; | |
8b494222 | 770 | case M_STRING: |
5eaef520 | 771 | if (!showstrings) |
772 | return; | |
773 | s = "STRING"; | |
774 | break; | |
8c3b414d | 775 | case M_KERBEROS: |
5eaef520 | 776 | if (!showkerberos) |
8b494222 | 777 | return; |
5eaef520 | 778 | s = "KERBEROS"; |
779 | break; | |
780 | case M_ANY: | |
781 | printf("%s\n", memberstruct->name); | |
782 | return; | |
8b494222 | 783 | } |
784 | ||
5eaef520 | 785 | if (verbose) |
786 | printf("%s:%s\n", s, memberstruct->name); | |
787 | else | |
788 | { | |
789 | if (memberstruct->type == M_LIST) | |
790 | printf("LIST:%s\n", memberstruct->name); | |
791 | else if (memberstruct->type == M_KERBEROS) | |
792 | printf("KERBEROS:%s\n", memberstruct->name); | |
793 | else if (memberstruct->type == M_STRING && | |
794 | !strchr(memberstruct->name, '@')) | |
795 | printf("STRING:%s\n", memberstruct->name); | |
796 | else | |
797 | printf("%s\n", memberstruct->name); | |
1b6b0a57 | 798 | } |
799 | } | |
800 | ||
b5b167d6 | 801 | |
802 | /* Show the retrieved information about a list */ | |
803 | ||
7ac48069 | 804 | int show_list_info(int argc, char **argv, void *hint) |
2d7360ca | 805 | { |
5eaef520 | 806 | printf("List: %s\n", argv[0]); |
807 | printf("Description: %s\n", argv[9]); | |
808 | printf("Flags: %s, %s, and %s\n", | |
809 | atoi(argv[1]) ? "active" : "inactive", | |
810 | atoi(argv[2]) ? "public" : "private", | |
811 | atoi(argv[3]) ? "hidden" : "visible"); | |
812 | printf("%s is %sa maillist and is %sa group", argv[0], | |
813 | atoi(argv[4]) ? "" : "not ", | |
814 | atoi(argv[5]) ? "" : "not "); | |
815 | if (atoi(argv[5])) | |
816 | printf(" with GID %d\n", atoi(argv[6])); | |
817 | else | |
818 | printf("\n"); | |
819 | printf("Owner: %s %s\n", argv[7], argv[8]); | |
820 | printf("Last modified by %s with %s on %s\n", argv[11], argv[12], argv[10]); | |
821 | return MR_CONT; | |
2d7360ca | 822 | } |
823 | ||
824 | ||
763465b8 | 825 | /* Copy retrieved information about a list into a new argv */ |
826 | ||
7ac48069 | 827 | int save_list_info(int argc, char **argv, void *hint) |
763465b8 | 828 | { |
7ac48069 | 829 | char **nargv = hint; |
5eaef520 | 830 | |
831 | for (argc = 0; argc < 10; argc++) | |
832 | nargv[argc + 1] = strdup(argv[argc]); | |
833 | return MR_CONT; | |
763465b8 | 834 | } |
835 | ||
b5b167d6 | 836 | /* Show the retrieve list member count */ |
837 | ||
7ac48069 | 838 | int show_list_count(int argc, char **argv, void *hint) |
2d7360ca | 839 | { |
5eaef520 | 840 | printf("Members: %s\n", argv[0]); |
7ac48069 | 841 | return MR_CONT; |
2d7360ca | 842 | } |
843 | ||
844 | ||
b5b167d6 | 845 | /* Recursively find all of the members of listname, and then display them */ |
846 | ||
7ac48069 | 847 | void recursive_display_list_members(void) |
1b6b0a57 | 848 | { |
5eaef520 | 849 | int status, count, savecount; |
850 | struct save_queue *lists, *members; | |
851 | struct member *m, *m1, *data; | |
852 | ||
853 | lists = sq_create(); | |
854 | members = sq_create(); | |
855 | m = malloc(sizeof(struct member)); | |
856 | m->type = M_LIST; | |
857 | m->name = listname; | |
858 | sq_save_data(lists, m); | |
859 | ||
860 | while (sq_get_data(lists, &m)) | |
861 | { | |
862 | sq_destroy(memberlist); | |
863 | memberlist = sq_create(); | |
864 | status = mr_query("get_members_of_list", 1, &(m->name), | |
7ac48069 | 865 | get_list_members, memberlist); |
5eaef520 | 866 | if (status) |
867 | com_err(whoami, status, "while getting members of list %s", m->name); | |
868 | while (sq_get_data(memberlist, &m1)) | |
869 | { | |
870 | if (m1->type == M_LIST) | |
871 | unique_add_member(lists, m1); | |
872 | else | |
873 | unique_add_member(members, m1); | |
b5b167d6 | 874 | } |
875 | } | |
5eaef520 | 876 | savecount = count = sq_count_elts(members); |
877 | data = malloc(count * sizeof(struct member)); | |
878 | count = 0; | |
879 | while (sq_get_data(members, &m)) | |
880 | memcpy(&data[count++], m, sizeof(struct member)); | |
881 | qsort(data, count, sizeof(struct member), membercmp); | |
882 | for (count = 0; count < savecount; count++) | |
883 | show_list_member(&data[count]); | |
b5b167d6 | 884 | } |
885 | ||
886 | ||
887 | /* add a struct member to a queue if that member isn't already there. */ | |
888 | ||
7ac48069 | 889 | void unique_add_member(struct save_queue *q, struct member *m) |
b5b167d6 | 890 | { |
5eaef520 | 891 | struct save_queue *qp; |
1b6b0a57 | 892 | |
5eaef520 | 893 | for (qp = q->q_next; qp != q; qp = qp->q_next) |
894 | { | |
895 | if (!membercmp(qp->q_data, m)) | |
896 | return; | |
b5b167d6 | 897 | } |
5eaef520 | 898 | sq_save_data(q, m); |
1b6b0a57 | 899 | } |
900 | ||
b5b167d6 | 901 | |
902 | /* Collect the retrieved members of the list */ | |
903 | ||
7ac48069 | 904 | int get_list_members(int argc, char **argv, void *sq) |
2d7360ca | 905 | { |
7ac48069 | 906 | struct save_queue *q = sq; |
5eaef520 | 907 | struct member *m; |
2d7360ca | 908 | |
5eaef520 | 909 | m = malloc(sizeof(struct member)); |
910 | switch (argv[0][0]) | |
911 | { | |
2d7360ca | 912 | case 'U': |
5eaef520 | 913 | m->type = M_USER; |
914 | break; | |
2d7360ca | 915 | case 'L': |
5eaef520 | 916 | m->type = M_LIST; |
917 | break; | |
2d7360ca | 918 | case 'S': |
5eaef520 | 919 | m->type = M_STRING; |
920 | break; | |
8c3b414d | 921 | case 'K': |
5eaef520 | 922 | m->type = M_KERBEROS; |
923 | break; | |
2d7360ca | 924 | } |
7ac48069 | 925 | m->name = strdup(argv[1]); |
5eaef520 | 926 | sq_save_data(q, m); |
927 | return MR_CONT; | |
2d7360ca | 928 | } |
929 | ||
930 | ||
7c02cbdb | 931 | /* Open file, parse members from file, and put them on the specified queue */ |
7ac48069 | 932 | void get_members_from_file(char *filename, struct save_queue *queue) |
7c02cbdb | 933 | { |
5eaef520 | 934 | FILE *in; |
935 | char buf[BUFSIZ]; | |
936 | struct member *memberstruct; | |
937 | ||
938 | if (!strcmp(filename, "-")) | |
939 | in = stdin; | |
940 | else | |
941 | { | |
942 | in = fopen(filename, "r"); | |
943 | if (!in) | |
944 | { | |
945 | com_err(whoami, errno, "while opening %s for input", filename); | |
946 | exit(2); | |
7c02cbdb | 947 | } |
948 | } | |
949 | ||
5eaef520 | 950 | while (fgets(buf, BUFSIZ, in)) |
951 | { | |
952 | if ((memberstruct = parse_member(buf))) | |
7c02cbdb | 953 | sq_save_data(queue, memberstruct); |
5eaef520 | 954 | } |
955 | if (!feof(in)) | |
956 | { | |
7c02cbdb | 957 | com_err(whoami, errno, "while reading from %s", filename); |
e681e918 | 958 | exit(2); |
959 | } | |
7c02cbdb | 960 | } |
961 | ||
962 | ||
99bdafb8 | 963 | /* Collect the possible expansions of the alias MAILHUB */ |
964 | ||
7ac48069 | 965 | int collect(int argc, char **argv, void *l) |
99bdafb8 | 966 | { |
7ac48069 | 967 | char ***list = l; |
5eaef520 | 968 | int i; |
969 | ||
970 | for (i = 0; (*list)[i]; i++) | |
971 | ; | |
972 | *list = realloc(*list, (i + 2) * sizeof(char *)); | |
7ac48069 | 973 | (*list)[i] = strdup(argv[2]); |
5eaef520 | 974 | (*list)[i + 1] = NULL; |
975 | return MR_CONT; | |
99bdafb8 | 976 | } |
977 | ||
978 | ||
b5b167d6 | 979 | /* Parse a line of input, fetching a member. NULL is returned if a member |
7c02cbdb | 980 | * is not found. ';' is a comment character. |
b5b167d6 | 981 | */ |
982 | ||
44d12d58 | 983 | struct member *parse_member(char *s) |
2d7360ca | 984 | { |
44d12d58 | 985 | struct member *m; |
5eaef520 | 986 | char *p, *lastchar; |
987 | ||
988 | while (*s && isspace(*s)) | |
989 | s++; | |
990 | lastchar = p = s; | |
991 | while (*p && *p != '\n' && *p != ';') | |
992 | { | |
7c02cbdb | 993 | if (isprint(*p) && !isspace(*p)) |
994 | lastchar = p++; | |
995 | else | |
996 | p++; | |
5eaef520 | 997 | } |
998 | lastchar++; | |
999 | *lastchar = '\0'; | |
1000 | if (p == s || strlen(s) == 0) | |
1001 | return NULL; | |
1002 | ||
1003 | if (!(m = malloc(sizeof(struct member)))) | |
1004 | return NULL; | |
1005 | ||
1006 | if ((p = strchr(s, ':'))) | |
1007 | { | |
1008 | *p = '\0'; | |
1009 | m->name = ++p; | |
1010 | if (!strcasecmp("user", s)) | |
1011 | m->type = M_USER; | |
1012 | else if (!strcasecmp("list", s)) | |
1013 | m->type = M_LIST; | |
1014 | else if (!strcasecmp("string", s)) | |
1015 | m->type = M_STRING; | |
1016 | else if (!strcasecmp("kerberos", s)) | |
1017 | m->type = M_KERBEROS; | |
1018 | else | |
1019 | { | |
1020 | m->type = M_ANY; | |
1021 | *(--p) = ':'; | |
1022 | m->name = s; | |
2d7360ca | 1023 | } |
7ac48069 | 1024 | m->name = strdup(m->name); |
5eaef520 | 1025 | } |
1026 | else | |
1027 | { | |
7ac48069 | 1028 | m->name = strdup(s); |
5eaef520 | 1029 | m->type = M_ANY; |
2d7360ca | 1030 | } |
5eaef520 | 1031 | return m; |
2d7360ca | 1032 | } |
1033 | ||
1034 | ||
5eaef520 | 1035 | /* |
1036 | * This routine two compares members by the following rules: | |
1037 | * 1. A USER is less than a LIST | |
1038 | * 2. A LIST is less than a STRING | |
1039 | * 3. If two members are of the same type, the one alphabetically first | |
1040 | * is less than the other | |
1041 | * It returs < 0 if the first member is less, 0 if they are identical, and | |
1042 | * > 0 if the second member is less (the first member is greater). | |
1043 | */ | |
b5b167d6 | 1044 | |
7ac48069 | 1045 | int membercmp(const void *mem1, const void *mem2) |
2d7360ca | 1046 | { |
7ac48069 | 1047 | const struct member *m1 = mem1, *m2 = mem2; |
1048 | ||
5eaef520 | 1049 | if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type)) |
1050 | return strcmp(m1->name, m2->name); | |
1051 | else | |
1052 | return m1->type - m2->type; | |
2d7360ca | 1053 | } |
b5b167d6 | 1054 | |
1055 | ||
5eaef520 | 1056 | int sq_count_elts(struct save_queue *q) |
b5b167d6 | 1057 | { |
5eaef520 | 1058 | char *foo; |
1059 | int count; | |
b5b167d6 | 1060 | |
5eaef520 | 1061 | count = 0; |
1062 | while (sq_get_data(q, &foo)) | |
1063 | count++; | |
1064 | return count; | |
b5b167d6 | 1065 | } |