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) {
116 /****************************************************************************
117 Send a 'keepalive' packet down the cli pipe.
118 ****************************************************************************/
120 static void send_server_keepalive(void **private_data_pointer)
122 struct cli_state **cli = (struct cli_state **)private_data_pointer;
124 /* also send a keepalive to the password server if its still
126 if (cli && *cli && (*cli)->initialised) {
127 if (!send_keepalive((*cli)->fd)) {
128 DEBUG( 2, ( "password server keepalive failed.\n"));
134 /****************************************************************************
135 Get the challenge out of a password server.
136 ****************************************************************************/
138 static DATA_BLOB auth_get_challenge_server(void **my_private_data, const struct authsupplied_info *auth_info)
140 struct cli_state *cli = server_cryptkey();
143 DEBUG(3,("using password server validation\n"));
144 if ((cli->sec_mode & 2) == 0) {
145 /* We can't work with unencrypted password servers
146 unless 'encrypt passwords = no' */
147 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
149 *my_private_data = (void *)cli;
150 return data_blob(NULL, 0);
152 } else if (cli->secblob.length < 8) {
153 /* We can't do much if we don't get a full challenge */
154 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
156 return data_blob(NULL, 0);
159 *my_private_data = (void *)cli;
161 return data_blob(cli->secblob.data,8);
163 return data_blob(NULL, 0);
168 /****************************************************************************
169 Check for a valid username and password in security=server mode.
170 - Validate a password with the password server.
171 ****************************************************************************/
173 static NTSTATUS check_smbserver_security(void *my_private_data,
174 const auth_usersupplied_info *user_info,
175 const auth_authsupplied_info *auth_info,
176 auth_serversupplied_info **server_info)
178 struct cli_state *cli;
179 static unsigned char badpass[24];
180 static fstring baduser;
181 static BOOL tested_password_server = False;
182 static BOOL bad_password_server = False;
183 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
184 BOOL locally_made_cli = False;
187 * Check that the requested domain is not our own machine name.
188 * If it is, we should never check the PDC here, we use our own local
192 if(is_netbios_alias_or_name(user_info->domain.str)) {
193 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
194 return NT_STATUS_LOGON_FAILURE;
197 cli = my_private_data;
201 cli = server_cryptkey();
202 locally_made_cli = True;
205 if (!cli || !cli->initialised) {
206 DEBUG(1,("password server %s is not connected\n", cli->desthost));
207 return NT_STATUS_LOGON_FAILURE;
210 if ((cli->sec_mode & 2) == 0) {
211 if (user_info->encrypted) {
212 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
213 return NT_STATUS_LOGON_FAILURE;
216 if (memcmp(cli->secblob.data, auth_info->challenge.data, 8) != 0) {
217 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));
218 return NT_STATUS_LOGON_FAILURE;
223 memset(badpass, 0x1f, sizeof(badpass));
225 if((user_info->nt_resp.length == sizeof(badpass)) &&
226 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
228 * Very unlikely, our random bad password is the same as the users
231 memset(badpass, badpass[0]+1, sizeof(badpass));
234 if(baduser[0] == 0) {
235 fstrcpy(baduser, INVALID_USER_PREFIX);
236 fstrcat(baduser, global_myname);
240 * Attempt a session setup with a totally incorrect password.
241 * If this succeeds with the guest bit *NOT* set then the password
242 * server is broken and is not correctly setting the guest bit. We
243 * need to detect this as some versions of NT4.x are broken. JRA.
246 /* I sure as hell hope that there arn't servers out there that take
247 * NTLMv2 and have this bug, as we don't test for that...
248 * - abartlet@samba.org
251 if ((!tested_password_server) && (lp_paranoid_server_security())) {
252 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass),
253 (char *)badpass, sizeof(badpass), user_info->domain.str)) {
256 * We connected to the password server so we
257 * can say we've tested it.
259 tested_password_server = True;
261 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
262 DEBUG(0,("server_validate: password server %s allows users as non-guest \
263 with a bad password.\n", cli->desthost));
264 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
265 use this machine as the password server.\n"));
269 * Password server has the bug.
271 bad_password_server = True;
272 return NT_STATUS_LOGON_FAILURE;
279 * We have already tested the password server.
280 * Fail immediately if it has the bug.
283 if(bad_password_server) {
284 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
285 with a bad password.\n", cli->desthost));
286 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
287 use this machine as the password server.\n"));
288 return NT_STATUS_LOGON_FAILURE;
293 * Now we know the password server will correctly set the guest bit, or is
294 * not guest enabled, we can try with the real password.
297 if (!user_info->encrypted) {
298 /* Plaintext available */
299 if (!cli_session_setup(cli, user_info->smb_name.str,
300 (char *)user_info->plaintext_password.data,
301 user_info->plaintext_password.length,
303 user_info->domain.str)) {
304 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
305 /* Make this cli_nt_error() when the conversion is in */
306 nt_status = cli_nt_error(cli);
308 nt_status = NT_STATUS_OK;
311 if (!cli_session_setup(cli, user_info->smb_name.str,
312 (char *)user_info->lm_resp.data,
313 user_info->lm_resp.length,
314 (char *)user_info->nt_resp.data,
315 user_info->nt_resp.length,
316 user_info->domain.str)) {
317 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
318 /* Make this cli_nt_error() when the conversion is in */
319 nt_status = cli_nt_error(cli);
321 nt_status = NT_STATUS_OK;
325 /* if logged in as guest then reject */
326 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
327 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
328 nt_status = NT_STATUS_LOGON_FAILURE;
333 if NT_STATUS_IS_OK(nt_status) {
334 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
336 if (!make_server_info_pw(server_info, pass)) {
337 nt_status = NT_STATUS_NO_MEMORY;
340 nt_status = NT_STATUS_NO_SUCH_USER;
344 if (locally_made_cli) {
351 BOOL auth_init_smbserver(auth_methods **auth_method)
353 if (!make_auth_methods(auth_method)) {
356 (*auth_method)->auth = check_smbserver_security;
357 (*auth_method)->get_chal = auth_get_challenge_server;
358 (*auth_method)->send_keepalive = send_server_keepalive;
359 (*auth_method)->free_private_data = free_server_private_data;