krb4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 - 2011 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. */
  41. #include "setup.h"
  42. #ifndef CURL_DISABLE_FTP
  43. #ifdef HAVE_KRB4
  44. #ifdef HAVE_NETDB_H
  45. #include <netdb.h>
  46. #endif
  47. #include <krb.h>
  48. #include <des.h>
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h> /* for getpid() */
  51. #endif
  52. #include "urldata.h"
  53. #include "curl_base64.h"
  54. #include "ftp.h"
  55. #include "sendf.h"
  56. #include "krb4.h"
  57. #include "inet_ntop.h"
  58. #include "curl_memory.h"
  59. /* The last #include file should be: */
  60. #include "memdebug.h"
  61. #define LOCAL_ADDR (&conn->local_addr)
  62. #define REMOTE_ADDR conn->ip_addr->ai_addr
  63. #define myctladdr LOCAL_ADDR
  64. #define hisctladdr REMOTE_ADDR
  65. struct krb4_data {
  66. des_cblock key;
  67. des_key_schedule schedule;
  68. char name[ANAME_SZ];
  69. char instance[INST_SZ];
  70. char realm[REALM_SZ];
  71. };
  72. #ifndef HAVE_STRLCPY
  73. /* if it ever goes non-static, make it Curl_ prefixed! */
  74. static size_t
  75. strlcpy (char *dst, const char *src, size_t dst_sz)
  76. {
  77. size_t n;
  78. char *p;
  79. for(p = dst, n = 0;
  80. n + 1 < dst_sz && *src != '\0';
  81. ++p, ++src, ++n)
  82. *p = *src;
  83. *p = '\0';
  84. if(*src == '\0')
  85. return n;
  86. else
  87. return n + strlen (src);
  88. }
  89. #else
  90. size_t strlcpy (char *dst, const char *src, size_t dst_sz);
  91. #endif
  92. static int
  93. krb4_check_prot(void *app_data, int level)
  94. {
  95. app_data = NULL; /* prevent compiler warning */
  96. if(level == PROT_CONFIDENTIAL)
  97. return -1;
  98. return 0;
  99. }
  100. static int
  101. krb4_decode(void *app_data, void *buf, int len, int level,
  102. struct connectdata *conn)
  103. {
  104. MSG_DAT m;
  105. int e;
  106. struct krb4_data *d = app_data;
  107. if(level == PROT_SAFE)
  108. e = krb_rd_safe(buf, len, &d->key,
  109. (struct sockaddr_in *)REMOTE_ADDR,
  110. (struct sockaddr_in *)LOCAL_ADDR, &m);
  111. else
  112. e = krb_rd_priv(buf, len, d->schedule, &d->key,
  113. (struct sockaddr_in *)REMOTE_ADDR,
  114. (struct sockaddr_in *)LOCAL_ADDR, &m);
  115. if(e) {
  116. struct SessionHandle *data = conn->data;
  117. infof(data, "krb4_decode: %s\n", krb_get_err_text(e));
  118. return -1;
  119. }
  120. memmove(buf, m.app_data, m.app_length);
  121. return m.app_length;
  122. }
  123. static int
  124. krb4_overhead(void *app_data, int level, int len)
  125. {
  126. /* no arguments are used, just init them to prevent compiler warnings */
  127. app_data = NULL;
  128. level = 0;
  129. len = 0;
  130. return 31;
  131. }
  132. static int
  133. krb4_encode(void *app_data, const void *from, int length, int level, void **to,
  134. struct connectdata *conn)
  135. {
  136. struct krb4_data *d = app_data;
  137. *to = malloc(length + 31);
  138. if(!*to)
  139. return -1;
  140. if(level == PROT_SAFE)
  141. /* NOTE that the void* cast is safe, krb_mk_safe/priv don't modify the
  142. * input buffer
  143. */
  144. return krb_mk_safe((void*)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((void*)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 = 0;
  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. size_t base64_sz = 0;
  197. if(getsockname(conn->sock[FIRSTSOCKET],
  198. (struct sockaddr *)LOCAL_ADDR, &l) < 0)
  199. perror("getsockname()");
  200. checksum = getpid();
  201. ret = mk_auth(d, &adat, "ftp", host, checksum);
  202. if(ret == KDC_PR_UNKNOWN)
  203. ret = mk_auth(d, &adat, "rcmd", host, checksum);
  204. if(ret) {
  205. infof(data, "%s\n", krb_get_err_text(ret));
  206. return AUTH_CONTINUE;
  207. }
  208. #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
  209. if(krb_get_config_bool("nat_in_use")) {
  210. struct sockaddr_in *localaddr = (struct sockaddr_in *)LOCAL_ADDR;
  211. struct in_addr natAddr;
  212. if(krb_get_our_ip_for_realm(krb_realmofhost(host),
  213. &natAddr) != KSUCCESS
  214. && krb_get_our_ip_for_realm(NULL, &natAddr) != KSUCCESS)
  215. infof(data, "Can't get address for realm %s\n",
  216. krb_realmofhost(host));
  217. else {
  218. if(natAddr.s_addr != localaddr->sin_addr.s_addr) {
  219. char addr_buf[128];
  220. if(Curl_inet_ntop(AF_INET, natAddr, addr_buf, sizeof(addr_buf)))
  221. infof(data, "Using NAT IP address (%s) for kerberos 4\n", addr_buf);
  222. localaddr->sin_addr = natAddr;
  223. }
  224. }
  225. }
  226. #endif
  227. result = Curl_base64_encode(conn->data, (char *)adat.dat, adat.length,
  228. &p, &base64_sz);
  229. if(result) {
  230. Curl_failf(data, "base64-encoding: %s", curl_easy_strerror(result));
  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. result = Curl_base64_decode(p, &ptr, &len);
  250. if(result) {
  251. Curl_failf(data, "base64-decoding: %s", curl_easy_strerror(result));
  252. return AUTH_ERROR;
  253. }
  254. if(len > sizeof(adat.dat)-1) {
  255. free(ptr);
  256. ptr = NULL;
  257. len = 0;
  258. }
  259. if(!len || !ptr) {
  260. Curl_failf(data, "Failed to decode base64 from server");
  261. return AUTH_ERROR;
  262. }
  263. memcpy((char *)adat.dat, ptr, len);
  264. free(ptr);
  265. adat.length = len;
  266. ret = krb_rd_safe(adat.dat, adat.length, &d->key,
  267. (struct sockaddr_in *)hisctladdr,
  268. (struct sockaddr_in *)myctladdr, &msg_data);
  269. if(ret) {
  270. Curl_failf(data, "Error reading reply from server: %s",
  271. krb_get_err_text(ret));
  272. return AUTH_ERROR;
  273. }
  274. krb_get_int(msg_data.app_data, &cs, 4, 0);
  275. if(cs - checksum != 1) {
  276. Curl_failf(data, "Bad checksum returned from server");
  277. return AUTH_ERROR;
  278. }
  279. return AUTH_OK;
  280. }
  281. struct Curl_sec_client_mech Curl_krb4_client_mech = {
  282. "KERBEROS_V4",
  283. sizeof(struct krb4_data),
  284. NULL, /* init */
  285. krb4_auth,
  286. NULL, /* end */
  287. krb4_check_prot,
  288. krb4_overhead,
  289. krb4_encode,
  290. krb4_decode
  291. };
  292. static enum protection_level
  293. krb4_set_command_prot(struct connectdata *conn, enum protection_level level)
  294. {
  295. enum protection_level old = conn->command_prot;
  296. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  297. conn->command_prot = level;
  298. return old;
  299. }
  300. CURLcode Curl_krb_kauth(struct connectdata *conn)
  301. {
  302. des_cblock key;
  303. des_key_schedule schedule;
  304. KTEXT_ST tkt, tktcopy;
  305. char *name;
  306. char *p;
  307. char passwd[100];
  308. size_t tmp = 0;
  309. ssize_t nread;
  310. enum protection_level save;
  311. CURLcode result;
  312. unsigned char *ptr;
  313. size_t base64_sz = 0;
  314. save = krb4_set_command_prot(conn, PROT_PRIVATE);
  315. result = Curl_ftpsendf(conn, "SITE KAUTH %s", conn->user);
  316. if(result)
  317. return result;
  318. result = Curl_GetFTPResponse(&nread, conn, NULL);
  319. if(result)
  320. return result;
  321. if(conn->data->state.buffer[0] != '3') {
  322. krb4_set_command_prot(conn, save);
  323. return CURLE_FTP_WEIRD_SERVER_REPLY;
  324. }
  325. p = strstr(conn->data->state.buffer, "T=");
  326. if(!p) {
  327. Curl_failf(conn->data, "Bad reply from server");
  328. krb4_set_command_prot(conn, save);
  329. return CURLE_FTP_WEIRD_SERVER_REPLY;
  330. }
  331. p += 2;
  332. result = Curl_base64_decode(p, &ptr, &tmp);
  333. if(result) {
  334. Curl_failf(conn->data, "base64-decoding: %s", curl_easy_strerror(result));
  335. return result;
  336. }
  337. if(tmp >= sizeof(tkt.dat)) {
  338. free(ptr);
  339. ptr = NULL;
  340. tmp = 0;
  341. }
  342. if(!tmp || !ptr) {
  343. Curl_failf(conn->data, "Failed to decode base64 in reply");
  344. krb4_set_command_prot(conn, save);
  345. return CURLE_FTP_WEIRD_SERVER_REPLY;
  346. }
  347. memcpy((char *)tkt.dat, ptr, tmp);
  348. free(ptr);
  349. tkt.length = tmp;
  350. tktcopy.length = tkt.length;
  351. p = strstr(conn->data->state.buffer, "P=");
  352. if(!p) {
  353. Curl_failf(conn->data, "Bad reply from server");
  354. krb4_set_command_prot(conn, save);
  355. return CURLE_FTP_WEIRD_SERVER_REPLY;
  356. }
  357. name = p + 2;
  358. for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
  359. *p = 0;
  360. des_string_to_key (conn->passwd, &key);
  361. des_key_sched(&key, schedule);
  362. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  363. tkt.length,
  364. schedule, &key, DES_DECRYPT);
  365. if(strcmp ((char*)tktcopy.dat + 8,
  366. KRB_TICKET_GRANTING_TICKET) != 0) {
  367. afs_string_to_key(passwd,
  368. krb_realmofhost(conn->host.name),
  369. &key);
  370. des_key_sched(&key, schedule);
  371. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  372. tkt.length,
  373. schedule, &key, DES_DECRYPT);
  374. }
  375. memset(key, 0, sizeof(key));
  376. memset(schedule, 0, sizeof(schedule));
  377. memset(passwd, 0, sizeof(passwd));
  378. result = Curl_base64_encode(conn->data, (char *)tktcopy.dat, tktcopy.length,
  379. &p, &base64_sz);
  380. if(result) {
  381. Curl_failf(conn->data, "base64-encoding: %s", curl_easy_strerror(result));
  382. krb4_set_command_prot(conn, save);
  383. return result;
  384. }
  385. memset (tktcopy.dat, 0, tktcopy.length);
  386. result = Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p);
  387. free(p);
  388. if(result)
  389. return result;
  390. result = Curl_GetFTPResponse(&nread, conn, NULL);
  391. if(result)
  392. return result;
  393. krb4_set_command_prot(conn, save);
  394. return CURLE_OK;
  395. }
  396. #endif /* HAVE_KRB4 */
  397. #endif /* CURL_DISABLE_FTP */