strerror.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2004 - 2017, 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_PEER_FAILED_VERIFICATION:
  143. return "SSL peer certificate or SSH remote key was not OK";
  144. case CURLE_GOT_NOTHING:
  145. return "Server returned nothing (no headers, no data)";
  146. case CURLE_SSL_ENGINE_NOTFOUND:
  147. return "SSL crypto engine not found";
  148. case CURLE_SSL_ENGINE_SETFAILED:
  149. return "Can not set SSL crypto engine as default";
  150. case CURLE_SSL_ENGINE_INITFAILED:
  151. return "Failed to initialise SSL crypto engine";
  152. case CURLE_SEND_ERROR:
  153. return "Failed sending data to the peer";
  154. case CURLE_RECV_ERROR:
  155. return "Failure when receiving data from the peer";
  156. case CURLE_SSL_CERTPROBLEM:
  157. return "Problem with the local SSL certificate";
  158. case CURLE_SSL_CIPHER:
  159. return "Couldn't use specified SSL cipher";
  160. case CURLE_SSL_CACERT:
  161. return "Peer certificate cannot be authenticated with given CA "
  162. "certificates";
  163. case CURLE_SSL_CACERT_BADFILE:
  164. return "Problem with the SSL CA cert (path? access rights?)";
  165. case CURLE_BAD_CONTENT_ENCODING:
  166. return "Unrecognized or bad HTTP Content or Transfer-Encoding";
  167. case CURLE_LDAP_INVALID_URL:
  168. return "Invalid LDAP URL";
  169. case CURLE_FILESIZE_EXCEEDED:
  170. return "Maximum file size exceeded";
  171. case CURLE_USE_SSL_FAILED:
  172. return "Requested SSL level failed";
  173. case CURLE_SSL_SHUTDOWN_FAILED:
  174. return "Failed to shut down the SSL connection";
  175. case CURLE_SSL_CRL_BADFILE:
  176. return "Failed to load CRL file (path? access rights?, format?)";
  177. case CURLE_SSL_ISSUER_ERROR:
  178. return "Issuer check against peer certificate failed";
  179. case CURLE_SEND_FAIL_REWIND:
  180. return "Send failed since rewinding of the data stream failed";
  181. case CURLE_LOGIN_DENIED:
  182. return "Login denied";
  183. case CURLE_TFTP_NOTFOUND:
  184. return "TFTP: File Not Found";
  185. case CURLE_TFTP_PERM:
  186. return "TFTP: Access Violation";
  187. case CURLE_REMOTE_DISK_FULL:
  188. return "Disk full or allocation exceeded";
  189. case CURLE_TFTP_ILLEGAL:
  190. return "TFTP: Illegal operation";
  191. case CURLE_TFTP_UNKNOWNID:
  192. return "TFTP: Unknown transfer ID";
  193. case CURLE_REMOTE_FILE_EXISTS:
  194. return "Remote file already exists";
  195. case CURLE_TFTP_NOSUCHUSER:
  196. return "TFTP: No such user";
  197. case CURLE_CONV_FAILED:
  198. return "Conversion failed";
  199. case CURLE_CONV_REQD:
  200. return "Caller must register CURLOPT_CONV_ callback options";
  201. case CURLE_REMOTE_FILE_NOT_FOUND:
  202. return "Remote file not found";
  203. case CURLE_SSH:
  204. return "Error in the SSH layer";
  205. case CURLE_AGAIN:
  206. return "Socket not ready for send/recv";
  207. case CURLE_RTSP_CSEQ_ERROR:
  208. return "RTSP CSeq mismatch or invalid CSeq";
  209. case CURLE_RTSP_SESSION_ERROR:
  210. return "RTSP session error";
  211. case CURLE_FTP_BAD_FILE_LIST:
  212. return "Unable to parse FTP file list";
  213. case CURLE_CHUNK_FAILED:
  214. return "Chunk callback failed";
  215. case CURLE_NO_CONNECTION_AVAILABLE:
  216. return "The max connection limit is reached";
  217. case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
  218. return "SSL public key does not match pinned public key";
  219. case CURLE_SSL_INVALIDCERTSTATUS:
  220. return "SSL server certificate status verification FAILED";
  221. case CURLE_HTTP2_STREAM:
  222. return "Stream error in the HTTP/2 framing layer";
  223. /* error codes not used by current libcurl */
  224. case CURLE_OBSOLETE20:
  225. case CURLE_OBSOLETE24:
  226. case CURLE_OBSOLETE29:
  227. case CURLE_OBSOLETE32:
  228. case CURLE_OBSOLETE40:
  229. case CURLE_OBSOLETE44:
  230. case CURLE_OBSOLETE46:
  231. case CURLE_OBSOLETE50:
  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_LAST:
  282. break;
  283. }
  284. return "Unknown error";
  285. #else
  286. if(error == CURLM_OK)
  287. return "No error";
  288. else
  289. return "Error";
  290. #endif
  291. }
  292. const char *
  293. curl_share_strerror(CURLSHcode error)
  294. {
  295. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  296. switch(error) {
  297. case CURLSHE_OK:
  298. return "No error";
  299. case CURLSHE_BAD_OPTION:
  300. return "Unknown share option";
  301. case CURLSHE_IN_USE:
  302. return "Share currently in use";
  303. case CURLSHE_INVALID:
  304. return "Invalid share handle";
  305. case CURLSHE_NOMEM:
  306. return "Out of memory";
  307. case CURLSHE_NOT_BUILT_IN:
  308. return "Feature not enabled in this library";
  309. case CURLSHE_LAST:
  310. break;
  311. }
  312. return "CURLSHcode unknown";
  313. #else
  314. if(error == CURLSHE_OK)
  315. return "No error";
  316. else
  317. return "Error";
  318. #endif
  319. }
  320. #ifdef USE_WINSOCK
  321. /* This function handles most / all (?) Winsock errors curl is able to produce.
  322. */
  323. static const char *
  324. get_winsock_error (int err, char *buf, size_t len)
  325. {
  326. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  327. DWORD old_win_err = GetLastError();
  328. #endif
  329. int old_errno = errno;
  330. const char *p;
  331. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  332. switch(err) {
  333. case WSAEINTR:
  334. p = "Call interrupted";
  335. break;
  336. case WSAEBADF:
  337. p = "Bad file";
  338. break;
  339. case WSAEACCES:
  340. p = "Bad access";
  341. break;
  342. case WSAEFAULT:
  343. p = "Bad argument";
  344. break;
  345. case WSAEINVAL:
  346. p = "Invalid arguments";
  347. break;
  348. case WSAEMFILE:
  349. p = "Out of file descriptors";
  350. break;
  351. case WSAEWOULDBLOCK:
  352. p = "Call would block";
  353. break;
  354. case WSAEINPROGRESS:
  355. case WSAEALREADY:
  356. p = "Blocking call in progress";
  357. break;
  358. case WSAENOTSOCK:
  359. p = "Descriptor is not a socket";
  360. break;
  361. case WSAEDESTADDRREQ:
  362. p = "Need destination address";
  363. break;
  364. case WSAEMSGSIZE:
  365. p = "Bad message size";
  366. break;
  367. case WSAEPROTOTYPE:
  368. p = "Bad protocol";
  369. break;
  370. case WSAENOPROTOOPT:
  371. p = "Protocol option is unsupported";
  372. break;
  373. case WSAEPROTONOSUPPORT:
  374. p = "Protocol is unsupported";
  375. break;
  376. case WSAESOCKTNOSUPPORT:
  377. p = "Socket is unsupported";
  378. break;
  379. case WSAEOPNOTSUPP:
  380. p = "Operation not supported";
  381. break;
  382. case WSAEAFNOSUPPORT:
  383. p = "Address family not supported";
  384. break;
  385. case WSAEPFNOSUPPORT:
  386. p = "Protocol family not supported";
  387. break;
  388. case WSAEADDRINUSE:
  389. p = "Address already in use";
  390. break;
  391. case WSAEADDRNOTAVAIL:
  392. p = "Address not available";
  393. break;
  394. case WSAENETDOWN:
  395. p = "Network down";
  396. break;
  397. case WSAENETUNREACH:
  398. p = "Network unreachable";
  399. break;
  400. case WSAENETRESET:
  401. p = "Network has been reset";
  402. break;
  403. case WSAECONNABORTED:
  404. p = "Connection was aborted";
  405. break;
  406. case WSAECONNRESET:
  407. p = "Connection was reset";
  408. break;
  409. case WSAENOBUFS:
  410. p = "No buffer space";
  411. break;
  412. case WSAEISCONN:
  413. p = "Socket is already connected";
  414. break;
  415. case WSAENOTCONN:
  416. p = "Socket is not connected";
  417. break;
  418. case WSAESHUTDOWN:
  419. p = "Socket has been shut down";
  420. break;
  421. case WSAETOOMANYREFS:
  422. p = "Too many references";
  423. break;
  424. case WSAETIMEDOUT:
  425. p = "Timed out";
  426. break;
  427. case WSAECONNREFUSED:
  428. p = "Connection refused";
  429. break;
  430. case WSAELOOP:
  431. p = "Loop??";
  432. break;
  433. case WSAENAMETOOLONG:
  434. p = "Name too long";
  435. break;
  436. case WSAEHOSTDOWN:
  437. p = "Host down";
  438. break;
  439. case WSAEHOSTUNREACH:
  440. p = "Host unreachable";
  441. break;
  442. case WSAENOTEMPTY:
  443. p = "Not empty";
  444. break;
  445. case WSAEPROCLIM:
  446. p = "Process limit reached";
  447. break;
  448. case WSAEUSERS:
  449. p = "Too many users";
  450. break;
  451. case WSAEDQUOT:
  452. p = "Bad quota";
  453. break;
  454. case WSAESTALE:
  455. p = "Something is stale";
  456. break;
  457. case WSAEREMOTE:
  458. p = "Remote error";
  459. break;
  460. #ifdef WSAEDISCON /* missing in SalfordC! */
  461. case WSAEDISCON:
  462. p = "Disconnected";
  463. break;
  464. #endif
  465. /* Extended Winsock errors */
  466. case WSASYSNOTREADY:
  467. p = "Winsock library is not ready";
  468. break;
  469. case WSANOTINITIALISED:
  470. p = "Winsock library not initialised";
  471. break;
  472. case WSAVERNOTSUPPORTED:
  473. p = "Winsock version not supported";
  474. break;
  475. /* getXbyY() errors (already handled in herrmsg):
  476. * Authoritative Answer: Host not found */
  477. case WSAHOST_NOT_FOUND:
  478. p = "Host not found";
  479. break;
  480. /* Non-Authoritative: Host not found, or SERVERFAIL */
  481. case WSATRY_AGAIN:
  482. p = "Host not found, try again";
  483. break;
  484. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  485. case WSANO_RECOVERY:
  486. p = "Unrecoverable error in call to nameserver";
  487. break;
  488. /* Valid name, no data record of requested type */
  489. case WSANO_DATA:
  490. p = "No data record of requested type";
  491. break;
  492. default:
  493. return NULL;
  494. }
  495. #else
  496. if(!err)
  497. return NULL;
  498. else
  499. p = "error";
  500. #endif
  501. strncpy(buf, p, len);
  502. buf [len-1] = '\0';
  503. if(errno != old_errno)
  504. errno = old_errno;
  505. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  506. if(old_win_err != GetLastError())
  507. SetLastError(old_win_err);
  508. #endif
  509. return buf;
  510. }
  511. #endif /* USE_WINSOCK */
  512. /*
  513. * Our thread-safe and smart strerror() replacement.
  514. *
  515. * The 'err' argument passed in to this function MUST be a true errno number
  516. * as reported on this system. We do no range checking on the number before
  517. * we pass it to the "number-to-message" conversion function and there might
  518. * be systems that don't do proper range checking in there themselves.
  519. *
  520. * We don't do range checking (on systems other than Windows) since there is
  521. * no good reliable and portable way to do it.
  522. */
  523. const char *Curl_strerror(struct connectdata *conn, int err)
  524. {
  525. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  526. DWORD old_win_err = GetLastError();
  527. #endif
  528. int old_errno = errno;
  529. char *buf, *p;
  530. size_t max;
  531. DEBUGASSERT(conn);
  532. DEBUGASSERT(err >= 0);
  533. buf = conn->syserr_buf;
  534. max = sizeof(conn->syserr_buf)-1;
  535. *buf = '\0';
  536. #ifdef USE_WINSOCK
  537. #ifdef _WIN32_WCE
  538. {
  539. wchar_t wbuf[256];
  540. wbuf[0] = L'\0';
  541. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  542. LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
  543. wcstombs(buf, wbuf, max);
  544. }
  545. #else
  546. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  547. if(err >= 0 && err < sys_nerr)
  548. strncpy(buf, strerror(err), max);
  549. else {
  550. if(!get_winsock_error(err, buf, max) &&
  551. !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
  552. LANG_NEUTRAL, buf, (DWORD)max, NULL))
  553. snprintf(buf, max, "Unknown error %d (%#x)", err, err);
  554. }
  555. #endif
  556. #else /* not USE_WINSOCK coming up */
  557. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  558. /*
  559. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  560. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  561. * message string, or EINVAL if 'errnum' is not a valid error number.
  562. */
  563. if(0 != strerror_r(err, buf, max)) {
  564. if('\0' == buf[0])
  565. snprintf(buf, max, "Unknown error %d", err);
  566. }
  567. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  568. /*
  569. * The glibc-style strerror_r() only *might* use the buffer we pass to
  570. * the function, but it always returns the error message as a pointer,
  571. * so we must copy that string unconditionally (if non-NULL).
  572. */
  573. {
  574. char buffer[256];
  575. char *msg = strerror_r(err, buffer, sizeof(buffer));
  576. if(msg)
  577. strncpy(buf, msg, max);
  578. else
  579. snprintf(buf, max, "Unknown error %d", err);
  580. }
  581. #elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
  582. /*
  583. * The vxworks-style strerror_r() does use the buffer we pass to the function.
  584. * The buffer size should be at least NAME_MAX (256)
  585. */
  586. {
  587. char buffer[256];
  588. if(OK == strerror_r(err, buffer))
  589. strncpy(buf, buffer, max);
  590. else
  591. snprintf(buf, max, "Unknown error %d", err);
  592. }
  593. #else
  594. {
  595. char *msg = strerror(err);
  596. if(msg)
  597. strncpy(buf, msg, max);
  598. else
  599. snprintf(buf, max, "Unknown error %d", err);
  600. }
  601. #endif
  602. #endif /* end of ! USE_WINSOCK */
  603. buf[max] = '\0'; /* make sure the string is zero terminated */
  604. /* strip trailing '\r\n' or '\n'. */
  605. p = strrchr(buf, '\n');
  606. if(p && (p - buf) >= 2)
  607. *p = '\0';
  608. p = strrchr(buf, '\r');
  609. if(p && (p - buf) >= 1)
  610. *p = '\0';
  611. if(errno != old_errno)
  612. errno = old_errno;
  613. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  614. if(old_win_err != GetLastError())
  615. SetLastError(old_win_err);
  616. #endif
  617. return buf;
  618. }
  619. #ifdef USE_WINDOWS_SSPI
  620. const char *Curl_sspi_strerror (struct connectdata *conn, int err)
  621. {
  622. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  623. DWORD old_win_err = GetLastError();
  624. #endif
  625. int old_errno = errno;
  626. const char *txt;
  627. char *outbuf;
  628. size_t outmax;
  629. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  630. char txtbuf[80];
  631. char msgbuf[sizeof(conn->syserr_buf)];
  632. char *p, *str, *msg = NULL;
  633. bool msg_formatted = FALSE;
  634. #endif
  635. DEBUGASSERT(conn);
  636. outbuf = conn->syserr_buf;
  637. outmax = sizeof(conn->syserr_buf)-1;
  638. *outbuf = '\0';
  639. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  640. switch(err) {
  641. case SEC_E_OK:
  642. txt = "No error";
  643. break;
  644. case CRYPT_E_REVOKED:
  645. txt = "CRYPT_E_REVOKED";
  646. break;
  647. case SEC_E_ALGORITHM_MISMATCH:
  648. txt = "SEC_E_ALGORITHM_MISMATCH";
  649. break;
  650. case SEC_E_BAD_BINDINGS:
  651. txt = "SEC_E_BAD_BINDINGS";
  652. break;
  653. case SEC_E_BAD_PKGID:
  654. txt = "SEC_E_BAD_PKGID";
  655. break;
  656. case SEC_E_BUFFER_TOO_SMALL:
  657. txt = "SEC_E_BUFFER_TOO_SMALL";
  658. break;
  659. case SEC_E_CANNOT_INSTALL:
  660. txt = "SEC_E_CANNOT_INSTALL";
  661. break;
  662. case SEC_E_CANNOT_PACK:
  663. txt = "SEC_E_CANNOT_PACK";
  664. break;
  665. case SEC_E_CERT_EXPIRED:
  666. txt = "SEC_E_CERT_EXPIRED";
  667. break;
  668. case SEC_E_CERT_UNKNOWN:
  669. txt = "SEC_E_CERT_UNKNOWN";
  670. break;
  671. case SEC_E_CERT_WRONG_USAGE:
  672. txt = "SEC_E_CERT_WRONG_USAGE";
  673. break;
  674. case SEC_E_CONTEXT_EXPIRED:
  675. txt = "SEC_E_CONTEXT_EXPIRED";
  676. break;
  677. case SEC_E_CROSSREALM_DELEGATION_FAILURE:
  678. txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
  679. break;
  680. case SEC_E_CRYPTO_SYSTEM_INVALID:
  681. txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
  682. break;
  683. case SEC_E_DECRYPT_FAILURE:
  684. txt = "SEC_E_DECRYPT_FAILURE";
  685. break;
  686. case SEC_E_DELEGATION_POLICY:
  687. txt = "SEC_E_DELEGATION_POLICY";
  688. break;
  689. case SEC_E_DELEGATION_REQUIRED:
  690. txt = "SEC_E_DELEGATION_REQUIRED";
  691. break;
  692. case SEC_E_DOWNGRADE_DETECTED:
  693. txt = "SEC_E_DOWNGRADE_DETECTED";
  694. break;
  695. case SEC_E_ENCRYPT_FAILURE:
  696. txt = "SEC_E_ENCRYPT_FAILURE";
  697. break;
  698. case SEC_E_ILLEGAL_MESSAGE:
  699. txt = "SEC_E_ILLEGAL_MESSAGE";
  700. break;
  701. case SEC_E_INCOMPLETE_CREDENTIALS:
  702. txt = "SEC_E_INCOMPLETE_CREDENTIALS";
  703. break;
  704. case SEC_E_INCOMPLETE_MESSAGE:
  705. txt = "SEC_E_INCOMPLETE_MESSAGE";
  706. break;
  707. case SEC_E_INSUFFICIENT_MEMORY:
  708. txt = "SEC_E_INSUFFICIENT_MEMORY";
  709. break;
  710. case SEC_E_INTERNAL_ERROR:
  711. txt = "SEC_E_INTERNAL_ERROR";
  712. break;
  713. case SEC_E_INVALID_HANDLE:
  714. txt = "SEC_E_INVALID_HANDLE";
  715. break;
  716. case SEC_E_INVALID_PARAMETER:
  717. txt = "SEC_E_INVALID_PARAMETER";
  718. break;
  719. case SEC_E_INVALID_TOKEN:
  720. txt = "SEC_E_INVALID_TOKEN";
  721. break;
  722. case SEC_E_ISSUING_CA_UNTRUSTED:
  723. txt = "SEC_E_ISSUING_CA_UNTRUSTED";
  724. break;
  725. case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
  726. txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
  727. break;
  728. case SEC_E_KDC_CERT_EXPIRED:
  729. txt = "SEC_E_KDC_CERT_EXPIRED";
  730. break;
  731. case SEC_E_KDC_CERT_REVOKED:
  732. txt = "SEC_E_KDC_CERT_REVOKED";
  733. break;
  734. case SEC_E_KDC_INVALID_REQUEST:
  735. txt = "SEC_E_KDC_INVALID_REQUEST";
  736. break;
  737. case SEC_E_KDC_UNABLE_TO_REFER:
  738. txt = "SEC_E_KDC_UNABLE_TO_REFER";
  739. break;
  740. case SEC_E_KDC_UNKNOWN_ETYPE:
  741. txt = "SEC_E_KDC_UNKNOWN_ETYPE";
  742. break;
  743. case SEC_E_LOGON_DENIED:
  744. txt = "SEC_E_LOGON_DENIED";
  745. break;
  746. case SEC_E_MAX_REFERRALS_EXCEEDED:
  747. txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
  748. break;
  749. case SEC_E_MESSAGE_ALTERED:
  750. txt = "SEC_E_MESSAGE_ALTERED";
  751. break;
  752. case SEC_E_MULTIPLE_ACCOUNTS:
  753. txt = "SEC_E_MULTIPLE_ACCOUNTS";
  754. break;
  755. case SEC_E_MUST_BE_KDC:
  756. txt = "SEC_E_MUST_BE_KDC";
  757. break;
  758. case SEC_E_NOT_OWNER:
  759. txt = "SEC_E_NOT_OWNER";
  760. break;
  761. case SEC_E_NO_AUTHENTICATING_AUTHORITY:
  762. txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
  763. break;
  764. case SEC_E_NO_CREDENTIALS:
  765. txt = "SEC_E_NO_CREDENTIALS";
  766. break;
  767. case SEC_E_NO_IMPERSONATION:
  768. txt = "SEC_E_NO_IMPERSONATION";
  769. break;
  770. case SEC_E_NO_IP_ADDRESSES:
  771. txt = "SEC_E_NO_IP_ADDRESSES";
  772. break;
  773. case SEC_E_NO_KERB_KEY:
  774. txt = "SEC_E_NO_KERB_KEY";
  775. break;
  776. case SEC_E_NO_PA_DATA:
  777. txt = "SEC_E_NO_PA_DATA";
  778. break;
  779. case SEC_E_NO_S4U_PROT_SUPPORT:
  780. txt = "SEC_E_NO_S4U_PROT_SUPPORT";
  781. break;
  782. case SEC_E_NO_TGT_REPLY:
  783. txt = "SEC_E_NO_TGT_REPLY";
  784. break;
  785. case SEC_E_OUT_OF_SEQUENCE:
  786. txt = "SEC_E_OUT_OF_SEQUENCE";
  787. break;
  788. case SEC_E_PKINIT_CLIENT_FAILURE:
  789. txt = "SEC_E_PKINIT_CLIENT_FAILURE";
  790. break;
  791. case SEC_E_PKINIT_NAME_MISMATCH:
  792. txt = "SEC_E_PKINIT_NAME_MISMATCH";
  793. break;
  794. case SEC_E_POLICY_NLTM_ONLY:
  795. txt = "SEC_E_POLICY_NLTM_ONLY";
  796. break;
  797. case SEC_E_QOP_NOT_SUPPORTED:
  798. txt = "SEC_E_QOP_NOT_SUPPORTED";
  799. break;
  800. case SEC_E_REVOCATION_OFFLINE_C:
  801. txt = "SEC_E_REVOCATION_OFFLINE_C";
  802. break;
  803. case SEC_E_REVOCATION_OFFLINE_KDC:
  804. txt = "SEC_E_REVOCATION_OFFLINE_KDC";
  805. break;
  806. case SEC_E_SECPKG_NOT_FOUND:
  807. txt = "SEC_E_SECPKG_NOT_FOUND";
  808. break;
  809. case SEC_E_SECURITY_QOS_FAILED:
  810. txt = "SEC_E_SECURITY_QOS_FAILED";
  811. break;
  812. case SEC_E_SHUTDOWN_IN_PROGRESS:
  813. txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
  814. break;
  815. case SEC_E_SMARTCARD_CERT_EXPIRED:
  816. txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
  817. break;
  818. case SEC_E_SMARTCARD_CERT_REVOKED:
  819. txt = "SEC_E_SMARTCARD_CERT_REVOKED";
  820. break;
  821. case SEC_E_SMARTCARD_LOGON_REQUIRED:
  822. txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
  823. break;
  824. case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
  825. txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
  826. break;
  827. case SEC_E_TARGET_UNKNOWN:
  828. txt = "SEC_E_TARGET_UNKNOWN";
  829. break;
  830. case SEC_E_TIME_SKEW:
  831. txt = "SEC_E_TIME_SKEW";
  832. break;
  833. case SEC_E_TOO_MANY_PRINCIPALS:
  834. txt = "SEC_E_TOO_MANY_PRINCIPALS";
  835. break;
  836. case SEC_E_UNFINISHED_CONTEXT_DELETED:
  837. txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
  838. break;
  839. case SEC_E_UNKNOWN_CREDENTIALS:
  840. txt = "SEC_E_UNKNOWN_CREDENTIALS";
  841. break;
  842. case SEC_E_UNSUPPORTED_FUNCTION:
  843. txt = "SEC_E_UNSUPPORTED_FUNCTION";
  844. break;
  845. case SEC_E_UNSUPPORTED_PREAUTH:
  846. txt = "SEC_E_UNSUPPORTED_PREAUTH";
  847. break;
  848. case SEC_E_UNTRUSTED_ROOT:
  849. txt = "SEC_E_UNTRUSTED_ROOT";
  850. break;
  851. case SEC_E_WRONG_CREDENTIAL_HANDLE:
  852. txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
  853. break;
  854. case SEC_E_WRONG_PRINCIPAL:
  855. txt = "SEC_E_WRONG_PRINCIPAL";
  856. break;
  857. case SEC_I_COMPLETE_AND_CONTINUE:
  858. txt = "SEC_I_COMPLETE_AND_CONTINUE";
  859. break;
  860. case SEC_I_COMPLETE_NEEDED:
  861. txt = "SEC_I_COMPLETE_NEEDED";
  862. break;
  863. case SEC_I_CONTEXT_EXPIRED:
  864. txt = "SEC_I_CONTEXT_EXPIRED";
  865. break;
  866. case SEC_I_CONTINUE_NEEDED:
  867. txt = "SEC_I_CONTINUE_NEEDED";
  868. break;
  869. case SEC_I_INCOMPLETE_CREDENTIALS:
  870. txt = "SEC_I_INCOMPLETE_CREDENTIALS";
  871. break;
  872. case SEC_I_LOCAL_LOGON:
  873. txt = "SEC_I_LOCAL_LOGON";
  874. break;
  875. case SEC_I_NO_LSA_CONTEXT:
  876. txt = "SEC_I_NO_LSA_CONTEXT";
  877. break;
  878. case SEC_I_RENEGOTIATE:
  879. txt = "SEC_I_RENEGOTIATE";
  880. break;
  881. case SEC_I_SIGNATURE_NEEDED:
  882. txt = "SEC_I_SIGNATURE_NEEDED";
  883. break;
  884. default:
  885. txt = "Unknown error";
  886. }
  887. if(err == SEC_E_OK)
  888. strncpy(outbuf, txt, outmax);
  889. else if(err == SEC_E_ILLEGAL_MESSAGE)
  890. snprintf(outbuf, outmax,
  891. "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
  892. "when a fatal SSL/TLS alert is received (e.g. handshake failed). "
  893. "More detail may be available in the Windows System event log.",
  894. err);
  895. else {
  896. str = txtbuf;
  897. snprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
  898. txtbuf[sizeof(txtbuf)-1] = '\0';
  899. #ifdef _WIN32_WCE
  900. {
  901. wchar_t wbuf[256];
  902. wbuf[0] = L'\0';
  903. if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  904. FORMAT_MESSAGE_IGNORE_INSERTS,
  905. NULL, err, LANG_NEUTRAL,
  906. wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
  907. wcstombs(msgbuf, wbuf, sizeof(msgbuf)-1);
  908. msg_formatted = TRUE;
  909. }
  910. }
  911. #else
  912. if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
  913. FORMAT_MESSAGE_IGNORE_INSERTS,
  914. NULL, err, LANG_NEUTRAL,
  915. msgbuf, sizeof(msgbuf)-1, NULL)) {
  916. msg_formatted = TRUE;
  917. }
  918. #endif
  919. if(msg_formatted) {
  920. msgbuf[sizeof(msgbuf)-1] = '\0';
  921. /* strip trailing '\r\n' or '\n' */
  922. p = strrchr(msgbuf, '\n');
  923. if(p && (p - msgbuf) >= 2)
  924. *p = '\0';
  925. p = strrchr(msgbuf, '\r');
  926. if(p && (p - msgbuf) >= 1)
  927. *p = '\0';
  928. msg = msgbuf;
  929. }
  930. if(msg)
  931. snprintf(outbuf, outmax, "%s - %s", str, msg);
  932. else
  933. strncpy(outbuf, str, outmax);
  934. }
  935. #else
  936. if(err == SEC_E_OK)
  937. txt = "No error";
  938. else
  939. txt = "Error";
  940. strncpy(outbuf, txt, outmax);
  941. #endif
  942. outbuf[outmax] = '\0';
  943. if(errno != old_errno)
  944. errno = old_errno;
  945. #ifdef PRESERVE_WINDOWS_ERROR_CODE
  946. if(old_win_err != GetLastError())
  947. SetLastError(old_win_err);
  948. #endif
  949. return outbuf;
  950. }
  951. #endif /* USE_WINDOWS_SSPI */