remove <heimbase.h> since its not used
[heimdal.git] / appl / test / gssapi_client.c
blobe7682195dc4bce43233cd2b1b4089db8548b5202
1 /*
2 * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
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"
39 RCSID("$Id$");
41 static int
42 do_trans (int sock, gss_ctx_id_t context_hdl)
44 OM_uint32 maj_stat, min_stat;
45 gss_buffer_desc real_input_token, real_output_token;
46 gss_buffer_t input_token = &real_input_token,
47 output_token = &real_output_token;
49 /* get_mic */
51 input_token->length = 3;
52 input_token->value = strdup("hej");
54 maj_stat = gss_get_mic(&min_stat,
55 context_hdl,
56 GSS_C_QOP_DEFAULT,
57 input_token,
58 output_token);
59 if (GSS_ERROR(maj_stat))
60 gss_err (1, min_stat, "gss_get_mic");
62 write_token (sock, input_token);
63 write_token (sock, output_token);
65 /* wrap */
67 input_token->length = 7;
68 input_token->value = "hemligt";
70 maj_stat = gss_wrap (&min_stat,
71 context_hdl,
73 GSS_C_QOP_DEFAULT,
74 input_token,
75 NULL,
76 output_token);
77 if (GSS_ERROR(maj_stat))
78 gss_err (1, min_stat, "gss_wrap");
80 write_token (sock, output_token);
82 maj_stat = gss_wrap (&min_stat,
83 context_hdl,
85 GSS_C_QOP_DEFAULT,
86 input_token,
87 NULL,
88 output_token);
89 if (GSS_ERROR(maj_stat))
90 gss_err (1, min_stat, "gss_wrap");
92 write_token (sock, output_token);
94 return 0;
97 static int
98 proto (int sock, const char *hostname, const char *service)
100 struct sockaddr_in remote, local;
101 socklen_t addrlen;
103 int context_established = 0;
104 gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;
105 gss_buffer_desc real_input_token, real_output_token;
106 gss_buffer_t input_token = &real_input_token,
107 output_token = &real_output_token;
108 OM_uint32 maj_stat, min_stat;
109 gss_name_t server;
110 gss_buffer_desc name_token;
111 struct gss_channel_bindings_struct input_chan_bindings;
112 u_char init_buf[4];
113 u_char acct_buf[4];
114 gss_OID mech_oid;
115 char *str;
117 mech_oid = select_mech(mech);
119 name_token.length = asprintf (&str,
120 "%s@%s", service, hostname);
121 if (str == NULL)
122 errx(1, "malloc - out of memory");
123 name_token.value = str;
125 maj_stat = gss_import_name (&min_stat,
126 &name_token,
127 GSS_C_NT_HOSTBASED_SERVICE,
128 &server);
129 if (GSS_ERROR(maj_stat))
130 gss_err (1, min_stat,
131 "Error importing name `%s@%s':\n", service, hostname);
133 addrlen = sizeof(local);
134 if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
135 || addrlen != sizeof(local))
136 err (1, "getsockname(%s)", hostname);
138 addrlen = sizeof(remote);
139 if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
140 || addrlen != sizeof(remote))
141 err (1, "getpeername(%s)", hostname);
143 input_token->length = 0;
144 output_token->length = 0;
146 input_chan_bindings.initiator_addrtype = GSS_C_AF_INET;
147 input_chan_bindings.initiator_address.length = 4;
148 init_buf[0] = (local.sin_addr.s_addr >> 24) & 0xFF;
149 init_buf[1] = (local.sin_addr.s_addr >> 16) & 0xFF;
150 init_buf[2] = (local.sin_addr.s_addr >> 8) & 0xFF;
151 init_buf[3] = (local.sin_addr.s_addr >> 0) & 0xFF;
152 input_chan_bindings.initiator_address.value = init_buf;
154 input_chan_bindings.acceptor_addrtype = GSS_C_AF_INET;
155 input_chan_bindings.acceptor_address.length = 4;
156 acct_buf[0] = (remote.sin_addr.s_addr >> 24) & 0xFF;
157 acct_buf[1] = (remote.sin_addr.s_addr >> 16) & 0xFF;
158 acct_buf[2] = (remote.sin_addr.s_addr >> 8) & 0xFF;
159 acct_buf[3] = (remote.sin_addr.s_addr >> 0) & 0xFF;
160 input_chan_bindings.acceptor_address.value = acct_buf;
162 #if 0
163 input_chan_bindings.application_data.value = emalloc(4);
164 * (unsigned short*)input_chan_bindings.application_data.value = local.sin_port;
165 * ((unsigned short *)input_chan_bindings.application_data.value + 1) = remote.sin_port;
166 input_chan_bindings.application_data.length = 4;
167 #else
168 input_chan_bindings.application_data.length = 0;
169 input_chan_bindings.application_data.value = NULL;
170 #endif
172 while(!context_established) {
173 maj_stat =
174 gss_init_sec_context(&min_stat,
175 GSS_C_NO_CREDENTIAL,
176 &context_hdl,
177 server,
178 mech_oid,
179 GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG
180 | GSS_C_DELEG_FLAG,
182 &input_chan_bindings,
183 input_token,
184 NULL,
185 output_token,
186 NULL,
187 NULL);
188 if (GSS_ERROR(maj_stat))
189 gss_err (1, min_stat, "gss_init_sec_context");
190 if (output_token->length != 0)
191 write_token (sock, output_token);
192 if (GSS_ERROR(maj_stat)) {
193 if (context_hdl != GSS_C_NO_CONTEXT)
194 gss_delete_sec_context (&min_stat,
195 &context_hdl,
196 GSS_C_NO_BUFFER);
197 break;
199 if (maj_stat & GSS_S_CONTINUE_NEEDED) {
200 read_token (sock, input_token);
201 } else {
202 context_established = 1;
206 if (fork_flag) {
207 pid_t pid;
208 int pipefd[2];
210 if (pipe (pipefd) < 0)
211 err (1, "pipe");
213 pid = fork ();
214 if (pid < 0)
215 err (1, "fork");
216 if (pid != 0) {
217 gss_buffer_desc buf;
219 maj_stat = gss_export_sec_context (&min_stat,
220 &context_hdl,
221 &buf);
222 if (GSS_ERROR(maj_stat))
223 gss_err (1, min_stat, "gss_export_sec_context");
224 write_token (pipefd[1], &buf);
225 exit (0);
226 } else {
227 gss_ctx_id_t context_hdl;
228 gss_buffer_desc buf;
230 close (pipefd[1]);
231 read_token (pipefd[0], &buf);
232 close (pipefd[0]);
233 maj_stat = gss_import_sec_context (&min_stat, &buf, &context_hdl);
234 if (GSS_ERROR(maj_stat))
235 gss_err (1, min_stat, "gss_import_sec_context");
236 gss_release_buffer (&min_stat, &buf);
237 return do_trans (sock, context_hdl);
239 } else {
240 return do_trans (sock, context_hdl);
245 main(int argc, char **argv)
247 krb5_context context; /* XXX */
248 int port = client_setup(&context, &argc, argv);
249 return client_doit (argv[argc], port, service, proto);