krb5.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 - 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_check_prot(void *app_data, int level)
  70. {
  71. app_data = NULL; /* prevent compiler warning */
  72. if(level == prot_confidential)
  73. return -1;
  74. return 0;
  75. }
  76. static int
  77. krb5_decode(void *app_data, void *buf, int len, int level,
  78. struct connectdata *conn)
  79. {
  80. gss_ctx_id_t *context = app_data;
  81. OM_uint32 maj, min;
  82. gss_buffer_desc enc, dec;
  83. /* shut gcc up */
  84. level = 0;
  85. conn = NULL;
  86. enc.value = buf;
  87. enc.length = len;
  88. maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL);
  89. if(maj != GSS_S_COMPLETE) {
  90. if(len >= 4)
  91. strcpy(buf, "599 ");
  92. return -1;
  93. }
  94. memcpy(buf, dec.value, dec.length);
  95. len = dec.length;
  96. gss_release_buffer(&min, &dec);
  97. return len;
  98. }
  99. static int
  100. krb5_overhead(void *app_data, int level, int len)
  101. {
  102. /* no arguments are used, just init them to prevent compiler warnings */
  103. app_data = NULL;
  104. level = 0;
  105. len = 0;
  106. return 0;
  107. }
  108. static int
  109. krb5_encode(void *app_data, const void *from, int length, int level, void **to,
  110. struct connectdata *conn)
  111. {
  112. gss_ctx_id_t *context = app_data;
  113. gss_buffer_desc dec, enc;
  114. OM_uint32 maj, min;
  115. int state;
  116. int len;
  117. /* shut gcc up */
  118. conn = NULL;
  119. /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
  120. * libraries modify the input buffer in gss_seal()
  121. */
  122. dec.value = (void*)from;
  123. dec.length = length;
  124. maj = gss_seal(&min, *context,
  125. level == prot_private,
  126. GSS_C_QOP_DEFAULT,
  127. &dec, &state, &enc);
  128. if(maj != GSS_S_COMPLETE)
  129. return -1;
  130. /* malloc a new buffer, in case gss_release_buffer doesn't work as expected */
  131. *to = malloc(enc.length);
  132. if(!*to)
  133. return -1;
  134. memcpy(*to, enc.value, enc.length);
  135. len = enc.length;
  136. gss_release_buffer(&min, &enc);
  137. return len;
  138. }
  139. static int
  140. krb5_auth(void *app_data, struct connectdata *conn)
  141. {
  142. int ret;
  143. char *p;
  144. const char *host = conn->dns_entry->addr->ai_canonname;
  145. ssize_t nread;
  146. curl_socklen_t l = sizeof(conn->local_addr);
  147. struct SessionHandle *data = conn->data;
  148. CURLcode result;
  149. const char *service = "ftp", *srv_host = "host";
  150. gss_buffer_desc gssbuf, _gssresp, *gssresp;
  151. OM_uint32 maj, min;
  152. gss_name_t gssname;
  153. gss_ctx_id_t *context = app_data;
  154. struct gss_channel_bindings_struct chan;
  155. if(getsockname(conn->sock[FIRSTSOCKET],
  156. (struct sockaddr *)LOCAL_ADDR, &l) < 0)
  157. perror("getsockname()");
  158. chan.initiator_addrtype = GSS_C_AF_INET;
  159. chan.initiator_address.length = l - 4;
  160. chan.initiator_address.value =
  161. &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
  162. chan.acceptor_addrtype = GSS_C_AF_INET;
  163. chan.acceptor_address.length = l - 4;
  164. chan.acceptor_address.value =
  165. &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
  166. chan.application_data.length = 0;
  167. chan.application_data.value = NULL;
  168. /* this loop will execute twice (once for service, once for host) */
  169. while(1) {
  170. /* this really shouldn't be repeated here, but can't help it */
  171. if(service == srv_host) {
  172. result = Curl_ftpsendf(conn, "AUTH GSSAPI");
  173. if(result)
  174. return -2;
  175. if(Curl_GetFTPResponse(&nread, conn, NULL))
  176. return -1;
  177. if(data->state.buffer[0] != '3')
  178. return -1;
  179. }
  180. gssbuf.value = data->state.buffer;
  181. gssbuf.length = snprintf(gssbuf.value, BUFSIZE, "%s@%s", service, host);
  182. maj = gss_import_name(&min, &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &gssname);
  183. if(maj != GSS_S_COMPLETE) {
  184. gss_release_name(&min, &gssname);
  185. if(service == srv_host) {
  186. Curl_failf(data, "Error importing service name %s", gssbuf.value);
  187. return AUTH_ERROR;
  188. }
  189. service = srv_host;
  190. continue;
  191. }
  192. {
  193. gss_OID t;
  194. gss_display_name(&min, gssname, &gssbuf, &t);
  195. Curl_infof(data, "Trying against %s\n", gssbuf.value);
  196. gss_release_buffer(&min, &gssbuf);
  197. }
  198. gssresp = GSS_C_NO_BUFFER;
  199. *context = GSS_C_NO_CONTEXT;
  200. do {
  201. ret = AUTH_OK;
  202. maj = gss_init_sec_context(&min,
  203. GSS_C_NO_CREDENTIAL,
  204. context,
  205. gssname,
  206. GSS_C_NO_OID,
  207. GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG,
  208. 0,
  209. &chan,
  210. gssresp,
  211. NULL,
  212. &gssbuf,
  213. NULL,
  214. NULL);
  215. if(gssresp) {
  216. free(_gssresp.value);
  217. gssresp = NULL;
  218. }
  219. if(maj != GSS_S_COMPLETE && maj != GSS_S_CONTINUE_NEEDED) {
  220. Curl_infof(data, "Error creating security context");
  221. ret = AUTH_ERROR;
  222. break;
  223. }
  224. if(gssbuf.length != 0) {
  225. if(Curl_base64_encode(data, (char *)gssbuf.value, gssbuf.length, &p)
  226. < 1) {
  227. Curl_infof(data, "Out of memory base64-encoding");
  228. ret = AUTH_CONTINUE;
  229. break;
  230. }
  231. result = Curl_ftpsendf(conn, "ADAT %s", p);
  232. free(p);
  233. if(result) {
  234. ret = -2;
  235. break;
  236. }
  237. if(Curl_GetFTPResponse(&nread, conn, NULL)) {
  238. ret = -1;
  239. break;
  240. }
  241. if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3'){
  242. Curl_infof(data, "Server didn't accept auth data\n");
  243. ret = AUTH_ERROR;
  244. break;
  245. }
  246. p = data->state.buffer + 4;
  247. p = strstr(p, "ADAT=");
  248. if(p) {
  249. _gssresp.length = Curl_base64_decode(p + 5, (unsigned char **)
  250. &_gssresp.value);
  251. if(_gssresp.length < 1) {
  252. Curl_failf(data, "Out of memory base64-encoding");
  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. if(gssresp)
  262. free(_gssresp.value);
  263. if(ret == AUTH_OK || service == srv_host)
  264. return ret;
  265. service = srv_host;
  266. }
  267. }
  268. struct Curl_sec_client_mech Curl_krb5_client_mech = {
  269. "GSSAPI",
  270. sizeof(gss_ctx_id_t),
  271. NULL, /* init */
  272. krb5_auth,
  273. NULL, /* end */
  274. krb5_check_prot,
  275. krb5_overhead,
  276. krb5_encode,
  277. krb5_decode
  278. };
  279. #endif /* HAVE_GSSAPI */
  280. #endif /* CURL_DISABLE_FTP */