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.
26 #define DBGC_CLASS DBGC_AUTH
28 extern fstring remote_machine;
29 extern pstring global_myname;
31 /****************************************************************************
32 Create a UNIX user on demand.
33 ****************************************************************************/
35 static int smb_create_user(const char *unix_user, const char *homedir)
40 pstrcpy(add_script, lp_adduser_script());
43 all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
45 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
46 ret = smbrun(add_script,NULL);
47 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
51 /****************************************************************************
52 Delete a UNIX user on demand.
53 ****************************************************************************/
55 int smb_delete_user(const char *unix_user)
60 pstrcpy(del_script, lp_deluser_script());
63 all_string_sub(del_script, "%u", unix_user, sizeof(pstring));
64 ret = smbrun(del_script,NULL);
65 DEBUG(3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
69 /****************************************************************************
70 Add and Delete UNIX users on demand, based on NTSTATUS codes.
71 ****************************************************************************/
73 void smb_user_control(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info, NTSTATUS nt_status)
75 struct passwd *pwd=NULL;
77 if (NT_STATUS_IS_OK(nt_status)) {
79 if (!(server_info->sam_fill_level & SAM_FILL_UNIX)) {
82 * User validated ok against Domain controller.
83 * If the admin wants us to try and create a UNIX
84 * user on the fly, do so.
87 if(lp_adduser_script() && !(pwd = Get_Pwnam(user_info->internal_username.str))) {
88 smb_create_user(user_info->internal_username.str, NULL);
91 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
93 * User failed to validate ok against Domain controller.
94 * If the failure was "user doesn't exist" and admin
95 * wants us to try and delete that UNIX user on the fly,
98 if (lp_deluser_script()) {
99 smb_delete_user(user_info->internal_username.str);
104 /****************************************************************************
105 Create an auth_usersupplied_data structure
106 ****************************************************************************/
108 static BOOL make_user_info(auth_usersupplied_info **user_info,
109 const char *smb_name,
110 const char *internal_username,
111 const char *client_domain,
113 const char *wksta_name,
114 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
116 uint32 auth_flags, BOOL encrypted)
119 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
121 *user_info = malloc(sizeof(**user_info));
123 DEBUG(0,("malloc failed for user_info (size %d)\n", sizeof(*user_info)));
127 ZERO_STRUCTP(*user_info);
129 DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
131 (*user_info)->smb_name.str = strdup(smb_name);
132 if ((*user_info)->smb_name.str) {
133 (*user_info)->smb_name.len = strlen(smb_name);
135 free_user_info(user_info);
139 (*user_info)->internal_username.str = strdup(internal_username);
140 if ((*user_info)->internal_username.str) {
141 (*user_info)->internal_username.len = strlen(internal_username);
143 free_user_info(user_info);
147 (*user_info)->domain.str = strdup(domain);
148 if ((*user_info)->domain.str) {
149 (*user_info)->domain.len = strlen(domain);
151 free_user_info(user_info);
155 (*user_info)->client_domain.str = strdup(client_domain);
156 if ((*user_info)->client_domain.str) {
157 (*user_info)->client_domain.len = strlen(client_domain);
159 free_user_info(user_info);
163 (*user_info)->wksta_name.str = strdup(wksta_name);
164 if ((*user_info)->wksta_name.str) {
165 (*user_info)->wksta_name.len = strlen(wksta_name);
167 free_user_info(user_info);
171 DEBUG(5,("makeing blobs for %s's user_info struct\n", internal_username));
173 (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
174 (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
175 (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
177 (*user_info)->encrypted = encrypted;
178 (*user_info)->auth_flags = auth_flags;
180 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
185 /****************************************************************************
186 Create an auth_usersupplied_data structure after appropriate mapping.
187 ****************************************************************************/
189 BOOL make_user_info_map(auth_usersupplied_info **user_info,
190 const char *smb_name,
191 const char *client_domain,
192 const char *wksta_name,
193 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
195 uint32 ntlmssp_flags, BOOL encrypted)
198 fstring internal_username;
199 fstrcpy(internal_username, smb_name);
200 map_username(internal_username);
202 DEBUG(5, ("make_user_info_map: Mapping user [%s]\\[%s] from workstation [%s]\n",
203 client_domain, smb_name, wksta_name));
205 if (lp_allow_trusted_domains() && *client_domain) {
207 /* the client could have given us a workstation name
208 or other crap for the workgroup - we really need a
209 way of telling if this domain name is one of our
212 Also don't allow "" as a domain, fixes a Win9X bug
213 where it doens't supply a domain for logon script
216 The way I do it here is by checking if the fully
217 qualified username exists. This is rather reliant
218 on winbind, but until we have a better method this
222 domain = client_domain;
224 if ((smb_name) && (*smb_name)) { /* Don't do this for guests */
226 if (asprintf(&user, "%s%s%s",
227 client_domain, lp_winbind_separator(),
229 DEBUG(0, ("make_user_info_map: asprintf() failed!\n"));
233 DEBUG(5, ("make_user_info_map: testing for user %s\n", user));
235 if (Get_Pwnam(user) == NULL) {
236 DEBUG(5, ("make_user_info_map: test for user %s failed\n", user));
237 domain = lp_workgroup();
238 DEBUG(5, ("make_user_info_map: trusted domain %s doesn't appear to exist, using %s\n",
239 client_domain, domain));
241 DEBUG(5, ("make_user_info_map: using trusted domain %s\n", domain));
246 domain = lp_workgroup();
249 return make_user_info(user_info,
250 smb_name, internal_username,
251 client_domain, domain,
255 ntlmssp_flags, encrypted);
259 /****************************************************************************
260 Create an auth_usersupplied_data, making the DATA_BLOBs here.
261 Decrypt and encrypt the passwords.
262 ****************************************************************************/
264 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
265 const char *smb_name,
266 const char *client_domain,
267 const char *wksta_name,
268 const uchar *lm_network_pwd, int lm_pwd_len,
269 const uchar *nt_network_pwd, int nt_pwd_len)
272 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
273 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
274 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
275 uint32 auth_flags = AUTH_FLAG_NONE;
278 auth_flags |= AUTH_FLAG_LM_RESP;
279 if (nt_pwd_len == 24) {
280 auth_flags |= AUTH_FLAG_NTLM_RESP;
281 } else if (nt_pwd_len != 0) {
282 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
285 ret = make_user_info_map(user_info,
286 smb_name, client_domain,
292 data_blob_free(&lm_blob);
293 data_blob_free(&nt_blob);
297 /****************************************************************************
298 Create an auth_usersupplied_data, making the DATA_BLOBs here.
299 Decrypt and encrypt the passwords.
300 ****************************************************************************/
302 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
303 const char *smb_name,
304 const char *client_domain,
305 const char *wksta_name,
307 const uchar lm_interactive_pwd[16],
308 const uchar nt_interactive_pwd[16],
309 const uchar *dc_sess_key)
313 unsigned char local_lm_response[24];
314 unsigned char local_nt_response[24];
315 unsigned char key[16];
316 uint32 auth_flags = AUTH_FLAG_NONE;
319 memcpy(key, dc_sess_key, 8);
321 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
322 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
324 #ifdef DEBUG_PASSWORD
326 dump_data(100, (char *)key, sizeof(key));
328 DEBUG(100,("lm owf password:"));
329 dump_data(100, lm_pwd, sizeof(lm_pwd));
331 DEBUG(100,("nt owf password:"));
332 dump_data(100, nt_pwd, sizeof(nt_pwd));
335 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
336 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
338 #ifdef DEBUG_PASSWORD
339 DEBUG(100,("decrypt of lm owf password:"));
340 dump_data(100, lm_pwd, sizeof(lm_pwd));
342 DEBUG(100,("decrypt of nt owf password:"));
343 dump_data(100, nt_pwd, sizeof(nt_pwd));
346 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
347 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
349 /* Password info paranoia */
356 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
357 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
358 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
360 if (lm_interactive_pwd)
361 auth_flags |= AUTH_FLAG_LM_RESP;
362 if (nt_interactive_pwd)
363 auth_flags |= AUTH_FLAG_NTLM_RESP;
365 ret = make_user_info_map(user_info,
366 smb_name, client_domain,
373 data_blob_free(&local_lm_blob);
374 data_blob_free(&local_nt_blob);
380 /****************************************************************************
381 Create an auth_usersupplied_data structure
382 ****************************************************************************/
384 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
385 const char *smb_name,
386 const char *client_domain,
388 DATA_BLOB plaintext_password)
391 DATA_BLOB local_lm_blob;
392 DATA_BLOB local_nt_blob;
394 uint32 auth_flags = AUTH_FLAG_NONE;
397 * Not encrypted - do so.
400 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
402 if (plaintext_password.data) {
403 unsigned char local_lm_response[24];
405 #ifdef DEBUG_PASSWORD
406 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
407 dump_data(100, plaintext_password.data, plaintext_password.length);
410 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
411 local_lm_blob = data_blob(local_lm_response, 24);
413 /* We can't do an NT hash here, as the password needs to be
415 local_nt_blob = data_blob(NULL, 0);
417 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
419 local_lm_blob = data_blob(NULL, 0);
420 local_nt_blob = data_blob(NULL, 0);
423 ret = make_user_info_map(user_info, smb_name,
431 data_blob_free(&local_lm_blob);
435 /****************************************************************************
436 Create an auth_usersupplied_data structure
437 ****************************************************************************/
439 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
440 const char *smb_name,
441 const char *client_domain,
442 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
444 uint32 auth_flags = AUTH_FLAG_NONE;
446 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
448 if (lm_resp.length == 24) {
449 auth_flags |= AUTH_FLAG_LM_RESP;
451 if (nt_resp.length == 0) {
452 } else if (nt_resp.length == 24) {
453 auth_flags |= AUTH_FLAG_NTLM_RESP;
455 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
458 return make_user_info_map(user_info, smb_name,
467 /****************************************************************************
468 Create a guest user_info blob, for anonymous authenticaion.
469 ****************************************************************************/
471 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
473 DATA_BLOB lm_blob = data_blob(NULL, 0);
474 DATA_BLOB nt_blob = data_blob(NULL, 0);
475 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
476 uint32 auth_flags = AUTH_FLAG_NONE;
478 return make_user_info(user_info,
487 /***************************************************************************
488 Make a user_info struct
489 ***************************************************************************/
491 BOOL make_server_info(auth_serversupplied_info **server_info)
493 *server_info = malloc(sizeof(**server_info));
495 DEBUG(0,("make_server_info: malloc failed!\n"));
498 ZERO_STRUCTP(*server_info);
502 /***************************************************************************
503 Make (and fill) a user_info struct from a SAM_ACCOUNT
504 ***************************************************************************/
506 BOOL make_server_info_sam(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass)
508 if (!make_server_info(server_info)) {
512 (*server_info)->sam_fill_level = SAM_FILL_ALL;
513 (*server_info)->sam_account = sampass;
515 DEBUG(5,("make_server_info_sam: made server info for user %s\n",
516 pdb_get_username((*server_info)->sam_account)));
520 /***************************************************************************
521 Make (and fill) a user_info struct from a 'struct passwd' by conversion
523 ***************************************************************************/
525 BOOL make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
527 SAM_ACCOUNT *sampass = NULL;
528 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sampass, pwd))) {
531 return make_server_info_sam(server_info, sampass);
534 /***************************************************************************
535 Free a user_info struct
536 ***************************************************************************/
538 void free_user_info(auth_usersupplied_info **user_info)
540 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
541 if (*user_info != NULL) {
542 if ((*user_info)->smb_name.str) {
543 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
545 SAFE_FREE((*user_info)->smb_name.str);
546 SAFE_FREE((*user_info)->internal_username.str);
547 SAFE_FREE((*user_info)->client_domain.str);
548 SAFE_FREE((*user_info)->domain.str);
549 SAFE_FREE((*user_info)->wksta_name.str);
550 data_blob_free(&(*user_info)->lm_resp);
551 data_blob_free(&(*user_info)->nt_resp);
552 SAFE_FREE((*user_info)->interactive_password);
553 data_blob_clear_free(&(*user_info)->plaintext_password);
554 ZERO_STRUCT(**user_info);
556 SAFE_FREE(*user_info);
559 /***************************************************************************
560 Clear out a server_info struct that has been allocated
561 ***************************************************************************/
563 void free_server_info(auth_serversupplied_info **server_info)
565 if (*server_info != NULL) {
566 pdb_free_sam(&(*server_info)->sam_account);
568 /* call pam_end here, unless we know we are keeping it */
569 delete_nt_token( &(*server_info)->ptok );
570 ZERO_STRUCT(**server_info);
572 SAFE_FREE(*server_info);
575 /***************************************************************************
576 Make a server_info struct for a guest user
577 ***************************************************************************/
579 BOOL make_server_info_guest(auth_serversupplied_info **server_info)
581 struct passwd *pass = getpwnam_alloc(lp_guestaccount());
584 if (!make_server_info_pw(server_info, pass)) {
588 (*server_info)->guest = True;
592 DEBUG(0,("make_server_info_guest: getpwnam_alloc() failed on guest account!\n"));
596 /***************************************************************************
597 Make an auth_methods struct
598 ***************************************************************************/
600 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
603 smb_panic("no auth_context supplied to make_auth_methods()!\n");
607 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
610 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
612 DEBUG(0,("make_auth_method: malloc failed!\n"));
615 ZERO_STRUCTP(*auth_method);
620 /****************************************************************************
622 ****************************************************************************/
624 void delete_nt_token(NT_USER_TOKEN **pptoken)
627 NT_USER_TOKEN *ptoken = *pptoken;
628 SAFE_FREE( ptoken->user_sids );
629 ZERO_STRUCTP(ptoken);
634 /****************************************************************************
635 Duplicate a SID token.
636 ****************************************************************************/
638 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
640 NT_USER_TOKEN *token;
645 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
650 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
655 token->num_sids = ptoken->num_sids;
661 * Squash an NT_STATUS in line with security requirements.
662 * In an attempt to avoid giving the whole game away when users
663 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
664 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
665 * (session setups in particular).
667 * @param nt_status NTSTATUS input for squashing.
668 * @return the 'squashed' nt_status
671 NTSTATUS nt_status_squash(NTSTATUS nt_status)
673 if NT_STATUS_IS_OK(nt_status) {
675 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
676 /* Match WinXP and don't give the game away */
677 return NT_STATUS_LOGON_FAILURE;
679 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
680 /* Match WinXP and don't give the game away */
681 return NT_STATUS_LOGON_FAILURE;