1 /* This source code was modified by Martin Hedenfalk <mhe@stacken.kth.se> for
2 * use in Curl. His latest changes were done 2000-09-18.
4 * It has since been patched away like a madman by Daniel Stenberg
5 * <daniel@haxx.se> to make it better applied to curl conditions, and to make
6 * it not use globals, pollute name space and more. This source code awaits a
7 * rewrite to work around the paragraph 2 in the BSD licenses as explained
10 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
11 * (Royal Institute of Technology, Stockholm, Sweden).
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the Institute nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 #ifndef CURL_DISABLE_FTP
57 #include <unistd.h> /* for getpid() */
64 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
65 #include "inet_ntoa_r.h"
68 /* The last #include file should be: */
73 #define LOCAL_ADDR (&conn->local_addr)
74 #define REMOTE_ADDR (&conn->serv_addr)
75 #define myctladdr LOCAL_ADDR
76 #define hisctladdr REMOTE_ADDR
80 des_key_schedule schedule;
82 char instance[INST_SZ];
87 /* if it ever goes non-static, make it Curl_ prefixed! */
89 strlcpy (char *dst, const char *src, size_t dst_sz)
95 n + 1 < dst_sz && *src != '\0';
102 return n + strlen (src);
105 size_t strlcpy (char *dst, const char *src, size_t dst_sz);
109 krb4_check_prot(void *app_data, int level)
111 app_data = NULL; /* prevent compiler warning */
112 if(level == prot_confidential)
118 krb4_decode(void *app_data, void *buf, int len, int level,
119 struct connectdata *conn)
123 struct krb4_data *d = app_data;
125 if(level == prot_safe)
126 e = krb_rd_safe(buf, len, &d->key,
127 (struct sockaddr_in *)REMOTE_ADDR,
128 (struct sockaddr_in *)LOCAL_ADDR, &m);
130 e = krb_rd_priv(buf, len, d->schedule, &d->key,
131 (struct sockaddr_in *)REMOTE_ADDR,
132 (struct sockaddr_in *)LOCAL_ADDR, &m);
134 struct SessionHandle *data = conn->data;
135 infof(data, "krb4_decode: %s\n", krb_get_err_text(e));
138 memmove(buf, m.app_data, m.app_length);
143 krb4_overhead(void *app_data, int level, int len)
145 /* no arguments are used, just init them to prevent compiler warnings */
153 krb4_encode(void *app_data, void *from, int length, int level, void **to,
154 struct connectdata *conn)
156 struct krb4_data *d = app_data;
157 *to = malloc(length + 31);
158 if(level == prot_safe)
159 return krb_mk_safe(from, *to, length, &d->key,
160 (struct sockaddr_in *)LOCAL_ADDR,
161 (struct sockaddr_in *)REMOTE_ADDR);
162 else if(level == prot_private)
163 return krb_mk_priv(from, *to, length, d->schedule, &d->key,
164 (struct sockaddr_in *)LOCAL_ADDR,
165 (struct sockaddr_in *)REMOTE_ADDR);
171 mk_auth(struct krb4_data *d, KTEXT adat,
172 const char *service, char *host, int checksum)
176 char sname[SNAME_SZ], inst[INST_SZ], realm[REALM_SZ];
178 strlcpy(sname, service, sizeof(sname));
179 strlcpy(inst, krb_get_phost(host), sizeof(inst));
180 strlcpy(realm, krb_realmofhost(host), sizeof(realm));
181 ret = krb_mk_req(adat, sname, inst, realm, checksum);
184 strlcpy(sname, service, sizeof(sname));
185 strlcpy(inst, krb_get_phost(host), sizeof(inst));
186 strlcpy(realm, krb_realmofhost(host), sizeof(realm));
187 ret = krb_get_cred(sname, inst, realm, &cred);
188 memmove(&d->key, &cred.session, sizeof(des_cblock));
189 des_key_sched(&d->key, d->schedule);
190 memset(&cred, 0, sizeof(cred));
194 #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
195 int krb_get_our_ip_for_realm(char *, struct in_addr *);
199 krb4_auth(void *app_data, struct connectdata *conn)
208 struct krb4_data *d = app_data;
209 char *host = conn->hostname;
211 int l = sizeof(conn->local_addr);
212 struct SessionHandle *data = conn->data;
215 if(getsockname(conn->sock[FIRSTSOCKET],
216 (struct sockaddr *)LOCAL_ADDR, &l) < 0)
217 perror("getsockname()");
220 ret = mk_auth(d, &adat, "ftp", host, checksum);
221 if(ret == KDC_PR_UNKNOWN)
222 ret = mk_auth(d, &adat, "rcmd", host, checksum);
224 Curl_infof(data, "%s\n", krb_get_err_text(ret));
225 return AUTH_CONTINUE;
228 #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
229 if (krb_get_config_bool("nat_in_use")) {
230 struct sockaddr_in *localaddr = (struct sockaddr_in *)LOCAL_ADDR;
231 struct in_addr natAddr;
233 if (krb_get_our_ip_for_realm(krb_realmofhost(host),
234 &natAddr) != KSUCCESS
235 && krb_get_our_ip_for_realm(NULL, &natAddr) != KSUCCESS)
236 Curl_infof(data, "Can't get address for realm %s\n",
237 krb_realmofhost(host));
239 if (natAddr.s_addr != localaddr->sin_addr.s_addr) {
240 #ifdef HAVE_INET_NTOA_R
242 char *ip = (char *)inet_ntoa_r(natAddr, ntoa_buf, sizeof(ntoa_buf));
244 char *ip = (char *)inet_ntoa(natAddr);
246 Curl_infof(data, "Using NAT IP address (%s) for kerberos 4\n", ip);
247 localaddr->sin_addr = natAddr;
253 if(Curl_base64_encode((char *)adat.dat, adat.length, &p) < 1) {
254 Curl_failf(data, "Out of memory base64-encoding");
255 return AUTH_CONTINUE;
258 result = Curl_ftpsendf(conn, "ADAT %s", p);
265 if(Curl_GetFTPResponse(&nread, conn, NULL))
268 if(data->state.buffer[0] != '2'){
269 Curl_failf(data, "Server didn't accept auth data");
273 p = strstr(data->state.buffer, "ADAT=");
275 Curl_failf(data, "Remote host didn't send adat reply");
279 len = Curl_base64_decode(p, (char *)adat.dat);
281 Curl_failf(data, "Failed to decode base64 from server");
285 ret = krb_rd_safe(adat.dat, adat.length, &d->key,
286 (struct sockaddr_in *)hisctladdr,
287 (struct sockaddr_in *)myctladdr, &msg_data);
289 Curl_failf(data, "Error reading reply from server: %s",
290 krb_get_err_text(ret));
293 krb_get_int(msg_data.app_data, &cs, 4, 0);
294 if(cs - checksum != 1) {
295 Curl_failf(data, "Bad checksum returned from server");
301 struct Curl_sec_client_mech Curl_krb4_client_mech = {
303 sizeof(struct krb4_data),
313 CURLcode Curl_krb_kauth(struct connectdata *conn)
316 des_key_schedule schedule;
317 KTEXT_ST tkt, tktcopy;
326 save = Curl_set_command_prot(conn, prot_private);
328 result = Curl_ftpsendf(conn, "SITE KAUTH %s", conn->user);
333 result = Curl_GetFTPResponse(&nread, conn, NULL);
337 if(conn->data->state.buffer[0] != '3'){
338 Curl_set_command_prot(conn, save);
339 return CURLE_FTP_WEIRD_SERVER_REPLY;
342 p = strstr(conn->data->state.buffer, "T=");
344 Curl_failf(conn->data, "Bad reply from server");
345 Curl_set_command_prot(conn, save);
346 return CURLE_FTP_WEIRD_SERVER_REPLY;
350 tmp = Curl_base64_decode(p, (char *)tkt.dat);
352 Curl_failf(conn->data, "Failed to decode base64 in reply.\n");
353 Curl_set_command_prot(conn, save);
354 return CURLE_FTP_WEIRD_SERVER_REPLY;
357 tktcopy.length = tkt.length;
359 p = strstr(conn->data->state.buffer, "P=");
361 Curl_failf(conn->data, "Bad reply from server");
362 Curl_set_command_prot(conn, save);
363 return CURLE_FTP_WEIRD_SERVER_REPLY;
366 for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
369 des_string_to_key (conn->passwd, &key);
370 des_key_sched(&key, schedule);
372 des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
374 schedule, &key, DES_DECRYPT);
375 if (strcmp ((char*)tktcopy.dat + 8,
376 KRB_TICKET_GRANTING_TICKET) != 0) {
377 afs_string_to_key(passwd,
378 krb_realmofhost(conn->hostname),
380 des_key_sched(&key, schedule);
381 des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
383 schedule, &key, DES_DECRYPT);
385 memset(key, 0, sizeof(key));
386 memset(schedule, 0, sizeof(schedule));
387 memset(passwd, 0, sizeof(passwd));
388 if(Curl_base64_encode((char *)tktcopy.dat, tktcopy.length, &p) < 1) {
389 failf(conn->data, "Out of memory base64-encoding.");
390 Curl_set_command_prot(conn, save);
391 return CURLE_OUT_OF_MEMORY;
393 memset (tktcopy.dat, 0, tktcopy.length);
395 result = Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p);
400 result = Curl_GetFTPResponse(&nread, conn, NULL);
403 Curl_set_command_prot(conn, save);
408 #endif /* HAVE_KRB4 */
409 #endif /* CURL_DISABLE_FTP */