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