r23792: convert Samba4 to GPLv3
[garming/samba-autobuild/.git] / source4 / 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 "system/time.h"
24 #include "auth/auth.h"
25 #include "db_wrap.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "libcli/security/security.h"
28 #include "libcli/ldap/ldap.h"
29 #include "librpc/gen_ndr/ndr_netlogon.h"
30
31 const char *user_attrs[] = {
32         /* required for the krb5 kdc */
33         "objectClass",
34         "sAMAccountName",
35         "userPrincipalName",
36         "servicePrincipalName",
37         "msDS-KeyVersionNumber",
38         "supplementalCredentials",
39
40         /* passwords */
41         "dBCSPwd", 
42         "unicodePwd",
43
44         "userAccountControl",
45
46         "pwdLastSet",
47         "accountExpires",
48         
49         "objectSid",
50
51         /* check 'allowed workstations' */
52         "userWorkstations",
53                        
54         /* required for server_info, not access control: */
55         "displayName",
56         "scriptPath",
57         "profilePath",
58         "homeDirectory",
59         "homeDrive",
60         "lastLogon",
61         "lastLogoff",
62         "accountExpires",
63         "badPwdCount",
64         "logonCount",
65         "primaryGroupID",
66         NULL,
67 };
68
69 const char *domain_ref_attrs[] =  {"nETBIOSName", "nCName", 
70                                           "dnsRoot", "objectClass", NULL};
71
72
73 /****************************************************************************
74  Do a specific test for a SAM_ACCOUNT being vaild for this connection 
75  (ie not disabled, expired and the like).
76 ****************************************************************************/
77 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
78                             struct ldb_context *sam_ctx,
79                             uint32_t logon_parameters,
80                             struct ldb_message *msg,
81                             struct ldb_message *msg_domain_ref,
82                             const char *logon_workstation,
83                             const char *name_for_logs)
84 {
85         uint16_t acct_flags;
86         const char *workstation_list;
87         NTTIME acct_expiry;
88         NTTIME must_change_time;
89         NTTIME last_set_time;
90
91         struct ldb_dn *domain_dn = samdb_result_dn(sam_ctx, mem_ctx, msg_domain_ref, "nCName", ldb_dn_new(mem_ctx, sam_ctx, NULL));
92
93         NTTIME now;
94         DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
95
96         acct_flags = samdb_result_acct_flags(msg, "userAccountControl");
97         
98         acct_expiry = samdb_result_nttime(msg, "accountExpires", 0);
99         must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx, 
100                                                               domain_dn, msg);
101         last_set_time = samdb_result_nttime(msg, "pwdLastSet", 0);
102
103         workstation_list = samdb_result_string(msg, "userWorkstations", NULL);
104
105         /* Quit if the account was disabled. */
106         if (acct_flags & ACB_DISABLED) {
107                 DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
108                 return NT_STATUS_ACCOUNT_DISABLED;
109         }
110
111         /* Quit if the account was locked out. */
112         if (acct_flags & ACB_AUTOLOCK) {
113                 DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
114                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
115         }
116
117         /* Test account expire time */
118         unix_to_nt_time(&now, time(NULL));
119         if (now > acct_expiry) {
120                 DEBUG(1,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
121                 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n", 
122                          nt_time_string(mem_ctx, acct_expiry)));
123                 return NT_STATUS_ACCOUNT_EXPIRED;
124         }
125
126         if (!(acct_flags & ACB_PWNOEXP)) {
127                 /* check for immediate expiry "must change at next logon" */
128                 if (must_change_time == 0 && last_set_time != 0) {
129                         DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n", 
130                                  name_for_logs));
131                         return NT_STATUS_PASSWORD_MUST_CHANGE;
132                 }
133
134                 /* check for expired password */
135                 if ((must_change_time != 0) && (must_change_time < now)) {
136                         DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n", 
137                                  name_for_logs));
138                         DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n", 
139                                  nt_time_string(mem_ctx, must_change_time)));
140                         return NT_STATUS_PASSWORD_EXPIRED;
141                 }
142         }
143
144         /* Test workstation. Workstation list is comma separated. */
145         if (logon_workstation && workstation_list && *workstation_list) {
146                 BOOL invalid_ws = True;
147                 int i;
148                 const char **workstations = str_list_make(mem_ctx, workstation_list, ",");
149                 
150                 for (i = 0; workstations && workstations[i]; i++) {
151                         DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
152                                   workstations[i], logon_workstation));
153
154                         if (strequal(workstations[i], logon_workstation) == 0) {
155                                 invalid_ws = False;
156                                 break;
157                         }
158                 }
159
160                 talloc_free(workstations);
161
162                 if (invalid_ws) {
163                         return NT_STATUS_INVALID_WORKSTATION;
164                 }
165         }
166         
167         if (acct_flags & ACB_DOMTRUST) {
168                 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
169                 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
170         }
171         
172         if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
173                 if (acct_flags & ACB_SVRTRUST) {
174                         DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
175                         return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
176                 }
177         }
178         if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
179                 if (acct_flags & ACB_WSTRUST) {
180                         DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
181                         return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
182                 }
183         }
184
185         return NT_STATUS_OK;
186 }
187
188 _PUBLIC_ NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
189                                   struct ldb_message *msg,
190                                   struct ldb_message *msg_domain_ref,
191                                   DATA_BLOB user_sess_key, DATA_BLOB lm_sess_key,
192                                   struct auth_serversupplied_info **_server_info)
193 {
194         struct auth_serversupplied_info *server_info;
195         struct ldb_message **group_msgs;
196         int group_ret;
197         const char *group_attrs[3] = { "sAMAccountType", "objectSid", NULL }; 
198         /* find list of sids */
199         struct dom_sid **groupSIDs = NULL;
200         struct dom_sid *account_sid;
201         struct dom_sid *primary_group_sid;
202         const char *str;
203         struct ldb_dn *ncname;
204         int i;
205         uint_t rid;
206         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
207
208         group_ret = gendb_search(sam_ctx,
209                                  tmp_ctx, NULL, &group_msgs, group_attrs,
210                                  "(&(member=%s)(sAMAccountType=*))", 
211                                  ldb_dn_get_linearized(msg->dn));
212         if (group_ret == -1) {
213                 talloc_free(tmp_ctx);
214                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
215         }
216
217         server_info = talloc(mem_ctx, struct auth_serversupplied_info);
218         NT_STATUS_HAVE_NO_MEMORY(server_info);
219         
220         if (group_ret > 0) {
221                 groupSIDs = talloc_array(server_info, struct dom_sid *, group_ret);
222                 NT_STATUS_HAVE_NO_MEMORY(groupSIDs);
223         }
224
225         /* Need to unroll some nested groups, but not aliases */
226         for (i = 0; i < group_ret; i++) {
227                 groupSIDs[i] = samdb_result_dom_sid(groupSIDs, 
228                                                     group_msgs[i], "objectSid");
229                 NT_STATUS_HAVE_NO_MEMORY(groupSIDs[i]);
230         }
231
232         talloc_free(tmp_ctx);
233
234         account_sid = samdb_result_dom_sid(server_info, msg, "objectSid");
235         NT_STATUS_HAVE_NO_MEMORY(account_sid);
236
237         primary_group_sid = dom_sid_dup(server_info, account_sid);
238         NT_STATUS_HAVE_NO_MEMORY(primary_group_sid);
239
240         rid = samdb_result_uint(msg, "primaryGroupID", ~0);
241         if (rid == ~0) {
242                 if (group_ret > 0) {
243                         primary_group_sid = groupSIDs[0];
244                 } else {
245                         primary_group_sid = NULL;
246                 }
247         } else {
248                 primary_group_sid->sub_auths[primary_group_sid->num_auths-1] = rid;
249         }
250
251         server_info->account_sid = account_sid;
252         server_info->primary_group_sid = primary_group_sid;
253         
254         server_info->n_domain_groups = group_ret;
255         server_info->domain_groups = groupSIDs;
256
257         server_info->account_name = talloc_steal(server_info, samdb_result_string(msg, "sAMAccountName", NULL));
258
259         server_info->domain_name = talloc_steal(server_info, samdb_result_string(msg_domain_ref, "nETBIOSName", NULL));
260
261         str = samdb_result_string(msg, "displayName", "");
262         server_info->full_name = talloc_strdup(server_info, str);
263         NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
264
265         str = samdb_result_string(msg, "scriptPath", "");
266         server_info->logon_script = talloc_strdup(server_info, str);
267         NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
268
269         str = samdb_result_string(msg, "profilePath", "");
270         server_info->profile_path = talloc_strdup(server_info, str);
271         NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
272
273         str = samdb_result_string(msg, "homeDirectory", "");
274         server_info->home_directory = talloc_strdup(server_info, str);
275         NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
276
277         str = samdb_result_string(msg, "homeDrive", "");
278         server_info->home_drive = talloc_strdup(server_info, str);
279         NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
280
281         server_info->logon_server = talloc_strdup(server_info, lp_netbios_name());
282         NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
283
284         server_info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
285         server_info->last_logoff = samdb_result_nttime(msg, "lastLogoff", 0);
286         server_info->acct_expiry = samdb_result_nttime(msg, "accountExpires", 0);
287         server_info->last_password_change = samdb_result_nttime(msg, "pwdLastSet", 0);
288
289         ncname = samdb_result_dn(sam_ctx, mem_ctx, msg_domain_ref, "nCName", NULL);
290         if (!ncname) {
291                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
292         }
293         server_info->allow_password_change
294                 = samdb_result_allow_password_change(sam_ctx, mem_ctx, 
295                                                      ncname, msg, "pwdLastSet");
296         server_info->force_password_change
297                 = samdb_result_force_password_change(sam_ctx, mem_ctx, 
298                                                      ncname, msg);
299         
300         server_info->logon_count = samdb_result_uint(msg, "logonCount", 0);
301         server_info->bad_password_count = samdb_result_uint(msg, "badPwdCount", 0);
302
303         server_info->acct_flags = samdb_result_acct_flags(msg, "userAccountControl");
304
305         server_info->user_session_key = user_sess_key;
306         server_info->lm_session_key = lm_sess_key;
307
308         server_info->authenticated = True;
309
310         *_server_info = server_info;
311
312         return NT_STATUS_OK;
313 }
314
315 _PUBLIC_ NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
316                                    TALLOC_CTX *mem_ctx, const char *principal,
317                                    struct ldb_message ***msgs,
318                                    struct ldb_message ***msgs_domain_ref)
319 {                          
320         struct ldb_dn *user_dn, *domain_dn;
321         NTSTATUS nt_status;
322         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
323         int ret;
324         struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
325
326         if (!tmp_ctx) {
327                 return NT_STATUS_NO_MEMORY;
328         }
329
330         nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal, &user_dn, &domain_dn);
331         if (!NT_STATUS_IS_OK(nt_status)) {
332                 talloc_free(tmp_ctx);
333                 return nt_status;
334         }
335         
336         /* grab domain info from the reference */
337         ret = gendb_search(sam_ctx, tmp_ctx, partitions_basedn, msgs_domain_ref, domain_ref_attrs,
338                            "(ncName=%s)", ldb_dn_get_linearized(domain_dn));
339
340         if (ret != 1) {
341                 talloc_free(tmp_ctx);
342                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
343         }
344         
345         /* pull the user attributes */
346         ret = gendb_search_dn(sam_ctx, tmp_ctx, user_dn, msgs, user_attrs);
347         if (ret != 1) {
348                 talloc_free(tmp_ctx);
349                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
350         }
351         talloc_steal(mem_ctx, *msgs);
352         talloc_steal(mem_ctx, *msgs_domain_ref);
353         talloc_free(tmp_ctx);
354         
355         return NT_STATUS_OK;
356 }
357                                    
358 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available */
359 NTSTATUS sam_get_server_info_principal(TALLOC_CTX *mem_ctx, const char *principal,
360                                        struct auth_serversupplied_info **server_info)
361 {
362         NTSTATUS nt_status;
363         DATA_BLOB user_sess_key = data_blob(NULL, 0);
364         DATA_BLOB lm_sess_key = data_blob(NULL, 0);
365
366         struct ldb_message **msgs;
367         struct ldb_message **msgs_domain_ref;
368         struct ldb_context *sam_ctx;
369
370         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
371         if (!tmp_ctx) {
372                 return NT_STATUS_NO_MEMORY;
373         }
374
375         sam_ctx = samdb_connect(tmp_ctx, system_session(tmp_ctx));
376         if (sam_ctx == NULL) {
377                 talloc_free(tmp_ctx);
378                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
379         }
380
381         nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal, 
382                                               &msgs, &msgs_domain_ref);
383         if (!NT_STATUS_IS_OK(nt_status)) {
384                 return nt_status;
385         }
386
387         nt_status = authsam_make_server_info(tmp_ctx, sam_ctx, msgs[0], msgs_domain_ref[0],
388                                              user_sess_key, lm_sess_key,
389                                              server_info);
390         if (NT_STATUS_IS_OK(nt_status)) {
391                 talloc_steal(mem_ctx, *server_info);
392         }
393         talloc_free(tmp_ctx);
394         return nt_status;
395 }