Path: blob/main/crypto/krb5/src/kadmin/dbutil/kadm5_create.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.3*4* $Source$5*/67/*8* Copyright (C) 1998 by the FundsXpress, INC.9*10* All rights reserved.11*12* Export of this software from the United States of America may require13* a specific license from the United States Government. It is the14* responsibility of any person or organization contemplating export to15* obtain such a license before exporting.16*17* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and18* distribute this software and its documentation for any purpose and19* without fee is hereby granted, provided that the above copyright20* notice appear in all copies and that both that copyright notice and21* this permission notice appear in supporting documentation, and that22* the name of FundsXpress. not be used in advertising or publicity pertaining23* to distribution of the software without specific, written prior24* permission. FundsXpress makes no representations about the suitability of25* this software for any purpose. It is provided "as is" without express26* or implied warranty.27*28* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR29* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED30* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.31*/3233#include <k5-int.h>34#include <ctype.h>35#include <kdb.h>36#include <kadm5/admin.h>37#include <adm_proto.h>3839#include "fake-addrinfo.h"404142#include <krb5.h>43#include <kdb.h>44#include "kdb5_util.h"4546static int add_admin_princ(void *handle, krb5_context context,47char *name, char *realm, int attrs, int lifetime);48static int add_admin_princs(void *handle, krb5_context context, char *realm);4950#define ERR 151#define OK 05253#define ADMIN_LIFETIME 60*60*3 /* 3 hours */54#define CHANGEPW_LIFETIME 60*5 /* 5 minutes */5556/*57* Function: kadm5_create58*59* Purpose: create admin principals in KDC database60*61* Arguments: params (r) configuration parameters to use62*63* Effects: Creates KADM5_ADMIN_SERVICE and KADM5_CHANGEPW_SERVICE64* principals in the KDC database and sets their attributes65* appropriately.66*/67int kadm5_create(kadm5_config_params *params)68{69int retval;70kadm5_config_params lparams;7172/*73* The lock file has to exist before calling kadm5_init, but74* params->admin_lockfile may not be set yet...75*/76retval = kadm5_get_config_params(util_context, 1, params, &lparams);77if (retval) {78com_err(progname, retval, _("while looking up the Kerberos "79"configuration"));80return 1;81}8283retval = kadm5_create_magic_princs(&lparams, util_context);8485kadm5_free_config_params(util_context, &lparams);8687return retval;88}8990int kadm5_create_magic_princs(kadm5_config_params *params,91krb5_context context)92{93int retval;94void *handle;9596retval = krb5_klog_init(context, "admin_server", progname, 0);97if (retval)98return retval;99if ((retval = kadm5_init(context, progname, NULL, NULL, params,100KADM5_STRUCT_VERSION,101KADM5_API_VERSION_4,102db5util_db_args,103&handle))) {104com_err(progname, retval, _("while initializing the Kerberos admin "105"interface"));106return retval;107}108109retval = add_admin_princs(handle, context, params->realm);110111kadm5_destroy(handle);112113krb5_klog_close(context);114115return retval;116}117118/*119* Function: add_admin_princs120*121* Purpose: create admin principals122*123* Arguments:124*125* rseed (input) random seed126* realm (input) realm, or NULL for default realm127* <return value> (output) status, 0 for success, 1 for serious error128*129* Requires:130*131* Effects:132*133* add_admin_princs creates KADM5_ADMIN_SERVICE,134* KADM5_CHANGEPW_SERVICE. If any of these exist a message is135* printed. If any of these existing principal do not have the proper136* attributes, a warning message is printed.137*/138static int add_admin_princs(void *handle, krb5_context context, char *realm)139{140krb5_error_code ret = 0;141142if ((ret = add_admin_princ(handle, context,143KADM5_ADMIN_SERVICE, realm,144KRB5_KDB_DISALLOW_TGT_BASED |145KRB5_KDB_LOCKDOWN_KEYS,146ADMIN_LIFETIME)))147return ret;148149return add_admin_princ(handle, context, KADM5_CHANGEPW_SERVICE, realm,150KRB5_KDB_DISALLOW_TGT_BASED |151KRB5_KDB_PWCHANGE_SERVICE | KRB5_KDB_LOCKDOWN_KEYS,152CHANGEPW_LIFETIME);153}154155/*156* Function: add_admin_princ157*158* Arguments:159*160* creator (r) principal to use as "mod_by"161* rseed (r) seed for random key generator162* name (r) principal name163* realm (r) realm name for principal164* attrs (r) principal's attributes165* lifetime (r) principal's max life, or 0166* not_unique (r) error message for multiple entries, never used167* exists (r) warning message for principal exists168* wrong_attrs (r) warning message for wrong attributes169*170* Returns:171*172* OK on success173* ERR on serious errors174*175* Effects:176*177* If the principal is not unique, not_unique is printed (but this178* never happens). If the principal exists, then exists is printed179* and if the principals attributes != attrs, wrong_attrs is printed.180* Otherwise, the principal is created with mod_by creator and181* attributes attrs and max life of lifetime (if not zero).182*/183184int add_admin_princ(void *handle, krb5_context context,185char *name, char *realm, int attrs, int lifetime)186{187char *fullname = NULL;188krb5_error_code ret;189kadm5_principal_ent_rec ent;190long flags;191int fret;192193memset(&ent, 0, sizeof(ent));194195if (asprintf(&fullname, "%s@%s", name, realm) < 0) {196com_err(progname, ENOMEM, _("while appending realm to principal"));197fret = ERR;198goto cleanup;199}200ret = krb5_parse_name(context, fullname, &ent.principal);201if (ret) {202com_err(progname, ret, _("while parsing admin principal name"));203fret = ERR;204goto cleanup;205}206ent.max_life = lifetime;207ent.attributes = attrs;208209flags = KADM5_PRINCIPAL | KADM5_ATTRIBUTES;210if (lifetime)211flags |= KADM5_MAX_LIFE;212ret = kadm5_create_principal(handle, &ent, flags, NULL);213if (ret && ret != KADM5_DUP) {214com_err(progname, ret, _("while creating principal %s"), fullname);215fret = ERR;216goto cleanup;217}218219fret = OK;220cleanup:221krb5_free_principal(context, ent.principal);222free(fullname);223return fret;224}225226227