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