1 /* @(#) $Id: ./src/shared/file_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
14 /* Functions to handle operation with files
21 /* Vista product information. */
23 #ifndef PRODUCT_UNLICENSED
24 #define PRODUCT_UNLICENSED 0xABCDABCD
25 #define PRODUCT_UNLICENSED_C "Product Unlicensed "
28 #ifndef PRODUCT_BUSINESS
29 #define PRODUCT_BUSINESS 0x00000006
30 #define PRODUCT_BUSINESS_C "Business Edition "
33 #ifndef PRODUCT_BUSINESS_N
34 #define PRODUCT_BUSINESS_N 0x00000010
35 #define PRODUCT_BUSINESS_N_C "Business Edition "
38 #ifndef PRODUCT_CLUSTER_SERVER
39 #define PRODUCT_CLUSTER_SERVER 0x00000012
40 #define PRODUCT_CLUSTER_SERVER_C "Cluster Server Edition "
43 #ifndef PRODUCT_DATACENTER_SERVER
44 #define PRODUCT_DATACENTER_SERVER 0x00000008
45 #define PRODUCT_DATACENTER_SERVER_C "Datacenter Edition (full) "
48 #ifndef PRODUCT_DATACENTER_SERVER_CORE
49 #define PRODUCT_DATACENTER_SERVER_CORE 0x0000000C
50 #define PRODUCT_DATACENTER_SERVER_CORE_C "Datacenter Edition (core) "
53 #ifndef PRODUCT_DATACENTER_SERVER_CORE_V
54 #define PRODUCT_DATACENTER_SERVER_CORE_V 0x00000027
55 #define PRODUCT_DATACENTER_SERVER_CORE_V_C "Datacenter Edition (core) "
58 #ifndef PRODUCT_DATACENTER_SERVER_V
59 #define PRODUCT_DATACENTER_SERVER_V 0x00000025
60 #define PRODUCT_DATACENTER_SERVER_V_C "Datacenter Edition (full) "
63 #ifndef PRODUCT_ENTERPRISE
64 #define PRODUCT_ENTERPRISE 0x00000004
65 #define PRODUCT_ENTERPRISE_C "Enterprise Edition "
68 #ifndef PRODUCT_ENTERPRISE_N
69 #define PRODUCT_ENTERPRISE_N 0x0000001B
70 #define PRODUCT_ENTERPRISE_N_C "Enterprise Edition "
73 #ifndef PRODUCT_ENTERPRISE_SERVER
74 #define PRODUCT_ENTERPRISE_SERVER 0x0000000A
75 #define PRODUCT_ENTERPRISE_SERVER_C "Enterprise Edition (full) "
78 #ifndef PRODUCT_ENTERPRISE_SERVER_CORE
79 #define PRODUCT_ENTERPRISE_SERVER_CORE 0x0000000E
80 #define PRODUCT_ENTERPRISE_SERVER_CORE_C "Enterprise Edition (core) "
83 #ifndef PRODUCT_ENTERPRISE_SERVER_CORE_V
84 #define PRODUCT_ENTERPRISE_SERVER_CORE_V 0x00000029
85 #define PRODUCT_ENTERPRISE_SERVER_CORE_V_C "Enterprise Edition (core) "
88 #ifndef PRODUCT_ENTERPRISE_SERVER_IA64
89 #define PRODUCT_ENTERPRISE_SERVER_IA64 0x0000000F
90 #define PRODUCT_ENTERPRISE_SERVER_IA64_C "Enterprise Edition for Itanium-based Systems "
93 #ifndef PRODUCT_ENTERPRISE_SERVER_V
94 #define PRODUCT_ENTERPRISE_SERVER_V 0x00000026
95 #define PRODUCT_ENTERPRISE_SERVER_V_C "Enterprise Edition (full) "
98 #ifndef PRODUCT_HOME_BASIC
99 #define PRODUCT_HOME_BASIC 0x00000002
100 #define PRODUCT_HOME_BASIC_C "Home Basic Edition "
103 #ifndef PRODUCT_HOME_BASIC_N
104 #define PRODUCT_HOME_BASIC_N 0x00000005
105 #define PRODUCT_HOME_BASIC_N_C "Home Basic Edition "
108 #ifndef PRODUCT_HOME_PREMIUM
109 #define PRODUCT_HOME_PREMIUM 0x00000003
110 #define PRODUCT_HOME_PREMIUM_C "Home Premium Edition "
113 #ifndef PRODUCT_HOME_PREMIUM_N
114 #define PRODUCT_HOME_PREMIUM_N 0x0000001A
115 #define PRODUCT_HOME_PREMIUM_N_C "Home Premium Edition "
118 #ifndef PRODUCT_HOME_SERVER
119 #define PRODUCT_HOME_SERVER 0x00000013
120 #define PRODUCT_HOME_SERVER_C "Home Server Edition "
123 #ifndef PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT
124 #define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT 0x0000001E
125 #define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT_C "Essential Business Server Management Server "
128 #ifndef PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING
129 #define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING 0x00000020
130 #define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING_C "Essential Business Server Messaging Server "
133 #ifndef PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY
134 #define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY 0x0000001F
135 #define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY_C "Essential Business Server Security Server "
138 #ifndef PRODUCT_SERVER_FOR_SMALLBUSINESS
139 #define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x00000018
140 #define PRODUCT_SERVER_FOR_SMALLBUSINESS_C "Small Business Edition "
143 #ifndef PRODUCT_SMALLBUSINESS_SERVER
144 #define PRODUCT_SMALLBUSINESS_SERVER 0x00000009
145 #define PRODUCT_SMALLBUSINESS_SERVER_C "Small Business Server "
148 #ifndef PRODUCT_SMALLBUSINESS_SERVER_PREMIUM
149 #define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x00000019
150 #define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_C "Small Business Server Premium Edition "
153 #ifndef PRODUCT_STANDARD_SERVER
154 #define PRODUCT_STANDARD_SERVER 0x00000007
155 #define PRODUCT_STANDARD_SERVER_C "Standard Edition "
158 #ifndef PRODUCT_STANDARD_SERVER_CORE
159 #define PRODUCT_STANDARD_SERVER_CORE 0x0000000D
160 #define PRODUCT_STANDARD_SERVER_CORE_C "Standard Edition (core) "
163 #ifndef PRODUCT_STANDARD_SERVER_CORE_V
164 #define PRODUCT_STANDARD_SERVER_CORE_V 0x00000028
165 #define PRODUCT_STANDARD_SERVER_CORE_V_C "Standard Edition "
168 #ifndef PRODUCT_STANDARD_SERVER_V
169 #define PRODUCT_STANDARD_SERVER_V 0x00000024
170 #define PRODUCT_STANDARD_SERVER_V_C "Standard Edition "
173 #ifndef PRODUCT_STARTER
174 #define PRODUCT_STARTER 0x0000000B
175 #define PRODUCT_STARTER_C "Starter Edition "
178 #ifndef PRODUCT_STORAGE_ENTERPRISE_SERVER
179 #define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x00000017
180 #define PRODUCT_STORAGE_ENTERPRISE_SERVER_C "Storage Server Enterprise Edition "
183 #ifndef PRODUCT_STORAGE_EXPRESS_SERVER
184 #define PRODUCT_STORAGE_EXPRESS_SERVER 0x00000014
185 #define PRODUCT_STORAGE_EXPRESS_SERVER_C "Storage Server Express Edition "
188 #ifndef PRODUCT_STORAGE_STANDARD_SERVER
189 #define PRODUCT_STORAGE_STANDARD_SERVER 0x00000015
190 #define PRODUCT_STORAGE_STANDARD_SERVER_C "Storage Server Standard Edition "
193 #ifndef PRODUCT_STORAGE_WORKGROUP_SERVER
194 #define PRODUCT_STORAGE_WORKGROUP_SERVER 0x00000016
195 #define PRODUCT_STORAGE_WORKGROUP_SERVER_C "Storage Server Workgroup Edition "
198 #ifndef PRODUCT_ULTIMATE
199 #define PRODUCT_ULTIMATE 0x00000001
200 #define PRODUCT_ULTIMATE_C "Ultimate Edition "
203 #ifndef PRODUCT_ULTIMATE_N
204 #define PRODUCT_ULTIMATE_N 0x0000001C
205 #define PRODUCT_ULTIMATE_N_C "Ultimate Edition "
208 #ifndef PRODUCT_WEB_SERVER
209 #define PRODUCT_WEB_SERVER 0x00000011
210 #define PRODUCT_WEB_SERVER_C "Web Server Edition "
213 #ifndef PRODUCT_WEB_SERVER_CORE
214 #define PRODUCT_WEB_SERVER_CORE 0x0000001D
215 #define PRODUCT_WEB_SERVER_CORE_C "Web Server Edition "
221 /* Sets the name of the starting program */
222 void OS_SetName(char *name)
229 int File_DateofChange(char *file)
231 struct stat file_status;
233 if(stat(file, &file_status) < 0)
236 return (file_status.st_mtime);
239 int IsDir(char *file)
241 struct stat file_status;
242 if(stat(file,&file_status) < 0)
244 if(S_ISDIR(file_status.st_mode))
250 int CreatePID(char *name, int pid)
257 snprintf(file,255,"%s/%s-%d.pid",OS_PIDFILE,name,pid);
261 snprintf(file,255,"%s%s/%s-%d.pid",DEFAULTDIR,
262 OS_PIDFILE,name,pid);
265 fp = fopen(file,"a");
269 fprintf(fp,"%d\n",pid);
278 int DeletePID(char *name)
284 snprintf(file,255,"%s/%s-%d.pid",OS_PIDFILE,name,(int)getpid());
288 snprintf(file,255,"%s%s/%s-%d.pid",DEFAULTDIR,
289 OS_PIDFILE,name,(int)getpid());
292 if(File_DateofChange(file) < 0)
301 int UnmergeFiles(char *finalpath, char *optdir)
303 int i = 0, n = 0, ret = 1;
307 char final_name[2048 +1];
312 finalfp = fopen(finalpath, "r");
315 merror("%s: ERROR: Unable to read merged file: '%s'.",
316 __local_name, finalpath);
322 /* Reading header portion. */
323 if(fgets(buf, sizeof(buf) -1, finalfp) == NULL)
334 /* Getting file size and name. */
335 files_size = atol(buf +1);
337 files = strchr(buf, '\n');
341 files = strchr(buf, ' ');
352 snprintf(final_name, 2048, "%s/%s", optdir, files);
356 strncpy(final_name, files, 2048);
357 final_name[2048] = '\0';
361 /* Opening file name. */
362 fp = fopen(final_name,"w");
366 merror("%s: ERROR: Unable to unmerge file '%s'.",
367 __local_name, final_name);
371 if(files_size < sizeof(buf) -1)
379 files_size -= sizeof(buf) -1;
382 while((n = fread(buf, 1, i, finalfp)) > 0)
388 fwrite(buf, n, 1, fp);
397 if(files_size < sizeof(buf) -1)
405 files_size -= sizeof(buf) -1;
419 int MergeAppendFile(char *finalpath, char *files)
430 /* Creating a new entry. */
433 finalfp = fopen(finalpath, "w");
436 merror("%s: ERROR: Unable to create merged file: '%s'.",
437 __local_name, finalpath);
446 finalfp = fopen(finalpath, "a");
449 merror("%s: ERROR: Unable to create merged file: '%s'.",
450 __local_name, finalpath);
455 fp = fopen(files,"r");
458 merror("%s: ERROR: Unable to merge file '%s'.", __local_name, files);
464 fseek(fp, 0, SEEK_END);
465 files_size = ftell(fp);
467 tmpfile = strrchr(files, '/');
476 fprintf(finalfp, "!%ld %s\n", files_size, tmpfile);
478 fseek(fp, 0, SEEK_SET);
480 while((n = fread(buf, 1, sizeof(buf) -1, fp)) > 0)
483 fwrite(buf, n, 1, finalfp);
494 int MergeFiles(char *finalpath, char **files)
496 int i = 0, n = 0, ret = 1;
504 finalfp = fopen(finalpath, "w");
507 merror("%s: ERROR: Unable to create merged file: '%s'.",
508 __local_name, finalpath);
514 fp = fopen(files[i],"r");
517 merror("%s: ERROR: Unable to merge file '%s'.", __local_name, files[i]);
523 fseek(fp, 0, SEEK_END);
524 files_size = ftell(fp);
526 /* Removing last entry. */
527 tmpfile = strrchr(files[i], '/');
537 fprintf(finalfp, "!%ld %s\n", files_size, tmpfile);
539 fseek(fp, 0, SEEK_SET);
541 while((n = fread(buf, 1, sizeof(buf) -1, fp)) > 0)
544 fwrite(buf, n, 1, finalfp);
557 /* getuname; Get uname and returns a string with it.
558 * Memory must be freed after use
562 struct utsname uts_buf;
564 if(uname(&uts_buf) >= 0)
568 ret = calloc(256, sizeof(char));
572 snprintf(ret, 255, "%s %s %s %s %s - %s %s",
585 ret = calloc(256, sizeof(char));
589 snprintf(ret, 255, "No system info available - %s %s",
600 /* goDaemon: Daemonize a process without closing stdin/stdout/stderr..
611 merror(FORK_ERROR, __local_name);
620 /* becoming session leader */
623 merror(SETSID_ERROR, __local_name);
632 merror(FORK_ERROR, __local_name);
653 /* goDaemon: Daemonize a process..
665 merror(FORK_ERROR, __local_name);
673 /* becoming session leader */
676 merror(SETSID_ERROR, __local_name);
684 merror(FORK_ERROR, __local_name);
693 /* Dup stdin, stdout and stderr to /dev/null */
694 if((fd = open("/dev/null", O_RDWR)) >= 0)
706 /* Closing stdin, stdout and stderr */
713 /* Openining stdin, stdout and stderr to /dev/null */
715 open("/dev/null", O_RDONLY);
716 open("/dev/null", O_RDWR);
717 open("/dev/null", O_RDWR);
730 m_uname = getuname();
733 merror(MEM_ERROR, __local_name);
738 /* We check if the system is vista (must be called during the startup.) */
739 if(strstr(m_uname, "Windows Server 2008") ||
740 strstr(m_uname, "Vista") ||
741 strstr(m_uname, "Windows 7"))
744 verbose("%s: INFO: System is Vista or Windows Server 2008.",
755 /** get uname for windows **/
758 int ret_size = OS_SIZE_1024 -2;
762 typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
763 typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
766 /* Extracted from ms web site
767 * http://msdn.microsoft.com/library/en-us/sysinfo/base/getting_the_system_version.asp
769 OSVERSIONINFOEX osvi;
773 BOOL bOsVersionInfoEx;
776 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
777 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
779 if(!(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)))
781 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
782 if (!GetVersionEx((OSVERSIONINFO *)&osvi))
786 /* Allocating the memory */
787 os_calloc(OS_SIZE_1024 +1, sizeof(char), ret);
788 ret[OS_SIZE_1024] = '\0';
790 switch(osvi.dwPlatformId)
792 /* Test for the Windows NT product family. */
793 case VER_PLATFORM_WIN32_NT:
794 if(osvi.dwMajorVersion == 6)
796 if(osvi.dwMinorVersion == 0)
798 if(osvi.wProductType == VER_NT_WORKSTATION )
799 strncat(ret, "Microsoft Windows Vista ", ret_size -1);
802 strncat(ret, "Microsoft Windows Server 2008 ", ret_size -1);
805 else if(osvi.dwMinorVersion == 1)
807 if(osvi.wProductType == VER_NT_WORKSTATION )
808 strncat(ret, "Microsoft Windows 7 ", ret_size -1);
811 strncat(ret, "Microsoft Windows Server 2008 R2 ", ret_size -1);
815 ret_size-=strlen(ret) +1;
818 /* Getting product version. */
819 pGPI = (PGPI) GetProcAddress(
820 GetModuleHandle(TEXT("kernel32.dll")),
823 pGPI( 6, 0, 0, 0, &dwType);
827 case PRODUCT_UNLICENSED:
828 strncat(ret, PRODUCT_UNLICENSED_C, ret_size -1);
830 case PRODUCT_BUSINESS:
831 strncat(ret, PRODUCT_BUSINESS_C, ret_size -1);
833 case PRODUCT_BUSINESS_N:
834 strncat(ret, PRODUCT_BUSINESS_N_C, ret_size -1);
836 case PRODUCT_CLUSTER_SERVER:
837 strncat(ret, PRODUCT_CLUSTER_SERVER_C, ret_size -1);
839 case PRODUCT_DATACENTER_SERVER:
840 strncat(ret, PRODUCT_DATACENTER_SERVER_C, ret_size -1);
842 case PRODUCT_DATACENTER_SERVER_CORE:
843 strncat(ret, PRODUCT_DATACENTER_SERVER_CORE_C, ret_size -1);
845 case PRODUCT_DATACENTER_SERVER_CORE_V:
846 strncat(ret, PRODUCT_DATACENTER_SERVER_CORE_V_C, ret_size -1);
848 case PRODUCT_DATACENTER_SERVER_V:
849 strncat(ret, PRODUCT_DATACENTER_SERVER_V_C, ret_size -1);
851 case PRODUCT_ENTERPRISE:
852 strncat(ret, PRODUCT_ENTERPRISE_C, ret_size -1);
854 case PRODUCT_ENTERPRISE_N:
855 strncat(ret, PRODUCT_ENTERPRISE_N_C, ret_size -1);
857 case PRODUCT_ENTERPRISE_SERVER:
858 strncat(ret, PRODUCT_ENTERPRISE_SERVER_C, ret_size -1);
860 case PRODUCT_ENTERPRISE_SERVER_CORE:
861 strncat(ret, PRODUCT_ENTERPRISE_SERVER_CORE_C, ret_size -1);
863 case PRODUCT_ENTERPRISE_SERVER_CORE_V:
864 strncat(ret, PRODUCT_ENTERPRISE_SERVER_CORE_V_C, ret_size -1);
866 case PRODUCT_ENTERPRISE_SERVER_IA64:
867 strncat(ret, PRODUCT_ENTERPRISE_SERVER_IA64_C, ret_size -1);
869 case PRODUCT_ENTERPRISE_SERVER_V:
870 strncat(ret, PRODUCT_ENTERPRISE_SERVER_V_C, ret_size -1);
872 case PRODUCT_HOME_BASIC:
873 strncat(ret, PRODUCT_HOME_BASIC_C, ret_size -1);
875 case PRODUCT_HOME_BASIC_N:
876 strncat(ret, PRODUCT_HOME_BASIC_N_C, ret_size -1);
878 case PRODUCT_HOME_PREMIUM:
879 strncat(ret, PRODUCT_HOME_PREMIUM_C, ret_size -1);
881 case PRODUCT_HOME_PREMIUM_N:
882 strncat(ret, PRODUCT_HOME_PREMIUM_N_C, ret_size -1);
884 case PRODUCT_HOME_SERVER:
885 strncat(ret, PRODUCT_HOME_SERVER_C, ret_size -1);
887 case PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT:
888 strncat(ret, PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT_C, ret_size -1);
890 case PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING:
891 strncat(ret, PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING_C, ret_size -1);
893 case PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY:
894 strncat(ret, PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY_C, ret_size -1);
896 case PRODUCT_SERVER_FOR_SMALLBUSINESS:
897 strncat(ret, PRODUCT_SERVER_FOR_SMALLBUSINESS_C, ret_size -1);
899 case PRODUCT_SMALLBUSINESS_SERVER:
900 strncat(ret, PRODUCT_SMALLBUSINESS_SERVER_C, ret_size -1);
902 case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
903 strncat(ret, PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_C, ret_size -1);
905 case PRODUCT_STANDARD_SERVER:
906 strncat(ret, PRODUCT_STANDARD_SERVER_C, ret_size -1);
908 case PRODUCT_STANDARD_SERVER_CORE:
909 strncat(ret, PRODUCT_STANDARD_SERVER_CORE_C, ret_size -1);
911 case PRODUCT_STANDARD_SERVER_CORE_V:
912 strncat(ret, PRODUCT_STANDARD_SERVER_CORE_V_C, ret_size -1);
914 case PRODUCT_STANDARD_SERVER_V:
915 strncat(ret, PRODUCT_STANDARD_SERVER_V_C, ret_size -1);
917 case PRODUCT_STARTER:
918 strncat(ret, PRODUCT_STARTER_C, ret_size -1);
920 case PRODUCT_STORAGE_ENTERPRISE_SERVER:
921 strncat(ret, PRODUCT_STORAGE_ENTERPRISE_SERVER_C, ret_size -1);
923 case PRODUCT_STORAGE_EXPRESS_SERVER:
924 strncat(ret, PRODUCT_STORAGE_EXPRESS_SERVER_C, ret_size -1);
926 case PRODUCT_STORAGE_STANDARD_SERVER:
927 strncat(ret, PRODUCT_STORAGE_STANDARD_SERVER_C, ret_size -1);
929 case PRODUCT_STORAGE_WORKGROUP_SERVER:
930 strncat(ret, PRODUCT_STORAGE_WORKGROUP_SERVER_C, ret_size -1);
932 case PRODUCT_ULTIMATE:
933 strncat(ret, PRODUCT_ULTIMATE_C, ret_size -1);
935 case PRODUCT_ULTIMATE_N:
936 strncat(ret, PRODUCT_ULTIMATE_N_C, ret_size -1);
938 case PRODUCT_WEB_SERVER:
939 strncat(ret, PRODUCT_WEB_SERVER_C, ret_size -1);
941 case PRODUCT_WEB_SERVER_CORE:
942 strncat(ret, PRODUCT_WEB_SERVER_CORE_C, ret_size -1);
947 ret_size-=strlen(ret) +1;
950 else if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
952 pGNSI = (PGNSI) GetProcAddress(
953 GetModuleHandle("kernel32.dll"),
954 "GetNativeSystemInfo");
958 if( GetSystemMetrics(89) )
959 strncat(ret, "Microsoft Windows Server 2003 R2 ",
961 else if(osvi.wProductType == VER_NT_WORKSTATION &&
962 si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
965 "Microsoft Windows XP Professional x64 Edition ",
970 strncat(ret, "Microsoft Windows Server 2003, ",ret_size-1);
973 ret_size-=strlen(ret) +1;
976 else if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
978 strncat(ret, "Microsoft Windows XP ", ret_size -1);
980 ret_size-=strlen(ret) +1;
983 else if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
985 strncat(ret, "Microsoft Windows 2000 ", ret_size -1);
987 ret_size-=strlen(ret) +1;
990 else if (osvi.dwMajorVersion <= 4)
992 strncat(ret, "Microsoft Windows NT ", ret_size -1);
994 ret_size-=strlen(ret) +1;
998 strncat(ret, "Microsoft Windows Unknown ", ret_size -1);
1000 ret_size-=strlen(ret) +1;
1003 /* Test for specific product on Windows NT 4.0 SP6 and later. */
1004 if(bOsVersionInfoEx)
1006 /* Test for the workstation type. */
1007 if (osvi.wProductType == VER_NT_WORKSTATION &&
1008 si.wProcessorArchitecture!=PROCESSOR_ARCHITECTURE_AMD64)
1010 if( osvi.dwMajorVersion == 4 )
1011 strncat(ret, "Workstation 4.0 ", ret_size -1);
1012 else if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
1013 strncat(ret, "Home Edition ", ret_size -1);
1015 strncat(ret, "Professional ",ret_size -1);
1018 ret_size-=strlen(ret) +1;
1021 /* Test for the server type. */
1022 else if( osvi.wProductType == VER_NT_SERVER ||
1023 osvi.wProductType == VER_NT_DOMAIN_CONTROLLER )
1025 if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==2)
1027 if (si.wProcessorArchitecture==
1028 PROCESSOR_ARCHITECTURE_IA64 )
1030 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
1032 "Datacenter Edition for Itanium-based Systems ",
1034 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
1036 "Enterprise Edition for Itanium-based Systems ",
1039 ret_size-=strlen(ret) +1;
1042 else if ( si.wProcessorArchitecture==
1043 PROCESSOR_ARCHITECTURE_AMD64 )
1045 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
1046 strncat(ret, "Datacenter x64 Edition ",
1048 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
1049 strncat(ret, "Enterprise x64 Edition ",
1052 strncat(ret, "Standard x64 Edition ",
1055 ret_size-=strlen(ret) +1;
1060 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
1061 strncat(ret, "Datacenter Edition ",
1063 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
1064 strncat(ret,"Enterprise Edition ",ret_size -1);
1065 else if ( osvi.wSuiteMask == VER_SUITE_BLADE )
1066 strncat(ret,"Web Edition ",ret_size -1 );
1068 strncat(ret, "Standard Edition ",ret_size -1);
1070 ret_size-=strlen(ret) +1;
1073 else if(osvi.dwMajorVersion==5 && osvi.dwMinorVersion==0)
1075 if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
1076 strncat(ret, "Datacenter Server ",ret_size -1);
1077 else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
1078 strncat(ret, "Advanced Server ",ret_size -1 );
1080 strncat(ret, "Server ",ret_size -1);
1082 ret_size-=strlen(ret) +1;
1084 else if(osvi.dwMajorVersion <= 4) /* Windows NT 4.0 */
1086 if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
1087 strncat(ret, "Server 4.0, Enterprise Edition ",
1090 strncat(ret, "Server 4.0 ",ret_size -1);
1092 ret_size-=strlen(ret) +1;
1096 /* Test for specific product on Windows NT 4.0 SP5 and earlier */
1100 char szProductType[81];
1104 lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1105 "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
1106 0, KEY_QUERY_VALUE, &hKey );
1107 if(lRet == ERROR_SUCCESS)
1111 lRet = RegQueryValueEx( hKey, "ProductType", NULL, NULL,
1112 (LPBYTE) szProductType, &dwBufLen);
1113 RegCloseKey( hKey );
1115 if((lRet == ERROR_SUCCESS) && (dwBufLen < 80) )
1117 if (lstrcmpi( "WINNT", szProductType) == 0 )
1118 strncat(ret, "Workstation ",ret_size -1);
1119 else if(lstrcmpi( "LANMANNT", szProductType) == 0 )
1120 strncat(ret, "Server ",ret_size -1);
1121 else if(lstrcmpi( "SERVERNT", szProductType) == 0 )
1122 strncat(ret, "Advanced Server " ,ret_size -1);
1124 ret_size-=strlen(ret) +1;
1126 memset(__wv, '\0', 32);
1129 (int)osvi.dwMajorVersion,
1130 (int)osvi.dwMinorVersion);
1132 strncat(ret, __wv, ret_size -1);
1133 ret_size-=strlen(__wv) +1;
1138 /* Display service pack (if any) and build number. */
1140 if( osvi.dwMajorVersion == 4 &&
1141 lstrcmpi( osvi.szCSDVersion, "Service Pack 6" ) == 0 )
1147 memset(__wp, '\0', 64);
1148 /* Test for SP6 versus SP6a. */
1149 lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
1150 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009",
1151 0, KEY_QUERY_VALUE, &hKey );
1152 if( lRet == ERROR_SUCCESS )
1153 snprintf(__wp, 63, "Service Pack 6a (Build %d)",
1154 (int)osvi.dwBuildNumber & 0xFFFF );
1155 else /* Windows NT 4.0 prior to SP6a */
1157 snprintf(__wp, 63, "%s (Build %d)",
1159 (int)osvi.dwBuildNumber & 0xFFFF);
1162 strncat(ret, __wp, ret_size -1);
1163 ret_size-=strlen(__wp) +1;
1164 RegCloseKey( hKey );
1170 memset(__wp, '\0', 64);
1172 snprintf(__wp, 63, "%s (Build %d)",
1174 (int)osvi.dwBuildNumber & 0xFFFF);
1176 strncat(ret, __wp, ret_size -1);
1177 ret_size-=strlen(__wp) +1;
1181 /* Test for the Windows Me/98/95. */
1182 case VER_PLATFORM_WIN32_WINDOWS:
1184 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
1186 strncat(ret, "Microsoft Windows 95 ", ret_size -1);
1187 ret_size-=strlen(ret) +1;
1190 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
1192 strncat(ret, "Microsoft Windows 98 ", ret_size -1);
1193 ret_size-=strlen(ret) +1;
1196 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
1198 strncat(ret, "Microsoft Windows Millennium Edition",
1201 ret_size-=strlen(ret) +1;
1205 case VER_PLATFORM_WIN32s:
1207 strncat(ret, "Microsoft Win32s", ret_size -1);
1208 ret_size-=strlen(ret) +1;
1213 /* Adding ossec version */
1214 snprintf(os_v, 128, " - %s %s", __name, __version);
1215 strncat(ret, os_v, ret_size -1);
1218 /* Returning system information */