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 <stdlib.h>
19 #include <string.h>
20 #include "misc/exec.h"
21 #include "authvlib.h"
22 // Courier-IMAP includes
23 #include "courier-authlib/auth.h"
24 #include "courier-authlib/authmod.h"
25 
26 static mystring username;
27 static mystring passcode;
28 static mystring domain;
29 static int global_argc;
30 static char** global_argv;
31 
32 const mystring exec_presetuid = "authvmailmgr-presetuid";
33 const mystring exec_postsetuid = "authvmailmgr-postsetuid";
34 
fail(const char * msg,const char * execfile)35 void fail(const char* msg, const char* execfile)
36 {
37   presetenv("AUTHVMAILMGR_ERROR=", msg);
38   execute(execfile);
39 }
40 
fail_login(const char * msg)41 void fail_login(const char* msg)
42 {
43   fail(msg, "authvmailmgr-loginfail");
44   authmod_fail_completely();
45 }
46 
fail_baddata(const char * msg)47 void fail_baddata(const char* msg)
48 {
49   fail(msg, "authvmailmgr-error");
50   authmod_fail_completely();
51 }
52 
fail_temporary(const char * msg)53 void fail_temporary(const char* msg)
54 {
55   fail(msg, "authvmailmgr-error");
56   authmod_fail_completely();
57 }
58 
parse_data(const char *,const char * authtype,const char * authdata,int)59 static void parse_data(const char* /*service*/, const char* authtype,
60 		       const char* authdata, int /*issession*/)
61 {
62   mystring_iter iter(authdata, '\n');
63   username = *iter;
64   ++iter;
65   passcode = *iter;
66 
67   if(strcmp(authtype, AUTHTYPE_LOGIN))
68     fail_temporary("Invalid authentication type, must be 'login'");
69   if(!username || !passcode)
70     fail_baddata("Invalid authentication data");
71 
72   set_domain(username, domain);
73 }
74 
auth_vmailmgr()75 void auth_vmailmgr()
76 {
77   user_data* udata = authenticate(username, passcode, domain, true);
78   if(!udata)
79     // This point is only reached if the domain is not virtual, in which
80     // case we pass the authentication on to the next module.
81     authmod_fail(global_argc, global_argv);
82 
83   if(execute("authvmailmgr-presetuid"))
84     fail_temporary("Execution of authvmailmgr-presetuid failed");
85 
86   // authsuccess() set ups the environment, CWD, and GID/UID
87   authsuccess(udata->home.c_str(), 0, &udata->uid, &udata->gid,
88 	      username.c_str(), 0);
89   presetenv("MAILDIR=", udata->maildir);
90 
91   if(execute("authvmailmgr-postsetuid"))
92     fail_temporary("Execution of authvmailmgr-postsetuid failed");
93 }
94 
main(int argc,char ** argv)95 int main(int argc, char **argv)
96 {
97   global_argc = argc;
98   global_argv = argv;
99 
100   const char *service, *type;
101   char *authdata;
102 
103   authmod_init(argc, argv, &service, &type, &authdata);
104   parse_data(service, type, authdata, 1);
105   auth_vmailmgr();
106   authmod_success(argc, argv, username.c_str());
107   return 0;
108 }
109 
110