155682Smarkm/*
2233294Sstas * Copyright (c) 1997-2001, 2003, 2005-2006 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
955682Smarkm *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2055682Smarkm *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "kadm5_locl.h"
3555682Smarkm
36233294SstasRCSID("$Id$");
3755682Smarkm
3855682Smarkmstatic kadm5_ret_t
3955682Smarkmmodify_principal(void *server_handle,
40233294Sstas		 kadm5_principal_ent_t princ,
41178825Sdfr		 uint32_t mask,
42178825Sdfr		 uint32_t forbidden_mask)
4355682Smarkm{
4455682Smarkm    kadm5_server_context *context = server_handle;
45178825Sdfr    hdb_entry_ex ent;
4655682Smarkm    kadm5_ret_t ret;
4755682Smarkm    if((mask & forbidden_mask))
4855682Smarkm	return KADM5_BAD_MASK;
4955682Smarkm    if((mask & KADM5_POLICY) && strcmp(princ->policy, "default"))
5055682Smarkm	return KADM5_UNK_POLICY;
51233294Sstas
52178825Sdfr    memset(&ent, 0, sizeof(ent));
53178825Sdfr    ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
5455682Smarkm    if(ret)
5555682Smarkm	return ret;
56233294Sstas    ret = context->db->hdb_fetch_kvno(context->context, context->db,
57233294Sstas				      princ->principal, HDB_F_GET_ANY|HDB_F_ADMIN_DATA, 0, &ent);
5855682Smarkm    if(ret)
5955682Smarkm	goto out;
6072445Sassar    ret = _kadm5_setup_entry(context, &ent, mask, princ, mask, NULL, 0);
6155682Smarkm    if(ret)
6255682Smarkm	goto out2;
63178825Sdfr    ret = _kadm5_set_modifier(context, &ent.entry);
6455682Smarkm    if(ret)
6555682Smarkm	goto out2;
6655682Smarkm
67178825Sdfr    ret = hdb_seal_keys(context->context, context->db, &ent.entry);
6872445Sassar    if (ret)
6972445Sassar	goto out2;
7055682Smarkm
71233294Sstas    ret = context->db->hdb_store(context->context, context->db,
72178825Sdfr			     HDB_F_REPLACE, &ent);
73178825Sdfr    if (ret)
74178825Sdfr	goto out2;
75178825Sdfr
7655682Smarkm    kadm5_log_modify (context,
77178825Sdfr		      &ent.entry,
7855682Smarkm		      mask | KADM5_MOD_NAME | KADM5_MOD_TIME);
79178825Sdfr
8055682Smarkmout2:
8155682Smarkm    hdb_free_entry(context->context, &ent);
8255682Smarkmout:
83178825Sdfr    context->db->hdb_close(context->context, context->db);
8455682Smarkm    return _kadm5_error_code(ret);
8555682Smarkm}
8655682Smarkm
8755682Smarkm
8855682Smarkmkadm5_ret_t
8955682Smarkmkadm5_s_modify_principal(void *server_handle,
90233294Sstas			 kadm5_principal_ent_t princ,
91178825Sdfr			 uint32_t mask)
9255682Smarkm{
93233294Sstas    return modify_principal(server_handle, princ, mask,
94233294Sstas			    KADM5_LAST_PWD_CHANGE | KADM5_MOD_TIME
95233294Sstas			    | KADM5_MOD_NAME | KADM5_MKVNO
9655682Smarkm			    | KADM5_AUX_ATTRIBUTES | KADM5_LAST_SUCCESS
9755682Smarkm			    | KADM5_LAST_FAILED);
9855682Smarkm}
99