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