krb4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* This source code was modified by Martin Hedenfalk <[email protected]> for
  2. * use in Curl. Martin's latest changes were done 2000-09-18.
  3. *
  4. * It has since been patched away like a madman by Daniel Stenberg to make it
  5. * better applied to curl conditions, and to make it not use globals, pollute
  6. * name space and more.
  7. *
  8. * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
  9. * (Royal Institute of Technology, Stockholm, Sweden).
  10. * Copyright (c) 2004 - 2008 Daniel Stenberg
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. *
  24. * 3. Neither the name of the Institute nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. *
  40. * $Id$
  41. */
  42. #include "setup.h"
  43. #ifndef CURL_DISABLE_FTP
  44. #ifdef HAVE_KRB4
  45. #include <stdlib.h>
  46. #ifdef HAVE_NETDB_H
  47. #include <netdb.h>
  48. #endif
  49. #include <string.h>
  50. #include <krb.h>
  51. #include <des.h>
  52. #ifdef HAVE_UNISTD_H
  53. #include <unistd.h> /* for getpid() */
  54. #endif
  55. #include "urldata.h"
  56. #include "curl_base64.h"
  57. #include "ftp.h"
  58. #include "sendf.h"
  59. #include "krb4.h"
  60. #include "memory.h"
  61. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  62. #include "inet_ntoa_r.h"
  63. #endif
  64. /* The last #include file should be: */
  65. #include "memdebug.h"
  66. #define LOCAL_ADDR (&conn->local_addr)
  67. #define REMOTE_ADDR conn->ip_addr->ai_addr
  68. #define myctladdr LOCAL_ADDR
  69. #define hisctladdr REMOTE_ADDR
  70. struct krb4_data {
  71. des_cblock key;
  72. des_key_schedule schedule;
  73. char name[ANAME_SZ];
  74. char instance[INST_SZ];
  75. char realm[REALM_SZ];
  76. };
  77. #ifndef HAVE_STRLCPY
  78. /* if it ever goes non-static, make it Curl_ prefixed! */
  79. static size_t
  80. strlcpy (char *dst, const char *src, size_t dst_sz)
  81. {
  82. size_t n;
  83. char *p;
  84. for (p = dst, n = 0;
  85. n + 1 < dst_sz && *src != '\0';
  86. ++p, ++src, ++n)
  87. *p = *src;
  88. *p = '\0';
  89. if(*src == '\0')
  90. return n;
  91. else
  92. return n + strlen (src);
  93. }
  94. #else
  95. size_t strlcpy (char *dst, const char *src, size_t dst_sz);
  96. #endif
  97. static int
  98. krb4_check_prot(void *app_data, int level)
  99. {
  100. app_data = NULL; /* prevent compiler warning */
  101. if(level == prot_confidential)
  102. return -1;
  103. return 0;
  104. }
  105. static int
  106. krb4_decode(void *app_data, void *buf, int len, int level,
  107. struct connectdata *conn)
  108. {
  109. MSG_DAT m;
  110. int e;
  111. struct krb4_data *d = app_data;
  112. if(level == prot_safe)
  113. e = krb_rd_safe(buf, len, &d->key,
  114. (struct sockaddr_in *)REMOTE_ADDR,
  115. (struct sockaddr_in *)LOCAL_ADDR, &m);
  116. else
  117. e = krb_rd_priv(buf, len, d->schedule, &d->key,
  118. (struct sockaddr_in *)REMOTE_ADDR,
  119. (struct sockaddr_in *)LOCAL_ADDR, &m);
  120. if(e) {
  121. struct SessionHandle *data = conn->data;
  122. infof(data, "krb4_decode: %s\n", krb_get_err_text(e));
  123. return -1;
  124. }
  125. memmove(buf, m.app_data, m.app_length);
  126. return m.app_length;
  127. }
  128. static int
  129. krb4_overhead(void *app_data, int level, int len)
  130. {
  131. /* no arguments are used, just init them to prevent compiler warnings */
  132. app_data = NULL;
  133. level = 0;
  134. len = 0;
  135. return 31;
  136. }
  137. static int
  138. krb4_encode(void *app_data, const void *from, int length, int level, void **to,
  139. struct connectdata *conn)
  140. {
  141. struct krb4_data *d = app_data;
  142. *to = malloc(length + 31);
  143. if(level == prot_safe)
  144. /* NOTE that the void* cast is safe, krb_mk_safe/priv don't modify the
  145. * input buffer
  146. */
  147. return krb_mk_safe((void*)from, *to, length, &d->key,
  148. (struct sockaddr_in *)LOCAL_ADDR,
  149. (struct sockaddr_in *)REMOTE_ADDR);
  150. else if(level == prot_private)
  151. return krb_mk_priv((void*)from, *to, length, d->schedule, &d->key,
  152. (struct sockaddr_in *)LOCAL_ADDR,
  153. (struct sockaddr_in *)REMOTE_ADDR);
  154. else
  155. return -1;
  156. }
  157. static int
  158. mk_auth(struct krb4_data *d, KTEXT adat,
  159. const char *service, char *host, int checksum)
  160. {
  161. int ret;
  162. CREDENTIALS cred;
  163. char sname[SNAME_SZ], inst[INST_SZ], realm[REALM_SZ];
  164. strlcpy(sname, service, sizeof(sname));
  165. strlcpy(inst, krb_get_phost(host), sizeof(inst));
  166. strlcpy(realm, krb_realmofhost(host), sizeof(realm));
  167. ret = krb_mk_req(adat, sname, inst, realm, checksum);
  168. if(ret)
  169. return ret;
  170. strlcpy(sname, service, sizeof(sname));
  171. strlcpy(inst, krb_get_phost(host), sizeof(inst));
  172. strlcpy(realm, krb_realmofhost(host), sizeof(realm));
  173. ret = krb_get_cred(sname, inst, realm, &cred);
  174. memmove(&d->key, &cred.session, sizeof(des_cblock));
  175. des_key_sched(&d->key, d->schedule);
  176. memset(&cred, 0, sizeof(cred));
  177. return ret;
  178. }
  179. #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
  180. int krb_get_our_ip_for_realm(char *, struct in_addr *);
  181. #endif
  182. static int
  183. krb4_auth(void *app_data, struct connectdata *conn)
  184. {
  185. int ret;
  186. char *p;
  187. unsigned char *ptr;
  188. size_t len;
  189. KTEXT_ST adat;
  190. MSG_DAT msg_data;
  191. int checksum;
  192. u_int32_t cs;
  193. struct krb4_data *d = app_data;
  194. char *host = conn->host.name;
  195. ssize_t nread;
  196. int l = sizeof(conn->local_addr);
  197. struct SessionHandle *data = conn->data;
  198. CURLcode result;
  199. if(getsockname(conn->sock[FIRSTSOCKET],
  200. (struct sockaddr *)LOCAL_ADDR, &l) < 0)
  201. perror("getsockname()");
  202. checksum = getpid();
  203. ret = mk_auth(d, &adat, "ftp", host, checksum);
  204. if(ret == KDC_PR_UNKNOWN)
  205. ret = mk_auth(d, &adat, "rcmd", host, checksum);
  206. if(ret) {
  207. infof(data, "%s\n", krb_get_err_text(ret));
  208. return AUTH_CONTINUE;
  209. }
  210. #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
  211. if(krb_get_config_bool("nat_in_use")) {
  212. struct sockaddr_in *localaddr = (struct sockaddr_in *)LOCAL_ADDR;
  213. struct in_addr natAddr;
  214. if(krb_get_our_ip_for_realm(krb_realmofhost(host),
  215. &natAddr) != KSUCCESS
  216. && krb_get_our_ip_for_realm(NULL, &natAddr) != KSUCCESS)
  217. infof(data, "Can't get address for realm %s\n",
  218. krb_realmofhost(host));
  219. else {
  220. if(natAddr.s_addr != localaddr->sin_addr.s_addr) {
  221. #ifdef HAVE_INET_NTOA_R
  222. char ntoa_buf[64];
  223. char *ip = (char *)inet_ntoa_r(natAddr, ntoa_buf, sizeof(ntoa_buf));
  224. #else
  225. char *ip = (char *)inet_ntoa(natAddr);
  226. #endif
  227. infof(data, "Using NAT IP address (%s) for kerberos 4\n", ip);
  228. localaddr->sin_addr = natAddr;
  229. }
  230. }
  231. }
  232. #endif
  233. if(Curl_base64_encode(conn->data, (char *)adat.dat, adat.length, &p) < 1) {
  234. Curl_failf(data, "Out of memory base64-encoding");
  235. return AUTH_CONTINUE;
  236. }
  237. result = Curl_ftpsendf(conn, "ADAT %s", p);
  238. free(p);
  239. if(result)
  240. return -2;
  241. if(Curl_GetFTPResponse(&nread, conn, NULL))
  242. return -1;
  243. if(data->state.buffer[0] != '2'){
  244. Curl_failf(data, "Server didn't accept auth data");
  245. return AUTH_ERROR;
  246. }
  247. p = strstr(data->state.buffer, "ADAT=");
  248. if(!p) {
  249. Curl_failf(data, "Remote host didn't send adat reply");
  250. return AUTH_ERROR;
  251. }
  252. p += 5;
  253. len = Curl_base64_decode(p, &ptr);
  254. if(len > sizeof(adat.dat)-1) {
  255. free(ptr);
  256. len=0;
  257. }
  258. if(!len || !ptr) {
  259. Curl_failf(data, "Failed to decode base64 from server");
  260. return AUTH_ERROR;
  261. }
  262. memcpy((char *)adat.dat, ptr, len);
  263. free(ptr);
  264. adat.length = len;
  265. ret = krb_rd_safe(adat.dat, adat.length, &d->key,
  266. (struct sockaddr_in *)hisctladdr,
  267. (struct sockaddr_in *)myctladdr, &msg_data);
  268. if(ret) {
  269. Curl_failf(data, "Error reading reply from server: %s",
  270. krb_get_err_text(ret));
  271. return AUTH_ERROR;
  272. }
  273. krb_get_int(msg_data.app_data, &cs, 4, 0);
  274. if(cs - checksum != 1) {
  275. Curl_failf(data, "Bad checksum returned from server");
  276. return AUTH_ERROR;
  277. }
  278. return AUTH_OK;
  279. }
  280. struct Curl_sec_client_mech Curl_krb4_client_mech = {
  281. "KERBEROS_V4",
  282. sizeof(struct krb4_data),
  283. NULL, /* init */
  284. krb4_auth,
  285. NULL, /* end */
  286. krb4_check_prot,
  287. krb4_overhead,
  288. krb4_encode,
  289. krb4_decode
  290. };
  291. CURLcode Curl_krb_kauth(struct connectdata *conn)
  292. {
  293. des_cblock key;
  294. des_key_schedule schedule;
  295. KTEXT_ST tkt, tktcopy;
  296. char *name;
  297. char *p;
  298. char passwd[100];
  299. size_t tmp;
  300. ssize_t nread;
  301. int save;
  302. CURLcode result;
  303. unsigned char *ptr;
  304. save = Curl_set_command_prot(conn, prot_private);
  305. result = Curl_ftpsendf(conn, "SITE KAUTH %s", conn->user);
  306. if(result)
  307. return result;
  308. result = Curl_GetFTPResponse(&nread, conn, NULL);
  309. if(result)
  310. return result;
  311. if(conn->data->state.buffer[0] != '3'){
  312. Curl_set_command_prot(conn, save);
  313. return CURLE_FTP_WEIRD_SERVER_REPLY;
  314. }
  315. p = strstr(conn->data->state.buffer, "T=");
  316. if(!p) {
  317. Curl_failf(conn->data, "Bad reply from server");
  318. Curl_set_command_prot(conn, save);
  319. return CURLE_FTP_WEIRD_SERVER_REPLY;
  320. }
  321. p += 2;
  322. tmp = Curl_base64_decode(p, &ptr);
  323. if(tmp >= sizeof(tkt.dat)) {
  324. free(ptr);
  325. tmp=0;
  326. }
  327. if(!tmp || !ptr) {
  328. Curl_failf(conn->data, "Failed to decode base64 in reply");
  329. Curl_set_command_prot(conn, save);
  330. return CURLE_FTP_WEIRD_SERVER_REPLY;
  331. }
  332. memcpy((char *)tkt.dat, ptr, tmp);
  333. free(ptr);
  334. tkt.length = tmp;
  335. tktcopy.length = tkt.length;
  336. p = strstr(conn->data->state.buffer, "P=");
  337. if(!p) {
  338. Curl_failf(conn->data, "Bad reply from server");
  339. Curl_set_command_prot(conn, save);
  340. return CURLE_FTP_WEIRD_SERVER_REPLY;
  341. }
  342. name = p + 2;
  343. for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
  344. *p = 0;
  345. des_string_to_key (conn->passwd, &key);
  346. des_key_sched(&key, schedule);
  347. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  348. tkt.length,
  349. schedule, &key, DES_DECRYPT);
  350. if(strcmp ((char*)tktcopy.dat + 8,
  351. KRB_TICKET_GRANTING_TICKET) != 0) {
  352. afs_string_to_key(passwd,
  353. krb_realmofhost(conn->host.name),
  354. &key);
  355. des_key_sched(&key, schedule);
  356. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  357. tkt.length,
  358. schedule, &key, DES_DECRYPT);
  359. }
  360. memset(key, 0, sizeof(key));
  361. memset(schedule, 0, sizeof(schedule));
  362. memset(passwd, 0, sizeof(passwd));
  363. if(Curl_base64_encode(conn->data, (char *)tktcopy.dat, tktcopy.length, &p)
  364. < 1) {
  365. failf(conn->data, "Out of memory base64-encoding.");
  366. Curl_set_command_prot(conn, save);
  367. return CURLE_OUT_OF_MEMORY;
  368. }
  369. memset (tktcopy.dat, 0, tktcopy.length);
  370. result = Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p);
  371. free(p);
  372. if(result)
  373. return result;
  374. result = Curl_GetFTPResponse(&nread, conn, NULL);
  375. if(result)
  376. return result;
  377. Curl_set_command_prot(conn, save);
  378. return CURLE_OK;
  379. }
  380. #endif /* HAVE_KRB4 */
  381. #endif /* CURL_DISABLE_FTP */