Back to index

courier  0.68.2
maildiracl.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2003 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 
00007 #if    HAVE_CONFIG_H
00008 #include      "config.h"
00009 #endif
00010 #include      <stdio.h>
00011 #include      <string.h>
00012 #include      <stdlib.h>
00013 #if    HAVE_FCNTL_H
00014 #include      <fcntl.h>
00015 #endif
00016 #if    HAVE_UNISTD_H
00017 #include      <unistd.h>
00018 #endif
00019 #include      <sys/types.h>
00020 #if    HAVE_SYS_STAT_H
00021 #include      <sys/stat.h>
00022 #endif
00023 #include      <ctype.h>
00024 #include      <errno.h>
00025 
00026 #include      "maildirmisc.h"
00027 #include      "maildiraclt.h"
00028 
00029 static const char resetcmd[]="-reset";
00030 static const char listcmd[]="-list";
00031 static const char setcmd[]="-set";
00032 static const char deletecmd[]="-delete";
00033 static const char computecmd[]="-compute";
00034 
00035 static void usage()
00036 {
00037        printf("Usage: maildiracl [ options ] maildir [INBOX[.folder] [identifier rights]]\n");
00038        exit(1);
00039 }
00040 
00041 static int acl_list(const char *identifier,
00042                   const maildir_aclt *acl,
00043                   void *cb_arg)
00044 {
00045        printf("%s\t%s\n", identifier, maildir_aclt_ascstr(acl));
00046        return (0);
00047 }
00048 
00049 struct computeinfo {
00050        int argc;
00051        char **argv;
00052 };
00053 
00054 static int isme(const char *identifier,
00055               void *void_arg)
00056 {
00057        struct computeinfo *ci=(struct computeinfo *)void_arg;
00058        int i;
00059 
00060        for (i=4; i<ci->argc; i++)
00061               if (strcmp(ci->argv[i], identifier) == 0)
00062                      return 1;
00063        return 0;
00064 }
00065 
00066 int main(int argc, char *argv[])
00067 {
00068        const char *cmd;
00069        const char *maildir;
00070        const char *folder;
00071 
00072        if (argc < 3)
00073               usage();
00074 
00075        cmd=argv[1];
00076 
00077        if (strcmp(cmd, resetcmd) &&
00078            strcmp(cmd, listcmd) &&
00079            strcmp(cmd, setcmd) &&
00080            strcmp(cmd, deletecmd) &&
00081            strcmp(cmd, computecmd))
00082               usage();
00083 
00084        maildir=argv[2];
00085 
00086        if (strcmp(cmd, resetcmd) == 0)
00087        {
00088               if (maildir_acl_reset(maildir))
00089               {
00090                      perror(maildir);
00091                      exit(1);
00092               }
00093               exit(0);
00094        }
00095 
00096        if (argc < 4)
00097               usage();
00098 
00099        folder=argv[3];
00100 
00101        if (strcmp(folder, INBOX) &&
00102            strncmp(folder, INBOX ".", sizeof(INBOX ".")-1))
00103        {
00104               errno=EINVAL;
00105               perror(folder);
00106               exit(1);
00107        }
00108        folder += sizeof(INBOX)-1;
00109 
00110        if (!*folder)
00111               folder=".";
00112 
00113        if (strcmp(cmd, listcmd) == 0)
00114        {
00115               maildir_aclt_list l;
00116 
00117               if (maildir_acl_read(&l, maildir, folder) ||
00118                   maildir_aclt_list_enum(&l, acl_list, NULL))
00119               {
00120                      perror(maildir);
00121                      exit(1);
00122               }
00123 
00124               maildir_aclt_list_destroy(&l);
00125               exit(0);
00126        }
00127 
00128        if (strcmp(cmd, setcmd) == 0)
00129        {
00130               maildir_aclt_list l;
00131               maildir_aclt a;
00132 
00133               const char *identifier;
00134               const char *rights;
00135               const char *err_failedrights;
00136 
00137               if (argc < 6)
00138                      usage();
00139 
00140               identifier=argv[4];
00141               rights=argv[5];
00142 
00143               if (maildir_acl_read(&l, maildir, folder))
00144               {
00145                      perror(maildir);
00146                      exit(1);
00147               }
00148 
00149               if (*rights == '+')
00150               {
00151                      if (maildir_aclt_init(&a, NULL,
00152                                          maildir_aclt_list_find(&l,
00153                                                              identifier
00154                                                              )) ||
00155                          maildir_aclt_add(&a, rights+1, NULL))
00156                      {
00157                             perror(argv[0]);
00158                             exit(1);
00159                      }
00160               } else if (*rights == '-')
00161               {
00162                      if (maildir_aclt_init(&a, NULL,
00163                                          maildir_aclt_list_find(&l,
00164                                                              identifier
00165                                                              )) ||
00166                          maildir_aclt_del(&a, rights+1, NULL))
00167                      {
00168                             perror(argv[0]);
00169                             exit(1);
00170                      }
00171               }
00172               else if (maildir_aclt_init(&a, rights, NULL))
00173               {
00174                      perror(argv[0]);
00175                      exit (1);
00176               }
00177 
00178               if (maildir_aclt_list_add(&l, identifier, NULL, &a))
00179               {
00180                      perror(argv[0]);
00181                      exit(1);
00182               }
00183 
00184               if (maildir_acl_write(&l, maildir, folder, "owner",
00185                                   &err_failedrights))
00186               {
00187                      if (err_failedrights)
00188                      {
00189                             fprintf(stderr,
00190                                    "Trying to set invalid access"
00191                                    " rights for %s\n",
00192                                    err_failedrights);
00193                      }
00194                      else perror(maildir);
00195                      exit(1);
00196               }
00197        }
00198 
00199        if (strcmp(cmd, deletecmd) == 0)
00200        {
00201               maildir_aclt_list l;
00202               const char *identifier;
00203               const char *err_failedrights;
00204 
00205               if (argc < 5)
00206                      usage();
00207 
00208               identifier=argv[4];
00209 
00210               if (maildir_acl_read(&l, maildir, folder))
00211               {
00212                      perror(maildir);
00213                      exit(1);
00214               }
00215 
00216               if (maildir_aclt_list_del(&l, identifier))
00217               {
00218                      perror(maildir);
00219                      exit(1);
00220               }
00221 
00222               if (maildir_acl_write(&l, maildir, folder, "owner",
00223                                   &err_failedrights))
00224               {
00225                      if (err_failedrights)
00226                      {
00227                             fprintf(stderr,
00228                                    "Trying to set invalid access"
00229                                    " rights for %s\n",
00230                                    err_failedrights);
00231                      }
00232                      else perror(maildir);
00233                      exit(1);
00234               }
00235        }
00236 
00237        if (strcmp(cmd, computecmd) == 0)
00238        {
00239               maildir_aclt_list l;
00240               maildir_aclt a;
00241 
00242               struct computeinfo ci;
00243 
00244               ci.argc=argc;
00245               ci.argv=argv;
00246 
00247               if (argc < 5)
00248                      usage();
00249 
00250               if (maildir_acl_read(&l, maildir, folder))
00251               {
00252                      perror(maildir);
00253                      exit(1);
00254               }
00255 
00256               if (maildir_acl_compute(&a, &l, isme, &ci))
00257               {
00258                      perror(maildir);
00259                      exit(1);
00260               }
00261 
00262               printf("%s\n", maildir_aclt_ascstr(&a));
00263        }
00264 
00265        return (0);
00266 }