xref: /freebsd/crypto/heimdal/appl/test/uu_client.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 /*
2  * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33 
34 #include "test_locl.h"
35 RCSID("$Id: uu_client.c,v 1.7 2000/12/31 07:41:39 assar Exp $");
36 
37 krb5_context context;
38 
39 static int
40 proto (int sock, const char *hostname, const char *service)
41 {
42     struct sockaddr_in remote, local;
43     socklen_t addrlen;
44     krb5_address remote_addr, local_addr;
45     krb5_context context;
46     krb5_ccache ccache;
47     krb5_auth_context auth_context;
48     krb5_error_code status;
49     krb5_principal client;
50     krb5_data data;
51     krb5_data packet;
52     krb5_creds mcred, cred;
53 
54     addrlen = sizeof(local);
55     if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
56 	|| addrlen != sizeof(local))
57 	err (1, "getsockname(%s)", hostname);
58 
59     addrlen = sizeof(remote);
60     if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
61 	|| addrlen != sizeof(remote))
62 	err (1, "getpeername(%s)", hostname);
63 
64     status = krb5_init_context(&context);
65     if (status)
66 	errx(1, "krb5_init_context failed: %d", status);
67 
68     status = krb5_cc_default (context, &ccache);
69     if (status)
70 	krb5_err(context, 1, status, "krb5_cc_default");
71 
72     status = krb5_auth_con_init (context, &auth_context);
73     if (status)
74 	krb5_err(context, 1, status, "krb5_auth_con_init");
75 
76     local_addr.addr_type = AF_INET;
77     local_addr.address.length = sizeof(local.sin_addr);
78     local_addr.address.data   = &local.sin_addr;
79 
80     remote_addr.addr_type = AF_INET;
81     remote_addr.address.length = sizeof(remote.sin_addr);
82     remote_addr.address.data   = &remote.sin_addr;
83 
84     status = krb5_auth_con_setaddrs (context,
85 				     auth_context,
86 				     &local_addr,
87 				     &remote_addr);
88     if (status)
89 	krb5_err(context, 1, status, "krb5_auth_con_setaddr");
90 
91     status = krb5_cc_get_principal(context, ccache, &client);
92     if(status)
93 	krb5_err(context, 1, status, "krb5_cc_get_principal");
94     status = krb5_make_principal(context, &mcred.server,
95 				 *krb5_princ_realm(context, client),
96 				 "krbtgt",
97 				 *krb5_princ_realm(context, client),
98 				 NULL);
99     if(status)
100 	krb5_err(context, 1, status, "krb5_make_principal");
101 
102     status = krb5_cc_retrieve_cred(context, ccache, 0, &mcred, &cred);
103     if(status)
104 	krb5_err(context, 1, status, "krb5_cc_retrieve_cred");
105 
106     {
107 	char *client_name;
108 	krb5_data data;
109 	status = krb5_unparse_name(context, cred.client, &client_name);
110 	if(status)
111 	    krb5_err(context, 1, status, "krb5_unparse_name");
112 	data.data = client_name;
113 	data.length = strlen(client_name) + 1;
114 	status = krb5_write_message(context, &sock, &data);
115 	if(status)
116 	    krb5_err(context, 1, status, "krb5_write_message");
117 	free(client_name);
118     }
119 
120     status = krb5_write_message(context, &sock, &cred.ticket);
121     if(status)
122 	krb5_err(context, 1, status, "krb5_write_message");
123 
124     status = krb5_auth_con_setuserkey(context, auth_context, &cred.session);
125     if(status)
126 	krb5_err(context, 1, status, "krb5_auth_con_setuserkey");
127 
128     status = krb5_recvauth(context, &auth_context, &sock,
129 			   VERSION, client, 0, NULL, NULL);
130 
131     if (status)
132 	krb5_err(context, 1, status, "krb5_recvauth");
133 
134     data.data   = "hej";
135     data.length = 3;
136 
137     krb5_data_zero (&packet);
138 
139     status = krb5_mk_safe (context,
140 			   auth_context,
141 			   &data,
142 			   &packet,
143 			   NULL);
144     if (status)
145 	krb5_err(context, 1, status, "krb5_mk_safe");
146 
147     status = krb5_write_message(context, &sock, &packet);
148     if(status)
149 	krb5_err(context, 1, status, "krb5_write_message");
150 
151     data.data   = "hemligt";
152     data.length = 7;
153 
154     krb5_data_free (&packet);
155 
156     status = krb5_mk_priv (context,
157 			   auth_context,
158 			   &data,
159 			   &packet,
160 			   NULL);
161     if (status)
162 	krb5_err(context, 1, status, "krb5_mk_priv");
163 
164     status = krb5_write_message(context, &sock, &packet);
165     if(status)
166 	krb5_err(context, 1, status, "krb5_write_message");
167     return 0;
168 }
169 
170 int
171 main(int argc, char **argv)
172 {
173     int port = client_setup(&context, &argc, argv);
174     return client_doit (argv[argc], port, service, proto);
175 }
176