40a32c7ee0c76ca5dd2c59515e11d6ea35e52003
[samba.git] / source / 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 cli_ds_getprimarydominfo(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
30                                   uint16 level, DS_DOMINFO_CTR *ctr)
31 {
32         prs_struct qbuf, rbuf;
33         DS_Q_GETPRIMDOMINFO q;
34         DS_R_GETPRIMDOMINFO r;
35         NTSTATUS result;
36
37         ZERO_STRUCT(q);
38         ZERO_STRUCT(r);
39
40         /* Initialise parse structures */
41
42         if (!prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL)) {
43                 return NT_STATUS_NO_MEMORY;
44         }
45         if (!prs_init(&rbuf, 0, mem_ctx, UNMARSHALL)) {
46                 prs_mem_free(&qbuf);
47                 return NT_STATUS_NO_MEMORY;
48         }
49         
50         q.level = level;
51         
52         if (!ds_io_q_getprimdominfo("", &qbuf, 0, &q) 
53             || !rpc_api_pipe_req(cli, PI_LSARPC_DS, DS_GETPRIMDOMINFO, &qbuf, &rbuf)) {
54                 result = NT_STATUS_UNSUCCESSFUL;
55                 goto done;
56         }
57
58         /* Unmarshall response */
59
60         if (!ds_io_r_getprimdominfo("", &rbuf, 0, &r)) {
61                 result = NT_STATUS_UNSUCCESSFUL;
62                 goto done;
63         }
64         
65         /* Return basic info - if we are requesting at info != 1 then
66            there could be trouble. */ 
67
68         result = r.status;
69
70         if ( r.ptr && ctr ) {
71                 ctr->basic = TALLOC_P(mem_ctx, DSROLE_PRIMARY_DOMAIN_INFO_BASIC);
72                 if (!ctr->basic)
73                         goto done;
74                 memcpy(ctr->basic, r.info.basic, sizeof(DSROLE_PRIMARY_DOMAIN_INFO_BASIC));
75         }
76         
77 done:
78         prs_mem_free(&qbuf);
79         prs_mem_free(&rbuf);
80
81         return result;
82 }
83
84 /********************************************************************
85  Enumerate trusted domains in an AD forest
86 ********************************************************************/
87
88 NTSTATUS cli_ds_enum_domain_trusts(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
89                                   const char *server, uint32 flags, 
90                                   struct ds_domain_trust **trusts, uint32 *num_domains)
91 {
92         prs_struct qbuf, rbuf;
93         DS_Q_ENUM_DOM_TRUSTS q;
94         DS_R_ENUM_DOM_TRUSTS r;
95         NTSTATUS result;
96
97         ZERO_STRUCT(q);
98         ZERO_STRUCT(r);
99
100         /* Initialise parse structures */
101
102         if (!prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL)) {
103                 return NT_STATUS_NO_MEMORY;;
104         }
105         if (!prs_init(&rbuf, 0, mem_ctx, UNMARSHALL)) {
106                 prs_mem_free(&qbuf);
107                 return NT_STATUS_NO_MEMORY;
108         }
109
110         init_q_ds_enum_domain_trusts( &q, server, flags );
111                 
112         if (!ds_io_q_enum_domain_trusts("", &qbuf, 0, &q) 
113             || !rpc_api_pipe_req(cli, PI_NETLOGON, DS_ENUM_DOM_TRUSTS, &qbuf, &rbuf)) {
114                 result = NT_STATUS_UNSUCCESSFUL;
115                 goto done;
116         }
117
118         /* Unmarshall response */
119
120         if (!ds_io_r_enum_domain_trusts("", &rbuf, 0, &r)) {
121                 result = NT_STATUS_UNSUCCESSFUL;
122                 goto done;
123         }
124         
125         result = r.status;
126         
127         if ( NT_STATUS_IS_OK(result) ) {
128                 int i;
129         
130                 *num_domains = r.num_domains;
131                 *trusts = TALLOC_ARRAY(mem_ctx, struct ds_domain_trust, r.num_domains);
132
133                 for ( i=0; i< *num_domains; i++ ) {
134                         (*trusts)[i].flags = r.domains.trusts[i].flags;
135                         (*trusts)[i].parent_index = r.domains.trusts[i].parent_index;
136                         (*trusts)[i].trust_type = r.domains.trusts[i].trust_type;
137                         (*trusts)[i].trust_attributes = r.domains.trusts[i].trust_attributes;
138                         (*trusts)[i].guid = r.domains.trusts[i].guid;
139
140                         if (r.domains.trusts[i].sid_ptr) {
141                                 sid_copy(&(*trusts)[i].sid, &r.domains.trusts[i].sid.sid);
142                         } else {
143                                 ZERO_STRUCT((*trusts)[i].sid);
144                         }
145
146                         if (r.domains.trusts[i].netbios_ptr) {
147                                 (*trusts)[i].netbios_domain = unistr2_tdup( mem_ctx, &r.domains.trusts[i].netbios_domain );
148                         } else {
149                                 (*trusts)[i].netbios_domain = NULL;
150                         }
151
152                         if (r.domains.trusts[i].dns_ptr) {
153                                 (*trusts)[i].dns_domain = unistr2_tdup( mem_ctx, &r.domains.trusts[i].dns_domain );
154                         } else {
155                                 (*trusts)[i].dns_domain = NULL;
156                         }
157                 }
158         }
159         
160 done:
161         prs_mem_free(&qbuf);
162         prs_mem_free(&rbuf);
163
164         return result;
165 }