155682Smarkm/*
2233294Sstas * Copyright (c) 1997 - 2006 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
955682Smarkm *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2055682Smarkm *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "krb5_locl.h"
3555682Smarkm
3655682Smarkm/*
3755682Smarkm * The format seems to be:
3855682Smarkm * client -> server
3955682Smarkm *
4055682Smarkm * 4 bytes - length
4155682Smarkm * KRB5_SENDAUTH_V1.0 (including zero)
4255682Smarkm * 4 bytes - length
4355682Smarkm * protocol string (with terminating zero)
4455682Smarkm *
4555682Smarkm * server -> client
4655682Smarkm * 1 byte - (0 = OK, else some kind of error)
4755682Smarkm *
4855682Smarkm * client -> server
4955682Smarkm * 4 bytes - length
5055682Smarkm * AP-REQ
5155682Smarkm *
5255682Smarkm * server -> client
5355682Smarkm * 4 bytes - length (0 = OK, else length of error)
5455682Smarkm * (error)
5555682Smarkm *
5655682Smarkm * if(mutual) {
5755682Smarkm * server -> client
5855682Smarkm * 4 bytes - length
5955682Smarkm * AP-REP
6055682Smarkm * }
6155682Smarkm */
6255682Smarkm
63233294SstasKRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
6455682Smarkmkrb5_sendauth(krb5_context context,
6555682Smarkm	      krb5_auth_context *auth_context,
6655682Smarkm	      krb5_pointer p_fd,
6755682Smarkm	      const char *appl_version,
6855682Smarkm	      krb5_principal client,
6955682Smarkm	      krb5_principal server,
7055682Smarkm	      krb5_flags ap_req_options,
7155682Smarkm	      krb5_data *in_data,
7255682Smarkm	      krb5_creds *in_creds,
7355682Smarkm	      krb5_ccache ccache,
7455682Smarkm	      krb5_error **ret_error,
7555682Smarkm	      krb5_ap_rep_enc_part **rep_result,
7655682Smarkm	      krb5_creds **out_creds)
7755682Smarkm{
7855682Smarkm    krb5_error_code ret;
79178825Sdfr    uint32_t len, net_len;
8055682Smarkm    const char *version = KRB5_SENDAUTH_VERSION;
8155682Smarkm    u_char repl;
8255682Smarkm    krb5_data ap_req, error_data;
8355682Smarkm    krb5_creds this_cred;
8455682Smarkm    krb5_principal this_client = NULL;
8555682Smarkm    krb5_creds *creds;
8655682Smarkm    ssize_t sret;
87103423Snectar    krb5_boolean my_ccache = FALSE;
8855682Smarkm
8955682Smarkm    len = strlen(version) + 1;
9055682Smarkm    net_len = htonl(len);
9155682Smarkm    if (krb5_net_write (context, p_fd, &net_len, 4) != 4
9278527Sassar	|| krb5_net_write (context, p_fd, version, len) != len) {
9378527Sassar	ret = errno;
94233294Sstas	krb5_set_error_message (context, ret, "write: %s", strerror(ret));
9578527Sassar	return ret;
9678527Sassar    }
9755682Smarkm
9855682Smarkm    len = strlen(appl_version) + 1;
9955682Smarkm    net_len = htonl(len);
10055682Smarkm    if (krb5_net_write (context, p_fd, &net_len, 4) != 4
10178527Sassar	|| krb5_net_write (context, p_fd, appl_version, len) != len) {
10278527Sassar	ret = errno;
103233294Sstas	krb5_set_error_message (context, ret, "write: %s", strerror(ret));
10478527Sassar	return ret;
10578527Sassar    }
10655682Smarkm
10755682Smarkm    sret = krb5_net_read (context, p_fd, &repl, sizeof(repl));
10878527Sassar    if (sret < 0) {
10978527Sassar	ret = errno;
110233294Sstas	krb5_set_error_message (context, ret, "read: %s", strerror(ret));
11178527Sassar	return ret;
11278527Sassar    } else if (sret != sizeof(repl)) {
113233294Sstas	krb5_clear_error_message (context);
11455682Smarkm	return KRB5_SENDAUTH_BADRESPONSE;
11578527Sassar    }
11655682Smarkm
11778527Sassar    if (repl != 0) {
118233294Sstas	krb5_clear_error_message (context);
11955682Smarkm	return KRB5_SENDAUTH_REJECTED;
12078527Sassar    }
12155682Smarkm
12255682Smarkm    if (in_creds == NULL) {
12355682Smarkm	if (ccache == NULL) {
12455682Smarkm	    ret = krb5_cc_default (context, &ccache);
12555682Smarkm	    if (ret)
12655682Smarkm		return ret;
127103423Snectar	    my_ccache = TRUE;
12855682Smarkm	}
12955682Smarkm
13055682Smarkm	if (client == NULL) {
13155682Smarkm	    ret = krb5_cc_get_principal (context, ccache, &this_client);
132103423Snectar	    if (ret) {
133103423Snectar		if(my_ccache)
134103423Snectar		    krb5_cc_close(context, ccache);
13555682Smarkm		return ret;
136103423Snectar	    }
13755682Smarkm	    client = this_client;
13855682Smarkm	}
13955682Smarkm	memset(&this_cred, 0, sizeof(this_cred));
14055682Smarkm	this_cred.client = client;
14155682Smarkm	this_cred.server = server;
14255682Smarkm	this_cred.times.endtime = 0;
14355682Smarkm	this_cred.ticket.length = 0;
14455682Smarkm	in_creds = &this_cred;
14555682Smarkm    }
14655682Smarkm    if (in_creds->ticket.length == 0) {
14755682Smarkm	ret = krb5_get_credentials (context, 0, ccache, in_creds, &creds);
148103423Snectar	if (ret) {
149103423Snectar	    if(my_ccache)
150103423Snectar		krb5_cc_close(context, ccache);
15155682Smarkm	    return ret;
152103423Snectar	}
15355682Smarkm    } else {
15455682Smarkm	creds = in_creds;
15555682Smarkm    }
156103423Snectar    if(my_ccache)
157103423Snectar	krb5_cc_close(context, ccache);
15855682Smarkm    ret = krb5_mk_req_extended (context,
15955682Smarkm				auth_context,
16055682Smarkm				ap_req_options,
16155682Smarkm				in_data,
16255682Smarkm				creds,
16355682Smarkm				&ap_req);
16455682Smarkm
16555682Smarkm    if (out_creds)
16655682Smarkm	*out_creds = creds;
16755682Smarkm    else
16855682Smarkm	krb5_free_creds(context, creds);
16955682Smarkm    if(this_client)
17055682Smarkm	krb5_free_principal(context, this_client);
17155682Smarkm
17255682Smarkm    if (ret)
17355682Smarkm	return ret;
17455682Smarkm
17555682Smarkm    ret = krb5_write_message (context,
17655682Smarkm			      p_fd,
17755682Smarkm			      &ap_req);
17855682Smarkm    if (ret)
17955682Smarkm	return ret;
18055682Smarkm
18155682Smarkm    krb5_data_free (&ap_req);
18255682Smarkm
18355682Smarkm    ret = krb5_read_message (context, p_fd, &error_data);
18455682Smarkm    if (ret)
18555682Smarkm	return ret;
18655682Smarkm
18755682Smarkm    if (error_data.length != 0) {
18855682Smarkm	KRB_ERROR error;
18955682Smarkm
19055682Smarkm	ret = krb5_rd_error (context, &error_data, &error);
19155682Smarkm	krb5_data_free (&error_data);
19255682Smarkm	if (ret == 0) {
19378527Sassar	    ret = krb5_error_from_rd_error(context, &error, NULL);
19455682Smarkm	    if (ret_error != NULL) {
19555682Smarkm		*ret_error = malloc (sizeof(krb5_error));
19655682Smarkm		if (*ret_error == NULL) {
19778527Sassar		    krb5_free_error_contents (context, &error);
19855682Smarkm		} else {
19955682Smarkm		    **ret_error = error;
20055682Smarkm		}
20155682Smarkm	    } else {
20278527Sassar		krb5_free_error_contents (context, &error);
20355682Smarkm	    }
20455682Smarkm	    return ret;
20578527Sassar	} else {
206233294Sstas	    krb5_clear_error_message(context);
20778527Sassar	    return ret;
20878527Sassar	}
209233294Sstas    } else
210233294Sstas	krb5_data_free (&error_data);
21155682Smarkm
21255682Smarkm    if (ap_req_options & AP_OPTS_MUTUAL_REQUIRED) {
21355682Smarkm	krb5_data ap_rep;
214233294Sstas	krb5_ap_rep_enc_part *ignore = NULL;
21555682Smarkm
21655682Smarkm	krb5_data_zero (&ap_rep);
21755682Smarkm	ret = krb5_read_message (context,
21855682Smarkm				 p_fd,
21955682Smarkm				 &ap_rep);
22055682Smarkm	if (ret)
22155682Smarkm	    return ret;
22255682Smarkm
22355682Smarkm	ret = krb5_rd_rep (context, *auth_context, &ap_rep,
22455682Smarkm			   rep_result ? rep_result : &ignore);
225178825Sdfr	krb5_data_free (&ap_rep);
22655682Smarkm	if (ret)
22755682Smarkm	    return ret;
22855682Smarkm	if (rep_result == NULL)
22955682Smarkm	    krb5_free_ap_rep_enc_part (context, ignore);
23055682Smarkm    }
23155682Smarkm    return 0;
23255682Smarkm}
233