NeonIntf.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "NeonIntf.h"
  5. #include "Interface.h"
  6. #include "CoreMain.h"
  7. #include "Exceptions.h"
  8. #include "Security.h"
  9. #include <TextsCore.h>
  10. #include <ne_auth.h>
  11. #include <ne_redirect.h>
  12. #include <StrUtils.hpp>
  13. //---------------------------------------------------------------------------
  14. #define SESSION_PROXY_AUTH_KEY "proxyauth"
  15. #define SESSION_TLS_INIT_KEY "tlsinit"
  16. //---------------------------------------------------------------------------
  17. void NeonParseUrl(const UnicodeString & Url, ne_uri & uri)
  18. {
  19. if (ne_uri_parse(StrToNeon(Url), &uri) != 0)
  20. {
  21. // should never happen
  22. throw Exception(FMTLOAD(INVALID_URL, (Url)));
  23. }
  24. // Will never happen for initial URL, but may happen for redirect URLs
  25. if (uri.port == 0)
  26. {
  27. uri.port = ne_uri_defaultport(uri.scheme);
  28. }
  29. }
  30. //---------------------------------------------------------------------------
  31. bool IsTlsUri(const ne_uri & uri)
  32. {
  33. return SameText(StrFromNeon(uri.scheme), WebDAVSProtocol);
  34. }
  35. //---------------------------------------------------------------------------
  36. struct TProxyAuthData
  37. {
  38. UnicodeString UserName;
  39. UnicodeString Password;
  40. };
  41. //------------------------------------------------------------------------------
  42. static int NeonProxyAuth(
  43. void * UserData, const char * /*Realm*/, int Attempt, char * UserName, char * Password)
  44. {
  45. TProxyAuthData * ProxyAuthData = static_cast<TProxyAuthData *>(UserData);
  46. int Result;
  47. // no point trying too many times as we always return the same credentials
  48. // (maybe just one would be enough)
  49. if (Attempt >= 2)
  50. {
  51. Result = 1;
  52. }
  53. else
  54. {
  55. strncpy(UserName, StrToNeon(ProxyAuthData->UserName), NE_ABUFSIZ);
  56. strncpy(Password, StrToNeon(ProxyAuthData->Password), NE_ABUFSIZ);
  57. Result = 0;
  58. }
  59. return Result;
  60. }
  61. //---------------------------------------------------------------------------
  62. ne_session * CreateNeonSession(
  63. const ne_uri & uri, TProxyMethod ProxyMethod, const UnicodeString & ProxyHost,
  64. int ProxyPort, const UnicodeString & ProxyUsername, const UnicodeString & ProxyPassword)
  65. {
  66. ne_session * Session = ne_session_create(uri.scheme, uri.host, uri.port);
  67. if (ProxyMethod != ::pmNone)
  68. {
  69. if ((ProxyMethod == pmSocks4) || (ProxyMethod == pmSocks5))
  70. {
  71. enum ne_sock_sversion vers = (ProxyMethod == pmSocks4) ? NE_SOCK_SOCKSV4A : NE_SOCK_SOCKSV5;
  72. ne_session_socks_proxy(Session, vers, StrToNeon(ProxyHost), ProxyPort, StrToNeon(ProxyUsername), StrToNeon(ProxyPassword));
  73. }
  74. else if (!ProxyHost.IsEmpty())
  75. {
  76. ne_session_proxy(Session, StrToNeon(ProxyHost), ProxyPort);
  77. if (!ProxyUsername.IsEmpty())
  78. {
  79. TProxyAuthData * ProxyAuthData = new TProxyAuthData();
  80. ProxyAuthData->UserName = ProxyUsername;
  81. ProxyAuthData->Password = ProxyPassword;
  82. ne_set_session_private(Session, SESSION_PROXY_AUTH_KEY, ProxyAuthData);
  83. ne_set_proxy_auth(Session, NeonProxyAuth, ProxyAuthData);
  84. }
  85. else
  86. {
  87. // Enable (only) the Negotiate scheme for proxy
  88. // authentication, if no username/password is
  89. // configured.
  90. ne_add_proxy_auth(Session, NE_AUTH_NEGOTIATE, NULL, NULL);
  91. }
  92. }
  93. }
  94. ne_redirect_register(Session);
  95. ne_set_useragent(Session, StrToNeon(FORMAT(L"%s/%s", (AppNameString(), Configuration->Version))));
  96. return Session;
  97. }
  98. //---------------------------------------------------------------------------
  99. void DestroyNeonSession(ne_session * Session)
  100. {
  101. TProxyAuthData * ProxyAuthData =
  102. static_cast<TProxyAuthData *>(ne_get_session_private(Session, SESSION_PROXY_AUTH_KEY));
  103. if (ProxyAuthData != NULL)
  104. {
  105. delete ProxyAuthData;
  106. }
  107. ne_session_destroy(Session);
  108. }
  109. //---------------------------------------------------------------------------
  110. UnicodeString GetNeonError(ne_session * Session)
  111. {
  112. return StrFromNeon(ne_get_error(Session));
  113. }
  114. //---------------------------------------------------------------------------
  115. void CheckNeonStatus(ne_session * Session, int NeonStatus,
  116. const UnicodeString & HostName, const UnicodeString & CustomError)
  117. {
  118. if (NeonStatus == NE_OK)
  119. {
  120. // noop
  121. }
  122. else
  123. {
  124. UnicodeString NeonError = GetNeonError(Session);
  125. UnicodeString Error;
  126. if (!CustomError.IsEmpty())
  127. {
  128. Error = CustomError;
  129. }
  130. else
  131. {
  132. switch (NeonStatus)
  133. {
  134. case NE_ERROR:
  135. // noop
  136. DebugAssert(!NeonError.IsEmpty());
  137. Error = NeonError;
  138. NeonError = L"";
  139. break;
  140. case NE_LOOKUP:
  141. Error = ReplaceStr(LoadStr(NET_TRANSL_HOST_NOT_EXIST2), L"%HOST%", HostName);
  142. break;
  143. case NE_AUTH:
  144. Error = LoadStr(AUTHENTICATION_FAILED);
  145. break;
  146. case NE_PROXYAUTH:
  147. Error = LoadStr(PROXY_AUTHENTICATION_FAILED);
  148. break;
  149. case NE_CONNECT:
  150. Error = LoadStr(CONNECTION_FAILED);
  151. break;
  152. case NE_TIMEOUT:
  153. Error = ReplaceStr(LoadStr(NET_TRANSL_TIMEOUT2), L"%HOST%", HostName);
  154. break;
  155. case NE_REDIRECT:
  156. {
  157. char * Uri = ne_uri_unparse(ne_redirect_location(Session));
  158. Error = FMTLOAD(REQUEST_REDIRECTED, (Uri));
  159. ne_free(Uri);
  160. }
  161. break;
  162. case NE_FAILED: // never used by neon as of 0.30.0
  163. case NE_RETRY: // not sure if this is a public API
  164. default:
  165. DebugFail();
  166. Error = FORMAT(L"Unexpected neon error %d", (NeonStatus));
  167. break;
  168. }
  169. }
  170. throw ExtException(Error, NeonError);
  171. }
  172. }
  173. //---------------------------------------------------------------------------
  174. UnicodeString GetNeonRedirectUrl(ne_session * Session)
  175. {
  176. const ne_uri * RedirectUri = ne_redirect_location(Session);
  177. char * RedirectUriStr = ne_uri_unparse(RedirectUri);
  178. UnicodeString Result = StrFromNeon(RedirectUriStr);
  179. ne_free(RedirectUriStr);
  180. return Result;
  181. }
  182. //---------------------------------------------------------------------------
  183. #define MAX_REDIRECT_ATTEMPTS 5
  184. //---------------------------------------------------------------------------
  185. void CheckRedirectLoop(const UnicodeString & RedirectUrl, TStrings * AttemptedUrls)
  186. {
  187. if (AttemptedUrls->Count > MAX_REDIRECT_ATTEMPTS)
  188. {
  189. throw Exception(LoadStr(TOO_MANY_REDIRECTS));
  190. }
  191. else
  192. {
  193. // Make sure we've not attempted this URL before.
  194. if (AttemptedUrls->IndexOf(RedirectUrl) >= 0)
  195. {
  196. throw Exception(LoadStr(REDIRECT_LOOP));
  197. }
  198. AttemptedUrls->Add(RedirectUrl);
  199. }
  200. }
  201. //---------------------------------------------------------------------------
  202. extern "C"
  203. {
  204. void ne_init_ssl_session(struct ssl_st * Ssl, ne_session * Session)
  205. {
  206. TNeonTlsInit OnNeonTlsInit =
  207. reinterpret_cast<TNeonTlsInit>(ne_get_session_private(Session, SESSION_TLS_INIT_KEY));
  208. if (DebugAlwaysTrue(OnNeonTlsInit != NULL))
  209. {
  210. OnNeonTlsInit(Ssl, Session);
  211. }
  212. }
  213. } // extern "C"
  214. //---------------------------------------------------------------------------
  215. void SetNeonTlsInit(ne_session * Session, TNeonTlsInit OnNeonTlsInit)
  216. {
  217. ne_set_session_private(Session, SESSION_TLS_INIT_KEY, OnNeonTlsInit);
  218. }
  219. //---------------------------------------------------------------------------
  220. AnsiString NeonExportCertificate(const ne_ssl_certificate * Certificate)
  221. {
  222. char * AsciiCert = ne_ssl_cert_export(Certificate);
  223. AnsiString Result = AsciiCert;
  224. ne_free(AsciiCert);
  225. return Result;
  226. }
  227. //---------------------------------------------------------------------------
  228. bool NeonWindowsValidateCertificate(int & Failures, const AnsiString & AsciiCert, UnicodeString & Error)
  229. {
  230. bool Result = false;
  231. // We can accept only unknown certificate authority.
  232. if (FLAGSET(Failures, NE_SSL_UNTRUSTED))
  233. {
  234. unsigned char * Certificate;
  235. size_t CertificateLen = ne_unbase64(AsciiCert.c_str(), &Certificate);
  236. if (CertificateLen > 0)
  237. {
  238. if (WindowsValidateCertificate(Certificate, CertificateLen, Error))
  239. {
  240. Failures &= ~NE_SSL_UNTRUSTED;
  241. Result = true;
  242. }
  243. ne_free(Certificate);
  244. }
  245. }
  246. return Result;
  247. }
  248. //---------------------------------------------------------------------------
  249. UnicodeString NeonCertificateFailuresErrorStr(int Failures, const UnicodeString & HostName)
  250. {
  251. int FailuresToList = Failures;
  252. UnicodeString Result;
  253. if (FLAGSET(FailuresToList, NE_SSL_NOTYETVALID))
  254. {
  255. AddToList(Result, LoadStr(CERT_ERR_CERT_NOT_YET_VALID), L" ");
  256. FailuresToList &= ~NE_SSL_NOTYETVALID;
  257. }
  258. if (FLAGSET(FailuresToList, NE_SSL_EXPIRED))
  259. {
  260. AddToList(Result, LoadStr(CERT_ERR_CERT_HAS_EXPIRED), L" ");
  261. FailuresToList &= ~NE_SSL_EXPIRED;
  262. }
  263. // NEON checks certificate host name on its own
  264. if (FLAGSET(FailuresToList, NE_SSL_IDMISMATCH))
  265. {
  266. AddToList(Result, FMTLOAD(CERT_NAME_MISMATCH, (HostName)), L" ");
  267. FailuresToList &= ~NE_SSL_IDMISMATCH;
  268. }
  269. if (FLAGSET(FailuresToList, NE_SSL_UNTRUSTED))
  270. {
  271. AddToList(Result, LoadStr(CERT_ERR_CERT_UNTRUSTED), L" ");
  272. FailuresToList &= ~NE_SSL_UNTRUSTED;
  273. }
  274. if (FLAGSET(FailuresToList, NE_SSL_BADCHAIN))
  275. {
  276. AddToList(Result, LoadStr(CERT_ERR_BAD_CHAIN), L" ");
  277. FailuresToList &= ~NE_SSL_BADCHAIN;
  278. }
  279. // nb, NE_SSL_REVOKED is never used by OpenSSL implementation
  280. if (FailuresToList != 0)
  281. {
  282. AddToList(Result, LoadStr(CERT_ERR_UNKNOWN), L" ");
  283. }
  284. return Result;
  285. }