2 * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "test_locl.h"
35 #include <gssapi/gssapi.h>
36 #include <gssapi/gssapi_krb5.h>
37 #include <gssapi/gssapi_spnego.h>
38 #include "gss_common.h"
43 gss_ctx_id_t context_hdl
,
44 gss_name_t client_name
47 OM_uint32 maj_stat
, min_stat
;
48 gss_buffer_desc real_input_token
, real_output_token
;
49 gss_buffer_t input_token
= &real_input_token
,
50 output_token
= &real_output_token
;
51 gss_name_t server_name
;
54 print_gss_name("User is", client_name
);
56 maj_stat
= gss_inquire_context(&min_stat
,
65 if (GSS_ERROR(maj_stat
))
66 gss_err (1, min_stat
, "gss_inquire_context");
68 print_gss_name("Server is", server_name
);
70 maj_stat
= gss_release_name(&min_stat
, &server_name
);
71 if (GSS_ERROR(maj_stat
))
72 gss_err (1, min_stat
, "gss_release_name");
76 read_token (sock
, input_token
);
77 read_token (sock
, output_token
);
79 maj_stat
= gss_verify_mic (&min_stat
,
84 if (GSS_ERROR(maj_stat
))
85 gss_err (1, min_stat
, "gss_verify_mic");
87 fprintf (stderr
, "gss_verify_mic: %.*s\n", (int)input_token
->length
,
88 (char *)input_token
->value
);
90 gss_release_buffer (&min_stat
, input_token
);
91 gss_release_buffer (&min_stat
, output_token
);
95 read_token (sock
, input_token
);
97 maj_stat
= gss_unwrap (&min_stat
,
103 if(GSS_ERROR(maj_stat
))
104 gss_err (1, min_stat
, "gss_unwrap");
106 fprintf (stderr
, "gss_unwrap: %.*s %s\n", (int)output_token
->length
,
107 (char *)output_token
->value
,
108 conf_flag
? "CONF" : "INT");
110 gss_release_buffer (&min_stat
, input_token
);
111 gss_release_buffer (&min_stat
, output_token
);
113 read_token (sock
, input_token
);
115 maj_stat
= gss_unwrap (&min_stat
,
121 if(GSS_ERROR(maj_stat
))
122 gss_err (1, min_stat
, "gss_unwrap");
124 fprintf (stderr
, "gss_unwrap: %.*s %s\n", (int)output_token
->length
,
125 (char *)output_token
->value
,
126 conf_flag
? "CONF" : "INT");
128 gss_release_buffer (&min_stat
, input_token
);
129 gss_release_buffer (&min_stat
, output_token
);
135 proto (int sock
, const char *service
)
137 struct sockaddr_in remote
, local
;
139 gss_ctx_id_t context_hdl
= GSS_C_NO_CONTEXT
;
140 gss_buffer_desc real_input_token
, real_output_token
;
141 gss_buffer_t input_token
= &real_input_token
,
142 output_token
= &real_output_token
;
143 OM_uint32 maj_stat
, min_stat
;
144 gss_name_t client_name
;
145 struct gss_channel_bindings_struct input_chan_bindings
;
146 gss_cred_id_t delegated_cred_handle
= NULL
;
153 addrlen
= sizeof(local
);
154 if (getsockname (sock
, (struct sockaddr
*)&local
, &addrlen
) < 0
155 || addrlen
!= sizeof(local
))
156 err (1, "getsockname)");
158 addrlen
= sizeof(remote
);
159 if (getpeername (sock
, (struct sockaddr
*)&remote
, &addrlen
) < 0
160 || addrlen
!= sizeof(remote
))
161 err (1, "getpeername");
163 input_chan_bindings
.initiator_addrtype
= GSS_C_AF_INET
;
164 input_chan_bindings
.initiator_address
.length
= 4;
165 init_buf
[0] = (remote
.sin_addr
.s_addr
>> 24) & 0xFF;
166 init_buf
[1] = (remote
.sin_addr
.s_addr
>> 16) & 0xFF;
167 init_buf
[2] = (remote
.sin_addr
.s_addr
>> 8) & 0xFF;
168 init_buf
[3] = (remote
.sin_addr
.s_addr
>> 0) & 0xFF;
170 input_chan_bindings
.initiator_address
.value
= init_buf
;
171 input_chan_bindings
.acceptor_addrtype
= GSS_C_AF_INET
;
173 input_chan_bindings
.acceptor_address
.length
= 4;
174 acct_buf
[0] = (local
.sin_addr
.s_addr
>> 24) & 0xFF;
175 acct_buf
[1] = (local
.sin_addr
.s_addr
>> 16) & 0xFF;
176 acct_buf
[2] = (local
.sin_addr
.s_addr
>> 8) & 0xFF;
177 acct_buf
[3] = (local
.sin_addr
.s_addr
>> 0) & 0xFF;
178 input_chan_bindings
.acceptor_address
.value
= acct_buf
;
179 input_chan_bindings
.application_data
.value
= emalloc(4);
181 * (unsigned short *)input_chan_bindings
.application_data
.value
=
183 * ((unsigned short *)input_chan_bindings
.application_data
.value
+ 1) =
185 input_chan_bindings
.application_data
.length
= 4;
187 input_chan_bindings
.application_data
.length
= 0;
188 input_chan_bindings
.application_data
.value
= NULL
;
191 delegated_cred_handle
= GSS_C_NO_CREDENTIAL
;
194 read_token (sock
, input_token
);
196 gss_accept_sec_context (&min_stat
,
200 &input_chan_bindings
,
206 &delegated_cred_handle
);
207 if(GSS_ERROR(maj_stat
))
208 gss_err (1, min_stat
, "gss_accept_sec_context");
209 if (output_token
->length
!= 0)
210 write_token (sock
, output_token
);
211 if (GSS_ERROR(maj_stat
)) {
212 if (context_hdl
!= GSS_C_NO_CONTEXT
)
213 gss_delete_sec_context (&min_stat
,
218 } while(maj_stat
& GSS_S_CONTINUE_NEEDED
);
220 p
= (char *)mech_oid
->elements
;
221 if (mech_oid
->length
== GSS_KRB5_MECHANISM
->length
222 && memcmp(p
, GSS_KRB5_MECHANISM
->elements
, mech_oid
->length
) == 0)
224 else if (mech_oid
->length
== GSS_SPNEGO_MECHANISM
->length
225 && memcmp(p
, GSS_SPNEGO_MECHANISM
->elements
, mech_oid
->length
) == 0)
226 mech
= "SPNEGO"; /* XXX Silly, wont show up */
230 printf("Using mech: %s\n", mech
);
232 if (delegated_cred_handle
!= GSS_C_NO_CREDENTIAL
) {
233 krb5_context context
;
235 printf("Delegated cred found\n");
237 maj_stat
= krb5_init_context(&context
);
238 maj_stat
= krb5_cc_resolve(context
, "FILE:/tmp/krb5cc_test", &ccache
);
239 maj_stat
= gss_krb5_copy_ccache(&min_stat
,
240 delegated_cred_handle
,
244 maj_stat
= krb5_cc_get_principal(context
, ccache
, &p
);
247 maj_stat
= krb5_unparse_name(context
, p
, &name
);
249 printf("Delegated user is: `%s'\n", name
);
252 krb5_free_principal(context
, p
);
255 krb5_cc_close(context
, ccache
);
256 gss_release_cred(&min_stat
, &delegated_cred_handle
);
263 if (pipe (pipefd
) < 0)
272 maj_stat
= gss_export_sec_context (&min_stat
,
275 if (GSS_ERROR(maj_stat
))
276 gss_err (1, min_stat
, "gss_export_sec_context");
277 write_token (pipefd
[1], &buf
);
280 gss_ctx_id_t context_hdl
;
284 read_token (pipefd
[0], &buf
);
286 maj_stat
= gss_import_sec_context (&min_stat
, &buf
, &context_hdl
);
287 if (GSS_ERROR(maj_stat
))
288 gss_err (1, min_stat
, "gss_import_sec_context");
289 gss_release_buffer (&min_stat
, &buf
);
290 return process_it (sock
, context_hdl
, client_name
);
293 return process_it (sock
, context_hdl
, client_name
);
298 doit (int port
, const char *service
)
301 struct sockaddr_in my_addr
;
305 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
309 memset (&my_addr
, 0, sizeof(my_addr
));
310 my_addr
.sin_family
= AF_INET
;
311 my_addr
.sin_port
= port
;
312 my_addr
.sin_addr
.s_addr
= INADDR_ANY
;
314 if (setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
,
315 (void *)&one
, sizeof(one
)) < 0)
316 warn ("setsockopt SO_REUSEADDR");
318 if (bind (sock
, (struct sockaddr
*)&my_addr
, sizeof(my_addr
)) < 0)
322 if (listen (sock
, 1) < 0)
325 sock2
= accept (sock
, NULL
, NULL
);
329 ret
= proto (sock2
, service
);
335 main(int argc
, char **argv
)
337 krb5_context context
= NULL
; /* XXX */
338 int port
= server_setup(&context
, argc
, argv
);
339 return doit (port
, service
);