2 * Copyright (c) 1995 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 /* Tiny program to help debug popper */
37 __RCSID("$Heimdal: pop_debug.c 10965 2002-05-02 16:27:16Z joda $"
50 if(select(s
+1, &fds
, 0, 0, 0) < 0)
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
));
62 fwrite(buf
, n
, 1, stdout
);
68 get_socket (const char *hostname
, int port
)
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
);
80 errx (1, "getaddrinfo %s: %s", hostname
, gai_strerror (ret
));
82 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
85 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
88 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
95 err (1, "failed to connect to %s", hostname
);
100 doit_v4 (char *host
, int port
)
105 des_key_schedule sched
;
107 int s
= get_socket (host
, port
);
109 ret
= krb_sendauth(0,
114 krb_realmofhost(host
),
123 warnx("krb_sendauth: %s", krb_get_err_text(ret
));
133 doit_v5 (char *host
, int port
)
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
);
143 errx (1, "krb5_init_context failed: %d", ret
);
145 ret
= krb5_sname_to_principal (context
,
151 warnx ("krb5_sname_to_principal: %s",
152 krb5_get_err_text (context
, ret
));
155 ret
= krb5_sendauth (context
,
169 warnx ("krb5_sendauth: %s",
170 krb5_get_err_text (context
, ret
));
180 static int use_v4
= -1;
183 static int use_v5
= -1;
185 static char *port_str
;
186 static int do_version
;
189 struct getargs args
[] = {
191 { "krb4", '4', arg_flag
, &use_v4
, "Use Kerberos V4",
195 { "krb5", '5', arg_flag
, &use_v5
, "Use Kerberos V5",
198 { "port", 'p', arg_string
, &port_str
, "Use this port",
199 "number-or-service" },
200 { "version", 0, arg_flag
, &do_version
, "Print version",
202 { "help", 0, arg_flag
, &do_help
, NULL
,
209 arg_printusage (args
,
210 sizeof(args
) / sizeof(args
[0]),
217 main(int argc
, char **argv
)
223 setprogname(argv
[0]);
225 if (getarg (args
, sizeof(args
) / sizeof(args
[0]), argc
, argv
,
236 print_version (NULL
);
244 struct servent
*s
= roken_getservbyname (port_str
, "tcp");
251 port
= strtol (port_str
, &ptr
, 10);
252 if (port
== 0 && ptr
== port_str
)
253 errx (1, "Bad port `%s'", port_str
);
259 port
= krb5_getportbyname (NULL
, "kpop", "tcp", 1109);
261 port
= k_getportbyname ("kpop", "tcp", 1109);
263 #error must define KRB4 or KRB5
267 #if defined(KRB4) && defined(KRB5)
268 if(use_v4
== -1 && use_v5
== 1)
270 if(use_v5
== -1 && use_v4
== 1)
276 ret
= doit_v5 (argv
[0], port
);
281 ret
= doit_v4 (argv
[0], port
);