1 /*
2 * Copyright (c) 1997-2007 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 "krb5_locl.h"
35
36 /*
37 * See `sendauth.c' for the format.
38 */
39
40 static krb5_boolean
match_exact(const void * data,const char * appl_version)41 match_exact(const void *data, const char *appl_version)
42 {
43 return strcmp(data, appl_version) == 0;
44 }
45
46 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)47 krb5_recvauth(krb5_context context,
48 krb5_auth_context *auth_context,
49 krb5_pointer p_fd,
50 const char *appl_version,
51 krb5_principal server,
52 int32_t flags,
53 krb5_keytab keytab,
54 krb5_ticket **ticket)
55 {
56 return krb5_recvauth_match_version(context, auth_context, p_fd,
57 match_exact, appl_version,
58 server, flags,
59 keytab, ticket);
60 }
61
62 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)63 krb5_recvauth_match_version(krb5_context context,
64 krb5_auth_context *auth_context,
65 krb5_pointer p_fd,
66 krb5_boolean (*match_appl_version)(const void *,
67 const char*),
68 const void *match_data,
69 krb5_principal server,
70 int32_t flags,
71 krb5_keytab keytab,
72 krb5_ticket **ticket)
73 {
74 krb5_error_code ret;
75 const char *version = KRB5_SENDAUTH_VERSION;
76 char her_version[sizeof(KRB5_SENDAUTH_VERSION)];
77 char *her_appl_version;
78 uint32_t len, bytes;
79 u_char repl;
80 krb5_data data;
81 krb5_flags ap_options;
82 ssize_t n;
83
84 /*
85 * If there are no addresses in auth_context, get them from `fd'.
86 */
87
88 if (*auth_context == NULL) {
89 ret = krb5_auth_con_init (context, auth_context);
90 if (ret)
91 return ret;
92 }
93
94 ret = krb5_auth_con_setaddrs_from_fd (context,
95 *auth_context,
96 p_fd);
97 if (ret)
98 return ret;
99
100 if(!(flags & KRB5_RECVAUTH_IGNORE_VERSION)) {
101 n = krb5_net_read (context, p_fd, &len, 4);
102 if (n < 0) {
103 ret = errno;
104 krb5_set_error_message(context, ret, "read: %s", strerror(ret));
105 return ret;
106 }
107 if (n == 0) {
108 krb5_set_error_message(context, KRB5_SENDAUTH_BADAUTHVERS,
109 N_("Failed to receive sendauth data", ""));
110 return KRB5_SENDAUTH_BADAUTHVERS;
111 }
112 len = ntohl(len);
113 if (len != sizeof(her_version)
114 || krb5_net_read (context, p_fd, her_version, len) != len
115 || strncmp (version, her_version, len)) {
116 repl = 1;
117 krb5_net_write (context, p_fd, &repl, 1);
118 krb5_clear_error_message (context);
119 return KRB5_SENDAUTH_BADAUTHVERS;
120 }
121 }
122
123 n = krb5_net_read (context, p_fd, &len, 4);
124 if (n < 0) {
125 ret = errno;
126 krb5_set_error_message(context, ret, "read: %s", strerror(ret));
127 return ret;
128 }
129 if (n == 0) {
130 krb5_clear_error_message (context);
131 return KRB5_SENDAUTH_BADAPPLVERS;
132 }
133 len = ntohl(len);
134 her_appl_version = malloc (len);
135 if (her_appl_version == NULL) {
136 repl = 2;
137 krb5_net_write (context, p_fd, &repl, 1);
138 krb5_set_error_message(context, ENOMEM,
139 N_("malloc: out of memory", ""));
140 return ENOMEM;
141 }
142 if ((bytes = krb5_net_read (context, p_fd, her_appl_version, len))) {
143 /* PR/267884: String read must always conatain a terminating NUL */
144 if (strnlen(her_appl_version, len) == len)
145 her_appl_version[len-1] = '\0';
146
147 if (bytes != len ||
148 !(*match_appl_version)(match_data, her_appl_version)) {
149 repl = 2;
150 krb5_net_write (context, p_fd, &repl, 1);
151 krb5_set_error_message(context, KRB5_SENDAUTH_BADAPPLVERS,
152 N_("wrong sendauth version (%s)", ""),
153 her_appl_version);
154 free (her_appl_version);
155 return KRB5_SENDAUTH_BADAPPLVERS;
156 }
157 }
158 free (her_appl_version);
159
160 repl = 0;
161 if (krb5_net_write (context, p_fd, &repl, 1) != 1) {
162 ret = errno;
163 krb5_set_error_message(context, ret, "write: %s", strerror(ret));
164 return ret;
165 }
166
167 krb5_data_zero (&data);
168 ret = krb5_read_message (context, p_fd, &data);
169 if (ret)
170 return ret;
171
172 ret = krb5_rd_req (context,
173 auth_context,
174 &data,
175 server,
176 keytab,
177 &ap_options,
178 ticket);
179 krb5_data_free (&data);
180 if (ret) {
181 krb5_data error_data;
182 krb5_error_code ret2;
183
184 ret2 = krb5_mk_error (context,
185 ret,
186 NULL,
187 NULL,
188 NULL,
189 server,
190 NULL,
191 NULL,
192 &error_data);
193 if (ret2 == 0) {
194 krb5_write_message (context, p_fd, &error_data);
195 krb5_data_free (&error_data);
196 }
197 return ret;
198 }
199
200 len = 0;
201 if (krb5_net_write (context, p_fd, &len, 4) != 4) {
202 ret = errno;
203 krb5_set_error_message(context, ret, "write: %s", strerror(ret));
204 krb5_free_ticket(context, *ticket);
205 *ticket = NULL;
206 return ret;
207 }
208
209 if (ap_options & AP_OPTS_MUTUAL_REQUIRED) {
210 ret = krb5_mk_rep (context, *auth_context, &data);
211 if (ret) {
212 krb5_free_ticket(context, *ticket);
213 *ticket = NULL;
214 return ret;
215 }
216
217 ret = krb5_write_message (context, p_fd, &data);
218 if (ret) {
219 krb5_free_ticket(context, *ticket);
220 *ticket = NULL;
221 return ret;
222 }
223 krb5_data_free (&data);
224 }
225 return 0;
226 }
227