Release 0.9.14.
[wine/multimedia.git] / dlls / rpcrt4 / rpc_binding.c
blobb1cd88e7b85cef48287cc5e8b0910435b868ea25
1 /*
2 * RPC binding API
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003 Mike Hearn
6 * Copyright 2004 Filip Navara
7 * Copyright 2006 CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32 #include "winerror.h"
33 #include "winreg.h"
34 #include "winternl.h"
35 #include "wine/unicode.h"
37 #include "rpc.h"
38 #include "rpcndr.h"
40 #include "wine/debug.h"
42 #include "rpc_binding.h"
43 #include "rpc_message.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
47 LPSTR RPCRT4_strndupA(LPCSTR src, INT slen)
49 DWORD len;
50 LPSTR s;
51 if (!src) return NULL;
52 if (slen == -1) slen = strlen(src);
53 len = slen;
54 s = HeapAlloc(GetProcessHeap(), 0, len+1);
55 memcpy(s, src, len);
56 s[len] = 0;
57 return s;
60 LPSTR RPCRT4_strdupWtoA(LPWSTR src)
62 DWORD len;
63 LPSTR s;
64 if (!src) return NULL;
65 len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
66 s = HeapAlloc(GetProcessHeap(), 0, len);
67 WideCharToMultiByte(CP_ACP, 0, src, -1, s, len, NULL, NULL);
68 return s;
71 LPWSTR RPCRT4_strdupAtoW(LPSTR src)
73 DWORD len;
74 LPWSTR s;
75 if (!src) return NULL;
76 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
77 s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
78 MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
79 return s;
82 LPWSTR RPCRT4_strndupW(LPWSTR src, INT slen)
84 DWORD len;
85 LPWSTR s;
86 if (!src) return NULL;
87 if (slen == -1) slen = strlenW(src);
88 len = slen;
89 s = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
90 memcpy(s, src, len*sizeof(WCHAR));
91 s[len] = 0;
92 return s;
95 void RPCRT4_strfree(LPSTR src)
97 HeapFree(GetProcessHeap(), 0, src);
100 static RPC_STATUS RPCRT4_AllocBinding(RpcBinding** Binding, BOOL server)
102 RpcBinding* NewBinding;
104 NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
105 NewBinding->refs = 1;
106 NewBinding->server = server;
108 *Binding = NewBinding;
110 return RPC_S_OK;
113 RPC_STATUS RPCRT4_CreateBindingA(RpcBinding** Binding, BOOL server, LPSTR Protseq)
115 RpcBinding* NewBinding;
117 RPCRT4_AllocBinding(&NewBinding, server);
118 NewBinding->Protseq = RPCRT4_strdupA(Protseq);
120 TRACE("binding: %p\n", NewBinding);
121 *Binding = NewBinding;
123 return RPC_S_OK;
126 RPC_STATUS RPCRT4_CreateBindingW(RpcBinding** Binding, BOOL server, LPWSTR Protseq)
128 RpcBinding* NewBinding;
130 RPCRT4_AllocBinding(&NewBinding, server);
131 NewBinding->Protseq = RPCRT4_strdupWtoA(Protseq);
133 TRACE("binding: %p\n", NewBinding);
134 *Binding = NewBinding;
136 return RPC_S_OK;
139 RPC_STATUS RPCRT4_CompleteBindingA(RpcBinding* Binding, LPSTR NetworkAddr, LPSTR Endpoint, LPSTR NetworkOptions)
141 TRACE("(RpcBinding == ^%p, NetworkAddr == \"%s\", EndPoint == \"%s\", NetworkOptions == \"%s\")\n", Binding,
142 debugstr_a(NetworkAddr), debugstr_a(Endpoint), debugstr_a(NetworkOptions));
144 RPCRT4_strfree(Binding->NetworkAddr);
145 Binding->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
146 RPCRT4_strfree(Binding->Endpoint);
147 if (Endpoint) {
148 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
149 } else {
150 Binding->Endpoint = RPCRT4_strdupA("");
152 if (!Binding->Endpoint) ERR("out of memory?\n");
154 return RPC_S_OK;
157 RPC_STATUS RPCRT4_CompleteBindingW(RpcBinding* Binding, LPWSTR NetworkAddr, LPWSTR Endpoint, LPWSTR NetworkOptions)
159 TRACE("(RpcBinding == ^%p, NetworkAddr == \"%s\", EndPoint == \"%s\", NetworkOptions == \"%s\")\n", Binding,
160 debugstr_w(NetworkAddr), debugstr_w(Endpoint), debugstr_w(NetworkOptions));
162 RPCRT4_strfree(Binding->NetworkAddr);
163 Binding->NetworkAddr = RPCRT4_strdupWtoA(NetworkAddr);
164 RPCRT4_strfree(Binding->Endpoint);
165 if (Endpoint) {
166 Binding->Endpoint = RPCRT4_strdupWtoA(Endpoint);
167 } else {
168 Binding->Endpoint = RPCRT4_strdupA("");
170 if (!Binding->Endpoint) ERR("out of memory?\n");
172 return RPC_S_OK;
175 RPC_STATUS RPCRT4_ResolveBinding(RpcBinding* Binding, LPSTR Endpoint)
177 TRACE("(RpcBinding == ^%p, EndPoint == \"%s\"\n", Binding, Endpoint);
179 RPCRT4_strfree(Binding->Endpoint);
180 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
182 return RPC_S_OK;
185 RPC_STATUS RPCRT4_SetBindingObject(RpcBinding* Binding, UUID* ObjectUuid)
187 TRACE("(*RpcBinding == ^%p, UUID == %s)\n", Binding, debugstr_guid(ObjectUuid));
188 if (ObjectUuid) memcpy(&Binding->ObjectUuid, ObjectUuid, sizeof(UUID));
189 else UuidCreateNil(&Binding->ObjectUuid);
190 return RPC_S_OK;
193 RPC_STATUS RPCRT4_MakeBinding(RpcBinding** Binding, RpcConnection* Connection)
195 RpcBinding* NewBinding;
196 TRACE("(RpcBinding == ^%p, Connection == ^%p)\n", Binding, Connection);
198 RPCRT4_AllocBinding(&NewBinding, Connection->server);
199 NewBinding->Protseq = RPCRT4_strdupA(rpcrt4_conn_get_name(Connection));
200 NewBinding->NetworkAddr = RPCRT4_strdupA(Connection->NetworkAddr);
201 NewBinding->Endpoint = RPCRT4_strdupA(Connection->Endpoint);
202 NewBinding->FromConn = Connection;
204 TRACE("binding: %p\n", NewBinding);
205 *Binding = NewBinding;
207 return RPC_S_OK;
210 RPC_STATUS RPCRT4_ExportBinding(RpcBinding** Binding, RpcBinding* OldBinding)
212 InterlockedIncrement(&OldBinding->refs);
213 *Binding = OldBinding;
214 return RPC_S_OK;
217 RPC_STATUS RPCRT4_DestroyBinding(RpcBinding* Binding)
219 if (InterlockedDecrement(&Binding->refs))
220 return RPC_S_OK;
222 TRACE("binding: %p\n", Binding);
223 /* FIXME: release connections */
224 RPCRT4_strfree(Binding->Endpoint);
225 RPCRT4_strfree(Binding->NetworkAddr);
226 RPCRT4_strfree(Binding->Protseq);
227 HeapFree(GetProcessHeap(), 0, Binding);
228 return RPC_S_OK;
231 RPC_STATUS RPCRT4_OpenBinding(RpcBinding* Binding, RpcConnection** Connection,
232 PRPC_SYNTAX_IDENTIFIER TransferSyntax,
233 PRPC_SYNTAX_IDENTIFIER InterfaceId)
235 RpcConnection* NewConnection;
236 RPC_STATUS status;
238 TRACE("(Binding == ^%p)\n", Binding);
240 /* if we try to bind a new interface and the connection is already opened,
241 * close the current connection and create a new with the new binding. */
242 if (!Binding->server && Binding->FromConn &&
243 (Binding->AuthInfo == Binding->FromConn->AuthInfo) &&
244 memcmp(&Binding->FromConn->ActiveInterface, InterfaceId,
245 sizeof(RPC_SYNTAX_IDENTIFIER))) {
247 TRACE("releasing pre-existing connection\n");
248 RPCRT4_DestroyConnection(Binding->FromConn);
249 Binding->FromConn = NULL;
250 } else {
251 /* we already have a connection with acceptable binding, so use it */
252 if (Binding->FromConn) {
253 *Connection = Binding->FromConn;
254 return RPC_S_OK;
258 /* create a new connection */
259 RPCRT4_CreateConnection(&NewConnection, Binding->server, Binding->Protseq,
260 Binding->NetworkAddr, Binding->Endpoint, NULL,
261 Binding->AuthInfo, Binding);
262 status = RPCRT4_OpenConnection(NewConnection);
263 if (status != RPC_S_OK)
265 RPCRT4_DestroyConnection(NewConnection);
266 return status;
269 /* we need to send a binding packet if we are client. */
270 if (!NewConnection->server) {
271 RpcPktHdr *hdr;
272 RpcPktHdr *response_hdr;
273 RPC_MESSAGE msg;
275 TRACE("sending bind request to server\n");
277 hdr = RPCRT4_BuildBindHeader(NDR_LOCAL_DATA_REPRESENTATION,
278 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE,
279 InterfaceId, TransferSyntax);
281 status = RPCRT4_Send(NewConnection, hdr, NULL, 0);
282 RPCRT4_FreeHeader(hdr);
283 if (status != RPC_S_OK) {
284 RPCRT4_DestroyConnection(NewConnection);
285 return status;
288 status = RPCRT4_Receive(NewConnection, &response_hdr, &msg);
289 if (status != RPC_S_OK) {
290 ERR("receive failed\n");
291 RPCRT4_DestroyConnection(NewConnection);
292 return status;
295 if (response_hdr->common.ptype != PKT_BIND_ACK ||
296 response_hdr->bind_ack.max_tsize < RPC_MIN_PACKET_SIZE) {
297 ERR("failed to bind\n");
298 RPCRT4_FreeHeader(response_hdr);
299 RPCRT4_DestroyConnection(NewConnection);
300 return RPC_S_PROTOCOL_ERROR;
303 /* FIXME: do more checks? */
305 NewConnection->MaxTransmissionSize = response_hdr->bind_ack.max_tsize;
306 NewConnection->ActiveInterface = *InterfaceId;
307 RPCRT4_FreeHeader(response_hdr);
310 Binding->FromConn = NewConnection;
311 *Connection = NewConnection;
313 return RPC_S_OK;
316 RPC_STATUS RPCRT4_CloseBinding(RpcBinding* Binding, RpcConnection* Connection)
318 TRACE("(Binding == ^%p)\n", Binding);
319 if (!Connection) return RPC_S_OK;
320 if (Binding->FromConn == Connection) return RPC_S_OK;
321 return RPCRT4_DestroyConnection(Connection);
324 /* utility functions for string composing and parsing */
325 static unsigned RPCRT4_strcopyA(LPSTR data, LPCSTR src)
327 unsigned len = strlen(src);
328 memcpy(data, src, len*sizeof(CHAR));
329 return len;
332 static unsigned RPCRT4_strcopyW(LPWSTR data, LPCWSTR src)
334 unsigned len = strlenW(src);
335 memcpy(data, src, len*sizeof(WCHAR));
336 return len;
339 static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
341 DWORD len = strlen(dst), slen = strlen(src);
342 LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
343 if (!ndst)
345 HeapFree(GetProcessHeap(), 0, dst);
346 return NULL;
348 ndst[len] = ',';
349 memcpy(ndst+len+1, src, slen+1);
350 return ndst;
353 static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
355 DWORD len = strlenW(dst), slen = strlenW(src);
356 LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
357 if (!ndst)
359 HeapFree(GetProcessHeap(), 0, dst);
360 return NULL;
362 ndst[len] = ',';
363 memcpy(ndst+len+1, src, (slen+1)*sizeof(WCHAR));
364 return ndst;
368 /***********************************************************************
369 * RpcStringBindingComposeA (RPCRT4.@)
371 RPC_STATUS WINAPI RpcStringBindingComposeA(unsigned char *ObjUuid, unsigned char *Protseq,
372 unsigned char *NetworkAddr, unsigned char *Endpoint,
373 unsigned char *Options, unsigned char** StringBinding )
375 DWORD len = 1;
376 LPSTR data;
378 TRACE( "(%s,%s,%s,%s,%s,%p)\n",
379 debugstr_a( (char*)ObjUuid ), debugstr_a( (char*)Protseq ),
380 debugstr_a( (char*)NetworkAddr ), debugstr_a( (char*)Endpoint ),
381 debugstr_a( (char*)Options ), StringBinding );
383 if (ObjUuid && *ObjUuid) len += strlen((char*)ObjUuid) + 1;
384 if (Protseq && *Protseq) len += strlen((char*)Protseq) + 1;
385 if (NetworkAddr && *NetworkAddr) len += strlen((char*)NetworkAddr);
386 if (Endpoint && *Endpoint) len += strlen((char*)Endpoint) + 2;
387 if (Options && *Options) len += strlen((char*)Options) + 2;
389 data = HeapAlloc(GetProcessHeap(), 0, len);
390 *StringBinding = (unsigned char*)data;
392 if (ObjUuid && *ObjUuid) {
393 data += RPCRT4_strcopyA(data, (char*)ObjUuid);
394 *data++ = '@';
396 if (Protseq && *Protseq) {
397 data += RPCRT4_strcopyA(data, (char*)Protseq);
398 *data++ = ':';
400 if (NetworkAddr && *NetworkAddr)
401 data += RPCRT4_strcopyA(data, (char*)NetworkAddr);
403 if ((Endpoint && *Endpoint) ||
404 (Options && *Options)) {
405 *data++ = '[';
406 if (Endpoint && *Endpoint) {
407 data += RPCRT4_strcopyA(data, (char*)Endpoint);
408 if (Options && *Options) *data++ = ',';
410 if (Options && *Options) {
411 data += RPCRT4_strcopyA(data, (char*)Options);
413 *data++ = ']';
415 *data = 0;
417 return RPC_S_OK;
420 /***********************************************************************
421 * RpcStringBindingComposeW (RPCRT4.@)
423 RPC_STATUS WINAPI RpcStringBindingComposeW( LPWSTR ObjUuid, LPWSTR Protseq,
424 LPWSTR NetworkAddr, LPWSTR Endpoint,
425 LPWSTR Options, LPWSTR* StringBinding )
427 DWORD len = 1;
428 LPWSTR data;
430 TRACE("(%s,%s,%s,%s,%s,%p)\n",
431 debugstr_w( ObjUuid ), debugstr_w( Protseq ),
432 debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
433 debugstr_w( Options ), StringBinding);
435 if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) + 1;
436 if (Protseq && *Protseq) len += strlenW(Protseq) + 1;
437 if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr);
438 if (Endpoint && *Endpoint) len += strlenW(Endpoint) + 2;
439 if (Options && *Options) len += strlenW(Options) + 2;
441 data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
442 *StringBinding = data;
444 if (ObjUuid && *ObjUuid) {
445 data += RPCRT4_strcopyW(data, ObjUuid);
446 *data++ = '@';
448 if (Protseq && *Protseq) {
449 data += RPCRT4_strcopyW(data, Protseq);
450 *data++ = ':';
452 if (NetworkAddr && *NetworkAddr) {
453 data += RPCRT4_strcopyW(data, NetworkAddr);
455 if ((Endpoint && *Endpoint) ||
456 (Options && *Options)) {
457 *data++ = '[';
458 if (Endpoint && *Endpoint) {
459 data += RPCRT4_strcopyW(data, Endpoint);
460 if (Options && *Options) *data++ = ',';
462 if (Options && *Options) {
463 data += RPCRT4_strcopyW(data, Options);
465 *data++ = ']';
467 *data = 0;
469 return RPC_S_OK;
473 /***********************************************************************
474 * RpcStringBindingParseA (RPCRT4.@)
476 RPC_STATUS WINAPI RpcStringBindingParseA( unsigned char *StringBinding, unsigned char **ObjUuid,
477 unsigned char **Protseq, unsigned char **NetworkAddr,
478 unsigned char **Endpoint, unsigned char **Options)
480 CHAR *data, *next;
481 static const char ep_opt[] = "endpoint=";
483 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a((char*)StringBinding),
484 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
486 if (ObjUuid) *ObjUuid = NULL;
487 if (Protseq) *Protseq = NULL;
488 if (NetworkAddr) *NetworkAddr = NULL;
489 if (Endpoint) *Endpoint = NULL;
490 if (Options) *Options = NULL;
492 data = (char*) StringBinding;
494 next = strchr(data, '@');
495 if (next) {
496 if (ObjUuid) *ObjUuid = (unsigned char*)RPCRT4_strndupA(data, next - data);
497 data = next+1;
500 next = strchr(data, ':');
501 if (next) {
502 if (Protseq) *Protseq = (unsigned char*)RPCRT4_strndupA(data, next - data);
503 data = next+1;
506 next = strchr(data, '[');
507 if (next) {
508 CHAR *close, *opt;
510 if (NetworkAddr) *NetworkAddr = (unsigned char*)RPCRT4_strndupA(data, next - data);
511 data = next+1;
512 close = strchr(data, ']');
513 if (!close) goto fail;
515 /* tokenize options */
516 while (data < close) {
517 next = strchr(data, ',');
518 if (!next || next > close) next = close;
519 /* FIXME: this is kind of inefficient */
520 opt = RPCRT4_strndupA(data, next - data);
521 data = next+1;
523 /* parse option */
524 next = strchr(opt, '=');
525 if (!next) {
526 /* not an option, must be an endpoint */
527 if (*Endpoint) goto fail;
528 *Endpoint = (unsigned char*) opt;
529 } else {
530 if (strncmp(opt, ep_opt, strlen(ep_opt)) == 0) {
531 /* endpoint option */
532 if (*Endpoint) goto fail;
533 *Endpoint = (unsigned char*) RPCRT4_strdupA(next+1);
534 HeapFree(GetProcessHeap(), 0, opt);
535 } else {
536 /* network option */
537 if (*Options) {
538 /* FIXME: this is kind of inefficient */
539 *Options = (unsigned char*) RPCRT4_strconcatA( (char*)*Options, opt);
540 HeapFree(GetProcessHeap(), 0, opt);
541 } else
542 *Options = (unsigned char*) opt;
547 data = close+1;
548 if (*data) goto fail;
550 else if (NetworkAddr)
551 *NetworkAddr = (unsigned char*)RPCRT4_strdupA(data);
553 return RPC_S_OK;
555 fail:
556 if (ObjUuid) RpcStringFreeA((unsigned char**)ObjUuid);
557 if (Protseq) RpcStringFreeA((unsigned char**)Protseq);
558 if (NetworkAddr) RpcStringFreeA((unsigned char**)NetworkAddr);
559 if (Endpoint) RpcStringFreeA((unsigned char**)Endpoint);
560 if (Options) RpcStringFreeA((unsigned char**)Options);
561 return RPC_S_INVALID_STRING_BINDING;
564 /***********************************************************************
565 * RpcStringBindingParseW (RPCRT4.@)
567 RPC_STATUS WINAPI RpcStringBindingParseW( LPWSTR StringBinding, LPWSTR *ObjUuid,
568 LPWSTR *Protseq, LPWSTR *NetworkAddr,
569 LPWSTR *Endpoint, LPWSTR *Options)
571 WCHAR *data, *next;
572 static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
574 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
575 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
577 if (ObjUuid) *ObjUuid = NULL;
578 if (Protseq) *Protseq = NULL;
579 if (NetworkAddr) *NetworkAddr = NULL;
580 if (Endpoint) *Endpoint = NULL;
581 if (Options) *Options = NULL;
583 data = StringBinding;
585 next = strchrW(data, '@');
586 if (next) {
587 if (ObjUuid) *ObjUuid = RPCRT4_strndupW(data, next - data);
588 data = next+1;
591 next = strchrW(data, ':');
592 if (next) {
593 if (Protseq) *Protseq = RPCRT4_strndupW(data, next - data);
594 data = next+1;
597 next = strchrW(data, '[');
598 if (next) {
599 WCHAR *close, *opt;
601 if (NetworkAddr) *NetworkAddr = RPCRT4_strndupW(data, next - data);
602 data = next+1;
603 close = strchrW(data, ']');
604 if (!close) goto fail;
606 /* tokenize options */
607 while (data < close) {
608 next = strchrW(data, ',');
609 if (!next || next > close) next = close;
610 /* FIXME: this is kind of inefficient */
611 opt = RPCRT4_strndupW(data, next - data);
612 data = next+1;
614 /* parse option */
615 next = strchrW(opt, '=');
616 if (!next) {
617 /* not an option, must be an endpoint */
618 if (*Endpoint) goto fail;
619 *Endpoint = opt;
620 } else {
621 if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
622 /* endpoint option */
623 if (*Endpoint) goto fail;
624 *Endpoint = RPCRT4_strdupW(next+1);
625 HeapFree(GetProcessHeap(), 0, opt);
626 } else {
627 /* network option */
628 if (*Options) {
629 /* FIXME: this is kind of inefficient */
630 *Options = RPCRT4_strconcatW(*Options, opt);
631 HeapFree(GetProcessHeap(), 0, opt);
632 } else
633 *Options = opt;
638 data = close+1;
639 if (*data) goto fail;
640 } else if (NetworkAddr)
641 *NetworkAddr = RPCRT4_strdupW(data);
643 return RPC_S_OK;
645 fail:
646 if (ObjUuid) RpcStringFreeW(ObjUuid);
647 if (Protseq) RpcStringFreeW(Protseq);
648 if (NetworkAddr) RpcStringFreeW(NetworkAddr);
649 if (Endpoint) RpcStringFreeW(Endpoint);
650 if (Options) RpcStringFreeW(Options);
651 return RPC_S_INVALID_STRING_BINDING;
654 /***********************************************************************
655 * RpcBindingFree (RPCRT4.@)
657 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
659 RPC_STATUS status;
660 TRACE("(%p) = %p\n", Binding, *Binding);
661 status = RPCRT4_DestroyBinding(*Binding);
662 if (status == RPC_S_OK) *Binding = 0;
663 return status;
666 /***********************************************************************
667 * RpcBindingVectorFree (RPCRT4.@)
669 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
671 RPC_STATUS status;
672 unsigned long c;
674 TRACE("(%p)\n", BindingVector);
675 for (c=0; c<(*BindingVector)->Count; c++) {
676 status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
678 HeapFree(GetProcessHeap(), 0, *BindingVector);
679 *BindingVector = NULL;
680 return RPC_S_OK;
683 /***********************************************************************
684 * RpcBindingInqObject (RPCRT4.@)
686 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
688 RpcBinding* bind = (RpcBinding*)Binding;
690 TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
691 memcpy(ObjectUuid, &bind->ObjectUuid, sizeof(UUID));
692 return RPC_S_OK;
695 /***********************************************************************
696 * RpcBindingSetObject (RPCRT4.@)
698 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
700 RpcBinding* bind = (RpcBinding*)Binding;
702 TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
703 if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
704 return RPCRT4_SetBindingObject(Binding, ObjectUuid);
707 /***********************************************************************
708 * RpcBindingFromStringBindingA (RPCRT4.@)
710 RPC_STATUS WINAPI RpcBindingFromStringBindingA( unsigned char *StringBinding, RPC_BINDING_HANDLE* Binding )
712 RPC_STATUS ret;
713 RpcBinding* bind = NULL;
714 unsigned char *ObjectUuid, *Protseq, *NetworkAddr, *Endpoint, *Options;
715 UUID Uuid;
717 TRACE("(%s,%p)\n", debugstr_a((char*)StringBinding), Binding);
719 ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
720 &NetworkAddr, &Endpoint, &Options);
721 if (ret != RPC_S_OK) return ret;
723 ret = UuidFromStringA(ObjectUuid, &Uuid);
725 if (ret == RPC_S_OK)
726 ret = RPCRT4_CreateBindingA(&bind, FALSE, (char*)Protseq);
727 if (ret == RPC_S_OK)
728 ret = RPCRT4_SetBindingObject(bind, &Uuid);
729 if (ret == RPC_S_OK)
730 ret = RPCRT4_CompleteBindingA(bind, (char*)NetworkAddr, (char*)Endpoint, (char*)Options);
732 RpcStringFreeA((unsigned char**)&Options);
733 RpcStringFreeA((unsigned char**)&Endpoint);
734 RpcStringFreeA((unsigned char**)&NetworkAddr);
735 RpcStringFreeA((unsigned char**)&Protseq);
736 RpcStringFreeA((unsigned char**)&ObjectUuid);
738 if (ret == RPC_S_OK)
739 *Binding = (RPC_BINDING_HANDLE)bind;
740 else
741 RPCRT4_DestroyBinding(bind);
743 return ret;
746 /***********************************************************************
747 * RpcBindingFromStringBindingW (RPCRT4.@)
749 RPC_STATUS WINAPI RpcBindingFromStringBindingW( LPWSTR StringBinding, RPC_BINDING_HANDLE* Binding )
751 RPC_STATUS ret;
752 RpcBinding* bind = NULL;
753 LPWSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
754 UUID Uuid;
756 TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
758 ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
759 &NetworkAddr, &Endpoint, &Options);
760 if (ret != RPC_S_OK) return ret;
762 ret = UuidFromStringW(ObjectUuid, &Uuid);
764 if (ret == RPC_S_OK)
765 ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
766 if (ret == RPC_S_OK)
767 ret = RPCRT4_SetBindingObject(bind, &Uuid);
768 if (ret == RPC_S_OK)
769 ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
771 RpcStringFreeW(&Options);
772 RpcStringFreeW(&Endpoint);
773 RpcStringFreeW(&NetworkAddr);
774 RpcStringFreeW(&Protseq);
775 RpcStringFreeW(&ObjectUuid);
777 if (ret == RPC_S_OK)
778 *Binding = (RPC_BINDING_HANDLE)bind;
779 else
780 RPCRT4_DestroyBinding(bind);
782 return ret;
785 /***********************************************************************
786 * RpcBindingToStringBindingA (RPCRT4.@)
788 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, unsigned char** StringBinding )
790 RPC_STATUS ret;
791 RpcBinding* bind = (RpcBinding*)Binding;
792 LPSTR ObjectUuid;
794 TRACE("(%p,%p)\n", Binding, StringBinding);
796 ret = UuidToStringA(&bind->ObjectUuid, (unsigned char**)&ObjectUuid);
797 if (ret != RPC_S_OK) return ret;
799 ret = RpcStringBindingComposeA((unsigned char*) ObjectUuid, (unsigned char*)bind->Protseq, (unsigned char*) bind->NetworkAddr,
800 (unsigned char*) bind->Endpoint, NULL, StringBinding);
802 RpcStringFreeA((unsigned char**)&ObjectUuid);
804 return ret;
807 /***********************************************************************
808 * RpcBindingToStringBindingW (RPCRT4.@)
810 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, unsigned short** StringBinding )
812 RPC_STATUS ret;
813 unsigned char *str = NULL;
814 TRACE("(%p,%p)\n", Binding, StringBinding);
815 ret = RpcBindingToStringBindingA(Binding, &str);
816 *StringBinding = RPCRT4_strdupAtoW((char*)str);
817 RpcStringFreeA((unsigned char**)&str);
818 return ret;
821 /***********************************************************************
822 * I_RpcBindingSetAsync (RPCRT4.@)
823 * NOTES
824 * Exists in win9x and winNT, but with different number of arguments
825 * (9x version has 3 arguments, NT has 2).
827 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
829 RpcBinding* bind = (RpcBinding*)Binding;
831 TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
833 bind->BlockingFn = BlockingFn;
835 return RPC_S_OK;
838 /***********************************************************************
839 * RpcImpersonateClient (RPCRT4.@)
841 * Impersonates the client connected via a binding handle so that security
842 * checks are done in the context of the client.
844 * PARAMS
845 * BindingHandle [I] Handle to the binding to the client.
847 * RETURNS
848 * Success: RPS_S_OK.
849 * Failure: RPC_STATUS value.
851 * NOTES
853 * If BindingHandle is NULL then the function impersonates the client
854 * connected to the binding handle of the current thread.
856 RPC_STATUS WINAPI RpcImpersonateClient(RPC_BINDING_HANDLE BindingHandle)
858 FIXME("(%p): stub\n", BindingHandle);
859 return RPC_S_OK;
862 /***********************************************************************
863 * RpcRevertToSelfEx (RPCRT4.@)
865 * Stops impersonating the client connected to the binding handle so that security
866 * checks are no longer done in the context of the client.
868 * PARAMS
869 * BindingHandle [I] Handle to the binding to the client.
871 * RETURNS
872 * Success: RPS_S_OK.
873 * Failure: RPC_STATUS value.
875 * NOTES
877 * If BindingHandle is NULL then the function stops impersonating the client
878 * connected to the binding handle of the current thread.
880 RPC_STATUS WINAPI RpcRevertToSelfEx(RPC_BINDING_HANDLE BindingHandle)
882 FIXME("(%p): stub\n", BindingHandle);
883 return RPC_S_OK;
886 static RPC_STATUS RpcAuthInfo_Create(unsigned long AuthnLevel, unsigned long AuthnSvc, CredHandle cred, TimeStamp exp, RpcAuthInfo **ret)
888 RpcAuthInfo *AuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo));
889 if (!AuthInfo)
890 return ERROR_OUTOFMEMORY;
892 AuthInfo->AuthnLevel = AuthnLevel;
893 AuthInfo->AuthnSvc = AuthnSvc;
894 AuthInfo->cred = cred;
895 AuthInfo->exp = exp;
896 *ret = AuthInfo;
897 return RPC_S_OK;
900 ULONG RpcAuthInfo_AddRef(RpcAuthInfo *AuthInfo)
902 return InterlockedIncrement(&AuthInfo->refs);
905 ULONG RpcAuthInfo_Release(RpcAuthInfo *AuthInfo)
907 ULONG refs = InterlockedDecrement(&AuthInfo->refs);
909 if (!refs)
911 FreeCredentialsHandle(&AuthInfo->cred);
912 HeapFree(GetProcessHeap(), 0, AuthInfo);
915 return refs;
918 /***********************************************************************
919 * RpcBindingInqAuthInfoExA (RPCRT4.@)
921 RPCRTAPI RPC_STATUS RPC_ENTRY
922 RpcBindingInqAuthInfoExA( RPC_BINDING_HANDLE Binding, unsigned char ** ServerPrincName, unsigned long *AuthnLevel,
923 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc,
924 unsigned long RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
926 FIXME("%p %p %p %p %p %p %lu %p\n", Binding, ServerPrincName, AuthnLevel,
927 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
928 return RPC_S_INVALID_BINDING;
931 /***********************************************************************
932 * RpcBindingInqAuthInfoExW (RPCRT4.@)
934 RPCRTAPI RPC_STATUS RPC_ENTRY
935 RpcBindingInqAuthInfoExW( RPC_BINDING_HANDLE Binding, unsigned short ** ServerPrincName, unsigned long *AuthnLevel,
936 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc,
937 unsigned long RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
939 FIXME("%p %p %p %p %p %p %lu %p\n", Binding, ServerPrincName, AuthnLevel,
940 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
941 return RPC_S_INVALID_BINDING;
944 /***********************************************************************
945 * RpcBindingInqAuthInfoA (RPCRT4.@)
947 RPCRTAPI RPC_STATUS RPC_ENTRY
948 RpcBindingInqAuthInfoA( RPC_BINDING_HANDLE Binding, unsigned char ** ServerPrincName, unsigned long *AuthnLevel,
949 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc )
951 FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
952 AuthnSvc, AuthIdentity, AuthzSvc);
953 return RPC_S_INVALID_BINDING;
956 /***********************************************************************
957 * RpcBindingInqAuthInfoW (RPCRT4.@)
959 RPCRTAPI RPC_STATUS RPC_ENTRY
960 RpcBindingInqAuthInfoW( RPC_BINDING_HANDLE Binding, unsigned short ** ServerPrincName, unsigned long *AuthnLevel,
961 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc )
963 FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
964 AuthnSvc, AuthIdentity, AuthzSvc);
965 return RPC_S_INVALID_BINDING;
968 /***********************************************************************
969 * RpcBindingSetAuthInfoExA (RPCRT4.@)
971 RPCRTAPI RPC_STATUS RPC_ENTRY
972 RpcBindingSetAuthInfoExA( RPC_BINDING_HANDLE Binding, unsigned char *ServerPrincName,
973 unsigned long AuthnLevel, unsigned long AuthnSvc,
974 RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr,
975 RPC_SECURITY_QOS *SecurityQos )
977 RpcBinding* bind = (RpcBinding*)Binding;
978 SECURITY_STATUS r;
979 CredHandle cred;
980 TimeStamp exp;
981 ULONG package_count;
982 ULONG i;
983 PSecPkgInfoA packages;
985 TRACE("%p %s %lu %lu %p %lu %p\n", Binding, debugstr_a((const char*)ServerPrincName),
986 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
988 if (AuthnLevel != RPC_C_AUTHN_LEVEL_CONNECT)
990 FIXME("unsupported AuthnLevel %lu\n", AuthnLevel);
991 return RPC_S_UNKNOWN_AUTHN_LEVEL;
994 if (AuthzSvr)
996 FIXME("unsupported AuthzSvr %lu\n", AuthzSvr);
997 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1000 if (SecurityQos)
1001 FIXME("SecurityQos ignored\n");
1003 r = EnumerateSecurityPackagesA(&package_count, &packages);
1004 if (r != SEC_E_OK)
1006 ERR("EnumerateSecurityPackagesA failed with error 0x%08lx\n", r);
1007 return RPC_S_SEC_PKG_ERROR;
1010 for (i = 0; i < package_count; i++)
1011 if (packages[i].wRPCID == AuthnSvc)
1012 break;
1014 if (i == package_count)
1016 FIXME("unsupported AuthnSvc %lu\n", AuthnSvc);
1017 FreeContextBuffer(packages);
1018 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1021 TRACE("found package %s for service %ld\n", packages[i].Name, AuthnSvc);
1022 r = AcquireCredentialsHandleA((SEC_CHAR *)ServerPrincName, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1023 AuthIdentity, NULL, NULL, &cred, &exp);
1024 FreeContextBuffer(packages);
1025 if (r == ERROR_SUCCESS)
1027 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1028 bind->AuthInfo = NULL;
1029 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, &bind->AuthInfo);
1030 if (r != RPC_S_OK)
1031 FreeCredentialsHandle(&cred);
1032 return RPC_S_OK;
1034 else
1036 ERR("AcquireCredentialsHandleA failed with error 0x%08lx\n", r);
1037 return RPC_S_SEC_PKG_ERROR;
1041 /***********************************************************************
1042 * RpcBindingSetAuthInfoExW (RPCRT4.@)
1044 RPCRTAPI RPC_STATUS RPC_ENTRY
1045 RpcBindingSetAuthInfoExW( RPC_BINDING_HANDLE Binding, unsigned short *ServerPrincName, unsigned long AuthnLevel,
1046 unsigned long AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr,
1047 RPC_SECURITY_QOS *SecurityQos )
1049 RpcBinding* bind = (RpcBinding*)Binding;
1050 SECURITY_STATUS r;
1051 CredHandle cred;
1052 TimeStamp exp;
1053 ULONG package_count;
1054 ULONG i;
1055 PSecPkgInfoW packages;
1057 TRACE("%p %s %lu %lu %p %lu %p\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1058 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1060 if (AuthnLevel != RPC_C_AUTHN_LEVEL_CONNECT)
1062 FIXME("unsupported AuthnLevel %lu\n", AuthnLevel);
1063 return RPC_S_UNKNOWN_AUTHN_LEVEL;
1066 if (AuthzSvr)
1068 FIXME("unsupported AuthzSvr %lu\n", AuthzSvr);
1069 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1072 if (SecurityQos)
1073 FIXME("SecurityQos ignored\n");
1075 r = EnumerateSecurityPackagesW(&package_count, &packages);
1076 if (r != SEC_E_OK)
1078 ERR("EnumerateSecurityPackagesA failed with error 0x%08lx\n", r);
1079 return RPC_S_SEC_PKG_ERROR;
1082 for (i = 0; i < package_count; i++)
1083 if (packages[i].wRPCID == AuthnSvc)
1084 break;
1086 if (i == package_count)
1088 FIXME("unsupported AuthnSvc %lu\n", AuthnSvc);
1089 FreeContextBuffer(packages);
1090 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1093 TRACE("found package %s for service %ld\n", debugstr_w(packages[i].Name), AuthnSvc);
1094 r = AcquireCredentialsHandleW((SEC_WCHAR *)ServerPrincName, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1095 AuthIdentity, NULL, NULL, &cred, &exp);
1096 FreeContextBuffer(packages);
1097 if (r == ERROR_SUCCESS)
1099 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1100 bind->AuthInfo = NULL;
1101 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, &bind->AuthInfo);
1102 if (r != RPC_S_OK)
1103 FreeCredentialsHandle(&cred);
1104 return RPC_S_OK;
1106 else
1108 ERR("AcquireCredentialsHandleA failed with error 0x%08lx\n", r);
1109 return RPC_S_SEC_PKG_ERROR;
1113 /***********************************************************************
1114 * RpcBindingSetAuthInfoA (RPCRT4.@)
1116 RPCRTAPI RPC_STATUS RPC_ENTRY
1117 RpcBindingSetAuthInfoA( RPC_BINDING_HANDLE Binding, unsigned char *ServerPrincName, unsigned long AuthnLevel,
1118 unsigned long AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr )
1120 TRACE("%p %s %lu %lu %p %lu\n", Binding, debugstr_a((const char*)ServerPrincName),
1121 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1122 return RpcBindingSetAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1125 /***********************************************************************
1126 * RpcBindingSetAuthInfoW (RPCRT4.@)
1128 RPCRTAPI RPC_STATUS RPC_ENTRY
1129 RpcBindingSetAuthInfoW( RPC_BINDING_HANDLE Binding, unsigned short *ServerPrincName, unsigned long AuthnLevel,
1130 unsigned long AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr )
1132 TRACE("%p %s %lu %lu %p %lu\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1133 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1134 return RpcBindingSetAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1137 /***********************************************************************
1138 * RpcBindingSetOption (RPCRT4.@)
1140 RPC_STATUS WINAPI RpcBindingSetOption(RPC_BINDING_HANDLE BindingHandle, ULONG Option, ULONG OptionValue)
1142 FIXME("(%p, %ld, %ld): stub\n", BindingHandle, Option, OptionValue);
1143 return RPC_S_OK;