2 Unix SMB/Netbios implementation.
4 Authenticate to a remote server
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Andrew Bartlett 2001
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.
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.
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.
25 extern pstring global_myname;
27 /****************************************************************************
28 Support for server level security.
29 ****************************************************************************/
31 static struct cli_state *server_cryptkey(void)
33 struct cli_state *cli = NULL;
35 struct in_addr dest_ip;
37 BOOL connected_ok = False;
39 if (!(cli = cli_initialise(cli)))
42 /* security = server just can't function with spnego */
43 cli->use_spnego = False;
45 pserver = strdup(lp_passwordserver());
48 while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
49 standard_sub_basic(desthost);
52 if(!resolve_name( desthost, &dest_ip, 0x20)) {
53 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
57 if (ismyip(dest_ip)) {
58 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
62 if (cli_connect(cli, desthost, &dest_ip)) {
63 DEBUG(3,("connected to password server %s\n",desthost));
72 DEBUG(0,("password server not available\n"));
77 if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
80 if (strequal(desthost,myhostname())) {
81 exit_server("Password server loop!");
84 DEBUG(3,("got session\n"));
86 if (!cli_negprot(cli)) {
87 DEBUG(1,("%s rejected the negprot\n",desthost));
92 if (cli->protocol < PROTOCOL_LANMAN2 ||
93 !(cli->sec_mode & 1)) {
94 DEBUG(1,("%s isn't in user level security mode\n",desthost));
99 DEBUG(3,("password server OK\n"));
104 /****************************************************************************
105 Clean up our allocated cli.
106 ****************************************************************************/
108 static void free_server_private_data(void **private_data_pointer)
110 struct cli_state **cli = (struct cli_state **)private_data_pointer;
111 if (*cli && (*cli)->initialised) {
118 /****************************************************************************
119 Send a 'keepalive' packet down the cli pipe.
120 ****************************************************************************/
122 static void send_server_keepalive(void **private_data_pointer)
124 struct cli_state **cli = (struct cli_state **)private_data_pointer;
126 /* also send a keepalive to the password server if its still
128 if (cli && *cli && (*cli)->initialised) {
129 if (!send_keepalive((*cli)->fd)) {
130 DEBUG( 2, ( "password server keepalive failed.\n"));
137 /****************************************************************************
138 Get the challenge out of a password server.
139 ****************************************************************************/
141 static DATA_BLOB auth_get_challenge_server(void **my_private_data, const struct authsupplied_info *auth_info)
143 struct cli_state *cli = server_cryptkey();
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 challenge available..\n"));
152 *my_private_data = (void *)cli;
153 return data_blob(NULL, 0);
155 } else if (cli->secblob.length < 8) {
156 /* We can't do much if we don't get a full challenge */
157 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
159 return data_blob(NULL, 0);
162 *my_private_data = (void *)cli;
164 return data_blob(cli->secblob.data,8);
166 return data_blob(NULL, 0);
171 /****************************************************************************
172 Check for a valid username and password in security=server mode.
173 - Validate a password with the password server.
174 ****************************************************************************/
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)
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;
190 * Check that the requested domain is not our own machine name.
191 * If it is, we should never check the PDC here, we use our own local
195 if(is_netbios_alias_or_name(user_info->domain.str)) {
196 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
197 return NT_STATUS_LOGON_FAILURE;
200 cli = my_private_data;
204 cli = server_cryptkey();
205 locally_made_cli = True;
208 if (!cli || !cli->initialised) {
209 DEBUG(1,("password server %s is not connected\n", cli->desthost));
210 return NT_STATUS_LOGON_FAILURE;
213 if ((cli->sec_mode & 2) == 0) {
214 if (user_info->encrypted) {
215 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
216 return NT_STATUS_LOGON_FAILURE;
219 if (memcmp(cli->secblob.data, auth_info->challenge.data, 8) != 0) {
220 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));
221 return NT_STATUS_LOGON_FAILURE;
226 memset(badpass, 0x1f, sizeof(badpass));
228 if((user_info->nt_resp.length == sizeof(badpass)) &&
229 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
231 * Very unlikely, our random bad password is the same as the users
234 memset(badpass, badpass[0]+1, sizeof(badpass));
237 if(baduser[0] == 0) {
238 fstrcpy(baduser, INVALID_USER_PREFIX);
239 fstrcat(baduser, global_myname);
243 * Attempt a session setup with a totally incorrect password.
244 * If this succeeds with the guest bit *NOT* set then the password
245 * server is broken and is not correctly setting the guest bit. We
246 * need to detect this as some versions of NT4.x are broken. JRA.
249 /* I sure as hell hope that there arn't servers out there that take
250 * NTLMv2 and have this bug, as we don't test for that...
251 * - abartlet@samba.org
254 if ((!tested_password_server) && (lp_paranoid_server_security())) {
255 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass),
256 (char *)badpass, sizeof(badpass), user_info->domain.str)) {
259 * We connected to the password server so we
260 * can say we've tested it.
262 tested_password_server = True;
264 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
265 DEBUG(0,("server_validate: password server %s allows users as non-guest \
266 with a bad password.\n", cli->desthost));
267 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
268 use this machine as the password server.\n"));
272 * Password server has the bug.
274 bad_password_server = True;
275 return NT_STATUS_LOGON_FAILURE;
282 * We have already tested the password server.
283 * Fail immediately if it has the bug.
286 if(bad_password_server) {
287 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
288 with a bad password.\n", cli->desthost));
289 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
290 use this machine as the password server.\n"));
291 return NT_STATUS_LOGON_FAILURE;
296 * Now we know the password server will correctly set the guest bit, or is
297 * not guest enabled, we can try with the real password.
300 if (!user_info->encrypted) {
301 /* Plaintext available */
302 if (!cli_session_setup(cli, user_info->smb_name.str,
303 (char *)user_info->plaintext_password.data,
304 user_info->plaintext_password.length,
306 user_info->domain.str)) {
307 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
308 /* Make this cli_nt_error() when the conversion is in */
309 nt_status = cli_nt_error(cli);
311 nt_status = NT_STATUS_OK;
314 if (!cli_session_setup(cli, user_info->smb_name.str,
315 (char *)user_info->lm_resp.data,
316 user_info->lm_resp.length,
317 (char *)user_info->nt_resp.data,
318 user_info->nt_resp.length,
319 user_info->domain.str)) {
320 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
321 /* Make this cli_nt_error() when the conversion is in */
322 nt_status = cli_nt_error(cli);
324 nt_status = NT_STATUS_OK;
328 /* if logged in as guest then reject */
329 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
330 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
331 nt_status = NT_STATUS_LOGON_FAILURE;
336 if NT_STATUS_IS_OK(nt_status) {
337 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
339 if (!make_server_info_pw(server_info, pass)) {
340 nt_status = NT_STATUS_NO_MEMORY;
343 nt_status = NT_STATUS_NO_SUCH_USER;
347 if (locally_made_cli) {
355 BOOL auth_init_smbserver(auth_methods **auth_method)
357 if (!make_auth_methods(auth_method)) {
360 (*auth_method)->auth = check_smbserver_security;
361 (*auth_method)->get_chal = auth_get_challenge_server;
362 (*auth_method)->send_keepalive = send_server_keepalive;
363 (*auth_method)->free_private_data = free_server_private_data;