Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / test / nt_gss_server.c
blob67a60f441742cff102d7dcd6506eaa7f835db7ca
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.h>
36 #include <krb5.h>
37 #include "nt_gss_common.h"
39 __RCSID("$Heimdal: nt_gss_server.c 12323 2003-05-21 15:15:34Z lha $"
40 "$NetBSD$");
43 * This program tries to act as a server for the sample in `Sample
44 * SSPI Code' in Windows 2000 RC1 SDK.
46 * use --dump-auth to get a binary dump of the authorization data in the ticket
49 static int help_flag;
50 static int version_flag;
51 static char *port_str;
52 char *service = SERVICE;
53 static char *auth_file;
55 static struct getargs args[] = {
56 { "port", 'p', arg_string, &port_str, "port to listen to", "port" },
57 { "service", 's', arg_string, &service, "service to use", "service" },
58 { "dump-auth", 0, arg_string, &auth_file, "dump authorization data",
59 "file" },
60 { "help", 'h', arg_flag, &help_flag },
61 { "version", 0, arg_flag, &version_flag }
64 static int num_args = sizeof(args) / sizeof(args[0]);
66 static int
67 proto (int sock, const char *service)
69 struct sockaddr_in remote, local;
70 socklen_t addrlen;
71 gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;
72 gss_buffer_t input_token, output_token;
73 gss_buffer_desc real_input_token, real_output_token;
74 OM_uint32 maj_stat, min_stat;
75 gss_name_t client_name;
76 gss_buffer_desc name_token;
78 addrlen = sizeof(local);
79 if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
80 || addrlen != sizeof(local))
81 err (1, "getsockname)");
83 addrlen = sizeof(remote);
84 if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
85 || addrlen != sizeof(remote))
86 err (1, "getpeername");
88 input_token = &real_input_token;
89 output_token = &real_output_token;
91 do {
92 nt_read_token (sock, input_token);
93 maj_stat =
94 gss_accept_sec_context (&min_stat,
95 &context_hdl,
96 GSS_C_NO_CREDENTIAL,
97 input_token,
98 GSS_C_NO_CHANNEL_BINDINGS,
99 &client_name,
100 NULL,
101 output_token,
102 NULL,
103 NULL,
104 NULL);
105 if(GSS_ERROR(maj_stat))
106 gss_err (1, min_stat, "gss_accept_sec_context");
107 if (output_token->length != 0)
108 nt_write_token (sock, output_token);
109 if (GSS_ERROR(maj_stat)) {
110 if (context_hdl != GSS_C_NO_CONTEXT)
111 gss_delete_sec_context (&min_stat,
112 &context_hdl,
113 GSS_C_NO_BUFFER);
114 break;
116 } while(maj_stat & GSS_S_CONTINUE_NEEDED);
118 if (auth_file != NULL) {
119 int fd = open (auth_file, O_WRONLY | O_CREAT, 0666);
120 #if 0
121 krb5_ticket *ticket;
122 krb5_data *data;
124 ticket = context_hdl->ticket;
125 data = &ticket->ticket.authorization_data->val[0].ad_data;
127 if(fd < 0)
128 err (1, "open %s", auth_file);
129 if (write (fd, data->data, data->length) != data->length)
130 errx (1, "write to %s failed", auth_file);
131 #endif
132 if (close (fd))
133 err (1, "close %s", auth_file);
136 maj_stat = gss_display_name (&min_stat,
137 client_name,
138 &name_token,
139 NULL);
140 if (GSS_ERROR(maj_stat))
141 gss_err (1, min_stat, "gss_display_name");
143 fprintf (stderr, "User is `%.*s'\n", (int)name_token.length,
144 (char *)name_token.value);
146 /* write something back */
148 output_token->value = strdup ("hejsan");
149 output_token->length = strlen (output_token->value) + 1;
150 nt_write_token (sock, output_token);
152 output_token->value = strdup ("hoppsan");
153 output_token->length = strlen (output_token->value) + 1;
154 nt_write_token (sock, output_token);
156 return 0;
159 static int
160 doit (int port, const char *service)
162 int sock, sock2;
163 struct sockaddr_in my_addr;
164 int one = 1;
166 sock = socket (AF_INET, SOCK_STREAM, 0);
167 if (sock < 0)
168 err (1, "socket");
170 memset (&my_addr, 0, sizeof(my_addr));
171 my_addr.sin_family = AF_INET;
172 my_addr.sin_port = port;
173 my_addr.sin_addr.s_addr = INADDR_ANY;
175 if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
176 (void *)&one, sizeof(one)) < 0)
177 warn ("setsockopt SO_REUSEADDR");
179 if (bind (sock, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
180 err (1, "bind");
182 if (listen (sock, 1) < 0)
183 err (1, "listen");
185 sock2 = accept (sock, NULL, NULL);
186 if (sock2 < 0)
187 err (1, "accept");
189 return proto (sock2, service);
192 static void
193 usage(int code, struct getargs *args, int num_args)
195 arg_printusage(args, num_args, NULL, "");
196 exit(code);
199 static int
200 common_setup(krb5_context *context, int *argc, char **argv,
201 void (*usage)(int, struct getargs*, int))
203 int port = 0;
204 *argc = krb5_program_setup(context, *argc, argv, args, num_args, usage);
206 if(help_flag)
207 (*usage)(0, args, num_args);
208 if(version_flag) {
209 print_version(NULL);
210 exit(0);
213 if(port_str){
214 struct servent *s = roken_getservbyname(port_str, "tcp");
215 if(s)
216 port = s->s_port;
217 else {
218 char *ptr;
220 port = strtol (port_str, &ptr, 10);
221 if (port == 0 && ptr == port_str)
222 errx (1, "Bad port `%s'", port_str);
223 port = htons(port);
227 if (port == 0)
228 port = krb5_getportbyname (*context, PORT, "tcp", 4711);
230 return port;
233 static int
234 setup(krb5_context *context, int argc, char **argv)
236 int port = common_setup(context, &argc, argv, usage);
237 if(argv[argc] != NULL)
238 usage(1, args, num_args);
239 return port;
243 main(int argc, char **argv)
245 krb5_context context = NULL; /* XXX */
246 int port = setup(&context, argc, argv);
247 return doit (port, service);