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