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 3 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, see <http://www.gnu.org/licenses/>.
23 /****************************************************************************
24 Support for server level security.
25 ****************************************************************************/
27 static struct smbcli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
29 struct smbcli_state *cli = NULL;
31 struct ipv4_addr dest_ip;
34 BOOL connected_ok = False;
36 if (!(cli = smbcli_initialise(cli)))
39 /* security = server just can't function with spnego */
40 cli->use_spnego = False;
42 pserver = talloc_strdup(mem_ctx, lp_passwordserver());
45 while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
48 if(!resolve_name( desthost, &dest_ip, 0x20)) {
49 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
53 if (ismyip(dest_ip)) {
54 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
58 /* we use a mutex to prevent two connections at once - when a
59 Win2k PDC get two connections where one hasn't completed a
60 session setup yet it will send a TCP reset to the first
61 connection (tridge) */
63 if (!grab_server_mutex(desthost)) {
67 if (smbcli_connect(cli, desthost, &dest_ip)) {
68 DEBUG(3,("connected to password server %s\n",desthost));
75 release_server_mutex();
76 DEBUG(0,("password server not available\n"));
81 if (!attempt_netbios_session_request(cli, lp_netbios_name(),
82 desthost, &dest_ip)) {
83 release_server_mutex();
84 DEBUG(1,("password server fails session request\n"));
89 if (strequal(desthost,myhostname(mem_ctx))) {
90 exit_server("Password server loop!");
93 DEBUG(3,("got session\n"));
95 if (!smbcli_negprot(cli)) {
96 DEBUG(1,("%s rejected the negprot\n",desthost));
97 release_server_mutex();
102 if (cli->protocol < PROTOCOL_LANMAN2 ||
103 !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
104 DEBUG(1,("%s isn't in user level security mode\n",desthost));
105 release_server_mutex();
110 /* Get the first session setup done quickly, to avoid silly
111 Win2k bugs. (The next connection to the server will kill
115 if (!smbcli_session_setup(cli, "", "", 0, "", 0,
117 DEBUG(0,("%s rejected the initial session setup (%s)\n",
118 desthost, smbcli_errstr(cli)));
119 release_server_mutex();
124 release_server_mutex();
126 DEBUG(3,("password server OK\n"));
131 /****************************************************************************
132 Clean up our allocated cli.
133 ****************************************************************************/
135 static void free_server_private_data(void **private_data_pointer)
137 struct smbcli_state **cli = (struct smbcli_state **)private_data_pointer;
138 if (*cli && (*cli)->initialised) {
143 /****************************************************************************
144 Get the challenge out of a password server.
145 ****************************************************************************/
147 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
148 void **my_private_data,
151 struct smbcli_state *cli = server_cryptkey(mem_ctx);
154 DEBUG(3,("using password server validation\n"));
156 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
157 /* We can't work with unencrypted password servers
158 unless 'encrypt passwords = no' */
159 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
161 /* However, it is still a perfectly fine connection
162 to pass that unencrypted password over */
163 *my_private_data = (void *)cli;
164 return data_blob(NULL, 0);
166 } else if (cli->secblob.length < 8) {
167 /* We can't do much if we don't get a full challenge */
168 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
170 return data_blob(NULL, 0);
173 *my_private_data = (void *)cli;
175 /* The return must be allocated on the caller's mem_ctx, as our own will be
176 destoyed just after the call. */
177 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
179 return data_blob(NULL, 0);
184 /****************************************************************************
185 Check for a valid username and password in security=server mode.
186 - Validate a password with the password server.
187 ****************************************************************************/
189 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
190 void *my_private_data,
192 const auth_usersupplied_info *user_info,
193 auth_serversupplied_info **server_info)
195 struct smbcli_state *cli;
196 static uint8_t badpass[24];
197 static fstring baduser;
198 static BOOL tested_password_server = False;
199 static BOOL bad_password_server = False;
200 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
201 BOOL locally_made_cli = False;
204 * Check that the requested domain is not our own machine name.
205 * If it is, we should never check the PDC here, we use our own local
209 if(is_myname(user_info->domain.str)) {
210 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
211 return NT_STATUS_LOGON_FAILURE;
214 cli = my_private_data;
218 cli = server_cryptkey(mem_ctx);
219 locally_made_cli = True;
222 if (!cli || !cli->initialised) {
223 DEBUG(1,("password server is not connected (cli not initilised)\n"));
224 return NT_STATUS_LOGON_FAILURE;
227 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
228 if (user_info->encrypted) {
229 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
230 return NT_STATUS_LOGON_FAILURE;
233 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
234 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));
235 return NT_STATUS_LOGON_FAILURE;
240 memset(badpass, 0x1f, sizeof(badpass));
242 if((user_info->nt_resp.length == sizeof(badpass)) &&
243 !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
245 * Very unlikely, our random bad password is the same as the users
248 memset(badpass, badpass[0]+1, sizeof(badpass));
251 if(baduser[0] == 0) {
252 fstrcpy(baduser, INVALID_USER_PREFIX);
253 fstrcat(baduser, lp_netbios_name());
257 * Attempt a session setup with a totally incorrect password.
258 * If this succeeds with the guest bit *NOT* set then the password
259 * server is broken and is not correctly setting the guest bit. We
260 * need to detect this as some versions of NT4.x are broken. JRA.
263 /* I sure as hell hope that there aren't servers out there that take
264 * NTLMv2 and have this bug, as we don't test for that...
265 * - abartlet@samba.org
268 if ((!tested_password_server) && (lp_paranoid_server_security())) {
269 if (smbcli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass),
270 (char *)badpass, sizeof(badpass), user_info->domain.str)) {
273 * We connected to the password server so we
274 * can say we've tested it.
276 tested_password_server = True;
278 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
279 DEBUG(0,("server_validate: password server %s allows users as non-guest \
280 with a bad password.\n", cli->desthost));
281 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
282 use this machine as the password server.\n"));
286 * Password server has the bug.
288 bad_password_server = True;
289 return NT_STATUS_LOGON_FAILURE;
296 * We have already tested the password server.
297 * Fail immediately if it has the bug.
300 if(bad_password_server) {
301 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
302 with a bad password.\n", cli->desthost));
303 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
304 use this machine as the password server.\n"));
305 return NT_STATUS_LOGON_FAILURE;
310 * Now we know the password server will correctly set the guest bit, or is
311 * not guest enabled, we can try with the real password.
314 if (!user_info->encrypted) {
315 /* Plaintext available */
316 if (!smbcli_session_setup(cli, user_info->smb_name.str,
317 (char *)user_info->plaintext_password.data,
318 user_info->plaintext_password.length,
320 user_info->domain.str)) {
321 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
322 /* Make this smbcli_nt_error() when the conversion is in */
323 nt_status = smbcli_nt_error(cli);
325 nt_status = NT_STATUS_OK;
328 if (!smbcli_session_setup(cli, user_info->smb_name.str,
329 (char *)user_info->lm_resp.data,
330 user_info->lm_resp.length,
331 (char *)user_info->nt_resp.data,
332 user_info->nt_resp.length,
333 user_info->domain.str)) {
334 DEBUG(1,("password server %s rejected the password\n", cli->desthost));
335 /* Make this smbcli_nt_error() when the conversion is in */
336 nt_status = smbcli_nt_error(cli);
338 nt_status = NT_STATUS_OK;
342 /* if logged in as guest then reject */
343 if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
344 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
345 nt_status = NT_STATUS_LOGON_FAILURE;
350 if NT_STATUS_IS_OK(nt_status) {
351 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
353 nt_status = make_server_info_pw(auth_context, server_info, pass);
355 nt_status = NT_STATUS_NO_SUCH_USER;
359 if (locally_made_cli) {
366 NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
368 if (!make_auth_methods(auth_context, auth_method)) {
369 return NT_STATUS_NO_MEMORY;
371 (*auth_method)->name = "smbserver";
372 (*auth_method)->auth = check_smbserver_security;
373 (*auth_method)->get_chal = auth_get_challenge_server;
374 (*auth_method)->send_keepalive = send_server_keepalive;
375 (*auth_method)->free_private_data = free_server_private_data;