No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / popper / pop_debug.c
blobf3c5b22828591c5fe472f7c99d24c53f95f0ada1
1 /*
2 * Copyright (c) 1995 - 2002 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.
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 /* Tiny program to help debug popper */
36 #include "popper.h"
37 __RCSID("$Heimdal: pop_debug.c 10965 2002-05-02 16:27:16Z joda $"
38 "$NetBSD$");
40 static void
41 loop(int s)
43 char cmd[1024];
44 char buf[1024];
45 fd_set fds;
46 while(1){
47 FD_ZERO(&fds);
48 FD_SET(0, &fds);
49 FD_SET(s, &fds);
50 if(select(s+1, &fds, 0, 0, 0) < 0)
51 err(1, "select");
52 if(FD_ISSET(0, &fds)){
53 fgets(cmd, sizeof(cmd), stdin);
54 cmd[strlen(cmd) - 1] = '\0';
55 strlcat (cmd, "\r\n", sizeof(cmd));
56 write(s, cmd, strlen(cmd));
58 if(FD_ISSET(s, &fds)){
59 int n = read(s, buf, sizeof(buf));
60 if(n == 0)
61 exit(0);
62 fwrite(buf, n, 1, stdout);
67 static int
68 get_socket (const char *hostname, int port)
70 int ret;
71 struct addrinfo *ai, *a;
72 struct addrinfo hints;
73 char portstr[NI_MAXSERV];
75 memset (&hints, 0, sizeof(hints));
76 hints.ai_socktype = SOCK_STREAM;
77 snprintf (portstr, sizeof(portstr), "%d", ntohs(port));
78 ret = getaddrinfo (hostname, portstr, &hints, &ai);
79 if (ret)
80 errx (1, "getaddrinfo %s: %s", hostname, gai_strerror (ret));
82 for (a = ai; a != NULL; a = a->ai_next) {
83 int s;
85 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
86 if (s < 0)
87 continue;
88 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
89 close (s);
90 continue;
92 freeaddrinfo (ai);
93 return s;
95 err (1, "failed to connect to %s", hostname);
98 #ifdef KRB4
99 static int
100 doit_v4 (char *host, int port)
102 KTEXT_ST ticket;
103 MSG_DAT msg_data;
104 CREDENTIALS cred;
105 des_key_schedule sched;
106 int ret;
107 int s = get_socket (host, port);
109 ret = krb_sendauth(0,
111 &ticket,
112 "pop",
113 host,
114 krb_realmofhost(host),
115 getpid(),
116 &msg_data,
117 &cred,
118 sched,
119 NULL,
120 NULL,
121 "KPOPV0.1");
122 if(ret) {
123 warnx("krb_sendauth: %s", krb_get_err_text(ret));
124 return 1;
126 loop(s);
127 return 0;
129 #endif
131 #ifdef KRB5
132 static int
133 doit_v5 (char *host, int port)
135 krb5_error_code ret;
136 krb5_context context;
137 krb5_auth_context auth_context = NULL;
138 krb5_principal server;
139 int s = get_socket (host, port);
141 ret = krb5_init_context (&context);
142 if (ret)
143 errx (1, "krb5_init_context failed: %d", ret);
145 ret = krb5_sname_to_principal (context,
146 host,
147 "pop",
148 KRB5_NT_SRV_HST,
149 &server);
150 if (ret) {
151 warnx ("krb5_sname_to_principal: %s",
152 krb5_get_err_text (context, ret));
153 return 1;
155 ret = krb5_sendauth (context,
156 &auth_context,
158 "KPOPV1.0",
159 NULL,
160 server,
162 NULL,
163 NULL,
164 NULL,
165 NULL,
166 NULL,
167 NULL);
168 if (ret) {
169 warnx ("krb5_sendauth: %s",
170 krb5_get_err_text (context, ret));
171 return 1;
173 loop (s);
174 return 0;
176 #endif
179 #ifdef KRB4
180 static int use_v4 = -1;
181 #endif
182 #ifdef KRB5
183 static int use_v5 = -1;
184 #endif
185 static char *port_str;
186 static int do_version;
187 static int do_help;
189 struct getargs args[] = {
190 #ifdef KRB4
191 { "krb4", '4', arg_flag, &use_v4, "Use Kerberos V4",
192 NULL },
193 #endif
194 #ifdef KRB5
195 { "krb5", '5', arg_flag, &use_v5, "Use Kerberos V5",
196 NULL },
197 #endif
198 { "port", 'p', arg_string, &port_str, "Use this port",
199 "number-or-service" },
200 { "version", 0, arg_flag, &do_version, "Print version",
201 NULL },
202 { "help", 0, arg_flag, &do_help, NULL,
203 NULL }
206 static void
207 usage (int ret)
209 arg_printusage (args,
210 sizeof(args) / sizeof(args[0]),
211 NULL,
212 "hostname");
213 exit (ret);
217 main(int argc, char **argv)
219 int port = 0;
220 int ret = 1;
221 int optind = 0;
223 setprogname(argv[0]);
225 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
226 &optind))
227 usage (1);
229 argc -= optind;
230 argv += optind;
232 if (do_help)
233 usage (0);
235 if (do_version) {
236 print_version (NULL);
237 return 0;
240 if (argc < 1)
241 usage (1);
243 if (port_str) {
244 struct servent *s = roken_getservbyname (port_str, "tcp");
246 if (s)
247 port = s->s_port;
248 else {
249 char *ptr;
251 port = strtol (port_str, &ptr, 10);
252 if (port == 0 && ptr == port_str)
253 errx (1, "Bad port `%s'", port_str);
254 port = htons(port);
257 if (port == 0) {
258 #ifdef KRB5
259 port = krb5_getportbyname (NULL, "kpop", "tcp", 1109);
260 #elif defined(KRB4)
261 port = k_getportbyname ("kpop", "tcp", 1109);
262 #else
263 #error must define KRB4 or KRB5
264 #endif
267 #if defined(KRB4) && defined(KRB5)
268 if(use_v4 == -1 && use_v5 == 1)
269 use_v4 = 0;
270 if(use_v5 == -1 && use_v4 == 1)
271 use_v5 = 0;
272 #endif
274 #ifdef KRB5
275 if (ret && use_v5) {
276 ret = doit_v5 (argv[0], port);
278 #endif
279 #ifdef KRB4
280 if (ret && use_v4) {
281 ret = doit_v4 (argv[0], port);
283 #endif
284 return ret;