1
0

strerror.c 27 KB

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