2 Unix SMB/CIFS implementation.
3 Authenticate to a remote server
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
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.
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.
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.
25 #define DBGC_CLASS DBGC_AUTH
27 extern userdom_struct current_user_info;
29 /****************************************************************************
30 Support for server level security.
31 ****************************************************************************/
33 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
35 struct cli_state *cli = NULL;
37 struct in_addr dest_ip;
40 BOOL connected_ok = False;
42 if (!(cli = cli_initialise()))
45 /* security = server just can't function with spnego */
46 cli->use_spnego = False;
48 pserver = talloc_strdup(mem_ctx, lp_passwordserver());
51 while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
52 standard_sub_basic(current_user_info.smb_name, current_user_info.domain,
53 desthost, sizeof(desthost));
56 if(!resolve_name( desthost, &dest_ip, 0x20)) {
57 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
61 if (ismyip(dest_ip)) {
62 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
66 /* we use a mutex to prevent two connections at once - when a
67 Win2k PDC get two connections where one hasn't completed a
68 session setup yet it will send a TCP reset to the first
69 connection (tridge) */
71 if (!grab_server_mutex(desthost)) {
75 if (cli_connect(cli, desthost, &dest_ip)) {
76 DEBUG(3,("connected to password server %s\n",desthost));
83 release_server_mutex();
84 DEBUG(0,("password server not available\n"));
89 if (!attempt_netbios_session_request(&cli, global_myname(),
90 desthost, &dest_ip)) {
91 release_server_mutex();
92 DEBUG(1,("password server fails session request\n"));
97 if (strequal(desthost,myhostname())) {
98 exit_server_cleanly("Password server loop!");
101 DEBUG(3,("got session\n"));
103 if (!cli_negprot(cli)) {
104 DEBUG(1,("%s rejected the negprot\n",desthost));
105 release_server_mutex();
110 if (cli->protocol < PROTOCOL_LANMAN2 ||
111 !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
112 DEBUG(1,("%s isn't in user level security mode\n",desthost));
113 release_server_mutex();
118 /* Get the first session setup done quickly, to avoid silly
119 Win2k bugs. (The next connection to the server will kill
123 if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 0, "", 0,
125 DEBUG(0,("%s rejected the initial session setup (%s)\n",
126 desthost, cli_errstr(cli)));
127 release_server_mutex();
132 release_server_mutex();
134 DEBUG(3,("password server OK\n"));
139 struct server_security_state {
140 struct cli_state *cli;
143 /****************************************************************************
144 Send a 'keepalive' packet down the cli pipe.
145 ****************************************************************************/
147 static BOOL send_server_keepalive(const struct timeval *now,
150 struct server_security_state *state = talloc_get_type_abort(
151 private_data, struct server_security_state);
153 if (!state->cli || !state->cli->initialised) {
157 if (send_keepalive(state->cli->fd)) {
161 DEBUG( 2, ( "send_server_keepalive: password server keepalive "
163 cli_shutdown(state->cli);
168 static int destroy_server_security(struct server_security_state *state)
171 cli_shutdown(state->cli);
176 static struct server_security_state *make_server_security_state(struct cli_state *cli)
178 struct server_security_state *result;
180 if (!(result = talloc(NULL, struct server_security_state))) {
181 DEBUG(0, ("talloc failed\n"));
187 talloc_set_destructor(result, destroy_server_security);
189 if (lp_keepalive() != 0) {
190 struct timeval interval;
191 interval.tv_sec = lp_keepalive();
192 interval.tv_usec = 0;
194 if (event_add_idle(smbd_event_context(), result, interval,
195 "server_security_keepalive",
196 send_server_keepalive,
198 DEBUG(0, ("event_add_idle failed\n"));
207 /****************************************************************************
208 Get the challenge out of a password server.
209 ****************************************************************************/
211 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
212 void **my_private_data,
215 struct cli_state *cli = server_cryptkey(mem_ctx);
218 DEBUG(3,("using password server validation\n"));
220 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
221 /* We can't work with unencrypted password servers
222 unless 'encrypt passwords = no' */
223 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
225 /* However, it is still a perfectly fine connection
226 to pass that unencrypted password over */
228 (void *)make_server_security_state(cli);
229 return data_blob_null;
231 } else if (cli->secblob.length < 8) {
232 /* We can't do much if we don't get a full challenge */
233 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
235 return data_blob_null;
238 if (!(*my_private_data = (void *)make_server_security_state(cli))) {
239 return data_blob_null;
242 /* The return must be allocated on the caller's mem_ctx, as our own will be
243 destoyed just after the call. */
244 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
246 return data_blob_null;
251 /****************************************************************************
252 Check for a valid username and password in security=server mode.
253 - Validate a password with the password server.
254 ****************************************************************************/
256 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
257 void *my_private_data,
259 const auth_usersupplied_info *user_info,
260 auth_serversupplied_info **server_info)
262 struct cli_state *cli;
263 static unsigned char badpass[24];
264 static fstring baduser;
265 static BOOL tested_password_server = False;
266 static BOOL bad_password_server = False;
267 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
268 BOOL locally_made_cli = False;
271 * Check that the requested domain is not our own machine name.
272 * If it is, we should never check the PDC here, we use our own local
276 if(is_myname(user_info->domain)) {
277 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
281 cli = (struct cli_state *)my_private_data;
285 cli = server_cryptkey(mem_ctx);
286 locally_made_cli = True;
289 if (!cli || !cli->initialised) {
290 DEBUG(1,("password server is not connected (cli not initilised)\n"));
291 return NT_STATUS_LOGON_FAILURE;
294 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
295 if (user_info->encrypted) {
296 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
297 return NT_STATUS_LOGON_FAILURE;
300 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
301 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));
302 return NT_STATUS_LOGON_FAILURE;
307 memset(badpass, 0x1f, sizeof(badpass));
309 if((user_info->nt_resp.length == sizeof(badpass)) &&
310 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
312 * Very unlikely, our random bad password is the same as the users
315 memset(badpass, badpass[0]+1, sizeof(badpass));
318 if(baduser[0] == 0) {
319 fstrcpy(baduser, INVALID_USER_PREFIX);
320 fstrcat(baduser, global_myname());
324 * Attempt a session setup with a totally incorrect password.
325 * If this succeeds with the guest bit *NOT* set then the password
326 * server is broken and is not correctly setting the guest bit. We
327 * need to detect this as some versions of NT4.x are broken. JRA.
330 /* I sure as hell hope that there aren't servers out there that take
331 * NTLMv2 and have this bug, as we don't test for that...
332 * - abartlet@samba.org
335 if ((!tested_password_server) && (lp_paranoid_server_security())) {
336 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
341 user_info->domain))) {
344 * We connected to the password server so we
345 * can say we've tested it.
347 tested_password_server = True;
349 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
350 DEBUG(0,("server_validate: password server %s allows users as non-guest \
351 with a bad password.\n", cli->desthost));
352 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
353 use this machine as the password server.\n"));
357 * Password server has the bug.
359 bad_password_server = True;
360 return NT_STATUS_LOGON_FAILURE;
367 * We have already tested the password server.
368 * Fail immediately if it has the bug.
371 if(bad_password_server) {
372 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
373 with a bad password.\n", cli->desthost));
374 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
375 use this machine as the password server.\n"));
376 return NT_STATUS_LOGON_FAILURE;
381 * Now we know the password server will correctly set the guest bit, or is
382 * not guest enabled, we can try with the real password.
385 if (!user_info->encrypted) {
386 /* Plaintext available */
387 nt_status = cli_session_setup(
388 cli, user_info->smb_name,
389 (char *)user_info->plaintext_password.data,
390 user_info->plaintext_password.length,
391 NULL, 0, user_info->domain);
394 nt_status = cli_session_setup(
395 cli, user_info->smb_name,
396 (char *)user_info->lm_resp.data,
397 user_info->lm_resp.length,
398 (char *)user_info->nt_resp.data,
399 user_info->nt_resp.length,
403 if (!NT_STATUS_IS_OK(nt_status)) {
404 DEBUG(1,("password server %s rejected the password: %s\n",
405 cli->desthost, nt_errstr(nt_status)));
408 /* if logged in as guest then reject */
409 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
410 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
411 nt_status = NT_STATUS_LOGON_FAILURE;
416 if (NT_STATUS_IS_OK(nt_status)) {
417 fstring real_username;
420 if ( (pass = smb_getpwnam( NULL, user_info->internal_username,
421 real_username, True )) != NULL )
423 /* if a real user check pam account restrictions */
424 /* only really perfomed if "obey pam restriction" is true */
425 nt_status = smb_pam_accountcheck(pass->pw_name);
426 if ( !NT_STATUS_IS_OK(nt_status)) {
427 DEBUG(1, ("PAM account restriction prevents user login\n"));
430 nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
436 nt_status = NT_STATUS_NO_SUCH_USER;
440 if (locally_made_cli) {
447 static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
449 if (!make_auth_methods(auth_context, auth_method)) {
450 return NT_STATUS_NO_MEMORY;
452 (*auth_method)->name = "smbserver";
453 (*auth_method)->auth = check_smbserver_security;
454 (*auth_method)->get_chal = auth_get_challenge_server;
458 NTSTATUS auth_server_init(void)
460 return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);