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