Debian Codegraph @

lib/gssapi/krb5/import_name.c

raw
1 /*
2 * Copyright (c) 1997 - 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include "gsskrb5_locl.h"
35
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)
41 {
42 krb5_principal princ;
43 krb5_error_code kerr;
44
45 kerr = krb5_parse_name (context, name, &princ);
46
47 if (kerr == 0) {
48 *output_name = (gss_name_t)princ;
49 return GSS_S_COMPLETE;
50 }
51 *minor_status = kerr;
52
53 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
54 return GSS_S_BAD_NAME;
55
56 return GSS_S_FAILURE;
57 }
58
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)
64 {
65 OM_uint32 ret;
66 char *tmp;
67
68 tmp = malloc (input_name_buffer->length + 1);
69 if (tmp == NULL) {
70 *minor_status = ENOMEM;
71 return GSS_S_FAILURE;
72 }
73 memcpy (tmp,
74 input_name_buffer->value,
75 input_name_buffer->length);
76 tmp[input_name_buffer->length] = '\0';
77
78 ret = parse_krb5_name(minor_status, context, tmp, output_name);
79 free(tmp);
80
81 return ret;
82 }
83
84 OM_uint32
85 _gsskrb5_canon_name(OM_uint32 *minor_status, krb5_context context,
86 gss_const_name_t targetname, krb5_principal *out)
87 {
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;
93
94 *minor_status = 0;
95
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];
105
106 service = p->name.name_string.val[0];
107
108 ret = krb5_sname_to_principal(context,
109 hostname,
110 service,
111 KRB5_NT_SRV_HST,
112 out);
113 } else {
114 ret = krb5_copy_principal(context, p, out);
115 }
116
117 if (ret) {
118 *minor_status = ret;
119 return GSS_S_FAILURE;
120 }
121
122 return 0;
123 }
124
125
126 static OM_uint32
127 import_hostbased_name(OM_uint32 *minor_status,
128 krb5_context context,
129 const gss_buffer_t input_name_buffer,
130 gss_name_t *output_name)
131 {
132 krb5_principal princ = NULL;
133 krb5_error_code kerr;
134 char *tmp, *p, *host = NULL;
135
136 tmp = malloc (input_name_buffer->length + 1);
137 if (tmp == NULL) {
138 *minor_status = ENOMEM;
139 return GSS_S_FAILURE;
140 }
141 memcpy (tmp,
142 input_name_buffer->value,
143 input_name_buffer->length);
144 tmp[input_name_buffer->length] = '\0';
145
146 p = strchr (tmp, '@');
147 if (p != NULL) {
148 *p = '\0';
149 host = p + 1;
150 }
151
152 kerr = krb5_make_principal(context, &princ, "", tmp, host, NULL);
153 free (tmp);
154 *minor_status = kerr;
155 if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED)
156 return GSS_S_BAD_NAME;
157 else if (kerr)
158 return GSS_S_FAILURE;
159
160 krb5_principal_set_type(context, princ, KRB5_NT_SRV_HST);
161 *output_name = (gss_name_t)princ;
162
163 return 0;
164 }
165
166 static OM_uint32
167 import_export_name (OM_uint32 *minor_status,
168 krb5_context context,
169 const gss_buffer_t input_name_buffer,
170 gss_name_t *output_name)
171 {
172 unsigned char *p;
173 uint32_t length;
174 OM_uint32 ret;
175 char *name;
176
177 if (input_name_buffer->length < 10 + GSS_KRB5_MECHANISM->length)
178 return GSS_S_BAD_NAME;
179
180 /* TOK, MECH_OID_LEN, DER(MECH_OID), NAME_LEN, NAME */
181
182 p = input_name_buffer->value;
183
184 if (memcmp(&p[0], "\x04\x01\x00", 3) != 0 ||
185 p[3] != GSS_KRB5_MECHANISM->length + 2 ||
186 p[4] != 0x06 ||
187 p[5] != GSS_KRB5_MECHANISM->length ||
188 memcmp(&p[6], GSS_KRB5_MECHANISM->elements,
189 GSS_KRB5_MECHANISM->length) != 0)
190 return GSS_S_BAD_NAME;
191
192 p += 6 + GSS_KRB5_MECHANISM->length;
193
194 length = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
195 p += 4;
196
197 if (length > input_name_buffer->length - 10 - GSS_KRB5_MECHANISM->length)
198 return GSS_S_BAD_NAME;
199
200 name = malloc(length + 1);
201 if (name == NULL) {
202 *minor_status = ENOMEM;
203 return GSS_S_FAILURE;
204 }
205 memcpy(name, p, length);
206 name[length] = '\0';
207
208 ret = parse_krb5_name(minor_status, context, name, output_name);
209 free(name);
210
211 return ret;
212 }
213
214 OM_uint32 GSSAPI_CALLCONV _gsskrb5_import_name
215 (OM_uint32 * minor_status,
216 const gss_buffer_t input_name_buffer,
217 const gss_OID input_name_type,
218 gss_name_t * output_name
219 )
220 {
221 krb5_context context;
222
223 *minor_status = 0;
224 *output_name = GSS_C_NO_NAME;
225
226 GSSAPI_KRB5_INIT (&context);
227
228 if (gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE) ||
229 gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE_X))
230 return import_hostbased_name (minor_status,
231 context,
232 input_name_buffer,
233 output_name);
234 else if (input_name_type == GSS_C_NO_OID
235 || gss_oid_equal(input_name_type, GSS_C_NT_USER_NAME)
236 || gss_oid_equal(input_name_type, GSS_KRB5_NT_PRINCIPAL_NAME))
237 /* default printable syntax */
238 return import_krb5_name (minor_status,
239 context,
240 input_name_buffer,
241 output_name);
242 else if (gss_oid_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) {
243 return import_export_name(minor_status,
244 context,
245 input_name_buffer,
246 output_name);
247 } else {
248 *minor_status = 0;
249 return GSS_S_BAD_NAMETYPE;
250 }
251 }