krb5.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 - 2012 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 "setup.h"
  35. #ifndef CURL_DISABLE_FTP
  36. #ifdef HAVE_GSSAPI
  37. #ifdef HAVE_OLD_GSSMIT
  38. #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
  39. #define NCOMPAT 1
  40. #endif
  41. #ifdef HAVE_NETDB_H
  42. #include <netdb.h>
  43. #endif
  44. #include "urldata.h"
  45. #include "curl_base64.h"
  46. #include "ftp.h"
  47. #include "curl_gssapi.h"
  48. #include "sendf.h"
  49. #include "krb4.h"
  50. #include "curl_memory.h"
  51. #define _MPRINTF_REPLACE /* use our functions only */
  52. #include <curl/mprintf.h>
  53. /* The last #include file should be: */
  54. #include "memdebug.h"
  55. #define LOCAL_ADDR (&conn->local_addr)
  56. #define REMOTE_ADDR conn->ip_addr->ai_addr
  57. static int
  58. krb5_init(void *app_data)
  59. {
  60. gss_ctx_id_t *context = app_data;
  61. /* Make sure our context is initialized for krb5_end. */
  62. *context = GSS_C_NO_CONTEXT;
  63. return 0;
  64. }
  65. static int
  66. krb5_check_prot(void *app_data, int level)
  67. {
  68. (void)app_data; /* unused */
  69. if(level == PROT_CONFIDENTIAL)
  70. return -1;
  71. return 0;
  72. }
  73. static int
  74. krb5_decode(void *app_data, void *buf, int len,
  75. int level UNUSED_PARAM,
  76. struct connectdata *conn UNUSED_PARAM)
  77. {
  78. gss_ctx_id_t *context = app_data;
  79. OM_uint32 maj, min;
  80. gss_buffer_desc enc, dec;
  81. (void)level;
  82. (void)conn;
  83. enc.value = buf;
  84. enc.length = len;
  85. maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL);
  86. if(maj != GSS_S_COMPLETE) {
  87. if(len >= 4)
  88. strcpy(buf, "599 ");
  89. return -1;
  90. }
  91. memcpy(buf, dec.value, dec.length);
  92. len = dec.length;
  93. gss_release_buffer(&min, &dec);
  94. return len;
  95. }
  96. static int
  97. krb5_overhead(void *app_data, int level, int len)
  98. {
  99. /* no arguments are used */
  100. (void)app_data;
  101. (void)level;
  102. (void)len;
  103. return 0;
  104. }
  105. static int
  106. krb5_encode(void *app_data, const void *from, int length, int level, void **to,
  107. struct connectdata *conn UNUSED_PARAM)
  108. {
  109. gss_ctx_id_t *context = app_data;
  110. gss_buffer_desc dec, enc;
  111. OM_uint32 maj, min;
  112. int state;
  113. int len;
  114. /* shut gcc up */
  115. conn = NULL;
  116. /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
  117. * libraries modify the input buffer in gss_seal()
  118. */
  119. dec.value = (void*)from;
  120. dec.length = length;
  121. maj = gss_seal(&min, *context,
  122. level == PROT_PRIVATE,
  123. GSS_C_QOP_DEFAULT,
  124. &dec, &state, &enc);
  125. if(maj != GSS_S_COMPLETE)
  126. return -1;
  127. /* malloc a new buffer, in case gss_release_buffer doesn't work as
  128. expected */
  129. *to = malloc(enc.length);
  130. if(!*to)
  131. return -1;
  132. memcpy(*to, enc.value, enc.length);
  133. len = enc.length;
  134. gss_release_buffer(&min, &enc);
  135. return len;
  136. }
  137. static int
  138. krb5_auth(void *app_data, struct connectdata *conn)
  139. {
  140. int ret = AUTH_OK;
  141. char *p;
  142. const char *host = conn->host.name;
  143. ssize_t nread;
  144. curl_socklen_t l = sizeof(conn->local_addr);
  145. struct SessionHandle *data = conn->data;
  146. CURLcode result;
  147. const char *service = "ftp", *srv_host = "host";
  148. gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
  149. OM_uint32 maj, min;
  150. gss_name_t gssname;
  151. gss_ctx_id_t *context = app_data;
  152. struct gss_channel_bindings_struct chan;
  153. size_t base64_sz = 0;
  154. if(getsockname(conn->sock[FIRSTSOCKET],
  155. (struct sockaddr *)LOCAL_ADDR, &l) < 0)
  156. perror("getsockname()");
  157. chan.initiator_addrtype = GSS_C_AF_INET;
  158. chan.initiator_address.length = l - 4;
  159. chan.initiator_address.value =
  160. &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
  161. chan.acceptor_addrtype = GSS_C_AF_INET;
  162. chan.acceptor_address.length = l - 4;
  163. chan.acceptor_address.value =
  164. &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
  165. chan.application_data.length = 0;
  166. chan.application_data.value = NULL;
  167. /* this loop will execute twice (once for service, once for host) */
  168. for(;;) {
  169. /* this really shouldn't be repeated here, but can't help it */
  170. if(service == srv_host) {
  171. result = Curl_ftpsendf(conn, "AUTH GSSAPI");
  172. if(result)
  173. return -2;
  174. if(Curl_GetFTPResponse(&nread, conn, NULL))
  175. return -1;
  176. if(data->state.buffer[0] != '3')
  177. return -1;
  178. }
  179. input_buffer.value = data->state.buffer;
  180. input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
  181. service, host);
  182. maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
  183. &gssname);
  184. if(maj != GSS_S_COMPLETE) {
  185. gss_release_name(&min, &gssname);
  186. if(service == srv_host) {
  187. Curl_failf(data, "Error importing service name %s",
  188. input_buffer.value);
  189. return AUTH_ERROR;
  190. }
  191. service = srv_host;
  192. continue;
  193. }
  194. /* We pass NULL as |output_name_type| to avoid a leak. */
  195. gss_display_name(&min, gssname, &output_buffer, NULL);
  196. Curl_infof(data, "Trying against %s\n", output_buffer.value);
  197. gssresp = GSS_C_NO_BUFFER;
  198. *context = GSS_C_NO_CONTEXT;
  199. do {
  200. /* Release the buffer at each iteration to avoid leaking: the first time
  201. we are releasing the memory from gss_display_name. The last item is
  202. taken care by a final gss_release_buffer. */
  203. gss_release_buffer(&min, &output_buffer);
  204. ret = AUTH_OK;
  205. maj = Curl_gss_init_sec_context(data,
  206. &min,
  207. context,
  208. gssname,
  209. &chan,
  210. gssresp,
  211. &output_buffer,
  212. NULL);
  213. if(gssresp) {
  214. free(_gssresp.value);
  215. gssresp = NULL;
  216. }
  217. if(GSS_ERROR(maj)) {
  218. Curl_infof(data, "Error creating security context\n");
  219. ret = AUTH_ERROR;
  220. break;
  221. }
  222. if(output_buffer.length != 0) {
  223. result = Curl_base64_encode(data, (char *)output_buffer.value,
  224. output_buffer.length, &p, &base64_sz);
  225. if(result) {
  226. Curl_infof(data,"base64-encoding: %s\n", curl_easy_strerror(result));
  227. ret = AUTH_CONTINUE;
  228. break;
  229. }
  230. result = Curl_ftpsendf(conn, "ADAT %s", p);
  231. free(p);
  232. if(result) {
  233. ret = -2;
  234. break;
  235. }
  236. if(Curl_GetFTPResponse(&nread, conn, NULL)) {
  237. ret = -1;
  238. break;
  239. }
  240. if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
  241. Curl_infof(data, "Server didn't accept auth data\n");
  242. ret = AUTH_ERROR;
  243. break;
  244. }
  245. p = data->state.buffer + 4;
  246. p = strstr(p, "ADAT=");
  247. if(p) {
  248. result = Curl_base64_decode(p + 5,
  249. (unsigned char **)&_gssresp.value,
  250. &_gssresp.length);
  251. if(result) {
  252. Curl_failf(data,"base64-decoding: %s", curl_easy_strerror(result));
  253. ret = AUTH_CONTINUE;
  254. break;
  255. }
  256. }
  257. gssresp = &_gssresp;
  258. }
  259. } while(maj == GSS_S_CONTINUE_NEEDED);
  260. gss_release_name(&min, &gssname);
  261. gss_release_buffer(&min, &output_buffer);
  262. if(gssresp)
  263. free(_gssresp.value);
  264. if(ret == AUTH_OK || service == srv_host)
  265. return ret;
  266. service = srv_host;
  267. }
  268. return ret;
  269. }
  270. static void krb5_end(void *app_data)
  271. {
  272. OM_uint32 min;
  273. gss_ctx_id_t *context = app_data;
  274. if(*context != GSS_C_NO_CONTEXT) {
  275. #ifdef DEBUGBUILD
  276. OM_uint32 maj =
  277. #endif
  278. gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
  279. DEBUGASSERT(maj == GSS_S_COMPLETE);
  280. }
  281. }
  282. struct Curl_sec_client_mech Curl_krb5_client_mech = {
  283. "GSSAPI",
  284. sizeof(gss_ctx_id_t),
  285. krb5_init,
  286. krb5_auth,
  287. krb5_end,
  288. krb5_check_prot,
  289. krb5_overhead,
  290. krb5_encode,
  291. krb5_decode
  292. };
  293. #endif /* HAVE_GSSAPI */
  294. #endif /* CURL_DISABLE_FTP */