Print this page
1575 untangle libmlrpc ... (libmlrpc)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/smbsrv/libmlrpc/common/ndr_svc.c
+++ new/usr/src/lib/libmlrpc/common/ndr_svc.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 *
25 25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
26 26 */
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
27 27
28 28 #include <uuid/uuid.h>
29 29 #include <ctype.h>
30 30 #include <synch.h>
31 31 #include <stdio.h>
32 32 #include <unistd.h>
33 33 #include <string.h>
34 34 #include <strings.h>
35 35 #include <assert.h>
36 36
37 -#include <smbsrv/libsmb.h>
38 -#include <smbsrv/libmlrpc.h>
37 +#include <libmlrpc.h>
39 38
40 39
41 40 /*
42 41 * Global list of allocated handles. Handles are used in various
43 42 * server-side RPC functions: typically, issued when a service is
44 43 * opened and obsoleted when it is closed. Clients should treat
45 44 * handles as opaque data.
46 45 */
47 46 static ndr_handle_t *ndr_handle_list;
48 47 static mutex_t ndr_handle_lock;
49 48
50 49 /*
51 50 * Table of registered services.
52 51 */
53 52 #define NDR_MAX_SERVICES 32
54 53 static ndr_service_t *ndr_services[NDR_MAX_SERVICES];
55 54
56 55 /*
57 56 * Register a service.
58 57 *
59 58 * Returns:
60 59 * 0 Success
61 60 * -1 Duplicate service
62 61 * -2 Duplicate name
63 62 * -3 Table overflow
64 63 */
65 64 int
66 65 ndr_svc_register(ndr_service_t *svc)
67 66 {
68 67 ndr_service_t *p;
69 68 int free_slot = -1;
70 69 int i;
71 70
72 71 for (i = 0; i < NDR_MAX_SERVICES; i++) {
73 72 if ((p = ndr_services[i]) == NULL) {
74 73 if (free_slot < 0)
75 74 free_slot = i;
76 75 continue;
77 76 }
78 77
79 78 if (p == svc)
80 79 return (-1);
81 80
82 81 if (strcasecmp(p->name, svc->name) == 0)
83 82 return (-2);
84 83 }
85 84
86 85 if (free_slot < 0)
87 86 return (-3);
88 87
89 88 ndr_services[free_slot] = svc;
90 89 return (0);
91 90 }
92 91
93 92 void
94 93 ndr_svc_unregister(ndr_service_t *svc)
95 94 {
96 95 int i;
97 96
98 97 for (i = 0; i < NDR_MAX_SERVICES; i++) {
99 98 if (ndr_services[i] == svc)
100 99 ndr_services[i] = NULL;
101 100 }
102 101 }
103 102
104 103 ndr_stub_table_t *
105 104 ndr_svc_find_stub(ndr_service_t *svc, int opnum)
106 105 {
107 106 ndr_stub_table_t *ste;
108 107
109 108 for (ste = svc->stub_table; ste->func; ste++) {
110 109 if (ste->opnum == opnum)
111 110 return (ste);
112 111 }
113 112
114 113 return (NULL);
115 114 }
116 115
117 116 ndr_service_t *
118 117 ndr_svc_lookup_name(const char *name)
119 118 {
120 119 ndr_service_t *svc;
121 120 int i;
122 121
123 122 for (i = 0; i < NDR_MAX_SERVICES; i++) {
124 123 if ((svc = ndr_services[i]) == NULL)
125 124 continue;
126 125
127 126 if (strcasecmp(name, svc->name) != 0)
128 127 continue;
129 128
130 129 ndo_printf(0, 0, "%s %s", svc->name, svc->desc);
131 130 return (svc);
132 131 }
133 132
134 133 return (NULL);
135 134 }
136 135
137 136 ndr_service_t *
138 137 ndr_svc_lookup_uuid(ndr_uuid_t *as_uuid, int as_vers,
139 138 ndr_uuid_t *ts_uuid, int ts_vers)
140 139 {
141 140 ndr_service_t *svc;
142 141 char abstract_syntax[UUID_PRINTABLE_STRING_LENGTH];
143 142 char transfer_syntax[UUID_PRINTABLE_STRING_LENGTH];
144 143 int i;
145 144
146 145 if (as_uuid)
147 146 ndr_uuid_unparse(as_uuid, abstract_syntax);
148 147
149 148 if (ts_uuid)
150 149 ndr_uuid_unparse(ts_uuid, transfer_syntax);
151 150
152 151 for (i = 0; i < NDR_MAX_SERVICES; i++) {
153 152 if ((svc = ndr_services[i]) == NULL)
154 153 continue;
155 154
156 155 if (as_uuid) {
157 156 if (svc->abstract_syntax_uuid == 0)
158 157 continue;
159 158
160 159 if (svc->abstract_syntax_version != as_vers)
161 160 continue;
162 161
163 162 if (strcasecmp(abstract_syntax,
164 163 svc->abstract_syntax_uuid))
165 164 continue;
166 165 }
167 166
168 167 if (ts_uuid) {
169 168 if (svc->transfer_syntax_uuid == 0)
170 169 continue;
171 170
172 171 if (svc->transfer_syntax_version != ts_vers)
173 172 continue;
174 173
175 174 if (strcasecmp(transfer_syntax,
176 175 svc->transfer_syntax_uuid))
177 176 continue;
178 177 }
179 178
180 179 ndo_printf(0, 0, "%s %s", svc->name, svc->desc);
181 180 return (svc);
182 181 }
183 182
184 183 ndo_printf(0, 0, "ndr_svc_lookup_uuid: unknown service");
185 184 ndo_printf(0, 0, "abstract=%s v%d, transfer=%s v%d",
186 185 abstract_syntax, as_vers, transfer_syntax, ts_vers);
187 186 return (NULL);
188 187 }
189 188
190 189 /*
191 190 * Allocate a handle for use with the server-side RPC functions.
192 191 *
193 192 * An arbitrary caller context can be associated with the handle
194 193 * via data; it will not be dereferenced by the handle API.
195 194 */
196 195 ndr_hdid_t *
197 196 ndr_hdalloc(const ndr_xa_t *xa, const void *data)
198 197 {
199 198 static ndr_hdid_t id;
200 199 ndr_handle_t *hd;
201 200 uuid_t uu;
202 201
203 202 if ((hd = malloc(sizeof (ndr_handle_t))) == NULL)
204 203 return (NULL);
205 204
206 205 if (id.data2 == 0) {
207 206 uuid_generate_random(uu);
208 207 bcopy(uu, &id.data2, sizeof (uuid_t));
209 208 id.data1 = 0;
210 209 id.data2 = 0;
211 210 }
212 211
213 212 ++id.data2;
214 213
215 214 bcopy(&id, &hd->nh_id, sizeof (ndr_hdid_t));
216 215 hd->nh_pipe = xa->pipe;
217 216 hd->nh_svc = xa->binding->service;
218 217 hd->nh_data = (void *)data;
219 218 hd->nh_data_free = NULL;
220 219
221 220 (void) mutex_lock(&ndr_handle_lock);
222 221 hd->nh_next = ndr_handle_list;
223 222 ndr_handle_list = hd;
224 223 (void) mutex_unlock(&ndr_handle_lock);
225 224
226 225 return (&hd->nh_id);
227 226 }
228 227
229 228 /*
230 229 * Remove a handle from the global list and free it.
231 230 */
232 231 void
233 232 ndr_hdfree(const ndr_xa_t *xa, const ndr_hdid_t *id)
234 233 {
235 234 ndr_service_t *svc = xa->binding->service;
236 235 ndr_handle_t *hd;
237 236 ndr_handle_t **pphd;
238 237
239 238 assert(id);
240 239
241 240 (void) mutex_lock(&ndr_handle_lock);
242 241 pphd = &ndr_handle_list;
243 242
244 243 while (*pphd) {
245 244 hd = *pphd;
246 245
247 246 if (bcmp(&hd->nh_id, id, sizeof (ndr_hdid_t)) == 0) {
248 247 if (hd->nh_svc == svc) {
249 248 *pphd = hd->nh_next;
250 249 free(hd);
251 250 }
252 251 break;
253 252 }
254 253
255 254 pphd = &(*pphd)->nh_next;
256 255 }
257 256
258 257 (void) mutex_unlock(&ndr_handle_lock);
259 258 }
260 259
261 260 /*
262 261 * Lookup a handle by id. If the handle is in the list and it matches
263 262 * the specified service, a pointer to it is returned. Otherwise a null
264 263 * pointer is returned.
265 264 */
266 265 ndr_handle_t *
267 266 ndr_hdlookup(const ndr_xa_t *xa, const ndr_hdid_t *id)
268 267 {
269 268 ndr_service_t *svc = xa->binding->service;
270 269 ndr_handle_t *hd;
271 270
272 271 assert(id);
273 272 (void) mutex_lock(&ndr_handle_lock);
274 273 hd = ndr_handle_list;
275 274
276 275 while (hd) {
277 276 if (bcmp(&hd->nh_id, id, sizeof (ndr_hdid_t)) == 0) {
278 277 if (hd->nh_svc != svc)
279 278 break;
280 279 (void) mutex_unlock(&ndr_handle_lock);
281 280 return (hd);
282 281 }
283 282
284 283 hd = hd->nh_next;
285 284 }
286 285
287 286 (void) mutex_unlock(&ndr_handle_lock);
288 287 return (NULL);
289 288 }
290 289
291 290 /*
292 291 * Called when a pipe is closed to release any associated handles.
293 292 */
294 293 void
295 294 ndr_hdclose(ndr_pipe_t *pipe)
296 295 {
297 296 ndr_handle_t *hd;
298 297 ndr_handle_t **pphd;
299 298
300 299 (void) mutex_lock(&ndr_handle_lock);
301 300 pphd = &ndr_handle_list;
302 301
303 302 while (*pphd) {
304 303 hd = *pphd;
305 304
306 305 if (hd->nh_pipe == pipe) {
307 306 *pphd = hd->nh_next;
308 307
309 308 if (hd->nh_data_free)
310 309 (*hd->nh_data_free)(hd->nh_data);
311 310
312 311 free(hd);
313 312 continue;
314 313 }
315 314
316 315 pphd = &(*pphd)->nh_next;
317 316 }
318 317
319 318 (void) mutex_unlock(&ndr_handle_lock);
320 319 }
321 320
322 321 /*
323 322 * Convert a UUID to a string.
324 323 */
325 324 void
326 325 ndr_uuid_unparse(ndr_uuid_t *uuid, char *out)
327 326 {
328 327 (void) sprintf(out, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
329 328 uuid->data1, uuid->data2, uuid->data3,
330 329 uuid->data4[0], uuid->data4[1],
331 330 uuid->data4[2], uuid->data4[3],
332 331 uuid->data4[4], uuid->data4[5],
333 332 uuid->data4[6], uuid->data4[7]);
334 333 }
335 334
336 335 /*
337 336 * Convert a string to a UUID.
338 337 */
339 338 int
340 339 ndr_uuid_parse(char *in, ndr_uuid_t *uuid)
341 340 {
342 341 char *p = in;
343 342 char *q;
344 343 char buf[4];
345 344 int i;
346 345
347 346 if (strlen(in) != UUID_PRINTABLE_STRING_LENGTH - 1)
348 347 return (-1);
349 348
350 349 uuid->data1 = strtoul(p, &p, 16);
351 350 if (*p != '-')
352 351 return (-1);
353 352 p++;
354 353
355 354 uuid->data2 = strtol(p, &p, 16);
356 355 if (*p != '-')
357 356 return (-1);
358 357 p++;
359 358
360 359 uuid->data3 = strtol(p, &p, 16);
361 360 if (*p != '-')
362 361 return (-1);
363 362 p++;
364 363
365 364 for (i = 0; i < 8; i++) {
366 365 if (*p == '-')
367 366 p++;
368 367
369 368 if (p[0] == 0 || p[1] == 0)
370 369 return (-1);
371 370
372 371 buf[0] = *p++;
373 372 buf[1] = *p++;
374 373 buf[2] = 0;
375 374 uuid->data4[i] = strtol(buf, &q, 16);
376 375 if (*q != 0)
377 376 return (-1);
378 377 }
379 378
380 379 if (*p != 0)
381 380 return (-1);
382 381
383 382 return (0);
384 383 }
385 384
386 385 void
387 386 ndr_svc_binding_pool_init(ndr_binding_t **headpp, ndr_binding_t pool[],
388 387 int n_pool)
389 388 {
390 389 ndr_binding_t *head = NULL;
391 390 int ix;
392 391
393 392 for (ix = n_pool - 1; ix >= 0; ix--) {
394 393 pool[ix].next = head;
395 394 pool[ix].service = NULL;
396 395 pool[ix].p_cont_id = 0xffff;
397 396 pool[ix].instance_specific = 0;
398 397 head = &pool[ix];
399 398 }
400 399
401 400 *headpp = head;
402 401 }
403 402
404 403 ndr_binding_t *
405 404 ndr_svc_find_binding(ndr_xa_t *mxa, ndr_p_context_id_t p_cont_id)
406 405 {
407 406 ndr_binding_t *mbind;
408 407
409 408 for (mbind = mxa->binding_list; mbind; mbind = mbind->next) {
410 409 if (mbind->service != NULL &&
411 410 mbind->which_side == NDR_BIND_SIDE_SERVER &&
412 411 mbind->p_cont_id == p_cont_id)
413 412 break;
414 413 }
415 414
416 415 return (mbind);
417 416 }
418 417
419 418 ndr_binding_t *
420 419 ndr_svc_new_binding(ndr_xa_t *mxa)
421 420 {
422 421 ndr_binding_t *mbind;
423 422
424 423 for (mbind = mxa->binding_list; mbind; mbind = mbind->next) {
425 424 if (mbind->service == NULL)
426 425 break;
427 426 }
428 427
429 428 return (mbind);
430 429 }
431 430
432 431 /*
433 432 * Move bytes between a buffer and a uio structure.
434 433 * The transfer direction is controlled by rw:
435 434 * UIO_READ: transfer from buf to uio
436 435 * UIO_WRITE: transfer from uio to buf
437 436 *
438 437 * Returns the number of bytes moved.
439 438 */
440 439 ssize_t
441 440 ndr_uiomove(caddr_t buf, size_t buflen, enum uio_rw rw, struct uio *uio)
442 441 {
443 442 struct iovec *iov;
444 443 int reading = (rw == UIO_READ);
445 444 size_t nbytes;
446 445 size_t nxfer = 0;
447 446
448 447 assert(rw == UIO_READ || rw == UIO_WRITE);
449 448
450 449 while (buflen && uio->uio_resid && uio->uio_iovcnt) {
451 450 iov = uio->uio_iov;
452 451 if ((nbytes = iov->iov_len) == 0) {
453 452 uio->uio_iov++;
454 453 uio->uio_iovcnt--;
455 454 continue;
456 455 }
457 456
458 457 if (nbytes > buflen)
459 458 nbytes = buflen;
460 459
461 460 if (reading)
462 461 bcopy(buf, iov->iov_base, nbytes);
463 462 else
464 463 bcopy(iov->iov_base, buf, nbytes);
465 464
466 465 iov->iov_base += nbytes;
467 466 iov->iov_len -= nbytes;
468 467 uio->uio_resid -= nbytes;
469 468 uio->uio_offset += nbytes;
470 469 buf += nbytes;
471 470 buflen -= nbytes;
472 471 nxfer += nbytes;
473 472 }
474 473
475 474 return (nxfer);
476 475 }
↓ open down ↓ |
428 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX