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