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    struct comb_principal *p;
72    krb5_error_code ret;
73
74    LOG_ENTRY();
75
76    p = calloc(1, sizeof(*p));
77    ret = heim_krb5_parse_name((krb5_context)context, str, &p->heim);
78    if (ret) {
79	free(p);
80	return ret;
81    }
82    map_mit_principal(p);
83    *principal = (mit_krb5_principal)p;
84    return 0;
85}
86
87mit_krb5_error_code KRB5_CALLCONV_C
88krb5_build_principal_ext(mit_krb5_context context, mit_krb5_principal *principal, unsigned int rlen, const char *realm, ...)
89{
90    struct comb_principal *p;
91    krb5_error_code ret;
92    va_list ap;
93
94    LOG_ENTRY();
95
96    va_start(ap, realm);
97    p = calloc(1, sizeof(*p));
98    ret = heim_krb5_build_principal_va_ext((krb5_context)context, &p->heim, rlen, realm, ap);
99    va_end(ap);
100    if (ret) {
101	free(p);
102	return ret;
103    }
104    map_mit_principal(p);
105    *principal = (mit_krb5_principal)p;
106    return ret;
107}
108
109mit_krb5_error_code KRB5_CALLCONV_C
110krb5_build_principal(mit_krb5_context context, mit_krb5_principal *principal, unsigned int rlen, const char *realm, ...)
111{
112    struct comb_principal *p;
113    krb5_error_code ret;
114    va_list ap;
115
116    LOG_ENTRY();
117
118    va_start(ap, realm);
119    p = calloc(1, sizeof(*p));
120    ret = heim_krb5_build_principal_va((krb5_context)context, &p->heim, rlen, realm, ap);
121    va_end(ap);
122    if (ret) {
123	free(p);
124	return ret;
125    }
126    map_mit_principal(p);
127    *principal = (mit_krb5_principal)p;
128    return ret;
129}
130
131mit_krb5_error_code KRB5_CALLCONV
132krb5_unparse_name(mit_krb5_context context, mit_krb5_const_principal principal, char **str)
133{
134    struct comb_principal *p = (struct comb_principal *)principal;
135    LOG_ENTRY();
136    return heim_krb5_unparse_name((krb5_context)context, p->heim, str);
137}
138
139void KRB5_CALLCONV
140krb5_free_unparsed_name(mit_krb5_context context, char *str)
141{
142    LOG_ENTRY();
143    heim_krb5_xfree(str);
144}
145
146mit_krb5_error_code KRB5_CALLCONV
147krb5_copy_principal(mit_krb5_context context,
148		    mit_krb5_const_principal from,
149		    mit_krb5_principal *to)
150{
151    struct comb_principal *p = (struct comb_principal *)from;
152    LOG_ENTRY();
153    *to = mshim_hprinc2mprinc(HC(context), p->heim);
154    return 0;
155}
156
157void KRB5_CALLCONV
158krb5_free_principal(mit_krb5_context context, mit_krb5_principal principal)
159{
160    struct comb_principal *p = (struct comb_principal *)principal;
161    LOG_ENTRY();
162    if (p) {
163	heim_krb5_free_principal(HC(context), p->heim);
164	free(p->mit.data);
165	free(p);
166    }
167}
168
169void KRB5_CALLCONV
170krb5_free_default_realm(mit_krb5_context context, char *str)
171{
172    LOG_ENTRY();
173    free(str);
174}
175
176mit_krb5_error_code KRB5_CALLCONV
177krb5_sname_to_principal(mit_krb5_context context,
178			const char *hostname, const char *service,
179			mit_krb5_int32 type,
180			mit_krb5_principal *principal)
181{
182    krb5_error_code ret;
183    krb5_principal p;
184
185    LOG_ENTRY();
186
187    *principal = NULL;
188
189    ret = heim_krb5_sname_to_principal(HC(context), hostname, service, type, &p);
190    if (ret)
191	return ret;
192
193    *principal = mshim_hprinc2mprinc(HC(context), p);
194    heim_krb5_free_principal(HC(context), p);
195    return 0;
196}
197
198mit_krb5_boolean KRB5_CALLCONV
199krb5_principal_compare(mit_krb5_context context,
200		       mit_krb5_const_principal p1,
201		       mit_krb5_const_principal p2)
202{
203    struct comb_principal *c1 = (struct comb_principal *)p1;
204    struct comb_principal *c2 = (struct comb_principal *)p2;
205
206    return heim_krb5_principal_compare(HC(context), c1->heim, c2->heim);
207}
208
209mit_krb5_boolean KRB5_CALLCONV
210krb5_realm_compare(mit_krb5_context context,
211		   mit_krb5_const_principal p1,
212		   mit_krb5_const_principal p2)
213{
214    struct comb_principal *c1 = (struct comb_principal *)p1;
215    struct comb_principal *c2 = (struct comb_principal *)p2;
216
217    return heim_krb5_realm_compare(HC(context), c1->heim, c2->heim);
218}
219
220mit_krb5_error_code KRB5_CALLCONV
221krb5_get_realm_domain(mit_krb5_context, const char *, char **);
222
223
224mit_krb5_error_code KRB5_CALLCONV
225krb5_get_realm_domain(mit_krb5_context context, const char *realm, char **domain)
226{
227    const char *d;
228
229    d = heim_krb5_config_get_string(HC(context), NULL, "realms", realm,
230				    "default_realm", NULL);
231    if (d == NULL) {
232	*domain = NULL;
233	return (-1429577726L); /* PROF_NO_SECTION */
234    }
235    *domain = strdup(d);
236    return 0;
237}
238