krb4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* This source code was modified by Martin Hedenfalk <mhe@stacken.kth.se> 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 - 2007 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 "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, 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. return krb_mk_safe(from, *to, length, &d->key,
  145. (struct sockaddr_in *)LOCAL_ADDR,
  146. (struct sockaddr_in *)REMOTE_ADDR);
  147. else if(level == prot_private)
  148. return krb_mk_priv(from, *to, length, d->schedule, &d->key,
  149. (struct sockaddr_in *)LOCAL_ADDR,
  150. (struct sockaddr_in *)REMOTE_ADDR);
  151. else
  152. return -1;
  153. }
  154. static int
  155. mk_auth(struct krb4_data *d, KTEXT adat,
  156. const char *service, char *host, int checksum)
  157. {
  158. int ret;
  159. CREDENTIALS cred;
  160. char sname[SNAME_SZ], inst[INST_SZ], realm[REALM_SZ];
  161. strlcpy(sname, service, sizeof(sname));
  162. strlcpy(inst, krb_get_phost(host), sizeof(inst));
  163. strlcpy(realm, krb_realmofhost(host), sizeof(realm));
  164. ret = krb_mk_req(adat, sname, inst, realm, checksum);
  165. if(ret)
  166. return ret;
  167. strlcpy(sname, service, sizeof(sname));
  168. strlcpy(inst, krb_get_phost(host), sizeof(inst));
  169. strlcpy(realm, krb_realmofhost(host), sizeof(realm));
  170. ret = krb_get_cred(sname, inst, realm, &cred);
  171. memmove(&d->key, &cred.session, sizeof(des_cblock));
  172. des_key_sched(&d->key, d->schedule);
  173. memset(&cred, 0, sizeof(cred));
  174. return ret;
  175. }
  176. #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
  177. int krb_get_our_ip_for_realm(char *, struct in_addr *);
  178. #endif
  179. static int
  180. krb4_auth(void *app_data, struct connectdata *conn)
  181. {
  182. int ret;
  183. char *p;
  184. unsigned char *ptr;
  185. size_t len;
  186. KTEXT_ST adat;
  187. MSG_DAT msg_data;
  188. int checksum;
  189. u_int32_t cs;
  190. struct krb4_data *d = app_data;
  191. char *host = conn->host.name;
  192. ssize_t nread;
  193. int l = sizeof(conn->local_addr);
  194. struct SessionHandle *data = conn->data;
  195. CURLcode result;
  196. if(getsockname(conn->sock[FIRSTSOCKET],
  197. (struct sockaddr *)LOCAL_ADDR, &l) < 0)
  198. perror("getsockname()");
  199. checksum = getpid();
  200. ret = mk_auth(d, &adat, "ftp", host, checksum);
  201. if(ret == KDC_PR_UNKNOWN)
  202. ret = mk_auth(d, &adat, "rcmd", host, checksum);
  203. if(ret) {
  204. infof(data, "%s\n", krb_get_err_text(ret));
  205. return AUTH_CONTINUE;
  206. }
  207. #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
  208. if (krb_get_config_bool("nat_in_use")) {
  209. struct sockaddr_in *localaddr = (struct sockaddr_in *)LOCAL_ADDR;
  210. struct in_addr natAddr;
  211. if (krb_get_our_ip_for_realm(krb_realmofhost(host),
  212. &natAddr) != KSUCCESS
  213. && krb_get_our_ip_for_realm(NULL, &natAddr) != KSUCCESS)
  214. infof(data, "Can't get address for realm %s\n",
  215. krb_realmofhost(host));
  216. else {
  217. if (natAddr.s_addr != localaddr->sin_addr.s_addr) {
  218. #ifdef HAVE_INET_NTOA_R
  219. char ntoa_buf[64];
  220. char *ip = (char *)inet_ntoa_r(natAddr, ntoa_buf, sizeof(ntoa_buf));
  221. #else
  222. char *ip = (char *)inet_ntoa(natAddr);
  223. #endif
  224. infof(data, "Using NAT IP address (%s) for kerberos 4\n", ip);
  225. localaddr->sin_addr = natAddr;
  226. }
  227. }
  228. }
  229. #endif
  230. if(Curl_base64_encode(conn->data, (char *)adat.dat, adat.length, &p) < 1) {
  231. Curl_failf(data, "Out of memory base64-encoding");
  232. return AUTH_CONTINUE;
  233. }
  234. result = Curl_ftpsendf(conn, "ADAT %s", p);
  235. free(p);
  236. if(result)
  237. return -2;
  238. if(Curl_GetFTPResponse(&nread, conn, NULL))
  239. return -1;
  240. if(data->state.buffer[0] != '2'){
  241. Curl_failf(data, "Server didn't accept auth data");
  242. return AUTH_ERROR;
  243. }
  244. p = strstr(data->state.buffer, "ADAT=");
  245. if(!p) {
  246. Curl_failf(data, "Remote host didn't send adat reply");
  247. return AUTH_ERROR;
  248. }
  249. p += 5;
  250. len = Curl_base64_decode(p, &ptr);
  251. if(len > sizeof(adat.dat)-1) {
  252. free(ptr);
  253. len=0;
  254. }
  255. if(!len || !ptr) {
  256. Curl_failf(data, "Failed to decode base64 from server");
  257. return AUTH_ERROR;
  258. }
  259. memcpy((char *)adat.dat, ptr, len);
  260. free(ptr);
  261. adat.length = len;
  262. ret = krb_rd_safe(adat.dat, adat.length, &d->key,
  263. (struct sockaddr_in *)hisctladdr,
  264. (struct sockaddr_in *)myctladdr, &msg_data);
  265. if(ret) {
  266. Curl_failf(data, "Error reading reply from server: %s",
  267. krb_get_err_text(ret));
  268. return AUTH_ERROR;
  269. }
  270. krb_get_int(msg_data.app_data, &cs, 4, 0);
  271. if(cs - checksum != 1) {
  272. Curl_failf(data, "Bad checksum returned from server");
  273. return AUTH_ERROR;
  274. }
  275. return AUTH_OK;
  276. }
  277. struct Curl_sec_client_mech Curl_krb4_client_mech = {
  278. "KERBEROS_V4",
  279. sizeof(struct krb4_data),
  280. NULL, /* init */
  281. krb4_auth,
  282. NULL, /* end */
  283. krb4_check_prot,
  284. krb4_overhead,
  285. krb4_encode,
  286. krb4_decode
  287. };
  288. CURLcode Curl_krb_kauth(struct connectdata *conn)
  289. {
  290. des_cblock key;
  291. des_key_schedule schedule;
  292. KTEXT_ST tkt, tktcopy;
  293. char *name;
  294. char *p;
  295. char passwd[100];
  296. size_t tmp;
  297. ssize_t nread;
  298. int save;
  299. CURLcode result;
  300. unsigned char *ptr;
  301. save = Curl_set_command_prot(conn, prot_private);
  302. result = Curl_ftpsendf(conn, "SITE KAUTH %s", conn->user);
  303. if(result)
  304. return result;
  305. result = Curl_GetFTPResponse(&nread, conn, NULL);
  306. if(result)
  307. return result;
  308. if(conn->data->state.buffer[0] != '3'){
  309. Curl_set_command_prot(conn, save);
  310. return CURLE_FTP_WEIRD_SERVER_REPLY;
  311. }
  312. p = strstr(conn->data->state.buffer, "T=");
  313. if(!p) {
  314. Curl_failf(conn->data, "Bad reply from server");
  315. Curl_set_command_prot(conn, save);
  316. return CURLE_FTP_WEIRD_SERVER_REPLY;
  317. }
  318. p += 2;
  319. tmp = Curl_base64_decode(p, &ptr);
  320. if(tmp >= sizeof(tkt.dat)) {
  321. free(ptr);
  322. tmp=0;
  323. }
  324. if(!tmp || !ptr) {
  325. Curl_failf(conn->data, "Failed to decode base64 in reply.\n");
  326. Curl_set_command_prot(conn, save);
  327. return CURLE_FTP_WEIRD_SERVER_REPLY;
  328. }
  329. memcpy((char *)tkt.dat, ptr, tmp);
  330. free(ptr);
  331. tkt.length = tmp;
  332. tktcopy.length = tkt.length;
  333. p = strstr(conn->data->state.buffer, "P=");
  334. if(!p) {
  335. Curl_failf(conn->data, "Bad reply from server");
  336. Curl_set_command_prot(conn, save);
  337. return CURLE_FTP_WEIRD_SERVER_REPLY;
  338. }
  339. name = p + 2;
  340. for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
  341. *p = 0;
  342. des_string_to_key (conn->passwd, &key);
  343. des_key_sched(&key, schedule);
  344. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  345. tkt.length,
  346. schedule, &key, DES_DECRYPT);
  347. if (strcmp ((char*)tktcopy.dat + 8,
  348. KRB_TICKET_GRANTING_TICKET) != 0) {
  349. afs_string_to_key(passwd,
  350. krb_realmofhost(conn->host.name),
  351. &key);
  352. des_key_sched(&key, schedule);
  353. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  354. tkt.length,
  355. schedule, &key, DES_DECRYPT);
  356. }
  357. memset(key, 0, sizeof(key));
  358. memset(schedule, 0, sizeof(schedule));
  359. memset(passwd, 0, sizeof(passwd));
  360. if(Curl_base64_encode(conn->data, (char *)tktcopy.dat, tktcopy.length, &p)
  361. < 1) {
  362. failf(conn->data, "Out of memory base64-encoding.");
  363. Curl_set_command_prot(conn, save);
  364. return CURLE_OUT_OF_MEMORY;
  365. }
  366. memset (tktcopy.dat, 0, tktcopy.length);
  367. result = Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p);
  368. free(p);
  369. if(result)
  370. return result;
  371. result = Curl_GetFTPResponse(&nread, conn, NULL);
  372. if(result)
  373. return result;
  374. Curl_set_command_prot(conn, save);
  375. return CURLE_OK;
  376. }
  377. #endif /* HAVE_KRB4 */
  378. #endif /* CURL_DISABLE_FTP */