1 #ifndef _DEFAULT_SOURCE
2 #define _DEFAULT_SOURCE
3 #endif // _DEFAULT_SOURCE
6 #define CONFIG "config.h"
11 #include "shared_globals.h"
16 static void vlogger(const char *message
, va_list args
)
21 if (!IsNTService
&& logstdout
) log
= stdout
;
23 if (logstdout
) log
= stdout
;
27 if (fn_log
== NULL
) return;
30 if (!strcmp(fn_log
, "syslog"))
32 openlog("vlmcsd", LOG_CONS
| LOG_PID
, LOG_USER
);
34 ////PORTABILITY: vsyslog is not in Posix but virtually all Unixes have it
35 vsyslog(LOG_INFO
, message
, args
);
42 log
= fopen(fn_log
, "a");
54 strftime(mbstr
, sizeof(mbstr
), "%Y-%m-%d %X", localtime(&now
));
58 fprintf(log
, "%s: ", mbstr
);
59 vfprintf(log
, message
, args
);
64 // We write everything to a string before we really log inside the critical section
65 // so formatting the output can be concurrent
67 int len
= strlen(mbstr
);
68 vsnprintf(mbstr
+ len
, sizeof(mbstr
) - len
, message
, args
);
70 lock_mutex(&logmutex
);
73 unlock_mutex(&logmutex
);
76 if (log
!= stdout
) fclose(log
);
80 // Always sends to log output
81 int logger(const char *const fmt
, ...)
94 // Output to stderr if it is available or to log otherwise (e.g. if running as daemon/service)
95 void printerrorf(const char *const fmt
, ...)
99 va_start(arglist
, fmt
);
103 if (InetdMode
|| IsNTService
)
107 vlogger(fmt
, arglist
);
111 vfprintf(stderr
, fmt
, arglist
);
119 // Always output to stderr
120 int errorout(const char* fmt
, ...)
125 int i
= vfprintf(stderr
, fmt
, args
);
133 static const char *LicenseStatusText
[] =
135 "Unlicensed", "Licensed", "OOB grace", "OOT grace", "Non-Genuine", "Notification", "Extended grace"
139 void uuid2StringLE(const GUID
*const guid
, char *const string
)
143 "%08x-%04x-%04x-%04x-%012I64x",
145 "%08x-%04x-%04x-%04x-%012llx",
147 (unsigned int)LE32( guid
->Data1
),
148 (unsigned int)LE16( guid
->Data2
),
149 (unsigned int)LE16( guid
->Data3
),
150 (unsigned int)BE16( *(uint16_t*)guid
->Data4
),
151 (unsigned long long)BE64(*(uint64_t*)(guid
->Data4
)) & 0xffffffffffffLL
156 void logRequestVerbose(const REQUEST
*const Request
, const PRINTFUNC p
)
158 char guidBuffer
[GUID_STRING_LENGTH
+ 1];
159 char WorkstationBuffer
[3 * WORKSTATION_NAME_BUFFER
];
160 const char *productName
;
161 ProdListIndex_t index
;
163 p("Protocol version : %u.%u\n", LE16(Request
->MajorVer
), LE16(Request
->MinorVer
));
164 p("Client is a virtual machine : %s\n", LE32(Request
->VMInfo
) ? "Yes" : "No");
165 p("Licensing status : %u (%s)\n", (uint32_t)LE32(Request
->LicenseStatus
), LE32(Request
->LicenseStatus
) < _countof(LicenseStatusText
) ? LicenseStatusText
[LE32(Request
->LicenseStatus
)] : "Unknown");
166 p("Remaining time (0 = forever) : %i minutes\n", (uint32_t)LE32(Request
->BindingExpiration
));
168 uuid2StringLE(&Request
->AppID
, guidBuffer
);
169 productName
= getProductNameLE(&Request
->AppID
, AppList
, &index
);
170 p("Application ID : %s (%s)\n", guidBuffer
, productName
);
172 uuid2StringLE(&Request
->ActID
, guidBuffer
);
174 #ifndef NO_EXTENDED_PRODUCT_LIST
175 productName
= getProductNameLE(&Request
->ActID
, ExtendedProductList
, &index
);
177 productName
= "Unknown";
180 p("Activation ID (Product) : %s (%s)\n", guidBuffer
, productName
);
182 uuid2StringLE(&Request
->KMSID
, guidBuffer
);
184 #ifndef NO_BASIC_PRODUCT_LIST
185 productName
= getProductNameLE(&Request
->KMSID
, ProductList
, &index
);
187 productName
= "Unknown";
190 p("Key Management Service ID : %s (%s)\n", guidBuffer
, productName
);
192 uuid2StringLE(&Request
->CMID
, guidBuffer
);
193 p("Client machine ID : %s\n", guidBuffer
);
195 uuid2StringLE(&Request
->CMID_prev
, guidBuffer
);
196 p("Previous client machine ID : %s\n", guidBuffer
);
201 st
= fileTimeToUnixTime(&Request
->ClientTime
);
202 strftime(mbstr
, sizeof(mbstr
), "%Y-%m-%d %X", gmtime(&st
));
203 p("Client request timestamp (UTC) : %s\n", mbstr
);
205 ucs2_to_utf8(Request
->WorkstationName
, WorkstationBuffer
, WORKSTATION_NAME_BUFFER
, sizeof(WorkstationBuffer
));
207 p("Workstation name : %s\n", WorkstationBuffer
);
208 p("N count policy (minimum clients): %u\n", (uint32_t)LE32(Request
->N_Policy
));
212 void logResponseVerbose(const char *const ePID
, const BYTE
*const hwid
, const RESPONSE
*const response
, const PRINTFUNC p
)
214 char guidBuffer
[GUID_STRING_LENGTH
+ 1];
217 p("Protocol version : %u.%u\n", (uint32_t)LE16(response
->MajorVer
), (uint32_t)LE16(response
->MinorVer
));
218 p("KMS host extended PID : %s\n", ePID
);
219 if (LE16(response
->MajorVer
) > 5)
221 p("KMS host Hardware ID : %016llX\n", (unsigned long long)BE64(*(uint64_t*)hwid
));
223 p("KMS host Hardware ID : %016I64X\n", (unsigned long long)BE64(*(uint64_t*)hwid
));
226 uuid2StringLE(&response
->CMID
, guidBuffer
);
227 p("Client machine ID : %s\n", guidBuffer
);
232 st
= fileTimeToUnixTime(&response
->ClientTime
);
233 strftime(mbstr
, sizeof(mbstr
), "%Y-%m-%d %X", gmtime(&st
));
234 p("Client request timestamp (UTC) : %s\n", mbstr
);
236 p("KMS host current active clients : %u\n", (uint32_t)LE32(response
->Count
));
237 p("Renewal interval policy : %u\n", (uint32_t)LE32(response
->VLRenewalInterval
));
238 p("Activation interval policy : %u\n", (uint32_t)LE32(response
->VLActivationInterval
));