krb4.c 12 KB

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