r26229: Set loadparm context as opaque pointer in ldb, remove more uses of global_loa...
[tprouty/samba.git] / source / auth / 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 "libcli/security/security.h"
27 #include "libcli/auth/libcli_auth.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "auth/credentials/credentials.h"
30 #include "param/param.h"
31
32 /* this default function can be used by mostly all backends
33  * which don't want to set a challenge
34  */
35 NTSTATUS auth_get_challenge_not_implemented(struct auth_method_context *ctx, TALLOC_CTX *mem_ctx, DATA_BLOB *challenge)
36 {
37         /* we don't want to set a challenge */
38         return NT_STATUS_NOT_IMPLEMENTED;
39 }
40
41 /****************************************************************************
42  Create an auth_usersupplied_data structure after appropriate mapping.
43 ****************************************************************************/
44
45 NTSTATUS map_user_info(TALLOC_CTX *mem_ctx,
46                        const struct auth_usersupplied_info *user_info,
47                        struct auth_usersupplied_info **user_info_mapped)
48 {
49         const char *domain;
50         char *account_name;
51         char *d;
52         DEBUG(5,("map_user_info: Mapping user [%s]\\[%s] from workstation [%s]\n",
53                 user_info->client.domain_name, user_info->client.account_name, user_info->workstation_name));
54
55         account_name = talloc_strdup(mem_ctx, user_info->client.account_name);
56         if (!account_name) {
57                 return NT_STATUS_NO_MEMORY;
58         }
59         
60         /* don't allow "" as a domain, fixes a Win9X bug 
61            where it doesn't supply a domain for logon script
62            'net use' commands.                                 */
63
64         /* Split user@realm names into user and realm components.  This is TODO to fix with proper userprincipalname support */
65         if (user_info->client.domain_name && *user_info->client.domain_name) {
66                 domain = user_info->client.domain_name;
67         } else if (strchr_m(user_info->client.account_name, '@')) {
68                 d = strchr_m(account_name, '@');
69                 if (!d) {
70                         return NT_STATUS_INTERNAL_ERROR;
71                 }
72                 d[0] = '\0';
73                 d++;
74                 domain = d;
75         } else {
76                 domain = lp_workgroup(global_loadparm);
77         }
78
79         *user_info_mapped = talloc(mem_ctx, struct auth_usersupplied_info);
80         if (!*user_info_mapped) {
81                 return NT_STATUS_NO_MEMORY;
82         }
83         talloc_reference(*user_info_mapped, user_info);
84         **user_info_mapped = *user_info;
85         (*user_info_mapped)->mapped_state = true;
86         (*user_info_mapped)->mapped.domain_name = talloc_strdup(*user_info_mapped, domain);
87         (*user_info_mapped)->mapped.account_name = talloc_strdup(*user_info_mapped, account_name);
88         talloc_free(account_name);
89         if (!(*user_info_mapped)->mapped.domain_name 
90             || !(*user_info_mapped)->mapped.account_name) {
91                 return NT_STATUS_NO_MEMORY;
92         }
93
94         return NT_STATUS_OK;
95 }
96
97 /****************************************************************************
98  Create an auth_usersupplied_data structure after appropriate mapping.
99 ****************************************************************************/
100
101 NTSTATUS encrypt_user_info(TALLOC_CTX *mem_ctx, struct auth_context *auth_context, 
102                            enum auth_password_state to_state,
103                            const struct auth_usersupplied_info *user_info_in,
104                            const struct auth_usersupplied_info **user_info_encrypted)
105 {
106         NTSTATUS nt_status;
107         struct auth_usersupplied_info *user_info_temp;
108         switch (to_state) {
109         case AUTH_PASSWORD_RESPONSE:
110                 switch (user_info_in->password_state) {
111                 case AUTH_PASSWORD_PLAIN:
112                 {
113                         const struct auth_usersupplied_info *user_info_temp2;
114                         nt_status = encrypt_user_info(mem_ctx, auth_context, 
115                                                       AUTH_PASSWORD_HASH, 
116                                                       user_info_in, &user_info_temp2);
117                         if (!NT_STATUS_IS_OK(nt_status)) {
118                                 return nt_status;
119                         }
120                         user_info_in = user_info_temp2;
121                         /* fall through */
122                 }
123                 case AUTH_PASSWORD_HASH:
124                 {
125                         const uint8_t *challenge;
126                         DATA_BLOB chall_blob;
127                         user_info_temp = talloc(mem_ctx, struct auth_usersupplied_info);
128                         if (!user_info_temp) {
129                                 return NT_STATUS_NO_MEMORY;
130                         }
131                         talloc_reference(user_info_temp, user_info_in);
132                         *user_info_temp = *user_info_in;
133                         user_info_temp->mapped_state = to_state;
134                         
135                         nt_status = auth_get_challenge(auth_context, &challenge);
136                         if (!NT_STATUS_IS_OK(nt_status)) {
137                                 return nt_status;
138                         }
139                         
140                         chall_blob = data_blob_talloc(mem_ctx, challenge, 8);
141                         if (lp_client_ntlmv2_auth(auth_context->lp_ctx)) {
142                                 DATA_BLOB names_blob = NTLMv2_generate_names_blob(mem_ctx, lp_netbios_name(auth_context->lp_ctx), lp_workgroup(auth_context->lp_ctx));
143                                 DATA_BLOB lmv2_response, ntlmv2_response, lmv2_session_key, ntlmv2_session_key;
144                                 
145                                 if (!SMBNTLMv2encrypt_hash(user_info_temp,
146                                                            user_info_in->client.account_name, 
147                                                            user_info_in->client.domain_name, 
148                                                            user_info_in->password.hash.nt->hash, &chall_blob,
149                                                            &names_blob,
150                                                            &lmv2_response, &ntlmv2_response, 
151                                                            &lmv2_session_key, &ntlmv2_session_key)) {
152                                         data_blob_free(&names_blob);
153                                         return NT_STATUS_NO_MEMORY;
154                                 }
155                                 data_blob_free(&names_blob);
156                                 user_info_temp->password.response.lanman = lmv2_response;
157                                 user_info_temp->password.response.nt = ntlmv2_response;
158                                 
159                                 data_blob_free(&lmv2_session_key);
160                                 data_blob_free(&ntlmv2_session_key);
161                         } else {
162                                 DATA_BLOB blob = data_blob_talloc(mem_ctx, NULL, 24);
163                                 SMBOWFencrypt(user_info_in->password.hash.nt->hash, challenge, blob.data);
164
165                                 user_info_temp->password.response.nt = blob;
166                                 if (lp_client_lanman_auth(auth_context->lp_ctx) && user_info_in->password.hash.lanman) {
167                                         DATA_BLOB lm_blob = data_blob_talloc(mem_ctx, NULL, 24);
168                                         SMBOWFencrypt(user_info_in->password.hash.lanman->hash, challenge, blob.data);
169                                         user_info_temp->password.response.lanman = lm_blob;
170                                 } else {
171                                         /* if not sending the LM password, send the NT password twice */
172                                         user_info_temp->password.response.lanman = user_info_temp->password.response.nt;
173                                 }
174                         }
175
176                         user_info_in = user_info_temp;
177                         /* fall through */
178                 }
179                 case AUTH_PASSWORD_RESPONSE:
180                         *user_info_encrypted = user_info_in;
181                 }
182                 break;
183         case AUTH_PASSWORD_HASH:
184         {       
185                 switch (user_info_in->password_state) {
186                 case AUTH_PASSWORD_PLAIN:
187                 {
188                         struct samr_Password lanman;
189                         struct samr_Password nt;
190                         
191                         user_info_temp = talloc(mem_ctx, struct auth_usersupplied_info);
192                         if (!user_info_temp) {
193                                 return NT_STATUS_NO_MEMORY;
194                         }
195                         talloc_reference(user_info_temp, user_info_in);
196                         *user_info_temp = *user_info_in;
197                         user_info_temp->mapped_state = to_state;
198                         
199                         if (E_deshash(user_info_in->password.plaintext, lanman.hash)) {
200                                 user_info_temp->password.hash.lanman = talloc(user_info_temp,
201                                                                               struct samr_Password);
202                                 *user_info_temp->password.hash.lanman = lanman;
203                         } else {
204                                 user_info_temp->password.hash.lanman = NULL;
205                         }
206                         
207                         E_md4hash(user_info_in->password.plaintext, nt.hash);
208                         user_info_temp->password.hash.nt = talloc(user_info_temp,
209                                                                    struct samr_Password);
210                         *user_info_temp->password.hash.nt = nt;
211                         
212                         user_info_in = user_info_temp;
213                         /* fall through */
214                 }
215                 case AUTH_PASSWORD_HASH:
216                         *user_info_encrypted = user_info_in;
217                         break;
218                 default:
219                         return NT_STATUS_INVALID_PARAMETER;
220                         break;
221                 }
222                 break;
223         }
224         default:
225                 return NT_STATUS_INVALID_PARAMETER;
226         }
227
228         return NT_STATUS_OK;
229 }
230
231
232 /**
233  * Squash an NT_STATUS in line with security requirements.
234  * In an attempt to avoid giving the whole game away when users
235  * are authenticating, NT replaces both NT_STATUS_NO_SUCH_USER and 
236  * NT_STATUS_WRONG_PASSWORD with NT_STATUS_LOGON_FAILURE in certain situations 
237  * (session setups in particular).
238  *
239  * @param nt_status NTSTATUS input for squashing.
240  * @return the 'squashed' nt_status
241  **/
242 NTSTATUS auth_nt_status_squash(NTSTATUS nt_status)
243 {
244         if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
245                 /* Match WinXP and don't give the game away */
246                 return NT_STATUS_LOGON_FAILURE;
247         } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
248                 /* Match WinXP and don't give the game away */
249                 return NT_STATUS_LOGON_FAILURE;
250         }
251
252         return nt_status;
253 }