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;
189 cli = my_private_data;
193 cli = server_cryptkey();
194 locally_made_cli = True;
197 if (!cli || !cli->initialised) {
198 DEBUG(1,("password server %s is not connected\n", cli->desthost));
199 return NT_STATUS_LOGON_FAILURE;
202 if ((cli->sec_mode & 2) == 0) {
203 if (user_info->encrypted) {
204 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
205 return NT_STATUS_LOGON_FAILURE;
208 if (memcmp(cli->secblob.data, auth_info->challenge.data, 8) != 0) {
209 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));
210 return NT_STATUS_LOGON_FAILURE;
215 memset(badpass, 0x1f, sizeof(badpass));
217 if((user_info->nt_resp.length == sizeof(badpass)) &&
218 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
220 * Very unlikely, our random bad password is the same as the users
223 memset(badpass, badpass[0]+1, sizeof(badpass));
226 if(baduser[0] == 0) {
227 fstrcpy(baduser, INVALID_USER_PREFIX);
228 fstrcat(baduser, global_myname);
232 * Attempt a session setup with a totally incorrect password.
233 * If this succeeds with the guest bit *NOT* set then the password
234 * server is broken and is not correctly setting the guest bit. We
235 * need to detect this as some versions of NT4.x are broken. JRA.
238 /* I sure as hell hope that there arn't servers out there that take
239 * NTLMv2 and have this bug, as we don't test for that...
240 * - abartlet@samba.org
243 if ((!tested_password_server) && (lp_paranoid_server_security())) {
244 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass),
245 (char *)badpass, sizeof(badpass), user_info->domain.str)) {
248 * We connected to the password server so we
249 * can say we've tested it.
251 tested_password_server = True;
253 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
254 DEBUG(0,("server_validate: password server %s allows users as non-guest \
255 with a bad password.\n", cli->desthost));
256 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
257 use this machine as the password server.\n"));
261 * Password server has the bug.
263 bad_password_server = True;
264 return NT_STATUS_LOGON_FAILURE;
271 * We have already tested the password server.
272 * Fail immediately if it has the bug.
275 if(bad_password_server) {
276 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
277 with a bad password.\n", cli->desthost));
278 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
279 use this machine as the password server.\n"));
280 return NT_STATUS_LOGON_FAILURE;
285 * Now we know the password server will correctly set the guest bit, or is
286 * not guest enabled, we can try with the real password.
289 if (!user_info->encrypted) {
290 /* Plaintext available */
291 if (!cli_session_setup(cli, user_info->smb_name.str,
292 (char *)user_info->plaintext_password.data,
293 user_info->plaintext_password.length,
295 user_info->domain.str)) {
296 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
297 /* Make this cli_nt_error() when the conversion is in */
298 nt_status = cli_nt_error(cli);
300 nt_status = NT_STATUS_OK;
303 if (!cli_session_setup(cli, user_info->smb_name.str,
304 (char *)user_info->lm_resp.data,
305 user_info->lm_resp.length,
306 (char *)user_info->nt_resp.data,
307 user_info->nt_resp.length,
308 user_info->domain.str)) {
309 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
310 /* Make this cli_nt_error() when the conversion is in */
311 nt_status = cli_nt_error(cli);
313 nt_status = NT_STATUS_OK;
317 /* if logged in as guest then reject */
318 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
319 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
320 nt_status = NT_STATUS_LOGON_FAILURE;
325 if NT_STATUS_IS_OK(nt_status) {
326 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
328 if (!make_server_info_pw(server_info, pass)) {
329 nt_status = NT_STATUS_NO_MEMORY;
332 nt_status = NT_STATUS_NO_SUCH_USER;
336 if (locally_made_cli) {
344 BOOL auth_init_smbserver(auth_methods **auth_method)
346 if (!make_auth_methods(auth_method)) {
349 (*auth_method)->auth = check_smbserver_security;
350 (*auth_method)->get_chal = auth_get_challenge_server;
351 (*auth_method)->send_keepalive = send_server_keepalive;
352 (*auth_method)->free_private_data = free_server_private_data;