ee2b590b7519dfa92d4a942200bf109f91fe7644
[metze/samba/wip.git] / source3 / rpc_client / util_netlogon.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 "../librpc/gen_ndr/netlogon.h"
22 #include "../libcli/security/security.h"
23 #include "rpc_client/util_netlogon.h"
24
25 #define COPY_LSA_STRING(mem_ctx, in, out, name) do { \
26         if (in->name.string) { \
27                 out->name.string = talloc_strdup(mem_ctx, in->name.string); \
28                 NT_STATUS_HAVE_NO_MEMORY(out->name.string); \
29         } \
30 } while (0)
31
32 NTSTATUS copy_netr_SamBaseInfo(TALLOC_CTX *mem_ctx,
33                                const struct netr_SamBaseInfo *in,
34                                struct netr_SamBaseInfo *out)
35 {
36         /* first copy all, then realloc pointers */
37         *out = *in;
38
39         COPY_LSA_STRING(mem_ctx, in, out, account_name);
40         COPY_LSA_STRING(mem_ctx, in, out, full_name);
41         COPY_LSA_STRING(mem_ctx, in, out, logon_script);
42         COPY_LSA_STRING(mem_ctx, in, out, profile_path);
43         COPY_LSA_STRING(mem_ctx, in, out, home_directory);
44         COPY_LSA_STRING(mem_ctx, in, out, home_drive);
45
46         if (in->groups.count) {
47                 out->groups.rids = (struct samr_RidWithAttribute *)
48                         talloc_memdup(mem_ctx, in->groups.rids,
49                                 (sizeof(struct samr_RidWithAttribute) *
50                                         in->groups.count));
51                 NT_STATUS_HAVE_NO_MEMORY(out->groups.rids);
52         }
53
54         COPY_LSA_STRING(mem_ctx, in, out, logon_server);
55         COPY_LSA_STRING(mem_ctx, in, out, logon_domain);
56
57         if (in->domain_sid) {
58                 out->domain_sid = dom_sid_dup(mem_ctx, in->domain_sid);
59                 NT_STATUS_HAVE_NO_MEMORY(out->domain_sid);
60         }
61
62         return NT_STATUS_OK;
63 }
64
65 #undef RET_NOMEM
66
67 #define RET_NOMEM(ptr) do { \
68         if (!ptr) { \
69                 TALLOC_FREE(info3); \
70                 return NULL; \
71         } } while(0)
72
73 struct netr_SamInfo3 *copy_netr_SamInfo3(TALLOC_CTX *mem_ctx,
74                                          const struct netr_SamInfo3 *orig)
75 {
76         struct netr_SamInfo3 *info3;
77         unsigned int i;
78         NTSTATUS status;
79
80         info3 = talloc_zero(mem_ctx, struct netr_SamInfo3);
81         if (!info3) return NULL;
82
83         status = copy_netr_SamBaseInfo(info3, &orig->base, &info3->base);
84         if (!NT_STATUS_IS_OK(status)) {
85                 TALLOC_FREE(info3);
86                 return NULL;
87         }
88
89         if (orig->sidcount) {
90                 info3->sidcount = orig->sidcount;
91                 info3->sids = talloc_array(info3, struct netr_SidAttr,
92                                            orig->sidcount);
93                 RET_NOMEM(info3->sids);
94                 for (i = 0; i < orig->sidcount; i++) {
95                         info3->sids[i].sid = dom_sid_dup(info3->sids,
96                                                             orig->sids[i].sid);
97                         RET_NOMEM(info3->sids[i].sid);
98                         info3->sids[i].attributes =
99                                 orig->sids[i].attributes;
100                 }
101         }
102
103         return info3;
104 }