r148: Ensure we do not dereference a null pointer when we return the user
[Samba/gebeck_regimport.git] / source3 / rpc_client / cli_ds.c
blob09e63a471474588880828b873a1e5bd1c718e7b4
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
4 Copyright (C) Gerald Carter 2002,
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.
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.
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.
21 #include "includes.h"
23 /* implementations of client side DsXXX() functions */
25 /********************************************************************
26 Get information about the server and directory services
27 ********************************************************************/
29 NTSTATUS cli_ds_getprimarydominfo(struct cli_state *cli, TALLOC_CTX *mem_ctx,
30 uint16 level, DS_DOMINFO_CTR *ctr)
32 prs_struct qbuf, rbuf;
33 DS_Q_GETPRIMDOMINFO q;
34 DS_R_GETPRIMDOMINFO r;
35 NTSTATUS result;
37 ZERO_STRUCT(q);
38 ZERO_STRUCT(r);
40 /* Initialise parse structures */
42 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
43 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
45 q.level = level;
47 if (!ds_io_q_getprimdominfo("", &qbuf, 0, &q)
48 || !rpc_api_pipe_req(cli, DS_GETPRIMDOMINFO, &qbuf, &rbuf)) {
49 result = NT_STATUS_UNSUCCESSFUL;
50 goto done;
53 /* Unmarshall response */
55 if (!ds_io_r_getprimdominfo("", &rbuf, 0, &r)) {
56 result = NT_STATUS_UNSUCCESSFUL;
57 goto done;
60 /* Return basic info - if we are requesting at info != 1 then
61 there could be trouble. */
63 result = r.status;
65 if ( r.ptr && ctr ) {
66 ctr->basic = talloc(mem_ctx, sizeof(DSROLE_PRIMARY_DOMAIN_INFO_BASIC));
67 if (!ctr->basic)
68 goto done;
69 memcpy(ctr->basic, r.info.basic, sizeof(DSROLE_PRIMARY_DOMAIN_INFO_BASIC));
72 done:
73 prs_mem_free(&qbuf);
74 prs_mem_free(&rbuf);
76 return result;
79 /********************************************************************
80 Enumerate trusted domains in an AD forest
81 ********************************************************************/
83 NTSTATUS cli_ds_enum_domain_trusts(struct cli_state *cli, TALLOC_CTX *mem_ctx,
84 const char *server, uint32 flags,
85 struct ds_domain_trust **trusts, uint32 *num_domains)
87 prs_struct qbuf, rbuf;
88 DS_Q_ENUM_DOM_TRUSTS q;
89 DS_R_ENUM_DOM_TRUSTS r;
90 NTSTATUS result;
92 ZERO_STRUCT(q);
93 ZERO_STRUCT(r);
95 /* Initialise parse structures */
97 prs_init(&qbuf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
98 prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
100 init_q_ds_enum_domain_trusts( &q, server, flags );
102 if (!ds_io_q_enum_domain_trusts("", &qbuf, 0, &q)
103 || !rpc_api_pipe_req(cli, DS_ENUM_DOM_TRUSTS, &qbuf, &rbuf)) {
104 result = NT_STATUS_UNSUCCESSFUL;
105 goto done;
108 /* Unmarshall response */
110 if (!ds_io_r_enum_domain_trusts("", &rbuf, 0, &r)) {
111 result = NT_STATUS_UNSUCCESSFUL;
112 goto done;
115 result = r.status;
117 if ( NT_STATUS_IS_OK(result) ) {
118 int i;
120 *num_domains = r.num_domains;
121 *trusts = (struct ds_domain_trust*)talloc(mem_ctx, r.num_domains*sizeof(**trusts));
123 for ( i=0; i< *num_domains; i++ ) {
124 (*trusts)[i].flags = r.domains.trusts[i].flags;
125 (*trusts)[i].parent_index = r.domains.trusts[i].parent_index;
126 (*trusts)[i].trust_type = r.domains.trusts[i].trust_type;
127 (*trusts)[i].trust_attributes = r.domains.trusts[i].trust_attributes;
128 (*trusts)[i].guid = r.domains.trusts[i].guid;
130 if (r.domains.trusts[i].sid_ptr) {
131 sid_copy(&(*trusts)[i].sid, &r.domains.trusts[i].sid.sid);
132 } else {
133 ZERO_STRUCT((*trusts)[i].sid);
136 if (r.domains.trusts[i].netbios_ptr) {
137 (*trusts)[i].netbios_domain = unistr2_tdup( mem_ctx, &r.domains.trusts[i].netbios_domain );
138 } else {
139 (*trusts)[i].netbios_domain = NULL;
142 if (r.domains.trusts[i].dns_ptr) {
143 (*trusts)[i].dns_domain = unistr2_tdup( mem_ctx, &r.domains.trusts[i].dns_domain );
144 } else {
145 (*trusts)[i].dns_domain = NULL;
150 done:
151 prs_mem_free(&qbuf);
152 prs_mem_free(&rbuf);
154 return result;