1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1997 - 2000 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include "test_locl.h"
35*ae771770SStanislav Sedov RCSID("$Id$");
36b528cefcSMark Murray
37b528cefcSMark Murray krb5_context context;
38b528cefcSMark Murray
39b528cefcSMark Murray static int
proto(int sock,const char * hostname,const char * service)40b528cefcSMark Murray proto (int sock, const char *hostname, const char *service)
41b528cefcSMark Murray {
42b528cefcSMark Murray struct sockaddr_in remote, local;
435e9cd1aeSAssar Westerlund socklen_t addrlen;
44b528cefcSMark Murray krb5_address remote_addr, local_addr;
45b528cefcSMark Murray krb5_context context;
46b528cefcSMark Murray krb5_ccache ccache;
47b528cefcSMark Murray krb5_auth_context auth_context;
48b528cefcSMark Murray krb5_error_code status;
49b528cefcSMark Murray krb5_principal client;
50b528cefcSMark Murray krb5_data data;
51b528cefcSMark Murray krb5_data packet;
52b528cefcSMark Murray krb5_creds mcred, cred;
53c19800e8SDoug Rabson krb5_ticket *ticket;
54b528cefcSMark Murray
55b528cefcSMark Murray addrlen = sizeof(local);
56b528cefcSMark Murray if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
57b528cefcSMark Murray || addrlen != sizeof(local))
58b528cefcSMark Murray err (1, "getsockname(%s)", hostname);
59b528cefcSMark Murray
60b528cefcSMark Murray addrlen = sizeof(remote);
61b528cefcSMark Murray if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
62b528cefcSMark Murray || addrlen != sizeof(remote))
63b528cefcSMark Murray err (1, "getpeername(%s)", hostname);
64b528cefcSMark Murray
65b528cefcSMark Murray status = krb5_init_context(&context);
66b528cefcSMark Murray if (status)
675e9cd1aeSAssar Westerlund errx(1, "krb5_init_context failed: %d", status);
68b528cefcSMark Murray
69b528cefcSMark Murray status = krb5_cc_default (context, &ccache);
70b528cefcSMark Murray if (status)
71b528cefcSMark Murray krb5_err(context, 1, status, "krb5_cc_default");
72b528cefcSMark Murray
73b528cefcSMark Murray status = krb5_auth_con_init (context, &auth_context);
74b528cefcSMark Murray if (status)
75b528cefcSMark Murray krb5_err(context, 1, status, "krb5_auth_con_init");
76b528cefcSMark Murray
77b528cefcSMark Murray local_addr.addr_type = AF_INET;
78b528cefcSMark Murray local_addr.address.length = sizeof(local.sin_addr);
79b528cefcSMark Murray local_addr.address.data = &local.sin_addr;
80b528cefcSMark Murray
81b528cefcSMark Murray remote_addr.addr_type = AF_INET;
82b528cefcSMark Murray remote_addr.address.length = sizeof(remote.sin_addr);
83b528cefcSMark Murray remote_addr.address.data = &remote.sin_addr;
84b528cefcSMark Murray
85b528cefcSMark Murray status = krb5_auth_con_setaddrs (context,
86b528cefcSMark Murray auth_context,
87b528cefcSMark Murray &local_addr,
88b528cefcSMark Murray &remote_addr);
89b528cefcSMark Murray if (status)
90b528cefcSMark Murray krb5_err(context, 1, status, "krb5_auth_con_setaddr");
91b528cefcSMark Murray
92c19800e8SDoug Rabson krb5_cc_clear_mcred(&mcred);
93c19800e8SDoug Rabson
94b528cefcSMark Murray status = krb5_cc_get_principal(context, ccache, &client);
95b528cefcSMark Murray if(status)
96b528cefcSMark Murray krb5_err(context, 1, status, "krb5_cc_get_principal");
97b528cefcSMark Murray status = krb5_make_principal(context, &mcred.server,
98*ae771770SStanislav Sedov krb5_principal_get_realm(context, client),
99b528cefcSMark Murray "krbtgt",
100*ae771770SStanislav Sedov krb5_principal_get_realm(context, client),
101b528cefcSMark Murray NULL);
102b528cefcSMark Murray if(status)
103b528cefcSMark Murray krb5_err(context, 1, status, "krb5_make_principal");
104c19800e8SDoug Rabson mcred.client = client;
105b528cefcSMark Murray
106b528cefcSMark Murray status = krb5_cc_retrieve_cred(context, ccache, 0, &mcred, &cred);
107b528cefcSMark Murray if(status)
108b528cefcSMark Murray krb5_err(context, 1, status, "krb5_cc_retrieve_cred");
109b528cefcSMark Murray
110b528cefcSMark Murray {
111b528cefcSMark Murray char *client_name;
112b528cefcSMark Murray krb5_data data;
113b528cefcSMark Murray status = krb5_unparse_name(context, cred.client, &client_name);
114b528cefcSMark Murray if(status)
115b528cefcSMark Murray krb5_err(context, 1, status, "krb5_unparse_name");
116b528cefcSMark Murray data.data = client_name;
117b528cefcSMark Murray data.length = strlen(client_name) + 1;
118b528cefcSMark Murray status = krb5_write_message(context, &sock, &data);
119b528cefcSMark Murray if(status)
120b528cefcSMark Murray krb5_err(context, 1, status, "krb5_write_message");
121b528cefcSMark Murray free(client_name);
122b528cefcSMark Murray }
123b528cefcSMark Murray
124b528cefcSMark Murray status = krb5_write_message(context, &sock, &cred.ticket);
125b528cefcSMark Murray if(status)
126b528cefcSMark Murray krb5_err(context, 1, status, "krb5_write_message");
127b528cefcSMark Murray
128b528cefcSMark Murray status = krb5_auth_con_setuserkey(context, auth_context, &cred.session);
129b528cefcSMark Murray if(status)
130b528cefcSMark Murray krb5_err(context, 1, status, "krb5_auth_con_setuserkey");
131b528cefcSMark Murray
132b528cefcSMark Murray status = krb5_recvauth(context, &auth_context, &sock,
133c19800e8SDoug Rabson VERSION, client, 0, NULL, &ticket);
134b528cefcSMark Murray
135b528cefcSMark Murray if (status)
136b528cefcSMark Murray krb5_err(context, 1, status, "krb5_recvauth");
137b528cefcSMark Murray
138c19800e8SDoug Rabson if (ticket->ticket.authorization_data) {
139c19800e8SDoug Rabson AuthorizationData *authz;
140c19800e8SDoug Rabson int i;
141c19800e8SDoug Rabson
142c19800e8SDoug Rabson printf("Authorization data:\n");
143c19800e8SDoug Rabson
144c19800e8SDoug Rabson authz = ticket->ticket.authorization_data;
145c19800e8SDoug Rabson for (i = 0; i < authz->len; i++) {
146c19800e8SDoug Rabson printf("\ttype %d, length %lu\n",
147c19800e8SDoug Rabson authz->val[i].ad_type,
148c19800e8SDoug Rabson (unsigned long)authz->val[i].ad_data.length);
149c19800e8SDoug Rabson }
150c19800e8SDoug Rabson }
151c19800e8SDoug Rabson
152b528cefcSMark Murray data.data = "hej";
153b528cefcSMark Murray data.length = 3;
154b528cefcSMark Murray
155b528cefcSMark Murray krb5_data_zero (&packet);
156b528cefcSMark Murray
157b528cefcSMark Murray status = krb5_mk_safe (context,
158b528cefcSMark Murray auth_context,
159b528cefcSMark Murray &data,
160b528cefcSMark Murray &packet,
161b528cefcSMark Murray NULL);
162b528cefcSMark Murray if (status)
163b528cefcSMark Murray krb5_err(context, 1, status, "krb5_mk_safe");
164b528cefcSMark Murray
165b528cefcSMark Murray status = krb5_write_message(context, &sock, &packet);
166b528cefcSMark Murray if(status)
167b528cefcSMark Murray krb5_err(context, 1, status, "krb5_write_message");
168b528cefcSMark Murray
169b528cefcSMark Murray data.data = "hemligt";
170b528cefcSMark Murray data.length = 7;
171b528cefcSMark Murray
172b528cefcSMark Murray krb5_data_free (&packet);
173b528cefcSMark Murray
174b528cefcSMark Murray status = krb5_mk_priv (context,
175b528cefcSMark Murray auth_context,
176b528cefcSMark Murray &data,
177b528cefcSMark Murray &packet,
178b528cefcSMark Murray NULL);
179b528cefcSMark Murray if (status)
180b528cefcSMark Murray krb5_err(context, 1, status, "krb5_mk_priv");
181b528cefcSMark Murray
182b528cefcSMark Murray status = krb5_write_message(context, &sock, &packet);
183b528cefcSMark Murray if(status)
184b528cefcSMark Murray krb5_err(context, 1, status, "krb5_write_message");
185b528cefcSMark Murray return 0;
186b528cefcSMark Murray }
187b528cefcSMark Murray
188b528cefcSMark Murray int
main(int argc,char ** argv)189b528cefcSMark Murray main(int argc, char **argv)
190b528cefcSMark Murray {
191b528cefcSMark Murray int port = client_setup(&context, &argc, argv);
192b528cefcSMark Murray return client_doit (argv[argc], port, service, proto);
193b528cefcSMark Murray }
194