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("$Id: gssapi_client.c,v 1.16 2000/08/09 20:53:06 assar Exp $");
40 do_trans (int sock
, gss_ctx_id_t context_hdl
)
42 OM_uint32 maj_stat
, min_stat
;
43 gss_buffer_desc real_input_token
, real_output_token
;
44 gss_buffer_t input_token
= &real_input_token
,
45 output_token
= &real_output_token
;
49 input_token
->length
= 3;
50 input_token
->value
= strdup("hej");
52 maj_stat
= gss_get_mic(&min_stat
,
57 if (GSS_ERROR(maj_stat
))
58 gss_err (1, min_stat
, "gss_get_mic");
60 write_token (sock
, input_token
);
61 write_token (sock
, output_token
);
65 input_token
->length
= 7;
66 input_token
->value
= "hemligt";
69 maj_stat
= gss_wrap (&min_stat
,
76 if (GSS_ERROR(maj_stat
))
77 gss_err (1, min_stat
, "gss_wrap");
79 write_token (sock
, output_token
);
85 proto (int sock
, const char *hostname
, const char *service
)
87 struct sockaddr_in remote
, local
;
90 int context_established
= 0;
91 gss_ctx_id_t context_hdl
= GSS_C_NO_CONTEXT
;
92 gss_buffer_desc real_input_token
, real_output_token
;
93 gss_buffer_t input_token
= &real_input_token
,
94 output_token
= &real_output_token
;
95 OM_uint32 maj_stat
, min_stat
;
97 gss_buffer_desc name_token
;
98 struct gss_channel_bindings_struct input_chan_bindings
;
102 name_token
.length
= asprintf ((char **)&name_token
.value
,
103 "%s@%s", service
, hostname
);
105 maj_stat
= gss_import_name (&min_stat
,
107 GSS_C_NT_HOSTBASED_SERVICE
,
109 if (GSS_ERROR(maj_stat
))
110 gss_err (1, min_stat
,
111 "Error importing name `%s@%s':\n", service
, hostname
);
113 addrlen
= sizeof(local
);
114 if (getsockname (sock
, (struct sockaddr
*)&local
, &addrlen
) < 0
115 || addrlen
!= sizeof(local
))
116 err (1, "getsockname(%s)", hostname
);
118 addrlen
= sizeof(remote
);
119 if (getpeername (sock
, (struct sockaddr
*)&remote
, &addrlen
) < 0
120 || addrlen
!= sizeof(remote
))
121 err (1, "getpeername(%s)", hostname
);
123 input_token
->length
= 0;
124 output_token
->length
= 0;
126 input_chan_bindings
.initiator_addrtype
= GSS_C_AF_INET
;
127 input_chan_bindings
.initiator_address
.length
= 4;
128 init_buf
[0] = (local
.sin_addr
.s_addr
>> 24) & 0xFF;
129 init_buf
[1] = (local
.sin_addr
.s_addr
>> 16) & 0xFF;
130 init_buf
[2] = (local
.sin_addr
.s_addr
>> 8) & 0xFF;
131 init_buf
[3] = (local
.sin_addr
.s_addr
>> 0) & 0xFF;
132 input_chan_bindings
.initiator_address
.value
= init_buf
;
134 input_chan_bindings
.acceptor_addrtype
= GSS_C_AF_INET
;
135 input_chan_bindings
.acceptor_address
.length
= 4;
136 acct_buf
[0] = (remote
.sin_addr
.s_addr
>> 24) & 0xFF;
137 acct_buf
[1] = (remote
.sin_addr
.s_addr
>> 16) & 0xFF;
138 acct_buf
[2] = (remote
.sin_addr
.s_addr
>> 8) & 0xFF;
139 acct_buf
[3] = (remote
.sin_addr
.s_addr
>> 0) & 0xFF;
140 input_chan_bindings
.acceptor_address
.value
= acct_buf
;
143 input_chan_bindings
.application_data
.value
= emalloc(4);
144 * (unsigned short*)input_chan_bindings
.application_data
.value
= local
.sin_port
;
145 * ((unsigned short *)input_chan_bindings
.application_data
.value
+ 1) = remote
.sin_port
;
146 input_chan_bindings
.application_data
.length
= 4;
148 input_chan_bindings
.application_data
.length
= 0;
149 input_chan_bindings
.application_data
.value
= NULL
;
152 while(!context_established
) {
154 gss_init_sec_context(&min_stat
,
159 GSS_C_MUTUAL_FLAG
| GSS_C_SEQUENCE_FLAG
162 &input_chan_bindings
,
168 if (GSS_ERROR(maj_stat
))
169 gss_err (1, min_stat
, "gss_init_sec_context");
170 if (output_token
->length
!= 0)
171 write_token (sock
, output_token
);
172 if (GSS_ERROR(maj_stat
)) {
173 if (context_hdl
!= GSS_C_NO_CONTEXT
)
174 gss_delete_sec_context (&min_stat
,
179 if (maj_stat
& GSS_S_CONTINUE_NEEDED
) {
180 read_token (sock
, input_token
);
182 context_established
= 1;
190 if (pipe (pipefd
) < 0)
199 maj_stat
= gss_export_sec_context (&min_stat
,
202 if (GSS_ERROR(maj_stat
))
203 gss_err (1, min_stat
, "gss_export_sec_context");
204 write_token (pipefd
[1], &buf
);
207 gss_ctx_id_t context_hdl
;
211 read_token (pipefd
[0], &buf
);
213 maj_stat
= gss_import_sec_context (&min_stat
, &buf
, &context_hdl
);
214 if (GSS_ERROR(maj_stat
))
215 gss_err (1, min_stat
, "gss_import_sec_context");
216 gss_release_buffer (&min_stat
, &buf
);
217 return do_trans (sock
, context_hdl
);
220 return do_trans (sock
, context_hdl
);
225 main(int argc
, char **argv
)
227 krb5_context context
; /* XXX */
228 int port
= client_setup(&context
, &argc
, argv
);
229 return client_doit (argv
[argc
], port
, service
, proto
);