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