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;
26 extern userdom_struct current_user_info;
28 /****************************************************************************
29 Support for server level security.
30 ****************************************************************************/
32 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
34 struct cli_state *cli = NULL;
36 struct in_addr dest_ip;
38 BOOL connected_ok = False;
40 if (!(cli = cli_initialise(cli)))
43 /* security = server just can't function with spnego */
44 cli->use_spnego = False;
46 pserver = talloc_strdup(mem_ctx, lp_passwordserver());
49 while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
50 standard_sub_basic(current_user_info.smb_name, desthost);
53 if(!resolve_name( desthost, &dest_ip, 0x20)) {
54 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
58 if (ismyip(dest_ip)) {
59 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
63 if (cli_connect(cli, desthost, &dest_ip)) {
64 DEBUG(3,("connected to password server %s\n",desthost));
71 DEBUG(0,("password server not available\n"));
76 if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
79 if (strequal(desthost,myhostname())) {
80 exit_server("Password server loop!");
83 DEBUG(3,("got session\n"));
85 if (!cli_negprot(cli)) {
86 DEBUG(1,("%s rejected the negprot\n",desthost));
91 if (cli->protocol < PROTOCOL_LANMAN2 ||
92 !(cli->sec_mode & 1)) {
93 DEBUG(1,("%s isn't in user level security mode\n",desthost));
98 DEBUG(3,("password server OK\n"));
103 /****************************************************************************
104 Clean up our allocated cli.
105 ****************************************************************************/
107 static void free_server_private_data(void **private_data_pointer)
109 struct cli_state **cli = (struct cli_state **)private_data_pointer;
110 if (*cli && (*cli)->initialised) {
115 /****************************************************************************
116 Send a 'keepalive' packet down the cli pipe.
117 ****************************************************************************/
119 static void send_server_keepalive(void **private_data_pointer)
121 struct cli_state **cli = (struct cli_state **)private_data_pointer;
123 /* also send a keepalive to the password server if its still
125 if (cli && *cli && (*cli)->initialised) {
126 if (!send_keepalive((*cli)->fd)) {
127 DEBUG( 2, ( "password server keepalive failed.\n"));
133 /****************************************************************************
134 Get the challenge out of a password server.
135 ****************************************************************************/
137 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
138 void **my_private_data,
141 struct cli_state *cli = server_cryptkey(mem_ctx);
144 DEBUG(3,("using password server validation\n"));
146 if ((cli->sec_mode & 2) == 0) {
147 /* We can't work with unencrypted password servers
148 unless 'encrypt passwords = no' */
149 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
151 /* However, it is still a perfectly fine connection
152 to pass that unencrypted password over */
153 *my_private_data = (void *)cli;
154 return data_blob(NULL, 0);
156 } else if (cli->secblob.length < 8) {
157 /* We can't do much if we don't get a full challenge */
158 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
160 return data_blob(NULL, 0);
163 *my_private_data = (void *)cli;
165 /* The return must be allocated on the caller's mem_ctx, as our own will be
166 destoyed just after the call. */
167 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
169 return data_blob(NULL, 0);
174 /****************************************************************************
175 Check for a valid username and password in security=server mode.
176 - Validate a password with the password server.
177 ****************************************************************************/
179 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
180 void *my_private_data,
182 const auth_usersupplied_info *user_info,
183 auth_serversupplied_info **server_info)
185 struct cli_state *cli;
186 static unsigned char badpass[24];
187 static fstring baduser;
188 static BOOL tested_password_server = False;
189 static BOOL bad_password_server = False;
190 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
191 BOOL locally_made_cli = False;
194 * Check that the requested domain is not our own machine name.
195 * If it is, we should never check the PDC here, we use our own local
199 if(is_netbios_alias_or_name(user_info->domain.str)) {
200 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
201 return NT_STATUS_LOGON_FAILURE;
204 cli = my_private_data;
208 cli = server_cryptkey(mem_ctx);
209 locally_made_cli = True;
212 if (!cli || !cli->initialised) {
213 DEBUG(1,("password server is not connected (cli not initilised)\n"));
214 return NT_STATUS_LOGON_FAILURE;
217 if ((cli->sec_mode & 2) == 0) {
218 if (user_info->encrypted) {
219 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
220 return NT_STATUS_LOGON_FAILURE;
223 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
224 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));
225 return NT_STATUS_LOGON_FAILURE;
230 memset(badpass, 0x1f, sizeof(badpass));
232 if((user_info->nt_resp.length == sizeof(badpass)) &&
233 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
235 * Very unlikely, our random bad password is the same as the users
238 memset(badpass, badpass[0]+1, sizeof(badpass));
241 if(baduser[0] == 0) {
242 fstrcpy(baduser, INVALID_USER_PREFIX);
243 fstrcat(baduser, global_myname);
247 * Attempt a session setup with a totally incorrect password.
248 * If this succeeds with the guest bit *NOT* set then the password
249 * server is broken and is not correctly setting the guest bit. We
250 * need to detect this as some versions of NT4.x are broken. JRA.
253 /* I sure as hell hope that there arn't servers out there that take
254 * NTLMv2 and have this bug, as we don't test for that...
255 * - abartlet@samba.org
258 if ((!tested_password_server) && (lp_paranoid_server_security())) {
259 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass),
260 (char *)badpass, sizeof(badpass), user_info->domain.str)) {
263 * We connected to the password server so we
264 * can say we've tested it.
266 tested_password_server = True;
268 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
269 DEBUG(0,("server_validate: password server %s allows users as non-guest \
270 with a bad password.\n", cli->desthost));
271 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
272 use this machine as the password server.\n"));
276 * Password server has the bug.
278 bad_password_server = True;
279 return NT_STATUS_LOGON_FAILURE;
286 * We have already tested the password server.
287 * Fail immediately if it has the bug.
290 if(bad_password_server) {
291 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
292 with a bad password.\n", cli->desthost));
293 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
294 use this machine as the password server.\n"));
295 return NT_STATUS_LOGON_FAILURE;
300 * Now we know the password server will correctly set the guest bit, or is
301 * not guest enabled, we can try with the real password.
304 if (!user_info->encrypted) {
305 /* Plaintext available */
306 if (!cli_session_setup(cli, user_info->smb_name.str,
307 (char *)user_info->plaintext_password.data,
308 user_info->plaintext_password.length,
310 user_info->domain.str)) {
311 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
312 /* Make this cli_nt_error() when the conversion is in */
313 nt_status = cli_nt_error(cli);
315 nt_status = NT_STATUS_OK;
318 if (!cli_session_setup(cli, user_info->smb_name.str,
319 (char *)user_info->lm_resp.data,
320 user_info->lm_resp.length,
321 (char *)user_info->nt_resp.data,
322 user_info->nt_resp.length,
323 user_info->domain.str)) {
324 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
325 /* Make this cli_nt_error() when the conversion is in */
326 nt_status = cli_nt_error(cli);
328 nt_status = NT_STATUS_OK;
332 /* if logged in as guest then reject */
333 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
334 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
335 nt_status = NT_STATUS_LOGON_FAILURE;
340 if NT_STATUS_IS_OK(nt_status) {
341 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
343 if (!make_server_info_pw(server_info, pass)) {
344 nt_status = NT_STATUS_NO_MEMORY;
347 nt_status = NT_STATUS_NO_SUCH_USER;
351 if (locally_made_cli) {
358 BOOL auth_init_smbserver(struct auth_context *auth_context, auth_methods **auth_method)
360 if (!make_auth_methods(auth_context, auth_method)) {
363 (*auth_method)->auth = check_smbserver_security;
364 (*auth_method)->get_chal = auth_get_challenge_server;
365 (*auth_method)->send_keepalive = send_server_keepalive;
366 (*auth_method)->free_private_data = free_server_private_data;