sendauth.c revision 103423
1139823Simp/*
2122219Sharti * Copyright (c) 1997 - 2001 Kungliga Tekniska H�gskolan
3122219Sharti * (Royal Institute of Technology, Stockholm, Sweden).
4122219Sharti * All rights reserved.
5122219Sharti *
6122219Sharti * Redistribution and use in source and binary forms, with or without
7122219Sharti * modification, are permitted provided that the following conditions
8122219Sharti * are met:
9122219Sharti *
10122219Sharti * 1. Redistributions of source code must retain the above copyright
11122219Sharti *    notice, this list of conditions and the following disclaimer.
12122219Sharti *
13122219Sharti * 2. Redistributions in binary form must reproduce the above copyright
14122219Sharti *    notice, this list of conditions and the following disclaimer in the
15122219Sharti *    documentation and/or other materials provided with the distribution.
16122219Sharti *
17122219Sharti * 3. Neither the name of the Institute nor the names of its contributors
18122219Sharti *    may be used to endorse or promote products derived from this software
19122219Sharti *    without specific prior written permission.
20122219Sharti *
21122219Sharti * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22122219Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23122219Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24122219Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25122219Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26122219Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27122219Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28122219Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29122219Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30122219Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31122219Sharti * SUCH DAMAGE.
32122219Sharti */
33122219Sharti
34122219Sharti#include "krb5_locl.h"
35122219Sharti
36122219ShartiRCSID("$Id: sendauth.c,v 1.19 2002/09/04 21:34:43 joda Exp $");
37122219Sharti
38122219Sharti/*
39122219Sharti * The format seems to be:
40122219Sharti * client -> server
41122219Sharti *
42122219Sharti * 4 bytes - length
43122219Sharti * KRB5_SENDAUTH_V1.0 (including zero)
44122219Sharti * 4 bytes - length
45122219Sharti * protocol string (with terminating zero)
46122219Sharti *
47122219Sharti * server -> client
48122219Sharti * 1 byte - (0 = OK, else some kind of error)
49122219Sharti *
50122219Sharti * client -> server
51122219Sharti * 4 bytes - length
52122219Sharti * AP-REQ
53122219Sharti *
54122219Sharti * server -> client
55122219Sharti * 4 bytes - length (0 = OK, else length of error)
56122219Sharti * (error)
57122219Sharti *
58122219Sharti * if(mutual) {
59122219Sharti * server -> client
60122219Sharti * 4 bytes - length
61122219Sharti * AP-REP
62122219Sharti * }
63122219Sharti */
64122219Sharti
65122219Shartikrb5_error_code
66122219Shartikrb5_sendauth(krb5_context context,
67122219Sharti	      krb5_auth_context *auth_context,
68122219Sharti	      krb5_pointer p_fd,
69122219Sharti	      const char *appl_version,
70122219Sharti	      krb5_principal client,
71122219Sharti	      krb5_principal server,
72122219Sharti	      krb5_flags ap_req_options,
73122219Sharti	      krb5_data *in_data,
74122219Sharti	      krb5_creds *in_creds,
75122219Sharti	      krb5_ccache ccache,
76122219Sharti	      krb5_error **ret_error,
77122219Sharti	      krb5_ap_rep_enc_part **rep_result,
78122219Sharti	      krb5_creds **out_creds)
79122219Sharti{
80122219Sharti    krb5_error_code ret;
81122219Sharti    u_int32_t len, net_len;
82122219Sharti    const char *version = KRB5_SENDAUTH_VERSION;
83122219Sharti    u_char repl;
84122219Sharti    krb5_data ap_req, error_data;
85122219Sharti    krb5_creds this_cred;
86122219Sharti    krb5_principal this_client = NULL;
87122219Sharti    krb5_creds *creds;
88122219Sharti    ssize_t sret;
89122219Sharti    krb5_boolean my_ccache = FALSE;
90122219Sharti
91122219Sharti    len = strlen(version) + 1;
92122219Sharti    net_len = htonl(len);
93122219Sharti    if (krb5_net_write (context, p_fd, &net_len, 4) != 4
94122219Sharti	|| krb5_net_write (context, p_fd, version, len) != len) {
95122219Sharti	ret = errno;
96122219Sharti	krb5_set_error_string (context, "write: %s", strerror(ret));
97122219Sharti	return ret;
98122219Sharti    }
99122219Sharti
100122219Sharti    len = strlen(appl_version) + 1;
101122219Sharti    net_len = htonl(len);
102122219Sharti    if (krb5_net_write (context, p_fd, &net_len, 4) != 4
103122219Sharti	|| krb5_net_write (context, p_fd, appl_version, len) != len) {
104122219Sharti	ret = errno;
105122219Sharti	krb5_set_error_string (context, "write: %s", strerror(ret));
106122219Sharti	return ret;
107122219Sharti    }
108122219Sharti
109122219Sharti    sret = krb5_net_read (context, p_fd, &repl, sizeof(repl));
110122219Sharti    if (sret < 0) {
111122219Sharti	ret = errno;
112122219Sharti	krb5_set_error_string (context, "read: %s", strerror(ret));
113122219Sharti	return ret;
114122219Sharti    } else if (sret != sizeof(repl)) {
115122219Sharti	krb5_clear_error_string (context);
116122219Sharti	return KRB5_SENDAUTH_BADRESPONSE;
117122219Sharti    }
118122219Sharti
119122219Sharti    if (repl != 0) {
120	krb5_clear_error_string (context);
121	return KRB5_SENDAUTH_REJECTED;
122    }
123
124    if (in_creds == NULL) {
125	if (ccache == NULL) {
126	    ret = krb5_cc_default (context, &ccache);
127	    if (ret)
128		return ret;
129	    my_ccache = TRUE;
130	}
131
132	if (client == NULL) {
133	    ret = krb5_cc_get_principal (context, ccache, &this_client);
134	    if (ret) {
135		if(my_ccache)
136		    krb5_cc_close(context, ccache);
137		return ret;
138	    }
139	    client = this_client;
140	}
141	memset(&this_cred, 0, sizeof(this_cred));
142	this_cred.client = client;
143	this_cred.server = server;
144	this_cred.times.endtime = 0;
145	this_cred.ticket.length = 0;
146	in_creds = &this_cred;
147    }
148    if (in_creds->ticket.length == 0) {
149	ret = krb5_get_credentials (context, 0, ccache, in_creds, &creds);
150	if (ret) {
151	    if(my_ccache)
152		krb5_cc_close(context, ccache);
153	    return ret;
154	}
155    } else {
156	creds = in_creds;
157    }
158    if(my_ccache)
159	krb5_cc_close(context, ccache);
160    ret = krb5_mk_req_extended (context,
161				auth_context,
162				ap_req_options,
163				in_data,
164				creds,
165				&ap_req);
166
167    if (out_creds)
168	*out_creds = creds;
169    else
170	krb5_free_creds(context, creds);
171    if(this_client)
172	krb5_free_principal(context, this_client);
173
174    if (ret)
175	return ret;
176
177    ret = krb5_write_message (context,
178			      p_fd,
179			      &ap_req);
180    if (ret)
181	return ret;
182
183    krb5_data_free (&ap_req);
184
185    ret = krb5_read_message (context, p_fd, &error_data);
186    if (ret)
187	return ret;
188
189    if (error_data.length != 0) {
190	KRB_ERROR error;
191
192	ret = krb5_rd_error (context, &error_data, &error);
193	krb5_data_free (&error_data);
194	if (ret == 0) {
195	    ret = krb5_error_from_rd_error(context, &error, NULL);
196	    if (ret_error != NULL) {
197		*ret_error = malloc (sizeof(krb5_error));
198		if (*ret_error == NULL) {
199		    krb5_free_error_contents (context, &error);
200		} else {
201		    **ret_error = error;
202		}
203	    } else {
204		krb5_free_error_contents (context, &error);
205	    }
206	    return ret;
207	} else {
208	    krb5_clear_error_string(context);
209	    return ret;
210	}
211    }
212
213    if (ap_req_options & AP_OPTS_MUTUAL_REQUIRED) {
214	krb5_data ap_rep;
215	krb5_ap_rep_enc_part *ignore;
216
217	krb5_data_zero (&ap_rep);
218	ret = krb5_read_message (context,
219				 p_fd,
220				 &ap_rep);
221	if (ret)
222	    return ret;
223
224	ret = krb5_rd_rep (context, *auth_context, &ap_rep,
225			   rep_result ? rep_result : &ignore);
226	if (ret)
227	    return ret;
228	if (rep_result == NULL)
229	    krb5_free_ap_rep_enc_part (context, ignore);
230	krb5_data_free (&ap_rep);
231    }
232    return 0;
233}
234