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