Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / test / uu_client.c
blob29ca5e3d5861500c9f94e24a0c4d2c6e66f6be7d
1 /*
2 * Copyright (c) 1997 - 2000 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 #include "test_locl.h"
35 __RCSID("$Heimdal: uu_client.c 14719 2005-04-03 19:53:32Z lha $"
36 "$NetBSD$");
38 krb5_context context;
40 static int
41 proto (int sock, const char *hostname, const char *service)
43 struct sockaddr_in remote, local;
44 socklen_t addrlen;
45 krb5_address remote_addr, local_addr;
46 krb5_context context;
47 krb5_ccache ccache;
48 krb5_auth_context auth_context;
49 krb5_error_code status;
50 krb5_principal client;
51 krb5_data data;
52 krb5_data packet;
53 krb5_creds mcred, cred;
54 krb5_ticket *ticket;
56 addrlen = sizeof(local);
57 if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
58 || addrlen != sizeof(local))
59 err (1, "getsockname(%s)", hostname);
61 addrlen = sizeof(remote);
62 if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
63 || addrlen != sizeof(remote))
64 err (1, "getpeername(%s)", hostname);
66 status = krb5_init_context(&context);
67 if (status)
68 errx(1, "krb5_init_context failed: %d", status);
70 status = krb5_cc_default (context, &ccache);
71 if (status)
72 krb5_err(context, 1, status, "krb5_cc_default");
74 status = krb5_auth_con_init (context, &auth_context);
75 if (status)
76 krb5_err(context, 1, status, "krb5_auth_con_init");
78 local_addr.addr_type = AF_INET;
79 local_addr.address.length = sizeof(local.sin_addr);
80 local_addr.address.data = &local.sin_addr;
82 remote_addr.addr_type = AF_INET;
83 remote_addr.address.length = sizeof(remote.sin_addr);
84 remote_addr.address.data = &remote.sin_addr;
86 status = krb5_auth_con_setaddrs (context,
87 auth_context,
88 &local_addr,
89 &remote_addr);
90 if (status)
91 krb5_err(context, 1, status, "krb5_auth_con_setaddr");
93 krb5_cc_clear_mcred(&mcred);
95 status = krb5_cc_get_principal(context, ccache, &client);
96 if(status)
97 krb5_err(context, 1, status, "krb5_cc_get_principal");
98 status = krb5_make_principal(context, &mcred.server,
99 *krb5_princ_realm(context, client),
100 "krbtgt",
101 *krb5_princ_realm(context, client),
102 NULL);
103 if(status)
104 krb5_err(context, 1, status, "krb5_make_principal");
105 mcred.client = client;
107 status = krb5_cc_retrieve_cred(context, ccache, 0, &mcred, &cred);
108 if(status)
109 krb5_err(context, 1, status, "krb5_cc_retrieve_cred");
112 char *client_name;
113 krb5_data data;
114 status = krb5_unparse_name(context, cred.client, &client_name);
115 if(status)
116 krb5_err(context, 1, status, "krb5_unparse_name");
117 data.data = client_name;
118 data.length = strlen(client_name) + 1;
119 status = krb5_write_message(context, &sock, &data);
120 if(status)
121 krb5_err(context, 1, status, "krb5_write_message");
122 free(client_name);
125 status = krb5_write_message(context, &sock, &cred.ticket);
126 if(status)
127 krb5_err(context, 1, status, "krb5_write_message");
129 status = krb5_auth_con_setuserkey(context, auth_context, &cred.session);
130 if(status)
131 krb5_err(context, 1, status, "krb5_auth_con_setuserkey");
133 status = krb5_recvauth(context, &auth_context, &sock,
134 VERSION, client, 0, NULL, &ticket);
136 if (status)
137 krb5_err(context, 1, status, "krb5_recvauth");
139 if (ticket->ticket.authorization_data) {
140 AuthorizationData *authz;
141 int i;
143 printf("Authorization data:\n");
145 authz = ticket->ticket.authorization_data;
146 for (i = 0; i < authz->len; i++) {
147 printf("\ttype %d, length %lu\n",
148 authz->val[i].ad_type,
149 (unsigned long)authz->val[i].ad_data.length);
153 data.data = "hej";
154 data.length = 3;
156 krb5_data_zero (&packet);
158 status = krb5_mk_safe (context,
159 auth_context,
160 &data,
161 &packet,
162 NULL);
163 if (status)
164 krb5_err(context, 1, status, "krb5_mk_safe");
166 status = krb5_write_message(context, &sock, &packet);
167 if(status)
168 krb5_err(context, 1, status, "krb5_write_message");
170 data.data = "hemligt";
171 data.length = 7;
173 krb5_data_free (&packet);
175 status = krb5_mk_priv (context,
176 auth_context,
177 &data,
178 &packet,
179 NULL);
180 if (status)
181 krb5_err(context, 1, status, "krb5_mk_priv");
183 status = krb5_write_message(context, &sock, &packet);
184 if(status)
185 krb5_err(context, 1, status, "krb5_write_message");
186 return 0;
190 main(int argc, char **argv)
192 int port = client_setup(&context, &argc, argv);
193 return client_doit (argv[argc], port, service, proto);