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