virnetsaslcontext.c 19.0 KB
Newer Older
1 2 3
/*
 * virnetsaslcontext.c: SASL encryption/auth handling
 *
4
 * Copyright (C) 2010-2012 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27
 */

#include <config.h>

#include <fnmatch.h>

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

28
#include "virerror.h"
29
#include "viralloc.h"
30
#include "virthread.h"
31
#include "virlog.h"
32
#include "virstring.h"
33 34 35

#define VIR_FROM_THIS VIR_FROM_RPC

36 37
VIR_LOG_INIT("rpc.netsaslcontext");

38
struct _virNetSASLContext {
39
    virObjectLockable parent;
40

41 42 43 44
    const char *const*usernameWhitelist;
};

struct _virNetSASLSession {
45
    virObjectLockable parent;
46

47 48
    sasl_conn_t *conn;
    size_t maxbufsize;
49
    sasl_callback_t *callbacks;
50 51 52
};


53 54
static virClassPtr virNetSASLContextClass;
static virClassPtr virNetSASLSessionClass;
55
static void virNetSASLContextDispose(void *obj);
56 57 58 59
static void virNetSASLSessionDispose(void *obj);

static int virNetSASLContextOnceInit(void)
{
60
    if (!VIR_CLASS_NEW(virNetSASLContext, virClassForObjectLockable()))
61 62
        return -1;

63
    if (!VIR_CLASS_NEW(virNetSASLSession, virClassForObjectLockable()))
64 65 66 67 68 69 70
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virNetSASLContext)

71 72 73 74 75 76 77 78
/* Apple have annotated all SASL functions as deprecated for
 * unknown reasons. Since they still work, lets just ignore
 * the warnings. If Apple finally delete the SASL functions
 * our configure check should already catch that
 */
#ifdef __APPLE__
VIR_WARNINGS_NO_DEPRECATED
#endif
79

80 81 82 83 84
virNetSASLContextPtr virNetSASLContextNewClient(void)
{
    virNetSASLContextPtr ctxt;
    int err;

85 86 87
    if (virNetSASLContextInitialize() < 0)
        return NULL;

88 89
    err = sasl_client_init(NULL);
    if (err != SASL_OK) {
90 91 92
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("failed to initialize SASL library: %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
93 94 95
        return NULL;
    }

96
    if (!(ctxt = virObjectLockableNew(virNetSASLContextClass)))
97 98 99 100 101 102 103 104 105 106
        return NULL;

    return ctxt;
}

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

107 108 109
    if (virNetSASLContextInitialize() < 0)
        return NULL;

110 111
    err = sasl_server_init(NULL, "libvirt");
    if (err != SASL_OK) {
112 113 114
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("failed to initialize SASL library: %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
115 116 117
        return NULL;
    }

118
    if (!(ctxt = virObjectLockableNew(virNetSASLContextClass)))
119 120
        return NULL;

121 122 123 124 125 126 127 128 129
    ctxt->usernameWhitelist = usernameWhitelist;

    return ctxt;
}

int virNetSASLContextCheckIdentity(virNetSASLContextPtr ctxt,
                                   const char *identity)
{
    const char *const*wildcards;
130 131
    int ret = -1;

132
    virObjectLock(ctxt);
133 134 135

    /* If the list is not set, allow any DN. */
    wildcards = ctxt->usernameWhitelist;
136 137 138 139
    if (!wildcards) {
        ret = 1; /* No ACL, allow all */
        goto cleanup;
    }
140 141

    while (*wildcards) {
142
        int rv = fnmatch(*wildcards, identity, 0);
143 144
        if (rv == 0) {
            ret = 1;
J
Ján Tomko 已提交
145
            goto cleanup; /* Successful match */
146
        }
147
        if (rv != FNM_NOMATCH) {
148 149 150
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Malformed TLS whitelist regular expression '%s'"),
                           *wildcards);
151
            goto cleanup;
152 153 154 155 156 157
        }

        wildcards++;
    }

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

    /* This is the most common error: make it informative. */
161 162
    virReportError(VIR_ERR_SYSTEM_ERROR, "%s",
                   _("Client's username is not on the list of allowed clients"));
163 164
    ret = 0;

165
 cleanup:
166
    virObjectUnlock(ctxt);
167
    return ret;
168 169 170 171 172 173 174 175
}


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

181
    if (!(sasl = virObjectLockableNew(virNetSASLSessionClass)))
182 183
        return NULL;

184 185 186 187 188 189 190 191 192 193 194
    /* 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) {
195 196 197
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("Failed to create SASL client context: %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
198 199
        goto cleanup;
    }
200
    sasl->callbacks = cbs;
201 202 203

    return sasl;

204
 cleanup:
205
    virObjectUnref(sasl);
206 207 208 209 210 211 212 213 214 215 216
    return NULL;
}

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

217
    if (!(sasl = virObjectLockableNew(virNetSASLSessionClass)))
218 219
        return NULL;

220 221 222 223 224 225 226 227 228 229 230 231
    /* 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) {
232 233 234
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("Failed to create SASL client context: %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
235 236 237 238 239
        goto cleanup;
    }

    return sasl;

240
 cleanup:
241
    virObjectUnref(sasl);
242 243 244 245 246 247 248
    return NULL;
}

int virNetSASLSessionExtKeySize(virNetSASLSessionPtr sasl,
                                int ssf)
{
    int err;
249
    int ret = -1;
250
    virObjectLock(sasl);
251 252 253

    err = sasl_setprop(sasl->conn, SASL_SSF_EXTERNAL, &ssf);
    if (err != SASL_OK) {
254 255 256
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot set external SSF %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
257
        goto cleanup;
258
    }
259 260 261

    ret = 0;

262
 cleanup:
263
    virObjectUnlock(sasl);
264
    return ret;
265 266 267 268
}

const char *virNetSASLSessionGetIdentity(virNetSASLSessionPtr sasl)
{
269
    const void *val = NULL;
270
    int err;
271
    virObjectLock(sasl);
272 273 274

    err = sasl_getprop(sasl->conn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
275 276 277
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("cannot query SASL username on connection %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
278 279
        val = NULL;
        goto cleanup;
280 281
    }
    if (val == NULL) {
282
        virReportError(VIR_ERR_AUTH_FAILED, "%s",
283
                       _("no client username was found"));
284
        goto cleanup;
285 286 287
    }
    VIR_DEBUG("SASL client username %s", (const char *)val);

288
 cleanup:
289
    virObjectUnlock(sasl);
290 291 292 293 294 295 296 297 298
    return (const char*)val;
}


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

300
    virObjectLock(sasl);
301 302
    err = sasl_getprop(sasl->conn, SASL_SSF, &val);
    if (err != SASL_OK) {
303 304 305
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("cannot query SASL ssf on connection %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
306 307
        ssf = -1;
        goto cleanup;
308 309
    }
    ssf = *(const int *)val;
310

311
 cleanup:
312
    virObjectUnlock(sasl);
313 314 315 316 317 318 319 320 321 322
    return ssf;
}

int virNetSASLSessionSecProps(virNetSASLSessionPtr sasl,
                              int minSSF,
                              int maxSSF,
                              bool allowAnonymous)
{
    sasl_security_properties_t secprops;
    int err;
323
    int ret = -1;
324 325 326 327

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

328
    virObjectLock(sasl);
329
    memset(&secprops, 0, sizeof(secprops));
330 331 332 333 334 335 336 337 338

    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) {
339 340 341
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot set security props %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
342
        goto cleanup;
343 344
    }

345 346
    ret = 0;

347
 cleanup:
348
    virObjectUnlock(sasl);
349
    return ret;
350 351 352 353 354
}


static int virNetSASLSessionUpdateBufSize(virNetSASLSessionPtr sasl)
{
355 356 357 358
    union {
        unsigned *maxbufsize;
        const void *ptr;
    } u;
359 360
    int err;

361
    err = sasl_getprop(sasl->conn, SASL_MAXOUTBUF, &u.ptr);
362
    if (err != SASL_OK) {
363 364 365
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot get security props %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
366 367 368 369
        return -1;
    }

    VIR_DEBUG("Negotiated bufsize is %u vs requested size %zu",
370 371
              *u.maxbufsize, sasl->maxbufsize);
    sasl->maxbufsize = *u.maxbufsize;
372 373 374 375 376 377
    return 0;
}

char *virNetSASLSessionListMechanisms(virNetSASLSessionPtr sasl)
{
    const char *mechlist;
378
    char *ret = NULL;
379 380
    int err;

381
    virObjectLock(sasl);
382 383 384 385 386 387 388 389 390
    err = sasl_listmech(sasl->conn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
391 392 393
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("cannot list SASL mechanisms %d (%s)"),
                       err, sasl_errdetail(sasl->conn));
394
        goto cleanup;
395
    }
396 397 398 399 400 401
    VIR_DEBUG("SASL mechanism list is '%s'", mechlist);
    if (STREQ(mechlist, "")) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no SASL mechanisms are available"));
        goto cleanup;
    }
402
    ignore_value(VIR_STRDUP(ret, mechlist));
403

404
 cleanup:
405
    virObjectUnlock(sasl);
406 407 408 409 410 411 412 413 414 415 416 417
    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;
418 419
    int err;
    int ret = -1;
420 421 422 423

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

424
    virObjectLock(sasl);
425 426 427 428 429 430
    err = sasl_client_start(sasl->conn,
                            mechlist,
                            prompt_need,
                            clientout,
                            &outlen,
                            mech);
431 432 433 434 435 436

    *clientoutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
437 438 439
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
440
    case SASL_CONTINUE:
441 442
        ret = VIR_NET_SASL_CONTINUE;
        break;
443
    case SASL_INTERACT:
444 445
        ret = VIR_NET_SASL_INTERACT;
        break;
446
    default:
447 448 449
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("Failed to start SASL negotiation: %d (%s)"),
                       err, sasl_errdetail(sasl->conn));
450
        break;
451
    }
452

453
 cleanup:
454
    virObjectUnlock(sasl);
455
    return ret;
456 457 458 459 460 461 462 463 464 465 466 467
}


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;
468 469
    int err;
    int ret = -1;
470

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

474
    virObjectLock(sasl);
475 476 477 478 479 480
    err = sasl_client_step(sasl->conn,
                           serverin,
                           inlen,
                           prompt_need,
                           clientout,
                           &outlen);
481 482 483 484 485
    *clientoutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
486 487 488
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
489
    case SASL_CONTINUE:
490 491
        ret = VIR_NET_SASL_CONTINUE;
        break;
492
    case SASL_INTERACT:
493 494
        ret = VIR_NET_SASL_INTERACT;
        break;
495
    default:
496 497 498
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("Failed to step SASL negotiation: %d (%s)"),
                       err, sasl_errdetail(sasl->conn));
499
        break;
500
    }
501

502
 cleanup:
503
    virObjectUnlock(sasl);
504
    return ret;
505 506 507 508 509 510 511 512 513 514 515
}

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;
516 517 518
    int err;
    int ret = -1;

519
    virObjectLock(sasl);
520 521 522 523 524 525
    err = sasl_server_start(sasl->conn,
                            mechname,
                            clientin,
                            inlen,
                            serverout,
                            &outlen);
526 527 528 529 530 531

    *serveroutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
532 533 534
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
535
    case SASL_CONTINUE:
536 537
        ret = VIR_NET_SASL_CONTINUE;
        break;
538
    case SASL_INTERACT:
539 540
        ret = VIR_NET_SASL_INTERACT;
        break;
541
    default:
542 543 544
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("Failed to start SASL negotiation: %d (%s)"),
                       err, sasl_errdetail(sasl->conn));
545
        break;
546
    }
547

548
 cleanup:
549
    virObjectUnlock(sasl);
550
    return ret;
551 552 553 554 555 556 557 558 559 560 561
}


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

565
    virObjectLock(sasl);
566 567 568 569 570
    err = sasl_server_step(sasl->conn,
                           clientin,
                           inlen,
                           serverout,
                           &outlen);
571 572 573 574 575 576

    *serveroutlen = outlen;

    switch (err) {
    case SASL_OK:
        if (virNetSASLSessionUpdateBufSize(sasl) < 0)
577 578 579
            goto cleanup;
        ret = VIR_NET_SASL_COMPLETE;
        break;
580
    case SASL_CONTINUE:
581 582
        ret = VIR_NET_SASL_CONTINUE;
        break;
583
    case SASL_INTERACT:
584 585
        ret = VIR_NET_SASL_INTERACT;
        break;
586
    default:
587 588 589
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("Failed to start SASL negotiation: %d (%s)"),
                       err, sasl_errdetail(sasl->conn));
590
        break;
591
    }
592

593
 cleanup:
594
    virObjectUnlock(sasl);
595
    return ret;
596 597 598 599
}

size_t virNetSASLSessionGetMaxBufSize(virNetSASLSessionPtr sasl)
{
600
    size_t ret;
601
    virObjectLock(sasl);
602
    ret = sasl->maxbufsize;
603
    virObjectUnlock(sasl);
604
    return ret;
605 606 607 608 609 610 611 612 613 614 615
}

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;
616
    ssize_t ret = -1;
617

618
    virObjectLock(sasl);
619 620 621 622
    if (inputLen > sasl->maxbufsize) {
        virReportSystemError(EINVAL,
                             _("SASL data length %zu too long, max %zu"),
                             inputLen, sasl->maxbufsize);
623
        goto cleanup;
624 625 626 627 628 629 630 631 632 633
    }

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

    if (err != SASL_OK) {
634 635 636
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to encode SASL data: %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
637
        goto cleanup;
638
    }
639 640
    ret = 0;

641
 cleanup:
642
    virObjectUnlock(sasl);
643
    return ret;
644 645 646 647 648 649 650 651 652 653 654
}

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;
655
    ssize_t ret = -1;
656

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

    err = sasl_decode(sasl->conn,
                      input,
                      inlen,
                      output,
                      &outlen);
    *outputlen = outlen;
    if (err != SASL_OK) {
672 673 674
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("failed to decode SASL data: %d (%s)"),
                       err, sasl_errstring(err, NULL, NULL));
675
        goto cleanup;
676
    }
677 678
    ret = 0;

679
 cleanup:
680
    virObjectUnlock(sasl);
681
    return ret;
682 683
}

684 685 686 687 688
void virNetSASLContextDispose(void *obj ATTRIBUTE_UNUSED)
{
    return;
}

689
void virNetSASLSessionDispose(void *obj)
690
{
691
    virNetSASLSessionPtr sasl = obj;
692 693 694

    if (sasl->conn)
        sasl_dispose(&sasl->conn);
695
    VIR_FREE(sasl->callbacks);
696
}
697 698 699 700

#ifdef __APPLE__
VIR_WARNINGS_RESET
#endif