rpc_binding.c 60 KB
Newer Older
1 2 3
/*
 * 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
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 <stdarg.h>
25 26
#include <stdio.h>
#include <string.h>
27
#include <assert.h>
28 29 30 31 32

#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "winerror.h"
33
#include "winternl.h"
34 35 36
#include "wine/unicode.h"

#include "rpc.h"
37
#include "rpcndr.h"
38 39 40 41

#include "wine/debug.h"

#include "rpc_binding.h"
42
#include "rpc_assoc.h"
43

44
WINE_DEFAULT_DEBUG_CHANNEL(rpc);
45

46
LPSTR RPCRT4_strndupA(LPCSTR src, INT slen)
47 48 49 50 51 52 53 54 55 56 57 58
{
  DWORD len;
  LPSTR s;
  if (!src) return NULL;
  if (slen == -1) slen = strlen(src);
  len = slen;
  s = HeapAlloc(GetProcessHeap(), 0, len+1);
  memcpy(s, src, len);
  s[len] = 0;
  return s;
}

59
LPSTR RPCRT4_strdupWtoA(LPCWSTR src)
60 61 62 63 64 65 66 67 68 69
{
  DWORD len;
  LPSTR s;
  if (!src) return NULL;
  len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
  s = HeapAlloc(GetProcessHeap(), 0, len);
  WideCharToMultiByte(CP_ACP, 0, src, -1, s, len, NULL, NULL);
  return s;
}

70
LPWSTR RPCRT4_strdupAtoW(LPCSTR src)
71 72 73 74 75 76 77 78 79 80
{
  DWORD len;
  LPWSTR s;
  if (!src) return NULL;
  len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
  s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
  MultiByteToWideChar(CP_ACP, 0, src, -1, s, len);
  return s;
}

81 82 83 84 85 86 87 88 89 90 91
static LPWSTR RPCRT4_strndupAtoW(LPCSTR src, INT slen)
{
  DWORD len;
  LPWSTR s;
  if (!src) return NULL;
  len = MultiByteToWideChar(CP_ACP, 0, src, slen, NULL, 0);
  s = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
  MultiByteToWideChar(CP_ACP, 0, src, slen, s, len);
  return s;
}

92
LPWSTR RPCRT4_strndupW(LPCWSTR src, INT slen)
93 94 95 96 97 98 99 100 101 102 103 104 105 106
{
  DWORD len;
  LPWSTR s;
  if (!src) return NULL;
  if (slen == -1) slen = strlenW(src);
  len = slen;
  s = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
  memcpy(s, src, len*sizeof(WCHAR));
  s[len] = 0;
  return s;
}

void RPCRT4_strfree(LPSTR src)
{
107
  HeapFree(GetProcessHeap(), 0, src);
108 109
}

110
static RPC_STATUS RPCRT4_AllocBinding(RpcBinding** Binding, BOOL server)
111 112 113 114 115 116
{
  RpcBinding* NewBinding;

  NewBinding = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(RpcBinding));
  NewBinding->refs = 1;
  NewBinding->server = server;
117 118 119 120 121 122

  *Binding = NewBinding;

  return RPC_S_OK;
}

123
static RPC_STATUS RPCRT4_CreateBindingA(RpcBinding** Binding, BOOL server, LPCSTR Protseq)
124 125 126 127
{
  RpcBinding* NewBinding;

  RPCRT4_AllocBinding(&NewBinding, server);
128 129 130 131 132 133 134 135
  NewBinding->Protseq = RPCRT4_strdupA(Protseq);

  TRACE("binding: %p\n", NewBinding);
  *Binding = NewBinding;

  return RPC_S_OK;
}

136
static RPC_STATUS RPCRT4_CreateBindingW(RpcBinding** Binding, BOOL server, LPCWSTR Protseq)
137 138 139
{
  RpcBinding* NewBinding;

140
  RPCRT4_AllocBinding(&NewBinding, server);
141 142 143 144 145 146 147 148
  NewBinding->Protseq = RPCRT4_strdupWtoA(Protseq);

  TRACE("binding: %p\n", NewBinding);
  *Binding = NewBinding;

  return RPC_S_OK;
}

149 150
static RPC_STATUS RPCRT4_CompleteBindingA(RpcBinding* Binding, LPCSTR NetworkAddr,
                                          LPCSTR Endpoint, LPCSTR NetworkOptions)
151
{
152 153
  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));
156

157 158 159
  RPCRT4_strfree(Binding->NetworkAddr);
  Binding->NetworkAddr = RPCRT4_strdupA(NetworkAddr);
  RPCRT4_strfree(Binding->Endpoint);
160
  Binding->Endpoint = RPCRT4_strdupA(Endpoint);
161 162
  HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
  Binding->NetworkOptions = RPCRT4_strdupAtoW(NetworkOptions);
163

164 165 166 167 168 169 170 171 172
  /* only attempt to get an association if the binding is complete */
  if (Endpoint && Endpoint[0] != '\0')
  {
    status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
                                   Binding->Endpoint, Binding->NetworkOptions,
                                   &Binding->Assoc);
    if (status != RPC_S_OK)
        return status;
  }
173

174 175 176
  return RPC_S_OK;
}

177 178
static RPC_STATUS RPCRT4_CompleteBindingW(RpcBinding* Binding, LPCWSTR NetworkAddr,
                                          LPCWSTR Endpoint, LPCWSTR NetworkOptions)
179
{
180 181
  RPC_STATUS status;

182
  TRACE("(RpcBinding == ^%p, NetworkAddr == %s, EndPoint == %s, NetworkOptions == %s)\n", Binding, 
183 184
   debugstr_w(NetworkAddr), debugstr_w(Endpoint), debugstr_w(NetworkOptions));

185 186 187
  RPCRT4_strfree(Binding->NetworkAddr);
  Binding->NetworkAddr = RPCRT4_strdupWtoA(NetworkAddr);
  RPCRT4_strfree(Binding->Endpoint);
188
  Binding->Endpoint = RPCRT4_strdupWtoA(Endpoint);
189 190
  HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
  Binding->NetworkOptions = RPCRT4_strdupW(NetworkOptions);
191

192 193 194 195 196 197 198 199 200
  /* only attempt to get an association if the binding is complete */
  if (Endpoint && Endpoint[0] != '\0')
  {
    status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
                                   Binding->Endpoint, Binding->NetworkOptions,
                                   &Binding->Assoc);
    if (status != RPC_S_OK)
        return status;
  }
201

202 203 204
  return RPC_S_OK;
}

205
RPC_STATUS RPCRT4_ResolveBinding(RpcBinding* Binding, LPCSTR Endpoint)
206
{
207 208
  RPC_STATUS status;

209 210
  TRACE("(RpcBinding == ^%p, EndPoint == \"%s\"\n", Binding, Endpoint);

211 212 213
  RPCRT4_strfree(Binding->Endpoint);
  Binding->Endpoint = RPCRT4_strdupA(Endpoint);

214
  if (Binding->Assoc) RpcAssoc_Release(Binding->Assoc);
215 216 217 218 219 220 221
  Binding->Assoc = NULL;
  status = RPCRT4_GetAssociation(Binding->Protseq, Binding->NetworkAddr,
                                 Binding->Endpoint, Binding->NetworkOptions,
                                 &Binding->Assoc);
  if (status != RPC_S_OK)
      return status;

222 223 224
  return RPC_S_OK;
}

225
RPC_STATUS RPCRT4_SetBindingObject(RpcBinding* Binding, const UUID* ObjectUuid)
226
{
227
  TRACE("(*RpcBinding == ^%p, UUID == %s)\n", Binding, debugstr_guid(ObjectUuid)); 
228
  if (ObjectUuid) Binding->ObjectUuid = *ObjectUuid;
229 230 231 232
  else UuidCreateNil(&Binding->ObjectUuid);
  return RPC_S_OK;
}

233
RPC_STATUS RPCRT4_MakeBinding(RpcBinding** Binding, RpcConnection* Connection)
234 235
{
  RpcBinding* NewBinding;
236
  TRACE("(RpcBinding == ^%p, Connection == ^%p)\n", Binding, Connection);
237

238
  RPCRT4_AllocBinding(&NewBinding, Connection->server);
239
  NewBinding->Protseq = RPCRT4_strdupA(rpcrt4_conn_get_name(Connection));
240 241 242 243 244
  NewBinding->NetworkAddr = RPCRT4_strdupA(Connection->NetworkAddr);
  NewBinding->Endpoint = RPCRT4_strdupA(Connection->Endpoint);
  NewBinding->FromConn = Connection;

  TRACE("binding: %p\n", NewBinding);
245 246 247 248 249
  *Binding = NewBinding;

  return RPC_S_OK;
}

250
void RPCRT4_AddRefBinding(RpcBinding* Binding)
251
{
252
  InterlockedIncrement(&Binding->refs);
253 254
}

255
RPC_STATUS RPCRT4_ReleaseBinding(RpcBinding* Binding)
256 257 258 259 260
{
  if (InterlockedDecrement(&Binding->refs))
    return RPC_S_OK;

  TRACE("binding: %p\n", Binding);
261
  if (Binding->Assoc) RpcAssoc_Release(Binding->Assoc);
262 263 264
  RPCRT4_strfree(Binding->Endpoint);
  RPCRT4_strfree(Binding->NetworkAddr);
  RPCRT4_strfree(Binding->Protseq);
265
  HeapFree(GetProcessHeap(), 0, Binding->NetworkOptions);
266 267
  if (Binding->AuthInfo) RpcAuthInfo_Release(Binding->AuthInfo);
  if (Binding->QOS) RpcQualityOfService_Release(Binding->QOS);
268 269 270 271
  HeapFree(GetProcessHeap(), 0, Binding);
  return RPC_S_OK;
}

272
RPC_STATUS RPCRT4_OpenBinding(RpcBinding* Binding, RpcConnection** Connection,
273 274
                              const RPC_SYNTAX_IDENTIFIER *TransferSyntax,
                              const RPC_SYNTAX_IDENTIFIER *InterfaceId)
275
{
276
  TRACE("(Binding == ^%p)\n", Binding);
277

278
  if (!Binding->server) {
279 280
     return RpcAssoc_GetClientConnection(Binding->Assoc, InterfaceId,
         TransferSyntax, Binding->AuthInfo, Binding->QOS, Connection);
281
  } else {
282
    /* we already have a connection with acceptable binding, so use it */
283 284 285
    if (Binding->FromConn) {
      *Connection = Binding->FromConn;
      return RPC_S_OK;
286 287 288
    } else {
       ERR("no connection in binding\n");
       return RPC_S_INTERNAL_ERROR;
289 290
    }
  }
291 292
}

293
RPC_STATUS RPCRT4_CloseBinding(RpcBinding* Binding, RpcConnection* Connection)
294
{
295
  TRACE("(Binding == ^%p)\n", Binding);
296
  if (!Connection) return RPC_S_OK;
297 298 299 300 301 302 303
  if (Binding->server) {
    /* don't destroy a connection that is cached in the binding */
    if (Binding->FromConn == Connection)
      return RPC_S_OK;
    return RPCRT4_DestroyConnection(Connection);
  }
  else {
304
    RpcAssoc_ReleaseIdleConnection(Binding->Assoc, Connection);
305 306
    return RPC_S_OK;
  }
307 308 309 310 311 312
}

static LPSTR RPCRT4_strconcatA(LPSTR dst, LPCSTR src)
{
  DWORD len = strlen(dst), slen = strlen(src);
  LPSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(CHAR));
313 314 315 316 317
  if (!ndst)
  {
    HeapFree(GetProcessHeap(), 0, dst);
    return NULL;
  }
318
  ndst[len] = ',';
319
  memcpy(ndst+len+1, src, slen+1);
320 321 322 323 324 325 326
  return ndst;
}

static LPWSTR RPCRT4_strconcatW(LPWSTR dst, LPCWSTR src)
{
  DWORD len = strlenW(dst), slen = strlenW(src);
  LPWSTR ndst = HeapReAlloc(GetProcessHeap(), 0, dst, (len+slen+2)*sizeof(WCHAR));
327 328 329 330 331
  if (!ndst) 
  {
    HeapFree(GetProcessHeap(), 0, dst);
    return NULL;
  }
332
  ndst[len] = ',';
333
  memcpy(ndst+len+1, src, (slen+1)*sizeof(WCHAR));
334 335 336
  return ndst;
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/* Copies the escaped version of a component into a string binding.
 * Note: doesn't nul-terminate the string */
static RPC_CSTR escape_string_binding_component(RPC_CSTR string_binding,
                                                const unsigned char *component)
{
  for (; *component; component++) {
    switch (*component) {
      case '@':
      case ':':
      case '[':
      case ']':
      case '\\':
        *string_binding++ = '\\';
        *string_binding++ = *component;
        break;
      default:
        *string_binding++ = *component;
        break;
    }
  }
  return string_binding;
}

static RPC_WSTR escape_string_binding_componentW(RPC_WSTR string_binding,
                                                 const WCHAR *component)
{
  for (; *component; component++) {
    switch (*component) {
      case '@':
      case ':':
      case '[':
      case ']':
      case '\\':
        *string_binding++ = '\\';
        *string_binding++ = *component;
        break;
      default:
        *string_binding++ = *component;
        break;
    }
  }
  return string_binding;
}

static const unsigned char *string_binding_find_delimiter(
    const unsigned char *string_binding, unsigned char delim)
{
  const unsigned char *next;
  for (next = string_binding; *next; next++) {
    if (*next == '\\') {
      next++;
      continue;
    }
    if (*next == delim)
      return next;
  }
  return NULL;
}

static const WCHAR *string_binding_find_delimiterW(
    const WCHAR *string_binding, WCHAR delim)
{
  const WCHAR *next;
  for (next = string_binding; *next; next++) {
    if (*next == '\\') {
      next++;
      continue;
    }
    if (*next == delim)
      return next;
  }
  return NULL;
}

static RPC_CSTR unescape_string_binding_component(
    const unsigned char *string_binding, int len)
{
  RPC_CSTR component, p;

  if (len == -1) len = strlen((const char *)string_binding);

  component = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(*component));
  if (!component) return NULL;
  for (p = component; len > 0; string_binding++, len--) {
    if (*string_binding == '\\') {
      string_binding++;
      len--;
      *p++ = *string_binding;
    } else {
      *p++ = *string_binding;
    }
  }
  *p = '\0';
  return component;
}

static RPC_WSTR unescape_string_binding_componentW(
    const WCHAR *string_binding, int len)
{
  RPC_WSTR component, p;

438
  if (len == -1) len = strlenW(string_binding);
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

  component = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(*component));
  if (!component) return NULL;
  for (p = component; len > 0; string_binding++, len--) {
    if (*string_binding == '\\') {
      string_binding++;
      len--;
      *p++ = *string_binding;
    } else {
      *p++ = *string_binding;
    }
  }
  *p = '\0';
  return component;
}
454 455 456 457

/***********************************************************************
 *             RpcStringBindingComposeA (RPCRT4.@)
 */
458 459 460
RPC_STATUS WINAPI RpcStringBindingComposeA(RPC_CSTR ObjUuid, RPC_CSTR Protseq,
                                           RPC_CSTR NetworkAddr, RPC_CSTR Endpoint,
                                           RPC_CSTR Options, RPC_CSTR *StringBinding )
461 462
{
  DWORD len = 1;
463
  RPC_CSTR data;
464 465

  TRACE( "(%s,%s,%s,%s,%s,%p)\n",
Mike McCormack's avatar
Mike McCormack committed
466 467 468
        debugstr_a( (char*)ObjUuid ), debugstr_a( (char*)Protseq ),
        debugstr_a( (char*)NetworkAddr ), debugstr_a( (char*)Endpoint ),
        debugstr_a( (char*)Options ), StringBinding );
469

470 471 472 473 474 475
  /* overestimate for each component for escaping of delimiters */
  if (ObjUuid && *ObjUuid) len += strlen((char*)ObjUuid) * 2 + 1;
  if (Protseq && *Protseq) len += strlen((char*)Protseq) * 2 + 1;
  if (NetworkAddr && *NetworkAddr) len += strlen((char*)NetworkAddr) * 2;
  if (Endpoint && *Endpoint) len += strlen((char*)Endpoint) * 2 + 2;
  if (Options && *Options) len += strlen((char*)Options) * 2 + 2;
476 477

  data = HeapAlloc(GetProcessHeap(), 0, len);
478
  *StringBinding = data;
479 480

  if (ObjUuid && *ObjUuid) {
481
    data = escape_string_binding_component(data, ObjUuid);
482 483 484
    *data++ = '@';
  }
  if (Protseq && *Protseq) {
485
    data = escape_string_binding_component(data, Protseq);
486 487
    *data++ = ':';
  }
Greg Turner's avatar
Greg Turner committed
488
  if (NetworkAddr && *NetworkAddr)
489
    data = escape_string_binding_component(data, NetworkAddr);
Greg Turner's avatar
Greg Turner committed
490

491 492 493 494
  if ((Endpoint && *Endpoint) ||
      (Options && *Options)) {
    *data++ = '[';
    if (Endpoint && *Endpoint) {
495
      data = escape_string_binding_component(data, Endpoint);
496 497 498
      if (Options && *Options) *data++ = ',';
    }
    if (Options && *Options) {
499
      data = escape_string_binding_component(data, Options);
500 501 502 503 504 505 506 507 508 509 510
    }
    *data++ = ']';
  }
  *data = 0;

  return RPC_S_OK;
}

/***********************************************************************
 *             RpcStringBindingComposeW (RPCRT4.@)
 */
511 512 513
RPC_STATUS WINAPI RpcStringBindingComposeW( RPC_WSTR ObjUuid, RPC_WSTR Protseq,
                                            RPC_WSTR NetworkAddr, RPC_WSTR Endpoint,
                                            RPC_WSTR Options, RPC_WSTR* StringBinding )
514 515
{
  DWORD len = 1;
516
  RPC_WSTR data;
517 518 519 520 521 522

  TRACE("(%s,%s,%s,%s,%s,%p)\n",
       debugstr_w( ObjUuid ), debugstr_w( Protseq ),
       debugstr_w( NetworkAddr ), debugstr_w( Endpoint ),
       debugstr_w( Options ), StringBinding);

523 524 525 526 527 528
  /* overestimate for each component for escaping of delimiters */
  if (ObjUuid && *ObjUuid) len += strlenW(ObjUuid) * 2 + 1;
  if (Protseq && *Protseq) len += strlenW(Protseq) * 2 + 1;
  if (NetworkAddr && *NetworkAddr) len += strlenW(NetworkAddr) * 2;
  if (Endpoint && *Endpoint) len += strlenW(Endpoint) * 2 + 2;
  if (Options && *Options) len += strlenW(Options) * 2 + 2;
529 530 531 532 533

  data = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
  *StringBinding = data;

  if (ObjUuid && *ObjUuid) {
534
    data = escape_string_binding_componentW(data, ObjUuid);
535 536 537
    *data++ = '@';
  }
  if (Protseq && *Protseq) {
538
    data = escape_string_binding_componentW(data, Protseq);
539 540 541
    *data++ = ':';
  }
  if (NetworkAddr && *NetworkAddr) {
542
    data = escape_string_binding_componentW(data, NetworkAddr);
543 544 545 546 547
  }
  if ((Endpoint && *Endpoint) ||
      (Options && *Options)) {
    *data++ = '[';
    if (Endpoint && *Endpoint) {
548
      data = escape_string_binding_componentW(data, Endpoint);
549 550 551
      if (Options && *Options) *data++ = ',';
    }
    if (Options && *Options) {
552
      data = escape_string_binding_componentW(data, Options);
553 554 555 556 557 558 559 560 561 562 563 564
    }
    *data++ = ']';
  }
  *data = 0;

  return RPC_S_OK;
}


/***********************************************************************
 *             RpcStringBindingParseA (RPCRT4.@)
 */
565 566 567
RPC_STATUS WINAPI RpcStringBindingParseA( RPC_CSTR StringBinding, RPC_CSTR *ObjUuid,
                                          RPC_CSTR *Protseq, RPC_CSTR *NetworkAddr,
                                          RPC_CSTR *Endpoint, RPC_CSTR *Options)
568
{
569
  const unsigned char *data, *next;
570
  static const char ep_opt[] = "endpoint=";
571
  BOOL endpoint_already_found = FALSE;
572

Mike McCormack's avatar
Mike McCormack committed
573
  TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_a((char*)StringBinding),
574 575 576 577 578 579 580 581
       ObjUuid, Protseq, NetworkAddr, Endpoint, Options);

  if (ObjUuid) *ObjUuid = NULL;
  if (Protseq) *Protseq = NULL;
  if (NetworkAddr) *NetworkAddr = NULL;
  if (Endpoint) *Endpoint = NULL;
  if (Options) *Options = NULL;

582
  data = StringBinding;
583

584
  next = string_binding_find_delimiter(data, '@');
585
  if (next) {
586 587
    UUID uuid;
    RPC_STATUS status;
588
    RPC_CSTR str_uuid = unescape_string_binding_component(data, next - data);
589 590 591 592 593 594 595 596 597
    status = UuidFromStringA(str_uuid, &uuid);
    if (status != RPC_S_OK) {
      HeapFree(GetProcessHeap(), 0, str_uuid);
      return status;
    }
    if (ObjUuid)
      *ObjUuid = str_uuid;
    else
      HeapFree(GetProcessHeap(), 0, str_uuid);
598 599 600
    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 605 606
    data = next+1;
  }

607
  next = string_binding_find_delimiter(data, '[');
608
  if (next) {
609 610
    const unsigned char *close;
    RPC_CSTR opt;
611

612
    if (NetworkAddr) *NetworkAddr = unescape_string_binding_component(data, next - data);
613
    data = next+1;
614
    close = string_binding_find_delimiter(data, ']');
615 616 617 618
    if (!close) goto fail;

    /* tokenize options */
    while (data < close) {
619
      next = string_binding_find_delimiter(data, ',');
620 621
      if (!next || next > close) next = close;
      /* FIXME: this is kind of inefficient */
622
      opt = unescape_string_binding_component(data, next - data);
623 624 625
      data = next+1;

      /* parse option */
626
      next = string_binding_find_delimiter(opt, '=');
627
      if (!next) {
Greg Turner's avatar
Greg Turner committed
628
        /* not an option, must be an endpoint */
629
        if (endpoint_already_found) goto fail;
630
        if (Endpoint) *Endpoint = opt;
631 632
        else HeapFree(GetProcessHeap(), 0, opt);
        endpoint_already_found = TRUE;
Greg Turner's avatar
Greg Turner committed
633
      } else {
634
        if (strncmp((const char *)opt, ep_opt, strlen(ep_opt)) == 0) {
Greg Turner's avatar
Greg Turner committed
635
          /* endpoint option */
636
          if (endpoint_already_found) goto fail;
637
          if (Endpoint) *Endpoint = unescape_string_binding_component(next+1, -1);
Greg Turner's avatar
Greg Turner committed
638
          HeapFree(GetProcessHeap(), 0, opt);
639
          endpoint_already_found = TRUE;
Greg Turner's avatar
Greg Turner committed
640 641
        } else {
          /* network option */
642 643 644
          if (Options) {
            if (*Options) {
              /* FIXME: this is kind of inefficient */
645
              *Options = (unsigned char*) RPCRT4_strconcatA( (char*)*Options, (char *)opt);
646 647
              HeapFree(GetProcessHeap(), 0, opt);
            } else
648
              *Options = opt;
649
          } else
Greg Turner's avatar
Greg Turner committed
650 651
            HeapFree(GetProcessHeap(), 0, opt);
        }
652 653 654 655 656 657
      }
    }

    data = close+1;
    if (*data) goto fail;
  }
Greg Turner's avatar
Greg Turner committed
658
  else if (NetworkAddr) 
659
    *NetworkAddr = unescape_string_binding_component(data, -1);
Greg Turner's avatar
Greg Turner committed
660

661 662 663
  return RPC_S_OK;

fail:
664 665 666 667 668
  if (ObjUuid) RpcStringFreeA(ObjUuid);
  if (Protseq) RpcStringFreeA(Protseq);
  if (NetworkAddr) RpcStringFreeA(NetworkAddr);
  if (Endpoint) RpcStringFreeA(Endpoint);
  if (Options) RpcStringFreeA(Options);
669 670 671 672 673 674
  return RPC_S_INVALID_STRING_BINDING;
}

/***********************************************************************
 *             RpcStringBindingParseW (RPCRT4.@)
 */
675 676 677
RPC_STATUS WINAPI RpcStringBindingParseW( RPC_WSTR StringBinding, RPC_WSTR *ObjUuid,
                                          RPC_WSTR *Protseq, RPC_WSTR *NetworkAddr,
                                          RPC_WSTR *Endpoint, RPC_WSTR *Options)
678
{
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;
682 683 684 685 686 687 688 689 690 691 692 693

  TRACE("(%s,%p,%p,%p,%p,%p)\n", debugstr_w(StringBinding),
       ObjUuid, Protseq, NetworkAddr, Endpoint, Options);

  if (ObjUuid) *ObjUuid = NULL;
  if (Protseq) *Protseq = NULL;
  if (NetworkAddr) *NetworkAddr = NULL;
  if (Endpoint) *Endpoint = NULL;
  if (Options) *Options = NULL;

  data = StringBinding;

694
  next = string_binding_find_delimiterW(data, '@');
695
  if (next) {
696 697
    UUID uuid;
    RPC_STATUS status;
698
    RPC_WSTR str_uuid = unescape_string_binding_componentW(data, next - data);
699 700 701 702 703 704 705 706 707
    status = UuidFromStringW(str_uuid, &uuid);
    if (status != RPC_S_OK) {
      HeapFree(GetProcessHeap(), 0, str_uuid);
      return status;
    }
    if (ObjUuid)
      *ObjUuid = str_uuid;
    else
      HeapFree(GetProcessHeap(), 0, str_uuid);
708 709 710
    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 715 716
    data = next+1;
  }

717
  next = string_binding_find_delimiterW(data, '[');
718
  if (next) {
719 720
    const WCHAR *close;
    RPC_WSTR opt;
721

722
    if (NetworkAddr) *NetworkAddr = unescape_string_binding_componentW(data, next - data);
723
    data = next+1;
724
    close = string_binding_find_delimiterW(data, ']');
725 726 727 728
    if (!close) goto fail;

    /* tokenize options */
    while (data < close) {
729
      next = string_binding_find_delimiterW(data, ',');
730 731
      if (!next || next > close) next = close;
      /* FIXME: this is kind of inefficient */
732
      opt = unescape_string_binding_componentW(data, next - data);
733 734 735
      data = next+1;

      /* parse option */
736
      next = string_binding_find_delimiterW(opt, '=');
737
      if (!next) {
Greg Turner's avatar
Greg Turner committed
738
        /* not an option, must be an endpoint */
739 740 741 742
        if (endpoint_already_found) goto fail;
        if (Endpoint) *Endpoint = opt;
        else HeapFree(GetProcessHeap(), 0, opt);
        endpoint_already_found = TRUE;
Greg Turner's avatar
Greg Turner committed
743 744 745
      } else {
        if (strncmpW(opt, ep_opt, strlenW(ep_opt)) == 0) {
          /* endpoint option */
746
          if (endpoint_already_found) goto fail;
747
          if (Endpoint) *Endpoint = unescape_string_binding_componentW(next+1, -1);
Greg Turner's avatar
Greg Turner committed
748
          HeapFree(GetProcessHeap(), 0, opt);
749
          endpoint_already_found = TRUE;
Greg Turner's avatar
Greg Turner committed
750 751
        } else {
          /* network option */
752 753 754 755 756 757 758 759
          if (Options) {
            if (*Options) {
              /* FIXME: this is kind of inefficient */
              *Options = RPCRT4_strconcatW(*Options, opt);
              HeapFree(GetProcessHeap(), 0, opt);
            } else
              *Options = opt;
          } else
Greg Turner's avatar
Greg Turner committed
760 761
            HeapFree(GetProcessHeap(), 0, opt);
        }
762 763 764 765 766
      }
    }

    data = close+1;
    if (*data) goto fail;
Greg Turner's avatar
Greg Turner committed
767
  } else if (NetworkAddr) 
768
    *NetworkAddr = unescape_string_binding_componentW(data, -1);
769 770

  return RPC_S_OK;
Greg Turner's avatar
Greg Turner committed
771

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
fail:
  if (ObjUuid) RpcStringFreeW(ObjUuid);
  if (Protseq) RpcStringFreeW(Protseq);
  if (NetworkAddr) RpcStringFreeW(NetworkAddr);
  if (Endpoint) RpcStringFreeW(Endpoint);
  if (Options) RpcStringFreeW(Options);
  return RPC_S_INVALID_STRING_BINDING;
}

/***********************************************************************
 *             RpcBindingFree (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcBindingFree( RPC_BINDING_HANDLE* Binding )
{
  RPC_STATUS status;
  TRACE("(%p) = %p\n", Binding, *Binding);
788
  status = RPCRT4_ReleaseBinding(*Binding);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
  if (status == RPC_S_OK) *Binding = 0;
  return status;
}
  
/***********************************************************************
 *             RpcBindingVectorFree (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcBindingVectorFree( RPC_BINDING_VECTOR** BindingVector )
{
  RPC_STATUS status;
  unsigned long c;

  TRACE("(%p)\n", BindingVector);
  for (c=0; c<(*BindingVector)->Count; c++) {
    status = RpcBindingFree(&(*BindingVector)->BindingH[c]);
  }
  HeapFree(GetProcessHeap(), 0, *BindingVector);
  *BindingVector = NULL;
  return RPC_S_OK;
}
  
/***********************************************************************
 *             RpcBindingInqObject (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcBindingInqObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
{
815
  RpcBinding* bind = Binding;
816 817

  TRACE("(%p,%p) = %s\n", Binding, ObjectUuid, debugstr_guid(&bind->ObjectUuid));
818
  *ObjectUuid = bind->ObjectUuid;
819 820
  return RPC_S_OK;
}
821

822 823 824 825 826
/***********************************************************************
 *             RpcBindingSetObject (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcBindingSetObject( RPC_BINDING_HANDLE Binding, UUID* ObjectUuid )
{
827
  RpcBinding* bind = Binding;
828 829 830 831 832 833 834 835 836

  TRACE("(%p,%s)\n", Binding, debugstr_guid(ObjectUuid));
  if (bind->server) return RPC_S_WRONG_KIND_OF_BINDING;
  return RPCRT4_SetBindingObject(Binding, ObjectUuid);
}

/***********************************************************************
 *             RpcBindingFromStringBindingA (RPCRT4.@)
 */
837
RPC_STATUS WINAPI RpcBindingFromStringBindingA( RPC_CSTR StringBinding, RPC_BINDING_HANDLE* Binding )
838 839 840
{
  RPC_STATUS ret;
  RpcBinding* bind = NULL;
841
  RPC_CSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
842 843
  UUID Uuid;

Mike McCormack's avatar
Mike McCormack committed
844
  TRACE("(%s,%p)\n", debugstr_a((char*)StringBinding), Binding);
845 846 847 848 849 850 851 852

  ret = RpcStringBindingParseA(StringBinding, &ObjectUuid, &Protseq,
                              &NetworkAddr, &Endpoint, &Options);
  if (ret != RPC_S_OK) return ret;

  ret = UuidFromStringA(ObjectUuid, &Uuid);

  if (ret == RPC_S_OK)
Mike McCormack's avatar
Mike McCormack committed
853
    ret = RPCRT4_CreateBindingA(&bind, FALSE, (char*)Protseq);
854 855
  if (ret != RPC_S_OK) return ret;
  ret = RPCRT4_SetBindingObject(bind, &Uuid);
856
  if (ret == RPC_S_OK)
Mike McCormack's avatar
Mike McCormack committed
857
    ret = RPCRT4_CompleteBindingA(bind, (char*)NetworkAddr, (char*)Endpoint, (char*)Options);
858

859 860 861 862 863
  RpcStringFreeA(&Options);
  RpcStringFreeA(&Endpoint);
  RpcStringFreeA(&NetworkAddr);
  RpcStringFreeA(&Protseq);
  RpcStringFreeA(&ObjectUuid);
864

Greg Turner's avatar
Greg Turner committed
865 866 867
  if (ret == RPC_S_OK) 
    *Binding = (RPC_BINDING_HANDLE)bind;
  else 
868
    RPCRT4_ReleaseBinding(bind);
869 870 871 872 873 874 875

  return ret;
}

/***********************************************************************
 *             RpcBindingFromStringBindingW (RPCRT4.@)
 */
876
RPC_STATUS WINAPI RpcBindingFromStringBindingW( RPC_WSTR StringBinding, RPC_BINDING_HANDLE* Binding )
877 878 879
{
  RPC_STATUS ret;
  RpcBinding* bind = NULL;
880
  RPC_WSTR ObjectUuid, Protseq, NetworkAddr, Endpoint, Options;
881 882 883 884 885 886 887 888 889 890 891 892
  UUID Uuid;

  TRACE("(%s,%p)\n", debugstr_w(StringBinding), Binding);

  ret = RpcStringBindingParseW(StringBinding, &ObjectUuid, &Protseq,
                              &NetworkAddr, &Endpoint, &Options);
  if (ret != RPC_S_OK) return ret;

  ret = UuidFromStringW(ObjectUuid, &Uuid);

  if (ret == RPC_S_OK)
    ret = RPCRT4_CreateBindingW(&bind, FALSE, Protseq);
893 894
  if (ret != RPC_S_OK) return ret;
  ret = RPCRT4_SetBindingObject(bind, &Uuid);
895 896 897 898 899 900 901 902 903
  if (ret == RPC_S_OK)
    ret = RPCRT4_CompleteBindingW(bind, NetworkAddr, Endpoint, Options);

  RpcStringFreeW(&Options);
  RpcStringFreeW(&Endpoint);
  RpcStringFreeW(&NetworkAddr);
  RpcStringFreeW(&Protseq);
  RpcStringFreeW(&ObjectUuid);

Greg Turner's avatar
Greg Turner committed
904 905 906
  if (ret == RPC_S_OK)
    *Binding = (RPC_BINDING_HANDLE)bind;
  else
907
    RPCRT4_ReleaseBinding(bind);
908 909 910 911 912 913 914

  return ret;
}
  
/***********************************************************************
 *             RpcBindingToStringBindingA (RPCRT4.@)
 */
915
RPC_STATUS WINAPI RpcBindingToStringBindingA( RPC_BINDING_HANDLE Binding, RPC_CSTR *StringBinding )
916 917
{
  RPC_STATUS ret;
918
  RpcBinding* bind = Binding;
919
  RPC_CSTR ObjectUuid;
920 921 922

  TRACE("(%p,%p)\n", Binding, StringBinding);

923 924 925 926 927 928 929
  if (UuidIsNil(&bind->ObjectUuid, &ret))
    ObjectUuid = NULL;
  else
  {
    ret = UuidToStringA(&bind->ObjectUuid, &ObjectUuid);
    if (ret != RPC_S_OK) return ret;
  }
930

931
  ret = RpcStringBindingComposeA(ObjectUuid, (unsigned char*)bind->Protseq, (unsigned char*) bind->NetworkAddr,
Mike McCormack's avatar
Mike McCormack committed
932
                                 (unsigned char*) bind->Endpoint, NULL, StringBinding);
933

934
  RpcStringFreeA(&ObjectUuid);
935 936 937 938 939 940 941

  return ret;
}
  
/***********************************************************************
 *             RpcBindingToStringBindingW (RPCRT4.@)
 */
942
RPC_STATUS WINAPI RpcBindingToStringBindingW( RPC_BINDING_HANDLE Binding, RPC_WSTR *StringBinding )
943 944
{
  RPC_STATUS ret;
945
  unsigned char *str = NULL;
946 947
  TRACE("(%p,%p)\n", Binding, StringBinding);
  ret = RpcBindingToStringBindingA(Binding, &str);
Mike McCormack's avatar
Mike McCormack committed
948
  *StringBinding = RPCRT4_strdupAtoW((char*)str);
949
  RpcStringFreeA(&str);
950 951
  return ret;
}
952

953 954 955 956 957 958 959 960 961 962 963
/***********************************************************************
 *             I_RpcBindingInqTransportType (RPCRT4.@)
 */
RPC_STATUS WINAPI I_RpcBindingInqTransportType( RPC_BINDING_HANDLE Binding, unsigned int * Type )
{

  FIXME( "(%p,%p): stub\n", Binding, Type);
  *Type = TRANSPORT_TYPE_LPC;
  return RPC_S_OK;
}

964 965 966 967 968 969
/***********************************************************************
 *             I_RpcBindingSetAsync (RPCRT4.@)
 * NOTES
 *  Exists in win9x and winNT, but with different number of arguments
 *  (9x version has 3 arguments, NT has 2).
 */
970
RPC_STATUS WINAPI I_RpcBindingSetAsync( RPC_BINDING_HANDLE Binding, RPC_BLOCKING_FN BlockingFn)
971
{
972
  RpcBinding* bind = Binding;
973

974
  TRACE( "(%p,%p): stub\n", Binding, BlockingFn );
975 976

  bind->BlockingFn = BlockingFn;
977

978 979
  return RPC_S_OK;
}
980

981 982 983 984 985 986 987 988
/***********************************************************************
 *             RpcBindingCopy (RPCRT4.@)
 */
RPC_STATUS RPC_ENTRY RpcBindingCopy(
  RPC_BINDING_HANDLE SourceBinding,
  RPC_BINDING_HANDLE* DestinationBinding)
{
  RpcBinding *DestBinding;
989
  RpcBinding *SrcBinding = SourceBinding;
990 991 992 993 994 995 996 997 998 999 1000 1001
  RPC_STATUS status;

  TRACE("(%p, %p)\n", SourceBinding, DestinationBinding);

  status = RPCRT4_AllocBinding(&DestBinding, SrcBinding->server);
  if (status != RPC_S_OK) return status;

  DestBinding->ObjectUuid = SrcBinding->ObjectUuid;
  DestBinding->BlockingFn = SrcBinding->BlockingFn;
  DestBinding->Protseq = RPCRT4_strndupA(SrcBinding->Protseq, -1);
  DestBinding->NetworkAddr = RPCRT4_strndupA(SrcBinding->NetworkAddr, -1);
  DestBinding->Endpoint = RPCRT4_strndupA(SrcBinding->Endpoint, -1);
1002
  DestBinding->NetworkOptions = RPCRT4_strdupW(SrcBinding->NetworkOptions);
1003 1004
  if (SrcBinding->Assoc) SrcBinding->Assoc->refs++;
  DestBinding->Assoc = SrcBinding->Assoc;
1005 1006 1007

  if (SrcBinding->AuthInfo) RpcAuthInfo_AddRef(SrcBinding->AuthInfo);
  DestBinding->AuthInfo = SrcBinding->AuthInfo;
1008 1009
  if (SrcBinding->QOS) RpcQualityOfService_AddRef(SrcBinding->QOS);
  DestBinding->QOS = SrcBinding->QOS;
1010 1011 1012 1013 1014

  *DestinationBinding = DestBinding;
  return RPC_S_OK;
}

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
/***********************************************************************
 *             RpcBindingReset (RPCRT4.@)
 */
RPC_STATUS RPC_ENTRY RpcBindingReset(RPC_BINDING_HANDLE Binding)
{
    RpcBinding *bind = Binding;

    TRACE("(%p)\n", Binding);

    RPCRT4_strfree(bind->Endpoint);
    bind->Endpoint = NULL;
    if (bind->Assoc) RpcAssoc_Release(bind->Assoc);
    bind->Assoc = NULL;

    return RPC_S_OK;
}

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
/***********************************************************************
 *             RpcImpersonateClient (RPCRT4.@)
 *
 * Impersonates the client connected via a binding handle so that security
 * checks are done in the context of the client.
 *
 * PARAMS
 *  BindingHandle [I] Handle to the binding to the client.
 *
 * RETURNS
 *  Success: RPS_S_OK.
 *  Failure: RPC_STATUS value.
1044 1045 1046 1047 1048
 *
 * NOTES
 *
 * If BindingHandle is NULL then the function impersonates the client
 * connected to the binding handle of the current thread.
1049 1050 1051 1052
 */
RPC_STATUS WINAPI RpcImpersonateClient(RPC_BINDING_HANDLE BindingHandle)
{
    FIXME("(%p): stub\n", BindingHandle);
1053
    ImpersonateSelf(SecurityImpersonation);
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    return RPC_S_OK;
}

/***********************************************************************
 *             RpcRevertToSelfEx (RPCRT4.@)
 *
 * Stops impersonating the client connected to the binding handle so that security
 * checks are no longer done in the context of the client.
 *
 * PARAMS
 *  BindingHandle [I] Handle to the binding to the client.
 *
 * RETURNS
 *  Success: RPS_S_OK.
 *  Failure: RPC_STATUS value.
 *
 * NOTES
 *
 * If BindingHandle is NULL then the function stops impersonating the client
 * connected to the binding handle of the current thread.
 */
RPC_STATUS WINAPI RpcRevertToSelfEx(RPC_BINDING_HANDLE BindingHandle)
{
    FIXME("(%p): stub\n", BindingHandle);
    return RPC_S_OK;
1079
}
1080

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
static inline BOOL has_nt_auth_identity(ULONG AuthnLevel)
{
    switch (AuthnLevel)
    {
    case RPC_C_AUTHN_GSS_NEGOTIATE:
    case RPC_C_AUTHN_WINNT:
    case RPC_C_AUTHN_GSS_KERBEROS:
        return TRUE;
    default:
        return FALSE;
    }
}

1094 1095
static RPC_STATUS RpcAuthInfo_Create(ULONG AuthnLevel, ULONG AuthnSvc,
                                     CredHandle cred, TimeStamp exp,
1096 1097 1098
                                     ULONG cbMaxToken,
                                     RPC_AUTH_IDENTITY_HANDLE identity,
                                     RpcAuthInfo **ret)
1099 1100 1101 1102 1103
{
    RpcAuthInfo *AuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo));
    if (!AuthInfo)
        return ERROR_OUTOFMEMORY;

1104
    AuthInfo->refs = 1;
1105 1106 1107 1108
    AuthInfo->AuthnLevel = AuthnLevel;
    AuthInfo->AuthnSvc = AuthnSvc;
    AuthInfo->cred = cred;
    AuthInfo->exp = exp;
1109
    AuthInfo->cbMaxToken = cbMaxToken;
1110
    AuthInfo->identity = identity;
1111
    AuthInfo->server_principal_name = NULL;
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

    /* duplicate the SEC_WINNT_AUTH_IDENTITY structure, if applicable, to
     * enable better matching in RpcAuthInfo_IsEqual */
    if (identity && has_nt_auth_identity(AuthnSvc))
    {
        const SEC_WINNT_AUTH_IDENTITY_W *nt_identity = identity;
        AuthInfo->nt_identity = HeapAlloc(GetProcessHeap(), 0, sizeof(*AuthInfo->nt_identity));
        if (!AuthInfo->nt_identity)
        {
            HeapFree(GetProcessHeap(), 0, AuthInfo);
            return ERROR_OUTOFMEMORY;
        }

        AuthInfo->nt_identity->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
        if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
            AuthInfo->nt_identity->User = RPCRT4_strndupW(nt_identity->User, nt_identity->UserLength);
        else
            AuthInfo->nt_identity->User = RPCRT4_strndupAtoW((const char *)nt_identity->User, nt_identity->UserLength);
        AuthInfo->nt_identity->UserLength = nt_identity->UserLength;
        if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
            AuthInfo->nt_identity->Domain = RPCRT4_strndupW(nt_identity->Domain, nt_identity->DomainLength);
        else
            AuthInfo->nt_identity->Domain = RPCRT4_strndupAtoW((const char *)nt_identity->Domain, nt_identity->DomainLength);
        AuthInfo->nt_identity->DomainLength = nt_identity->DomainLength;
        if (nt_identity->Flags & SEC_WINNT_AUTH_IDENTITY_UNICODE)
            AuthInfo->nt_identity->Password = RPCRT4_strndupW(nt_identity->Password, nt_identity->PasswordLength);
        else
            AuthInfo->nt_identity->Password = RPCRT4_strndupAtoW((const char *)nt_identity->Password, nt_identity->PasswordLength);
        AuthInfo->nt_identity->PasswordLength = nt_identity->PasswordLength;

1142 1143 1144
        if ((nt_identity->User && !AuthInfo->nt_identity->User) ||
            (nt_identity->Domain && !AuthInfo->nt_identity->Domain) ||
            (nt_identity->Password && !AuthInfo->nt_identity->Password))
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
        {
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
            HeapFree(GetProcessHeap(), 0, AuthInfo);
            return ERROR_OUTOFMEMORY;
        }
    }
    else
        AuthInfo->nt_identity = NULL;
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    *ret = AuthInfo;
    return RPC_S_OK;
}

ULONG RpcAuthInfo_AddRef(RpcAuthInfo *AuthInfo)
{
    return InterlockedIncrement(&AuthInfo->refs);
}

ULONG RpcAuthInfo_Release(RpcAuthInfo *AuthInfo)
{
    ULONG refs = InterlockedDecrement(&AuthInfo->refs);

    if (!refs)
    {
        FreeCredentialsHandle(&AuthInfo->cred);
1172 1173 1174 1175
        if (AuthInfo->nt_identity)
        {
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->User);
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Domain);
1176
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity->Password);
1177 1178
            HeapFree(GetProcessHeap(), 0, AuthInfo->nt_identity);
        }
1179
        HeapFree(GetProcessHeap(), 0, AuthInfo->server_principal_name);
1180 1181 1182 1183 1184 1185
        HeapFree(GetProcessHeap(), 0, AuthInfo);
    }

    return refs;
}

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
BOOL RpcAuthInfo_IsEqual(const RpcAuthInfo *AuthInfo1, const RpcAuthInfo *AuthInfo2)
{
    if (AuthInfo1 == AuthInfo2)
        return TRUE;

    if (!AuthInfo1 || !AuthInfo2)
        return FALSE;

    if ((AuthInfo1->AuthnLevel != AuthInfo2->AuthnLevel) ||
        (AuthInfo1->AuthnSvc != AuthInfo2->AuthnSvc))
        return FALSE;

    if (AuthInfo1->identity == AuthInfo2->identity)
        return TRUE;

    if (!AuthInfo1->identity || !AuthInfo2->identity)
        return FALSE;

    if (has_nt_auth_identity(AuthInfo1->AuthnSvc))
    {
        const SEC_WINNT_AUTH_IDENTITY_W *identity1 = AuthInfo1->nt_identity;
        const SEC_WINNT_AUTH_IDENTITY_W *identity2 = AuthInfo2->nt_identity;
        /* compare user names */
        if (identity1->UserLength != identity2->UserLength ||
            memcmp(identity1->User, identity2->User, identity1->UserLength))
            return FALSE;
        /* compare domain names */
        if (identity1->DomainLength != identity2->DomainLength ||
            memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
            return FALSE;
        /* compare passwords */
        if (identity1->PasswordLength != identity2->PasswordLength ||
            memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
            return FALSE;
    }
    else
        return FALSE;

    return TRUE;
}

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
static RPC_STATUS RpcQualityOfService_Create(const RPC_SECURITY_QOS *qos_src, BOOL unicode, RpcQualityOfService **qos_dst)
{
    RpcQualityOfService *qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos));

    if (!qos)
        return RPC_S_OUT_OF_RESOURCES;

    qos->refs = 1;
    qos->qos = HeapAlloc(GetProcessHeap(), 0, sizeof(*qos->qos));
    if (!qos->qos) goto error;
    qos->qos->Version = qos_src->Version;
    qos->qos->Capabilities = qos_src->Capabilities;
    qos->qos->IdentityTracking = qos_src->IdentityTracking;
    qos->qos->ImpersonationType = qos_src->ImpersonationType;
    qos->qos->AdditionalSecurityInfoType = 0;

    if (qos_src->Version >= 2)
    {
        const RPC_SECURITY_QOS_V2_W *qos_src2 = (const RPC_SECURITY_QOS_V2_W *)qos_src;
        qos->qos->AdditionalSecurityInfoType = qos_src2->AdditionalSecurityInfoType;
        if (qos_src2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
        {
            const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_src = qos_src2->u.HttpCredentials;
            RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials_dst;

            http_credentials_dst = HeapAlloc(GetProcessHeap(), 0, sizeof(*http_credentials_dst));
            qos->qos->u.HttpCredentials = http_credentials_dst;
            if (!http_credentials_dst) goto error;
1255
            http_credentials_dst->TransportCredentials = NULL;
1256 1257 1258 1259 1260
            http_credentials_dst->Flags = http_credentials_src->Flags;
            http_credentials_dst->AuthenticationTarget = http_credentials_src->AuthenticationTarget;
            http_credentials_dst->NumberOfAuthnSchemes = http_credentials_src->NumberOfAuthnSchemes;
            http_credentials_dst->AuthnSchemes = NULL;
            http_credentials_dst->ServerCertificateSubject = NULL;
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
            if (http_credentials_src->TransportCredentials)
            {
                SEC_WINNT_AUTH_IDENTITY_W *cred_dst;
                cred_dst = http_credentials_dst->TransportCredentials = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cred_dst));
                if (!cred_dst) goto error;
                cred_dst->Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
                if (unicode)
                {
                    const SEC_WINNT_AUTH_IDENTITY_W *cred_src = http_credentials_src->TransportCredentials;
                    cred_dst->UserLength = cred_src->UserLength;
                    cred_dst->PasswordLength = cred_src->PasswordLength;
                    cred_dst->DomainLength = cred_src->DomainLength;
                    cred_dst->User = RPCRT4_strndupW(cred_src->User, cred_src->UserLength);
                    cred_dst->Password = RPCRT4_strndupW(cred_src->Password, cred_src->PasswordLength);
                    cred_dst->Domain = RPCRT4_strndupW(cred_src->Domain, cred_src->DomainLength);
                }
                else
                {
                    const SEC_WINNT_AUTH_IDENTITY_A *cred_src = (const SEC_WINNT_AUTH_IDENTITY_A *)http_credentials_src->TransportCredentials;
                    cred_dst->UserLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, NULL, 0);
                    cred_dst->DomainLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, NULL, 0);
                    cred_dst->PasswordLength = MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, NULL, 0);
                    cred_dst->User = HeapAlloc(GetProcessHeap(), 0, cred_dst->UserLength * sizeof(WCHAR));
                    cred_dst->Password = HeapAlloc(GetProcessHeap(), 0, cred_dst->PasswordLength * sizeof(WCHAR));
                    cred_dst->Domain = HeapAlloc(GetProcessHeap(), 0, cred_dst->DomainLength * sizeof(WCHAR));
                    if (!cred_dst || !cred_dst->Password || !cred_dst->Domain) goto error;
                    MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->User, cred_src->UserLength, cred_dst->User, cred_dst->UserLength);
                    MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Domain, cred_src->DomainLength, cred_dst->Domain, cred_dst->DomainLength);
                    MultiByteToWideChar(CP_ACP, 0, (char *)cred_src->Password, cred_src->PasswordLength, cred_dst->Password, cred_dst->PasswordLength);
                }
            }
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
            if (http_credentials_src->NumberOfAuthnSchemes)
            {
                http_credentials_dst->AuthnSchemes = HeapAlloc(GetProcessHeap(), 0, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
                if (!http_credentials_dst->AuthnSchemes) goto error;
                memcpy(http_credentials_dst->AuthnSchemes, http_credentials_src->AuthnSchemes, http_credentials_src->NumberOfAuthnSchemes * sizeof(*http_credentials_dst->AuthnSchemes));
            }
            if (http_credentials_src->ServerCertificateSubject)
            {
                if (unicode)
                    http_credentials_dst->ServerCertificateSubject =
                        RPCRT4_strndupW(http_credentials_src->ServerCertificateSubject,
                                        strlenW(http_credentials_src->ServerCertificateSubject));
                else
                    http_credentials_dst->ServerCertificateSubject =
                        RPCRT4_strdupAtoW((char *)http_credentials_src->ServerCertificateSubject);
                if (!http_credentials_dst->ServerCertificateSubject) goto error;
            }
        }
    }
    *qos_dst = qos;
    return RPC_S_OK;

error:
    if (qos->qos)
    {
        if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP &&
            qos->qos->u.HttpCredentials)
        {
1320 1321 1322 1323 1324 1325 1326
            if (qos->qos->u.HttpCredentials->TransportCredentials)
            {
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
            }
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
            HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
            HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
            HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
        }
        HeapFree(GetProcessHeap(), 0, qos->qos);
    }
    HeapFree(GetProcessHeap(), 0, qos);
    return RPC_S_OUT_OF_RESOURCES;
}

ULONG RpcQualityOfService_AddRef(RpcQualityOfService *qos)
{
    return InterlockedIncrement(&qos->refs);
}

ULONG RpcQualityOfService_Release(RpcQualityOfService *qos)
{
    ULONG refs = InterlockedDecrement(&qos->refs);

    if (!refs)
    {
        if (qos->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
        {
1350 1351 1352 1353 1354 1355 1356
            if (qos->qos->u.HttpCredentials->TransportCredentials)
            {
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->User);
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Domain);
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials->Password);
                HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->TransportCredentials);
            }
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
            HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->AuthnSchemes);
            HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials->ServerCertificateSubject);
            HeapFree(GetProcessHeap(), 0, qos->qos->u.HttpCredentials);
        }
        HeapFree(GetProcessHeap(), 0, qos->qos);
        HeapFree(GetProcessHeap(), 0, qos);
    }
    return refs;
}

1367 1368 1369 1370 1371 1372 1373 1374
BOOL RpcQualityOfService_IsEqual(const RpcQualityOfService *qos1, const RpcQualityOfService *qos2)
{
    if (qos1 == qos2)
        return TRUE;

    if (!qos1 || !qos2)
        return FALSE;

1375
    TRACE("qos1 = { %d %d %d %d }, qos2 = { %d %d %d %d }\n",
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
        qos1->qos->Capabilities, qos1->qos->IdentityTracking,
        qos1->qos->ImpersonationType, qos1->qos->AdditionalSecurityInfoType,
        qos2->qos->Capabilities, qos2->qos->IdentityTracking,
        qos2->qos->ImpersonationType, qos2->qos->AdditionalSecurityInfoType);

    if ((qos1->qos->Capabilities != qos2->qos->Capabilities) ||
        (qos1->qos->IdentityTracking != qos2->qos->IdentityTracking) ||
        (qos1->qos->ImpersonationType != qos2->qos->ImpersonationType) ||
        (qos1->qos->AdditionalSecurityInfoType != qos2->qos->AdditionalSecurityInfoType))
        return FALSE;

    if (qos1->qos->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
    {
        const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials1 = qos1->qos->u.HttpCredentials;
        const RPC_HTTP_TRANSPORT_CREDENTIALS_W *http_credentials2 = qos2->qos->u.HttpCredentials;

        if (http_credentials1->Flags != http_credentials2->Flags)
            return FALSE;

        if (http_credentials1->AuthenticationTarget != http_credentials2->AuthenticationTarget)
            return FALSE;

        /* authentication schemes and server certificate subject not currently used */

        if (http_credentials1->TransportCredentials != http_credentials2->TransportCredentials)
        {
            const SEC_WINNT_AUTH_IDENTITY_W *identity1 = http_credentials1->TransportCredentials;
            const SEC_WINNT_AUTH_IDENTITY_W *identity2 = http_credentials2->TransportCredentials;

            if (!identity1 || !identity2)
                return FALSE;

            /* compare user names */
            if (identity1->UserLength != identity2->UserLength ||
                memcmp(identity1->User, identity2->User, identity1->UserLength))
                return FALSE;
            /* compare domain names */
            if (identity1->DomainLength != identity2->DomainLength ||
                memcmp(identity1->Domain, identity2->Domain, identity1->DomainLength))
                return FALSE;
            /* compare passwords */
            if (identity1->PasswordLength != identity2->PasswordLength ||
                memcmp(identity1->Password, identity2->Password, identity1->PasswordLength))
                return FALSE;
        }
    }

    return TRUE;
}

1426 1427 1428 1429 1430 1431
/***********************************************************************
 *             RpcRevertToSelf (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcRevertToSelf(void)
{
    FIXME("stub\n");
1432
    RevertToSelf();
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    return RPC_S_OK;
}

/***********************************************************************
 *             RpcMgmtSetComTimeout (RPCRT4.@)
 */
RPC_STATUS WINAPI RpcMgmtSetComTimeout(RPC_BINDING_HANDLE BindingHandle, unsigned int Timeout)
{
    FIXME("(%p, %d): stub\n", BindingHandle, Timeout);
    return RPC_S_OK;
}

1445 1446 1447 1448
/***********************************************************************
 *             RpcBindingInqAuthInfoExA (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1449 1450 1451
RpcBindingInqAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
                          ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
                          ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1452
{
1453
    FIXME("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1454 1455 1456 1457 1458 1459 1460 1461
          AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
    return RPC_S_INVALID_BINDING;
}

/***********************************************************************
 *             RpcBindingInqAuthInfoExW (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1462 1463 1464
RpcBindingInqAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
                          ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc,
                          ULONG RpcQosVersion, RPC_SECURITY_QOS *SecurityQOS )
1465
{
1466
    FIXME("%p %p %p %p %p %p %u %p\n", Binding, ServerPrincName, AuthnLevel,
1467 1468 1469 1470 1471 1472 1473 1474
          AuthnSvc, AuthIdentity, AuthzSvc, RpcQosVersion, SecurityQOS);
    return RPC_S_INVALID_BINDING;
}

/***********************************************************************
 *             RpcBindingInqAuthInfoA (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1475 1476
RpcBindingInqAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR *ServerPrincName, ULONG *AuthnLevel,
                        ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
{
    FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
          AuthnSvc, AuthIdentity, AuthzSvc);
    return RPC_S_INVALID_BINDING;
}

/***********************************************************************
 *             RpcBindingInqAuthInfoW (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1487 1488
RpcBindingInqAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR *ServerPrincName, ULONG *AuthnLevel,
                        ULONG *AuthnSvc, RPC_AUTH_IDENTITY_HANDLE *AuthIdentity, ULONG *AuthzSvc )
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
{
    FIXME("%p %p %p %p %p %p\n", Binding, ServerPrincName, AuthnLevel,
          AuthnSvc, AuthIdentity, AuthzSvc);
    return RPC_S_INVALID_BINDING;
}

/***********************************************************************
 *             RpcBindingSetAuthInfoExA (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1499
RpcBindingSetAuthInfoExA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName,
1500 1501
                          ULONG AuthnLevel, ULONG AuthnSvc,
                          RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1502 1503
                          RPC_SECURITY_QOS *SecurityQos )
{
1504
  RpcBinding* bind = Binding;
1505
  SECURITY_STATUS r;
1506 1507
  CredHandle cred;
  TimeStamp exp;
1508 1509 1510
  ULONG package_count;
  ULONG i;
  PSecPkgInfoA packages;
1511
  ULONG cbMaxToken;
1512

1513
  TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_a((const char*)ServerPrincName),
1514 1515
        AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);

1516 1517 1518 1519
  if (SecurityQos)
  {
      RPC_STATUS status;

1520
      TRACE("SecurityQos { Version=%d, Capabilities=0x%x, IdentityTracking=%d, ImpersonationLevel=%d",
1521 1522 1523 1524
            SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
      if (SecurityQos->Version >= 2)
      {
          const RPC_SECURITY_QOS_V2_A *SecurityQos2 = (const RPC_SECURITY_QOS_V2_A *)SecurityQos;
1525
          TRACE(", AdditionalSecurityInfoType=%d", SecurityQos2->AdditionalSecurityInfoType);
1526
          if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1527
              TRACE(", { %p, 0x%x, %d, %d, %p, %s }",
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
                    SecurityQos2->u.HttpCredentials->TransportCredentials,
                    SecurityQos2->u.HttpCredentials->Flags,
                    SecurityQos2->u.HttpCredentials->AuthenticationTarget,
                    SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
                    SecurityQos2->u.HttpCredentials->AuthnSchemes,
                    SecurityQos2->u.HttpCredentials->ServerCertificateSubject);
      }
      TRACE("}\n");
      status = RpcQualityOfService_Create(SecurityQos, FALSE, &bind->QOS);
      if (status != RPC_S_OK)
          return status;
  }
  else
  {
      if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
      bind->QOS = NULL;
  }

1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
  if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
    AuthnSvc = RPC_C_AUTHN_WINNT;

  /* FIXME: the mapping should probably be retrieved using SSPI somehow */
  if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
    AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;

  if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
  {
    if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
    bind->AuthInfo = NULL;
    return RPC_S_OK;
  }

  if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1561
  {
1562
    FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1563 1564 1565
    return RPC_S_UNKNOWN_AUTHN_LEVEL;
  }

1566 1567
  /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
  if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1568
  {
1569
    FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1570 1571 1572
    return RPC_S_UNKNOWN_AUTHZ_SERVICE;
  }

1573 1574 1575
  r = EnumerateSecurityPackagesA(&package_count, &packages);
  if (r != SEC_E_OK)
  {
1576
    ERR("EnumerateSecurityPackagesA failed with error 0x%08x\n", r);
1577 1578 1579 1580 1581 1582 1583 1584 1585
    return RPC_S_SEC_PKG_ERROR;
  }

  for (i = 0; i < package_count; i++)
    if (packages[i].wRPCID == AuthnSvc)
        break;

  if (i == package_count)
  {
1586
    FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1587 1588 1589 1590
    FreeContextBuffer(packages);
    return RPC_S_UNKNOWN_AUTHN_SERVICE;
  }

1591
  TRACE("found package %s for service %u\n", packages[i].Name, AuthnSvc);
1592
  r = AcquireCredentialsHandleA(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1593
                                AuthIdentity, NULL, NULL, &cred, &exp);
1594
  cbMaxToken = packages[i].cbMaxToken;
1595
  FreeContextBuffer(packages);
1596 1597
  if (r == ERROR_SUCCESS)
  {
1598
    RpcAuthInfo *new_auth_info;
1599
    r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1600 1601 1602
                           AuthIdentity, &new_auth_info);
    if (r == RPC_S_OK)
    {
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
      new_auth_info->server_principal_name = RPCRT4_strdupAtoW((char *)ServerPrincName);
      if (new_auth_info->server_principal_name)
      {
        if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
        bind->AuthInfo = new_auth_info;
      }
      else
      {
        RpcAuthInfo_Release(new_auth_info);
        r = ERROR_OUTOFMEMORY;
      }
1614 1615
    }
    else
1616
      FreeCredentialsHandle(&cred);
1617
    return r;
1618
  }
1619
  else
1620
  {
1621
    ERR("AcquireCredentialsHandleA failed with error 0x%08x\n", r);
1622 1623
    return RPC_S_SEC_PKG_ERROR;
  }
1624 1625 1626 1627 1628 1629
}

/***********************************************************************
 *             RpcBindingSetAuthInfoExW (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1630 1631
RpcBindingSetAuthInfoExW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
                          ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr,
1632 1633
                          RPC_SECURITY_QOS *SecurityQos )
{
1634
  RpcBinding* bind = Binding;
1635 1636 1637 1638 1639 1640
  SECURITY_STATUS r;
  CredHandle cred;
  TimeStamp exp;
  ULONG package_count;
  ULONG i;
  PSecPkgInfoW packages;
1641
  ULONG cbMaxToken;
1642

1643
  TRACE("%p %s %u %u %p %u %p\n", Binding, debugstr_w(ServerPrincName),
1644 1645
        AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, SecurityQos);

1646 1647 1648 1649
  if (SecurityQos)
  {
      RPC_STATUS status;

1650
      TRACE("SecurityQos { Version=%d, Capabilities=0x%x, IdentityTracking=%d, ImpersonationLevel=%d",
1651 1652 1653 1654
            SecurityQos->Version, SecurityQos->Capabilities, SecurityQos->IdentityTracking, SecurityQos->ImpersonationType);
      if (SecurityQos->Version >= 2)
      {
          const RPC_SECURITY_QOS_V2_W *SecurityQos2 = (const RPC_SECURITY_QOS_V2_W *)SecurityQos;
1655
          TRACE(", AdditionalSecurityInfoType=%d", SecurityQos2->AdditionalSecurityInfoType);
1656
          if (SecurityQos2->AdditionalSecurityInfoType == RPC_C_AUTHN_INFO_TYPE_HTTP)
1657
              TRACE(", { %p, 0x%x, %d, %d, %p, %s }",
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
                    SecurityQos2->u.HttpCredentials->TransportCredentials,
                    SecurityQos2->u.HttpCredentials->Flags,
                    SecurityQos2->u.HttpCredentials->AuthenticationTarget,
                    SecurityQos2->u.HttpCredentials->NumberOfAuthnSchemes,
                    SecurityQos2->u.HttpCredentials->AuthnSchemes,
                    debugstr_w(SecurityQos2->u.HttpCredentials->ServerCertificateSubject));
      }
      TRACE("}\n");
      status = RpcQualityOfService_Create(SecurityQos, TRUE, &bind->QOS);
      if (status != RPC_S_OK)
          return status;
  }
  else
  {
      if (bind->QOS) RpcQualityOfService_Release(bind->QOS);
      bind->QOS = NULL;
  }

1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
  if (AuthnSvc == RPC_C_AUTHN_DEFAULT)
    AuthnSvc = RPC_C_AUTHN_WINNT;

  /* FIXME: the mapping should probably be retrieved using SSPI somehow */
  if (AuthnLevel == RPC_C_AUTHN_LEVEL_DEFAULT)
    AuthnLevel = RPC_C_AUTHN_LEVEL_NONE;

  if ((AuthnLevel == RPC_C_AUTHN_LEVEL_NONE) || (AuthnSvc == RPC_C_AUTHN_NONE))
  {
    if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
    bind->AuthInfo = NULL;
    return RPC_S_OK;
  }

  if (AuthnLevel > RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
1691
  {
1692
    FIXME("unknown AuthnLevel %u\n", AuthnLevel);
1693 1694 1695
    return RPC_S_UNKNOWN_AUTHN_LEVEL;
  }

1696 1697
  /* RPC_C_AUTHN_WINNT ignores the AuthzSvr parameter */
  if (AuthzSvr && AuthnSvc != RPC_C_AUTHN_WINNT)
1698
  {
1699
    FIXME("unsupported AuthzSvr %u\n", AuthzSvr);
1700 1701 1702 1703 1704 1705
    return RPC_S_UNKNOWN_AUTHZ_SERVICE;
  }

  r = EnumerateSecurityPackagesW(&package_count, &packages);
  if (r != SEC_E_OK)
  {
1706
    ERR("EnumerateSecurityPackagesW failed with error 0x%08x\n", r);
1707 1708 1709 1710 1711 1712 1713 1714 1715
    return RPC_S_SEC_PKG_ERROR;
  }

  for (i = 0; i < package_count; i++)
    if (packages[i].wRPCID == AuthnSvc)
        break;

  if (i == package_count)
  {
1716
    FIXME("unsupported AuthnSvc %u\n", AuthnSvc);
1717 1718 1719 1720
    FreeContextBuffer(packages);
    return RPC_S_UNKNOWN_AUTHN_SERVICE;
  }

1721
  TRACE("found package %s for service %u\n", debugstr_w(packages[i].Name), AuthnSvc);
1722
  r = AcquireCredentialsHandleW(NULL, packages[i].Name, SECPKG_CRED_OUTBOUND, NULL,
1723
                                AuthIdentity, NULL, NULL, &cred, &exp);
1724
  cbMaxToken = packages[i].cbMaxToken;
1725 1726 1727
  FreeContextBuffer(packages);
  if (r == ERROR_SUCCESS)
  {
1728
    RpcAuthInfo *new_auth_info;
1729
    r = RpcAuthInfo_Create(AuthnLevel, AuthnSvc, cred, exp, cbMaxToken,
1730 1731 1732
                           AuthIdentity, &new_auth_info);
    if (r == RPC_S_OK)
    {
1733
      new_auth_info->server_principal_name = RPCRT4_strdupW(ServerPrincName);
1734
      if (!ServerPrincName || new_auth_info->server_principal_name)
1735 1736 1737 1738 1739 1740 1741 1742 1743
      {
        if (bind->AuthInfo) RpcAuthInfo_Release(bind->AuthInfo);
        bind->AuthInfo = new_auth_info;
      }
      else
      {
        RpcAuthInfo_Release(new_auth_info);
        r = ERROR_OUTOFMEMORY;
      }
1744 1745
    }
    else
1746
      FreeCredentialsHandle(&cred);
1747
    return r;
1748 1749 1750
  }
  else
  {
1751
    ERR("AcquireCredentialsHandleW failed with error 0x%08x\n", r);
1752 1753
    return RPC_S_SEC_PKG_ERROR;
  }
1754 1755 1756 1757 1758 1759
}

/***********************************************************************
 *             RpcBindingSetAuthInfoA (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1760 1761
RpcBindingSetAuthInfoA( RPC_BINDING_HANDLE Binding, RPC_CSTR ServerPrincName, ULONG AuthnLevel,
                        ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1762
{
1763
    TRACE("%p %s %u %u %p %u\n", Binding, debugstr_a((const char*)ServerPrincName),
1764
          AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1765
    return RpcBindingSetAuthInfoExA(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1766 1767 1768 1769 1770 1771
}

/***********************************************************************
 *             RpcBindingSetAuthInfoW (RPCRT4.@)
 */
RPCRTAPI RPC_STATUS RPC_ENTRY
1772 1773
RpcBindingSetAuthInfoW( RPC_BINDING_HANDLE Binding, RPC_WSTR ServerPrincName, ULONG AuthnLevel,
                        ULONG AuthnSvc, RPC_AUTH_IDENTITY_HANDLE AuthIdentity, ULONG AuthzSvr )
1774
{
1775
    TRACE("%p %s %u %u %p %u\n", Binding, debugstr_w(ServerPrincName),
1776
          AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr);
1777
    return RpcBindingSetAuthInfoExW(Binding, ServerPrincName, AuthnLevel, AuthnSvc, AuthIdentity, AuthzSvr, NULL);
1778
}
1779 1780 1781 1782

/***********************************************************************
 *             RpcBindingSetOption (RPCRT4.@)
 */
1783
RPC_STATUS WINAPI RpcBindingSetOption(RPC_BINDING_HANDLE BindingHandle, ULONG Option, ULONG_PTR OptionValue)
1784
{
1785
    FIXME("(%p, %d, %ld): stub\n", BindingHandle, Option, OptionValue);
1786 1787
    return RPC_S_OK;
}