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 /****************************************************************************
140 Clean up our allocated cli.
141 ****************************************************************************/
143 static void free_server_private_data(void **private_data_pointer)
145 struct cli_state **cli = (struct cli_state **)private_data_pointer;
146 if (*cli && (*cli)->initialised) {
147 DEBUG(10, ("Shutting down smbserver connection\n"));
150 *private_data_pointer = NULL;
153 /****************************************************************************
154 Send a 'keepalive' packet down the cli pipe.
155 ****************************************************************************/
157 static void send_server_keepalive(void **private_data_pointer)
159 /* also send a keepalive to the password server if its still
161 if (private_data_pointer) {
162 struct cli_state *cli = (struct cli_state *)(*private_data_pointer);
163 if (cli && cli->initialised) {
164 if (!send_keepalive(cli->fd)) {
165 DEBUG( 2, ( "send_server_keepalive: password server keepalive failed.\n"));
167 *private_data_pointer = NULL;
173 /****************************************************************************
174 Get the challenge out of a password server.
175 ****************************************************************************/
177 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
178 void **my_private_data,
181 struct cli_state *cli = server_cryptkey(mem_ctx);
184 DEBUG(3,("using password server validation\n"));
186 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
187 /* We can't work with unencrypted password servers
188 unless 'encrypt passwords = no' */
189 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
191 /* However, it is still a perfectly fine connection
192 to pass that unencrypted password over */
193 *my_private_data = (void *)cli;
194 return data_blob(NULL, 0);
196 } else if (cli->secblob.length < 8) {
197 /* We can't do much if we don't get a full challenge */
198 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
200 return data_blob(NULL, 0);
203 *my_private_data = (void *)cli;
205 /* The return must be allocated on the caller's mem_ctx, as our own will be
206 destoyed just after the call. */
207 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
209 return data_blob(NULL, 0);
214 /****************************************************************************
215 Check for a valid username and password in security=server mode.
216 - Validate a password with the password server.
217 ****************************************************************************/
219 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
220 void *my_private_data,
222 const auth_usersupplied_info *user_info,
223 auth_serversupplied_info **server_info)
225 struct cli_state *cli;
226 static unsigned char badpass[24];
227 static fstring baduser;
228 static BOOL tested_password_server = False;
229 static BOOL bad_password_server = False;
230 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
231 BOOL locally_made_cli = False;
234 * Check that the requested domain is not our own machine name.
235 * If it is, we should never check the PDC here, we use our own local
239 if(is_myname(user_info->domain)) {
240 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
244 cli = (struct cli_state *)my_private_data;
248 cli = server_cryptkey(mem_ctx);
249 locally_made_cli = True;
252 if (!cli || !cli->initialised) {
253 DEBUG(1,("password server is not connected (cli not initilised)\n"));
254 return NT_STATUS_LOGON_FAILURE;
257 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
258 if (user_info->encrypted) {
259 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
260 return NT_STATUS_LOGON_FAILURE;
263 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
264 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));
265 return NT_STATUS_LOGON_FAILURE;
270 memset(badpass, 0x1f, sizeof(badpass));
272 if((user_info->nt_resp.length == sizeof(badpass)) &&
273 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
275 * Very unlikely, our random bad password is the same as the users
278 memset(badpass, badpass[0]+1, sizeof(badpass));
281 if(baduser[0] == 0) {
282 fstrcpy(baduser, INVALID_USER_PREFIX);
283 fstrcat(baduser, global_myname());
287 * Attempt a session setup with a totally incorrect password.
288 * If this succeeds with the guest bit *NOT* set then the password
289 * server is broken and is not correctly setting the guest bit. We
290 * need to detect this as some versions of NT4.x are broken. JRA.
293 /* I sure as hell hope that there aren't servers out there that take
294 * NTLMv2 and have this bug, as we don't test for that...
295 * - abartlet@samba.org
298 if ((!tested_password_server) && (lp_paranoid_server_security())) {
299 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
304 user_info->domain))) {
307 * We connected to the password server so we
308 * can say we've tested it.
310 tested_password_server = True;
312 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
313 DEBUG(0,("server_validate: password server %s allows users as non-guest \
314 with a bad password.\n", cli->desthost));
315 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
316 use this machine as the password server.\n"));
320 * Password server has the bug.
322 bad_password_server = True;
323 return NT_STATUS_LOGON_FAILURE;
330 * We have already tested the password server.
331 * Fail immediately if it has the bug.
334 if(bad_password_server) {
335 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
336 with a bad password.\n", cli->desthost));
337 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
338 use this machine as the password server.\n"));
339 return NT_STATUS_LOGON_FAILURE;
344 * Now we know the password server will correctly set the guest bit, or is
345 * not guest enabled, we can try with the real password.
348 if (!user_info->encrypted) {
349 /* Plaintext available */
350 nt_status = cli_session_setup(
351 cli, user_info->smb_name,
352 (char *)user_info->plaintext_password.data,
353 user_info->plaintext_password.length,
354 NULL, 0, user_info->domain);
357 nt_status = cli_session_setup(
358 cli, user_info->smb_name,
359 (char *)user_info->lm_resp.data,
360 user_info->lm_resp.length,
361 (char *)user_info->nt_resp.data,
362 user_info->nt_resp.length,
366 if (!NT_STATUS_IS_OK(nt_status)) {
367 DEBUG(1,("password server %s rejected the password: %s\n",
368 cli->desthost, nt_errstr(nt_status)));
371 /* if logged in as guest then reject */
372 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
373 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
374 nt_status = NT_STATUS_LOGON_FAILURE;
379 if (NT_STATUS_IS_OK(nt_status)) {
380 fstring real_username;
383 if ( (pass = smb_getpwnam( NULL, user_info->internal_username,
384 real_username, True )) != NULL )
386 /* if a real user check pam account restrictions */
387 /* only really perfomed if "obey pam restriction" is true */
388 nt_status = smb_pam_accountcheck(pass->pw_name);
389 if ( !NT_STATUS_IS_OK(nt_status)) {
390 DEBUG(1, ("PAM account restriction prevents user login\n"));
393 nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
399 nt_status = NT_STATUS_NO_SUCH_USER;
403 if (locally_made_cli) {
410 static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
412 if (!make_auth_methods(auth_context, auth_method)) {
413 return NT_STATUS_NO_MEMORY;
415 (*auth_method)->name = "smbserver";
416 (*auth_method)->auth = check_smbserver_security;
417 (*auth_method)->get_chal = auth_get_challenge_server;
418 (*auth_method)->send_keepalive = send_server_keepalive;
419 (*auth_method)->free_private_data = free_server_private_data;
423 NTSTATUS auth_server_init(void)
425 return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);