virnetsaslcontext.c 19.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * virnetsaslcontext.c: SASL encryption/auth handling
 *
 * Copyright (C) 2010-2011 Red Hat, Inc.
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include <config.h>

#include <fnmatch.h>

#include "virnetsaslcontext.h"
#include "virnetmessage.h"

#include "virterror_internal.h"
#include "memory.h"
30
#include "threads.h"
31 32 33 34 35 36 37 38 39
#include "logging.h"

#define VIR_FROM_THIS VIR_FROM_RPC
#define virNetError(code, ...)                                    \
    virReportErrorHelper(VIR_FROM_THIS, code, __FILE__,           \
                         __FUNCTION__, __LINE__, __VA_ARGS__)


struct _virNetSASLContext {
40
    virMutex lock;
41 42 43 44 45
    const char *const*usernameWhitelist;
    int refs;
};

struct _virNetSASLSession {
46
    virMutex lock;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    sasl_conn_t *conn;
    int refs;
    size_t maxbufsize;
};


virNetSASLContextPtr virNetSASLContextNewClient(void)
{
    virNetSASLContextPtr ctxt;
    int err;

    err = sasl_client_init(NULL);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("failed to initialize SASL library: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
        return NULL;
    }

    if (VIR_ALLOC(ctxt) < 0) {
        virReportOOMError();
        return NULL;
    }

71 72 73 74 75 76 77
    if (virMutexInit(&ctxt->lock) < 0) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("Failed to initialized mutex"));
        VIR_FREE(ctxt);
        return NULL;
    }

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    ctxt->refs = 1;

    return ctxt;
}

virNetSASLContextPtr virNetSASLContextNewServer(const char *const*usernameWhitelist)
{
    virNetSASLContextPtr ctxt;
    int err;

    err = sasl_server_init(NULL, "libvirt");
    if (err != SASL_OK) {
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("failed to initialize SASL library: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
        return NULL;
    }

    if (VIR_ALLOC(ctxt) < 0) {
        virReportOOMError();
        return NULL;
    }

101 102 103 104 105 106 107
    if (virMutexInit(&ctxt->lock) < 0) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("Failed to initialized mutex"));
        VIR_FREE(ctxt);
        return NULL;
    }

108 109 110 111 112 113 114 115 116 117
    ctxt->usernameWhitelist = usernameWhitelist;
    ctxt->refs = 1;

    return ctxt;
}

int virNetSASLContextCheckIdentity(virNetSASLContextPtr ctxt,
                                   const char *identity)
{
    const char *const*wildcards;
118 119 120
    int ret = -1;

    virMutexLock(&ctxt->lock);
121 122 123

    /* If the list is not set, allow any DN. */
    wildcards = ctxt->usernameWhitelist;
124 125 126 127
    if (!wildcards) {
        ret = 1; /* No ACL, allow all */
        goto cleanup;
    }
128 129

    while (*wildcards) {
130 131 132 133 134
        int rv = fnmatch (*wildcards, identity, 0);
        if (rv == 0) {
            ret = 1;
            goto cleanup; /* Succesful match */
        }
135
        if (rv != FNM_NOMATCH) {
136 137 138
            virNetError(VIR_ERR_INTERNAL_ERROR,
                        _("Malformed TLS whitelist regular expression '%s'"),
                        *wildcards);
139
            goto cleanup;
140 141 142 143 144 145 146 147 148 149 150
        }

        wildcards++;
    }

    /* Denied */
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), identity);

    /* This is the most common error: make it informative. */
    virNetError(VIR_ERR_SYSTEM_ERROR, "%s",
                _("Client's username is not on the list of allowed clients"));
151 152 153 154 155
    ret = 0;

cleanup:
    virMutexUnlock(&ctxt->lock);
    return ret;
156 157 158 159 160
}


void virNetSASLContextRef(virNetSASLContextPtr ctxt)
{
161
    virMutexLock(&ctxt->lock);
162
    ctxt->refs++;
163
    virMutexUnlock(&ctxt->lock);
164 165 166 167 168 169 170
}

void virNetSASLContextFree(virNetSASLContextPtr ctxt)
{
    if (!ctxt)
        return;

171
    virMutexLock(&ctxt->lock);
172
    ctxt->refs--;
173 174
    if (ctxt->refs > 0) {
        virMutexUnlock(&ctxt->lock);
175
        return;
176
    }
177

178 179
    virMutexUnlock(&ctxt->lock);
    virMutexDestroy(&ctxt->lock);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    VIR_FREE(ctxt);
}

virNetSASLSessionPtr virNetSASLSessionNewClient(virNetSASLContextPtr ctxt ATTRIBUTE_UNUSED,
                                                const char *service,
                                                const char *hostname,
                                                const char *localAddr,
                                                const char *remoteAddr,
                                                const sasl_callback_t *cbs)
{
    virNetSASLSessionPtr sasl = NULL;
    int err;

    if (VIR_ALLOC(sasl) < 0) {
        virReportOOMError();
        goto cleanup;
    }

198 199 200 201 202 203 204
    if (virMutexInit(&sasl->lock) < 0) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("Failed to initialized mutex"));
        VIR_FREE(sasl);
        return NULL;
    }

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    sasl->refs = 1;
    /* Arbitrary size for amount of data we can encode in a single block */
    sasl->maxbufsize = 1 << 16;

    err = sasl_client_new(service,
                          hostname,
                          localAddr,
                          remoteAddr,
                          cbs,
                          SASL_SUCCESS_DATA,
                          &sasl->conn);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("Failed to create SASL client context: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
        goto cleanup;
    }

    return sasl;

cleanup:
    virNetSASLSessionFree(sasl);
    return NULL;
}

virNetSASLSessionPtr virNetSASLSessionNewServer(virNetSASLContextPtr ctxt ATTRIBUTE_UNUSED,
                                                const char *service,
                                                const char *localAddr,
                                                const char *remoteAddr)
{
    virNetSASLSessionPtr sasl = NULL;
    int err;

    if (VIR_ALLOC(sasl) < 0) {
        virReportOOMError();
        goto cleanup;
    }

243 244 245 246 247 248 249
    if (virMutexInit(&sasl->lock) < 0) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("Failed to initialized mutex"));
        VIR_FREE(sasl);
        return NULL;
    }

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    sasl->refs = 1;
    /* Arbitrary size for amount of data we can encode in a single block */
    sasl->maxbufsize = 1 << 16;

    err = sasl_server_new(service,
                          NULL,
                          NULL,
                          localAddr,
                          remoteAddr,
                          NULL,
                          SASL_SUCCESS_DATA,
                          &sasl->conn);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("Failed to create SASL client context: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
        goto cleanup;
    }

    return sasl;

cleanup:
    virNetSASLSessionFree(sasl);
    return NULL;
}

void virNetSASLSessionRef(virNetSASLSessionPtr sasl)
{
278
    virMutexLock(&sasl->lock);
279
    sasl->refs++;
280
    virMutexUnlock(&sasl->lock);
281 282 283 284 285 286
}

int virNetSASLSessionExtKeySize(virNetSASLSessionPtr sasl,
                                int ssf)
{
    int err;
287 288
    int ret = -1;
    virMutexLock(&sasl->lock);
289 290 291 292 293 294

    err = sasl_setprop(sasl->conn, SASL_SSF_EXTERNAL, &ssf);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("cannot set external SSF %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
295
        goto cleanup;
296
    }
297 298 299 300 301 302

    ret = 0;

cleanup:
    virMutexLock(&sasl->lock);
    return ret;
303 304 305 306
}

const char *virNetSASLSessionGetIdentity(virNetSASLSessionPtr sasl)
{
307
    const void *val = NULL;
308
    int err;
309
    virMutexLock(&sasl->lock);
310 311 312 313 314 315

    err = sasl_getprop(sasl->conn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("cannot query SASL username on connection %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
316 317
        val = NULL;
        goto cleanup;
318 319 320 321
    }
    if (val == NULL) {
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("no client username was found"));
322
        goto cleanup;
323 324 325
    }
    VIR_DEBUG("SASL client username %s", (const char *)val);

326 327
cleanup:
    virMutexUnlock(&sasl->lock);
328 329 330 331 332 333 334 335 336
    return (const char*)val;
}


int virNetSASLSessionGetKeySize(virNetSASLSessionPtr sasl)
{
    int err;
    int ssf;
    const void *val;
337 338

    virMutexLock(&sasl->lock);
339 340 341 342 343
    err = sasl_getprop(sasl->conn, SASL_SSF, &val);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("cannot query SASL ssf on connection %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
344 345
        ssf = -1;
        goto cleanup;
346 347
    }
    ssf = *(const int *)val;
348 349 350

cleanup:
    virMutexUnlock(&sasl->lock);
351 352 353 354 355 356 357 358 359 360
    return ssf;
}

int virNetSASLSessionSecProps(virNetSASLSessionPtr sasl,
                              int minSSF,
                              int maxSSF,
                              bool allowAnonymous)
{
    sasl_security_properties_t secprops;
    int err;
361
    int ret = -1;
362 363 364 365

    VIR_DEBUG("minSSF=%d maxSSF=%d allowAnonymous=%d maxbufsize=%zu",
              minSSF, maxSSF, allowAnonymous, sasl->maxbufsize);

366
    virMutexLock(&sasl->lock);
367 368 369 370 371 372 373 374 375 376 377 378 379
    memset(&secprops, 0, sizeof secprops);

    secprops.min_ssf = minSSF;
    secprops.max_ssf = maxSSF;
    secprops.maxbufsize = sasl->maxbufsize;
    secprops.security_flags = allowAnonymous ? 0 :
        SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;

    err = sasl_setprop(sasl->conn, SASL_SEC_PROPS, &secprops);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("cannot set security props %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
380
        goto cleanup;
381 382
    }

383 384 385 386 387
    ret = 0;

cleanup:
    virMutexUnlock(&sasl->lock);
    return ret;
388 389 390 391 392
}


static int virNetSASLSessionUpdateBufSize(virNetSASLSessionPtr sasl)
{
393 394 395 396
    union {
        unsigned *maxbufsize;
        const void *ptr;
    } u;
397 398
    int err;

399
    err = sasl_getprop(sasl->conn, SASL_MAXOUTBUF, &u.ptr);
400 401 402 403 404 405 406 407
    if (err != SASL_OK) {
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("cannot get security props %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
        return -1;
    }

    VIR_DEBUG("Negotiated bufsize is %u vs requested size %zu",
408 409
              *u.maxbufsize, sasl->maxbufsize);
    sasl->maxbufsize = *u.maxbufsize;
410 411 412 413 414 415
    return 0;
}

char *virNetSASLSessionListMechanisms(virNetSASLSessionPtr sasl)
{
    const char *mechlist;
416
    char *ret = NULL;
417 418
    int err;

419
    virMutexLock(&sasl->lock);
420 421 422 423 424 425 426 427 428 429 430 431
    err = sasl_listmech(sasl->conn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("cannot list SASL mechanisms %d (%s)"),
                    err, sasl_errdetail(sasl->conn));
432
        goto cleanup;
433 434 435
    }
    if (!(ret = strdup(mechlist))) {
        virReportOOMError();
436
        goto cleanup;
437
    }
438 439 440

cleanup:
    virMutexUnlock(&sasl->lock);
441 442 443 444 445 446 447 448 449 450 451 452
    return ret;
}


int virNetSASLSessionClientStart(virNetSASLSessionPtr sasl,
                                 const char *mechlist,
                                 sasl_interact_t **prompt_need,
                                 const char **clientout,
                                 size_t *clientoutlen,
                                 const char **mech)
{
    unsigned outlen = 0;
453 454
    int err;
    int ret = -1;
455 456 457 458

    VIR_DEBUG("sasl=%p mechlist=%s prompt_need=%p clientout=%p clientoutlen=%p mech=%p",
              sasl, mechlist, prompt_need, clientout, clientoutlen, mech);

459 460 461 462 463 464 465
    virMutexLock(&sasl->lock);
    err = sasl_client_start(sasl->conn,
                            mechlist,
                            prompt_need,
                            clientout,
                            &outlen,
                            mech);
466 467 468 469 470 471

    *clientoutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
472 473 474
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
475
    case SASL_CONTINUE:
476 477
        ret = VIR_NET_SASL_CONTINUE;
        break;
478
    case SASL_INTERACT:
479 480
        ret = VIR_NET_SASL_INTERACT;
        break;
481 482 483 484
    default:
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("Failed to start SASL negotiation: %d (%s)"),
                    err, sasl_errdetail(sasl->conn));
485
        break;
486
    }
487 488 489 490

cleanup:
    virMutexUnlock(&sasl->lock);
    return ret;
491 492 493 494 495 496 497 498 499 500 501 502
}


int virNetSASLSessionClientStep(virNetSASLSessionPtr sasl,
                                const char *serverin,
                                size_t serverinlen,
                                sasl_interact_t **prompt_need,
                                const char **clientout,
                                size_t *clientoutlen)
{
    unsigned inlen = serverinlen;
    unsigned outlen = 0;
503 504
    int err;
    int ret = -1;
505 506 507 508

    VIR_DEBUG("sasl=%p serverin=%s serverinlen=%zu prompt_need=%p clientout=%p clientoutlen=%p",
              sasl, serverin, serverinlen, prompt_need, clientout, clientoutlen);

509 510 511 512 513 514 515
    virMutexLock(&sasl->lock);
    err = sasl_client_step(sasl->conn,
                           serverin,
                           inlen,
                           prompt_need,
                           clientout,
                           &outlen);
516 517 518 519 520
    *clientoutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
521 522 523
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
524
    case SASL_CONTINUE:
525 526
        ret = VIR_NET_SASL_CONTINUE;
        break;
527
    case SASL_INTERACT:
528 529
        ret = VIR_NET_SASL_INTERACT;
        break;
530 531 532 533
    default:
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("Failed to step SASL negotiation: %d (%s)"),
                    err, sasl_errdetail(sasl->conn));
534
        break;
535
    }
536 537 538 539

cleanup:
    virMutexUnlock(&sasl->lock);
    return ret;
540 541 542 543 544 545 546 547 548 549 550
}

int virNetSASLSessionServerStart(virNetSASLSessionPtr sasl,
                                 const char *mechname,
                                 const char *clientin,
                                 size_t clientinlen,
                                 const char **serverout,
                                 size_t *serveroutlen)
{
    unsigned inlen = clientinlen;
    unsigned outlen = 0;
551 552 553 554 555 556 557 558 559 560
    int err;
    int ret = -1;

    virMutexLock(&sasl->lock);
    err = sasl_server_start(sasl->conn,
                            mechname,
                            clientin,
                            inlen,
                            serverout,
                            &outlen);
561 562 563 564 565 566

    *serveroutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
567 568 569
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
570
    case SASL_CONTINUE:
571 572
        ret = VIR_NET_SASL_CONTINUE;
        break;
573
    case SASL_INTERACT:
574 575
        ret = VIR_NET_SASL_INTERACT;
        break;
576 577 578 579
    default:
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("Failed to start SASL negotiation: %d (%s)"),
                    err, sasl_errdetail(sasl->conn));
580
        break;
581
    }
582 583 584 585

cleanup:
    virMutexUnlock(&sasl->lock);
    return ret;
586 587 588 589 590 591 592 593 594 595 596
}


int virNetSASLSessionServerStep(virNetSASLSessionPtr sasl,
                                const char *clientin,
                                size_t clientinlen,
                                const char **serverout,
                                size_t *serveroutlen)
{
    unsigned inlen = clientinlen;
    unsigned outlen = 0;
597 598
    int err;
    int ret = -1;
599

600 601 602 603 604 605
    virMutexLock(&sasl->lock);
    err = sasl_server_step(sasl->conn,
                           clientin,
                           inlen,
                           serverout,
                           &outlen);
606 607 608 609 610 611

    *serveroutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
612 613 614
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
615
    case SASL_CONTINUE:
616 617
        ret = VIR_NET_SASL_CONTINUE;
        break;
618
    case SASL_INTERACT:
619 620
        ret = VIR_NET_SASL_INTERACT;
        break;
621 622 623 624
    default:
        virNetError(VIR_ERR_AUTH_FAILED,
                    _("Failed to start SASL negotiation: %d (%s)"),
                    err, sasl_errdetail(sasl->conn));
625
        break;
626
    }
627 628 629 630

cleanup:
    virMutexUnlock(&sasl->lock);
    return ret;
631 632 633 634
}

size_t virNetSASLSessionGetMaxBufSize(virNetSASLSessionPtr sasl)
{
635 636 637 638 639
    size_t ret;
    virMutexLock(&sasl->lock);
    ret = sasl->maxbufsize;
    virMutexUnlock(&sasl->lock);
    return ret;
640 641 642 643 644 645 646 647 648 649 650
}

ssize_t virNetSASLSessionEncode(virNetSASLSessionPtr sasl,
                                const char *input,
                                size_t inputLen,
                                const char **output,
                                size_t *outputlen)
{
    unsigned inlen = inputLen;
    unsigned outlen = 0;
    int err;
651
    ssize_t ret = -1;
652

653
    virMutexLock(&sasl->lock);
654 655 656 657
    if (inputLen > sasl->maxbufsize) {
        virReportSystemError(EINVAL,
                             _("SASL data length %zu too long, max %zu"),
                             inputLen, sasl->maxbufsize);
658
        goto cleanup;
659 660 661 662 663 664 665 666 667 668 669 670 671
    }

    err = sasl_encode(sasl->conn,
                      input,
                      inlen,
                      output,
                      &outlen);
    *outputlen = outlen;

    if (err != SASL_OK) {
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("failed to encode SASL data: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
672
        goto cleanup;
673
    }
674 675 676 677 678
    ret = 0;

cleanup:
    virMutexUnlock(&sasl->lock);
    return ret;
679 680 681 682 683 684 685 686 687 688 689
}

ssize_t virNetSASLSessionDecode(virNetSASLSessionPtr sasl,
                                const char *input,
                                size_t inputLen,
                                const char **output,
                                size_t *outputlen)
{
    unsigned inlen = inputLen;
    unsigned outlen = 0;
    int err;
690
    ssize_t ret = -1;
691

692
    virMutexLock(&sasl->lock);
693 694 695 696
    if (inputLen > sasl->maxbufsize) {
        virReportSystemError(EINVAL,
                             _("SASL data length %zu too long, max %zu"),
                             inputLen, sasl->maxbufsize);
697
        goto cleanup;
698 699 700 701 702 703 704 705 706 707 708 709
    }

    err = sasl_decode(sasl->conn,
                      input,
                      inlen,
                      output,
                      &outlen);
    *outputlen = outlen;
    if (err != SASL_OK) {
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("failed to decode SASL data: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
710
        goto cleanup;
711
    }
712 713 714 715 716
    ret = 0;

cleanup:
    virMutexUnlock(&sasl->lock);
    return ret;
717 718 719 720 721 722 723
}

void virNetSASLSessionFree(virNetSASLSessionPtr sasl)
{
    if (!sasl)
        return;

724
    virMutexLock(&sasl->lock);
725
    sasl->refs--;
726 727
    if (sasl->refs > 0) {
        virMutexUnlock(&sasl->lock);
728
        return;
729
    }
730 731 732 733

    if (sasl->conn)
        sasl_dispose(&sasl->conn);

734 735
    virMutexUnlock(&sasl->lock);
    virMutexDestroy(&sasl->lock);
736 737
    VIR_FREE(sasl);
}