[SHELL32] CDrivesFolder: Implement the eject and disconnect menu items. CORE-13841
[reactos.git] / dll / win32 / rpcrt4 / rpc_binding.c
1 /*
2 * RPC binding API
3 *
4 * Copyright 2001 Ove Kåven, TransGaming Technologies
5 * Copyright 2003 Mike Hearn
6 * Copyright 2004 Filip Navara
7 * Copyright 2006 CodeWeavers
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "precomp.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
27
28 LPSTR RPCRT4_strndupA(LPCSTR src, INT slen)
29 {
30 DWORD len;
31 LPSTR s;
32 if (!src) return NULL;
33 if (slen == -1) slen = strlen(src);
34 len = slen;
35 s = HeapAlloc(GetProcessHeap(), 0, len+1);
36 memcpy(s, src, len);
37 s[len] = 0;
38 return s;
39 }
40
41 LPSTR RPCRT4_strdupWtoA(LPCWSTR src)
42 {
43 DWORD len;
44 LPSTR s;
45 if (!src) return NULL;
46 len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
47 s = HeapAlloc(GetProcessHeap(), 0, len);
48 WideCharToMultiByte(CP_ACP, 0, src, -1, s, len, NULL, NULL);
49 return s;
50 }
51
52 LPWSTR RPCRT4_strdupAtoW(LPCSTR src)
53 {
54 DWORD len;
55 LPWSTR s;
56 if (!src) return NULL;
57 len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
58 s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
59 MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
60 return s;
61 }
62
63 static LPWSTR RPCRT4_strndupAtoW(LPCSTR src, INT slen)
64 {
65 DWORD len;
66 LPWSTR s;
67 if (!src) return NULL;
68 len = MultiByteToWideChar(CP_ACP, 0, src, slen, NULL, 0);
69 s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
70 MultiByteToWideChar(CP_ACP, 0, src, slen, s, len);
71 return s;
72 }
73
74 LPWSTR RPCRT4_strndupW(LPCWSTR src, INT slen)
75 {
76 DWORD len;
77 LPWSTR s;
78 if (!src) return NULL;
79 if (slen == -1) slen = strlenW(src);
80 len = slen;
81 s = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
82 memcpy(s, src, len*sizeof(WCHAR));
83 s[len] = 0;
84 return s;
85 }
86
87 void RPCRT4_strfree(LPSTR src)
88 {
89 HeapFree(GetProcessHeap(), 0, src);
90 }
91
92 static RPC_STATUS RPCRT4_AllocBinding(RpcBinding** Binding, BOOL server)
93 {
94 RpcBinding* NewBinding;
95
96 NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
97 NewBinding->refs = 1;
98 NewBinding->server = server;
99
100 *Binding = NewBinding;
101
102 return RPC_S_OK;
103 }
104
105 static RPC_STATUS RPCRT4_CreateBindingA(RpcBinding** Binding, BOOL server, LPCSTR Protseq)
106 {
107 RpcBinding* NewBinding;
108
109 RPCRT4_AllocBinding(&NewBinding, server);
110 NewBinding->Protseq = RPCRT4_strdupA(Protseq);
111
112 TRACE("binding: %p\n", NewBinding);
113 *Binding = NewBinding;
114
115 return RPC_S_OK;
116 }
117
118 static RPC_STATUS RPCRT4_CreateBindingW(RpcBinding** Binding, BOOL server, LPCWSTR Protseq)
119 {
120 RpcBinding* NewBinding;
121
122 RPCRT4_AllocBinding(&NewBinding, server);
123 NewBinding->Protseq = RPCRT4_strdupWtoA(Protseq);
124
125 TRACE("binding: %p\n", NewBinding);
126 *Binding = NewBinding;
127
128 return RPC_S_OK;
129 }
130
131 static RPC_STATUS RPCRT4_CompleteBindingA(RpcBinding* Binding, LPCSTR NetworkAddr,
132 LPCSTR Endpoint, LPCSTR NetworkOptions)
133 {
134 RPC_STATUS status;
135
136 TRACE("(RpcBinding == ^%p, NetworkAddr == %s, EndPoint == %s, NetworkOptions == %s)\n", Binding,
137 debugstr_a(NetworkAddr), debugstr_a(Endpoint), debugstr_a(NetworkOptions));
138
139 RPCRT4_strfree(Binding->NetworkAddr);
140 Binding->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
141 RPCRT4_strfree(Binding->Endpoint);
142 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
143 HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
144 Binding->NetworkOptions = RPCRT4_strdupAtoW(NetworkOptions);
145
146 /* only attempt to get an association if the binding is complete */
147 if (Endpoint && Endpoint[0] != '\0')
148 {
149 status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
150 Binding->Endpoint, Binding->NetworkOptions,
151 &Binding->Assoc);
152 if (status != RPC_S_OK)
153 return status;
154 }
155
156 return RPC_S_OK;
157 }
158
159 static RPC_STATUS RPCRT4_CompleteBindingW(RpcBinding* Binding, LPCWSTR NetworkAddr,
160 LPCWSTR Endpoint, LPCWSTR NetworkOptions)
161 {
162 RPC_STATUS status;
163
164 TRACE("(RpcBinding == ^%p, NetworkAddr == %s, EndPoint == %s, NetworkOptions == %s)\n", Binding,
165 debugstr_w(NetworkAddr), debugstr_w(Endpoint), debugstr_w(NetworkOptions));
166
167 RPCRT4_strfree(Binding->NetworkAddr);
168 Binding->NetworkAddr = RPCRT4_strdupWtoA(NetworkAddr);
169 RPCRT4_strfree(Binding->Endpoint);
170 Binding->Endpoint = RPCRT4_strdupWtoA(Endpoint);
171 HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
172 Binding->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
173
174 /* only attempt to get an association if the binding is complete */
175 if (Endpoint && Endpoint[0] != '\0')
176 {
177 status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
178 Binding->Endpoint, Binding->NetworkOptions,
179 &Binding->Assoc);
180 if (status != RPC_S_OK)
181 return status;
182 }
183
184 return RPC_S_OK;
185 }
186
187 RPC_STATUS RPCRT4_ResolveBinding(RpcBinding* Binding, LPCSTR Endpoint)
188 {
189 RPC_STATUS status;
190
191 TRACE("(RpcBinding == ^%p, EndPoint == \"%s\"\n", Binding, Endpoint);
192
193 RPCRT4_strfree(Binding->Endpoint);
194 Binding->Endpoint = RPCRT4_strdupA(Endpoint);
195
196 if (Binding->Assoc) RpcAssoc_Release(Binding->Assoc);
197 Binding->Assoc = NULL;
198 status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
199 Binding->Endpoint, Binding->NetworkOptions,
200 &Binding->Assoc);
201 if (status != RPC_S_OK)
202 return status;
203
204 return RPC_S_OK;
205 }
206
207 RPC_STATUS RPCRT4_SetBindingObject(RpcBinding* Binding, const UUID* ObjectUuid)
208 {
209 TRACE("(*RpcBinding == ^%p, UUID == %s)\n", Binding, debugstr_guid(ObjectUuid));
210 if (ObjectUuid) Binding->ObjectUuid = *ObjectUuid;
211 else UuidCreateNil(&Binding->ObjectUuid);
212 return RPC_S_OK;
213 }
214
215 RPC_STATUS RPCRT4_MakeBinding(RpcBinding** Binding, RpcConnection* Connection)
216 {
217 RpcBinding* NewBinding;
218 TRACE("(RpcBinding == ^%p, Connection == ^%p)\n", Binding, Connection);
219
220 RPCRT4_AllocBinding(&NewBinding, Connection->server);
221 NewBinding->Protseq = RPCRT4_strdupA(rpcrt4_conn_get_name(Connection));
222 NewBinding->NetworkAddr = RPCRT4_strdupA(Connection->NetworkAddr);
223 NewBinding->Endpoint = RPCRT4_strdupA(Connection->Endpoint);
224 NewBinding->FromConn = Connection;
225
226 TRACE("binding: %p\n", NewBinding);
227 *Binding = NewBinding;
228
229 return RPC_S_OK;
230 }
231
232 void RPCRT4_AddRefBinding(RpcBinding* Binding)
233 {
234 InterlockedIncrement(&Binding->refs);
235 }
236
237 RPC_STATUS RPCRT4_ReleaseBinding(RpcBinding* Binding)
238 {
239 if (InterlockedDecrement(&Binding->refs))
240 return RPC_S_OK;
241
242 TRACE("binding: %p\n", Binding);
243 if (Binding->Assoc) RpcAssoc_Release(Binding->Assoc);
244 RPCRT4_strfree(Binding->Endpoint);
245 RPCRT4_strfree(Binding->NetworkAddr);
246 RPCRT4_strfree(Binding->Protseq);
247 HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
248 HeapFree(GetProcessHeap(), 0, Binding->CookieAuth);
249 if (Binding->AuthInfo) RpcAuthInfo_Release(Binding->AuthInfo);
250 if (Binding->QOS) RpcQualityOfService_Release(Binding->QOS);
251 HeapFree(GetProcessHeap(), 0, Binding);
252 return RPC_S_OK;
253 }
254
255 RPC_STATUS RPCRT4_OpenBinding(RpcBinding* Binding, RpcConnection** Connection,
256 const RPC_SYNTAX_IDENTIFIER *TransferSyntax,
257 const RPC_SYNTAX_IDENTIFIER *InterfaceId)
258 {
259 TRACE("(Binding == ^%p)\n", Binding);
260
261 if (!Binding->server) {
262 return RpcAssoc_GetClientConnection(Binding->Assoc, InterfaceId,
263 TransferSyntax, Binding->AuthInfo, Binding->QOS, Binding->CookieAuth, Connection);
264 } else {
265 /* we already have a connection with acceptable binding, so use it */
266 if (Binding->FromConn) {
267 *Connection = Binding->FromConn;
268 return RPC_S_OK;
269 } else {
270 ERR("no connection in binding\n");
271 return RPC_S_INTERNAL_ERROR;
272 }
273 }
274 }
275
276 RPC_STATUS RPCRT4_CloseBinding(RpcBinding* Binding, RpcConnection* Connection)
277 {
278 TRACE("(Binding == ^%p)\n", Binding);
279 if (!Connection) return RPC_S_OK;
280 if (Binding->server) {
281 /* don't destroy a connection that is cached in the binding */
282 if (Binding->FromConn != Connection)
283 RPCRT4_ReleaseConnection(Connection);
284 }
285 else {
286 RpcAssoc_ReleaseIdleConnection(Binding->Assoc, Connection);
287 }
288 return RPC_S_OK;
289 }
290
291 static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
292 {
293 DWORD len = strlen(dst), slen = strlen(src);
294 LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
295 if (!ndst)
296 {
297 HeapFree(GetProcessHeap(), 0, dst);
298 return NULL;
299 }
300 ndst[len] = ',';
301 memcpy(ndst+len+1, src, slen+1);
302 return ndst;
303 }
304
305 static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
306 {
307 DWORD len = strlenW(dst), slen = strlenW(src);
308 LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
309 if (!ndst)
310 {
311 HeapFree(GetProcessHeap(), 0, dst);
312 return NULL;
313 }
314 ndst[len] = ',';
315 memcpy(ndst+len+1, src, (slen+1)*sizeof(WCHAR));
316 return ndst;
317 }
318
319 /* Copies the escaped version of a component into a string binding.
320 * Note: doesn't nul-terminate the string */
321 static RPC_CSTR escape_string_binding_component(RPC_CSTR string_binding,
322 const unsigned char *component)
323 {
324 for (; *component; component++) {
325 switch (*component) {
326 case '@':
327 case ':':
328 case '[':
329 case ']':
330 case '\\':
331 *string_binding++ = '\\';
332 *string_binding++ = *component;
333 break;
334 default:
335 *string_binding++ = *component;
336 break;
337 }
338 }
339 return string_binding;
340 }
341
342 static RPC_WSTR escape_string_binding_componentW(RPC_WSTR string_binding,
343 const WCHAR *component)
344 {
345 for (; *component; component++) {
346 switch (*component) {
347 case '@':
348 case ':':
349 case '[':
350 case ']':
351 case '\\':
352 *string_binding++ = '\\';
353 *string_binding++ = *component;
354 break;
355 default:
356 *string_binding++ = *component;
357 break;
358 }
359 }
360 return string_binding;
361 }
362
363 static const unsigned char *string_binding_find_delimiter(
364 const unsigned char *string_binding, unsigned char delim)
365 {
366 const unsigned char *next;
367 for (next = string_binding; *next; next++) {
368 if (*next == '\\') {
369 next++;
370 continue;
371 }
372 if (*next == delim)
373 return next;
374 }
375 return NULL;
376 }
377
378 static const WCHAR *string_binding_find_delimiterW(
379 const WCHAR *string_binding, WCHAR delim)
380 {
381 const WCHAR *next;
382 for (next = string_binding; *next; next++) {
383 if (*next == '\\') {
384 next++;
385 continue;
386 }
387 if (*next == delim)
388 return next;
389 }
390 return NULL;
391 }
392
393 static RPC_CSTR unescape_string_binding_component(
394 const unsigned char *string_binding, int len)
395 {
396 RPC_CSTR component, p;
397
398 if (len == -1) len = strlen((const char *)string_binding);
399
400 component = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(*component));
401 if (!component) return NULL;
402 for (p = component; len > 0; string_binding++, len--) {
403 if (*string_binding == '\\') {
404 string_binding++;
405 len--;
406 *p++ = *string_binding;
407 } else {
408 *p++ = *string_binding;
409 }
410 }
411 *p = '\0';
412 return component;
413 }
414
415 static RPC_WSTR unescape_string_binding_componentW(
416 const WCHAR *string_binding, int len)
417 {
418 RPC_WSTR component, p;
419
420 if (len == -1) len = strlenW(string_binding);
421
422 component = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(*component));
423 if (!component) return NULL;
424 for (p = component; len > 0; string_binding++, len--) {
425 if (*string_binding == '\\') {
426 string_binding++;
427 len--;
428 *p++ = *string_binding;
429 } else {
430 *p++ = *string_binding;
431 }
432 }
433 *p = '\0';
434 return component;
435 }
436
437 /***********************************************************************
438 * RpcStringBindingComposeA (RPCRT4.@)
439 */
440 RPC_STATUS WINAPI RpcStringBindingComposeA(RPC_CSTR ObjUuid, RPC_CSTR Protseq,
441 RPC_CSTR NetworkAddr, RPC_CSTR Endpoint,
442 RPC_CSTR Options, RPC_CSTR *StringBinding )
443 {
444 DWORD len = 1;
445 RPC_CSTR data;
446
447 TRACE( "(%s,%s,%s,%s,%s,%p)\n",
448 debugstr_a( (char*)ObjUuid ), debugstr_a( (char*)Protseq ),
449 debugstr_a( (char*)NetworkAddr ), debugstr_a( (char*)Endpoint ),
450 debugstr_a( (char*)Options ), StringBinding );
451
452 /* overestimate for each component for escaping of delimiters */
453 if (ObjUuid && *ObjUuid) len += strlen((char*)ObjUuid) * 2 + 1;
454 if (Protseq && *Protseq) len += strlen((char*)Protseq) * 2 + 1;
455 if (NetworkAddr && *NetworkAddr) len += strlen((char*)NetworkAddr) * 2;
456 if (Endpoint && *Endpoint) len += strlen((char*)Endpoint) * 2 + 2;
457 if (Options && *Options) len += strlen((char*)Options) * 2 + 2;
458
459 data = HeapAlloc(GetProcessHeap(), 0, len);
460 *StringBinding = data;
461
462 if (ObjUuid && *ObjUuid) {
463 data = escape_string_binding_component(data, ObjUuid);
464 *data++ = '@';
465 }
466 if (Protseq && *Protseq) {
467 data = escape_string_binding_component(data, Protseq);
468 *data++ = ':';
469 }
470 if (NetworkAddr && *NetworkAddr)
471 data = escape_string_binding_component(data, NetworkAddr);
472
473 if ((Endpoint && *Endpoint) ||
474 (Options && *Options)) {
475 *data++ = '[';
476 if (Endpoint && *Endpoint) {
477 data = escape_string_binding_component(data, Endpoint);
478 if (Options && *Options) *data++ = ',';
479 }
480 if (Options && *Options) {
481 data = escape_string_binding_component(data, Options);
482 }
483 *data++ = ']';
484 }
485 *data = 0;
486
487 return RPC_S_OK;
488 }
489
490 /***********************************************************************
491 * RpcStringBindingComposeW (RPCRT4.@)
492 */
493 RPC_STATUS WINAPI RpcStringBindingComposeW( RPC_WSTR ObjUuid, RPC_WSTR Protseq,
494 RPC_WSTR NetworkAddr, RPC_WSTR Endpoint,
495 RPC_WSTR Options, RPC_WSTR* StringBinding )
496 {
497 DWORD len = 1;
498 RPC_WSTR data;
499
500 TRACE("(%s,%s,%s,%s,%s,%p)\n",
501 debugstr_w( ObjUuid ), debugstr_w( Protseq ),
502 debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
503 debugstr_w( Options ), StringBinding);
504
505 /* overestimate for each component for escaping of delimiters */
506 if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) * 2 + 1;
507 if (Protseq && *Protseq) len += strlenW(Protseq) * 2 + 1;
508 if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr) * 2;
509 if (Endpoint && *Endpoint) len += strlenW(Endpoint) * 2 + 2;
510 if (Options && *Options) len += strlenW(Options) * 2 + 2;
511
512 data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
513 *StringBinding = data;
514
515 if (ObjUuid && *ObjUuid) {
516 data = escape_string_binding_componentW(data, ObjUuid);
517 *data++ = '@';
518 }
519 if (Protseq && *Protseq) {
520 data = escape_string_binding_componentW(data, Protseq);
521 *data++ = ':';
522 }
523 if (NetworkAddr && *NetworkAddr) {
524 data = escape_string_binding_componentW(data, NetworkAddr);
525 }
526 if ((Endpoint && *Endpoint) ||
527 (Options && *Options)) {
528 *data++ = '[';
529 if (Endpoint && *Endpoint) {
530 data = escape_string_binding_componentW(data, Endpoint);
531 if (Options && *Options) *data++ = ',';
532 }
533 if (Options && *Options) {
534 data = escape_string_binding_componentW(data, Options);
535 }
536 *data++ = ']';
537 }
538 *data = 0;
539
540 return RPC_S_OK;
541 }
542
543
544 /***********************************************************************
545 * RpcStringBindingParseA (RPCRT4.@)
546 */
547 RPC_STATUS WINAPI RpcStringBindingParseA( RPC_CSTR StringBinding, RPC_CSTR *ObjUuid,
548 RPC_CSTR *Protseq, RPC_CSTR *NetworkAddr,
549 RPC_CSTR *Endpoint, RPC_CSTR *Options)
550 {
551 const unsigned char *data, *next;
552 static const char ep_opt[] = "endpoint=";
553 BOOL endpoint_already_found = FALSE;
554
555 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a((char*)StringBinding),
556 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
557
558 if (ObjUuid) *ObjUuid = NULL;
559 if (Protseq) *Protseq = NULL;
560 if (NetworkAddr) *NetworkAddr = NULL;
561 if (Endpoint) *Endpoint = NULL;
562 if (Options) *Options = NULL;
563
564 data = StringBinding;
565
566 next = string_binding_find_delimiter(data, '@');
567 if (next) {
568 UUID uuid;
569 RPC_STATUS status;
570 RPC_CSTR str_uuid = unescape_string_binding_component(data, next - data);
571 status = UuidFromStringA(str_uuid, &uuid);
572 if (status != RPC_S_OK) {
573 HeapFree(GetProcessHeap(), 0, str_uuid);
574 return status;
575 }
576 if (ObjUuid)
577 *ObjUuid = str_uuid;
578 else
579 HeapFree(GetProcessHeap(), 0, str_uuid);
580 data = next+1;
581 }
582
583 next = string_binding_find_delimiter(data, ':');
584 if (next) {
585 if (Protseq) *Protseq = unescape_string_binding_component(data, next - data);
586 data = next+1;
587 }
588
589 next = string_binding_find_delimiter(data, '[');
590 if (next) {
591 const unsigned char *close;
592 RPC_CSTR opt;
593
594 if (NetworkAddr) *NetworkAddr = unescape_string_binding_component(data, next - data);
595 data = next+1;
596 close = string_binding_find_delimiter(data, ']');
597 if (!close) goto fail;
598
599 /* tokenize options */
600 while (data < close) {
601 next = string_binding_find_delimiter(data, ',');
602 if (!next || next > close) next = close;
603 /* FIXME: this is kind of inefficient */
604 opt = unescape_string_binding_component(data, next - data);
605 data = next+1;
606
607 /* parse option */
608 next = string_binding_find_delimiter(opt, '=');
609 if (!next) {
610 /* not an option, must be an endpoint */
611 if (endpoint_already_found) goto fail;
612 if (Endpoint) *Endpoint = opt;
613 else HeapFree(GetProcessHeap(), 0, opt);
614 endpoint_already_found = TRUE;
615 } else {
616 if (strncmp((const char *)opt, ep_opt, strlen(ep_opt)) == 0) {
617 /* endpoint option */
618 if (endpoint_already_found) goto fail;
619 if (Endpoint) *Endpoint = unescape_string_binding_component(next+1, -1);
620 HeapFree(GetProcessHeap(), 0, opt);
621 endpoint_already_found = TRUE;
622 } else {
623 /* network option */
624 if (Options) {
625 if (*Options) {
626 /* FIXME: this is kind of inefficient */
627 *Options = (unsigned char*) RPCRT4_strconcatA( (char*)*Options, (char *)opt);
628 HeapFree(GetProcessHeap(), 0, opt);
629 } else
630 *Options = opt;
631 } else
632 HeapFree(GetProcessHeap(), 0, opt);
633 }
634 }
635 }
636
637 data = close+1;
638 if (*data) goto fail;
639 }
640 else if (NetworkAddr)
641 *NetworkAddr = unescape_string_binding_component(data, -1);
642
643 return RPC_S_OK;
644
645 fail:
646 if (ObjUuid) RpcStringFreeA(ObjUuid);
647 if (Protseq) RpcStringFreeA(Protseq);
648 if (NetworkAddr) RpcStringFreeA(NetworkAddr);
649 if (Endpoint) RpcStringFreeA(Endpoint);
650 if (Options) RpcStringFreeA(Options);
651 return RPC_S_INVALID_STRING_BINDING;
652 }
653
654 /***********************************************************************
655 * RpcStringBindingParseW (RPCRT4.@)
656 */
657 RPC_STATUS WINAPI RpcStringBindingParseW( RPC_WSTR StringBinding, RPC_WSTR *ObjUuid,
658 RPC_WSTR *Protseq, RPC_WSTR *NetworkAddr,
659 RPC_WSTR *Endpoint, RPC_WSTR *Options)
660 {
661 const WCHAR *data, *next;
662 static const WCHAR ep_opt[] = {'e','n','d','p','o','i','n','t','=',0};
663 BOOL endpoint_already_found = FALSE;
664
665 TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
666 ObjUuid, Protseq, NetworkAddr, Endpoint, Options);
667
668 if (ObjUuid) *ObjUuid = NULL;
669 if (Protseq) *Protseq = NULL;
670 if (NetworkAddr) *NetworkAddr = NULL;
671 if (Endpoint) *Endpoint = NULL;
672 if (Options) *Options = NULL;
673
674 data = StringBinding;
675
676 next = string_binding_find_delimiterW(data, '@');
677 if (next) {
678 UUID uuid;
679 RPC_STATUS status;
680 RPC_WSTR str_uuid = unescape_string_binding_componentW(data, next - data);
681 status = UuidFromStringW(str_uuid, &uuid);
682 if (status != RPC_S_OK) {
683 HeapFree(GetProcessHeap(), 0, str_uuid);
684 return status;
685 }
686 if (ObjUuid)
687 *ObjUuid = str_uuid;
688 else
689 HeapFree(GetProcessHeap(), 0, str_uuid);
690 data = next+1;
691 }
692
693 next = string_binding_find_delimiterW(data, ':');
694 if (next) {
695 if (Protseq) *Protseq = unescape_string_binding_componentW(data, next - data);
696 data = next+1;
697 }
698
699 next = string_binding_find_delimiterW(data, '[');
700 if (next) {
701 const WCHAR *close;
702 RPC_WSTR opt;
703
704 if (NetworkAddr) *NetworkAddr = unescape_string_binding_componentW(data, next - data);
705 data = next+1;
706 close = string_binding_find_delimiterW(data, ']');
707 if (!close) goto fail;
708
709 /* tokenize options */
710 while (data < close) {
711 next = string_binding_find_delimiterW(data, ',');
712 if (!next || next > close) next = close;
713 /* FIXME: this is kind of inefficient */
714 opt = unescape_string_binding_componentW(data, next - data);
715 data = next+1;
716
717 /* parse option */
718 next = string_binding_find_delimiterW(opt, '=');
719 if (!next) {
720 /* not an option, must be an endpoint */
721 if (endpoint_already_found) goto fail;
722 if (Endpoint) *Endpoint = opt;
723 else HeapFree(GetProcessHeap(), 0, opt);
724 endpoint_already_found = TRUE;
725 } else {
726 if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
727 /* endpoint option */
728 if (endpoint_already_found) goto fail;
729 if (Endpoint) *Endpoint = unescape_string_binding_componentW(next+1, -1);
730 HeapFree(GetProcessHeap(), 0, opt);
731 endpoint_already_found = TRUE;
732 } else {
733 /* network option */
734 if (Options) {
735 if (*Options) {
736 /* FIXME: this is kind of inefficient */
737 *Options = RPCRT4_strconcatW(*Options, opt);
738 HeapFree(GetProcessHeap(), 0, opt);
739 } else
740 *Options = opt;
741 } else
742 HeapFree(GetProcessHeap(), 0, opt);
743 }
744 }
745 }
746
747 data = close+1;
748 if (*data) goto fail;
749 } else if (NetworkAddr)
750 *NetworkAddr = unescape_string_binding_componentW(data, -1);
751
752 return RPC_S_OK;
753
754 fail:
755 if (ObjUuid) RpcStringFreeW(ObjUuid);
756 if (Protseq) RpcStringFreeW(Protseq);
757 if (NetworkAddr) RpcStringFreeW(NetworkAddr);
758 if (Endpoint) RpcStringFreeW(Endpoint);
759 if (Options) RpcStringFreeW(Options);
760 return RPC_S_INVALID_STRING_BINDING;
761 }
762
763 /***********************************************************************
764 * RpcBindingFree (RPCRT4.@)
765 */
766 RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
767 {
768 RPC_STATUS status;
769 TRACE("(%p) = %p\n", Binding, *Binding);
770 if (*Binding)
771 status = RPCRT4_ReleaseBinding(*Binding);
772 else
773 status = RPC_S_INVALID_BINDING;
774 if (status == RPC_S_OK) *Binding = NULL;
775 return status;
776 }
777
778 /***********************************************************************
779 * RpcBindingVectorFree (RPCRT4.@)
780 */
781 RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
782 {
783 ULONG c;
784
785 TRACE("(%p)\n", BindingVector);
786 for (c=0; c<(*BindingVector)->Count; c++) RpcBindingFree(&(*BindingVector)->BindingH[c]);
787 HeapFree(GetProcessHeap(), 0, *BindingVector);
788 *BindingVector = NULL;
789 return RPC_S_OK;
790 }
791
792 /***********************************************************************
793 * RpcBindingInqObject (RPCRT4.@)
794 */
795 RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
796 {
797 RpcBinding* bind = Binding;
798
799 TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
800 *ObjectUuid = bind->ObjectUuid;
801 return RPC_S_OK;
802 }
803
804 /***********************************************************************
805 * RpcBindingSetObject (RPCRT4.@)
806 */
807 RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
808 {
809 RpcBinding* bind = Binding;
810
811 TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
812 if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
813 return RPCRT4_SetBindingObject(Binding, ObjectUuid);
814 }
815
816 /***********************************************************************
817 * RpcBindingFromStringBindingA (RPCRT4.@)
818 */
819 RPC_STATUS WINAPI RpcBindingFromStringBindingA( RPC_CSTR StringBinding, RPC_BINDING_HANDLE* Binding )
820 {
821 RPC_STATUS ret;
822 RpcBinding* bind = NULL;
823 RPC_CSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
824 UUID Uuid;
825
826 TRACE("(%s,%p)\n", debugstr_a((char*)StringBinding), Binding);
827
828 ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
829 &NetworkAddr, &Endpoint, &Options);
830 if (ret != RPC_S_OK) return ret;
831
832 ret = UuidFromStringA(ObjectUuid, &Uuid);
833
834 if (ret == RPC_S_OK)
835 ret = RPCRT4_CreateBindingA(&bind, FALSE, (char*)Protseq);
836 if (ret == RPC_S_OK) {
837 ret = RPCRT4_SetBindingObject(bind, &Uuid);
838 if (ret == RPC_S_OK)
839 ret = RPCRT4_CompleteBindingA(bind, (char*)NetworkAddr, (char*)Endpoint, (char*)Options);
840
841 if (ret == RPC_S_OK)
842 *Binding = (RPC_BINDING_HANDLE)bind;
843 else
844 RPCRT4_ReleaseBinding(bind);
845 }
846
847 RpcStringFreeA(&Options);
848 RpcStringFreeA(&Endpoint);
849 RpcStringFreeA(&NetworkAddr);
850 RpcStringFreeA(&Protseq);
851 RpcStringFreeA(&ObjectUuid);
852
853 return ret;
854 }
855
856 /***********************************************************************
857 * RpcBindingFromStringBindingW (RPCRT4.@)
858 */
859 RPC_STATUS WINAPI RpcBindingFromStringBindingW( RPC_WSTR StringBinding, RPC_BINDING_HANDLE* Binding )
860 {
861 RPC_STATUS ret;
862 RpcBinding* bind = NULL;
863 RPC_WSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
864 UUID Uuid;
865
866 TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);
867
868 ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
869 &NetworkAddr, &Endpoint, &Options);
870 if (ret != RPC_S_OK) return ret;
871
872 ret = UuidFromStringW(ObjectUuid, &Uuid);
873
874 if (ret == RPC_S_OK)
875 ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
876 if (ret == RPC_S_OK) {
877 ret = RPCRT4_SetBindingObject(bind, &Uuid);
878 if (ret == RPC_S_OK)
879 ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);
880
881 if (ret == RPC_S_OK)
882 *Binding = (RPC_BINDING_HANDLE)bind;
883 else
884 RPCRT4_ReleaseBinding(bind);
885 }
886
887 RpcStringFreeW(&Options);
888 RpcStringFreeW(&Endpoint);
889 RpcStringFreeW(&NetworkAddr);
890 RpcStringFreeW(&Protseq);
891 RpcStringFreeW(&ObjectUuid);
892
893 return ret;
894 }
895
896 /***********************************************************************
897 * RpcBindingToStringBindingA (RPCRT4.@)
898 */
899 RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, RPC_CSTR *StringBinding )
900 {
901 RPC_STATUS ret;
902 RpcBinding* bind = Binding;
903 RPC_CSTR ObjectUuid;
904
905 TRACE("(%p,%p)\n", Binding, StringBinding);
906
907 if (UuidIsNil(&bind->ObjectUuid, &ret))
908 ObjectUuid = NULL;
909 else
910 {
911 ret = UuidToStringA(&bind->ObjectUuid, &ObjectUuid);
912 if (ret != RPC_S_OK) return ret;
913 }
914
915 ret = RpcStringBindingComposeA(ObjectUuid, (unsigned char*)bind->Protseq, (unsigned char*) bind->NetworkAddr,
916 (unsigned char*) bind->Endpoint, NULL, StringBinding);
917
918 RpcStringFreeA(&ObjectUuid);
919
920 return ret;
921 }
922
923 /***********************************************************************
924 * RpcBindingToStringBindingW (RPCRT4.@)
925 */
926 RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, RPC_WSTR *StringBinding )
927 {
928 RPC_STATUS ret;
929 unsigned char *str = NULL;
930 TRACE("(%p,%p)\n", Binding, StringBinding);
931 ret = RpcBindingToStringBindingA(Binding, &str);
932 *StringBinding = RPCRT4_strdupAtoW((char*)str);
933 RpcStringFreeA(&str);
934 return ret;
935 }
936
937 /***********************************************************************
938 * I_RpcBindingInqTransportType (RPCRT4.@)
939 */
940 RPC_STATUS WINAPI I_RpcBindingInqTransportType( RPC_BINDING_HANDLE Binding, unsigned int * Type )
941 {
942
943 FIXME( "(%p,%p): stub\n", Binding, Type);
944 *Type = TRANSPORT_TYPE_LPC;
945 return RPC_S_OK;
946 }
947
948 /***********************************************************************
949 * I_RpcBindingSetAsync (RPCRT4.@)
950 * NOTES
951 * Exists in win9x and winNT, but with different number of arguments
952 * (9x version has 3 arguments, NT has 2).
953 */
954 RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
955 {
956 RpcBinding* bind = Binding;
957
958 TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
959
960 bind->BlockingFn = BlockingFn;
961
962 return RPC_S_OK;
963 }
964
965 /***********************************************************************
966 * RpcBindingCopy (RPCRT4.@)
967 */
968 RPC_STATUS RPC_ENTRY RpcBindingCopy(
969 RPC_BINDING_HANDLE SourceBinding,
970 RPC_BINDING_HANDLE* DestinationBinding)
971 {
972 RpcBinding *DestBinding;
973 RpcBinding *SrcBinding = SourceBinding;
974 RPC_STATUS status;
975
976 TRACE("(%p, %p)\n", SourceBinding, DestinationBinding);
977
978 status = RPCRT4_AllocBinding(&DestBinding, SrcBinding->server);
979 if (status != RPC_S_OK) return status;
980
981 DestBinding->ObjectUuid = SrcBinding->ObjectUuid;
982 DestBinding->BlockingFn = SrcBinding->BlockingFn;
983 DestBinding->Protseq = RPCRT4_strndupA(SrcBinding->Protseq, -1);
984 DestBinding->NetworkAddr = RPCRT4_strndupA(SrcBinding->NetworkAddr, -1);
985 DestBinding->Endpoint = RPCRT4_strndupA(SrcBinding->Endpoint, -1);
986 DestBinding->NetworkOptions = RPCRT4_strdupW(SrcBinding->NetworkOptions);
987 DestBinding->CookieAuth = RPCRT4_strdupW(SrcBinding->CookieAuth);
988 if (SrcBinding->Assoc) SrcBinding->Assoc->refs++;
989 DestBinding->Assoc = SrcBinding->Assoc;
990
991 if (SrcBinding->AuthInfo) RpcAuthInfo_AddRef(SrcBinding->AuthInfo);
992 DestBinding->AuthInfo = SrcBinding->AuthInfo;
993 if (SrcBinding->QOS) RpcQualityOfService_AddRef(SrcBinding->QOS);
994 DestBinding->QOS = SrcBinding->QOS;
995
996 *DestinationBinding = DestBinding;
997 return RPC_S_OK;
998 }
999
1000 /***********************************************************************
1001 * RpcBindingReset (RPCRT4.@)
1002 */
1003 RPC_STATUS RPC_ENTRY RpcBindingReset(RPC_BINDING_HANDLE Binding)
1004 {
1005 RpcBinding *bind = Binding;
1006
1007 TRACE("(%p)\n", Binding);
1008
1009 RPCRT4_strfree(bind->Endpoint);
1010 bind->Endpoint = NULL;
1011 if (bind->Assoc) RpcAssoc_Release(bind->Assoc);
1012 bind->Assoc = NULL;
1013
1014 return RPC_S_OK;
1015 }
1016
1017 /***********************************************************************
1018 * RpcImpersonateClient (RPCRT4.@)
1019 *
1020 * Impersonates the client connected via a binding handle so that security
1021 * checks are done in the context of the client.
1022 *
1023 * PARAMS
1024 * BindingHandle [I] Handle to the binding to the client.
1025 *
1026 * RETURNS
1027 * Success: RPS_S_OK.
1028 * Failure: RPC_STATUS value.
1029 *
1030 * NOTES
1031 *
1032 * If BindingHandle is NULL then the function impersonates the client
1033 * connected to the binding handle of the current thread.
1034 */
1035 RPC_STATUS WINAPI RpcImpersonateClient(RPC_BINDING_HANDLE BindingHandle)
1036 {
1037 RpcBinding *bind;
1038
1039 TRACE("(%p)\n", BindingHandle);
1040
1041 if (!BindingHandle) BindingHandle = I_RpcGetCurrentCallHandle();
1042 if (!BindingHandle) return RPC_S_INVALID_BINDING;
1043
1044 bind = BindingHandle;
1045 if (bind->FromConn)
1046 return rpcrt4_conn_impersonate_client(bind->FromConn);
1047 return RPC_S_WRONG_KIND_OF_BINDING;
1048 }
1049
1050 /***********************************************************************
1051 * RpcRevertToSelfEx (RPCRT4.@)
1052 *
1053 * Stops impersonating the client connected to the binding handle so that security
1054 * checks are no longer done in the context of the client.
1055 *
1056 * PARAMS
1057 * BindingHandle [I] Handle to the binding to the client.
1058 *
1059 * RETURNS
1060 * Success: RPS_S_OK.
1061 * Failure: RPC_STATUS value.
1062 *
1063 * NOTES
1064 *
1065 * If BindingHandle is NULL then the function stops impersonating the client
1066 * connected to the binding handle of the current thread.
1067 */
1068 RPC_STATUS WINAPI RpcRevertToSelfEx(RPC_BINDING_HANDLE BindingHandle)
1069 {
1070 RpcBinding *bind;
1071
1072 TRACE("(%p)\n", BindingHandle);
1073
1074 if (!BindingHandle) BindingHandle = I_RpcGetCurrentCallHandle();
1075 if (!BindingHandle) return RPC_S_INVALID_BINDING;
1076
1077 bind = BindingHandle;
1078 if (bind->FromConn)
1079 return rpcrt4_conn_revert_to_self(bind->FromConn);
1080 return RPC_S_WRONG_KIND_OF_BINDING;
1081 }
1082
1083 static inline BOOL has_nt_auth_identity(ULONG AuthnLevel)
1084 {
1085 switch (AuthnLevel)
1086 {
1087 case RPC_C_AUTHN_GSS_NEGOTIATE:
1088 case RPC_C_AUTHN_WINNT:
1089 case RPC_C_AUTHN_GSS_KERBEROS:
1090 return TRUE;
1091 default:
1092 return FALSE;
1093 }
1094 }
1095
1096 RPC_STATUS RpcAuthInfo_Create(ULONG AuthnLevel, ULONG AuthnSvc,
1097 CredHandle cred, TimeStamp exp,
1098 ULONG cbMaxToken,
1099 RPC_AUTH_IDENTITY_HANDLE identity,
1100 RpcAuthInfo **ret)
1101 {
1102 RpcAuthInfo *AuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo));
1103 if (!AuthInfo)
1104 return RPC_S_OUT_OF_MEMORY;
1105
1106 AuthInfo->refs = 1;
1107 AuthInfo->AuthnLevel = AuthnLevel;
1108 AuthInfo->AuthnSvc = AuthnSvc;
1109 AuthInfo->cred = cred;
1110 AuthInfo->exp = exp;
1111 AuthInfo->cbMaxToken = cbMaxToken;
1112 AuthInfo->identity = identity;
1113 AuthInfo->server_principal_name = NULL;
1114
1115 /* duplicate the SEC_WINNT_AUTH_IDENTITY structure, if applicable, to
1116 * enable better matching in RpcAuthInfo_IsEqual */
1117 if (identity && has_nt_auth_identity(AuthnSvc))
1118 {
1119 const SEC_WINNT_AUTH_IDENTITY_W *nt_identity = identity;
1120 AuthInfo->nt_identity = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo->nt_identity));
1121 if (!AuthInfo->nt_identity)
1122 {
1123 HeapFree(GetProcessHeap(), 0, AuthInfo);
1124 return RPC_S_OUT_OF_MEMORY;
1125 }
1126
1127 AuthInfo->nt_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1128 if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1129 AuthInfo->nt_identity->User = RPCRT4_strndupW(nt_identity->User, nt_identity->UserLength);
1130 else
1131 AuthInfo->nt_identity->User = RPCRT4_strndupAtoW((const char *)nt_identity->User, nt_identity->UserLength);
1132 AuthInfo->nt_identity->UserLength = nt_identity->UserLength;
1133 if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1134 AuthInfo->nt_identity->Domain = RPCRT4_strndupW(nt_identity->Domain, nt_identity->DomainLength);
1135 else
1136 AuthInfo->nt_identity->Domain = RPCRT4_strndupAtoW((const char *)nt_identity->Domain, nt_identity->DomainLength);
1137 AuthInfo->nt_identity->DomainLength = nt_identity->DomainLength;
1138 if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
1139 AuthInfo->nt_identity->Password = RPCRT4_strndupW(nt_identity->Password, nt_identity->PasswordLength);
1140 else
1141 AuthInfo->nt_identity->Password = RPCRT4_strndupAtoW((const char *)nt_identity->Password, nt_identity->PasswordLength);
1142 AuthInfo->nt_identity->PasswordLength = nt_identity->PasswordLength;
1143
1144 if ((nt_identity->User && !AuthInfo->nt_identity->User) ||
1145 (nt_identity->Domain && !AuthInfo->nt_identity->Domain) ||
1146 (nt_identity->Password && !AuthInfo->nt_identity->Password))
1147 {
1148 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
1149 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1150 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1151 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
1152 HeapFree(GetProcessHeap(), 0, AuthInfo);
1153 return RPC_S_OUT_OF_MEMORY;
1154 }
1155 }
1156 else
1157 AuthInfo->nt_identity = NULL;
1158 *ret = AuthInfo;
1159 return RPC_S_OK;
1160 }
1161
1162 ULONG RpcAuthInfo_AddRef(RpcAuthInfo *AuthInfo)
1163 {
1164 return InterlockedIncrement(&AuthInfo->refs);
1165 }
1166
1167 ULONG RpcAuthInfo_Release(RpcAuthInfo *AuthInfo)
1168 {
1169 ULONG refs = InterlockedDecrement(&AuthInfo->refs);
1170
1171 if (!refs)
1172 {
1173 FreeCredentialsHandle(&AuthInfo->cred);
1174 if (AuthInfo->nt_identity)
1175 {
1176 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
1177 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1178 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1179 HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
1180 }
1181 HeapFree(GetProcessHeap(), 0, AuthInfo->server_principal_name);
1182 HeapFree(GetProcessHeap(), 0, AuthInfo);
1183 }
1184
1185 return refs;
1186 }
1187
1188 BOOL RpcAuthInfo_IsEqual(const RpcAuthInfo *AuthInfo1, const RpcAuthInfo *AuthInfo2)
1189 {
1190 if (AuthInfo1 == AuthInfo2)
1191 return TRUE;
1192
1193 if (!AuthInfo1 || !AuthInfo2)
1194 return FALSE;
1195
1196 if ((AuthInfo1->AuthnLevel != AuthInfo2->AuthnLevel) ||
1197 (AuthInfo1->AuthnSvc != AuthInfo2->AuthnSvc))
1198 return FALSE;
1199
1200 if (AuthInfo1->identity == AuthInfo2->identity)
1201 return TRUE;
1202
1203 if (!AuthInfo1->identity || !AuthInfo2->identity)
1204 return FALSE;
1205
1206 if (has_nt_auth_identity(AuthInfo1->AuthnSvc))
1207 {
1208 const SEC_WINNT_AUTH_IDENTITY_W *identity1 = AuthInfo1->nt_identity;
1209 const SEC_WINNT_AUTH_IDENTITY_W *identity2 = AuthInfo2->nt_identity;
1210 /* compare user names */
1211 if (identity1->UserLength != identity2->UserLength ||
1212 memcmp(identity1->User, identity2->User, identity1->UserLength))
1213 return FALSE;
1214 /* compare domain names */
1215 if (identity1->DomainLength != identity2->DomainLength ||
1216 memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1217 return FALSE;
1218 /* compare passwords */
1219 if (identity1->PasswordLength != identity2->PasswordLength ||
1220 memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1221 return FALSE;
1222 }
1223 else
1224 return FALSE;
1225
1226 return TRUE;
1227 }
1228
1229 static RPC_STATUS RpcQualityOfService_Create(const RPC_SECURITY_QOS *qos_src, BOOL unicode, RpcQualityOfService **qos_dst)
1230 {
1231 RpcQualityOfService *qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos));
1232
1233 if (!qos)
1234 return RPC_S_OUT_OF_RESOURCES;
1235
1236 qos->refs = 1;
1237 qos->qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos->qos));
1238 if (!qos->qos) goto error;
1239 qos->qos->Version = qos_src->Version;
1240 qos->qos->Capabilities = qos_src->Capabilities;
1241 qos->qos->IdentityTracking = qos_src->IdentityTracking;
1242 qos->qos->ImpersonationType = qos_src->ImpersonationType;
1243 qos->qos->AdditionalSecurityInfoType = 0;
1244
1245 if (qos_src->Version >= 2)
1246 {
1247 const RPC_SECURITY_QOS_V2_W *qos_src2 = (const RPC_SECURITY_QOS_V2_W *)qos_src;
1248 qos->qos->AdditionalSecurityInfoType = qos_src2->AdditionalSecurityInfoType;
1249 if (qos_src2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1250 {
1251 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_src = qos_src2->u.HttpCredentials;
1252 RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_dst;
1253
1254 http_credentials_dst = HeapAlloc(GetProcessHeap(), 0, sizeof(*http_credentials_dst));
1255 qos->qos->u.HttpCredentials = http_credentials_dst;
1256 if (!http_credentials_dst) goto error;
1257 http_credentials_dst->TransportCredentials = NULL;
1258 http_credentials_dst->Flags = http_credentials_src->Flags;
1259 http_credentials_dst->AuthenticationTarget = http_credentials_src->AuthenticationTarget;
1260 http_credentials_dst->NumberOfAuthnSchemes = http_credentials_src->NumberOfAuthnSchemes;
1261 http_credentials_dst->AuthnSchemes = NULL;
1262 http_credentials_dst->ServerCertificateSubject = NULL;
1263 if (http_credentials_src->TransportCredentials)
1264 {
1265 SEC_WINNT_AUTH_IDENTITY_W *cred_dst;
1266 cred_dst = http_credentials_dst->TransportCredentials = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cred_dst));
1267 if (!cred_dst) goto error;
1268 cred_dst->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1269 if (unicode)
1270 {
1271 const SEC_WINNT_AUTH_IDENTITY_W *cred_src = http_credentials_src->TransportCredentials;
1272 cred_dst->UserLength = cred_src->UserLength;
1273 cred_dst->PasswordLength = cred_src->PasswordLength;
1274 cred_dst->DomainLength = cred_src->DomainLength;
1275 cred_dst->User = RPCRT4_strndupW(cred_src->User, cred_src->UserLength);
1276 cred_dst->Password = RPCRT4_strndupW(cred_src->Password, cred_src->PasswordLength);
1277 cred_dst->Domain = RPCRT4_strndupW(cred_src->Domain, cred_src->DomainLength);
1278 }
1279 else
1280 {
1281 const SEC_WINNT_AUTH_IDENTITY_A *cred_src = (const SEC_WINNT_AUTH_IDENTITY_A *)http_credentials_src->TransportCredentials;
1282 cred_dst->UserLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, NULL, 0);
1283 cred_dst->DomainLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, NULL, 0);
1284 cred_dst->PasswordLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, NULL, 0);
1285 cred_dst->User = HeapAlloc(GetProcessHeap(), 0, cred_dst->UserLength * sizeof(WCHAR));
1286 cred_dst->Password = HeapAlloc(GetProcessHeap(), 0, cred_dst->PasswordLength * sizeof(WCHAR));
1287 cred_dst->Domain = HeapAlloc(GetProcessHeap(), 0, cred_dst->DomainLength * sizeof(WCHAR));
1288 if (!cred_dst->Password || !cred_dst->Domain) goto error;
1289 MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, cred_dst->User, cred_dst->UserLength);
1290 MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, cred_dst->Domain, cred_dst->DomainLength);
1291 MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, cred_dst->Password, cred_dst->PasswordLength);
1292 }
1293 }
1294 if (http_credentials_src->NumberOfAuthnSchemes)
1295 {
1296 http_credentials_dst->AuthnSchemes = HeapAlloc(GetProcessHeap(), 0, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1297 if (!http_credentials_dst->AuthnSchemes) goto error;
1298 memcpy(http_credentials_dst->AuthnSchemes, http_credentials_src->AuthnSchemes, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
1299 }
1300 if (http_credentials_src->ServerCertificateSubject)
1301 {
1302 if (unicode)
1303 http_credentials_dst->ServerCertificateSubject =
1304 RPCRT4_strndupW(http_credentials_src->ServerCertificateSubject,
1305 strlenW(http_credentials_src->ServerCertificateSubject));
1306 else
1307 http_credentials_dst->ServerCertificateSubject =
1308 RPCRT4_strdupAtoW((char *)http_credentials_src->ServerCertificateSubject);
1309 if (!http_credentials_dst->ServerCertificateSubject) goto error;
1310 }
1311 }
1312 }
1313 *qos_dst = qos;
1314 return RPC_S_OK;
1315
1316 error:
1317 if (qos->qos)
1318 {
1319 if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP &&
1320 qos->qos->u.HttpCredentials)
1321 {
1322 if (qos->qos->u.HttpCredentials->TransportCredentials)
1323 {
1324 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1325 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1326 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1327 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1328 }
1329 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1330 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1331 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1332 }
1333 HeapFree(GetProcessHeap(), 0, qos->qos);
1334 }
1335 HeapFree(GetProcessHeap(), 0, qos);
1336 return RPC_S_OUT_OF_RESOURCES;
1337 }
1338
1339 ULONG RpcQualityOfService_AddRef(RpcQualityOfService *qos)
1340 {
1341 return InterlockedIncrement(&qos->refs);
1342 }
1343
1344 ULONG RpcQualityOfService_Release(RpcQualityOfService *qos)
1345 {
1346 ULONG refs = InterlockedDecrement(&qos->refs);
1347
1348 if (!refs)
1349 {
1350 if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1351 {
1352 if (qos->qos->u.HttpCredentials->TransportCredentials)
1353 {
1354 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
1355 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
1356 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
1357 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
1358 }
1359 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
1360 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
1361 HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
1362 }
1363 HeapFree(GetProcessHeap(), 0, qos->qos);
1364 HeapFree(GetProcessHeap(), 0, qos);
1365 }
1366 return refs;
1367 }
1368
1369 BOOL RpcQualityOfService_IsEqual(const RpcQualityOfService *qos1, const RpcQualityOfService *qos2)
1370 {
1371 if (qos1 == qos2)
1372 return TRUE;
1373
1374 if (!qos1 || !qos2)
1375 return FALSE;
1376
1377 TRACE("qos1 = { %d %d %d %d }, qos2 = { %d %d %d %d }\n",
1378 qos1->qos->Capabilities, qos1->qos->IdentityTracking,
1379 qos1->qos->ImpersonationType, qos1->qos->AdditionalSecurityInfoType,
1380 qos2->qos->Capabilities, qos2->qos->IdentityTracking,
1381 qos2->qos->ImpersonationType, qos2->qos->AdditionalSecurityInfoType);
1382
1383 if ((qos1->qos->Capabilities != qos2->qos->Capabilities) ||
1384 (qos1->qos->IdentityTracking != qos2->qos->IdentityTracking) ||
1385 (qos1->qos->ImpersonationType != qos2->qos->ImpersonationType) ||
1386 (qos1->qos->AdditionalSecurityInfoType != qos2->qos->AdditionalSecurityInfoType))
1387 return FALSE;
1388
1389 if (qos1->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1390 {
1391 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials1 = qos1->qos->u.HttpCredentials;
1392 const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials2 = qos2->qos->u.HttpCredentials;
1393
1394 if (http_credentials1->Flags != http_credentials2->Flags)
1395 return FALSE;
1396
1397 if (http_credentials1->AuthenticationTarget != http_credentials2->AuthenticationTarget)
1398 return FALSE;
1399
1400 if (http_credentials1->NumberOfAuthnSchemes != http_credentials2->NumberOfAuthnSchemes)
1401 return FALSE;
1402
1403 if ((!http_credentials1->AuthnSchemes && http_credentials2->AuthnSchemes) ||
1404 (http_credentials1->AuthnSchemes && !http_credentials2->AuthnSchemes))
1405 return FALSE;
1406
1407 if (memcmp(http_credentials1->AuthnSchemes, http_credentials2->AuthnSchemes,
1408 http_credentials1->NumberOfAuthnSchemes * sizeof(http_credentials1->AuthnSchemes[0])))
1409 return FALSE;
1410
1411 /* server certificate subject not currently used */
1412
1413 if (http_credentials1->TransportCredentials != http_credentials2->TransportCredentials)
1414 {
1415 const SEC_WINNT_AUTH_IDENTITY_W *identity1 = http_credentials1->TransportCredentials;
1416 const SEC_WINNT_AUTH_IDENTITY_W *identity2 = http_credentials2->TransportCredentials;
1417
1418 if (!identity1 || !identity2)
1419 return FALSE;
1420
1421 /* compare user names */
1422 if (identity1->UserLength != identity2->UserLength ||
1423 memcmp(identity1->User, identity2->User, identity1->UserLength))
1424 return FALSE;
1425 /* compare domain names */
1426 if (identity1->DomainLength != identity2->DomainLength ||
1427 memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
1428 return FALSE;
1429 /* compare passwords */
1430 if (identity1->PasswordLength != identity2->PasswordLength ||
1431 memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
1432 return FALSE;
1433 }
1434 }
1435
1436 return TRUE;
1437 }
1438
1439 /***********************************************************************
1440 * RpcRevertToSelf (RPCRT4.@)
1441 */
1442 RPC_STATUS WINAPI RpcRevertToSelf(void)
1443 {
1444 TRACE("\n");
1445 return RpcRevertToSelfEx(NULL);
1446 }
1447
1448 /***********************************************************************
1449 * RpcMgmtSetComTimeout (RPCRT4.@)
1450 */
1451 RPC_STATUS WINAPI RpcMgmtSetComTimeout(RPC_BINDING_HANDLE BindingHandle, unsigned int Timeout)
1452 {
1453 FIXME("(%p, %d): stub\n", BindingHandle, Timeout);
1454 return RPC_S_OK;
1455 }
1456
1457 /***********************************************************************
1458 * RpcBindingInqAuthInfoExA (RPCRT4.@)
1459 */
1460 RPCRTAPI RPC_STATUS RPC_ENTRY
1461 RpcBindingInqAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1462 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1463 ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1464 {
1465 RPC_STATUS status;
1466 RPC_WSTR principal;
1467
1468 TRACE("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1469 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1470
1471 status = RpcBindingInqAuthInfoExW(Binding, ServerPrincName ? &principal : NULL, AuthnLevel,
1472 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1473 if (status == RPC_S_OK && ServerPrincName)
1474 {
1475 *ServerPrincName = (RPC_CSTR)RPCRT4_strdupWtoA(principal);
1476 RpcStringFreeW(&principal);
1477 if (!*ServerPrincName) return RPC_S_OUT_OF_MEMORY;
1478 }
1479
1480 return status;
1481 }
1482
1483 /***********************************************************************
1484 * RpcBindingInqAuthInfoExW (RPCRT4.@)
1485 */
1486 RPCRTAPI RPC_STATUS RPC_ENTRY
1487 RpcBindingInqAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1488 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
1489 ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1490 {
1491 RpcBinding *bind = Binding;
1492
1493 TRACE("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1494 AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
1495
1496 if (!bind->AuthInfo) return RPC_S_BINDING_HAS_NO_AUTH;
1497
1498 if (SecurityQOS)
1499 {
1500 FIXME("QOS not implemented\n");
1501 return RPC_S_INVALID_BINDING;
1502 }
1503
1504 if (ServerPrincName)
1505 {
1506 if (bind->AuthInfo->server_principal_name)
1507 {
1508 *ServerPrincName = RPCRT4_strdupW(bind->AuthInfo->server_principal_name);
1509 if (!*ServerPrincName) return RPC_S_OUT_OF_MEMORY;
1510 }
1511 else *ServerPrincName = NULL;
1512 }
1513 if (AuthnLevel) *AuthnLevel = bind->AuthInfo->AuthnLevel;
1514 if (AuthnSvc) *AuthnSvc = bind->AuthInfo->AuthnSvc;
1515 if (AuthIdentity) *AuthIdentity = bind->AuthInfo->identity;
1516 if (AuthzSvc)
1517 {
1518 FIXME("authorization service not implemented\n");
1519 *AuthzSvc = RPC_C_AUTHZ_NONE;
1520 }
1521
1522 return RPC_S_OK;
1523 }
1524
1525 /***********************************************************************
1526 * RpcBindingInqAuthInfoA (RPCRT4.@)
1527 */
1528 RPCRTAPI RPC_STATUS RPC_ENTRY
1529 RpcBindingInqAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
1530 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1531 {
1532 return RpcBindingInqAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity,
1533 AuthzSvc, 0, NULL);
1534 }
1535
1536 /***********************************************************************
1537 * RpcBindingInqAuthInfoW (RPCRT4.@)
1538 */
1539 RPCRTAPI RPC_STATUS RPC_ENTRY
1540 RpcBindingInqAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
1541 ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1542 {
1543 return RpcBindingInqAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity,
1544 AuthzSvc, 0, NULL);
1545 }
1546
1547 /***********************************************************************
1548 * RpcBindingInqAuthClientA (RPCRT4.@)
1549 */
1550 RPCRTAPI RPC_STATUS RPC_ENTRY
1551 RpcBindingInqAuthClientA( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1552 RPC_CSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1553 ULONG *AuthzSvc )
1554 {
1555 return RpcBindingInqAuthClientExA(ClientBinding, Privs, ServerPrincName, AuthnLevel,
1556 AuthnSvc, AuthzSvc, 0);
1557 }
1558
1559 /***********************************************************************
1560 * RpcBindingInqAuthClientW (RPCRT4.@)
1561 */
1562 RPCRTAPI RPC_STATUS RPC_ENTRY
1563 RpcBindingInqAuthClientW( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1564 RPC_WSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1565 ULONG *AuthzSvc )
1566 {
1567 return RpcBindingInqAuthClientExW(ClientBinding, Privs, ServerPrincName, AuthnLevel,
1568 AuthnSvc, AuthzSvc, 0);
1569 }
1570
1571 /***********************************************************************
1572 * RpcBindingInqAuthClientExA (RPCRT4.@)
1573 */
1574 RPCRTAPI RPC_STATUS RPC_ENTRY
1575 RpcBindingInqAuthClientExA( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1576 RPC_CSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1577 ULONG *AuthzSvc, ULONG Flags )
1578 {
1579 RPC_STATUS status;
1580 RPC_WSTR principal;
1581
1582 TRACE("%p %p %p %p %p %p 0x%x\n", ClientBinding, Privs, ServerPrincName, AuthnLevel,
1583 AuthnSvc, AuthzSvc, Flags);
1584
1585 status = RpcBindingInqAuthClientExW(ClientBinding, Privs, ServerPrincName ? &principal : NULL,
1586 AuthnLevel, AuthnSvc, AuthzSvc, Flags);
1587 if (status == RPC_S_OK && ServerPrincName)
1588 {
1589 *ServerPrincName = (RPC_CSTR)RPCRT4_strdupWtoA(principal);
1590 if (!*ServerPrincName && principal) status = RPC_S_OUT_OF_MEMORY;
1591 RpcStringFreeW(&principal);
1592 }
1593
1594 return status;
1595 }
1596
1597 /***********************************************************************
1598 * RpcBindingInqAuthClientExW (RPCRT4.@)
1599 */
1600 RPCRTAPI RPC_STATUS RPC_ENTRY
1601 RpcBindingInqAuthClientExW( RPC_BINDING_HANDLE ClientBinding, RPC_AUTHZ_HANDLE *Privs,
1602 RPC_WSTR *ServerPrincName, ULONG *AuthnLevel, ULONG *AuthnSvc,
1603 ULONG *AuthzSvc, ULONG Flags )
1604 {
1605 RpcBinding *bind;
1606
1607 TRACE("%p %p %p %p %p %p 0x%x\n", ClientBinding, Privs, ServerPrincName, AuthnLevel,
1608 AuthnSvc, AuthzSvc, Flags);
1609
1610 if (!ClientBinding) ClientBinding = I_RpcGetCurrentCallHandle();
1611 if (!ClientBinding) return RPC_S_INVALID_BINDING;
1612
1613 bind = ClientBinding;
1614 if (!bind->FromConn) return RPC_S_INVALID_BINDING;
1615
1616 return rpcrt4_conn_inquire_auth_client(bind->FromConn, Privs,
1617 ServerPrincName, AuthnLevel,
1618 AuthnSvc, AuthzSvc, Flags);
1619 }
1620
1621 /***********************************************************************
1622 * RpcBindingServerFromClient (RPCRT4.@)
1623 */
1624 RPCRTAPI RPC_STATUS RPC_ENTRY
1625 RpcBindingServerFromClient(RPC_BINDING_HANDLE ClientBinding, RPC_BINDING_HANDLE* ServerBinding)
1626 {
1627 RpcBinding* bind = ClientBinding;
1628 RpcBinding* NewBinding;
1629
1630 if (!bind)
1631 bind = I_RpcGetCurrentCallHandle();
1632
1633 if (!bind->server)
1634 return RPC_S_INVALID_BINDING;
1635
1636 RPCRT4_AllocBinding(&NewBinding, TRUE);
1637 NewBinding->Protseq = RPCRT4_strdupA(bind->Protseq);
1638 NewBinding->NetworkAddr = RPCRT4_strdupA(bind->NetworkAddr);
1639
1640 *ServerBinding = NewBinding;
1641
1642 return RPC_S_OK;
1643 }
1644
1645 /***********************************************************************
1646 * RpcBindingSetAuthInfoExA (RPCRT4.@)
1647 */
1648 RPCRTAPI RPC_STATUS RPC_ENTRY
1649 RpcBindingSetAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName,
1650 ULONG AuthnLevel, ULONG AuthnSvc,
1651 RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1652 RPC_SECURITY_QOS *SecurityQos )
1653 {
1654 RpcBinding* bind = Binding;
1655 SECURITY_STATUS r;
1656 CredHandle cred;
1657 TimeStamp exp;
1658 ULONG package_count;
1659 ULONG i;
1660 PSecPkgInfoA packages;
1661 ULONG cbMaxToken;
1662
1663 TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_a((const char*)ServerPrincName),
1664 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1665
1666 if (SecurityQos)
1667 {
1668 RPC_STATUS status;
1669
1670 TRACE("SecurityQos { Version=%d, Capabilities=0x%x, IdentityTracking=%d, ImpersonationLevel=%d",
1671 SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1672 if (SecurityQos->Version >= 2)
1673 {
1674 const RPC_SECURITY_QOS_V2_A *SecurityQos2 = (const RPC_SECURITY_QOS_V2_A *)SecurityQos;
1675 TRACE(", AdditionalSecurityInfoType=%d", SecurityQos2->AdditionalSecurityInfoType);
1676 if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1677 TRACE(", { %p, 0x%x, %d, %d, %p(%u), %s }",
1678 SecurityQos2->u.HttpCredentials->TransportCredentials,
1679 SecurityQos2->u.HttpCredentials->Flags,
1680 SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1681 SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1682 SecurityQos2->u.HttpCredentials->AuthnSchemes,
1683 SecurityQos2->u.HttpCredentials->AuthnSchemes ? *SecurityQos2->u.HttpCredentials->AuthnSchemes : 0,
1684 SecurityQos2->u.HttpCredentials->ServerCertificateSubject);
1685 }
1686 TRACE("}\n");
1687 status = RpcQualityOfService_Create(SecurityQos, FALSE, &bind->QOS);
1688 if (status != RPC_S_OK)
1689 return status;
1690 }
1691 else
1692 {
1693 if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1694 bind->QOS = NULL;
1695 }
1696
1697 if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1698 AuthnSvc = RPC_C_AUTHN_WINNT;
1699
1700 /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1701 if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1702 AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1703
1704 if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1705 {
1706 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1707 bind->AuthInfo = NULL;
1708 return RPC_S_OK;
1709 }
1710
1711 if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1712 {
1713 FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1714 return RPC_S_UNKNOWN_AUTHN_LEVEL;
1715 }
1716
1717 /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1718 if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1719 {
1720 FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1721 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1722 }
1723
1724 r = EnumerateSecurityPackagesA(&package_count, &packages);
1725 if (r != SEC_E_OK)
1726 {
1727 ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n", r);
1728 return RPC_S_SEC_PKG_ERROR;
1729 }
1730
1731 for (i = 0; i < package_count; i++)
1732 if (packages[i].wRPCID == AuthnSvc)
1733 break;
1734
1735 if (i == package_count)
1736 {
1737 FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1738 FreeContextBuffer(packages);
1739 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1740 }
1741
1742 TRACE("found package %s for service %u\n", packages[i].Name, AuthnSvc);
1743 r = AcquireCredentialsHandleA(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1744 AuthIdentity, NULL, NULL, &cred, &exp);
1745 cbMaxToken = packages[i].cbMaxToken;
1746 FreeContextBuffer(packages);
1747 if (r == ERROR_SUCCESS)
1748 {
1749 RpcAuthInfo *new_auth_info;
1750 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1751 AuthIdentity, &new_auth_info);
1752 if (r == RPC_S_OK)
1753 {
1754 new_auth_info->server_principal_name = RPCRT4_strdupAtoW((char *)ServerPrincName);
1755 if (!ServerPrincName || new_auth_info->server_principal_name)
1756 {
1757 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1758 bind->AuthInfo = new_auth_info;
1759 }
1760 else
1761 {
1762 RpcAuthInfo_Release(new_auth_info);
1763 r = RPC_S_OUT_OF_MEMORY;
1764 }
1765 }
1766 else
1767 FreeCredentialsHandle(&cred);
1768 return r;
1769 }
1770 else
1771 {
1772 ERR("AcquireCredentialsHandleA failed with error 0x%08x\n", r);
1773 return RPC_S_SEC_PKG_ERROR;
1774 }
1775 }
1776
1777 /***********************************************************************
1778 * RpcBindingSetAuthInfoExW (RPCRT4.@)
1779 */
1780 RPCRTAPI RPC_STATUS RPC_ENTRY
1781 RpcBindingSetAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1782 ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1783 RPC_SECURITY_QOS *SecurityQos )
1784 {
1785 RpcBinding* bind = Binding;
1786 SECURITY_STATUS r;
1787 CredHandle cred;
1788 TimeStamp exp;
1789 ULONG package_count;
1790 ULONG i;
1791 PSecPkgInfoW packages;
1792 ULONG cbMaxToken;
1793
1794 TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_w(ServerPrincName),
1795 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);
1796
1797 if (SecurityQos)
1798 {
1799 RPC_STATUS status;
1800
1801 TRACE("SecurityQos { Version=%d, Capabilities=0x%x, IdentityTracking=%d, ImpersonationLevel=%d",
1802 SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
1803 if (SecurityQos->Version >= 2)
1804 {
1805 const RPC_SECURITY_QOS_V2_W *SecurityQos2 = (const RPC_SECURITY_QOS_V2_W *)SecurityQos;
1806 TRACE(", AdditionalSecurityInfoType=%d", SecurityQos2->AdditionalSecurityInfoType);
1807 if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1808 TRACE(", { %p, 0x%x, %d, %d, %p(%u), %s }",
1809 SecurityQos2->u.HttpCredentials->TransportCredentials,
1810 SecurityQos2->u.HttpCredentials->Flags,
1811 SecurityQos2->u.HttpCredentials->AuthenticationTarget,
1812 SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
1813 SecurityQos2->u.HttpCredentials->AuthnSchemes,
1814 SecurityQos2->u.HttpCredentials->AuthnSchemes ? *SecurityQos2->u.HttpCredentials->AuthnSchemes : 0,
1815 debugstr_w(SecurityQos2->u.HttpCredentials->ServerCertificateSubject));
1816 }
1817 TRACE("}\n");
1818 status = RpcQualityOfService_Create(SecurityQos, TRUE, &bind->QOS);
1819 if (status != RPC_S_OK)
1820 return status;
1821 }
1822 else
1823 {
1824 if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
1825 bind->QOS = NULL;
1826 }
1827
1828 if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
1829 AuthnSvc = RPC_C_AUTHN_WINNT;
1830
1831 /* FIXME: the mapping should probably be retrieved using SSPI somehow */
1832 if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
1833 AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;
1834
1835 if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
1836 {
1837 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1838 bind->AuthInfo = NULL;
1839 return RPC_S_OK;
1840 }
1841
1842 if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1843 {
1844 FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1845 return RPC_S_UNKNOWN_AUTHN_LEVEL;
1846 }
1847
1848 /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
1849 if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1850 {
1851 FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1852 return RPC_S_UNKNOWN_AUTHZ_SERVICE;
1853 }
1854
1855 r = EnumerateSecurityPackagesW(&package_count, &packages);
1856 if (r != SEC_E_OK)
1857 {
1858 ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", r);
1859 return RPC_S_SEC_PKG_ERROR;
1860 }
1861
1862 for (i = 0; i < package_count; i++)
1863 if (packages[i].wRPCID == AuthnSvc)
1864 break;
1865
1866 if (i == package_count)
1867 {
1868 FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1869 FreeContextBuffer(packages);
1870 return RPC_S_UNKNOWN_AUTHN_SERVICE;
1871 }
1872
1873 TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name), AuthnSvc);
1874 r = AcquireCredentialsHandleW(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1875 AuthIdentity, NULL, NULL, &cred, &exp);
1876 cbMaxToken = packages[i].cbMaxToken;
1877 FreeContextBuffer(packages);
1878 if (r == ERROR_SUCCESS)
1879 {
1880 RpcAuthInfo *new_auth_info;
1881 r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1882 AuthIdentity, &new_auth_info);
1883 if (r == RPC_S_OK)
1884 {
1885 new_auth_info->server_principal_name = RPCRT4_strdupW(ServerPrincName);
1886 if (!ServerPrincName || new_auth_info->server_principal_name)
1887 {
1888 if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
1889 bind->AuthInfo = new_auth_info;
1890 }
1891 else
1892 {
1893 RpcAuthInfo_Release(new_auth_info);
1894 r = RPC_S_OUT_OF_MEMORY;
1895 }
1896 }
1897 else
1898 FreeCredentialsHandle(&cred);
1899 return r;
1900 }
1901 else
1902 {
1903 ERR("AcquireCredentialsHandleW failed with error 0x%08x\n", r);
1904 return RPC_S_SEC_PKG_ERROR;
1905 }
1906 }
1907
1908 /***********************************************************************
1909 * RpcBindingSetAuthInfoA (RPCRT4.@)
1910 */
1911 RPCRTAPI RPC_STATUS RPC_ENTRY
1912 RpcBindingSetAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName, ULONG AuthnLevel,
1913 ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1914 {
1915 TRACE("%p %s %u %u %p %u\n", Binding, debugstr_a((const char*)ServerPrincName),
1916 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1917 return RpcBindingSetAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1918 }
1919
1920 /***********************************************************************
1921 * RpcBindingSetAuthInfoW (RPCRT4.@)
1922 */
1923 RPCRTAPI RPC_STATUS RPC_ENTRY
1924 RpcBindingSetAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
1925 ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1926 {
1927 TRACE("%p %s %u %u %p %u\n", Binding, debugstr_w(ServerPrincName),
1928 AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1929 return RpcBindingSetAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1930 }
1931
1932 /***********************************************************************
1933 * RpcBindingSetOption (RPCRT4.@)
1934 */
1935 RPC_STATUS WINAPI RpcBindingSetOption(RPC_BINDING_HANDLE BindingHandle, ULONG Option, ULONG_PTR OptionValue)
1936 {
1937 TRACE("(%p, %d, %ld)\n", BindingHandle, Option, OptionValue);
1938
1939 switch (Option)
1940 {
1941 case RPC_C_OPT_COOKIE_AUTH:
1942 {
1943 RPC_C_OPT_COOKIE_AUTH_DESCRIPTOR *cookie = (RPC_C_OPT_COOKIE_AUTH_DESCRIPTOR *)OptionValue;
1944 RpcBinding *binding = BindingHandle;
1945 int len = MultiByteToWideChar(CP_ACP, 0, cookie->Buffer, cookie->BufferSize, NULL, 0);
1946 WCHAR *str;
1947
1948 if (!(str = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR)))) return RPC_S_OUT_OF_MEMORY;
1949 MultiByteToWideChar(CP_ACP, 0, cookie->Buffer, cookie->BufferSize, str, len);
1950 str[len] = 0;
1951 HeapFree(GetProcessHeap(), 0, binding->CookieAuth);
1952 binding->CookieAuth = str;
1953 break;
1954 }
1955 default:
1956 FIXME("option %u not supported\n", Option);
1957 break;
1958 }
1959 return RPC_S_OK;
1960 }
1961
1962 /***********************************************************************
1963 * I_RpcBindingInqLocalClientPID (RPCRT4.@)
1964 */
1965
1966 RPC_STATUS WINAPI I_RpcBindingInqLocalClientPID(RPC_BINDING_HANDLE ClientBinding, ULONG *ClientPID)
1967 {
1968 FIXME("%p %p: stub\n", ClientBinding, ClientPID);
1969 return RPC_S_INVALID_BINDING;
1970 }