output.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. strftime(mbstr, sizeof(mbstr), "%Y-%m-%d %X", localtime(&now));
  44. #ifndef USE_THREADS
  45. fprintf(log, "%s: ", mbstr);
  46. vfprintf(log, message, args);
  47. fflush(log);
  48. #else // USE_THREADS
  49. // We write everything to a string before we really log inside the critical section
  50. // so formatting the output can be concurrent
  51. strcat(mbstr, ": ");
  52. int len = strlen(mbstr);
  53. vsnprintf(mbstr + len, sizeof(mbstr) - len, message, args);
  54. lock_mutex(&logmutex);
  55. fputs(mbstr, log);
  56. fflush(log);
  57. unlock_mutex(&logmutex);
  58. #endif // USE_THREADS
  59. if (log != stdout) fclose(log);
  60. }
  61. // Always sends to log output
  62. int logger(const char *const fmt, ...)
  63. {
  64. va_list args;
  65. va_start(args, fmt);
  66. vlogger(fmt, args);
  67. va_end(args);
  68. return 0;
  69. }
  70. #endif //NO_LOG
  71. // Output to stderr if it is available or to log otherwise (e.g. if running as daemon/service)
  72. void printerrorf(const char *const fmt, ...)
  73. {
  74. int error = errno;
  75. va_list arglist;
  76. va_start(arglist, fmt);
  77. # ifdef IS_LIBRARY
  78. snprintf(ErrorMessage, MESSAGE_BUFFER_SIZE, fmt, arglist);
  79. # else // !IS_LIBRARY
  80. # ifndef NO_LOG
  81. # ifdef _NTSERVICE
  82. if (InetdMode || IsNTService)
  83. # else // !_NTSERVICE
  84. if (InetdMode)
  85. # endif // NTSERVIICE
  86. vlogger(fmt, arglist);
  87. else
  88. # endif //NO_LOG
  89. # endif // IS_LIBRARY
  90. {
  91. vfprintf(stderr, fmt, arglist);
  92. fflush(stderr);
  93. }
  94. va_end(arglist);
  95. errno = error;
  96. }
  97. // Always output to stderr
  98. int errorout(const char* fmt, ...)
  99. {
  100. va_list args;
  101. va_start(args, fmt);
  102. int i = vfprintf(stderr, fmt, args);
  103. va_end(args);
  104. fflush(stderr);
  105. return i;
  106. }
  107. #ifndef NO_VERBOSE_LOG
  108. static const char *LicenseStatusText[] =
  109. {
  110. "Unlicensed", "Licensed", "OOB grace", "OOT grace", "Non-Genuine", "Notification", "Extended grace"
  111. };
  112. #endif // NO_VERBOSE_LOG
  113. void uuid2StringLE(const GUID *const guid, char *const string)
  114. {
  115. sprintf(string,
  116. #ifdef _WIN32
  117. "%08x-%04x-%04x-%04x-%012I64x",
  118. #else
  119. "%08x-%04x-%04x-%04x-%012llx",
  120. #endif
  121. (unsigned int)LE32( guid->Data1 ),
  122. (unsigned int)LE16( guid->Data2 ),
  123. (unsigned int)LE16( guid->Data3 ),
  124. (unsigned int)BE16( *(uint16_t*)guid->Data4 ),
  125. (unsigned long long)BE64(*(uint64_t*)(guid->Data4)) & 0xffffffffffffLL
  126. );
  127. }
  128. #ifndef NO_VERBOSE_LOG
  129. void logRequestVerbose(const REQUEST *const Request, const PRINTFUNC p)
  130. {
  131. char guidBuffer[GUID_STRING_LENGTH + 1];
  132. char WorkstationBuffer[3 * WORKSTATION_NAME_BUFFER];
  133. const char *productName;
  134. ProdListIndex_t index;
  135. p("Protocol version : %u.%u\n", LE16(Request->MajorVer), LE16(Request->MinorVer));
  136. p("Client is a virtual machine : %s\n", LE32(Request->VMInfo) ? "Yes" : "No");
  137. p("Licensing status : %u (%s)\n", (uint32_t)LE32(Request->LicenseStatus), LE32(Request->LicenseStatus) < _countof(LicenseStatusText) ? LicenseStatusText[LE32(Request->LicenseStatus)] : "Unknown");
  138. p("Remaining time (0 = forever) : %i minutes\n", (uint32_t)LE32(Request->BindingExpiration));
  139. uuid2StringLE(&Request->AppID, guidBuffer);
  140. productName = getProductNameLE(&Request->AppID, AppList, &index);
  141. p("Application ID : %s (%s)\n", guidBuffer, productName);
  142. uuid2StringLE(&Request->ActID, guidBuffer);
  143. #ifndef NO_EXTENDED_PRODUCT_LIST
  144. productName = getProductNameLE(&Request->ActID, ExtendedProductList, &index);
  145. #else
  146. productName = "Unknown";
  147. #endif
  148. p("Activation ID (Product) : %s (%s)\n", guidBuffer, productName);
  149. uuid2StringLE(&Request->KMSID, guidBuffer);
  150. #ifndef NO_BASIC_PRODUCT_LIST
  151. productName = getProductNameLE(&Request->KMSID, ProductList, &index);
  152. #else
  153. productName = "Unknown";
  154. #endif
  155. p("Key Management Service 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 // NO_VERBOSE_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. );
  472. }
  473. #endif // NO_VERSION_INFORMATION