1b528cefcSMark Murray /* 25e9cd1aeSAssar Westerlund * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan 3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden). 4b528cefcSMark Murray * All rights reserved. 5b528cefcSMark Murray * 6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without 7b528cefcSMark Murray * modification, are permitted provided that the following conditions 8b528cefcSMark Murray * are met: 9b528cefcSMark Murray * 10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright 11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer. 12b528cefcSMark Murray * 13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright 14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the 15b528cefcSMark Murray * documentation and/or other materials provided with the distribution. 16b528cefcSMark Murray * 17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors 18b528cefcSMark Murray * may be used to endorse or promote products derived from this software 19b528cefcSMark Murray * without specific prior written permission. 20b528cefcSMark Murray * 21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31b528cefcSMark Murray * SUCH DAMAGE. 32b528cefcSMark Murray */ 33b528cefcSMark Murray 34b528cefcSMark Murray #include "test_locl.h" 35b528cefcSMark Murray #include <gssapi.h> 36b528cefcSMark Murray #include <krb5.h> 37b528cefcSMark Murray #include "nt_gss_common.h" 38b528cefcSMark Murray 39c19800e8SDoug Rabson RCSID("$Id: nt_gss_server.c 12323 2003-05-21 15:15:34Z lha $"); 40b528cefcSMark Murray 41b528cefcSMark Murray /* 42b528cefcSMark Murray * This program tries to act as a server for the sample in `Sample 43b528cefcSMark Murray * SSPI Code' in Windows 2000 RC1 SDK. 44b528cefcSMark Murray * 455e9cd1aeSAssar Westerlund * use --dump-auth to get a binary dump of the authorization data in the ticket 46b528cefcSMark Murray */ 47b528cefcSMark Murray 48b528cefcSMark Murray static int help_flag; 49b528cefcSMark Murray static int version_flag; 50b528cefcSMark Murray static char *port_str; 51b528cefcSMark Murray char *service = SERVICE; 52b528cefcSMark Murray static char *auth_file; 53b528cefcSMark Murray 54b528cefcSMark Murray static struct getargs args[] = { 55b528cefcSMark Murray { "port", 'p', arg_string, &port_str, "port to listen to", "port" }, 56b528cefcSMark Murray { "service", 's', arg_string, &service, "service to use", "service" }, 57b528cefcSMark Murray { "dump-auth", 0, arg_string, &auth_file, "dump authorization data", 58b528cefcSMark Murray "file" }, 59b528cefcSMark Murray { "help", 'h', arg_flag, &help_flag }, 60b528cefcSMark Murray { "version", 0, arg_flag, &version_flag } 61b528cefcSMark Murray }; 62b528cefcSMark Murray 63b528cefcSMark Murray static int num_args = sizeof(args) / sizeof(args[0]); 64b528cefcSMark Murray 65b528cefcSMark Murray static int 66b528cefcSMark Murray proto (int sock, const char *service) 67b528cefcSMark Murray { 68b528cefcSMark Murray struct sockaddr_in remote, local; 695e9cd1aeSAssar Westerlund socklen_t addrlen; 70b528cefcSMark Murray gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT; 71b528cefcSMark Murray gss_buffer_t input_token, output_token; 72b528cefcSMark Murray gss_buffer_desc real_input_token, real_output_token; 73b528cefcSMark Murray OM_uint32 maj_stat, min_stat; 74b528cefcSMark Murray gss_name_t client_name; 75b528cefcSMark Murray gss_buffer_desc name_token; 76b528cefcSMark Murray 77b528cefcSMark Murray addrlen = sizeof(local); 78b528cefcSMark Murray if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0 79b528cefcSMark Murray || addrlen != sizeof(local)) 80b528cefcSMark Murray err (1, "getsockname)"); 81b528cefcSMark Murray 82b528cefcSMark Murray addrlen = sizeof(remote); 83b528cefcSMark Murray if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0 84b528cefcSMark Murray || addrlen != sizeof(remote)) 85b528cefcSMark Murray err (1, "getpeername"); 86b528cefcSMark Murray 87b528cefcSMark Murray input_token = &real_input_token; 88b528cefcSMark Murray output_token = &real_output_token; 89b528cefcSMark Murray 90b528cefcSMark Murray do { 91b528cefcSMark Murray nt_read_token (sock, input_token); 92b528cefcSMark Murray maj_stat = 93b528cefcSMark Murray gss_accept_sec_context (&min_stat, 94b528cefcSMark Murray &context_hdl, 95b528cefcSMark Murray GSS_C_NO_CREDENTIAL, 96b528cefcSMark Murray input_token, 97b528cefcSMark Murray GSS_C_NO_CHANNEL_BINDINGS, 98b528cefcSMark Murray &client_name, 99b528cefcSMark Murray NULL, 100b528cefcSMark Murray output_token, 101b528cefcSMark Murray NULL, 102b528cefcSMark Murray NULL, 103b528cefcSMark Murray NULL); 104b528cefcSMark Murray if(GSS_ERROR(maj_stat)) 105b528cefcSMark Murray gss_err (1, min_stat, "gss_accept_sec_context"); 106b528cefcSMark Murray if (output_token->length != 0) 107b528cefcSMark Murray nt_write_token (sock, output_token); 108b528cefcSMark Murray if (GSS_ERROR(maj_stat)) { 109b528cefcSMark Murray if (context_hdl != GSS_C_NO_CONTEXT) 110b528cefcSMark Murray gss_delete_sec_context (&min_stat, 111b528cefcSMark Murray &context_hdl, 112b528cefcSMark Murray GSS_C_NO_BUFFER); 113b528cefcSMark Murray break; 114b528cefcSMark Murray } 115b528cefcSMark Murray } while(maj_stat & GSS_S_CONTINUE_NEEDED); 116b528cefcSMark Murray 117b528cefcSMark Murray if (auth_file != NULL) { 118b528cefcSMark Murray int fd = open (auth_file, O_WRONLY | O_CREAT, 0666); 119c19800e8SDoug Rabson #if 0 120c19800e8SDoug Rabson krb5_ticket *ticket; 121c19800e8SDoug Rabson krb5_data *data; 122c19800e8SDoug Rabson 123c19800e8SDoug Rabson ticket = context_hdl->ticket; 124c19800e8SDoug Rabson data = &ticket->ticket.authorization_data->val[0].ad_data; 125b528cefcSMark Murray 126b528cefcSMark Murray if(fd < 0) 127b528cefcSMark Murray err (1, "open %s", auth_file); 128b528cefcSMark Murray if (write (fd, data->data, data->length) != data->length) 129b528cefcSMark Murray errx (1, "write to %s failed", auth_file); 130c19800e8SDoug Rabson #endif 131b528cefcSMark Murray if (close (fd)) 132b528cefcSMark Murray err (1, "close %s", auth_file); 133b528cefcSMark Murray } 134b528cefcSMark Murray 135b528cefcSMark Murray maj_stat = gss_display_name (&min_stat, 136b528cefcSMark Murray client_name, 137b528cefcSMark Murray &name_token, 138b528cefcSMark Murray NULL); 139b528cefcSMark Murray if (GSS_ERROR(maj_stat)) 140b528cefcSMark Murray gss_err (1, min_stat, "gss_display_name"); 141b528cefcSMark Murray 142b528cefcSMark Murray fprintf (stderr, "User is `%.*s'\n", (int)name_token.length, 143b528cefcSMark Murray (char *)name_token.value); 144b528cefcSMark Murray 145b528cefcSMark Murray /* write something back */ 146b528cefcSMark Murray 147b528cefcSMark Murray output_token->value = strdup ("hejsan"); 148b528cefcSMark Murray output_token->length = strlen (output_token->value) + 1; 149b528cefcSMark Murray nt_write_token (sock, output_token); 150b528cefcSMark Murray 151b528cefcSMark Murray output_token->value = strdup ("hoppsan"); 152b528cefcSMark Murray output_token->length = strlen (output_token->value) + 1; 153b528cefcSMark Murray nt_write_token (sock, output_token); 154b528cefcSMark Murray 155b528cefcSMark Murray return 0; 156b528cefcSMark Murray } 157b528cefcSMark Murray 158b528cefcSMark Murray static int 159b528cefcSMark Murray doit (int port, const char *service) 160b528cefcSMark Murray { 161b528cefcSMark Murray int sock, sock2; 162b528cefcSMark Murray struct sockaddr_in my_addr; 163b528cefcSMark Murray int one = 1; 164b528cefcSMark Murray 165b528cefcSMark Murray sock = socket (AF_INET, SOCK_STREAM, 0); 166b528cefcSMark Murray if (sock < 0) 167b528cefcSMark Murray err (1, "socket"); 168b528cefcSMark Murray 169b528cefcSMark Murray memset (&my_addr, 0, sizeof(my_addr)); 170b528cefcSMark Murray my_addr.sin_family = AF_INET; 171b528cefcSMark Murray my_addr.sin_port = port; 172b528cefcSMark Murray my_addr.sin_addr.s_addr = INADDR_ANY; 173b528cefcSMark Murray 174b528cefcSMark Murray if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, 175b528cefcSMark Murray (void *)&one, sizeof(one)) < 0) 176b528cefcSMark Murray warn ("setsockopt SO_REUSEADDR"); 177b528cefcSMark Murray 178b528cefcSMark Murray if (bind (sock, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) 179b528cefcSMark Murray err (1, "bind"); 180b528cefcSMark Murray 181b528cefcSMark Murray if (listen (sock, 1) < 0) 182b528cefcSMark Murray err (1, "listen"); 183b528cefcSMark Murray 184b528cefcSMark Murray sock2 = accept (sock, NULL, NULL); 185b528cefcSMark Murray if (sock2 < 0) 186b528cefcSMark Murray err (1, "accept"); 187b528cefcSMark Murray 188b528cefcSMark Murray return proto (sock2, service); 189b528cefcSMark Murray } 190b528cefcSMark Murray 191b528cefcSMark Murray static void 192b528cefcSMark Murray usage(int code, struct getargs *args, int num_args) 193b528cefcSMark Murray { 194b528cefcSMark Murray arg_printusage(args, num_args, NULL, ""); 195b528cefcSMark Murray exit(code); 196b528cefcSMark Murray } 197b528cefcSMark Murray 198b528cefcSMark Murray static int 199b528cefcSMark Murray common_setup(krb5_context *context, int *argc, char **argv, 200b528cefcSMark Murray void (*usage)(int, struct getargs*, int)) 201b528cefcSMark Murray { 202b528cefcSMark Murray int port = 0; 203b528cefcSMark Murray *argc = krb5_program_setup(context, *argc, argv, args, num_args, usage); 204b528cefcSMark Murray 205b528cefcSMark Murray if(help_flag) 206b528cefcSMark Murray (*usage)(0, args, num_args); 207b528cefcSMark Murray if(version_flag) { 208b528cefcSMark Murray print_version(NULL); 209b528cefcSMark Murray exit(0); 210b528cefcSMark Murray } 211b528cefcSMark Murray 212b528cefcSMark Murray if(port_str){ 213b528cefcSMark Murray struct servent *s = roken_getservbyname(port_str, "tcp"); 214b528cefcSMark Murray if(s) 215b528cefcSMark Murray port = s->s_port; 216b528cefcSMark Murray else { 217b528cefcSMark Murray char *ptr; 218b528cefcSMark Murray 219b528cefcSMark Murray port = strtol (port_str, &ptr, 10); 220b528cefcSMark Murray if (port == 0 && ptr == port_str) 221b528cefcSMark Murray errx (1, "Bad port `%s'", port_str); 222b528cefcSMark Murray port = htons(port); 223b528cefcSMark Murray } 224b528cefcSMark Murray } 225b528cefcSMark Murray 226b528cefcSMark Murray if (port == 0) 227b528cefcSMark Murray port = krb5_getportbyname (*context, PORT, "tcp", 4711); 228b528cefcSMark Murray 229b528cefcSMark Murray return port; 230b528cefcSMark Murray } 231b528cefcSMark Murray 232b528cefcSMark Murray static int 233b528cefcSMark Murray setup(krb5_context *context, int argc, char **argv) 234b528cefcSMark Murray { 235b528cefcSMark Murray int port = common_setup(context, &argc, argv, usage); 236b528cefcSMark Murray if(argv[argc] != NULL) 237b528cefcSMark Murray usage(1, args, num_args); 238b528cefcSMark Murray return port; 239b528cefcSMark Murray } 240b528cefcSMark Murray 241b528cefcSMark Murray int 242b528cefcSMark Murray main(int argc, char **argv) 243b528cefcSMark Murray { 244b528cefcSMark Murray krb5_context context = NULL; /* XXX */ 245b528cefcSMark Murray int port = setup(&context, argc, argv); 246b528cefcSMark Murray return doit (port, service); 247b528cefcSMark Murray } 248