]> andersk Git - moira.git/blob - clients/blanche/blanche.c
use libmrclient to simplify connecting to the server
[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         com_err(whoami, status, "while updating list.");
407       else if (newname)
408         listname = newname;
409     }
410
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");
423         }
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,
437                         get_list_members, memberlist);
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;
456                 }
457             }
458           if (!removed)
459             sq_save_data(addlist, memberstruct);
460         }
461       while (sq_get_data(memberlist, &memberstruct))
462         sq_save_data(dellist, memberstruct);
463       sq_destroy(memberlist);
464       memberlist = sq_create();
465     }
466
467   /* Process the add list */
468   while (sq_get_data(addlist, &memberstruct))
469     {
470       /* canonicalize string if necessary */
471       if (memberstruct->type == M_STRING &&
472           (p = strchr(memberstruct->name, '@')))
473         {
474           char *host = canonicalize_hostname(strdup(++p));
475           static char **mailhubs = NULL;
476           char *argv[4];
477           int i;
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,
487                                 &mailhubs);
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;
493                 }
494             }
495           for (i = 0; (p = mailhubs[i]); i++)
496             {
497               if (!strcasecmp(p, host))
498                 {
499                   host = strdup(memberstruct->name);
500                   *(strchr(memberstruct->name, '@')) = 0;
501                   memberstruct->type = M_ANY;
502                   fprintf(stderr, "Warning: \"STRING:%s\" converted to "
503                           "\"%s\" because it is a local name.\n",
504                           host, memberstruct->name);
505                   break;
506                 }
507             }
508           free(host);
509         }
510       /* now continue adding member */
511       membervec[0] = listname;
512       membervec[2] = memberstruct->name;
513       if (verbose)
514         {
515           printf("Adding member ");
516           show_list_member(memberstruct);
517         }
518       switch (memberstruct->type)
519         {
520         case M_ANY:
521         case M_USER:
522           membervec[1] = "USER";
523           status = mr_query("add_member_to_list", 3, membervec, NULL, NULL);
524           if (status == MR_SUCCESS)
525             break;
526           else if (status != MR_USER || memberstruct->type != M_ANY)
527             {
528               com_err(whoami, status, "while adding member %s to %s",
529                       memberstruct->name, listname);
530               success = 0;
531               break;
532             }
533         case M_LIST:
534           membervec[1] = "LIST";
535           status = mr_query("add_member_to_list", 3, membervec,
536                             NULL, NULL);
537           if (status == MR_SUCCESS)
538             {
539               if (!strcmp(membervec[0], getenv("USER")))
540                 {
541                   fprintf(stderr, "\nWARNING: \"LIST:%s\" was just added "
542                           "to list \"%s\".\n", membervec[2], membervec[0]);
543                   fprintf(stderr, "If you meant to add yourself to the list "
544                           "\"%s\", type:\n", membervec[2]);
545                   fprintf(stderr, "\tblanche %s -d %s\t(to undo this)\n",
546                           membervec[0], membervec[2]);
547                   fprintf(stderr, "\tblanche %s -a %s\t(to add yourself to "
548                           "that list)\n", membervec[2], membervec[0]);
549                 }
550               break;
551             }
552           else if (status != MR_LIST || memberstruct->type != M_ANY)
553             {
554               com_err(whoami, status, "while adding member %s to %s",
555                       memberstruct->name, listname);
556               success = 0;
557               break;
558             }
559         case M_STRING:
560           if (memberstruct->type == M_ANY &&
561               !strchr(memberstruct->name, '@') &&
562               !strchr(memberstruct->name, '!') &&
563               !strchr(memberstruct->name, '%'))
564             {
565               /* if user is trying to add something which isn't a
566                  remote string, or a list, or a user, and didn't
567                  explicitly specify `STRING:', it's probably a typo */
568               com_err(whoami, MR_NO_MATCH, "while adding member %s to %s",
569                       memberstruct->name, listname);
570               success = 0;
571               break;
572             }
573
574           membervec[1] = "STRING";
575           status = mr_query("add_member_to_list", 3, membervec,
576                             NULL, NULL);
577           if (status != MR_SUCCESS)
578             {
579               com_err(whoami, status, "while adding member %s to %s",
580                       memberstruct->name, listname);
581               success = 0;
582             }
583           break;
584         case M_KERBEROS:
585           membervec[1] = "KERBEROS";
586           status = mr_query("add_member_to_list", 3, membervec,
587                             NULL, NULL);
588           if (status != MR_SUCCESS)
589             {
590               com_err(whoami, status, "while adding member %s to %s",
591                       memberstruct->name, listname);
592               success = 0;
593             }
594         }
595     }
596
597   /* Process the delete list */
598   while (sq_get_data(dellist, &memberstruct))
599     {
600       membervec[0] = listname;
601       membervec[2] = memberstruct->name;
602       if (verbose)
603         {
604           printf("Deleting member ");
605           show_list_member(memberstruct);
606         }
607       switch (memberstruct->type)
608         {
609         case M_ANY:
610         case M_USER:
611           membervec[1] = "USER";
612           status = mr_query("delete_member_from_list", 3, membervec,
613                             NULL, NULL);
614           if (status == MR_SUCCESS)
615             break;
616           else if ((status != MR_USER && status != MR_NO_MATCH) ||
617                    memberstruct->type != M_ANY)
618             {
619               com_err(whoami, status, "while deleting member %s from %s",
620                       memberstruct->name, listname);
621               success = 0;
622               break;
623             }
624         case M_LIST:
625           membervec[1] = "LIST";
626           status = mr_query("delete_member_from_list", 3, membervec,
627                             NULL, NULL);
628           if (status == MR_SUCCESS)
629             break;
630           else if ((status != MR_LIST && status != MR_NO_MATCH) ||
631                    memberstruct->type != M_ANY)
632             {
633               if (status == MR_PERM && memberstruct->type == M_ANY &&
634                   !strcmp(membervec[2], getenv("USER")))
635                 {
636                   /* M_ANY means we've fallen through from the user
637                    * case. The user is trying to remove himself from
638                    * a list, but we got MR_USER or MR_NO_MATCH above,
639                    * meaning he's not really on it, and we got MR_PERM
640                    * when trying to remove LIST:$USER because he's not
641                    * on the acl. That error is useless, so return
642                    * MR_NO_MATCH instead. However, this will generate the
643                    * wrong error if the user was trying to remove the list
644                    * with his username from a list he doesn't administrate
645                    * without explicitly specifying "list:".
646                    */
647                   status = MR_NO_MATCH;
648                 }
649               com_err(whoami, status, "while deleting member %s from %s",
650                       memberstruct->name, listname);
651               success = 0;
652               break;
653             }
654         case M_STRING:
655           membervec[1] = "STRING";
656           status = mr_query("delete_member_from_list", 3, membervec,
657                             NULL, NULL);
658           if (status == MR_STRING && memberstruct->type == M_ANY)
659             {
660               com_err(whoami, 0, " Unable to find member %s to delete from %s",
661                       memberstruct->name, listname);
662               success = 0;
663               if (!strcmp(membervec[0], getenv("USER")))
664                 {
665                   fprintf(stderr, "(If you were trying to remove yourself "
666                           "from the list \"%s\",\n", membervec[2]);
667                   fprintf(stderr, "the correct command is \"blanche %s -d "
668                           "%s\".)\n", membervec[2], membervec[0]);
669                 }
670             }
671           else if (status != MR_SUCCESS)
672             {
673               com_err(whoami, status, "while deleting member %s from %s",
674                       memberstruct->name, listname);
675               success = 0;
676             }
677           break;
678         case M_KERBEROS:
679           membervec[1] = "KERBEROS";
680           status = mr_query("delete_member_from_list", 3, membervec,
681                             NULL, NULL);
682           if (status != MR_SUCCESS)
683             {
684               com_err(whoami, status, "while deleting member %s from %s",
685                       memberstruct->name, listname);
686               success = 0;
687             }
688         }
689     }
690
691   /* Display the members of the list now, if requested */
692   if (memberflg)
693     {
694       if (recursflg)
695         recursive_display_list_members();
696       else
697         {
698           status = mr_query("get_members_of_list", 1, &listname,
699                             get_list_members, memberlist);
700           if (status)
701             com_err(whoami, status, "while getting members of list %s",
702                     listname);
703           while (sq_get_data(memberlist, &memberstruct))
704             show_list_member(memberstruct);
705         }
706     }
707
708   /* We're done! */
709   mr_disconnect();
710   exit(success ? 0 : 1);
711 }
712
713 void usage(char **argv)
714 {
715   fprintf(stderr, "Usage: %s listname [options]\n", argv[0]);
716   fprintf(stderr, "Options are\n");
717   fprintf(stderr, "  %-39s%-39s\n", "-v  | -verbose",
718           "-C  | -create");
719   fprintf(stderr, "  %-39s%-39s\n", "-m  | -members",
720           "-R  | -rename newname");
721   fprintf(stderr, "  %-39s%-39s\n", "-u  | -users",
722           "-P  | -public");
723   fprintf(stderr, "  %-39s%-39s\n", "-l  | -lists",
724           "-NP | -private");
725   fprintf(stderr, "  %-39s%-39s\n", "-s  | -strings",
726           "-A  | -active");
727   fprintf(stderr, "  %-39s%-39s\n", "-k  | -kerberos",
728           "-I  | -inactive");
729   fprintf(stderr, "  %-39s%-39s\n", "-i  | -info",
730           "-V  | -visible");
731   fprintf(stderr, "  %-39s%-39s\n", "-r  | -recursive",
732           "-H  | -hidden");
733   fprintf(stderr, "  %-39s%-39s\n", "-a  | -add member",
734           "-M  | -mail");
735   fprintf(stderr, "  %-39s%-39s\n", "-d  | -delete member",
736           "-NM | -notmail");
737   fprintf(stderr, "  %-39s%-39s\n", "-al | -addlist filename",
738           "-G  | -group");
739   fprintf(stderr, "  %-39s%-39s\n", "-dl | -deletelist filename",
740           "-NG | -notgroup");
741   fprintf(stderr, "  %-39s%-39s\n", "-f  | -file filename",
742           "-D  | -desc description");
743   fprintf(stderr, "  %-39s%-39s\n", "-n  | -noauth",
744           "-O  | -owner owner");
745   fprintf(stderr, "  %-39s%-39s\n", "-db | -database host[:port]",
746           "");
747   exit(1);
748 }
749
750
751 /* Display the members stored in the queue */
752
753 void show_list_member(struct member *memberstruct)
754 {
755   char *s = "";
756
757   switch (memberstruct->type)
758     {
759     case M_USER:
760       if (!showusers)
761         return;
762       s = "USER";
763       break;
764     case M_LIST:
765       if (!showlists)
766         return;
767       s = "LIST";
768       break;
769     case M_STRING:
770       if (!showstrings)
771         return;
772       s = "STRING";
773       break;
774     case M_KERBEROS:
775       if (!showkerberos)
776         return;
777       s = "KERBEROS";
778       break;
779     case M_ANY:
780       printf("%s\n", memberstruct->name);
781       return;
782     }
783
784   if (verbose)
785     printf("%s:%s\n", s, memberstruct->name);
786   else
787     {
788       if (memberstruct->type == M_LIST)
789         printf("LIST:%s\n", memberstruct->name);
790       else if (memberstruct->type == M_KERBEROS)
791         printf("KERBEROS:%s\n", memberstruct->name);
792       else if (memberstruct->type == M_STRING &&
793                !strchr(memberstruct->name, '@'))
794         printf("STRING:%s\n", memberstruct->name);
795       else
796         printf("%s\n", memberstruct->name);
797     }
798 }
799
800
801 /* Show the retrieved information about a list */
802
803 int show_list_info(int argc, char **argv, void *hint)
804 {
805   printf("List: %s\n", argv[0]);
806   printf("Description: %s\n", argv[9]);
807   printf("Flags: %s, %s, and %s\n",
808          atoi(argv[1]) ? "active" : "inactive",
809          atoi(argv[2]) ? "public" : "private",
810          atoi(argv[3]) ? "hidden" : "visible");
811   printf("%s is %sa maillist and is %sa group", argv[0],
812          atoi(argv[4]) ? "" : "not ",
813          atoi(argv[5]) ? "" : "not ");
814   if (atoi(argv[5]))
815     printf(" with GID %d\n", atoi(argv[6]));
816   else
817     printf("\n");
818   printf("Owner: %s %s\n", argv[7], argv[8]);
819   printf("Last modified by %s with %s on %s\n", argv[11], argv[12], argv[10]);
820   return MR_CONT;
821 }
822
823
824 /* Copy retrieved information about a list into a new argv */
825
826 int save_list_info(int argc, char **argv, void *hint)
827 {
828   char **nargv = hint;
829
830   for (argc = 0; argc < 10; argc++)
831     nargv[argc + 1] = strdup(argv[argc]);
832   return MR_CONT;
833 }
834
835 /* Show the retrieve list member count */
836
837 int show_list_count(int argc, char **argv, void *hint)
838 {
839   printf("Members: %s\n", argv[0]);
840   return MR_CONT;
841 }
842
843
844 /* Recursively find all of the members of listname, and then display them */
845
846 void recursive_display_list_members(void)
847 {
848   int status, count, savecount;
849   struct save_queue *lists, *members;
850   struct member *m, *m1, *data;
851
852   lists = sq_create();
853   members = sq_create();
854   m = malloc(sizeof(struct member));
855   m->type = M_LIST;
856   m->name = listname;
857   sq_save_data(lists, m);
858
859   while (sq_get_data(lists, &m))
860     {
861       sq_destroy(memberlist);
862       memberlist = sq_create();
863       status = mr_query("get_members_of_list", 1, &(m->name),
864                         get_list_members, memberlist);
865       if (status)
866         com_err(whoami, status, "while getting members of list %s", m->name);
867       while (sq_get_data(memberlist, &m1))
868         {
869           if (m1->type == M_LIST)
870             unique_add_member(lists, m1);
871           else
872             unique_add_member(members, m1);
873         }
874     }
875   savecount = count = sq_count_elts(members);
876   data = malloc(count * sizeof(struct member));
877   count = 0;
878   while (sq_get_data(members, &m))
879     memcpy(&data[count++], m, sizeof(struct member));
880   qsort(data, count, sizeof(struct member), membercmp);
881   for (count = 0; count < savecount; count++)
882     show_list_member(&data[count]);
883 }
884
885
886 /* add a struct member to a queue if that member isn't already there. */
887
888 void unique_add_member(struct save_queue *q, struct member *m)
889 {
890   struct save_queue *qp;
891
892   for (qp = q->q_next; qp != q; qp = qp->q_next)
893     {
894       if (!membercmp(qp->q_data, m))
895         return;
896     }
897   sq_save_data(q, m);
898 }
899
900
901 /* Collect the retrieved members of the list */
902
903 int get_list_members(int argc, char **argv, void *sq)
904 {
905   struct save_queue *q = sq;
906   struct member *m;
907
908   m = malloc(sizeof(struct member));
909   switch (argv[0][0])
910     {
911     case 'U':
912       m->type = M_USER;
913       break;
914     case 'L':
915       m->type = M_LIST;
916       break;
917     case 'S':
918       m->type = M_STRING;
919       break;
920     case 'K':
921       m->type = M_KERBEROS;
922       break;
923     }
924   m->name = strdup(argv[1]);
925   sq_save_data(q, m);
926   return MR_CONT;
927 }
928
929
930 /* Open file, parse members from file, and put them on the specified queue */
931 void get_members_from_file(char *filename, struct save_queue *queue)
932 {
933   FILE *in;
934   char buf[BUFSIZ];
935   struct member *memberstruct;
936
937   if (!strcmp(filename, "-"))
938     in = stdin;
939   else
940     {
941       in = fopen(filename, "r");
942       if (!in)
943         {
944           com_err(whoami, errno, "while opening %s for input", filename);
945           exit(2);
946         }
947     }
948
949   while (fgets(buf, BUFSIZ, in))
950     {
951       if ((memberstruct = parse_member(buf)))
952         sq_save_data(queue, memberstruct);
953     }
954   if (!feof(in))
955     {
956       com_err(whoami, errno, "while reading from %s", filename);
957       exit(2);
958     }
959 }
960
961
962 /* Collect the possible expansions of the alias MAILHUB */
963
964 int collect(int argc, char **argv, void *l)
965 {
966   char ***list = l;
967   int i;
968
969   for (i = 0; (*list)[i]; i++)
970     ;
971   *list = realloc(*list, (i + 2) * sizeof(char *));
972   (*list)[i] = strdup(argv[2]);
973   (*list)[i + 1] = NULL;
974   return MR_CONT;
975 }
976
977
978 /* Parse a line of input, fetching a member.  NULL is returned if a member
979  * is not found.  ';' is a comment character.
980  */
981
982 struct member *parse_member(char *s)
983 {
984   struct member *m;
985   char *p, *lastchar;
986
987   while (*s && isspace(*s))
988     s++;
989   lastchar = p = s;
990   while (*p && *p != '\n' && *p != ';')
991     {
992       if (isprint(*p) && !isspace(*p))
993         lastchar = p++;
994       else
995         p++;
996     }
997   lastchar++;
998   *lastchar = '\0';
999   if (p == s || strlen(s) == 0)
1000     return NULL;
1001
1002   if (!(m = malloc(sizeof(struct member))))
1003     return NULL;
1004
1005   if ((p = strchr(s, ':')))
1006     {
1007       *p = '\0';
1008       m->name = ++p;
1009       if (!strcasecmp("user", s))
1010         m->type = M_USER;
1011       else if (!strcasecmp("list", s))
1012         m->type = M_LIST;
1013       else if (!strcasecmp("string", s))
1014         m->type = M_STRING;
1015       else if (!strcasecmp("kerberos", s))
1016         m->type = M_KERBEROS;
1017       else
1018         {
1019           m->type = M_ANY;
1020           *(--p) = ':';
1021           m->name = s;
1022         }
1023       m->name = strdup(m->name);
1024     }
1025   else
1026     {
1027       m->name = strdup(s);
1028       m->type = M_ANY;
1029     }
1030   return m;
1031 }
1032
1033
1034 /*
1035  * This routine two compares members by the following rules:
1036  * 1.  A USER is less than a LIST
1037  * 2.  A LIST is less than a STRING
1038  * 3.  If two members are of the same type, the one alphabetically first
1039  *     is less than the other
1040  * It returs < 0 if the first member is less, 0 if they are identical, and
1041  * > 0 if the second member is less (the first member is greater).
1042  */
1043
1044 int membercmp(const void *mem1, const void *mem2)
1045 {
1046   const struct member *m1 = mem1, *m2 = mem2;
1047
1048   if (m1->type == M_ANY || m2->type == M_ANY || (m1->type == m2->type))
1049     return strcmp(m1->name, m2->name);
1050   else
1051     return m1->type - m2->type;
1052 }
1053
1054
1055 int sq_count_elts(struct save_queue *q)
1056 {
1057   char *foo;
1058   int count;
1059
1060   count = 0;
1061   while (sq_get_data(q, &foo))
1062     count++;
1063   return count;
1064 }
This page took 0.131403 seconds and 5 git commands to generate.