I've decided to move the auth code around a bit more...
[nivanova/samba-autobuild/.git] / source3 / auth / auth_server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Authenticate to a remote server
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Andrew Bartlett 2001
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 extern pstring global_myname;
26 extern userdom_struct current_user_info;
27
28 /****************************************************************************
29  Support for server level security.
30 ****************************************************************************/
31
32 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
33 {
34         struct cli_state *cli = NULL;
35         fstring desthost;
36         struct in_addr dest_ip;
37         char *p, *pserver;
38         BOOL connected_ok = False;
39
40         if (!(cli = cli_initialise(cli)))
41                 return NULL;
42
43         /* security = server just can't function with spnego */
44         cli->use_spnego = False;
45
46         pserver = talloc_strdup(mem_ctx, lp_passwordserver());
47         p = pserver;
48
49         while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
50                 standard_sub_basic(current_user_info.smb_name, desthost);
51                 strupper(desthost);
52
53                 if(!resolve_name( desthost, &dest_ip, 0x20)) {
54                         DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
55                         continue;
56                 }
57
58                 if (ismyip(dest_ip)) {
59                         DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
60                         continue;
61                 }
62
63                 if (cli_connect(cli, desthost, &dest_ip)) {
64                         DEBUG(3,("connected to password server %s\n",desthost));
65                         connected_ok = True;
66                         break;
67                 }
68         }
69
70         if (!connected_ok) {
71                 DEBUG(0,("password server not available\n"));
72                 cli_shutdown(cli);
73                 return NULL;
74         }
75
76         if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
77                 return NULL;
78         
79         if (strequal(desthost,myhostname())) {
80                 exit_server("Password server loop!");
81         }
82         
83         DEBUG(3,("got session\n"));
84
85         if (!cli_negprot(cli)) {
86                 DEBUG(1,("%s rejected the negprot\n",desthost));
87                 cli_shutdown(cli);
88                 return NULL;
89         }
90
91         if (cli->protocol < PROTOCOL_LANMAN2 ||
92             !(cli->sec_mode & 1)) {
93                 DEBUG(1,("%s isn't in user level security mode\n",desthost));
94                 cli_shutdown(cli);
95                 return NULL;
96         }
97
98         DEBUG(3,("password server OK\n"));
99
100         return cli;
101 }
102
103 /****************************************************************************
104  Clean up our allocated cli.
105 ****************************************************************************/
106
107 static void free_server_private_data(void **private_data_pointer) 
108 {
109         struct cli_state **cli = (struct cli_state **)private_data_pointer;
110         if (*cli && (*cli)->initialised) {
111                 cli_shutdown(*cli);
112         }
113 }
114
115 /****************************************************************************
116  Send a 'keepalive' packet down the cli pipe.
117 ****************************************************************************/
118
119 static void send_server_keepalive(void **private_data_pointer) 
120 {
121         struct cli_state **cli = (struct cli_state **)private_data_pointer;
122         
123         /* also send a keepalive to the password server if its still
124            connected */
125         if (cli && *cli && (*cli)->initialised) {
126                 if (!send_keepalive((*cli)->fd)) {
127                         DEBUG( 2, ( "password server keepalive failed.\n"));
128                         cli_shutdown(*cli);
129                 }
130         }
131 }
132
133 /****************************************************************************
134  Get the challenge out of a password server.
135 ****************************************************************************/
136
137 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
138                                            void **my_private_data, 
139                                            TALLOC_CTX *mem_ctx)
140 {
141         struct cli_state *cli = server_cryptkey(mem_ctx);
142         
143         if (cli) {
144                 DEBUG(3,("using password server validation\n"));
145
146                 if ((cli->sec_mode & 2) == 0) {
147                         /* We can't work with unencrypted password servers
148                            unless 'encrypt passwords = no' */
149                         DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
150                         
151                         /* However, it is still a perfectly fine connection
152                            to pass that unencrypted password over */
153                         *my_private_data = (void *)cli;
154                         return data_blob(NULL, 0);
155                         
156                 } else if (cli->secblob.length < 8) {
157                         /* We can't do much if we don't get a full challenge */
158                         DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
159                         cli_shutdown(cli);
160                         return data_blob(NULL, 0);
161                 }
162
163                 *my_private_data = (void *)cli;
164
165                 /* The return must be allocated on the caller's mem_ctx, as our own will be
166                    destoyed just after the call. */
167                 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
168         } else {
169                 return data_blob(NULL, 0);
170         }
171 }
172
173
174 /****************************************************************************
175  Check for a valid username and password in security=server mode.
176   - Validate a password with the password server.
177 ****************************************************************************/
178
179 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
180                                          void *my_private_data, 
181                                          TALLOC_CTX *mem_ctx,
182                                          const auth_usersupplied_info *user_info, 
183                                          auth_serversupplied_info **server_info)
184 {
185         struct cli_state *cli;
186         static unsigned char badpass[24];
187         static fstring baduser; 
188         static BOOL tested_password_server = False;
189         static BOOL bad_password_server = False;
190         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
191         BOOL locally_made_cli = False;
192
193         /* 
194          * Check that the requested domain is not our own machine name.
195          * If it is, we should never check the PDC here, we use our own local
196          * password file.
197          */
198
199         if(is_netbios_alias_or_name(user_info->domain.str)) {
200                 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
201                 return NT_STATUS_LOGON_FAILURE;
202         }
203
204         cli = my_private_data;
205         
206         if (cli) {
207         } else {
208                 cli = server_cryptkey(mem_ctx);
209                 locally_made_cli = True;
210         }
211
212         if (!cli || !cli->initialised) {
213                 DEBUG(1,("password server is not connected (cli not initilised)\n"));
214                 return NT_STATUS_LOGON_FAILURE;
215         }  
216         
217         if ((cli->sec_mode & 2) == 0) {
218                 if (user_info->encrypted) {
219                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
220                         return NT_STATUS_LOGON_FAILURE;         
221                 }
222         } else {
223                 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
224                         DEBUG(1,("the challenge that the password server (%s) supplied us is not the one we gave our client. This just can't work :-(\n", cli->desthost));
225                         return NT_STATUS_LOGON_FAILURE;         
226                 }
227         }
228
229         if(badpass[0] == 0)
230                 memset(badpass, 0x1f, sizeof(badpass));
231
232         if((user_info->nt_resp.length == sizeof(badpass)) && 
233            !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
234                 /* 
235                  * Very unlikely, our random bad password is the same as the users
236                  * password.
237                  */
238                 memset(badpass, badpass[0]+1, sizeof(badpass));
239         }
240
241         if(baduser[0] == 0) {
242                 fstrcpy(baduser, INVALID_USER_PREFIX);
243                 fstrcat(baduser, global_myname);
244         }
245
246         /*
247          * Attempt a session setup with a totally incorrect password.
248          * If this succeeds with the guest bit *NOT* set then the password
249          * server is broken and is not correctly setting the guest bit. We
250          * need to detect this as some versions of NT4.x are broken. JRA.
251          */
252
253         /* I sure as hell hope that there arn't servers out there that take 
254          * NTLMv2 and have this bug, as we don't test for that... 
255          *  - abartlet@samba.org
256          */
257
258         if ((!tested_password_server) && (lp_paranoid_server_security())) {
259                 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass), 
260                                         (char *)badpass, sizeof(badpass), user_info->domain.str)) {
261
262                         /*
263                          * We connected to the password server so we
264                          * can say we've tested it.
265                          */
266                         tested_password_server = True;
267
268                         if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
269                                 DEBUG(0,("server_validate: password server %s allows users as non-guest \
270 with a bad password.\n", cli->desthost));
271                                 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
272 use this machine as the password server.\n"));
273                                 cli_ulogoff(cli);
274
275                                 /*
276                                  * Password server has the bug.
277                                  */
278                                 bad_password_server = True;
279                                 return NT_STATUS_LOGON_FAILURE;
280                         }
281                         cli_ulogoff(cli);
282                 }
283         } else {
284
285                 /*
286                  * We have already tested the password server.
287                  * Fail immediately if it has the bug.
288                  */
289
290                 if(bad_password_server) {
291                         DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
292 with a bad password.\n", cli->desthost));
293                         DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
294 use this machine as the password server.\n"));
295                         return NT_STATUS_LOGON_FAILURE;
296                 }
297         }
298
299         /*
300          * Now we know the password server will correctly set the guest bit, or is
301          * not guest enabled, we can try with the real password.
302          */
303
304         if (!user_info->encrypted) {
305                 /* Plaintext available */
306                 if (!cli_session_setup(cli, user_info->smb_name.str, 
307                                        (char *)user_info->plaintext_password.data, 
308                                        user_info->plaintext_password.length, 
309                                        NULL, 0,
310                                        user_info->domain.str)) {
311                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
312                         /* Make this cli_nt_error() when the conversion is in */
313                         nt_status = cli_nt_error(cli);
314                 } else {
315                         nt_status = NT_STATUS_OK;
316                 }
317         } else {
318                 if (!cli_session_setup(cli, user_info->smb_name.str, 
319                                        (char *)user_info->lm_resp.data, 
320                                        user_info->lm_resp.length, 
321                                        (char *)user_info->nt_resp.data, 
322                                        user_info->nt_resp.length, 
323                                        user_info->domain.str)) {
324                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
325                         /* Make this cli_nt_error() when the conversion is in */
326                         nt_status = cli_nt_error(cli);
327                 } else {
328                         nt_status = NT_STATUS_OK;
329                 }
330         }
331
332         /* if logged in as guest then reject */
333         if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
334                 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
335                 nt_status = NT_STATUS_LOGON_FAILURE;
336         }
337
338         cli_ulogoff(cli);
339
340         if NT_STATUS_IS_OK(nt_status) {
341                 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
342                 if (pass) {
343                         if (!make_server_info_pw(server_info, pass)) { 
344                                 nt_status = NT_STATUS_NO_MEMORY;
345                         }
346                 } else {
347                         nt_status = NT_STATUS_NO_SUCH_USER;
348                 }
349         }
350
351         if (locally_made_cli) {
352                 cli_shutdown(cli);
353         }
354
355         return(nt_status);
356 }
357
358 BOOL auth_init_smbserver(struct auth_context *auth_context, auth_methods **auth_method) 
359 {
360         if (!make_auth_methods(auth_context, auth_method)) {
361                 return False;
362         }
363         (*auth_method)->auth = check_smbserver_security;
364         (*auth_method)->get_chal = auth_get_challenge_server;
365         (*auth_method)->send_keepalive = send_server_keepalive;
366         (*auth_method)->free_private_data = free_server_private_data;
367         return True;
368 }