Path: blob/main/crypto/heimdal/appl/test/common.c
105866 views
/*1* Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "test_locl.h"3435RCSID("$Id$");3637static int help_flag;38static int version_flag;39static char *port_str;40static char *keytab_str;41krb5_keytab keytab;42char *service = SERVICE;43char *mech = "krb5";44int fork_flag;45char *password = NULL;4647static struct getargs args[] = {48{ "port", 'p', arg_string, &port_str, "port to listen to", "port" },49{ "service", 's', arg_string, &service, "service to use", "service" },50{ "keytab", 'k', arg_string, &keytab_str, "keytab to use", "keytab" },51{ "mech", 'm', arg_string, &mech, "gssapi mech to use", "mech" },52{ "password", 'P', arg_string, &password, "password to use", "password" },53{ "fork", 'f', arg_flag, &fork_flag, "do fork" },54{ "help", 'h', arg_flag, &help_flag },55{ "version", 0, arg_flag, &version_flag }56};5758static int num_args = sizeof(args) / sizeof(args[0]);5960static void61server_usage(int code, struct getargs *args, int num_args)62{63arg_printusage(args, num_args, NULL, "");64exit(code);65}6667static void68client_usage(int code, struct getargs *args, int num_args)69{70arg_printusage(args, num_args, NULL, "host");71exit(code);72}737475static int76common_setup(krb5_context *context, int *argc, char **argv,77void (*usage)(int, struct getargs*, int))78{79int port = 0;80*argc = krb5_program_setup(context, *argc, argv, args, num_args, usage);8182if(help_flag)83(*usage)(0, args, num_args);84if(version_flag) {85print_version(NULL);86exit(0);87}8889if(port_str){90struct servent *s = roken_getservbyname(port_str, "tcp");91if(s)92port = s->s_port;93else {94char *ptr;9596port = strtol (port_str, &ptr, 10);97if (port == 0 && ptr == port_str)98errx (1, "Bad port `%s'", port_str);99port = htons(port);100}101}102103if (port == 0)104port = krb5_getportbyname (*context, PORT, "tcp", 4711);105106return port;107}108109int110server_setup(krb5_context *context, int argc, char **argv)111{112int port = common_setup(context, &argc, argv, server_usage);113krb5_error_code ret;114115if(argv[argc] != NULL)116server_usage(1, args, num_args);117if (keytab_str != NULL)118ret = krb5_kt_resolve (*context, keytab_str, &keytab);119else120ret = krb5_kt_default (*context, &keytab);121if (ret)122krb5_err (*context, 1, ret, "krb5_kt_resolve/default");123return port;124}125126int127client_setup(krb5_context *context, int *argc, char **argv)128{129int optind = *argc;130int port = common_setup(context, &optind, argv, client_usage);131if(*argc - optind != 1)132client_usage(1, args, num_args);133*argc = optind;134return port;135}136137int138client_doit (const char *hostname, int port, const char *service,139int (*func)(int, const char *hostname, const char *service))140{141struct addrinfo *ai, *a;142struct addrinfo hints;143int error;144char portstr[NI_MAXSERV];145146memset (&hints, 0, sizeof(hints));147hints.ai_socktype = SOCK_STREAM;148hints.ai_protocol = IPPROTO_TCP;149150snprintf (portstr, sizeof(portstr), "%u", ntohs(port));151152error = getaddrinfo (hostname, portstr, &hints, &ai);153if (error) {154errx (1, "%s: %s", hostname, gai_strerror(error));155return -1;156}157158for (a = ai; a != NULL; a = a->ai_next) {159int s;160161s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);162if (s < 0)163continue;164if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {165warn ("connect(%s)", hostname);166close (s);167continue;168}169freeaddrinfo (ai);170return (*func) (s, hostname, service);171}172warnx ("failed to contact %s", hostname);173freeaddrinfo (ai);174return 1;175}176177178