krb5.c 9.7 KB

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