s4:heimdal A real fix for bug 6801
[heimdal.git] / appl / popper / pop_debug.c
blobbca03e66e79a54d6ee2eacf3be5d9033d8c8b3a4
1 /*
2 * Copyright (c) 1995 - 2002 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 /* Tiny program to help debug popper */
36 #include "popper.h"
37 RCSID("$Id$");
39 static void
40 loop(int s)
42 char cmd[1024];
43 char buf[1024];
44 fd_set fds;
45 while(1){
46 FD_ZERO(&fds);
47 FD_SET(0, &fds);
48 FD_SET(s, &fds);
49 if(select(s+1, &fds, 0, 0, 0) < 0)
50 err(1, "select");
51 if(FD_ISSET(0, &fds)){
52 fgets(cmd, sizeof(cmd), stdin);
53 cmd[strlen(cmd) - 1] = '\0';
54 strlcat (cmd, "\r\n", sizeof(cmd));
55 write(s, cmd, strlen(cmd));
57 if(FD_ISSET(s, &fds)){
58 int n = read(s, buf, sizeof(buf));
59 if(n == 0)
60 exit(0);
61 fwrite(buf, n, 1, stdout);
66 static int
67 get_socket (const char *hostname, int port)
69 int ret;
70 struct addrinfo *ai, *a;
71 struct addrinfo hints;
72 char portstr[NI_MAXSERV];
74 memset (&hints, 0, sizeof(hints));
75 hints.ai_socktype = SOCK_STREAM;
76 snprintf (portstr, sizeof(portstr), "%d", ntohs(port));
77 ret = getaddrinfo (hostname, portstr, &hints, &ai);
78 if (ret)
79 errx (1, "getaddrinfo %s: %s", hostname, gai_strerror (ret));
81 for (a = ai; a != NULL; a = a->ai_next) {
82 int s;
84 s = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
85 if (s < 0)
86 continue;
87 if (connect (s, a->ai_addr, a->ai_addrlen) < 0) {
88 close (s);
89 continue;
91 freeaddrinfo (ai);
92 return s;
94 err (1, "failed to connect to %s", hostname);
97 #ifdef KRB5
98 static int
99 doit_v5 (char *host, int port)
101 krb5_error_code ret;
102 krb5_context context;
103 krb5_auth_context auth_context = NULL;
104 krb5_principal server;
105 int s = get_socket (host, port);
107 ret = krb5_init_context (&context);
108 if (ret)
109 errx (1, "krb5_init_context failed: %d", ret);
111 ret = krb5_sname_to_principal (context,
112 host,
113 "pop",
114 KRB5_NT_SRV_HST,
115 &server);
116 if (ret) {
117 warnx ("krb5_sname_to_principal: %s",
118 krb5_get_err_text (context, ret));
119 return 1;
121 ret = krb5_sendauth (context,
122 &auth_context,
124 "KPOPV1.0",
125 NULL,
126 server,
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL);
134 if (ret) {
135 warnx ("krb5_sendauth: %s",
136 krb5_get_err_text (context, ret));
137 return 1;
139 loop (s);
140 return 0;
142 #endif
145 #ifdef KRB5
146 static int use_v5 = -1;
147 #endif
148 static char *port_str;
149 static int do_version;
150 static int do_help;
152 struct getargs args[] = {
153 #ifdef KRB5
154 { "krb5", '5', arg_flag, &use_v5, "Use Kerberos V5",
155 NULL },
156 #endif
157 { "port", 'p', arg_string, &port_str, "Use this port",
158 "number-or-service" },
159 { "version", 0, arg_flag, &do_version, "Print version",
160 NULL },
161 { "help", 0, arg_flag, &do_help, NULL,
162 NULL }
165 static void
166 usage (int ret)
168 arg_printusage (args,
169 sizeof(args) / sizeof(args[0]),
170 NULL,
171 "hostname");
172 exit (ret);
176 main(int argc, char **argv)
178 int port = 0;
179 int ret = 1;
180 int optind = 0;
182 setprogname(argv[0]);
184 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
185 &optind))
186 usage (1);
188 argc -= optind;
189 argv += optind;
191 if (do_help)
192 usage (0);
194 if (do_version) {
195 print_version (NULL);
196 return 0;
199 if (argc < 1)
200 usage (1);
202 if (port_str) {
203 struct servent *s = roken_getservbyname (port_str, "tcp");
205 if (s)
206 port = s->s_port;
207 else {
208 char *ptr;
210 port = strtol (port_str, &ptr, 10);
211 if (port == 0 && ptr == port_str)
212 errx (1, "Bad port `%s'", port_str);
213 port = htons(port);
216 if (port == 0) {
217 #ifdef KRB5
218 port = krb5_getportbyname (NULL, "kpop", "tcp", 1109);
219 #else
220 #error must define KRB5
221 #endif
224 #ifdef KRB5
225 if (ret && use_v5) {
226 ret = doit_v5 (argv[0], port);
228 #endif
229 return ret;