int
main(int argc, char **argv)
{
    KLLoginOptions options;
    KLPrincipal princ;
    KLStatus ret;
    KLBoolean foundV5;
    KLIdleCallback idlecall;
    KLRefCon refcon;

    if (argc != 2)
	errx(1, "argc != 2");

    printf("test NULL argument\n");
    ret = KLCreatePrincipalFromString(NULL, kerberosVersion_V5, &princ);
    if (ret == 0)
	errx(1, "KLCreatePrincipalFromString: %d", ret);

    printf("create principal\n");
    ret = KLCreatePrincipalFromString(argv[1],
				      kerberosVersion_V5, &princ);
    if (ret)
	errx(1, "KLCreatePrincipalFromString: %d", ret);

    printf("acquire cred\n");

    KLCreateLoginOptions(&options);
    KLLoginOptionsSetRenewableLifetime(options, 3600 * 24 * 7);

    ret = KLAcquireInitialTickets(princ, options, NULL, NULL);
    if (ret)
	errx(1, "KLAcquireTicketsWithPassword: %d", ret);

    KLDisposeLoginOptions(options);

    printf("get valid tickets\n");
    ret = KLCacheHasValidTickets(princ, kerberosVersion_V5, &foundV5, NULL, NULL);
    if (ret)
	errx(1, "KLCacheHasValidTickets failed");
    else if (!foundV5)
	errx(1, "found no valid tickets");

    printf("renew tickets\n");
    ret = KLRenewInitialTickets(princ, NULL, NULL, NULL);
    if (ret)
	errx(1, "KLRenewInitialTickets: %d", ret);
    KLDisposePrincipal(princ);

    printf("test callbacks\n");
    ret = KLGetIdleCallback(&idlecall, &refcon);
    if (ret != klNoErr)
	errx(1, "KLGetIdleCallback: %d", ret);

    ret = KLSetIdleCallback(NULL, refcon);
    if (ret != klNoErr)
	errx(1, "KLSetIdleCallback: %d", ret);


    return 0;
}
Beispiel #2
0
void TestKLPrincipal (void)
{
    KLStatus err = klNoErr;
    KLPrincipal extraLongPrincipal = NULL;
    KLPrincipal	principal = NULL;
    KLPrincipal adminPrincipal = NULL;
    KLPrincipal adminPrincipalV4 = NULL;
    KLPrincipal adminPrincipalV5 = NULL;
    char *principalString = NULL;
    char *user = NULL;
    char *instance = NULL;
    char *realm = NULL;

    printf ("Entering TestKLPrincipal()\n");
    printf ("----------------------------------------------------------------\n");

    err = KLCreatePrincipalFromString ("thisprincipalnameislongerthanissupportedbyKerberos4@TEST-KERBEROS-1.3.1",
                                       kerberosVersion_V5, &extraLongPrincipal);
    printf ("KLCreatePrincipalFromString "
            "('thisprincipalnameislongerthanissupportedbyKerberos4@TEST-KERBEROS-1.3.1') "
            "(err = %s)\n", error_message(err));

    printf ("----------------------------------------------------------------\n");

    err = KLCreatePrincipalFromTriplet ("nobody", "", "TEST-KERBEROS-1.3.1", &principal);
    printf ("KLCreatePrincipalFromTriplet ('nobody' '' 'TEST-KERBEROS-1.3.1') (err = %s)\n",
            error_message(err));

    if (err == klNoErr) {
        err = KLGetStringFromPrincipal (principal, kerberosVersion_V5, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal ([email protected], v5) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal([email protected], v5) returned (err = %s)\n", error_message(err));
        }

        err = KLGetStringFromPrincipal (principal, kerberosVersion_V4, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal ([email protected], v4) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal([email protected], v4) returned (err = %s)\n", error_message(err));
        }

        err = KLGetTripletFromPrincipal (principal, &user, &instance, &realm);
        if (err == klNoErr) {
            printf ("KLGetTripletFromPrincipal ([email protected]) returned triplet %s' '%s' '%s'\n",
                    user, instance, realm);
            KLDisposeString (user);
            KLDisposeString (instance);
            KLDisposeString (realm);
        } else {
            printf ("KLGetTripletFromPrincipal([email protected]) returned (err = %s)\n", error_message(err));
        }
    }

    printf ("----------------------------------------------------------------\n");

    err = KLCreatePrincipalFromTriplet ("nobody", "admin", "TEST-KERBEROS-1.3.1", &adminPrincipal);
    printf ("KLCreatePrincipalFromTriplet ('nobody' 'admin' 'TEST-KERBEROS-1.3.1') (err = %d)\n", err);

    if (err == klNoErr) {
        err = KLGetStringFromPrincipal (adminPrincipal, kerberosVersion_V5, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal (nobody/[email protected], v5) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal(nobody/[email protected], v5) returned (err = %d)\n", err);
        }

        err = KLGetStringFromPrincipal (adminPrincipal, kerberosVersion_V4, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal (nobody/[email protected], v4) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal(nobody/[email protected], v4) returned (err = %d)\n", err);
        }

        err = KLGetTripletFromPrincipal (adminPrincipal, &user, &instance, &realm);
        if (err == klNoErr) {
            printf ("KLGetTripletFromPrincipal (nobody/[email protected]) returned triplet %s' '%s' '%s'\n",
                    user, instance, realm);
            KLDisposeString (user);
            KLDisposeString (instance);
            KLDisposeString (realm);
        } else {
            printf ("KLGetTripletFromPrincipal(lxs/[email protected]) returned (err = %d)\n", err);
        }
    }

    printf ("----------------------------------------------------------------\n");

    err = KLCreatePrincipalFromString ("nobody/[email protected]", kerberosVersion_V5, &adminPrincipalV5);
    printf ("KLCreatePrincipalFromString ('nobody/[email protected]', v5) (err = %d)\n", err);
    if (err == klNoErr) {
        err = KLGetStringFromPrincipal (adminPrincipalV5, kerberosVersion_V5, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal (nobody/[email protected], v5) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal(nobody/[email protected], v5) returned (err = %d)\n", err);
        }

        err = KLGetStringFromPrincipal (adminPrincipalV5, kerberosVersion_V4, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal (nobody/[email protected], v4) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal(nobody/[email protected], v4) returned (err = %d)\n", err);
        }

        err = KLGetTripletFromPrincipal (adminPrincipalV5, &user, &instance, &realm);
        if (err == klNoErr) {
            printf ("KLGetTripletFromPrincipal (nobody/[email protected]) returned triplet %s' '%s' '%s'\n",
                    user, instance, realm);
            KLDisposeString (user);
            KLDisposeString (instance);
            KLDisposeString (realm);
        } else {
            printf ("KLGetTripletFromPrincipal(nobody/[email protected]) returned (err = %d)\n", err);
        }
    }

    printf ("----------------------------------------------------------------\n");

    err = KLCreatePrincipalFromString ("[email protected]", kerberosVersion_V4, &adminPrincipalV4);
    printf ("KLCreatePrincipalFromString ('[email protected]') (err = %d)\n", err);
    if (err == klNoErr) {
        err = KLGetStringFromPrincipal (adminPrincipalV4, kerberosVersion_V5, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal ([email protected], v5) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal([email protected], v5) returned (err = %d)\n", err);
        }

        err = KLGetStringFromPrincipal (adminPrincipalV4, kerberosVersion_V4, &principalString);
        if (err == klNoErr) {
            printf ("KLGetStringFromPrincipal ([email protected], v4) returned string '%s'\n", principalString);
            KLDisposeString (principalString);
        } else {
            printf ("KLGetStringFromPrincipal([email protected], v4) returned (err = %d)\n", err);
        }

        err = KLGetTripletFromPrincipal (adminPrincipalV4, &user, &instance, &realm);
        if (err == klNoErr) {
            printf ("KLGetTripletFromPrincipal ([email protected]) returned triplet %s' '%s' '%s'\n",
                    user, instance, realm);
            KLDisposeString (user);
            KLDisposeString (instance);
            KLDisposeString (realm);
        } else {
            printf ("KLGetTripletFromPrincipal([email protected]) returned (err = %d)\n", err);
        }
    }

    printf ("----------------------------------------------------------------\n");

    if (adminPrincipalV4 != NULL && adminPrincipalV5 != NULL) {
        KLBoolean equivalent;

        err = KLComparePrincipal (adminPrincipalV5, adminPrincipalV4, &equivalent);
        if (err == klNoErr) {
            printf ("KLComparePrincipal %s comparing nobody/[email protected] and [email protected]\n",
                    equivalent ? "passed" : "FAILED");
        } else {
            printf ("KLComparePrincipal returned (err = %d)\n", err);
        }
    }

    if (principal != NULL && adminPrincipalV5 != NULL) {
        KLBoolean equivalent;

        err = KLComparePrincipal (principal, adminPrincipalV4, &equivalent);
        if (err == klNoErr) {
            printf ("KLComparePrincipal %s comparing [email protected] and [email protected]\n",
                    equivalent ? "FAILED" : "passed");
        } else {
            printf ("KLComparePrincipal returned (err = %d)\n", err);
        }
    }

    if (principal != NULL && adminPrincipalV5 != NULL) {
        KLBoolean equivalent;

        err = KLComparePrincipal (principal, adminPrincipalV5, &equivalent);
        if (err == klNoErr) {
            printf ("KLComparePrincipal %s comparing [email protected] and nobody/[email protected]\n",
                    equivalent ? "FAILED" : "passed");
        } else {
            printf ("KLComparePrincipal returned (err = %d)\n", err);
        }
    }

    if (adminPrincipal != NULL && adminPrincipalV5 != NULL) {
        KLBoolean equivalent;

        err = KLComparePrincipal (adminPrincipalV5, principal, &equivalent);
        if (err == klNoErr) {
            printf ("KLComparePrincipal %s comparing nobody/[email protected] and [email protected]\n",
                    equivalent ? "FAILED" : "passed");
        } else {
            printf ("KLComparePrincipal returned (err = %d)\n", err);
        }
    }

    printf ("----------------------------------------------------------------\n\n");

    if (extraLongPrincipal != NULL) KLDisposePrincipal (extraLongPrincipal);
    if (adminPrincipalV5   != NULL) KLDisposePrincipal (adminPrincipalV5);
    if (adminPrincipalV4   != NULL) KLDisposePrincipal (adminPrincipalV4);
    if (adminPrincipal     != NULL) KLDisposePrincipal (adminPrincipal);
    if (principal          != NULL) KLDisposePrincipal (principal);
}