5ce7075ab9f9379a35a6bdc23242d711ad055f11
[kai/samba.git] / source3 / auth / auth_builtin.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Generic authenticaion types
4    Copyright (C) Andrew Bartlett         2001-2002
5    Copyright (C) Jelmer Vernooij              2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_AUTH
26
27 /**
28  * Return a guest logon for guest users (username = "")
29  *
30  * Typically used as the first module in the auth chain, this allows
31  * guest logons to be dealt with in one place.  Non-guest logons 'fail'
32  * and pass onto the next module.
33  **/
34
35 static NTSTATUS check_guest_security(const struct auth_context *auth_context,
36                                      void *my_private_data, 
37                                      TALLOC_CTX *mem_ctx,
38                                      const auth_usersupplied_info *user_info, 
39                                      auth_serversupplied_info **server_info)
40 {
41         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
42
43         if (!(user_info->internal_username.str 
44               && *user_info->internal_username.str)) { 
45                 if (make_server_info_guest(server_info)) {
46                         nt_status = NT_STATUS_OK;
47                 } else {
48                         nt_status = NT_STATUS_NO_SUCH_USER;
49                 }
50         }
51
52         return nt_status;
53 }
54
55 /* Guest modules initialisation */
56 NTSTATUS auth_init_guest(struct auth_context *auth_context, const char *options, auth_methods **auth_method) 
57 {
58         if (!make_auth_methods(auth_context, auth_method)) {
59                 return NT_STATUS_NO_MEMORY;
60         }
61
62         (*auth_method)->auth = check_guest_security;
63         (*auth_method)->name = "guest";
64         return NT_STATUS_OK;
65 }
66
67 /** 
68  * Return an error based on username
69  *
70  * This function allows the testing of obsure errors, as well as the generation
71  * of NT_STATUS -> DOS error mapping tables.
72  *
73  * This module is of no value to end-users.
74  *
75  * The password is ignored.
76  *
77  * @return An NTSTATUS value based on the username
78  **/
79
80 static NTSTATUS check_name_to_ntstatus_security(const struct auth_context *auth_context,
81                                                 void *my_private_data, 
82                                                 TALLOC_CTX *mem_ctx,
83                                                 const auth_usersupplied_info *user_info, 
84                                                 auth_serversupplied_info **server_info)
85 {
86         NTSTATUS nt_status;
87         fstring user;
88         long error_num;
89         fstrcpy(user, user_info->smb_name.str);
90         
91         if (strncasecmp("NT_STATUS", user, strlen("NT_STATUS")) == 0) {
92                 strupper(user);
93                 return nt_status_string_to_code(user);
94         }
95
96         strlower(user);
97         error_num = strtoul(user, NULL, 16);
98         
99         DEBUG(5,("Error for user %s was %lx\n", user, error_num));
100
101         nt_status = NT_STATUS(error_num);
102         
103         return nt_status;
104 }
105
106 /** Module initailisation function */
107 NTSTATUS auth_init_name_to_ntstatus(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
108 {
109         if (!make_auth_methods(auth_context, auth_method)) {
110                 return NT_STATUS_NO_MEMORY;
111         }
112
113         (*auth_method)->auth = check_name_to_ntstatus_security;
114         (*auth_method)->name = "name_to_ntstatus";
115         return NT_STATUS_OK;
116 }
117
118 /** 
119  * Return a 'fixed' challenge instead of a varaible one.
120  *
121  * The idea of this function is to make packet snifs consistant
122  * with a fixed challenge, so as to aid debugging.
123  *
124  * This module is of no value to end-users.
125  *
126  * This module does not actually authenticate the user, but
127  * just pretenteds to need a specified challenge.  
128  * This module removes *all* security from the challenge-response system
129  *
130  * @return NT_STATUS_UNSUCCESSFUL
131  **/
132
133 static NTSTATUS check_fixed_challenge_security(const struct auth_context *auth_context,
134                                                void *my_private_data, 
135                                                TALLOC_CTX *mem_ctx,
136                                                const auth_usersupplied_info *user_info, 
137                                                auth_serversupplied_info **server_info)
138 {
139         return NT_STATUS_UNSUCCESSFUL;
140 }
141
142 /****************************************************************************
143  Get the challenge out of a password server.
144 ****************************************************************************/
145
146 static DATA_BLOB auth_get_fixed_challenge(const struct auth_context *auth_context,
147                                           void **my_private_data, 
148                                           TALLOC_CTX *mem_ctx)
149 {
150         const char *challenge = "I am a teapot";   
151         return data_blob(challenge, 8);
152 }
153
154
155 /** Module initailisation function */
156 NTSTATUS auth_init_fixed_challenge(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
157 {
158         if (!make_auth_methods(auth_context, auth_method)) {
159                 return NT_STATUS_NO_MEMORY;
160         }
161
162         (*auth_method)->auth = check_fixed_challenge_security;
163         (*auth_method)->get_chal = auth_get_fixed_challenge;
164         (*auth_method)->name = "fixed_challenge";
165         return NT_STATUS_OK;
166 }
167
168 /**
169  * Outsorce an auth module to an external loadable .so
170  *
171  * Only works on systems with dlopen() etc.
172  **/
173
174 /* Plugin modules initialisation */
175 NTSTATUS auth_init_plugin(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
176 {
177         void * dl_handle;
178         char *plugin_param, *plugin_name, *p;
179         auth_init_function plugin_init;
180
181         if (param == NULL) {
182                 DEBUG(0, ("The plugin module needs an argument!\n"));
183                 return NT_STATUS_UNSUCCESSFUL;
184         }
185
186         plugin_name = smb_xstrdup(param);
187         p = strchr(plugin_name, ':');
188         if (p) {
189                 *p = 0;
190                 plugin_param = p+1;
191                 trim_string(plugin_param, " ", " ");
192         } else plugin_param = NULL;
193
194         trim_string(plugin_name, " ", " ");
195
196         DEBUG(5, ("Trying to load auth plugin %s\n", plugin_name));
197         dl_handle = sys_dlopen(plugin_name, RTLD_NOW | RTLD_GLOBAL );
198         if (!dl_handle) {
199                 DEBUG(0, ("Failed to load auth plugin %s using sys_dlopen (%s)\n", plugin_name, sys_dlerror()));
200                 return NT_STATUS_UNSUCCESSFUL;
201         }
202     
203         plugin_init = sys_dlsym(dl_handle, "auth_init");
204         if (!plugin_init){
205                 DEBUG(0, ("Failed to find function 'pdb_init' using sys_dlsym in sam plugin %s (%s)\n", plugin_name, sys_dlerror()));       
206                 return NT_STATUS_UNSUCCESSFUL;
207         }
208
209         DEBUG(5, ("Starting sam plugin %s with paramater %s\n", plugin_name, plugin_param?plugin_param:"(null)"));
210         return plugin_init(auth_context, plugin_param, auth_method);
211 }
212
213