示例#1
0
static PyKAdminObject *_kadmin_init_with_ccache(PyObject *self, PyObject *args) {
    
    PyKAdminObject *kadmin = PyKAdminObject_create();
    PyObject *db_args_dict = NULL;
    kadm5_ret_t retval = KADM5_OK;
    krb5_error_code code = 0;

    krb5_principal princ = NULL;
    char *ccache_name    = NULL;
    char *client_name    = NULL;
    char **db_args       = NULL;

    krb5_ccache cc;             

    kadm5_config_params *params = calloc(0x1, sizeof(kadm5_config_params));

    memset(&cc, 0, sizeof(krb5_ccache));

    // TODO : unpack database args as an optional third parameter (will be a dict or array)
    if (!PyArg_ParseTuple(args, "|zzO!", &client_name, &ccache_name, &PyDict_Type, &db_args_dict))
        return NULL; 

    db_args = _kadmin_dict_to_db_args(db_args_dict);

    if (!ccache_name) {
        code = krb5_cc_default(kadmin->context, &cc);
        if (code) { PyKAdmin_RETURN_ERROR(code, "krb5_cc_default"); }
    } else {
        code = krb5_cc_resolve(kadmin->context, ccache_name, &cc);
        if (code) { PyKAdmin_RETURN_ERROR(code, "krb5_cc_resolve"); }
    } 

    if (!client_name) {
        code = krb5_cc_get_principal(kadmin->context, cc, &princ);
        if (code) { PyKAdmin_RETURN_ERROR(code, "krb5_cc_get_principal"); }

        code = krb5_unparse_name(kadmin->context, princ, &client_name);
        if (code) { PyKAdmin_RETURN_ERROR(code, "krb5_unparse_name"); }

        krb5_free_principal(kadmin->context, princ);
    }
    
    retval = kadm5_init_with_creds(
                kadmin->context, 
                client_name, 
                cc, 
                service_name, 
                params,
                struct_version, 
                api_version, 
                db_args, 
                &kadmin->server_handle);

    if (db_args) 
        _kadmin_free_db_args(db_args);

    if (retval != KADM5_OK) { PyKAdmin_RETURN_ERROR(retval, "kadm5_init_with_creds"); }

    Py_XINCREF(kadmin);
    return kadmin;
}
示例#2
0
static PyKAdminObject *_kadmin_init_with_ccache(PyObject *self, PyObject *args) {
    
    PyKAdminObject *kadmin = NULL;
    PyObject *py_db_args   = NULL;
    kadm5_ret_t retval     = KADM5_OK;
    krb5_error_code code   = 0;

    krb5_principal princ   = NULL;
    char *ccache_name      = NULL;
    char *client_name      = NULL;
    char *_resolved_client = NULL;
    char **db_args         = NULL;

    kadm5_config_params *params = NULL;

    krb5_ccache cc;             

    memset(&cc, 0, sizeof(krb5_ccache));

    // TODO : unpack database args as an optional third parameter (will be a dict or array)
    if (!PyArg_ParseTuple(args, "|zzO", &client_name, &ccache_name, &py_db_args))
        return NULL; 

    kadmin = PyKAdminObject_create();
    params = calloc(0x1, sizeof(kadm5_config_params));

    db_args = pykadmin_parse_db_args(py_db_args);

    if (!ccache_name) {
        code = krb5_cc_default(kadmin->context, &cc);
        if (code) { 
            PyKAdminError_raise_error(code, "krb5_cc_default");
            goto cleanup;
        }
    } else {
        code = krb5_cc_resolve(kadmin->context, ccache_name, &cc);
        if (code) { 
            PyKAdminError_raise_error(code, "krb5_cc_resolve");
            goto cleanup;
        }
    } 

    _resolved_client = client_name;

    if (!_resolved_client) {
        code = krb5_cc_get_principal(kadmin->context, cc, &princ);
        if (code) { 
            PyKAdminError_raise_error(code, "krb5_cc_get_principal");
            goto cleanup;
        }

        code = krb5_unparse_name(kadmin->context, princ, &_resolved_client);
        if (code) { 
            PyKAdminError_raise_error(code, "krb5_unparse_name");
            goto cleanup;
        }
    }
    
    retval = kadm5_init_with_creds(
                kadmin->context, 
                _resolved_client, 
                cc, 
                service_name, 
                params,
                struct_version, 
                api_version, 
                db_args, 
                &kadmin->server_handle);


cleanup:
    
    // we only clean up _resolved_client if we calculated it, otherwise it is 
    //  an internal pointer of a python object and freeing it will be illegal.
    if ((client_name == NULL) && _resolved_client)
        free(_resolved_client);

    krb5_free_principal(kadmin->context, princ);
    krb5_cc_close(kadmin->context, cc);

    if (retval != KADM5_OK) {

        Py_XDECREF(kadmin);
        kadmin = NULL;

        PyKAdminError_raise_error(retval, "kadm5_init_with_creds");
    }

    if (params)
        free(params);  

    pykadmin_free_db_args(db_args);

    return kadmin;
}