krb4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 - 2009 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 "inet_ntop.h"
  61. #include "curl_memory.h"
  62. /* The last #include file should be: */
  63. #include "memdebug.h"
  64. #define LOCAL_ADDR (&conn->local_addr)
  65. #define REMOTE_ADDR conn->ip_addr->ai_addr
  66. #define myctladdr LOCAL_ADDR
  67. #define hisctladdr REMOTE_ADDR
  68. struct krb4_data {
  69. des_cblock key;
  70. des_key_schedule schedule;
  71. char name[ANAME_SZ];
  72. char instance[INST_SZ];
  73. char realm[REALM_SZ];
  74. };
  75. #ifndef HAVE_STRLCPY
  76. /* if it ever goes non-static, make it Curl_ prefixed! */
  77. static size_t
  78. strlcpy (char *dst, const char *src, size_t dst_sz)
  79. {
  80. size_t n;
  81. char *p;
  82. for (p = dst, n = 0;
  83. n + 1 < dst_sz && *src != '\0';
  84. ++p, ++src, ++n)
  85. *p = *src;
  86. *p = '\0';
  87. if(*src == '\0')
  88. return n;
  89. else
  90. return n + strlen (src);
  91. }
  92. #else
  93. size_t strlcpy (char *dst, const char *src, size_t dst_sz);
  94. #endif
  95. static int
  96. krb4_check_prot(void *app_data, int level)
  97. {
  98. app_data = NULL; /* prevent compiler warning */
  99. if(level == prot_confidential)
  100. return -1;
  101. return 0;
  102. }
  103. static int
  104. krb4_decode(void *app_data, void *buf, int len, int level,
  105. struct connectdata *conn)
  106. {
  107. MSG_DAT m;
  108. int e;
  109. struct krb4_data *d = app_data;
  110. if(level == prot_safe)
  111. e = krb_rd_safe(buf, len, &d->key,
  112. (struct sockaddr_in *)REMOTE_ADDR,
  113. (struct sockaddr_in *)LOCAL_ADDR, &m);
  114. else
  115. e = krb_rd_priv(buf, len, d->schedule, &d->key,
  116. (struct sockaddr_in *)REMOTE_ADDR,
  117. (struct sockaddr_in *)LOCAL_ADDR, &m);
  118. if(e) {
  119. struct SessionHandle *data = conn->data;
  120. infof(data, "krb4_decode: %s\n", krb_get_err_text(e));
  121. return -1;
  122. }
  123. memmove(buf, m.app_data, m.app_length);
  124. return m.app_length;
  125. }
  126. static int
  127. krb4_overhead(void *app_data, int level, int len)
  128. {
  129. /* no arguments are used, just init them to prevent compiler warnings */
  130. app_data = NULL;
  131. level = 0;
  132. len = 0;
  133. return 31;
  134. }
  135. static int
  136. krb4_encode(void *app_data, const void *from, int length, int level, void **to,
  137. struct connectdata *conn)
  138. {
  139. struct krb4_data *d = app_data;
  140. *to = malloc(length + 31);
  141. if(!*to)
  142. return -1;
  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. char addr_buf[128];
  222. if(Curl_inet_ntop(AF_INET, natAddr, addr_buf, sizeof(addr_buf)))
  223. infof(data, "Using NAT IP address (%s) for kerberos 4\n", addr_buf);
  224. localaddr->sin_addr = natAddr;
  225. }
  226. }
  227. }
  228. #endif
  229. if(Curl_base64_encode(conn->data, (char *)adat.dat, adat.length, &p) < 1) {
  230. Curl_failf(data, "Out of memory base64-encoding");
  231. return AUTH_CONTINUE;
  232. }
  233. result = Curl_ftpsendf(conn, "ADAT %s", p);
  234. free(p);
  235. if(result)
  236. return -2;
  237. if(Curl_GetFTPResponse(&nread, conn, NULL))
  238. return -1;
  239. if(data->state.buffer[0] != '2'){
  240. Curl_failf(data, "Server didn't accept auth data");
  241. return AUTH_ERROR;
  242. }
  243. p = strstr(data->state.buffer, "ADAT=");
  244. if(!p) {
  245. Curl_failf(data, "Remote host didn't send adat reply");
  246. return AUTH_ERROR;
  247. }
  248. p += 5;
  249. len = Curl_base64_decode(p, &ptr);
  250. if(len > sizeof(adat.dat)-1) {
  251. free(ptr);
  252. len=0;
  253. }
  254. if(!len || !ptr) {
  255. Curl_failf(data, "Failed to decode base64 from server");
  256. return AUTH_ERROR;
  257. }
  258. memcpy((char *)adat.dat, ptr, len);
  259. free(ptr);
  260. adat.length = len;
  261. ret = krb_rd_safe(adat.dat, adat.length, &d->key,
  262. (struct sockaddr_in *)hisctladdr,
  263. (struct sockaddr_in *)myctladdr, &msg_data);
  264. if(ret) {
  265. Curl_failf(data, "Error reading reply from server: %s",
  266. krb_get_err_text(ret));
  267. return AUTH_ERROR;
  268. }
  269. krb_get_int(msg_data.app_data, &cs, 4, 0);
  270. if(cs - checksum != 1) {
  271. Curl_failf(data, "Bad checksum returned from server");
  272. return AUTH_ERROR;
  273. }
  274. return AUTH_OK;
  275. }
  276. struct Curl_sec_client_mech Curl_krb4_client_mech = {
  277. "KERBEROS_V4",
  278. sizeof(struct krb4_data),
  279. NULL, /* init */
  280. krb4_auth,
  281. NULL, /* end */
  282. krb4_check_prot,
  283. krb4_overhead,
  284. krb4_encode,
  285. krb4_decode
  286. };
  287. CURLcode Curl_krb_kauth(struct connectdata *conn)
  288. {
  289. des_cblock key;
  290. des_key_schedule schedule;
  291. KTEXT_ST tkt, tktcopy;
  292. char *name;
  293. char *p;
  294. char passwd[100];
  295. size_t tmp;
  296. ssize_t nread;
  297. int save;
  298. CURLcode result;
  299. unsigned char *ptr;
  300. save = Curl_set_command_prot(conn, prot_private);
  301. result = Curl_ftpsendf(conn, "SITE KAUTH %s", conn->user);
  302. if(result)
  303. return result;
  304. result = Curl_GetFTPResponse(&nread, conn, NULL);
  305. if(result)
  306. return result;
  307. if(conn->data->state.buffer[0] != '3'){
  308. Curl_set_command_prot(conn, save);
  309. return CURLE_FTP_WEIRD_SERVER_REPLY;
  310. }
  311. p = strstr(conn->data->state.buffer, "T=");
  312. if(!p) {
  313. Curl_failf(conn->data, "Bad reply from server");
  314. Curl_set_command_prot(conn, save);
  315. return CURLE_FTP_WEIRD_SERVER_REPLY;
  316. }
  317. p += 2;
  318. tmp = Curl_base64_decode(p, &ptr);
  319. if(tmp >= sizeof(tkt.dat)) {
  320. free(ptr);
  321. tmp=0;
  322. }
  323. if(!tmp || !ptr) {
  324. Curl_failf(conn->data, "Failed to decode base64 in reply");
  325. Curl_set_command_prot(conn, save);
  326. return CURLE_FTP_WEIRD_SERVER_REPLY;
  327. }
  328. memcpy((char *)tkt.dat, ptr, tmp);
  329. free(ptr);
  330. tkt.length = tmp;
  331. tktcopy.length = tkt.length;
  332. p = strstr(conn->data->state.buffer, "P=");
  333. if(!p) {
  334. Curl_failf(conn->data, "Bad reply from server");
  335. Curl_set_command_prot(conn, save);
  336. return CURLE_FTP_WEIRD_SERVER_REPLY;
  337. }
  338. name = p + 2;
  339. for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
  340. *p = 0;
  341. des_string_to_key (conn->passwd, &key);
  342. des_key_sched(&key, schedule);
  343. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  344. tkt.length,
  345. schedule, &key, DES_DECRYPT);
  346. if(strcmp ((char*)tktcopy.dat + 8,
  347. KRB_TICKET_GRANTING_TICKET) != 0) {
  348. afs_string_to_key(passwd,
  349. krb_realmofhost(conn->host.name),
  350. &key);
  351. des_key_sched(&key, schedule);
  352. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  353. tkt.length,
  354. schedule, &key, DES_DECRYPT);
  355. }
  356. memset(key, 0, sizeof(key));
  357. memset(schedule, 0, sizeof(schedule));
  358. memset(passwd, 0, sizeof(passwd));
  359. if(Curl_base64_encode(conn->data, (char *)tktcopy.dat, tktcopy.length, &p)
  360. < 1) {
  361. failf(conn->data, "Out of memory base64-encoding.");
  362. Curl_set_command_prot(conn, save);
  363. return CURLE_OUT_OF_MEMORY;
  364. }
  365. memset (tktcopy.dat, 0, tktcopy.length);
  366. result = Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p);
  367. free(p);
  368. if(result)
  369. return result;
  370. result = Curl_GetFTPResponse(&nread, conn, NULL);
  371. if(result)
  372. return result;
  373. Curl_set_command_prot(conn, save);
  374. return CURLE_OK;
  375. }
  376. #endif /* HAVE_KRB4 */
  377. #endif /* CURL_DISABLE_FTP */