rename_s.c revision 178825
120253Sjoerg/*
220302Sjoerg * Copyright (c) 1997 - 2001, 2003, 2005 - 2005 Kungliga Tekniska H�gskolan
320302Sjoerg * (Royal Institute of Technology, Stockholm, Sweden).
420253Sjoerg * All rights reserved.
520253Sjoerg *
620253Sjoerg * Redistribution and use in source and binary forms, with or without
720253Sjoerg * modification, are permitted provided that the following conditions
820253Sjoerg * are met:
920302Sjoerg *
1020253Sjoerg * 1. Redistributions of source code must retain the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer.
1220253Sjoerg *
1320253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1420302Sjoerg *    notice, this list of conditions and the following disclaimer in the
1520253Sjoerg *    documentation and/or other materials provided with the distribution.
1620253Sjoerg *
1720302Sjoerg * 3. Neither the name of the Institute nor the names of its contributors
1820253Sjoerg *    may be used to endorse or promote products derived from this software
1920253Sjoerg *    without specific prior written permission.
2020253Sjoerg *
2120253Sjoerg * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2220253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2320253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2420253Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2544229Sdavidn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2620253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2720253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2830259Scharnier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2930259Scharnier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3050479Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3130259Scharnier * SUCH DAMAGE.
3230259Scharnier */
3330259Scharnier
3430259Scharnier#include "kadm5_locl.h"
3520253Sjoerg
3620253SjoergRCSID("$Id: rename_s.c 21745 2007-07-31 16:11:25Z lha $");
3720253Sjoerg
3830259Scharnierkadm5_ret_t
3920253Sjoergkadm5_s_rename_principal(void *server_handle,
4020555Sdavidn			 krb5_principal source,
4120555Sdavidn			 krb5_principal target)
4220555Sdavidn{
4330259Scharnier    kadm5_server_context *context = server_handle;
4422394Sdavidn    kadm5_ret_t ret;
4564918Sgreen    hdb_entry_ex ent;
4622394Sdavidn    krb5_principal oldname;
4720555Sdavidn
4822394Sdavidn    memset(&ent, 0, sizeof(ent));
4920253Sjoerg    if(krb5_principal_compare(context->context, source, target))
5020253Sjoerg	return KADM5_DUP; /* XXX is this right? */
5120253Sjoerg    ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
5223318Sache    if(ret)
5322394Sdavidn	return ret;
5422394Sdavidn    ret = context->db->hdb_fetch(context->context, context->db,
5523318Sache				 source, HDB_F_GET_ANY, &ent);
5622394Sdavidn    if(ret){
5722394Sdavidn	context->db->hdb_close(context->context, context->db);
5852512Sdavidn	goto out;
5924214Sache    }
6044386Sdavidn    ret = _kadm5_set_modifier(context, &ent.entry);
6120253Sjoerg    if(ret)
6220253Sjoerg	goto out2;
6320253Sjoerg    {
6420253Sjoerg	/* fix salt */
6520253Sjoerg	int i;
6620253Sjoerg	Salt salt;
6720253Sjoerg	krb5_salt salt2;
6820253Sjoerg	krb5_get_pw_salt(context->context, source, &salt2);
6920253Sjoerg	salt.type = hdb_pw_salt;
7085145Sache	salt.salt = salt2.saltvalue;
7120253Sjoerg	for(i = 0; i < ent.entry.keys.len; i++){
7220253Sjoerg	    if(ent.entry.keys.val[i].salt == NULL){
7320253Sjoerg		ent.entry.keys.val[i].salt =
7420253Sjoerg		    malloc(sizeof(*ent.entry.keys.val[i].salt));
7520253Sjoerg		if(ent.entry.keys.val[i].salt == NULL)
7620253Sjoerg		    return ENOMEM;
7720253Sjoerg		ret = copy_Salt(&salt, ent.entry.keys.val[i].salt);
7820253Sjoerg		if(ret)
7920253Sjoerg		    break;
8020253Sjoerg	    }
8120253Sjoerg	}
8220253Sjoerg	krb5_free_salt(context->context, salt2);
8320253Sjoerg    }
8420253Sjoerg    if(ret)
8520253Sjoerg	goto out2;
8620253Sjoerg    oldname = ent.entry.principal;
8720253Sjoerg    ent.entry.principal = target;
8820253Sjoerg
89124382Siedowse    ret = hdb_seal_keys(context->context, context->db, &ent.entry);
9020253Sjoerg    if (ret) {
9120253Sjoerg	ent.entry.principal = oldname;
9220253Sjoerg	goto out2;
9320253Sjoerg    }
9420253Sjoerg
9520253Sjoerg    kadm5_log_rename (context, source, &ent.entry);
9620253Sjoerg
9720253Sjoerg    ret = context->db->hdb_store(context->context, context->db, 0, &ent);
9820253Sjoerg    if(ret){
9920253Sjoerg	ent.entry.principal = oldname;
10020253Sjoerg	goto out2;
10120253Sjoerg    }
10220253Sjoerg    ret = context->db->hdb_remove(context->context, context->db, oldname);
10320253Sjoerg    ent.entry.principal = oldname;
10420253Sjoergout2:
10520253Sjoerg    context->db->hdb_close(context->context, context->db);
10620253Sjoerg    hdb_free_entry(context->context, &ent);
10752527Sdavidnout:
10820253Sjoerg    return _kadm5_error_code(ret);
10952512Sdavidn}
11020253Sjoerg
11120253Sjoerg