int create_krb5_ccache()

in src/kerberosgss.c [940:1007]


int create_krb5_ccache(
    gss_server_state *state, krb5_context kcontext, krb5_principal princ,
    krb5_ccache *ccache
) {
    int fd;
    char ccname[32];
    krb5_error_code problem;
    int ret;
    krb5_ccache tmp_ccache = NULL;

    snprintf(ccname, sizeof(ccname), "/tmp/krb5cc_pyserv_XXXXXX");
    fd = mkstemp(ccname);
    if (fd < 0) {
        PyErr_SetObject(
            KrbException_class,
            Py_BuildValue("(s:s)", "Error in mkstemp", strerror(errno))
        );
        ret = 1;
        goto end;
    }
    close(fd);

    problem = krb5_cc_resolve(kcontext, ccname, &tmp_ccache);
    if (problem) {
        PyErr_SetObject(
            KrbException_class,
            Py_BuildValue(
                "(s:s)", "Error resolving the credential cache",
                krb5_get_err_text(kcontext, problem)
            )
        );
        ret = 1;
        unlink(ccname);
        goto end;
    }

    problem = krb5_cc_initialize(kcontext, tmp_ccache, princ);
    if (problem) {
        PyErr_SetObject(
            KrbException_class,
            Py_BuildValue(
                "(s:s)", "Error initialising the credential cache",
                krb5_get_err_text(kcontext, problem)
            )
        );
        ret = 1;
        goto end;
    }

    *ccache = tmp_ccache;
    tmp_ccache = NULL;

    ret = 0;

end:
    if (tmp_ccache) {
        krb5_cc_destroy(kcontext, tmp_ccache);
    }

    state->ccname = (char *)malloc(32*sizeof(char));
    if (state->ccname == NULL) {
        PyErr_NoMemory();
        return 1;
    }
    strcpy(state->ccname, ccname);

    return ret;
}