1*ebfedea0SLionel Sambuc /* $NetBSD: recvauth.c,v 1.1.1.1 2011/04/13 18:15:37 elric Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc * All rights reserved.
7*ebfedea0SLionel Sambuc *
8*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc * are met:
11*ebfedea0SLionel Sambuc *
12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc *
15*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc *
19*ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc * without specific prior written permission.
22*ebfedea0SLionel Sambuc *
23*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc */
35*ebfedea0SLionel Sambuc
36*ebfedea0SLionel Sambuc #include "krb5_locl.h"
37*ebfedea0SLionel Sambuc
38*ebfedea0SLionel Sambuc /*
39*ebfedea0SLionel Sambuc * See `sendauth.c' for the format.
40*ebfedea0SLionel Sambuc */
41*ebfedea0SLionel Sambuc
42*ebfedea0SLionel Sambuc static krb5_boolean
match_exact(const void * data,const char * appl_version)43*ebfedea0SLionel Sambuc match_exact(const void *data, const char *appl_version)
44*ebfedea0SLionel Sambuc {
45*ebfedea0SLionel Sambuc return strcmp(data, appl_version) == 0;
46*ebfedea0SLionel Sambuc }
47*ebfedea0SLionel Sambuc
48*ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_recvauth(krb5_context context,krb5_auth_context * auth_context,krb5_pointer p_fd,const char * appl_version,krb5_principal server,int32_t flags,krb5_keytab keytab,krb5_ticket ** ticket)49*ebfedea0SLionel Sambuc krb5_recvauth(krb5_context context,
50*ebfedea0SLionel Sambuc krb5_auth_context *auth_context,
51*ebfedea0SLionel Sambuc krb5_pointer p_fd,
52*ebfedea0SLionel Sambuc const char *appl_version,
53*ebfedea0SLionel Sambuc krb5_principal server,
54*ebfedea0SLionel Sambuc int32_t flags,
55*ebfedea0SLionel Sambuc krb5_keytab keytab,
56*ebfedea0SLionel Sambuc krb5_ticket **ticket)
57*ebfedea0SLionel Sambuc {
58*ebfedea0SLionel Sambuc return krb5_recvauth_match_version(context, auth_context, p_fd,
59*ebfedea0SLionel Sambuc match_exact, appl_version,
60*ebfedea0SLionel Sambuc server, flags,
61*ebfedea0SLionel Sambuc keytab, ticket);
62*ebfedea0SLionel Sambuc }
63*ebfedea0SLionel Sambuc
64*ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_recvauth_match_version(krb5_context context,krb5_auth_context * auth_context,krb5_pointer p_fd,krb5_boolean (* match_appl_version)(const void *,const char *),const void * match_data,krb5_principal server,int32_t flags,krb5_keytab keytab,krb5_ticket ** ticket)65*ebfedea0SLionel Sambuc krb5_recvauth_match_version(krb5_context context,
66*ebfedea0SLionel Sambuc krb5_auth_context *auth_context,
67*ebfedea0SLionel Sambuc krb5_pointer p_fd,
68*ebfedea0SLionel Sambuc krb5_boolean (*match_appl_version)(const void *,
69*ebfedea0SLionel Sambuc const char*),
70*ebfedea0SLionel Sambuc const void *match_data,
71*ebfedea0SLionel Sambuc krb5_principal server,
72*ebfedea0SLionel Sambuc int32_t flags,
73*ebfedea0SLionel Sambuc krb5_keytab keytab,
74*ebfedea0SLionel Sambuc krb5_ticket **ticket)
75*ebfedea0SLionel Sambuc {
76*ebfedea0SLionel Sambuc krb5_error_code ret;
77*ebfedea0SLionel Sambuc const char *version = KRB5_SENDAUTH_VERSION;
78*ebfedea0SLionel Sambuc char her_version[sizeof(KRB5_SENDAUTH_VERSION)];
79*ebfedea0SLionel Sambuc char *her_appl_version;
80*ebfedea0SLionel Sambuc uint32_t len;
81*ebfedea0SLionel Sambuc u_char repl;
82*ebfedea0SLionel Sambuc krb5_data data;
83*ebfedea0SLionel Sambuc krb5_flags ap_options;
84*ebfedea0SLionel Sambuc ssize_t n;
85*ebfedea0SLionel Sambuc
86*ebfedea0SLionel Sambuc /*
87*ebfedea0SLionel Sambuc * If there are no addresses in auth_context, get them from `fd'.
88*ebfedea0SLionel Sambuc */
89*ebfedea0SLionel Sambuc
90*ebfedea0SLionel Sambuc if (*auth_context == NULL) {
91*ebfedea0SLionel Sambuc ret = krb5_auth_con_init (context, auth_context);
92*ebfedea0SLionel Sambuc if (ret)
93*ebfedea0SLionel Sambuc return ret;
94*ebfedea0SLionel Sambuc }
95*ebfedea0SLionel Sambuc
96*ebfedea0SLionel Sambuc ret = krb5_auth_con_setaddrs_from_fd (context,
97*ebfedea0SLionel Sambuc *auth_context,
98*ebfedea0SLionel Sambuc p_fd);
99*ebfedea0SLionel Sambuc if (ret)
100*ebfedea0SLionel Sambuc return ret;
101*ebfedea0SLionel Sambuc
102*ebfedea0SLionel Sambuc if(!(flags & KRB5_RECVAUTH_IGNORE_VERSION)) {
103*ebfedea0SLionel Sambuc n = krb5_net_read (context, p_fd, &len, 4);
104*ebfedea0SLionel Sambuc if (n < 0) {
105*ebfedea0SLionel Sambuc ret = errno;
106*ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "read: %s", strerror(ret));
107*ebfedea0SLionel Sambuc return ret;
108*ebfedea0SLionel Sambuc }
109*ebfedea0SLionel Sambuc if (n == 0) {
110*ebfedea0SLionel Sambuc krb5_set_error_message(context, KRB5_SENDAUTH_BADAUTHVERS,
111*ebfedea0SLionel Sambuc N_("Failed to receive sendauth data", ""));
112*ebfedea0SLionel Sambuc return KRB5_SENDAUTH_BADAUTHVERS;
113*ebfedea0SLionel Sambuc }
114*ebfedea0SLionel Sambuc len = ntohl(len);
115*ebfedea0SLionel Sambuc if (len != sizeof(her_version)
116*ebfedea0SLionel Sambuc || krb5_net_read (context, p_fd, her_version, len) != len
117*ebfedea0SLionel Sambuc || strncmp (version, her_version, len)) {
118*ebfedea0SLionel Sambuc repl = 1;
119*ebfedea0SLionel Sambuc krb5_net_write (context, p_fd, &repl, 1);
120*ebfedea0SLionel Sambuc krb5_clear_error_message (context);
121*ebfedea0SLionel Sambuc return KRB5_SENDAUTH_BADAUTHVERS;
122*ebfedea0SLionel Sambuc }
123*ebfedea0SLionel Sambuc }
124*ebfedea0SLionel Sambuc
125*ebfedea0SLionel Sambuc n = krb5_net_read (context, p_fd, &len, 4);
126*ebfedea0SLionel Sambuc if (n < 0) {
127*ebfedea0SLionel Sambuc ret = errno;
128*ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "read: %s", strerror(ret));
129*ebfedea0SLionel Sambuc return ret;
130*ebfedea0SLionel Sambuc }
131*ebfedea0SLionel Sambuc if (n == 0) {
132*ebfedea0SLionel Sambuc krb5_clear_error_message (context);
133*ebfedea0SLionel Sambuc return KRB5_SENDAUTH_BADAPPLVERS;
134*ebfedea0SLionel Sambuc }
135*ebfedea0SLionel Sambuc len = ntohl(len);
136*ebfedea0SLionel Sambuc her_appl_version = malloc (len);
137*ebfedea0SLionel Sambuc if (her_appl_version == NULL) {
138*ebfedea0SLionel Sambuc repl = 2;
139*ebfedea0SLionel Sambuc krb5_net_write (context, p_fd, &repl, 1);
140*ebfedea0SLionel Sambuc krb5_set_error_message(context, ENOMEM,
141*ebfedea0SLionel Sambuc N_("malloc: out of memory", ""));
142*ebfedea0SLionel Sambuc return ENOMEM;
143*ebfedea0SLionel Sambuc }
144*ebfedea0SLionel Sambuc if (krb5_net_read (context, p_fd, her_appl_version, len) != len
145*ebfedea0SLionel Sambuc || !(*match_appl_version)(match_data, her_appl_version)) {
146*ebfedea0SLionel Sambuc repl = 2;
147*ebfedea0SLionel Sambuc krb5_net_write (context, p_fd, &repl, 1);
148*ebfedea0SLionel Sambuc krb5_set_error_message(context, KRB5_SENDAUTH_BADAPPLVERS,
149*ebfedea0SLionel Sambuc N_("wrong sendauth version (%s)", ""),
150*ebfedea0SLionel Sambuc her_appl_version);
151*ebfedea0SLionel Sambuc free (her_appl_version);
152*ebfedea0SLionel Sambuc return KRB5_SENDAUTH_BADAPPLVERS;
153*ebfedea0SLionel Sambuc }
154*ebfedea0SLionel Sambuc free (her_appl_version);
155*ebfedea0SLionel Sambuc
156*ebfedea0SLionel Sambuc repl = 0;
157*ebfedea0SLionel Sambuc if (krb5_net_write (context, p_fd, &repl, 1) != 1) {
158*ebfedea0SLionel Sambuc ret = errno;
159*ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "write: %s", strerror(ret));
160*ebfedea0SLionel Sambuc return ret;
161*ebfedea0SLionel Sambuc }
162*ebfedea0SLionel Sambuc
163*ebfedea0SLionel Sambuc krb5_data_zero (&data);
164*ebfedea0SLionel Sambuc ret = krb5_read_message (context, p_fd, &data);
165*ebfedea0SLionel Sambuc if (ret)
166*ebfedea0SLionel Sambuc return ret;
167*ebfedea0SLionel Sambuc
168*ebfedea0SLionel Sambuc ret = krb5_rd_req (context,
169*ebfedea0SLionel Sambuc auth_context,
170*ebfedea0SLionel Sambuc &data,
171*ebfedea0SLionel Sambuc server,
172*ebfedea0SLionel Sambuc keytab,
173*ebfedea0SLionel Sambuc &ap_options,
174*ebfedea0SLionel Sambuc ticket);
175*ebfedea0SLionel Sambuc krb5_data_free (&data);
176*ebfedea0SLionel Sambuc if (ret) {
177*ebfedea0SLionel Sambuc krb5_data error_data;
178*ebfedea0SLionel Sambuc krb5_error_code ret2;
179*ebfedea0SLionel Sambuc
180*ebfedea0SLionel Sambuc ret2 = krb5_mk_error (context,
181*ebfedea0SLionel Sambuc ret,
182*ebfedea0SLionel Sambuc NULL,
183*ebfedea0SLionel Sambuc NULL,
184*ebfedea0SLionel Sambuc NULL,
185*ebfedea0SLionel Sambuc server,
186*ebfedea0SLionel Sambuc NULL,
187*ebfedea0SLionel Sambuc NULL,
188*ebfedea0SLionel Sambuc &error_data);
189*ebfedea0SLionel Sambuc if (ret2 == 0) {
190*ebfedea0SLionel Sambuc krb5_write_message (context, p_fd, &error_data);
191*ebfedea0SLionel Sambuc krb5_data_free (&error_data);
192*ebfedea0SLionel Sambuc }
193*ebfedea0SLionel Sambuc return ret;
194*ebfedea0SLionel Sambuc }
195*ebfedea0SLionel Sambuc
196*ebfedea0SLionel Sambuc len = 0;
197*ebfedea0SLionel Sambuc if (krb5_net_write (context, p_fd, &len, 4) != 4) {
198*ebfedea0SLionel Sambuc ret = errno;
199*ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "write: %s", strerror(ret));
200*ebfedea0SLionel Sambuc krb5_free_ticket(context, *ticket);
201*ebfedea0SLionel Sambuc *ticket = NULL;
202*ebfedea0SLionel Sambuc return ret;
203*ebfedea0SLionel Sambuc }
204*ebfedea0SLionel Sambuc
205*ebfedea0SLionel Sambuc if (ap_options & AP_OPTS_MUTUAL_REQUIRED) {
206*ebfedea0SLionel Sambuc ret = krb5_mk_rep (context, *auth_context, &data);
207*ebfedea0SLionel Sambuc if (ret) {
208*ebfedea0SLionel Sambuc krb5_free_ticket(context, *ticket);
209*ebfedea0SLionel Sambuc *ticket = NULL;
210*ebfedea0SLionel Sambuc return ret;
211*ebfedea0SLionel Sambuc }
212*ebfedea0SLionel Sambuc
213*ebfedea0SLionel Sambuc ret = krb5_write_message (context, p_fd, &data);
214*ebfedea0SLionel Sambuc if (ret) {
215*ebfedea0SLionel Sambuc krb5_free_ticket(context, *ticket);
216*ebfedea0SLionel Sambuc *ticket = NULL;
217*ebfedea0SLionel Sambuc return ret;
218*ebfedea0SLionel Sambuc }
219*ebfedea0SLionel Sambuc krb5_data_free (&data);
220*ebfedea0SLionel Sambuc }
221*ebfedea0SLionel Sambuc return 0;
222*ebfedea0SLionel Sambuc }
223