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