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 DGBC_AUTH
27 extern pstring global_myname;
28 extern userdom_struct current_user_info;
30 /****************************************************************************
31 Support for server level security.
32 ****************************************************************************/
34 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
36 struct cli_state *cli = NULL;
38 struct in_addr dest_ip;
40 BOOL connected_ok = False;
42 if (!(cli = cli_initialise(cli)))
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, desthost);
55 if(!resolve_name( desthost, &dest_ip, 0x20)) {
56 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
60 if (ismyip(dest_ip)) {
61 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
65 if (cli_connect(cli, desthost, &dest_ip)) {
66 DEBUG(3,("connected to password server %s\n",desthost));
73 DEBUG(0,("password server not available\n"));
78 if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
81 if (strequal(desthost,myhostname())) {
82 exit_server("Password server loop!");
85 DEBUG(3,("got session\n"));
87 if (!cli_negprot(cli)) {
88 DEBUG(1,("%s rejected the negprot\n",desthost));
93 if (cli->protocol < PROTOCOL_LANMAN2 ||
94 !(cli->sec_mode & 1)) {
95 DEBUG(1,("%s isn't in user level security mode\n",desthost));
100 DEBUG(3,("password server OK\n"));
105 /****************************************************************************
106 Clean up our allocated cli.
107 ****************************************************************************/
109 static void free_server_private_data(void **private_data_pointer)
111 struct cli_state **cli = (struct cli_state **)private_data_pointer;
112 if (*cli && (*cli)->initialised) {
117 /****************************************************************************
118 Send a 'keepalive' packet down the cli pipe.
119 ****************************************************************************/
121 static void send_server_keepalive(void **private_data_pointer)
123 struct cli_state **cli = (struct cli_state **)private_data_pointer;
125 /* also send a keepalive to the password server if its still
127 if (cli && *cli && (*cli)->initialised) {
128 if (!send_keepalive((*cli)->fd)) {
129 DEBUG( 2, ( "password server keepalive failed.\n"));
135 /****************************************************************************
136 Get the challenge out of a password server.
137 ****************************************************************************/
139 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
140 void **my_private_data,
143 struct cli_state *cli = server_cryptkey(mem_ctx);
146 DEBUG(3,("using password server validation\n"));
148 if ((cli->sec_mode & 2) == 0) {
149 /* We can't work with unencrypted password servers
150 unless 'encrypt passwords = no' */
151 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
153 /* However, it is still a perfectly fine connection
154 to pass that unencrypted password over */
155 *my_private_data = (void *)cli;
156 return data_blob(NULL, 0);
158 } else if (cli->secblob.length < 8) {
159 /* We can't do much if we don't get a full challenge */
160 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
162 return data_blob(NULL, 0);
165 *my_private_data = (void *)cli;
167 /* The return must be allocated on the caller's mem_ctx, as our own will be
168 destoyed just after the call. */
169 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
171 return data_blob(NULL, 0);
176 /****************************************************************************
177 Check for a valid username and password in security=server mode.
178 - Validate a password with the password server.
179 ****************************************************************************/
181 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
182 void *my_private_data,
184 const auth_usersupplied_info *user_info,
185 auth_serversupplied_info **server_info)
187 struct cli_state *cli;
188 static unsigned char badpass[24];
189 static fstring baduser;
190 static BOOL tested_password_server = False;
191 static BOOL bad_password_server = False;
192 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
193 BOOL locally_made_cli = False;
196 * Check that the requested domain is not our own machine name.
197 * If it is, we should never check the PDC here, we use our own local
201 if(is_netbios_alias_or_name(user_info->domain.str)) {
202 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
203 return NT_STATUS_LOGON_FAILURE;
206 cli = my_private_data;
210 cli = server_cryptkey(mem_ctx);
211 locally_made_cli = True;
214 if (!cli || !cli->initialised) {
215 DEBUG(1,("password server is not connected (cli not initilised)\n"));
216 return NT_STATUS_LOGON_FAILURE;
219 if ((cli->sec_mode & 2) == 0) {
220 if (user_info->encrypted) {
221 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
222 return NT_STATUS_LOGON_FAILURE;
225 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
226 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));
227 return NT_STATUS_LOGON_FAILURE;
232 memset(badpass, 0x1f, sizeof(badpass));
234 if((user_info->nt_resp.length == sizeof(badpass)) &&
235 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
237 * Very unlikely, our random bad password is the same as the users
240 memset(badpass, badpass[0]+1, sizeof(badpass));
243 if(baduser[0] == 0) {
244 fstrcpy(baduser, INVALID_USER_PREFIX);
245 fstrcat(baduser, global_myname);
249 * Attempt a session setup with a totally incorrect password.
250 * If this succeeds with the guest bit *NOT* set then the password
251 * server is broken and is not correctly setting the guest bit. We
252 * need to detect this as some versions of NT4.x are broken. JRA.
255 /* I sure as hell hope that there arn't servers out there that take
256 * NTLMv2 and have this bug, as we don't test for that...
257 * - abartlet@samba.org
260 if ((!tested_password_server) && (lp_paranoid_server_security())) {
261 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass),
262 (char *)badpass, sizeof(badpass), user_info->domain.str)) {
265 * We connected to the password server so we
266 * can say we've tested it.
268 tested_password_server = True;
270 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
271 DEBUG(0,("server_validate: password server %s allows users as non-guest \
272 with a bad password.\n", cli->desthost));
273 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
274 use this machine as the password server.\n"));
278 * Password server has the bug.
280 bad_password_server = True;
281 return NT_STATUS_LOGON_FAILURE;
288 * We have already tested the password server.
289 * Fail immediately if it has the bug.
292 if(bad_password_server) {
293 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
294 with a bad password.\n", cli->desthost));
295 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
296 use this machine as the password server.\n"));
297 return NT_STATUS_LOGON_FAILURE;
302 * Now we know the password server will correctly set the guest bit, or is
303 * not guest enabled, we can try with the real password.
306 if (!user_info->encrypted) {
307 /* Plaintext available */
308 if (!cli_session_setup(cli, user_info->smb_name.str,
309 (char *)user_info->plaintext_password.data,
310 user_info->plaintext_password.length,
312 user_info->domain.str)) {
313 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
314 /* Make this cli_nt_error() when the conversion is in */
315 nt_status = cli_nt_error(cli);
317 nt_status = NT_STATUS_OK;
320 if (!cli_session_setup(cli, user_info->smb_name.str,
321 (char *)user_info->lm_resp.data,
322 user_info->lm_resp.length,
323 (char *)user_info->nt_resp.data,
324 user_info->nt_resp.length,
325 user_info->domain.str)) {
326 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
327 /* Make this cli_nt_error() when the conversion is in */
328 nt_status = cli_nt_error(cli);
330 nt_status = NT_STATUS_OK;
334 /* if logged in as guest then reject */
335 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
336 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
337 nt_status = NT_STATUS_LOGON_FAILURE;
342 if NT_STATUS_IS_OK(nt_status) {
343 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
345 if (!make_server_info_pw(server_info, pass)) {
346 nt_status = NT_STATUS_NO_MEMORY;
349 nt_status = NT_STATUS_NO_SUCH_USER;
353 if (locally_made_cli) {
360 BOOL auth_init_smbserver(struct auth_context *auth_context, auth_methods **auth_method)
362 if (!make_auth_methods(auth_context, auth_method)) {
365 (*auth_method)->auth = check_smbserver_security;
366 (*auth_method)->get_chal = auth_get_challenge_server;
367 (*auth_method)->send_keepalive = send_server_keepalive;
368 (*auth_method)->free_private_data = free_server_private_data;