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