2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
6 Copyright (C) Jeremy Allison 2000-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 fstring remote_machine;
26 extern pstring global_myname;
28 /****************************************************************************
29 Create a UNIX user on demand.
30 ****************************************************************************/
32 static int smb_create_user(const char *unix_user, const char *homedir)
37 pstrcpy(add_script, lp_adduser_script());
40 all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
42 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
43 ret = smbrun(add_script,NULL);
44 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
48 /****************************************************************************
49 Delete a UNIX user on demand.
50 ****************************************************************************/
52 int smb_delete_user(const char *unix_user)
57 pstrcpy(del_script, lp_deluser_script());
60 all_string_sub(del_script, "%u", unix_user, sizeof(pstring));
61 ret = smbrun(del_script,NULL);
62 DEBUG(3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
66 /****************************************************************************
67 Add and Delete UNIX users on demand, based on NTSTATUS codes.
68 ****************************************************************************/
70 void smb_user_control(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info, NTSTATUS nt_status)
72 struct passwd *pwd=NULL;
74 if (NT_STATUS_IS_OK(nt_status)) {
76 if (!(server_info->sam_fill_level & SAM_FILL_UNIX)) {
79 * User validated ok against Domain controller.
80 * If the admin wants us to try and create a UNIX
81 * user on the fly, do so.
84 if(lp_adduser_script() && !(pwd = Get_Pwnam(user_info->internal_username.str))) {
85 smb_create_user(user_info->internal_username.str, NULL);
88 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
90 * User failed to validate ok against Domain controller.
91 * If the failure was "user doesn't exist" and admin
92 * wants us to try and delete that UNIX user on the fly,
95 if (lp_deluser_script()) {
96 smb_delete_user(user_info->internal_username.str);
101 /****************************************************************************
102 Create an auth_usersupplied_data structure
103 ****************************************************************************/
105 static BOOL make_user_info(auth_usersupplied_info **user_info,
106 const char *smb_name,
107 const char *internal_username,
108 const char *client_domain,
110 const char *wksta_name,
111 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
113 uint32 auth_flags, BOOL encrypted)
116 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
118 *user_info = malloc(sizeof(**user_info));
120 DEBUG(0,("malloc failed for user_info (size %d)\n", sizeof(*user_info)));
124 ZERO_STRUCTP(*user_info);
126 DEBUG(5,("makeing strings for %s's user_info struct\n", internal_username));
128 (*user_info)->smb_name.str = strdup(smb_name);
129 if ((*user_info)->smb_name.str) {
130 (*user_info)->smb_name.len = strlen(smb_name);
132 free_user_info(user_info);
136 (*user_info)->internal_username.str = strdup(internal_username);
137 if ((*user_info)->internal_username.str) {
138 (*user_info)->internal_username.len = strlen(internal_username);
140 free_user_info(user_info);
144 (*user_info)->domain.str = strdup(domain);
145 if ((*user_info)->domain.str) {
146 (*user_info)->domain.len = strlen(domain);
148 free_user_info(user_info);
152 (*user_info)->client_domain.str = strdup(client_domain);
153 if ((*user_info)->client_domain.str) {
154 (*user_info)->client_domain.len = strlen(client_domain);
156 free_user_info(user_info);
160 (*user_info)->wksta_name.str = strdup(wksta_name);
161 if ((*user_info)->wksta_name.str) {
162 (*user_info)->wksta_name.len = strlen(wksta_name);
164 free_user_info(user_info);
168 DEBUG(5,("makeing blobs for %s's user_info struct\n", internal_username));
170 (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
171 (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
172 (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
174 (*user_info)->encrypted = encrypted;
175 (*user_info)->auth_flags = auth_flags;
177 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
182 /****************************************************************************
183 Create an auth_usersupplied_data structure after appropriate mapping.
184 ****************************************************************************/
186 BOOL make_user_info_map(auth_usersupplied_info **user_info,
187 const char *smb_name,
188 const char *client_domain,
189 const char *wksta_name,
190 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
192 uint32 ntlmssp_flags, BOOL encrypted)
195 fstring internal_username;
196 fstrcpy(internal_username, smb_name);
197 map_username(internal_username);
199 if (lp_allow_trusted_domains()) {
200 /* the client could have given us a workstation name
201 or other crap for the workgroup - we really need a
202 way of telling if this domain name is one of our
205 The way I do it here is by checking if the fully
206 qualified username exists. This is rather reliant
207 on winbind, but until we have a better method this
211 domain = client_domain;
213 if ((smb_name) && (*smb_name)) { /* Don't do this for guests */
215 asprintf(&user, "%s%s%s",
216 client_domain, lp_winbind_separator(),
218 if (Get_Pwnam(user) == NULL) {
219 domain = lp_workgroup();
224 domain = lp_workgroup();
227 return make_user_info(user_info,
228 smb_name, internal_username,
229 client_domain, domain,
233 ntlmssp_flags, encrypted);
237 /****************************************************************************
238 Create an auth_usersupplied_data, making the DATA_BLOBs here.
239 Decrypt and encrypt the passwords.
240 ****************************************************************************/
242 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
243 const char *smb_name,
244 const char *client_domain,
245 const char *wksta_name,
246 const uchar *lm_network_pwd, int lm_pwd_len,
247 const uchar *nt_network_pwd, int nt_pwd_len)
250 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
251 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
252 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
253 uint32 auth_flags = AUTH_FLAG_NONE;
256 auth_flags |= AUTH_FLAG_LM_RESP;
257 if (nt_pwd_len == 24) {
258 auth_flags |= AUTH_FLAG_NTLM_RESP;
259 } else if (nt_pwd_len != 0) {
260 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
263 ret = make_user_info_map(user_info,
264 smb_name, client_domain,
270 data_blob_free(&lm_blob);
271 data_blob_free(&nt_blob);
275 /****************************************************************************
276 Create an auth_usersupplied_data, making the DATA_BLOBs here.
277 Decrypt and encrypt the passwords.
278 ****************************************************************************/
280 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
281 const char *smb_name,
282 const char *client_domain,
283 const char *wksta_name,
285 const uchar lm_interactive_pwd[16],
286 const uchar nt_interactive_pwd[16],
287 const uchar *dc_sess_key)
291 unsigned char local_lm_response[24];
292 unsigned char local_nt_response[24];
293 unsigned char key[16];
294 uint32 auth_flags = AUTH_FLAG_NONE;
297 memcpy(key, dc_sess_key, 8);
299 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
300 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
302 #ifdef DEBUG_PASSWORD
304 dump_data(100, (char *)key, sizeof(key));
306 DEBUG(100,("lm owf password:"));
307 dump_data(100, lm_pwd, sizeof(lm_pwd));
309 DEBUG(100,("nt owf password:"));
310 dump_data(100, nt_pwd, sizeof(nt_pwd));
313 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
314 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
316 #ifdef DEBUG_PASSWORD
317 DEBUG(100,("decrypt of lm owf password:"));
318 dump_data(100, lm_pwd, sizeof(lm_pwd));
320 DEBUG(100,("decrypt of nt owf password:"));
321 dump_data(100, nt_pwd, sizeof(nt_pwd));
324 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
325 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
327 /* Password info parinoia */
334 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
335 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
336 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
338 if (lm_interactive_pwd)
339 auth_flags |= AUTH_FLAG_LM_RESP;
340 if (nt_interactive_pwd)
341 auth_flags |= AUTH_FLAG_NTLM_RESP;
343 ret = make_user_info_map(user_info,
344 smb_name, client_domain,
351 data_blob_free(&local_lm_blob);
352 data_blob_free(&local_nt_blob);
358 /****************************************************************************
359 Create an auth_usersupplied_data structure
360 ****************************************************************************/
362 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
363 const char *smb_name,
364 const char *client_domain,
366 DATA_BLOB plaintext_password)
369 DATA_BLOB local_lm_blob;
370 DATA_BLOB local_nt_blob;
372 uint32 auth_flags = AUTH_FLAG_NONE;
375 * Not encrypted - do so.
378 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
380 if (plaintext_password.data) {
381 unsigned char local_lm_response[24];
383 #ifdef DEBUG_PASSWORD
384 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
385 dump_data(100, plaintext_password.data, plaintext_password.length);
388 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
389 local_lm_blob = data_blob(local_lm_response, 24);
391 /* We can't do an NT hash here, as the password needs to be
393 local_nt_blob = data_blob(NULL, 0);
395 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
397 local_lm_blob = data_blob(NULL, 0);
398 local_nt_blob = data_blob(NULL, 0);
401 ret = make_user_info_map(user_info, smb_name,
409 data_blob_free(&local_lm_blob);
413 /****************************************************************************
414 Create an auth_usersupplied_data structure
415 ****************************************************************************/
417 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
418 const char *smb_name,
419 const char *client_domain,
420 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
422 uint32 auth_flags = AUTH_FLAG_NONE;
424 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
426 if (lm_resp.length == 24) {
427 auth_flags |= AUTH_FLAG_LM_RESP;
429 if (nt_resp.length == 0) {
430 } else if (nt_resp.length == 24) {
431 auth_flags |= AUTH_FLAG_NTLM_RESP;
433 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
436 return make_user_info_map(user_info, smb_name,
445 /****************************************************************************
446 Create a guest user_info blob, for anonymous authenticaion.
447 ****************************************************************************/
449 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
451 DATA_BLOB lm_blob = data_blob(NULL, 0);
452 DATA_BLOB nt_blob = data_blob(NULL, 0);
453 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
454 uint32 auth_flags = AUTH_FLAG_NONE;
456 return make_user_info(user_info,
465 /***************************************************************************
466 Make a user_info struct
467 ***************************************************************************/
469 BOOL make_server_info(auth_serversupplied_info **server_info)
471 *server_info = malloc(sizeof(**server_info));
473 DEBUG(0,("make_server_info: malloc failed!\n"));
476 ZERO_STRUCTP(*server_info);
480 /***************************************************************************
481 Make (and fill) a user_info struct from a SAM_ACCOUNT
482 ***************************************************************************/
484 BOOL make_server_info_sam(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass)
486 if (!make_server_info(server_info)) {
490 (*server_info)->sam_fill_level = SAM_FILL_ALL;
491 (*server_info)->sam_account = sampass;
493 DEBUG(5,("make_server_info_sam: made server info for user %s\n",
494 pdb_get_username((*server_info)->sam_account)));
498 /***************************************************************************
499 Make (and fill) a user_info struct from a 'struct passwd' by conversion
501 ***************************************************************************/
503 BOOL make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
505 SAM_ACCOUNT *sampass = NULL;
506 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sampass, pwd))) {
509 return make_server_info_sam(server_info, sampass);
512 /***************************************************************************
513 Free a user_info struct
514 ***************************************************************************/
516 void free_user_info(auth_usersupplied_info **user_info)
518 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
519 if (*user_info != NULL) {
520 if ((*user_info)->smb_name.str) {
521 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
523 SAFE_FREE((*user_info)->smb_name.str);
524 SAFE_FREE((*user_info)->internal_username.str);
525 SAFE_FREE((*user_info)->client_domain.str);
526 SAFE_FREE((*user_info)->domain.str);
527 SAFE_FREE((*user_info)->wksta_name.str);
528 data_blob_free(&(*user_info)->lm_resp);
529 data_blob_free(&(*user_info)->nt_resp);
530 SAFE_FREE((*user_info)->interactive_password);
531 data_blob_clear_free(&(*user_info)->plaintext_password);
532 ZERO_STRUCT(**user_info);
534 SAFE_FREE(*user_info);
537 /***************************************************************************
538 Clear out a server_info struct that has been allocated
539 ***************************************************************************/
541 void free_server_info(auth_serversupplied_info **server_info)
543 if (*server_info != NULL) {
544 pdb_free_sam(&(*server_info)->sam_account);
546 /* call pam_end here, unless we know we are keeping it */
547 delete_nt_token( &(*server_info)->ptok );
548 ZERO_STRUCT(**server_info);
550 SAFE_FREE(*server_info);
553 /***************************************************************************
554 Make a server_info struct for a guest user
555 ***************************************************************************/
557 BOOL make_server_info_guest(auth_serversupplied_info **server_info)
559 struct passwd *pass = getpwnam_alloc(lp_guestaccount());
562 if (!make_server_info_pw(server_info, pass)) {
566 (*server_info)->guest = True;
570 DEBUG(0,("make_server_info_guest: getpwnam_alloc() failed on guest account!\n"));
574 /***************************************************************************
575 Make an auth_methods struct
576 ***************************************************************************/
578 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
581 smb_panic("no auth_context supplied to make_auth_methods()!\n");
585 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
588 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
590 DEBUG(0,("make_auth_method: malloc failed!\n"));
593 ZERO_STRUCTP(*auth_method);
598 /****************************************************************************
600 ****************************************************************************/
602 void delete_nt_token(NT_USER_TOKEN **pptoken)
605 NT_USER_TOKEN *ptoken = *pptoken;
606 SAFE_FREE( ptoken->user_sids );
607 ZERO_STRUCTP(ptoken);
612 /****************************************************************************
613 Duplicate a SID token.
614 ****************************************************************************/
616 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
618 NT_USER_TOKEN *token;
623 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
628 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
633 token->num_sids = ptoken->num_sids;
639 * Squash an NT_STATUS in line with security requirements.
640 * In an attempt to avoid giving the whole game away when users
641 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
642 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
643 * (session setups in particular).
645 * @param nt_status NTSTATUS input for squashing.
646 * @return the 'squashed' nt_status
649 NTSTATUS nt_status_squash(NTSTATUS nt_status)
651 if NT_STATUS_IS_OK(nt_status) {
653 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
654 /* Match WinXP and don't give the game away */
655 return NT_STATUS_LOGON_FAILURE;
657 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
658 /* Match WinXP and don't give the game away */
659 return NT_STATUS_LOGON_FAILURE;