krb4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #ifdef KRB4
  42. #include "security.h"
  43. #include "base64.h"
  44. #include <stdlib.h>
  45. #include <netdb.h>
  46. #include <syslog.h>
  47. #include <string.h>
  48. #include <krb.h>
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h> /* for getpid() */
  51. #endif
  52. #include "ftp.h"
  53. #include "sendf.h"
  54. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  55. #include "inet_ntoa_r.h"
  56. #endif
  57. /* The last #include file should be: */
  58. #ifdef MALLOCDEBUG
  59. #include "memdebug.h"
  60. #endif
  61. #define LOCAL_ADDR (&conn->local_addr)
  62. #define REMOTE_ADDR (&conn->serv_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. syslog(LOG_ERR, "krb4_decode: %s", krb_get_err_text(e));
  117. return -1;
  118. }
  119. memmove(buf, m.app_data, m.app_length);
  120. return m.app_length;
  121. }
  122. static int
  123. krb4_overhead(void *app_data, int level, int len)
  124. {
  125. /* no arguments are used, just init them to prevent compiler warnings */
  126. app_data = NULL;
  127. level = 0;
  128. len = 0;
  129. return 31;
  130. }
  131. static int
  132. krb4_encode(void *app_data, void *from, int length, int level, void **to,
  133. struct connectdata *conn)
  134. {
  135. struct krb4_data *d = app_data;
  136. *to = malloc(length + 31);
  137. if(level == prot_safe)
  138. return krb_mk_safe(from, *to, length, &d->key,
  139. (struct sockaddr_in *)LOCAL_ADDR,
  140. (struct sockaddr_in *)REMOTE_ADDR);
  141. else if(level == prot_private)
  142. return krb_mk_priv(from, *to, length, d->schedule, &d->key,
  143. (struct sockaddr_in *)LOCAL_ADDR,
  144. (struct sockaddr_in *)REMOTE_ADDR);
  145. else
  146. return -1;
  147. }
  148. static int
  149. mk_auth(struct krb4_data *d, KTEXT adat,
  150. const char *service, char *host, int checksum)
  151. {
  152. int ret;
  153. CREDENTIALS cred;
  154. char sname[SNAME_SZ], inst[INST_SZ], realm[REALM_SZ];
  155. strlcpy(sname, service, sizeof(sname));
  156. strlcpy(inst, krb_get_phost(host), sizeof(inst));
  157. strlcpy(realm, krb_realmofhost(host), sizeof(realm));
  158. ret = krb_mk_req(adat, sname, inst, realm, checksum);
  159. if(ret)
  160. return ret;
  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_get_cred(sname, inst, realm, &cred);
  165. memmove(&d->key, &cred.session, sizeof(des_cblock));
  166. des_key_sched(&d->key, d->schedule);
  167. memset(&cred, 0, sizeof(cred));
  168. return ret;
  169. }
  170. static int
  171. krb4_auth(void *app_data, struct connectdata *conn)
  172. {
  173. int ret;
  174. char *p;
  175. int len;
  176. KTEXT_ST adat;
  177. MSG_DAT msg_data;
  178. int checksum;
  179. u_int32_t cs;
  180. struct krb4_data *d = app_data;
  181. char *host = conn->hostaddr->h_name;
  182. ssize_t nread;
  183. int l = sizeof(conn->local_addr);
  184. struct SessionHandle *data = conn->data;
  185. if(getsockname(conn->firstsocket,
  186. (struct sockaddr *)LOCAL_ADDR, &l) < 0)
  187. perror("getsockname()");
  188. checksum = getpid();
  189. ret = mk_auth(d, &adat, "ftp", host, checksum);
  190. if(ret == KDC_PR_UNKNOWN)
  191. ret = mk_auth(d, &adat, "rcmd", host, checksum);
  192. if(ret) {
  193. Curl_infof(data, "%s\n", krb_get_err_text(ret));
  194. return AUTH_CONTINUE;
  195. }
  196. #ifdef HAVE_KRB_GET_OUR_IP_FOR_REALM
  197. if (krb_get_config_bool("nat_in_use")) {
  198. struct sockaddr_in *localaddr = (struct sockaddr_in *)LOCAL_ADDR;
  199. struct in_addr natAddr;
  200. if (krb_get_our_ip_for_realm(krb_realmofhost(host),
  201. &natAddr) != KSUCCESS
  202. && krb_get_our_ip_for_realm(NULL, &natAddr) != KSUCCESS)
  203. Curl_infof(data, "Can't get address for realm %s\n",
  204. krb_realmofhost(host));
  205. else {
  206. if (natAddr.s_addr != localaddr->sin_addr.s_addr) {
  207. #ifdef HAVE_INET_NTOA_R
  208. char ntoa_buf[64];
  209. char *ip = (char *)inet_ntoa_r(natAddr, ntoa_buf, sizeof(ntoa_buf));
  210. #else
  211. char *ip = (char *)inet_ntoa(natAddr);
  212. #endif
  213. Curl_infof(data, "Using NAT IP address (%s) for kerberos 4\n", ip);
  214. localaddr->sin_addr = natAddr;
  215. }
  216. }
  217. }
  218. #endif
  219. if(Curl_base64_encode(adat.dat, adat.length, &p) < 0) {
  220. Curl_failf(data, "Out of memory base64-encoding");
  221. return AUTH_CONTINUE;
  222. }
  223. if(Curl_ftpsendf(conn, "ADAT %s", p))
  224. return -2;
  225. nread = Curl_GetFTPResponse(data->state.buffer, conn, NULL);
  226. if(nread < 0)
  227. return -1;
  228. free(p);
  229. if(data->state.buffer[0] != '2'){
  230. Curl_failf(data, "Server didn't accept auth data");
  231. return AUTH_ERROR;
  232. }
  233. p = strstr(data->state.buffer, "ADAT=");
  234. if(!p) {
  235. Curl_failf(data, "Remote host didn't send adat reply");
  236. return AUTH_ERROR;
  237. }
  238. p += 5;
  239. len = Curl_base64_decode(p, adat.dat);
  240. if(len < 0) {
  241. Curl_failf(data, "Failed to decode base64 from server");
  242. return AUTH_ERROR;
  243. }
  244. adat.length = len;
  245. ret = krb_rd_safe(adat.dat, adat.length, &d->key,
  246. (struct sockaddr_in *)hisctladdr,
  247. (struct sockaddr_in *)myctladdr, &msg_data);
  248. if(ret) {
  249. Curl_failf(data, "Error reading reply from server: %s",
  250. krb_get_err_text(ret));
  251. return AUTH_ERROR;
  252. }
  253. krb_get_int(msg_data.app_data, &cs, 4, 0);
  254. if(cs - checksum != 1) {
  255. Curl_failf(data, "Bad checksum returned from server");
  256. return AUTH_ERROR;
  257. }
  258. return AUTH_OK;
  259. }
  260. struct Curl_sec_client_mech Curl_krb4_client_mech = {
  261. "KERBEROS_V4",
  262. sizeof(struct krb4_data),
  263. NULL, /* init */
  264. krb4_auth,
  265. NULL, /* end */
  266. krb4_check_prot,
  267. krb4_overhead,
  268. krb4_encode,
  269. krb4_decode
  270. };
  271. void Curl_krb_kauth(struct connectdata *conn)
  272. {
  273. des_cblock key;
  274. des_key_schedule schedule;
  275. KTEXT_ST tkt, tktcopy;
  276. char *name;
  277. char *p;
  278. char passwd[100];
  279. int tmp;
  280. ssize_t nread;
  281. int save;
  282. save = Curl_set_command_prot(conn, prot_private);
  283. if(Curl_ftpsendf(conn, "SITE KAUTH %s", conn->data->state.user))
  284. return;
  285. nread = Curl_GetFTPResponse(conn->data->state.buffer,
  286. conn, NULL);
  287. if(nread < 0)
  288. return /*CURLE_OPERATION_TIMEOUTED*/;
  289. if(conn->data->state.buffer[0] != '3'){
  290. Curl_set_command_prot(conn, save);
  291. return;
  292. }
  293. p = strstr(conn->data->state.buffer, "T=");
  294. if(!p) {
  295. Curl_failf(conn->data, "Bad reply from server");
  296. Curl_set_command_prot(conn, save);
  297. return;
  298. }
  299. p += 2;
  300. tmp = Curl_base64_decode(p, &tkt.dat);
  301. if(tmp < 0) {
  302. Curl_failf(conn->data, "Failed to decode base64 in reply.\n");
  303. Curl_set_command_prot(conn, save);
  304. return;
  305. }
  306. tkt.length = tmp;
  307. tktcopy.length = tkt.length;
  308. p = strstr(conn->data->state.buffer, "P=");
  309. if(!p) {
  310. Curl_failf(conn->data, "Bad reply from server");
  311. Curl_set_command_prot(conn, save);
  312. return;
  313. }
  314. name = p + 2;
  315. for(; *p && *p != ' ' && *p != '\r' && *p != '\n'; p++);
  316. *p = 0;
  317. des_string_to_key (conn->data->state.passwd, &key);
  318. des_key_sched(&key, schedule);
  319. des_pcbc_encrypt((des_cblock*)tkt.dat, (des_cblock*)tktcopy.dat,
  320. tkt.length,
  321. schedule, &key, DES_DECRYPT);
  322. if (strcmp ((char*)tktcopy.dat + 8,
  323. KRB_TICKET_GRANTING_TICKET) != 0) {
  324. afs_string_to_key (passwd,
  325. krb_realmofhost(conn->hostaddr->h_name),
  326. &key);
  327. des_key_sched (&key, schedule);
  328. des_pcbc_encrypt((des_cblock*)tkt.dat, (des_cblock*)tktcopy.dat,
  329. tkt.length,
  330. schedule, &key, DES_DECRYPT);
  331. }
  332. memset(key, 0, sizeof(key));
  333. memset(schedule, 0, sizeof(schedule));
  334. memset(passwd, 0, sizeof(passwd));
  335. if(Curl_base64_encode(tktcopy.dat, tktcopy.length, &p) < 0) {
  336. failf(conn->data, "Out of memory base64-encoding.");
  337. Curl_set_command_prot(conn, save);
  338. return;
  339. }
  340. memset (tktcopy.dat, 0, tktcopy.length);
  341. if(Curl_ftpsendf(conn, "SITE KAUTH %s %s", name, p))
  342. return;
  343. nread = Curl_GetFTPResponse(conn->data->state.buffer,
  344. conn, NULL);
  345. if(nread < 0)
  346. return /*CURLE_OPERATION_TIMEOUTED*/;
  347. free(p);
  348. Curl_set_command_prot(conn, save);
  349. }
  350. #endif /* KRB4 */
  351. /*
  352. * local variables:
  353. * eval: (load-file "../curl-mode.el")
  354. * end:
  355. * vim600: fdm=marker
  356. * vim: et sw=2 ts=2 sts=2 tw=78
  357. */