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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/*
Unix SMB/Netbios implementation.
Version 1.9.
SMB msrpcent generic functions
Copyright (C) Andrew Tridgell 1994-1999
Copyright (C) Luke Kenneth Casson Leighton 1996-1999
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.
*/
#define NO_SYSLOG
#include "includes.h"
extern int DEBUGLEVEL;
/****************************************************************************
read an msrpc pdu from a fd.
The timeout is in milliseconds.
****************************************************************************/
BOOL receive_msrpc(int fd, prs_struct *data, unsigned int timeout)
{
BOOL ok;
size_t len;
RPC_HDR hdr;
prs_init(data, 0, NULL, True);
ok = prs_read(data, fd, 16, timeout);
if (!ok)
{
prs_mem_free(data);
return False;
}
if (!smb_io_rpc_hdr("hdr", &hdr, data, 0))
{
prs_mem_free(data);
return False;
}
len = hdr.frag_len - 16;
if (len > 0)
{
ok = prs_read(data, fd, hdr.frag_len, 0);
if (!ok)
{
prs_mem_free(data);
return False;
}
data->data_offset = hdr.frag_len;
return True;
}
prs_mem_free(data);
return False;
}
/****************************************************************************
send an smb to a fd and re-establish if necessary
****************************************************************************/
BOOL msrpc_send(int fd, prs_struct *ps)
{
size_t len = ps != NULL ? ps->buffer_size : 0;
size_t nwritten=0;
ssize_t ret;
char *outbuf = ps->data_p;
DEBUG(10,("msrpc_send_prs: data: %p len %d\n", outbuf, len));
dbgflush();
dump_data(10, outbuf, len);
while (nwritten < len)
{
ret = write_socket(fd,outbuf+nwritten,len - nwritten);
if (ret <= 0)
{
DEBUG(0,("Error writing %d msrpc bytes. %d.\n",
len,ret));
prs_mem_free(ps);
return False;
}
nwritten += ret;
}
prs_mem_free(ps);
return True;
}
/****************************************************************************
receive msrpc packet
****************************************************************************/
BOOL msrpc_receive(int fd, prs_struct *ps)
{
int len;
DEBUG(10,("msrpc_receive: %d\n", __LINE__));
if (!receive_msrpc(fd, ps, 0))
{
return False;
}
len = ps->buffer_size;
if (ps->data_p == NULL || len <= 0)
{
return False;
}
dump_data(10, ps->data_p, len);
DEBUG(10,("msrpc_receive: len %d\n", len));
return True;
}
/****************************************************************************
open the msrpcent sockets
****************************************************************************/
BOOL msrpc_connect(struct msrpc_state *msrpc, const char *pipe_name)
{
fstring path;
slprintf(path, sizeof(path)-1, "%s/.msrpc/%s", LOCKDIR, pipe_name);
fstrcpy(msrpc->pipe_name, pipe_name);
msrpc->fd = open_pipe_sock(path);
if (msrpc->fd == -1)
{
return False;
}
return True;
}
/****************************************************************************
initialise a msrpcent structure
****************************************************************************/
void msrpc_init_creds(struct msrpc_state *msrpc, const struct user_creds *usr)
{
copy_user_creds(&msrpc->usr, usr);
}
/****************************************************************************
close the socket descriptor
****************************************************************************/
void msrpc_close_socket(struct msrpc_state *msrpc)
{
if (msrpc->fd != -1)
{
close(msrpc->fd);
}
msrpc->fd = -1;
}
/****************************************************************************
set socket options on a open connection
****************************************************************************/
void msrpc_sockopt(struct msrpc_state *msrpc, char *options)
{
set_socket_options(msrpc->fd, options);
}
static BOOL msrpc_authenticate(struct msrpc_state *msrpc,
struct user_creds *usr)
{
struct msrpc_state msrpc_redir;
int sock = msrpc->fd;
char *data;
prs_struct ps;
uint32 len;
char *in = msrpc->inbuf;
char *out = msrpc->outbuf;
uint16 command;
command = usr != NULL ? AGENT_CMD_CON : AGENT_CMD_CON_ANON;
if (!create_user_creds(&ps, msrpc->pipe_name, 0x0, command,
msrpc->pid, usr))
{
DEBUG(0,("could not parse credentials\n"));
close(sock);
return False;
}
len = ps.data_offset;
data = ps.data_p;
SIVAL(data, 0, len);
#ifdef DEBUG_PASSWORD
DEBUG(100,("data len: %d\n", len));
dump_data(100, data, len);
#endif
if (write(sock, data, len) <= 0)
{
DEBUG(0,("write failed\n"));
return False;
}
if (msrpc->redirect)
{
len = read(sock, &msrpc_redir, sizeof(msrpc_redir));
if (len != sizeof(msrpc_redir))
{
DEBUG(0,("read failed\n"));
return False;
}
memcpy(msrpc, &msrpc_redir, sizeof(msrpc_redir));
msrpc->inbuf = in;
msrpc->outbuf = out;
msrpc->fd = sock;
msrpc->usr.reuse = False;
}
else
{
uint32 status;
len = read(sock, &status, sizeof(status));
return len == sizeof(status) && status == 0x0;
}
return True;
}
static BOOL msrpc_init_redirect(struct msrpc_state *msrpc,
const char* pipe_name,
struct user_creds *usr)
{
int sock;
fstring path;
slprintf(path, sizeof(path)-1, "/tmp/.msrpc/.%s/agent", pipe_name);
sock = open_pipe_sock(path);
if (sock < 0)
{
return False;
}
msrpc->fd = sock;
if (!msrpc_authenticate(msrpc, usr))
{
DEBUG(0,("authenticate failed\n"));
close(msrpc->fd);
msrpc->fd = -1;
return False;
}
return True;
}
BOOL msrpc_connect_auth(struct msrpc_state *msrpc,
uint32 pid,
const char* pipename,
const struct user_creds *usr)
{
ZERO_STRUCTP(msrpc);
if (!msrpc_initialise(msrpc, pid))
{
DEBUG(0,("unable to initialise msrpcent connection.\n"));
return False;
}
msrpc_init_creds(msrpc, usr);
if (!msrpc_establish_connection(msrpc, pipename))
{
msrpc_shutdown(msrpc);
return False;
}
return True;
}
/****************************************************************************
initialise a msrpcent structure
****************************************************************************/
struct msrpc_state *msrpc_initialise(struct msrpc_state *msrpc, uint32 pid)
{
if (!msrpc) {
msrpc = (struct msrpc_state *)malloc(sizeof(*msrpc));
if (!msrpc)
return NULL;
ZERO_STRUCTP(msrpc);
}
if (msrpc->initialised) {
msrpc_shutdown(msrpc);
}
ZERO_STRUCTP(msrpc);
msrpc->fd = -1;
msrpc->outbuf = (char *)malloc(CLI_BUFFER_SIZE+4);
msrpc->inbuf = (char *)malloc(CLI_BUFFER_SIZE+4);
if (!msrpc->outbuf || !msrpc->inbuf)
{
return False;
}
msrpc->initialised = 1;
msrpc_init_creds(msrpc, NULL);
msrpc->pid = pid;
return msrpc;
}
/****************************************************************************
shutdown a msrpcent structure
****************************************************************************/
void msrpc_shutdown(struct msrpc_state *msrpc)
{
DEBUG(10,("msrpc_shutdown\n"));
if (msrpc->outbuf)
{
free(msrpc->outbuf);
}
if (msrpc->inbuf)
{
free(msrpc->inbuf);
}
msrpc_close_socket(msrpc);
memset(msrpc, 0, sizeof(*msrpc));
}
/****************************************************************************
establishes a connection right up to doing tconX, reading in a password.
****************************************************************************/
BOOL msrpc_establish_connection(struct msrpc_state *msrpc,
const char *pipe_name)
{
DEBUG(5,("msrpc_establish_connection: connecting to %s (%s) - %s\n",
pipe_name,
msrpc->usr.ntc.user_name, msrpc->usr.ntc.domain));
/* establish connection */
if ((!msrpc->initialised))
{
return False;
}
if (msrpc->fd == -1 && msrpc->redirect)
{
if (msrpc_init_redirect(msrpc, pipe_name, &msrpc->usr))
{
DEBUG(10,("msrpc_establish_connection: redirected OK\n"));
return True;
}
else
{
DEBUG(10,("redirect FAILED\n"));
return False;
}
}
if (msrpc->fd == -1)
{
if (!msrpc_connect(msrpc, pipe_name))
{
DEBUG(1,("msrpc_establish_connection: failed %s)\n",
pipe_name));
return False;
}
}
if (!msrpc_authenticate(msrpc, &msrpc->usr))
{
DEBUG(0,("authenticate failed\n"));
close(msrpc->fd);
msrpc->fd = -1;
return False;
}
return True;
}
|