2 * Copyright (c) 1997 - 2000, 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
35 __RCSID("$Heimdal: kf.c 11400 2002-09-05 15:00:03Z joda $"
40 static int version_flag
;
41 static char *port_str
;
42 const char *service
= KF_SERVICE
;
43 const char *remote_name
= NULL
;
45 const char *ccache_name
= NULL
;
47 static struct getargs args
[] = {
48 { "port", 'p', arg_string
, &port_str
, "port to connect to", "port" },
49 { "login", 'l',arg_string
, &remote_name
,"remote login name","login"},
50 { "ccache", 'c',arg_string
, &ccache_name
, "remote cred cache","ccache"},
51 { "forwardable",'F',arg_flag
,&forwardable
,
52 "Forward forwardable credentials", NULL
},
53 { "forwardable",'G',arg_negative_flag
,&forwardable
,
54 "Don't forward forwardable credentials", NULL
},
55 { "help", 'h', arg_flag
, &help_flag
},
56 { "version", 0, arg_flag
, &version_flag
}
59 static int num_args
= sizeof(args
) / sizeof(args
[0]);
62 usage(int code
, struct getargs
*args
, int num_args
)
64 arg_printusage(args
, num_args
, NULL
, "hosts");
69 client_setup(krb5_context
*context
, int *argc
, char **argv
)
75 setprogname (argv
[0]);
77 status
= krb5_init_context (context
);
79 errx(1, "krb5_init_context failed: %d", status
);
81 forwardable
= krb5_config_get_bool (*context
, NULL
,
86 if (getarg (args
, num_args
, *argc
, argv
, &optind
))
87 usage(1, args
, num_args
);
90 usage (0, args
, num_args
);
97 struct servent
*s
= roken_getservbyname(port_str
, "tcp");
103 port
= strtol (port_str
, &ptr
, 10);
104 if (port
== 0 && ptr
== port_str
)
105 errx (1, "Bad port `%s'", port_str
);
111 port
= krb5_getportbyname (*context
, KF_PORT_NAME
, "tcp", KF_PORT_NUM
);
113 if(*argc
- optind
< 1)
114 usage(1, args
, num_args
);
121 * forward creds to `hostname'/`service' over `sock'
126 proto (int sock
, const char *hostname
, const char *service
,
127 char *message
, size_t len
)
129 krb5_auth_context auth_context
;
130 krb5_error_code status
;
131 krb5_principal server
;
137 krb5_kdc_flags flags
;
138 krb5_principal principal
;
140 status
= krb5_auth_con_init (context
, &auth_context
);
142 krb5_warn (context
, status
, "krb5_auth_con_init");
146 status
= krb5_auth_con_setaddrs_from_fd (context
,
150 krb5_warn (context
, status
, "krb5_auth_con_setaddr");
154 status
= krb5_sname_to_principal (context
,
160 krb5_warn (context
, status
, "krb5_sname_to_principal");
164 status
= krb5_sendauth (context
,
170 AP_OPTS_MUTUAL_REQUIRED
| AP_OPTS_USE_SUBKEY
,
178 krb5_warn(context
, status
, "krb5_sendauth");
182 if (ccache_name
== NULL
)
185 data_send
.data
= (void *)remote_name
;
186 data_send
.length
= strlen(remote_name
) + 1;
187 status
= krb5_write_priv_message(context
, auth_context
, &sock
, &data_send
);
189 krb5_warn (context
, status
, "krb5_write_message");
192 data_send
.data
= (void *)ccache_name
;
193 data_send
.length
= strlen(ccache_name
)+1;
194 status
= krb5_write_priv_message(context
, auth_context
, &sock
, &data_send
);
196 krb5_warn (context
, status
, "krb5_write_message");
200 memset (&creds
, 0, sizeof(creds
));
202 status
= krb5_cc_default (context
, &ccache
);
204 krb5_warn (context
, status
, "krb5_cc_default");
208 status
= krb5_cc_get_principal (context
, ccache
, &principal
);
210 krb5_warn (context
, status
, "krb5_cc_get_principal");
214 creds
.client
= principal
;
216 status
= krb5_make_principal (context
,
224 krb5_warn (context
, status
, "krb5_make_principal");
228 creds
.times
.endtime
= 0;
231 flags
.b
.forwarded
= 1;
232 flags
.b
.forwardable
= forwardable
;
234 status
= krb5_get_forwarded_creds (context
,
242 krb5_warn (context
, status
, "krb5_get_forwarded_creds");
246 status
= krb5_write_priv_message(context
, auth_context
, &sock
, &data
);
249 krb5_warn (context
, status
, "krb5_mk_priv");
253 krb5_data_free (&data
);
255 status
= krb5_read_priv_message(context
, auth_context
, &sock
, &data
);
257 krb5_warn (context
, status
, "krb5_mk_priv");
260 if(data
.length
>= len
) {
261 krb5_warnx (context
, "returned string is too long, truncating");
262 memcpy(message
, data
.data
, len
);
263 message
[len
- 1] = '\0';
265 memcpy(message
, data
.data
, data
.length
);
266 message
[data
.length
] = '\0';
268 krb5_data_free (&data
);
270 return(strcmp(message
, "ok"));
274 doit (const char *hostname
, int port
, const char *service
,
275 char *message
, size_t len
)
277 struct addrinfo
*ai
, *a
;
278 struct addrinfo hints
;
280 char portstr
[NI_MAXSERV
];
282 memset (&hints
, 0, sizeof(hints
));
283 hints
.ai_socktype
= SOCK_STREAM
;
284 hints
.ai_protocol
= IPPROTO_TCP
;
286 snprintf (portstr
, sizeof(portstr
), "%u", ntohs(port
));
288 error
= getaddrinfo (hostname
, portstr
, &hints
, &ai
);
290 errx (1, "getaddrinfo(%s): %s", hostname
, gai_strerror(error
));
293 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
296 s
= socket (a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
299 if (connect (s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
300 warn ("connect(%s)", hostname
);
305 return proto (s
, hostname
, service
, message
, len
);
307 warnx ("failed to contact %s", hostname
);
313 main(int argc
, char **argv
)
319 port
= client_setup(&context
, &argcc
, argv
);
321 if (remote_name
== NULL
) {
322 remote_name
= get_default_username ();
323 if (remote_name
== NULL
)
324 errx (1, "who are you?");
327 for (i
= argcc
;i
< argc
; i
++) {
329 ret
= doit (argv
[i
], port
, service
, message
, sizeof(message
));
331 warnx ("%s: ok", argv
[i
]);
333 warnx ("%s: failed: %s", argv
[i
], message
);