1#pragma ident	"%Z%%M%	%I%	%E% SMI"
2
3/*
4 * lib/kdb/kdb_ldap/ldap_fetch_mkey.c
5 *
6 * Copyright (c) 2004-2005, Novell, Inc.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *   * Redistributions of source code must retain the above copyright notice,
13 *       this list of conditions and the following disclaimer.
14 *   * Redistributions in binary form must reproduce the above copyright
15 *       notice, this list of conditions and the following disclaimer in the
16 *       documentation and/or other materials provided with the distribution.
17 *   * The copyright holder's name is not used to endorse or promote products
18 *       derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "ldap_main.h"
34#include "kdb_ldap.h"
35
36/*
37 * get the master key from the database specific context
38 */
39
40krb5_error_code
41krb5_ldap_get_mkey (context, key)
42    krb5_context               context;
43    krb5_keyblock              **key;
44
45{
46    kdb5_dal_handle             *dal_handle=NULL;
47    krb5_ldap_context           *ldap_context=NULL;
48
49    /* Clear the global error string */
50    krb5_clear_error_message(context);
51
52    dal_handle = (kdb5_dal_handle *) context->db_context;
53    ldap_context = (krb5_ldap_context *) dal_handle->db_context;
54
55    if (ldap_context == NULL || ldap_context->lrparams == NULL)
56	return KRB5_KDB_DBNOTINITED;
57
58    *key = &ldap_context->lrparams->mkey;
59    return 0;
60}
61
62
63/*
64 * set the master key into the database specific context
65 */
66
67krb5_error_code
68krb5_ldap_set_mkey (context, pwd, key)
69    krb5_context                context;
70    char                        *pwd;
71    krb5_keyblock               *key;
72{
73    kdb5_dal_handle             *dal_handle=NULL;
74    krb5_ldap_context           *ldap_context=NULL;
75    krb5_ldap_realm_params      *r_params = NULL;
76
77    /* Clear the global error string */
78    krb5_clear_error_message(context);
79
80    dal_handle = (kdb5_dal_handle *) context->db_context;
81    ldap_context = (krb5_ldap_context *) dal_handle->db_context;
82
83    if (ldap_context == NULL || ldap_context->lrparams == NULL)
84	return KRB5_KDB_DBNOTINITED;
85
86    r_params = ldap_context->lrparams;
87
88    if (r_params->mkey.contents) {
89	free (r_params->mkey.contents);
90	r_params->mkey.contents=NULL;
91    }
92
93    r_params->mkey.magic = key->magic;
94    r_params->mkey.enctype = key->enctype;
95    r_params->mkey.length = key->length;
96    r_params->mkey.contents = malloc(key->length);
97    if (r_params->mkey.contents == NULL)
98	return ENOMEM;
99
100    memcpy(r_params->mkey.contents, key->contents, key->length);
101    return 0;
102}
103