41063a5d7fb60f3ce623bf6b7cf63fe13b1e2690
[ab/samba.git/.git] / source3 / rpc_client / cli_ds.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4    Copyright (C) Gerald Carter                        2002,
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /* implementations of client side DsXXX() functions */
24
25 /********************************************************************
26  Get information about the server and directory services
27 ********************************************************************/
28
29 NTSTATUS rpccli_ds_getprimarydominfo(struct rpc_pipe_client *cli,
30                                      TALLOC_CTX *mem_ctx, 
31                                      uint16 level, DS_DOMINFO_CTR *ctr)
32 {
33         prs_struct qbuf, rbuf;
34         DS_Q_GETPRIMDOMINFO q;
35         DS_R_GETPRIMDOMINFO r;
36         NTSTATUS result;
37
38         ZERO_STRUCT(q);
39         ZERO_STRUCT(r);
40
41         /* Initialise parse structures */
42
43         if (!prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL)) {
44                 return NT_STATUS_NO_MEMORY;
45         }
46         if (!prs_init(&rbuf, 0, mem_ctx, UNMARSHALL)) {
47                 prs_mem_free(&qbuf);
48                 return NT_STATUS_NO_MEMORY;
49         }
50         
51         q.level = level;
52         
53         if (!ds_io_q_getprimdominfo("", &qbuf, 0, &q) 
54             || !rpc_api_pipe_req_int(cli, DS_GETPRIMDOMINFO, &qbuf, &rbuf)) {
55                 result = NT_STATUS_UNSUCCESSFUL;
56                 goto done;
57         }
58
59         /* Unmarshall response */
60
61         if (!ds_io_r_getprimdominfo("", &rbuf, 0, &r)) {
62                 result = NT_STATUS_UNSUCCESSFUL;
63                 goto done;
64         }
65         
66         /* Return basic info - if we are requesting at info != 1 then
67            there could be trouble. */ 
68
69         result = r.status;
70
71         if ( r.ptr && ctr ) {
72                 ctr->basic = TALLOC_P(mem_ctx, DSROLE_PRIMARY_DOMAIN_INFO_BASIC);
73                 if (!ctr->basic)
74                         goto done;
75                 memcpy(ctr->basic, r.info.basic, sizeof(DSROLE_PRIMARY_DOMAIN_INFO_BASIC));
76         }
77         
78 done:
79         prs_mem_free(&qbuf);
80         prs_mem_free(&rbuf);
81
82         return result;
83 }
84
85 NTSTATUS cli_ds_getprimarydominfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
86                                   uint16 level, DS_DOMINFO_CTR *ctr)
87 {
88         return rpccli_ds_getprimarydominfo(&cli->pipes[PI_LSARPC_DS], mem_ctx,
89                                            level, ctr);
90 }
91
92
93 /********************************************************************
94  Enumerate trusted domains in an AD forest
95 ********************************************************************/
96
97 NTSTATUS rpccli_ds_enum_domain_trusts(struct rpc_pipe_client *cli,
98                                       TALLOC_CTX *mem_ctx, 
99                                       const char *server, uint32 flags, 
100                                       struct ds_domain_trust **trusts,
101                                       uint32 *num_domains)
102 {
103         prs_struct qbuf, rbuf;
104         DS_Q_ENUM_DOM_TRUSTS q;
105         DS_R_ENUM_DOM_TRUSTS r;
106         NTSTATUS result;
107
108         ZERO_STRUCT(q);
109         ZERO_STRUCT(r);
110
111         /* Initialise parse structures */
112
113         if (!prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL)) {
114                 return NT_STATUS_NO_MEMORY;;
115         }
116         if (!prs_init(&rbuf, 0, mem_ctx, UNMARSHALL)) {
117                 prs_mem_free(&qbuf);
118                 return NT_STATUS_NO_MEMORY;
119         }
120
121         init_q_ds_enum_domain_trusts( &q, server, flags );
122                 
123         if (!ds_io_q_enum_domain_trusts("", &qbuf, 0, &q) 
124             || !rpc_api_pipe_req_int(cli, DS_ENUM_DOM_TRUSTS, &qbuf, &rbuf)) {
125                 result = NT_STATUS_UNSUCCESSFUL;
126                 goto done;
127         }
128
129         /* Unmarshall response */
130
131         if (!ds_io_r_enum_domain_trusts("", &rbuf, 0, &r)) {
132                 result = NT_STATUS_UNSUCCESSFUL;
133                 goto done;
134         }
135         
136         result = r.status;
137         
138         if ( NT_STATUS_IS_OK(result) ) {
139                 int i;
140         
141                 *num_domains = r.num_domains;
142                 *trusts = TALLOC_ARRAY(mem_ctx, struct ds_domain_trust, r.num_domains);
143
144                 for ( i=0; i< *num_domains; i++ ) {
145                         (*trusts)[i].flags = r.domains.trusts[i].flags;
146                         (*trusts)[i].parent_index = r.domains.trusts[i].parent_index;
147                         (*trusts)[i].trust_type = r.domains.trusts[i].trust_type;
148                         (*trusts)[i].trust_attributes = r.domains.trusts[i].trust_attributes;
149                         (*trusts)[i].guid = r.domains.trusts[i].guid;
150
151                         if (r.domains.trusts[i].sid_ptr) {
152                                 sid_copy(&(*trusts)[i].sid, &r.domains.trusts[i].sid.sid);
153                         } else {
154                                 ZERO_STRUCT((*trusts)[i].sid);
155                         }
156
157                         if (r.domains.trusts[i].netbios_ptr) {
158                                 (*trusts)[i].netbios_domain = unistr2_tdup( mem_ctx, &r.domains.trusts[i].netbios_domain );
159                         } else {
160                                 (*trusts)[i].netbios_domain = NULL;
161                         }
162
163                         if (r.domains.trusts[i].dns_ptr) {
164                                 (*trusts)[i].dns_domain = unistr2_tdup( mem_ctx, &r.domains.trusts[i].dns_domain );
165                         } else {
166                                 (*trusts)[i].dns_domain = NULL;
167                         }
168                 }
169         }
170         
171 done:
172         prs_mem_free(&qbuf);
173         prs_mem_free(&rbuf);
174
175         return result;
176 }
177
178 NTSTATUS cli_ds_enum_domain_trusts(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
179                                    const char *server, uint32 flags, 
180                                    struct ds_domain_trust **trusts,
181                                    uint32 *num_domains)
182 {
183         return rpccli_ds_enum_domain_trusts(&cli->pipes[PI_NETLOGON], mem_ctx,
184                                             server, flags, trusts,
185                                             num_domains);
186 }