wined3d: Draw the cursor.
[wine/testsucceed.git] / dlls / rpcrt4 / rpc_binding.c
blob7f5f14d04d5a74c50cc88dd6713b7cfbf4885c01
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 (!Binding->server) {
241 /* try to find a compatible connection from the connection pool */
242 NewConnection = RPCRT4_GetIdleConnection(InterfaceId, TransferSyntax,
243 Binding->Protseq, Binding->NetworkAddr, Binding->Endpoint,
244 Binding->AuthInfo);
245 if (NewConnection) {
246 *Connection = NewConnection;
247 return RPC_S_OK;
249 } else {
250 /* we already have a connection with acceptable binding, so use it */
251 if (Binding->FromConn) {
252 *Connection = Binding->FromConn;
253 return RPC_S_OK;
257 /* create a new connection */
258 RPCRT4_CreateConnection(&NewConnection, Binding->server, Binding->Protseq,
259 Binding->NetworkAddr, Binding->Endpoint, NULL,
260 Binding->AuthInfo, Binding);
261 status = RPCRT4_OpenConnection(NewConnection);
262 if (status != RPC_S_OK)
264 RPCRT4_DestroyConnection(NewConnection);
265 return status;
268 /* we need to send a binding packet if we are client. */
269 if (!NewConnection->server) {
270 RpcPktHdr *hdr;
271 RpcPktHdr *response_hdr;
272 RPC_MESSAGE msg;
274 TRACE("sending bind request to server\n");
276 hdr = RPCRT4_BuildBindHeader(NDR_LOCAL_DATA_REPRESENTATION,
277 RPC_MAX_PACKET_SIZE, RPC_MAX_PACKET_SIZE,
278 InterfaceId, TransferSyntax);
280 status = RPCRT4_Send(NewConnection, hdr, NULL, 0);
281 RPCRT4_FreeHeader(hdr);
282 if (status != RPC_S_OK) {
283 RPCRT4_DestroyConnection(NewConnection);
284 return status;
287 status = RPCRT4_Receive(NewConnection, &response_hdr, &msg);
288 if (status != RPC_S_OK) {
289 ERR("receive failed\n");
290 RPCRT4_DestroyConnection(NewConnection);
291 return status;
294 if (response_hdr->common.ptype != PKT_BIND_ACK ||
295 response_hdr->bind_ack.max_tsize < RPC_MIN_PACKET_SIZE) {
296 ERR("failed to bind\n");
297 RPCRT4_FreeHeader(response_hdr);
298 RPCRT4_DestroyConnection(NewConnection);
299 return RPC_S_PROTOCOL_ERROR;
302 /* FIXME: do more checks? */
304 NewConnection->MaxTransmissionSize = response_hdr->bind_ack.max_tsize;
305 NewConnection->ActiveInterface = *InterfaceId;
306 RPCRT4_FreeHeader(response_hdr);
309 if (Binding->server)
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->server) {
321 /* don't destroy a connection that is cached in the binding */
322 if (Binding->FromConn == Connection)
323 return RPC_S_OK;
324 return RPCRT4_DestroyConnection(Connection);
326 else {
327 RPCRT4_ReleaseIdleConnection(Connection);
328 return RPC_S_OK;
332 /* utility functions for string composing and parsing */
333 static unsigned RPCRT4_strcopyA(LPSTR data, LPCSTR src)
335 unsigned len = strlen(src);
336 memcpy(data, src, len*sizeof(CHAR));
337 return len;
340 static unsigned RPCRT4_strcopyW(LPWSTR data, LPCWSTR src)
342 unsigned len = strlenW(src);
343 memcpy(data, src, len*sizeof(WCHAR));
344 return len;
347 static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
349 DWORD len = strlen(dst), slen = strlen(src);
350 LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
351 if (!ndst)
353 HeapFree(GetProcessHeap(), 0, dst);
354 return NULL;
356 ndst[len] = ',';
357 memcpy(ndst+len+1, src, slen+1);
358 return ndst;
361 static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
363 DWORD len = strlenW(dst), slen = strlenW(src);
364 LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
365 if (!ndst)
367 HeapFree(GetProcessHeap(), 0, dst);
368 return NULL;
370 ndst[len] = ',';
371 memcpy(ndst+len+1, src, (slen+1)*sizeof(WCHAR));
372 return ndst;
376 /***********************************************************************
377 * RpcStringBindingComposeA (RPCRT4.@)
379 RPC_STATUS WINAPI RpcStringBindingComposeA(unsigned char *ObjUuid, unsigned char *Protseq,
380 unsigned char *NetworkAddr, unsigned char *Endpoint,
381 unsigned char *Options, unsigned char** StringBinding )
383 DWORD len = 1;
384 LPSTR data;
386 TRACE( "(%s,%s,%s,%s,%s,%p)\n",
387 debugstr_a( (char*)ObjUuid ), debugstr_a( (char*)Protseq ),
388 debugstr_a( (char*)NetworkAddr ), debugstr_a( (char*)Endpoint ),
389 debugstr_a( (char*)Options ), StringBinding );
391 if (ObjUuid && *ObjUuid) len += strlen((char*)ObjUuid) + 1;
392 if (Protseq && *Protseq) len += strlen((char*)Protseq) + 1;
393 if (NetworkAddr && *NetworkAddr) len += strlen((char*)NetworkAddr);
394 if (Endpoint && *Endpoint) len += strlen((char*)Endpoint) + 2;
395 if (Options && *Options) len += strlen((char*)Options) + 2;
397 data = HeapAlloc(GetProcessHeap(), 0, len);
398 *StringBinding = (unsigned char*)data;
400 if (ObjUuid && *ObjUuid) {
401 data += RPCRT4_strcopyA(data, (char*)ObjUuid);
402 *data++ = '@';
404 if (Protseq && *Protseq) {
405 data += RPCRT4_strcopyA(data, (char*)Protseq);
406 *data++ = ':';
408 if (NetworkAddr && *NetworkAddr)
409 data += RPCRT4_strcopyA(data, (char*)NetworkAddr);
411 if ((Endpoint && *Endpoint) ||
412 (Options && *Options)) {
413 *data++ = '[';
414 if (Endpoint && *Endpoint) {
415 data += RPCRT4_strcopyA(data, (char*)Endpoint);
416 if (Options && *Options) *data++ = ',';
418 if (Options && *Options) {
419 data += RPCRT4_strcopyA(data, (char*)Options);
421 *data++ = ']';
423 *data = 0;
425 return RPC_S_OK;
428 /***********************************************************************
429 * RpcStringBindingComposeW (RPCRT4.@)
431 RPC_STATUS WINAPI RpcStringBindingComposeW( LPWSTR ObjUuid, LPWSTR Protseq,
432 LPWSTR NetworkAddr, LPWSTR Endpoint,
433 LPWSTR Options, LPWSTR* StringBinding )
435 DWORD len = 1;
436 LPWSTR data;
438 TRACE("(%s,%s,%s,%s,%s,%p)\n",
439 debugstr_w( ObjUuid ), debugstr_w( Protseq ),
440 debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
441 debugstr_w( Options ), StringBinding);
443 if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) + 1;
444 if (Protseq && *Protseq) len += strlenW(Protseq) + 1;
445 if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr);
446 if (Endpoint && *Endpoint) len += strlenW(Endpoint) + 2;
447 if (Options && *Options) len += strlenW(Options) + 2;
449 data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
450 *StringBinding = data;
452 if (ObjUuid && *ObjUuid) {
453 data += RPCRT4_strcopyW(data, ObjUuid);
454 *data++ = '@';
456 if (Protseq && *Protseq) {
457 data += RPCRT4_strcopyW(data, Protseq);
458 *data++ = ':';
460 if (NetworkAddr && *NetworkAddr) {
461 data += RPCRT4_strcopyW(data, NetworkAddr);
463 if ((Endpoint && *Endpoint) ||
464 (Options && *Options)) {
465 *data++ = '[';
466 if (Endpoint && *Endpoint) {
467 data += RPCRT4_strcopyW(data, Endpoint);
468 if (Options && *Options) *data++ = ',';
470 if (Options && *Options) {
471 data += RPCRT4_strcopyW(data, Options);
473 *data++ = ']';
475 *data = 0;
477 return RPC_S_OK;
481 /***********************************************************************
482 * RpcStringBindingParseA (RPCRT4.@)
484 RPC_STATUS WINAPI RpcStringBindingParseA( unsigned char *StringBinding, unsigned char **ObjUuid,
485 unsigned char **Protseq, unsigned char **NetworkAddr,
486 unsigned char **Endpoint, unsigned char **Options)
488 CHAR *data, *next;
489 static const char ep_opt[] = "endpoint=";
491 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a((char*)StringBinding),
492 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
494 if (ObjUuid) *ObjUuid = NULL;
495 if (Protseq) *Protseq = NULL;
496 if (NetworkAddr) *NetworkAddr = NULL;
497 if (Endpoint) *Endpoint = NULL;
498 if (Options) *Options = NULL;
500 data = (char*) StringBinding;
502 next = strchr(data, '@');
503 if (next) {
504 if (ObjUuid) *ObjUuid = (unsigned char*)RPCRT4_strndupA(data, next - data);
505 data = next+1;
508 next = strchr(data, ':');
509 if (next) {
510 if (Protseq) *Protseq = (unsigned char*)RPCRT4_strndupA(data, next - data);
511 data = next+1;
514 next = strchr(data, '[');
515 if (next) {
516 CHAR *close, *opt;
518 if (NetworkAddr) *NetworkAddr = (unsigned char*)RPCRT4_strndupA(data, next - data);
519 data = next+1;
520 close = strchr(data, ']');
521 if (!close) goto fail;
523 /* tokenize options */
524 while (data < close) {
525 next = strchr(data, ',');
526 if (!next || next > close) next = close;
527 /* FIXME: this is kind of inefficient */
528 opt = RPCRT4_strndupA(data, next - data);
529 data = next+1;
531 /* parse option */
532 next = strchr(opt, '=');
533 if (!next) {
534 /* not an option, must be an endpoint */
535 if (*Endpoint) goto fail;
536 *Endpoint = (unsigned char*) opt;
537 } else {
538 if (strncmp(opt, ep_opt, strlen(ep_opt)) == 0) {
539 /* endpoint option */
540 if (*Endpoint) goto fail;
541 *Endpoint = (unsigned char*) RPCRT4_strdupA(next+1);
542 HeapFree(GetProcessHeap(), 0, opt);
543 } else {
544 /* network option */
545 if (*Options) {
546 /* FIXME: this is kind of inefficient */
547 *Options = (unsigned char*) RPCRT4_strconcatA( (char*)*Options, opt);
548 HeapFree(GetProcessHeap(), 0, opt);
549 } else
550 *Options = (unsigned char*) opt;
555 data = close+1;
556 if (*data) goto fail;
558 else if (NetworkAddr)
559 *NetworkAddr = (unsigned char*)RPCRT4_strdupA(data);
561 return RPC_S_OK;
563 fail:
564 if (ObjUuid) RpcStringFreeA((unsigned char**)ObjUuid);
565 if (Protseq) RpcStringFreeA((unsigned char**)Protseq);
566 if (NetworkAddr) RpcStringFreeA((unsigned char**)NetworkAddr);
567 if (Endpoint) RpcStringFreeA((unsigned char**)Endpoint);
568 if (Options) RpcStringFreeA((unsigned char**)Options);
569 return RPC_S_INVALID_STRING_BINDING;
572 /***********************************************************************
573 * RpcStringBindingParseW (RPCRT4.@)
575 RPC_STATUS WINAPI RpcStringBindingParseW( LPWSTR StringBinding, LPWSTR *ObjUuid,
576 LPWSTR *Protseq, LPWSTR *NetworkAddr,
577 LPWSTR *Endpoint, LPWSTR *Options)
579 WCHAR *data, *next;
580 static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
582 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
583 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
585 if (ObjUuid) *ObjUuid = NULL;
586 if (Protseq) *Protseq = NULL;
587 if (NetworkAddr) *NetworkAddr = NULL;
588 if (Endpoint) *Endpoint = NULL;
589 if (Options) *Options = NULL;
591 data = StringBinding;
593 next = strchrW(data, '@');
594 if (next) {
595 if (ObjUuid) *ObjUuid = RPCRT4_strndupW(data, next - data);
596 data = next+1;
599 next = strchrW(data, ':');
600 if (next) {
601 if (Protseq) *Protseq = RPCRT4_strndupW(data, next - data);
602 data = next+1;
605 next = strchrW(data, '[');
606 if (next) {
607 WCHAR *close, *opt;
609 if (NetworkAddr) *NetworkAddr = RPCRT4_strndupW(data, next - data);
610 data = next+1;
611 close = strchrW(data, ']');
612 if (!close) goto fail;
614 /* tokenize options */
615 while (data < close) {
616 next = strchrW(data, ',');
617 if (!next || next > close) next = close;
618 /* FIXME: this is kind of inefficient */
619 opt = RPCRT4_strndupW(data, next - data);
620 data = next+1;
622 /* parse option */
623 next = strchrW(opt, '=');
624 if (!next) {
625 /* not an option, must be an endpoint */
626 if (*Endpoint) goto fail;
627 *Endpoint = opt;
628 } else {
629 if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
630 /* endpoint option */
631 if (*Endpoint) goto fail;
632 *Endpoint = RPCRT4_strdupW(next+1);
633 HeapFree(GetProcessHeap(), 0, opt);
634 } else {
635 /* network option */
636 if (*Options) {
637 /* FIXME: this is kind of inefficient */
638 *Options = RPCRT4_strconcatW(*Options, opt);
639 HeapFree(GetProcessHeap(), 0, opt);
640 } else
641 *Options = opt;
646 data = close+1;
647 if (*data) goto fail;
648 } else if (NetworkAddr)
649 *NetworkAddr = RPCRT4_strdupW(data);
651 return RPC_S_OK;
653 fail:
654 if (ObjUuid) RpcStringFreeW(ObjUuid);
655 if (Protseq) RpcStringFreeW(Protseq);
656 if (NetworkAddr) RpcStringFreeW(NetworkAddr);
657 if (Endpoint) RpcStringFreeW(Endpoint);
658 if (Options) RpcStringFreeW(Options);
659 return RPC_S_INVALID_STRING_BINDING;
662 /***********************************************************************
663 * RpcBindingFree (RPCRT4.@)
665 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
667 RPC_STATUS status;
668 TRACE("(%p) = %p\n", Binding, *Binding);
669 status = RPCRT4_DestroyBinding(*Binding);
670 if (status == RPC_S_OK) *Binding = 0;
671 return status;
674 /***********************************************************************
675 * RpcBindingVectorFree (RPCRT4.@)
677 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
679 RPC_STATUS status;
680 unsigned long c;
682 TRACE("(%p)\n", BindingVector);
683 for (c=0; c<(*BindingVector)->Count; c++) {
684 status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
686 HeapFree(GetProcessHeap(), 0, *BindingVector);
687 *BindingVector = NULL;
688 return RPC_S_OK;
691 /***********************************************************************
692 * RpcBindingInqObject (RPCRT4.@)
694 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
696 RpcBinding* bind = (RpcBinding*)Binding;
698 TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
699 memcpy(ObjectUuid, &bind->ObjectUuid, sizeof(UUID));
700 return RPC_S_OK;
703 /***********************************************************************
704 * RpcBindingSetObject (RPCRT4.@)
706 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
708 RpcBinding* bind = (RpcBinding*)Binding;
710 TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
711 if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
712 return RPCRT4_SetBindingObject(Binding, ObjectUuid);
715 /***********************************************************************
716 * RpcBindingFromStringBindingA (RPCRT4.@)
718 RPC_STATUS WINAPI RpcBindingFromStringBindingA( unsigned char *StringBinding, RPC_BINDING_HANDLE* Binding )
720 RPC_STATUS ret;
721 RpcBinding* bind = NULL;
722 unsigned char *ObjectUuid, *Protseq, *NetworkAddr, *Endpoint, *Options;
723 UUID Uuid;
725 TRACE("(%s,%p)\n", debugstr_a((char*)StringBinding), Binding);
727 ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
728 &NetworkAddr, &Endpoint, &Options);
729 if (ret != RPC_S_OK) return ret;
731 ret = UuidFromStringA(ObjectUuid, &Uuid);
733 if (ret == RPC_S_OK)
734 ret = RPCRT4_CreateBindingA(&bind, FALSE, (char*)Protseq);
735 if (ret == RPC_S_OK)
736 ret = RPCRT4_SetBindingObject(bind, &Uuid);
737 if (ret == RPC_S_OK)
738 ret = RPCRT4_CompleteBindingA(bind, (char*)NetworkAddr, (char*)Endpoint, (char*)Options);
740 RpcStringFreeA((unsigned char**)&Options);
741 RpcStringFreeA((unsigned char**)&Endpoint);
742 RpcStringFreeA((unsigned char**)&NetworkAddr);
743 RpcStringFreeA((unsigned char**)&Protseq);
744 RpcStringFreeA((unsigned char**)&ObjectUuid);
746 if (ret == RPC_S_OK)
747 *Binding = (RPC_BINDING_HANDLE)bind;
748 else
749 RPCRT4_DestroyBinding(bind);
751 return ret;
754 /***********************************************************************
755 * RpcBindingFromStringBindingW (RPCRT4.@)
757 RPC_STATUS WINAPI RpcBindingFromStringBindingW( LPWSTR StringBinding, RPC_BINDING_HANDLE* Binding )
759 RPC_STATUS ret;
760 RpcBinding* bind = NULL;
761 LPWSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
762 UUID Uuid;
764 TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
766 ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
767 &NetworkAddr, &Endpoint, &Options);
768 if (ret != RPC_S_OK) return ret;
770 ret = UuidFromStringW(ObjectUuid, &Uuid);
772 if (ret == RPC_S_OK)
773 ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
774 if (ret == RPC_S_OK)
775 ret = RPCRT4_SetBindingObject(bind, &Uuid);
776 if (ret == RPC_S_OK)
777 ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
779 RpcStringFreeW(&Options);
780 RpcStringFreeW(&Endpoint);
781 RpcStringFreeW(&NetworkAddr);
782 RpcStringFreeW(&Protseq);
783 RpcStringFreeW(&ObjectUuid);
785 if (ret == RPC_S_OK)
786 *Binding = (RPC_BINDING_HANDLE)bind;
787 else
788 RPCRT4_DestroyBinding(bind);
790 return ret;
793 /***********************************************************************
794 * RpcBindingToStringBindingA (RPCRT4.@)
796 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, unsigned char** StringBinding )
798 RPC_STATUS ret;
799 RpcBinding* bind = (RpcBinding*)Binding;
800 LPSTR ObjectUuid;
802 TRACE("(%p,%p)\n", Binding, StringBinding);
804 ret = UuidToStringA(&bind->ObjectUuid, (unsigned char**)&ObjectUuid);
805 if (ret != RPC_S_OK) return ret;
807 ret = RpcStringBindingComposeA((unsigned char*) ObjectUuid, (unsigned char*)bind->Protseq, (unsigned char*) bind->NetworkAddr,
808 (unsigned char*) bind->Endpoint, NULL, StringBinding);
810 RpcStringFreeA((unsigned char**)&ObjectUuid);
812 return ret;
815 /***********************************************************************
816 * RpcBindingToStringBindingW (RPCRT4.@)
818 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, unsigned short** StringBinding )
820 RPC_STATUS ret;
821 unsigned char *str = NULL;
822 TRACE("(%p,%p)\n", Binding, StringBinding);
823 ret = RpcBindingToStringBindingA(Binding, &str);
824 *StringBinding = RPCRT4_strdupAtoW((char*)str);
825 RpcStringFreeA((unsigned char**)&str);
826 return ret;
829 /***********************************************************************
830 * I_RpcBindingSetAsync (RPCRT4.@)
831 * NOTES
832 * Exists in win9x and winNT, but with different number of arguments
833 * (9x version has 3 arguments, NT has 2).
835 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
837 RpcBinding* bind = (RpcBinding*)Binding;
839 TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
841 bind->BlockingFn = BlockingFn;
843 return RPC_S_OK;
846 /***********************************************************************
847 * RpcBindingCopy (RPCRT4.@)
849 RPC_STATUS RPC_ENTRY RpcBindingCopy(
850 RPC_BINDING_HANDLE SourceBinding,
851 RPC_BINDING_HANDLE* DestinationBinding)
853 RpcBinding *DestBinding;
854 RpcBinding *SrcBinding = (RpcBinding*)SourceBinding;
855 RPC_STATUS status;
857 TRACE("(%p, %p)\n", SourceBinding, DestinationBinding);
859 status = RPCRT4_AllocBinding(&DestBinding, SrcBinding->server);
860 if (status != RPC_S_OK) return status;
862 DestBinding->ObjectUuid = SrcBinding->ObjectUuid;
863 DestBinding->BlockingFn = SrcBinding->BlockingFn;
864 DestBinding->Protseq = RPCRT4_strndupA(SrcBinding->Protseq, -1);
865 DestBinding->NetworkAddr = RPCRT4_strndupA(SrcBinding->NetworkAddr, -1);
866 DestBinding->Endpoint = RPCRT4_strndupA(SrcBinding->Endpoint, -1);
868 if (SrcBinding->AuthInfo) RpcAuthInfo_AddRef(SrcBinding->AuthInfo);
869 DestBinding->AuthInfo = SrcBinding->AuthInfo;
871 *DestinationBinding = DestBinding;
872 return RPC_S_OK;
875 /***********************************************************************
876 * RpcImpersonateClient (RPCRT4.@)
878 * Impersonates the client connected via a binding handle so that security
879 * checks are done in the context of the client.
881 * PARAMS
882 * BindingHandle [I] Handle to the binding to the client.
884 * RETURNS
885 * Success: RPS_S_OK.
886 * Failure: RPC_STATUS value.
888 * NOTES
890 * If BindingHandle is NULL then the function impersonates the client
891 * connected to the binding handle of the current thread.
893 RPC_STATUS WINAPI RpcImpersonateClient(RPC_BINDING_HANDLE BindingHandle)
895 FIXME("(%p): stub\n", BindingHandle);
896 return RPC_S_OK;
899 /***********************************************************************
900 * RpcRevertToSelfEx (RPCRT4.@)
902 * Stops impersonating the client connected to the binding handle so that security
903 * checks are no longer done in the context of the client.
905 * PARAMS
906 * BindingHandle [I] Handle to the binding to the client.
908 * RETURNS
909 * Success: RPS_S_OK.
910 * Failure: RPC_STATUS value.
912 * NOTES
914 * If BindingHandle is NULL then the function stops impersonating the client
915 * connected to the binding handle of the current thread.
917 RPC_STATUS WINAPI RpcRevertToSelfEx(RPC_BINDING_HANDLE BindingHandle)
919 FIXME("(%p): stub\n", BindingHandle);
920 return RPC_S_OK;
923 static RPC_STATUS RpcAuthInfo_Create(unsigned long AuthnLevel, unsigned long AuthnSvc, CredHandle cred, TimeStamp exp, RpcAuthInfo **ret)
925 RpcAuthInfo *AuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo));
926 if (!AuthInfo)
927 return ERROR_OUTOFMEMORY;
929 AuthInfo->AuthnLevel = AuthnLevel;
930 AuthInfo->AuthnSvc = AuthnSvc;
931 AuthInfo->cred = cred;
932 AuthInfo->exp = exp;
933 *ret = AuthInfo;
934 return RPC_S_OK;
937 ULONG RpcAuthInfo_AddRef(RpcAuthInfo *AuthInfo)
939 return InterlockedIncrement(&AuthInfo->refs);
942 ULONG RpcAuthInfo_Release(RpcAuthInfo *AuthInfo)
944 ULONG refs = InterlockedDecrement(&AuthInfo->refs);
946 if (!refs)
948 FreeCredentialsHandle(&AuthInfo->cred);
949 HeapFree(GetProcessHeap(), 0, AuthInfo);
952 return refs;
955 /***********************************************************************
956 * RpcRevertToSelf (RPCRT4.@)
958 RPC_STATUS WINAPI RpcRevertToSelf(void)
960 FIXME("stub\n");
961 return RPC_S_OK;
964 /***********************************************************************
965 * RpcMgmtSetComTimeout (RPCRT4.@)
967 RPC_STATUS WINAPI RpcMgmtSetComTimeout(RPC_BINDING_HANDLE BindingHandle, unsigned int Timeout)
969 FIXME("(%p, %d): stub\n", BindingHandle, Timeout);
970 return RPC_S_OK;
973 /***********************************************************************
974 * RpcBindingInqAuthInfoExA (RPCRT4.@)
976 RPCRTAPI RPC_STATUS RPC_ENTRY
977 RpcBindingInqAuthInfoExA( RPC_BINDING_HANDLE Binding, unsigned char ** ServerPrincName, unsigned long *AuthnLevel,
978 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc,
979 unsigned long RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
981 FIXME("%p %p %p %p %p %p %lu %p\n", Binding, ServerPrincName, AuthnLevel,
982 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
983 return RPC_S_INVALID_BINDING;
986 /***********************************************************************
987 * RpcBindingInqAuthInfoExW (RPCRT4.@)
989 RPCRTAPI RPC_STATUS RPC_ENTRY
990 RpcBindingInqAuthInfoExW( RPC_BINDING_HANDLE Binding, unsigned short ** ServerPrincName, unsigned long *AuthnLevel,
991 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc,
992 unsigned long RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
994 FIXME("%p %p %p %p %p %p %lu %p\n", Binding, ServerPrincName, AuthnLevel,
995 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
996 return RPC_S_INVALID_BINDING;
999 /***********************************************************************
1000 * RpcBindingInqAuthInfoA (RPCRT4.@)
1002 RPCRTAPI RPC_STATUS RPC_ENTRY
1003 RpcBindingInqAuthInfoA( RPC_BINDING_HANDLE Binding, unsigned char ** ServerPrincName, unsigned long *AuthnLevel,
1004 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc )
1006 FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
1007 AuthnSvc, AuthIdentity, AuthzSvc);
1008 return RPC_S_INVALID_BINDING;
1011 /***********************************************************************
1012 * RpcBindingInqAuthInfoW (RPCRT4.@)
1014 RPCRTAPI RPC_STATUS RPC_ENTRY
1015 RpcBindingInqAuthInfoW( RPC_BINDING_HANDLE Binding, unsigned short ** ServerPrincName, unsigned long *AuthnLevel,
1016 unsigned long *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, unsigned long *AuthzSvc )
1018 FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
1019 AuthnSvc, AuthIdentity, AuthzSvc);
1020 return RPC_S_INVALID_BINDING;
1023 /***********************************************************************
1024 * RpcBindingSetAuthInfoExA (RPCRT4.@)
1026 RPCRTAPI RPC_STATUS RPC_ENTRY
1027 RpcBindingSetAuthInfoExA( RPC_BINDING_HANDLE Binding, unsigned char *ServerPrincName,
1028 unsigned long AuthnLevel, unsigned long AuthnSvc,
1029 RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr,
1030 RPC_SECURITY_QOS *SecurityQos )
1032 RpcBinding* bind = (RpcBinding*)Binding;
1033 SECURITY_STATUS r;
1034 CredHandle cred;
1035 TimeStamp exp;
1036 ULONG package_count;
1037 ULONG i;
1038 PSecPkgInfoA packages;
1040 TRACE("%p %s %lu %lu %p %lu %p\n", Binding, debugstr_a((const char*)ServerPrincName),
1041 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1043 if (AuthnLevel != RPC_C_AUTHN_LEVEL_CONNECT)
1045 FIXME("unsupported AuthnLevel %lu\n", AuthnLevel);
1046 return RPC_S_UNKNOWN_AUTHN_LEVEL;
1049 if (AuthzSvr)
1051 FIXME("unsupported AuthzSvr %lu\n", AuthzSvr);
1052 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1055 if (SecurityQos)
1056 FIXME("SecurityQos ignored\n");
1058 r = EnumerateSecurityPackagesA(&package_count, &packages);
1059 if (r != SEC_E_OK)
1061 ERR("EnumerateSecurityPackagesA failed with error 0x%08lx\n", r);
1062 return RPC_S_SEC_PKG_ERROR;
1065 for (i = 0; i < package_count; i++)
1066 if (packages[i].wRPCID == AuthnSvc)
1067 break;
1069 if (i == package_count)
1071 FIXME("unsupported AuthnSvc %lu\n", AuthnSvc);
1072 FreeContextBuffer(packages);
1073 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1076 TRACE("found package %s for service %ld\n", packages[i].Name, AuthnSvc);
1077 r = AcquireCredentialsHandleA((SEC_CHAR *)ServerPrincName, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1078 AuthIdentity, NULL, NULL, &cred, &exp);
1079 FreeContextBuffer(packages);
1080 if (r == ERROR_SUCCESS)
1082 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1083 bind->AuthInfo = NULL;
1084 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, &bind->AuthInfo);
1085 if (r != RPC_S_OK)
1086 FreeCredentialsHandle(&cred);
1087 return RPC_S_OK;
1089 else
1091 ERR("AcquireCredentialsHandleA failed with error 0x%08lx\n", r);
1092 return RPC_S_SEC_PKG_ERROR;
1096 /***********************************************************************
1097 * RpcBindingSetAuthInfoExW (RPCRT4.@)
1099 RPCRTAPI RPC_STATUS RPC_ENTRY
1100 RpcBindingSetAuthInfoExW( RPC_BINDING_HANDLE Binding, unsigned short *ServerPrincName, unsigned long AuthnLevel,
1101 unsigned long AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr,
1102 RPC_SECURITY_QOS *SecurityQos )
1104 RpcBinding* bind = (RpcBinding*)Binding;
1105 SECURITY_STATUS r;
1106 CredHandle cred;
1107 TimeStamp exp;
1108 ULONG package_count;
1109 ULONG i;
1110 PSecPkgInfoW packages;
1112 TRACE("%p %s %lu %lu %p %lu %p\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1113 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1115 if (AuthnLevel != RPC_C_AUTHN_LEVEL_CONNECT)
1117 FIXME("unsupported AuthnLevel %lu\n", AuthnLevel);
1118 return RPC_S_UNKNOWN_AUTHN_LEVEL;
1121 if (AuthzSvr)
1123 FIXME("unsupported AuthzSvr %lu\n", AuthzSvr);
1124 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1127 if (SecurityQos)
1128 FIXME("SecurityQos ignored\n");
1130 r = EnumerateSecurityPackagesW(&package_count, &packages);
1131 if (r != SEC_E_OK)
1133 ERR("EnumerateSecurityPackagesA failed with error 0x%08lx\n", r);
1134 return RPC_S_SEC_PKG_ERROR;
1137 for (i = 0; i < package_count; i++)
1138 if (packages[i].wRPCID == AuthnSvc)
1139 break;
1141 if (i == package_count)
1143 FIXME("unsupported AuthnSvc %lu\n", AuthnSvc);
1144 FreeContextBuffer(packages);
1145 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1148 TRACE("found package %s for service %ld\n", debugstr_w(packages[i].Name), AuthnSvc);
1149 r = AcquireCredentialsHandleW((SEC_WCHAR *)ServerPrincName, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1150 AuthIdentity, NULL, NULL, &cred, &exp);
1151 FreeContextBuffer(packages);
1152 if (r == ERROR_SUCCESS)
1154 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1155 bind->AuthInfo = NULL;
1156 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, &bind->AuthInfo);
1157 if (r != RPC_S_OK)
1158 FreeCredentialsHandle(&cred);
1159 return RPC_S_OK;
1161 else
1163 ERR("AcquireCredentialsHandleA failed with error 0x%08lx\n", r);
1164 return RPC_S_SEC_PKG_ERROR;
1168 /***********************************************************************
1169 * RpcBindingSetAuthInfoA (RPCRT4.@)
1171 RPCRTAPI RPC_STATUS RPC_ENTRY
1172 RpcBindingSetAuthInfoA( RPC_BINDING_HANDLE Binding, unsigned char *ServerPrincName, unsigned long AuthnLevel,
1173 unsigned long AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr )
1175 TRACE("%p %s %lu %lu %p %lu\n", Binding, debugstr_a((const char*)ServerPrincName),
1176 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1177 return RpcBindingSetAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1180 /***********************************************************************
1181 * RpcBindingSetAuthInfoW (RPCRT4.@)
1183 RPCRTAPI RPC_STATUS RPC_ENTRY
1184 RpcBindingSetAuthInfoW( RPC_BINDING_HANDLE Binding, unsigned short *ServerPrincName, unsigned long AuthnLevel,
1185 unsigned long AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, unsigned long AuthzSvr )
1187 TRACE("%p %s %lu %lu %p %lu\n", Binding, debugstr_w((const WCHAR*)ServerPrincName),
1188 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1189 return RpcBindingSetAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1192 /***********************************************************************
1193 * RpcBindingSetOption (RPCRT4.@)
1195 RPC_STATUS WINAPI RpcBindingSetOption(RPC_BINDING_HANDLE BindingHandle, ULONG Option, ULONG OptionValue)
1197 FIXME("(%p, %ld, %ld): stub\n", BindingHandle, Option, OptionValue);
1198 return RPC_S_OK;