krb5.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* GSSAPI/krb5 support for FTP - loosely based on old krb4.c
  2. *
  3. * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
  4. * (Royal Institute of Technology, Stockholm, Sweden).
  5. * Copyright (c) 2004 - 2015 Daniel Stenberg
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * 3. Neither the name of the Institute nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE. */
  34. #include "curl_setup.h"
  35. #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_FTP)
  36. #ifdef HAVE_NETDB_H
  37. #include <netdb.h>
  38. #endif
  39. #include "urldata.h"
  40. #include "curl_base64.h"
  41. #include "ftp.h"
  42. #include "curl_gssapi.h"
  43. #include "sendf.h"
  44. #include "curl_sec.h"
  45. #include "warnless.h"
  46. #include "curl_printf.h"
  47. /* The last #include files should be: */
  48. #include "curl_memory.h"
  49. #include "memdebug.h"
  50. #define LOCAL_ADDR (&conn->local_addr)
  51. #define REMOTE_ADDR conn->ip_addr->ai_addr
  52. static int
  53. krb5_init(void *app_data)
  54. {
  55. gss_ctx_id_t *context = app_data;
  56. /* Make sure our context is initialized for krb5_end. */
  57. *context = GSS_C_NO_CONTEXT;
  58. return 0;
  59. }
  60. static int
  61. krb5_check_prot(void *app_data, int level)
  62. {
  63. (void)app_data; /* unused */
  64. if(level == PROT_CONFIDENTIAL)
  65. return -1;
  66. return 0;
  67. }
  68. static int
  69. krb5_decode(void *app_data, void *buf, int len,
  70. int level UNUSED_PARAM,
  71. struct connectdata *conn UNUSED_PARAM)
  72. {
  73. gss_ctx_id_t *context = app_data;
  74. OM_uint32 maj, min;
  75. gss_buffer_desc enc, dec;
  76. (void)level;
  77. (void)conn;
  78. enc.value = buf;
  79. enc.length = len;
  80. maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL);
  81. if(maj != GSS_S_COMPLETE) {
  82. if(len >= 4)
  83. strcpy(buf, "599 ");
  84. return -1;
  85. }
  86. memcpy(buf, dec.value, dec.length);
  87. len = curlx_uztosi(dec.length);
  88. gss_release_buffer(&min, &dec);
  89. return len;
  90. }
  91. static int
  92. krb5_overhead(void *app_data, int level, int len)
  93. {
  94. /* no arguments are used */
  95. (void)app_data;
  96. (void)level;
  97. (void)len;
  98. return 0;
  99. }
  100. static int
  101. krb5_encode(void *app_data, const void *from, int length, int level, void **to)
  102. {
  103. gss_ctx_id_t *context = app_data;
  104. gss_buffer_desc dec, enc;
  105. OM_uint32 maj, min;
  106. int state;
  107. int len;
  108. /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
  109. * libraries modify the input buffer in gss_seal()
  110. */
  111. dec.value = (void*)from;
  112. dec.length = length;
  113. maj = gss_seal(&min, *context,
  114. level == PROT_PRIVATE,
  115. GSS_C_QOP_DEFAULT,
  116. &dec, &state, &enc);
  117. if(maj != GSS_S_COMPLETE)
  118. return -1;
  119. /* malloc a new buffer, in case gss_release_buffer doesn't work as
  120. expected */
  121. *to = malloc(enc.length);
  122. if(!*to)
  123. return -1;
  124. memcpy(*to, enc.value, enc.length);
  125. len = curlx_uztosi(enc.length);
  126. gss_release_buffer(&min, &enc);
  127. return len;
  128. }
  129. static int
  130. krb5_auth(void *app_data, struct connectdata *conn)
  131. {
  132. int ret = AUTH_OK;
  133. char *p;
  134. const char *host = conn->host.name;
  135. ssize_t nread;
  136. curl_socklen_t l = sizeof(conn->local_addr);
  137. struct SessionHandle *data = conn->data;
  138. CURLcode result;
  139. const char *service = "ftp", *srv_host = "host";
  140. gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
  141. OM_uint32 maj, min;
  142. gss_name_t gssname;
  143. gss_ctx_id_t *context = app_data;
  144. struct gss_channel_bindings_struct chan;
  145. size_t base64_sz = 0;
  146. if(getsockname(conn->sock[FIRSTSOCKET],
  147. (struct sockaddr *)LOCAL_ADDR, &l) < 0)
  148. perror("getsockname()");
  149. chan.initiator_addrtype = GSS_C_AF_INET;
  150. chan.initiator_address.length = l - 4;
  151. chan.initiator_address.value =
  152. &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
  153. chan.acceptor_addrtype = GSS_C_AF_INET;
  154. chan.acceptor_address.length = l - 4;
  155. chan.acceptor_address.value =
  156. &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
  157. chan.application_data.length = 0;
  158. chan.application_data.value = NULL;
  159. /* this loop will execute twice (once for service, once for host) */
  160. for(;;) {
  161. /* this really shouldn't be repeated here, but can't help it */
  162. if(service == srv_host) {
  163. result = Curl_ftpsendf(conn, "AUTH GSSAPI");
  164. if(result)
  165. return -2;
  166. if(Curl_GetFTPResponse(&nread, conn, NULL))
  167. return -1;
  168. if(data->state.buffer[0] != '3')
  169. return -1;
  170. }
  171. input_buffer.value = data->state.buffer;
  172. input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
  173. service, host);
  174. maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
  175. &gssname);
  176. if(maj != GSS_S_COMPLETE) {
  177. gss_release_name(&min, &gssname);
  178. if(service == srv_host) {
  179. Curl_failf(data, "Error importing service name %s",
  180. input_buffer.value);
  181. return AUTH_ERROR;
  182. }
  183. service = srv_host;
  184. continue;
  185. }
  186. /* We pass NULL as |output_name_type| to avoid a leak. */
  187. gss_display_name(&min, gssname, &output_buffer, NULL);
  188. Curl_infof(data, "Trying against %s\n", output_buffer.value);
  189. gssresp = GSS_C_NO_BUFFER;
  190. *context = GSS_C_NO_CONTEXT;
  191. do {
  192. /* Release the buffer at each iteration to avoid leaking: the first time
  193. we are releasing the memory from gss_display_name. The last item is
  194. taken care by a final gss_release_buffer. */
  195. gss_release_buffer(&min, &output_buffer);
  196. ret = AUTH_OK;
  197. maj = Curl_gss_init_sec_context(data,
  198. &min,
  199. context,
  200. gssname,
  201. &Curl_krb5_mech_oid,
  202. &chan,
  203. gssresp,
  204. &output_buffer,
  205. TRUE,
  206. NULL);
  207. if(gssresp) {
  208. free(_gssresp.value);
  209. gssresp = NULL;
  210. }
  211. if(GSS_ERROR(maj)) {
  212. Curl_infof(data, "Error creating security context\n");
  213. ret = AUTH_ERROR;
  214. break;
  215. }
  216. if(output_buffer.length != 0) {
  217. result = Curl_base64_encode(data, (char *)output_buffer.value,
  218. output_buffer.length, &p, &base64_sz);
  219. if(result) {
  220. Curl_infof(data, "base64-encoding: %s\n",
  221. curl_easy_strerror(result));
  222. ret = AUTH_CONTINUE;
  223. break;
  224. }
  225. result = Curl_ftpsendf(conn, "ADAT %s", p);
  226. free(p);
  227. if(result) {
  228. ret = -2;
  229. break;
  230. }
  231. if(Curl_GetFTPResponse(&nread, conn, NULL)) {
  232. ret = -1;
  233. break;
  234. }
  235. if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
  236. Curl_infof(data, "Server didn't accept auth data\n");
  237. ret = AUTH_ERROR;
  238. break;
  239. }
  240. p = data->state.buffer + 4;
  241. p = strstr(p, "ADAT=");
  242. if(p) {
  243. result = Curl_base64_decode(p + 5,
  244. (unsigned char **)&_gssresp.value,
  245. &_gssresp.length);
  246. if(result) {
  247. Curl_failf(data, "base64-decoding: %s",
  248. curl_easy_strerror(result));
  249. ret = AUTH_CONTINUE;
  250. break;
  251. }
  252. }
  253. gssresp = &_gssresp;
  254. }
  255. } while(maj == GSS_S_CONTINUE_NEEDED);
  256. gss_release_name(&min, &gssname);
  257. gss_release_buffer(&min, &output_buffer);
  258. if(gssresp)
  259. free(_gssresp.value);
  260. if(ret == AUTH_OK || service == srv_host)
  261. return ret;
  262. service = srv_host;
  263. }
  264. return ret;
  265. }
  266. static void krb5_end(void *app_data)
  267. {
  268. OM_uint32 min;
  269. gss_ctx_id_t *context = app_data;
  270. if(*context != GSS_C_NO_CONTEXT) {
  271. #ifdef DEBUGBUILD
  272. OM_uint32 maj =
  273. #endif
  274. gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
  275. DEBUGASSERT(maj == GSS_S_COMPLETE);
  276. }
  277. }
  278. struct Curl_sec_client_mech Curl_krb5_client_mech = {
  279. "GSSAPI",
  280. sizeof(gss_ctx_id_t),
  281. krb5_init,
  282. krb5_auth,
  283. krb5_end,
  284. krb5_check_prot,
  285. krb5_overhead,
  286. krb5_encode,
  287. krb5_decode
  288. };
  289. #endif /* HAVE_GSSAPI && !CURL_DISABLE_FTP */