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,("making 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 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
200 client_domain, smb_name, wksta_name));
202 if (lp_allow_trusted_domains() && *client_domain) {
204 /* the client could have given us a workstation name
205 or other crap for the workgroup - we really need a
206 way of telling if this domain name is one of our
209 Also don't allow "" as a domain, fixes a Win9X bug
210 where it doens't supply a domain for logon script
213 The way I do it here is by checking if the fully
214 qualified username exists. This is rather reliant
215 on winbind, but until we have a better method this
219 domain = client_domain;
221 if ((smb_name) && (*smb_name)) { /* Don't do this for guests */
223 if (asprintf(&user, "%s%s%s",
224 client_domain, lp_winbind_separator(),
226 DEBUG(0, ("make_user_info_map: asprintf() failed!\n"));
230 DEBUG(5, ("make_user_info_map: testing for user %s\n", user));
232 if (Get_Pwnam(user) == NULL) {
233 DEBUG(5, ("make_user_info_map: test for user %s failed\n", user));
234 domain = lp_workgroup();
235 DEBUG(5, ("make_user_info_map: trusted domain %s doesn't appear to exist, using %s\n",
236 client_domain, domain));
238 DEBUG(5, ("make_user_info_map: using trusted domain %s\n", domain));
243 domain = lp_workgroup();
246 return make_user_info(user_info,
247 smb_name, internal_username,
248 client_domain, domain,
252 ntlmssp_flags, encrypted);
256 /****************************************************************************
257 Create an auth_usersupplied_data, making the DATA_BLOBs here.
258 Decrypt and encrypt the passwords.
259 ****************************************************************************/
261 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
262 const char *smb_name,
263 const char *client_domain,
264 const char *wksta_name,
265 const uchar *lm_network_pwd, int lm_pwd_len,
266 const uchar *nt_network_pwd, int nt_pwd_len)
269 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
270 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
271 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
272 uint32 auth_flags = AUTH_FLAG_NONE;
275 auth_flags |= AUTH_FLAG_LM_RESP;
276 if (nt_pwd_len == 24) {
277 auth_flags |= AUTH_FLAG_NTLM_RESP;
278 } else if (nt_pwd_len != 0) {
279 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
282 ret = make_user_info_map(user_info,
283 smb_name, client_domain,
289 data_blob_free(&lm_blob);
290 data_blob_free(&nt_blob);
294 /****************************************************************************
295 Create an auth_usersupplied_data, making the DATA_BLOBs here.
296 Decrypt and encrypt the passwords.
297 ****************************************************************************/
299 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
300 const char *smb_name,
301 const char *client_domain,
302 const char *wksta_name,
304 const uchar lm_interactive_pwd[16],
305 const uchar nt_interactive_pwd[16],
306 const uchar *dc_sess_key)
310 unsigned char local_lm_response[24];
311 unsigned char local_nt_response[24];
312 unsigned char key[16];
313 uint32 auth_flags = AUTH_FLAG_NONE;
316 memcpy(key, dc_sess_key, 8);
318 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
319 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
321 #ifdef DEBUG_PASSWORD
323 dump_data(100, (char *)key, sizeof(key));
325 DEBUG(100,("lm owf password:"));
326 dump_data(100, lm_pwd, sizeof(lm_pwd));
328 DEBUG(100,("nt owf password:"));
329 dump_data(100, nt_pwd, sizeof(nt_pwd));
332 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
333 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
335 #ifdef DEBUG_PASSWORD
336 DEBUG(100,("decrypt of lm owf password:"));
337 dump_data(100, lm_pwd, sizeof(lm_pwd));
339 DEBUG(100,("decrypt of nt owf password:"));
340 dump_data(100, nt_pwd, sizeof(nt_pwd));
343 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
344 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
346 /* Password info paranoia */
353 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
354 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
355 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
357 if (lm_interactive_pwd)
358 auth_flags |= AUTH_FLAG_LM_RESP;
359 if (nt_interactive_pwd)
360 auth_flags |= AUTH_FLAG_NTLM_RESP;
362 ret = make_user_info_map(user_info,
363 smb_name, client_domain,
370 data_blob_free(&local_lm_blob);
371 data_blob_free(&local_nt_blob);
377 /****************************************************************************
378 Create an auth_usersupplied_data structure
379 ****************************************************************************/
381 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
382 const char *smb_name,
383 const char *client_domain,
385 DATA_BLOB plaintext_password)
388 DATA_BLOB local_lm_blob;
389 DATA_BLOB local_nt_blob;
391 uint32 auth_flags = AUTH_FLAG_NONE;
394 * Not encrypted - do so.
397 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
399 if (plaintext_password.data) {
400 unsigned char local_lm_response[24];
402 #ifdef DEBUG_PASSWORD
403 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
404 dump_data(100, plaintext_password.data, plaintext_password.length);
407 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
408 local_lm_blob = data_blob(local_lm_response, 24);
410 /* We can't do an NT hash here, as the password needs to be
412 local_nt_blob = data_blob(NULL, 0);
414 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
416 local_lm_blob = data_blob(NULL, 0);
417 local_nt_blob = data_blob(NULL, 0);
420 ret = make_user_info_map(user_info, smb_name,
428 data_blob_free(&local_lm_blob);
432 /****************************************************************************
433 Create an auth_usersupplied_data structure
434 ****************************************************************************/
436 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
437 const char *smb_name,
438 const char *client_domain,
439 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
441 uint32 auth_flags = AUTH_FLAG_NONE;
443 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
445 if (lm_resp.length == 24) {
446 auth_flags |= AUTH_FLAG_LM_RESP;
448 if (nt_resp.length == 0) {
449 } else if (nt_resp.length == 24) {
450 auth_flags |= AUTH_FLAG_NTLM_RESP;
452 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
455 return make_user_info_map(user_info, smb_name,
464 /****************************************************************************
465 Create a guest user_info blob, for anonymous authenticaion.
466 ****************************************************************************/
468 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
470 DATA_BLOB lm_blob = data_blob(NULL, 0);
471 DATA_BLOB nt_blob = data_blob(NULL, 0);
472 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
473 uint32 auth_flags = AUTH_FLAG_NONE;
475 return make_user_info(user_info,
484 /***************************************************************************
485 Make a user_info struct
486 ***************************************************************************/
488 BOOL make_server_info(auth_serversupplied_info **server_info)
490 *server_info = malloc(sizeof(**server_info));
492 DEBUG(0,("make_server_info: malloc failed!\n"));
495 ZERO_STRUCTP(*server_info);
499 /***************************************************************************
500 Make (and fill) a user_info struct from a SAM_ACCOUNT
501 ***************************************************************************/
503 BOOL make_server_info_sam(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass)
505 if (!make_server_info(server_info)) {
509 (*server_info)->sam_fill_level = SAM_FILL_ALL;
510 (*server_info)->sam_account = sampass;
512 DEBUG(5,("make_server_info_sam: made server info for user %s\n",
513 pdb_get_username((*server_info)->sam_account)));
517 /***************************************************************************
518 Make (and fill) a user_info struct from a 'struct passwd' by conversion
520 ***************************************************************************/
522 BOOL make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
524 SAM_ACCOUNT *sampass = NULL;
525 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sampass, pwd))) {
528 return make_server_info_sam(server_info, sampass);
531 /***************************************************************************
532 Free a user_info struct
533 ***************************************************************************/
535 void free_user_info(auth_usersupplied_info **user_info)
537 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
538 if (*user_info != NULL) {
539 if ((*user_info)->smb_name.str) {
540 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
542 SAFE_FREE((*user_info)->smb_name.str);
543 SAFE_FREE((*user_info)->internal_username.str);
544 SAFE_FREE((*user_info)->client_domain.str);
545 SAFE_FREE((*user_info)->domain.str);
546 SAFE_FREE((*user_info)->wksta_name.str);
547 data_blob_free(&(*user_info)->lm_resp);
548 data_blob_free(&(*user_info)->nt_resp);
549 SAFE_FREE((*user_info)->interactive_password);
550 data_blob_clear_free(&(*user_info)->plaintext_password);
551 ZERO_STRUCT(**user_info);
553 SAFE_FREE(*user_info);
556 /***************************************************************************
557 Clear out a server_info struct that has been allocated
558 ***************************************************************************/
560 void free_server_info(auth_serversupplied_info **server_info)
562 if (*server_info != NULL) {
563 pdb_free_sam(&(*server_info)->sam_account);
565 /* call pam_end here, unless we know we are keeping it */
566 delete_nt_token( &(*server_info)->ptok );
567 ZERO_STRUCT(**server_info);
569 SAFE_FREE(*server_info);
572 /***************************************************************************
573 Make a server_info struct for a guest user
574 ***************************************************************************/
576 BOOL make_server_info_guest(auth_serversupplied_info **server_info)
578 struct passwd *pass = getpwnam_alloc(lp_guestaccount());
581 if (!make_server_info_pw(server_info, pass)) {
585 (*server_info)->guest = True;
589 DEBUG(0,("make_server_info_guest: getpwnam_alloc() failed on guest account!\n"));
593 /***************************************************************************
594 Make an auth_methods struct
595 ***************************************************************************/
597 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
600 smb_panic("no auth_context supplied to make_auth_methods()!\n");
604 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
607 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
609 DEBUG(0,("make_auth_method: malloc failed!\n"));
612 ZERO_STRUCTP(*auth_method);
617 /****************************************************************************
619 ****************************************************************************/
621 void delete_nt_token(NT_USER_TOKEN **pptoken)
624 NT_USER_TOKEN *ptoken = *pptoken;
625 SAFE_FREE( ptoken->user_sids );
626 ZERO_STRUCTP(ptoken);
631 /****************************************************************************
632 Duplicate a SID token.
633 ****************************************************************************/
635 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
637 NT_USER_TOKEN *token;
642 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
647 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
652 token->num_sids = ptoken->num_sids;
658 * Squash an NT_STATUS in line with security requirements.
659 * In an attempt to avoid giving the whole game away when users
660 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
661 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
662 * (session setups in particular).
664 * @param nt_status NTSTATUS input for squashing.
665 * @return the 'squashed' nt_status
668 NTSTATUS nt_status_squash(NTSTATUS nt_status)
670 if NT_STATUS_IS_OK(nt_status) {
672 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
673 /* Match WinXP and don't give the game away */
674 return NT_STATUS_LOGON_FAILURE;
676 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
677 /* Match WinXP and don't give the game away */
678 return NT_STATUS_LOGON_FAILURE;