auth: Move make_user_info_SamBaseInfo() to talloc_strdup and out of memory checking
[obnox/samba/samba-obnox.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                                               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->last_logon = info->last_logon;
63         sam->last_logoff =  info->last_logoff;
64         sam->acct_expiry = 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->domain.string = user_info_dc->info->domain_name;
111
112         ZERO_STRUCT(sam->unknown);
113
114         ZERO_STRUCT(sam->key);
115         if (user_info_dc->user_session_key.length == sizeof(sam->key.key)) {
116                 memcpy(sam->key.key, user_info_dc->user_session_key.data, sizeof(sam->key.key));
117         }
118
119         ZERO_STRUCT(sam->LMSessKey);
120         if (user_info_dc->lm_session_key.length == sizeof(sam->LMSessKey.key)) {
121                 memcpy(sam->LMSessKey.key, user_info_dc->lm_session_key.data,
122                        sizeof(sam->LMSessKey.key));
123         }
124
125         *_sam = sam;
126
127         return NT_STATUS_OK;
128 }
129
130 /* Note that the validity of the _sam3 structure is only as long as
131  * the user_info_dc it was generated from */
132 NTSTATUS auth_convert_user_info_dc_saminfo3(TALLOC_CTX *mem_ctx,
133                                            struct auth_user_info_dc *user_info_dc,
134                                            struct netr_SamInfo3 **_sam3)
135 {
136         struct netr_SamBaseInfo *sam;
137         struct netr_SamInfo3 *sam3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
138         NTSTATUS status;
139         size_t i;
140         NT_STATUS_HAVE_NO_MEMORY(sam3);
141
142         status = auth_convert_user_info_dc_sambaseinfo(sam3, user_info_dc, &sam);
143         if (!NT_STATUS_IS_OK(status)) {
144                 talloc_free(sam3);
145                 return status;
146         }
147         sam3->base = *sam;
148         sam3->sidcount  = 0;
149         sam3->sids      = NULL;
150
151
152         sam3->sids = talloc_array(sam, struct netr_SidAttr,
153                                   user_info_dc->num_sids);
154         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sam3->sids, sam3);
155
156         /* We don't put the user and group SIDs in there */
157         for (i=2; i<user_info_dc->num_sids; i++) {
158                 if (dom_sid_in_domain(sam->domain_sid, &user_info_dc->sids[i])) {
159                         continue;
160                 }
161                 sam3->sids[sam3->sidcount].sid = dom_sid_dup(sam3->sids, &user_info_dc->sids[i]);
162                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sam3->sids[sam3->sidcount].sid, sam3);
163                 sam3->sids[sam3->sidcount].attributes =
164                         SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
165                 sam3->sidcount += 1;
166         }
167         if (sam3->sidcount) {
168                 sam3->base.user_flags |= NETLOGON_EXTRA_SIDS;
169         } else {
170                 sam3->sids = NULL;
171         }
172         *_sam3 = sam3;
173
174         return NT_STATUS_OK;
175 }
176
177 /**
178  * Make a user_info struct from the info3 or similar returned by a domain logon.
179  *
180  * The netr_SamInfo3 is also a key structure in the source3 auth subsystem
181  */
182
183 NTSTATUS make_user_info_SamBaseInfo(TALLOC_CTX *mem_ctx,
184                                     const char *account_name,
185                                     struct netr_SamBaseInfo *base,
186                                     bool authenticated,
187                                     struct auth_user_info **_user_info)
188 {
189         struct auth_user_info *info;
190
191         info = talloc_zero(mem_ctx, struct auth_user_info);
192         NT_STATUS_HAVE_NO_MEMORY(info);
193
194         if (base->account_name.string) {
195                 info->account_name = talloc_strdup(info, base->account_name.string);
196         } else {
197                 info->account_name = talloc_strdup(info, account_name);
198         }
199         NT_STATUS_HAVE_NO_MEMORY(info->account_name);
200
201         if (base->domain.string) {
202                 info->domain_name = talloc_strdup(info, base->domain.string);
203                 NT_STATUS_HAVE_NO_MEMORY(info->domain_name);
204         }
205
206         if (base->full_name.string) {
207                 info->full_name = talloc_strdup(info, base->full_name.string);
208                 NT_STATUS_HAVE_NO_MEMORY(info->full_name);
209         }
210         if (base->logon_script.string) {
211                 info->logon_script = talloc_strdup(info, base->logon_script.string);
212                 NT_STATUS_HAVE_NO_MEMORY(info->logon_script);
213         }
214         if (base->profile_path.string) {
215                 info->profile_path = talloc_strdup(info, base->profile_path.string);
216                 NT_STATUS_HAVE_NO_MEMORY(info->profile_path);
217         }
218         if (base->home_directory.string) {
219                 info->home_directory = talloc_strdup(info, base->home_directory.string);
220                 NT_STATUS_HAVE_NO_MEMORY(info->home_directory);
221         }
222         if (base->home_drive.string) {
223                 info->home_drive = talloc_strdup(info, base->home_drive.string);
224                 NT_STATUS_HAVE_NO_MEMORY(info->home_drive);
225         }
226         if (base->logon_server.string) {
227                 info->logon_server = talloc_strdup(info, base->logon_server.string);
228                 NT_STATUS_HAVE_NO_MEMORY(info->logon_server);
229         }
230         info->last_logon = base->last_logon;
231         info->last_logoff = base->last_logoff;
232         info->acct_expiry = base->acct_expiry;
233         info->last_password_change = base->last_password_change;
234         info->allow_password_change = base->allow_password_change;
235         info->force_password_change = base->force_password_change;
236         info->logon_count = base->logon_count;
237         info->bad_password_count = base->bad_password_count;
238         info->acct_flags = base->acct_flags;
239
240         info->authenticated = authenticated;
241
242         *_user_info = info;
243         return NT_STATUS_OK;
244 }
245
246 /**
247  * Make a user_info_dc struct from the info3 returned by a domain logon
248  */
249 NTSTATUS make_user_info_dc_netlogon_validation(TALLOC_CTX *mem_ctx,
250                                               const char *account_name,
251                                               uint16_t validation_level,
252                                               union netr_Validation *validation,
253                                                bool authenticated,
254                                               struct auth_user_info_dc **_user_info_dc)
255 {
256         NTSTATUS status;
257         struct auth_user_info_dc *user_info_dc;
258         struct netr_SamBaseInfo *base = NULL;
259         uint32_t i;
260
261         switch (validation_level) {
262         case 2:
263                 if (!validation || !validation->sam2) {
264                         return NT_STATUS_INVALID_PARAMETER;
265                 }
266                 base = &validation->sam2->base;
267                 break;
268         case 3:
269                 if (!validation || !validation->sam3) {
270                         return NT_STATUS_INVALID_PARAMETER;
271                 }
272                 base = &validation->sam3->base;
273                 break;
274         case 6:
275                 if (!validation || !validation->sam6) {
276                         return NT_STATUS_INVALID_PARAMETER;
277                 }
278                 base = &validation->sam6->base;
279                 break;
280         default:
281                 return NT_STATUS_INVALID_LEVEL;
282         }
283
284         user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
285         NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
286
287         /*
288            Here is where we should check the list of
289            trusted domains, and verify that the SID
290            matches.
291         */
292         if (!base->domain_sid) {
293                 DEBUG(0, ("Cannot operate on a Netlogon Validation without a domain SID"));
294                 return NT_STATUS_INVALID_PARAMETER;
295         }
296
297         /* The IDL layer would be a better place to check this, but to
298          * guard the integer addition below, we double-check */
299         if (base->groups.count > 65535) {
300                 return NT_STATUS_INVALID_PARAMETER;
301         }
302
303         user_info_dc->num_sids = 2;
304
305         user_info_dc->sids = talloc_array(user_info_dc, struct dom_sid,  user_info_dc->num_sids + base->groups.count);
306         NT_STATUS_HAVE_NO_MEMORY(user_info_dc->sids);
307
308         user_info_dc->sids[PRIMARY_USER_SID_INDEX] = *base->domain_sid;
309         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_USER_SID_INDEX], base->rid)) {
310                 return NT_STATUS_INVALID_PARAMETER;
311         }
312
313         user_info_dc->sids[PRIMARY_GROUP_SID_INDEX] = *base->domain_sid;
314         if (!sid_append_rid(&user_info_dc->sids[PRIMARY_GROUP_SID_INDEX], base->primary_gid)) {
315                 return NT_STATUS_INVALID_PARAMETER;
316         }
317
318         for (i = 0; i < base->groups.count; i++) {
319                 user_info_dc->sids[user_info_dc->num_sids] = *base->domain_sid;
320                 if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids], base->groups.rids[i].rid)) {
321                         return NT_STATUS_INVALID_PARAMETER;
322                 }
323                 user_info_dc->num_sids++;
324         }
325
326         /* Copy 'other' sids.  We need to do sid filtering here to
327            prevent possible elevation of privileges.  See:
328
329            http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
330          */
331
332         if (validation_level == 3) {
333                 struct dom_sid *dgrps = user_info_dc->sids;
334                 size_t sidcount;
335
336                 /* The IDL layer would be a better place to check this, but to
337                  * guard the integer addition below, we double-check */
338                 if (validation->sam3->sidcount > 65535) {
339                         return NT_STATUS_INVALID_PARAMETER;
340                 }
341
342                 sidcount = user_info_dc->num_sids + validation->sam3->sidcount;
343                 if (validation->sam3->sidcount > 0) {
344                         dgrps = talloc_realloc(user_info_dc, dgrps, struct dom_sid, sidcount);
345                         NT_STATUS_HAVE_NO_MEMORY(dgrps);
346
347                         for (i = 0; i < validation->sam3->sidcount; i++) {
348                                 if (validation->sam3->sids[i].sid) {
349                                         dgrps[user_info_dc->num_sids] = *validation->sam3->sids[i].sid;
350                                         user_info_dc->num_sids++;
351                                 }
352                         }
353                 }
354
355                 user_info_dc->sids = dgrps;
356
357                 /* Where are the 'global' sids?... */
358         }
359
360         status = make_user_info_SamBaseInfo(user_info_dc, account_name, base, authenticated, &user_info_dc->info);
361         if (!NT_STATUS_IS_OK(status)) {
362                 return status;
363         }
364
365         /* ensure we are never given NULL session keys */
366
367         if (all_zero(base->key.key, sizeof(base->key.key))) {
368                 user_info_dc->user_session_key = data_blob(NULL, 0);
369         } else {
370                 user_info_dc->user_session_key = data_blob_talloc(user_info_dc, base->key.key, sizeof(base->key.key));
371                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->user_session_key.data);
372         }
373
374         if (all_zero(base->LMSessKey.key, sizeof(base->LMSessKey.key))) {
375                 user_info_dc->lm_session_key = data_blob(NULL, 0);
376         } else {
377                 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc, base->LMSessKey.key, sizeof(base->LMSessKey.key));
378                 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->lm_session_key.data);
379         }
380
381         *_user_info_dc = user_info_dc;
382         return NT_STATUS_OK;
383 }
384
385 /**
386  * Make a user_info_dc struct from the PAC_LOGON_INFO supplied in the krb5 logon
387  */
388 NTSTATUS make_user_info_dc_pac(TALLOC_CTX *mem_ctx,
389                               struct PAC_LOGON_INFO *pac_logon_info,
390                               struct auth_user_info_dc **_user_info_dc)
391 {
392         uint32_t i;
393         NTSTATUS nt_status;
394         union netr_Validation validation;
395         struct auth_user_info_dc *user_info_dc;
396
397         validation.sam3 = &pac_logon_info->info3;
398
399         nt_status = make_user_info_dc_netlogon_validation(mem_ctx, "", 3, &validation,
400                                                           true, /* This user was authenticated */
401                                                           &user_info_dc);
402         if (!NT_STATUS_IS_OK(nt_status)) {
403                 return nt_status;
404         }
405
406         if (pac_logon_info->res_groups.count > 0) {
407                 size_t sidcount;
408                 /* The IDL layer would be a better place to check this, but to
409                  * guard the integer addition below, we double-check */
410                 if (pac_logon_info->res_groups.count > 65535) {
411                         talloc_free(user_info_dc);
412                         return NT_STATUS_INVALID_PARAMETER;
413                 }
414
415                 /*
416                   Here is where we should check the list of
417                   trusted domains, and verify that the SID
418                   matches.
419                 */
420                 if (!pac_logon_info->res_group_dom_sid) {
421                         DEBUG(0, ("Cannot operate on a PAC without a resource domain SID"));
422                         return NT_STATUS_INVALID_PARAMETER;
423                 }
424
425                 sidcount = user_info_dc->num_sids + pac_logon_info->res_groups.count;
426                 user_info_dc->sids
427                         = talloc_realloc(user_info_dc, user_info_dc->sids, struct dom_sid, sidcount);
428                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info_dc->sids, user_info_dc);
429
430                 for (i = 0; pac_logon_info->res_group_dom_sid && i < pac_logon_info->res_groups.count; i++) {
431                         user_info_dc->sids[user_info_dc->num_sids] = *pac_logon_info->res_group_dom_sid;
432                         if (!sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
433                                             pac_logon_info->res_groups.rids[i].rid)) {
434                                 return NT_STATUS_INVALID_PARAMETER;
435                         }
436                         user_info_dc->num_sids++;
437                 }
438         }
439         *_user_info_dc = user_info_dc;
440         return NT_STATUS_OK;
441 }