Until the new ldb changes land, make ldb_wait set the error string.
[samba.git] / source4 / auth / auth_sam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2004
5    Copyright (C) Gerald Carter                             2003
6    Copyright (C) Stefan Metzmacher                         2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_netlogon.h"
24 #include "system/time.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "util/util_ldb.h"
27 #include "auth/auth.h"
28 #include "auth/auth_sam.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "libcli/security/security.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "param/param.h"
33
34 extern const char *user_attrs[];
35 extern const char *domain_ref_attrs[];
36
37 /****************************************************************************
38  Look for the specified user in the sam, return ldb result structures
39 ****************************************************************************/
40
41 static NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
42                                        const char *account_name,
43                                        const char *domain_name,
44                                        struct ldb_message ***ret_msgs,
45                                        struct ldb_message ***ret_msgs_domain_ref)
46 {
47         struct ldb_message **msgs_tmp;
48         struct ldb_message **msgs;
49         struct ldb_message **msgs_domain_ref;
50         struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
51
52         int ret;
53         int ret_domain;
54
55         struct ldb_dn *domain_dn = NULL;
56
57         if (domain_name) {
58                 domain_dn = samdb_domain_to_dn(sam_ctx, mem_ctx, domain_name);
59                 if (!domain_dn) {
60                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
61                 }
62         }
63
64         /* pull the user attributes */
65         ret = gendb_search(sam_ctx, mem_ctx, domain_dn, &msgs, user_attrs,
66                            "(&(sAMAccountName=%s)(objectclass=user))", 
67                            ldb_binary_encode_string(mem_ctx, account_name));
68         if (ret == -1) {
69                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
70         }
71
72         if (ret == 0) {
73                 DEBUG(3,("sam_search_user: Couldn't find user [%s\\%s] in samdb, under %s\n", 
74                          domain_name, account_name, ldb_dn_get_linearized(domain_dn)));
75                 return NT_STATUS_NO_SUCH_USER;
76         }
77
78         if (ret > 1) {
79                 DEBUG(0,("Found %d records matching user [%s]\n", ret, account_name));
80                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
81         }
82
83         if (!domain_dn) {
84                 struct dom_sid *domain_sid;
85
86                 domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
87                 if (!domain_sid) {
88                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
89                 }
90
91                 /* find the domain's DN */
92                 ret = gendb_search(sam_ctx, mem_ctx, NULL, &msgs_tmp, NULL,
93                                    "(&(objectSid=%s)(objectClass=domain))", 
94                                    ldap_encode_ndr_dom_sid(mem_ctx, domain_sid));
95                 if (ret == -1) {
96                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
97                 }
98                 
99                 if (ret == 0) {
100                         DEBUG(3,("check_sam_security: Couldn't find domain_sid [%s] in passdb file.\n",
101                                  dom_sid_string(mem_ctx, domain_sid)));
102                         return NT_STATUS_NO_SUCH_USER;
103                 }
104                 
105                 if (ret > 1) {
106                         DEBUG(0,("Found %d records matching domain_sid [%s]\n", 
107                                  ret, dom_sid_string(mem_ctx, domain_sid)));
108                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
109                 }
110
111                 domain_dn = msgs_tmp[0]->dn;
112         }
113
114         ret_domain = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &msgs_domain_ref, domain_ref_attrs,
115                                   "(nCName=%s)", ldb_dn_get_linearized(domain_dn));
116         if (ret_domain == -1) {
117                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
118         }
119                 
120         if (ret_domain == 0) {
121                 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
122                          ldb_dn_get_linearized(msgs_tmp[0]->dn)));
123                 return NT_STATUS_NO_SUCH_USER;
124         }
125                 
126         if (ret_domain > 1) {
127                 DEBUG(0,("Found %d records matching domain [%s]\n", 
128                          ret_domain, ldb_dn_get_linearized(msgs_tmp[0]->dn)));
129                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
130         }
131
132         *ret_msgs = msgs;
133         *ret_msgs_domain_ref = msgs_domain_ref;
134         
135         return NT_STATUS_OK;
136 }
137
138 /****************************************************************************
139  Do a specific test for an smb password being correct, given a smb_password and
140  the lanman and NT responses.
141 ****************************************************************************/
142 static NTSTATUS authsam_password_ok(struct auth_context *auth_context,
143                                     TALLOC_CTX *mem_ctx,
144                                     uint16_t acct_flags,
145                                     const struct samr_Password *lm_pwd, 
146                                     const struct samr_Password *nt_pwd,
147                                     const struct auth_usersupplied_info *user_info, 
148                                     DATA_BLOB *user_sess_key, 
149                                     DATA_BLOB *lm_sess_key)
150 {
151         NTSTATUS status;
152
153         if (acct_flags & ACB_PWNOTREQ) {
154                 if (lp_null_passwords(auth_context->lp_ctx)) {
155                         DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", 
156                                  user_info->mapped.account_name));
157                         return NT_STATUS_OK;
158                 } else {
159                         DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", 
160                                  user_info->mapped.account_name));
161                         return NT_STATUS_LOGON_FAILURE;
162                 }               
163         }
164
165         switch (user_info->password_state) {
166         case AUTH_PASSWORD_PLAIN: 
167         {
168                 const struct auth_usersupplied_info *user_info_temp;    
169                 status = encrypt_user_info(mem_ctx, auth_context, 
170                                            AUTH_PASSWORD_HASH, 
171                                            user_info, &user_info_temp);
172                 if (!NT_STATUS_IS_OK(status)) {
173                         DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
174                         return status;
175                 }
176                 user_info = user_info_temp;
177
178                 /*fall through*/
179         }
180         case AUTH_PASSWORD_HASH:
181                 *lm_sess_key = data_blob(NULL, 0);
182                 *user_sess_key = data_blob(NULL, 0);
183                 status = hash_password_check(mem_ctx, 
184                                              auth_context->lp_ctx,
185                                              user_info->password.hash.lanman,
186                                              user_info->password.hash.nt,
187                                              user_info->mapped.account_name,
188                                              lm_pwd, nt_pwd);
189                 NT_STATUS_NOT_OK_RETURN(status);
190                 break;
191                 
192         case AUTH_PASSWORD_RESPONSE:
193                 status = ntlm_password_check(mem_ctx, 
194                                              auth_context->lp_ctx,
195                                              user_info->logon_parameters, 
196                                              &auth_context->challenge.data, 
197                                              &user_info->password.response.lanman, 
198                                              &user_info->password.response.nt,
199                                              user_info->mapped.account_name,
200                                              user_info->client.account_name, 
201                                              user_info->client.domain_name, 
202                                              lm_pwd, nt_pwd,
203                                              user_sess_key, lm_sess_key);
204                 NT_STATUS_NOT_OK_RETURN(status);
205                 break;
206         }
207
208         if (user_sess_key && user_sess_key->data) {
209                 talloc_steal(auth_context, user_sess_key->data);
210         }
211         if (lm_sess_key && lm_sess_key->data) {
212                 talloc_steal(auth_context, lm_sess_key->data);
213         }
214
215         return NT_STATUS_OK;
216 }
217
218
219
220 static NTSTATUS authsam_authenticate(struct auth_context *auth_context, 
221                                      TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx, 
222                                      struct ldb_message **msgs,
223                                      struct ldb_message **msgs_domain_ref,
224                                      const struct auth_usersupplied_info *user_info, 
225                                      DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key) 
226 {
227         struct samr_Password *lm_pwd, *nt_pwd;
228         NTSTATUS nt_status;
229         uint16_t acct_flags = samdb_result_acct_flags(msgs[0], "userAccountControl");
230         
231         /* Quit if the account was locked out. */
232         if (acct_flags & ACB_AUTOLOCK) {
233                 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n", 
234                          user_info->mapped.account_name));
235                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
236         }
237
238         /* You can only do an interactive login to normal accounts */
239         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
240                 if (!(acct_flags & ACB_NORMAL)) {
241                         return NT_STATUS_NO_SUCH_USER;
242                 }
243         }
244
245         nt_status = samdb_result_passwords(mem_ctx, msgs[0], &lm_pwd, &nt_pwd);
246         NT_STATUS_NOT_OK_RETURN(nt_status);
247
248         nt_status = authsam_password_ok(auth_context, mem_ctx, 
249                                         acct_flags, lm_pwd, nt_pwd,
250                                         user_info, user_sess_key, lm_sess_key);
251         NT_STATUS_NOT_OK_RETURN(nt_status);
252
253         nt_status = authsam_account_ok(mem_ctx, sam_ctx, 
254                                        user_info->logon_parameters,
255                                        msgs[0],
256                                        msgs_domain_ref[0],
257                                        user_info->workstation_name,
258                                        user_info->mapped.account_name);
259
260         return nt_status;
261 }
262
263
264
265 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
266                                                  TALLOC_CTX *mem_ctx,
267                                                  const char *domain,
268                                                  const struct auth_usersupplied_info *user_info, 
269                                                  struct auth_serversupplied_info **server_info)
270 {
271         NTSTATUS nt_status;
272         const char *account_name = user_info->mapped.account_name;
273         struct ldb_message **msgs;
274         struct ldb_message **domain_ref_msgs;
275         struct ldb_context *sam_ctx;
276         DATA_BLOB user_sess_key, lm_sess_key;
277         TALLOC_CTX *tmp_ctx;
278
279         if (!account_name || !*account_name) {
280                 /* 'not for me' */
281                 return NT_STATUS_NOT_IMPLEMENTED;
282         }
283
284         tmp_ctx = talloc_new(mem_ctx);
285         if (!tmp_ctx) {
286                 return NT_STATUS_NO_MEMORY;
287         }
288
289         sam_ctx = samdb_connect(tmp_ctx, ctx->auth_ctx->lp_ctx, system_session(mem_ctx, ctx->auth_ctx->lp_ctx));
290         if (sam_ctx == NULL) {
291                 talloc_free(tmp_ctx);
292                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
293         }
294
295         nt_status = authsam_search_account(tmp_ctx, sam_ctx, account_name, domain, &msgs, &domain_ref_msgs);
296         if (!NT_STATUS_IS_OK(nt_status)) {
297                 talloc_free(tmp_ctx);
298                 return nt_status;
299         }
300
301         nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, sam_ctx, msgs, domain_ref_msgs, user_info,
302                                          &user_sess_key, &lm_sess_key);
303         if (!NT_STATUS_IS_OK(nt_status)) {
304                 talloc_free(tmp_ctx);
305                 return nt_status;
306         }
307
308         nt_status = authsam_make_server_info(tmp_ctx, sam_ctx, lp_netbios_name(ctx->auth_ctx->lp_ctx), 
309                                              msgs[0], domain_ref_msgs[0],
310                                              user_sess_key, lm_sess_key,
311                                              server_info);
312         if (!NT_STATUS_IS_OK(nt_status)) {
313                 talloc_free(tmp_ctx);
314                 return nt_status;
315         }
316
317         talloc_steal(mem_ctx, *server_info);
318         talloc_free(tmp_ctx);
319
320         return NT_STATUS_OK;
321 }
322
323 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
324                                                 TALLOC_CTX *mem_ctx,
325                                                 const struct auth_usersupplied_info *user_info)
326 {
327         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
328                 return NT_STATUS_NOT_IMPLEMENTED;
329         }
330
331         return NT_STATUS_OK;
332 }
333
334 static NTSTATUS authsam_ignoredomain_check_password(struct auth_method_context *ctx,
335                                                     TALLOC_CTX *mem_ctx,
336                                                     const struct auth_usersupplied_info *user_info, 
337                                                     struct auth_serversupplied_info **server_info)
338 {
339         return authsam_check_password_internals(ctx, mem_ctx, NULL, user_info, server_info);
340 }
341
342 /****************************************************************************
343 Check SAM security (above) but with a few extra checks.
344 ****************************************************************************/
345 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
346                                    TALLOC_CTX *mem_ctx,
347                                    const struct auth_usersupplied_info *user_info)
348 {
349         bool is_local_name, is_my_domain;
350
351         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
352                 return NT_STATUS_NOT_IMPLEMENTED;
353         }
354
355         is_local_name = lp_is_myname(ctx->auth_ctx->lp_ctx, 
356                                   user_info->mapped.domain_name);
357         is_my_domain  = lp_is_mydomain(ctx->auth_ctx->lp_ctx, 
358                                        user_info->mapped.domain_name); 
359
360         /* check whether or not we service this domain/workgroup name */
361         switch (lp_server_role(ctx->auth_ctx->lp_ctx)) {
362                 case ROLE_STANDALONE:
363                         return NT_STATUS_OK;
364
365                 case ROLE_DOMAIN_MEMBER:
366                         if (!is_local_name) {
367                                 DEBUG(6,("authsam_check_password: %s is not one of my local names (DOMAIN_MEMBER)\n",
368                                         user_info->mapped.domain_name));
369                                 return NT_STATUS_NOT_IMPLEMENTED;
370                         }
371                         return NT_STATUS_OK;
372
373                 case ROLE_DOMAIN_CONTROLLER:
374                         if (!is_local_name && !is_my_domain) {
375                                 DEBUG(6,("authsam_check_password: %s is not one of my local names or domain name (DC)\n",
376                                         user_info->mapped.domain_name));
377                                 return NT_STATUS_NOT_IMPLEMENTED;
378                         }
379                         return NT_STATUS_OK;
380         }
381
382         DEBUG(6,("authsam_check_password: lp_server_role() has an undefined value\n"));
383         return NT_STATUS_NOT_IMPLEMENTED;
384 }
385
386 /****************************************************************************
387 Check SAM security (above) but with a few extra checks.
388 ****************************************************************************/
389 static NTSTATUS authsam_check_password(struct auth_method_context *ctx,
390                                        TALLOC_CTX *mem_ctx,
391                                        const struct auth_usersupplied_info *user_info, 
392                                        struct auth_serversupplied_info **server_info)
393 {
394         const char *domain;
395
396         /* check whether or not we service this domain/workgroup name */
397         switch (lp_server_role(ctx->auth_ctx->lp_ctx)) {
398                 case ROLE_STANDALONE:
399                 case ROLE_DOMAIN_MEMBER:
400                         domain = lp_netbios_name(ctx->auth_ctx->lp_ctx);
401                         break;
402
403                 case ROLE_DOMAIN_CONTROLLER:
404                         domain = lp_workgroup(ctx->auth_ctx->lp_ctx);
405                         break;
406
407                 default:
408                         return NT_STATUS_NO_SUCH_USER;
409         }
410
411         return authsam_check_password_internals(ctx, mem_ctx, domain, user_info, server_info);
412 }
413
414 static const struct auth_operations sam_ignoredomain_ops = {
415         .name           = "sam_ignoredomain",
416         .get_challenge  = auth_get_challenge_not_implemented,
417         .want_check     = authsam_ignoredomain_want_check,
418         .check_password = authsam_ignoredomain_check_password
419 };
420
421 static const struct auth_operations sam_ops = {
422         .name           = "sam",
423         .get_challenge  = auth_get_challenge_not_implemented,
424         .want_check     = authsam_want_check,
425         .check_password = authsam_check_password
426 };
427
428 NTSTATUS auth_sam_init(void)
429 {
430         NTSTATUS ret;
431
432         ret = auth_register(&sam_ops);
433         if (!NT_STATUS_IS_OK(ret)) {
434                 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
435                 return ret;
436         }
437
438         ret = auth_register(&sam_ignoredomain_ops);
439         if (!NT_STATUS_IS_OK(ret)) {
440                 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));
441                 return ret;
442         }
443
444         return ret;
445 }