output.c 13 KB

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