krb4.c 11 KB

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