Path: blob/main/crypto/krb5/src/lib/kadm5/clnt/client_principal.c
39566 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved3*4* $Header$5*/67#include <gssrpc/rpc.h>8#include <kadm5/admin.h>9#include <kadm5/kadm_rpc.h>10#ifdef HAVE_MEMORY_H11#include <memory.h>12#endif13#include <string.h>14#include <errno.h>15#include "client_internal.h"1617#ifdef DEBUG18#define eret() do { clnt_perror(handle->clnt, "null ret"); return KADM5_RPC_ERROR; } while (0)19#else20#define eret() do { return KADM5_RPC_ERROR; } while (0)21#endif2223kadm5_ret_t24kadm5_create_principal(void *server_handle,25kadm5_principal_ent_t princ, long mask,26char *pw)27{28generic_ret r = { 0, 0 };29cprinc_arg arg;30kadm5_server_handle_t handle = server_handle;3132CHECK_HANDLE(server_handle);3334memset(&arg, 0, sizeof(arg));35arg.mask = mask;36arg.passwd = pw;37arg.api_version = handle->api_version;3839if(princ == NULL)40return EINVAL;4142memcpy(&arg.rec, princ, sizeof(kadm5_principal_ent_rec));43arg.rec.mod_name = NULL;4445if(!(mask & KADM5_POLICY))46arg.rec.policy = NULL;47if (! (mask & KADM5_KEY_DATA)) {48arg.rec.n_key_data = 0;49arg.rec.key_data = NULL;50}51if (! (mask & KADM5_TL_DATA)) {52arg.rec.n_tl_data = 0;53arg.rec.tl_data = NULL;54}5556if (create_principal_2(&arg, &r, handle->clnt))57eret();58return r.code;59}6061kadm5_ret_t62kadm5_create_principal_3(void *server_handle,63kadm5_principal_ent_t princ, long mask,64int n_ks_tuple,65krb5_key_salt_tuple *ks_tuple,66char *pw)67{68generic_ret r = { 0, 0 };69cprinc3_arg arg;70kadm5_server_handle_t handle = server_handle;7172CHECK_HANDLE(server_handle);7374memset(&arg, 0, sizeof(arg));75arg.mask = mask;76arg.passwd = pw;77arg.api_version = handle->api_version;78arg.n_ks_tuple = n_ks_tuple;79arg.ks_tuple = ks_tuple;8081if(princ == NULL)82return EINVAL;8384memcpy(&arg.rec, princ, sizeof(kadm5_principal_ent_rec));85arg.rec.mod_name = NULL;8687if(!(mask & KADM5_POLICY))88arg.rec.policy = NULL;89if (! (mask & KADM5_KEY_DATA)) {90arg.rec.n_key_data = 0;91arg.rec.key_data = NULL;92}93if (! (mask & KADM5_TL_DATA)) {94arg.rec.n_tl_data = 0;95arg.rec.tl_data = NULL;96}9798if (create_principal3_2(&arg, &r, handle->clnt))99eret();100return r.code;101}102103kadm5_ret_t104kadm5_delete_principal(void *server_handle, krb5_principal principal)105{106dprinc_arg arg;107generic_ret r = { 0, 0 };108kadm5_server_handle_t handle = server_handle;109110CHECK_HANDLE(server_handle);111112if(principal == NULL)113return EINVAL;114arg.princ = principal;115arg.api_version = handle->api_version;116if (delete_principal_2(&arg, &r, handle->clnt))117eret();118return r.code;119}120121kadm5_ret_t122kadm5_modify_principal(void *server_handle,123kadm5_principal_ent_t princ, long mask)124{125mprinc_arg arg;126generic_ret r = { 0, 0 };127kadm5_server_handle_t handle = server_handle;128129CHECK_HANDLE(server_handle);130131memset(&arg, 0, sizeof(arg));132arg.mask = mask;133arg.api_version = handle->api_version;134if(princ == NULL)135return EINVAL;136memcpy(&arg.rec, princ, sizeof(kadm5_principal_ent_rec));137if(!(mask & KADM5_POLICY))138arg.rec.policy = NULL;139if (! (mask & KADM5_KEY_DATA)) {140arg.rec.n_key_data = 0;141arg.rec.key_data = NULL;142}143if (! (mask & KADM5_TL_DATA)) {144arg.rec.n_tl_data = 0;145arg.rec.tl_data = NULL;146}147148arg.rec.mod_name = NULL;149150if (modify_principal_2(&arg, &r, handle->clnt))151eret();152return r.code;153}154155kadm5_ret_t156kadm5_get_principal(void *server_handle,157krb5_principal princ, kadm5_principal_ent_t ent,158long mask)159{160gprinc_arg arg;161gprinc_ret r;162kadm5_server_handle_t handle = server_handle;163164CHECK_HANDLE(server_handle);165166if(princ == NULL)167return EINVAL;168arg.princ = princ;169arg.mask = mask;170arg.api_version = handle->api_version;171memset(&r, 0, sizeof(gprinc_ret));172if (get_principal_2(&arg, &r, handle->clnt))173eret();174if (r.code == 0)175memcpy(ent, &r.rec, sizeof(r.rec));176177return r.code;178}179180kadm5_ret_t181kadm5_get_principals(void *server_handle,182char *exp, char ***princs, int *count)183{184gprincs_arg arg;185gprincs_ret r;186kadm5_server_handle_t handle = server_handle;187188CHECK_HANDLE(server_handle);189190if(princs == NULL || count == NULL)191return EINVAL;192arg.exp = exp;193arg.api_version = handle->api_version;194memset(&r, 0, sizeof(gprincs_ret));195if (get_princs_2(&arg, &r, handle->clnt))196eret();197if (r.code == 0) {198*count = r.count;199*princs = r.princs;200} else {201*count = 0;202*princs = NULL;203}204205return r.code;206}207208kadm5_ret_t209kadm5_rename_principal(void *server_handle,210krb5_principal source, krb5_principal dest)211{212rprinc_arg arg;213generic_ret r = { 0, 0 };214kadm5_server_handle_t handle = server_handle;215216CHECK_HANDLE(server_handle);217218arg.src = source;219arg.dest = dest;220arg.api_version = handle->api_version;221if (source == NULL || dest == NULL)222return EINVAL;223if (rename_principal_2(&arg, &r, handle->clnt))224eret();225return r.code;226}227228kadm5_ret_t229kadm5_chpass_principal(void *server_handle,230krb5_principal princ, char *password)231{232chpass_arg arg;233generic_ret r = { 0, 0 };234kadm5_server_handle_t handle = server_handle;235236CHECK_HANDLE(server_handle);237238arg.princ = princ;239arg.pass = password;240arg.api_version = handle->api_version;241242if(princ == NULL)243return EINVAL;244if (chpass_principal_2(&arg, &r, handle->clnt))245eret();246return r.code;247}248249kadm5_ret_t250kadm5_chpass_principal_3(void *server_handle,251krb5_principal princ, unsigned int keepold,252int n_ks_tuple, krb5_key_salt_tuple *ks_tuple,253char *password)254{255chpass3_arg arg;256generic_ret r = { 0, 0 };257kadm5_server_handle_t handle = server_handle;258259CHECK_HANDLE(server_handle);260261arg.princ = princ;262arg.pass = password;263arg.api_version = handle->api_version;264arg.keepold = keepold;265arg.n_ks_tuple = n_ks_tuple;266arg.ks_tuple = ks_tuple;267268if(princ == NULL)269return EINVAL;270if (chpass_principal3_2(&arg, &r, handle->clnt))271eret();272return r.code;273}274275kadm5_ret_t276kadm5_setkey_principal(void *server_handle,277krb5_principal princ,278krb5_keyblock *keyblocks,279int n_keys)280{281setkey_arg arg;282generic_ret r = { 0, 0 };283kadm5_server_handle_t handle = server_handle;284285CHECK_HANDLE(server_handle);286287arg.princ = princ;288arg.keyblocks = keyblocks;289arg.n_keys = n_keys;290arg.api_version = handle->api_version;291292if(princ == NULL || keyblocks == NULL)293return EINVAL;294if (setkey_principal_2(&arg, &r, handle->clnt))295eret();296return r.code;297}298299kadm5_ret_t300kadm5_setkey_principal_3(void *server_handle,301krb5_principal princ,302unsigned int keepold, int n_ks_tuple,303krb5_key_salt_tuple *ks_tuple,304krb5_keyblock *keyblocks,305int n_keys)306{307setkey3_arg arg;308generic_ret r = { 0, 0 };309kadm5_server_handle_t handle = server_handle;310311CHECK_HANDLE(server_handle);312313arg.princ = princ;314arg.keyblocks = keyblocks;315arg.n_keys = n_keys;316arg.api_version = handle->api_version;317arg.keepold = keepold;318arg.n_ks_tuple = n_ks_tuple;319arg.ks_tuple = ks_tuple;320321if(princ == NULL || keyblocks == NULL)322return EINVAL;323if (setkey_principal3_2(&arg, &r, handle->clnt))324eret();325return r.code;326}327328kadm5_ret_t329kadm5_setkey_principal_4(void *server_handle,330krb5_principal princ,331unsigned int keepold,332kadm5_key_data *key_data,333int n_key_data)334{335setkey4_arg arg;336generic_ret r = { 0, 0 };337kadm5_server_handle_t handle = server_handle;338339CHECK_HANDLE(server_handle);340341arg.api_version = handle->api_version;342arg.princ = princ;343arg.keepold = keepold;344arg.key_data = key_data;345arg.n_key_data = n_key_data;346347if (princ == NULL || key_data == NULL || n_key_data == 0)348return EINVAL;349if (setkey_principal4_2(&arg, &r, handle->clnt))350eret();351return r.code;352}353354kadm5_ret_t355kadm5_randkey_principal_3(void *server_handle,356krb5_principal princ,357unsigned int keepold, int n_ks_tuple,358krb5_key_salt_tuple *ks_tuple,359krb5_keyblock **key, int *n_keys)360{361chrand3_arg arg;362chrand_ret r;363kadm5_server_handle_t handle = server_handle;364int i;365366CHECK_HANDLE(server_handle);367368arg.princ = princ;369arg.api_version = handle->api_version;370arg.keepold = keepold;371arg.n_ks_tuple = n_ks_tuple;372arg.ks_tuple = ks_tuple;373374if(princ == NULL)375return EINVAL;376memset(&r, 0, sizeof(chrand_ret));377if (chrand_principal3_2(&arg, &r, handle->clnt))378eret();379if (n_keys)380*n_keys = r.n_keys;381if (key) {382*key = r.keys;383} else {384for (i = 0; i < r.n_keys; i++)385krb5_free_keyblock_contents(handle->context, &r.keys[i]);386free(r.keys);387}388return r.code;389}390391kadm5_ret_t392kadm5_randkey_principal(void *server_handle,393krb5_principal princ,394krb5_keyblock **key, int *n_keys)395{396chrand_arg arg;397chrand_ret r;398kadm5_server_handle_t handle = server_handle;399int i;400401CHECK_HANDLE(server_handle);402403arg.princ = princ;404arg.api_version = handle->api_version;405406if(princ == NULL)407return EINVAL;408memset(&r, 0, sizeof(chrand_ret));409if (chrand_principal_2(&arg, &r, handle->clnt))410eret();411if (n_keys)412*n_keys = r.n_keys;413if (key) {414*key = r.keys;415} else {416for (i = 0; i < r.n_keys; i++)417krb5_free_keyblock_contents(handle->context, &r.keys[i]);418free(r.keys);419}420return r.code;421}422423/* not supported on client side */424kadm5_ret_t kadm5_decrypt_key(void *server_handle,425kadm5_principal_ent_t entry, krb5_int32426ktype, krb5_int32 stype, krb5_int32427kvno, krb5_keyblock *keyblock,428krb5_keysalt *keysalt, int *kvnop)429{430return EINVAL;431}432433kadm5_ret_t434kadm5_purgekeys(void *server_handle,435krb5_principal princ,436int keepkvno)437{438purgekeys_arg arg;439generic_ret r = { 0, 0 };440kadm5_server_handle_t handle = server_handle;441442CHECK_HANDLE(server_handle);443444arg.princ = princ;445arg.keepkvno = keepkvno;446arg.api_version = handle->api_version;447448if (princ == NULL)449return EINVAL;450if (purgekeys_2(&arg, &r, handle->clnt))451eret();452return r.code;453}454455kadm5_ret_t456kadm5_get_strings(void *server_handle, krb5_principal principal,457krb5_string_attr **strings_out, int *count_out)458{459gstrings_arg arg;460gstrings_ret r;461kadm5_server_handle_t handle = server_handle;462463*strings_out = NULL;464*count_out = 0;465CHECK_HANDLE(server_handle);466if (principal == NULL)467return EINVAL;468469arg.princ = principal;470arg.api_version = handle->api_version;471memset(&r, 0, sizeof(gstrings_ret));472if (get_strings_2(&arg, &r, handle->clnt))473eret();474if (r.code == 0) {475*strings_out = r.strings;476*count_out = r.count;477}478return r.code;479}480481kadm5_ret_t482kadm5_set_string(void *server_handle, krb5_principal principal,483const char *key, const char *value)484{485sstring_arg arg;486generic_ret r = { 0, 0 };487kadm5_server_handle_t handle = server_handle;488489CHECK_HANDLE(server_handle);490if (principal == NULL || key == NULL)491return EINVAL;492493arg.princ = principal;494arg.key = (char *)key;495arg.value = (char *)value;496arg.api_version = handle->api_version;497if (set_string_2(&arg, &r, handle->clnt))498eret();499return r.code;500}501502kadm5_ret_t503kadm5_get_principal_keys(void *server_handle, krb5_principal princ,504krb5_kvno kvno, kadm5_key_data **key_data,505int *n_key_data)506{507getpkeys_arg arg;508getpkeys_ret r;509kadm5_server_handle_t handle = server_handle;510511CHECK_HANDLE(server_handle);512513arg.api_version = handle->api_version;514arg.princ = princ;515arg.kvno = kvno;516517if (princ == NULL || key_data == NULL || n_key_data == 0)518return EINVAL;519memset(&r, 0, sizeof(getpkeys_ret));520if (get_principal_keys_2(&arg, &r, handle->clnt))521eret();522if (r.code == 0) {523*key_data = r.key_data;524*n_key_data = r.n_key_data;525}526return r.code;527}528529kadm5_ret_t530kadm5_create_alias(void *server_handle, krb5_principal alias,531krb5_principal target)532{533calias_arg arg;534generic_ret r = { 0, 0 };535kadm5_server_handle_t handle = server_handle;536537CHECK_HANDLE(server_handle);538539arg.alias = alias;540arg.target = target;541arg.api_version = handle->api_version;542if (alias == NULL || target == NULL)543return EINVAL;544if (create_alias_2(&arg, &r, handle->clnt))545eret();546return r.code;547}548549550