1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2004 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
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_ADDED_ALREADY:
365    return "The easy handle is already added to a multi handle";
366
367  case CURLM_LAST:
368    break;
369  }
370
371  return "Unknown error";
372#else
373  if(error == CURLM_OK)
374    return "No error";
375  else
376    return "Error";
377#endif
378}
379
380const char *
381curl_share_strerror(CURLSHcode error)
382{
383#ifndef CURL_DISABLE_VERBOSE_STRINGS
384  switch (error) {
385  case CURLSHE_OK:
386    return "No error";
387
388  case CURLSHE_BAD_OPTION:
389    return "Unknown share option";
390
391  case CURLSHE_IN_USE:
392    return "Share currently in use";
393
394  case CURLSHE_INVALID:
395    return "Invalid share handle";
396
397  case CURLSHE_NOMEM:
398    return "Out of memory";
399
400  case CURLSHE_NOT_BUILT_IN:
401    return "Feature not enabled in this library";
402
403  case CURLSHE_LAST:
404    break;
405  }
406
407  return "CURLSHcode unknown";
408#else
409  if(error == CURLSHE_OK)
410    return "No error";
411  else
412    return "Error";
413#endif
414}
415
416#ifdef USE_WINSOCK
417
418/* This function handles most / all (?) Winsock errors cURL is able to produce.
419 */
420static const char *
421get_winsock_error (int err, char *buf, size_t len)
422{
423  const char *p;
424
425#ifndef CURL_DISABLE_VERBOSE_STRINGS
426  switch (err) {
427  case WSAEINTR:
428    p = "Call interrupted";
429    break;
430  case WSAEBADF:
431    p = "Bad file";
432    break;
433  case WSAEACCES:
434    p = "Bad access";
435    break;
436  case WSAEFAULT:
437    p = "Bad argument";
438    break;
439  case WSAEINVAL:
440    p = "Invalid arguments";
441    break;
442  case WSAEMFILE:
443    p = "Out of file descriptors";
444    break;
445  case WSAEWOULDBLOCK:
446    p = "Call would block";
447    break;
448  case WSAEINPROGRESS:
449  case WSAEALREADY:
450    p = "Blocking call in progress";
451    break;
452  case WSAENOTSOCK:
453    p = "Descriptor is not a socket";
454    break;
455  case WSAEDESTADDRREQ:
456    p = "Need destination address";
457    break;
458  case WSAEMSGSIZE:
459    p = "Bad message size";
460    break;
461  case WSAEPROTOTYPE:
462    p = "Bad protocol";
463    break;
464  case WSAENOPROTOOPT:
465    p = "Protocol option is unsupported";
466    break;
467  case WSAEPROTONOSUPPORT:
468    p = "Protocol is unsupported";
469    break;
470  case WSAESOCKTNOSUPPORT:
471    p = "Socket is unsupported";
472    break;
473  case WSAEOPNOTSUPP:
474    p = "Operation not supported";
475    break;
476  case WSAEAFNOSUPPORT:
477    p = "Address family not supported";
478    break;
479  case WSAEPFNOSUPPORT:
480    p = "Protocol family not supported";
481    break;
482  case WSAEADDRINUSE:
483    p = "Address already in use";
484    break;
485  case WSAEADDRNOTAVAIL:
486    p = "Address not available";
487    break;
488  case WSAENETDOWN:
489    p = "Network down";
490    break;
491  case WSAENETUNREACH:
492    p = "Network unreachable";
493    break;
494  case WSAENETRESET:
495    p = "Network has been reset";
496    break;
497  case WSAECONNABORTED:
498    p = "Connection was aborted";
499    break;
500  case WSAECONNRESET:
501    p = "Connection was reset";
502    break;
503  case WSAENOBUFS:
504    p = "No buffer space";
505    break;
506  case WSAEISCONN:
507    p = "Socket is already connected";
508    break;
509  case WSAENOTCONN:
510    p = "Socket is not connected";
511    break;
512  case WSAESHUTDOWN:
513    p = "Socket has been shut down";
514    break;
515  case WSAETOOMANYREFS:
516    p = "Too many references";
517    break;
518  case WSAETIMEDOUT:
519    p = "Timed out";
520    break;
521  case WSAECONNREFUSED:
522    p = "Connection refused";
523    break;
524  case WSAELOOP:
525    p = "Loop??";
526    break;
527  case WSAENAMETOOLONG:
528    p = "Name too long";
529    break;
530  case WSAEHOSTDOWN:
531    p = "Host down";
532    break;
533  case WSAEHOSTUNREACH:
534    p = "Host unreachable";
535    break;
536  case WSAENOTEMPTY:
537    p = "Not empty";
538    break;
539  case WSAEPROCLIM:
540    p = "Process limit reached";
541    break;
542  case WSAEUSERS:
543    p = "Too many users";
544    break;
545  case WSAEDQUOT:
546    p = "Bad quota";
547    break;
548  case WSAESTALE:
549    p = "Something is stale";
550    break;
551  case WSAEREMOTE:
552    p = "Remote error";
553    break;
554#ifdef WSAEDISCON  /* missing in SalfordC! */
555  case WSAEDISCON:
556    p = "Disconnected";
557    break;
558#endif
559    /* Extended Winsock errors */
560  case WSASYSNOTREADY:
561    p = "Winsock library is not ready";
562    break;
563  case WSANOTINITIALISED:
564    p = "Winsock library not initialised";
565    break;
566  case WSAVERNOTSUPPORTED:
567    p = "Winsock version not supported";
568    break;
569
570    /* getXbyY() errors (already handled in herrmsg):
571     * Authoritative Answer: Host not found */
572  case WSAHOST_NOT_FOUND:
573    p = "Host not found";
574    break;
575
576    /* Non-Authoritative: Host not found, or SERVERFAIL */
577  case WSATRY_AGAIN:
578    p = "Host not found, try again";
579    break;
580
581    /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
582  case WSANO_RECOVERY:
583    p = "Unrecoverable error in call to nameserver";
584    break;
585
586    /* Valid name, no data record of requested type */
587  case WSANO_DATA:
588    p = "No data record of requested type";
589    break;
590
591  default:
592    return NULL;
593  }
594#else
595  if(err == CURLE_OK)
596    return NULL;
597  else
598    p = "error";
599#endif
600  strncpy (buf, p, len);
601  buf [len-1] = '\0';
602  return buf;
603}
604#endif   /* USE_WINSOCK */
605
606/*
607 * Our thread-safe and smart strerror() replacement.
608 *
609 * The 'err' argument passed in to this function MUST be a true errno number
610 * as reported on this system. We do no range checking on the number before
611 * we pass it to the "number-to-message" conversion function and there might
612 * be systems that don't do proper range checking in there themselves.
613 *
614 * We don't do range checking (on systems other than Windows) since there is
615 * no good reliable and portable way to do it.
616 */
617const char *Curl_strerror(struct connectdata *conn, int err)
618{
619  char *buf, *p;
620  size_t max;
621  int old_errno = ERRNO;
622
623  DEBUGASSERT(conn);
624  DEBUGASSERT(err >= 0);
625
626  buf = conn->syserr_buf;
627  max = sizeof(conn->syserr_buf)-1;
628  *buf = '\0';
629
630#ifdef USE_WINSOCK
631
632#ifdef _WIN32_WCE
633  {
634    wchar_t wbuf[256];
635    wbuf[0] = L'\0';
636
637    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
638                  LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
639    wcstombs(buf,wbuf,max);
640  }
641#else
642  /* 'sys_nerr' is the maximum errno number, it is not widely portable */
643  if(err >= 0 && err < sys_nerr)
644    strncpy(buf, strerror(err), max);
645  else {
646    if(!get_winsock_error(err, buf, max) &&
647       !FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
648                       LANG_NEUTRAL, buf, (DWORD)max, NULL))
649      snprintf(buf, max, "Unknown error %d (%#x)", err, err);
650  }
651#endif
652
653#else /* not USE_WINSOCK coming up */
654
655#if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
656 /*
657  * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
658  * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
659  * message string, or EINVAL if 'errnum' is not a valid error number.
660  */
661  if(0 != strerror_r(err, buf, max)) {
662    if('\0' == buf[0])
663      snprintf(buf, max, "Unknown error %d", err);
664  }
665#elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
666 /*
667  * The glibc-style strerror_r() only *might* use the buffer we pass to
668  * the function, but it always returns the error message as a pointer,
669  * so we must copy that string unconditionally (if non-NULL).
670  */
671  {
672    char buffer[256];
673    char *msg = strerror_r(err, buffer, sizeof(buffer));
674    if(msg)
675      strncpy(buf, msg, max);
676    else
677      snprintf(buf, max, "Unknown error %d", err);
678  }
679#elif defined(HAVE_STRERROR_R) && defined(HAVE_VXWORKS_STRERROR_R)
680 /*
681  * The vxworks-style strerror_r() does use the buffer we pass to the function.
682  * The buffer size should be at least NAME_MAX (256)
683  */
684  {
685    char buffer[256];
686    if(OK == strerror_r(err, buffer))
687      strncpy(buf, buffer, max);
688    else
689      snprintf(buf, max, "Unknown error %d", err);
690  }
691#else
692  {
693    char *msg = strerror(err);
694    if(msg)
695      strncpy(buf, msg, max);
696    else
697      snprintf(buf, max, "Unknown error %d", err);
698  }
699#endif
700
701#endif /* end of ! USE_WINSOCK */
702
703  buf[max] = '\0'; /* make sure the string is zero terminated */
704
705  /* strip trailing '\r\n' or '\n'. */
706  if((p = strrchr(buf,'\n')) != NULL && (p - buf) >= 2)
707     *p = '\0';
708  if((p = strrchr(buf,'\r')) != NULL && (p - buf) >= 1)
709     *p = '\0';
710
711  if(old_errno != ERRNO)
712    SET_ERRNO(old_errno);
713
714  return buf;
715}
716
717#ifdef USE_LIBIDN
718/*
719 * Return error-string for libidn status as returned from idna_to_ascii_lz().
720 */
721const char *Curl_idn_strerror (struct connectdata *conn, int err)
722{
723#ifdef HAVE_IDNA_STRERROR
724  (void)conn;
725  return idna_strerror((Idna_rc) err);
726#else
727  const char *str;
728  char *buf;
729  size_t max;
730
731  DEBUGASSERT(conn);
732
733  buf = conn->syserr_buf;
734  max = sizeof(conn->syserr_buf)-1;
735  *buf = '\0';
736
737#ifndef CURL_DISABLE_VERBOSE_STRINGS
738  switch ((Idna_rc)err) {
739    case IDNA_SUCCESS:
740      str = "No error";
741      break;
742    case IDNA_STRINGPREP_ERROR:
743      str = "Error in string preparation";
744      break;
745    case IDNA_PUNYCODE_ERROR:
746      str = "Error in Punycode operation";
747      break;
748    case IDNA_CONTAINS_NON_LDH:
749      str = "Illegal ASCII characters";
750      break;
751    case IDNA_CONTAINS_MINUS:
752      str = "Contains minus";
753      break;
754    case IDNA_INVALID_LENGTH:
755      str = "Invalid output length";
756      break;
757    case IDNA_NO_ACE_PREFIX:
758      str = "No ACE prefix (\"xn--\")";
759      break;
760    case IDNA_ROUNDTRIP_VERIFY_ERROR:
761      str = "Round trip verify error";
762      break;
763    case IDNA_CONTAINS_ACE_PREFIX:
764      str = "Already have ACE prefix (\"xn--\")";
765      break;
766    case IDNA_ICONV_ERROR:
767      str = "Locale conversion failed";
768      break;
769    case IDNA_MALLOC_ERROR:
770      str = "Allocation failed";
771      break;
772    case IDNA_DLOPEN_ERROR:
773      str = "dlopen() error";
774      break;
775    default:
776      snprintf(buf, max, "error %d", err);
777      str = NULL;
778      break;
779  }
780#else
781  if((Idna_rc)err == IDNA_SUCCESS)
782    str = "No error";
783  else
784    str = "Error";
785#endif
786  if(str)
787    strncpy(buf, str, max);
788  buf[max] = '\0';
789  return (buf);
790#endif
791}
792#endif  /* USE_LIBIDN */
793
794#ifdef USE_WINDOWS_SSPI
795const char *Curl_sspi_strerror (struct connectdata *conn, int err)
796{
797#ifndef CURL_DISABLE_VERBOSE_STRINGS
798  char txtbuf[80];
799  char msgbuf[sizeof(conn->syserr_buf)];
800  char *p, *str, *msg = NULL;
801  bool msg_formatted = FALSE;
802  int old_errno;
803#endif
804  const char *txt;
805  char *outbuf;
806  size_t outmax;
807
808  DEBUGASSERT(conn);
809
810  outbuf = conn->syserr_buf;
811  outmax = sizeof(conn->syserr_buf)-1;
812  *outbuf = '\0';
813
814#ifndef CURL_DISABLE_VERBOSE_STRINGS
815
816  old_errno = ERRNO;
817
818  switch (err) {
819    case SEC_E_OK:
820      txt = "No error";
821      break;
822    case SEC_E_ALGORITHM_MISMATCH:
823      txt = "SEC_E_ALGORITHM_MISMATCH";
824      break;
825    case SEC_E_BAD_BINDINGS:
826      txt = "SEC_E_BAD_BINDINGS";
827      break;
828    case SEC_E_BAD_PKGID:
829      txt = "SEC_E_BAD_PKGID";
830      break;
831    case SEC_E_BUFFER_TOO_SMALL:
832      txt = "SEC_E_BUFFER_TOO_SMALL";
833      break;
834    case SEC_E_CANNOT_INSTALL:
835      txt = "SEC_E_CANNOT_INSTALL";
836      break;
837    case SEC_E_CANNOT_PACK:
838      txt = "SEC_E_CANNOT_PACK";
839      break;
840    case SEC_E_CERT_EXPIRED:
841      txt = "SEC_E_CERT_EXPIRED";
842      break;
843    case SEC_E_CERT_UNKNOWN:
844      txt = "SEC_E_CERT_UNKNOWN";
845      break;
846    case SEC_E_CERT_WRONG_USAGE:
847      txt = "SEC_E_CERT_WRONG_USAGE";
848      break;
849    case SEC_E_CONTEXT_EXPIRED:
850      txt = "SEC_E_CONTEXT_EXPIRED";
851      break;
852    case SEC_E_CROSSREALM_DELEGATION_FAILURE:
853      txt = "SEC_E_CROSSREALM_DELEGATION_FAILURE";
854      break;
855    case SEC_E_CRYPTO_SYSTEM_INVALID:
856      txt = "SEC_E_CRYPTO_SYSTEM_INVALID";
857      break;
858    case SEC_E_DECRYPT_FAILURE:
859      txt = "SEC_E_DECRYPT_FAILURE";
860      break;
861    case SEC_E_DELEGATION_POLICY:
862      txt = "SEC_E_DELEGATION_POLICY";
863      break;
864    case SEC_E_DELEGATION_REQUIRED:
865      txt = "SEC_E_DELEGATION_REQUIRED";
866      break;
867    case SEC_E_DOWNGRADE_DETECTED:
868      txt = "SEC_E_DOWNGRADE_DETECTED";
869      break;
870    case SEC_E_ENCRYPT_FAILURE:
871      txt = "SEC_E_ENCRYPT_FAILURE";
872      break;
873    case SEC_E_ILLEGAL_MESSAGE:
874      txt = "SEC_E_ILLEGAL_MESSAGE";
875      break;
876    case SEC_E_INCOMPLETE_CREDENTIALS:
877      txt = "SEC_E_INCOMPLETE_CREDENTIALS";
878      break;
879    case SEC_E_INCOMPLETE_MESSAGE:
880      txt = "SEC_E_INCOMPLETE_MESSAGE";
881      break;
882    case SEC_E_INSUFFICIENT_MEMORY:
883      txt = "SEC_E_INSUFFICIENT_MEMORY";
884      break;
885    case SEC_E_INTERNAL_ERROR:
886      txt = "SEC_E_INTERNAL_ERROR";
887      break;
888    case SEC_E_INVALID_HANDLE:
889      txt = "SEC_E_INVALID_HANDLE";
890      break;
891    case SEC_E_INVALID_PARAMETER:
892      txt = "SEC_E_INVALID_PARAMETER";
893      break;
894    case SEC_E_INVALID_TOKEN:
895      txt = "SEC_E_INVALID_TOKEN";
896      break;
897    case SEC_E_ISSUING_CA_UNTRUSTED:
898      txt = "SEC_E_ISSUING_CA_UNTRUSTED";
899      break;
900    case SEC_E_ISSUING_CA_UNTRUSTED_KDC:
901      txt = "SEC_E_ISSUING_CA_UNTRUSTED_KDC";
902      break;
903    case SEC_E_KDC_CERT_EXPIRED:
904      txt = "SEC_E_KDC_CERT_EXPIRED";
905      break;
906    case SEC_E_KDC_CERT_REVOKED:
907      txt = "SEC_E_KDC_CERT_REVOKED";
908      break;
909    case SEC_E_KDC_INVALID_REQUEST:
910      txt = "SEC_E_KDC_INVALID_REQUEST";
911      break;
912    case SEC_E_KDC_UNABLE_TO_REFER:
913      txt = "SEC_E_KDC_UNABLE_TO_REFER";
914      break;
915    case SEC_E_KDC_UNKNOWN_ETYPE:
916      txt = "SEC_E_KDC_UNKNOWN_ETYPE";
917      break;
918    case SEC_E_LOGON_DENIED:
919      txt = "SEC_E_LOGON_DENIED";
920      break;
921    case SEC_E_MAX_REFERRALS_EXCEEDED:
922      txt = "SEC_E_MAX_REFERRALS_EXCEEDED";
923      break;
924    case SEC_E_MESSAGE_ALTERED:
925      txt = "SEC_E_MESSAGE_ALTERED";
926      break;
927    case SEC_E_MULTIPLE_ACCOUNTS:
928      txt = "SEC_E_MULTIPLE_ACCOUNTS";
929      break;
930    case SEC_E_MUST_BE_KDC:
931      txt = "SEC_E_MUST_BE_KDC";
932      break;
933    case SEC_E_NOT_OWNER:
934      txt = "SEC_E_NOT_OWNER";
935      break;
936    case SEC_E_NO_AUTHENTICATING_AUTHORITY:
937      txt = "SEC_E_NO_AUTHENTICATING_AUTHORITY";
938      break;
939    case SEC_E_NO_CREDENTIALS:
940      txt = "SEC_E_NO_CREDENTIALS";
941      break;
942    case SEC_E_NO_IMPERSONATION:
943      txt = "SEC_E_NO_IMPERSONATION";
944      break;
945    case SEC_E_NO_IP_ADDRESSES:
946      txt = "SEC_E_NO_IP_ADDRESSES";
947      break;
948    case SEC_E_NO_KERB_KEY:
949      txt = "SEC_E_NO_KERB_KEY";
950      break;
951    case SEC_E_NO_PA_DATA:
952      txt = "SEC_E_NO_PA_DATA";
953      break;
954    case SEC_E_NO_S4U_PROT_SUPPORT:
955      txt = "SEC_E_NO_S4U_PROT_SUPPORT";
956      break;
957    case SEC_E_NO_TGT_REPLY:
958      txt = "SEC_E_NO_TGT_REPLY";
959      break;
960    case SEC_E_OUT_OF_SEQUENCE:
961      txt = "SEC_E_OUT_OF_SEQUENCE";
962      break;
963    case SEC_E_PKINIT_CLIENT_FAILURE:
964      txt = "SEC_E_PKINIT_CLIENT_FAILURE";
965      break;
966    case SEC_E_PKINIT_NAME_MISMATCH:
967      txt = "SEC_E_PKINIT_NAME_MISMATCH";
968      break;
969    case SEC_E_POLICY_NLTM_ONLY:
970      txt = "SEC_E_POLICY_NLTM_ONLY";
971      break;
972    case SEC_E_QOP_NOT_SUPPORTED:
973      txt = "SEC_E_QOP_NOT_SUPPORTED";
974      break;
975    case SEC_E_REVOCATION_OFFLINE_C:
976      txt = "SEC_E_REVOCATION_OFFLINE_C";
977      break;
978    case SEC_E_REVOCATION_OFFLINE_KDC:
979      txt = "SEC_E_REVOCATION_OFFLINE_KDC";
980      break;
981    case SEC_E_SECPKG_NOT_FOUND:
982      txt = "SEC_E_SECPKG_NOT_FOUND";
983      break;
984    case SEC_E_SECURITY_QOS_FAILED:
985      txt = "SEC_E_SECURITY_QOS_FAILED";
986      break;
987    case SEC_E_SHUTDOWN_IN_PROGRESS:
988      txt = "SEC_E_SHUTDOWN_IN_PROGRESS";
989      break;
990    case SEC_E_SMARTCARD_CERT_EXPIRED:
991      txt = "SEC_E_SMARTCARD_CERT_EXPIRED";
992      break;
993    case SEC_E_SMARTCARD_CERT_REVOKED:
994      txt = "SEC_E_SMARTCARD_CERT_REVOKED";
995      break;
996    case SEC_E_SMARTCARD_LOGON_REQUIRED:
997      txt = "SEC_E_SMARTCARD_LOGON_REQUIRED";
998      break;
999    case SEC_E_STRONG_CRYPTO_NOT_SUPPORTED:
1000      txt = "SEC_E_STRONG_CRYPTO_NOT_SUPPORTED";
1001      break;
1002    case SEC_E_TARGET_UNKNOWN:
1003      txt = "SEC_E_TARGET_UNKNOWN";
1004      break;
1005    case SEC_E_TIME_SKEW:
1006      txt = "SEC_E_TIME_SKEW";
1007      break;
1008    case SEC_E_TOO_MANY_PRINCIPALS:
1009      txt = "SEC_E_TOO_MANY_PRINCIPALS";
1010      break;
1011    case SEC_E_UNFINISHED_CONTEXT_DELETED:
1012      txt = "SEC_E_UNFINISHED_CONTEXT_DELETED";
1013      break;
1014    case SEC_E_UNKNOWN_CREDENTIALS:
1015      txt = "SEC_E_UNKNOWN_CREDENTIALS";
1016      break;
1017    case SEC_E_UNSUPPORTED_FUNCTION:
1018      txt = "SEC_E_UNSUPPORTED_FUNCTION";
1019      break;
1020    case SEC_E_UNSUPPORTED_PREAUTH:
1021      txt = "SEC_E_UNSUPPORTED_PREAUTH";
1022      break;
1023    case SEC_E_UNTRUSTED_ROOT:
1024      txt = "SEC_E_UNTRUSTED_ROOT";
1025      break;
1026    case SEC_E_WRONG_CREDENTIAL_HANDLE:
1027      txt = "SEC_E_WRONG_CREDENTIAL_HANDLE";
1028      break;
1029    case SEC_E_WRONG_PRINCIPAL:
1030      txt = "SEC_E_WRONG_PRINCIPAL";
1031      break;
1032    case SEC_I_COMPLETE_AND_CONTINUE:
1033      txt = "SEC_I_COMPLETE_AND_CONTINUE";
1034      break;
1035    case SEC_I_COMPLETE_NEEDED:
1036      txt = "SEC_I_COMPLETE_NEEDED";
1037      break;
1038    case SEC_I_CONTEXT_EXPIRED:
1039      txt = "SEC_I_CONTEXT_EXPIRED";
1040      break;
1041    case SEC_I_CONTINUE_NEEDED:
1042      txt = "SEC_I_CONTINUE_NEEDED";
1043      break;
1044    case SEC_I_INCOMPLETE_CREDENTIALS:
1045      txt = "SEC_I_INCOMPLETE_CREDENTIALS";
1046      break;
1047    case SEC_I_LOCAL_LOGON:
1048      txt = "SEC_I_LOCAL_LOGON";
1049      break;
1050    case SEC_I_NO_LSA_CONTEXT:
1051      txt = "SEC_I_NO_LSA_CONTEXT";
1052      break;
1053    case SEC_I_RENEGOTIATE:
1054      txt = "SEC_I_RENEGOTIATE";
1055      break;
1056    case SEC_I_SIGNATURE_NEEDED:
1057      txt = "SEC_I_SIGNATURE_NEEDED";
1058      break;
1059    default:
1060      txt = "Unknown error";
1061  }
1062
1063  if(err == SEC_E_OK)
1064    strncpy(outbuf, txt, outmax);
1065  else {
1066    str = txtbuf;
1067    snprintf(txtbuf, sizeof(txtbuf), "%s (0x%04X%04X)",
1068             txt, (err >> 16) & 0xffff, err & 0xffff);
1069    txtbuf[sizeof(txtbuf)-1] = '\0';
1070
1071#ifdef _WIN32_WCE
1072    {
1073      wchar_t wbuf[256];
1074      wbuf[0] = L'\0';
1075
1076      if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
1077                       FORMAT_MESSAGE_IGNORE_INSERTS,
1078                       NULL, err, LANG_NEUTRAL,
1079                       wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL)) {
1080        wcstombs(msgbuf,wbuf,sizeof(msgbuf)-1);
1081        msg_formatted = TRUE;
1082      }
1083    }
1084#else
1085    if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
1086                      FORMAT_MESSAGE_IGNORE_INSERTS,
1087                      NULL, err, LANG_NEUTRAL,
1088                      msgbuf, sizeof(msgbuf)-1, NULL)) {
1089      msg_formatted = TRUE;
1090    }
1091#endif
1092    if(msg_formatted) {
1093      msgbuf[sizeof(msgbuf)-1] = '\0';
1094      /* strip trailing '\r\n' or '\n' */
1095      if((p = strrchr(msgbuf,'\n')) != NULL && (p - msgbuf) >= 2)
1096         *p = '\0';
1097      if((p = strrchr(msgbuf,'\r')) != NULL && (p - msgbuf) >= 1)
1098         *p = '\0';
1099      msg = msgbuf;
1100    }
1101    if(msg)
1102      snprintf(outbuf, outmax, "%s - %s", str, msg);
1103    else
1104      strncpy(outbuf, str, outmax);
1105  }
1106
1107  if(old_errno != ERRNO)
1108    SET_ERRNO(old_errno);
1109
1110#else
1111
1112  if(err == SEC_E_OK)
1113    txt = "No error";
1114  else
1115    txt = "Error";
1116
1117  strncpy(outbuf, txt, outmax);
1118
1119#endif
1120
1121  outbuf[outmax] = '\0';
1122
1123  return outbuf;
1124}
1125#endif /* USE_WINDOWS_SSPI */
1126