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