]> andersk Git - moira.git/blob - clients/blanche/blanche.c
set "success" to 0 in a few more places (like, if "blanche -i" fails)
[moira.git] / clients / blanche / blanche.c
1 /* $Id$
2  *
3  * Command line oriented Moira List tool.
4  *
5  * by Mark Rosenstein, September 1988.
6  *
7  * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
8  * For copying and distribution information, please see the file
9  * <mit-copyright.h>.
10  */
11
12 #include <mit-copyright.h>
13 #include <moira.h>
14 #include <moira_site.h>
15 #include <mrclient.h>
16
17 #include <ctype.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 RCSID("$Header$");
24
25 struct member {
26   int type;
27   char *name;
28 };
29
30 /* It is important to membercmp that M_USER < M_LIST < M_STRING */
31 #define M_ANY           0
32 #define M_USER          1
33 #define M_LIST          2
34 #define M_STRING        3
35 #define M_KERBEROS      4
36
37 /* argument parsing macro */
38 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
39
40 /* flags from command line */
41 int infoflg, verbose, syncflg, memberflg, recursflg, noauth;
42 int showusers, showstrings, showkerberos, showlists;
43 int createflag, setinfo, active, public, hidden, maillist, grouplist;
44 struct member *owner;
45 char *desc, *newname;
46
47 /* various member lists */
48 struct save_queue *addlist, *dellist, *memberlist, *synclist;
49
50 char *listname, *whoami;
51
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);
65
66 int main(int argc, char **argv)
67 {
68   int status, success;
69   char **arg = argv;
70   char *membervec[3];
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 == '-')
93         {
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
258                 usage(argv);
259             }
260           else
261             usage(argv);
262         }
263       else if (listname == NULL)
264         listname = *arg;
265       else
266         usage(argv);
267     }
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 */
281   status = mrcl_connect(server, "blanche", !noauth);
282   if (status == MRCL_AUTH_ERROR)
283     {
284       com_err(whoami, 0, "Try the -noauth flag if you don't "
285               "need authentication.");
286     }
287   if (status)
288     exit(2);
289
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
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";
322               status = mr_query("add_list", 10, argv, NULL, NULL);
323               if (owner->type != M_ANY || status != MR_USER)
324                 break;
325
326             case M_LIST:
327               argv[7] = "LIST";
328               status = mr_query("add_list", 10, argv, NULL, NULL);
329               break;
330
331             case M_KERBEROS:
332               argv[7] = "KERBEROS";
333               status = mr_query("add_list", 10, argv, NULL, NULL);
334               break;
335             }
336         }
337       else
338         {
339           argv[7] = "USER";
340           argv[8] = getenv("USER");
341
342           status = mr_query("add_list", 10, argv, NULL, NULL);
343         }
344
345       if (status)
346         {
347           com_err(whoami, status, "while creating list.");
348           exit(1);
349         }
350     }
351   else if (setinfo)
352     {
353       char *argv[11];
354
355       status = mr_query("get_list_info", 1, &listname,
356                         save_list_info, argv);
357       if (status)
358         {
359           com_err(whoami, status, "while getting list information");
360           exit(1);
361         }
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";
387               status = mr_query("update_list", 11, argv, NULL, NULL);
388               if (owner->type != M_ANY || status != MR_USER)
389                 break;
390
391             case M_LIST:
392               argv[8] = "LIST";
393               status = mr_query("update_list", 11, argv, NULL, NULL);
394               break;
395
396             case M_KERBEROS:
397               argv[8] = "KERBEROS";
398               status = mr_query("update_list", 11, argv, NULL, NULL);
399               break;
400             }
401         }
402       else
403         status = mr_query("update_list", 11, argv, NULL, NULL);
404
405       if (status)
406         {
407           com_err(whoami, status, "while updating list.");
408           success = 0;
409         }
410       else if (newname)
411         listname = newname;
412     }
413
414   /* display list info if requested to */
415   if (infoflg)
416     {
417       status = mr_query("get_list_info", 1, &listname, show_list_info, NULL);
418       if (status)
419         {
420           com_err(whoami, status, "while getting list information");
421           success = 0;
422         }
423       if (verbose && !memberflg)
424         {
425           status = mr_query("count_members_of_list", 1, &listname,
426                             show_list_count, NULL);
427           if (status)
428             {
429               com_err(whoami, status, "while getting list count");
430               success = 0;
431             }
432         }
433     }
434
435   /* if we're synchronizing to a file, we need to:
436    *  get the current members of the list
437    *    for each member of the sync file
438    *       if they are on the list, remove them from the in-memory copy
439    *       if they're not on the list, add them to add-list
440    *    if anyone is left on the in-memory copy, put them on the delete-list
441    * lastly, reset memberlist so we can use it again later
442    */
443   if (syncflg)
444     {
445       status = mr_query("get_members_of_list", 1, &listname,
446                         get_list_members, memberlist);
447       if (status)
448         {
449           com_err(whoami, status, "getting members of list %s", listname);
450           exit(2);
451         }
452       while (sq_get_data(synclist, &memberstruct))
453         {
454           struct save_queue *q;
455           int removed = 0;
456
457           for (q = memberlist->q_next; q != memberlist; q = q->q_next)
458             {
459               if (membercmp(q->q_data, memberstruct) == 0)
460                 {
461                   q->q_prev->q_next = q->q_next;
462                   q->q_next->q_prev = q->q_prev;
463                   removed++;
464                   break;
465                 }
466             }
467           if (!removed)
468             sq_save_data(addlist, memberstruct);
469         }
470       while (sq_get_data(memberlist, &memberstruct))
471         sq_save_data(dellist, memberstruct);
472       sq_destroy(memberlist);
473       memberlist = sq_create();
474     }
475
476   /* Process the add list */
477   while (sq_get_data(addlist, &memberstruct))
478     {
479       /* canonicalize string if necessary */
480       if (memberstruct->type != M_KERBEROS &&
481           (p = strchr(memberstruct->name, '@')))
482         {
483           char *host = canonicalize_hostname(strdup(++p));
484           static char **mailhubs = NULL;
485           char *argv[4];
486           int i;
487
488           if (!mailhubs)
489             {
490               argv[0] = "mailhub";
491               argv[1] = "TYPE";
492               argv[2] = "*";
493               mailhubs = malloc(sizeof(char *));
494               mailhubs[0] = NULL;
495               status = mr_query("get_alias", 3, argv, collect,
496                                 &mailhubs);
497               if (status != MR_SUCCESS && status != MR_NO_MATCH)
498                 {
499                   com_err(whoami, status,
500                           " while reading list of MAILHUB servers");
501                   mailhubs[0] = NULL;
502                 }
503             }
504           for (i = 0; (p = mailhubs[i]); i++)
505             {
506               if (!strcasecmp(p, host))
507                 {
508                   host = strdup(memberstruct->name);
509                   *(strchr(memberstruct->name, '@')) = 0;
510                   if (memberstruct->type == M_STRING)
511                       memberstruct->type = M_ANY;
512                   fprintf(stderr, "Warning: \"%s\" converted to "
513                           "\"%s\" because it is a local name.\n",
514                           host, memberstruct->name);
515                   break;
516                 }
517             }
518           free(host);
519         }
520       /* now continue adding member */
521       membervec[0] = listname;
522       membervec[2] = memberstruct->name;
523       if (verbose)
524         {
525           printf("Adding member ");
526           show_list_member(memberstruct);
527         }
528       switch (memberstruct->type)
529         {
530         case M_ANY:
531         case M_USER:
532           membervec[1] = "USER";
533           status = mr_query("add_member_to_list", 3, membervec, NULL, NULL);
534           if (status == MR_SUCCESS)
535             break;
536           else if (status != MR_USER || memberstruct->type != M_ANY)
537             {
538               com_err(whoami, status, "while adding member %s to %s",
539                       memberstruct->name, listname);
540               success = 0;
541               break;
542             }
543         case M_LIST:
544           membervec[1] = "LIST";
545           status = mr_query("add_member_to_list", 3, membervec,
546                             NULL, NULL);
547           if (status == MR_SUCCESS)
548             {
549               if (!strcmp(membervec[0], getenv("USER")))
550                 {
551                   fprintf(stderr, "\nWARNING: \"LIST:%s\" was just added "
552                           "to list \"%s\".\n", membervec[2], membervec[0]);
553                   fprintf(stderr, "If you meant to add yourself to the list "
554                           "\"%s\", type:\n", membervec[2]);
555                   fprintf(stderr, "\tblanche %s -d %s\t(to undo this)\n",
556                           membervec[0], membervec[2]);
557                   fprintf(stderr, "\tblanche %s -a %s\t(to add yourself to "
558                           "that list)\n", membervec[2], membervec[0]);
559                 }
560               break;
561             }
562           else if (status != MR_LIST || memberstruct->type != M_ANY)
563             {
564               com_err(whoami, status, "while adding member %s to %s",
565                       memberstruct->name, listname);
566               success = 0;
567               break;
568             }
569         case M_STRING:
570           if (memberstruct->type == M_ANY &&
571               !strchr(memberstruct->name, '@') &&
572               !strchr(memberstruct->name, '!') &&
573               !strchr(memberstruct->name, '%'))
574             {
575               /* if user is trying to add something which isn't a
576                  remote string, or a list, or a user, and didn't
577                  explicitly specify `STRING:', it's probably a typo */
578               com_err(whoami, MR_NO_MATCH, "while adding member %s to %s",
579                       memberstruct->name, listname);
580               success = 0;
581               break;
582             }
583
584           membervec[1] = "STRING";
585           status = mr_query("add_member_to_list", 3, membervec,
586                             NULL, NULL);
587           if (status != MR_SUCCESS)
588             {
589               com_err(whoami, status, "while adding member %s to %s",
590                       memberstruct->name, listname);
591               success = 0;
592             }
593           break;
594         case M_KERBEROS:
595           membervec[1] = "KERBEROS";
596           status = mr_query("add_member_to_list", 3, membervec,
597                             NULL, NULL);
598           if (status != MR_SUCCESS)
599             {
600               com_err(whoami, status, "while adding member %s to %s",
601                       memberstruct->name, listname);
602               success = 0;
603             }
604         }
605     }
606
607   /* Process the delete list */
608   while (sq_get_data(dellist, &memberstruct))
609     {
610       membervec[0] = listname;
611       membervec[2] = memberstruct->name;
612       if (verbose)
613         {
614           printf("Deleting member ");
615           show_list_member(memberstruct);
616         }
617       switch (memberstruct->type)
618         {
619         case M_ANY:
620         case M_USER:
621           membervec[1] = "USER";
622           status = mr_query("delete_member_from_list", 3, membervec,
623                             NULL, NULL);
624           if (status == MR_SUCCESS)
625             break;
626           else if ((status != MR_USER && status != MR_NO_MATCH) ||
627                    memberstruct->type != M_ANY)
628             {
629               com_err(whoami, status, "while deleting member %s from %s",
630                       memberstruct->name, listname);
631               success = 0;
632               break;
633             }
634         case M_LIST:
635           membervec[1] = "LIST";
636           status = mr_query("delete_member_from_list", 3, membervec,
637                             NULL, NULL);
638           if (status == MR_SUCCESS)
639             break;
640           else if ((status != MR_LIST && status != MR_NO_MATCH) ||
641                    memberstruct->type != M_ANY)
642             {
643               if (status == MR_PERM && memberstruct->type == M_ANY &&
644                   !strcmp(membervec[2], getenv("USER")))
645                 {
646                   /* M_ANY means we've fallen through from the user
647                    * case. The user is trying to remove himself from
648                    * a list, but we got MR_USER or MR_NO_MATCH above,
649                    * meaning he's not really on it, and we got MR_PERM
650                    * when trying to remove LIST:$USER because he's not
651                    * on the acl. That error is useless, so return
652                    * MR_NO_MATCH instead. However, this will generate the
653                    * wrong error if the user was trying to remove the list
654                    * with his username from a list he doesn't administrate
655                    * without explicitly specifying "list:".
656                    */
657                   status = MR_NO_MATCH;
658                 }
659               com_err(whoami, status, "while deleting member %s from %s",
660                       memberstruct->name, listname);
661               success = 0;
662               break;
663             }
664         case M_STRING:
665           membervec[1] = "STRING";
666           status = mr_query("delete_member_from_list", 3, membervec,
667                             NULL, NULL);
668           if (status == MR_STRING && memberstruct->type == M_ANY)
669             {
670               com_err(whoami, 0, " Unable to find member %s to delete from %s",
671                       memberstruct->name, listname);
672               success = 0;
673               if (!strcmp(membervec[0], getenv("USER")))
674                 {
675                   fprintf(stderr, "(If you were trying to remove yourself "
676                           "from the list \"%s\",\n", membervec[2]);
677                   fprintf(stderr, "the correct command is \"blanche %s -d "
678                           "%s\".)\n", membervec[2], membervec[0]);
679                 }
680             }
681           else if (status != MR_SUCCESS)
682             {
683               com_err(whoami, status, "while deleting member %s from %s",
684                       memberstruct->name, listname);
685               success = 0;
686             }
687           break;
688         case M_KERBEROS:
689           membervec[1] = "KERBEROS";
690           status = mr_query("delete_member_from_list", 3, membervec,
691                             NULL, NULL);
692           if (status != MR_SUCCESS)
693             {
694               com_err(whoami, status, "while deleting member %s from %s",
695                       memberstruct->name, listname);
696               success = 0;
697             }
698         }
699     }
700
701   /* Display the members of the list now, if requested */
702   if (memberflg)
703     {
704       if (recursflg)
705         recursive_display_list_members();
706       else
707         {
708           status = mr_query("get_members_of_list", 1, &listname,
709                             get_list_members, memberlist);
710           if (status)
711             com_err(whoami, status, "while getting members of list %s",
712                     listname);
713           while (sq_get_data(memberlist, &memberstruct))
714             show_list_member(memberstruct);
715         }
716     }
717
718   /* We're done! */
719   mr_disconnect();
720   exit(success ? 0 : 1);
721 }
722
723 void usage(char **argv)
724 {
725   fprintf(stderr, "Usage: %s listname [options]\n", argv[0]);
726   fprintf(stderr, "Options are\n");
727   fprintf(stderr, "  %-39s%-39s\n", "-v  | -verbose",
728           "-C  | -create");
729   fprintf(stderr, "  %-39s%-39s\n", "-m  | -members",
730           "-R  | -rename newname");
731   fprintf(stderr, "  %-39s%-39s\n", "-u  | -users",
732           "-P  | -public");
733   fprintf(stderr, "  %-39s%-39s\n", "-l  | -lists",
734           "-NP | -private");
735   fprintf(stderr, "  %-39s%-39s\n", "-s  | -strings",
736           "-A  | -active");
737   fprintf(stderr, "  %-39s%-39s\n", "-k  | -kerberos",
738           "-I  | -inactive");
739   fprintf(stderr, "  %-39s%-39s\n", "-i  | -info",
740           "-V  | -visible");
741   fprintf(stderr, "  %-39s%-39s\n", "-r  | -recursive",
742           "-H  | -hidden");
743   fprintf(stderr, "  %-39s%-39s\n", "-a  | -add member",
744           "-M  | -mail");
745   fprintf(stderr, "  %-39s%-39s\n", "-d  | -delete member",
746           "-NM | -notmail");
747   fprintf(stderr, "  %-39s%-39s\n", "-al | -addlist filename",
748           "-G  | -group");
749   fprintf(stderr, "  %-39s%-39s\n", "-dl | -deletelist filename",
750           "-NG | -notgroup");
751   fprintf(stderr, "  %-39s%-39s\n", "-f  | -file filename",
752           "-D  | -desc description");
753   fprintf(stderr, "  %-39s%-39s\n", "-n  | -noauth",
754           "-O  | -owner owner");
755   fprintf(stderr, "  %-39s%-39s\n", "-db | -database host[:port]",
756           "");
757   exit(1);
758 }
759
760
761 /* Display the members stored in the queue */
762
763 void show_list_member(struct member *memberstruct)
764 {
765   char *s = "";
766
767   switch (memberstruct->type)
768     {
769     case M_USER:
770       if (!showusers)
771         return;
772       s = "USER";
773       break;
774     case M_LIST:
775       if (!showlists)
776         return;
777       s = "LIST";
778       break;
779     case M_STRING:
780       if (!showstrings)
781         return;
782       s = "STRING";
783       break;
784     case M_KERBEROS:
785       if (!showkerberos)
786         return;
787       s = "KERBEROS";
788       break;
789     case M_ANY:
790       printf("%s\n", memberstruct->name);
791       return;
792     }
793
794   if (verbose)
795     printf("%s:%s\n", s, memberstruct->name);
796   else
797     {
798       if (memberstruct->type == M_LIST)
799         printf("LIST:%s\n", memberstruct->name);
800       else if (memberstruct->type == M_KERBEROS)
801         printf("KERBEROS:%s\n", memberstruct->name);
802       else if (memberstruct->type == M_STRING &&
803                !strchr(memberstruct->name, '@'))
804         printf("STRING:%s\n", memberstruct->name);
805       else
806         printf("%s\n", memberstruct->name);
807     }
808 }
809
810
811 /* Show the retrieved information about a list */
812
813 int show_list_info(int argc, char **argv, void *hint)
814 {
815   printf("List: %s\n", argv[0]);
816   printf("Description: %s\n", argv[9]);
817   printf("Flags: %s, %s, and %s\n",
818          atoi(argv[1]) ? "active" : "inactive",
819          atoi(argv[2]) ? "public" : "private",
820          atoi(argv[3]) ? "hidden" : "visible");
821   printf("%s is %sa maillist and is %sa group", argv[0],
822          atoi(argv[4]) ? "" : "not ",
823          atoi(argv[5]) ? "" : "not ");
824   if (atoi(argv[5]))
825     printf(" with GID %d\n", atoi(argv[6]));
826   else
827     printf("\n");
828   printf("Owner: %s %s\n", argv[7], argv[8]);
829   printf("Last modified by %s with %s on %s\n", argv[11], argv[12], argv[10]);
830   return MR_CONT;
831 }
832
833
834 /* Copy retrieved information about a list into a new argv */
835
836 int save_list_info(int argc, char **argv, void *hint)
837 {
838   char **nargv = hint;
839
840   for (argc = 0; argc < 10; argc++)
841     nargv[argc + 1] = strdup(argv[argc]);
842   return MR_CONT;
843 }
844
845 /* Show the retrieve list member count */
846
847 int show_list_count(int argc, char **argv, void *hint)
848 {
849   printf("Members: %s\n", argv[0]);
850   return MR_CONT;
851 }
852
853
854 /* Recursively find all of the members of listname, and then display them */
855
856 void recursive_display_list_members(void)
857 {
858   int status, count, savecount;
859   struct save_queue *lists, *members;
860   struct member *m, *m1, *data;
861
862   lists = sq_create();
863   members = sq_create();
864   m = malloc(sizeof(struct member));
865   m->type = M_LIST;
866   m->name = listname;
867   sq_save_data(lists, m);
868
869   while (sq_get_data(lists, &m))
870     {
871       sq_destroy(memberlist);
872       memberlist = sq_create();
873       status = mr_query("get_members_of_list", 1, &(m->name),
874                         get_list_members, memberlist);
875       if (status)
876         com_err(whoami, status, "while getting members of list %s", m->name);
877       while (sq_get_data(memberlist, &m1))
878         {
879           if (m1->type == M_LIST)
880             unique_add_member(lists, m1);
881           else
882             unique_add_member(members, m1);
883         }
884     }
885   savecount = count = sq_count_elts(members);
886   data = malloc(count * sizeof(struct member));
887   count = 0;
888   while (sq_get_data(members, &m))
889     memcpy(&data[count++], m, sizeof(struct member));
890   qsort(data, count, sizeof(struct member), membercmp);
891   for (count = 0; count < savecount; count++)
892     show_list_member(&data[count]);
893 }
894
895
896 /* add a struct member to a queue if that member isn't already there. */
897
898 void unique_add_member(struct save_queue *q, struct member *m)
899 {
900   struct save_queue *qp;
901
902   for (qp = q->q_next; qp != q; qp = qp->q_next)
903     {
904       if (!membercmp(qp->q_data, m))
905         return;
906     }
907   sq_save_data(q, m);
908 }
909
910
911 /* Collect the retrieved members of the list */
912
913 int get_list_members(int argc, char **argv, void *sq)
914 {
915   struct save_queue *q = sq;
916   struct member *m;
917
918   m = malloc(sizeof(struct member));
919   switch (argv[0][0])
920     {
921     case 'U':
922       m->type = M_USER;
923       break;
924     case 'L':
925       m->type = M_LIST;
926       break;
927     case 'S':
928       m->type = M_STRING;
929       break;
930     case 'K':
931       m->type = M_KERBEROS;
932       break;
933     }
934   m->name = strdup(argv[1]);
935   sq_save_data(q, m);
936   return MR_CONT;
937 }
938
939
940 /* Open file, parse members from file, and put them on the specified queue */
941 void get_members_from_file(char *filename, struct save_queue *queue)
942 {
943   FILE *in;
944   char buf[BUFSIZ];
945   struct member *memberstruct;
946
947   if (!strcmp(filename, "-"))
948     in = stdin;
949   else
950     {
951       in = fopen(filename, "r");
952       if (!in)
953         {
954           com_err(whoami, errno, "while opening %s for input", filename);
955           exit(2);
956         }
957     }
958
959   while (fgets(buf, BUFSIZ, in))
960     {
961       if ((memberstruct = parse_member(buf)))
962         sq_save_data(queue, memberstruct);
963     }
964   if (!feof(in))
965     {
966       com_err(whoami, errno, "while reading from %s", filename);
967       exit(2);
968     }
969 }
970
971
972 /* Collect the possible expansions of the alias MAILHUB */
973
974 int collect(int argc, char **argv, void *l)
975 {
976   char ***list = l;
977   int i;
978
979   for (i = 0; (*list)[i]; i++)
980     ;
981   *list = realloc(*list, (i + 2) * sizeof(char *));
982   (*list)[i] = strdup(argv[2]);
983   (*list)[i + 1] = NULL;
984   return MR_CONT;
985 }
986
987
988 /* Parse a line of input, fetching a member.  NULL is returned if a member
989  * is not found.  ';' is a comment character.
990  */
991
992 struct member *parse_member(char *s)
993 {
994   struct member *m;
995   char *p, *lastchar;
996
997   while (*s && isspace(*s))
998     s++;
999   lastchar = p = s;
1000   while (*p && *p != '\n' && *p != ';')
1001     {
1002       if (isprint(*p) && !isspace(*p))
1003         lastchar = p++;
1004       else
1005         p++;
1006     }
1007   lastchar++;
1008   *lastchar = '\0';
1009   if (p == s || strlen(s) == 0)
1010     return NULL;
1011
1012   if (!(m = malloc(sizeof(struct member))))
1013     return NULL;
1014
1015   if ((p = strchr(s, ':')))
1016     {
1017       *p = '\0';
1018       m->name = ++p;
1019       if (!strcasecmp("user", s))
1020         m->type = M_USER;
1021       else if (!strcasecmp("list", s))
1022         m->type = M_LIST;
1023       else if (!strcasecmp("string", s))
1024         m->type = M_STRING;
1025       else if (!strcasecmp("kerberos", s))
1026         m->type = M_KERBEROS;
1027       else
1028         {
1029           m->type = M_ANY;
1030           *(--p) = ':';
1031           m->name = s;
1032         }
1033       m->name = strdup(m->name);
1034     }
1035   else
1036     {
1037       m->name = strdup(s);
1038       m->type = M_ANY;
1039     }
1040   return m;
1041 }
1042
1043
1044 /*
1045  * This routine two compares members by the following rules:
1046  * 1.  A USER is less than a LIST
1047  * 2.  A LIST is less than a STRING
1048  * 3.  If two members are of the same type, the one alphabetically first
1049  *     is less than the other
1050  * It returs < 0 if the first member is less, 0 if they are identical, and
1051  * > 0 if the second member is less (the first member is greater).
1052  */
1053
1054 int membercmp(const void *mem1, const void *mem2)
1055 {
1056   const struct member *m1 = mem1, *m2 = mem2;
1057
1058   if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type))
1059     return strcmp(m1->name, m2->name);
1060   else
1061     return m1->type - m2->type;
1062 }
1063
1064
1065 int sq_count_elts(struct save_queue *q)
1066 {
1067   char *foo;
1068   int count;
1069
1070   count = 0;
1071   while (sq_get_data(q, &foo))
1072     count++;
1073   return count;
1074 }
This page took 0.126393 seconds and 5 git commands to generate.