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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
/*
* ML-RPC Client handle interface and support functions.
*/
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/poll.h>
#include <errno.h>
#include <strings.h>
#include <unistd.h>
#include <netsmb/smbfs_api.h>
#include <smb/ntstatus.h>
#include <libmlrpc.h>
#include <assert.h>
static int ndr_xa_init(ndr_client_t *, ndr_xa_t *);
static int ndr_xa_exchange(ndr_client_t *, ndr_xa_t *);
static int ndr_xa_read(ndr_client_t *, ndr_xa_t *);
static void ndr_xa_preserve(ndr_client_t *, ndr_xa_t *);
static void ndr_xa_destruct(ndr_client_t *, ndr_xa_t *);
static void ndr_xa_release(ndr_client_t *);
/* See notes in mlrpc_clh_bind */
int rpc_pipe_open_retries = 10;
/*
* Create an RPC client binding handle using the given smb_ctx.
* That context must already have a session and tree connected.
*
* Returns zero or an errno value.
*/
int
mlrpc_clh_create(mlrpc_handle_t *handle, void *ctx)
{
ndr_client_t *clnt = NULL;
if (ctx == NULL)
return (EINVAL);
/*
* Allocate...
*/
if ((clnt = malloc(sizeof (*clnt))) == NULL)
return (ENOMEM);
bzero(clnt, sizeof (*clnt));
clnt->xa_fd = -1;
/*
* Setup the transport functions.
* Always a named pipe (for now).
*/
clnt->xa_private = ctx;
clnt->xa_init = ndr_xa_init;
clnt->xa_exchange = ndr_xa_exchange;
clnt->xa_read = ndr_xa_read;
clnt->xa_preserve = ndr_xa_preserve;
clnt->xa_destruct = ndr_xa_destruct;
clnt->xa_release = ndr_xa_release;
/* See _is_bind_handle */
clnt->handle = &handle->handle;
ndr_svc_binding_pool_init(&clnt->binding_list,
clnt->binding_pool, NDR_N_BINDING_POOL);
if ((clnt->heap = ndr_heap_create()) == NULL)
goto nomem;
/* success! */
bzero(handle, sizeof (*handle));
handle->clnt = clnt;
return (0);
nomem:
free(clnt);
return (ENOMEM);
}
/*
* This call must be made to initialize an RPC client structure and bind
* to the remote service before any RPCs can be exchanged with that service.
*
* The mlrpc_handle_t is a wrapper that is used to associate an RPC handle
* with the client context for an instance of the interface. The handle
* is zeroed to ensure that it doesn't look like a valid handle -
* handle content is provided by the remove service.
*
* The client points to this top-level handle so that we know when to
* unbind and teardown the connection. As each handle is initialized it
* will inherit a reference to the client context.
*
*
* Similar to MSRPC RpcBindingBind()
*
* Returns 0 or an NT_STATUS: (failed in...)
*
* RPC_NT_SERVER_TOO_BUSY (open pipe)
* RPC_NT_SERVER_UNAVAILABLE (open pipe)
* NT_STATUS_ACCESS_DENIED (open pipe)
* NT_STATUS_INVALID_PARAMETER (rpc bind)
* NT_STATUS_INTERNAL_ERROR (bad args etc)
* NT_STATUS_NO_MEMORY
*/
uint32_t
mlrpc_clh_bind(mlrpc_handle_t *handle, ndr_service_t *svc)
{
ndr_client_t *clnt = NULL;
struct smb_ctx *ctx = NULL;
uint32_t status = 0;
int fd = -1;
int rc, retries;
if ((clnt = handle->clnt) == NULL)
return (NT_STATUS_INTERNAL_ERROR);
if ((ctx = clnt->xa_private) == NULL)
return (NT_STATUS_INTERNAL_ERROR);
if (clnt->xa_fd != -1)
return (NT_STATUS_INTERNAL_ERROR);
/*
* Open the named pipe.
*
* Sometimes a DC may return NT_STATUS_PIPE_NOT_AVAILABLE for
* the first few seconds during service auto-start. The client
* translates that to EBUSY, so when we see that, wait a bit
* and retry the open for up to rpc_pipe_open_retries. If we
* fail even after retries, return RPC_NT_SERVER_TOO_BUSY,
* which is how callers of this layer expect that reported.
* We try up to 10 times, with a 0.5 sec. wait after each
* BUSY failure, giving a total wait here of 5 sec.
*/
retries = rpc_pipe_open_retries;
retry_open:
fd = smb_fh_open(ctx, svc->endpoint, O_RDWR);
if (fd < 0) {
rc = errno;
switch (rc) {
case EBUSY:
if (--retries > 0) {
(void) poll(NULL, 0, 500);
goto retry_open;
}
status = RPC_NT_SERVER_TOO_BUSY;
break;
case EACCES:
status = NT_STATUS_ACCESS_DENIED;
break;
default:
status = RPC_NT_SERVER_UNAVAILABLE;
break;
}
return (status);
}
clnt->xa_fd = fd;
/* Paranoia, in case of re-bind. */
bzero(&handle->handle, sizeof (ndr_hdid_t));
/*
* Do the OtW RPC bind.
*/
rc = ndr_clnt_bind(clnt, svc, &clnt->binding);
switch (rc) {
case NDR_DRC_FAULT_OUT_OF_MEMORY:
status = NT_STATUS_NO_MEMORY;
break;
case NDR_DRC_FAULT_API_SERVICE_INVALID:
/* svc->..._uuid parse errors */
status = NT_STATUS_INTERNAL_ERROR;
break;
default:
if (NDR_DRC_IS_FAULT(rc)) {
status = RPC_NT_PROTOCOL_ERROR;
break;
}
/* FALLTHROUGH */
case NDR_DRC_OK:
status = NT_STATUS_SUCCESS;
}
if (status != 0) {
if (fd != -1)
(void) smb_fh_close(fd);
clnt->xa_fd = -1;
}
return (status);
}
/*
* Unbind and close the pipe to an RPC service.
*
* Similar to MSRPC RpcBindingUnbind()
* This should be called after a dropped connection.
*/
void
mlrpc_clh_unbind(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
if (clnt->xa_fd != -1) {
(void) smb_fh_close(clnt->xa_fd);
clnt->xa_fd = -1;
}
}
/*
* If the heap has been preserved we need to go through an xa release.
* The heap is preserved during an RPC call because that's where data
* returned from the server is stored.
*
* Otherwise we destroy the heap directly.
*
* Returns the xa_private pointer (if non-NULL) to inform the caller
* that it can now be destroyed.
*/
void *
mlrpc_clh_free(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
void *private;
if (clnt == NULL)
return (NULL);
/*
* Should never get an unbind on inherited handles.
* Callers of ndr_inherit_handle() check handles
* with ndr_is_bind_handle() before calling this.
*
* Maybe make this function more tolerant?
*/
assert(handle->clnt->handle == &handle->handle);
mlrpc_clh_unbind(handle);
if (clnt->heap_preserved)
ndr_clnt_free_heap(clnt); /* xa_release */
else
ndr_heap_destroy(clnt->heap);
/*
* Note: Caller will free the smb_ctx stored in
* clnt->xa_private (or possibly reuse it).
*/
private = clnt->xa_private;
free(clnt);
bzero(handle, sizeof (*handle));
return (private);
}
/*
* Call the RPC function identified by opnum. The remote service is
* identified by the handle, which should have been initialized by
* ndr_rpc_bind.
*
* If the RPC call is successful (returns 0), the caller must call
* ndr_rpc_release to release the heap. Otherwise, we release the
* heap here.
*/
int
ndr_rpc_call(mlrpc_handle_t *handle, int opnum, void *params)
{
ndr_client_t *clnt = handle->clnt;
int rc;
if (ndr_rpc_get_heap(handle) == NULL)
return (-1);
rc = ndr_clnt_call(clnt->binding, opnum, params);
/*
* Always clear the nonull flag to ensure
* it is not applied to subsequent calls.
*/
clnt->nonull = B_FALSE;
if (NDR_DRC_IS_FAULT(rc)) {
ndr_rpc_release(handle);
return (-1);
}
return (0);
}
/*
* Outgoing strings should not be null terminated.
*/
void
ndr_rpc_set_nonull(mlrpc_handle_t *handle)
{
handle->clnt->nonull = B_TRUE;
}
/*
* Get the session key from a bound RPC client handle.
*
* The key returned is the 16-byte "user session key"
* established by the underlying authentication protocol
* (either Kerberos or NTLM). This key is needed for
* SAM RPC calls such as SamrSetInformationUser, etc.
* See [MS-SAMR] sections: 2.2.3.3, 2.2.7.21, 2.2.7.25.
*
* Returns zero (success) or an errno.
*/
int
ndr_rpc_get_ssnkey(mlrpc_handle_t *handle, uchar_t *key, size_t len)
{
ndr_client_t *clnt = handle->clnt;
if (clnt == NULL || clnt->xa_fd == -1)
return (EINVAL);
return (smb_fh_getssnkey(clnt->xa_fd, key, len));
}
void *
ndr_rpc_malloc(mlrpc_handle_t *handle, size_t size)
{
ndr_heap_t *heap;
if ((heap = ndr_rpc_get_heap(handle)) == NULL)
return (NULL);
return (ndr_heap_malloc(heap, size));
}
ndr_heap_t *
ndr_rpc_get_heap(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
if (clnt->heap == NULL)
clnt->heap = ndr_heap_create();
return (clnt->heap);
}
/*
* Must be called by RPC clients to free the heap after a successful RPC
* call, i.e. ndr_rpc_call returned 0. The caller should take a copy
* of any data returned by the RPC prior to calling this function because
* returned data is in the heap.
*/
void
ndr_rpc_release(mlrpc_handle_t *handle)
{
ndr_client_t *clnt = handle->clnt;
if (clnt->heap_preserved)
ndr_clnt_free_heap(clnt);
else
ndr_heap_destroy(clnt->heap);
clnt->heap = NULL;
}
/*
* Returns true if the handle is null.
* Otherwise returns false.
*/
boolean_t
ndr_is_null_handle(mlrpc_handle_t *handle)
{
static const ndr_hdid_t hdid0 = {0};
if (handle == NULL || handle->clnt == NULL)
return (B_TRUE);
if (!memcmp(&handle->handle, &hdid0, sizeof (hdid0)))
return (B_TRUE);
return (B_FALSE);
}
/*
* Returns true if the handle is the top level bind handle.
* Otherwise returns false.
*/
boolean_t
ndr_is_bind_handle(mlrpc_handle_t *handle)
{
return (handle->clnt->handle == &handle->handle);
}
/*
* Pass the client reference from parent to child.
*/
void
ndr_inherit_handle(mlrpc_handle_t *child, mlrpc_handle_t *parent)
{
child->clnt = parent->clnt;
}
/*
* ndr_rpc_status remains in libmlsvc mlsvc_client.c
*/
/*
* The following functions provide the client callback interface.
* If the caller hasn't provided a heap, create one here.
*/
static int
ndr_xa_init(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_stream_t *recv_nds = &mxa->recv_nds;
ndr_stream_t *send_nds = &mxa->send_nds;
ndr_heap_t *heap = clnt->heap;
int rc;
if (heap == NULL) {
if ((heap = ndr_heap_create()) == NULL)
return (-1);
clnt->heap = heap;
}
mxa->heap = heap;
rc = nds_initialize(send_nds, 0, NDR_MODE_CALL_SEND, heap);
if (rc == 0)
rc = nds_initialize(recv_nds, NDR_PDU_SIZE_HINT_DEFAULT,
NDR_MODE_RETURN_RECV, heap);
if (rc != 0) {
nds_destruct(&mxa->recv_nds);
nds_destruct(&mxa->send_nds);
ndr_heap_destroy(mxa->heap);
mxa->heap = NULL;
clnt->heap = NULL;
return (-1);
}
if (clnt->nonull)
NDS_SETF(send_nds, NDS_F_NONULL);
return (0);
}
/*
* This is the entry pointy for an RPC client call exchange with
* a server, which will result in an smbrdr SmbTransact request.
*
* SmbTransact should return the number of bytes received, which
* we record as the PDU size, or a negative error code.
*/
static int
ndr_xa_exchange(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_stream_t *recv_nds = &mxa->recv_nds;
ndr_stream_t *send_nds = &mxa->send_nds;
int err, more, nbytes;
nbytes = recv_nds->pdu_max_size;
err = smb_fh_xactnp(clnt->xa_fd,
send_nds->pdu_size, (char *)send_nds->pdu_base_offset,
&nbytes, (char *)recv_nds->pdu_base_offset, &more);
if (err) {
recv_nds->pdu_size = 0;
return (-1);
}
recv_nds->pdu_size = nbytes;
return (0);
}
/*
* This entry point will be invoked if the xa-exchange response contained
* only the first fragment of a multi-fragment response. The RPC client
* code will then make repeated xa-read requests to obtain the remaining
* fragments, which will result in smbrdr SmbReadX requests.
*
* SmbReadX should return the number of bytes received, in which case we
* expand the PDU size to include the received data, or a negative error
* code.
*/
static int
ndr_xa_read(ndr_client_t *clnt, ndr_xa_t *mxa)
{
ndr_stream_t *nds = &mxa->recv_nds;
int len;
int nbytes;
if ((len = (nds->pdu_max_size - nds->pdu_size)) < 0)
return (-1);
nbytes = smb_fh_read(clnt->xa_fd, 0, len,
(char *)nds->pdu_base_offset + nds->pdu_size);
if (nbytes < 0)
return (-1);
nds->pdu_size += nbytes;
if (nds->pdu_size > nds->pdu_max_size) {
nds->pdu_size = nds->pdu_max_size;
return (-1);
}
return (nbytes);
}
/*
* Preserve the heap so that the client application has access to data
* returned from the server after an RPC call.
*/
static void
ndr_xa_preserve(ndr_client_t *clnt, ndr_xa_t *mxa)
{
assert(clnt->heap == mxa->heap);
clnt->heap_preserved = B_TRUE;
mxa->heap = NULL;
}
/*
* Dispose of the transaction streams. If the heap has not been
* preserved, we can destroy it here.
*/
static void
ndr_xa_destruct(ndr_client_t *clnt, ndr_xa_t *mxa)
{
nds_destruct(&mxa->recv_nds);
nds_destruct(&mxa->send_nds);
if (!clnt->heap_preserved) {
ndr_heap_destroy(mxa->heap);
mxa->heap = NULL;
clnt->heap = NULL;
}
}
/*
* Dispose of a preserved heap.
*/
static void
ndr_xa_release(ndr_client_t *clnt)
{
if (clnt->heap_preserved) {
ndr_heap_destroy(clnt->heap);
clnt->heap = NULL;
clnt->heap_preserved = B_FALSE;
}
}
|