remove gcc34
[dragonfly.git] / crypto / heimdal-0.6.3 / lib / gssapi / import_name.c
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 "gssapi_locl.h"
35
36 RCSID("$Id: import_name.c,v 1.13 2003/03/16 17:33:31 lha Exp $");
37
38 static OM_uint32
39 parse_krb5_name (OM_uint32 *minor_status,
40                  const char *name,
41                  gss_name_t *output_name)
42 {
43     krb5_error_code kerr;
44
45     kerr = krb5_parse_name (gssapi_krb5_context, name, output_name);
46
47     if (kerr == 0)
48         return GSS_S_COMPLETE;
49     else if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED) {
50         gssapi_krb5_set_error_string ();
51         *minor_status = kerr;
52         return GSS_S_BAD_NAME;
53     } else {
54         gssapi_krb5_set_error_string ();
55         *minor_status = kerr;
56         return GSS_S_FAILURE;
57     }
58 }
59
60 static OM_uint32
61 import_krb5_name (OM_uint32 *minor_status,
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, tmp, output_name);
79     free(tmp);
80
81     return ret;
82 }
83
84 static OM_uint32
85 import_hostbased_name (OM_uint32 *minor_status,
86                        const gss_buffer_t input_name_buffer,
87                        gss_name_t *output_name)
88 {
89     krb5_error_code kerr;
90     char *tmp;
91     char *p;
92     char *host;
93     char local_hostname[MAXHOSTNAMELEN];
94
95     *output_name = NULL;
96
97     tmp = malloc (input_name_buffer->length + 1);
98     if (tmp == NULL) {
99         *minor_status = ENOMEM;
100         return GSS_S_FAILURE;
101     }
102     memcpy (tmp,
103             input_name_buffer->value,
104             input_name_buffer->length);
105     tmp[input_name_buffer->length] = '\0';
106
107     p = strchr (tmp, '@');
108     if (p != NULL) {
109         *p = '\0';
110         host = p + 1;
111     } else {
112         if (gethostname(local_hostname, sizeof(local_hostname)) < 0) {
113             *minor_status = errno;
114             free (tmp);
115             return GSS_S_FAILURE;
116         }
117         host = local_hostname;
118     }
119
120     kerr = krb5_sname_to_principal (gssapi_krb5_context,
121                                     host,
122                                     tmp,
123                                     KRB5_NT_SRV_HST,
124                                     output_name);
125     free (tmp);
126     *minor_status = kerr;
127     if (kerr == 0)
128         return GSS_S_COMPLETE;
129     else if (kerr == KRB5_PARSE_ILLCHAR || kerr == KRB5_PARSE_MALFORMED) {
130         gssapi_krb5_set_error_string ();
131         *minor_status = kerr;
132         return GSS_S_BAD_NAME;
133     } else {
134         gssapi_krb5_set_error_string ();
135         *minor_status = kerr;
136         return GSS_S_FAILURE;
137     }
138 }
139
140 static OM_uint32
141 import_export_name (OM_uint32 *minor_status,
142                     const gss_buffer_t input_name_buffer,
143                     gss_name_t *output_name)
144 {
145     unsigned char *p;
146     uint32_t length;
147     OM_uint32 ret;
148     char *name;
149
150     if (input_name_buffer->length < 10 + GSS_KRB5_MECHANISM->length)
151         return GSS_S_BAD_NAME;
152
153     /* TOK, MECH_OID_LEN, DER(MECH_OID), NAME_LEN, NAME */
154
155     p = input_name_buffer->value;
156
157     if (memcmp(&p[0], "\x04\x01\x00", 3) != 0 ||
158         p[3] != GSS_KRB5_MECHANISM->length + 2 ||
159         p[4] != 0x06 ||
160         p[5] != GSS_KRB5_MECHANISM->length ||
161         memcmp(&p[6], GSS_KRB5_MECHANISM->elements, 
162                GSS_KRB5_MECHANISM->length) != 0)
163         return GSS_S_BAD_NAME;
164
165     p += 6 + GSS_KRB5_MECHANISM->length;
166
167     length = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
168     p += 4;
169
170     if (length > input_name_buffer->length - 10 - GSS_KRB5_MECHANISM->length)
171         return GSS_S_BAD_NAME;
172
173     name = malloc(length + 1);
174     if (name == NULL) {
175         *minor_status = ENOMEM;
176         return GSS_S_FAILURE;
177     }
178     memcpy(name, p, length);
179     name[length] = '\0';
180
181     ret = parse_krb5_name(minor_status, name, output_name);
182     free(name);
183
184     return ret;
185 }
186
187 int
188 gss_oid_equal(const gss_OID a, const gss_OID b)
189 {
190         if (a == b)
191                 return 1;
192         else if (a == GSS_C_NO_OID || b == GSS_C_NO_OID || a->length != b->length)
193                 return 0;
194         else
195                 return memcmp(a->elements, b->elements, a->length) == 0;
196 }
197
198 OM_uint32 gss_import_name
199            (OM_uint32 * minor_status,
200             const gss_buffer_t input_name_buffer,
201             const gss_OID input_name_type,
202             gss_name_t * output_name
203            )
204 {
205     GSSAPI_KRB5_INIT ();
206
207     *minor_status = 0;
208     *output_name = GSS_C_NO_NAME;
209     
210     if (gss_oid_equal(input_name_type, GSS_C_NT_HOSTBASED_SERVICE))
211         return import_hostbased_name (minor_status,
212                                       input_name_buffer,
213                                       output_name);
214     else if (gss_oid_equal(input_name_type, GSS_C_NO_OID)
215              || gss_oid_equal(input_name_type, GSS_C_NT_USER_NAME)
216              || gss_oid_equal(input_name_type, GSS_KRB5_NT_PRINCIPAL_NAME))
217         /* default printable syntax */
218         return import_krb5_name (minor_status,
219                                  input_name_buffer,
220                                  output_name);
221     else if (gss_oid_equal(input_name_type, GSS_C_NT_EXPORT_NAME)) {
222         return import_export_name(minor_status,
223                                   input_name_buffer, 
224                                   output_name);
225     } else {
226         *minor_status = 0;
227         return GSS_S_BAD_NAMETYPE;
228     }
229 }