auth/auth_sam_reply: do a real copy of strings in auth_convert_user_info_dc_sambaseinfo()
[kai/samba-autobuild/.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 #define _COPY_STRING_TALLOC(src_name, dst_name) do { \
70         if (info->src_name != NULL) {\
71                 sam->dst_name.string = talloc_strdup(sam, info->src_name); \
72                 if (sam->dst_name.string == NULL) { \
73                         return NT_STATUS_NO_MEMORY; \
74                 } \
75         } \
76 } while(0)
77         _COPY_STRING_TALLOC(account_name, account_name);
78         _COPY_STRING_TALLOC(full_name, full_name);
79         _COPY_STRING_TALLOC(logon_script, logon_script);
80         _COPY_STRING_TALLOC(profile_path, profile_path);
81         _COPY_STRING_TALLOC(home_directory, home_directory);
82         _COPY_STRING_TALLOC(home_drive, home_drive);
83         _COPY_STRING_TALLOC(logon_server, logon_server);
84         _COPY_STRING_TALLOC(domain_name, logon_domain);
85 #undef _COPY_STRING_TALLOC
86
87         sam->logon_count = info->logon_count;
88         sam->bad_password_count = info->bad_password_count;
89         sam->groups.count = 0;
90         sam->groups.rids = NULL;
91
92         if (user_info_dc->num_sids > 2) {
93                 size_t i;
94                 sam->groups.rids = talloc_array(sam, struct samr_RidWithAttribute,
95                                                 user_info_dc->num_sids);
96
97                 if (sam->groups.rids == NULL)
98                         return NT_STATUS_NO_MEMORY;
99
100                 for (i=2; i<user_info_dc->num_sids; i++) {
101                         struct dom_sid *group_sid = &user_info_dc->sids[i];
102                         if (!dom_sid_in_domain(sam->domain_sid, group_sid)) {
103                                 /* We handle this elsewhere */
104                                 continue;
105                         }
106                         sam->groups.rids[sam->groups.count].rid =
107                                 group_sid->sub_auths[group_sid->num_auths-1];
108
109                         sam->groups.rids[sam->groups.count].attributes =
110                                 SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
111                         sam->groups.count += 1;
112                 }
113         }
114
115         sam->user_flags = 0; /* w2k3 uses NETLOGON_EXTRA_SIDS | NETLOGON_NTLMV2_ENABLED */
116         if (!user_info_dc->info->authenticated) {
117                 sam->user_flags |= NETLOGON_GUEST;
118         }
119         sam->acct_flags = user_info_dc->info->acct_flags;
120         sam->sub_auth_status = 0;
121         sam->last_successful_logon = 0;
122         sam->last_failed_logon = 0;
123         sam->failed_logon_count = 0;
124         sam->reserved = 0;
125
126         ZERO_STRUCT(sam->key);
127         if (user_info_dc->user_session_key.length == sizeof(sam->key.key)) {
128                 memcpy(sam->key.key, user_info_dc->user_session_key.data, sizeof(sam->key.key));
129         }
130
131         ZERO_STRUCT(sam->LMSessKey);
132         if (user_info_dc->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
133                 memcpy(sam->LMSessKey.key, user_info_dc->lm_session_key.data,
134                        sizeof(sam->LMSessKey.key));
135         }
136
137         *_sam = sam;
138
139         return NT_STATUS_OK;
140 }
141
142 /* Note that the validity of the _sam6 structure is only as long as
143  * the user_info_dc it was generated from */
144 NTSTATUS auth_convert_user_info_dc_saminfo6(TALLOC_CTX *mem_ctx,
145                                             const struct auth_user_info_dc *user_info_dc,
146                                             struct netr_SamInfo6 **_sam6)
147 {
148         NTSTATUS status;
149         struct netr_SamBaseInfo *sam = NULL;
150         struct netr_SamInfo6 *sam6 = NULL;
151         size_t i;
152
153         sam6 = talloc_zero(mem_ctx, struct netr_SamInfo6);
154         if (sam6 == NULL) {
155                 return NT_STATUS_NO_MEMORY;
156         }
157
158         status = auth_convert_user_info_dc_sambaseinfo(sam6, user_info_dc, &sam);
159         if (!NT_STATUS_IS_OK(status)) {
160                 TALLOC_FREE(sam6);
161                 return status;
162         }
163         sam6->base = *sam;
164
165         sam6->sids = talloc_array(sam6, struct netr_SidAttr,
166                                   user_info_dc->num_sids);
167         if (sam6->sids == NULL) {
168                 TALLOC_FREE(sam6);
169                 return NT_STATUS_NO_MEMORY;
170         }
171
172         /* We don't put the user and group SIDs in there */
173         for (i=2; i<user_info_dc->num_sids; i++) {
174                 if (dom_sid_in_domain(sam->domain_sid, &user_info_dc->sids[i])) {
175                         continue;
176                 }
177                 sam6->sids[sam6->sidcount].sid = dom_sid_dup(sam6->sids, &user_info_dc->sids[i]);
178                 if (sam6->sids[sam6->sidcount].sid == NULL) {
179                         TALLOC_FREE(sam6);
180                         return NT_STATUS_NO_MEMORY;
181                 }
182                 sam6->sids[sam6->sidcount].attributes =
183                         SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
184                 sam6->sidcount += 1;
185         }
186         if (sam6->sidcount) {
187                 sam6->base.user_flags |= NETLOGON_EXTRA_SIDS;
188         } else {
189                 sam6->sids = NULL;
190         }
191
192         if (user_info_dc->info->dns_domain_name != NULL) {
193                 sam6->dns_domainname.string = talloc_strdup(sam6,
194                                         user_info_dc->info->dns_domain_name);
195                 if (sam6->dns_domainname.string == NULL) {
196                         TALLOC_FREE(sam6);
197                         return NT_STATUS_NO_MEMORY;
198                 }
199         }
200
201         if (user_info_dc->info->user_principal_name != NULL) {
202                 sam6->principal_name.string = talloc_strdup(sam6,
203                                         user_info_dc->info->user_principal_name);
204                 if (sam6->principal_name.string == NULL) {
205                         TALLOC_FREE(sam6);
206                         return NT_STATUS_NO_MEMORY;
207                 }
208         }
209
210         *_sam6 = sam6;
211         return NT_STATUS_OK;
212 }
213
214 /* Note that the validity of the _sam2 structure is only as long as
215  * the user_info_dc it was generated from */
216 NTSTATUS auth_convert_user_info_dc_saminfo2(TALLOC_CTX *mem_ctx,
217                                            const struct auth_user_info_dc *user_info_dc,
218                                            struct netr_SamInfo2 **_sam2)
219 {
220         NTSTATUS status;
221         struct netr_SamInfo6 *sam6 = NULL;
222         struct netr_SamInfo2 *sam2 = NULL;
223
224         sam2 = talloc_zero(mem_ctx, struct netr_SamInfo2);
225         if (sam2 == NULL) {
226                 return NT_STATUS_NO_MEMORY;
227         }
228
229         status = auth_convert_user_info_dc_saminfo6(sam2, user_info_dc, &sam6);
230         if (!NT_STATUS_IS_OK(status)) {
231                 TALLOC_FREE(sam2);
232                 return status;
233         }
234         sam2->base      = sam6->base;
235
236         *_sam2 = sam2;
237         return NT_STATUS_OK;
238 }
239
240 /* Note that the validity of the _sam3 structure is only as long as
241  * the user_info_dc it was generated from */
242 NTSTATUS auth_convert_user_info_dc_saminfo3(TALLOC_CTX *mem_ctx,
243                                            const struct auth_user_info_dc *user_info_dc,
244                                            struct netr_SamInfo3 **_sam3)
245 {
246         NTSTATUS status;
247         struct netr_SamInfo6 *sam6 = NULL;
248         struct netr_SamInfo3 *sam3 = NULL;
249
250         sam3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
251         if (sam3 == NULL) {
252                 return NT_STATUS_NO_MEMORY;
253         }
254
255         status = auth_convert_user_info_dc_saminfo6(sam3, user_info_dc, &sam6);
256         if (!NT_STATUS_IS_OK(status)) {
257                 TALLOC_FREE(sam3);
258                 return status;
259         }
260         sam3->base      = sam6->base;
261         sam3->sidcount  = sam6->sidcount;
262         sam3->sids      = sam6->sids;
263
264         *_sam3 = sam3;
265         return NT_STATUS_OK;
266 }
267
268 /**
269  * Make a user_info struct from the info3 or similar returned by a domain logon.
270  *
271  * The netr_SamInfo3 is also a key structure in the source3 auth subsystem
272  */
273
274 NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx,
275                                     const char *account_name,
276                                     const struct netr_SamBaseInfo *base,
277                                     bool authenticated,
278                                     struct auth_user_info **_user_info)
279 {
280         struct auth_user_info *info;
281
282         info = talloc_zero(mem_ctx, struct auth_user_info);
283         NT_STATUS_HAVE_NO_MEMORY(info);
284
285         if (base->account_name.string) {
286                 info->account_name = talloc_strdup(info, base->account_name.string);
287         } else {
288                 info->account_name = talloc_strdup(info, account_name);
289         }
290         NT_STATUS_HAVE_NO_MEMORY(info->account_name);
291
292         if (base->logon_domain.string) {
293                 info->domain_name = talloc_strdup(info, base->logon_domain.string);
294                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
295         }
296
297         if (base->full_name.string) {
298                 info->full_name = talloc_strdup(info, base->full_name.string);
299                 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
300         }
301         if (base->logon_script.string) {
302                 info->logon_script = talloc_strdup(info, base->logon_script.string);
303                 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
304         }
305         if (base->profile_path.string) {
306                 info->profile_path = talloc_strdup(info, base->profile_path.string);
307                 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
308         }
309         if (base->home_directory.string) {
310                 info->home_directory = talloc_strdup(info, base->home_directory.string);
311                 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
312         }
313         if (base->home_drive.string) {
314                 info->home_drive = talloc_strdup(info, base->home_drive.string);
315                 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
316         }
317         if (base->logon_server.string) {
318                 info->logon_server = talloc_strdup(info, base->logon_server.string);
319                 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
320         }
321         info->last_logon = base->logon_time;
322         info->last_logoff = base->logoff_time;
323         info->acct_expiry = base->kickoff_time;
324         info->last_password_change = base->last_password_change;
325         info->allow_password_change = base->allow_password_change;
326         info->force_password_change = base->force_password_change;
327         info->logon_count = base->logon_count;
328         info->bad_password_count = base->bad_password_count;
329         info->acct_flags = base->acct_flags;
330
331         /* Only set authenticated if both NETLOGON_GUEST is not set, and authenticated is set */
332         info->authenticated = (authenticated && (!(base->user_flags & NETLOGON_GUEST)));
333
334         *_user_info = info;
335         return NT_STATUS_OK;
336 }
337
338 /**
339  * Make a user_info_dc struct from the info3 returned by a domain logon
340  */
341 NTSTATUS make_user_info_dc_netlogon_validation(TALLOC_CTX *mem_ctx,
342                                               const char *account_name,
343                                               uint16_t validation_level,
344                                               const union netr_Validation *validation,
345                                                bool authenticated,
346                                               struct auth_user_info_dc **_user_info_dc)
347 {
348         NTSTATUS status;
349         struct auth_user_info_dc *user_info_dc = NULL;
350         const struct netr_SamBaseInfo *base = NULL;
351         uint32_t sidcount = 0;
352         const struct netr_SidAttr *sids = NULL;
353         const char *dns_domainname = NULL;
354         const char *principal = NULL;
355         uint32_t i;
356
357         switch (validation_level) {
358         case 2:
359                 if (!validation || !validation->sam2) {
360                         return NT_STATUS_INVALID_PARAMETER;
361                 }
362                 base = &validation->sam2->base;
363                 break;
364         case 3:
365                 if (!validation || !validation->sam3) {
366                         return NT_STATUS_INVALID_PARAMETER;
367                 }
368                 base = &validation->sam3->base;
369                 sidcount = validation->sam3->sidcount;
370                 sids = validation->sam3->sids;
371                 break;
372         case 6:
373                 if (!validation || !validation->sam6) {
374                         return NT_STATUS_INVALID_PARAMETER;
375                 }
376                 base = &validation->sam6->base;
377                 sidcount = validation->sam6->sidcount;
378                 sids = validation->sam6->sids;
379                 dns_domainname = validation->sam6->dns_domainname.string;
380                 principal = validation->sam6->principal_name.string;
381                 break;
382         default:
383                 return NT_STATUS_INVALID_LEVEL;
384         }
385
386         user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
387         NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
388
389         /*
390            Here is where we should check the list of
391            trusted domains, and verify that the SID
392            matches.
393         */
394         if (!base->domain_sid) {
395                 DEBUG(0, ("Cannot operate on a Netlogon Validation without a domain SID"));
396                 return NT_STATUS_INVALID_PARAMETER;
397         }
398
399         /* The IDL layer would be a better place to check this, but to
400          * guard the integer addition below, we double-check */
401         if (base->groups.count > 65535) {
402                 return NT_STATUS_INVALID_PARAMETER;
403         }
404
405         user_info_dc->num_sids = 2;
406
407         user_info_dc->sids = talloc_array(user_info_dc, struct dom_sid,  user_info_dc->num_sids + base->groups.count);
408         NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids);
409
410         user_info_dc->sids[PRIMARY_USER_SID_INDEX] = *base->domain_sid;
411         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_USER_SID_INDEX], base->rid)) {
412                 return NT_STATUS_INVALID_PARAMETER;
413         }
414
415         user_info_dc->sids[PRIMARY_GROUP_SID_INDEX] = *base->domain_sid;
416         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], base->primary_gid)) {
417                 return NT_STATUS_INVALID_PARAMETER;
418         }
419
420         for (i = 0; i < base->groups.count; i++) {
421                 user_info_dc->sids[user_info_dc->num_sids] = *base->domain_sid;
422                 if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids], base->groups.rids[i].rid)) {
423                         return NT_STATUS_INVALID_PARAMETER;
424                 }
425                 user_info_dc->num_sids++;
426         }
427
428         /* Copy 'other' sids.  We need to do sid filtering here to
429            prevent possible elevation of privileges.  See:
430
431            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
432          */
433
434         /*
435          * The IDL layer would be a better place to check this, but to
436          * guard the integer addition below, we double-check
437          */
438         if (sidcount > UINT16_MAX) {
439                 return NT_STATUS_INVALID_PARAMETER;
440         }
441
442         if (sidcount > 0) {
443                 struct dom_sid *dgrps = user_info_dc->sids;
444                 size_t dgrps_count;
445
446                 dgrps_count = user_info_dc->num_sids + sidcount;
447                 dgrps = talloc_realloc(user_info_dc, dgrps, struct dom_sid,
448                                        dgrps_count);
449                 if (dgrps == NULL) {
450                         return NT_STATUS_NO_MEMORY;
451                 }
452
453                 for (i = 0; i < sidcount; i++) {
454                         if (sids[i].sid) {
455                                 dgrps[user_info_dc->num_sids] = *sids[i].sid;
456                                 user_info_dc->num_sids++;
457                         }
458                 }
459
460                 user_info_dc->sids = dgrps;
461
462                 /* Where are the 'global' sids?... */
463         }
464
465         status = make_user_info_SamBaseInfo(user_info_dc, account_name, base, authenticated, &user_info_dc->info);
466         if (!NT_STATUS_IS_OK(status)) {
467                 return status;
468         }
469
470         if (dns_domainname != NULL) {
471                 user_info_dc->info->dns_domain_name = talloc_strdup(user_info_dc->info,
472                                                                     dns_domainname);
473                 if (user_info_dc->info->dns_domain_name == NULL) {
474                         return NT_STATUS_NO_MEMORY;
475                 }
476         }
477
478         if (principal != NULL) {
479                 user_info_dc->info->user_principal_name = talloc_strdup(user_info_dc->info,
480                                                                         principal);
481                 if (user_info_dc->info->user_principal_name == NULL) {
482                         return NT_STATUS_NO_MEMORY;
483                 }
484         }
485
486         /* ensure we are never given NULL session keys */
487
488         if (all_zero(base->key.key, sizeof(base->key.key))) {
489                 user_info_dc->user_session_key = data_blob(NULL, 0);
490         } else {
491                 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, base->key.key, sizeof(base->key.key));
492                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
493         }
494
495         if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
496                 user_info_dc->lm_session_key = data_blob(NULL, 0);
497         } else {
498                 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, base->LMSessKey.key, sizeof(base->LMSessKey.key));
499                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
500         }
501
502         *_user_info_dc = user_info_dc;
503         return NT_STATUS_OK;
504 }
505
506 /**
507  * Make a user_info_dc struct from the PAC_LOGON_INFO supplied in the krb5 logon
508  */
509 NTSTATUS make_user_info_dc_pac(TALLOC_CTX *mem_ctx,
510                               const struct PAC_LOGON_INFO *pac_logon_info,
511                               struct auth_user_info_dc **_user_info_dc)
512 {
513         uint32_t i;
514         NTSTATUS nt_status;
515         union netr_Validation validation;
516         struct auth_user_info_dc *user_info_dc;
517
518         validation.sam3 = discard_const_p(struct netr_SamInfo3, &pac_logon_info->info3);
519
520         nt_status = make_user_info_dc_netlogon_validation(mem_ctx, "", 3, &validation,
521                                                           true, /* This user was authenticated */
522                                                           &user_info_dc);
523         if (!NT_STATUS_IS_OK(nt_status)) {
524                 return nt_status;
525         }
526
527         if (pac_logon_info->res_groups.count > 0) {
528                 size_t sidcount;
529                 /* The IDL layer would be a better place to check this, but to
530                  * guard the integer addition below, we double-check */
531                 if (pac_logon_info->res_groups.count > 65535) {
532                         talloc_free(user_info_dc);
533                         return NT_STATUS_INVALID_PARAMETER;
534                 }
535
536                 /*
537                   Here is where we should check the list of
538                   trusted domains, and verify that the SID
539                   matches.
540                 */
541                 if (!pac_logon_info->res_group_dom_sid) {
542                         DEBUG(0, ("Cannot operate on a PAC without a resource domain SID"));
543                         return NT_STATUS_INVALID_PARAMETER;
544                 }
545
546                 sidcount = user_info_dc->num_sids + pac_logon_info->res_groups.count;
547                 user_info_dc->sids
548                         = talloc_realloc(user_info_dc, user_info_dc->sids, struct dom_sid, sidcount);
549                 if (user_info_dc->sids == NULL) {
550                         TALLOC_FREE(user_info_dc);
551                         return NT_STATUS_NO_MEMORY;
552                 }
553
554                 for (i = 0; pac_logon_info->res_group_dom_sid && i < pac_logon_info->res_groups.count; i++) {
555                         user_info_dc->sids[user_info_dc->num_sids] = *pac_logon_info->res_group_dom_sid;
556                         if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
557                                             pac_logon_info->res_groups.rids[i].rid)) {
558                                 return NT_STATUS_INVALID_PARAMETER;
559                         }
560                         user_info_dc->num_sids++;
561                 }
562         }
563         *_user_info_dc = user_info_dc;
564         return NT_STATUS_OK;
565 }