krb4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* This source code was modified by Martin Hedenfalk <mhe@stacken.kth.se> 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. * <daniel@haxx.se> 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 HAVE_KRB4
  43. #include "security.h"
  44. #include "base64.h"
  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 "ftp.h"
  56. #include "sendf.h"
  57. #include "krb4.h"
  58. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  59. #include "inet_ntoa_r.h"
  60. #endif
  61. /* The last #include file should be: */
  62. #ifdef CURLDEBUG
  63. #include "memdebug.h"
  64. #endif
  65. #define LOCAL_ADDR (&conn->local_addr)
  66. #define REMOTE_ADDR (&conn->serv_addr)
  67. #define myctladdr LOCAL_ADDR
  68. #define hisctladdr REMOTE_ADDR
  69. struct krb4_data {
  70. des_cblock key;
  71. des_key_schedule schedule;
  72. char name[ANAME_SZ];
  73. char instance[INST_SZ];
  74. char realm[REALM_SZ];
  75. };
  76. #ifndef HAVE_STRLCPY
  77. /* if it ever goes non-static, make it Curl_ prefixed! */
  78. static size_t
  79. strlcpy (char *dst, const char *src, size_t dst_sz)
  80. {
  81. size_t n;
  82. char *p;
  83. for (p = dst, n = 0;
  84. n + 1 < dst_sz && *src != '\0';
  85. ++p, ++src, ++n)
  86. *p = *src;
  87. *p = '\0';
  88. if (*src == '\0')
  89. return n;
  90. else
  91. return n + strlen (src);
  92. }
  93. #else
  94. size_t strlcpy (char *dst, const char *src, size_t dst_sz);
  95. #endif
  96. static int
  97. krb4_check_prot(void *app_data, int level)
  98. {
  99. app_data = NULL; /* prevent compiler warning */
  100. if(level == prot_confidential)
  101. return -1;
  102. return 0;
  103. }
  104. static int
  105. krb4_decode(void *app_data, void *buf, int len, int level,
  106. struct connectdata *conn)
  107. {
  108. MSG_DAT m;
  109. int e;
  110. struct krb4_data *d = app_data;
  111. if(level == prot_safe)
  112. e = krb_rd_safe(buf, len, &d->key,
  113. (struct sockaddr_in *)REMOTE_ADDR,
  114. (struct sockaddr_in *)LOCAL_ADDR, &m);
  115. else
  116. e = krb_rd_priv(buf, len, d->schedule, &d->key,
  117. (struct sockaddr_in *)REMOTE_ADDR,
  118. (struct sockaddr_in *)LOCAL_ADDR, &m);
  119. if(e) {
  120. struct SessionHandle *data = conn->data;
  121. infof(data, "krb4_decode: %s\n", krb_get_err_text(e));
  122. return -1;
  123. }
  124. memmove(buf, m.app_data, m.app_length);
  125. return m.app_length;
  126. }
  127. static int
  128. krb4_overhead(void *app_data, int level, int len)
  129. {
  130. /* no arguments are used, just init them to prevent compiler warnings */
  131. app_data = NULL;
  132. level = 0;
  133. len = 0;
  134. return 31;
  135. }
  136. static int
  137. krb4_encode(void *app_data, void *from, int length, int level, void **to,
  138. struct connectdata *conn)
  139. {
  140. struct krb4_data *d = app_data;
  141. *to = malloc(length + 31);
  142. if(level == prot_safe)
  143. return krb_mk_safe(from, *to, length, &d->key,
  144. (struct sockaddr_in *)LOCAL_ADDR,
  145. (struct sockaddr_in *)REMOTE_ADDR);
  146. else if(level == prot_private)
  147. return krb_mk_priv(from, *to, length, d->schedule, &d->key,
  148. (struct sockaddr_in *)LOCAL_ADDR,
  149. (struct sockaddr_in *)REMOTE_ADDR);
  150. else
  151. return -1;
  152. }
  153. static int
  154. mk_auth(struct krb4_data *d, KTEXT adat,
  155. const char *service, char *host, int checksum)
  156. {
  157. int ret;
  158. CREDENTIALS cred;
  159. char sname[SNAME_SZ], inst[INST_SZ], realm[REALM_SZ];
  160. strlcpy(sname, service, sizeof(sname));
  161. strlcpy(inst, krb_get_phost(host), sizeof(inst));
  162. strlcpy(realm, krb_realmofhost(host), sizeof(realm));
  163. ret = krb_mk_req(adat, sname, inst, realm, checksum);
  164. if(ret)
  165. return ret;
  166. strlcpy(sname, service, sizeof(sname));
  167. strlcpy(inst, krb_get_phost(host), sizeof(inst));
  168. strlcpy(realm, krb_realmofhost(host), sizeof(realm));
  169. ret = krb_get_cred(sname, inst, realm, &cred);
  170. memmove(&d->key, &cred.session, sizeof(des_cblock));
  171. des_key_sched(&d->key, d->schedule);
  172. memset(&cred, 0, sizeof(cred));
  173. return ret;
  174. }
  175. #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
  176. int krb_get_our_ip_for_realm(char *, struct in_addr *);
  177. #endif
  178. static int
  179. krb4_auth(void *app_data, struct connectdata *conn)
  180. {
  181. int ret;
  182. char *p;
  183. int len;
  184. KTEXT_ST adat;
  185. MSG_DAT msg_data;
  186. int checksum;
  187. u_int32_t cs;
  188. struct krb4_data *d = app_data;
  189. char *host = conn->hostname;
  190. ssize_t nread;
  191. int l = sizeof(conn->local_addr);
  192. struct SessionHandle *data = conn->data;
  193. CURLcode result;
  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. Curl_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. Curl_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. #ifdef HAVE_INET_NTOA_R
  217. char ntoa_buf[64];
  218. char *ip = (char *)inet_ntoa_r(natAddr, ntoa_buf, sizeof(ntoa_buf));
  219. #else
  220. char *ip = (char *)inet_ntoa(natAddr);
  221. #endif
  222. Curl_infof(data, "Using NAT IP address (%s) for kerberos 4\n", ip);
  223. localaddr->sin_addr = natAddr;
  224. }
  225. }
  226. }
  227. #endif
  228. if(Curl_base64_encode((char *)adat.dat, adat.length, &p) < 1) {
  229. Curl_failf(data, "Out of memory base64-encoding");
  230. return AUTH_CONTINUE;
  231. }
  232. result = Curl_ftpsendf(conn, "ADAT %s", p);
  233. free(p);
  234. if(result)
  235. return -2;
  236. if(Curl_GetFTPResponse(&nread, conn, NULL))
  237. return -1;
  238. if(data->state.buffer[0] != '2'){
  239. Curl_failf(data, "Server didn't accept auth data");
  240. return AUTH_ERROR;
  241. }
  242. p = strstr(data->state.buffer, "ADAT=");
  243. if(!p) {
  244. Curl_failf(data, "Remote host didn't send adat reply");
  245. return AUTH_ERROR;
  246. }
  247. p += 5;
  248. len = Curl_base64_decode(p, (char *)adat.dat);
  249. if(len < 0) {
  250. Curl_failf(data, "Failed to decode base64 from server");
  251. return AUTH_ERROR;
  252. }
  253. adat.length = len;
  254. ret = krb_rd_safe(adat.dat, adat.length, &d->key,
  255. (struct sockaddr_in *)hisctladdr,
  256. (struct sockaddr_in *)myctladdr, &msg_data);
  257. if(ret) {
  258. Curl_failf(data, "Error reading reply from server: %s",
  259. krb_get_err_text(ret));
  260. return AUTH_ERROR;
  261. }
  262. krb_get_int(msg_data.app_data, &cs, 4, 0);
  263. if(cs - checksum != 1) {
  264. Curl_failf(data, "Bad checksum returned from server");
  265. return AUTH_ERROR;
  266. }
  267. return AUTH_OK;
  268. }
  269. struct Curl_sec_client_mech Curl_krb4_client_mech = {
  270. "KERBEROS_V4",
  271. sizeof(struct krb4_data),
  272. NULL, /* init */
  273. krb4_auth,
  274. NULL, /* end */
  275. krb4_check_prot,
  276. krb4_overhead,
  277. krb4_encode,
  278. krb4_decode
  279. };
  280. CURLcode Curl_krb_kauth(struct connectdata *conn)
  281. {
  282. des_cblock key;
  283. des_key_schedule schedule;
  284. KTEXT_ST tkt, tktcopy;
  285. char *name;
  286. char *p;
  287. char passwd[100];
  288. int tmp;
  289. ssize_t nread;
  290. int save;
  291. CURLcode result;
  292. save = Curl_set_command_prot(conn, prot_private);
  293. result = Curl_ftpsendf(conn, "SITE KAUTH %s", conn->user);
  294. if(result)
  295. return result;
  296. result = Curl_GetFTPResponse(&nread, conn, NULL);
  297. if(result)
  298. return result;
  299. if(conn->data->state.buffer[0] != '3'){
  300. Curl_set_command_prot(conn, save);
  301. return CURLE_FTP_WEIRD_SERVER_REPLY;
  302. }
  303. p = strstr(conn->data->state.buffer, "T=");
  304. if(!p) {
  305. Curl_failf(conn->data, "Bad reply from server");
  306. Curl_set_command_prot(conn, save);
  307. return CURLE_FTP_WEIRD_SERVER_REPLY;
  308. }
  309. p += 2;
  310. tmp = Curl_base64_decode(p, (char *)tkt.dat);
  311. if(tmp < 0) {
  312. Curl_failf(conn->data, "Failed to decode base64 in reply.\n");
  313. Curl_set_command_prot(conn, save);
  314. return CURLE_FTP_WEIRD_SERVER_REPLY;
  315. }
  316. tkt.length = tmp;
  317. tktcopy.length = tkt.length;
  318. p = strstr(conn->data->state.buffer, "P=");
  319. if(!p) {
  320. Curl_failf(conn->data, "Bad reply from server");
  321. Curl_set_command_prot(conn, save);
  322. return CURLE_FTP_WEIRD_SERVER_REPLY;
  323. }
  324. name = p + 2;
  325. for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
  326. *p = 0;
  327. des_string_to_key (conn->passwd, &key);
  328. des_key_sched(&key, schedule);
  329. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  330. tkt.length,
  331. schedule, &key, DES_DECRYPT);
  332. if (strcmp ((char*)tktcopy.dat + 8,
  333. KRB_TICKET_GRANTING_TICKET) != 0) {
  334. afs_string_to_key(passwd,
  335. krb_realmofhost(conn->hostname),
  336. &key);
  337. des_key_sched(&key, schedule);
  338. des_pcbc_encrypt((void *)tkt.dat, (void *)tktcopy.dat,
  339. tkt.length,
  340. schedule, &key, DES_DECRYPT);
  341. }
  342. memset(key, 0, sizeof(key));
  343. memset(schedule, 0, sizeof(schedule));
  344. memset(passwd, 0, sizeof(passwd));
  345. if(Curl_base64_encode((char *)tktcopy.dat, tktcopy.length, &p) < 1) {
  346. failf(conn->data, "Out of memory base64-encoding.");
  347. Curl_set_command_prot(conn, save);
  348. return CURLE_OUT_OF_MEMORY;
  349. }
  350. memset (tktcopy.dat, 0, tktcopy.length);
  351. result = Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p);
  352. free(p);
  353. if(result)
  354. return result;
  355. result = Curl_GetFTPResponse(&nread, conn, NULL);
  356. if(result)
  357. return result;
  358. Curl_set_command_prot(conn, save);
  359. return CURLE_OK;
  360. }
  361. #endif /* HAVE_KRB4 */
  362. #endif /* CURL_DISABLE_FTP */