s3: auth: Add create_info3_from_pac_logon_info() to create a new info3 and merge...
[samba.git] / source3 / auth / server_info.c
1 /*
2    Unix SMB/CIFS implementation.
3    Authentication utility functions
4    Copyright (C) Volker Lendecke 2010
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "auth.h"
22 #include "../lib/crypto/arcfour.h"
23 #include "../librpc/gen_ndr/netlogon.h"
24 #include "../libcli/security/security.h"
25 #include "rpc_client/util_netlogon.h"
26 #include "nsswitch/libwbclient/wbclient.h"
27 #include "passdb.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_AUTH
31
32 /***************************************************************************
33  Make a server_info struct. Free with TALLOC_FREE().
34 ***************************************************************************/
35
36 struct auth_serversupplied_info *make_server_info(TALLOC_CTX *mem_ctx)
37 {
38         struct auth_serversupplied_info *result;
39
40         result = talloc_zero(mem_ctx, struct auth_serversupplied_info);
41         if (result == NULL) {
42                 DEBUG(0, ("talloc failed\n"));
43                 return NULL;
44         }
45
46         /* Initialise the uid and gid values to something non-zero
47            which may save us from giving away root access if there
48            is a bug in allocating these fields. */
49
50         result->utok.uid = -1;
51         result->utok.gid = -1;
52
53         return result;
54 }
55
56 /****************************************************************************
57  inits a netr_SamInfo2 structure from an auth_serversupplied_info. sam2 must
58  already be initialized and is used as the talloc parent for its members.
59 *****************************************************************************/
60
61 NTSTATUS serverinfo_to_SamInfo2(struct auth_serversupplied_info *server_info,
62                                 struct netr_SamInfo2 *sam2)
63 {
64         struct netr_SamInfo3 *info3;
65
66         info3 = copy_netr_SamInfo3(sam2, server_info->info3);
67         if (!info3) {
68                 return NT_STATUS_NO_MEMORY;
69         }
70
71         if (server_info->session_key.length) {
72                 memcpy(info3->base.key.key,
73                        server_info->session_key.data,
74                        MIN(sizeof(info3->base.key.key),
75                            server_info->session_key.length));
76         }
77         if (server_info->lm_session_key.length) {
78                 memcpy(info3->base.LMSessKey.key,
79                        server_info->lm_session_key.data,
80                        MIN(sizeof(info3->base.LMSessKey.key),
81                            server_info->lm_session_key.length));
82         }
83
84         sam2->base = info3->base;
85
86         return NT_STATUS_OK;
87 }
88
89 /****************************************************************************
90  inits a netr_SamInfo3 structure from an auth_serversupplied_info. sam3 must
91  already be initialized and is used as the talloc parent for its members.
92 *****************************************************************************/
93
94 NTSTATUS serverinfo_to_SamInfo3(const struct auth_serversupplied_info *server_info,
95                                 struct netr_SamInfo3 *sam3)
96 {
97         struct netr_SamInfo3 *info3;
98
99         info3 = copy_netr_SamInfo3(sam3, server_info->info3);
100         if (!info3) {
101                 return NT_STATUS_NO_MEMORY;
102         }
103
104         if (server_info->session_key.length) {
105                 memcpy(info3->base.key.key,
106                        server_info->session_key.data,
107                        MIN(sizeof(info3->base.key.key),
108                            server_info->session_key.length));
109         }
110         if (server_info->lm_session_key.length) {
111                 memcpy(info3->base.LMSessKey.key,
112                        server_info->lm_session_key.data,
113                        MIN(sizeof(info3->base.LMSessKey.key),
114                            server_info->lm_session_key.length));
115         }
116
117         sam3->base = info3->base;
118
119         sam3->sidcount          = 0;
120         sam3->sids              = NULL;
121
122         return NT_STATUS_OK;
123 }
124
125 /****************************************************************************
126  inits a netr_SamInfo6 structure from an auth_serversupplied_info. sam6 must
127  already be initialized and is used as the talloc parent for its members.
128 *****************************************************************************/
129
130 NTSTATUS serverinfo_to_SamInfo6(struct auth_serversupplied_info *server_info,
131                                 struct netr_SamInfo6 *sam6)
132 {
133         struct pdb_domain_info *dominfo;
134         struct netr_SamInfo3 *info3;
135
136         if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
137                 DEBUG(10,("Not adding validation info level 6 "
138                            "without ADS passdb backend\n"));
139                 return NT_STATUS_INVALID_INFO_CLASS;
140         }
141
142         dominfo = pdb_get_domain_info(sam6);
143         if (dominfo == NULL) {
144                 return NT_STATUS_NO_MEMORY;
145         }
146
147         info3 = copy_netr_SamInfo3(sam6, server_info->info3);
148         if (!info3) {
149                 return NT_STATUS_NO_MEMORY;
150         }
151
152         if (server_info->session_key.length) {
153                 memcpy(info3->base.key.key,
154                        server_info->session_key.data,
155                        MIN(sizeof(info3->base.key.key),
156                            server_info->session_key.length));
157         }
158         if (server_info->lm_session_key.length) {
159                 memcpy(info3->base.LMSessKey.key,
160                        server_info->lm_session_key.data,
161                        MIN(sizeof(info3->base.LMSessKey.key),
162                            server_info->lm_session_key.length));
163         }
164
165         sam6->base = info3->base;
166
167         sam6->sidcount          = 0;
168         sam6->sids              = NULL;
169
170         sam6->dns_domainname.string = talloc_strdup(sam6, dominfo->dns_domain);
171         if (sam6->dns_domainname.string == NULL) {
172                 return NT_STATUS_NO_MEMORY;
173         }
174
175         sam6->principle.string  = talloc_asprintf(sam6, "%s@%s",
176                                                   sam6->base.account_name.string,
177                                                   sam6->dns_domainname.string);
178         if (sam6->principle.string == NULL) {
179                 return NT_STATUS_NO_MEMORY;
180         }
181
182         return NT_STATUS_OK;
183 }
184
185 static NTSTATUS append_netr_SidAttr(TALLOC_CTX *mem_ctx,
186                                     struct netr_SidAttr **sids,
187                                     uint32_t *count,
188                                     const struct dom_sid2 *asid,
189                                     uint32_t attributes)
190 {
191         uint32_t t = *count;
192
193         *sids = talloc_realloc(mem_ctx, *sids, struct netr_SidAttr, t + 1);
194         if (*sids == NULL) {
195                 return NT_STATUS_NO_MEMORY;
196         }
197         (*sids)[t].sid = dom_sid_dup(*sids, asid);
198         if ((*sids)[t].sid == NULL) {
199                 return NT_STATUS_NO_MEMORY;
200         }
201         (*sids)[t].attributes = attributes;
202         *count = t + 1;
203
204         return NT_STATUS_OK;
205 }
206
207 /* Fills the samr_RidWithAttributeArray with the provided sids.
208  * If it happens that we have additional groups that do not belong
209  * to the domain, add their sids as extra sids */
210 static NTSTATUS group_sids_to_info3(struct netr_SamInfo3 *info3,
211                                     const struct dom_sid *sids,
212                                     size_t num_sids)
213 {
214         uint32_t attributes = SE_GROUP_MANDATORY |
215                                 SE_GROUP_ENABLED_BY_DEFAULT |
216                                 SE_GROUP_ENABLED;
217         struct samr_RidWithAttributeArray *groups;
218         struct dom_sid *domain_sid;
219         unsigned int i;
220         NTSTATUS status;
221         uint32_t rid;
222         bool ok;
223
224         domain_sid = info3->base.domain_sid;
225         groups = &info3->base.groups;
226
227         groups->rids = talloc_array(info3,
228                                     struct samr_RidWithAttribute, num_sids);
229         if (!groups->rids) {
230                 return NT_STATUS_NO_MEMORY;
231         }
232
233         for (i = 0; i < num_sids; i++) {
234                 ok = sid_peek_check_rid(domain_sid, &sids[i], &rid);
235                 if (ok) {
236                         /* store domain group rid */
237                         groups->rids[groups->count].rid = rid;
238                         groups->rids[groups->count].attributes = attributes;
239                         groups->count++;
240                         continue;
241                 }
242
243                 /* if this wasn't a domain sid, add it as extra sid */
244                 status = append_netr_SidAttr(info3, &info3->sids,
245                                              &info3->sidcount,
246                                              &sids[i], attributes);
247                 if (!NT_STATUS_IS_OK(status)) {
248                         return status;
249                 }
250         }
251
252         return NT_STATUS_OK;
253 }
254
255 /*
256  * Merge resource SIDs, if any, into the passed in info3 structure.
257  */
258
259 static NTSTATUS merge_resource_sids(const struct PAC_LOGON_INFO *logon_info,
260                                 struct netr_SamInfo3 *info3)
261 {
262         uint32_t i = 0;
263
264         if (!(logon_info->info3.base.user_flags & NETLOGON_RESOURCE_GROUPS)) {
265                 return NT_STATUS_OK;
266         }
267
268         /*
269          * If there are any resource groups (SID Compression) add
270          * them to the extra sids portion of the info3 in the PAC.
271          *
272          * This makes the info3 look like it would if we got the info
273          * from the DC rather than the PAC.
274          */
275
276         /*
277          * Construct a SID for each RID in the list and then append it
278          * to the info3.
279          */
280         for (i = 0; i < logon_info->res_groups.count; i++) {
281                 NTSTATUS status;
282                 struct dom_sid new_sid;
283                 uint32_t attributes = logon_info->res_groups.rids[i].attributes;
284
285                 sid_compose(&new_sid,
286                         logon_info->res_group_dom_sid,
287                         logon_info->res_groups.rids[i].rid);
288
289                 DEBUG(10, ("Adding SID %s to extra SIDS\n",
290                         sid_string_dbg(&new_sid)));
291
292                 status = append_netr_SidAttr(info3, &info3->sids,
293                                         &info3->sidcount,
294                                         &new_sid,
295                                         attributes);
296                 if (!NT_STATUS_IS_OK(status)) {
297                         DEBUG(1, ("failed to append SID %s to extra SIDS: %s\n",
298                                 sid_string_dbg(&new_sid),
299                                 nt_errstr(status)));
300                         return status;
301                 }
302         }
303
304         return NT_STATUS_OK;
305 }
306
307 /*
308  * Create a copy of an info3 struct from the struct PAC_LOGON_INFO,
309  * then merge resource SIDs, if any, into it. If successful return
310  * the created info3 struct.
311  */
312
313 NTSTATUS create_info3_from_pac_logon_info(TALLOC_CTX *mem_ctx,
314                                         const struct PAC_LOGON_INFO *logon_info,
315                                         struct netr_SamInfo3 **pp_info3)
316 {
317         NTSTATUS status;
318         struct netr_SamInfo3 *info3 = copy_netr_SamInfo3(mem_ctx,
319                                         &logon_info->info3);
320         if (info3 == NULL) {
321                 return NT_STATUS_NO_MEMORY;
322         }
323         status = merge_resource_sids(logon_info, info3);
324         if (!NT_STATUS_IS_OK(status)) {
325                 TALLOC_FREE(info3);
326                 return status;
327         }
328         *pp_info3 = info3;
329         return NT_STATUS_OK;
330 }
331
332 #define RET_NOMEM(ptr) do { \
333         if (!ptr) { \
334                 TALLOC_FREE(info3); \
335                 return NT_STATUS_NO_MEMORY; \
336         } } while(0)
337
338 NTSTATUS samu_to_SamInfo3(TALLOC_CTX *mem_ctx,
339                           struct samu *samu,
340                           const char *login_server,
341                           struct netr_SamInfo3 **_info3,
342                           struct extra_auth_info *extra)
343 {
344         struct netr_SamInfo3 *info3;
345         const struct dom_sid *user_sid;
346         const struct dom_sid *group_sid;
347         struct dom_sid domain_sid;
348         struct dom_sid *group_sids;
349         uint32_t num_group_sids = 0;
350         const char *tmp;
351         gid_t *gids;
352         NTSTATUS status;
353         bool ok;
354
355         user_sid = pdb_get_user_sid(samu);
356         group_sid = pdb_get_group_sid(samu);
357
358         if (!user_sid || !group_sid) {
359                 DEBUG(1, ("Sam account is missing sids!\n"));
360                 return NT_STATUS_UNSUCCESSFUL;
361         }
362
363         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
364         if (!info3) {
365                 return NT_STATUS_NO_MEMORY;
366         }
367
368         ZERO_STRUCT(domain_sid);
369
370         /* check if this is a "Unix Users" domain user,
371          * we need to handle it in a special way if that's the case */
372         if (sid_check_is_in_unix_users(user_sid)) {
373                 /* in info3 you can only set rids for the user and the
374                  * primary group, and the domain sid must be that of
375                  * the sam domain.
376                  *
377                  * Store a completely bogus value here.
378                  * The real SID is stored in the extra sids.
379                  * Other code will know to look there if (-1) is found
380                  */
381                 info3->base.rid = (uint32_t)(-1);
382                 sid_copy(&extra->user_sid, user_sid);
383
384                 DEBUG(10, ("Unix User found in struct samu. Rid marked as "
385                            "special and sid (%s) saved as extra sid\n",
386                            sid_string_dbg(user_sid)));
387         } else {
388                 sid_copy(&domain_sid, user_sid);
389                 sid_split_rid(&domain_sid, &info3->base.rid);
390         }
391
392         if (is_null_sid(&domain_sid)) {
393                 sid_copy(&domain_sid, get_global_sam_sid());
394         }
395
396         /* check if this is a "Unix Groups" domain group,
397          * if so we need special handling */
398         if (sid_check_is_in_unix_groups(group_sid)) {
399                 /* in info3 you can only set rids for the user and the
400                  * primary group, and the domain sid must be that of
401                  * the sam domain.
402                  *
403                  * Store a completely bogus value here.
404                  * The real SID is stored in the extra sids.
405                  * Other code will know to look there if (-1) is found
406                  */
407                 info3->base.primary_gid = (uint32_t)(-1);
408                 sid_copy(&extra->pgid_sid, group_sid);
409
410                 DEBUG(10, ("Unix Group found in struct samu. Rid marked as "
411                            "special and sid (%s) saved as extra sid\n",
412                            sid_string_dbg(group_sid)));
413
414         } else {
415                 ok = sid_peek_check_rid(&domain_sid, group_sid,
416                                         &info3->base.primary_gid);
417                 if (!ok) {
418                         DEBUG(1, ("The primary group domain sid(%s) does not "
419                                   "match the domain sid(%s) for %s(%s)\n",
420                                   sid_string_dbg(group_sid),
421                                   sid_string_dbg(&domain_sid),
422                                   pdb_get_username(samu),
423                                   sid_string_dbg(user_sid)));
424                         TALLOC_FREE(info3);
425                         return NT_STATUS_UNSUCCESSFUL;
426                 }
427         }
428
429         unix_to_nt_time(&info3->base.logon_time, pdb_get_logon_time(samu));
430         unix_to_nt_time(&info3->base.logoff_time, get_time_t_max());
431         unix_to_nt_time(&info3->base.kickoff_time, get_time_t_max());
432         unix_to_nt_time(&info3->base.last_password_change,
433                         pdb_get_pass_last_set_time(samu));
434         unix_to_nt_time(&info3->base.allow_password_change,
435                         pdb_get_pass_can_change_time(samu));
436         unix_to_nt_time(&info3->base.force_password_change,
437                         pdb_get_pass_must_change_time(samu));
438
439         tmp = pdb_get_username(samu);
440         if (tmp) {
441                 info3->base.account_name.string = talloc_strdup(info3, tmp);
442                 RET_NOMEM(info3->base.account_name.string);
443         }
444         tmp = pdb_get_fullname(samu);
445         if (tmp) {
446                 info3->base.full_name.string = talloc_strdup(info3, tmp);
447                 RET_NOMEM(info3->base.full_name.string);
448         }
449         tmp = pdb_get_logon_script(samu);
450         if (tmp) {
451                 info3->base.logon_script.string = talloc_strdup(info3, tmp);
452                 RET_NOMEM(info3->base.logon_script.string);
453         }
454         tmp = pdb_get_profile_path(samu);
455         if (tmp) {
456                 info3->base.profile_path.string = talloc_strdup(info3, tmp);
457                 RET_NOMEM(info3->base.profile_path.string);
458         }
459         tmp = pdb_get_homedir(samu);
460         if (tmp) {
461                 info3->base.home_directory.string = talloc_strdup(info3, tmp);
462                 RET_NOMEM(info3->base.home_directory.string);
463         }
464         tmp = pdb_get_dir_drive(samu);
465         if (tmp) {
466                 info3->base.home_drive.string = talloc_strdup(info3, tmp);
467                 RET_NOMEM(info3->base.home_drive.string);
468         }
469
470         info3->base.logon_count = pdb_get_logon_count(samu);
471         info3->base.bad_password_count = pdb_get_bad_password_count(samu);
472
473         info3->base.logon_domain.string = talloc_strdup(info3,
474                                                   pdb_get_domain(samu));
475         RET_NOMEM(info3->base.logon_domain.string);
476
477         info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
478         RET_NOMEM(info3->base.domain_sid);
479
480         status = pdb_enum_group_memberships(mem_ctx, samu,
481                                             &group_sids, &gids,
482                                             &num_group_sids);
483         if (!NT_STATUS_IS_OK(status)) {
484                 DEBUG(1, ("Failed to get groups from sam account.\n"));
485                 TALLOC_FREE(info3);
486                 return status;
487         }
488
489         if (num_group_sids) {
490                 status = group_sids_to_info3(info3, group_sids, num_group_sids);
491                 if (!NT_STATUS_IS_OK(status)) {
492                         TALLOC_FREE(info3);
493                         return status;
494                 }
495         }
496
497         /* We don't need sids and gids after the conversion */
498         TALLOC_FREE(group_sids);
499         TALLOC_FREE(gids);
500         num_group_sids = 0;
501
502         /* FIXME: should we add other flags ? */
503         info3->base.user_flags = NETLOGON_EXTRA_SIDS;
504
505         if (login_server) {
506                 info3->base.logon_server.string = talloc_strdup(info3, login_server);
507                 RET_NOMEM(info3->base.logon_server.string);
508         }
509
510         info3->base.acct_flags = pdb_get_acct_ctrl(samu);
511
512         *_info3 = info3;
513         return NT_STATUS_OK;
514 }
515
516 #undef RET_NOMEM
517
518 #define RET_NOMEM(ptr) do { \
519         if (!ptr) { \
520                 TALLOC_FREE(info3); \
521                 return NULL; \
522         } } while(0)
523
524 struct netr_SamInfo3 *copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
525                                          const struct netr_SamInfo3 *orig)
526 {
527         struct netr_SamInfo3 *info3;
528         unsigned int i;
529         NTSTATUS status;
530
531         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
532         if (!info3) return NULL;
533
534         status = copy_netr_SamBaseInfo(info3, &orig->base, &info3->base);
535         if (!NT_STATUS_IS_OK(status)) {
536                 TALLOC_FREE(info3);
537                 return NULL;
538         }
539
540         if (orig->sidcount) {
541                 info3->sidcount = orig->sidcount;
542                 info3->sids = talloc_array(info3, struct netr_SidAttr,
543                                            orig->sidcount);
544                 RET_NOMEM(info3->sids);
545                 for (i = 0; i < orig->sidcount; i++) {
546                         info3->sids[i].sid = dom_sid_dup(info3->sids,
547                                                             orig->sids[i].sid);
548                         RET_NOMEM(info3->sids[i].sid);
549                         info3->sids[i].attributes =
550                                 orig->sids[i].attributes;
551                 }
552         }
553
554         return info3;
555 }
556
557 static NTSTATUS wbcsids_to_samr_RidWithAttributeArray(
558                                 TALLOC_CTX *mem_ctx,
559                                 struct samr_RidWithAttributeArray *groups,
560                                 const struct dom_sid *domain_sid,
561                                 const struct wbcSidWithAttr *sids,
562                                 size_t num_sids)
563 {
564         unsigned int i, j = 0;
565         bool ok;
566
567         groups->rids = talloc_array(mem_ctx,
568                                     struct samr_RidWithAttribute, num_sids);
569         if (!groups->rids) {
570                 return NT_STATUS_NO_MEMORY;
571         }
572
573         /* a wbcDomainSid is the same as a dom_sid */
574         for (i = 0; i < num_sids; i++) {
575                 ok = sid_peek_check_rid(domain_sid,
576                                         (const struct dom_sid *)&sids[i].sid,
577                                         &groups->rids[j].rid);
578                 if (!ok) continue;
579
580                 groups->rids[j].attributes = SE_GROUP_MANDATORY |
581                                              SE_GROUP_ENABLED_BY_DEFAULT |
582                                              SE_GROUP_ENABLED;
583                 j++;
584         }
585
586         groups->count = j;
587         return NT_STATUS_OK;
588 }
589
590 static NTSTATUS wbcsids_to_netr_SidAttrArray(
591                                 const struct dom_sid *domain_sid,
592                                 const struct wbcSidWithAttr *sids,
593                                 size_t num_sids,
594                                 TALLOC_CTX *mem_ctx,
595                                 struct netr_SidAttr **_info3_sids,
596                                 uint32_t *info3_num_sids)
597 {
598         unsigned int i, j = 0;
599         struct netr_SidAttr *info3_sids;
600
601         info3_sids = talloc_array(mem_ctx, struct netr_SidAttr, num_sids);
602         if (info3_sids == NULL) {
603                 return NT_STATUS_NO_MEMORY;
604         }
605
606         /* a wbcDomainSid is the same as a dom_sid */
607         for (i = 0; i < num_sids; i++) {
608                 const struct dom_sid *sid;
609
610                 sid = (const struct dom_sid *)&sids[i].sid;
611
612                 if (dom_sid_in_domain(domain_sid, sid)) {
613                         continue;
614                 }
615
616                 info3_sids[j].sid = dom_sid_dup(info3_sids, sid);
617                 if (info3_sids[j].sid == NULL) {
618                         talloc_free(info3_sids);
619                         return NT_STATUS_NO_MEMORY;
620                 }
621                 info3_sids[j].attributes = SE_GROUP_MANDATORY |
622                                            SE_GROUP_ENABLED_BY_DEFAULT |
623                                            SE_GROUP_ENABLED;
624                 j++;
625         }
626
627         *info3_num_sids = j;
628         *_info3_sids = info3_sids;
629         return NT_STATUS_OK;
630 }
631
632 struct netr_SamInfo3 *wbcAuthUserInfo_to_netr_SamInfo3(TALLOC_CTX *mem_ctx,
633                                         const struct wbcAuthUserInfo *info)
634 {
635         struct netr_SamInfo3 *info3;
636         struct dom_sid user_sid;
637         struct dom_sid group_sid;
638         struct dom_sid domain_sid;
639         NTSTATUS status;
640         bool ok;
641
642         memcpy(&user_sid, &info->sids[0].sid, sizeof(user_sid));
643         memcpy(&group_sid, &info->sids[1].sid, sizeof(group_sid));
644
645         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
646         if (!info3) return NULL;
647
648         unix_to_nt_time(&info3->base.logon_time, info->logon_time);
649         unix_to_nt_time(&info3->base.logoff_time, info->logoff_time);
650         unix_to_nt_time(&info3->base.kickoff_time, info->kickoff_time);
651         unix_to_nt_time(&info3->base.last_password_change, info->pass_last_set_time);
652         unix_to_nt_time(&info3->base.allow_password_change,
653                         info->pass_can_change_time);
654         unix_to_nt_time(&info3->base.force_password_change,
655                         info->pass_must_change_time);
656
657         if (info->account_name) {
658                 info3->base.account_name.string =
659                                 talloc_strdup(info3, info->account_name);
660                 RET_NOMEM(info3->base.account_name.string);
661         }
662         if (info->full_name) {
663                 info3->base.full_name.string =
664                                 talloc_strdup(info3, info->full_name);
665                 RET_NOMEM(info3->base.full_name.string);
666         }
667         if (info->logon_script) {
668                 info3->base.logon_script.string =
669                                 talloc_strdup(info3, info->logon_script);
670                 RET_NOMEM(info3->base.logon_script.string);
671         }
672         if (info->profile_path) {
673                 info3->base.profile_path.string =
674                                 talloc_strdup(info3, info->profile_path);
675                 RET_NOMEM(info3->base.profile_path.string);
676         }
677         if (info->home_directory) {
678                 info3->base.home_directory.string =
679                                 talloc_strdup(info3, info->home_directory);
680                 RET_NOMEM(info3->base.home_directory.string);
681         }
682         if (info->home_drive) {
683                 info3->base.home_drive.string =
684                                 talloc_strdup(info3, info->home_drive);
685                 RET_NOMEM(info3->base.home_drive.string);
686         }
687
688         info3->base.logon_count = info->logon_count;
689         info3->base.bad_password_count = info->bad_password_count;
690
691         sid_copy(&domain_sid, &user_sid);
692         sid_split_rid(&domain_sid, &info3->base.rid);
693
694         ok = sid_peek_check_rid(&domain_sid, &group_sid,
695                                 &info3->base.primary_gid);
696         if (!ok) {
697                 DEBUG(1, ("The primary group sid domain does not"
698                           "match user sid domain for user: %s\n",
699                           info->account_name));
700                 TALLOC_FREE(info3);
701                 return NULL;
702         }
703
704         status = wbcsids_to_samr_RidWithAttributeArray(info3,
705                                                        &info3->base.groups,
706                                                        &domain_sid,
707                                                        &info->sids[1],
708                                                        info->num_sids - 1);
709         if (!NT_STATUS_IS_OK(status)) {
710                 TALLOC_FREE(info3);
711                 return NULL;
712         }
713
714         status = wbcsids_to_netr_SidAttrArray(&domain_sid,
715                                               &info->sids[1],
716                                               info->num_sids - 1,
717                                               info3,
718                                               &info3->sids,
719                                               &info3->sidcount);
720         if (!NT_STATUS_IS_OK(status)) {
721                 TALLOC_FREE(info3);
722                 return NULL;
723         }
724
725         info3->base.user_flags = info->user_flags;
726         memcpy(info3->base.key.key, info->user_session_key, 16);
727
728         if (info->logon_server) {
729                 info3->base.logon_server.string =
730                                 talloc_strdup(info3, info->logon_server);
731                 RET_NOMEM(info3->base.logon_server.string);
732         }
733         if (info->domain_name) {
734                 info3->base.logon_domain.string =
735                                 talloc_strdup(info3, info->domain_name);
736                 RET_NOMEM(info3->base.logon_domain.string);
737         }
738
739         info3->base.domain_sid = dom_sid_dup(info3, &domain_sid);
740         RET_NOMEM(info3->base.domain_sid);
741
742         memcpy(info3->base.LMSessKey.key, info->lm_session_key, 8);
743         info3->base.acct_flags = info->acct_flags;
744
745         return info3;
746 }