5602daf62de2fa39bfa9a61d907e3516f83d0bbd
[samba.git] / auth / auth_sam_reply.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Convert a server info struct into the form for PAC and NETLOGON replies
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2011
7    Copyright (C) Stefan Metzmacher <metze@samba.org>  2005
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/auth.h"
25 #include "libcli/security/security.h"
26 #include "auth/auth_sam_reply.h"
27
28 NTSTATUS auth_convert_user_info_dc_sambaseinfo(TALLOC_CTX *mem_ctx,
29                                               const struct auth_user_info_dc *user_info_dc,
30                                               struct netr_SamBaseInfo **_sam)
31 {
32         NTSTATUS status;
33         struct auth_user_info *info;
34         struct netr_SamBaseInfo *sam = talloc_zero(mem_ctx, struct netr_SamBaseInfo);
35         NT_STATUS_HAVE_NO_MEMORY(sam);
36
37         if (user_info_dc->num_sids > PRIMARY_USER_SID_INDEX) {
38                 status = dom_sid_split_rid(sam, &user_info_dc->sids[PRIMARY_USER_SID_INDEX],
39                                            &sam->domain_sid, &sam->rid);
40                 if (!NT_STATUS_IS_OK(status)) {
41                         return status;
42                 }
43         } else {
44                 return NT_STATUS_INVALID_PARAMETER;
45         }
46
47         if (user_info_dc->num_sids > PRIMARY_GROUP_SID_INDEX) {
48                 status = dom_sid_split_rid(NULL, &user_info_dc->sids[PRIMARY_GROUP_SID_INDEX],
49                                            NULL, &sam->primary_gid);
50                 if (!NT_STATUS_IS_OK(status)) {
51                         return status;
52                 }
53         } else {
54                 /* if we have to encode something like SYSTEM (with no
55                  * second SID in the token) then this is the only
56                  * choice */
57                 sam->primary_gid = sam->rid;
58         }
59
60         info = user_info_dc->info;
61
62         sam->logon_time = info->last_logon;
63         sam->logoff_time =  info->last_logoff;
64         sam->kickoff_time = info->acct_expiry;
65         sam->last_password_change = info->last_password_change;
66         sam->allow_password_change = info->allow_password_change;
67         sam->force_password_change = info->force_password_change;
68
69         sam->account_name.string = info->account_name;
70         sam->full_name.string = info->full_name;
71         sam->logon_script.string = info->logon_script;
72         sam->profile_path.string = info->profile_path;
73         sam->home_directory.string = info->home_directory;
74         sam->home_drive.string = info->home_drive;
75
76         sam->logon_count = info->logon_count;
77         sam->bad_password_count = info->bad_password_count;
78         sam->groups.count = 0;
79         sam->groups.rids = NULL;
80
81         if (user_info_dc->num_sids > 2) {
82                 size_t i;
83                 sam->groups.rids = talloc_array(sam, struct samr_RidWithAttribute,
84                                                 user_info_dc->num_sids);
85
86                 if (sam->groups.rids == NULL)
87                         return NT_STATUS_NO_MEMORY;
88
89                 for (i=2; i<user_info_dc->num_sids; i++) {
90                         struct dom_sid *group_sid = &user_info_dc->sids[i];
91                         if (!dom_sid_in_domain(sam->domain_sid, group_sid)) {
92                                 /* We handle this elsewhere */
93                                 continue;
94                         }
95                         sam->groups.rids[sam->groups.count].rid =
96                                 group_sid->sub_auths[group_sid->num_auths-1];
97
98                         sam->groups.rids[sam->groups.count].attributes =
99                                 SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
100                         sam->groups.count += 1;
101                 }
102         }
103
104         sam->user_flags = 0; /* w2k3 uses NETLOGON_EXTRA_SIDS | NETLOGON_NTLMV2_ENABLED */
105         if (!user_info_dc->info->authenticated) {
106                 sam->user_flags |= NETLOGON_GUEST;
107         }
108         sam->acct_flags = user_info_dc->info->acct_flags;
109         sam->logon_server.string = user_info_dc->info->logon_server;
110         sam->logon_domain.string = user_info_dc->info->domain_name;
111         sam->sub_auth_status = 0;
112         sam->last_successful_logon = 0;
113         sam->last_failed_logon = 0;
114         sam->failed_logon_count = 0;
115         sam->reserved = 0;
116
117         ZERO_STRUCT(sam->key);
118         if (user_info_dc->user_session_key.length == sizeof(sam->key.key)) {
119                 memcpy(sam->key.key, user_info_dc->user_session_key.data, sizeof(sam->key.key));
120         }
121
122         ZERO_STRUCT(sam->LMSessKey);
123         if (user_info_dc->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
124                 memcpy(sam->LMSessKey.key, user_info_dc->lm_session_key.data,
125                        sizeof(sam->LMSessKey.key));
126         }
127
128         *_sam = sam;
129
130         return NT_STATUS_OK;
131 }
132
133 /* Note that the validity of the _sam6 structure is only as long as
134  * the user_info_dc it was generated from */
135 NTSTATUS auth_convert_user_info_dc_saminfo6(TALLOC_CTX *mem_ctx,
136                                             const struct auth_user_info_dc *user_info_dc,
137                                             struct netr_SamInfo6 **_sam6)
138 {
139         NTSTATUS status;
140         struct netr_SamBaseInfo *sam = NULL;
141         struct netr_SamInfo6 *sam6 = NULL;
142         size_t i;
143
144         sam6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
145         if (sam6 == NULL) {
146                 return NT_STATUS_NO_MEMORY;
147         }
148
149         status = auth_convert_user_info_dc_sambaseinfo(sam6, user_info_dc, &sam);
150         if (!NT_STATUS_IS_OK(status)) {
151                 TALLOC_FREE(sam6);
152                 return status;
153         }
154         sam6->base = *sam;
155
156         sam6->sids = talloc_array(sam6, struct netr_SidAttr,
157                                   user_info_dc->num_sids);
158         if (sam6->sids == NULL) {
159                 TALLOC_FREE(sam6);
160                 return NT_STATUS_NO_MEMORY;
161         }
162
163         /* We don't put the user and group SIDs in there */
164         for (i=2; i<user_info_dc->num_sids; i++) {
165                 if (dom_sid_in_domain(sam->domain_sid, &user_info_dc->sids[i])) {
166                         continue;
167                 }
168                 sam6->sids[sam6->sidcount].sid = dom_sid_dup(sam6->sids, &user_info_dc->sids[i]);
169                 if (sam6->sids[sam6->sidcount].sid == NULL) {
170                         TALLOC_FREE(sam6);
171                         return NT_STATUS_NO_MEMORY;
172                 }
173                 sam6->sids[sam6->sidcount].attributes =
174                         SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
175                 sam6->sidcount += 1;
176         }
177         if (sam6->sidcount) {
178                 sam6->base.user_flags |= NETLOGON_EXTRA_SIDS;
179         } else {
180                 sam6->sids = NULL;
181         }
182
183         if (user_info_dc->info->dns_domain_name != NULL) {
184                 sam6->dns_domainname.string = talloc_strdup(sam6,
185                                         user_info_dc->info->dns_domain_name);
186                 if (sam6->dns_domainname.string == NULL) {
187                         TALLOC_FREE(sam6);
188                         return NT_STATUS_NO_MEMORY;
189                 }
190         }
191
192         if (user_info_dc->info->user_principal_name != NULL) {
193                 sam6->principal_name.string = talloc_strdup(sam6,
194                                         user_info_dc->info->user_principal_name);
195                 if (sam6->principal_name.string == NULL) {
196                         TALLOC_FREE(sam6);
197                         return NT_STATUS_NO_MEMORY;
198                 }
199         }
200
201         *_sam6 = sam6;
202         return NT_STATUS_OK;
203 }
204
205 /* Note that the validity of the _sam3 structure is only as long as
206  * the user_info_dc it was generated from */
207 NTSTATUS auth_convert_user_info_dc_saminfo3(TALLOC_CTX *mem_ctx,
208                                            const struct auth_user_info_dc *user_info_dc,
209                                            struct netr_SamInfo3 **_sam3)
210 {
211         NTSTATUS status;
212         struct netr_SamInfo6 *sam6 = NULL;
213         struct netr_SamInfo3 *sam3 = NULL;
214
215         sam3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
216         if (sam3 == NULL) {
217                 return NT_STATUS_NO_MEMORY;
218         }
219
220         status = auth_convert_user_info_dc_saminfo6(sam3, user_info_dc, &sam6);
221         if (!NT_STATUS_IS_OK(status)) {
222                 TALLOC_FREE(sam3);
223                 return status;
224         }
225         sam3->base      = sam6->base;
226         sam3->sidcount  = sam6->sidcount;
227         sam3->sids      = sam6->sids;
228
229         *_sam3 = sam3;
230         return NT_STATUS_OK;
231 }
232
233 /**
234  * Make a user_info struct from the info3 or similar returned by a domain logon.
235  *
236  * The netr_SamInfo3 is also a key structure in the source3 auth subsystem
237  */
238
239 NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx,
240                                     const char *account_name,
241                                     const struct netr_SamBaseInfo *base,
242                                     bool authenticated,
243                                     struct auth_user_info **_user_info)
244 {
245         struct auth_user_info *info;
246
247         info = talloc_zero(mem_ctx, struct auth_user_info);
248         NT_STATUS_HAVE_NO_MEMORY(info);
249
250         if (base->account_name.string) {
251                 info->account_name = talloc_strdup(info, base->account_name.string);
252         } else {
253                 info->account_name = talloc_strdup(info, account_name);
254         }
255         NT_STATUS_HAVE_NO_MEMORY(info->account_name);
256
257         if (base->logon_domain.string) {
258                 info->domain_name = talloc_strdup(info, base->logon_domain.string);
259                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
260         }
261
262         if (base->full_name.string) {
263                 info->full_name = talloc_strdup(info, base->full_name.string);
264                 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
265         }
266         if (base->logon_script.string) {
267                 info->logon_script = talloc_strdup(info, base->logon_script.string);
268                 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
269         }
270         if (base->profile_path.string) {
271                 info->profile_path = talloc_strdup(info, base->profile_path.string);
272                 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
273         }
274         if (base->home_directory.string) {
275                 info->home_directory = talloc_strdup(info, base->home_directory.string);
276                 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
277         }
278         if (base->home_drive.string) {
279                 info->home_drive = talloc_strdup(info, base->home_drive.string);
280                 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
281         }
282         if (base->logon_server.string) {
283                 info->logon_server = talloc_strdup(info, base->logon_server.string);
284                 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
285         }
286         info->last_logon = base->logon_time;
287         info->last_logoff = base->logoff_time;
288         info->acct_expiry = base->kickoff_time;
289         info->last_password_change = base->last_password_change;
290         info->allow_password_change = base->allow_password_change;
291         info->force_password_change = base->force_password_change;
292         info->logon_count = base->logon_count;
293         info->bad_password_count = base->bad_password_count;
294         info->acct_flags = base->acct_flags;
295
296         /* Only set authenticated if both NETLOGON_GUEST is not set, and authenticated is set */
297         info->authenticated = (authenticated && (!(base->user_flags & NETLOGON_GUEST)));
298
299         *_user_info = info;
300         return NT_STATUS_OK;
301 }
302
303 /**
304  * Make a user_info_dc struct from the info3 returned by a domain logon
305  */
306 NTSTATUS make_user_info_dc_netlogon_validation(TALLOC_CTX *mem_ctx,
307                                               const char *account_name,
308                                               uint16_t validation_level,
309                                               const union netr_Validation *validation,
310                                                bool authenticated,
311                                               struct auth_user_info_dc **_user_info_dc)
312 {
313         NTSTATUS status;
314         struct auth_user_info_dc *user_info_dc = NULL;
315         const struct netr_SamBaseInfo *base = NULL;
316         uint32_t sidcount = 0;
317         const struct netr_SidAttr *sids = NULL;
318         const char *dns_domainname = NULL;
319         const char *principal = NULL;
320         uint32_t i;
321
322         switch (validation_level) {
323         case 2:
324                 if (!validation || !validation->sam2) {
325                         return NT_STATUS_INVALID_PARAMETER;
326                 }
327                 base = &validation->sam2->base;
328                 break;
329         case 3:
330                 if (!validation || !validation->sam3) {
331                         return NT_STATUS_INVALID_PARAMETER;
332                 }
333                 base = &validation->sam3->base;
334                 sidcount = validation->sam3->sidcount;
335                 sids = validation->sam3->sids;
336                 break;
337         case 6:
338                 if (!validation || !validation->sam6) {
339                         return NT_STATUS_INVALID_PARAMETER;
340                 }
341                 base = &validation->sam6->base;
342                 sidcount = validation->sam6->sidcount;
343                 sids = validation->sam6->sids;
344                 dns_domainname = validation->sam6->dns_domainname.string;
345                 principal = validation->sam6->principal_name.string;
346                 break;
347         default:
348                 return NT_STATUS_INVALID_LEVEL;
349         }
350
351         user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
352         NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
353
354         /*
355            Here is where we should check the list of
356            trusted domains, and verify that the SID
357            matches.
358         */
359         if (!base->domain_sid) {
360                 DEBUG(0, ("Cannot operate on a Netlogon Validation without a domain SID"));
361                 return NT_STATUS_INVALID_PARAMETER;
362         }
363
364         /* The IDL layer would be a better place to check this, but to
365          * guard the integer addition below, we double-check */
366         if (base->groups.count > 65535) {
367                 return NT_STATUS_INVALID_PARAMETER;
368         }
369
370         user_info_dc->num_sids = 2;
371
372         user_info_dc->sids = talloc_array(user_info_dc, struct dom_sid,  user_info_dc->num_sids + base->groups.count);
373         NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids);
374
375         user_info_dc->sids[PRIMARY_USER_SID_INDEX] = *base->domain_sid;
376         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_USER_SID_INDEX], base->rid)) {
377                 return NT_STATUS_INVALID_PARAMETER;
378         }
379
380         user_info_dc->sids[PRIMARY_GROUP_SID_INDEX] = *base->domain_sid;
381         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], base->primary_gid)) {
382                 return NT_STATUS_INVALID_PARAMETER;
383         }
384
385         for (i = 0; i < base->groups.count; i++) {
386                 user_info_dc->sids[user_info_dc->num_sids] = *base->domain_sid;
387                 if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids], base->groups.rids[i].rid)) {
388                         return NT_STATUS_INVALID_PARAMETER;
389                 }
390                 user_info_dc->num_sids++;
391         }
392
393         /* Copy 'other' sids.  We need to do sid filtering here to
394            prevent possible elevation of privileges.  See:
395
396            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
397          */
398
399         /*
400          * The IDL layer would be a better place to check this, but to
401          * guard the integer addition below, we double-check
402          */
403         if (sidcount > UINT16_MAX) {
404                 return NT_STATUS_INVALID_PARAMETER;
405         }
406
407         if (sidcount > 0) {
408                 struct dom_sid *dgrps = user_info_dc->sids;
409                 size_t dgrps_count;
410
411                 dgrps_count = user_info_dc->num_sids + sidcount;
412                 dgrps = talloc_realloc(user_info_dc, dgrps, struct dom_sid,
413                                        dgrps_count);
414                 if (dgrps == NULL) {
415                         return NT_STATUS_NO_MEMORY;
416                 }
417
418                 for (i = 0; i < sidcount; i++) {
419                         if (sids[i].sid) {
420                                 dgrps[user_info_dc->num_sids] = *sids[i].sid;
421                                 user_info_dc->num_sids++;
422                         }
423                 }
424
425                 user_info_dc->sids = dgrps;
426
427                 /* Where are the 'global' sids?... */
428         }
429
430         status = make_user_info_SamBaseInfo(user_info_dc, account_name, base, authenticated, &user_info_dc->info);
431         if (!NT_STATUS_IS_OK(status)) {
432                 return status;
433         }
434
435         if (dns_domainname != NULL) {
436                 user_info_dc->info->dns_domain_name = talloc_strdup(user_info_dc->info,
437                                                                     dns_domainname);
438                 if (user_info_dc->info->dns_domain_name == NULL) {
439                         return NT_STATUS_NO_MEMORY;
440                 }
441         }
442
443         if (principal != NULL) {
444                 user_info_dc->info->user_principal_name = talloc_strdup(user_info_dc->info,
445                                                                         principal);
446                 if (user_info_dc->info->user_principal_name == NULL) {
447                         return NT_STATUS_NO_MEMORY;
448                 }
449         }
450
451         /* ensure we are never given NULL session keys */
452
453         if (all_zero(base->key.key, sizeof(base->key.key))) {
454                 user_info_dc->user_session_key = data_blob(NULL, 0);
455         } else {
456                 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, base->key.key, sizeof(base->key.key));
457                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
458         }
459
460         if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
461                 user_info_dc->lm_session_key = data_blob(NULL, 0);
462         } else {
463                 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, base->LMSessKey.key, sizeof(base->LMSessKey.key));
464                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
465         }
466
467         *_user_info_dc = user_info_dc;
468         return NT_STATUS_OK;
469 }
470
471 /**
472  * Make a user_info_dc struct from the PAC_LOGON_INFO supplied in the krb5 logon
473  */
474 NTSTATUS make_user_info_dc_pac(TALLOC_CTX *mem_ctx,
475                               const struct PAC_LOGON_INFO *pac_logon_info,
476                               struct auth_user_info_dc **_user_info_dc)
477 {
478         uint32_t i;
479         NTSTATUS nt_status;
480         union netr_Validation validation;
481         struct auth_user_info_dc *user_info_dc;
482
483         validation.sam3 = discard_const_p(struct netr_SamInfo3, &pac_logon_info->info3);
484
485         nt_status = make_user_info_dc_netlogon_validation(mem_ctx, "", 3, &validation,
486                                                           true, /* This user was authenticated */
487                                                           &user_info_dc);
488         if (!NT_STATUS_IS_OK(nt_status)) {
489                 return nt_status;
490         }
491
492         if (pac_logon_info->res_groups.count > 0) {
493                 size_t sidcount;
494                 /* The IDL layer would be a better place to check this, but to
495                  * guard the integer addition below, we double-check */
496                 if (pac_logon_info->res_groups.count > 65535) {
497                         talloc_free(user_info_dc);
498                         return NT_STATUS_INVALID_PARAMETER;
499                 }
500
501                 /*
502                   Here is where we should check the list of
503                   trusted domains, and verify that the SID
504                   matches.
505                 */
506                 if (!pac_logon_info->res_group_dom_sid) {
507                         DEBUG(0, ("Cannot operate on a PAC without a resource domain SID"));
508                         return NT_STATUS_INVALID_PARAMETER;
509                 }
510
511                 sidcount = user_info_dc->num_sids + pac_logon_info->res_groups.count;
512                 user_info_dc->sids
513                         = talloc_realloc(user_info_dc, user_info_dc->sids, struct dom_sid, sidcount);
514                 if (user_info_dc->sids == NULL) {
515                         TALLOC_FREE(user_info_dc);
516                         return NT_STATUS_NO_MEMORY;
517                 }
518
519                 for (i = 0; pac_logon_info->res_group_dom_sid && i < pac_logon_info->res_groups.count; i++) {
520                         user_info_dc->sids[user_info_dc->num_sids] = *pac_logon_info->res_group_dom_sid;
521                         if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
522                                             pac_logon_info->res_groups.rids[i].rid)) {
523                                 return NT_STATUS_INVALID_PARAMETER;
524                         }
525                         user_info_dc->num_sids++;
526                 }
527         }
528         *_user_info_dc = user_info_dc;
529         return NT_STATUS_OK;
530 }