Merge from appliance-head: when creating a default security descriptor
[samba.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
27 /****************************************************************************
28  Support for server level security.
29 ****************************************************************************/
30
31 static struct cli_state *server_cryptkey(void)
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 = strdup(lp_passwordserver());
46         p = pserver;
47
48         while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
49                 standard_sub_basic(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         SAFE_FREE(pserver);
70
71         if (!connected_ok) {
72                 DEBUG(0,("password server not available\n"));
73                 cli_shutdown(cli);
74                 return NULL;
75         }
76
77         if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
78                 return NULL;
79         
80         if (strequal(desthost,myhostname())) {
81                 exit_server("Password server loop!");
82         }
83         
84         DEBUG(3,("got session\n"));
85
86         if (!cli_negprot(cli)) {
87                 DEBUG(1,("%s rejected the negprot\n",desthost));
88                 cli_shutdown(cli);
89                 return NULL;
90         }
91
92         if (cli->protocol < PROTOCOL_LANMAN2 ||
93             !(cli->sec_mode & 1)) {
94                 DEBUG(1,("%s isn't in user level security mode\n",desthost));
95                 cli_shutdown(cli);
96                 return NULL;
97         }
98
99         DEBUG(3,("password server OK\n"));
100
101         return cli;
102 }
103
104 /****************************************************************************
105  Clean up our allocated cli.
106 ****************************************************************************/
107
108 static void free_server_private_data(void **private_data_pointer) 
109 {
110         struct cli_state **cli = (struct cli_state **)private_data_pointer;
111         if (*cli && (*cli)->initialised) {
112                 cli_shutdown(*cli);
113                 
114                 SAFE_FREE(*cli);
115         }
116 }
117
118 /****************************************************************************
119  Send a 'keepalive' packet down the cli pipe.
120 ****************************************************************************/
121
122 static void send_server_keepalive(void **private_data_pointer) 
123 {
124         struct cli_state **cli = (struct cli_state **)private_data_pointer;
125         
126         /* also send a keepalive to the password server if its still
127            connected */
128         if (cli && *cli && (*cli)->initialised) {
129                 if (!send_keepalive((*cli)->fd)) {
130                         DEBUG( 2, ( "password server keepalive failed.\n"));
131                         cli_shutdown(*cli);
132                         SAFE_FREE(*cli);
133                 }
134         }
135 }
136
137 /****************************************************************************
138  Get the challange out of a password server.
139 ****************************************************************************/
140
141 static DATA_BLOB auth_get_challange_server(void **my_private_data, const struct authsupplied_info *auth_info) 
142 {
143         struct cli_state *cli = server_cryptkey();
144         
145         if (cli) {
146                 DEBUG(3,("using password server validation\n"));
147                 if ((cli->sec_mode & 2) == 0) {
148                         /* We can't work with unencrypted password servers
149                            unless 'encrypt passwords = no' */
150                         DEBUG(5,("make_auth_info_server: Server is unencrypted, no challange available..\n"));
151
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 challange */
157                         DEBUG(2,("make_auth_info_server: Didn't receive a full challange from server\n"));
158                         cli_shutdown(cli);
159                         return data_blob(NULL, 0);
160                 }
161
162                 *my_private_data = (void *)cli;
163                 
164                 return data_blob(cli->secblob.data,8);
165         } else {
166                 return data_blob(NULL, 0);
167         }
168 }
169
170
171 /****************************************************************************
172  Check for a valid username and password in security=server mode.
173   - Validate a password with the password server.
174 ****************************************************************************/
175
176 static NTSTATUS check_smbserver_security(void *my_private_data, 
177                                   const auth_usersupplied_info *user_info, 
178                                   const auth_authsupplied_info *auth_info,
179                                   auth_serversupplied_info **server_info)
180 {
181         struct cli_state *cli;
182         static unsigned char badpass[24];
183         static fstring baduser; 
184         static BOOL tested_password_server = False;
185         static BOOL bad_password_server = False;
186         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
187         BOOL locally_made_cli = False;
188
189         cli = my_private_data;
190         
191         if (cli) {
192         } else {
193                 cli = server_cryptkey();
194                 locally_made_cli = True;
195         }
196
197         if (!cli || !cli->initialised) {
198                 DEBUG(1,("password server %s is not connected\n", cli->desthost));
199                 return NT_STATUS_LOGON_FAILURE;
200         }  
201         
202         if ((cli->sec_mode & 2) == 0) {
203                 if (user_info->encrypted) {
204                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
205                         return NT_STATUS_LOGON_FAILURE;         
206                 }
207         } else {
208                 if (memcmp(cli->secblob.data, auth_info->challange.data, 8) != 0) {
209                         DEBUG(1,("the challange that the password server (%s) supplied us is not the one we gave our client. This just can't work :-(\n", cli->desthost));
210                         return NT_STATUS_LOGON_FAILURE;         
211                 }
212         }
213
214         if(badpass[0] == 0)
215                 memset(badpass, 0x1f, sizeof(badpass));
216
217         if((user_info->nt_resp.length == sizeof(badpass)) && 
218            !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
219                 /* 
220                  * Very unlikely, our random bad password is the same as the users
221                  * password.
222                  */
223                 memset(badpass, badpass[0]+1, sizeof(badpass));
224         }
225
226         if(baduser[0] == 0) {
227                 fstrcpy(baduser, INVALID_USER_PREFIX);
228                 fstrcat(baduser, global_myname);
229         }
230
231         /*
232          * Attempt a session setup with a totally incorrect password.
233          * If this succeeds with the guest bit *NOT* set then the password
234          * server is broken and is not correctly setting the guest bit. We
235          * need to detect this as some versions of NT4.x are broken. JRA.
236          */
237
238         /* I sure as hell hope that there arn't servers out there that take 
239          * NTLMv2 and have this bug, as we don't test for that... 
240          *  - abartlet@samba.org
241          */
242
243         if ((!tested_password_server) && (lp_paranoid_server_security())) {
244                 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass), 
245                                         (char *)badpass, sizeof(badpass), user_info->domain.str)) {
246
247                         /*
248                          * We connected to the password server so we
249                          * can say we've tested it.
250                          */
251                         tested_password_server = True;
252
253                         if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
254                                 DEBUG(0,("server_validate: password server %s allows users as non-guest \
255 with a bad password.\n", cli->desthost));
256                                 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
257 use this machine as the password server.\n"));
258                                 cli_ulogoff(cli);
259
260                                 /*
261                                  * Password server has the bug.
262                                  */
263                                 bad_password_server = True;
264                                 return NT_STATUS_LOGON_FAILURE;
265                         }
266                         cli_ulogoff(cli);
267                 }
268         } else {
269
270                 /*
271                  * We have already tested the password server.
272                  * Fail immediately if it has the bug.
273                  */
274
275                 if(bad_password_server) {
276                         DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
277 with a bad password.\n", cli->desthost));
278                         DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
279 use this machine as the password server.\n"));
280                         return NT_STATUS_LOGON_FAILURE;
281                 }
282         }
283
284         /*
285          * Now we know the password server will correctly set the guest bit, or is
286          * not guest enabled, we can try with the real password.
287          */
288
289         if (!user_info->encrypted) {
290                 /* Plaintext available */
291                 if (!cli_session_setup(cli, user_info->smb_name.str, 
292                                        (char *)user_info->plaintext_password.data, 
293                                        user_info->plaintext_password.length, 
294                                        NULL, 0,
295                                        user_info->domain.str)) {
296                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
297                         /* Make this cli_nt_error() when the conversion is in */
298                         nt_status = cli_nt_error(cli);
299                 } else {
300                         nt_status = NT_STATUS_OK;
301                 }
302         } else {
303                 if (!cli_session_setup(cli, user_info->smb_name.str, 
304                                        (char *)user_info->lm_resp.data, 
305                                        user_info->lm_resp.length, 
306                                        (char *)user_info->nt_resp.data, 
307                                        user_info->nt_resp.length, 
308                                        user_info->domain.str)) {
309                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
310                         /* Make this cli_nt_error() when the conversion is in */
311                         nt_status = cli_nt_error(cli);
312                 } else {
313                         nt_status = NT_STATUS_OK;
314                 }
315         }
316
317         /* if logged in as guest then reject */
318         if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
319                 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
320                 nt_status = NT_STATUS_LOGON_FAILURE;
321         }
322
323         cli_ulogoff(cli);
324
325         if NT_STATUS_IS_OK(nt_status) {
326                 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
327                 if (pass) {
328                         if (!make_server_info_pw(server_info, pass)) { 
329                                 nt_status = NT_STATUS_NO_MEMORY;
330                         }
331                 } else {
332                         nt_status = NT_STATUS_NO_SUCH_USER;
333                 }
334         }
335
336         if (locally_made_cli) {
337                 cli_shutdown(cli);
338                 SAFE_FREE(cli);
339         }
340
341         return(nt_status);
342 }
343
344 BOOL auth_init_smbserver(auth_methods **auth_method) 
345 {
346         if (!make_auth_methods(auth_method)) {
347                 return False;
348         }
349         (*auth_method)->auth = check_smbserver_security;
350         (*auth_method)->get_chal = auth_get_challange_server;
351         (*auth_method)->send_keepalive = send_server_keepalive;
352         (*auth_method)->free_private_data = free_server_private_data;
353         return True;
354 }