strerror.c 26 KB

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