Remove a number of NT_STATUS_HAVE_NO_MEMORY_AND_FREE macros from the codebase.
[bbaumbach/samba-autobuild/.git] / source3 / auth / user_info.c
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 #include "../lib/tsocket/tsocket.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_AUTH
27
28 static int clear_samr_Password(struct samr_Password *password)
29 {
30         memset(password->hash, '\0', sizeof(password->hash));
31         return 0;
32 }
33
34 static int clear_string(char *password)
35 {
36         memset(password, '\0', strlen(password));
37         return 0;
38 }
39
40 /****************************************************************************
41  Create an auth_usersupplied_data structure
42 ****************************************************************************/
43
44 NTSTATUS make_user_info(struct auth_usersupplied_info **ret_user_info,
45                         const char *smb_name,
46                         const char *internal_username,
47                         const char *client_domain,
48                         const char *domain,
49                         const char *workstation_name,
50                         const struct tsocket_address *remote_address,
51                         const DATA_BLOB *lm_pwd,
52                         const DATA_BLOB *nt_pwd,
53                         const struct samr_Password *lm_interactive_pwd,
54                         const struct samr_Password *nt_interactive_pwd,
55                         const char *plaintext_password,
56                         enum auth_password_state password_state)
57 {
58         struct auth_usersupplied_info *user_info;
59         *ret_user_info = NULL;
60
61         DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
62
63         /* FIXME: Have the caller provide a talloc context of the
64          * correct lifetime (possibly talloc_tos(), but it depends on
65          * the caller) */
66         user_info = talloc_zero(NULL, struct auth_usersupplied_info);
67         if (user_info == NULL) {
68                 DEBUG(0,("talloc failed for user_info\n"));
69                 return NT_STATUS_NO_MEMORY;
70         }
71
72         DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
73
74         user_info->client.account_name = talloc_strdup(user_info, smb_name);
75         if (user_info->client.account_name == NULL) {
76                 TALLOC_FREE(user_info);
77                 return NT_STATUS_NO_MEMORY;
78         }
79
80         user_info->mapped.account_name = talloc_strdup(user_info, internal_username);
81         if (user_info->mapped.account_name == NULL) {
82                 TALLOC_FREE(user_info);
83                 return NT_STATUS_NO_MEMORY;
84         }
85
86         user_info->mapped.domain_name = talloc_strdup(user_info, domain);
87         if (user_info->mapped.domain_name == NULL) {
88                 TALLOC_FREE(user_info);
89                 return NT_STATUS_NO_MEMORY;
90         }
91
92         user_info->client.domain_name = talloc_strdup(user_info, client_domain);
93         if (user_info->client.domain_name == NULL) {
94                 TALLOC_FREE(user_info);
95                 return NT_STATUS_NO_MEMORY;
96         }
97
98         user_info->workstation_name = talloc_strdup(user_info, workstation_name);
99         if (user_info->workstation_name == NULL) {
100                 TALLOC_FREE(user_info);
101                 return NT_STATUS_NO_MEMORY;
102         }
103
104         user_info->remote_host = tsocket_address_copy(remote_address, user_info);
105         if (user_info->remote_host == NULL) {
106                 TALLOC_FREE(user_info);
107                 return NT_STATUS_NO_MEMORY;
108         }
109
110         DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
111
112         if (lm_pwd && lm_pwd->data) {
113                 user_info->password.response.lanman = data_blob_talloc(user_info, lm_pwd->data, lm_pwd->length);
114                 if (user_info->password.response.lanman.data == NULL) {
115                         TALLOC_FREE(user_info);
116                         return NT_STATUS_NO_MEMORY;
117                 }
118         }
119         if (nt_pwd && nt_pwd->data) {
120                 user_info->password.response.nt = data_blob_talloc(user_info, nt_pwd->data, nt_pwd->length);
121                 if (user_info->password.response.nt.data == NULL) {
122                         TALLOC_FREE(user_info);
123                         return NT_STATUS_NO_MEMORY;
124                 }
125         }
126         if (lm_interactive_pwd) {
127                 user_info->password.hash.lanman = talloc(user_info, struct samr_Password);
128                 if (user_info->password.hash.lanman == NULL) {
129                         TALLOC_FREE(user_info);
130                         return NT_STATUS_NO_MEMORY;
131                 }
132                 memcpy(user_info->password.hash.lanman->hash, lm_interactive_pwd->hash,
133                        sizeof(user_info->password.hash.lanman->hash));
134                 talloc_set_destructor(user_info->password.hash.lanman, clear_samr_Password);
135         }
136
137         if (nt_interactive_pwd) {
138                 user_info->password.hash.nt = talloc(user_info, struct samr_Password);
139                 if (user_info->password.hash.nt == NULL) {
140                         TALLOC_FREE(user_info);
141                         return NT_STATUS_NO_MEMORY;
142                 }
143                 memcpy(user_info->password.hash.nt->hash, nt_interactive_pwd->hash,
144                        sizeof(user_info->password.hash.nt->hash));
145                 talloc_set_destructor(user_info->password.hash.nt, clear_samr_Password);
146         }
147
148         if (plaintext_password) {
149                 user_info->password.plaintext = talloc_strdup(user_info, plaintext_password);
150                 if (user_info->password.plaintext == NULL) {
151                         TALLOC_FREE(user_info);
152                         return NT_STATUS_NO_MEMORY;
153                 }
154                 talloc_set_destructor(user_info->password.plaintext, clear_string);
155         }
156
157         user_info->password_state = password_state;
158
159         user_info->logon_parameters = 0;
160
161         DEBUG(10,("made a user_info for %s (%s)\n", internal_username, smb_name));
162         *ret_user_info = user_info;
163         return NT_STATUS_OK;
164 }
165
166 /***************************************************************************
167  Free a user_info struct
168 ***************************************************************************/
169
170 void free_user_info(struct auth_usersupplied_info **user_info)
171 {
172         TALLOC_FREE(*user_info);
173 }