strerror.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef HAVE_STRERROR_R
  24. # if (!defined(HAVE_POSIX_STRERROR_R) && \
  25. !defined(HAVE_GLIBC_STRERROR_R) && \
  26. !defined(HAVE_VXWORKS_STRERROR_R)) || \
  27. (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)) || \
  28. (defined(HAVE_GLIBC_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)) || \
  29. (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R))
  30. # error "strerror_r MUST be either POSIX, glibc or vxworks-style"
  31. # endif
  32. #endif
  33. #include <curl/curl.h>
  34. #ifdef USE_LIBIDN2
  35. #include <idn2.h>
  36. #endif
  37. #ifdef USE_WINDOWS_SSPI
  38. #include "curl_sspi.h"
  39. #endif
  40. #include "strerror.h"
  41. /* The last 3 #include files should be in this order */
  42. #include "curl_printf.h"
  43. #include "curl_memory.h"
  44. #include "memdebug.h"
  45. #if defined(WIN32) || defined(_WIN32_WCE)
  46. #define PRESERVE_WINDOWS_ERROR_CODE
  47. #endif
  48. const char *
  49. curl_easy_strerror(CURLcode error)
  50. {
  51. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  52. switch(error) {
  53. case CURLE_OK:
  54. return "No error";
  55. case CURLE_UNSUPPORTED_PROTOCOL:
  56. return "Unsupported protocol";
  57. case CURLE_FAILED_INIT:
  58. return "Failed initialization";
  59. case CURLE_URL_MALFORMAT:
  60. return "URL using bad/illegal format or missing URL";
  61. case CURLE_NOT_BUILT_IN:
  62. return "A requested feature, protocol or option was not found built-in in"
  63. " this libcurl due to a build-time decision.";
  64. case CURLE_COULDNT_RESOLVE_PROXY:
  65. return "Couldn't resolve proxy name";
  66. case CURLE_COULDNT_RESOLVE_HOST:
  67. return "Couldn't resolve host name";
  68. case CURLE_COULDNT_CONNECT:
  69. return "Couldn't connect to server";
  70. case CURLE_WEIRD_SERVER_REPLY:
  71. return "Weird server reply";
  72. case CURLE_REMOTE_ACCESS_DENIED:
  73. return "Access denied to remote resource";
  74. case CURLE_FTP_ACCEPT_FAILED:
  75. return "FTP: The server failed to connect to data port";
  76. case CURLE_FTP_ACCEPT_TIMEOUT:
  77. return "FTP: Accepting server connect has timed out";
  78. case CURLE_FTP_PRET_FAILED:
  79. return "FTP: The server did not accept the PRET command.";
  80. case CURLE_FTP_WEIRD_PASS_REPLY:
  81. return "FTP: unknown PASS reply";
  82. case CURLE_FTP_WEIRD_PASV_REPLY:
  83. return "FTP: unknown PASV reply";
  84. case CURLE_FTP_WEIRD_227_FORMAT:
  85. return "FTP: unknown 227 response format";
  86. case CURLE_FTP_CANT_GET_HOST:
  87. return "FTP: can't figure out the host in the PASV response";
  88. case CURLE_HTTP2:
  89. return "Error in the HTTP2 framing layer";
  90. case CURLE_FTP_COULDNT_SET_TYPE:
  91. return "FTP: couldn't set file type";
  92. case CURLE_PARTIAL_FILE:
  93. return "Transferred a partial file";
  94. case CURLE_FTP_COULDNT_RETR_FILE:
  95. return "FTP: couldn't retrieve (RETR failed) the specified file";
  96. case CURLE_QUOTE_ERROR:
  97. return "Quote command returned error";
  98. case CURLE_HTTP_RETURNED_ERROR:
  99. return "HTTP response code said error";
  100. case CURLE_WRITE_ERROR:
  101. return "Failed writing received data to disk/application";
  102. case CURLE_UPLOAD_FAILED:
  103. return "Upload failed (at start/before it took off)";
  104. case CURLE_READ_ERROR:
  105. return "Failed to open/read local data from file/application";
  106. case CURLE_OUT_OF_MEMORY:
  107. return "Out of memory";
  108. case CURLE_OPERATION_TIMEDOUT:
  109. return "Timeout was reached";
  110. case CURLE_FTP_PORT_FAILED:
  111. return "FTP: command PORT failed";
  112. case CURLE_FTP_COULDNT_USE_REST:
  113. return "FTP: command REST failed";
  114. case CURLE_RANGE_ERROR:
  115. return "Requested range was not delivered by the server";
  116. case CURLE_HTTP_POST_ERROR:
  117. return "Internal problem setting up the POST";
  118. case CURLE_SSL_CONNECT_ERROR:
  119. return "SSL connect error";
  120. case CURLE_BAD_DOWNLOAD_RESUME:
  121. return "Couldn't resume download";
  122. case CURLE_FILE_COULDNT_READ_FILE:
  123. return "Couldn't read a file:// file";
  124. case CURLE_LDAP_CANNOT_BIND:
  125. return "LDAP: cannot bind";
  126. case CURLE_LDAP_SEARCH_FAILED:
  127. return "LDAP: search failed";
  128. case CURLE_FUNCTION_NOT_FOUND:
  129. return "A required function in the library was not found";
  130. case CURLE_ABORTED_BY_CALLBACK:
  131. return "Operation was aborted by an application callback";
  132. case CURLE_BAD_FUNCTION_ARGUMENT:
  133. return "A libcurl function was given a bad argument";
  134. case CURLE_INTERFACE_FAILED:
  135. return "Failed binding local connection end";
  136. case CURLE_TOO_MANY_REDIRECTS :
  137. return "Number of redirects hit maximum amount";
  138. case CURLE_UNKNOWN_OPTION:
  139. return "An unknown option was passed in to libcurl";
  140. case CURLE_TELNET_OPTION_SYNTAX :
  141. return "Malformed telnet option";
  142. case CURLE_PEER_FAILED_VERIFICATION:
  143. return "SSL peer certificate or SSH remote key was not OK";
  144. case CURLE_GOT_NOTHING:
  145. return "Server returned nothing (no headers, no data)";
  146. case CURLE_SSL_ENGINE_NOTFOUND:
  147. return "SSL crypto engine not found";
  148. case CURLE_SSL_ENGINE_SETFAILED:
  149. return "Can not set SSL crypto engine as default";
  150. case CURLE_SSL_ENGINE_INITFAILED:
  151. return "Failed to initialise SSL crypto engine";
  152. case CURLE_SEND_ERROR:
  153. return "Failed sending data to the peer";
  154. case CURLE_RECV_ERROR:
  155. return "Failure when receiving data from the peer";
  156. case CURLE_SSL_CERTPROBLEM:
  157. return "Problem with the local SSL certificate";
  158. case CURLE_SSL_CIPHER:
  159. return "Couldn't use specified SSL cipher";
  160. case CURLE_SSL_CACERT:
  161. return "Peer certificate cannot be authenticated with given CA "
  162. "certificates";
  163. case CURLE_SSL_CACERT_BADFILE:
  164. return "Problem with the SSL CA cert (path? access rights?)";
  165. case CURLE_BAD_CONTENT_ENCODING:
  166. return "Unrecognized or bad HTTP Content or Transfer-Encoding";
  167. case CURLE_LDAP_INVALID_URL:
  168. return "Invalid LDAP URL";
  169. case CURLE_FILESIZE_EXCEEDED:
  170. return "Maximum file size exceeded";
  171. case CURLE_USE_SSL_FAILED:
  172. return "Requested SSL level failed";
  173. case CURLE_SSL_SHUTDOWN_FAILED:
  174. return "Failed to shut down the SSL connection";
  175. case CURLE_SSL_CRL_BADFILE:
  176. return "Failed to load CRL file (path? access rights?, format?)";
  177. case CURLE_SSL_ISSUER_ERROR:
  178. return "Issuer check against peer certificate failed";
  179. case CURLE_SEND_FAIL_REWIND:
  180. return "Send failed since rewinding of the data stream failed";
  181. case CURLE_LOGIN_DENIED:
  182. return "Login denied";
  183. case CURLE_TFTP_NOTFOUND:
  184. return "TFTP: File Not Found";
  185. case CURLE_TFTP_PERM:
  186. return "TFTP: Access Violation";
  187. case CURLE_REMOTE_DISK_FULL:
  188. return "Disk full or allocation exceeded";
  189. case CURLE_TFTP_ILLEGAL:
  190. return "TFTP: Illegal operation";
  191. case CURLE_TFTP_UNKNOWNID:
  192. return "TFTP: Unknown transfer ID";
  193. case CURLE_REMOTE_FILE_EXISTS:
  194. return "Remote file already exists";
  195. case CURLE_TFTP_NOSUCHUSER:
  196. return "TFTP: No such user";
  197. case CURLE_CONV_FAILED:
  198. return "Conversion failed";
  199. case CURLE_CONV_REQD:
  200. return "Caller must register CURLOPT_CONV_ callback options";
  201. case CURLE_REMOTE_FILE_NOT_FOUND:
  202. return "Remote file not found";
  203. case CURLE_SSH:
  204. return "Error in the SSH layer";
  205. case CURLE_AGAIN:
  206. return "Socket not ready for send/recv";
  207. case CURLE_RTSP_CSEQ_ERROR:
  208. return "RTSP CSeq mismatch or invalid CSeq";
  209. case CURLE_RTSP_SESSION_ERROR:
  210. return "RTSP session error";
  211. case CURLE_FTP_BAD_FILE_LIST:
  212. return "Unable to parse FTP file list";
  213. case CURLE_CHUNK_FAILED:
  214. return "Chunk callback failed";
  215. case CURLE_NO_CONNECTION_AVAILABLE:
  216. return "The max connection limit is reached";
  217. case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
  218. return "SSL public key does not match pinned public key";
  219. case CURLE_SSL_INVALIDCERTSTATUS:
  220. return "SSL server certificate status verification FAILED";
  221. case CURLE_HTTP2_STREAM:
  222. return "Stream error in the HTTP/2 framing layer";
  223. case CURLE_RECURSIVE_API_CALL:
  224. return "API function called from within callback";
  225. /* error codes not used by current libcurl */
  226. case CURLE_OBSOLETE20:
  227. case CURLE_OBSOLETE24:
  228. case CURLE_OBSOLETE29:
  229. case CURLE_OBSOLETE32:
  230. case CURLE_OBSOLETE40:
  231. case CURLE_OBSOLETE44:
  232. case CURLE_OBSOLETE46:
  233. case CURLE_OBSOLETE50:
  234. case CURLE_OBSOLETE57:
  235. case CURL_LAST:
  236. break;
  237. }
  238. /*
  239. * By using a switch, gcc -Wall will complain about enum values
  240. * which do not appear, helping keep this function up-to-date.
  241. * By using gcc -Wall -Werror, you can't forget.
  242. *
  243. * A table would not have the same benefit. Most compilers will
  244. * generate code very similar to a table in any case, so there
  245. * is little performance gain from a table. And something is broken
  246. * for the user's application, anyways, so does it matter how fast
  247. * it _doesn't_ work?
  248. *
  249. * The line number for the error will be near this comment, which
  250. * is why it is here, and not at the start of the switch.
  251. */
  252. return "Unknown error";
  253. #else
  254. if(!error)
  255. return "No error";
  256. else
  257. return "Error";
  258. #endif
  259. }
  260. const char *
  261. curl_multi_strerror(CURLMcode error)
  262. {
  263. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  264. switch(error) {
  265. case CURLM_CALL_MULTI_PERFORM:
  266. return "Please call curl_multi_perform() soon";
  267. case CURLM_OK:
  268. return "No error";
  269. case CURLM_BAD_HANDLE:
  270. return "Invalid multi handle";
  271. case CURLM_BAD_EASY_HANDLE:
  272. return "Invalid easy handle";
  273. case CURLM_OUT_OF_MEMORY:
  274. return "Out of memory";
  275. case CURLM_INTERNAL_ERROR:
  276. return "Internal error";
  277. case CURLM_BAD_SOCKET:
  278. return "Invalid socket argument";
  279. case CURLM_UNKNOWN_OPTION:
  280. return "Unknown option";
  281. case CURLM_ADDED_ALREADY:
  282. return "The easy handle is already added to a multi handle";
  283. case CURLM_RECURSIVE_API_CALL:
  284. return "API function called from within callback";
  285. case CURLM_LAST:
  286. break;
  287. }
  288. return "Unknown error";
  289. #else
  290. if(error == CURLM_OK)
  291. return "No error";
  292. else
  293. return "Error";
  294. #endif
  295. }
  296. const char *
  297. curl_share_strerror(CURLSHcode error)
  298. {
  299. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  300. switch(error) {
  301. case CURLSHE_OK:
  302. return "No error";
  303. case CURLSHE_BAD_OPTION:
  304. return "Unknown share option";
  305. case CURLSHE_IN_USE:
  306. return "Share currently in use";
  307. case CURLSHE_INVALID:
  308. return "Invalid share handle";
  309. case CURLSHE_NOMEM:
  310. return "Out of memory";
  311. case CURLSHE_NOT_BUILT_IN:
  312. return "Feature not enabled in this library";
  313. case CURLSHE_LAST:
  314. break;
  315. }
  316. return "CURLSHcode unknown";
  317. #else
  318. if(error == CURLSHE_OK)
  319. return "No error";
  320. else
  321. return "Error";
  322. #endif
  323. }
  324. #ifdef USE_WINSOCK
  325. /* This function handles most / all (?) Winsock errors curl is able to produce.
  326. */
  327. static const char *
  328. get_winsock_error (int err, char *buf, size_t len)
  329. {
  330. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  331. DWORD old_win_err = GetLastError();
  332. #endif
  333. int old_errno = errno;
  334. const char *p;
  335. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  336. switch(err) {
  337. case WSAEINTR:
  338. p = "Call interrupted";
  339. break;
  340. case WSAEBADF:
  341. p = "Bad file";
  342. break;
  343. case WSAEACCES:
  344. p = "Bad access";
  345. break;
  346. case WSAEFAULT:
  347. p = "Bad argument";
  348. break;
  349. case WSAEINVAL:
  350. p = "Invalid arguments";
  351. break;
  352. case WSAEMFILE:
  353. p = "Out of file descriptors";
  354. break;
  355. case WSAEWOULDBLOCK:
  356. p = "Call would block";
  357. break;
  358. case WSAEINPROGRESS:
  359. case WSAEALREADY:
  360. p = "Blocking call in progress";
  361. break;
  362. case WSAENOTSOCK:
  363. p = "Descriptor is not a socket";
  364. break;
  365. case WSAEDESTADDRREQ:
  366. p = "Need destination address";
  367. break;
  368. case WSAEMSGSIZE:
  369. p = "Bad message size";
  370. break;
  371. case WSAEPROTOTYPE:
  372. p = "Bad protocol";
  373. break;
  374. case WSAENOPROTOOPT:
  375. p = "Protocol option is unsupported";
  376. break;
  377. case WSAEPROTONOSUPPORT:
  378. p = "Protocol is unsupported";
  379. break;
  380. case WSAESOCKTNOSUPPORT:
  381. p = "Socket is unsupported";
  382. break;
  383. case WSAEOPNOTSUPP:
  384. p = "Operation not supported";
  385. break;
  386. case WSAEAFNOSUPPORT:
  387. p = "Address family not supported";
  388. break;
  389. case WSAEPFNOSUPPORT:
  390. p = "Protocol family not supported";
  391. break;
  392. case WSAEADDRINUSE:
  393. p = "Address already in use";
  394. break;
  395. case WSAEADDRNOTAVAIL:
  396. p = "Address not available";
  397. break;
  398. case WSAENETDOWN:
  399. p = "Network down";
  400. break;
  401. case WSAENETUNREACH:
  402. p = "Network unreachable";
  403. break;
  404. case WSAENETRESET:
  405. p = "Network has been reset";
  406. break;
  407. case WSAECONNABORTED:
  408. p = "Connection was aborted";
  409. break;
  410. case WSAECONNRESET:
  411. p = "Connection was reset";
  412. break;
  413. case WSAENOBUFS:
  414. p = "No buffer space";
  415. break;
  416. case WSAEISCONN:
  417. p = "Socket is already connected";
  418. break;
  419. case WSAENOTCONN:
  420. p = "Socket is not connected";
  421. break;
  422. case WSAESHUTDOWN:
  423. p = "Socket has been shut down";
  424. break;
  425. case WSAETOOMANYREFS:
  426. p = "Too many references";
  427. break;
  428. case WSAETIMEDOUT:
  429. p = "Timed out";
  430. break;
  431. case WSAECONNREFUSED:
  432. p = "Connection refused";
  433. break;
  434. case WSAELOOP:
  435. p = "Loop??";
  436. break;
  437. case WSAENAMETOOLONG:
  438. p = "Name too long";
  439. break;
  440. case WSAEHOSTDOWN:
  441. p = "Host down";
  442. break;
  443. case WSAEHOSTUNREACH:
  444. p = "Host unreachable";
  445. break;
  446. case WSAENOTEMPTY:
  447. p = "Not empty";
  448. break;
  449. case WSAEPROCLIM:
  450. p = "Process limit reached";
  451. break;
  452. case WSAEUSERS:
  453. p = "Too many users";
  454. break;
  455. case WSAEDQUOT:
  456. p = "Bad quota";
  457. break;
  458. case WSAESTALE:
  459. p = "Something is stale";
  460. break;
  461. case WSAEREMOTE:
  462. p = "Remote error";
  463. break;
  464. #ifdef WSAEDISCON /* missing in SalfordC! */
  465. case WSAEDISCON:
  466. p = "Disconnected";
  467. break;
  468. #endif
  469. /* Extended Winsock errors */
  470. case WSASYSNOTREADY:
  471. p = "Winsock library is not ready";
  472. break;
  473. case WSANOTINITIALISED:
  474. p = "Winsock library not initialised";
  475. break;
  476. case WSAVERNOTSUPPORTED:
  477. p = "Winsock version not supported";
  478. break;
  479. /* getXbyY() errors (already handled in herrmsg):
  480. * Authoritative Answer: Host not found */
  481. case WSAHOST_NOT_FOUND:
  482. p = "Host not found";
  483. break;
  484. /* Non-Authoritative: Host not found, or SERVERFAIL */
  485. case WSATRY_AGAIN:
  486. p = "Host not found, try again";
  487. break;
  488. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  489. case WSANO_RECOVERY:
  490. p = "Unrecoverable error in call to nameserver";
  491. break;
  492. /* Valid name, no data record of requested type */
  493. case WSANO_DATA:
  494. p = "No data record of requested type";
  495. break;
  496. default:
  497. return NULL;
  498. }
  499. #else
  500. if(!err)
  501. return NULL;
  502. else
  503. p = "error";
  504. #endif
  505. strncpy(buf, p, len);
  506. buf [len-1] = '\0';
  507. if(errno != old_errno)
  508. errno = old_errno;
  509. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  510. if(old_win_err != GetLastError())
  511. SetLastError(old_win_err);
  512. #endif
  513. return buf;
  514. }
  515. #endif /* USE_WINSOCK */
  516. /*
  517. * Our thread-safe and smart strerror() replacement.
  518. *
  519. * The 'err' argument passed in to this function MUST be a true errno number
  520. * as reported on this system. We do no range checking on the number before
  521. * we pass it to the "number-to-message" conversion function and there might
  522. * be systems that don't do proper range checking in there themselves.
  523. *
  524. * We don't do range checking (on systems other than Windows) since there is
  525. * no good reliable and portable way to do it.
  526. */
  527. const char *Curl_strerror(struct connectdata *conn, int err)
  528. {
  529. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  530. DWORD old_win_err = GetLastError();
  531. #endif
  532. int old_errno = errno;
  533. char *buf, *p;
  534. size_t max;
  535. DEBUGASSERT(conn);
  536. DEBUGASSERT(err >= 0);
  537. buf = conn->syserr_buf;
  538. max = sizeof(conn->syserr_buf)-1;
  539. *buf = '\0';
  540. #ifdef USE_WINSOCK
  541. #ifdef _WIN32_WCE
  542. {
  543. wchar_t wbuf[256];
  544. wbuf[0] = L'\0';
  545. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  546. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
  547. wcstombs(buf, wbuf, max);
  548. }
  549. #else
  550. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  551. if(err >= 0 && err < sys_nerr)
  552. strncpy(buf, strerror(err), max);
  553. else {
  554. if(!get_winsock_error(err, buf, max) &&
  555. !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  556. LANG_NEUTRAL, buf, (DWORD)max, NULL))
  557. snprintf(buf, max, "Unknown error %d (%#x)", err, err);
  558. }
  559. #endif
  560. #else /* not USE_WINSOCK coming up */
  561. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  562. /*
  563. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  564. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  565. * message string, or EINVAL if 'errnum' is not a valid error number.
  566. */
  567. if(0 != strerror_r(err, buf, max)) {
  568. if('\0' == buf[0])
  569. snprintf(buf, max, "Unknown error %d", err);
  570. }
  571. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  572. /*
  573. * The glibc-style strerror_r() only *might* use the buffer we pass to
  574. * the function, but it always returns the error message as a pointer,
  575. * so we must copy that string unconditionally (if non-NULL).
  576. */
  577. {
  578. char buffer[256];
  579. char *msg = strerror_r(err, buffer, sizeof(buffer));
  580. if(msg)
  581. strncpy(buf, msg, max);
  582. else
  583. snprintf(buf, max, "Unknown error %d", err);
  584. }
  585. #elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
  586. /*
  587. * The vxworks-style strerror_r() does use the buffer we pass to the function.
  588. * The buffer size should be at least NAME_MAX (256)
  589. */
  590. {
  591. char buffer[256];
  592. if(OK == strerror_r(err, buffer))
  593. strncpy(buf, buffer, max);
  594. else
  595. snprintf(buf, max, "Unknown error %d", err);
  596. }
  597. #else
  598. {
  599. char *msg = strerror(err);
  600. if(msg)
  601. strncpy(buf, msg, max);
  602. else
  603. snprintf(buf, max, "Unknown error %d", err);
  604. }
  605. #endif
  606. #endif /* end of ! USE_WINSOCK */
  607. buf[max] = '\0'; /* make sure the string is zero terminated */
  608. /* strip trailing '\r\n' or '\n'. */
  609. p = strrchr(buf, '\n');
  610. if(p && (p - buf) >= 2)
  611. *p = '\0';
  612. p = strrchr(buf, '\r');
  613. if(p && (p - buf) >= 1)
  614. *p = '\0';
  615. if(errno != old_errno)
  616. errno = old_errno;
  617. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  618. if(old_win_err != GetLastError())
  619. SetLastError(old_win_err);
  620. #endif
  621. return buf;
  622. }
  623. #ifdef USE_WINDOWS_SSPI
  624. const char *Curl_sspi_strerror (struct connectdata *conn, int err)
  625. {
  626. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  627. DWORD old_win_err = GetLastError();
  628. #endif
  629. int old_errno = errno;
  630. const char *txt;
  631. char *outbuf;
  632. size_t outmax;
  633. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  634. char txtbuf[80];
  635. char msgbuf[sizeof(conn->syserr_buf)];
  636. char *p, *str, *msg = NULL;
  637. bool msg_formatted = FALSE;
  638. #endif
  639. DEBUGASSERT(conn);
  640. outbuf = conn->syserr_buf;
  641. outmax = sizeof(conn->syserr_buf)-1;
  642. *outbuf = '\0';
  643. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  644. switch(err) {
  645. case SEC_E_OK:
  646. txt = "No error";
  647. break;
  648. case CRYPT_E_REVOKED:
  649. txt = "CRYPT_E_REVOKED";
  650. break;
  651. case SEC_E_ALGORITHM_MISMATCH:
  652. txt = "SEC_E_ALGORITHM_MISMATCH";
  653. break;
  654. case SEC_E_BAD_BINDINGS:
  655. txt = "SEC_E_BAD_BINDINGS";
  656. break;
  657. case SEC_E_BAD_PKGID:
  658. txt = "SEC_E_BAD_PKGID";
  659. break;
  660. case SEC_E_BUFFER_TOO_SMALL:
  661. txt = "SEC_E_BUFFER_TOO_SMALL";
  662. break;
  663. case SEC_E_CANNOT_INSTALL:
  664. txt = "SEC_E_CANNOT_INSTALL";
  665. break;
  666. case SEC_E_CANNOT_PACK:
  667. txt = "SEC_E_CANNOT_PACK";
  668. break;
  669. case SEC_E_CERT_EXPIRED:
  670. txt = "SEC_E_CERT_EXPIRED";
  671. break;
  672. case SEC_E_CERT_UNKNOWN:
  673. txt = "SEC_E_CERT_UNKNOWN";
  674. break;
  675. case SEC_E_CERT_WRONG_USAGE:
  676. txt = "SEC_E_CERT_WRONG_USAGE";
  677. break;
  678. case SEC_E_CONTEXT_EXPIRED:
  679. txt = "SEC_E_CONTEXT_EXPIRED";
  680. break;
  681. case SEC_E_CROSSREALM_DELEGATION_FAILURE:
  682. txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
  683. break;
  684. case SEC_E_CRYPTO_SYSTEM_INVALID:
  685. txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
  686. break;
  687. case SEC_E_DECRYPT_FAILURE:
  688. txt = "SEC_E_DECRYPT_FAILURE";
  689. break;
  690. case SEC_E_DELEGATION_POLICY:
  691. txt = "SEC_E_DELEGATION_POLICY";
  692. break;
  693. case SEC_E_DELEGATION_REQUIRED:
  694. txt = "SEC_E_DELEGATION_REQUIRED";
  695. break;
  696. case SEC_E_DOWNGRADE_DETECTED:
  697. txt = "SEC_E_DOWNGRADE_DETECTED";
  698. break;
  699. case SEC_E_ENCRYPT_FAILURE:
  700. txt = "SEC_E_ENCRYPT_FAILURE";
  701. break;
  702. case SEC_E_ILLEGAL_MESSAGE:
  703. txt = "SEC_E_ILLEGAL_MESSAGE";
  704. break;
  705. case SEC_E_INCOMPLETE_CREDENTIALS:
  706. txt = "SEC_E_INCOMPLETE_CREDENTIALS";
  707. break;
  708. case SEC_E_INCOMPLETE_MESSAGE:
  709. txt = "SEC_E_INCOMPLETE_MESSAGE";
  710. break;
  711. case SEC_E_INSUFFICIENT_MEMORY:
  712. txt = "SEC_E_INSUFFICIENT_MEMORY";
  713. break;
  714. case SEC_E_INTERNAL_ERROR:
  715. txt = "SEC_E_INTERNAL_ERROR";
  716. break;
  717. case SEC_E_INVALID_HANDLE:
  718. txt = "SEC_E_INVALID_HANDLE";
  719. break;
  720. case SEC_E_INVALID_PARAMETER:
  721. txt = "SEC_E_INVALID_PARAMETER";
  722. break;
  723. case SEC_E_INVALID_TOKEN:
  724. txt = "SEC_E_INVALID_TOKEN";
  725. break;
  726. case SEC_E_ISSUING_CA_UNTRUSTED:
  727. txt = "SEC_E_ISSUING_CA_UNTRUSTED";
  728. break;
  729. case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
  730. txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
  731. break;
  732. case SEC_E_KDC_CERT_EXPIRED:
  733. txt = "SEC_E_KDC_CERT_EXPIRED";
  734. break;
  735. case SEC_E_KDC_CERT_REVOKED:
  736. txt = "SEC_E_KDC_CERT_REVOKED";
  737. break;
  738. case SEC_E_KDC_INVALID_REQUEST:
  739. txt = "SEC_E_KDC_INVALID_REQUEST";
  740. break;
  741. case SEC_E_KDC_UNABLE_TO_REFER:
  742. txt = "SEC_E_KDC_UNABLE_TO_REFER";
  743. break;
  744. case SEC_E_KDC_UNKNOWN_ETYPE:
  745. txt = "SEC_E_KDC_UNKNOWN_ETYPE";
  746. break;
  747. case SEC_E_LOGON_DENIED:
  748. txt = "SEC_E_LOGON_DENIED";
  749. break;
  750. case SEC_E_MAX_REFERRALS_EXCEEDED:
  751. txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
  752. break;
  753. case SEC_E_MESSAGE_ALTERED:
  754. txt = "SEC_E_MESSAGE_ALTERED";
  755. break;
  756. case SEC_E_MULTIPLE_ACCOUNTS:
  757. txt = "SEC_E_MULTIPLE_ACCOUNTS";
  758. break;
  759. case SEC_E_MUST_BE_KDC:
  760. txt = "SEC_E_MUST_BE_KDC";
  761. break;
  762. case SEC_E_NOT_OWNER:
  763. txt = "SEC_E_NOT_OWNER";
  764. break;
  765. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  766. txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
  767. break;
  768. case SEC_E_NO_CREDENTIALS:
  769. txt = "SEC_E_NO_CREDENTIALS";
  770. break;
  771. case SEC_E_NO_IMPERSONATION:
  772. txt = "SEC_E_NO_IMPERSONATION";
  773. break;
  774. case SEC_E_NO_IP_ADDRESSES:
  775. txt = "SEC_E_NO_IP_ADDRESSES";
  776. break;
  777. case SEC_E_NO_KERB_KEY:
  778. txt = "SEC_E_NO_KERB_KEY";
  779. break;
  780. case SEC_E_NO_PA_DATA:
  781. txt = "SEC_E_NO_PA_DATA";
  782. break;
  783. case SEC_E_NO_S4U_PROT_SUPPORT:
  784. txt = "SEC_E_NO_S4U_PROT_SUPPORT";
  785. break;
  786. case SEC_E_NO_TGT_REPLY:
  787. txt = "SEC_E_NO_TGT_REPLY";
  788. break;
  789. case SEC_E_OUT_OF_SEQUENCE:
  790. txt = "SEC_E_OUT_OF_SEQUENCE";
  791. break;
  792. case SEC_E_PKINIT_CLIENT_FAILURE:
  793. txt = "SEC_E_PKINIT_CLIENT_FAILURE";
  794. break;
  795. case SEC_E_PKINIT_NAME_MISMATCH:
  796. txt = "SEC_E_PKINIT_NAME_MISMATCH";
  797. break;
  798. case SEC_E_POLICY_NLTM_ONLY:
  799. txt = "SEC_E_POLICY_NLTM_ONLY";
  800. break;
  801. case SEC_E_QOP_NOT_SUPPORTED:
  802. txt = "SEC_E_QOP_NOT_SUPPORTED";
  803. break;
  804. case SEC_E_REVOCATION_OFFLINE_C:
  805. txt = "SEC_E_REVOCATION_OFFLINE_C";
  806. break;
  807. case SEC_E_REVOCATION_OFFLINE_KDC:
  808. txt = "SEC_E_REVOCATION_OFFLINE_KDC";
  809. break;
  810. case SEC_E_SECPKG_NOT_FOUND:
  811. txt = "SEC_E_SECPKG_NOT_FOUND";
  812. break;
  813. case SEC_E_SECURITY_QOS_FAILED:
  814. txt = "SEC_E_SECURITY_QOS_FAILED";
  815. break;
  816. case SEC_E_SHUTDOWN_IN_PROGRESS:
  817. txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
  818. break;
  819. case SEC_E_SMARTCARD_CERT_EXPIRED:
  820. txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
  821. break;
  822. case SEC_E_SMARTCARD_CERT_REVOKED:
  823. txt = "SEC_E_SMARTCARD_CERT_REVOKED";
  824. break;
  825. case SEC_E_SMARTCARD_LOGON_REQUIRED:
  826. txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
  827. break;
  828. case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
  829. txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
  830. break;
  831. case SEC_E_TARGET_UNKNOWN:
  832. txt = "SEC_E_TARGET_UNKNOWN";
  833. break;
  834. case SEC_E_TIME_SKEW:
  835. txt = "SEC_E_TIME_SKEW";
  836. break;
  837. case SEC_E_TOO_MANY_PRINCIPALS:
  838. txt = "SEC_E_TOO_MANY_PRINCIPALS";
  839. break;
  840. case SEC_E_UNFINISHED_CONTEXT_DELETED:
  841. txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
  842. break;
  843. case SEC_E_UNKNOWN_CREDENTIALS:
  844. txt = "SEC_E_UNKNOWN_CREDENTIALS";
  845. break;
  846. case SEC_E_UNSUPPORTED_FUNCTION:
  847. txt = "SEC_E_UNSUPPORTED_FUNCTION";
  848. break;
  849. case SEC_E_UNSUPPORTED_PREAUTH:
  850. txt = "SEC_E_UNSUPPORTED_PREAUTH";
  851. break;
  852. case SEC_E_UNTRUSTED_ROOT:
  853. txt = "SEC_E_UNTRUSTED_ROOT";
  854. break;
  855. case SEC_E_WRONG_CREDENTIAL_HANDLE:
  856. txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
  857. break;
  858. case SEC_E_WRONG_PRINCIPAL:
  859. txt = "SEC_E_WRONG_PRINCIPAL";
  860. break;
  861. case SEC_I_COMPLETE_AND_CONTINUE:
  862. txt = "SEC_I_COMPLETE_AND_CONTINUE";
  863. break;
  864. case SEC_I_COMPLETE_NEEDED:
  865. txt = "SEC_I_COMPLETE_NEEDED";
  866. break;
  867. case SEC_I_CONTEXT_EXPIRED:
  868. txt = "SEC_I_CONTEXT_EXPIRED";
  869. break;
  870. case SEC_I_CONTINUE_NEEDED:
  871. txt = "SEC_I_CONTINUE_NEEDED";
  872. break;
  873. case SEC_I_INCOMPLETE_CREDENTIALS:
  874. txt = "SEC_I_INCOMPLETE_CREDENTIALS";
  875. break;
  876. case SEC_I_LOCAL_LOGON:
  877. txt = "SEC_I_LOCAL_LOGON";
  878. break;
  879. case SEC_I_NO_LSA_CONTEXT:
  880. txt = "SEC_I_NO_LSA_CONTEXT";
  881. break;
  882. case SEC_I_RENEGOTIATE:
  883. txt = "SEC_I_RENEGOTIATE";
  884. break;
  885. case SEC_I_SIGNATURE_NEEDED:
  886. txt = "SEC_I_SIGNATURE_NEEDED";
  887. break;
  888. default:
  889. txt = "Unknown error";
  890. }
  891. if(err == SEC_E_OK)
  892. strncpy(outbuf, txt, outmax);
  893. else if(err == SEC_E_ILLEGAL_MESSAGE)
  894. snprintf(outbuf, outmax,
  895. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  896. "when a fatal SSL/TLS alert is received (e.g. handshake failed). "
  897. "More detail may be available in the Windows System event log.",
  898. err);
  899. else {
  900. str = txtbuf;
  901. snprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
  902. txtbuf[sizeof(txtbuf)-1] = '\0';
  903. #ifdef _WIN32_WCE
  904. {
  905. wchar_t wbuf[256];
  906. wbuf[0] = L'\0';
  907. if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  908. FORMAT_MESSAGE_IGNORE_INSERTS,
  909. NULL, err, LANG_NEUTRAL,
  910. wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  911. wcstombs(msgbuf, wbuf, sizeof(msgbuf)-1);
  912. msg_formatted = TRUE;
  913. }
  914. }
  915. #else
  916. if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  917. FORMAT_MESSAGE_IGNORE_INSERTS,
  918. NULL, err, LANG_NEUTRAL,
  919. msgbuf, sizeof(msgbuf)-1, NULL)) {
  920. msg_formatted = TRUE;
  921. }
  922. #endif
  923. if(msg_formatted) {
  924. msgbuf[sizeof(msgbuf)-1] = '\0';
  925. /* strip trailing '\r\n' or '\n' */
  926. p = strrchr(msgbuf, '\n');
  927. if(p && (p - msgbuf) >= 2)
  928. *p = '\0';
  929. p = strrchr(msgbuf, '\r');
  930. if(p && (p - msgbuf) >= 1)
  931. *p = '\0';
  932. msg = msgbuf;
  933. }
  934. if(msg)
  935. snprintf(outbuf, outmax, "%s - %s", str, msg);
  936. else
  937. strncpy(outbuf, str, outmax);
  938. }
  939. #else
  940. if(err == SEC_E_OK)
  941. txt = "No error";
  942. else
  943. txt = "Error";
  944. strncpy(outbuf, txt, outmax);
  945. #endif
  946. outbuf[outmax] = '\0';
  947. if(errno != old_errno)
  948. errno = old_errno;
  949. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  950. if(old_win_err != GetLastError())
  951. SetLastError(old_win_err);
  952. #endif
  953. return outbuf;
  954. }
  955. #endif /* USE_WINDOWS_SSPI */