Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / test / common.c
blob523f96b6cb20058a5c6f5581898c63ac5cacbf37
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"
36 __RCSID("$Heimdal: common.c 12796 2003-09-09 03:38:04Z lha $"
37 "$NetBSD$");
39 static int help_flag;
40 static int version_flag;
41 static char *port_str;
42 static char *keytab_str;
43 krb5_keytab keytab;
44 char *service = SERVICE;
45 char *mech = "krb5";
46 int fork_flag;
48 static struct getargs args[] = {
49 { "port", 'p', arg_string, &port_str, "port to listen to", "port" },
50 { "service", 's', arg_string, &service, "service to use", "service" },
51 { "keytab", 'k', arg_string, &keytab_str, "keytab to use", "keytab" },
52 { "mech", 'm', arg_string, &mech, "gssapi mech to use", "mech" },
53 { "fork", 'f', arg_flag, &fork_flag, "do fork" },
54 { "help", 'h', arg_flag, &help_flag },
55 { "version", 0, arg_flag, &version_flag }
58 static int num_args = sizeof(args) / sizeof(args[0]);
60 static void
61 server_usage(int code, struct getargs *args, int num_args)
63 arg_printusage(args, num_args, NULL, "");
64 exit(code);
67 static void
68 client_usage(int code, struct getargs *args, int num_args)
70 arg_printusage(args, num_args, NULL, "host");
71 exit(code);
75 static int
76 common_setup(krb5_context *context, int *argc, char **argv,
77 void (*usage)(int, struct getargs*, int))
79 int port = 0;
80 *argc = krb5_program_setup(context, *argc, argv, args, num_args, usage);
82 if(help_flag)
83 (*usage)(0, args, num_args);
84 if(version_flag) {
85 print_version(NULL);
86 exit(0);
89 if(port_str){
90 struct servent *s = roken_getservbyname(port_str, "tcp");
91 if(s)
92 port = s->s_port;
93 else {
94 char *ptr;
96 port = strtol (port_str, &ptr, 10);
97 if (port == 0 && ptr == port_str)
98 errx (1, "Bad port `%s'", port_str);
99 port = htons(port);
103 if (port == 0)
104 port = krb5_getportbyname (*context, PORT, "tcp", 4711);
106 return port;
110 server_setup(krb5_context *context, int argc, char **argv)
112 int port = common_setup(context, &argc, argv, server_usage);
113 krb5_error_code ret;
115 if(argv[argc] != NULL)
116 server_usage(1, args, num_args);
117 if (keytab_str != NULL)
118 ret = krb5_kt_resolve (*context, keytab_str, &keytab);
119 else
120 ret = krb5_kt_default (*context, &keytab);
121 if (ret)
122 krb5_err (*context, 1, ret, "krb5_kt_resolve/default");
123 return port;
127 client_setup(krb5_context *context, int *argc, char **argv)
129 int optind = *argc;
130 int port = common_setup(context, &optind, argv, client_usage);
131 if(*argc - optind != 1)
132 client_usage(1, args, num_args);
133 *argc = optind;
134 return port;
138 client_doit (const char *hostname, int port, const char *service,
139 int (*func)(int, const char *hostname, const char *service))
141 struct addrinfo *ai, *a;
142 struct addrinfo hints;
143 int error;
144 char portstr[NI_MAXSERV];
146 memset (&hints, 0, sizeof(hints));
147 hints.ai_socktype = SOCK_STREAM;
148 hints.ai_protocol = IPPROTO_TCP;
150 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
152 error = getaddrinfo (hostname, portstr, &hints, &ai);
153 if (error) {
154 errx (1, "%s: %s", hostname, gai_strerror(error));
155 return -1;
158 for (a = ai; a != NULL; a = a->ai_next) {
159 int s;
161 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
162 if (s < 0)
163 continue;
164 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
165 warn ("connect(%s)", hostname);
166 close (s);
167 continue;
169 freeaddrinfo (ai);
170 return (*func) (s, hostname, service);
172 warnx ("failed to contact %s", hostname);
173 freeaddrinfo (ai);
174 return 1;