1/*
2 * Copyright (c) 2008-2010 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2008-2010 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "heim.h"
37#include <string.h>
38
39static void
40map_mit_principal(struct comb_principal *p)
41{
42    unsigned long i;
43
44    p->mit.magic = MIT_KV5M_PRINCIPAL;
45    p->mit.type = p->heim->name.name_type;
46    p->mit.realm.magic = MIT_KV5M_DATA;
47    p->mit.realm.data = p->heim->realm;
48    p->mit.realm.length = strlen(p->heim->realm);
49    p->mit.data = calloc(p->heim->name.name_string.len, sizeof(*p->mit.data));
50    for (i = 0; i < p->heim->name.name_string.len; i++) {
51	p->mit.data[i].magic = MIT_KV5M_DATA;
52	p->mit.data[i].data = p->heim->name.name_string.val[i];
53	p->mit.data[i].length = strlen(p->heim->name.name_string.val[i]);
54    }
55    p->mit.length = p->heim->name.name_string.len;
56}
57
58mit_krb5_principal
59mshim_hprinc2mprinc(krb5_context context, krb5_principal princ)
60{
61    struct comb_principal *p;
62    p = calloc(1, sizeof(*p));
63    heim_krb5_copy_principal(context, princ, &p->heim);
64    map_mit_principal(p);
65    return (mit_krb5_principal)p;
66}
67
68mit_krb5_error_code KRB5_CALLCONV
69krb5_parse_name(mit_krb5_context context, const char *str, mit_krb5_principal *principal)
70{
71    return krb5_parse_name_flags(context, str, 0, principal);
72}
73
74mit_krb5_error_code KRB5_CALLCONV
75krb5_parse_name_flags(mit_krb5_context context, const char *str, int flags, mit_krb5_principal *principal)
76{
77    struct comb_principal *p;
78    krb5_error_code ret;
79
80    LOG_ENTRY();
81
82    p = calloc(1, sizeof(*p));
83    ret = heim_krb5_parse_name_flags((krb5_context)context, str, flags, &p->heim);
84    if (ret) {
85        free(p);
86        return ret;
87    }
88    map_mit_principal(p);
89    *principal = (mit_krb5_principal)p;
90    return 0;
91}
92
93
94mit_krb5_error_code KRB5_CALLCONV_C
95krb5_build_principal_ext(mit_krb5_context context, mit_krb5_principal *principal, unsigned int rlen, const char *realm, ...)
96{
97    struct comb_principal *p;
98    krb5_error_code ret;
99    va_list ap;
100
101    LOG_ENTRY();
102
103    va_start(ap, realm);
104    p = calloc(1, sizeof(*p));
105    ret = heim_krb5_build_principal_va_ext((krb5_context)context, &p->heim, rlen, realm, ap);
106    va_end(ap);
107    if (ret) {
108	free(p);
109	return ret;
110    }
111    map_mit_principal(p);
112    *principal = (mit_krb5_principal)p;
113    return ret;
114}
115
116mit_krb5_error_code KRB5_CALLCONV_C
117krb5_build_principal(mit_krb5_context context, mit_krb5_principal *principal, unsigned int rlen, const char *realm, ...)
118{
119    struct comb_principal *p;
120    krb5_error_code ret;
121    va_list ap;
122
123    LOG_ENTRY();
124
125    va_start(ap, realm);
126    p = calloc(1, sizeof(*p));
127    ret = heim_krb5_build_principal_va((krb5_context)context, &p->heim, rlen, realm, ap);
128    va_end(ap);
129    if (ret) {
130	free(p);
131	return ret;
132    }
133    map_mit_principal(p);
134    *principal = (mit_krb5_principal)p;
135    return ret;
136}
137
138mit_krb5_error_code KRB5_CALLCONV
139krb5_unparse_name(mit_krb5_context context, mit_krb5_const_principal principal, char **str)
140{
141    struct comb_principal *p = (struct comb_principal *)principal;
142    LOG_ENTRY();
143    return heim_krb5_unparse_name((krb5_context)context, p->heim, str);
144}
145
146void KRB5_CALLCONV
147krb5_free_unparsed_name(mit_krb5_context context, char *str)
148{
149    LOG_ENTRY();
150    heim_krb5_xfree(str);
151}
152
153mit_krb5_error_code KRB5_CALLCONV
154krb5_copy_principal(mit_krb5_context context,
155		    mit_krb5_const_principal from,
156		    mit_krb5_principal *to)
157{
158    struct comb_principal *p = (struct comb_principal *)from;
159    LOG_ENTRY();
160    *to = mshim_hprinc2mprinc(HC(context), p->heim);
161    return 0;
162}
163
164void KRB5_CALLCONV
165krb5_free_principal(mit_krb5_context context, mit_krb5_principal principal)
166{
167    struct comb_principal *p = (struct comb_principal *)principal;
168    LOG_ENTRY();
169    if (p) {
170	heim_krb5_free_principal(HC(context), p->heim);
171	free(p->mit.data);
172	free(p);
173    }
174}
175
176void KRB5_CALLCONV
177krb5_free_default_realm(mit_krb5_context context, char *str)
178{
179    LOG_ENTRY();
180    free(str);
181}
182
183mit_krb5_error_code KRB5_CALLCONV
184krb5_sname_to_principal(mit_krb5_context context,
185			const char *hostname, const char *service,
186			mit_krb5_int32 type,
187			mit_krb5_principal *principal)
188{
189    krb5_error_code ret;
190    krb5_principal p;
191
192    LOG_ENTRY();
193
194    *principal = NULL;
195
196    ret = heim_krb5_sname_to_principal(HC(context), hostname, service, type, &p);
197    if (ret)
198	return ret;
199
200    *principal = mshim_hprinc2mprinc(HC(context), p);
201    heim_krb5_free_principal(HC(context), p);
202    return 0;
203}
204
205mit_krb5_boolean KRB5_CALLCONV
206krb5_principal_compare(mit_krb5_context context,
207		       mit_krb5_const_principal p1,
208		       mit_krb5_const_principal p2)
209{
210    struct comb_principal *c1 = (struct comb_principal *)p1;
211    struct comb_principal *c2 = (struct comb_principal *)p2;
212
213    return heim_krb5_principal_compare(HC(context), c1->heim, c2->heim);
214}
215
216mit_krb5_boolean KRB5_CALLCONV
217krb5_realm_compare(mit_krb5_context context,
218		   mit_krb5_const_principal p1,
219		   mit_krb5_const_principal p2)
220{
221    struct comb_principal *c1 = (struct comb_principal *)p1;
222    struct comb_principal *c2 = (struct comb_principal *)p2;
223
224    return heim_krb5_realm_compare(HC(context), c1->heim, c2->heim);
225}
226
227mit_krb5_error_code KRB5_CALLCONV
228krb5_get_realm_domain(mit_krb5_context, const char *, char **);
229
230
231mit_krb5_error_code KRB5_CALLCONV
232krb5_get_realm_domain(mit_krb5_context context, const char *realm, char **domain)
233{
234    const char *d;
235
236    d = heim_krb5_config_get_string(HC(context), NULL, "realms", realm,
237				    "default_realm", NULL);
238    if (d == NULL) {
239	*domain = NULL;
240	return (-1429577726L); /* PROF_NO_SECTION */
241    }
242    *domain = strdup(d);
243    return 0;
244}
245