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"
36 #include "gss_common.h"
37 __RCSID("$Heimdal: gssapi_server.c 14762 2005-04-10 14:47:41Z lha $"
42 gss_ctx_id_t context_hdl
,
43 gss_name_t client_name
46 OM_uint32 maj_stat
, min_stat
;
47 gss_buffer_desc real_input_token
, real_output_token
;
48 gss_buffer_t input_token
= &real_input_token
,
49 output_token
= &real_output_token
;
50 gss_name_t server_name
;
53 print_gss_name("User is", client_name
);
55 maj_stat
= gss_inquire_context(&min_stat
,
64 if (GSS_ERROR(maj_stat
))
65 gss_err (1, min_stat
, "gss_inquire_context");
67 print_gss_name("Server is", server_name
);
69 maj_stat
= gss_release_name(&min_stat
, &server_name
);
70 if (GSS_ERROR(maj_stat
))
71 gss_err (1, min_stat
, "gss_release_name");
75 read_token (sock
, input_token
);
76 read_token (sock
, output_token
);
78 maj_stat
= gss_verify_mic (&min_stat
,
83 if (GSS_ERROR(maj_stat
))
84 gss_err (1, min_stat
, "gss_verify_mic");
86 fprintf (stderr
, "gss_verify_mic: %.*s\n", (int)input_token
->length
,
87 (char *)input_token
->value
);
89 gss_release_buffer (&min_stat
, input_token
);
90 gss_release_buffer (&min_stat
, output_token
);
94 read_token (sock
, input_token
);
96 maj_stat
= gss_unwrap (&min_stat
,
102 if(GSS_ERROR(maj_stat
))
103 gss_err (1, min_stat
, "gss_unwrap");
105 fprintf (stderr
, "gss_unwrap: %.*s %s\n", (int)output_token
->length
,
106 (char *)output_token
->value
,
107 conf_flag
? "CONF" : "INT");
109 gss_release_buffer (&min_stat
, input_token
);
110 gss_release_buffer (&min_stat
, output_token
);
112 read_token (sock
, input_token
);
114 maj_stat
= gss_unwrap (&min_stat
,
120 if(GSS_ERROR(maj_stat
))
121 gss_err (1, min_stat
, "gss_unwrap");
123 fprintf (stderr
, "gss_unwrap: %.*s %s\n", (int)output_token
->length
,
124 (char *)output_token
->value
,
125 conf_flag
? "CONF" : "INT");
127 gss_release_buffer (&min_stat
, input_token
);
128 gss_release_buffer (&min_stat
, output_token
);
134 proto (int sock
, const char *service
)
136 struct sockaddr_in remote
, local
;
138 gss_ctx_id_t context_hdl
= GSS_C_NO_CONTEXT
;
139 gss_buffer_desc real_input_token
, real_output_token
;
140 gss_buffer_t input_token
= &real_input_token
,
141 output_token
= &real_output_token
;
142 OM_uint32 maj_stat
, min_stat
;
143 gss_name_t client_name
;
144 struct gss_channel_bindings_struct input_chan_bindings
;
145 gss_cred_id_t delegated_cred_handle
= NULL
;
152 addrlen
= sizeof(local
);
153 if (getsockname (sock
, (struct sockaddr
*)&local
, &addrlen
) < 0
154 || addrlen
!= sizeof(local
))
155 err (1, "getsockname)");
157 addrlen
= sizeof(remote
);
158 if (getpeername (sock
, (struct sockaddr
*)&remote
, &addrlen
) < 0
159 || addrlen
!= sizeof(remote
))
160 err (1, "getpeername");
162 input_chan_bindings
.initiator_addrtype
= GSS_C_AF_INET
;
163 input_chan_bindings
.initiator_address
.length
= 4;
164 init_buf
[0] = (remote
.sin_addr
.s_addr
>> 24) & 0xFF;
165 init_buf
[1] = (remote
.sin_addr
.s_addr
>> 16) & 0xFF;
166 init_buf
[2] = (remote
.sin_addr
.s_addr
>> 8) & 0xFF;
167 init_buf
[3] = (remote
.sin_addr
.s_addr
>> 0) & 0xFF;
169 input_chan_bindings
.initiator_address
.value
= init_buf
;
170 input_chan_bindings
.acceptor_addrtype
= GSS_C_AF_INET
;
172 input_chan_bindings
.acceptor_address
.length
= 4;
173 acct_buf
[0] = (local
.sin_addr
.s_addr
>> 24) & 0xFF;
174 acct_buf
[1] = (local
.sin_addr
.s_addr
>> 16) & 0xFF;
175 acct_buf
[2] = (local
.sin_addr
.s_addr
>> 8) & 0xFF;
176 acct_buf
[3] = (local
.sin_addr
.s_addr
>> 0) & 0xFF;
177 input_chan_bindings
.acceptor_address
.value
= acct_buf
;
178 input_chan_bindings
.application_data
.value
= emalloc(4);
180 * (unsigned short *)input_chan_bindings
.application_data
.value
=
182 * ((unsigned short *)input_chan_bindings
.application_data
.value
+ 1) =
184 input_chan_bindings
.application_data
.length
= 4;
186 input_chan_bindings
.application_data
.length
= 0;
187 input_chan_bindings
.application_data
.value
= NULL
;
190 delegated_cred_handle
= GSS_C_NO_CREDENTIAL
;
193 read_token (sock
, input_token
);
195 gss_accept_sec_context (&min_stat
,
199 &input_chan_bindings
,
205 &delegated_cred_handle
);
206 if(GSS_ERROR(maj_stat
))
207 gss_err (1, min_stat
, "gss_accept_sec_context");
208 if (output_token
->length
!= 0)
209 write_token (sock
, output_token
);
210 if (GSS_ERROR(maj_stat
)) {
211 if (context_hdl
!= GSS_C_NO_CONTEXT
)
212 gss_delete_sec_context (&min_stat
,
217 } while(maj_stat
& GSS_S_CONTINUE_NEEDED
);
219 p
= (char *)mech_oid
->elements
;
220 if (mech_oid
->length
== GSS_KRB5_MECHANISM
->length
221 && memcmp(p
, GSS_KRB5_MECHANISM
->elements
, mech_oid
->length
) == 0)
223 else if (mech_oid
->length
== GSS_SPNEGO_MECHANISM
->length
224 && memcmp(p
, GSS_SPNEGO_MECHANISM
->elements
, mech_oid
->length
) == 0)
225 mech
= "SPNEGO"; /* XXX Silly, wont show up */
229 printf("Using mech: %s\n", mech
);
231 if (delegated_cred_handle
!= GSS_C_NO_CREDENTIAL
) {
232 krb5_context context
;
234 printf("Delegated cred found\n");
236 maj_stat
= krb5_init_context(&context
);
237 maj_stat
= krb5_cc_resolve(context
, "FILE:/tmp/krb5cc_test", &ccache
);
238 maj_stat
= gss_krb5_copy_ccache(&min_stat
,
239 delegated_cred_handle
,
243 maj_stat
= krb5_cc_get_principal(context
, ccache
, &p
);
246 maj_stat
= krb5_unparse_name(context
, p
, &name
);
248 printf("Delegated user is: `%s'\n", name
);
251 krb5_free_principal(context
, p
);
254 krb5_cc_close(context
, ccache
);
255 gss_release_cred(&min_stat
, &delegated_cred_handle
);
262 if (pipe (pipefd
) < 0)
271 maj_stat
= gss_export_sec_context (&min_stat
,
274 if (GSS_ERROR(maj_stat
))
275 gss_err (1, min_stat
, "gss_export_sec_context");
276 write_token (pipefd
[1], &buf
);
279 gss_ctx_id_t context_hdl
;
283 read_token (pipefd
[0], &buf
);
285 maj_stat
= gss_import_sec_context (&min_stat
, &buf
, &context_hdl
);
286 if (GSS_ERROR(maj_stat
))
287 gss_err (1, min_stat
, "gss_import_sec_context");
288 gss_release_buffer (&min_stat
, &buf
);
289 return process_it (sock
, context_hdl
, client_name
);
292 return process_it (sock
, context_hdl
, client_name
);
297 doit (int port
, const char *service
)
300 struct sockaddr_in my_addr
;
303 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
307 memset (&my_addr
, 0, sizeof(my_addr
));
308 my_addr
.sin_family
= AF_INET
;
309 my_addr
.sin_port
= port
;
310 my_addr
.sin_addr
.s_addr
= INADDR_ANY
;
312 if (setsockopt (sock
, SOL_SOCKET
, SO_REUSEADDR
,
313 (void *)&one
, sizeof(one
)) < 0)
314 warn ("setsockopt SO_REUSEADDR");
316 if (bind (sock
, (struct sockaddr
*)&my_addr
, sizeof(my_addr
)) < 0)
319 if (listen (sock
, 1) < 0)
322 sock2
= accept (sock
, NULL
, NULL
);
326 return proto (sock2
, service
);
330 main(int argc
, char **argv
)
332 krb5_context context
= NULL
; /* XXX */
333 int port
= server_setup(&context
, argc
, argv
);
334 return doit (port
, service
);