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