A bunch of "{Mac} OS X" -> "macOS" changes.
[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 macOS (we want the macOS 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 macOS 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 macOS, report the macOS 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         HMODULE kernel_dll_handle;
203         OSVERSIONINFOEX info;
204         SYSTEM_INFO system_info;
205         nativesi_func_ptr nativesi_func;
206 #elif defined(HAVE_SYS_UTSNAME_H)
207         struct utsname name;
208 #endif
209
210 #if defined(_WIN32)
211         /*
212          * See
213          *
214          *      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp
215          *
216          * for more than you ever wanted to know about determining the
217          * flavor of Windows on which you're running.  Implementing more
218          * of that is left as an exercise to the reader - who should
219          * check any copyright information about code samples on MSDN
220          * before cutting and pasting into Wireshark.
221          *
222          * They should also note that you need an OSVERSIONINFOEX structure
223          * to get some of that information, and that not only is that
224          * structure not supported on older versions of Windows, you might
225          * not even be able to compile code that *uses* that structure with
226          * older versions of the SDK.
227          */
228
229         memset(&info, '\0', sizeof info);
230         info.dwOSVersionInfoSize = sizeof info;
231         if (!GetVersionEx((OSVERSIONINFO *)&info)) {
232                 /*
233                  * XXX - get the failure reason.
234                  */
235                 g_string_append(str, "unknown Windows version");
236                 return;
237         }
238
239         memset(&system_info, '\0', sizeof system_info);
240         /* Look for and use the GetNativeSystemInfo() function if available to get the correct processor
241          * architecture even when running 32-bit Wireshark in WOW64 (x86 emulation on 64-bit Windows) */
242         kernel_dll_handle = GetModuleHandle(_T("kernel32.dll"));
243         if (kernel_dll_handle == NULL) {
244                 /*
245                  * XXX - get the failure reason.
246                  */
247                 g_string_append(str, "unknown Windows version");
248                 return;
249         }
250         nativesi_func = (nativesi_func_ptr)GetProcAddress(kernel_dll_handle, "GetNativeSystemInfo");
251         if(nativesi_func)
252                 nativesi_func(&system_info);
253         else
254                 GetSystemInfo(&system_info);
255
256         switch (info.dwPlatformId) {
257
258         case VER_PLATFORM_WIN32s:
259                 /* Shyeah, right. */
260                 g_string_append_printf(str, "Windows 3.1 with Win32s");
261                 break;
262
263         case VER_PLATFORM_WIN32_WINDOWS:
264                 /* Windows OT */
265                 switch (info.dwMajorVersion) {
266
267                 case 4:
268                         /* 3 cheers for Microsoft marketing! */
269                         switch (info.dwMinorVersion) {
270
271                         case 0:
272                                 g_string_append_printf(str, "Windows 95");
273                                 break;
274
275                         case 10:
276                                 g_string_append_printf(str, "Windows 98");
277                                 break;
278
279                         case 90:
280                                 g_string_append_printf(str, "Windows Me");
281                                 break;
282
283                         default:
284                                 g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
285                                     info.dwMajorVersion, info.dwMinorVersion);
286                                 break;
287                         }
288                         break;
289
290                 default:
291                         g_string_append_printf(str, "Windows OT, unknown version %lu.%lu",
292                             info.dwMajorVersion, info.dwMinorVersion);
293                         break;
294                 }
295                 break;
296
297         case VER_PLATFORM_WIN32_NT:
298                 /* Windows NT */
299                 switch (info.dwMajorVersion) {
300
301                 case 3:
302                 case 4:
303                         g_string_append_printf(str, "Windows NT %lu.%lu",
304                             info.dwMajorVersion, info.dwMinorVersion);
305                         break;
306
307                 case 5:
308                         /* 3 cheers for Microsoft marketing! */
309                         switch (info.dwMinorVersion) {
310
311                         case 0:
312                                 g_string_append_printf(str, "Windows 2000");
313                                 break;
314
315                         case 1:
316                                 g_string_append_printf(str, "Windows XP");
317                                 break;
318
319                         case 2:
320                                 if ((info.wProductType == VER_NT_WORKSTATION) &&
321                                     (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)) {
322                                         g_string_append_printf(str, "Windows XP Professional x64 Edition");
323                                 } else {
324                                         g_string_append_printf(str, "Windows Server 2003");
325                                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
326                                                 g_string_append_printf(str, " x64 Edition");
327                                 }
328                                 break;
329
330                         default:
331                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
332                                                        info.dwMajorVersion, info.dwMinorVersion);
333                                 break;
334                         }
335                         break;
336
337                 case 6: {
338                         gboolean is_nt_workstation;
339
340                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
341                                 g_string_append(str, "64-bit ");
342                         else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
343                                 g_string_append(str, "32-bit ");
344 #ifndef VER_NT_WORKSTATION
345 #define VER_NT_WORKSTATION 0x01
346                         is_nt_workstation = ((info.wReserved[1] & 0xff) == VER_NT_WORKSTATION);
347 #else
348                         is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
349 #endif
350                         switch (info.dwMinorVersion) {
351                         case 0:
352                                 g_string_append_printf(str, is_nt_workstation ? "Windows Vista" : "Windows Server 2008");
353                                 break;
354                         case 1:
355                                 g_string_append_printf(str, is_nt_workstation ? "Windows 7" : "Windows Server 2008 R2");
356                                 break;
357                         case 2:
358                                 g_string_append_printf(str, is_nt_workstation ? "Windows 8" : "Windows Server 2012");
359                                 break;
360                         case 3:
361                                 g_string_append_printf(str, is_nt_workstation ? "Windows 8.1" : "Windows Server 2012 R2");
362                                 break;
363                         default:
364                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
365                                                        info.dwMajorVersion, info.dwMinorVersion);
366                                 break;
367                         }
368                         break;
369                 }  /* case 6 */
370
371                 case 10: {
372                         gboolean is_nt_workstation;
373
374                         if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
375                                 g_string_append(str, "64-bit ");
376                         else if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
377                                 g_string_append(str, "32-bit ");
378                         is_nt_workstation = (info.wProductType == VER_NT_WORKSTATION);
379                         switch (info.dwMinorVersion) {
380                         case 0:
381                                 g_string_append_printf(str, is_nt_workstation ? "Windows 10" : "Windows Server 2016");
382                                 break;
383                         default:
384                                 g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
385                                                        info.dwMajorVersion, info.dwMinorVersion);
386                                 break;
387                         }
388                         break;
389                 }  /* case 10 */
390
391                 default:
392                         g_string_append_printf(str, "Windows NT, unknown version %lu.%lu",
393                             info.dwMajorVersion, info.dwMinorVersion);
394                         break;
395                 } /* info.dwMajorVersion */
396                 break;
397
398         default:
399                 g_string_append_printf(str, "Unknown Windows platform %lu version %lu.%lu",
400                     info.dwPlatformId, info.dwMajorVersion, info.dwMinorVersion);
401                 break;
402         }
403         if (info.szCSDVersion[0] != '\0')
404                 g_string_append_printf(str, " %s", utf_16to8(info.szCSDVersion));
405         g_string_append_printf(str, ", build %lu", info.dwBuildNumber);
406 #elif defined(HAVE_SYS_UTSNAME_H)
407         /*
408          * We have <sys/utsname.h>, so we assume we have "uname()".
409          */
410         if (uname(&name) < 0) {
411                 g_string_append_printf(str, "unknown OS version (uname failed - %s)",
412                     g_strerror(errno));
413                 return;
414         }
415
416         if (strcmp(name.sysname, "AIX") == 0) {
417                 /*
418                  * Yay, IBM!  Thanks for doing something different
419                  * from most of the other UNIXes out there, and
420                  * making "name.version" apparently be the major
421                  * version number and "name.release" be the minor
422                  * version number.
423                  */
424                 g_string_append_printf(str, "%s %s.%s", name.sysname, name.version,
425                     name.release);
426         } else {
427                 /*
428                  * XXX - get "version" on any other platforms?
429                  *
430                  * On Digital/Tru64 UNIX, it's something unknown.
431                  * On Solaris, it's some kind of build information.
432                  * On HP-UX, it appears to be some sort of subrevision
433                  * thing.
434                  * On *BSD and Darwin/macOS, it's a long string giving
435                  * a build date, config file name, etc., etc., etc..
436                  */
437 #ifdef HAVE_OS_X_FRAMEWORKS
438                 /*
439                  * On macOS, report the macOS version number as the OS
440                  * version if we can, and put the Darwin information
441                  * in parentheses.
442                  */
443                 if (get_os_x_version_info(str)) {
444                         /* Success - append the Darwin information. */
445                         g_string_append_printf(str, " (%s %s)", name.sysname, name.release);
446                 } else {
447                         /* Failure - just use the Darwin information. */
448                         g_string_append_printf(str, "%s %s", name.sysname, name.release);
449                 }
450 #else /* HAVE_OS_X_FRAMEWORKS */
451                 /*
452                  * XXX - on Linux, are there any APIs to get the distribution
453                  * name and version number?  I think some distributions have
454                  * that.
455                  *
456                  * At least on Linux Standard Base-compliant distributions,
457                  * there's an "lsb_release" command.  However:
458                  *
459                  *      http://forums.fedoraforum.org/showthread.php?t=220885
460                  *
461                  * seems to suggest that if you don't have the redhat-lsb
462                  * package installed, you don't have lsb_release, and that
463                  * /etc/fedora-release has the release information on
464                  * Fedora.
465                  *
466                  *      http://linux.die.net/man/1/lsb_release
467                  *
468                  * suggests that there's an /etc/distrib-release file, but
469                  * it doesn't indicate whether "distrib" is literally
470                  * "distrib" or is the name for the distribution, and
471                  * also speaks of an /etc/debian_version file.
472                  *
473                  * "lsb_release" apparently parses /etc/lsb-release, which
474                  * has shell-style assignments, assigning to, among other
475                  * values, DISTRIB_ID (distributor/distribution name),
476                  * DISTRIB_RELEASE (release number of the distribution),
477                  * DISTRIB_DESCRIPTION (*might* be name followed by version,
478                  * but the manpage for lsb_release seems to indicate that's
479                  * not guaranteed), and DISTRIB_CODENAME (code name, e.g.
480                  * "licentious" for the Ubuntu Licentious Lemur release).
481                  * the lsb_release man page also speaks of the distrib-release
482                  * file, but Debian doesn't have one, and Ubuntu 7's
483                  * lsb_release command doesn't look for one.
484                  *
485                  * I've seen references to /etc/redhat-release as well.
486                  *
487                  * At least on my Ubuntu 7 system, /etc/debian_version
488                  * doesn't contain anything interesting (just some Debian
489                  * codenames).
490                  *
491                  * See also
492                  *
493                  *      http://bugs.python.org/issue1322
494                  *
495                  *      http://www.novell.com/coolsolutions/feature/11251.html
496                  *
497                  *      http://linuxmafia.com/faq/Admin/release-files.html
498                  *
499                  * and the Lib/Platform.py file in recent Python 2.x
500                  * releases.
501                  */
502                 g_string_append_printf(str, "%s %s", name.sysname, name.release);
503 #endif /* HAVE_OS_X_FRAMEWORKS */
504         }
505 #else
506         g_string_append(str, "an unknown OS");
507 #endif
508 }
509
510 #if defined(_WIN32)
511 /*
512  * Get the Windows major OS version.
513  *
514  * XXX - Should this return the minor version as well, e.g. 0x00050002?
515  *
516  * XXX - I think Microsoft have now stuck it at 6 forever, so it really
517  * distinguishes, on the versions of Windows we currently support and
518  * future Windows versions between Windows 2000/XP (major version 5) and
519  * everything after it (major version 6).
520  */
521 guint32
522 get_windows_major_version(void)
523 {
524         OSVERSIONINFO info;
525
526         info.dwOSVersionInfoSize = sizeof info;
527         if (GetVersionEx(&info)) {
528                 return info.dwMajorVersion;
529         }
530         return 0;
531 }
532 #endif
533
534 /*
535  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
536  *
537  * Local variables:
538  * c-basic-offset: 8
539  * tab-width: 8
540  * indent-tabs-mode: t
541  * End:
542  *
543  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
544  * :indentSize=8:tabSize=8:noTabs=false:
545  */