strerror.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004 - 2019, 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. /* error codes not used by current libcurl */
  225. case CURLE_OBSOLETE20:
  226. case CURLE_OBSOLETE24:
  227. case CURLE_OBSOLETE29:
  228. case CURLE_OBSOLETE32:
  229. case CURLE_OBSOLETE40:
  230. case CURLE_OBSOLETE44:
  231. case CURLE_OBSOLETE46:
  232. case CURLE_OBSOLETE50:
  233. case CURLE_OBSOLETE51:
  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(int err, char *buf, size_t buflen)
  528. {
  529. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  530. DWORD old_win_err = GetLastError();
  531. #endif
  532. int old_errno = errno;
  533. char *p;
  534. size_t max;
  535. DEBUGASSERT(err >= 0);
  536. max = buflen - 1;
  537. *buf = '\0';
  538. #ifdef USE_WINSOCK
  539. #ifdef _WIN32_WCE
  540. {
  541. wchar_t wbuf[256];
  542. wbuf[0] = L'\0';
  543. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  544. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
  545. wcstombs(buf, wbuf, max);
  546. }
  547. #else
  548. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  549. if(err >= 0 && err < sys_nerr)
  550. strncpy(buf, strerror(err), max);
  551. else {
  552. if(!get_winsock_error(err, buf, max) &&
  553. !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  554. LANG_NEUTRAL, buf, (DWORD)max, NULL))
  555. msnprintf(buf, max, "Unknown error %d (%#x)", err, err);
  556. }
  557. #endif
  558. #else /* not USE_WINSOCK coming up */
  559. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  560. /*
  561. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  562. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  563. * message string, or EINVAL if 'errnum' is not a valid error number.
  564. */
  565. if(0 != strerror_r(err, buf, max)) {
  566. if('\0' == buf[0])
  567. msnprintf(buf, max, "Unknown error %d", err);
  568. }
  569. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  570. /*
  571. * The glibc-style strerror_r() only *might* use the buffer we pass to
  572. * the function, but it always returns the error message as a pointer,
  573. * so we must copy that string unconditionally (if non-NULL).
  574. */
  575. {
  576. char buffer[256];
  577. char *msg = strerror_r(err, buffer, sizeof(buffer));
  578. if(msg)
  579. strncpy(buf, msg, max);
  580. else
  581. msnprintf(buf, max, "Unknown error %d", err);
  582. }
  583. #elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
  584. /*
  585. * The vxworks-style strerror_r() does use the buffer we pass to the function.
  586. * The buffer size should be at least NAME_MAX (256)
  587. */
  588. {
  589. char buffer[256];
  590. if(OK == strerror_r(err, buffer))
  591. strncpy(buf, buffer, max);
  592. else
  593. msnprintf(buf, max, "Unknown error %d", err);
  594. }
  595. #else
  596. {
  597. char *msg = strerror(err);
  598. if(msg)
  599. strncpy(buf, msg, max);
  600. else
  601. msnprintf(buf, max, "Unknown error %d", err);
  602. }
  603. #endif
  604. #endif /* end of ! USE_WINSOCK */
  605. buf[max] = '\0'; /* make sure the string is zero terminated */
  606. /* strip trailing '\r\n' or '\n'. */
  607. p = strrchr(buf, '\n');
  608. if(p && (p - buf) >= 2)
  609. *p = '\0';
  610. p = strrchr(buf, '\r');
  611. if(p && (p - buf) >= 1)
  612. *p = '\0';
  613. if(errno != old_errno)
  614. errno = old_errno;
  615. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  616. if(old_win_err != GetLastError())
  617. SetLastError(old_win_err);
  618. #endif
  619. return buf;
  620. }
  621. #ifdef USE_WINDOWS_SSPI
  622. const char *Curl_sspi_strerror(int err, char *buf, size_t buflen)
  623. {
  624. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  625. DWORD old_win_err = GetLastError();
  626. #endif
  627. int old_errno = errno;
  628. const char *txt;
  629. char *outbuf;
  630. size_t outmax;
  631. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  632. char txtbuf[80];
  633. char msgbuf[256];
  634. char *p, *str, *msg = NULL;
  635. bool msg_formatted = FALSE;
  636. #endif
  637. outbuf = buf;
  638. outmax = buflen - 1;
  639. *outbuf = '\0';
  640. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  641. switch(err) {
  642. case SEC_E_OK:
  643. txt = "No error";
  644. break;
  645. case CRYPT_E_REVOKED:
  646. txt = "CRYPT_E_REVOKED";
  647. break;
  648. case SEC_E_ALGORITHM_MISMATCH:
  649. txt = "SEC_E_ALGORITHM_MISMATCH";
  650. break;
  651. case SEC_E_BAD_BINDINGS:
  652. txt = "SEC_E_BAD_BINDINGS";
  653. break;
  654. case SEC_E_BAD_PKGID:
  655. txt = "SEC_E_BAD_PKGID";
  656. break;
  657. case SEC_E_BUFFER_TOO_SMALL:
  658. txt = "SEC_E_BUFFER_TOO_SMALL";
  659. break;
  660. case SEC_E_CANNOT_INSTALL:
  661. txt = "SEC_E_CANNOT_INSTALL";
  662. break;
  663. case SEC_E_CANNOT_PACK:
  664. txt = "SEC_E_CANNOT_PACK";
  665. break;
  666. case SEC_E_CERT_EXPIRED:
  667. txt = "SEC_E_CERT_EXPIRED";
  668. break;
  669. case SEC_E_CERT_UNKNOWN:
  670. txt = "SEC_E_CERT_UNKNOWN";
  671. break;
  672. case SEC_E_CERT_WRONG_USAGE:
  673. txt = "SEC_E_CERT_WRONG_USAGE";
  674. break;
  675. case SEC_E_CONTEXT_EXPIRED:
  676. txt = "SEC_E_CONTEXT_EXPIRED";
  677. break;
  678. case SEC_E_CROSSREALM_DELEGATION_FAILURE:
  679. txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
  680. break;
  681. case SEC_E_CRYPTO_SYSTEM_INVALID:
  682. txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
  683. break;
  684. case SEC_E_DECRYPT_FAILURE:
  685. txt = "SEC_E_DECRYPT_FAILURE";
  686. break;
  687. case SEC_E_DELEGATION_POLICY:
  688. txt = "SEC_E_DELEGATION_POLICY";
  689. break;
  690. case SEC_E_DELEGATION_REQUIRED:
  691. txt = "SEC_E_DELEGATION_REQUIRED";
  692. break;
  693. case SEC_E_DOWNGRADE_DETECTED:
  694. txt = "SEC_E_DOWNGRADE_DETECTED";
  695. break;
  696. case SEC_E_ENCRYPT_FAILURE:
  697. txt = "SEC_E_ENCRYPT_FAILURE";
  698. break;
  699. case SEC_E_ILLEGAL_MESSAGE:
  700. txt = "SEC_E_ILLEGAL_MESSAGE";
  701. break;
  702. case SEC_E_INCOMPLETE_CREDENTIALS:
  703. txt = "SEC_E_INCOMPLETE_CREDENTIALS";
  704. break;
  705. case SEC_E_INCOMPLETE_MESSAGE:
  706. txt = "SEC_E_INCOMPLETE_MESSAGE";
  707. break;
  708. case SEC_E_INSUFFICIENT_MEMORY:
  709. txt = "SEC_E_INSUFFICIENT_MEMORY";
  710. break;
  711. case SEC_E_INTERNAL_ERROR:
  712. txt = "SEC_E_INTERNAL_ERROR";
  713. break;
  714. case SEC_E_INVALID_HANDLE:
  715. txt = "SEC_E_INVALID_HANDLE";
  716. break;
  717. case SEC_E_INVALID_PARAMETER:
  718. txt = "SEC_E_INVALID_PARAMETER";
  719. break;
  720. case SEC_E_INVALID_TOKEN:
  721. txt = "SEC_E_INVALID_TOKEN";
  722. break;
  723. case SEC_E_ISSUING_CA_UNTRUSTED:
  724. txt = "SEC_E_ISSUING_CA_UNTRUSTED";
  725. break;
  726. case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
  727. txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
  728. break;
  729. case SEC_E_KDC_CERT_EXPIRED:
  730. txt = "SEC_E_KDC_CERT_EXPIRED";
  731. break;
  732. case SEC_E_KDC_CERT_REVOKED:
  733. txt = "SEC_E_KDC_CERT_REVOKED";
  734. break;
  735. case SEC_E_KDC_INVALID_REQUEST:
  736. txt = "SEC_E_KDC_INVALID_REQUEST";
  737. break;
  738. case SEC_E_KDC_UNABLE_TO_REFER:
  739. txt = "SEC_E_KDC_UNABLE_TO_REFER";
  740. break;
  741. case SEC_E_KDC_UNKNOWN_ETYPE:
  742. txt = "SEC_E_KDC_UNKNOWN_ETYPE";
  743. break;
  744. case SEC_E_LOGON_DENIED:
  745. txt = "SEC_E_LOGON_DENIED";
  746. break;
  747. case SEC_E_MAX_REFERRALS_EXCEEDED:
  748. txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
  749. break;
  750. case SEC_E_MESSAGE_ALTERED:
  751. txt = "SEC_E_MESSAGE_ALTERED";
  752. break;
  753. case SEC_E_MULTIPLE_ACCOUNTS:
  754. txt = "SEC_E_MULTIPLE_ACCOUNTS";
  755. break;
  756. case SEC_E_MUST_BE_KDC:
  757. txt = "SEC_E_MUST_BE_KDC";
  758. break;
  759. case SEC_E_NOT_OWNER:
  760. txt = "SEC_E_NOT_OWNER";
  761. break;
  762. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  763. txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
  764. break;
  765. case SEC_E_NO_CREDENTIALS:
  766. txt = "SEC_E_NO_CREDENTIALS";
  767. break;
  768. case SEC_E_NO_IMPERSONATION:
  769. txt = "SEC_E_NO_IMPERSONATION";
  770. break;
  771. case SEC_E_NO_IP_ADDRESSES:
  772. txt = "SEC_E_NO_IP_ADDRESSES";
  773. break;
  774. case SEC_E_NO_KERB_KEY:
  775. txt = "SEC_E_NO_KERB_KEY";
  776. break;
  777. case SEC_E_NO_PA_DATA:
  778. txt = "SEC_E_NO_PA_DATA";
  779. break;
  780. case SEC_E_NO_S4U_PROT_SUPPORT:
  781. txt = "SEC_E_NO_S4U_PROT_SUPPORT";
  782. break;
  783. case SEC_E_NO_TGT_REPLY:
  784. txt = "SEC_E_NO_TGT_REPLY";
  785. break;
  786. case SEC_E_OUT_OF_SEQUENCE:
  787. txt = "SEC_E_OUT_OF_SEQUENCE";
  788. break;
  789. case SEC_E_PKINIT_CLIENT_FAILURE:
  790. txt = "SEC_E_PKINIT_CLIENT_FAILURE";
  791. break;
  792. case SEC_E_PKINIT_NAME_MISMATCH:
  793. txt = "SEC_E_PKINIT_NAME_MISMATCH";
  794. break;
  795. case SEC_E_POLICY_NLTM_ONLY:
  796. txt = "SEC_E_POLICY_NLTM_ONLY";
  797. break;
  798. case SEC_E_QOP_NOT_SUPPORTED:
  799. txt = "SEC_E_QOP_NOT_SUPPORTED";
  800. break;
  801. case SEC_E_REVOCATION_OFFLINE_C:
  802. txt = "SEC_E_REVOCATION_OFFLINE_C";
  803. break;
  804. case SEC_E_REVOCATION_OFFLINE_KDC:
  805. txt = "SEC_E_REVOCATION_OFFLINE_KDC";
  806. break;
  807. case SEC_E_SECPKG_NOT_FOUND:
  808. txt = "SEC_E_SECPKG_NOT_FOUND";
  809. break;
  810. case SEC_E_SECURITY_QOS_FAILED:
  811. txt = "SEC_E_SECURITY_QOS_FAILED";
  812. break;
  813. case SEC_E_SHUTDOWN_IN_PROGRESS:
  814. txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
  815. break;
  816. case SEC_E_SMARTCARD_CERT_EXPIRED:
  817. txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
  818. break;
  819. case SEC_E_SMARTCARD_CERT_REVOKED:
  820. txt = "SEC_E_SMARTCARD_CERT_REVOKED";
  821. break;
  822. case SEC_E_SMARTCARD_LOGON_REQUIRED:
  823. txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
  824. break;
  825. case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
  826. txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
  827. break;
  828. case SEC_E_TARGET_UNKNOWN:
  829. txt = "SEC_E_TARGET_UNKNOWN";
  830. break;
  831. case SEC_E_TIME_SKEW:
  832. txt = "SEC_E_TIME_SKEW";
  833. break;
  834. case SEC_E_TOO_MANY_PRINCIPALS:
  835. txt = "SEC_E_TOO_MANY_PRINCIPALS";
  836. break;
  837. case SEC_E_UNFINISHED_CONTEXT_DELETED:
  838. txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
  839. break;
  840. case SEC_E_UNKNOWN_CREDENTIALS:
  841. txt = "SEC_E_UNKNOWN_CREDENTIALS";
  842. break;
  843. case SEC_E_UNSUPPORTED_FUNCTION:
  844. txt = "SEC_E_UNSUPPORTED_FUNCTION";
  845. break;
  846. case SEC_E_UNSUPPORTED_PREAUTH:
  847. txt = "SEC_E_UNSUPPORTED_PREAUTH";
  848. break;
  849. case SEC_E_UNTRUSTED_ROOT:
  850. txt = "SEC_E_UNTRUSTED_ROOT";
  851. break;
  852. case SEC_E_WRONG_CREDENTIAL_HANDLE:
  853. txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
  854. break;
  855. case SEC_E_WRONG_PRINCIPAL:
  856. txt = "SEC_E_WRONG_PRINCIPAL";
  857. break;
  858. case SEC_I_COMPLETE_AND_CONTINUE:
  859. txt = "SEC_I_COMPLETE_AND_CONTINUE";
  860. break;
  861. case SEC_I_COMPLETE_NEEDED:
  862. txt = "SEC_I_COMPLETE_NEEDED";
  863. break;
  864. case SEC_I_CONTEXT_EXPIRED:
  865. txt = "SEC_I_CONTEXT_EXPIRED";
  866. break;
  867. case SEC_I_CONTINUE_NEEDED:
  868. txt = "SEC_I_CONTINUE_NEEDED";
  869. break;
  870. case SEC_I_INCOMPLETE_CREDENTIALS:
  871. txt = "SEC_I_INCOMPLETE_CREDENTIALS";
  872. break;
  873. case SEC_I_LOCAL_LOGON:
  874. txt = "SEC_I_LOCAL_LOGON";
  875. break;
  876. case SEC_I_NO_LSA_CONTEXT:
  877. txt = "SEC_I_NO_LSA_CONTEXT";
  878. break;
  879. case SEC_I_RENEGOTIATE:
  880. txt = "SEC_I_RENEGOTIATE";
  881. break;
  882. case SEC_I_SIGNATURE_NEEDED:
  883. txt = "SEC_I_SIGNATURE_NEEDED";
  884. break;
  885. default:
  886. txt = "Unknown error";
  887. }
  888. if(err == SEC_E_OK)
  889. strncpy(outbuf, txt, outmax);
  890. else if(err == SEC_E_ILLEGAL_MESSAGE)
  891. msnprintf(outbuf, outmax,
  892. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  893. "when a fatal SSL/TLS alert is received (e.g. handshake failed)."
  894. " More detail may be available in the Windows System event log.",
  895. err);
  896. else {
  897. str = txtbuf;
  898. msnprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
  899. txtbuf[sizeof(txtbuf)-1] = '\0';
  900. #ifdef _WIN32_WCE
  901. {
  902. wchar_t wbuf[256];
  903. wbuf[0] = L'\0';
  904. if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  905. FORMAT_MESSAGE_IGNORE_INSERTS,
  906. NULL, err, LANG_NEUTRAL,
  907. wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  908. wcstombs(msgbuf, wbuf, sizeof(msgbuf)-1);
  909. msg_formatted = TRUE;
  910. }
  911. }
  912. #else
  913. if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  914. FORMAT_MESSAGE_IGNORE_INSERTS,
  915. NULL, err, LANG_NEUTRAL,
  916. msgbuf, sizeof(msgbuf)-1, NULL)) {
  917. msg_formatted = TRUE;
  918. }
  919. #endif
  920. if(msg_formatted) {
  921. msgbuf[sizeof(msgbuf)-1] = '\0';
  922. /* strip trailing '\r\n' or '\n' */
  923. p = strrchr(msgbuf, '\n');
  924. if(p && (p - msgbuf) >= 2)
  925. *p = '\0';
  926. p = strrchr(msgbuf, '\r');
  927. if(p && (p - msgbuf) >= 1)
  928. *p = '\0';
  929. msg = msgbuf;
  930. }
  931. if(msg)
  932. msnprintf(outbuf, outmax, "%s - %s", str, msg);
  933. else
  934. strncpy(outbuf, str, outmax);
  935. }
  936. #else
  937. if(err == SEC_E_OK)
  938. txt = "No error";
  939. else
  940. txt = "Error";
  941. strncpy(outbuf, txt, outmax);
  942. #endif
  943. outbuf[outmax] = '\0';
  944. if(errno != old_errno)
  945. errno = old_errno;
  946. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  947. if(old_win_err != GetLastError())
  948. SetLastError(old_win_err);
  949. #endif
  950. return outbuf;
  951. }
  952. #endif /* USE_WINDOWS_SSPI */