output.c 13 KB

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