strerror.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004 - 2017, 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. #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. /* error codes not used by current libcurl */
  223. case CURLE_OBSOLETE20:
  224. case CURLE_OBSOLETE24:
  225. case CURLE_OBSOLETE29:
  226. case CURLE_OBSOLETE32:
  227. case CURLE_OBSOLETE40:
  228. case CURLE_OBSOLETE44:
  229. case CURLE_OBSOLETE46:
  230. case CURLE_OBSOLETE50:
  231. case CURLE_OBSOLETE51:
  232. case CURLE_OBSOLETE57:
  233. case CURL_LAST:
  234. break;
  235. }
  236. /*
  237. * By using a switch, gcc -Wall will complain about enum values
  238. * which do not appear, helping keep this function up-to-date.
  239. * By using gcc -Wall -Werror, you can't forget.
  240. *
  241. * A table would not have the same benefit. Most compilers will
  242. * generate code very similar to a table in any case, so there
  243. * is little performance gain from a table. And something is broken
  244. * for the user's application, anyways, so does it matter how fast
  245. * it _doesn't_ work?
  246. *
  247. * The line number for the error will be near this comment, which
  248. * is why it is here, and not at the start of the switch.
  249. */
  250. return "Unknown error";
  251. #else
  252. if(!error)
  253. return "No error";
  254. else
  255. return "Error";
  256. #endif
  257. }
  258. const char *
  259. curl_multi_strerror(CURLMcode error)
  260. {
  261. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  262. switch(error) {
  263. case CURLM_CALL_MULTI_PERFORM:
  264. return "Please call curl_multi_perform() soon";
  265. case CURLM_OK:
  266. return "No error";
  267. case CURLM_BAD_HANDLE:
  268. return "Invalid multi handle";
  269. case CURLM_BAD_EASY_HANDLE:
  270. return "Invalid easy handle";
  271. case CURLM_OUT_OF_MEMORY:
  272. return "Out of memory";
  273. case CURLM_INTERNAL_ERROR:
  274. return "Internal error";
  275. case CURLM_BAD_SOCKET:
  276. return "Invalid socket argument";
  277. case CURLM_UNKNOWN_OPTION:
  278. return "Unknown option";
  279. case CURLM_ADDED_ALREADY:
  280. return "The easy handle is already added to a multi handle";
  281. case CURLM_RECURSIVE_API_CALL:
  282. return "API function called from within callback";
  283. case CURLM_LAST:
  284. break;
  285. }
  286. return "Unknown error";
  287. #else
  288. if(error == CURLM_OK)
  289. return "No error";
  290. else
  291. return "Error";
  292. #endif
  293. }
  294. const char *
  295. curl_share_strerror(CURLSHcode error)
  296. {
  297. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  298. switch(error) {
  299. case CURLSHE_OK:
  300. return "No error";
  301. case CURLSHE_BAD_OPTION:
  302. return "Unknown share option";
  303. case CURLSHE_IN_USE:
  304. return "Share currently in use";
  305. case CURLSHE_INVALID:
  306. return "Invalid share handle";
  307. case CURLSHE_NOMEM:
  308. return "Out of memory";
  309. case CURLSHE_NOT_BUILT_IN:
  310. return "Feature not enabled in this library";
  311. case CURLSHE_LAST:
  312. break;
  313. }
  314. return "CURLSHcode unknown";
  315. #else
  316. if(error == CURLSHE_OK)
  317. return "No error";
  318. else
  319. return "Error";
  320. #endif
  321. }
  322. #ifdef USE_WINSOCK
  323. /* This function handles most / all (?) Winsock errors curl is able to produce.
  324. */
  325. static const char *
  326. get_winsock_error (int err, char *buf, size_t len)
  327. {
  328. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  329. DWORD old_win_err = GetLastError();
  330. #endif
  331. int old_errno = errno;
  332. const char *p;
  333. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  334. switch(err) {
  335. case WSAEINTR:
  336. p = "Call interrupted";
  337. break;
  338. case WSAEBADF:
  339. p = "Bad file";
  340. break;
  341. case WSAEACCES:
  342. p = "Bad access";
  343. break;
  344. case WSAEFAULT:
  345. p = "Bad argument";
  346. break;
  347. case WSAEINVAL:
  348. p = "Invalid arguments";
  349. break;
  350. case WSAEMFILE:
  351. p = "Out of file descriptors";
  352. break;
  353. case WSAEWOULDBLOCK:
  354. p = "Call would block";
  355. break;
  356. case WSAEINPROGRESS:
  357. case WSAEALREADY:
  358. p = "Blocking call in progress";
  359. break;
  360. case WSAENOTSOCK:
  361. p = "Descriptor is not a socket";
  362. break;
  363. case WSAEDESTADDRREQ:
  364. p = "Need destination address";
  365. break;
  366. case WSAEMSGSIZE:
  367. p = "Bad message size";
  368. break;
  369. case WSAEPROTOTYPE:
  370. p = "Bad protocol";
  371. break;
  372. case WSAENOPROTOOPT:
  373. p = "Protocol option is unsupported";
  374. break;
  375. case WSAEPROTONOSUPPORT:
  376. p = "Protocol is unsupported";
  377. break;
  378. case WSAESOCKTNOSUPPORT:
  379. p = "Socket is unsupported";
  380. break;
  381. case WSAEOPNOTSUPP:
  382. p = "Operation not supported";
  383. break;
  384. case WSAEAFNOSUPPORT:
  385. p = "Address family not supported";
  386. break;
  387. case WSAEPFNOSUPPORT:
  388. p = "Protocol family not supported";
  389. break;
  390. case WSAEADDRINUSE:
  391. p = "Address already in use";
  392. break;
  393. case WSAEADDRNOTAVAIL:
  394. p = "Address not available";
  395. break;
  396. case WSAENETDOWN:
  397. p = "Network down";
  398. break;
  399. case WSAENETUNREACH:
  400. p = "Network unreachable";
  401. break;
  402. case WSAENETRESET:
  403. p = "Network has been reset";
  404. break;
  405. case WSAECONNABORTED:
  406. p = "Connection was aborted";
  407. break;
  408. case WSAECONNRESET:
  409. p = "Connection was reset";
  410. break;
  411. case WSAENOBUFS:
  412. p = "No buffer space";
  413. break;
  414. case WSAEISCONN:
  415. p = "Socket is already connected";
  416. break;
  417. case WSAENOTCONN:
  418. p = "Socket is not connected";
  419. break;
  420. case WSAESHUTDOWN:
  421. p = "Socket has been shut down";
  422. break;
  423. case WSAETOOMANYREFS:
  424. p = "Too many references";
  425. break;
  426. case WSAETIMEDOUT:
  427. p = "Timed out";
  428. break;
  429. case WSAECONNREFUSED:
  430. p = "Connection refused";
  431. break;
  432. case WSAELOOP:
  433. p = "Loop??";
  434. break;
  435. case WSAENAMETOOLONG:
  436. p = "Name too long";
  437. break;
  438. case WSAEHOSTDOWN:
  439. p = "Host down";
  440. break;
  441. case WSAEHOSTUNREACH:
  442. p = "Host unreachable";
  443. break;
  444. case WSAENOTEMPTY:
  445. p = "Not empty";
  446. break;
  447. case WSAEPROCLIM:
  448. p = "Process limit reached";
  449. break;
  450. case WSAEUSERS:
  451. p = "Too many users";
  452. break;
  453. case WSAEDQUOT:
  454. p = "Bad quota";
  455. break;
  456. case WSAESTALE:
  457. p = "Something is stale";
  458. break;
  459. case WSAEREMOTE:
  460. p = "Remote error";
  461. break;
  462. #ifdef WSAEDISCON /* missing in SalfordC! */
  463. case WSAEDISCON:
  464. p = "Disconnected";
  465. break;
  466. #endif
  467. /* Extended Winsock errors */
  468. case WSASYSNOTREADY:
  469. p = "Winsock library is not ready";
  470. break;
  471. case WSANOTINITIALISED:
  472. p = "Winsock library not initialised";
  473. break;
  474. case WSAVERNOTSUPPORTED:
  475. p = "Winsock version not supported";
  476. break;
  477. /* getXbyY() errors (already handled in herrmsg):
  478. * Authoritative Answer: Host not found */
  479. case WSAHOST_NOT_FOUND:
  480. p = "Host not found";
  481. break;
  482. /* Non-Authoritative: Host not found, or SERVERFAIL */
  483. case WSATRY_AGAIN:
  484. p = "Host not found, try again";
  485. break;
  486. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  487. case WSANO_RECOVERY:
  488. p = "Unrecoverable error in call to nameserver";
  489. break;
  490. /* Valid name, no data record of requested type */
  491. case WSANO_DATA:
  492. p = "No data record of requested type";
  493. break;
  494. default:
  495. return NULL;
  496. }
  497. #else
  498. if(!err)
  499. return NULL;
  500. else
  501. p = "error";
  502. #endif
  503. strncpy(buf, p, len);
  504. buf [len-1] = '\0';
  505. if(errno != old_errno)
  506. errno = old_errno;
  507. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  508. if(old_win_err != GetLastError())
  509. SetLastError(old_win_err);
  510. #endif
  511. return buf;
  512. }
  513. #endif /* USE_WINSOCK */
  514. /*
  515. * Our thread-safe and smart strerror() replacement.
  516. *
  517. * The 'err' argument passed in to this function MUST be a true errno number
  518. * as reported on this system. We do no range checking on the number before
  519. * we pass it to the "number-to-message" conversion function and there might
  520. * be systems that don't do proper range checking in there themselves.
  521. *
  522. * We don't do range checking (on systems other than Windows) since there is
  523. * no good reliable and portable way to do it.
  524. */
  525. const char *Curl_strerror(struct connectdata *conn, int err)
  526. {
  527. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  528. DWORD old_win_err = GetLastError();
  529. #endif
  530. int old_errno = errno;
  531. char *buf, *p;
  532. size_t max;
  533. DEBUGASSERT(conn);
  534. DEBUGASSERT(err >= 0);
  535. buf = conn->syserr_buf;
  536. max = sizeof(conn->syserr_buf)-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. snprintf(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. snprintf(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. snprintf(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. snprintf(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. snprintf(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 (struct connectdata *conn, int err)
  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[sizeof(conn->syserr_buf)];
  634. char *p, *str, *msg = NULL;
  635. bool msg_formatted = FALSE;
  636. #endif
  637. DEBUGASSERT(conn);
  638. outbuf = conn->syserr_buf;
  639. outmax = sizeof(conn->syserr_buf)-1;
  640. *outbuf = '\0';
  641. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  642. switch(err) {
  643. case SEC_E_OK:
  644. txt = "No error";
  645. break;
  646. case CRYPT_E_REVOKED:
  647. txt = "CRYPT_E_REVOKED";
  648. break;
  649. case SEC_E_ALGORITHM_MISMATCH:
  650. txt = "SEC_E_ALGORITHM_MISMATCH";
  651. break;
  652. case SEC_E_BAD_BINDINGS:
  653. txt = "SEC_E_BAD_BINDINGS";
  654. break;
  655. case SEC_E_BAD_PKGID:
  656. txt = "SEC_E_BAD_PKGID";
  657. break;
  658. case SEC_E_BUFFER_TOO_SMALL:
  659. txt = "SEC_E_BUFFER_TOO_SMALL";
  660. break;
  661. case SEC_E_CANNOT_INSTALL:
  662. txt = "SEC_E_CANNOT_INSTALL";
  663. break;
  664. case SEC_E_CANNOT_PACK:
  665. txt = "SEC_E_CANNOT_PACK";
  666. break;
  667. case SEC_E_CERT_EXPIRED:
  668. txt = "SEC_E_CERT_EXPIRED";
  669. break;
  670. case SEC_E_CERT_UNKNOWN:
  671. txt = "SEC_E_CERT_UNKNOWN";
  672. break;
  673. case SEC_E_CERT_WRONG_USAGE:
  674. txt = "SEC_E_CERT_WRONG_USAGE";
  675. break;
  676. case SEC_E_CONTEXT_EXPIRED:
  677. txt = "SEC_E_CONTEXT_EXPIRED";
  678. break;
  679. case SEC_E_CROSSREALM_DELEGATION_FAILURE:
  680. txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
  681. break;
  682. case SEC_E_CRYPTO_SYSTEM_INVALID:
  683. txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
  684. break;
  685. case SEC_E_DECRYPT_FAILURE:
  686. txt = "SEC_E_DECRYPT_FAILURE";
  687. break;
  688. case SEC_E_DELEGATION_POLICY:
  689. txt = "SEC_E_DELEGATION_POLICY";
  690. break;
  691. case SEC_E_DELEGATION_REQUIRED:
  692. txt = "SEC_E_DELEGATION_REQUIRED";
  693. break;
  694. case SEC_E_DOWNGRADE_DETECTED:
  695. txt = "SEC_E_DOWNGRADE_DETECTED";
  696. break;
  697. case SEC_E_ENCRYPT_FAILURE:
  698. txt = "SEC_E_ENCRYPT_FAILURE";
  699. break;
  700. case SEC_E_ILLEGAL_MESSAGE:
  701. txt = "SEC_E_ILLEGAL_MESSAGE";
  702. break;
  703. case SEC_E_INCOMPLETE_CREDENTIALS:
  704. txt = "SEC_E_INCOMPLETE_CREDENTIALS";
  705. break;
  706. case SEC_E_INCOMPLETE_MESSAGE:
  707. txt = "SEC_E_INCOMPLETE_MESSAGE";
  708. break;
  709. case SEC_E_INSUFFICIENT_MEMORY:
  710. txt = "SEC_E_INSUFFICIENT_MEMORY";
  711. break;
  712. case SEC_E_INTERNAL_ERROR:
  713. txt = "SEC_E_INTERNAL_ERROR";
  714. break;
  715. case SEC_E_INVALID_HANDLE:
  716. txt = "SEC_E_INVALID_HANDLE";
  717. break;
  718. case SEC_E_INVALID_PARAMETER:
  719. txt = "SEC_E_INVALID_PARAMETER";
  720. break;
  721. case SEC_E_INVALID_TOKEN:
  722. txt = "SEC_E_INVALID_TOKEN";
  723. break;
  724. case SEC_E_ISSUING_CA_UNTRUSTED:
  725. txt = "SEC_E_ISSUING_CA_UNTRUSTED";
  726. break;
  727. case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
  728. txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
  729. break;
  730. case SEC_E_KDC_CERT_EXPIRED:
  731. txt = "SEC_E_KDC_CERT_EXPIRED";
  732. break;
  733. case SEC_E_KDC_CERT_REVOKED:
  734. txt = "SEC_E_KDC_CERT_REVOKED";
  735. break;
  736. case SEC_E_KDC_INVALID_REQUEST:
  737. txt = "SEC_E_KDC_INVALID_REQUEST";
  738. break;
  739. case SEC_E_KDC_UNABLE_TO_REFER:
  740. txt = "SEC_E_KDC_UNABLE_TO_REFER";
  741. break;
  742. case SEC_E_KDC_UNKNOWN_ETYPE:
  743. txt = "SEC_E_KDC_UNKNOWN_ETYPE";
  744. break;
  745. case SEC_E_LOGON_DENIED:
  746. txt = "SEC_E_LOGON_DENIED";
  747. break;
  748. case SEC_E_MAX_REFERRALS_EXCEEDED:
  749. txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
  750. break;
  751. case SEC_E_MESSAGE_ALTERED:
  752. txt = "SEC_E_MESSAGE_ALTERED";
  753. break;
  754. case SEC_E_MULTIPLE_ACCOUNTS:
  755. txt = "SEC_E_MULTIPLE_ACCOUNTS";
  756. break;
  757. case SEC_E_MUST_BE_KDC:
  758. txt = "SEC_E_MUST_BE_KDC";
  759. break;
  760. case SEC_E_NOT_OWNER:
  761. txt = "SEC_E_NOT_OWNER";
  762. break;
  763. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  764. txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
  765. break;
  766. case SEC_E_NO_CREDENTIALS:
  767. txt = "SEC_E_NO_CREDENTIALS";
  768. break;
  769. case SEC_E_NO_IMPERSONATION:
  770. txt = "SEC_E_NO_IMPERSONATION";
  771. break;
  772. case SEC_E_NO_IP_ADDRESSES:
  773. txt = "SEC_E_NO_IP_ADDRESSES";
  774. break;
  775. case SEC_E_NO_KERB_KEY:
  776. txt = "SEC_E_NO_KERB_KEY";
  777. break;
  778. case SEC_E_NO_PA_DATA:
  779. txt = "SEC_E_NO_PA_DATA";
  780. break;
  781. case SEC_E_NO_S4U_PROT_SUPPORT:
  782. txt = "SEC_E_NO_S4U_PROT_SUPPORT";
  783. break;
  784. case SEC_E_NO_TGT_REPLY:
  785. txt = "SEC_E_NO_TGT_REPLY";
  786. break;
  787. case SEC_E_OUT_OF_SEQUENCE:
  788. txt = "SEC_E_OUT_OF_SEQUENCE";
  789. break;
  790. case SEC_E_PKINIT_CLIENT_FAILURE:
  791. txt = "SEC_E_PKINIT_CLIENT_FAILURE";
  792. break;
  793. case SEC_E_PKINIT_NAME_MISMATCH:
  794. txt = "SEC_E_PKINIT_NAME_MISMATCH";
  795. break;
  796. case SEC_E_POLICY_NLTM_ONLY:
  797. txt = "SEC_E_POLICY_NLTM_ONLY";
  798. break;
  799. case SEC_E_QOP_NOT_SUPPORTED:
  800. txt = "SEC_E_QOP_NOT_SUPPORTED";
  801. break;
  802. case SEC_E_REVOCATION_OFFLINE_C:
  803. txt = "SEC_E_REVOCATION_OFFLINE_C";
  804. break;
  805. case SEC_E_REVOCATION_OFFLINE_KDC:
  806. txt = "SEC_E_REVOCATION_OFFLINE_KDC";
  807. break;
  808. case SEC_E_SECPKG_NOT_FOUND:
  809. txt = "SEC_E_SECPKG_NOT_FOUND";
  810. break;
  811. case SEC_E_SECURITY_QOS_FAILED:
  812. txt = "SEC_E_SECURITY_QOS_FAILED";
  813. break;
  814. case SEC_E_SHUTDOWN_IN_PROGRESS:
  815. txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
  816. break;
  817. case SEC_E_SMARTCARD_CERT_EXPIRED:
  818. txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
  819. break;
  820. case SEC_E_SMARTCARD_CERT_REVOKED:
  821. txt = "SEC_E_SMARTCARD_CERT_REVOKED";
  822. break;
  823. case SEC_E_SMARTCARD_LOGON_REQUIRED:
  824. txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
  825. break;
  826. case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
  827. txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
  828. break;
  829. case SEC_E_TARGET_UNKNOWN:
  830. txt = "SEC_E_TARGET_UNKNOWN";
  831. break;
  832. case SEC_E_TIME_SKEW:
  833. txt = "SEC_E_TIME_SKEW";
  834. break;
  835. case SEC_E_TOO_MANY_PRINCIPALS:
  836. txt = "SEC_E_TOO_MANY_PRINCIPALS";
  837. break;
  838. case SEC_E_UNFINISHED_CONTEXT_DELETED:
  839. txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
  840. break;
  841. case SEC_E_UNKNOWN_CREDENTIALS:
  842. txt = "SEC_E_UNKNOWN_CREDENTIALS";
  843. break;
  844. case SEC_E_UNSUPPORTED_FUNCTION:
  845. txt = "SEC_E_UNSUPPORTED_FUNCTION";
  846. break;
  847. case SEC_E_UNSUPPORTED_PREAUTH:
  848. txt = "SEC_E_UNSUPPORTED_PREAUTH";
  849. break;
  850. case SEC_E_UNTRUSTED_ROOT:
  851. txt = "SEC_E_UNTRUSTED_ROOT";
  852. break;
  853. case SEC_E_WRONG_CREDENTIAL_HANDLE:
  854. txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
  855. break;
  856. case SEC_E_WRONG_PRINCIPAL:
  857. txt = "SEC_E_WRONG_PRINCIPAL";
  858. break;
  859. case SEC_I_COMPLETE_AND_CONTINUE:
  860. txt = "SEC_I_COMPLETE_AND_CONTINUE";
  861. break;
  862. case SEC_I_COMPLETE_NEEDED:
  863. txt = "SEC_I_COMPLETE_NEEDED";
  864. break;
  865. case SEC_I_CONTEXT_EXPIRED:
  866. txt = "SEC_I_CONTEXT_EXPIRED";
  867. break;
  868. case SEC_I_CONTINUE_NEEDED:
  869. txt = "SEC_I_CONTINUE_NEEDED";
  870. break;
  871. case SEC_I_INCOMPLETE_CREDENTIALS:
  872. txt = "SEC_I_INCOMPLETE_CREDENTIALS";
  873. break;
  874. case SEC_I_LOCAL_LOGON:
  875. txt = "SEC_I_LOCAL_LOGON";
  876. break;
  877. case SEC_I_NO_LSA_CONTEXT:
  878. txt = "SEC_I_NO_LSA_CONTEXT";
  879. break;
  880. case SEC_I_RENEGOTIATE:
  881. txt = "SEC_I_RENEGOTIATE";
  882. break;
  883. case SEC_I_SIGNATURE_NEEDED:
  884. txt = "SEC_I_SIGNATURE_NEEDED";
  885. break;
  886. default:
  887. txt = "Unknown error";
  888. }
  889. if(err == SEC_E_OK)
  890. strncpy(outbuf, txt, outmax);
  891. else if(err == SEC_E_ILLEGAL_MESSAGE)
  892. snprintf(outbuf, outmax,
  893. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  894. "when a fatal SSL/TLS alert is received (e.g. handshake failed). "
  895. "More detail may be available in the Windows System event log.",
  896. err);
  897. else {
  898. str = txtbuf;
  899. snprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
  900. txtbuf[sizeof(txtbuf)-1] = '\0';
  901. #ifdef _WIN32_WCE
  902. {
  903. wchar_t wbuf[256];
  904. wbuf[0] = L'\0';
  905. if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  906. FORMAT_MESSAGE_IGNORE_INSERTS,
  907. NULL, err, LANG_NEUTRAL,
  908. wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  909. wcstombs(msgbuf, wbuf, sizeof(msgbuf)-1);
  910. msg_formatted = TRUE;
  911. }
  912. }
  913. #else
  914. if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  915. FORMAT_MESSAGE_IGNORE_INSERTS,
  916. NULL, err, LANG_NEUTRAL,
  917. msgbuf, sizeof(msgbuf)-1, NULL)) {
  918. msg_formatted = TRUE;
  919. }
  920. #endif
  921. if(msg_formatted) {
  922. msgbuf[sizeof(msgbuf)-1] = '\0';
  923. /* strip trailing '\r\n' or '\n' */
  924. p = strrchr(msgbuf, '\n');
  925. if(p && (p - msgbuf) >= 2)
  926. *p = '\0';
  927. p = strrchr(msgbuf, '\r');
  928. if(p && (p - msgbuf) >= 1)
  929. *p = '\0';
  930. msg = msgbuf;
  931. }
  932. if(msg)
  933. snprintf(outbuf, outmax, "%s - %s", str, msg);
  934. else
  935. strncpy(outbuf, str, outmax);
  936. }
  937. #else
  938. if(err == SEC_E_OK)
  939. txt = "No error";
  940. else
  941. txt = "Error";
  942. strncpy(outbuf, txt, outmax);
  943. #endif
  944. outbuf[outmax] = '\0';
  945. if(errno != old_errno)
  946. errno = old_errno;
  947. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  948. if(old_win_err != GetLastError())
  949. SetLastError(old_win_err);
  950. #endif
  951. return outbuf;
  952. }
  953. #endif /* USE_WINDOWS_SSPI */