2 * Copyright (c) 1997-2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
34 #include "krb5_locl.h"
36 __RCSID("$Heimdal: recvauth.c 20306 2007-04-11 11:15:55Z lha $"
40 * See `sendauth.c' for the format.
44 match_exact(const void *data
, const char *appl_version
)
46 return strcmp(data
, appl_version
) == 0;
49 krb5_error_code KRB5_LIB_FUNCTION
50 krb5_recvauth(krb5_context context
,
51 krb5_auth_context
*auth_context
,
53 const char *appl_version
,
54 krb5_principal server
,
59 return krb5_recvauth_match_version(context
, auth_context
, p_fd
,
60 match_exact
, appl_version
,
65 krb5_error_code KRB5_LIB_FUNCTION
66 krb5_recvauth_match_version(krb5_context context
,
67 krb5_auth_context
*auth_context
,
69 krb5_boolean (*match_appl_version
)(const void *,
71 const void *match_data
,
72 krb5_principal server
,
78 const char *version
= KRB5_SENDAUTH_VERSION
;
79 char her_version
[sizeof(KRB5_SENDAUTH_VERSION
)];
80 char *her_appl_version
;
84 krb5_flags ap_options
;
88 * If there are no addresses in auth_context, get them from `fd'.
91 if (*auth_context
== NULL
) {
92 ret
= krb5_auth_con_init (context
, auth_context
);
97 ret
= krb5_auth_con_setaddrs_from_fd (context
,
103 if(!(flags
& KRB5_RECVAUTH_IGNORE_VERSION
)) {
104 n
= krb5_net_read (context
, p_fd
, &len
, 4);
107 krb5_set_error_string (context
, "read: %s", strerror(errno
));
111 krb5_set_error_string (context
, "Failed to receive sendauth data");
112 return KRB5_SENDAUTH_BADAUTHVERS
;
115 if (len
!= sizeof(her_version
)
116 || krb5_net_read (context
, p_fd
, her_version
, len
) != len
117 || strncmp (version
, her_version
, len
)) {
119 krb5_net_write (context
, p_fd
, &repl
, 1);
120 krb5_clear_error_string (context
);
121 return KRB5_SENDAUTH_BADAUTHVERS
;
125 n
= krb5_net_read (context
, p_fd
, &len
, 4);
128 krb5_set_error_string (context
, "read: %s", strerror(errno
));
132 krb5_clear_error_string (context
);
133 return KRB5_SENDAUTH_BADAPPLVERS
;
136 her_appl_version
= malloc (len
);
137 if (her_appl_version
== NULL
) {
139 krb5_net_write (context
, p_fd
, &repl
, 1);
140 krb5_set_error_string (context
, "malloc: out of memory");
143 if (krb5_net_read (context
, p_fd
, her_appl_version
, len
) != len
144 || !(*match_appl_version
)(match_data
, her_appl_version
)) {
146 krb5_net_write (context
, p_fd
, &repl
, 1);
147 krb5_set_error_string (context
, "wrong sendauth version (%s)",
149 free (her_appl_version
);
150 return KRB5_SENDAUTH_BADAPPLVERS
;
152 free (her_appl_version
);
155 if (krb5_net_write (context
, p_fd
, &repl
, 1) != 1) {
157 krb5_set_error_string (context
, "write: %s", strerror(errno
));
161 krb5_data_zero (&data
);
162 ret
= krb5_read_message (context
, p_fd
, &data
);
166 ret
= krb5_rd_req (context
,
173 krb5_data_free (&data
);
175 krb5_data error_data
;
176 krb5_error_code ret2
;
178 ret2
= krb5_mk_error (context
,
188 krb5_write_message (context
, p_fd
, &error_data
);
189 krb5_data_free (&error_data
);
195 if (krb5_net_write (context
, p_fd
, &len
, 4) != 4) {
197 krb5_set_error_string (context
, "write: %s", strerror(errno
));
201 if (ap_options
& AP_OPTS_MUTUAL_REQUIRED
) {
202 ret
= krb5_mk_rep (context
, *auth_context
, &data
);
206 ret
= krb5_write_message (context
, p_fd
, &data
);
209 krb5_data_free (&data
);