2 Unix SMB/CIFS implementation.
4 dcerpc utility functions
6 Copyright (C) Andrew Tridgell 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 this ndr_size_* stuff should really be auto-generated ....
29 static size_t ndr_size_epm_floor(struct epm_floor *fl)
32 if (fl->lhs.protocol == EPM_PROTOCOL_UUID) {
35 ret += fl->lhs.info.lhs_data.length;
37 ret += fl->rhs.rhs_data.length;
41 size_t ndr_size_epm_towers(struct epm_towers *towers)
45 for (i=0;i<towers->num_floors;i++) {
46 ret += ndr_size_epm_floor(&towers->floors[i]);
52 work out what TCP port to use for a given interface on a given host
54 NTSTATUS dcerpc_epm_map_tcp_port(const char *server,
55 const char *uuid, unsigned version,
58 struct dcerpc_pipe *p;
61 struct policy_handle handle;
63 struct epm_twr_t twr, *twr_r;
65 if (strcasecmp(uuid, DCERPC_EPMAPPER_UUID) == 0 ||
66 strcasecmp(uuid, DCERPC_MGMT_UUID) == 0) {
67 /* don't lookup epmapper via epmapper! */
68 *port = EPMAPPER_PORT;
72 status = dcerpc_pipe_open_tcp(&p, server, EPMAPPER_PORT);
73 if (!NT_STATUS_IS_OK(status)) {
77 /* we can use the pipes memory context here as we will have a short
79 status = dcerpc_bind_byuuid(p, p->mem_ctx,
81 DCERPC_EPMAPPER_VERSION);
82 if (!NT_STATUS_IS_OK(status)) {
90 twr.towers.num_floors = 5;
91 twr.towers.floors = talloc(p->mem_ctx, sizeof(twr.towers.floors[0]) * 5);
93 /* what I'd like for christmas ... */
95 /* an RPC interface ... */
96 twr.towers.floors[0].lhs.protocol = EPM_PROTOCOL_UUID;
97 GUID_from_string(uuid, &twr.towers.floors[0].lhs.info.uuid.uuid);
98 twr.towers.floors[0].lhs.info.uuid.version = version;
99 twr.towers.floors[0].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
101 /* encoded with NDR ... */
102 twr.towers.floors[1].lhs.protocol = EPM_PROTOCOL_UUID;
103 GUID_from_string(NDR_GUID, &twr.towers.floors[1].lhs.info.uuid.uuid);
104 twr.towers.floors[1].lhs.info.uuid.version = NDR_GUID_VERSION;
105 twr.towers.floors[1].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
107 /* on an RPC connection ... */
108 twr.towers.floors[2].lhs.protocol = EPM_PROTOCOL_RPC_C;
109 twr.towers.floors[2].lhs.info.lhs_data = data_blob(NULL, 0);
110 twr.towers.floors[2].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
112 /* on a TCP port ... */
113 twr.towers.floors[3].lhs.protocol = EPM_PROTOCOL_TCP;
114 twr.towers.floors[3].lhs.info.lhs_data = data_blob(NULL, 0);
115 twr.towers.floors[3].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 2);
117 /* on an IP link ... */
118 twr.towers.floors[4].lhs.protocol = EPM_PROTOCOL_IP;
119 twr.towers.floors[4].lhs.info.lhs_data = data_blob(NULL, 0);
120 twr.towers.floors[4].rhs.rhs_data = data_blob_talloc_zero(p->mem_ctx, 4);
122 /* with some nice pretty paper around it of course */
124 r.in.map_tower = &twr;
125 r.in.entry_handle = &handle;
127 r.out.entry_handle = &handle;
129 status = dcerpc_epm_Map(p, p->mem_ctx, &r);
130 if (!NT_STATUS_IS_OK(status)) {
131 dcerpc_pipe_close(p);
134 if (r.out.status != 0 || r.out.num_towers != 1) {
135 dcerpc_pipe_close(p);
136 return NT_STATUS_PORT_UNREACHABLE;
139 twr_r = r.out.towers[0].twr;
141 dcerpc_pipe_close(p);
142 return NT_STATUS_PORT_UNREACHABLE;
145 if (twr_r->towers.num_floors != 5 ||
146 twr_r->towers.floors[3].lhs.protocol != twr.towers.floors[3].lhs.protocol ||
147 twr_r->towers.floors[3].rhs.rhs_data.length != 2) {
148 dcerpc_pipe_close(p);
149 return NT_STATUS_PORT_UNREACHABLE;
152 *port = RSVAL(twr_r->towers.floors[3].rhs.rhs_data.data, 0);
154 dcerpc_pipe_close(p);
161 find the pipe name for a local IDL interface
163 const char *idl_pipe_name(const char *uuid, uint32 if_version)
166 for (i=0;dcerpc_pipes[i];i++) {
167 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
168 dcerpc_pipes[i]->if_version == if_version) {
169 return dcerpc_pipes[i]->name;
176 find the number of calls defined by local IDL
178 int idl_num_calls(const char *uuid, uint32 if_version)
181 for (i=0;dcerpc_pipes[i];i++) {
182 if (strcasecmp(dcerpc_pipes[i]->uuid, uuid) == 0 &&
183 dcerpc_pipes[i]->if_version == if_version) {
184 return dcerpc_pipes[i]->num_calls;
192 find a dcerpc interface by name
194 const struct dcerpc_interface_table *idl_iface_by_name(const char *name)
197 for (i=0;dcerpc_pipes[i];i++) {
198 if (strcasecmp(dcerpc_pipes[i]->name, name) == 0) {
199 return dcerpc_pipes[i];
208 push a dcerpc_packet into a blob, potentially with auth info
210 NTSTATUS dcerpc_push_auth(DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
211 struct dcerpc_packet *pkt,
212 struct dcerpc_auth *auth_info)
215 struct ndr_push *ndr;
217 ndr = ndr_push_init_ctx(mem_ctx);
219 return NT_STATUS_NO_MEMORY;
223 pkt->auth_length = auth_info->credentials.length;
225 pkt->auth_length = 0;
228 status = ndr_push_dcerpc_packet(ndr, NDR_SCALARS|NDR_BUFFERS, pkt);
229 if (!NT_STATUS_IS_OK(status)) {
234 status = ndr_push_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, auth_info);
237 *blob = ndr_push_blob(ndr);
239 /* fill in the frag length */
240 SSVAL(blob->data, DCERPC_FRAG_LEN_OFFSET, blob->length);