output.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #if !defined(NO_VERBOSE_LOG) && !defined(NO_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. productName = getProductNameLE(&Request->KMSID, ProductList, &index);
  155. p("KMS ID (aka KMS counted ID) : %s (%s)\n", guidBuffer, productName);
  156. uuid2StringLE(&Request->CMID, guidBuffer);
  157. p("Client machine ID : %s\n", guidBuffer);
  158. uuid2StringLE(&Request->CMID_prev, guidBuffer);
  159. p("Previous client machine ID : %s\n", guidBuffer);
  160. char mbstr[64];
  161. time_t st;
  162. st = fileTimeToUnixTime(&Request->ClientTime);
  163. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", gmtime(&st));
  164. p("Client request timestamp (UTC) : %s\n", mbstr);
  165. ucs2_to_utf8(Request->WorkstationName, WorkstationBuffer, WORKSTATION_NAME_BUFFER, sizeof(WorkstationBuffer));
  166. p("Workstation name : %s\n", WorkstationBuffer);
  167. p("N count policy (minimum clients): %u\n", (uint32_t)LE32(Request->N_Policy));
  168. }
  169. void logResponseVerbose(const char *const ePID, const BYTE *const hwid, const RESPONSE *const response, const PRINTFUNC p)
  170. {
  171. char guidBuffer[GUID_STRING_LENGTH + 1];
  172. //SYSTEMTIME st;
  173. p("Protocol version : %u.%u\n", (uint32_t)LE16(response->MajorVer), (uint32_t)LE16(response->MinorVer));
  174. p("KMS host extended PID : %s\n", ePID);
  175. if (LE16(response->MajorVer) > 5)
  176. # ifndef _WIN32
  177. p("KMS host Hardware ID : %016llX\n", (unsigned long long)BE64(*(uint64_t*)hwid));
  178. # else // _WIN32
  179. p("KMS host Hardware ID : %016I64X\n", (unsigned long long)BE64(*(uint64_t*)hwid));
  180. # endif // WIN32
  181. uuid2StringLE(&response->CMID, guidBuffer);
  182. p("Client machine ID : %s\n", guidBuffer);
  183. char mbstr[64];
  184. time_t st;
  185. st = fileTimeToUnixTime(&response->ClientTime);
  186. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", gmtime(&st));
  187. p("Client request timestamp (UTC) : %s\n", mbstr);
  188. p("KMS host current active clients : %u\n", (uint32_t)LE32(response->Count));
  189. p("Renewal interval policy : %u\n", (uint32_t)LE32(response->VLRenewalInterval));
  190. p("Activation interval policy : %u\n", (uint32_t)LE32(response->VLActivationInterval));
  191. }
  192. #endif // !defined(NO_VERBOSE_LOG) && !defined(NO_LOG)
  193. #ifndef NO_VERSION_INFORMATION
  194. void printPlatform()
  195. {
  196. int testNumber = 0x1234;
  197. # ifdef VLMCSD_COMPILER
  198. printf
  199. (
  200. "Compiler: %s\n", VLMCSD_COMPILER
  201. # ifdef __VERSION__
  202. " " __VERSION__
  203. # endif // __VERSION__
  204. );
  205. # endif // VLMCSD_COMPILER
  206. printf
  207. (
  208. "Intended platform:%s %s\n", ""
  209. # if __i386__ || _M_IX86
  210. " Intel x86"
  211. # endif
  212. # if __x86_64__ || __amd64__ || _M_X64 || _M_AMD64
  213. " Intel x86_64"
  214. # endif
  215. # if _M_ARM || __arm__
  216. " ARM"
  217. # endif
  218. # if __thumb__
  219. " thumb"
  220. # endif
  221. # if __aarch64__
  222. " ARM64"
  223. # endif
  224. # if __hppa__
  225. " HP/PA RISC"
  226. # endif
  227. # if __ia64__
  228. " Intel Itanium"
  229. # endif
  230. # if __mips__
  231. " MIPS"
  232. # endif
  233. # if defined(_MIPS_ARCH)
  234. " " _MIPS_ARCH
  235. # endif
  236. # if __mips16
  237. " mips16"
  238. # endif
  239. # if __mips_micromips
  240. " micromips"
  241. # endif
  242. # if __ppc__ || __powerpc__
  243. " PowerPC"
  244. # endif
  245. # if __powerpc64__ || __ppc64__
  246. " PowerPC64"
  247. # endif
  248. # if __sparc__
  249. " SPARC"
  250. # endif
  251. # if defined(__s390__) && !defined(__zarch__) && !defined(__s390x__)
  252. " IBM S/390"
  253. # endif
  254. # if __zarch__ || __s390x__
  255. " IBM z/Arch (S/390x)"
  256. # endif
  257. # if __m68k__
  258. " Motorola 68k"
  259. # endif
  260. # if __ANDROID__
  261. " Android"
  262. # endif
  263. # if __ANDROID_API__
  264. " (API level " ANDROID_API_LEVEL ")"
  265. # endif
  266. # if __FreeBSD__ || __FreeBSD_kernel__
  267. " FreeBSD"
  268. # endif
  269. # if __NetBSD__
  270. " NetBSD"
  271. # endif
  272. # if __OpenBSD__
  273. " OpenBSD"
  274. # endif
  275. # if __DragonFly__
  276. " DragonFly BSD"
  277. # endif
  278. # if defined(__CYGWIN__) && !defined(_WIN64)
  279. " Cygwin32"
  280. # endif
  281. # if defined(__CYGWIN__) && defined(_WIN64)
  282. " Cygwin64"
  283. # endif
  284. # if __GNU__
  285. " GNU"
  286. # endif
  287. # if __gnu_hurd__
  288. " Hurd"
  289. # endif
  290. # if __MACH__
  291. " Mach"
  292. # endif
  293. # if __linux__
  294. " Linux"
  295. # endif
  296. # if __APPLE__ && __MACH__
  297. " Darwin"
  298. # endif
  299. # if __minix__
  300. " Minix"
  301. # endif
  302. # if __QNX__
  303. " QNX"
  304. # endif
  305. # if __svr4__ || __SVR4
  306. " SYSV R4"
  307. # endif
  308. # if (defined(__sun__) || defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
  309. " Solaris"
  310. # endif
  311. # if (defined(__sun__) || defined(sun) || defined(__sun)) && !defined(__SVR4) && !defined(__svr4__)
  312. " SunOS"
  313. # endif
  314. # if defined(_WIN32) && !defined(_WIN64)
  315. " Windows32"
  316. # endif
  317. # if defined(_WIN32) && defined(_WIN64)
  318. " Windows64"
  319. # endif
  320. # if __MVS__ || __TOS_MVS__
  321. " z/OS"
  322. # endif
  323. # if defined(__GLIBC__) && !defined(__UCLIBC__)
  324. " glibc"
  325. # endif
  326. # if __UCLIBC__
  327. " uclibc"
  328. # endif
  329. # if defined(__linux__) && !defined(__GLIBC__) && !defined(__UCLIBC__) && !defined(__ANDROID__) && !defined(__BIONIC__)
  330. " musl"
  331. # endif
  332. //# if _MIPSEL || __MIPSEL__ || __ARMEL__ || __THUMBEL__
  333. // " little-endian"
  334. //# endif
  335. //
  336. //# if _MIPSEB || __MIPSEB__ || __ARMEB__ || __THUMBEB__
  337. // " big-endian"
  338. //# endif
  339. # if __PIE__ || __pie__
  340. " PIE"
  341. # endif
  342. ,
  343. *((uint8_t*)&testNumber) == 0x34 ? "little-endian" : "big-endian"
  344. );
  345. }
  346. void printCommonFlags()
  347. {
  348. printf
  349. (
  350. "Common flags:%s\n",""
  351. # ifdef NO_EXTENDED_PRODUCT_LIST
  352. " NO_EXTENDED_PRODUCT_LIST"
  353. # endif // NO_EXTENDED_PRODUCT_LIST
  354. # ifdef NO_BASIC_PRODUCT_LIST
  355. " NO_BASIC_PRODUCT_LIST"
  356. # endif // NO_BASIC_PRODUCT_LIST
  357. # ifdef USE_MSRPC
  358. " USE_MSRPC"
  359. # endif // USE_MSRPC
  360. # ifdef _CRYPTO_OPENSSL
  361. " _CRYPTO_OPENSSL"
  362. # endif // _CRYPTO_OPENSSL
  363. # ifdef _CRYPTO_POLARSSL
  364. " _CRYPTO_POLARSSL"
  365. # endif // _CRYPTO_POLARSSL
  366. # ifdef _CRYPTO_WINDOWS
  367. " _CRYPTO_WINDOWS"
  368. # endif // _CRYPTO_WINDOWS
  369. # if defined(_OPENSSL_SOFTWARE) && defined(_CRYPTO_OPENSSL)
  370. " _OPENSSL_SOFTWARE"
  371. # endif // _OPENSSL_SOFTWARE
  372. # if defined(_USE_AES_FROM_OPENSSL) && defined(_CRYPTO_OPENSSL)
  373. " _USE_AES_FROM_OPENSSL"
  374. # endif // _USE_AES_FROM_OPENSSL
  375. # if defined(_OPENSSL_NO_HMAC) && defined(_CRYPTO_OPENSSL)
  376. " OPENSSL_HMAC=0"
  377. # endif // _OPENSSL_NO_HMAC
  378. # ifdef _PEDANTIC
  379. " _PEDANTIC"
  380. # endif // _PEDANTIC
  381. # ifdef INCLUDE_BETAS
  382. " INCLUDE_BETAS"
  383. # endif // INCLUDE_BETAS
  384. # if __minix__ || defined(NO_TIMEOUT)
  385. " NO_TIMEOUT=1"
  386. # endif // __minix__ || defined(NO_TIMEOUT)
  387. );
  388. }
  389. void printClientFlags()
  390. {
  391. printf
  392. (
  393. "vlmcs flags:%s\n",""
  394. # ifdef NO_DNS
  395. " NO_DNS=1"
  396. # endif
  397. # if !defined(NO_DNS)
  398. # if defined(DNS_PARSER_INTERNAL) && !defined(_WIN32)
  399. " DNS_PARSER=internal"
  400. # else // !defined(DNS_PARSER_INTERNAL) || defined(_WIN32)
  401. " DNS_PARSER=OS"
  402. # endif // !defined(DNS_PARSER_INTERNAL) || defined(_WIN32)
  403. # endif // !defined(NO_DNS)
  404. # if defined(DISPLAY_WIDTH)
  405. " TERMINAL_WIDTH=" DISPLAY_WIDTH
  406. # endif
  407. );
  408. }
  409. void printServerFlags()
  410. {
  411. printf
  412. (
  413. "vlmcsd flags:%s\n",""
  414. # ifdef NO_LOG
  415. " NO_LOG"
  416. # endif // NO_LOG
  417. # ifdef NO_RANDOM_EPID
  418. " NO_RANDOM_EPID"
  419. # endif // NO_RANDOM_EPID
  420. # ifdef NO_INI_FILE
  421. " NO_INI_FILE"
  422. # endif // NO_INI_FILE
  423. # if !defined(NO_INI_FILE) && defined(INI_FILE)
  424. " INI=" INI_FILE
  425. # endif // !defined(NO_INI_FILE)
  426. # ifdef NO_PID_FILE
  427. " NO_PID_FILE"
  428. # endif // NO_PID_FILE
  429. # ifdef NO_USER_SWITCH
  430. " NO_USER_SWITCH"
  431. # endif // NO_USER_SWITCH
  432. # ifdef NO_HELP
  433. " NO_HELP"
  434. # endif // NO_HELP
  435. # ifdef NO_CUSTOM_INTERVALS
  436. " NO_CUSTOM_INTERVALS"
  437. # endif // NO_CUSTOM_INTERVALS
  438. # ifdef NO_SOCKETS
  439. " NO_SOCKETS"
  440. # endif // NO_SOCKETS
  441. # ifdef NO_CL_PIDS
  442. " NO_CL_PIDS"
  443. # endif // NO_CL_PIDS
  444. # ifdef NO_LIMIT
  445. " NO_LIMIT"
  446. # endif // NO_LIMIT
  447. # ifdef NO_SIGHUP
  448. " NO_SIGHUP"
  449. # endif // NO_SIGHUP
  450. # ifdef NO_PROCFS
  451. " NOPROCFS=1"
  452. # endif // NO_PROCFS
  453. # ifdef USE_THREADS
  454. " THREADS=1"
  455. # endif // USE_THREADS
  456. # ifdef USE_AUXV
  457. " AUXV=1"
  458. # endif // USE_AUXV
  459. # if defined(CHILD_HANDLER) || __minix__
  460. " CHILD_HANDLER=1"
  461. # endif // defined(CHILD_HANDLER) || __minix__
  462. # if !defined(NO_SOCKETS) && defined(SIMPLE_SOCKETS)
  463. " SIMPLE_SOCKETS"
  464. # endif // !defined(NO_SOCKETS) && defined(SIMPLE_SOCKETS)
  465. # if (_WIN32 || __CYGWIN__) && (!defined(USE_MSRPC) || defined(SUPPORT_WINE))
  466. " SUPPORT_WINE"
  467. # endif // (_WIN32 || __CYGWIN__) && (!defined(USE_MSRPC) || defined(SUPPORT_WINE))
  468. # if !HAVE_FREEBIND
  469. " NO_FREEBIND"
  470. # endif //!HAVE_FREEBIND
  471. # if !HAVE_GETIFADDR
  472. " !HAVE_GETIFADDR"
  473. # endif // !HAVE_GETIFADDR
  474. # if HAVE_GETIFADDR && defined(GETIFADDRS_MUSL)
  475. " GETIFADDRS=musl"
  476. # endif // HAVE_GETIFADDR && defined(GETIFADDRS_MUSL)
  477. # if defined(NO_PRIVATE_IP_DETECT)
  478. " NO_PRIVATE_IP_DETECT"
  479. # endif // defined(NO_PRIVATE_IP_DETECT)
  480. );
  481. }
  482. #endif // NO_VERSION_INFORMATION