2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * lib/kdb/kdb_ldap/ldap_principal2.c
2N/A *
2N/A * Copyright (c) 2004-2005, Novell, Inc.
2N/A * All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions are met:
2N/A *
2N/A * * Redistributions of source code must retain the above copyright notice,
2N/A * this list of conditions and the following disclaimer.
2N/A * * Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in the
2N/A * documentation and/or other materials provided with the distribution.
2N/A * * The copyright holder's name is not used to endorse or promote products
2N/A * derived from this software without specific prior written permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2N/A * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2N/A * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2N/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2N/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2N/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2N/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2N/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2N/A * POSSIBILITY OF SUCH DAMAGE.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <time.h>
2N/A#include "ldap_main.h"
2N/A#include "kdb_ldap.h"
2N/A#include "ldap_principal.h"
2N/A#include "princ_xdr.h"
2N/A#include "ldap_tkt_policy.h"
2N/A#include "ldap_pwd_policy.h"
2N/A#include "ldap_err.h"
2N/A#include <kadm5/admin.h>
2N/A#include <libintl.h> /* Solaris Kerberos */
2N/A
2N/Aextern char* principal_attributes[];
2N/Aextern char* max_pwd_life_attr[];
2N/A
2N/Astatic char *
2N/Agetstringtime(krb5_timestamp);
2N/A
2N/Akrb5_error_code
2N/Aberval2tl_data(struct berval *in, krb5_tl_data **out)
2N/A{
2N/A *out = (krb5_tl_data *) malloc (sizeof (krb5_tl_data));
2N/A if (*out == NULL)
2N/A return ENOMEM;
2N/A
2N/A (*out)->tl_data_length = in->bv_len - 2;
2N/A (*out)->tl_data_contents = (krb5_octet *) malloc
2N/A ((*out)->tl_data_length * sizeof (krb5_octet));
2N/A if ((*out)->tl_data_contents == NULL) {
2N/A free (*out);
2N/A return ENOMEM;
2N/A }
2N/A
2N/A UNSTORE16_INT (in->bv_val, (*out)->tl_data_type);
2N/A memcpy ((*out)->tl_data_contents, in->bv_val + 2, (*out)->tl_data_length);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A/* Return true if it's okay to return aliases according to flags. */
2N/Astatic krb5_boolean
2N/Aaliases_ok(unsigned int flags)
2N/A{
2N/A /*
2N/A * The current DAL does not have a flag to indicate whether
2N/A * aliases are okay. For service name lookups (AS or TGT path),
2N/A * we can always return aliases. For client name lookups, we can
2N/A * only return aliases if the client passed the canonicalize flag.
2N/A * We abuse the CLIENT_REFERRALS_ONLY flag to detect client name
2N/A * lookups.
2N/A *
2N/A * This method has the side effect of permitting aliases for
2N/A * lookups by administrative interfaces (e.g. kadmin). Since we
2N/A * don't have explicit admin support for aliases yet, this is
2N/A * okay.
2N/A */
2N/A if (!(flags & KRB5_KDB_FLAG_CLIENT_REFERRALS_ONLY))
2N/A return TRUE;
2N/A if (flags & KRB5_KDB_FLAG_CANONICALIZE)
2N/A return TRUE;
2N/A return FALSE;
2N/A}
2N/A
2N/A/*
2N/A * look up a principal in the directory.
2N/A */
2N/A
2N/Akrb5_error_code
2N/Akrb5_ldap_get_principal(krb5_context context, krb5_const_principal searchfor,
2N/A unsigned int flags, krb5_db_entry *entries,
2N/A int *nentries, krb5_boolean *more)
2N/A{
2N/A char *user=NULL, *filter=NULL, *filtuser=NULL;
2N/A unsigned int tree=0, ntrees=1, princlen=0;
2N/A krb5_error_code tempst=0, st=0;
2N/A char **values=NULL, *cname=NULL, **subtree=NULL;
2N/A LDAP *ld=NULL;
2N/A LDAPMessage *result=NULL, *ent=NULL;
2N/A krb5_ldap_context *ldap_context=NULL;
2N/A kdb5_dal_handle *dal_handle=NULL;
2N/A krb5_ldap_server_handle *ldap_server_handle=NULL;
2N/A krb5_principal cprinc=NULL;
2N/A
2N/A /* Clear the global error string */
2N/A krb5_clear_error_message(context);
2N/A
2N/A /* set initial values */
2N/A *nentries = 0;
2N/A *more = 0;
2N/A memset(entries, 0, sizeof(*entries));
2N/A
2N/A if (searchfor == NULL)
2N/A return EINVAL;
2N/A
2N/A dal_handle = context->dal_handle;
2N/A ldap_context = (krb5_ldap_context *) dal_handle->db_context;
2N/A
2N/A CHECK_LDAP_HANDLE(ldap_context);
2N/A
2N/A if (is_principal_in_realm(ldap_context, searchfor) != 0) {
2N/A st = KRB5_KDB_NOENTRY;
2N/A *more = 0;
2N/A krb5_set_error_message (context, st, gettext("Principal does not belong to realm"));
2N/A goto cleanup;
2N/A }
2N/A
2N/A if ((st=krb5_unparse_name(context, searchfor, &user)) != 0)
2N/A goto cleanup;
2N/A
2N/A if ((st=krb5_ldap_unparse_principal_name(user)) != 0)
2N/A goto cleanup;
2N/A
2N/A filtuser = ldap_filter_correct(user);
2N/A if (filtuser == NULL) {
2N/A st = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A
2N/A princlen = strlen(FILTER) + strlen(filtuser) + 2 + 1; /* 2 for closing brackets */
2N/A if ((filter = malloc(princlen)) == NULL) {
2N/A st = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A snprintf(filter, princlen, FILTER"%s))", filtuser);
2N/A
2N/A if ((st = krb5_get_subtree_info(ldap_context, &subtree, &ntrees)) != 0)
2N/A goto cleanup;
2N/A
2N/A GET_HANDLE();
2N/A for (tree=0; tree < ntrees && *nentries == 0; ++tree) {
2N/A
2N/A LDAP_SEARCH(subtree[tree], ldap_context->lrparams->search_scope, filter, principal_attributes);
2N/A for (ent=ldap_first_entry(ld, result); ent != NULL && *nentries == 0; ent=ldap_next_entry(ld, ent)) {
2N/A
2N/A /* get the associated directory user information */
2N/A if ((values=ldap_get_values(ld, ent, "krbprincipalname")) != NULL) {
2N/A int i;
2N/A
2N/A /* a wild-card in a principal name can return a list of kerberos principals.
2N/A * Make sure that the correct principal is returned.
2N/A * NOTE: a principalname k* in ldap server will return all the principals starting with a k
2N/A */
2N/A for (i=0; values[i] != NULL; ++i) {
2N/A if (strcmp(values[i], user) == 0) {
2N/A *nentries = 1;
2N/A break;
2N/A }
2N/A }
2N/A ldap_value_free(values);
2N/A
2N/A if (*nentries == 0) /* no matching principal found */
2N/A continue;
2N/A }
2N/A
2N/A if ((values=ldap_get_values(ld, ent, "krbcanonicalname")) != NULL) {
2N/A if (values[0] && strcmp(values[0], user) != 0) {
2N/A /* We matched an alias, not the canonical name. */
2N/A if (aliases_ok(flags)) {
2N/A st = krb5_ldap_parse_principal_name(values[0], &cname);
2N/A if (st != 0)
2N/A goto cleanup;
2N/A st = krb5_parse_name(context, cname, &cprinc);
2N/A if (st != 0)
2N/A goto cleanup;
2N/A } else /* No canonicalization, so don't return aliases. */
2N/A *nentries = 0;
2N/A }
2N/A ldap_value_free(values);
2N/A if (*nentries == 0)
2N/A continue;
2N/A }
2N/A
2N/A if ((st = populate_krb5_db_entry(context, ldap_context, ld, ent,
2N/A cprinc ? cprinc : searchfor,
2N/A entries)) != 0)
2N/A goto cleanup;
2N/A }
2N/A ldap_msgfree(result);
2N/A result = NULL;
2N/A } /* for (tree=0 ... */
2N/A
2N/A /* once done, put back the ldap handle */
2N/A krb5_ldap_put_handle_to_pool(ldap_context, ldap_server_handle);
2N/A ldap_server_handle = NULL;
2N/A
2N/Acleanup:
2N/A ldap_msgfree(result);
2N/A
2N/A if (*nentries == 0 || st != 0)
2N/A krb5_dbe_free_contents(context, entries);
2N/A
2N/A if (filter)
2N/A free (filter);
2N/A
2N/A if (subtree) {
2N/A for (; ntrees; --ntrees)
2N/A if (subtree[ntrees-1])
2N/A free (subtree[ntrees-1]);
2N/A free (subtree);
2N/A }
2N/A
2N/A if (ldap_server_handle)
2N/A krb5_ldap_put_handle_to_pool(ldap_context, ldap_server_handle);
2N/A
2N/A if (user)
2N/A free(user);
2N/A
2N/A if (filtuser)
2N/A free(filtuser);
2N/A if (cname)
2N/A free(cname);
2N/A
2N/A if (cprinc)
2N/A krb5_free_principal(context, cprinc);
2N/A
2N/A return st;
2N/A}
2N/A
2N/Atypedef enum{ ADD_PRINCIPAL, MODIFY_PRINCIPAL } OPERATION;
2N/A/*
2N/A * ptype is creating confusions. Additionally the logic
2N/A * surronding ptype is redundunt and can be achevied
2N/A * with the help of dn and containerdn members.
2N/A * so dropping the ptype member
2N/A */
2N/A
2N/Atypedef struct _xargs_t {
2N/A char *dn;
2N/A char *linkdn;
2N/A krb5_boolean dn_from_kbd;
2N/A char *containerdn;
2N/A char *tktpolicydn;
2N/A}xargs_t;
2N/A
2N/Astatic void
2N/Afree_xargs(xargs_t xargs)
2N/A{
2N/A if (xargs.dn)
2N/A free (xargs.dn);
2N/A if (xargs.linkdn)
2N/A free(xargs.linkdn);
2N/A if (xargs.containerdn)
2N/A free (xargs.containerdn);
2N/A if (xargs.tktpolicydn)
2N/A free (xargs.tktpolicydn);
2N/A}
2N/A
2N/Astatic krb5_error_code
2N/Aprocess_db_args(krb5_context context, char **db_args, xargs_t *xargs,
2N/A OPERATION optype)
2N/A{
2N/A int i=0;
2N/A krb5_error_code st=0;
2N/A char errbuf[1024];
2N/A char *arg=NULL, *arg_val=NULL;
2N/A char **dptr=NULL;
2N/A unsigned int arg_val_len=0;
2N/A
2N/A if (db_args) {
2N/A for (i=0; db_args[i]; ++i) {
2N/A arg = strtok_r(db_args[i], "=", &arg_val);
2N/A if (strcmp(arg, TKTPOLICY_ARG) == 0) {
2N/A dptr = &xargs->tktpolicydn;
2N/A } else {
2N/A if (strcmp(arg, USERDN_ARG) == 0) {
2N/A if (optype == MODIFY_PRINCIPAL ||
2N/A xargs->dn != NULL || xargs->containerdn != NULL ||
2N/A xargs->linkdn != NULL) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("%s option not supported"), arg);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A dptr = &xargs->dn;
2N/A } else if (strcmp(arg, CONTAINERDN_ARG) == 0) {
2N/A if (optype == MODIFY_PRINCIPAL ||
2N/A xargs->dn != NULL || xargs->containerdn != NULL) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("%s option not supported"), arg);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A dptr = &xargs->containerdn;
2N/A } else if (strcmp(arg, LINKDN_ARG) == 0) {
2N/A if (xargs->dn != NULL || xargs->linkdn != NULL) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("%s option not supported"), arg);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A dptr = &xargs->linkdn;
2N/A } else {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf), gettext("unknown option: %s"), arg);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A
2N/A xargs->dn_from_kbd = TRUE;
2N/A if (arg_val == NULL || strlen(arg_val) == 0) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("%s option value missing"), arg);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A }
2N/A
2N/A if (arg_val == NULL) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("%s option value missing"), arg);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A arg_val_len = strlen(arg_val) + 1;
2N/A
2N/A if (strcmp(arg, TKTPOLICY_ARG) == 0) {
2N/A if ((st = krb5_ldap_name_to_policydn (context,
2N/A arg_val,
2N/A dptr)) != 0)
2N/A goto cleanup;
2N/A } else {
2N/A *dptr = calloc (1, arg_val_len);
2N/A if (*dptr == NULL) {
2N/A st = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A memcpy(*dptr, arg_val, arg_val_len);
2N/A }
2N/A }
2N/A }
2N/A
2N/Acleanup:
2N/A return st;
2N/A}
2N/A
2N/Akrb5int_access accessor;
2N/A
2N/Astatic krb5_error_code
2N/Aasn1_encode_sequence_of_keys(krb5_key_data *key_data, krb5_int16 n_key_data,
2N/A krb5_int32 mkvno, krb5_data **code)
2N/A{
2N/A krb5_error_code err;
2N/A ldap_seqof_key_data val;
2N/A
2N/A /*
2N/A * This should be pushed back into other library initialization
2N/A * code.
2N/A */
2N/A err = kldap_ensure_initialized ();
2N/A if (err)
2N/A return err;
2N/A
2N/A val.key_data = key_data;
2N/A val.n_key_data = n_key_data;
2N/A val.mkvno = mkvno;
2N/A
2N/A return accessor.asn1_ldap_encode_sequence_of_keys(&val, code);
2N/A}
2N/A
2N/Astatic krb5_error_code
2N/Aasn1_decode_sequence_of_keys(krb5_data *in, krb5_key_data **out,
2N/A krb5_int16 *n_key_data, krb5_kvno *mkvno)
2N/A{
2N/A krb5_error_code err;
2N/A ldap_seqof_key_data *p;
2N/A
2N/A /*
2N/A * This should be pushed back into other library initialization
2N/A * code.
2N/A */
2N/A err = kldap_ensure_initialized ();
2N/A if (err)
2N/A return err;
2N/A
2N/A err = accessor.asn1_ldap_decode_sequence_of_keys(in, &p);
2N/A if (err)
2N/A return err;
2N/A *out = p->key_data;
2N/A *n_key_data = p->n_key_data;
2N/A *mkvno = p->mkvno;
2N/A free(p);
2N/A return 0;
2N/A}
2N/A
2N/A
2N/A/* Decoding ASN.1 encoded key */
2N/Astatic struct berval **
2N/Akrb5_encode_krbsecretkey(krb5_key_data *key_data, int n_key_data,
2N/A krb5_kvno mkvno) {
2N/A struct berval **ret = NULL;
2N/A int currkvno;
2N/A int num_versions = 1;
2N/A int i, j, last;
2N/A krb5_error_code err = 0;
2N/A
2N/A if (n_key_data <= 0)
2N/A return NULL;
2N/A
2N/A /* Find the number of key versions */
2N/A for (i = 0; i < n_key_data - 1; i++)
2N/A if (key_data[i].key_data_kvno != key_data[i + 1].key_data_kvno)
2N/A num_versions++;
2N/A
2N/A ret = (struct berval **) calloc (num_versions + 1, sizeof (struct berval *));
2N/A if (ret == NULL) {
2N/A err = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A for (i = 0, last = 0, j = 0, currkvno = key_data[0].key_data_kvno; i < n_key_data; i++) {
2N/A krb5_data *code;
2N/A if (i == n_key_data - 1 || key_data[i + 1].key_data_kvno != currkvno) {
2N/A asn1_encode_sequence_of_keys (key_data+last,
2N/A (krb5_int16) i - last + 1,
2N/A mkvno,
2N/A &code);
2N/A ret[j] = malloc (sizeof (struct berval));
2N/A if (ret[j] == NULL) {
2N/A err = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A /*CHECK_NULL(ret[j]); */
2N/A ret[j]->bv_len = code->length;
2N/A ret[j]->bv_val = code->data;
2N/A j++;
2N/A last = i + 1;
2N/A
2N/A currkvno = key_data[i].key_data_kvno;
2N/A }
2N/A }
2N/A ret[num_versions] = NULL;
2N/A
2N/Acleanup:
2N/A
2N/A if (err != 0) {
2N/A if (ret != NULL) {
2N/A for (i = 0; i <= num_versions; i++)
2N/A if (ret[i] != NULL)
2N/A free (ret[i]);
2N/A free (ret);
2N/A ret = NULL;
2N/A }
2N/A }
2N/A
2N/A return ret;
2N/A}
2N/A
2N/Astatic krb5_error_code
2N/Atl_data2berval (krb5_tl_data *in, struct berval **out)
2N/A{
2N/A *out = (struct berval *) malloc (sizeof (struct berval));
2N/A if (*out == NULL)
2N/A return ENOMEM;
2N/A
2N/A (*out)->bv_len = in->tl_data_length + 2;
2N/A (*out)->bv_val = (char *) malloc ((*out)->bv_len);
2N/A if ((*out)->bv_val == NULL) {
2N/A free (*out);
2N/A return ENOMEM;
2N/A }
2N/A
2N/A STORE16_INT((*out)->bv_val, in->tl_data_type);
2N/A memcpy ((*out)->bv_val + 2, in->tl_data_contents, in->tl_data_length);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Akrb5_error_code
2N/Akrb5_ldap_put_principal(krb5_context context, krb5_db_entry *entries,
2N/A int *nentries, char **db_args)
2N/A{
2N/A int i=0, l=0, kerberos_principal_object_type=0;
2N/A krb5_error_code st=0, tempst=0;
2N/A LDAP *ld=NULL;
2N/A LDAPMessage *result=NULL, *ent=NULL;
2N/A char *user=NULL, *subtree=NULL, *principal_dn=NULL;
2N/A char **values=NULL, *strval[10]={NULL}, errbuf[1024];
2N/A struct berval **bersecretkey=NULL;
2N/A LDAPMod **mods=NULL;
2N/A krb5_boolean create_standalone_prinicipal=FALSE;
2N/A krb5_boolean krb_identity_exists=FALSE, establish_links=FALSE;
2N/A char *standalone_principal_dn=NULL;
2N/A krb5_tl_data *tl_data=NULL;
2N/A krb5_key_data **keys=NULL;
2N/A kdb5_dal_handle *dal_handle=NULL;
2N/A krb5_ldap_context *ldap_context=NULL;
2N/A krb5_ldap_server_handle *ldap_server_handle=NULL;
2N/A osa_princ_ent_rec princ_ent;
2N/A xargs_t xargs = {0};
2N/A char *polname = NULL;
2N/A OPERATION optype;
2N/A krb5_boolean found_entry = FALSE;
2N/A
2N/A /* Clear the global error string */
2N/A krb5_clear_error_message(context);
2N/A
2N/A SETUP_CONTEXT();
2N/A if (ldap_context->lrparams == NULL || ldap_context->krbcontainer == NULL)
2N/A return EINVAL;
2N/A
2N/A /* get ldap handle */
2N/A GET_HANDLE();
2N/A
2N/A for (i=0; i < *nentries; ++i, ++entries) {
2N/A if (is_principal_in_realm(ldap_context, entries->princ) != 0) {
2N/A st = EINVAL;
2N/A krb5_set_error_message(context, st, gettext("Principal does not belong to the default realm"));
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* get the principal information to act on */
2N/A if (entries->princ) {
2N/A if (((st=krb5_unparse_name(context, entries->princ, &user)) != 0) ||
2N/A ((st=krb5_ldap_unparse_principal_name(user)) != 0))
2N/A goto cleanup;
2N/A }
2N/A
2N/A /* Identity the type of operation, it can be
2N/A * add principal or modify principal.
2N/A * hack if the entries->mask has KRB_PRINCIPAL flag set
2N/A * then it is a add operation
2N/A */
2N/A if (entries->mask & KADM5_PRINCIPAL)
2N/A optype = ADD_PRINCIPAL;
2N/A else
2N/A optype = MODIFY_PRINCIPAL;
2N/A
2N/A if (((st=krb5_get_princ_type(context, entries, &kerberos_principal_object_type)) != 0) ||
2N/A ((st=krb5_get_userdn(context, entries, &principal_dn)) != 0))
2N/A goto cleanup;
2N/A
2N/A if ((st=process_db_args(context, db_args, &xargs, optype)) != 0)
2N/A goto cleanup;
2N/A
2N/A if (entries->mask & KADM5_LOAD) {
2N/A int tree = 0, ntrees = 0, princlen = 0, numlentries = 0;
2N/A char **subtreelist = NULL, *filter = NULL;
2N/A
2N/A /* A load operation is special, will do a mix-in (add krbprinc
2N/A * attrs to a non-krb object entry) if an object exists with a
2N/A * matching krbprincipalname attribute so try to find existing
2N/A * object and set principal_dn. This assumes that the
2N/A * krbprincipalname attribute is unique (only one object entry has
2N/A * a particular krbprincipalname attribute).
2N/A */
2N/A if (user == NULL) {
2N/A /* must have principal name for search */
2N/A st = EINVAL;
2N/A krb5_set_error_message(context, st, gettext("operation can not continue, principal name not found"));
2N/A goto cleanup;
2N/A }
2N/A princlen = strlen(FILTER) + strlen(user) + 2 + 1; /* 2 for closing brackets */
2N/A if ((filter = malloc(princlen)) == NULL) {
2N/A st = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A snprintf(filter, princlen, FILTER"%s))", user);
2N/A
2N/A /* get the current subtree list */
2N/A if ((st = krb5_get_subtree_info(ldap_context, &subtreelist, &ntrees)) != 0)
2N/A goto cleanup;
2N/A
2N/A found_entry = FALSE;
2N/A /* search for entry with matching krbprincipalname attribute */
2N/A for (tree = 0; found_entry == FALSE && tree < ntrees; ++tree) {
2N/A result = NULL;
2N/A if (principal_dn == NULL) {
2N/A LDAP_SEARCH_1(subtreelist[tree], ldap_context->lrparams->search_scope, filter, principal_attributes, IGNORE_STATUS);
2N/A } else {
2N/A /* just look for entry with principal_dn */
2N/A LDAP_SEARCH_1(principal_dn, LDAP_SCOPE_BASE, filter, principal_attributes, IGNORE_STATUS);
2N/A }
2N/A if (st == LDAP_SUCCESS) {
2N/A numlentries = ldap_count_entries(ld, result);
2N/A if (numlentries > 1) {
2N/A ldap_msgfree(result);
2N/A free(filter);
2N/A st = EINVAL;
2N/A krb5_set_error_message(context, st,
2N/A gettext("operation can not continue, more than one entry with principal name \"%s\" found"),
2N/A user);
2N/A goto cleanup;
2N/A } else if (numlentries == 1) {
2N/A found_entry = TRUE;
2N/A if (principal_dn == NULL) {
2N/A ent = ldap_first_entry(ld, result);
2N/A if (ent != NULL) {
2N/A /* setting principal_dn will cause that entry to be modified further down */
2N/A if ((principal_dn = ldap_get_dn(ld, ent)) == NULL) {
2N/A ldap_get_option (ld, LDAP_OPT_RESULT_CODE, &st);
2N/A st = set_ldap_error (context, st, 0);
2N/A ldap_msgfree(result);
2N/A free(filter);
2N/A goto cleanup;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A if (result)
2N/A ldap_msgfree(result);
2N/A } else if (st != LDAP_NO_SUCH_OBJECT) {
2N/A /* could not perform search, return with failure */
2N/A st = set_ldap_error (context, st, 0);
2N/A free(filter);
2N/A goto cleanup;
2N/A }
2N/A /*
2N/A * If it isn't found then assume a standalone princ entry is to
2N/A * be created.
2N/A */
2N/A } /* end for (tree = 0; principal_dn == ... */
2N/A
2N/A free(filter);
2N/A
2N/A if (found_entry == FALSE && principal_dn != NULL) {
2N/A /*
2N/A * if principal_dn is null then there is code further down to
2N/A * deal with setting standalone_principal_dn. Also note that
2N/A * this will set create_standalone_prinicipal true for
2N/A * non-mix-in entries which is okay if loading from a dump.
2N/A */
2N/A create_standalone_prinicipal = TRUE;
2N/A standalone_principal_dn = strdup(principal_dn);
2N/A CHECK_NULL(standalone_principal_dn);
2N/A }
2N/A } /* end if (entries->mask & KADM5_LOAD */
2N/A
2N/A /* time to generate the DN information with the help of
2N/A * containerdn, principalcontainerreference or
2N/A * realmcontainerdn information
2N/A */
2N/A if (principal_dn == NULL && xargs.dn == NULL) { /* creation of standalone principal */
2N/A /* get the subtree information */
2N/A if (entries->princ->length == 2 && entries->princ->data[0].length == strlen("krbtgt") &&
2N/A strncmp(entries->princ->data[0].data, "krbtgt", entries->princ->data[0].length) == 0) {
2N/A /* if the principal is a inter-realm principal, always created in the realm container */
2N/A subtree = strdup(ldap_context->lrparams->realmdn);
2N/A } else if (xargs.containerdn) {
2N/A if ((st=checkattributevalue(ld, xargs.containerdn, NULL, NULL, NULL)) != 0) {
2N/A if (st == KRB5_KDB_NOENTRY || st == KRB5_KDB_CONSTRAINT_VIOLATION) {
2N/A int ost = st;
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf), "'%s' not found: ",
2N/A xargs.containerdn);
2N/A prepend_err_str(context, errbuf, st, ost);
2N/A }
2N/A goto cleanup;
2N/A }
2N/A subtree = strdup(xargs.containerdn);
2N/A } else if (ldap_context->lrparams->containerref && strlen(ldap_context->lrparams->containerref) != 0) {
2N/A /*
2N/A * Here the subtree should be changed with
2N/A * principalcontainerreference attribute value
2N/A */
2N/A subtree = strdup(ldap_context->lrparams->containerref);
2N/A } else {
2N/A subtree = strdup(ldap_context->lrparams->realmdn);
2N/A }
2N/A CHECK_NULL(subtree);
2N/A
2N/A if (asprintf(&standalone_principal_dn, "krbprincipalname=%s,%s",
2N/A user, subtree) < 0)
2N/A standalone_principal_dn = NULL;
2N/A CHECK_NULL(standalone_principal_dn);
2N/A /*
2N/A * free subtree when you are done using the subtree
2N/A * set the boolean create_standalone_prinicipal to TRUE
2N/A */
2N/A create_standalone_prinicipal = TRUE;
2N/A free(subtree);
2N/A subtree = NULL;
2N/A }
2N/A
2N/A /*
2N/A * If the DN information is presented by the user, time to
2N/A * validate the input to ensure that the DN falls under
2N/A * any of the subtrees
2N/A */
2N/A if (xargs.dn_from_kbd == TRUE) {
2N/A /* make sure the DN falls in the subtree */
2N/A int tre=0, dnlen=0, subtreelen=0, ntrees=0;
2N/A char **subtreelist=NULL;
2N/A char *dn=NULL;
2N/A krb5_boolean outofsubtree=TRUE;
2N/A
2N/A if (xargs.dn != NULL) {
2N/A dn = xargs.dn;
2N/A } else if (xargs.linkdn != NULL) {
2N/A dn = xargs.linkdn;
2N/A } else if (standalone_principal_dn != NULL) {
2N/A /*
2N/A * Even though the standalone_principal_dn is constructed
2N/A * within this function, there is the containerdn input
2N/A * from the user that can become part of the it.
2N/A */
2N/A dn = standalone_principal_dn;
2N/A }
2N/A
2N/A /* get the current subtree list */
2N/A if ((st = krb5_get_subtree_info(ldap_context, &subtreelist, &ntrees)) != 0)
2N/A goto cleanup;
2N/A
2N/A for (tre=0; tre<ntrees; ++tre) {
2N/A if (subtreelist[tre] == NULL || strlen(subtreelist[tre]) == 0) {
2N/A outofsubtree = FALSE;
2N/A break;
2N/A } else {
2N/A dnlen = strlen (dn);
2N/A subtreelen = strlen(subtreelist[tre]);
2N/A if ((dnlen >= subtreelen) && (strcasecmp((dn + dnlen - subtreelen), subtreelist[tre]) == 0)) {
2N/A outofsubtree = FALSE;
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A for (tre=0; tre < ntrees; ++tre) {
2N/A free(subtreelist[tre]);
2N/A }
2N/A
2N/A if (outofsubtree == TRUE) {
2N/A st = EINVAL;
2N/A krb5_set_error_message(context, st, gettext("DN is out of the realm subtree"));
2N/A goto cleanup;
2N/A }
2N/A
2N/A /*
2N/A * dn value will be set either by dn, linkdn or the standalone_principal_dn
2N/A * In the first 2 cases, the dn should be existing and in the last case we
2N/A * are supposed to create the ldap object. so the below should not be
2N/A * executed for the last case.
2N/A */
2N/A
2N/A if (standalone_principal_dn == NULL) {
2N/A /*
2N/A * If the ldap object is missing, this results in an error.
2N/A */
2N/A
2N/A /*
2N/A * Search for krbprincipalname attribute here.
2N/A * This is to find if a kerberos identity is already present
2N/A * on the ldap object, in which case adding a kerberos identity
2N/A * on the ldap object should result in an error.
2N/A */
2N/A char *attributes[]={"krbticketpolicyreference", "krbprincipalname", NULL};
2N/A
2N/A LDAP_SEARCH_1(dn, LDAP_SCOPE_BASE, 0, attributes, IGNORE_STATUS);
2N/A if (st == LDAP_SUCCESS) {
2N/A ent = ldap_first_entry(ld, result);
2N/A if (ent != NULL) {
2N/A if ((values=ldap_get_values(ld, ent, "krbticketpolicyreference")) != NULL) {
2N/A ldap_value_free(values);
2N/A }
2N/A
2N/A if ((values=ldap_get_values(ld, ent, "krbprincipalname")) != NULL) {
2N/A krb_identity_exists = TRUE;
2N/A ldap_value_free(values);
2N/A }
2N/A }
2N/A ldap_msgfree(result);
2N/A } else {
2N/A st = set_ldap_error(context, st, OP_SEARCH);
2N/A goto cleanup;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * If xargs.dn is set then the request is to add a
2N/A * kerberos principal on a ldap object, but if
2N/A * there is one already on the ldap object this
2N/A * should result in an error.
2N/A */
2N/A
2N/A if (xargs.dn != NULL && krb_identity_exists == TRUE) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf), gettext("ldap object is already kerberized"));
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (xargs.linkdn != NULL) {
2N/A /*
2N/A * link information can be changed using modprinc.
2N/A * However, link information can be changed only on the
2N/A * standalone kerberos principal objects. A standalone
2N/A * kerberos principal object is of type krbprincipal
2N/A * structural objectclass.
2N/A *
2N/A * NOTE: kerberos principals on an ldap object can't be
2N/A * linked to other ldap objects.
2N/A */
2N/A if (optype == MODIFY_PRINCIPAL &&
2N/A kerberos_principal_object_type != KDB_STANDALONE_PRINCIPAL_OBJECT) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("link information can not be set/updated as the kerberos principal belongs to an ldap object"));
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A /*
2N/A * Check the link information. If there is already a link
2N/A * existing then this operation is not allowed.
2N/A */
2N/A {
2N/A char **linkdns=NULL;
2N/A int j=0;
2N/A
2N/A if ((st=krb5_get_linkdn(context, entries, &linkdns)) != 0) {
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("Failed getting object references"));
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A if (linkdns != NULL) {
2N/A st = EINVAL;
2N/A snprintf(errbuf, sizeof(errbuf),
2N/A gettext("kerberos principal is already linked "
2N/A "to a ldap object"));
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A for (j=0; linkdns[j] != NULL; ++j)
2N/A free (linkdns[j]);
2N/A free (linkdns);
2N/A goto cleanup;
2N/A }
2N/A }
2N/A
2N/A establish_links = TRUE;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_LAST_SUCCESS) {
2N/A memset(strval, 0, sizeof(strval));
2N/A if ((strval[0]=getstringtime(entries->last_success)) == NULL)
2N/A goto cleanup;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbLastSuccessfulAuth", LDAP_MOD_REPLACE, strval)) != 0) {
2N/A free (strval[0]);
2N/A goto cleanup;
2N/A }
2N/A free (strval[0]);
2N/A }
2N/A
2N/A if (entries->mask & KADM5_LAST_FAILED) {
2N/A memset(strval, 0, sizeof(strval));
2N/A if ((strval[0]=getstringtime(entries->last_failed)) == NULL)
2N/A goto cleanup;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbLastFailedAuth", LDAP_MOD_REPLACE, strval)) != 0) {
2N/A free (strval[0]);
2N/A goto cleanup;
2N/A }
2N/A free(strval[0]);
2N/A }
2N/A
2N/A if (entries->mask & KADM5_FAIL_AUTH_COUNT) {
2N/A krb5_kvno fail_auth_count;
2N/A
2N/A fail_auth_count = entries->fail_auth_count;
2N/A if (entries->mask & KADM5_FAIL_AUTH_COUNT_INCREMENT)
2N/A fail_auth_count++;
2N/A
2N/A st = krb5_add_int_mem_ldap_mod(&mods, "krbLoginFailedCount",
2N/A LDAP_MOD_REPLACE,
2N/A fail_auth_count);
2N/A if (st != 0)
2N/A goto cleanup;
2N/A } else if (entries->mask & KADM5_FAIL_AUTH_COUNT_INCREMENT) {
2N/A int attr_mask = 0;
2N/A krb5_boolean has_fail_count;
2N/A
2N/A /* Check if the krbLoginFailedCount attribute exists. (Through
2N/A * krb5 1.8.1, it wasn't set in new entries.) */
2N/A st = krb5_get_attributes_mask(context, entries, &attr_mask);
2N/A if (st != 0)
2N/A goto cleanup;
2N/A has_fail_count = ((attr_mask & KDB_FAIL_AUTH_COUNT_ATTR) != 0);
2N/A
2N/A /*
2N/A * If the client library and server supports RFC 4525,
2N/A * then use it to increment by one the value of the
2N/A * krbLoginFailedCount attribute. Otherwise, assert the
2N/A * (provided) old value by deleting it before adding.
2N/A */
2N/A#ifdef LDAP_MOD_INCREMENT
2N/A if (ldap_server_handle->server_info->modify_increment &&
2N/A has_fail_count) {
2N/A st = krb5_add_int_mem_ldap_mod(&mods, "krbLoginFailedCount",
2N/A LDAP_MOD_INCREMENT, 1);
2N/A if (st != 0)
2N/A goto cleanup;
2N/A } else {
2N/A#endif /* LDAP_MOD_INCREMENT */
2N/A if (has_fail_count) {
2N/A st = krb5_add_int_mem_ldap_mod(&mods,
2N/A "krbLoginFailedCount",
2N/A LDAP_MOD_DELETE,
2N/A entries->fail_auth_count);
2N/A if (st != 0)
2N/A goto cleanup;
2N/A }
2N/A st = krb5_add_int_mem_ldap_mod(&mods, "krbLoginFailedCount",
2N/A LDAP_MOD_ADD,
2N/A entries->fail_auth_count + 1);
2N/A if (st != 0)
2N/A goto cleanup;
2N/A#ifdef LDAP_MOD_INCREMENT
2N/A }
2N/A#endif
2N/A } else if (optype == ADD_PRINCIPAL) {
2N/A /* Initialize krbLoginFailedCount in new entries to help avoid a
2N/A * race during the first failed login. */
2N/A st = krb5_add_int_mem_ldap_mod(&mods, "krbLoginFailedCount",
2N/A LDAP_MOD_ADD, 0);
2N/A }
2N/A
2N/A if (entries->mask & KADM5_MAX_LIFE) {
2N/A if ((st=krb5_add_int_mem_ldap_mod(&mods, "krbmaxticketlife", LDAP_MOD_REPLACE, entries->max_life)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_MAX_RLIFE) {
2N/A if ((st=krb5_add_int_mem_ldap_mod(&mods, "krbmaxrenewableage", LDAP_MOD_REPLACE,
2N/A entries->max_renewable_life)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_ATTRIBUTES) {
2N/A if ((st=krb5_add_int_mem_ldap_mod(&mods, "krbticketflags", LDAP_MOD_REPLACE,
2N/A entries->attributes)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_PRINCIPAL) {
2N/A memset(strval, 0, sizeof(strval));
2N/A strval[0] = user;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbprincipalname", LDAP_MOD_REPLACE, strval)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_PRINC_EXPIRE_TIME) {
2N/A memset(strval, 0, sizeof(strval));
2N/A if ((strval[0]=getstringtime(entries->expiration)) == NULL)
2N/A goto cleanup;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbprincipalexpiration", LDAP_MOD_REPLACE, strval)) != 0) {
2N/A free (strval[0]);
2N/A goto cleanup;
2N/A }
2N/A free (strval[0]);
2N/A }
2N/A
2N/A if (entries->mask & KADM5_PW_EXPIRATION) {
2N/A memset(strval, 0, sizeof(strval));
2N/A if ((strval[0]=getstringtime(entries->pw_expiration)) == NULL)
2N/A goto cleanup;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbpasswordexpiration",
2N/A LDAP_MOD_REPLACE,
2N/A strval)) != 0) {
2N/A free (strval[0]);
2N/A goto cleanup;
2N/A }
2N/A free (strval[0]);
2N/A }
2N/A
2N/A if (entries->mask & KADM5_POLICY) {
2N/A memset(&princ_ent, 0, sizeof(princ_ent));
2N/A for (tl_data=entries->tl_data; tl_data; tl_data=tl_data->tl_data_next) {
2N/A if (tl_data->tl_data_type == KRB5_TL_KADM_DATA) {
2N/A /* FIX ME: I guess the princ_ent should be freed after this call */
2N/A if ((st = krb5_lookup_tl_kadm_data(tl_data, &princ_ent)) != 0) {
2N/A goto cleanup;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (princ_ent.aux_attributes & KADM5_POLICY) {
2N/A memset(strval, 0, sizeof(strval));
2N/A if ((st = krb5_ldap_name_to_policydn (context, princ_ent.policy, &polname)) != 0)
2N/A goto cleanup;
2N/A strval[0] = polname;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbpwdpolicyreference", LDAP_MOD_REPLACE, strval)) != 0)
2N/A goto cleanup;
2N/A } else {
2N/A st = EINVAL;
2N/A krb5_set_error_message(context, st, gettext("Password policy value null"));
2N/A goto cleanup;
2N/A }
2N/A } else if (entries->mask & KADM5_LOAD && found_entry == TRUE) {
2N/A /*
2N/A * a load is special in that existing entries must have attrs that
2N/A * removed.
2N/A */
2N/A
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbpwdpolicyreference", LDAP_MOD_REPLACE, NULL)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_POLICY_CLR) {
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbpwdpolicyreference", LDAP_MOD_DELETE, NULL)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_KEY_DATA || entries->mask & KADM5_KVNO) {
2N/A krb5_kvno mkvno;
2N/A
2N/A if ((st=krb5_dbe_lookup_mkvno(context, entries, &mkvno)) != 0)
2N/A goto cleanup;
2N/A bersecretkey = krb5_encode_krbsecretkey (entries->key_data,
2N/A entries->n_key_data, mkvno);
2N/A
2N/A if ((st=krb5_add_ber_mem_ldap_mod(&mods, "krbprincipalkey",
2N/A LDAP_MOD_REPLACE | LDAP_MOD_BVALUES, bersecretkey)) != 0)
2N/A goto cleanup;
2N/A
2N/A if (!(entries->mask & KADM5_PRINCIPAL)) {
2N/A memset(strval, 0, sizeof(strval));
2N/A if ((strval[0]=getstringtime(entries->pw_expiration)) == NULL)
2N/A goto cleanup;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods,
2N/A "krbpasswordexpiration",
2N/A LDAP_MOD_REPLACE, strval)) != 0) {
2N/A free (strval[0]);
2N/A goto cleanup;
2N/A }
2N/A free (strval[0]);
2N/A }
2N/A
2N/A /* Update last password change whenever a new key is set */
2N/A {
2N/A krb5_timestamp last_pw_changed;
2N/A if ((st=krb5_dbe_lookup_last_pwd_change(context, entries,
2N/A &last_pw_changed)) != 0)
2N/A goto cleanup;
2N/A
2N/A memset(strval, 0, sizeof(strval));
2N/A if ((strval[0] = getstringtime(last_pw_changed)) == NULL)
2N/A goto cleanup;
2N/A
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbLastPwdChange",
2N/A LDAP_MOD_REPLACE, strval)) != 0) {
2N/A free (strval[0]);
2N/A goto cleanup;
2N/A }
2N/A free (strval[0]);
2N/A }
2N/A
2N/A } /* Modify Key data ends here */
2N/A
2N/A /* Set tl_data */
2N/A if (entries->tl_data != NULL) {
2N/A int count = 0;
2N/A struct berval **ber_tl_data = NULL;
2N/A krb5_tl_data *ptr;
2N/A for (ptr = entries->tl_data; ptr != NULL; ptr = ptr->tl_data_next) {
2N/A if (ptr->tl_data_type == KRB5_TL_LAST_PWD_CHANGE
2N/A#ifdef SECURID
2N/A || ptr->tl_data_type == KRB5_TL_DB_ARGS
2N/A#endif
2N/A || ptr->tl_data_type == KRB5_TL_KADM_DATA
2N/A || ptr->tl_data_type == KDB_TL_USER_INFO
2N/A || ptr->tl_data_type == KRB5_TL_CONSTRAINED_DELEGATION_ACL)
2N/A continue;
2N/A count++;
2N/A }
2N/A if (count != 0) {
2N/A int j;
2N/A ber_tl_data = (struct berval **) calloc (count + 1,
2N/A sizeof (struct berval*));
2N/A if (ber_tl_data == NULL) {
2N/A st = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A for (j = 0, ptr = entries->tl_data; ptr != NULL; ptr = ptr->tl_data_next) {
2N/A /* Ignore tl_data that are stored in separate directory
2N/A * attributes */
2N/A if (ptr->tl_data_type == KRB5_TL_LAST_PWD_CHANGE
2N/A#ifdef SECURID
2N/A || ptr->tl_data_type == KRB5_TL_DB_ARGS
2N/A#endif
2N/A || ptr->tl_data_type == KRB5_TL_KADM_DATA
2N/A || ptr->tl_data_type == KDB_TL_USER_INFO
2N/A || ptr->tl_data_type == KRB5_TL_CONSTRAINED_DELEGATION_ACL)
2N/A continue;
2N/A if ((st = tl_data2berval (ptr, &ber_tl_data[j])) != 0)
2N/A break;
2N/A j++;
2N/A }
2N/A if (st != 0) {
2N/A for (j = 0; ber_tl_data[j] != NULL; j++) {
2N/A free (ber_tl_data[j]->bv_val);
2N/A free (ber_tl_data[j]);
2N/A }
2N/A free (ber_tl_data);
2N/A goto cleanup;
2N/A }
2N/A ber_tl_data[count] = NULL;
2N/A if ((st=krb5_add_ber_mem_ldap_mod(&mods, "krbExtraData",
2N/A LDAP_MOD_REPLACE | LDAP_MOD_BVALUES,
2N/A ber_tl_data)) != 0)
2N/A goto cleanup;
2N/A }
2N/A }
2N/A
2N/A /* Directory specific attribute */
2N/A if (xargs.tktpolicydn != NULL) {
2N/A int tmask=0;
2N/A
2N/A if (strlen(xargs.tktpolicydn) != 0) {
2N/A st = checkattributevalue(ld, xargs.tktpolicydn, "objectclass", policyclass, &tmask);
2N/A CHECK_CLASS_VALIDITY(st, tmask, "ticket policy object value: ");
2N/A
2N/A strval[0] = xargs.tktpolicydn;
2N/A strval[1] = NULL;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbticketpolicyreference", LDAP_MOD_REPLACE, strval)) != 0)
2N/A goto cleanup;
2N/A
2N/A } else {
2N/A /* if xargs.tktpolicydn is a empty string, then delete
2N/A * already existing krbticketpolicyreference attr */
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbticketpolicyreference", LDAP_MOD_DELETE, NULL)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A }
2N/A
2N/A if (establish_links == TRUE) {
2N/A memset(strval, 0, sizeof(strval));
2N/A strval[0] = xargs.linkdn;
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "krbObjectReferences", LDAP_MOD_REPLACE, strval)) != 0)
2N/A goto cleanup;
2N/A }
2N/A
2N/A /*
2N/A * in case mods is NULL then return
2N/A * not sure but can happen in a modprinc
2N/A * so no need to return an error
2N/A * addprinc will at least have the principal name
2N/A * and the keys passed in
2N/A */
2N/A if (mods == NULL)
2N/A goto cleanup;
2N/A
2N/A if (create_standalone_prinicipal == TRUE) {
2N/A memset(strval, 0, sizeof(strval));
2N/A strval[0] = "krbprincipal";
2N/A strval[1] = "krbprincipalaux";
2N/A strval[2] = "krbTicketPolicyAux";
2N/A
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "objectclass", LDAP_MOD_ADD, strval)) != 0)
2N/A goto cleanup;
2N/A
2N/A st = ldap_add_ext_s(ld, standalone_principal_dn, mods, NULL, NULL);
2N/A if (st == LDAP_ALREADY_EXISTS && entries->mask & KADM5_LOAD) {
2N/A /* a load operation must replace an existing entry */
2N/A st = ldap_delete_ext_s(ld, standalone_principal_dn, NULL, NULL);
2N/A if (st != LDAP_SUCCESS) {
2N/A snprintf(errbuf, sizeof(errbuf), gettext("Principal delete failed (trying to replace entry): %s"),
2N/A ldap_err2string(st));
2N/A st = translate_ldap_error (st, OP_ADD);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A } else {
2N/A st = ldap_add_ext_s(ld, standalone_principal_dn, mods, NULL, NULL);
2N/A }
2N/A }
2N/A if (st != LDAP_SUCCESS) {
2N/A snprintf(errbuf, sizeof(errbuf), gettext("Principal add failed: %s"), ldap_err2string(st));
2N/A st = translate_ldap_error (st, OP_ADD);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A } else {
2N/A /*
2N/A * Here existing ldap object is modified and can be related
2N/A * to any attribute, so always ensure that the ldap
2N/A * object is extended with all the kerberos related
2N/A * objectclasses so that there are no constraint
2N/A * violations.
2N/A */
2N/A {
2N/A char *attrvalues[] = {"krbprincipalaux", "krbTicketPolicyAux", NULL};
2N/A int p, q, r=0, amask=0;
2N/A
2N/A if ((st=checkattributevalue(ld, (xargs.dn) ? xargs.dn : principal_dn,
2N/A "objectclass", attrvalues, &amask)) != 0)
2N/A goto cleanup;
2N/A
2N/A memset(strval, 0, sizeof(strval));
2N/A for (p=1, q=0; p<=2; p<<=1, ++q) {
2N/A if ((p & amask) == 0)
2N/A strval[r++] = attrvalues[q];
2N/A }
2N/A if (r != 0) {
2N/A if ((st=krb5_add_str_mem_ldap_mod(&mods, "objectclass", LDAP_MOD_ADD, strval)) != 0)
2N/A goto cleanup;
2N/A }
2N/A }
2N/A if (xargs.dn != NULL)
2N/A st=ldap_modify_ext_s(ld, xargs.dn, mods, NULL, NULL);
2N/A else
2N/A st = ldap_modify_ext_s(ld, principal_dn, mods, NULL, NULL);
2N/A
2N/A if (st != LDAP_SUCCESS) {
2N/A snprintf(errbuf, sizeof(errbuf), gettext("User modification failed: %s"), ldap_err2string(st));
2N/A st = translate_ldap_error (st, OP_MOD);
2N/A krb5_set_error_message(context, st, "%s", errbuf);
2N/A goto cleanup;
2N/A }
2N/A
2N/A if (entries->mask & KADM5_FAIL_AUTH_COUNT_INCREMENT)
2N/A entries->fail_auth_count++;
2N/A }
2N/A }
2N/A
2N/Acleanup:
2N/A if (user)
2N/A free(user);
2N/A
2N/A free_xargs(xargs);
2N/A
2N/A if (standalone_principal_dn)
2N/A free(standalone_principal_dn);
2N/A
2N/A if (principal_dn)
2N/A free (principal_dn);
2N/A
2N/A if (polname != NULL)
2N/A free(polname);
2N/A
2N/A if (subtree)
2N/A free (subtree);
2N/A
2N/A if (bersecretkey) {
2N/A for (l=0; bersecretkey[l]; ++l) {
2N/A if (bersecretkey[l]->bv_val)
2N/A free (bersecretkey[l]->bv_val);
2N/A free (bersecretkey[l]);
2N/A }
2N/A free (bersecretkey);
2N/A }
2N/A
2N/A if (keys)
2N/A free (keys);
2N/A
2N/A ldap_mods_free(mods, 1);
2N/A krb5_ldap_put_handle_to_pool(ldap_context, ldap_server_handle);
2N/A *nentries = i;
2N/A return(st);
2N/A}
2N/A
2N/Akrb5_error_code
2N/Akrb5_read_tkt_policy(krb5_context context, krb5_ldap_context *ldap_context,
2N/A krb5_db_entry *entries, char *policy)
2N/A{
2N/A krb5_error_code st=0;
2N/A unsigned int mask=0, omask=0;
2N/A int tkt_mask=(KDB_MAX_LIFE_ATTR | KDB_MAX_RLIFE_ATTR | KDB_TKT_FLAGS_ATTR);
2N/A krb5_ldap_policy_params *tktpoldnparam=NULL;
2N/A
2N/A if ((st=krb5_get_attributes_mask(context, entries, &mask)) != 0)
2N/A goto cleanup;
2N/A
2N/A if ((mask & tkt_mask) == tkt_mask)
2N/A goto cleanup;
2N/A
2N/A if (policy != NULL) {
2N/A st = krb5_ldap_read_policy(context, policy, &tktpoldnparam, &omask);
2N/A if (st && st != KRB5_KDB_NOENTRY) {
2N/A prepend_err_str(context, gettext("Error reading ticket policy. "), st, st);
2N/A goto cleanup;
2N/A }
2N/A
2N/A st = 0; /* reset the return status */
2N/A }
2N/A
2N/A if ((mask & KDB_MAX_LIFE_ATTR) == 0) {
2N/A if ((omask & KDB_MAX_LIFE_ATTR) == KDB_MAX_LIFE_ATTR)
2N/A entries->max_life = tktpoldnparam->maxtktlife;
2N/A else if (ldap_context->lrparams->max_life)
2N/A entries->max_life = ldap_context->lrparams->max_life;
2N/A }
2N/A
2N/A if ((mask & KDB_MAX_RLIFE_ATTR) == 0) {
2N/A if ((omask & KDB_MAX_RLIFE_ATTR) == KDB_MAX_RLIFE_ATTR)
2N/A entries->max_renewable_life = tktpoldnparam->maxrenewlife;
2N/A else if (ldap_context->lrparams->max_renewable_life)
2N/A entries->max_renewable_life = ldap_context->lrparams->max_renewable_life;
2N/A }
2N/A
2N/A if ((mask & KDB_TKT_FLAGS_ATTR) == 0) {
2N/A if ((omask & KDB_TKT_FLAGS_ATTR) == KDB_TKT_FLAGS_ATTR)
2N/A entries->attributes = tktpoldnparam->tktflags;
2N/A else if (ldap_context->lrparams->tktflags)
2N/A entries->attributes |= ldap_context->lrparams->tktflags;
2N/A }
2N/A krb5_ldap_free_policy(context, tktpoldnparam);
2N/A
2N/Acleanup:
2N/A return st;
2N/A}
2N/A
2N/Akrb5_error_code
2N/Akrb5_decode_krbsecretkey(krb5_context context, krb5_db_entry *entries,
2N/A struct berval **bvalues,
2N/A krb5_tl_data *userinfo_tl_data, krb5_kvno *mkvno)
2N/A{
2N/A char *user=NULL;
2N/A int i=0, j=0, noofkeys=0;
2N/A krb5_key_data *key_data=NULL, *tmp;
2N/A krb5_error_code st=0;
2N/A
2N/A if ((st=krb5_unparse_name(context, entries->princ, &user)) != 0)
2N/A goto cleanup;
2N/A
2N/A for (i=0; bvalues[i] != NULL; ++i) {
2N/A krb5_int16 n_kd;
2N/A krb5_key_data *kd;
2N/A krb5_data in;
2N/A
2N/A if (bvalues[i]->bv_len == 0)
2N/A continue;
2N/A in.length = bvalues[i]->bv_len;
2N/A in.data = bvalues[i]->bv_val;
2N/A
2N/A st = asn1_decode_sequence_of_keys (&in,
2N/A &kd,
2N/A &n_kd,
2N/A mkvno);
2N/A
2N/A if (st != 0) {
2N/A const char *msg = error_message(st);
2N/A st = -1; /* Something more appropriate ? */
2N/A krb5_set_error_message (context, st,
2N/A gettext("unable to decode stored principal key data (%s)"), msg);
2N/A goto cleanup;
2N/A }
2N/A noofkeys += n_kd;
2N/A tmp = key_data;
2N/A key_data = realloc (key_data, noofkeys * sizeof (krb5_key_data));
2N/A if (key_data == NULL) {
2N/A key_data = tmp;
2N/A st = ENOMEM;
2N/A goto cleanup;
2N/A }
2N/A for (j = 0; j < n_kd; j++)
2N/A key_data[noofkeys - n_kd + j] = kd[j];
2N/A free (kd);
2N/A }
2N/A
2N/A entries->n_key_data = noofkeys;
2N/A entries->key_data = key_data;
2N/A
2N/Acleanup:
2N/A ldap_value_free_len(bvalues);
2N/A free (user);
2N/A return st;
2N/A}
2N/A
2N/Astatic char *
2N/Agetstringtime(krb5_timestamp epochtime)
2N/A{
2N/A struct tm tme;
2N/A char *strtime=NULL;
2N/A time_t posixtime = epochtime;
2N/A
2N/A strtime = calloc (50, 1);
2N/A if (strtime == NULL)
2N/A return NULL;
2N/A
2N/A if (gmtime_r(&posixtime, &tme) == NULL) {
2N/A /* Solaris Kerberos */
2N/A free(strtime);
2N/A return NULL;
2N/A }
2N/A
2N/A strftime(strtime, 50, "%Y%m%d%H%M%SZ", &tme);
2N/A return strtime;
2N/A}