Make sure prototypes are always included, make some functions static and
[tprouty/samba.git] / source4 / auth / ntlm / auth_util.c
1 /* 
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
7    Copyright (C) Rafal Szczesniak 2002
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "auth/auth.h"
26 #include "auth/auth_proto.h"
27 #include "libcli/security/security.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "auth/credentials/credentials.h"
31 #include "param/param.h"
32
33 /* this default function can be used by mostly all backends
34  * which don't want to set a challenge
35  */
36 NTSTATUS auth_get_challenge_not_implemented(struct auth_method_context *ctx, TALLOC_CTX *mem_ctx, DATA_BLOB *challenge)
37 {
38         /* we don't want to set a challenge */
39         return NT_STATUS_NOT_IMPLEMENTED;
40 }
41
42 /****************************************************************************
43  Create an auth_usersupplied_data structure after appropriate mapping.
44 ****************************************************************************/
45
46 NTSTATUS map_user_info(TALLOC_CTX *mem_ctx,
47                        const char *default_domain,
48                        const struct auth_usersupplied_info *user_info,
49                        struct auth_usersupplied_info **user_info_mapped)
50 {
51         const char *domain;
52         char *account_name;
53         char *d;
54         DEBUG(5,("map_user_info: Mapping user [%s]\\[%s] from workstation [%s]\n",
55                 user_info->client.domain_name, user_info->client.account_name, user_info->workstation_name));
56
57         account_name = talloc_strdup(mem_ctx, user_info->client.account_name);
58         if (!account_name) {
59                 return NT_STATUS_NO_MEMORY;
60         }
61         
62         /* don't allow "" as a domain, fixes a Win9X bug 
63            where it doesn't supply a domain for logon script
64            'net use' commands.                                 */
65
66         /* Split user@realm names into user and realm components.  This is TODO to fix with proper userprincipalname support */
67         if (user_info->client.domain_name && *user_info->client.domain_name) {
68                 domain = user_info->client.domain_name;
69         } else if (strchr_m(user_info->client.account_name, '@')) {
70                 d = strchr_m(account_name, '@');
71                 if (!d) {
72                         return NT_STATUS_INTERNAL_ERROR;
73                 }
74                 d[0] = '\0';
75                 d++;
76                 domain = d;
77         } else {
78                 domain = default_domain;
79         }
80
81         *user_info_mapped = talloc(mem_ctx, struct auth_usersupplied_info);
82         if (!*user_info_mapped) {
83                 return NT_STATUS_NO_MEMORY;
84         }
85         if (!talloc_reference(*user_info_mapped, user_info)) {
86                 return NT_STATUS_NO_MEMORY;
87         }
88         **user_info_mapped = *user_info;
89         (*user_info_mapped)->mapped_state = true;
90         (*user_info_mapped)->mapped.domain_name = talloc_strdup(*user_info_mapped, domain);
91         (*user_info_mapped)->mapped.account_name = talloc_strdup(*user_info_mapped, account_name);
92         talloc_free(account_name);
93         if (!(*user_info_mapped)->mapped.domain_name 
94             || !(*user_info_mapped)->mapped.account_name) {
95                 return NT_STATUS_NO_MEMORY;
96         }
97
98         return NT_STATUS_OK;
99 }
100
101 /****************************************************************************
102  Create an auth_usersupplied_data structure after appropriate mapping.
103 ****************************************************************************/
104
105 NTSTATUS encrypt_user_info(TALLOC_CTX *mem_ctx, struct auth_context *auth_context, 
106                            enum auth_password_state to_state,
107                            const struct auth_usersupplied_info *user_info_in,
108                            const struct auth_usersupplied_info **user_info_encrypted)
109 {
110         NTSTATUS nt_status;
111         struct auth_usersupplied_info *user_info_temp;
112         switch (to_state) {
113         case AUTH_PASSWORD_RESPONSE:
114                 switch (user_info_in->password_state) {
115                 case AUTH_PASSWORD_PLAIN:
116                 {
117                         const struct auth_usersupplied_info *user_info_temp2;
118                         nt_status = encrypt_user_info(mem_ctx, auth_context, 
119                                                       AUTH_PASSWORD_HASH, 
120                                                       user_info_in, &user_info_temp2);
121                         if (!NT_STATUS_IS_OK(nt_status)) {
122                                 return nt_status;
123                         }
124                         user_info_in = user_info_temp2;
125                         /* fall through */
126                 }
127                 case AUTH_PASSWORD_HASH:
128                 {
129                         const uint8_t *challenge;
130                         DATA_BLOB chall_blob;
131                         user_info_temp = talloc(mem_ctx, struct auth_usersupplied_info);
132                         if (!user_info_temp) {
133                                 return NT_STATUS_NO_MEMORY;
134                         }
135                         if (!talloc_reference(user_info_temp, user_info_in)) {
136                                 return NT_STATUS_NO_MEMORY;
137                         }
138                         *user_info_temp = *user_info_in;
139                         user_info_temp->mapped_state = to_state;
140                         
141                         nt_status = auth_get_challenge(auth_context, &challenge);
142                         if (!NT_STATUS_IS_OK(nt_status)) {
143                                 return nt_status;
144                         }
145                         
146                         chall_blob = data_blob_talloc(mem_ctx, challenge, 8);
147                         if (lp_client_ntlmv2_auth(auth_context->lp_ctx)) {
148                                 DATA_BLOB names_blob = NTLMv2_generate_names_blob(mem_ctx, lp_iconv_convenience(auth_context->lp_ctx), lp_netbios_name(auth_context->lp_ctx), lp_workgroup(auth_context->lp_ctx));
149                                 DATA_BLOB lmv2_response, ntlmv2_response, lmv2_session_key, ntlmv2_session_key;
150                                 
151                                 if (!SMBNTLMv2encrypt_hash(user_info_temp,
152                                                            user_info_in->client.account_name, 
153                                                            user_info_in->client.domain_name, 
154                                                            user_info_in->password.hash.nt->hash, &chall_blob,
155                                                            &names_blob,
156                                                            &lmv2_response, &ntlmv2_response, 
157                                                            &lmv2_session_key, &ntlmv2_session_key)) {
158                                         data_blob_free(&names_blob);
159                                         return NT_STATUS_NO_MEMORY;
160                                 }
161                                 data_blob_free(&names_blob);
162                                 user_info_temp->password.response.lanman = lmv2_response;
163                                 user_info_temp->password.response.nt = ntlmv2_response;
164                                 
165                                 data_blob_free(&lmv2_session_key);
166                                 data_blob_free(&ntlmv2_session_key);
167                         } else {
168                                 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, 24);
169                                 SMBOWFencrypt(user_info_in->password.hash.nt->hash, challenge, blob.data);
170
171                                 user_info_temp->password.response.nt = blob;
172                                 if (lp_client_lanman_auth(auth_context->lp_ctx) && user_info_in->password.hash.lanman) {
173                                         DATA_BLOB lm_blob = data_blob_talloc(mem_ctx, NULL, 24);
174                                         SMBOWFencrypt(user_info_in->password.hash.lanman->hash, challenge, blob.data);
175                                         user_info_temp->password.response.lanman = lm_blob;
176                                 } else {
177                                         /* if not sending the LM password, send the NT password twice */
178                                         user_info_temp->password.response.lanman = user_info_temp->password.response.nt;
179                                 }
180                         }
181
182                         user_info_in = user_info_temp;
183                         /* fall through */
184                 }
185                 case AUTH_PASSWORD_RESPONSE:
186                         *user_info_encrypted = user_info_in;
187                 }
188                 break;
189         case AUTH_PASSWORD_HASH:
190         {       
191                 switch (user_info_in->password_state) {
192                 case AUTH_PASSWORD_PLAIN:
193                 {
194                         struct samr_Password lanman;
195                         struct samr_Password nt;
196                         
197                         user_info_temp = talloc(mem_ctx, struct auth_usersupplied_info);
198                         if (!user_info_temp) {
199                                 return NT_STATUS_NO_MEMORY;
200                         }
201                         if (!talloc_reference(user_info_temp, user_info_in)) {
202                                 return NT_STATUS_NO_MEMORY;
203                         }
204                         *user_info_temp = *user_info_in;
205                         user_info_temp->mapped_state = to_state;
206                         
207                         if (E_deshash(user_info_in->password.plaintext, lanman.hash)) {
208                                 user_info_temp->password.hash.lanman = talloc(user_info_temp,
209                                                                               struct samr_Password);
210                                 *user_info_temp->password.hash.lanman = lanman;
211                         } else {
212                                 user_info_temp->password.hash.lanman = NULL;
213                         }
214                         
215                         E_md4hash(user_info_in->password.plaintext, nt.hash);
216                         user_info_temp->password.hash.nt = talloc(user_info_temp,
217                                                                    struct samr_Password);
218                         *user_info_temp->password.hash.nt = nt;
219                         
220                         user_info_in = user_info_temp;
221                         /* fall through */
222                 }
223                 case AUTH_PASSWORD_HASH:
224                         *user_info_encrypted = user_info_in;
225                         break;
226                 default:
227                         return NT_STATUS_INVALID_PARAMETER;
228                         break;
229                 }
230                 break;
231         }
232         default:
233                 return NT_STATUS_INVALID_PARAMETER;
234         }
235
236         return NT_STATUS_OK;
237 }
238
239
240 /**
241  * Squash an NT_STATUS in line with security requirements.
242  * In an attempt to avoid giving the whole game away when users
243  * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and 
244  * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations 
245  * (session setups in particular).
246  *
247  * @param nt_status NTSTATUS input for squashing.
248  * @return the 'squashed' nt_status
249  **/
250 _PUBLIC_ NTSTATUS auth_nt_status_squash(NTSTATUS nt_status)
251 {
252         if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
253                 /* Match WinXP and don't give the game away */
254                 return NT_STATUS_LOGON_FAILURE;
255         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
256                 /* Match WinXP and don't give the game away */
257                 return NT_STATUS_LOGON_FAILURE;
258         }
259
260         return nt_status;
261 }