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()))
|
---|
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 | NTSTATUS status;
|
---|
53 |
|
---|
54 | standard_sub_basic(current_user_info.smb_name, current_user_info.domain,
|
---|
55 | desthost, sizeof(desthost));
|
---|
56 | strupper_m(desthost);
|
---|
57 |
|
---|
58 | if(!resolve_name( desthost, &dest_ip, 0x20)) {
|
---|
59 | DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
|
---|
60 | continue;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (ismyip(dest_ip)) {
|
---|
64 | DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
|
---|
65 | continue;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* we use a mutex to prevent two connections at once - when a
|
---|
69 | Win2k PDC get two connections where one hasn't completed a
|
---|
70 | session setup yet it will send a TCP reset to the first
|
---|
71 | connection (tridge) */
|
---|
72 |
|
---|
73 | if (!grab_server_mutex(desthost)) {
|
---|
74 | cli_shutdown(cli);
|
---|
75 | return NULL;
|
---|
76 | }
|
---|
77 |
|
---|
78 | status = cli_connect(cli, desthost, &dest_ip);
|
---|
79 | if (NT_STATUS_IS_OK(status)) {
|
---|
80 | DEBUG(3,("connected to password server %s\n",desthost));
|
---|
81 | connected_ok = True;
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
|
---|
85 | desthost, nt_errstr(status) ));
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (!connected_ok) {
|
---|
89 | release_server_mutex();
|
---|
90 | DEBUG(0,("password server not available\n"));
|
---|
91 | cli_shutdown(cli);
|
---|
92 | return NULL;
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (!attempt_netbios_session_request(&cli, global_myname(),
|
---|
96 | desthost, &dest_ip)) {
|
---|
97 | release_server_mutex();
|
---|
98 | DEBUG(1,("password server fails session request\n"));
|
---|
99 | cli_shutdown(cli);
|
---|
100 | return NULL;
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (strequal(desthost,myhostname())) {
|
---|
104 | exit_server_cleanly("Password server loop!");
|
---|
105 | }
|
---|
106 |
|
---|
107 | DEBUG(3,("got session\n"));
|
---|
108 |
|
---|
109 | if (!cli_negprot(cli)) {
|
---|
110 | DEBUG(1,("%s rejected the negprot\n",desthost));
|
---|
111 | release_server_mutex();
|
---|
112 | cli_shutdown(cli);
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (cli->protocol < PROTOCOL_LANMAN2 ||
|
---|
117 | !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
|
---|
118 | DEBUG(1,("%s isn't in user level security mode\n",desthost));
|
---|
119 | release_server_mutex();
|
---|
120 | cli_shutdown(cli);
|
---|
121 | return NULL;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Get the first session setup done quickly, to avoid silly
|
---|
125 | Win2k bugs. (The next connection to the server will kill
|
---|
126 | this one...
|
---|
127 | */
|
---|
128 |
|
---|
129 | if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 0, "", 0,
|
---|
130 | ""))) {
|
---|
131 | DEBUG(0,("%s rejected the initial session setup (%s)\n",
|
---|
132 | desthost, cli_errstr(cli)));
|
---|
133 | release_server_mutex();
|
---|
134 | cli_shutdown(cli);
|
---|
135 | return NULL;
|
---|
136 | }
|
---|
137 |
|
---|
138 | release_server_mutex();
|
---|
139 |
|
---|
140 | DEBUG(3,("password server OK\n"));
|
---|
141 |
|
---|
142 | return cli;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /****************************************************************************
|
---|
146 | Clean up our allocated cli.
|
---|
147 | ****************************************************************************/
|
---|
148 |
|
---|
149 | static void free_server_private_data(void **private_data_pointer)
|
---|
150 | {
|
---|
151 | struct cli_state **cli = (struct cli_state **)private_data_pointer;
|
---|
152 | if (*cli && (*cli)->initialised) {
|
---|
153 | DEBUG(10, ("Shutting down smbserver connection\n"));
|
---|
154 | cli_shutdown(*cli);
|
---|
155 | }
|
---|
156 | *private_data_pointer = NULL;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /****************************************************************************
|
---|
160 | Send a 'keepalive' packet down the cli pipe.
|
---|
161 | ****************************************************************************/
|
---|
162 |
|
---|
163 | static void send_server_keepalive(void **private_data_pointer)
|
---|
164 | {
|
---|
165 | /* also send a keepalive to the password server if its still
|
---|
166 | connected */
|
---|
167 | if (private_data_pointer) {
|
---|
168 | struct cli_state *cli = (struct cli_state *)(*private_data_pointer);
|
---|
169 | if (cli && cli->initialised) {
|
---|
170 | if (!send_keepalive(cli->fd)) {
|
---|
171 | DEBUG( 2, ( "send_server_keepalive: password server keepalive failed.\n"));
|
---|
172 | cli_shutdown(cli);
|
---|
173 | *private_data_pointer = NULL;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /****************************************************************************
|
---|
180 | Get the challenge out of a password server.
|
---|
181 | ****************************************************************************/
|
---|
182 |
|
---|
183 | static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
|
---|
184 | void **my_private_data,
|
---|
185 | TALLOC_CTX *mem_ctx)
|
---|
186 | {
|
---|
187 | struct cli_state *cli = server_cryptkey(mem_ctx);
|
---|
188 |
|
---|
189 | if (cli) {
|
---|
190 | DEBUG(3,("using password server validation\n"));
|
---|
191 |
|
---|
192 | if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
|
---|
193 | /* We can't work with unencrypted password servers
|
---|
194 | unless 'encrypt passwords = no' */
|
---|
195 | DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
|
---|
196 |
|
---|
197 | /* However, it is still a perfectly fine connection
|
---|
198 | to pass that unencrypted password over */
|
---|
199 | *my_private_data = (void *)cli;
|
---|
200 | return data_blob(NULL, 0);
|
---|
201 |
|
---|
202 | } else if (cli->secblob.length < 8) {
|
---|
203 | /* We can't do much if we don't get a full challenge */
|
---|
204 | DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
|
---|
205 | cli_shutdown(cli);
|
---|
206 | return data_blob(NULL, 0);
|
---|
207 | }
|
---|
208 |
|
---|
209 | *my_private_data = (void *)cli;
|
---|
210 |
|
---|
211 | /* The return must be allocated on the caller's mem_ctx, as our own will be
|
---|
212 | destoyed just after the call. */
|
---|
213 | return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
|
---|
214 | } else {
|
---|
215 | return data_blob(NULL, 0);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | /****************************************************************************
|
---|
221 | Check for a valid username and password in security=server mode.
|
---|
222 | - Validate a password with the password server.
|
---|
223 | ****************************************************************************/
|
---|
224 |
|
---|
225 | static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
|
---|
226 | void *my_private_data,
|
---|
227 | TALLOC_CTX *mem_ctx,
|
---|
228 | const auth_usersupplied_info *user_info,
|
---|
229 | auth_serversupplied_info **server_info)
|
---|
230 | {
|
---|
231 | struct cli_state *cli;
|
---|
232 | static unsigned char badpass[24];
|
---|
233 | static fstring baduser;
|
---|
234 | static BOOL tested_password_server = False;
|
---|
235 | static BOOL bad_password_server = False;
|
---|
236 | NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
|
---|
237 | BOOL locally_made_cli = False;
|
---|
238 |
|
---|
239 | cli = (struct cli_state *)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 (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
|
---|
295 | (char *)badpass,
|
---|
296 | sizeof(badpass),
|
---|
297 | (char *)badpass,
|
---|
298 | sizeof(badpass),
|
---|
299 | user_info->domain))) {
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * We connected to the password server so we
|
---|
303 | * can say we've tested it.
|
---|
304 | */
|
---|
305 | tested_password_server = True;
|
---|
306 |
|
---|
307 | if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
|
---|
308 | DEBUG(0,("server_validate: password server %s allows users as non-guest \
|
---|
309 | with a bad password.\n", cli->desthost));
|
---|
310 | DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
|
---|
311 | use this machine as the password server.\n"));
|
---|
312 | cli_ulogoff(cli);
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Password server has the bug.
|
---|
316 | */
|
---|
317 | bad_password_server = True;
|
---|
318 | return NT_STATUS_LOGON_FAILURE;
|
---|
319 | }
|
---|
320 | cli_ulogoff(cli);
|
---|
321 | }
|
---|
322 | } else {
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * We have already tested the password server.
|
---|
326 | * Fail immediately if it has the bug.
|
---|
327 | */
|
---|
328 |
|
---|
329 | if(bad_password_server) {
|
---|
330 | DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
|
---|
331 | with a bad password.\n", cli->desthost));
|
---|
332 | DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
|
---|
333 | use this machine as the password server.\n"));
|
---|
334 | return NT_STATUS_LOGON_FAILURE;
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * Now we know the password server will correctly set the guest bit, or is
|
---|
340 | * not guest enabled, we can try with the real password.
|
---|
341 | */
|
---|
342 |
|
---|
343 | if (!user_info->encrypted) {
|
---|
344 | /* Plaintext available */
|
---|
345 | nt_status = cli_session_setup(
|
---|
346 | cli, user_info->smb_name,
|
---|
347 | (char *)user_info->plaintext_password.data,
|
---|
348 | user_info->plaintext_password.length,
|
---|
349 | NULL, 0, user_info->domain);
|
---|
350 |
|
---|
351 | } else {
|
---|
352 | nt_status = cli_session_setup(
|
---|
353 | cli, user_info->smb_name,
|
---|
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);
|
---|
359 | }
|
---|
360 |
|
---|
361 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
362 | DEBUG(1,("password server %s rejected the password: %s\n",
|
---|
363 | cli->desthost, nt_errstr(nt_status)));
|
---|
364 | }
|
---|
365 |
|
---|
366 | /* if logged in as guest then reject */
|
---|
367 | if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
|
---|
368 | DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
|
---|
369 | nt_status = NT_STATUS_LOGON_FAILURE;
|
---|
370 | }
|
---|
371 |
|
---|
372 | cli_ulogoff(cli);
|
---|
373 |
|
---|
374 | if (NT_STATUS_IS_OK(nt_status)) {
|
---|
375 | fstring real_username;
|
---|
376 | struct passwd *pass;
|
---|
377 |
|
---|
378 | if ( (pass = smb_getpwnam( NULL, user_info->internal_username,
|
---|
379 | real_username, True )) != NULL )
|
---|
380 | {
|
---|
381 | /* if a real user check pam account restrictions */
|
---|
382 | /* only really perfomed if "obey pam restriction" is true */
|
---|
383 | nt_status = smb_pam_accountcheck(pass->pw_name);
|
---|
384 | if ( !NT_STATUS_IS_OK(nt_status)) {
|
---|
385 | DEBUG(1, ("PAM account restriction prevents user login\n"));
|
---|
386 | } else {
|
---|
387 |
|
---|
388 | nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
|
---|
389 | }
|
---|
390 | TALLOC_FREE(pass);
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | nt_status = NT_STATUS_NO_SUCH_USER;
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | if (locally_made_cli) {
|
---|
399 | cli_shutdown(cli);
|
---|
400 | }
|
---|
401 |
|
---|
402 | return(nt_status);
|
---|
403 | }
|
---|
404 |
|
---|
405 | static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
|
---|
406 | {
|
---|
407 | if (!make_auth_methods(auth_context, auth_method)) {
|
---|
408 | return NT_STATUS_NO_MEMORY;
|
---|
409 | }
|
---|
410 | (*auth_method)->name = "smbserver";
|
---|
411 | (*auth_method)->auth = check_smbserver_security;
|
---|
412 | (*auth_method)->get_chal = auth_get_challenge_server;
|
---|
413 | (*auth_method)->send_keepalive = send_server_keepalive;
|
---|
414 | (*auth_method)->free_private_data = free_server_private_data;
|
---|
415 | return NT_STATUS_OK;
|
---|
416 | }
|
---|
417 |
|
---|
418 | NTSTATUS auth_server_init(void)
|
---|
419 | {
|
---|
420 | return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);
|
---|
421 | }
|
---|