xref: /freebsd/crypto/heimdal/appl/test/gssapi_client.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Copyright (c) 1997 - 2000 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 "test_locl.h"
35 #include <gssapi.h>
36 #include "gss_common.h"
37 RCSID("$Id: gssapi_client.c,v 1.12 2000/02/12 21:33:17 assar Exp $");
38 
39 static int
40 do_trans (int sock, gss_ctx_id_t context_hdl)
41 {
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;
46 
47     /* get_mic */
48 
49     input_token->length = 3;
50     input_token->value  = strdup("hej");
51 
52     maj_stat = gss_get_mic(&min_stat,
53 			   context_hdl,
54 			   GSS_C_QOP_DEFAULT,
55 			   input_token,
56 			   output_token);
57     if (GSS_ERROR(maj_stat))
58 	gss_err (1, min_stat, "gss_get_mic");
59 
60     write_token (sock, input_token);
61     write_token (sock, output_token);
62 
63     /* wrap */
64 
65     input_token->length = 7;
66     input_token->value  = "hemligt";
67 
68 
69     maj_stat = gss_wrap (&min_stat,
70 			 context_hdl,
71 			 1,
72 			 GSS_C_QOP_DEFAULT,
73 			 input_token,
74 			 NULL,
75 			 output_token);
76     if (GSS_ERROR(maj_stat))
77 	gss_err (1, min_stat, "gss_wrap");
78 
79     write_token (sock, output_token);
80 
81     return 0;
82 }
83 
84 static int
85 proto (int sock, const char *hostname, const char *service)
86 {
87     struct sockaddr_in remote, local;
88     int addrlen;
89 
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;
96     gss_name_t server;
97     gss_buffer_desc name_token;
98 
99     name_token.length = asprintf ((char **)&name_token.value,
100 				  "%s@%s", service, hostname);
101 
102     maj_stat = gss_import_name (&min_stat,
103 				&name_token,
104 				GSS_C_NT_HOSTBASED_SERVICE,
105 				&server);
106     if (GSS_ERROR(maj_stat))
107 	gss_err (1, min_stat,
108 		 "Error importing name `%s@%s':\n", service, hostname);
109 
110     addrlen = sizeof(local);
111     if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
112 	|| addrlen != sizeof(local))
113 	err (1, "getsockname(%s)", hostname);
114 
115     addrlen = sizeof(remote);
116     if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
117 	|| addrlen != sizeof(remote))
118 	err (1, "getpeername(%s)", hostname);
119 
120     input_token->length = 0;
121     output_token->length = 0;
122 
123     while(!context_established) {
124 	maj_stat =
125 	    gss_init_sec_context(&min_stat,
126 				 GSS_C_NO_CREDENTIAL,
127 				 &context_hdl,
128 				 server,
129 				 GSS_C_NO_OID,
130 				 GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
131 				 0,
132 				 GSS_C_NO_CHANNEL_BINDINGS,
133 				 input_token,
134 				 NULL,
135 				 output_token,
136 				 NULL,
137 				 NULL);
138 	if (GSS_ERROR(maj_stat))
139 	    gss_err (1, min_stat, "gss_init_sec_context");
140 	if (output_token->length != 0)
141 	    write_token (sock, output_token);
142 	if (GSS_ERROR(maj_stat)) {
143 	    if (context_hdl != GSS_C_NO_CONTEXT)
144 		gss_delete_sec_context (&min_stat,
145 					&context_hdl,
146 					GSS_C_NO_BUFFER);
147 	    break;
148 	}
149 	if (maj_stat & GSS_S_CONTINUE_NEEDED) {
150 	    read_token (sock, input_token);
151 	} else {
152 	    context_established = 1;
153 	}
154 
155     }
156     if (fork_flag) {
157 	pid_t pid;
158 	int pipefd[2];
159 
160 	if (pipe (pipefd) < 0)
161 	    err (1, "pipe");
162 
163 	pid = fork ();
164 	if (pid < 0)
165 	    err (1, "fork");
166 	if (pid != 0) {
167 	    gss_buffer_desc buf;
168 
169 	    maj_stat = gss_export_sec_context (&min_stat,
170 					       &context_hdl,
171 					       &buf);
172 	    if (GSS_ERROR(maj_stat))
173 		gss_err (1, min_stat, "gss_export_sec_context");
174 	    write_token (pipefd[1], &buf);
175 	    exit (0);
176 	} else {
177 	    gss_ctx_id_t context_hdl;
178 	    gss_buffer_desc buf;
179 
180 	    close (pipefd[1]);
181 	    read_token (pipefd[0], &buf);
182 	    close (pipefd[0]);
183 	    maj_stat = gss_import_sec_context (&min_stat, &buf, &context_hdl);
184 	    if (GSS_ERROR(maj_stat))
185 		gss_err (1, min_stat, "gss_import_sec_context");
186 	    gss_release_buffer (&min_stat, &buf);
187 	    return do_trans (sock, context_hdl);
188 	}
189     } else {
190 	return do_trans (sock, context_hdl);
191     }
192 }
193 
194 int
195 main(int argc, char **argv)
196 {
197     krb5_context context; /* XXX */
198     int port = client_setup(&context, &argc, argv);
199     return client_doit (argv[argc], port, service, proto);
200 }
201