summaryrefslogtreecommitdiff
path: root/source4/libcli/clidcerpc.c
blob: fffd4e094114f42ae84d324a65188ac26a878703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* 
   Unix SMB/CIFS implementation.
   raw dcerpc operations

   Copyright (C) Tim Potter, 2003
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

struct cli_dcerpc_pipe *cli_dcerpc_pipe_init(struct cli_tree *tree)
{
	struct cli_dcerpc_pipe *p;

	TALLOC_CTX *mem_ctx = talloc_init("cli_dcerpc_tree");
	if (mem_ctx == NULL)
		return NULL;

	p = talloc_zero(mem_ctx, sizeof(*p));
	if (!p) {
		talloc_destroy(mem_ctx);
		return NULL;
	}

	p->mem_ctx = mem_ctx;
	p->tree = tree;
	p->tree->reference_count++;

	return p;
}

void cli_dcerpc_pipe_close(struct cli_dcerpc_pipe *p)
{
	if (!p) return;
	p->reference_count--;
	if (p->reference_count <= 0) {
		cli_tree_close(p->tree);
		talloc_destroy(p->mem_ctx);
	}
}

static void init_dcerpc_hdr(struct dcerpc_hdr *hdr, uint8 ptype,
			    uint8 pfc_flags, uint32 call_id)
{
        hdr->rpc_vers = 5;
        hdr->rpc_vers_minor = 0;
        hdr->ptype = ptype;
        hdr->pfc_flags = pfc_flags;
        hdr->drep[0] = 0x10; /* Little endian */
        hdr->call_id = call_id;
}

struct syntax_id trans_synt_v2 = 
{
	{
		0x8a885d04, 0x1ceb, 0x11c9,
		{ 0x9f, 0xe8, 0x08, 0x00,
		0x2b, 0x10, 0x48, 0x60 }
	}, 0x02
};

struct syntax_id synt_netlogon_v2 =
{
	{
		0x8a885d04, 0x1ceb, 0x11c9,
		{ 0x9f, 0xe8, 0x08, 0x00,
		0x2b, 0x10, 0x48, 0x60 }
	}, 0x02
};

struct syntax_id synt_wkssvc_v1 =
{
	{
		0x6bffd098, 0xa112, 0x3610,
		{ 0x98, 0x33, 0x46, 0xc3,
		0xf8, 0x7e, 0x34, 0x5a }
	}, 0x01
};

struct syntax_id synt_srvsvc_v3 =
{
	{
		0x4b324fc8, 0x1670, 0x01d3,
		{ 0x12, 0x78, 0x5a, 0x47,
		0xbf, 0x6e, 0xe1, 0x88 }
	}, 0x03
};

struct syntax_id synt_lsarpc_v0 =
{
	{
		0x12345778, 0x1234, 0xabcd,
		{ 0xef, 0x00, 0x01, 0x23,
		0x45, 0x67, 0x89, 0xab }
	}, 0x00
};

struct syntax_id synt_lsarpc_v0_ds =
{
	{
		0x3919286a, 0xb10c, 0x11d0,
		{ 0x9b, 0xa8, 0x00, 0xc0,
		0x4f, 0xd9, 0x2e, 0xf5 }
	}, 0x00
};

struct syntax_id synt_samr_v1 =
{
	{
		0x12345778, 0x1234, 0xabcd,
		{ 0xef, 0x00, 0x01, 0x23,
		0x45, 0x67, 0x89, 0xac }
	}, 0x01
};

struct syntax_id synt_netlogon_v1 =
{
	{
		0x12345678, 0x1234, 0xabcd,
		{ 0xef, 0x00, 0x01, 0x23,
		0x45, 0x67, 0xcf, 0xfb }
	}, 0x01
};

struct syntax_id synt_winreg_v1 =
{
	{
		0x338cd001, 0x2244, 0x31f1,
		{ 0xaa, 0xaa, 0x90, 0x00,
		0x38, 0x00, 0x10, 0x03 }
	}, 0x01
};

struct syntax_id synt_spoolss_v1 =
{
	{
		0x12345678, 0x1234, 0xabcd,
		{ 0xef, 0x00, 0x01, 0x23,
		0x45, 0x67, 0x89, 0xab }
	}, 0x01
};

struct syntax_id synt_netdfs_v3 =
{
        {
                0x4fc742e0, 0x4a10, 0x11cf,
                { 0x82, 0x73, 0x00, 0xaa,
                  0x00, 0x4a, 0xe6, 0x73 }
        }, 0x03
};

struct known_pipes {
	const char *client_pipe;
	struct p_ctx_list ctx_list;
};

const struct known_pipes known_pipes[] =
{
	{ PIPE_LSARPC  , { 0, 1, &synt_lsarpc_v0,   &trans_synt_v2 }},
	{ PIPE_SAMR    , { 0, 1, &synt_samr_v1,     &trans_synt_v2 }},
	{ PIPE_NETLOGON, { 0, 1, &synt_netlogon_v1, &trans_synt_v2 }},
	{ PIPE_SRVSVC  , { 0, 1, &synt_srvsvc_v3 ,  &trans_synt_v2 }},
	{ PIPE_WKSSVC  , { 0, 1, &synt_wkssvc_v1 ,  &trans_synt_v2 }},
	{ PIPE_WINREG  , { 0, 1, &synt_winreg_v1 ,  &trans_synt_v2 }},
	{ PIPE_SPOOLSS , { 0, 1, &synt_spoolss_v1,  &trans_synt_v2 }},
	{ PIPE_NETDFS  , { 0, 1, &synt_netdfs_v3 ,  &trans_synt_v2 }},
	{ NULL         , { 0, 0, NULL,              NULL }}
};

/* Perform a bind using the given syntaxes */

NTSTATUS cli_dcerpc_bind(struct cli_dcerpc_pipe *p, int num_contexts,
			 struct p_ctx_list *ctx_list)
{
	TALLOC_CTX *mem_ctx;
        struct dcerpc_bind parms;
	NTSTATUS status;

	mem_ctx = talloc_init("cli_dcerpc_bind");

        ZERO_STRUCT(parms);
 
	init_dcerpc_hdr(&parms.in.hdr, RPC_BIND, RPC_FLG_FIRST|RPC_FLG_LAST,
			p->call_id++);

        parms.in.max_xmit_frag = 5680;
        parms.in.max_recv_frag = 5680;
        parms.in.num_contexts = num_contexts;
	parms.in.ctx_list = ctx_list;
 
        status = dcerpc_raw_bind(p, &parms);

	talloc_destroy(mem_ctx);

	return status;	
}

/* Perform a bind using the given well-known pipe name */

NTSTATUS cli_dcerpc_bind_byname(struct cli_dcerpc_pipe *p, 
				const char *pipe_name)
{
	const struct known_pipes *pi;

	for (pi = known_pipes; pi->client_pipe; pi++) {
		if (strequal(&pi->client_pipe[5], pipe_name))
			break;
	}
	
	if (pi->client_pipe == NULL)
		return NT_STATUS_UNSUCCESSFUL;


	return cli_dcerpc_bind(p, 1, &pi->ctx_list);
}

NTSTATUS cli_dcerpc_request(struct cli_dcerpc_pipe *p, uint16 opnum,
			    DATA_BLOB stub_data)
{
	TALLOC_CTX *mem_ctx;
	struct dcerpc_request parms;
	NTSTATUS status;

	mem_ctx = talloc_init("cli_dcerpc_request");

	ZERO_STRUCT(parms);
	
	init_dcerpc_hdr(&parms.in.hdr, RPC_REQUEST, 
			RPC_FLG_FIRST|RPC_FLG_LAST, p->call_id++);

	parms.in.alloc_hint = 0;
	parms.in.cont_id = 0;
	parms.in.opnum = opnum;
	parms.in.stub_data = stub_data;

	status = dcerpc_raw_request(p, &parms);	

	talloc_destroy(mem_ctx);

	return status;
}