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 36static int help_flag; 37static int version_flag; 38static char *port_str; 39char *keytab_str; 40krb5_keytab keytab; 41char *service_str = SERVICE; 42char *mech = "krb5"; 43int fork_flag; 44char *password = NULL; 45 46static struct getargs args[] = { 47 { "port", 'p', arg_string, &port_str, "port to listen to", "port" }, 48 { "service", 's', arg_string, &service_str, "service to use", "service" }, 49 { "keytab", 'k', arg_string, &keytab_str, "keytab to use", "keytab" }, 50 { "mech", 'm', arg_string, &mech, "gssapi mech to use", "mech" }, 51 { "password", 'P', arg_string, &password, "password to use", "password" }, 52 { "fork", 'f', arg_flag, &fork_flag, "do fork" }, 53 { "help", 'h', arg_flag, &help_flag }, 54 { "version", 0, arg_flag, &version_flag } 55}; 56 57static int num_args = sizeof(args) / sizeof(args[0]); 58 59static void 60server_usage(int code, struct getargs *largs, int lnum_args) 61{ 62 arg_printusage(largs, lnum_args, NULL, ""); 63 exit(code); 64} 65 66static void 67client_usage(int code, struct getargs *largs, int lnum_args) 68{ 69 arg_printusage(largs, lnum_args, NULL, "host"); 70 exit(code); 71} 72 73 74static int 75common_setup(krb5_context *context, int *argc, char **argv, 76 void (*usage)(int, struct getargs*, int)) 77{ 78 int port = 0; 79 *argc = krb5_program_setup(context, *argc, argv, args, num_args, usage); 80 81 if(help_flag) 82 (*usage)(0, args, num_args); 83 if(version_flag) { 84 print_version(NULL); 85 exit(0); 86 } 87 88 if(port_str){ 89 struct servent *s = roken_getservbyname(port_str, "tcp"); 90 if(s) 91 port = s->s_port; 92 else { 93 char *ptr; 94 95 port = (int)strtol (port_str, &ptr, 10); 96 if (port == 0 && ptr == port_str) 97 errx (1, "Bad port `%s'", port_str); 98 port = htons(port); 99 } 100 } 101 102 if (port == 0) 103 port = krb5_getportbyname (*context, PORT, "tcp", 4711); 104 105 return port; 106} 107 108int 109server_setup(krb5_context *context, int argc, char **argv) 110{ 111 int port = common_setup(context, &argc, argv, server_usage); 112 krb5_error_code ret; 113 114 if(argv[argc] != NULL) 115 server_usage(1, args, num_args); 116 if (keytab_str != NULL) 117 ret = krb5_kt_resolve (*context, keytab_str, &keytab); 118 else 119 ret = krb5_kt_default (*context, &keytab); 120 if (ret) 121 krb5_err (*context, 1, ret, "krb5_kt_resolve/default"); 122 return port; 123} 124 125int 126client_setup(krb5_context *context, int *argc, char **argv) 127{ 128 int optidx = *argc; 129 int port = common_setup(context, &optidx, argv, client_usage); 130 if(*argc - optidx != 1) 131 client_usage(1, args, num_args); 132 *argc = optidx; 133 return port; 134} 135 136int 137client_doit (const char *hostname, int port, const char *serv, 138 int (*func)(int, const char *hostname, const char *service)) 139{ 140 struct addrinfo *ai, *a; 141 struct addrinfo hints; 142 int error; 143 char portstr[NI_MAXSERV]; 144 145 memset (&hints, 0, sizeof(hints)); 146 hints.ai_socktype = SOCK_STREAM; 147 hints.ai_protocol = IPPROTO_TCP; 148 149 snprintf (portstr, sizeof(portstr), "%u", ntohs(port)); 150 151 error = getaddrinfo (hostname, portstr, &hints, &ai); 152 if (error) { 153 errx (1, "%s: %s", hostname, gai_strerror(error)); 154 } 155 156 for (a = ai; a != NULL; a = a->ai_next) { 157 int s; 158 159 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol); 160 if (s < 0) 161 continue; 162 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) { 163 warn ("connect(%s)", hostname); 164 close (s); 165 continue; 166 } 167 freeaddrinfo (ai); 168 return (*func) (s, hostname, serv); 169 } 170 warnx ("failed to contact %s", hostname); 171 freeaddrinfo (ai); 172 return 1; 173} 174