1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* plugins/kdb/ldap/libkdb_ldap/ldap_create.c */
3 /*
4 * Copyright (c) 2004-2005, Novell, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * The copyright holder's name is not used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
33 * Use is subject to license terms.
34 */
35
36 #include "ldap_main.h"
37 #include "ldap_realm.h"
38 #include "ldap_principal.h"
39 #include "ldap_krbcontainer.h"
40 #include "ldap_err.h"
41
42 /*
43 * ******************************************************************************
44 * DAL functions
45 * ******************************************************************************
46 */
47
48 /*
49 * This function will create a krbcontainer and realm on the LDAP Server, with
50 * the specified attributes.
51 */
52 krb5_error_code
krb5_ldap_create(krb5_context context,char * conf_section,char ** db_args)53 krb5_ldap_create(krb5_context context, char *conf_section, char **db_args)
54 {
55 krb5_error_code status = 0;
56 krb5_ldap_realm_params *rparams = NULL;
57 krb5_ldap_context *ldap_context=NULL;
58 int mask = 0;
59
60 /* Clear the global error string */
61 krb5_clear_error_message(context);
62
63 ldap_context = k5alloc(sizeof(krb5_ldap_context), &status);
64 if (ldap_context == NULL)
65 goto cleanup;
66 context->dal_handle->db_context = ldap_context;
67 ldap_context->kcontext = context;
68
69 status = krb5_ldap_parse_db_params(context, db_args);
70 if (status) {
71 k5_prependmsg(context, status, _("Error processing LDAP DB params"));
72 goto cleanup;
73 }
74
75 status = krb5_ldap_read_server_params(context, conf_section, KRB5_KDB_SRV_TYPE_ADMIN);
76 if (status) {
77 k5_prependmsg(context, status, _("Error reading LDAP server params"));
78 goto cleanup;
79 }
80 status = krb5_ldap_db_init(context, ldap_context);
81 if (status) {
82 goto cleanup;
83 }
84
85 /* read the kerberos container */
86 status = krb5_ldap_read_krbcontainer_dn(context,
87 &ldap_context->container_dn);
88 if (status)
89 goto cleanup;
90
91 status = krb5_ldap_create_krbcontainer(context,
92 ldap_context->container_dn);
93 if (status)
94 goto cleanup;
95
96 rparams = (krb5_ldap_realm_params *) malloc(sizeof(krb5_ldap_realm_params));
97 if (rparams == NULL) {
98 status = ENOMEM;
99 goto cleanup;
100 }
101 memset(rparams, 0, sizeof(*rparams));
102 rparams->realm_name = strdup(context->default_realm);
103 if (rparams->realm_name == NULL) {
104 status = ENOMEM;
105 goto cleanup;
106 }
107
108 if ((status = krb5_ldap_create_realm(context, rparams, mask)))
109 goto cleanup;
110
111 /* verify realm object */
112 if ((status = krb5_ldap_read_realm_params(context,
113 rparams->realm_name,
114 &(ldap_context->lrparams),
115 &mask)))
116 goto cleanup;
117
118 cleanup:
119 if (rparams)
120 krb5_ldap_free_realm_params(rparams);
121
122 if (status)
123 krb5_ldap_close(context);
124 return(status);
125 }
126