71516b89f24f33bec2dcdad1d202ce622b303715
[bbaumbach/samba-autobuild/.git] / source3 / rpc_client / cli_ds.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RPC pipe client
4    Copyright (C) Gerald Carter                        2002,
5    Copyright (C) Jeremy Allison                         2005.
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
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         q.level = level;
42         
43         CLI_DO_RPC( cli, mem_ctx, PI_LSARPC_DS, DS_GETPRIMDOMINFO,
44                 q, r,
45                 qbuf, rbuf,
46                 ds_io_q_getprimdominfo,
47                 ds_io_r_getprimdominfo,
48                 NT_STATUS_UNSUCCESSFUL);
49         
50         /* Return basic info - if we are requesting at info != 1 then
51            there could be trouble. */ 
52
53         result = r.status;
54
55         if ( r.ptr && ctr ) {
56                 ctr->basic = TALLOC_P(mem_ctx, DSROLE_PRIMARY_DOMAIN_INFO_BASIC);
57                 if (!ctr->basic)
58                         goto done;
59                 memcpy(ctr->basic, r.info.basic, sizeof(DSROLE_PRIMARY_DOMAIN_INFO_BASIC));
60         }
61         
62 done:
63
64         return result;
65 }
66
67 /********************************************************************
68  Enumerate trusted domains in an AD forest
69 ********************************************************************/
70
71 NTSTATUS rpccli_ds_enum_domain_trusts(struct rpc_pipe_client *cli,
72                                       TALLOC_CTX *mem_ctx, 
73                                       const char *server, uint32 flags, 
74                                       struct ds_domain_trust **trusts,
75                                       uint32 *num_domains)
76 {
77         prs_struct qbuf, rbuf;
78         DS_Q_ENUM_DOM_TRUSTS q;
79         DS_R_ENUM_DOM_TRUSTS r;
80         NTSTATUS result;
81
82         ZERO_STRUCT(q);
83         ZERO_STRUCT(r);
84
85         init_q_ds_enum_domain_trusts( &q, server, flags );
86                 
87         CLI_DO_RPC( cli, mem_ctx, PI_NETLOGON, DS_ENUM_DOM_TRUSTS,
88                 q, r,
89                 qbuf, rbuf,
90                 ds_io_q_enum_domain_trusts,
91                 ds_io_r_enum_domain_trusts,
92                 NT_STATUS_UNSUCCESSFUL);
93         
94         result = r.status;
95         
96         if ( NT_STATUS_IS_OK(result) ) {
97                 int i;
98         
99                 *num_domains = r.num_domains;
100                 if (r.num_domains) {
101                         *trusts = TALLOC_ARRAY(mem_ctx, struct ds_domain_trust, r.num_domains);
102
103                         if (*trusts == NULL) {
104                                 return NT_STATUS_NO_MEMORY;
105                         }
106                 } else {
107                         *trusts = NULL;
108                 }
109
110                 for ( i=0; i< *num_domains; i++ ) {
111                         (*trusts)[i].flags = r.domains.trusts[i].flags;
112                         (*trusts)[i].parent_index = r.domains.trusts[i].parent_index;
113                         (*trusts)[i].trust_type = r.domains.trusts[i].trust_type;
114                         (*trusts)[i].trust_attributes = r.domains.trusts[i].trust_attributes;
115                         (*trusts)[i].guid = r.domains.trusts[i].guid;
116
117                         if (r.domains.trusts[i].sid_ptr) {
118                                 sid_copy(&(*trusts)[i].sid, &r.domains.trusts[i].sid.sid);
119                         } else {
120                                 ZERO_STRUCT((*trusts)[i].sid);
121                         }
122
123                         if (r.domains.trusts[i].netbios_ptr) {
124                                 (*trusts)[i].netbios_domain = unistr2_to_ascii_talloc( mem_ctx, &r.domains.trusts[i].netbios_domain );
125                         } else {
126                                 (*trusts)[i].netbios_domain = NULL;
127                         }
128
129                         if (r.domains.trusts[i].dns_ptr) {
130                                 (*trusts)[i].dns_domain = unistr2_to_ascii_talloc( mem_ctx, &r.domains.trusts[i].dns_domain );
131                         } else {
132                                 (*trusts)[i].dns_domain = NULL;
133                         }
134                 }
135         }
136         
137         return result;
138 }