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