Fix a potential buffer overflow bug in lib/version.c
[platform/upstream/curl.git] / lib / version.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2006, 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  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <curl/curl.h>
30 #include "urldata.h"
31 #include "sslgen.h"
32
33 #define _MPRINTF_REPLACE /* use the internal *printf() functions */
34 #include <curl/mprintf.h>
35
36 #ifdef USE_ARES
37 #include <ares_version.h>
38 #endif
39
40 #ifdef USE_LIBIDN
41 #include <stringprep.h>
42 #endif
43
44 #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
45 #include <iconv.h>
46 #endif
47
48 #ifdef USE_LIBSSH2
49 #include <libssh2.h>
50 #endif
51
52
53 char *curl_version(void)
54 {
55   static char version[200];
56   char *ptr=version;
57   size_t len;
58   size_t left = sizeof(version);
59   strcpy(ptr, LIBCURL_NAME "/" LIBCURL_VERSION );
60   len = strlen(ptr);
61   left -= len;
62   ptr += len;
63
64   len = Curl_ssl_version(ptr, left);
65   left -= len;
66   ptr += len;
67
68 #ifdef HAVE_LIBZ
69   len = snprintf(ptr, left, " zlib/%s", zlibVersion());
70   left -= len;
71   ptr += len;
72 #endif
73 #ifdef USE_ARES
74   /* this function is only present in c-ares, not in the original ares */
75   len = snprintf(ptr, left, " c-ares/%s", ares_version(NULL));
76   left -= len;
77   ptr += len;
78 #endif
79 #ifdef USE_LIBIDN
80   if(stringprep_check_version(LIBIDN_REQUIRED_VERSION)) {
81     len = snprintf(ptr, left, " libidn/%s", stringprep_check_version(NULL));
82     left -= len;
83     ptr += len;
84   }
85 #endif
86 #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
87 #ifdef _LIBICONV_VERSION
88   len = snprintf(ptr, left, " iconv/%d.%d",
89                  _LIBICONV_VERSION >> 8, _LIBICONV_VERSION & 255);
90 #else
91   /* version unknown */
92   len = snprintf(ptr, left, " iconv");
93 #endif /* _LIBICONV_VERSION */
94   left -= len;
95   ptr += len;
96 #endif
97 #ifdef USE_LIBSSH2
98   len = snprintf(ptr, left, " libssh2/%s", LIBSSH2_VERSION);
99   left -= len;
100   ptr += len;
101 #endif
102
103   return version;
104 }
105
106 /* data for curl_version_info */
107
108 static const char * const protocols[] = {
109 #ifndef CURL_DISABLE_TFTP
110   "tftp",
111 #endif
112 #ifndef CURL_DISABLE_FTP
113   "ftp",
114 #endif
115 #ifndef CURL_DISABLE_TELNET
116   "telnet",
117 #endif
118 #ifndef CURL_DISABLE_DICT
119   "dict",
120 #endif
121 #ifndef CURL_DISABLE_LDAP
122   "ldap",
123 #endif
124 #ifndef CURL_DISABLE_HTTP
125   "http",
126 #endif
127 #ifndef CURL_DISABLE_FILE
128   "file",
129 #endif
130
131 #ifdef USE_SSL
132 #ifndef CURL_DISABLE_HTTP
133   "https",
134 #endif
135 #ifndef CURL_DISABLE_FTP
136   "ftps",
137 #endif
138 #endif
139
140 #ifdef USE_LIBSSH2
141   "scp",
142   "sftp",
143 #endif
144
145   NULL
146 };
147
148 static curl_version_info_data version_info = {
149   CURLVERSION_NOW,
150   LIBCURL_VERSION,
151   LIBCURL_VERSION_NUM,
152   OS, /* as found by configure or set by hand at build-time */
153   0 /* features is 0 by default */
154 #ifdef ENABLE_IPV6
155   | CURL_VERSION_IPV6
156 #endif
157 #ifdef HAVE_KRB4
158   | CURL_VERSION_KERBEROS4
159 #endif
160 #ifdef USE_SSL
161   | CURL_VERSION_SSL
162 #endif
163 #ifdef USE_NTLM
164   | CURL_VERSION_NTLM
165 #endif
166 #ifdef USE_WINDOWS_SSPI
167   | CURL_VERSION_SSPI
168 #endif
169 #ifdef HAVE_LIBZ
170   | CURL_VERSION_LIBZ
171 #endif
172 #ifdef HAVE_GSSAPI
173   | CURL_VERSION_GSSNEGOTIATE
174 #endif
175 #ifdef CURLDEBUG
176   | CURL_VERSION_DEBUG
177 #endif
178 #ifdef USE_ARES
179   | CURL_VERSION_ASYNCHDNS
180 #endif
181 #ifdef HAVE_SPNEGO
182   | CURL_VERSION_SPNEGO
183 #endif
184 #if defined(ENABLE_64BIT) && (SIZEOF_CURL_OFF_T > 4)
185   | CURL_VERSION_LARGEFILE
186 #endif
187 #if defined(CURL_DOES_CONVERSIONS)
188   | CURL_VERSION_CONV
189 #endif
190   ,
191   NULL, /* ssl_version */
192   0,    /* ssl_version_num, this is kept at zero */
193   NULL, /* zlib_version */
194   protocols,
195   NULL, /* c-ares version */
196   0,    /* c-ares version numerical */
197   NULL, /* libidn version */
198   0,    /* iconv version */
199   NULL, /* ssh lib version */
200 };
201
202 curl_version_info_data *curl_version_info(CURLversion stamp)
203 {
204 #ifdef USE_LIBSSH2
205   static char ssh_buffer[80];
206 #endif
207
208 #ifdef USE_SSL
209   static char ssl_buffer[80];
210   Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
211   version_info.ssl_version = ssl_buffer;
212 #endif
213
214 #ifdef HAVE_LIBZ
215   version_info.libz_version = zlibVersion();
216   /* libz left NULL if non-existing */
217 #endif
218 #ifdef USE_ARES
219   {
220     int aresnum;
221     version_info.ares = ares_version(&aresnum);
222     version_info.ares_num = aresnum;
223   }
224 #endif
225 #ifdef USE_LIBIDN
226   /* This returns a version string if we use the given version or later,
227      otherwise it returns NULL */
228   version_info.libidn = stringprep_check_version(LIBIDN_REQUIRED_VERSION);
229   if(version_info.libidn)
230     version_info.features |= CURL_VERSION_IDN;
231 #endif
232
233 #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
234 #ifdef _LIBICONV_VERSION
235   version_info.iconv_ver_num = _LIBICONV_VERSION;
236 #else
237   /* version unknown */
238   version_info.iconv_ver_num = -1;
239 #endif /* _LIBICONV_VERSION */
240 #endif
241
242 #ifdef USE_LIBSSH2
243   snprintf(ssh_buffer, sizeof(ssh_buffer), "libssh2/%s", LIBSSH2_VERSION);
244   version_info.libssh_version = ssh_buffer;
245 #endif
246
247   (void)stamp; /* avoid compiler warnings, we don't use this */
248
249   return &version_info;
250 }