libsecurity: Simplify struct ace_condition_script
[Samba.git] / third_party / heimdal / lib / gssapi / krb5 / import_name.c
blobf4ee2313c16fa521dfe251bcbfc783d02ae8a301
1 /*
2 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "gsskrb5_locl.h"
36 static OM_uint32
37 parse_krb5_name (OM_uint32 *minor_status,
38 krb5_context context,
39 const char *name,
40 gss_name_t *output_name)
42 krb5_principal princ;
43 krb5_error_code kerr;
45 kerr = krb5_parse_name (context, name, &princ);
47 if (kerr == 0) {
48 *output_name = (gss_name_t)princ;
49 return GSS_S_COMPLETE;
51 *minor_status = kerr;
53 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
54 return GSS_S_BAD_NAME;
56 return GSS_S_FAILURE;
59 static OM_uint32
60 import_krb5_name (OM_uint32 *minor_status,
61 krb5_context context,
62 const gss_buffer_t input_name_buffer,
63 gss_name_t *output_name)
65 OM_uint32 ret;
66 char *tmp;
68 tmp = malloc (input_name_buffer->length + 1);
69 if (tmp == NULL) {
70 *minor_status = ENOMEM;
71 return GSS_S_FAILURE;
73 memcpy (tmp,
74 input_name_buffer->value,
75 input_name_buffer->length);
76 tmp[input_name_buffer->length] = '\0';
78 ret = parse_krb5_name(minor_status, context, tmp, output_name);
79 free(tmp);
81 return ret;
84 OM_uint32
85 _gsskrb5_canon_name(OM_uint32 *minor_status, krb5_context context,
86 gss_const_name_t targetname, krb5_principal *out)
88 krb5_const_principal p = (krb5_const_principal)targetname;
89 krb5_error_code ret;
90 char *hostname = NULL, *service;
91 int type;
92 const char *comp;
94 *minor_status = 0;
96 /* If its not a hostname */
97 type = krb5_principal_get_type(context, p);
98 comp = krb5_principal_get_comp_string(context, p, 0);
99 if (type == KRB5_NT_SRV_HST || type == KRB5_NT_SRV_HST_NEEDS_CANON ||
100 (type == KRB5_NT_UNKNOWN && comp != NULL && strcmp(comp, "host") == 0)) {
101 if (p->name.name_string.len == 0)
102 return GSS_S_BAD_NAME;
103 else if (p->name.name_string.len > 1)
104 hostname = p->name.name_string.val[1];
106 service = p->name.name_string.val[0];
108 ret = krb5_sname_to_principal(context,
109 hostname,
110 service,
111 KRB5_NT_SRV_HST,
112 out);
113 if (ret == 0) {
114 const char *in_realm = krb5_principal_get_realm(context,
116 const char *out_realm = krb5_principal_get_realm(context,
117 *out);
120 * Avoid loss of information, check for the "referral
121 * realm" and set back what was specified.
123 if (out_realm != NULL && out_realm[0] == '\0') {
124 ret = krb5_principal_set_realm(context, *out, in_realm);
127 } else {
128 ret = krb5_copy_principal(context, p, out);
131 if (ret) {
132 *minor_status = ret;
133 return GSS_S_FAILURE;
136 return 0;
140 static OM_uint32
141 import_hostbased_name(OM_uint32 *minor_status,
142 krb5_context context,
143 const gss_buffer_t input_name_buffer,
144 gss_name_t *output_name)
146 krb5_principal princ = NULL;
147 krb5_error_code kerr;
148 char *tmp, *p, *host = NULL;
150 tmp = malloc (input_name_buffer->length + 1);
151 if (tmp == NULL) {
152 *minor_status = ENOMEM;
153 return GSS_S_FAILURE;
155 memcpy (tmp,
156 input_name_buffer->value,
157 input_name_buffer->length);
158 tmp[input_name_buffer->length] = '\0';
160 p = strchr (tmp, '@');
161 if (p != NULL) {
162 *p = '\0';
163 host = p + 1;
166 kerr = krb5_make_principal(context, &princ, "", tmp, host, NULL);
167 free (tmp);
168 *minor_status = kerr;
169 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
170 return GSS_S_BAD_NAME;
171 else if (kerr)
172 return GSS_S_FAILURE;
174 krb5_principal_set_type(context, princ, KRB5_NT_SRV_HST);
175 *output_name = (gss_name_t)princ;
177 return 0;
180 static OM_uint32
181 import_export_name (OM_uint32 *minor_status,
182 krb5_context context,
183 const gss_buffer_t input_name_buffer,
184 gss_name_t *output_name)
186 CompositePrincipal *composite;
187 unsigned char *p;
188 uint32_t length;
189 size_t sz;
190 OM_uint32 ret;
191 int is_composite;
192 char *name;
194 if (input_name_buffer->length < 10 + GSS_KRB5_MECHANISM->length)
195 return GSS_S_BAD_NAME;
197 /* TOK, MECH_OID_LEN, DER(MECH_OID), NAME_LEN, NAME */
199 p = input_name_buffer->value;
201 if (p[0] != 0x04 ||
202 (p[1] != 0x01 && p[1] != 0x02) ||
203 p[2] != 0x00 ||
204 p[3] != GSS_KRB5_MECHANISM->length + 2 ||
205 p[4] != 0x06 ||
206 p[5] != GSS_KRB5_MECHANISM->length ||
207 memcmp(&p[6], GSS_KRB5_MECHANISM->elements,
208 GSS_KRB5_MECHANISM->length) != 0)
209 return GSS_S_BAD_NAME;
211 is_composite = p[1] == 0x02;
213 p += 6 + GSS_KRB5_MECHANISM->length;
215 length = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
216 p += 4;
218 if (length > input_name_buffer->length - 10 - GSS_KRB5_MECHANISM->length)
219 return GSS_S_BAD_NAME;
221 if (is_composite) {
222 if ((composite = calloc(1, sizeof(*composite))) == NULL) {
223 *minor_status = ENOMEM;
224 return GSS_S_FAILURE;
227 ret = decode_CompositePrincipal(p, length, composite, &sz);
228 if (ret) {
229 *minor_status = ret;
230 return GSS_S_FAILURE;
232 if (sz != length) {
233 free_CompositePrincipal(composite);
234 free(composite);
235 *minor_status = EINVAL;
236 return GSS_S_FAILURE;
239 *output_name = (void *)composite;
240 return GSS_S_COMPLETE;
243 name = malloc(length + 1);
244 if (name == NULL) {
245 *minor_status = ENOMEM;
246 return GSS_S_FAILURE;
248 memcpy(name, p, length);
249 name[length] = '\0';
251 ret = parse_krb5_name(minor_status, context, name, output_name);
252 free(name);
253 return ret;
256 OM_uint32 GSSAPI_CALLCONV _gsskrb5_import_name
257 (OM_uint32 * minor_status,
258 const gss_buffer_t input_name_buffer,
259 const gss_OID input_name_type,
260 gss_name_t * output_name
263 krb5_context context;
265 *minor_status = 0;
266 *output_name = GSS_C_NO_NAME;
268 GSSAPI_KRB5_INIT (&context);
270 if (gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE) ||
271 gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE_X))
272 return import_hostbased_name (minor_status,
273 context,
274 input_name_buffer,
275 output_name);
276 else if (input_name_type == GSS_C_NO_OID
277 || gss_oid_equal(input_name_type, GSS_C_NT_USER_NAME)
278 || gss_oid_equal(input_name_type, GSS_KRB5_NT_PRINCIPAL_NAME))
279 /* default printable syntax */
280 return import_krb5_name (minor_status,
281 context,
282 input_name_buffer,
283 output_name);
284 else if (gss_oid_equal(input_name_type, GSS_C_NT_EXPORT_NAME) ||
285 gss_oid_equal(input_name_type, GSS_C_NT_COMPOSITE_EXPORT)) {
286 return import_export_name(minor_status,
287 context,
288 input_name_buffer,
289 output_name);
290 } else {
291 *minor_status = 0;
292 return GSS_S_BAD_NAMETYPE;