authenticate/authvmailmgr.cc
changeset 0 6f7a81934006
child 2 b3afb9f1e801
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #include <config.h>
       
    18 #include "misc/exec.h"
       
    19 #include "authvlib.h"
       
    20 // Courier-IMAP includes
       
    21 #include "courier-authlib/auth.h"
       
    22 #include "courier-authlib/authmod.h"
       
    23 
       
    24 static mystring username;
       
    25 static mystring passcode;
       
    26 static mystring domain;
       
    27 static int global_argc;
       
    28 static char** global_argv;
       
    29 
       
    30 const mystring exec_presetuid = "authvmailmgr-presetuid";
       
    31 const mystring exec_postsetuid = "authvmailmgr-postsetuid";
       
    32 
       
    33 void fail(const char* msg, const char* execfile)
       
    34 {
       
    35   presetenv("AUTHVMAILMGR_ERROR=", msg);
       
    36   execute(execfile);
       
    37 }
       
    38 
       
    39 void fail_login(const char* msg)
       
    40 {
       
    41   fail(msg, "authvmailmgr-loginfail");
       
    42   authmod_fail_completely();
       
    43 }
       
    44 
       
    45 void fail_baddata(const char* msg)
       
    46 {
       
    47   fail(msg, "authvmailmgr-error");
       
    48   authmod_fail_completely();
       
    49 }
       
    50 
       
    51 void fail_temporary(const char* msg)
       
    52 {
       
    53   fail(msg, "authvmailmgr-error");
       
    54   authmod_fail_completely();
       
    55 }
       
    56 
       
    57 static void parse_data(const char* /*service*/, const char* authtype,
       
    58 		       const char* authdata, int /*issession*/)
       
    59 {
       
    60   mystring_iter iter(authdata, '\n');
       
    61   username = *iter;
       
    62   ++iter;
       
    63   passcode = *iter;
       
    64 
       
    65   if(strcmp(authtype, AUTHTYPE_LOGIN))
       
    66     fail_temporary("Invalid authentication type, must be 'login'");
       
    67   if(!username || !passcode)
       
    68     fail_baddata("Invalid authentication data");
       
    69 
       
    70   set_domain(username, domain);
       
    71 }
       
    72 
       
    73 void auth_vmailmgr()
       
    74 {
       
    75   user_data* udata = authenticate(username, passcode, domain, true);
       
    76   if(!udata)
       
    77     // This point is only reached if the domain is not virtual, in which
       
    78     // case we pass the authentication on to the next module.
       
    79     authmod_fail(global_argc, global_argv);
       
    80 
       
    81   if(execute("authvmailmgr-presetuid"))
       
    82     fail_temporary("Execution of authvmailmgr-presetuid failed");
       
    83   
       
    84   // authsuccess() set ups the environment, CWD, and GID/UID
       
    85   authsuccess(udata->home.c_str(), 0, &udata->uid, &udata->gid,
       
    86 	      username.c_str(), 0);
       
    87   presetenv("MAILDIR=", udata->maildir);
       
    88 
       
    89   if(execute("authvmailmgr-postsetuid"))
       
    90     fail_temporary("Execution of authvmailmgr-postsetuid failed");
       
    91 }
       
    92 
       
    93 int main(int argc, char **argv)
       
    94 {
       
    95   global_argc = argc;
       
    96   global_argv = argv;
       
    97   
       
    98   const char *service, *type;
       
    99   char *authdata;
       
   100 
       
   101   authmod_init(argc, argv, &service, &type, &authdata);
       
   102   parse_data(service, type, authdata, 1);
       
   103   auth_vmailmgr();
       
   104   authmod_success(argc, argv, username.c_str());
       
   105   return 0;
       
   106 }
       
   107