crypt32: Add more tests for CertVerifyRevocation.
[wine/testsucceed.git] / dlls / rpcrt4 / rpc_binding.c
blob212e201dbbb5fb4335fac71e472e076dca3bbed3
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 "winternl.h"
34 #include "wine/unicode.h"
36 #include "rpc.h"
37 #include "rpcndr.h"
39 #include "wine/debug.h"
41 #include "rpc_binding.h"
42 #include "rpc_assoc.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
46 LPSTR RPCRT4_strndupA(LPCSTR src, INT slen)
48 DWORD len;
49 LPSTR s;
50 if (!src) return NULL;
51 if (slen == -1) slen = strlen(src);
52 len = slen;
53 s = HeapAlloc(GetProcessHeap(), 0, len+1);
54 memcpy(s, src, len);
55 s[len] = 0;
56 return s;
59 LPSTR RPCRT4_strdupWtoA(LPCWSTR src)
61 DWORD len;
62 LPSTR s;
63 if (!src) return NULL;
64 len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
65 s = HeapAlloc(GetProcessHeap(), 0, len);
66 WideCharToMultiByte(CP_ACP, 0, src, -1, s, len, NULL, NULL);
67 return s;
70 LPWSTR RPCRT4_strdupAtoW(LPCSTR src)
72 DWORD len;
73 LPWSTR s;
74 if (!src) return NULL;
75 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
76 s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
77 MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
78 return s;
81 static LPWSTR RPCRT4_strndupAtoW(LPCSTR src, INT slen)
83 DWORD len;
84 LPWSTR s;
85 if (!src) return NULL;
86 len = MultiByteToWideChar(CP_ACP, 0, src, slen, NULL, 0);
87 s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
88 MultiByteToWideChar(CP_ACP, 0, src, slen, s, len);
89 return s;
92 LPWSTR RPCRT4_strndupW(LPCWSTR src, INT slen)
94 DWORD len;
95 LPWSTR s;
96 if (!src) return NULL;
97 if (slen == -1) slen = strlenW(src);
98 len = slen;
99 s = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
100 memcpy(s, src, len*sizeof(WCHAR));
101 s[len] = 0;
102 return s;
105 void RPCRT4_strfree(LPSTR src)
107 HeapFree(GetProcessHeap(), 0, src);
110 static RPC_STATUS RPCRT4_AllocBinding(RpcBinding** Binding, BOOL server)
112 RpcBinding* NewBinding;
114 NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
115 NewBinding->refs = 1;
116 NewBinding->server = server;
118 *Binding = NewBinding;
120 return RPC_S_OK;
123 static RPC_STATUS RPCRT4_CreateBindingA(RpcBinding** Binding, BOOL server, LPCSTR Protseq)
125 RpcBinding* NewBinding;
127 RPCRT4_AllocBinding(&NewBinding, server);
128 NewBinding->Protseq = RPCRT4_strdupA(Protseq);
130 TRACE("binding: %p\n", NewBinding);
131 *Binding = NewBinding;
133 return RPC_S_OK;
136 static RPC_STATUS RPCRT4_CreateBindingW(RpcBinding** Binding, BOOL server, LPCWSTR Protseq)
138 RpcBinding* NewBinding;
140 RPCRT4_AllocBinding(&NewBinding, server);
141 NewBinding->Protseq = RPCRT4_strdupWtoA(Protseq);
143 TRACE("binding: %p\n", NewBinding);
144 *Binding = NewBinding;
146 return RPC_S_OK;
149 static RPC_STATUS RPCRT4_CompleteBindingA(RpcBinding* Binding, LPCSTR NetworkAddr,
150 LPCSTR Endpoint, LPCSTR NetworkOptions)
152 RPC_STATUS status;
154 TRACE("(RpcBinding == ^%p, NetworkAddr == %s, EndPoint == %s, NetworkOptions == %s)\n", Binding,
155 debugstr_a(NetworkAddr), debugstr_a(Endpoint), debugstr_a(NetworkOptions));
157 RPCRT4_strfree(Binding->NetworkAddr);
158 Binding->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
159 RPCRT4_strfree(Binding->Endpoint);
160 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
161 HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
162 Binding->NetworkOptions = RPCRT4_strdupAtoW(NetworkOptions);
164 /* only attempt to get an association if the binding is complete */
165 if (Endpoint && Endpoint[0] != '\0')
167 status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
168 Binding->Endpoint, Binding->NetworkOptions,
169 &Binding->Assoc);
170 if (status != RPC_S_OK)
171 return status;
174 return RPC_S_OK;
177 static RPC_STATUS RPCRT4_CompleteBindingW(RpcBinding* Binding, LPCWSTR NetworkAddr,
178 LPCWSTR Endpoint, LPCWSTR NetworkOptions)
180 RPC_STATUS status;
182 TRACE("(RpcBinding == ^%p, NetworkAddr == %s, EndPoint == %s, NetworkOptions == %s)\n", Binding,
183 debugstr_w(NetworkAddr), debugstr_w(Endpoint), debugstr_w(NetworkOptions));
185 RPCRT4_strfree(Binding->NetworkAddr);
186 Binding->NetworkAddr = RPCRT4_strdupWtoA(NetworkAddr);
187 RPCRT4_strfree(Binding->Endpoint);
188 Binding->Endpoint = RPCRT4_strdupWtoA(Endpoint);
189 HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
190 Binding->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
192 /* only attempt to get an association if the binding is complete */
193 if (Endpoint && Endpoint[0] != '\0')
195 status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
196 Binding->Endpoint, Binding->NetworkOptions,
197 &Binding->Assoc);
198 if (status != RPC_S_OK)
199 return status;
202 return RPC_S_OK;
205 RPC_STATUS RPCRT4_ResolveBinding(RpcBinding* Binding, LPCSTR Endpoint)
207 RPC_STATUS status;
209 TRACE("(RpcBinding == ^%p, EndPoint == \"%s\"\n", Binding, Endpoint);
211 RPCRT4_strfree(Binding->Endpoint);
212 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
214 if (Binding->Assoc) RpcAssoc_Release(Binding->Assoc);
215 Binding->Assoc = NULL;
216 status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
217 Binding->Endpoint, Binding->NetworkOptions,
218 &Binding->Assoc);
219 if (status != RPC_S_OK)
220 return status;
222 return RPC_S_OK;
225 RPC_STATUS RPCRT4_SetBindingObject(RpcBinding* Binding, const UUID* ObjectUuid)
227 TRACE("(*RpcBinding == ^%p, UUID == %s)\n", Binding, debugstr_guid(ObjectUuid));
228 if (ObjectUuid) Binding->ObjectUuid = *ObjectUuid;
229 else UuidCreateNil(&Binding->ObjectUuid);
230 return RPC_S_OK;
233 RPC_STATUS RPCRT4_MakeBinding(RpcBinding** Binding, RpcConnection* Connection)
235 RpcBinding* NewBinding;
236 TRACE("(RpcBinding == ^%p, Connection == ^%p)\n", Binding, Connection);
238 RPCRT4_AllocBinding(&NewBinding, Connection->server);
239 NewBinding->Protseq = RPCRT4_strdupA(rpcrt4_conn_get_name(Connection));
240 NewBinding->NetworkAddr = RPCRT4_strdupA(Connection->NetworkAddr);
241 NewBinding->Endpoint = RPCRT4_strdupA(Connection->Endpoint);
242 NewBinding->FromConn = Connection;
244 TRACE("binding: %p\n", NewBinding);
245 *Binding = NewBinding;
247 return RPC_S_OK;
250 void RPCRT4_AddRefBinding(RpcBinding* Binding)
252 InterlockedIncrement(&Binding->refs);
255 RPC_STATUS RPCRT4_ReleaseBinding(RpcBinding* Binding)
257 if (InterlockedDecrement(&Binding->refs))
258 return RPC_S_OK;
260 TRACE("binding: %p\n", Binding);
261 if (Binding->Assoc) RpcAssoc_Release(Binding->Assoc);
262 RPCRT4_strfree(Binding->Endpoint);
263 RPCRT4_strfree(Binding->NetworkAddr);
264 RPCRT4_strfree(Binding->Protseq);
265 HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
266 if (Binding->AuthInfo) RpcAuthInfo_Release(Binding->AuthInfo);
267 if (Binding->QOS) RpcQualityOfService_Release(Binding->QOS);
268 HeapFree(GetProcessHeap(), 0, Binding);
269 return RPC_S_OK;
272 RPC_STATUS RPCRT4_OpenBinding(RpcBinding* Binding, RpcConnection** Connection,
273 const RPC_SYNTAX_IDENTIFIER *TransferSyntax,
274 const RPC_SYNTAX_IDENTIFIER *InterfaceId)
276 TRACE("(Binding == ^%p)\n", Binding);
278 if (!Binding->server) {
279 return RpcAssoc_GetClientConnection(Binding->Assoc, InterfaceId,
280 TransferSyntax, Binding->AuthInfo, Binding->QOS, Connection);
281 } else {
282 /* we already have a connection with acceptable binding, so use it */
283 if (Binding->FromConn) {
284 *Connection = Binding->FromConn;
285 return RPC_S_OK;
286 } else {
287 ERR("no connection in binding\n");
288 return RPC_S_INTERNAL_ERROR;
293 RPC_STATUS RPCRT4_CloseBinding(RpcBinding* Binding, RpcConnection* Connection)
295 TRACE("(Binding == ^%p)\n", Binding);
296 if (!Connection) return RPC_S_OK;
297 if (Binding->server) {
298 /* don't destroy a connection that is cached in the binding */
299 if (Binding->FromConn == Connection)
300 return RPC_S_OK;
301 return RPCRT4_DestroyConnection(Connection);
303 else {
304 RpcAssoc_ReleaseIdleConnection(Binding->Assoc, Connection);
305 return RPC_S_OK;
309 static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
311 DWORD len = strlen(dst), slen = strlen(src);
312 LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
313 if (!ndst)
315 HeapFree(GetProcessHeap(), 0, dst);
316 return NULL;
318 ndst[len] = ',';
319 memcpy(ndst+len+1, src, slen+1);
320 return ndst;
323 static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
325 DWORD len = strlenW(dst), slen = strlenW(src);
326 LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
327 if (!ndst)
329 HeapFree(GetProcessHeap(), 0, dst);
330 return NULL;
332 ndst[len] = ',';
333 memcpy(ndst+len+1, src, (slen+1)*sizeof(WCHAR));
334 return ndst;
337 /* Copies the escaped version of a component into a string binding.
338 * Note: doesn't nul-terminate the string */
339 static RPC_CSTR escape_string_binding_component(RPC_CSTR string_binding,
340 const unsigned char *component)
342 for (; *component; component++) {
343 switch (*component) {
344 case '@':
345 case ':':
346 case '[':
347 case ']':
348 case '\\':
349 *string_binding++ = '\\';
350 *string_binding++ = *component;
351 break;
352 default:
353 *string_binding++ = *component;
354 break;
357 return string_binding;
360 static RPC_WSTR escape_string_binding_componentW(RPC_WSTR string_binding,
361 const WCHAR *component)
363 for (; *component; component++) {
364 switch (*component) {
365 case '@':
366 case ':':
367 case '[':
368 case ']':
369 case '\\':
370 *string_binding++ = '\\';
371 *string_binding++ = *component;
372 break;
373 default:
374 *string_binding++ = *component;
375 break;
378 return string_binding;
381 static const unsigned char *string_binding_find_delimiter(
382 const unsigned char *string_binding, unsigned char delim)
384 const unsigned char *next;
385 for (next = string_binding; *next; next++) {
386 if (*next == '\\') {
387 next++;
388 continue;
390 if (*next == delim)
391 return next;
393 return NULL;
396 static const WCHAR *string_binding_find_delimiterW(
397 const WCHAR *string_binding, WCHAR delim)
399 const WCHAR *next;
400 for (next = string_binding; *next; next++) {
401 if (*next == '\\') {
402 next++;
403 continue;
405 if (*next == delim)
406 return next;
408 return NULL;
411 static RPC_CSTR unescape_string_binding_component(
412 const unsigned char *string_binding, int len)
414 RPC_CSTR component, p;
416 if (len == -1) len = strlen((const char *)string_binding);
418 component = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(*component));
419 if (!component) return NULL;
420 for (p = component; len > 0; string_binding++, len--) {
421 if (*string_binding == '\\') {
422 string_binding++;
423 len--;
424 *p++ = *string_binding;
425 } else {
426 *p++ = *string_binding;
429 *p = '\0';
430 return component;
433 static RPC_WSTR unescape_string_binding_componentW(
434 const WCHAR *string_binding, int len)
436 RPC_WSTR component, p;
438 if (len == -1) len = strlenW(string_binding);
440 component = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(*component));
441 if (!component) return NULL;
442 for (p = component; len > 0; string_binding++, len--) {
443 if (*string_binding == '\\') {
444 string_binding++;
445 len--;
446 *p++ = *string_binding;
447 } else {
448 *p++ = *string_binding;
451 *p = '\0';
452 return component;
455 /***********************************************************************
456 * RpcStringBindingComposeA (RPCRT4.@)
458 RPC_STATUS WINAPI RpcStringBindingComposeA(RPC_CSTR ObjUuid, RPC_CSTR Protseq,
459 RPC_CSTR NetworkAddr, RPC_CSTR Endpoint,
460 RPC_CSTR Options, RPC_CSTR *StringBinding )
462 DWORD len = 1;
463 RPC_CSTR data;
465 TRACE( "(%s,%s,%s,%s,%s,%p)\n",
466 debugstr_a( (char*)ObjUuid ), debugstr_a( (char*)Protseq ),
467 debugstr_a( (char*)NetworkAddr ), debugstr_a( (char*)Endpoint ),
468 debugstr_a( (char*)Options ), StringBinding );
470 /* overestimate for each component for escaping of delimiters */
471 if (ObjUuid && *ObjUuid) len += strlen((char*)ObjUuid) * 2 + 1;
472 if (Protseq && *Protseq) len += strlen((char*)Protseq) * 2 + 1;
473 if (NetworkAddr && *NetworkAddr) len += strlen((char*)NetworkAddr) * 2;
474 if (Endpoint && *Endpoint) len += strlen((char*)Endpoint) * 2 + 2;
475 if (Options && *Options) len += strlen((char*)Options) * 2 + 2;
477 data = HeapAlloc(GetProcessHeap(), 0, len);
478 *StringBinding = data;
480 if (ObjUuid && *ObjUuid) {
481 data = escape_string_binding_component(data, ObjUuid);
482 *data++ = '@';
484 if (Protseq && *Protseq) {
485 data = escape_string_binding_component(data, Protseq);
486 *data++ = ':';
488 if (NetworkAddr && *NetworkAddr)
489 data = escape_string_binding_component(data, NetworkAddr);
491 if ((Endpoint && *Endpoint) ||
492 (Options && *Options)) {
493 *data++ = '[';
494 if (Endpoint && *Endpoint) {
495 data = escape_string_binding_component(data, Endpoint);
496 if (Options && *Options) *data++ = ',';
498 if (Options && *Options) {
499 data = escape_string_binding_component(data, Options);
501 *data++ = ']';
503 *data = 0;
505 return RPC_S_OK;
508 /***********************************************************************
509 * RpcStringBindingComposeW (RPCRT4.@)
511 RPC_STATUS WINAPI RpcStringBindingComposeW( RPC_WSTR ObjUuid, RPC_WSTR Protseq,
512 RPC_WSTR NetworkAddr, RPC_WSTR Endpoint,
513 RPC_WSTR Options, RPC_WSTR* StringBinding )
515 DWORD len = 1;
516 RPC_WSTR data;
518 TRACE("(%s,%s,%s,%s,%s,%p)\n",
519 debugstr_w( ObjUuid ), debugstr_w( Protseq ),
520 debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
521 debugstr_w( Options ), StringBinding);
523 /* overestimate for each component for escaping of delimiters */
524 if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) * 2 + 1;
525 if (Protseq && *Protseq) len += strlenW(Protseq) * 2 + 1;
526 if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr) * 2;
527 if (Endpoint && *Endpoint) len += strlenW(Endpoint) * 2 + 2;
528 if (Options && *Options) len += strlenW(Options) * 2 + 2;
530 data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
531 *StringBinding = data;
533 if (ObjUuid && *ObjUuid) {
534 data = escape_string_binding_componentW(data, ObjUuid);
535 *data++ = '@';
537 if (Protseq && *Protseq) {
538 data = escape_string_binding_componentW(data, Protseq);
539 *data++ = ':';
541 if (NetworkAddr && *NetworkAddr) {
542 data = escape_string_binding_componentW(data, NetworkAddr);
544 if ((Endpoint && *Endpoint) ||
545 (Options && *Options)) {
546 *data++ = '[';
547 if (Endpoint && *Endpoint) {
548 data = escape_string_binding_componentW(data, Endpoint);
549 if (Options && *Options) *data++ = ',';
551 if (Options && *Options) {
552 data = escape_string_binding_componentW(data, Options);
554 *data++ = ']';
556 *data = 0;
558 return RPC_S_OK;
562 /***********************************************************************
563 * RpcStringBindingParseA (RPCRT4.@)
565 RPC_STATUS WINAPI RpcStringBindingParseA( RPC_CSTR StringBinding, RPC_CSTR *ObjUuid,
566 RPC_CSTR *Protseq, RPC_CSTR *NetworkAddr,
567 RPC_CSTR *Endpoint, RPC_CSTR *Options)
569 const unsigned char *data, *next;
570 static const char ep_opt[] = "endpoint=";
571 BOOL endpoint_already_found = FALSE;
573 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a((char*)StringBinding),
574 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
576 if (ObjUuid) *ObjUuid = NULL;
577 if (Protseq) *Protseq = NULL;
578 if (NetworkAddr) *NetworkAddr = NULL;
579 if (Endpoint) *Endpoint = NULL;
580 if (Options) *Options = NULL;
582 data = StringBinding;
584 next = string_binding_find_delimiter(data, '@');
585 if (next) {
586 UUID uuid;
587 RPC_STATUS status;
588 RPC_CSTR str_uuid = unescape_string_binding_component(data, next - data);
589 status = UuidFromStringA(str_uuid, &uuid);
590 if (status != RPC_S_OK) {
591 HeapFree(GetProcessHeap(), 0, str_uuid);
592 return status;
594 if (ObjUuid)
595 *ObjUuid = str_uuid;
596 else
597 HeapFree(GetProcessHeap(), 0, str_uuid);
598 data = next+1;
601 next = string_binding_find_delimiter(data, ':');
602 if (next) {
603 if (Protseq) *Protseq = unescape_string_binding_component(data, next - data);
604 data = next+1;
607 next = string_binding_find_delimiter(data, '[');
608 if (next) {
609 const unsigned char *close;
610 RPC_CSTR opt;
612 if (NetworkAddr) *NetworkAddr = unescape_string_binding_component(data, next - data);
613 data = next+1;
614 close = string_binding_find_delimiter(data, ']');
615 if (!close) goto fail;
617 /* tokenize options */
618 while (data < close) {
619 next = string_binding_find_delimiter(data, ',');
620 if (!next || next > close) next = close;
621 /* FIXME: this is kind of inefficient */
622 opt = unescape_string_binding_component(data, next - data);
623 data = next+1;
625 /* parse option */
626 next = string_binding_find_delimiter(opt, '=');
627 if (!next) {
628 /* not an option, must be an endpoint */
629 if (endpoint_already_found) goto fail;
630 if (Endpoint) *Endpoint = opt;
631 else HeapFree(GetProcessHeap(), 0, opt);
632 endpoint_already_found = TRUE;
633 } else {
634 if (strncmp((const char *)opt, ep_opt, strlen(ep_opt)) == 0) {
635 /* endpoint option */
636 if (endpoint_already_found) goto fail;
637 if (Endpoint) *Endpoint = unescape_string_binding_component(next+1, -1);
638 HeapFree(GetProcessHeap(), 0, opt);
639 endpoint_already_found = TRUE;
640 } else {
641 /* network option */
642 if (Options) {
643 if (*Options) {
644 /* FIXME: this is kind of inefficient */
645 *Options = (unsigned char*) RPCRT4_strconcatA( (char*)*Options, (char *)opt);
646 HeapFree(GetProcessHeap(), 0, opt);
647 } else
648 *Options = opt;
649 } else
650 HeapFree(GetProcessHeap(), 0, opt);
655 data = close+1;
656 if (*data) goto fail;
658 else if (NetworkAddr)
659 *NetworkAddr = unescape_string_binding_component(data, -1);
661 return RPC_S_OK;
663 fail:
664 if (ObjUuid) RpcStringFreeA(ObjUuid);
665 if (Protseq) RpcStringFreeA(Protseq);
666 if (NetworkAddr) RpcStringFreeA(NetworkAddr);
667 if (Endpoint) RpcStringFreeA(Endpoint);
668 if (Options) RpcStringFreeA(Options);
669 return RPC_S_INVALID_STRING_BINDING;
672 /***********************************************************************
673 * RpcStringBindingParseW (RPCRT4.@)
675 RPC_STATUS WINAPI RpcStringBindingParseW( RPC_WSTR StringBinding, RPC_WSTR *ObjUuid,
676 RPC_WSTR *Protseq, RPC_WSTR *NetworkAddr,
677 RPC_WSTR *Endpoint, RPC_WSTR *Options)
679 const WCHAR *data, *next;
680 static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
681 BOOL endpoint_already_found = FALSE;
683 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
684 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
686 if (ObjUuid) *ObjUuid = NULL;
687 if (Protseq) *Protseq = NULL;
688 if (NetworkAddr) *NetworkAddr = NULL;
689 if (Endpoint) *Endpoint = NULL;
690 if (Options) *Options = NULL;
692 data = StringBinding;
694 next = string_binding_find_delimiterW(data, '@');
695 if (next) {
696 UUID uuid;
697 RPC_STATUS status;
698 RPC_WSTR str_uuid = unescape_string_binding_componentW(data, next - data);
699 status = UuidFromStringW(str_uuid, &uuid);
700 if (status != RPC_S_OK) {
701 HeapFree(GetProcessHeap(), 0, str_uuid);
702 return status;
704 if (ObjUuid)
705 *ObjUuid = str_uuid;
706 else
707 HeapFree(GetProcessHeap(), 0, str_uuid);
708 data = next+1;
711 next = string_binding_find_delimiterW(data, ':');
712 if (next) {
713 if (Protseq) *Protseq = unescape_string_binding_componentW(data, next - data);
714 data = next+1;
717 next = string_binding_find_delimiterW(data, '[');
718 if (next) {
719 const WCHAR *close;
720 RPC_WSTR opt;
722 if (NetworkAddr) *NetworkAddr = unescape_string_binding_componentW(data, next - data);
723 data = next+1;
724 close = string_binding_find_delimiterW(data, ']');
725 if (!close) goto fail;
727 /* tokenize options */
728 while (data < close) {
729 next = string_binding_find_delimiterW(data, ',');
730 if (!next || next > close) next = close;
731 /* FIXME: this is kind of inefficient */
732 opt = unescape_string_binding_componentW(data, next - data);
733 data = next+1;
735 /* parse option */
736 next = string_binding_find_delimiterW(opt, '=');
737 if (!next) {
738 /* not an option, must be an endpoint */
739 if (endpoint_already_found) goto fail;
740 if (Endpoint) *Endpoint = opt;
741 else HeapFree(GetProcessHeap(), 0, opt);
742 endpoint_already_found = TRUE;
743 } else {
744 if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
745 /* endpoint option */
746 if (endpoint_already_found) goto fail;
747 if (Endpoint) *Endpoint = unescape_string_binding_componentW(next+1, -1);
748 HeapFree(GetProcessHeap(), 0, opt);
749 endpoint_already_found = TRUE;
750 } else {
751 /* network option */
752 if (Options) {
753 if (*Options) {
754 /* FIXME: this is kind of inefficient */
755 *Options = RPCRT4_strconcatW(*Options, opt);
756 HeapFree(GetProcessHeap(), 0, opt);
757 } else
758 *Options = opt;
759 } else
760 HeapFree(GetProcessHeap(), 0, opt);
765 data = close+1;
766 if (*data) goto fail;
767 } else if (NetworkAddr)
768 *NetworkAddr = unescape_string_binding_componentW(data, -1);
770 return RPC_S_OK;
772 fail:
773 if (ObjUuid) RpcStringFreeW(ObjUuid);
774 if (Protseq) RpcStringFreeW(Protseq);
775 if (NetworkAddr) RpcStringFreeW(NetworkAddr);
776 if (Endpoint) RpcStringFreeW(Endpoint);
777 if (Options) RpcStringFreeW(Options);
778 return RPC_S_INVALID_STRING_BINDING;
781 /***********************************************************************
782 * RpcBindingFree (RPCRT4.@)
784 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
786 RPC_STATUS status;
787 TRACE("(%p) = %p\n", Binding, *Binding);
788 if (*Binding)
789 status = RPCRT4_ReleaseBinding(*Binding);
790 else
791 status = RPC_S_INVALID_BINDING;
792 if (status == RPC_S_OK) *Binding = NULL;
793 return status;
796 /***********************************************************************
797 * RpcBindingVectorFree (RPCRT4.@)
799 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
801 RPC_STATUS status;
802 ULONG c;
804 TRACE("(%p)\n", BindingVector);
805 for (c=0; c<(*BindingVector)->Count; c++) {
806 status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
808 HeapFree(GetProcessHeap(), 0, *BindingVector);
809 *BindingVector = NULL;
810 return RPC_S_OK;
813 /***********************************************************************
814 * RpcBindingInqObject (RPCRT4.@)
816 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
818 RpcBinding* bind = Binding;
820 TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
821 *ObjectUuid = bind->ObjectUuid;
822 return RPC_S_OK;
825 /***********************************************************************
826 * RpcBindingSetObject (RPCRT4.@)
828 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
830 RpcBinding* bind = Binding;
832 TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
833 if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
834 return RPCRT4_SetBindingObject(Binding, ObjectUuid);
837 /***********************************************************************
838 * RpcBindingFromStringBindingA (RPCRT4.@)
840 RPC_STATUS WINAPI RpcBindingFromStringBindingA( RPC_CSTR StringBinding, RPC_BINDING_HANDLE* Binding )
842 RPC_STATUS ret;
843 RpcBinding* bind = NULL;
844 RPC_CSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
845 UUID Uuid;
847 TRACE("(%s,%p)\n", debugstr_a((char*)StringBinding), Binding);
849 ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
850 &NetworkAddr, &Endpoint, &Options);
851 if (ret != RPC_S_OK) return ret;
853 ret = UuidFromStringA(ObjectUuid, &Uuid);
855 if (ret == RPC_S_OK)
856 ret = RPCRT4_CreateBindingA(&bind, FALSE, (char*)Protseq);
857 if (ret != RPC_S_OK) return ret;
858 ret = RPCRT4_SetBindingObject(bind, &Uuid);
859 if (ret == RPC_S_OK)
860 ret = RPCRT4_CompleteBindingA(bind, (char*)NetworkAddr, (char*)Endpoint, (char*)Options);
862 RpcStringFreeA(&Options);
863 RpcStringFreeA(&Endpoint);
864 RpcStringFreeA(&NetworkAddr);
865 RpcStringFreeA(&Protseq);
866 RpcStringFreeA(&ObjectUuid);
868 if (ret == RPC_S_OK)
869 *Binding = (RPC_BINDING_HANDLE)bind;
870 else
871 RPCRT4_ReleaseBinding(bind);
873 return ret;
876 /***********************************************************************
877 * RpcBindingFromStringBindingW (RPCRT4.@)
879 RPC_STATUS WINAPI RpcBindingFromStringBindingW( RPC_WSTR StringBinding, RPC_BINDING_HANDLE* Binding )
881 RPC_STATUS ret;
882 RpcBinding* bind = NULL;
883 RPC_WSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
884 UUID Uuid;
886 TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
888 ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
889 &NetworkAddr, &Endpoint, &Options);
890 if (ret != RPC_S_OK) return ret;
892 ret = UuidFromStringW(ObjectUuid, &Uuid);
894 if (ret == RPC_S_OK)
895 ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
896 if (ret != RPC_S_OK) return ret;
897 ret = RPCRT4_SetBindingObject(bind, &Uuid);
898 if (ret == RPC_S_OK)
899 ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
901 RpcStringFreeW(&Options);
902 RpcStringFreeW(&Endpoint);
903 RpcStringFreeW(&NetworkAddr);
904 RpcStringFreeW(&Protseq);
905 RpcStringFreeW(&ObjectUuid);
907 if (ret == RPC_S_OK)
908 *Binding = (RPC_BINDING_HANDLE)bind;
909 else
910 RPCRT4_ReleaseBinding(bind);
912 return ret;
915 /***********************************************************************
916 * RpcBindingToStringBindingA (RPCRT4.@)
918 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, RPC_CSTR *StringBinding )
920 RPC_STATUS ret;
921 RpcBinding* bind = Binding;
922 RPC_CSTR ObjectUuid;
924 TRACE("(%p,%p)\n", Binding, StringBinding);
926 if (UuidIsNil(&bind->ObjectUuid, &ret))
927 ObjectUuid = NULL;
928 else
930 ret = UuidToStringA(&bind->ObjectUuid, &ObjectUuid);
931 if (ret != RPC_S_OK) return ret;
934 ret = RpcStringBindingComposeA(ObjectUuid, (unsigned char*)bind->Protseq, (unsigned char*) bind->NetworkAddr,
935 (unsigned char*) bind->Endpoint, NULL, StringBinding);
937 RpcStringFreeA(&ObjectUuid);
939 return ret;
942 /***********************************************************************
943 * RpcBindingToStringBindingW (RPCRT4.@)
945 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, RPC_WSTR *StringBinding )
947 RPC_STATUS ret;
948 unsigned char *str = NULL;
949 TRACE("(%p,%p)\n", Binding, StringBinding);
950 ret = RpcBindingToStringBindingA(Binding, &str);
951 *StringBinding = RPCRT4_strdupAtoW((char*)str);
952 RpcStringFreeA(&str);
953 return ret;
956 /***********************************************************************
957 * I_RpcBindingInqTransportType (RPCRT4.@)
959 RPC_STATUS WINAPI I_RpcBindingInqTransportType( RPC_BINDING_HANDLE Binding, unsigned int * Type )
962 FIXME( "(%p,%p): stub\n", Binding, Type);
963 *Type = TRANSPORT_TYPE_LPC;
964 return RPC_S_OK;
967 /***********************************************************************
968 * I_RpcBindingSetAsync (RPCRT4.@)
969 * NOTES
970 * Exists in win9x and winNT, but with different number of arguments
971 * (9x version has 3 arguments, NT has 2).
973 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
975 RpcBinding* bind = Binding;
977 TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
979 bind->BlockingFn = BlockingFn;
981 return RPC_S_OK;
984 /***********************************************************************
985 * RpcBindingCopy (RPCRT4.@)
987 RPC_STATUS RPC_ENTRY RpcBindingCopy(
988 RPC_BINDING_HANDLE SourceBinding,
989 RPC_BINDING_HANDLE* DestinationBinding)
991 RpcBinding *DestBinding;
992 RpcBinding *SrcBinding = SourceBinding;
993 RPC_STATUS status;
995 TRACE("(%p, %p)\n", SourceBinding, DestinationBinding);
997 status = RPCRT4_AllocBinding(&DestBinding, SrcBinding->server);
998 if (status != RPC_S_OK) return status;
1000 DestBinding->ObjectUuid = SrcBinding->ObjectUuid;
1001 DestBinding->BlockingFn = SrcBinding->BlockingFn;
1002 DestBinding->Protseq = RPCRT4_strndupA(SrcBinding->Protseq, -1);
1003 DestBinding->NetworkAddr = RPCRT4_strndupA(SrcBinding->NetworkAddr, -1);
1004 DestBinding->Endpoint = RPCRT4_strndupA(SrcBinding->Endpoint, -1);
1005 DestBinding->NetworkOptions = RPCRT4_strdupW(SrcBinding->NetworkOptions);
1006 if (SrcBinding->Assoc) SrcBinding->Assoc->refs++;
1007 DestBinding->Assoc = SrcBinding->Assoc;
1009 if (SrcBinding->AuthInfo) RpcAuthInfo_AddRef(SrcBinding->AuthInfo);
1010 DestBinding->AuthInfo = SrcBinding->AuthInfo;
1011 if (SrcBinding->QOS) RpcQualityOfService_AddRef(SrcBinding->QOS);
1012 DestBinding->QOS = SrcBinding->QOS;
1014 *DestinationBinding = DestBinding;
1015 return RPC_S_OK;
1018 /***********************************************************************
1019 * RpcBindingReset (RPCRT4.@)
1021 RPC_STATUS RPC_ENTRY RpcBindingReset(RPC_BINDING_HANDLE Binding)
1023 RpcBinding *bind = Binding;
1025 TRACE("(%p)\n", Binding);
1027 RPCRT4_strfree(bind->Endpoint);
1028 bind->Endpoint = NULL;
1029 if (bind->Assoc) RpcAssoc_Release(bind->Assoc);
1030 bind->Assoc = NULL;
1032 return RPC_S_OK;
1035 /***********************************************************************
1036 * RpcImpersonateClient (RPCRT4.@)
1038 * Impersonates the client connected via a binding handle so that security
1039 * checks are done in the context of the client.
1041 * PARAMS
1042 * BindingHandle [I] Handle to the binding to the client.
1044 * RETURNS
1045 * Success: RPS_S_OK.
1046 * Failure: RPC_STATUS value.
1048 * NOTES
1050 * If BindingHandle is NULL then the function impersonates the client
1051 * connected to the binding handle of the current thread.
1053 RPC_STATUS WINAPI RpcImpersonateClient(RPC_BINDING_HANDLE BindingHandle)
1055 FIXME("(%p): stub\n", BindingHandle);
1056 ImpersonateSelf(SecurityImpersonation);
1057 return RPC_S_OK;
1060 /***********************************************************************
1061 * RpcRevertToSelfEx (RPCRT4.@)
1063 * Stops impersonating the client connected to the binding handle so that security
1064 * checks are no longer done in the context of the client.
1066 * PARAMS
1067 * BindingHandle [I] Handle to the binding to the client.
1069 * RETURNS
1070 * Success: RPS_S_OK.
1071 * Failure: RPC_STATUS value.
1073 * NOTES
1075 * If BindingHandle is NULL then the function stops impersonating the client
1076 * connected to the binding handle of the current thread.
1078 RPC_STATUS WINAPI RpcRevertToSelfEx(RPC_BINDING_HANDLE BindingHandle)
1080 FIXME("(%p): stub\n", BindingHandle);
1081 return RPC_S_OK;
1084 static inline BOOL has_nt_auth_identity(ULONG AuthnLevel)
1086 switch (AuthnLevel)
1088 case RPC_C_AUTHN_GSS_NEGOTIATE:
1089 case RPC_C_AUTHN_WINNT:
1090 case RPC_C_AUTHN_GSS_KERBEROS:
1091 return TRUE;
1092 default:
1093 return FALSE;
1097 static RPC_STATUS RpcAuthInfo_Create(ULONG AuthnLevel, ULONG AuthnSvc,
1098 CredHandle cred, TimeStamp exp,
1099 ULONG cbMaxToken,
1100 RPC_AUTH_IDENTITY_HANDLE identity,
1101 RpcAuthInfo **ret)
1103 RpcAuthInfo *AuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo));
1104 if (!AuthInfo)
1105 return ERROR_OUTOFMEMORY;
1107 AuthInfo->refs = 1;
1108 AuthInfo->AuthnLevel = AuthnLevel;
1109 AuthInfo->AuthnSvc = AuthnSvc;
1110 AuthInfo->cred = cred;
1111 AuthInfo->exp = exp;
1112 AuthInfo->cbMaxToken = cbMaxToken;
1113 AuthInfo->identity = identity;
1114 AuthInfo->server_principal_name = NULL;
1116 /* duplicate the SEC_WINNT_AUTH_IDENTITY structure, if applicable, to
1117 * enable better matching in RpcAuthInfo_IsEqual */
1118 if (identity && has_nt_auth_identity(AuthnSvc))
1120 const SEC_WINNT_AUTH_IDENTITY_W *nt_identity = identity;
1121 AuthInfo->nt_identity = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo->nt_identity));
1122 if (!AuthInfo->nt_identity)
1124 HeapFree(GetProcessHeap(), 0, AuthInfo);
1125 return ERROR_OUTOFMEMORY;
1128 AuthInfo->nt_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1129 if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1130 AuthInfo->nt_identity->User = RPCRT4_strndupW(nt_identity->User, nt_identity->UserLength);
1131 else
1132 AuthInfo->nt_identity->User = RPCRT4_strndupAtoW((const char *)nt_identity->User, nt_identity->UserLength);
1133 AuthInfo->nt_identity->UserLength = nt_identity->UserLength;
1134 if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1135 AuthInfo->nt_identity->Domain = RPCRT4_strndupW(nt_identity->Domain, nt_identity->DomainLength);
1136 else
1137 AuthInfo->nt_identity->Domain = RPCRT4_strndupAtoW((const char *)nt_identity->Domain, nt_identity->DomainLength);
1138 AuthInfo->nt_identity->DomainLength = nt_identity->DomainLength;
1139 if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1140 AuthInfo->nt_identity->Password = RPCRT4_strndupW(nt_identity->Password, nt_identity->PasswordLength);
1141 else
1142 AuthInfo->nt_identity->Password = RPCRT4_strndupAtoW((const char *)nt_identity->Password, nt_identity->PasswordLength);
1143 AuthInfo->nt_identity->PasswordLength = nt_identity->PasswordLength;
1145 if ((nt_identity->User && !AuthInfo->nt_identity->User) ||
1146 (nt_identity->Domain && !AuthInfo->nt_identity->Domain) ||
1147 (nt_identity->Password && !AuthInfo->nt_identity->Password))
1149 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
1150 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1151 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1152 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
1153 HeapFree(GetProcessHeap(), 0, AuthInfo);
1154 return ERROR_OUTOFMEMORY;
1157 else
1158 AuthInfo->nt_identity = NULL;
1159 *ret = AuthInfo;
1160 return RPC_S_OK;
1163 ULONG RpcAuthInfo_AddRef(RpcAuthInfo *AuthInfo)
1165 return InterlockedIncrement(&AuthInfo->refs);
1168 ULONG RpcAuthInfo_Release(RpcAuthInfo *AuthInfo)
1170 ULONG refs = InterlockedDecrement(&AuthInfo->refs);
1172 if (!refs)
1174 FreeCredentialsHandle(&AuthInfo->cred);
1175 if (AuthInfo->nt_identity)
1177 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
1178 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1179 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1180 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
1182 HeapFree(GetProcessHeap(), 0, AuthInfo->server_principal_name);
1183 HeapFree(GetProcessHeap(), 0, AuthInfo);
1186 return refs;
1189 BOOL RpcAuthInfo_IsEqual(const RpcAuthInfo *AuthInfo1, const RpcAuthInfo *AuthInfo2)
1191 if (AuthInfo1 == AuthInfo2)
1192 return TRUE;
1194 if (!AuthInfo1 || !AuthInfo2)
1195 return FALSE;
1197 if ((AuthInfo1->AuthnLevel != AuthInfo2->AuthnLevel) ||
1198 (AuthInfo1->AuthnSvc != AuthInfo2->AuthnSvc))
1199 return FALSE;
1201 if (AuthInfo1->identity == AuthInfo2->identity)
1202 return TRUE;
1204 if (!AuthInfo1->identity || !AuthInfo2->identity)
1205 return FALSE;
1207 if (has_nt_auth_identity(AuthInfo1->AuthnSvc))
1209 const SEC_WINNT_AUTH_IDENTITY_W *identity1 = AuthInfo1->nt_identity;
1210 const SEC_WINNT_AUTH_IDENTITY_W *identity2 = AuthInfo2->nt_identity;
1211 /* compare user names */
1212 if (identity1->UserLength != identity2->UserLength ||
1213 memcmp(identity1->User, identity2->User, identity1->UserLength))
1214 return FALSE;
1215 /* compare domain names */
1216 if (identity1->DomainLength != identity2->DomainLength ||
1217 memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1218 return FALSE;
1219 /* compare passwords */
1220 if (identity1->PasswordLength != identity2->PasswordLength ||
1221 memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1222 return FALSE;
1224 else
1225 return FALSE;
1227 return TRUE;
1230 static RPC_STATUS RpcQualityOfService_Create(const RPC_SECURITY_QOS *qos_src, BOOL unicode, RpcQualityOfService **qos_dst)
1232 RpcQualityOfService *qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos));
1234 if (!qos)
1235 return RPC_S_OUT_OF_RESOURCES;
1237 qos->refs = 1;
1238 qos->qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos->qos));
1239 if (!qos->qos) goto error;
1240 qos->qos->Version = qos_src->Version;
1241 qos->qos->Capabilities = qos_src->Capabilities;
1242 qos->qos->IdentityTracking = qos_src->IdentityTracking;
1243 qos->qos->ImpersonationType = qos_src->ImpersonationType;
1244 qos->qos->AdditionalSecurityInfoType = 0;
1246 if (qos_src->Version >= 2)
1248 const RPC_SECURITY_QOS_V2_W *qos_src2 = (const RPC_SECURITY_QOS_V2_W *)qos_src;
1249 qos->qos->AdditionalSecurityInfoType = qos_src2->AdditionalSecurityInfoType;
1250 if (qos_src2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1252 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_src = qos_src2->u.HttpCredentials;
1253 RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_dst;
1255 http_credentials_dst = HeapAlloc(GetProcessHeap(), 0, sizeof(*http_credentials_dst));
1256 qos->qos->u.HttpCredentials = http_credentials_dst;
1257 if (!http_credentials_dst) goto error;
1258 http_credentials_dst->TransportCredentials = NULL;
1259 http_credentials_dst->Flags = http_credentials_src->Flags;
1260 http_credentials_dst->AuthenticationTarget = http_credentials_src->AuthenticationTarget;
1261 http_credentials_dst->NumberOfAuthnSchemes = http_credentials_src->NumberOfAuthnSchemes;
1262 http_credentials_dst->AuthnSchemes = NULL;
1263 http_credentials_dst->ServerCertificateSubject = NULL;
1264 if (http_credentials_src->TransportCredentials)
1266 SEC_WINNT_AUTH_IDENTITY_W *cred_dst;
1267 cred_dst = http_credentials_dst->TransportCredentials = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cred_dst));
1268 if (!cred_dst) goto error;
1269 cred_dst->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1270 if (unicode)
1272 const SEC_WINNT_AUTH_IDENTITY_W *cred_src = http_credentials_src->TransportCredentials;
1273 cred_dst->UserLength = cred_src->UserLength;
1274 cred_dst->PasswordLength = cred_src->PasswordLength;
1275 cred_dst->DomainLength = cred_src->DomainLength;
1276 cred_dst->User = RPCRT4_strndupW(cred_src->User, cred_src->UserLength);
1277 cred_dst->Password = RPCRT4_strndupW(cred_src->Password, cred_src->PasswordLength);
1278 cred_dst->Domain = RPCRT4_strndupW(cred_src->Domain, cred_src->DomainLength);
1280 else
1282 const SEC_WINNT_AUTH_IDENTITY_A *cred_src = (const SEC_WINNT_AUTH_IDENTITY_A *)http_credentials_src->TransportCredentials;
1283 cred_dst->UserLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, NULL, 0);
1284 cred_dst->DomainLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, NULL, 0);
1285 cred_dst->PasswordLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, NULL, 0);
1286 cred_dst->User = HeapAlloc(GetProcessHeap(), 0, cred_dst->UserLength * sizeof(WCHAR));
1287 cred_dst->Password = HeapAlloc(GetProcessHeap(), 0, cred_dst->PasswordLength * sizeof(WCHAR));
1288 cred_dst->Domain = HeapAlloc(GetProcessHeap(), 0, cred_dst->DomainLength * sizeof(WCHAR));
1289 if (!cred_dst || !cred_dst->Password || !cred_dst->Domain) goto error;
1290 MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, cred_dst->User, cred_dst->UserLength);
1291 MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, cred_dst->Domain, cred_dst->DomainLength);
1292 MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, cred_dst->Password, cred_dst->PasswordLength);
1295 if (http_credentials_src->NumberOfAuthnSchemes)
1297 http_credentials_dst->AuthnSchemes = HeapAlloc(GetProcessHeap(), 0, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1298 if (!http_credentials_dst->AuthnSchemes) goto error;
1299 memcpy(http_credentials_dst->AuthnSchemes, http_credentials_src->AuthnSchemes, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1301 if (http_credentials_src->ServerCertificateSubject)
1303 if (unicode)
1304 http_credentials_dst->ServerCertificateSubject =
1305 RPCRT4_strndupW(http_credentials_src->ServerCertificateSubject,
1306 strlenW(http_credentials_src->ServerCertificateSubject));
1307 else
1308 http_credentials_dst->ServerCertificateSubject =
1309 RPCRT4_strdupAtoW((char *)http_credentials_src->ServerCertificateSubject);
1310 if (!http_credentials_dst->ServerCertificateSubject) goto error;
1314 *qos_dst = qos;
1315 return RPC_S_OK;
1317 error:
1318 if (qos->qos)
1320 if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP &&
1321 qos->qos->u.HttpCredentials)
1323 if (qos->qos->u.HttpCredentials->TransportCredentials)
1325 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1326 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1327 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1328 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1330 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1331 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1332 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1334 HeapFree(GetProcessHeap(), 0, qos->qos);
1336 HeapFree(GetProcessHeap(), 0, qos);
1337 return RPC_S_OUT_OF_RESOURCES;
1340 ULONG RpcQualityOfService_AddRef(RpcQualityOfService *qos)
1342 return InterlockedIncrement(&qos->refs);
1345 ULONG RpcQualityOfService_Release(RpcQualityOfService *qos)
1347 ULONG refs = InterlockedDecrement(&qos->refs);
1349 if (!refs)
1351 if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1353 if (qos->qos->u.HttpCredentials->TransportCredentials)
1355 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1356 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1357 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1358 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1360 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1361 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1362 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1364 HeapFree(GetProcessHeap(), 0, qos->qos);
1365 HeapFree(GetProcessHeap(), 0, qos);
1367 return refs;
1370 BOOL RpcQualityOfService_IsEqual(const RpcQualityOfService *qos1, const RpcQualityOfService *qos2)
1372 if (qos1 == qos2)
1373 return TRUE;
1375 if (!qos1 || !qos2)
1376 return FALSE;
1378 TRACE("qos1 = { %d %d %d %d }, qos2 = { %d %d %d %d }\n",
1379 qos1->qos->Capabilities, qos1->qos->IdentityTracking,
1380 qos1->qos->ImpersonationType, qos1->qos->AdditionalSecurityInfoType,
1381 qos2->qos->Capabilities, qos2->qos->IdentityTracking,
1382 qos2->qos->ImpersonationType, qos2->qos->AdditionalSecurityInfoType);
1384 if ((qos1->qos->Capabilities != qos2->qos->Capabilities) ||
1385 (qos1->qos->IdentityTracking != qos2->qos->IdentityTracking) ||
1386 (qos1->qos->ImpersonationType != qos2->qos->ImpersonationType) ||
1387 (qos1->qos->AdditionalSecurityInfoType != qos2->qos->AdditionalSecurityInfoType))
1388 return FALSE;
1390 if (qos1->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1392 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials1 = qos1->qos->u.HttpCredentials;
1393 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials2 = qos2->qos->u.HttpCredentials;
1395 if (http_credentials1->Flags != http_credentials2->Flags)
1396 return FALSE;
1398 if (http_credentials1->AuthenticationTarget != http_credentials2->AuthenticationTarget)
1399 return FALSE;
1401 /* authentication schemes and server certificate subject not currently used */
1403 if (http_credentials1->TransportCredentials != http_credentials2->TransportCredentials)
1405 const SEC_WINNT_AUTH_IDENTITY_W *identity1 = http_credentials1->TransportCredentials;
1406 const SEC_WINNT_AUTH_IDENTITY_W *identity2 = http_credentials2->TransportCredentials;
1408 if (!identity1 || !identity2)
1409 return FALSE;
1411 /* compare user names */
1412 if (identity1->UserLength != identity2->UserLength ||
1413 memcmp(identity1->User, identity2->User, identity1->UserLength))
1414 return FALSE;
1415 /* compare domain names */
1416 if (identity1->DomainLength != identity2->DomainLength ||
1417 memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1418 return FALSE;
1419 /* compare passwords */
1420 if (identity1->PasswordLength != identity2->PasswordLength ||
1421 memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1422 return FALSE;
1426 return TRUE;
1429 /***********************************************************************
1430 * RpcRevertToSelf (RPCRT4.@)
1432 RPC_STATUS WINAPI RpcRevertToSelf(void)
1434 FIXME("stub\n");
1435 RevertToSelf();
1436 return RPC_S_OK;
1439 /***********************************************************************
1440 * RpcMgmtSetComTimeout (RPCRT4.@)
1442 RPC_STATUS WINAPI RpcMgmtSetComTimeout(RPC_BINDING_HANDLE BindingHandle, unsigned int Timeout)
1444 FIXME("(%p, %d): stub\n", BindingHandle, Timeout);
1445 return RPC_S_OK;
1448 /***********************************************************************
1449 * RpcBindingInqAuthInfoExA (RPCRT4.@)
1451 RPCRTAPI RPC_STATUS RPC_ENTRY
1452 RpcBindingInqAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1453 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1454 ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1456 RPC_STATUS status;
1457 RPC_WSTR principal;
1459 TRACE("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1460 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1462 status = RpcBindingInqAuthInfoExW(Binding, ServerPrincName ? &principal : NULL, AuthnLevel,
1463 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1464 if (status == RPC_S_OK && ServerPrincName)
1466 *ServerPrincName = (RPC_CSTR)RPCRT4_strdupWtoA(principal);
1467 RpcStringFreeW(&principal);
1468 if (!*ServerPrincName) return ERROR_OUTOFMEMORY;
1471 return status;
1474 /***********************************************************************
1475 * RpcBindingInqAuthInfoExW (RPCRT4.@)
1477 RPCRTAPI RPC_STATUS RPC_ENTRY
1478 RpcBindingInqAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1479 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1480 ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1482 RpcBinding *bind = Binding;
1484 TRACE("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1485 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1487 if (!bind->AuthInfo) return RPC_S_BINDING_HAS_NO_AUTH;
1489 if (SecurityQOS)
1491 FIXME("QOS not implemented\n");
1492 return RPC_S_INVALID_BINDING;
1495 if (ServerPrincName)
1497 if (bind->AuthInfo->server_principal_name)
1499 *ServerPrincName = RPCRT4_strdupW(bind->AuthInfo->server_principal_name);
1500 if (!*ServerPrincName) return ERROR_OUTOFMEMORY;
1502 else *ServerPrincName = NULL;
1504 if (AuthnLevel) *AuthnLevel = bind->AuthInfo->AuthnLevel;
1505 if (AuthnSvc) *AuthnSvc = bind->AuthInfo->AuthnSvc;
1506 if (AuthIdentity) *AuthIdentity = bind->AuthInfo->identity;
1507 if (AuthzSvc)
1509 FIXME("authorization service not implemented\n");
1510 *AuthzSvc = RPC_C_AUTHZ_NONE;
1513 return RPC_S_OK;
1516 /***********************************************************************
1517 * RpcBindingInqAuthInfoA (RPCRT4.@)
1519 RPCRTAPI RPC_STATUS RPC_ENTRY
1520 RpcBindingInqAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1521 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1523 return RpcBindingInqAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity,
1524 AuthzSvc, 0, NULL);
1527 /***********************************************************************
1528 * RpcBindingInqAuthInfoW (RPCRT4.@)
1530 RPCRTAPI RPC_STATUS RPC_ENTRY
1531 RpcBindingInqAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1532 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1534 return RpcBindingInqAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity,
1535 AuthzSvc, 0, NULL);
1538 /***********************************************************************
1539 * RpcBindingInqAuthClientA (RPCRT4.@)
1541 RPCRTAPI RPC_STATUS RPC_ENTRY
1542 RpcBindingInqAuthClientA( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1543 RPC_CSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1544 ULONG *AuthzSvc )
1546 return RpcBindingInqAuthClientExA(ClientBinding, Privs, ServerPrincName, AuthnLevel,
1547 AuthnSvc, AuthzSvc, 0);
1550 /***********************************************************************
1551 * RpcBindingInqAuthClientW (RPCRT4.@)
1553 RPCRTAPI RPC_STATUS RPC_ENTRY
1554 RpcBindingInqAuthClientW( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1555 RPC_WSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1556 ULONG *AuthzSvc )
1558 return RpcBindingInqAuthClientExW(ClientBinding, Privs, ServerPrincName, AuthnLevel,
1559 AuthnSvc, AuthzSvc, 0);
1562 /***********************************************************************
1563 * RpcBindingInqAuthClientExA (RPCRT4.@)
1565 RPCRTAPI RPC_STATUS RPC_ENTRY
1566 RpcBindingInqAuthClientExA( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1567 RPC_CSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1568 ULONG *AuthzSvc, ULONG Flags )
1570 RPC_STATUS status;
1571 RPC_WSTR principal;
1573 TRACE("%p %p %p %p %p %p 0x%x\n", ClientBinding, Privs, ServerPrincName, AuthnLevel,
1574 AuthnSvc, AuthzSvc, Flags);
1576 status = RpcBindingInqAuthClientExW(ClientBinding, Privs, ServerPrincName ? &principal : NULL,
1577 AuthnLevel, AuthnSvc, AuthzSvc, Flags);
1578 if (status == RPC_S_OK && ServerPrincName)
1580 *ServerPrincName = (RPC_CSTR)RPCRT4_strdupWtoA(principal);
1581 RpcStringFreeW(&principal);
1582 if (!*ServerPrincName) return ERROR_OUTOFMEMORY;
1585 return status;
1588 /***********************************************************************
1589 * RpcBindingInqAuthClientExW (RPCRT4.@)
1591 RPCRTAPI RPC_STATUS RPC_ENTRY
1592 RpcBindingInqAuthClientExW( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1593 RPC_WSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1594 ULONG *AuthzSvc, ULONG Flags )
1596 RpcBinding *bind = ClientBinding;
1598 TRACE("%p %p %p %p %p %p 0x%x\n", ClientBinding, Privs, ServerPrincName, AuthnLevel,
1599 AuthnSvc, AuthzSvc, Flags);
1601 if (!bind->AuthInfo) return RPC_S_BINDING_HAS_NO_AUTH;
1603 if (Privs) *Privs = (RPC_AUTHZ_HANDLE)bind->AuthInfo->identity;
1604 if (ServerPrincName)
1606 *ServerPrincName = RPCRT4_strdupW(bind->AuthInfo->server_principal_name);
1607 if (!*ServerPrincName) return ERROR_OUTOFMEMORY;
1609 if (AuthnLevel) *AuthnLevel = bind->AuthInfo->AuthnLevel;
1610 if (AuthnSvc) *AuthnSvc = bind->AuthInfo->AuthnSvc;
1611 if (AuthzSvc)
1613 FIXME("authorization service not implemented\n");
1614 *AuthzSvc = RPC_C_AUTHZ_NONE;
1616 if (Flags)
1617 FIXME("flags 0x%x not implemented\n", Flags);
1619 return RPC_S_OK;
1622 /***********************************************************************
1623 * RpcBindingSetAuthInfoExA (RPCRT4.@)
1625 RPCRTAPI RPC_STATUS RPC_ENTRY
1626 RpcBindingSetAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName,
1627 ULONG AuthnLevel, ULONG AuthnSvc,
1628 RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1629 RPC_SECURITY_QOS *SecurityQos )
1631 RpcBinding* bind = Binding;
1632 SECURITY_STATUS r;
1633 CredHandle cred;
1634 TimeStamp exp;
1635 ULONG package_count;
1636 ULONG i;
1637 PSecPkgInfoA packages;
1638 ULONG cbMaxToken;
1640 TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_a((const char*)ServerPrincName),
1641 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1643 if (SecurityQos)
1645 RPC_STATUS status;
1647 TRACE("SecurityQos { Version=%d, Capabilities=0x%x, IdentityTracking=%d, ImpersonationLevel=%d",
1648 SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1649 if (SecurityQos->Version >= 2)
1651 const RPC_SECURITY_QOS_V2_A *SecurityQos2 = (const RPC_SECURITY_QOS_V2_A *)SecurityQos;
1652 TRACE(", AdditionalSecurityInfoType=%d", SecurityQos2->AdditionalSecurityInfoType);
1653 if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1654 TRACE(", { %p, 0x%x, %d, %d, %p, %s }",
1655 SecurityQos2->u.HttpCredentials->TransportCredentials,
1656 SecurityQos2->u.HttpCredentials->Flags,
1657 SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1658 SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1659 SecurityQos2->u.HttpCredentials->AuthnSchemes,
1660 SecurityQos2->u.HttpCredentials->ServerCertificateSubject);
1662 TRACE("}\n");
1663 status = RpcQualityOfService_Create(SecurityQos, FALSE, &bind->QOS);
1664 if (status != RPC_S_OK)
1665 return status;
1667 else
1669 if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1670 bind->QOS = NULL;
1673 if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1674 AuthnSvc = RPC_C_AUTHN_WINNT;
1676 /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1677 if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1678 AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1680 if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1682 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1683 bind->AuthInfo = NULL;
1684 return RPC_S_OK;
1687 if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1689 FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1690 return RPC_S_UNKNOWN_AUTHN_LEVEL;
1693 /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1694 if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1696 FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1697 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1700 r = EnumerateSecurityPackagesA(&package_count, &packages);
1701 if (r != SEC_E_OK)
1703 ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n", r);
1704 return RPC_S_SEC_PKG_ERROR;
1707 for (i = 0; i < package_count; i++)
1708 if (packages[i].wRPCID == AuthnSvc)
1709 break;
1711 if (i == package_count)
1713 FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1714 FreeContextBuffer(packages);
1715 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1718 TRACE("found package %s for service %u\n", packages[i].Name, AuthnSvc);
1719 r = AcquireCredentialsHandleA(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1720 AuthIdentity, NULL, NULL, &cred, &exp);
1721 cbMaxToken = packages[i].cbMaxToken;
1722 FreeContextBuffer(packages);
1723 if (r == ERROR_SUCCESS)
1725 RpcAuthInfo *new_auth_info;
1726 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1727 AuthIdentity, &new_auth_info);
1728 if (r == RPC_S_OK)
1730 new_auth_info->server_principal_name = RPCRT4_strdupAtoW((char *)ServerPrincName);
1731 if (new_auth_info->server_principal_name)
1733 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1734 bind->AuthInfo = new_auth_info;
1736 else
1738 RpcAuthInfo_Release(new_auth_info);
1739 r = ERROR_OUTOFMEMORY;
1742 else
1743 FreeCredentialsHandle(&cred);
1744 return r;
1746 else
1748 ERR("AcquireCredentialsHandleA failed with error 0x%08x\n", r);
1749 return RPC_S_SEC_PKG_ERROR;
1753 /***********************************************************************
1754 * RpcBindingSetAuthInfoExW (RPCRT4.@)
1756 RPCRTAPI RPC_STATUS RPC_ENTRY
1757 RpcBindingSetAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1758 ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1759 RPC_SECURITY_QOS *SecurityQos )
1761 RpcBinding* bind = Binding;
1762 SECURITY_STATUS r;
1763 CredHandle cred;
1764 TimeStamp exp;
1765 ULONG package_count;
1766 ULONG i;
1767 PSecPkgInfoW packages;
1768 ULONG cbMaxToken;
1770 TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_w(ServerPrincName),
1771 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1773 if (SecurityQos)
1775 RPC_STATUS status;
1777 TRACE("SecurityQos { Version=%d, Capabilities=0x%x, IdentityTracking=%d, ImpersonationLevel=%d",
1778 SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1779 if (SecurityQos->Version >= 2)
1781 const RPC_SECURITY_QOS_V2_W *SecurityQos2 = (const RPC_SECURITY_QOS_V2_W *)SecurityQos;
1782 TRACE(", AdditionalSecurityInfoType=%d", SecurityQos2->AdditionalSecurityInfoType);
1783 if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1784 TRACE(", { %p, 0x%x, %d, %d, %p, %s }",
1785 SecurityQos2->u.HttpCredentials->TransportCredentials,
1786 SecurityQos2->u.HttpCredentials->Flags,
1787 SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1788 SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1789 SecurityQos2->u.HttpCredentials->AuthnSchemes,
1790 debugstr_w(SecurityQos2->u.HttpCredentials->ServerCertificateSubject));
1792 TRACE("}\n");
1793 status = RpcQualityOfService_Create(SecurityQos, TRUE, &bind->QOS);
1794 if (status != RPC_S_OK)
1795 return status;
1797 else
1799 if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1800 bind->QOS = NULL;
1803 if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1804 AuthnSvc = RPC_C_AUTHN_WINNT;
1806 /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1807 if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1808 AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1810 if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1812 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1813 bind->AuthInfo = NULL;
1814 return RPC_S_OK;
1817 if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1819 FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1820 return RPC_S_UNKNOWN_AUTHN_LEVEL;
1823 /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1824 if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1826 FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1827 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1830 r = EnumerateSecurityPackagesW(&package_count, &packages);
1831 if (r != SEC_E_OK)
1833 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", r);
1834 return RPC_S_SEC_PKG_ERROR;
1837 for (i = 0; i < package_count; i++)
1838 if (packages[i].wRPCID == AuthnSvc)
1839 break;
1841 if (i == package_count)
1843 FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1844 FreeContextBuffer(packages);
1845 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1848 TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name), AuthnSvc);
1849 r = AcquireCredentialsHandleW(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1850 AuthIdentity, NULL, NULL, &cred, &exp);
1851 cbMaxToken = packages[i].cbMaxToken;
1852 FreeContextBuffer(packages);
1853 if (r == ERROR_SUCCESS)
1855 RpcAuthInfo *new_auth_info;
1856 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1857 AuthIdentity, &new_auth_info);
1858 if (r == RPC_S_OK)
1860 new_auth_info->server_principal_name = RPCRT4_strdupW(ServerPrincName);
1861 if (!ServerPrincName || new_auth_info->server_principal_name)
1863 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1864 bind->AuthInfo = new_auth_info;
1866 else
1868 RpcAuthInfo_Release(new_auth_info);
1869 r = ERROR_OUTOFMEMORY;
1872 else
1873 FreeCredentialsHandle(&cred);
1874 return r;
1876 else
1878 ERR("AcquireCredentialsHandleW failed with error 0x%08x\n", r);
1879 return RPC_S_SEC_PKG_ERROR;
1883 /***********************************************************************
1884 * RpcBindingSetAuthInfoA (RPCRT4.@)
1886 RPCRTAPI RPC_STATUS RPC_ENTRY
1887 RpcBindingSetAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName, ULONG AuthnLevel,
1888 ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1890 TRACE("%p %s %u %u %p %u\n", Binding, debugstr_a((const char*)ServerPrincName),
1891 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1892 return RpcBindingSetAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1895 /***********************************************************************
1896 * RpcBindingSetAuthInfoW (RPCRT4.@)
1898 RPCRTAPI RPC_STATUS RPC_ENTRY
1899 RpcBindingSetAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1900 ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1902 TRACE("%p %s %u %u %p %u\n", Binding, debugstr_w(ServerPrincName),
1903 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1904 return RpcBindingSetAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1907 /***********************************************************************
1908 * RpcBindingSetOption (RPCRT4.@)
1910 RPC_STATUS WINAPI RpcBindingSetOption(RPC_BINDING_HANDLE BindingHandle, ULONG Option, ULONG_PTR OptionValue)
1912 FIXME("(%p, %d, %ld): stub\n", BindingHandle, Option, OptionValue);
1913 return RPC_S_OK;