summaryrefslogtreecommitdiffstats
path: root/source4/scripting/python/wscript_build
Commit message (Expand)AuthorAgeFilesLines
* waf: Fix mispellingSimo Sorce2012-04-191-1/+1
* Revert "Install (platform-independent) python scripts to the PYTHONDIR rather...Jelmer Vernooij2011-12-091-1/+1
* Install (platform-independent) python scripts to the PYTHONDIR rather than PY...Jelmer Vernooij2011-12-061-1/+1
* waf: Don't link python modules against libpython2.x, consistent with other Py...Jelmer Vernooij2011-11-131-1/+1
* s4-waf Don't install any Samba packages into PYTHONDIRAndrew Bartlett2011-02-021-1/+1
* talloc: change pytalloc-util to be a public library.Jelmer Vernooij2010-10-261-2/+2
* waf: Remove unused EXT_LIB_PYTHON.Jelmer Vernooij2010-10-261-5/+1
* talloc: Move pytalloc to lib/talloc.Jelmer Vernooij2010-10-261-7/+0
* waf: Remove lib prefix from libraries manually.Jelmer Vernooij2010-10-261-2/+2
* s4: Drop duplicate 'lib' prefix for private libraries.Jelmer Vernooij2010-10-261-1/+1
* s4: Rename LIBSAMBA-* to libsamba-*Jelmer Vernooij2010-10-241-1/+1
* s4: Rename LIBNETIF to libnetif.Jelmer Vernooij2010-10-231-24/+24
* s4-python: python_samba needs pyextAndrew Tridgell2010-10-211-1/+2
* s4-python: added a samba_python grouping libraryAndrew Tridgell2010-10-211-0/+6
* wafsamba: Fix handling of pyembed/pyext.Jelmer Vernooij2010-10-101-19/+19
* wafsamba: Clarify needs_python argument name, use pyembed/pyext whereJelmer Vernooij2010-10-101-9/+9
* s4-pyglue: pyglue now depends on pytallocAndrew Tridgell2010-08-251-1/+1
* pyglue: Trim dependencies.Jelmer Vernooij2010-06-191-1/+1
* waf: Add missing dependency (so header paths are inherited) on talloc to inte...Jelmer Vernooij2010-06-191-0/+1
* s4: Don't build a $prefix/lib/libpython.so, as such a library often already e...Jelmer Vernooij2010-06-181-1/+2
* s4-waf: Don't include pytalloc when embedding python.Jelmer Vernooij2010-06-181-1/+0
* s4: Build more python convenience files as subsystems rather than as python m...Jelmer Vernooij2010-06-181-2/+3
* ldb: Fix dependencies when building with system ldb.Jelmer Vernooij2010-05-311-1/+1
* s4-waf: only enable the python uuid module if python <= 2.4Andrew Tridgell2010-04-091-1/+2
* s4-python: rename samba.glue to samba._glue to indicate it's private.Jelmer Vernooij2010-04-081-1/+1
* s4-python: Use standard infrastructure for finding included testtools, subuni...Jelmer Vernooij2010-04-071-2/+0
* s4-waf: updates for the new python installer from jelmerAndrew Tridgell2010-04-061-2/+0
* s4-waf: mark the wscript files as python so vim/emacs knows how to highlight ...Andrew Tridgell2010-04-061-0/+2
* s4-waf: install the rest of our python filesAndrew Tridgell2010-04-061-0/+4
* build: waf quicktest nearly worksAndrew Tridgell2010-04-061-1/+7
* build: commit all the waf build files in the treeAndrew Tridgell2010-04-061-0/+32
0' href='#n400'>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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 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 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
/*
 * Copyright (c) 2005 Massachusetts Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* $Id$ */

#include<kcreddbinternal.h>
#include<assert.h>

/* cs_creds protects the *collection* of credentials, while l_creds
   protects the *contents* of individual credentials. */
CRITICAL_SECTION cs_creds;
kcdb_cred * kcdb_creds = NULL;

/* a read lock must be obtained when querying any existing credential.
   a write lock must be obtained when modifying any existing credential.
   */
RWLOCK l_creds;

/* serial number */
khm_ui_8 kcdb_cred_id = 0;

void kcdb_cred_init(void)
{
    InitializeCriticalSection(&cs_creds);
    InitializeRwLock(&l_creds);
    kcdb_cred_id = 0;
}

void kcdb_cred_exit(void)
{
    /*TODO: Free the credentials */
    DeleteCriticalSection(&cs_creds);
    DeleteRwLock(&l_creds);
}

/*! \internal

    can be called by kcdb_cred_dup with a write lock on l_creds and in other
    places with a read lock on l_creds.  New credentials must be creatable while
    holding either lock. */
KHMEXP khm_int32 KHMAPI 
kcdb_cred_create(const wchar_t *   name, 
                 khm_handle  identity,
                 khm_int32   cred_type,
                 khm_handle * result) 
{
    kcdb_cred * cred;
    size_t cb_name;

    if(!name || !result ||
        FAILED(StringCbLength(name, KCDB_CRED_MAXCB_NAME, &cb_name)) ||
        KHM_FAILED(kcdb_credtype_get_info(cred_type, NULL)) ||
        KHM_FAILED(kcdb_identity_hold(identity))) {
        return KHM_ERROR_INVALID_PARAM;
    }

    cb_name += sizeof(wchar_t);

    cred = PMALLOC(sizeof(kcdb_cred));
    ZeroMemory(cred, sizeof(kcdb_cred));

    cred->magic = KCDB_CRED_MAGIC;
    cred->identity = identity;
    cred->name = PMALLOC(cb_name);
    StringCbCopy(cred->name, cb_name, name);
    cred->type = cred_type;

    cred->refcount = 1; /* initially held */
    
    LINIT(cred);

    kcdb_buf_new(&cred->buf, KCDB_ATTR_MAX_ID + 1);

    /* Not obtaining a write lock on l_cred on purpose.
       Well, because no one should be referencing this credential until
       this function returns. */
    EnterCriticalSection(&cs_creds);
    cred->id = kcdb_cred_id++;
    LPUSH(&kcdb_creds, cred);
    LeaveCriticalSection(&cs_creds);

    *result = cred;

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_update(khm_handle vdest,
                                         khm_handle vsrc)
{
    khm_int32 rv = KHM_ERROR_EQUIVALENT;
    kcdb_cred * src;
    kcdb_cred * dest;
    kcdb_type * t;
    kcdb_attrib * a;
    void * srcbuf;
    void * destbuf;
    khm_size cbsrcbuf;
    khm_size cbdestbuf;

    int i;

    kcdb_cred_lock_write();

    if(!kcdb_cred_is_active_cred(vsrc) ||
       !kcdb_cred_is_active_cred(vdest))
        goto _exit;

    src = (kcdb_cred *) vsrc;
    dest = (kcdb_cred *) vdest;

    for(i=0;i<KCDB_ATTR_MAX_ID;i++) {
        if(kcdb_cred_val_exist(src, i)) {
            /*NOTE: the logic here has to reflect the logic in
              kcdb_cred_set_attr() */
            if(KHM_FAILED(kcdb_attrib_get_info(i, &a)))
                continue;

            if((a->flags & KCDB_ATTR_FLAG_COMPUTED) ||
                KHM_FAILED(kcdb_type_get_info(a->type, &t))) {
                kcdb_attrib_release_info(a);
                continue;
            }

            srcbuf = kcdb_cred_buf_get(src,i);
            cbsrcbuf = kcdb_cred_buf_size(src, i);

            if(kcdb_cred_val_exist(dest, i)) {
                destbuf = kcdb_cred_buf_get(dest, i);
                cbdestbuf = kcdb_cred_buf_size(dest, i);

                if(!t->comp(srcbuf, cbsrcbuf, destbuf, cbdestbuf))
                    goto _skip_copy;
            }

            kcdb_buf_set_value(&dest->buf, i, i, srcbuf, cbsrcbuf);
            rv = KHM_ERROR_SUCCESS;

	_skip_copy:
            kcdb_attrib_release_info(a);
            kcdb_type_release_info(t);
        } else {
	    if (KHM_FAILED(kcdb_attrib_get_info(i, &a)))
		continue;

	    if (!(a->flags & KCDB_ATTR_FLAG_COMPUTED) &&
		(a->flags & KCDB_ATTR_FLAG_TRANSIENT) &&
		kcdb_cred_val_exist(dest, i)) {
		kcdb_buf_set_value(&dest->buf, i, i, NULL, 0);

		rv = KHM_ERROR_SUCCESS;
	    }

	    kcdb_attrib_release_info(a);
	}
    }

    if (dest->flags != src->flags) {
        khm_int32 old_flags;

        old_flags = dest->flags;

        dest->flags = (src->flags & ~KCDB_CRED_FLAGMASK_ADDITIVE) |
            ((src->flags | dest->flags) & KCDB_CRED_FLAGMASK_ADDITIVE);

        if (dest->flags != old_flags)
            rv = KHM_ERROR_SUCCESS;
    }

 _exit:
    kcdb_cred_unlock_write();
    return rv;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_dup(
    khm_handle vcred,
    khm_handle * pnewcred)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred;
    kcdb_cred * newcred;
    khm_handle vnewcred;

    if(!pnewcred)
        return KHM_ERROR_INVALID_PARAM;

    *pnewcred = NULL;

    kcdb_cred_lock_write();

    if(!kcdb_cred_is_active_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    if(KHM_FAILED(kcdb_cred_create(cred->name,
                                   cred->identity,
                                   cred->type,
                                   &vnewcred))) 
    {
        code = KHM_ERROR_UNKNOWN;
        goto _exit;
    }

    newcred = (kcdb_cred *) vnewcred;

    newcred->flags = cred->flags;

    kcdb_buf_dup(&newcred->buf, &cred->buf);

    /* newcred is already held from the call to kcdb_cred_create */
    *pnewcred = (khm_handle) newcred;

_exit:
    kcdb_cred_unlock_write();
    return code;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_get_serial(
    khm_handle vcred,
    khm_ui_8 * pserial)
{
    kcdb_cred * c;

    if(!pserial)
        return KHM_ERROR_INVALID_PARAM;

    kcdb_cred_lock_read();

    if(!kcdb_cred_is_active_cred(vcred)) {
        kcdb_cred_unlock_read();
        return KHM_ERROR_INVALID_PARAM;
    }

    c = (kcdb_cred *) vcred;

    *pserial = c->id;

    kcdb_cred_unlock_read();

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_set_identity(
    khm_handle vcred,
    khm_handle id)
{
    kcdb_cred * c;

    if(!kcdb_is_identity(id))
        return KHM_ERROR_INVALID_PARAM;

    kcdb_cred_lock_write();
    if(!kcdb_cred_is_active_cred(vcred)) {
        kcdb_cred_unlock_write();
        return KHM_ERROR_INVALID_PARAM;
    }

    c = (kcdb_cred *) vcred;

    if(c->identity) {
        kcdb_identity_release((khm_handle) c->identity);
    }
    kcdb_identity_hold(id);
    c->identity = (kcdb_identity *) id;

    kcdb_cred_unlock_write();

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_get_type(
    khm_handle vcred,
    khm_int32 * type)
{
    kcdb_cred * c;

    if(!type)
        return KHM_ERROR_INVALID_PARAM;

    kcdb_cred_lock_read();

    if(!kcdb_cred_is_active_cred(vcred)) {
        kcdb_cred_unlock_read();
        return KHM_ERROR_INVALID_PARAM;
    }

    c = (kcdb_cred *) vcred;

    *type = c->type;

    kcdb_cred_unlock_read();

    return KHM_ERROR_SUCCESS;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_set_attrib(
    khm_handle cred, 
    const wchar_t * name, 
    void * buffer, 
    khm_size cbbuf)
{
    khm_int32 attr_id = -1;

    if(KHM_FAILED(kcdb_attrib_get_id(name, &attr_id)))
        return KHM_ERROR_INVALID_PARAM;

    return kcdb_cred_set_attr(
        cred,
        attr_id,
        buffer,
        cbbuf);
}

KHMEXP khm_int32 KHMAPI kcdb_cred_set_attr(
    khm_handle vcred, 
    khm_int32 attr_id, 
    void * buffer, 
    khm_size cbbuf)
{
    kcdb_cred * cred;
    kcdb_type * type = NULL;
    kcdb_attrib * attrib = NULL;
    khm_size cbdest;
    khm_int32 code = KHM_ERROR_SUCCESS;

    if(attr_id < 0 || attr_id > KCDB_ATTR_MAX_ID)
        return KHM_ERROR_INVALID_PARAM;

    kcdb_cred_lock_write();

    if(!kcdb_cred_is_active_cred(vcred)) {
        kcdb_cred_unlock_write();
        return KHM_ERROR_INVALID_PARAM;
    }

    cred = (kcdb_cred *) vcred;

    if(KHM_FAILED(kcdb_attrib_get_info(attr_id, &attrib))) {
        kcdb_cred_unlock_write();
        return KHM_ERROR_INVALID_PARAM;
    }

    if(attrib->flags & KCDB_ATTR_FLAG_COMPUTED)
    {
        kcdb_cred_unlock_write();
        kcdb_attrib_release_info(attrib);
        return KHM_ERROR_INVALID_OPERATION;
    }

    if (buffer == 0) {
        /* we are removing the value */
        kcdb_buf_alloc(&cred->buf, attr_id, attr_id, 0);
        code = KHM_ERROR_SUCCESS;
        goto _exit;
    }

    if(KHM_FAILED(kcdb_type_get_info(attrib->type, &type))) {
        kcdb_cred_unlock_write();
        kcdb_attrib_release_info(attrib);
        return KHM_ERROR_INVALID_PARAM;
    }

    if(!(type->isValid(buffer,cbbuf))) {
        code = KHM_ERROR_TYPE_MISMATCH;
        goto _exit;
    }

    if((type->dup(buffer, cbbuf, NULL, &cbdest)) != KHM_ERROR_TOO_LONG) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    kcdb_buf_alloc(&cred->buf, attr_id, attr_id, cbdest);
    if(!kcdb_cred_buf_exist(cred, attr_id)) {
        code = KHM_ERROR_NO_RESOURCES;
        goto _exit;
    }

    if(KHM_FAILED(code =
        type->dup(buffer, cbbuf, kcdb_cred_buf_get(cred,attr_id), &cbdest))) 
    {
        kcdb_buf_alloc(&cred->buf, attr_id, attr_id, 0);
        goto _exit;
    }

    kcdb_buf_set_value_flag(&cred->buf, attr_id);

_exit:
    kcdb_cred_unlock_write();

    if(attrib)
        kcdb_attrib_release_info(attrib);
    if(type)
        kcdb_type_release_info(type);

    return code;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_get_attrib(
    khm_handle cred, 
    const wchar_t * name, 
    khm_int32 * attr_type,
    void * buffer, 
    khm_size * cbbuf) 
{
    khm_int32 attr_id = -1;

    if(KHM_FAILED(kcdb_attrib_get_id(name, &attr_id)))
        return KHM_ERROR_NOT_FOUND;

    return kcdb_cred_get_attr(
        cred,
        attr_id,
        attr_type,
        buffer,
        cbbuf);
}

KHMEXP khm_int32 KHMAPI kcdb_cred_get_attrib_string(
    khm_handle cred, 
    const wchar_t * name, 
    wchar_t * buffer, 
    khm_size * cbbuf,
    khm_int32 flags) 
{
    khm_int32 attr_id = -1;

    if(KHM_FAILED(kcdb_attrib_get_id(name, &attr_id)))
        return KHM_ERROR_NOT_FOUND;

    return kcdb_cred_get_attr_string(
        cred,
        attr_id,
        buffer,
        cbbuf,
        flags);
}

KHMEXP khm_int32 KHMAPI 
kcdb_cred_get_attr(khm_handle vcred, 
                   khm_int32 attr_id,
                   khm_int32 * attr_type,
                   void * buffer, 
                   khm_size * pcbbuf)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred = NULL;
    kcdb_attrib * attrib = NULL;
    kcdb_type * type = NULL;

    if(attr_id < 0 || attr_id > KCDB_ATTR_MAX_ID)
        return KHM_ERROR_INVALID_PARAM;

    if(KHM_FAILED(kcdb_attrib_get_info(attr_id, &attrib))) {
        return KHM_ERROR_INVALID_PARAM;
    }

    if(KHM_FAILED(kcdb_type_get_info(attrib->type, &type))) {
        kcdb_attrib_release_info(attrib);
        return KHM_ERROR_UNKNOWN;
    }

    if(attr_type)
        *attr_type = attrib->type;

    kcdb_cred_lock_read();
    if(!kcdb_cred_is_active_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    if(!buffer && !pcbbuf) {
        /* in this case the caller is only trying to determine if the
            field contains data.  We assume that computed fields are
            always non-null. */
        code = (kcdb_cred_val_exist(cred, attr_id) ||
            (attrib->flags & KCDB_ATTR_FLAG_COMPUTED))?KHM_ERROR_SUCCESS:KHM_ERROR_NOT_FOUND;
        goto _exit;
    }

    if(attrib->flags & KCDB_ATTR_FLAG_COMPUTED) {
        code = attrib->compute_cb(
            vcred,
            attr_id,
            buffer,
            pcbbuf);
    } else if (kcdb_cred_val_exist(cred, attr_id)) {
        code = type->dup(
            kcdb_cred_buf_get(cred, attr_id),
            kcdb_cred_buf_size(cred, attr_id),
            buffer,
            pcbbuf);
    } else {
        code = KHM_ERROR_NOT_FOUND;
    }

_exit:
    kcdb_cred_unlock_read();
    if(type)
        kcdb_type_release_info(type);
    if(attrib)
        kcdb_attrib_release_info(attrib);

    return code;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_get_attr_string(
    khm_handle vcred, 
    khm_int32 attr_id,
    wchar_t * buffer, 
    khm_size * pcbbuf,
    khm_int32 flags)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred = NULL;
    kcdb_attrib * attrib = NULL;
    kcdb_type * type = NULL;

    if(attr_id < 0 || attr_id > KCDB_ATTR_MAX_ID)
        return KHM_ERROR_INVALID_PARAM;

    if(KHM_FAILED(kcdb_attrib_get_info(attr_id, &attrib))) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit_nolock;
    }

    if(KHM_FAILED(kcdb_type_get_info(attrib->type, &type))) {
        code = KHM_ERROR_UNKNOWN;
        goto _exit_nolock;
    }

    kcdb_cred_lock_read();
    if(!kcdb_cred_is_active_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    if(!buffer && !pcbbuf) {
        /* in this case the caller is only trying to determine if the
            field contains data.  We assume that computed fields are
            always non-null. */
        code = (kcdb_cred_val_exist(cred, attr_id) ||
            (attrib->flags & KCDB_ATTR_FLAG_COMPUTED))?KHM_ERROR_SUCCESS:KHM_ERROR_NOT_FOUND;
        goto _exit;
    }

    if(attrib->flags & KCDB_ATTR_FLAG_COMPUTED) {
        void * buf;
        khm_size cbbuf;

        code = attrib->compute_cb(vcred,
                                  attr_id,
                                  NULL,
                                  &cbbuf);
        if(code == KHM_ERROR_TOO_LONG) {
            wchar_t vbuf[KCDB_MAXCCH_NAME];

            if (cbbuf < sizeof(vbuf))
                buf = vbuf;
            else
                buf = PMALLOC(cbbuf);

            code = attrib->compute_cb(vcred,
                                      attr_id,
                                      buf,
                                      &cbbuf);
            if(KHM_SUCCEEDED(code)) {
                code = type->toString(buf,
                                      cbbuf,
                                      buffer,
                                      pcbbuf,
                                      flags);
            }

            if (buf != vbuf)
                PFREE(buf);
        }
    } else {
        if(kcdb_cred_buf_exist(cred, attr_id)) {
            code = type->toString(
                kcdb_cred_buf_get(cred, attr_id),
                kcdb_cred_buf_size(cred, attr_id),
                buffer,
                pcbbuf,
                flags);
        } else
            code = KHM_ERROR_NOT_FOUND;
    }

 _exit:
    kcdb_cred_unlock_read();
 _exit_nolock:
    if(type)
        kcdb_type_release_info(type);
    if(attrib)
        kcdb_attrib_release_info(attrib);

    return code;
}


KHMEXP khm_int32 KHMAPI kcdb_cred_get_name(
    khm_handle vcred, 
    wchar_t * buffer, 
    khm_size * cbbuf)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred = NULL;
    size_t cbsize;

    if(!cbbuf)
        return KHM_ERROR_INVALID_PARAM;

    kcdb_cred_lock_read();
    
    if(!kcdb_cred_is_active_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    if(FAILED(StringCbLength(cred->name, KCDB_CRED_MAXCB_NAME, &cbsize))) {
        code = KHM_ERROR_UNKNOWN;
        goto _exit;
    }

    cbsize += sizeof(wchar_t);

    if(!buffer || *cbbuf < cbsize) {
        *cbbuf = cbsize;
        code = KHM_ERROR_TOO_LONG;
        goto _exit;
    }

    StringCbCopy(buffer, *cbbuf, cred->name);

    *cbbuf = cbsize;

_exit:

    kcdb_cred_unlock_read();
    return code;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_get_identity(
    khm_handle vcred, 
    khm_handle * identity)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred;

    if(!identity)
        return KHM_ERROR_INVALID_PARAM;

    kcdb_cred_lock_read();

    if(!kcdb_cred_is_active_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    kcdb_identity_hold((khm_handle) cred->identity);

    *identity = cred->identity;
    
_exit:
    kcdb_cred_unlock_read();
    return code;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_hold(khm_handle vcred)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred;

    kcdb_cred_lock_write();

    if(!kcdb_cred_is_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    cred->refcount++;

_exit:
    kcdb_cred_unlock_write();
    return code;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_release(khm_handle vcred)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred;

    kcdb_cred_lock_write();

    if(!kcdb_cred_is_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    cred->refcount--;

_exit:
    kcdb_cred_unlock_write();

    kcdb_cred_check_and_delete(vcred);
    
    return code;
}

void kcdb_cred_check_and_delete(khm_handle vcred)
{
    kcdb_cred * cred;

    kcdb_cred_lock_read();
    if(!kcdb_cred_is_cred(vcred)) {
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    if(cred->refcount)
        goto _exit;

    kcdb_cred_unlock_read();
    kcdb_cred_lock_write();
    if(!kcdb_cred_is_cred(vcred)) {
        /* did we lose the race? */
        goto _exit2;
    }

    cred->magic = 0; /* no longer a cred */
    kcdb_identity_release(cred->identity);

    EnterCriticalSection(&cs_creds);
    LDELETE(&kcdb_creds, cred);
    LeaveCriticalSection(&cs_creds);

    kcdb_buf_delete(&cred->buf);
    PFREE(cred->name);
    PFREE(cred);

    /*TODO: notifications */

_exit2:
    kcdb_cred_unlock_write();
    return;

_exit:
    kcdb_cred_unlock_read();
}

KHMEXP khm_int32 KHMAPI kcdb_cred_delete(khm_handle vcred)
{
    khm_int32 code = KHM_ERROR_SUCCESS;
    kcdb_cred * cred;

    kcdb_cred_lock_write();

    if(!kcdb_cred_is_active_cred(vcred)) {
        code = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = (kcdb_cred *) vcred;

    cred->flags |= KCDB_CRED_FLAG_DELETED;

_exit:
    kcdb_cred_unlock_write();

    kcdb_cred_check_and_delete(vcred);

    return code;
}

KHMEXP khm_int32 KHMAPI 
kcdb_creds_comp_attrib(khm_handle cred1, 
                       khm_handle cred2, 
                       const wchar_t * name)
{
    khm_int32 attr_id;

    if(KHM_FAILED(kcdb_attrib_get_id(name, &attr_id)))
        return 0;

    return kcdb_creds_comp_attr(cred1, cred2, attr_id);
}

KHMEXP khm_int32 KHMAPI 
kcdb_creds_comp_attr(khm_handle vcred1, 
                     khm_handle vcred2, 
                     khm_int32 attr_id)
{
    khm_int32 code = 0;
    kcdb_cred * cred1;
    kcdb_cred * cred2;
    kcdb_attrib * attrib = NULL;
    kcdb_type * type = NULL;

    if(attr_id < 0 || attr_id > KCDB_ATTR_MAX_ID)
        return 0;

    cred1 = (kcdb_cred *) vcred1;
    cred2 = (kcdb_cred *) vcred2;

    kcdb_cred_lock_read();
    if(
        !kcdb_cred_is_active_cred(vcred1) ||
        !kcdb_cred_is_active_cred(vcred2))
        goto _exit;

    cred1 = (kcdb_cred *) vcred1;
    cred2 = (kcdb_cred *) vcred2;

    if(KHM_FAILED(kcdb_attrib_get_info(attr_id, &attrib)))
        goto _exit;

    if(!(attrib->flags & KCDB_ATTR_FLAG_COMPUTED)) {
        int nc = 0;

        if(!kcdb_cred_val_exist(cred1, attr_id)) {
            code = -1;
            nc = 1;
        }
        if(!kcdb_cred_val_exist(cred2, attr_id)) {
            code += 1;
            nc = 1;
        }

        if(nc)
            goto _exit;
    }

    if(KHM_FAILED(kcdb_type_get_info(attrib->type, &type)))
        goto _exit;

    if(attrib->flags & KCDB_ATTR_FLAG_COMPUTED) {
        khm_octet  vbuf[KCDB_MAXCB_NAME * 2];
        void * buf1 = NULL;
        void * buf2 = NULL;
        khm_size cb1;
        khm_size cb2;

        code = 0;

        if(attrib->compute_cb(vcred1, attr_id, 
                              NULL, &cb1) != KHM_ERROR_TOO_LONG)
            goto _exit_1;

        if(attrib->compute_cb(vcred2, attr_id, 
                              NULL, &cb2) != KHM_ERROR_TOO_LONG)
            goto _exit_1;

        if(cb1) {
            if (cb1 < sizeof(vbuf))
                buf1 = vbuf;
            else
                buf1 = PMALLOC(cb1);

            if(KHM_FAILED(attrib->compute_cb(vcred1, attr_id, buf1, &cb1)))
                goto _exit_1;
        }

        if(cb2) {
            if (cb1 + cb2 < sizeof(vbuf))
                buf2 = vbuf + cb1;
            else
                buf2 = PMALLOC(cb2);

            if(KHM_FAILED(attrib->compute_cb(vcred2, attr_id, buf2, &cb2)))
                goto _exit_1;
        }

        code = type->comp(buf1, cb1,
                          buf2, cb2);
_exit_1:
        if(buf1 && (buf1 < (void *)vbuf || 
                    buf1 >= (void*)(vbuf + sizeof(vbuf))))
            PFREE(buf1);
        if(buf2 && (buf2 < (void *)vbuf ||
                    buf2 >= (void *)(vbuf + sizeof(vbuf))))
            PFREE(buf2);
    } else {
        code = type->comp(
            kcdb_cred_buf_get(cred1, attr_id),
            kcdb_cred_buf_size(cred1, attr_id),
            kcdb_cred_buf_get(cred2, attr_id),
            kcdb_cred_buf_size(cred2, attr_id));
    }

_exit:
    kcdb_cred_unlock_read();
    if(attrib)
        kcdb_attrib_release_info(attrib);
    if(type)
        kcdb_type_release_info(type);
    return code;
}

KHMEXP khm_int32 KHMAPI 
kcdb_creds_is_equal(khm_handle vcred1,
                    khm_handle vcred2)
{
    khm_int32 code = 0;
    kcdb_cred * cred1;
    kcdb_cred * cred2;

    kcdb_cred_lock_read();
    if(!kcdb_cred_is_active_cred(vcred1) ||
       !kcdb_cred_is_active_cred(vcred2)) {

        code = FALSE;
        goto _exit;

    }

    if(vcred1 == vcred2) {

        code = TRUE;
        goto _exit;

    }

    cred1 = vcred1;
    cred2 = vcred2;

    if(cred1->identity == cred2->identity &&
       cred1->type == cred2->type &&
       !wcscmp(cred1->name, cred2->name)) {

        kcdb_credtype * type;

        code = TRUE;

        if (KHM_SUCCEEDED(kcdb_credtype_get_info(cred1->type, &type))) {
            if (type->is_equal &&
                (*type->is_equal)(vcred1, vcred2, NULL))
                code = 0;

            kcdb_credtype_release_info(type);
        }
    }

_exit:
    kcdb_cred_unlock_read();
    return code;
}

KHMEXP khm_int32 KHMAPI 
kcdb_cred_get_flags(khm_handle vcred,
                    khm_int32 * pflags)
{
    khm_int32 f;
    khm_int32 rv = KHM_ERROR_SUCCESS;
    kcdb_cred * cred;
    int release_lock = TRUE;

    if (pflags == NULL)
        return KHM_ERROR_INVALID_PARAM;

    kcdb_cred_lock_read();
    if (!kcdb_cred_is_active_cred(vcred)) {
        *pflags = 0;
        rv = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = vcred;
    f = cred->flags;

    /* Update flags if necessary */

    if (!(f & KCDB_CRED_FLAG_EXPIRED) && 
        kcdb_cred_buf_exist(cred, KCDB_ATTR_EXPIRE)) {

        FILETIME ftc;
            
        GetSystemTimeAsFileTime(&ftc);
        if (CompareFileTime(&ftc, ((FILETIME *) 
                                   kcdb_cred_buf_get(cred, KCDB_ATTR_EXPIRE)))
            >= 0)
            f |= KCDB_CRED_FLAG_EXPIRED;
    }

#if 0
    /* Commented out: if the credential has expired, then checking the
       renewable time is not useful */
    if (!(f & KCDB_CRED_FLAG_INVALID)) {
        if (f & KCDB_CRED_FLAG_RENEWABLE) {
            if (kcdb_cred_buf_exist(cred, KCDB_ATTR_RENEW_EXPIRE)) {
                FILETIME ftc;

                GetSystemTimeAsFileTime(&ftc);
                if (CompareFileTime(&ftc, ((FILETIME *)
                                           kcdb_cred_buf_get(cred, KCDB_ATTR_RENEW_EXPIRE))) >= 0)
                    f |= KCDB_CRED_FLAG_INVALID;
            }
        } else {
            if (f & KCDB_CRED_FLAG_EXPIRED)
                f |= KCDB_CRED_FLAG_INVALID;
        }
    }

    /* Commented out: this is a read operation.  We shouldn't attempt
       to lock for writing */
    if (f != cred->flags) {
        kcdb_cred_unlock_read();
        kcdb_cred_lock_write();
        /* Did we lose a race? */
        if (kcdb_cred_is_active_cred(vcred))
            cred->flags = f;
        else {
            rv = KHM_ERROR_INVALID_PARAM;
            f = 0;
        }
        kcdb_cred_unlock_write();
        release_lock = FALSE;
    }
#endif

    *pflags = f;

 _exit:
    if (release_lock)
        kcdb_cred_unlock_read();

    return rv;
}

KHMEXP khm_int32 KHMAPI kcdb_cred_set_flags(
    khm_handle vcred,
    khm_int32 flags,
    khm_int32 mask)
{
    khm_int32 rv = KHM_ERROR_SUCCESS;
    kcdb_cred * cred;

    kcdb_cred_lock_write();
    if(!kcdb_cred_is_active_cred(vcred)) {
        rv = KHM_ERROR_INVALID_PARAM;
        goto _exit;
    }

    cred = vcred;

    flags &= ~(KCDB_CRED_FLAG_DELETED);
    mask &= ~(KCDB_CRED_FLAG_DELETED);

    cred->flags =
        (cred->flags & (~mask)) |
        (flags & mask);

 _exit:
    kcdb_cred_unlock_write();
    return rv;
}