r13316: Let the carnage begin....
[nivanova/samba-autobuild/.git] / source3 / auth / auth_script.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Call out to a shell script for an authentication check.
5
6    Copyright (C) Jeremy Allison 2005.
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #undef malloc
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_AUTH
29
30 /* Create a string containing the supplied :
31  * domain\n
32  * user\n
33  * ascii hex challenge\n
34  * ascii hex LM response\n
35  * ascii hex NT response\n\0
36  * and execute a shell script to check this.
37  * Allows external programs to create users on demand.
38  * Script returns zero on success, non-zero on fail.
39  */
40
41 static NTSTATUS script_check_user_credentials(const struct auth_context *auth_context,
42                                         void *my_private_data, 
43                                         TALLOC_CTX *mem_ctx,
44                                         const auth_usersupplied_info *user_info, 
45                                         auth_serversupplied_info **server_info)
46 {
47         const char *script = lp_parm_const_string( GLOBAL_SECTION_SNUM, "auth_script", "script", NULL);
48         char *secret_str;
49         size_t secret_str_len;
50         char hex_str[49];
51         int ret, i;
52
53         if (!script) {
54                 return NT_STATUS_INVALID_PARAMETER;
55         }
56
57         if (!user_info) {
58                 return NT_STATUS_INVALID_PARAMETER;
59         }
60
61         if (!auth_context) {
62                 DEBUG(3,("script_check_user_credentials: no auth_info !\n"));
63                 return NT_STATUS_INVALID_PARAMETER;
64         }               
65
66         secret_str_len = strlen(user_info->domain) + 1 +
67                         strlen(user_info->smb_name) + 1 +
68                         16 + 1 + /* 8 bytes of challenge going to 16 */
69                         48 + 1 + /* 24 bytes of challenge going to 48 */
70                         48 + 1;
71
72         secret_str = malloc(secret_str_len);
73         if (!secret_str) {
74                 return NT_STATUS_NO_MEMORY;
75         }
76
77         safe_strcpy( secret_str, user_info->domain, secret_str_len - 1);
78         safe_strcat( secret_str, "\n", secret_str_len - 1);
79         safe_strcat( secret_str, user_info->smb_name, secret_str_len - 1);
80         safe_strcat( secret_str, "\n", secret_str_len - 1);
81
82         for (i = 0; i < 8; i++) {
83                 slprintf(&hex_str[i*2], 3, "%02X", auth_context->challenge.data[i]);
84         }
85         safe_strcat( secret_str, hex_str, secret_str_len - 1);
86         safe_strcat( secret_str, "\n", secret_str_len - 1);
87
88         if (user_info->lm_resp.data) {
89                 for (i = 0; i < 24; i++) {
90                         slprintf(&hex_str[i*2], 3, "%02X", user_info->lm_resp.data[i]);
91                 }
92                 safe_strcat( secret_str, hex_str, secret_str_len - 1);
93         }
94         safe_strcat( secret_str, "\n", secret_str_len - 1);
95
96         if (user_info->nt_resp.data) {
97                 for (i = 0; i < 24; i++) {
98                         slprintf(&hex_str[i*2], 3, "%02X", user_info->nt_resp.data[i]);
99                 }
100                 safe_strcat( secret_str, hex_str, secret_str_len - 1);
101         }
102         safe_strcat( secret_str, "\n", secret_str_len - 1);
103
104         DEBUG(10,("script_check_user_credentials: running %s with parameters:\n%s\n",
105                 script, secret_str ));
106
107         ret = smbrunsecret( script, secret_str);
108
109         SAFE_FREE(secret_str);
110
111         if (ret) {
112                 DEBUG(1,("script_check_user_credentials: failed to authenticate %s\\%s\n",
113                         user_info->domain, user_info->smb_name ));
114                 /* auth failed. */
115                 return NT_STATUS_NO_SUCH_USER;
116         }
117
118         /* Cause the auth system to keep going.... */
119         return NT_STATUS_NOT_IMPLEMENTED;
120 }
121
122 /* module initialisation */
123 static NTSTATUS auth_init_script(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
124 {
125         if (!make_auth_methods(auth_context, auth_method)) {
126                 return NT_STATUS_NO_MEMORY;
127         }
128
129         (*auth_method)->name = "script";
130         (*auth_method)->auth = script_check_user_credentials;
131
132         if (param && *param) {
133                 /* we load the 'fallback' module - if script isn't here, call this
134                    module */
135                 if (!load_auth_module(auth_context, param, (auth_methods **)&(*auth_method)->private_data)) {
136                         return NT_STATUS_UNSUCCESSFUL;
137                 }
138                 
139         }
140         return NT_STATUS_OK;
141 }
142
143 #if 0
144 /* Define this to build static. */
145 NTSTATUS auth_script_init(void)
146 {
147         return smb_register_auth(AUTH_INTERFACE_VERSION, "script", auth_init_script);
148 }
149 #else
150 /* Define this to build shared. */
151 NTSTATUS init_module(void)
152 {
153         return smb_register_auth(AUTH_INTERFACE_VERSION, "script", auth_init_script);
154 }
155 #endif