Imported Upstream version 7.59.0
[platform/upstream/curl.git] / lib / strerror.c
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
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 *
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_PEER_FAILED_VERIFICATION:
195     return "SSL peer certificate or SSH remote key was not OK";
196
197   case CURLE_GOT_NOTHING:
198     return "Server returned nothing (no headers, no data)";
199
200   case CURLE_SSL_ENGINE_NOTFOUND:
201     return "SSL crypto engine not found";
202
203   case CURLE_SSL_ENGINE_SETFAILED:
204     return "Can not set SSL crypto engine as default";
205
206   case CURLE_SSL_ENGINE_INITFAILED:
207     return "Failed to initialise SSL crypto engine";
208
209   case CURLE_SEND_ERROR:
210     return "Failed sending data to the peer";
211
212   case CURLE_RECV_ERROR:
213     return "Failure when receiving data from the peer";
214
215   case CURLE_SSL_CERTPROBLEM:
216     return "Problem with the local SSL certificate";
217
218   case CURLE_SSL_CIPHER:
219     return "Couldn't use specified SSL cipher";
220
221   case CURLE_SSL_CACERT:
222     return "Peer certificate cannot be authenticated with given CA "
223       "certificates";
224
225   case CURLE_SSL_CACERT_BADFILE:
226     return "Problem with the SSL CA cert (path? access rights?)";
227
228   case CURLE_BAD_CONTENT_ENCODING:
229     return "Unrecognized or bad HTTP Content or Transfer-Encoding";
230
231   case CURLE_LDAP_INVALID_URL:
232     return "Invalid LDAP URL";
233
234   case CURLE_FILESIZE_EXCEEDED:
235     return "Maximum file size exceeded";
236
237   case CURLE_USE_SSL_FAILED:
238     return "Requested SSL level failed";
239
240   case CURLE_SSL_SHUTDOWN_FAILED:
241     return "Failed to shut down the SSL connection";
242
243   case CURLE_SSL_CRL_BADFILE:
244     return "Failed to load CRL file (path? access rights?, format?)";
245
246   case CURLE_SSL_ISSUER_ERROR:
247     return "Issuer check against peer certificate failed";
248
249   case CURLE_SEND_FAIL_REWIND:
250     return "Send failed since rewinding of the data stream failed";
251
252   case CURLE_LOGIN_DENIED:
253     return "Login denied";
254
255   case CURLE_TFTP_NOTFOUND:
256     return "TFTP: File Not Found";
257
258   case CURLE_TFTP_PERM:
259     return "TFTP: Access Violation";
260
261   case CURLE_REMOTE_DISK_FULL:
262     return "Disk full or allocation exceeded";
263
264   case CURLE_TFTP_ILLEGAL:
265     return "TFTP: Illegal operation";
266
267   case CURLE_TFTP_UNKNOWNID:
268     return "TFTP: Unknown transfer ID";
269
270   case CURLE_REMOTE_FILE_EXISTS:
271     return "Remote file already exists";
272
273   case CURLE_TFTP_NOSUCHUSER:
274     return "TFTP: No such user";
275
276   case CURLE_CONV_FAILED:
277     return "Conversion failed";
278
279   case CURLE_CONV_REQD:
280     return "Caller must register CURLOPT_CONV_ callback options";
281
282   case CURLE_REMOTE_FILE_NOT_FOUND:
283     return "Remote file not found";
284
285   case CURLE_SSH:
286     return "Error in the SSH layer";
287
288   case CURLE_AGAIN:
289     return "Socket not ready for send/recv";
290
291   case CURLE_RTSP_CSEQ_ERROR:
292     return "RTSP CSeq mismatch or invalid CSeq";
293
294   case CURLE_RTSP_SESSION_ERROR:
295     return "RTSP session error";
296
297   case CURLE_FTP_BAD_FILE_LIST:
298     return "Unable to parse FTP file list";
299
300   case CURLE_CHUNK_FAILED:
301     return "Chunk callback failed";
302
303   case CURLE_NO_CONNECTION_AVAILABLE:
304     return "The max connection limit is reached";
305
306   case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
307     return "SSL public key does not match pinned public key";
308
309   case CURLE_SSL_INVALIDCERTSTATUS:
310     return "SSL server certificate status verification FAILED";
311
312   case CURLE_HTTP2_STREAM:
313     return "Stream error in the HTTP/2 framing layer";
314
315   case CURLE_RECURSIVE_API_CALL:
316     return "API function called from within callback";
317
318     /* error codes not used by current libcurl */
319   case CURLE_OBSOLETE20:
320   case CURLE_OBSOLETE24:
321   case CURLE_OBSOLETE29:
322   case CURLE_OBSOLETE32:
323   case CURLE_OBSOLETE40:
324   case CURLE_OBSOLETE44:
325   case CURLE_OBSOLETE46:
326   case CURLE_OBSOLETE50:
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 *
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 *
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 *
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  */
652 const char *Curl_strerror(struct connectdata *conn, int err)
653 {
654 #ifdef PRESERVE_WINDOWS_ERROR_CODE
655   DWORD old_win_err = GetLastError();
656 #endif
657   int old_errno = errno;
658   char *buf, *p;
659   size_t max;
660
661   DEBUGASSERT(conn);
662   DEBUGASSERT(err >= 0);
663
664   buf = conn->syserr_buf;
665   max = sizeof(conn->syserr_buf)-1;
666   *buf = '\0';
667
668 #ifdef USE_WINSOCK
669
670 #ifdef _WIN32_WCE
671   {
672     wchar_t wbuf[256];
673     wbuf[0] = L'\0';
674
675     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
676                   LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
677     wcstombs(buf, wbuf, max);
678   }
679 #else
680   /* 'sys_nerr' is the maximum errno number, it is not widely portable */
681   if(err >= 0 && err < sys_nerr)
682     strncpy(buf, strerror(err), max);
683   else {
684     if(!get_winsock_error(err, buf, max) &&
685        !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
686                        LANG_NEUTRAL, buf, (DWORD)max, NULL))
687       snprintf(buf, max, "Unknown error %d (%#x)", err, err);
688   }
689 #endif
690
691 #else /* not USE_WINSOCK coming up */
692
693 #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
694  /*
695   * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
696   * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
697   * message string, or EINVAL if 'errnum' is not a valid error number.
698   */
699   if(0 != strerror_r(err, buf, max)) {
700     if('\0' == buf[0])
701       snprintf(buf, max, "Unknown error %d", err);
702   }
703 #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
704  /*
705   * The glibc-style strerror_r() only *might* use the buffer we pass to
706   * the function, but it always returns the error message as a pointer,
707   * so we must copy that string unconditionally (if non-NULL).
708   */
709   {
710     char buffer[256];
711     char *msg = strerror_r(err, buffer, sizeof(buffer));
712     if(msg)
713       strncpy(buf, msg, max);
714     else
715       snprintf(buf, max, "Unknown error %d", err);
716   }
717 #elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
718  /*
719   * The vxworks-style strerror_r() does use the buffer we pass to the function.
720   * The buffer size should be at least NAME_MAX (256)
721   */
722   {
723     char buffer[256];
724     if(OK == strerror_r(err, buffer))
725       strncpy(buf, buffer, max);
726     else
727       snprintf(buf, max, "Unknown error %d", err);
728   }
729 #else
730   {
731     char *msg = strerror(err);
732     if(msg)
733       strncpy(buf, msg, max);
734     else
735       snprintf(buf, max, "Unknown error %d", err);
736   }
737 #endif
738
739 #endif /* end of ! USE_WINSOCK */
740
741   buf[max] = '\0'; /* make sure the string is zero terminated */
742
743   /* strip trailing '\r\n' or '\n'. */
744   p = strrchr(buf, '\n');
745   if(p && (p - buf) >= 2)
746     *p = '\0';
747   p = strrchr(buf, '\r');
748   if(p && (p - buf) >= 1)
749     *p = '\0';
750
751   if(errno != old_errno)
752     errno = old_errno;
753
754 #ifdef PRESERVE_WINDOWS_ERROR_CODE
755   if(old_win_err != GetLastError())
756     SetLastError(old_win_err);
757 #endif
758
759   return buf;
760 }
761
762 #ifdef USE_WINDOWS_SSPI
763 const char *Curl_sspi_strerror (struct connectdata *conn, int err)
764 {
765 #ifdef PRESERVE_WINDOWS_ERROR_CODE
766   DWORD old_win_err = GetLastError();
767 #endif
768   int old_errno = errno;
769   const char *txt;
770   char *outbuf;
771   size_t outmax;
772 #ifndef CURL_DISABLE_VERBOSE_STRINGS
773   char txtbuf[80];
774   char msgbuf[sizeof(conn->syserr_buf)];
775   char *p, *str, *msg = NULL;
776   bool msg_formatted = FALSE;
777 #endif
778
779   DEBUGASSERT(conn);
780
781   outbuf = conn->syserr_buf;
782   outmax = sizeof(conn->syserr_buf)-1;
783   *outbuf = '\0';
784
785 #ifndef CURL_DISABLE_VERBOSE_STRINGS
786
787   switch(err) {
788     case SEC_E_OK:
789       txt = "No error";
790       break;
791     case CRYPT_E_REVOKED:
792       txt = "CRYPT_E_REVOKED";
793       break;
794     case SEC_E_ALGORITHM_MISMATCH:
795       txt = "SEC_E_ALGORITHM_MISMATCH";
796       break;
797     case SEC_E_BAD_BINDINGS:
798       txt = "SEC_E_BAD_BINDINGS";
799       break;
800     case SEC_E_BAD_PKGID:
801       txt = "SEC_E_BAD_PKGID";
802       break;
803     case SEC_E_BUFFER_TOO_SMALL:
804       txt = "SEC_E_BUFFER_TOO_SMALL";
805       break;
806     case SEC_E_CANNOT_INSTALL:
807       txt = "SEC_E_CANNOT_INSTALL";
808       break;
809     case SEC_E_CANNOT_PACK:
810       txt = "SEC_E_CANNOT_PACK";
811       break;
812     case SEC_E_CERT_EXPIRED:
813       txt = "SEC_E_CERT_EXPIRED";
814       break;
815     case SEC_E_CERT_UNKNOWN:
816       txt = "SEC_E_CERT_UNKNOWN";
817       break;
818     case SEC_E_CERT_WRONG_USAGE:
819       txt = "SEC_E_CERT_WRONG_USAGE";
820       break;
821     case SEC_E_CONTEXT_EXPIRED:
822       txt = "SEC_E_CONTEXT_EXPIRED";
823       break;
824     case SEC_E_CROSSREALM_DELEGATION_FAILURE:
825       txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
826       break;
827     case SEC_E_CRYPTO_SYSTEM_INVALID:
828       txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
829       break;
830     case SEC_E_DECRYPT_FAILURE:
831       txt = "SEC_E_DECRYPT_FAILURE";
832       break;
833     case SEC_E_DELEGATION_POLICY:
834       txt = "SEC_E_DELEGATION_POLICY";
835       break;
836     case SEC_E_DELEGATION_REQUIRED:
837       txt = "SEC_E_DELEGATION_REQUIRED";
838       break;
839     case SEC_E_DOWNGRADE_DETECTED:
840       txt = "SEC_E_DOWNGRADE_DETECTED";
841       break;
842     case SEC_E_ENCRYPT_FAILURE:
843       txt = "SEC_E_ENCRYPT_FAILURE";
844       break;
845     case SEC_E_ILLEGAL_MESSAGE:
846       txt = "SEC_E_ILLEGAL_MESSAGE";
847       break;
848     case SEC_E_INCOMPLETE_CREDENTIALS:
849       txt = "SEC_E_INCOMPLETE_CREDENTIALS";
850       break;
851     case SEC_E_INCOMPLETE_MESSAGE:
852       txt = "SEC_E_INCOMPLETE_MESSAGE";
853       break;
854     case SEC_E_INSUFFICIENT_MEMORY:
855       txt = "SEC_E_INSUFFICIENT_MEMORY";
856       break;
857     case SEC_E_INTERNAL_ERROR:
858       txt = "SEC_E_INTERNAL_ERROR";
859       break;
860     case SEC_E_INVALID_HANDLE:
861       txt = "SEC_E_INVALID_HANDLE";
862       break;
863     case SEC_E_INVALID_PARAMETER:
864       txt = "SEC_E_INVALID_PARAMETER";
865       break;
866     case SEC_E_INVALID_TOKEN:
867       txt = "SEC_E_INVALID_TOKEN";
868       break;
869     case SEC_E_ISSUING_CA_UNTRUSTED:
870       txt = "SEC_E_ISSUING_CA_UNTRUSTED";
871       break;
872     case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
873       txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
874       break;
875     case SEC_E_KDC_CERT_EXPIRED:
876       txt = "SEC_E_KDC_CERT_EXPIRED";
877       break;
878     case SEC_E_KDC_CERT_REVOKED:
879       txt = "SEC_E_KDC_CERT_REVOKED";
880       break;
881     case SEC_E_KDC_INVALID_REQUEST:
882       txt = "SEC_E_KDC_INVALID_REQUEST";
883       break;
884     case SEC_E_KDC_UNABLE_TO_REFER:
885       txt = "SEC_E_KDC_UNABLE_TO_REFER";
886       break;
887     case SEC_E_KDC_UNKNOWN_ETYPE:
888       txt = "SEC_E_KDC_UNKNOWN_ETYPE";
889       break;
890     case SEC_E_LOGON_DENIED:
891       txt = "SEC_E_LOGON_DENIED";
892       break;
893     case SEC_E_MAX_REFERRALS_EXCEEDED:
894       txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
895       break;
896     case SEC_E_MESSAGE_ALTERED:
897       txt = "SEC_E_MESSAGE_ALTERED";
898       break;
899     case SEC_E_MULTIPLE_ACCOUNTS:
900       txt = "SEC_E_MULTIPLE_ACCOUNTS";
901       break;
902     case SEC_E_MUST_BE_KDC:
903       txt = "SEC_E_MUST_BE_KDC";
904       break;
905     case SEC_E_NOT_OWNER:
906       txt = "SEC_E_NOT_OWNER";
907       break;
908     case SEC_E_NO_AUTHENTICATING_AUTHORITY:
909       txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
910       break;
911     case SEC_E_NO_CREDENTIALS:
912       txt = "SEC_E_NO_CREDENTIALS";
913       break;
914     case SEC_E_NO_IMPERSONATION:
915       txt = "SEC_E_NO_IMPERSONATION";
916       break;
917     case SEC_E_NO_IP_ADDRESSES:
918       txt = "SEC_E_NO_IP_ADDRESSES";
919       break;
920     case SEC_E_NO_KERB_KEY:
921       txt = "SEC_E_NO_KERB_KEY";
922       break;
923     case SEC_E_NO_PA_DATA:
924       txt = "SEC_E_NO_PA_DATA";
925       break;
926     case SEC_E_NO_S4U_PROT_SUPPORT:
927       txt = "SEC_E_NO_S4U_PROT_SUPPORT";
928       break;
929     case SEC_E_NO_TGT_REPLY:
930       txt = "SEC_E_NO_TGT_REPLY";
931       break;
932     case SEC_E_OUT_OF_SEQUENCE:
933       txt = "SEC_E_OUT_OF_SEQUENCE";
934       break;
935     case SEC_E_PKINIT_CLIENT_FAILURE:
936       txt = "SEC_E_PKINIT_CLIENT_FAILURE";
937       break;
938     case SEC_E_PKINIT_NAME_MISMATCH:
939       txt = "SEC_E_PKINIT_NAME_MISMATCH";
940       break;
941     case SEC_E_POLICY_NLTM_ONLY:
942       txt = "SEC_E_POLICY_NLTM_ONLY";
943       break;
944     case SEC_E_QOP_NOT_SUPPORTED:
945       txt = "SEC_E_QOP_NOT_SUPPORTED";
946       break;
947     case SEC_E_REVOCATION_OFFLINE_C:
948       txt = "SEC_E_REVOCATION_OFFLINE_C";
949       break;
950     case SEC_E_REVOCATION_OFFLINE_KDC:
951       txt = "SEC_E_REVOCATION_OFFLINE_KDC";
952       break;
953     case SEC_E_SECPKG_NOT_FOUND:
954       txt = "SEC_E_SECPKG_NOT_FOUND";
955       break;
956     case SEC_E_SECURITY_QOS_FAILED:
957       txt = "SEC_E_SECURITY_QOS_FAILED";
958       break;
959     case SEC_E_SHUTDOWN_IN_PROGRESS:
960       txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
961       break;
962     case SEC_E_SMARTCARD_CERT_EXPIRED:
963       txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
964       break;
965     case SEC_E_SMARTCARD_CERT_REVOKED:
966       txt = "SEC_E_SMARTCARD_CERT_REVOKED";
967       break;
968     case SEC_E_SMARTCARD_LOGON_REQUIRED:
969       txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
970       break;
971     case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
972       txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
973       break;
974     case SEC_E_TARGET_UNKNOWN:
975       txt = "SEC_E_TARGET_UNKNOWN";
976       break;
977     case SEC_E_TIME_SKEW:
978       txt = "SEC_E_TIME_SKEW";
979       break;
980     case SEC_E_TOO_MANY_PRINCIPALS:
981       txt = "SEC_E_TOO_MANY_PRINCIPALS";
982       break;
983     case SEC_E_UNFINISHED_CONTEXT_DELETED:
984       txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
985       break;
986     case SEC_E_UNKNOWN_CREDENTIALS:
987       txt = "SEC_E_UNKNOWN_CREDENTIALS";
988       break;
989     case SEC_E_UNSUPPORTED_FUNCTION:
990       txt = "SEC_E_UNSUPPORTED_FUNCTION";
991       break;
992     case SEC_E_UNSUPPORTED_PREAUTH:
993       txt = "SEC_E_UNSUPPORTED_PREAUTH";
994       break;
995     case SEC_E_UNTRUSTED_ROOT:
996       txt = "SEC_E_UNTRUSTED_ROOT";
997       break;
998     case SEC_E_WRONG_CREDENTIAL_HANDLE:
999       txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
1000       break;
1001     case SEC_E_WRONG_PRINCIPAL:
1002       txt = "SEC_E_WRONG_PRINCIPAL";
1003       break;
1004     case SEC_I_COMPLETE_AND_CONTINUE:
1005       txt = "SEC_I_COMPLETE_AND_CONTINUE";
1006       break;
1007     case SEC_I_COMPLETE_NEEDED:
1008       txt = "SEC_I_COMPLETE_NEEDED";
1009       break;
1010     case SEC_I_CONTEXT_EXPIRED:
1011       txt = "SEC_I_CONTEXT_EXPIRED";
1012       break;
1013     case SEC_I_CONTINUE_NEEDED:
1014       txt = "SEC_I_CONTINUE_NEEDED";
1015       break;
1016     case SEC_I_INCOMPLETE_CREDENTIALS:
1017       txt = "SEC_I_INCOMPLETE_CREDENTIALS";
1018       break;
1019     case SEC_I_LOCAL_LOGON:
1020       txt = "SEC_I_LOCAL_LOGON";
1021       break;
1022     case SEC_I_NO_LSA_CONTEXT:
1023       txt = "SEC_I_NO_LSA_CONTEXT";
1024       break;
1025     case SEC_I_RENEGOTIATE:
1026       txt = "SEC_I_RENEGOTIATE";
1027       break;
1028     case SEC_I_SIGNATURE_NEEDED:
1029       txt = "SEC_I_SIGNATURE_NEEDED";
1030       break;
1031     default:
1032       txt = "Unknown error";
1033   }
1034
1035   if(err == SEC_E_OK)
1036     strncpy(outbuf, txt, outmax);
1037   else if(err == SEC_E_ILLEGAL_MESSAGE)
1038     snprintf(outbuf, outmax,
1039              "SEC_E_ILLEGAL_MESSAGE (0x%08X) - This error usually occurs "
1040              "when a fatal SSL/TLS alert is received (e.g. handshake failed). "
1041              "More detail may be available in the Windows System event log.",
1042              err);
1043   else {
1044     str = txtbuf;
1045     snprintf(txtbuf, sizeof(txtbuf), "%s (0x%08X)", txt, err);
1046     txtbuf[sizeof(txtbuf)-1] = '\0';
1047
1048 #ifdef _WIN32_WCE
1049     {
1050       wchar_t wbuf[256];
1051       wbuf[0] = L'\0';
1052
1053       if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
1054                        FORMAT_MESSAGE_IGNORE_INSERTS,
1055                        NULL, err, LANG_NEUTRAL,
1056                        wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
1057         wcstombs(msgbuf, wbuf, sizeof(msgbuf)-1);
1058         msg_formatted = TRUE;
1059       }
1060     }
1061 #else
1062     if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
1063                       FORMAT_MESSAGE_IGNORE_INSERTS,
1064                       NULL, err, LANG_NEUTRAL,
1065                       msgbuf, sizeof(msgbuf)-1, NULL)) {
1066       msg_formatted = TRUE;
1067     }
1068 #endif
1069     if(msg_formatted) {
1070       msgbuf[sizeof(msgbuf)-1] = '\0';
1071       /* strip trailing '\r\n' or '\n' */
1072       p = strrchr(msgbuf, '\n');
1073       if(p && (p - msgbuf) >= 2)
1074         *p = '\0';
1075       p = strrchr(msgbuf, '\r');
1076       if(p && (p - msgbuf) >= 1)
1077         *p = '\0';
1078       msg = msgbuf;
1079     }
1080     if(msg)
1081       snprintf(outbuf, outmax, "%s - %s", str, msg);
1082     else
1083       strncpy(outbuf, str, outmax);
1084   }
1085
1086 #else
1087
1088   if(err == SEC_E_OK)
1089     txt = "No error";
1090   else
1091     txt = "Error";
1092
1093   strncpy(outbuf, txt, outmax);
1094
1095 #endif
1096
1097   outbuf[outmax] = '\0';
1098
1099   if(errno != old_errno)
1100     errno = old_errno;
1101
1102 #ifdef PRESERVE_WINDOWS_ERROR_CODE
1103   if(old_win_err != GetLastError())
1104     SetLastError(old_win_err);
1105 #endif
1106
1107   return outbuf;
1108 }
1109 #endif /* USE_WINDOWS_SSPI */