2 Unix SMB/Netbios implementation.
4 Authentication utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Andrew Bartlett 2001
7 Copyright (C) Jeremy Allison 2000-2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern fstring remote_machine;
27 extern pstring global_myname;
29 /****************************************************************************
30 Create a UNIX user on demand.
31 ****************************************************************************/
33 static int smb_create_user(const char *unix_user, const char *homedir)
38 pstrcpy(add_script, lp_adduser_script());
41 all_string_sub(add_script, "%u", unix_user, sizeof(pstring));
43 all_string_sub(add_script, "%H", homedir, sizeof(pstring));
44 ret = smbrun(add_script,NULL);
45 DEBUG(3,("smb_create_user: Running the command `%s' gave %d\n",add_script,ret));
49 /****************************************************************************
50 Delete a UNIX user on demand.
51 ****************************************************************************/
53 int smb_delete_user(const char *unix_user)
58 pstrcpy(del_script, lp_deluser_script());
61 all_string_sub(del_script, "%u", unix_user, sizeof(pstring));
62 ret = smbrun(del_script,NULL);
63 DEBUG(3,("smb_delete_user: Running the command `%s' gave %d\n",del_script,ret));
67 /****************************************************************************
68 Add and Delete UNIX users on demand, based on NTSTATUS codes.
69 ****************************************************************************/
71 void smb_user_control(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info, NTSTATUS nt_status)
73 struct passwd *pwd=NULL;
75 if (NT_STATUS_IS_OK(nt_status)) {
77 if (!(server_info->sam_fill_level & SAM_FILL_UNIX)) {
80 * User validated ok against Domain controller.
81 * If the admin wants us to try and create a UNIX
82 * user on the fly, do so.
85 if(lp_adduser_script() && !(pwd = Get_Pwnam(user_info->internal_username.str))) {
86 smb_create_user(user_info->internal_username.str, NULL);
89 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
91 * User failed to validate ok against Domain controller.
92 * If the failure was "user doesn't exist" and admin
93 * wants us to try and delete that UNIX user on the fly,
96 if (lp_deluser_script()) {
97 smb_delete_user(user_info->internal_username.str);
102 /****************************************************************************
103 Create an auth_usersupplied_data structure
104 ****************************************************************************/
106 static BOOL make_user_info(auth_usersupplied_info **user_info,
107 const char *smb_name,
108 const char *internal_username,
109 const char *client_domain,
111 const char *wksta_name,
112 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
114 uint32 auth_flags, BOOL encrypted)
117 DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
119 *user_info = malloc(sizeof(**user_info));
121 DEBUG(0,("malloc failed for user_info (size %d)\n", sizeof(*user_info)));
125 ZERO_STRUCTP(*user_info);
127 DEBUG(5,("makeing strings for %s's user_info struct\n", internal_username));
129 (*user_info)->smb_name.str = strdup(smb_name);
130 if ((*user_info)->smb_name.str) {
131 (*user_info)->smb_name.len = strlen(smb_name);
133 free_user_info(user_info);
137 (*user_info)->internal_username.str = strdup(internal_username);
138 if ((*user_info)->internal_username.str) {
139 (*user_info)->internal_username.len = strlen(internal_username);
141 free_user_info(user_info);
145 (*user_info)->domain.str = strdup(domain);
146 if ((*user_info)->domain.str) {
147 (*user_info)->domain.len = strlen(domain);
149 free_user_info(user_info);
153 (*user_info)->client_domain.str = strdup(client_domain);
154 if ((*user_info)->client_domain.str) {
155 (*user_info)->client_domain.len = strlen(client_domain);
157 free_user_info(user_info);
161 (*user_info)->wksta_name.str = strdup(wksta_name);
162 if ((*user_info)->wksta_name.str) {
163 (*user_info)->wksta_name.len = strlen(wksta_name);
165 free_user_info(user_info);
169 DEBUG(5,("makeing blobs for %s's user_info struct\n", internal_username));
171 (*user_info)->lm_resp = data_blob(lm_pwd.data, lm_pwd.length);
172 (*user_info)->nt_resp = data_blob(nt_pwd.data, nt_pwd.length);
173 (*user_info)->plaintext_password = data_blob(plaintext.data, plaintext.length);
175 (*user_info)->encrypted = encrypted;
176 (*user_info)->auth_flags = auth_flags;
178 DEBUG(10,("made an %sencrypted user_info for %s (%s)\n", encrypted ? "":"un" , internal_username, smb_name));
183 /****************************************************************************
184 Create an auth_usersupplied_data structure after appropriate mapping.
185 ****************************************************************************/
187 BOOL make_user_info_map(auth_usersupplied_info **user_info,
188 const char *smb_name,
189 const char *client_domain,
190 const char *wksta_name,
191 DATA_BLOB lm_pwd, DATA_BLOB nt_pwd,
193 uint32 ntlmssp_flags, BOOL encrypted)
196 fstring internal_username;
197 fstrcpy(internal_username, smb_name);
198 map_username(internal_username);
200 if (lp_allow_trusted_domains()) {
202 /* the client could have given us a workstation name
203 or other crap for the workgroup - we really need a
204 way of telling if this domain name is one of our
207 The way I do it here is by checking if the fully
208 qualified username exists. This is rather reliant
209 on winbind, but until we have a better method this
212 asprintf(&user, "%s%s%s",
213 client_domain, lp_winbind_separator(),
215 if (Get_Pwnam(user) != NULL) {
216 domain = client_domain;
218 domain = lp_workgroup();
222 domain = lp_workgroup();
225 return make_user_info(user_info,
226 smb_name, internal_username,
227 client_domain, domain,
231 ntlmssp_flags, encrypted);
235 /****************************************************************************
236 Create an auth_usersupplied_data, making the DATA_BLOBs here.
237 Decrypt and encrypt the passwords.
238 ****************************************************************************/
240 BOOL make_user_info_netlogon_network(auth_usersupplied_info **user_info,
244 uchar *lm_network_pwd, int lm_pwd_len,
245 uchar *nt_network_pwd, int nt_pwd_len)
248 DATA_BLOB lm_blob = data_blob(lm_network_pwd, lm_pwd_len);
249 DATA_BLOB nt_blob = data_blob(nt_network_pwd, nt_pwd_len);
250 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
251 uint32 auth_flags = AUTH_FLAG_NONE;
254 auth_flags |= AUTH_FLAG_LM_RESP;
255 if (nt_pwd_len == 24) {
256 auth_flags |= AUTH_FLAG_NTLM_RESP;
257 } else if (nt_pwd_len != 0) {
258 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
261 ret = make_user_info_map(user_info,
262 smb_name, client_domain,
268 data_blob_free(&lm_blob);
269 data_blob_free(&nt_blob);
273 /****************************************************************************
274 Create an auth_usersupplied_data, making the DATA_BLOBs here.
275 Decrypt and encrypt the passwords.
276 ****************************************************************************/
278 BOOL make_user_info_netlogon_interactive(auth_usersupplied_info **user_info,
279 const char *smb_name,
280 const char *client_domain,
281 const char *wksta_name,
283 const uchar lm_interactive_pwd[16],
284 const uchar nt_interactive_pwd[16],
285 const uchar *dc_sess_key)
289 unsigned char local_lm_response[24];
290 unsigned char local_nt_response[24];
291 unsigned char key[16];
292 uint32 auth_flags = AUTH_FLAG_NONE;
295 memcpy(key, dc_sess_key, 8);
297 if (lm_interactive_pwd) memcpy(lm_pwd, lm_interactive_pwd, sizeof(lm_pwd));
298 if (nt_interactive_pwd) memcpy(nt_pwd, nt_interactive_pwd, sizeof(nt_pwd));
300 #ifdef DEBUG_PASSWORD
302 dump_data(100, (char *)key, sizeof(key));
304 DEBUG(100,("lm owf password:"));
305 dump_data(100, lm_pwd, sizeof(lm_pwd));
307 DEBUG(100,("nt owf password:"));
308 dump_data(100, nt_pwd, sizeof(nt_pwd));
311 SamOEMhash((uchar *)lm_pwd, key, sizeof(lm_pwd));
312 SamOEMhash((uchar *)nt_pwd, key, sizeof(nt_pwd));
314 #ifdef DEBUG_PASSWORD
315 DEBUG(100,("decrypt of lm owf password:"));
316 dump_data(100, lm_pwd, sizeof(lm_pwd));
318 DEBUG(100,("decrypt of nt owf password:"));
319 dump_data(100, nt_pwd, sizeof(nt_pwd));
322 SMBOWFencrypt((const unsigned char *)lm_pwd, chal, local_lm_response);
323 SMBOWFencrypt((const unsigned char *)nt_pwd, chal, local_nt_response);
325 /* Password info parinoia */
332 DATA_BLOB local_lm_blob = data_blob(local_lm_response, sizeof(local_lm_response));
333 DATA_BLOB local_nt_blob = data_blob(local_nt_response, sizeof(local_nt_response));
334 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
336 if (lm_interactive_pwd)
337 auth_flags |= AUTH_FLAG_LM_RESP;
338 if (nt_interactive_pwd)
339 auth_flags |= AUTH_FLAG_NTLM_RESP;
341 ret = make_user_info_map(user_info,
342 smb_name, client_domain,
349 data_blob_free(&local_lm_blob);
350 data_blob_free(&local_nt_blob);
356 /****************************************************************************
357 Create an auth_usersupplied_data structure
358 ****************************************************************************/
360 BOOL make_user_info_for_reply(auth_usersupplied_info **user_info,
364 DATA_BLOB plaintext_password)
367 DATA_BLOB local_lm_blob;
368 DATA_BLOB local_nt_blob;
370 uint32 auth_flags = AUTH_FLAG_NONE;
373 * Not encrypted - do so.
376 DEBUG(5,("make_user_info_for_reply: User passwords not in encrypted format.\n"));
378 if (plaintext_password.data) {
379 unsigned char local_lm_response[24];
381 #ifdef DEBUG_PASSWORD
382 DEBUG(10,("Unencrypted password (len %d):\n",plaintext_password.length));
383 dump_data(100, plaintext_password.data, plaintext_password.length);
386 SMBencrypt( (const uchar *)plaintext_password.data, (const uchar*)chal, local_lm_response);
387 local_lm_blob = data_blob(local_lm_response, 24);
389 /* We can't do an NT hash here, as the password needs to be
391 local_nt_blob = data_blob(NULL, 0);
393 auth_flags = (AUTH_FLAG_PLAINTEXT | AUTH_FLAG_LM_RESP);
395 local_lm_blob = data_blob(NULL, 0);
396 local_nt_blob = data_blob(NULL, 0);
399 ret = make_user_info_map(user_info, smb_name,
407 data_blob_free(&local_lm_blob);
411 /****************************************************************************
412 Create an auth_usersupplied_data structure
413 ****************************************************************************/
415 BOOL make_user_info_for_reply_enc(auth_usersupplied_info **user_info,
418 DATA_BLOB lm_resp, DATA_BLOB nt_resp)
420 uint32 auth_flags = AUTH_FLAG_NONE;
422 DATA_BLOB no_plaintext_blob = data_blob(NULL, 0);
424 if (lm_resp.length == 24) {
425 auth_flags |= AUTH_FLAG_LM_RESP;
427 if (nt_resp.length == 0) {
428 } else if (nt_resp.length == 24) {
429 auth_flags |= AUTH_FLAG_NTLM_RESP;
431 auth_flags |= AUTH_FLAG_NTLMv2_RESP;
434 return make_user_info_map(user_info, smb_name,
443 /****************************************************************************
444 Create a guest user_info blob, for anonymous authenticaion.
445 ****************************************************************************/
447 BOOL make_user_info_guest(auth_usersupplied_info **user_info)
449 DATA_BLOB lm_blob = data_blob(NULL, 0);
450 DATA_BLOB nt_blob = data_blob(NULL, 0);
451 DATA_BLOB plaintext_blob = data_blob(NULL, 0);
452 uint32 auth_flags = AUTH_FLAG_NONE;
454 return make_user_info(user_info,
463 /***************************************************************************
464 Make a user_info struct
465 ***************************************************************************/
467 BOOL make_server_info(auth_serversupplied_info **server_info)
469 *server_info = malloc(sizeof(**server_info));
471 DEBUG(0,("make_server_info: malloc failed!\n"));
474 ZERO_STRUCTP(*server_info);
478 /***************************************************************************
479 Make (and fill) a user_info struct from a SAM_ACCOUNT
480 ***************************************************************************/
482 BOOL make_server_info_sam(auth_serversupplied_info **server_info, SAM_ACCOUNT *sampass)
484 if (!make_server_info(server_info)) {
488 (*server_info)->sam_fill_level = SAM_FILL_ALL;
489 (*server_info)->sam_account = sampass;
491 DEBUG(5,("make_server_info_sam: made server info for user %s\n",
492 pdb_get_username((*server_info)->sam_account)));
496 /***************************************************************************
497 Make (and fill) a user_info struct from a 'struct passwd' by conversion
499 ***************************************************************************/
501 BOOL make_server_info_pw(auth_serversupplied_info **server_info, const struct passwd *pwd)
503 SAM_ACCOUNT *sampass = NULL;
504 if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sampass, pwd))) {
507 return make_server_info_sam(server_info, sampass);
510 /***************************************************************************
511 Free a user_info struct
512 ***************************************************************************/
514 void free_user_info(auth_usersupplied_info **user_info)
516 DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
517 if (*user_info != NULL) {
518 if ((*user_info)->smb_name.str) {
519 DEBUG(10,("structure was created for %s\n", (*user_info)->smb_name.str));
521 SAFE_FREE((*user_info)->smb_name.str);
522 SAFE_FREE((*user_info)->internal_username.str);
523 SAFE_FREE((*user_info)->client_domain.str);
524 SAFE_FREE((*user_info)->domain.str);
525 SAFE_FREE((*user_info)->wksta_name.str);
526 data_blob_free(&(*user_info)->lm_resp);
527 data_blob_free(&(*user_info)->nt_resp);
528 SAFE_FREE((*user_info)->interactive_password);
529 data_blob_clear_free(&(*user_info)->plaintext_password);
530 ZERO_STRUCT(**user_info);
532 SAFE_FREE(*user_info);
535 /***************************************************************************
536 Clear out a server_info struct that has been allocated
537 ***************************************************************************/
539 void free_server_info(auth_serversupplied_info **server_info)
541 if (*server_info != NULL) {
542 pdb_free_sam(&(*server_info)->sam_account);
544 /* call pam_end here, unless we know we are keeping it */
545 delete_nt_token( &(*server_info)->ptok );
546 ZERO_STRUCT(**server_info);
548 SAFE_FREE(*server_info);
551 /***************************************************************************
552 Make a server_info struct for a guest user
553 ***************************************************************************/
555 BOOL make_server_info_guest(auth_serversupplied_info **server_info)
557 struct passwd *pass = sys_getpwnam(lp_guestaccount());
560 if (!make_server_info_pw(server_info, pass)) {
563 (*server_info)->guest = True;
566 DEBUG(0,("make_server_info_guest: sys_getpwnam() failed on guest account!\n"));
570 /***************************************************************************
571 Make an auth_methods struct
572 ***************************************************************************/
574 BOOL make_auth_methods(struct auth_context *auth_context, auth_methods **auth_method)
577 smb_panic("no auth_context supplied to make_auth_methods()!\n");
581 smb_panic("make_auth_methods: pointer to auth_method pointer is NULL!\n");
584 *auth_method = talloc(auth_context->mem_ctx, sizeof(**auth_method));
586 DEBUG(0,("make_auth_method: malloc failed!\n"));
589 ZERO_STRUCTP(*auth_method);
594 /****************************************************************************
596 ****************************************************************************/
598 void delete_nt_token(NT_USER_TOKEN **pptoken)
601 NT_USER_TOKEN *ptoken = *pptoken;
602 SAFE_FREE( ptoken->user_sids );
603 ZERO_STRUCTP(ptoken);
608 /****************************************************************************
609 Duplicate a SID token.
610 ****************************************************************************/
612 NT_USER_TOKEN *dup_nt_token(NT_USER_TOKEN *ptoken)
614 NT_USER_TOKEN *token;
619 if ((token = (NT_USER_TOKEN *)malloc( sizeof(NT_USER_TOKEN) ) ) == NULL)
624 if ((token->user_sids = (DOM_SID *)memdup( ptoken->user_sids, sizeof(DOM_SID) * ptoken->num_sids )) == NULL) {
629 token->num_sids = ptoken->num_sids;
635 * Squash an NT_STATUS in line with security requirements.
636 * In an attempt to avoid giving the whole game away when users
637 * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and
638 * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations
639 * (session setups in particular).
641 * @param nt_status NTSTATUS input for squashing.
642 * @return the 'squashed' nt_status
645 NTSTATUS nt_status_squash(NTSTATUS nt_status)
647 if NT_STATUS_IS_OK(nt_status) {
649 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
650 /* Match WinXP and don't give the game away */
651 return NT_STATUS_LOGON_FAILURE;
653 } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
654 /* Match WinXP and don't give the game away */
655 return NT_STATUS_LOGON_FAILURE;