Add editor-modelines; adjust whitespace.
[metze/wireshark/wip.git] / wsutil / os_version_info.c
1 /* os_version_info.c
2  * Routines to report operating system version information
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26 #include <errno.h>
27
28 #ifdef HAVE_SYS_UTSNAME_H
29 #include <sys/utsname.h>
30 #endif
31
32 #ifdef HAVE_OS_X_FRAMEWORKS
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <wsutil/cfutils.h>
35 #endif
36
37 #include <glib.h>
38
39 #include <wsutil/unicode-utils.h>
40
41 #include <wsutil/os_version_info.h>
42
43 #ifdef _WIN32
44 typedef void (WINAPI *nativesi_func_ptr)(LPSYSTEM_INFO);
45 #endif
46
47 /*
48  * Handles the rather elaborate process of getting OS version information
49  * from OS X (we want the OS X version, not the Darwin version, the latter
50  * being easy to get with uname()).
51  */
52 #ifdef HAVE_OS_X_FRAMEWORKS
53
54 /*
55  * Fetch a string, as a UTF-8 C string, from a dictionary, given a key.
56  */
57 static char *
58 get_string_from_dictionary(CFPropertyListRef dict, CFStringRef key)
59 {
60         CFStringRef cfstring;
61
62         cfstring = (CFStringRef)CFDictionaryGetValue((CFDictionaryRef)dict,
63             (const void *)key);
64         if (cfstring == NULL)
65                 return NULL;
66         if (CFGetTypeID(cfstring) != CFStringGetTypeID()) {
67                 /* It isn't a string.  Punt. */
68                 return NULL;
69         }
70         return CFString_to_C_string(cfstring);
71 }
72
73 /*
74  * Get the OS X version information, and append it to the GString.
75  * Return TRUE if we succeed, FALSE if we fail.
76  */
77 static gboolean
78 get_os_x_version_info(GString *str)
79 {
80         static const UInt8 server_version_plist_path[] =
81             "/System/Library/CoreServices/ServerVersion.plist";
82         static const UInt8 system_version_plist_path[] =
83             "/System/Library/CoreServices/SystemVersion.plist";
84         CFURLRef version_plist_file_url;
85         CFReadStreamRef version_plist_stream;
86         CFDictionaryRef version_dict;
87         char *string;
88
89         /*
90          * On OS X, report the OS X version number as the OS, and put
91          * the Darwin information in parentheses.
92          *
93          * Alas, Gestalt() is deprecated in Mountain Lion, so the build
94          * fails if you treat deprecation warnings as fatal.  I don't
95          * know of any replacement API, so we fall back on reading
96          * /System/Library/CoreServices/ServerVersion.plist if it
97          * exists, otherwise /System/Library/CoreServices/SystemVersion.plist,
98          * and using ProductUserVisibleVersion.  We also get the build
99          * version from ProductBuildVersion and the product name from
100          * ProductName.
101          */
102         version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
103             server_version_plist_path, sizeof server_version_plist_path - 1,
104             false);
105         if (version_plist_file_url == NULL)
106                 return FALSE;
107         version_plist_stream = CFReadStreamCreateWithFile(NULL,
108             version_plist_file_url);
109         CFRelease(version_plist_file_url);
110         if (version_plist_stream == NULL)
111                 return FALSE;
112         if (!CFReadStreamOpen(version_plist_stream)) {
113                 CFRelease(version_plist_stream);
114
115                 /*
116                  * Try SystemVersion.plist.
117                  */
118                 version_plist_file_url = CFURLCreateFromFileSystemRepresentation(NULL,
119                     system_version_plist_path, sizeof system_version_plist_path - 1,
120                     false);
121                 if (version_plist_file_url == NULL)
122                         return FALSE;
123                 version_plist_stream = CFReadStreamCreateWithFile(NULL,
124                     version_plist_file_url);
125                 CFRelease(version_plist_file_url);
126                 if (version_plist_stream == NULL)
127                         return FALSE;
128                 if (!CFReadStreamOpen(version_plist_stream)) {
129                         CFRelease(version_plist_stream);
130                         return FALSE;
131                 }
132         }
133 #ifdef HAVE_CFPROPERTYLISTCREATEWITHSTREAM
134         version_dict = (CFDictionaryRef)CFPropertyListCreateWithStream(NULL,
135             version_plist_stream, 0, kCFPropertyListImmutable,
136             NULL, NULL);
137 #else
138         version_dict = (CFDictionaryRef)CFPropertyListCreateFromStream(NULL,
139             version_plist_stream, 0, kCFPropertyListImmutable,
140             NULL, NULL);
141 #endif
142         if (version_dict == NULL) {
143                 CFRelease(version_plist_stream);
144                 return FALSE;
145         }
146         if (CFGetTypeID(version_dict) != CFDictionaryGetTypeID()) {
147                 /* This is *supposed* to be a dictionary.  Punt. */
148                 CFRelease(version_dict);
149                 CFReadStreamClose(version_plist_stream);
150                 CFRelease(version_plist_stream);
151                 return FALSE;
152         }
153         /* Get the product name string. */
154         string = get_string_from_dictionary(version_dict,
155             CFSTR("ProductName"));
156         if (string == NULL) {
157                 CFRelease(version_dict);
158                 CFReadStreamClose(version_plist_stream);
159                 CFRelease(version_plist_stream);
160                 return FALSE;
161         }
162         g_string_append_printf(str, "%s", string);
163         g_free(string);
164
165         /* Get the OS version string. */
166         string = get_string_from_dictionary(version_dict,
167             CFSTR("ProductUserVisibleVersion"));
168         if (string == NULL) {
169                 CFRelease(version_dict);
170                 CFReadStreamClose(version_plist_stream);
171                 CFRelease(version_plist_stream);
172                 return FALSE;
173         }
174         g_string_append_printf(str, " %s", string);
175         g_free(string);
176
177         /* Get the build string */
178         string = get_string_from_dictionary(version_dict,
179             CFSTR("ProductBuildVersion"));
180         if (string == NULL) {
181                 CFRelease(version_dict);
182                 CFReadStreamClose(version_plist_stream);
183                 CFRelease(version_plist_stream);
184                 return FALSE;
185         }
186         g_string_append_printf(str, ", build %s", string);
187         g_free(string);
188         CFRelease(version_dict);
189         CFReadStreamClose(version_plist_stream);
190         CFRelease(version_plist_stream);
191         return TRUE;
192 }
193 #endif
194
195 /*
196  * Get the OS version, and append it to the GString
197  */
198 void
199 get_os_version_info(GString *str)
200 {
201 #if defined(_WIN32)
202         OSVERSIONINFOEX info;
203         SYSTEM_INFO system_info;
204         nativesi_func_ptr nativesi_func;
205 #elif defined(HAVE_SYS_UTSNAME_H)
206         struct utsname name;
207 #endif
208
209 #if defined(_WIN32)
210         /*
211          * See
212          *
213          *      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
214          *
215          * for more than you ever wanted to know about determining the
216          * flavor of Windows on which you're running.  Implementing more
217          * of that is left as an exercise to the reader - who should
218          * check any copyright information about code samples on MSDN
219          * before cutting and pasting into Wireshark.
220          *
221          * They should also note that you need an OSVERSIONINFOEX structure
222          * to get some of that information, and that not only is that
223          * structure not supported on older versions of Windows, you might
224          * not even be able to compile code that *uses* that structure with
225          * older versions of the SDK.
226          */
227
228         memset(&info, '\0', sizeof info);
229         info.dwOSVersionInfoSize = sizeof info;
230         if (!GetVersionEx((OSVERSIONINFO *)&info)) {
231                 /*
232                  * XXX - get the failure reason.
233                  */
234                 g_string_append(str, "unknown Windows version");
235                 return;
236         }
237
238         memset(&system_info, '\0', sizeof system_info);
239         /* Look for and use the GetNativeSystemInfo() function if available to get the correct processor
240          * architecture even when running 32-bit Wireshark in WOW64 (x86 emulation on 64-bit Windows) */
241         nativesi_func = (nativesi_func_ptr)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetNativeSystemInfo");
242         if(nativesi_func)
243                 nativesi_func(&system_info);
244         else
245                 GetSystemInfo(&system_info);
246
247         switch (info.dwPlatformId) {
248
249         case VER_PLATFORM_WIN32s:
250                 /* Shyeah, right. */
251                 g_string_append_printf(str, "Windows 3.1 with Win32s");
252                 break;
253
254         case VER_PLATFORM_WIN32_WINDOWS:
255                 /* Windows OT */
256                 switch (info.dwMajorVersion) {
257
258                 case 4:
259                         /* 3 cheers for Microsoft marketing! */
260                         switch (info.dwMinorVersion) {
261
262                         case 0:
263                                 g_string_append_printf(str, "Windows 95");
264                                 break;
265
266                         case 10:
267                                 g_string_append_printf(str, "Windows 98");
268                                 break;
269
270                         case 90:
271                                 g_string_append_printf(str, "Windows Me");
272                                 break;
273
274                         default:
275                                 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
276                                     info.dwMajorVersion, info.dwMinorVersion);
277                                 break;
278                         }
279                         break;
280
281                 default:
282                         g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
283                             info.dwMajorVersion, info.dwMinorVersion);
284                         break;
285                 }
286                 break;
287
288         case VER_PLATFORM_WIN32_NT:
289                 /* Windows NT */
290                 switch (info.dwMajorVersion) {
291
292                 case 3:
293                 case 4:
294                         g_string_append_printf(str, "Windows NT %lu.%lu",
295                             info.dwMajorVersion, info.dwMinorVersion);
296                         break;
297
298                 case 5:
299                         /* 3 cheers for Microsoft marketing! */
300                         switch (info.dwMinorVersion) {
301
302                         case 0:
303                                 g_string_append_printf(str, "Windows 2000");
304                                 break;
305
306                         case 1:
307                                 g_string_append_printf(str, "Windows XP");
308                                 break;
309
310                         case 2:
311                                 if ((info.wProductType == VER_NT_WORKSTATION) &&
312                                     (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
313                                         g_string_append_printf(str, "Windows XP Professional x64 Edition");
314                                 } else {
315                                         g_string_append_printf(str, "Windows Server 2003");
316                                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
317                                                 g_string_append_printf(str, " x64 Edition");
318                                 }
319                                 break;
320
321                         default:
322                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
323                                                        info.dwMajorVersion, info.dwMinorVersion);
324                                 break;
325                         }
326                         break;
327
328                 case 6: {
329                         gboolean is_nt_workstation;
330
331                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
332                                 g_string_append(str, "64-bit ");
333                         else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
334                                 g_string_append(str, "32-bit ");
335 #ifndef VER_NT_WORKSTATION
336 #define VER_NT_WORKSTATION 0x01
337                         is_nt_workstation = ((info.wReserved[1] & 0xff) == VER_NT_WORKSTATION);
338 #else
339                         is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
340 #endif
341                         switch (info.dwMinorVersion) {
342                         case 0:
343                                 g_string_append_printf(str, is_nt_workstation ? "Windows Vista" : "Windows Server 2008");
344                                 break;
345                         case 1:
346                                 g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
347                                 break;
348                         case 2:
349                                 g_string_append_printf(str, is_nt_workstation ? "Windows 8" : "Windows Server 2012");
350                                 break;
351                         case 3:
352                                 g_string_append_printf(str, is_nt_workstation ? "Windows 8.1" : "Windows Server 2012 R2");
353                                 break;
354                         default:
355                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
356                                                        info.dwMajorVersion, info.dwMinorVersion);
357                                 break;
358                         }
359                         break;
360                 }  /* case 6 */
361                 default:
362                         g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
363                             info.dwMajorVersion, info.dwMinorVersion);
364                         break;
365                 } /* info.dwMajorVersion */
366                 break;
367
368         default:
369                 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
370                     info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
371                 break;
372         }
373         if (info.szCSDVersion[0] != '\0')
374                 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
375         g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
376 #elif defined(HAVE_SYS_UTSNAME_H)
377         /*
378          * We have <sys/utsname.h>, so we assume we have "uname()".
379          */
380         if (uname(&name) < 0) {
381                 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
382                     g_strerror(errno));
383                 return;
384         }
385
386         if (strcmp(name.sysname, "AIX") == 0) {
387                 /*
388                  * Yay, IBM!  Thanks for doing something different
389                  * from most of the other UNIXes out there, and
390                  * making "name.version" apparently be the major
391                  * version number and "name.release" be the minor
392                  * version number.
393                  */
394                 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
395                     name.release);
396         } else {
397                 /*
398                  * XXX - get "version" on any other platforms?
399                  *
400                  * On Digital/Tru64 UNIX, it's something unknown.
401                  * On Solaris, it's some kind of build information.
402                  * On HP-UX, it appears to be some sort of subrevision
403                  * thing.
404                  * On *BSD and Darwin/OS X, it's a long string giving
405                  * a build date, config file name, etc., etc., etc..
406                  */
407 #ifdef HAVE_OS_X_FRAMEWORKS
408                 /*
409                  * On Mac OS X, report the Mac OS X version number as
410                  * the OS version if we can, and put the Darwin information
411                  * in parentheses.
412                  */
413                 if (get_os_x_version_info(str)) {
414                         /* Success - append the Darwin information. */
415                         g_string_append_printf(str, " (%s %s)", name.sysname, name.release);
416                 } else {
417                         /* Failure - just use the Darwin information. */
418                         g_string_append_printf(str, "%s %s", name.sysname, name.release);
419                 }
420 #else /* HAVE_OS_X_FRAMEWORKS */
421                 /*
422                  * XXX - on Linux, are there any APIs to get the distribution
423                  * name and version number?  I think some distributions have
424                  * that.
425                  *
426                  * At least on Linux Standard Base-compliant distributions,
427                  * there's an "lsb_release" command.  However:
428                  *
429                  *      http://forums.fedoraforum.org/showthread.php?t=220885
430                  *
431                  * seems to suggest that if you don't have the redhat-lsb
432                  * package installed, you don't have lsb_release, and that
433                  * /etc/fedora-release has the release information on
434                  * Fedora.
435                  *
436                  *      http://linux.die.net/man/1/lsb_release
437                  *
438                  * suggests that there's an /etc/distrib-release file, but
439                  * it doesn't indicate whether "distrib" is literally
440                  * "distrib" or is the name for the distribution, and
441                  * also speaks of an /etc/debian_version file.
442                  *
443                  * "lsb_release" apparently parses /etc/lsb-release, which
444                  * has shell-style assignments, assigning to, among other
445                  * values, DISTRIB_ID (distributor/distribution name),
446                  * DISTRIB_RELEASE (release number of the distribution),
447                  * DISTRIB_DESCRIPTION (*might* be name followed by version,
448                  * but the manpage for lsb_release seems to indicate that's
449                  * not guaranteed), and DISTRIB_CODENAME (code name, e.g.
450                  * "licentious" for the Ubuntu Licentious Lemur release).
451                  * the lsb_release man page also speaks of the distrib-release
452                  * file, but Debian doesn't have one, and Ubuntu 7's
453                  * lsb_release command doesn't look for one.
454                  *
455                  * I've seen references to /etc/redhat-release as well.
456                  *
457                  * At least on my Ubuntu 7 system, /etc/debian_version
458                  * doesn't contain anything interesting (just some Debian
459                  * codenames).
460                  *
461                  * See also
462                  *
463                  *      http://bugs.python.org/issue1322
464                  *
465                  *      http://www.novell.com/coolsolutions/feature/11251.html
466                  *
467                  *      http://linuxmafia.com/faq/Admin/release-files.html
468                  *
469                  * and the Lib/Platform.py file in recent Python 2.x
470                  * releases.
471                  */
472                 g_string_append_printf(str, "%s %s", name.sysname, name.release);
473 #endif /* HAVE_OS_X_FRAMEWORKS */
474         }
475 #else
476         g_string_append(str, "an unknown OS");
477 #endif
478 }
479
480 #if defined(_WIN32)
481 /*
482  * Get the Windows major OS version.
483  *
484  * XXX - Should this return the minor version as well, e.g. 0x00050002?
485  *
486  * XXX - I think Microsoft have now stuck it at 6 forever, so it really
487  * distinguishes, on the versions of Windows we currently support and
488  * future Windows versions between Windows 2000/XP (major version 5) and
489  * everything after it (major version 6).
490  */
491 guint32
492 get_windows_major_version(void)
493 {
494         OSVERSIONINFO info;
495
496         info.dwOSVersionInfoSize = sizeof info;
497         if (GetVersionEx(&info)) {
498                 return info.dwMajorVersion;
499         }
500         return 0;
501 }
502 #endif
503
504 /*
505  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
506  *
507  * Local variables:
508  * c-basic-offset: 8
509  * tab-width: 8
510  * indent-tabs-mode: t
511  * End:
512  *
513  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
514  * :indentSize=8:tabSize=8:noTabs=false:
515  */