strerror.c 28 KB

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