output.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #ifndef _DEFAULT_SOURCE
  2. #define _DEFAULT_SOURCE
  3. #endif // _DEFAULT_SOURCE
  4. #ifndef _CRT_SECURE_NO_WARNINGS
  5. #define _CRT_SECURE_NO_WARNINGS
  6. #endif
  7. #ifndef CONFIG
  8. #define CONFIG "config.h"
  9. #endif // CONFIG
  10. #include CONFIG
  11. #include "output.h"
  12. #include "shared_globals.h"
  13. #include "endian.h"
  14. #include "helpers.h"
  15. #ifndef NO_LOG
  16. static void vlogger(const char *message, va_list args)
  17. {
  18. FILE *log;
  19. #ifdef _NTSERVICE
  20. if (!IsNTService && logstdout) log = stdout;
  21. #else
  22. if (logstdout) log = stdout;
  23. #endif
  24. else
  25. {
  26. if (fn_log == NULL) return;
  27. #ifndef _WIN32
  28. if (!strcmp(fn_log, "syslog"))
  29. {
  30. openlog("vlmcsd", LOG_CONS | LOG_PID, LOG_USER);
  31. ////PORTABILITY: vsyslog is not in Posix but virtually all Unixes have it
  32. vsyslog(LOG_INFO, message, args);
  33. closelog();
  34. return;
  35. }
  36. #endif // _WIN32
  37. log = fopen(fn_log, "a");
  38. if ( !log ) return;
  39. }
  40. time_t now = time(0);
  41. #ifdef USE_THREADS
  42. char mbstr[2048];
  43. #else
  44. char mbstr[24];
  45. #endif
  46. if (LogDateAndTime)
  47. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X: ", localtime(&now));
  48. else
  49. *mbstr = 0;
  50. #ifndef USE_THREADS
  51. fprintf(log, "%s", mbstr);
  52. vfprintf(log, message, args);
  53. fflush(log);
  54. #else // USE_THREADS
  55. // We write everything to a string before we really log inside the critical section
  56. // so formatting the output can be concurrent
  57. int len = (int)strlen(mbstr);
  58. //# if !_MSC_VER
  59. vlmcsd_vsnprintf(mbstr + len, sizeof(mbstr) - len, message, args);
  60. //# else
  61. // wvsprintf(mbstr + len, message, args);
  62. //# endif
  63. lock_mutex(&logmutex);
  64. fprintf(log, "%s", mbstr);
  65. fflush(log);
  66. unlock_mutex(&logmutex);
  67. #endif // USE_THREADS
  68. if (log != stdout) fclose(log);
  69. }
  70. // Always sends to log output
  71. int logger(const char *const fmt, ...)
  72. {
  73. va_list args;
  74. va_start(args, fmt);
  75. vlogger(fmt, args);
  76. va_end(args);
  77. return 0;
  78. }
  79. #endif //NO_LOG
  80. // Output to stderr if it is available or to log otherwise (e.g. if running as daemon/service)
  81. int printerrorf(const char *const fmt, ...)
  82. {
  83. int error = errno;
  84. va_list arglist;
  85. va_start(arglist, fmt);
  86. # ifdef IS_LIBRARY
  87. size_t len = strlen(ErrorMessage);
  88. vlmcsd_vsnprintf(ErrorMessage + len, MESSAGE_BUFFER_SIZE - len - 1, fmt, arglist);
  89. # else // !IS_LIBRARY
  90. # ifndef NO_LOG
  91. # ifdef _NTSERVICE
  92. if (InetdMode || IsNTService)
  93. # else // !_NTSERVICE
  94. if (InetdMode)
  95. # endif // NTSERVIICE
  96. vlogger(fmt, arglist);
  97. else
  98. # endif //NO_LOG
  99. # endif // IS_LIBRARY
  100. {
  101. vfprintf(stderr, fmt, arglist);
  102. fflush(stderr);
  103. }
  104. va_end(arglist);
  105. errno = error;
  106. return 0;
  107. }
  108. // Always output to stderr
  109. int errorout(const char* fmt, ...)
  110. {
  111. va_list args;
  112. va_start(args, fmt);
  113. int i = vfprintf(stderr, fmt, args);
  114. va_end(args);
  115. fflush(stderr);
  116. return i;
  117. }
  118. #ifndef NO_VERBOSE_LOG
  119. static const char *LicenseStatusText[] =
  120. {
  121. "Unlicensed", "Licensed", "OOB grace", "OOT grace", "Non-Genuine", "Notification", "Extended grace"
  122. };
  123. #endif // NO_VERBOSE_LOG
  124. void uuid2StringLE(const GUID *const guid, char *const string)
  125. {
  126. sprintf(string,
  127. #ifdef _WIN32
  128. "%08x-%04x-%04x-%04x-%012I64x",
  129. #else
  130. "%08x-%04x-%04x-%04x-%012llx",
  131. #endif
  132. (unsigned int)LE32( guid->Data1 ),
  133. (unsigned int)LE16( guid->Data2 ),
  134. (unsigned int)LE16( guid->Data3 ),
  135. (unsigned int)BE16( *(uint16_t*)guid->Data4 ),
  136. (unsigned long long)BE64(*(uint64_t*)(guid->Data4)) & 0xffffffffffffLL
  137. );
  138. }
  139. #if !defined(NO_VERBOSE_LOG) && !defined(NO_LOG)
  140. void logRequestVerbose(const REQUEST *const Request, const PRINTFUNC p)
  141. {
  142. char guidBuffer[GUID_STRING_LENGTH + 1];
  143. char WorkstationBuffer[3 * WORKSTATION_NAME_BUFFER];
  144. const char *productName;
  145. ProdListIndex_t index;
  146. p("Protocol version : %u.%u\n", LE16(Request->MajorVer), LE16(Request->MinorVer));
  147. p("Client is a virtual machine : %s\n", LE32(Request->VMInfo) ? "Yes" : "No");
  148. p("Licensing status : %u (%s)\n", (uint32_t)LE32(Request->LicenseStatus), LE32(Request->LicenseStatus) < _countof(LicenseStatusText) ? LicenseStatusText[LE32(Request->LicenseStatus)] : "Unknown");
  149. p("Remaining time (0 = forever) : %i minutes\n", (uint32_t)LE32(Request->BindingExpiration));
  150. uuid2StringLE(&Request->AppID, guidBuffer);
  151. productName = getProductNameLE(&Request->AppID, AppList, &index);
  152. p("Application ID : %s (%s)\n", guidBuffer, productName);
  153. uuid2StringLE(&Request->ActID, guidBuffer);
  154. #ifndef NO_EXTENDED_PRODUCT_LIST
  155. productName = getProductNameLE(&Request->ActID, ExtendedProductList, &index);
  156. #else
  157. productName = "Unknown";
  158. #endif
  159. p("SKU ID (aka Activation ID) : %s (%s)\n", guidBuffer, productName);
  160. uuid2StringLE(&Request->KMSID, guidBuffer);
  161. productName = getProductNameLE(&Request->KMSID, ProductList, &index);
  162. p("KMS ID (aka KMS counted ID) : %s (%s)\n", guidBuffer, productName);
  163. uuid2StringLE(&Request->CMID, guidBuffer);
  164. p("Client machine ID : %s\n", guidBuffer);
  165. uuid2StringLE(&Request->CMID_prev, guidBuffer);
  166. p("Previous client machine ID : %s\n", guidBuffer);
  167. char mbstr[64];
  168. time_t st;
  169. st = fileTimeToUnixTime(&Request->ClientTime);
  170. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", gmtime(&st));
  171. p("Client request timestamp (UTC) : %s\n", mbstr);
  172. ucs2_to_utf8(Request->WorkstationName, WorkstationBuffer, WORKSTATION_NAME_BUFFER, sizeof(WorkstationBuffer));
  173. p("Workstation name : %s\n", WorkstationBuffer);
  174. p("N count policy (minimum clients): %u\n", (uint32_t)LE32(Request->N_Policy));
  175. }
  176. void logResponseVerbose(const char *const ePID, const BYTE *const hwid, const RESPONSE *const response, const PRINTFUNC p)
  177. {
  178. char guidBuffer[GUID_STRING_LENGTH + 1];
  179. //SYSTEMTIME st;
  180. p("Protocol version : %u.%u\n", (uint32_t)LE16(response->MajorVer), (uint32_t)LE16(response->MinorVer));
  181. p("KMS host extended PID : %s\n", ePID);
  182. if (LE16(response->MajorVer) > 5)
  183. # ifndef _WIN32
  184. p("KMS host Hardware ID : %016llX\n", (unsigned long long)BE64(*(uint64_t*)hwid));
  185. # else // _WIN32
  186. p("KMS host Hardware ID : %016I64X\n", (unsigned long long)BE64(*(uint64_t*)hwid));
  187. # endif // WIN32
  188. uuid2StringLE(&response->CMID, guidBuffer);
  189. p("Client machine ID : %s\n", guidBuffer);
  190. char mbstr[64];
  191. time_t st;
  192. st = fileTimeToUnixTime(&response->ClientTime);
  193. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", gmtime(&st));
  194. p("Client request timestamp (UTC) : %s\n", mbstr);
  195. p("KMS host current active clients : %u\n", (uint32_t)LE32(response->Count));
  196. p("Renewal interval policy : %u\n", (uint32_t)LE32(response->VLRenewalInterval));
  197. p("Activation interval policy : %u\n", (uint32_t)LE32(response->VLActivationInterval));
  198. }
  199. #endif // !defined(NO_VERBOSE_LOG) && !defined(NO_LOG)
  200. #ifndef NO_VERSION_INFORMATION
  201. void printPlatform()
  202. {
  203. int testNumber = 0x1234;
  204. # ifdef VLMCSD_COMPILER
  205. printf
  206. (
  207. "Compiler: %s\n", VLMCSD_COMPILER
  208. # ifdef __VERSION__
  209. " " __VERSION__
  210. # endif // __VERSION__
  211. );
  212. # endif // VLMCSD_COMPILER
  213. printf
  214. (
  215. "Intended platform:%s %s\n", ""
  216. # if __i386__ || _M_IX86
  217. " Intel x86"
  218. # endif
  219. # if __x86_64__ || __amd64__ || _M_X64 || _M_AMD64
  220. " Intel x86_64"
  221. # endif
  222. # if _M_ARM || __arm__
  223. " ARM"
  224. # endif
  225. # if __thumb__
  226. " thumb"
  227. # endif
  228. # if __aarch64__
  229. " ARM64"
  230. # endif
  231. # if __hppa__
  232. " HP/PA RISC"
  233. # endif
  234. # if __ia64__
  235. " Intel Itanium"
  236. # endif
  237. # if __mips__
  238. " MIPS"
  239. # endif
  240. # if defined(_MIPS_ARCH)
  241. " " _MIPS_ARCH
  242. # endif
  243. # if __mips16
  244. " mips16"
  245. # endif
  246. # if __mips_micromips
  247. " micromips"
  248. # endif
  249. # if __ppc__ || __powerpc__
  250. " PowerPC"
  251. # endif
  252. # if __powerpc64__ || __ppc64__
  253. " PowerPC64"
  254. # endif
  255. # if __sparc__
  256. " SPARC"
  257. # endif
  258. # if defined(__s390__) && !defined(__zarch__) && !defined(__s390x__)
  259. " IBM S/390"
  260. # endif
  261. # if __zarch__ || __s390x__
  262. " IBM z/Arch (S/390x)"
  263. # endif
  264. # if __m68k__
  265. " Motorola 68k"
  266. # endif
  267. # if __ANDROID__
  268. " Android"
  269. # endif
  270. # if __ANDROID_API__
  271. " (API level " ANDROID_API_LEVEL ")"
  272. # endif
  273. # if __FreeBSD__ || __FreeBSD_kernel__
  274. " FreeBSD"
  275. # endif
  276. # if __NetBSD__
  277. " NetBSD"
  278. # endif
  279. # if __OpenBSD__
  280. " OpenBSD"
  281. # endif
  282. # if __DragonFly__
  283. " DragonFly BSD"
  284. # endif
  285. # if defined(__CYGWIN__) && !defined(_WIN64)
  286. " Cygwin32"
  287. # endif
  288. # if defined(__CYGWIN__) && defined(_WIN64)
  289. " Cygwin64"
  290. # endif
  291. # if __GNU__
  292. " GNU"
  293. # endif
  294. # if __gnu_hurd__
  295. " Hurd"
  296. # endif
  297. # if __MACH__
  298. " Mach"
  299. # endif
  300. # if __linux__
  301. " Linux"
  302. # endif
  303. # if __APPLE__ && __MACH__
  304. " Darwin"
  305. # endif
  306. # if __minix__
  307. " Minix"
  308. # endif
  309. # if __QNX__
  310. " QNX"
  311. # endif
  312. # if __svr4__ || __SVR4
  313. " SYSV R4"
  314. # endif
  315. # if (defined(__sun__) || defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
  316. " Solaris"
  317. # endif
  318. # if (defined(__sun__) || defined(sun) || defined(__sun)) && !defined(__SVR4) && !defined(__svr4__)
  319. " SunOS"
  320. # endif
  321. # if defined(_WIN32) && !defined(_WIN64)
  322. " Windows32"
  323. # endif
  324. # if defined(_WIN32) && defined(_WIN64)
  325. " Windows64"
  326. # endif
  327. # if __MVS__ || __TOS_MVS__
  328. " z/OS"
  329. # endif
  330. # if defined(__GLIBC__) && !defined(__UCLIBC__)
  331. " glibc"
  332. # endif
  333. # if __UCLIBC__
  334. " uclibc"
  335. # endif
  336. # if defined(__linux__) && !defined(__GLIBC__) && !defined(__UCLIBC__) && !defined(__ANDROID__) && !defined(__BIONIC__)
  337. " musl"
  338. # endif
  339. //# if _MIPSEL || __MIPSEL__ || __ARMEL__ || __THUMBEL__
  340. // " little-endian"
  341. //# endif
  342. //
  343. //# if _MIPSEB || __MIPSEB__ || __ARMEB__ || __THUMBEB__
  344. // " big-endian"
  345. //# endif
  346. # if __PIE__ || __pie__
  347. " PIE"
  348. # endif
  349. ,
  350. *((uint8_t*)&testNumber) == 0x34 ? "little-endian" : "big-endian"
  351. );
  352. }
  353. void printCommonFlags()
  354. {
  355. printf
  356. (
  357. "Common flags:%s\n",""
  358. # ifdef NO_EXTENDED_PRODUCT_LIST
  359. " NO_EXTENDED_PRODUCT_LIST"
  360. # endif // NO_EXTENDED_PRODUCT_LIST
  361. # ifdef NO_BASIC_PRODUCT_LIST
  362. " NO_BASIC_PRODUCT_LIST"
  363. # endif // NO_BASIC_PRODUCT_LIST
  364. # ifdef USE_MSRPC
  365. " USE_MSRPC"
  366. # endif // USE_MSRPC
  367. # ifdef _CRYPTO_OPENSSL
  368. " _CRYPTO_OPENSSL"
  369. # endif // _CRYPTO_OPENSSL
  370. # ifdef _CRYPTO_POLARSSL
  371. " _CRYPTO_POLARSSL"
  372. # endif // _CRYPTO_POLARSSL
  373. # ifdef _CRYPTO_WINDOWS
  374. " _CRYPTO_WINDOWS"
  375. # endif // _CRYPTO_WINDOWS
  376. # if defined(_OPENSSL_SOFTWARE) && defined(_CRYPTO_OPENSSL)
  377. " _OPENSSL_SOFTWARE"
  378. # endif // _OPENSSL_SOFTWARE
  379. # if defined(_USE_AES_FROM_OPENSSL) && defined(_CRYPTO_OPENSSL)
  380. " _USE_AES_FROM_OPENSSL"
  381. # endif // _USE_AES_FROM_OPENSSL
  382. # if defined(_OPENSSL_NO_HMAC) && defined(_CRYPTO_OPENSSL)
  383. " OPENSSL_HMAC=0"
  384. # endif // _OPENSSL_NO_HMAC
  385. # ifdef _PEDANTIC
  386. " _PEDANTIC"
  387. # endif // _PEDANTIC
  388. # ifdef INCLUDE_BETAS
  389. " INCLUDE_BETAS"
  390. # endif // INCLUDE_BETAS
  391. # if __minix__ || defined(NO_TIMEOUT)
  392. " NO_TIMEOUT=1"
  393. # endif // __minix__ || defined(NO_TIMEOUT)
  394. );
  395. }
  396. void printClientFlags()
  397. {
  398. printf
  399. (
  400. "vlmcs flags:%s\n",""
  401. # ifdef NO_DNS
  402. " NO_DNS=1"
  403. # endif
  404. # if !defined(NO_DNS)
  405. # if defined(DNS_PARSER_INTERNAL) && !defined(_WIN32)
  406. " DNS_PARSER=internal"
  407. # else // !defined(DNS_PARSER_INTERNAL) || defined(_WIN32)
  408. " DNS_PARSER=OS"
  409. # endif // !defined(DNS_PARSER_INTERNAL) || defined(_WIN32)
  410. # endif // !defined(NO_DNS)
  411. # if defined(DISPLAY_WIDTH)
  412. " TERMINAL_WIDTH=" DISPLAY_WIDTH
  413. # endif
  414. );
  415. }
  416. void printServerFlags()
  417. {
  418. printf
  419. (
  420. "vlmcsd flags:%s\n",""
  421. # ifdef NO_LOG
  422. " NO_LOG"
  423. # endif // NO_LOG
  424. # ifdef NO_RANDOM_EPID
  425. " NO_RANDOM_EPID"
  426. # endif // NO_RANDOM_EPID
  427. # ifdef NO_INI_FILE
  428. " NO_INI_FILE"
  429. # endif // NO_INI_FILE
  430. # if !defined(NO_INI_FILE) && defined(INI_FILE)
  431. " INI=" INI_FILE
  432. # endif // !defined(NO_INI_FILE)
  433. # ifdef NO_PID_FILE
  434. " NO_PID_FILE"
  435. # endif // NO_PID_FILE
  436. # ifdef NO_USER_SWITCH
  437. " NO_USER_SWITCH"
  438. # endif // NO_USER_SWITCH
  439. # ifdef NO_HELP
  440. " NO_HELP"
  441. # endif // NO_HELP
  442. # ifdef NO_CUSTOM_INTERVALS
  443. " NO_CUSTOM_INTERVALS"
  444. # endif // NO_CUSTOM_INTERVALS
  445. # ifdef NO_SOCKETS
  446. " NO_SOCKETS"
  447. # endif // NO_SOCKETS
  448. # ifdef NO_CL_PIDS
  449. " NO_CL_PIDS"
  450. # endif // NO_CL_PIDS
  451. # ifdef NO_LIMIT
  452. " NO_LIMIT"
  453. # endif // NO_LIMIT
  454. # ifdef NO_SIGHUP
  455. " NO_SIGHUP"
  456. # endif // NO_SIGHUP
  457. # ifdef NO_PROCFS
  458. " NOPROCFS=1"
  459. # endif // NO_PROCFS
  460. # ifdef USE_THREADS
  461. " THREADS=1"
  462. # endif // USE_THREADS
  463. # ifdef USE_AUXV
  464. " AUXV=1"
  465. # endif // USE_AUXV
  466. # if defined(CHILD_HANDLER) || __minix__
  467. " CHILD_HANDLER=1"
  468. # endif // defined(CHILD_HANDLER) || __minix__
  469. # if !defined(NO_SOCKETS) && defined(SIMPLE_SOCKETS)
  470. " SIMPLE_SOCKETS"
  471. # endif // !defined(NO_SOCKETS) && defined(SIMPLE_SOCKETS)
  472. # if (_WIN32 || __CYGWIN__) && (!defined(USE_MSRPC) || defined(SUPPORT_WINE))
  473. " SUPPORT_WINE"
  474. # endif // (_WIN32 || __CYGWIN__) && (!defined(USE_MSRPC) || defined(SUPPORT_WINE))
  475. # if !HAVE_FREEBIND
  476. " NO_FREEBIND"
  477. # endif //!HAVE_FREEBIND
  478. # if !HAVE_GETIFADDR
  479. " !HAVE_GETIFADDR"
  480. # endif // !HAVE_GETIFADDR
  481. # if HAVE_GETIFADDR && defined(GETIFADDRS_MUSL)
  482. " GETIFADDRS=musl"
  483. # endif // HAVE_GETIFADDR && defined(GETIFADDRS_MUSL)
  484. # if defined(NO_PRIVATE_IP_DETECT)
  485. " NO_PRIVATE_IP_DETECT"
  486. # endif // defined(NO_PRIVATE_IP_DETECT)
  487. );
  488. }
  489. #endif // NO_VERSION_INFORMATION