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