| 1 | /*
 | 
|---|
| 2 |    Unix SMB/CIFS implementation.
 | 
|---|
| 3 |    Authentication utility functions
 | 
|---|
| 4 |    Copyright (C) Volker Lendecke 2010
 | 
|---|
| 5 | 
 | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify
 | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by
 | 
|---|
| 8 |    the Free Software Foundation; either version 3 of the License, or
 | 
|---|
| 9 |    (at your option) any later version.
 | 
|---|
| 10 | 
 | 
|---|
| 11 |    This program is distributed in the hope that it will be useful,
 | 
|---|
| 12 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 13 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 14 |    GNU General Public License for more details.
 | 
|---|
| 15 | 
 | 
|---|
| 16 |    You should have received a copy of the GNU General Public License
 | 
|---|
| 17 |    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 18 | */
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "includes.h"
 | 
|---|
| 21 | #include "auth.h"
 | 
|---|
| 22 | #include "librpc/gen_ndr/samr.h"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #undef DBGC_CLASS
 | 
|---|
| 25 | #define DBGC_CLASS DBGC_AUTH
 | 
|---|
| 26 | 
 | 
|---|
| 27 | static int clear_samr_Password(struct samr_Password *password)
 | 
|---|
| 28 | {
 | 
|---|
| 29 |         memset(password->hash, '\0', sizeof(password->hash));
 | 
|---|
| 30 |         return 0;
 | 
|---|
| 31 | }
 | 
|---|
| 32 | 
 | 
|---|
| 33 | static int clear_string(char *password)
 | 
|---|
| 34 | {
 | 
|---|
| 35 |         memset(password, '\0', strlen(password));
 | 
|---|
| 36 |         return 0;
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | /****************************************************************************
 | 
|---|
| 40 |  Create an auth_usersupplied_data structure
 | 
|---|
| 41 | ****************************************************************************/
 | 
|---|
| 42 | 
 | 
|---|
| 43 | NTSTATUS make_user_info(struct auth_usersupplied_info **ret_user_info,
 | 
|---|
| 44 |                         const char *smb_name,
 | 
|---|
| 45 |                         const char *internal_username,
 | 
|---|
| 46 |                         const char *client_domain,
 | 
|---|
| 47 |                         const char *domain,
 | 
|---|
| 48 |                         const char *workstation_name,
 | 
|---|
| 49 |                         const DATA_BLOB *lm_pwd,
 | 
|---|
| 50 |                         const DATA_BLOB *nt_pwd,
 | 
|---|
| 51 |                         const struct samr_Password *lm_interactive_pwd,
 | 
|---|
| 52 |                         const struct samr_Password *nt_interactive_pwd,
 | 
|---|
| 53 |                         const char *plaintext_password,
 | 
|---|
| 54 |                         enum auth_password_state password_state)
 | 
|---|
| 55 | {
 | 
|---|
| 56 |         struct auth_usersupplied_info *user_info;
 | 
|---|
| 57 |         *ret_user_info = NULL;
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         /* FIXME: Have the caller provide a talloc context of the
 | 
|---|
| 62 |          * correct lifetime (possibly talloc_tos(), but it depends on
 | 
|---|
| 63 |          * the caller) */
 | 
|---|
| 64 |         user_info = talloc_zero(NULL, struct auth_usersupplied_info);
 | 
|---|
| 65 |         if (user_info == NULL) {
 | 
|---|
| 66 |                 DEBUG(0,("talloc failed for user_info\n"));
 | 
|---|
| 67 |                 return NT_STATUS_NO_MEMORY;
 | 
|---|
| 68 |         }
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         user_info->client.account_name = talloc_strdup(user_info, smb_name);
 | 
|---|
| 73 |         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->client.account_name, user_info);
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         user_info->mapped.account_name = talloc_strdup(user_info, internal_username);
 | 
|---|
| 76 |         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->mapped.account_name, user_info);
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         user_info->mapped.domain_name = talloc_strdup(user_info, domain);
 | 
|---|
| 79 |         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->mapped.domain_name, user_info);
 | 
|---|
| 80 | 
 | 
|---|
| 81 |         user_info->client.domain_name = talloc_strdup(user_info, client_domain);
 | 
|---|
| 82 |         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->client.domain_name, user_info);
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         user_info->workstation_name = talloc_strdup(user_info, workstation_name);
 | 
|---|
| 85 |         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->workstation_name, user_info);
 | 
|---|
| 86 | 
 | 
|---|
| 87 |         DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
 | 
|---|
| 88 | 
 | 
|---|
| 89 |         if (lm_pwd && lm_pwd->data) {
 | 
|---|
| 90 |                 user_info->password.response.lanman = data_blob_talloc(user_info, lm_pwd->data, lm_pwd->length);
 | 
|---|
| 91 |                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->password.response.lanman.data, user_info);
 | 
|---|
| 92 |         }
 | 
|---|
| 93 |         if (nt_pwd && nt_pwd->data) {
 | 
|---|
| 94 |                 user_info->password.response.nt = data_blob_talloc(user_info, nt_pwd->data, nt_pwd->length);
 | 
|---|
| 95 |                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->password.response.nt.data, user_info);
 | 
|---|
| 96 |         }
 | 
|---|
| 97 |         if (lm_interactive_pwd) {
 | 
|---|
| 98 |                 user_info->password.hash.lanman = talloc(user_info, struct samr_Password);
 | 
|---|
| 99 |                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->password.hash.lanman, user_info);
 | 
|---|
| 100 |                 memcpy(user_info->password.hash.lanman->hash, lm_interactive_pwd->hash,
 | 
|---|
| 101 |                        sizeof(user_info->password.hash.lanman->hash));
 | 
|---|
| 102 |                 talloc_set_destructor(user_info->password.hash.lanman, clear_samr_Password);
 | 
|---|
| 103 |         }
 | 
|---|
| 104 | 
 | 
|---|
| 105 |         if (nt_interactive_pwd) {
 | 
|---|
| 106 |                 user_info->password.hash.nt = talloc(user_info, struct samr_Password);
 | 
|---|
| 107 |                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->password.hash.nt, user_info);
 | 
|---|
| 108 |                 memcpy(user_info->password.hash.nt->hash, nt_interactive_pwd->hash,
 | 
|---|
| 109 |                        sizeof(user_info->password.hash.nt->hash));
 | 
|---|
| 110 |                 talloc_set_destructor(user_info->password.hash.nt, clear_samr_Password);
 | 
|---|
| 111 |         }
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         if (plaintext_password) {
 | 
|---|
| 114 |                 user_info->password.plaintext = talloc_strdup(user_info, plaintext_password);
 | 
|---|
| 115 |                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info->password.plaintext, user_info);
 | 
|---|
| 116 |                 talloc_set_destructor(user_info->password.plaintext, clear_string);
 | 
|---|
| 117 |         }
 | 
|---|
| 118 | 
 | 
|---|
| 119 |         user_info->password_state = password_state;
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         user_info->logon_parameters = 0;
 | 
|---|
| 122 | 
 | 
|---|
| 123 |         DEBUG(10,("made a user_info for %s (%s)\n", internal_username, smb_name));
 | 
|---|
| 124 |         *ret_user_info = user_info;
 | 
|---|
| 125 |         return NT_STATUS_OK;
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | /***************************************************************************
 | 
|---|
| 129 |  Free a user_info struct
 | 
|---|
| 130 | ***************************************************************************/
 | 
|---|
| 131 | 
 | 
|---|
| 132 | void free_user_info(struct auth_usersupplied_info **user_info)
 | 
|---|
| 133 | {
 | 
|---|
| 134 |         TALLOC_FREE(*user_info);
 | 
|---|
| 135 | }
 | 
|---|