strerror.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004 - 2020, 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.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_GOT_NOTHING:
  143. return "Server returned nothing (no headers, no data)";
  144. case CURLE_SSL_ENGINE_NOTFOUND:
  145. return "SSL crypto engine not found";
  146. case CURLE_SSL_ENGINE_SETFAILED:
  147. return "Can not set SSL crypto engine as default";
  148. case CURLE_SSL_ENGINE_INITFAILED:
  149. return "Failed to initialise SSL crypto engine";
  150. case CURLE_SEND_ERROR:
  151. return "Failed sending data to the peer";
  152. case CURLE_RECV_ERROR:
  153. return "Failure when receiving data from the peer";
  154. case CURLE_SSL_CERTPROBLEM:
  155. return "Problem with the local SSL certificate";
  156. case CURLE_SSL_CIPHER:
  157. return "Couldn't use specified SSL cipher";
  158. case CURLE_PEER_FAILED_VERIFICATION:
  159. return "SSL peer certificate or SSH remote key was not OK";
  160. case CURLE_SSL_CACERT_BADFILE:
  161. return "Problem with the SSL CA cert (path? access rights?)";
  162. case CURLE_BAD_CONTENT_ENCODING:
  163. return "Unrecognized or bad HTTP Content or Transfer-Encoding";
  164. case CURLE_LDAP_INVALID_URL:
  165. return "Invalid LDAP URL";
  166. case CURLE_FILESIZE_EXCEEDED:
  167. return "Maximum file size exceeded";
  168. case CURLE_USE_SSL_FAILED:
  169. return "Requested SSL level failed";
  170. case CURLE_SSL_SHUTDOWN_FAILED:
  171. return "Failed to shut down the SSL connection";
  172. case CURLE_SSL_CRL_BADFILE:
  173. return "Failed to load CRL file (path? access rights?, format?)";
  174. case CURLE_SSL_ISSUER_ERROR:
  175. return "Issuer check against peer certificate failed";
  176. case CURLE_SEND_FAIL_REWIND:
  177. return "Send failed since rewinding of the data stream failed";
  178. case CURLE_LOGIN_DENIED:
  179. return "Login denied";
  180. case CURLE_TFTP_NOTFOUND:
  181. return "TFTP: File Not Found";
  182. case CURLE_TFTP_PERM:
  183. return "TFTP: Access Violation";
  184. case CURLE_REMOTE_DISK_FULL:
  185. return "Disk full or allocation exceeded";
  186. case CURLE_TFTP_ILLEGAL:
  187. return "TFTP: Illegal operation";
  188. case CURLE_TFTP_UNKNOWNID:
  189. return "TFTP: Unknown transfer ID";
  190. case CURLE_REMOTE_FILE_EXISTS:
  191. return "Remote file already exists";
  192. case CURLE_TFTP_NOSUCHUSER:
  193. return "TFTP: No such user";
  194. case CURLE_CONV_FAILED:
  195. return "Conversion failed";
  196. case CURLE_CONV_REQD:
  197. return "Caller must register CURLOPT_CONV_ callback options";
  198. case CURLE_REMOTE_FILE_NOT_FOUND:
  199. return "Remote file not found";
  200. case CURLE_SSH:
  201. return "Error in the SSH layer";
  202. case CURLE_AGAIN:
  203. return "Socket not ready for send/recv";
  204. case CURLE_RTSP_CSEQ_ERROR:
  205. return "RTSP CSeq mismatch or invalid CSeq";
  206. case CURLE_RTSP_SESSION_ERROR:
  207. return "RTSP session error";
  208. case CURLE_FTP_BAD_FILE_LIST:
  209. return "Unable to parse FTP file list";
  210. case CURLE_CHUNK_FAILED:
  211. return "Chunk callback failed";
  212. case CURLE_NO_CONNECTION_AVAILABLE:
  213. return "The max connection limit is reached";
  214. case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
  215. return "SSL public key does not match pinned public key";
  216. case CURLE_SSL_INVALIDCERTSTATUS:
  217. return "SSL server certificate status verification FAILED";
  218. case CURLE_HTTP2_STREAM:
  219. return "Stream error in the HTTP/2 framing layer";
  220. case CURLE_RECURSIVE_API_CALL:
  221. return "API function called from within callback";
  222. case CURLE_AUTH_ERROR:
  223. return "An authentication function returned an error";
  224. case CURLE_HTTP3:
  225. return "HTTP/3 error";
  226. case CURLE_QUIC_CONNECT_ERROR:
  227. return "QUIC connection error";
  228. case CURLE_PROXY:
  229. return "proxy handshake error";
  230. /* error codes not used by current libcurl */
  231. case CURLE_OBSOLETE20:
  232. case CURLE_OBSOLETE24:
  233. case CURLE_OBSOLETE29:
  234. case CURLE_OBSOLETE32:
  235. case CURLE_OBSOLETE40:
  236. case CURLE_OBSOLETE44:
  237. case CURLE_OBSOLETE46:
  238. case CURLE_OBSOLETE50:
  239. case CURLE_OBSOLETE51:
  240. case CURLE_OBSOLETE57:
  241. case CURL_LAST:
  242. break;
  243. }
  244. /*
  245. * By using a switch, gcc -Wall will complain about enum values
  246. * which do not appear, helping keep this function up-to-date.
  247. * By using gcc -Wall -Werror, you can't forget.
  248. *
  249. * A table would not have the same benefit. Most compilers will
  250. * generate code very similar to a table in any case, so there
  251. * is little performance gain from a table. And something is broken
  252. * for the user's application, anyways, so does it matter how fast
  253. * it _doesn't_ work?
  254. *
  255. * The line number for the error will be near this comment, which
  256. * is why it is here, and not at the start of the switch.
  257. */
  258. return "Unknown error";
  259. #else
  260. if(!error)
  261. return "No error";
  262. else
  263. return "Error";
  264. #endif
  265. }
  266. const char *
  267. curl_multi_strerror(CURLMcode error)
  268. {
  269. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  270. switch(error) {
  271. case CURLM_CALL_MULTI_PERFORM:
  272. return "Please call curl_multi_perform() soon";
  273. case CURLM_OK:
  274. return "No error";
  275. case CURLM_BAD_HANDLE:
  276. return "Invalid multi handle";
  277. case CURLM_BAD_EASY_HANDLE:
  278. return "Invalid easy handle";
  279. case CURLM_OUT_OF_MEMORY:
  280. return "Out of memory";
  281. case CURLM_INTERNAL_ERROR:
  282. return "Internal error";
  283. case CURLM_BAD_SOCKET:
  284. return "Invalid socket argument";
  285. case CURLM_UNKNOWN_OPTION:
  286. return "Unknown option";
  287. case CURLM_ADDED_ALREADY:
  288. return "The easy handle is already added to a multi handle";
  289. case CURLM_RECURSIVE_API_CALL:
  290. return "API function called from within callback";
  291. case CURLM_WAKEUP_FAILURE:
  292. return "Wakeup is unavailable or failed";
  293. case CURLM_BAD_FUNCTION_ARGUMENT:
  294. return "A libcurl function was given a bad argument";
  295. case CURLM_LAST:
  296. break;
  297. }
  298. return "Unknown error";
  299. #else
  300. if(error == CURLM_OK)
  301. return "No error";
  302. else
  303. return "Error";
  304. #endif
  305. }
  306. const char *
  307. curl_share_strerror(CURLSHcode error)
  308. {
  309. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  310. switch(error) {
  311. case CURLSHE_OK:
  312. return "No error";
  313. case CURLSHE_BAD_OPTION:
  314. return "Unknown share option";
  315. case CURLSHE_IN_USE:
  316. return "Share currently in use";
  317. case CURLSHE_INVALID:
  318. return "Invalid share handle";
  319. case CURLSHE_NOMEM:
  320. return "Out of memory";
  321. case CURLSHE_NOT_BUILT_IN:
  322. return "Feature not enabled in this library";
  323. case CURLSHE_LAST:
  324. break;
  325. }
  326. return "CURLSHcode unknown";
  327. #else
  328. if(error == CURLSHE_OK)
  329. return "No error";
  330. else
  331. return "Error";
  332. #endif
  333. }
  334. #ifdef USE_WINSOCK
  335. /* This is a helper function for Curl_strerror that converts Winsock error
  336. * codes (WSAGetLastError) to error messages.
  337. * Returns NULL if no error message was found for error code.
  338. */
  339. static const char *
  340. get_winsock_error (int err, char *buf, size_t len)
  341. {
  342. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  343. const char *p;
  344. #endif
  345. if(!len)
  346. return NULL;
  347. *buf = '\0';
  348. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  349. (void)err;
  350. return NULL;
  351. #else
  352. switch(err) {
  353. case WSAEINTR:
  354. p = "Call interrupted";
  355. break;
  356. case WSAEBADF:
  357. p = "Bad file";
  358. break;
  359. case WSAEACCES:
  360. p = "Bad access";
  361. break;
  362. case WSAEFAULT:
  363. p = "Bad argument";
  364. break;
  365. case WSAEINVAL:
  366. p = "Invalid arguments";
  367. break;
  368. case WSAEMFILE:
  369. p = "Out of file descriptors";
  370. break;
  371. case WSAEWOULDBLOCK:
  372. p = "Call would block";
  373. break;
  374. case WSAEINPROGRESS:
  375. case WSAEALREADY:
  376. p = "Blocking call in progress";
  377. break;
  378. case WSAENOTSOCK:
  379. p = "Descriptor is not a socket";
  380. break;
  381. case WSAEDESTADDRREQ:
  382. p = "Need destination address";
  383. break;
  384. case WSAEMSGSIZE:
  385. p = "Bad message size";
  386. break;
  387. case WSAEPROTOTYPE:
  388. p = "Bad protocol";
  389. break;
  390. case WSAENOPROTOOPT:
  391. p = "Protocol option is unsupported";
  392. break;
  393. case WSAEPROTONOSUPPORT:
  394. p = "Protocol is unsupported";
  395. break;
  396. case WSAESOCKTNOSUPPORT:
  397. p = "Socket is unsupported";
  398. break;
  399. case WSAEOPNOTSUPP:
  400. p = "Operation not supported";
  401. break;
  402. case WSAEAFNOSUPPORT:
  403. p = "Address family not supported";
  404. break;
  405. case WSAEPFNOSUPPORT:
  406. p = "Protocol family not supported";
  407. break;
  408. case WSAEADDRINUSE:
  409. p = "Address already in use";
  410. break;
  411. case WSAEADDRNOTAVAIL:
  412. p = "Address not available";
  413. break;
  414. case WSAENETDOWN:
  415. p = "Network down";
  416. break;
  417. case WSAENETUNREACH:
  418. p = "Network unreachable";
  419. break;
  420. case WSAENETRESET:
  421. p = "Network has been reset";
  422. break;
  423. case WSAECONNABORTED:
  424. p = "Connection was aborted";
  425. break;
  426. case WSAECONNRESET:
  427. p = "Connection was reset";
  428. break;
  429. case WSAENOBUFS:
  430. p = "No buffer space";
  431. break;
  432. case WSAEISCONN:
  433. p = "Socket is already connected";
  434. break;
  435. case WSAENOTCONN:
  436. p = "Socket is not connected";
  437. break;
  438. case WSAESHUTDOWN:
  439. p = "Socket has been shut down";
  440. break;
  441. case WSAETOOMANYREFS:
  442. p = "Too many references";
  443. break;
  444. case WSAETIMEDOUT:
  445. p = "Timed out";
  446. break;
  447. case WSAECONNREFUSED:
  448. p = "Connection refused";
  449. break;
  450. case WSAELOOP:
  451. p = "Loop??";
  452. break;
  453. case WSAENAMETOOLONG:
  454. p = "Name too long";
  455. break;
  456. case WSAEHOSTDOWN:
  457. p = "Host down";
  458. break;
  459. case WSAEHOSTUNREACH:
  460. p = "Host unreachable";
  461. break;
  462. case WSAENOTEMPTY:
  463. p = "Not empty";
  464. break;
  465. case WSAEPROCLIM:
  466. p = "Process limit reached";
  467. break;
  468. case WSAEUSERS:
  469. p = "Too many users";
  470. break;
  471. case WSAEDQUOT:
  472. p = "Bad quota";
  473. break;
  474. case WSAESTALE:
  475. p = "Something is stale";
  476. break;
  477. case WSAEREMOTE:
  478. p = "Remote error";
  479. break;
  480. #ifdef WSAEDISCON /* missing in SalfordC! */
  481. case WSAEDISCON:
  482. p = "Disconnected";
  483. break;
  484. #endif
  485. /* Extended Winsock errors */
  486. case WSASYSNOTREADY:
  487. p = "Winsock library is not ready";
  488. break;
  489. case WSANOTINITIALISED:
  490. p = "Winsock library not initialised";
  491. break;
  492. case WSAVERNOTSUPPORTED:
  493. p = "Winsock version not supported";
  494. break;
  495. /* getXbyY() errors (already handled in herrmsg):
  496. * Authoritative Answer: Host not found */
  497. case WSAHOST_NOT_FOUND:
  498. p = "Host not found";
  499. break;
  500. /* Non-Authoritative: Host not found, or SERVERFAIL */
  501. case WSATRY_AGAIN:
  502. p = "Host not found, try again";
  503. break;
  504. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  505. case WSANO_RECOVERY:
  506. p = "Unrecoverable error in call to nameserver";
  507. break;
  508. /* Valid name, no data record of requested type */
  509. case WSANO_DATA:
  510. p = "No data record of requested type";
  511. break;
  512. default:
  513. return NULL;
  514. }
  515. strncpy(buf, p, len);
  516. buf [len-1] = '\0';
  517. return buf;
  518. #endif
  519. }
  520. #endif /* USE_WINSOCK */
  521. #if defined(WIN32) || defined(_WIN32_WCE)
  522. /* This is a helper function for Curl_strerror that converts Windows API error
  523. * codes (GetLastError) to error messages.
  524. * Returns NULL if no error message was found for error code.
  525. */
  526. static const char *
  527. get_winapi_error(int err, char *buf, size_t buflen)
  528. {
  529. char *p;
  530. wchar_t wbuf[256];
  531. if(!buflen)
  532. return NULL;
  533. *buf = '\0';
  534. *wbuf = L'\0';
  535. /* We return the local codepage version of the error string because if it is
  536. output to the user's terminal it will likely be with functions which
  537. expect the local codepage (eg fprintf, failf, infof).
  538. FormatMessageW -> wcstombs is used for Windows CE compatibility. */
  539. if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
  540. FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
  541. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  542. size_t written = wcstombs(buf, wbuf, buflen - 1);
  543. if(written != (size_t)-1)
  544. buf[written] = '\0';
  545. else
  546. *buf = '\0';
  547. }
  548. /* Truncate multiple lines */
  549. p = strchr(buf, '\n');
  550. if(p) {
  551. if(p > buf && *(p-1) == '\r')
  552. *(p-1) = '\0';
  553. else
  554. *p = '\0';
  555. }
  556. return (*buf ? buf : NULL);
  557. }
  558. #endif /* WIN32 || _WIN32_WCE */
  559. /*
  560. * Our thread-safe and smart strerror() replacement.
  561. *
  562. * The 'err' argument passed in to this function MUST be a true errno number
  563. * as reported on this system. We do no range checking on the number before
  564. * we pass it to the "number-to-message" conversion function and there might
  565. * be systems that don't do proper range checking in there themselves.
  566. *
  567. * We don't do range checking (on systems other than Windows) since there is
  568. * no good reliable and portable way to do it.
  569. *
  570. * On Windows different types of error codes overlap. This function has an
  571. * order of preference when trying to match error codes:
  572. * CRT (errno), Winsock (WSAGetLastError), Windows API (GetLastError).
  573. *
  574. * It may be more correct to call one of the variant functions instead:
  575. * Call Curl_sspi_strerror if the error code is definitely Windows SSPI.
  576. * Call Curl_winapi_strerror if the error code is definitely Windows API.
  577. */
  578. const char *Curl_strerror(int err, char *buf, size_t buflen)
  579. {
  580. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  581. DWORD old_win_err = GetLastError();
  582. #endif
  583. int old_errno = errno;
  584. char *p;
  585. size_t max;
  586. if(!buflen)
  587. return NULL;
  588. DEBUGASSERT(err >= 0);
  589. max = buflen - 1;
  590. *buf = '\0';
  591. #if defined(WIN32) || defined(_WIN32_WCE)
  592. #if defined(WIN32)
  593. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  594. if(err >= 0 && err < sys_nerr)
  595. strncpy(buf, strerror(err), max);
  596. else
  597. #endif
  598. {
  599. if(
  600. #ifdef USE_WINSOCK
  601. !get_winsock_error(err, buf, max) &&
  602. #endif
  603. !get_winapi_error((DWORD)err, buf, max))
  604. msnprintf(buf, max, "Unknown error %d (%#x)", err, err);
  605. }
  606. #else /* not Windows coming up */
  607. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  608. /*
  609. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  610. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  611. * message string, or EINVAL if 'errnum' is not a valid error number.
  612. */
  613. if(0 != strerror_r(err, buf, max)) {
  614. if('\0' == buf[0])
  615. msnprintf(buf, max, "Unknown error %d", err);
  616. }
  617. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  618. /*
  619. * The glibc-style strerror_r() only *might* use the buffer we pass to
  620. * the function, but it always returns the error message as a pointer,
  621. * so we must copy that string unconditionally (if non-NULL).
  622. */
  623. {
  624. char buffer[256];
  625. char *msg = strerror_r(err, buffer, sizeof(buffer));
  626. if(msg)
  627. strncpy(buf, msg, max);
  628. else
  629. msnprintf(buf, max, "Unknown error %d", err);
  630. }
  631. #elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
  632. /*
  633. * The vxworks-style strerror_r() does use the buffer we pass to the function.
  634. * The buffer size should be at least NAME_MAX (256)
  635. */
  636. {
  637. char buffer[256];
  638. if(OK == strerror_r(err, buffer))
  639. strncpy(buf, buffer, max);
  640. else
  641. msnprintf(buf, max, "Unknown error %d", err);
  642. }
  643. #else
  644. {
  645. const char *msg = strerror(err);
  646. if(msg)
  647. strncpy(buf, msg, max);
  648. else
  649. msnprintf(buf, max, "Unknown error %d", err);
  650. }
  651. #endif
  652. #endif /* end of not Windows */
  653. buf[max] = '\0'; /* make sure the string is null-terminated */
  654. /* strip trailing '\r\n' or '\n'. */
  655. p = strrchr(buf, '\n');
  656. if(p && (p - buf) >= 2)
  657. *p = '\0';
  658. p = strrchr(buf, '\r');
  659. if(p && (p - buf) >= 1)
  660. *p = '\0';
  661. if(errno != old_errno)
  662. errno = old_errno;
  663. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  664. if(old_win_err != GetLastError())
  665. SetLastError(old_win_err);
  666. #endif
  667. return buf;
  668. }
  669. /*
  670. * Curl_winapi_strerror:
  671. * Variant of Curl_strerror if the error code is definitely Windows API.
  672. */
  673. #if defined(WIN32) || defined(_WIN32_WCE)
  674. const char *Curl_winapi_strerror(DWORD err, char *buf, size_t buflen)
  675. {
  676. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  677. DWORD old_win_err = GetLastError();
  678. #endif
  679. int old_errno = errno;
  680. if(!buflen)
  681. return NULL;
  682. *buf = '\0';
  683. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  684. if(!get_winapi_error(err, buf, buflen)) {
  685. msnprintf(buf, buflen, "Unknown error %u (0x%08X)", err, err);
  686. }
  687. #else
  688. {
  689. const char *txt = (err == ERROR_SUCCESS) ? "No error" : "Error";
  690. strncpy(buf, txt, buflen);
  691. buf[buflen - 1] = '\0';
  692. }
  693. #endif
  694. if(errno != old_errno)
  695. errno = old_errno;
  696. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  697. if(old_win_err != GetLastError())
  698. SetLastError(old_win_err);
  699. #endif
  700. return buf;
  701. }
  702. #endif /* WIN32 || _WIN32_WCE */
  703. #ifdef USE_WINDOWS_SSPI
  704. /*
  705. * Curl_sspi_strerror:
  706. * Variant of Curl_strerror if the error code is definitely Windows SSPI.
  707. */
  708. const char *Curl_sspi_strerror(int err, char *buf, size_t buflen)
  709. {
  710. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  711. DWORD old_win_err = GetLastError();
  712. #endif
  713. int old_errno = errno;
  714. const char *txt;
  715. if(!buflen)
  716. return NULL;
  717. *buf = '\0';
  718. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  719. switch(err) {
  720. case SEC_E_OK:
  721. txt = "No error";
  722. break;
  723. #define SEC2TXT(sec) case sec: txt = #sec; break
  724. SEC2TXT(CRYPT_E_REVOKED);
  725. SEC2TXT(SEC_E_ALGORITHM_MISMATCH);
  726. SEC2TXT(SEC_E_BAD_BINDINGS);
  727. SEC2TXT(SEC_E_BAD_PKGID);
  728. SEC2TXT(SEC_E_BUFFER_TOO_SMALL);
  729. SEC2TXT(SEC_E_CANNOT_INSTALL);
  730. SEC2TXT(SEC_E_CANNOT_PACK);
  731. SEC2TXT(SEC_E_CERT_EXPIRED);
  732. SEC2TXT(SEC_E_CERT_UNKNOWN);
  733. SEC2TXT(SEC_E_CERT_WRONG_USAGE);
  734. SEC2TXT(SEC_E_CONTEXT_EXPIRED);
  735. SEC2TXT(SEC_E_CROSSREALM_DELEGATION_FAILURE);
  736. SEC2TXT(SEC_E_CRYPTO_SYSTEM_INVALID);
  737. SEC2TXT(SEC_E_DECRYPT_FAILURE);
  738. SEC2TXT(SEC_E_DELEGATION_POLICY);
  739. SEC2TXT(SEC_E_DELEGATION_REQUIRED);
  740. SEC2TXT(SEC_E_DOWNGRADE_DETECTED);
  741. SEC2TXT(SEC_E_ENCRYPT_FAILURE);
  742. SEC2TXT(SEC_E_ILLEGAL_MESSAGE);
  743. SEC2TXT(SEC_E_INCOMPLETE_CREDENTIALS);
  744. SEC2TXT(SEC_E_INCOMPLETE_MESSAGE);
  745. SEC2TXT(SEC_E_INSUFFICIENT_MEMORY);
  746. SEC2TXT(SEC_E_INTERNAL_ERROR);
  747. SEC2TXT(SEC_E_INVALID_HANDLE);
  748. SEC2TXT(SEC_E_INVALID_PARAMETER);
  749. SEC2TXT(SEC_E_INVALID_TOKEN);
  750. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED);
  751. SEC2TXT(SEC_E_ISSUING_CA_UNTRUSTED_KDC);
  752. SEC2TXT(SEC_E_KDC_CERT_EXPIRED);
  753. SEC2TXT(SEC_E_KDC_CERT_REVOKED);
  754. SEC2TXT(SEC_E_KDC_INVALID_REQUEST);
  755. SEC2TXT(SEC_E_KDC_UNABLE_TO_REFER);
  756. SEC2TXT(SEC_E_KDC_UNKNOWN_ETYPE);
  757. SEC2TXT(SEC_E_LOGON_DENIED);
  758. SEC2TXT(SEC_E_MAX_REFERRALS_EXCEEDED);
  759. SEC2TXT(SEC_E_MESSAGE_ALTERED);
  760. SEC2TXT(SEC_E_MULTIPLE_ACCOUNTS);
  761. SEC2TXT(SEC_E_MUST_BE_KDC);
  762. SEC2TXT(SEC_E_NOT_OWNER);
  763. SEC2TXT(SEC_E_NO_AUTHENTICATING_AUTHORITY);
  764. SEC2TXT(SEC_E_NO_CREDENTIALS);
  765. SEC2TXT(SEC_E_NO_IMPERSONATION);
  766. SEC2TXT(SEC_E_NO_IP_ADDRESSES);
  767. SEC2TXT(SEC_E_NO_KERB_KEY);
  768. SEC2TXT(SEC_E_NO_PA_DATA);
  769. SEC2TXT(SEC_E_NO_S4U_PROT_SUPPORT);
  770. SEC2TXT(SEC_E_NO_TGT_REPLY);
  771. SEC2TXT(SEC_E_OUT_OF_SEQUENCE);
  772. SEC2TXT(SEC_E_PKINIT_CLIENT_FAILURE);
  773. SEC2TXT(SEC_E_PKINIT_NAME_MISMATCH);
  774. SEC2TXT(SEC_E_POLICY_NLTM_ONLY);
  775. SEC2TXT(SEC_E_QOP_NOT_SUPPORTED);
  776. SEC2TXT(SEC_E_REVOCATION_OFFLINE_C);
  777. SEC2TXT(SEC_E_REVOCATION_OFFLINE_KDC);
  778. SEC2TXT(SEC_E_SECPKG_NOT_FOUND);
  779. SEC2TXT(SEC_E_SECURITY_QOS_FAILED);
  780. SEC2TXT(SEC_E_SHUTDOWN_IN_PROGRESS);
  781. SEC2TXT(SEC_E_SMARTCARD_CERT_EXPIRED);
  782. SEC2TXT(SEC_E_SMARTCARD_CERT_REVOKED);
  783. SEC2TXT(SEC_E_SMARTCARD_LOGON_REQUIRED);
  784. SEC2TXT(SEC_E_STRONG_CRYPTO_NOT_SUPPORTED);
  785. SEC2TXT(SEC_E_TARGET_UNKNOWN);
  786. SEC2TXT(SEC_E_TIME_SKEW);
  787. SEC2TXT(SEC_E_TOO_MANY_PRINCIPALS);
  788. SEC2TXT(SEC_E_UNFINISHED_CONTEXT_DELETED);
  789. SEC2TXT(SEC_E_UNKNOWN_CREDENTIALS);
  790. SEC2TXT(SEC_E_UNSUPPORTED_FUNCTION);
  791. SEC2TXT(SEC_E_UNSUPPORTED_PREAUTH);
  792. SEC2TXT(SEC_E_UNTRUSTED_ROOT);
  793. SEC2TXT(SEC_E_WRONG_CREDENTIAL_HANDLE);
  794. SEC2TXT(SEC_E_WRONG_PRINCIPAL);
  795. SEC2TXT(SEC_I_COMPLETE_AND_CONTINUE);
  796. SEC2TXT(SEC_I_COMPLETE_NEEDED);
  797. SEC2TXT(SEC_I_CONTEXT_EXPIRED);
  798. SEC2TXT(SEC_I_CONTINUE_NEEDED);
  799. SEC2TXT(SEC_I_INCOMPLETE_CREDENTIALS);
  800. SEC2TXT(SEC_I_LOCAL_LOGON);
  801. SEC2TXT(SEC_I_NO_LSA_CONTEXT);
  802. SEC2TXT(SEC_I_RENEGOTIATE);
  803. SEC2TXT(SEC_I_SIGNATURE_NEEDED);
  804. default:
  805. txt = "Unknown error";
  806. }
  807. if(err == SEC_E_ILLEGAL_MESSAGE) {
  808. msnprintf(buf, buflen,
  809. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  810. "when a fatal SSL/TLS alert is received (e.g. handshake failed)."
  811. " More detail may be available in the Windows System event log.",
  812. err);
  813. }
  814. else {
  815. char txtbuf[80];
  816. char msgbuf[256];
  817. msnprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
  818. if(get_winapi_error(err, msgbuf, sizeof(msgbuf)))
  819. msnprintf(buf, buflen, "%s - %s", txtbuf, msgbuf);
  820. else {
  821. strncpy(buf, txtbuf, buflen);
  822. buf[buflen - 1] = '\0';
  823. }
  824. }
  825. #else
  826. if(err == SEC_E_OK)
  827. txt = "No error";
  828. else
  829. txt = "Error";
  830. strncpy(buf, txt, buflen);
  831. buf[buflen - 1] = '\0';
  832. #endif
  833. if(errno != old_errno)
  834. errno = old_errno;
  835. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  836. if(old_win_err != GetLastError())
  837. SetLastError(old_win_err);
  838. #endif
  839. return buf;
  840. }
  841. #endif /* USE_WINDOWS_SSPI */