s4-dsdb: added dsdb_search_one() and cleanup dsdb_find_dn_by_guid()
[gd/samba-autobuild/.git] / source4 / auth / ntlm / auth_sam.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2009
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 "system/time.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "../lib/util/util_ldb.h"
26 #include "auth/auth.h"
27 #include "../libcli/auth/ntlm_check.h"
28 #include "auth/ntlm/auth_proto.h"
29 #include "auth/auth_sam.h"
30 #include "dsdb/samdb/samdb.h"
31 #include "dsdb/common/util.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                                        struct ldb_dn *domain_dn,
44                                        struct ldb_message **ret_msg)
45 {
46         int ret;
47
48         /* pull the user attributes */
49         ret = dsdb_search_one(sam_ctx, mem_ctx, ret_msg, domain_dn, LDB_SCOPE_SUBTREE,
50                               user_attrs,
51                               DSDB_SEARCH_SHOW_EXTENDED_DN,
52                               "(&(sAMAccountName=%s)(objectclass=user))",
53                               ldb_binary_encode_string(mem_ctx, account_name));
54         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
55                 DEBUG(3,("sam_search_user: Couldn't find user [%s] in samdb, under %s\n", 
56                          account_name, ldb_dn_get_linearized(domain_dn)));
57                 return NT_STATUS_NO_SUCH_USER;          
58         }
59         if (ret != LDB_SUCCESS) {
60                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
61         }
62         
63         return NT_STATUS_OK;
64 }
65
66 /****************************************************************************
67  Do a specific test for an smb password being correct, given a smb_password and
68  the lanman and NT responses.
69 ****************************************************************************/
70 static NTSTATUS authsam_password_ok(struct auth_context *auth_context,
71                                     TALLOC_CTX *mem_ctx,
72                                     uint16_t acct_flags,
73                                     const struct samr_Password *lm_pwd, 
74                                     const struct samr_Password *nt_pwd,
75                                     const struct auth_usersupplied_info *user_info, 
76                                     DATA_BLOB *user_sess_key, 
77                                     DATA_BLOB *lm_sess_key)
78 {
79         NTSTATUS status;
80
81         switch (user_info->password_state) {
82         case AUTH_PASSWORD_PLAIN: 
83         {
84                 const struct auth_usersupplied_info *user_info_temp;    
85                 status = encrypt_user_info(mem_ctx, auth_context, 
86                                            AUTH_PASSWORD_HASH, 
87                                            user_info, &user_info_temp);
88                 if (!NT_STATUS_IS_OK(status)) {
89                         DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
90                         return status;
91                 }
92                 user_info = user_info_temp;
93
94                 /*fall through*/
95         }
96         case AUTH_PASSWORD_HASH:
97                 *lm_sess_key = data_blob(NULL, 0);
98                 *user_sess_key = data_blob(NULL, 0);
99                 status = hash_password_check(mem_ctx, 
100                                              lp_lanman_auth(auth_context->lp_ctx),
101                                              user_info->password.hash.lanman,
102                                              user_info->password.hash.nt,
103                                              user_info->mapped.account_name,
104                                              lm_pwd, nt_pwd);
105                 NT_STATUS_NOT_OK_RETURN(status);
106                 break;
107                 
108         case AUTH_PASSWORD_RESPONSE:
109                 status = ntlm_password_check(mem_ctx, 
110                                              lp_lanman_auth(auth_context->lp_ctx),
111                                                  lp_ntlm_auth(auth_context->lp_ctx),
112                                              user_info->logon_parameters, 
113                                              &auth_context->challenge.data, 
114                                              &user_info->password.response.lanman, 
115                                              &user_info->password.response.nt,
116                                              user_info->mapped.account_name,
117                                              user_info->client.account_name, 
118                                              user_info->client.domain_name, 
119                                              lm_pwd, nt_pwd,
120                                              user_sess_key, lm_sess_key);
121                 NT_STATUS_NOT_OK_RETURN(status);
122                 break;
123         }
124
125         if (user_sess_key && user_sess_key->data) {
126                 talloc_steal(auth_context, user_sess_key->data);
127         }
128         if (lm_sess_key && lm_sess_key->data) {
129                 talloc_steal(auth_context, lm_sess_key->data);
130         }
131
132         return NT_STATUS_OK;
133 }
134
135
136
137 static NTSTATUS authsam_authenticate(struct auth_context *auth_context, 
138                                      TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx, 
139                                      struct ldb_dn *domain_dn,
140                                      struct ldb_message *msg,
141                                      const struct auth_usersupplied_info *user_info, 
142                                      DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key) 
143 {
144         struct samr_Password *lm_pwd, *nt_pwd;
145         NTSTATUS nt_status;
146
147         uint16_t acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
148         
149         /* Quit if the account was locked out. */
150         if (acct_flags & ACB_AUTOLOCK) {
151                 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n", 
152                          user_info->mapped.account_name));
153                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
154         }
155
156         /* You can only do an interactive login to normal accounts */
157         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
158                 if (!(acct_flags & ACB_NORMAL)) {
159                         return NT_STATUS_NO_SUCH_USER;
160                 }
161         }
162
163         nt_status = samdb_result_passwords(mem_ctx, auth_context->lp_ctx, msg, &lm_pwd, &nt_pwd);
164         NT_STATUS_NOT_OK_RETURN(nt_status);
165
166         nt_status = authsam_password_ok(auth_context, mem_ctx, 
167                                         acct_flags, lm_pwd, nt_pwd,
168                                         user_info, user_sess_key, lm_sess_key);
169         NT_STATUS_NOT_OK_RETURN(nt_status);
170
171         nt_status = authsam_account_ok(mem_ctx, sam_ctx, 
172                                        user_info->logon_parameters,
173                                        domain_dn,
174                                        msg,
175                                        user_info->workstation_name,
176                                        user_info->mapped.account_name,
177                                        false, false);
178
179         return nt_status;
180 }
181
182
183
184 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
185                                                  TALLOC_CTX *mem_ctx,
186                                                  const struct auth_usersupplied_info *user_info, 
187                                                  struct auth_serversupplied_info **server_info)
188 {
189         NTSTATUS nt_status;
190         const char *account_name = user_info->mapped.account_name;
191         struct ldb_message *msg;
192         struct ldb_context *sam_ctx;
193         struct ldb_dn *domain_dn;
194         DATA_BLOB user_sess_key, lm_sess_key;
195         TALLOC_CTX *tmp_ctx;
196
197         if (!account_name || !*account_name) {
198                 /* 'not for me' */
199                 return NT_STATUS_NOT_IMPLEMENTED;
200         }
201
202         tmp_ctx = talloc_new(mem_ctx);
203         if (!tmp_ctx) {
204                 return NT_STATUS_NO_MEMORY;
205         }
206
207         sam_ctx = samdb_connect(tmp_ctx, ctx->auth_ctx->event_ctx, ctx->auth_ctx->lp_ctx, system_session(ctx->auth_ctx->lp_ctx));
208         if (sam_ctx == NULL) {
209                 talloc_free(tmp_ctx);
210                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
211         }
212
213         domain_dn = ldb_get_default_basedn(sam_ctx);
214         if (domain_dn == NULL) {
215                 talloc_free(tmp_ctx);
216                 return NT_STATUS_NO_SUCH_DOMAIN;
217         }
218
219         nt_status = authsam_search_account(tmp_ctx, sam_ctx, account_name, domain_dn, &msg);
220         if (!NT_STATUS_IS_OK(nt_status)) {
221                 talloc_free(tmp_ctx);
222                 return nt_status;
223         }
224
225         nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, sam_ctx, domain_dn, msg, user_info,
226                                          &user_sess_key, &lm_sess_key);
227         if (!NT_STATUS_IS_OK(nt_status)) {
228                 talloc_free(tmp_ctx);
229                 return nt_status;
230         }
231
232         nt_status = authsam_make_server_info(tmp_ctx, sam_ctx, lp_netbios_name(ctx->auth_ctx->lp_ctx), 
233                                              lp_sam_name(ctx->auth_ctx->lp_ctx),
234                                              domain_dn,
235                                              msg,
236                                              user_sess_key, lm_sess_key,
237                                              server_info);
238         if (!NT_STATUS_IS_OK(nt_status)) {
239                 talloc_free(tmp_ctx);
240                 return nt_status;
241         }
242
243         talloc_steal(mem_ctx, *server_info);
244         talloc_free(tmp_ctx);
245
246         return NT_STATUS_OK;
247 }
248
249 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
250                                                 TALLOC_CTX *mem_ctx,
251                                                 const struct auth_usersupplied_info *user_info)
252 {
253         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
254                 return NT_STATUS_NOT_IMPLEMENTED;
255         }
256
257         return NT_STATUS_OK;
258 }
259
260 /****************************************************************************
261 Check SAM security (above) but with a few extra checks.
262 ****************************************************************************/
263 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
264                                    TALLOC_CTX *mem_ctx,
265                                    const struct auth_usersupplied_info *user_info)
266 {
267         bool is_local_name, is_my_domain;
268
269         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
270                 return NT_STATUS_NOT_IMPLEMENTED;
271         }
272
273         is_local_name = lp_is_myname(ctx->auth_ctx->lp_ctx, 
274                                   user_info->mapped.domain_name);
275         is_my_domain  = lp_is_mydomain(ctx->auth_ctx->lp_ctx, 
276                                        user_info->mapped.domain_name); 
277
278         /* check whether or not we service this domain/workgroup name */
279         switch (lp_server_role(ctx->auth_ctx->lp_ctx)) {
280                 case ROLE_STANDALONE:
281                         return NT_STATUS_OK;
282
283                 case ROLE_DOMAIN_MEMBER:
284                         if (!is_local_name) {
285                                 DEBUG(6,("authsam_check_password: %s is not one of my local names (DOMAIN_MEMBER)\n",
286                                         user_info->mapped.domain_name));
287                                 return NT_STATUS_NOT_IMPLEMENTED;
288                         }
289                         return NT_STATUS_OK;
290
291                 case ROLE_DOMAIN_CONTROLLER:
292                         if (!is_local_name && !is_my_domain) {
293                                 DEBUG(6,("authsam_check_password: %s is not one of my local names or domain name (DC)\n",
294                                         user_info->mapped.domain_name));
295                                 return NT_STATUS_NOT_IMPLEMENTED;
296                         }
297                         return NT_STATUS_OK;
298         }
299
300         DEBUG(6,("authsam_check_password: lp_server_role() has an undefined value\n"));
301         return NT_STATUS_NOT_IMPLEMENTED;
302 }
303
304                                    
305 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available */
306 NTSTATUS authsam_get_server_info_principal(TALLOC_CTX *mem_ctx, 
307                                            struct auth_context *auth_context,
308                                            const char *principal,
309                                            struct auth_serversupplied_info **server_info)
310 {
311         NTSTATUS nt_status;
312         DATA_BLOB user_sess_key = data_blob(NULL, 0);
313         DATA_BLOB lm_sess_key = data_blob(NULL, 0);
314
315         struct ldb_message *msg;
316         struct ldb_context *sam_ctx;
317         struct ldb_dn *domain_dn;
318         
319         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
320         if (!tmp_ctx) {
321                 return NT_STATUS_NO_MEMORY;
322         }
323
324         sam_ctx = samdb_connect(tmp_ctx, auth_context->event_ctx, auth_context->lp_ctx, 
325                                 system_session(auth_context->lp_ctx));
326         if (sam_ctx == NULL) {
327                 talloc_free(tmp_ctx);
328                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
329         }
330
331         nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal, 
332                                               user_attrs, &domain_dn, &msg);
333         if (!NT_STATUS_IS_OK(nt_status)) {
334                 talloc_free(tmp_ctx);
335                 return nt_status;
336         }
337
338         nt_status = authsam_make_server_info(tmp_ctx, sam_ctx, 
339                                              lp_netbios_name(auth_context->lp_ctx),
340                                              lp_workgroup(auth_context->lp_ctx),
341                                              domain_dn, 
342                                              msg,
343                                              user_sess_key, lm_sess_key,
344                                              server_info);
345         if (!NT_STATUS_IS_OK(nt_status)) {
346                 talloc_free(tmp_ctx);
347                 return nt_status;
348         }
349
350         talloc_steal(mem_ctx, *server_info);
351         talloc_free(tmp_ctx);
352
353         return NT_STATUS_OK;
354 }
355
356 static const struct auth_operations sam_ignoredomain_ops = {
357         .name                      = "sam_ignoredomain",
358         .get_challenge             = auth_get_challenge_not_implemented,
359         .want_check                = authsam_ignoredomain_want_check,
360         .check_password            = authsam_check_password_internals,
361         .get_server_info_principal = authsam_get_server_info_principal
362 };
363
364 static const struct auth_operations sam_ops = {
365         .name                      = "sam",
366         .get_challenge             = auth_get_challenge_not_implemented,
367         .want_check                = authsam_want_check,
368         .check_password            = authsam_check_password_internals,
369         .get_server_info_principal = authsam_get_server_info_principal
370 };
371
372 _PUBLIC_ NTSTATUS auth_sam_init(void)
373 {
374         NTSTATUS ret;
375
376         ret = auth_register(&sam_ops);
377         if (!NT_STATUS_IS_OK(ret)) {
378                 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
379                 return ret;
380         }
381
382         ret = auth_register(&sam_ignoredomain_ops);
383         if (!NT_STATUS_IS_OK(ret)) {
384                 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));
385                 return ret;
386         }
387
388         return ret;
389 }