Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

536 lines
15 KiB

  1. #include <precomp.h>
  2. #include "zcdblog.h"
  3. #include "wzcsvc.h"
  4. #include "intflist.h"
  5. #include "tracing.h"
  6. // internal-use tracing variables
  7. UINT g_nLineNo;
  8. LPSTR g_szFileName;
  9. // handles for database usage
  10. extern JET_SESID serverSession;
  11. extern JET_DBID databasehandle;
  12. extern JET_TABLEID tablehandle;
  13. extern SESSION_CONTAINER sesscon;
  14. // global buffers to be used when formatting database
  15. // logging message parameters
  16. WCHAR g_wszDbLogBuffer[DBLOG_SZFMT_BUFFS][DBLOG_SZFMT_SIZE];
  17. // global tracing variables
  18. DWORD g_TraceLog = 0;
  19. // global handle to the event log
  20. HANDLE g_hWzcEventLog = NULL;
  21. // debug utility calls
  22. VOID _DebugPrint(DWORD dwFlags, LPCSTR lpFormat, ...)
  23. {
  24. va_list arglist;
  25. va_start(arglist, lpFormat);
  26. TraceVprintfExA(
  27. g_TraceLog,
  28. dwFlags | TRACE_USE_MASK,
  29. lpFormat,
  30. arglist);
  31. }
  32. // if bCheck is not true, print out the assert message & user defined string.
  33. VOID _DebugAssert(BOOL bChecked, LPCSTR lpFormat, ...)
  34. {
  35. if (!bChecked)
  36. {
  37. va_list arglist;
  38. CHAR pBuffer[500];
  39. LPSTR pFileName;
  40. pFileName = strrchr(g_szFileName, '\\');
  41. if (pFileName == NULL)
  42. pFileName = g_szFileName;
  43. else
  44. pFileName++;
  45. sprintf(pBuffer,"##Assert %s:%d## ", pFileName, g_nLineNo);
  46. strcat(pBuffer, lpFormat);
  47. va_start(arglist, lpFormat);
  48. TraceVprintfExA(
  49. g_TraceLog,
  50. TRC_ASSERT | TRACE_USE_MASK,
  51. pBuffer,
  52. arglist);
  53. }
  54. }
  55. VOID _DebugBinary(DWORD dwFlags, LPCSTR lpMessage, LPBYTE pBuffer, UINT nBuffLen)
  56. {
  57. CHAR strHex[128];
  58. UINT nMsgLen = strlen(lpMessage);
  59. if (3*nBuffLen >= 120)
  60. {
  61. strcpy(strHex, "##Binary data too large##");
  62. }
  63. else
  64. {
  65. LPSTR pHexDigit = strHex;
  66. UINT i = nBuffLen;
  67. while(i > 0)
  68. {
  69. sprintf(pHexDigit, "%02x ", *pBuffer);
  70. pHexDigit += 3;
  71. pBuffer++;
  72. i--;
  73. }
  74. *pHexDigit = '\0';
  75. }
  76. TracePrintfExA(
  77. g_TraceLog,
  78. dwFlags | TRACE_USE_MASK,
  79. "%s [%d]:{%s}", lpMessage, nBuffLen, strHex);
  80. }
  81. VOID TrcInitialize()
  82. {
  83. #ifdef DBG
  84. g_TraceLog = TraceRegister(TRC_NAME);
  85. #endif
  86. }
  87. VOID TrcTerminate()
  88. {
  89. #ifdef DBG
  90. TraceDeregister(g_TraceLog);
  91. #endif
  92. }
  93. //------------- Event Logging functions -------------------
  94. VOID EvtInitialize()
  95. {
  96. g_hWzcEventLog = RouterLogRegisterW(L"WZCSVC");
  97. }
  98. VOID EvtTerminate()
  99. {
  100. if (g_hWzcEventLog != NULL)
  101. {
  102. RouterLogDeregisterW(g_hWzcEventLog);
  103. g_hWzcEventLog = NULL;
  104. }
  105. }
  106. VOID EvtLogWzcError(DWORD dwMsgId, DWORD dwErrCode)
  107. {
  108. if (g_hWzcEventLog != NULL)
  109. {
  110. RouterLogErrorW(
  111. g_hWzcEventLog,
  112. dwMsgId,
  113. 0, // 0 insertion strings
  114. NULL, // no insertion strings
  115. dwErrCode);
  116. }
  117. }
  118. //------------- Database Logging functions -------------------
  119. DWORD _DBRecord (
  120. DWORD eventID,
  121. PWZC_DB_RECORD pDbRecord,
  122. va_list *pvaList)
  123. {
  124. DWORD dwErr = ERROR_SUCCESS;
  125. WCHAR wchBuffer[MAX_RAW_DATA_SIZE/sizeof(WCHAR)];
  126. if (g_hInstance == NULL)
  127. dwErr = ERROR_DLL_INIT_FAILED;
  128. if (dwErr == ERROR_SUCCESS)
  129. {
  130. // format the message
  131. if (FormatMessageW(
  132. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_MAX_WIDTH_MASK,
  133. g_hInstance,
  134. eventID,
  135. 0, // Default language
  136. wchBuffer,
  137. sizeof(wchBuffer)/sizeof(WCHAR),
  138. pvaList) == 0)
  139. {
  140. dwErr = GetLastError();
  141. }
  142. }
  143. if (dwErr == ERROR_SUCCESS)
  144. {
  145. pDbRecord->message.pData = (LPBYTE)wchBuffer;
  146. pDbRecord->message.dwDataLen = sizeof(WCHAR)*(wcslen(wchBuffer) + 1);
  147. //make a insertion
  148. dwErr = AddWZCDbLogRecord(NULL, 0, pDbRecord, NULL);
  149. }
  150. return dwErr;
  151. }
  152. DWORD DbLogWzcError (
  153. DWORD eventID,
  154. PINTF_CONTEXT pIntfContext,
  155. ...
  156. )
  157. {
  158. DWORD dwErr = ERROR_SUCCESS;
  159. va_list argList;
  160. WZC_DB_RECORD DbRecord = {0};
  161. BOOL bLogEnabled;
  162. if (!g_wzcInternalCtxt.bValid)
  163. {
  164. dwErr = ERROR_ARENA_TRASHED;
  165. goto exit;
  166. }
  167. va_start(argList, pIntfContext);
  168. EnterCriticalSection(&g_wzcInternalCtxt.csContext);
  169. bLogEnabled = ((g_wzcInternalCtxt.wzcContext.dwFlags & WZC_CTXT_LOGGING_ON) != 0);
  170. LeaveCriticalSection(&g_wzcInternalCtxt.csContext);
  171. // If the database is not opened or the logging functionality is disabled,
  172. // do not record any thing
  173. if (!bLogEnabled || !IsDBOpened())
  174. goto exit;
  175. // prepare the info that is about to be logged.
  176. // get the service specific info first (i.e WZC specific part)
  177. // then build up the message.
  178. DbLogInitDbRecord(DBLOG_CATEG_ERR, pIntfContext, &DbRecord);
  179. dwErr = _DBRecord(
  180. eventID,
  181. &DbRecord,
  182. &argList);
  183. exit:
  184. return dwErr;
  185. }
  186. DWORD DbLogWzcInfo (
  187. DWORD eventID,
  188. PINTF_CONTEXT pIntfContext,
  189. ...
  190. )
  191. {
  192. DWORD dwErr = ERROR_SUCCESS;
  193. va_list argList;
  194. WZC_DB_RECORD DbRecord = {0};
  195. BOOL bLogEnabled;
  196. LPWSTR wszContext = NULL;
  197. DWORD nchContext = 0;
  198. if (!g_wzcInternalCtxt.bValid)
  199. {
  200. dwErr = ERROR_ARENA_TRASHED;
  201. goto exit;
  202. }
  203. va_start(argList, pIntfContext);
  204. EnterCriticalSection(&g_wzcInternalCtxt.csContext);
  205. bLogEnabled = ((g_wzcInternalCtxt.wzcContext.dwFlags & WZC_CTXT_LOGGING_ON) != 0);
  206. LeaveCriticalSection(&g_wzcInternalCtxt.csContext);
  207. // If the database is not opened or the logging functionality is disabled,
  208. // do not record any thing
  209. if (!bLogEnabled || !IsDBOpened())
  210. goto exit;
  211. // prepare the info that is about to be logged.
  212. // get the service specific info first (i.e WZC specific part)
  213. // then build up the message.
  214. DbLogInitDbRecord(DBLOG_CATEG_INFO, pIntfContext, &DbRecord);
  215. // if WZCSVC_USR_CFGCHANGE, build the context string
  216. if (eventID == WZCSVC_USR_CFGCHANGE &&
  217. pIntfContext != NULL)
  218. {
  219. DWORD nOffset = 0;
  220. DWORD nchWritten = 0;
  221. nchContext = 64; // large enough for "Flags = 0x00000000"
  222. if (pIntfContext->pwzcPList != NULL)
  223. {
  224. // large enough for "{SSID, Infrastructure, Flags}"
  225. nchContext += pIntfContext->pwzcPList->NumberOfItems * 128;
  226. }
  227. wszContext = (LPWSTR)MemCAlloc(nchContext * sizeof(WCHAR));
  228. if (wszContext == NULL)
  229. dwErr = GetLastError();
  230. if (dwErr == ERROR_SUCCESS)
  231. {
  232. nchWritten = nchContext;
  233. dwErr = DbLogFmtFlags(
  234. wszContext,
  235. &nchWritten,
  236. pIntfContext->dwCtlFlags);
  237. if (dwErr == ERROR_SUCCESS)
  238. nOffset += nchWritten;
  239. }
  240. if (dwErr == ERROR_SUCCESS && pIntfContext->pwzcPList != NULL)
  241. {
  242. UINT i;
  243. for (i = 0; i < pIntfContext->pwzcPList->NumberOfItems; i++)
  244. {
  245. nchWritten = nchContext - nOffset;
  246. dwErr = DbLogFmtWConfig(
  247. wszContext + nOffset,
  248. &nchWritten,
  249. &(pIntfContext->pwzcPList->Config[i]));
  250. if (dwErr != ERROR_SUCCESS)
  251. break;
  252. nOffset += nchWritten;
  253. }
  254. }
  255. if (dwErr == ERROR_SUCCESS)
  256. {
  257. DbRecord.context.pData = (LPBYTE)wszContext;
  258. DbRecord.context.dwDataLen = (wcslen(wszContext) + 1) * sizeof(WCHAR);
  259. }
  260. }
  261. else if (eventID == WZCSVC_BLIST_CHANGED &&
  262. pIntfContext != NULL &&
  263. pIntfContext->pwzcBList != NULL &&
  264. pIntfContext->pwzcBList->NumberOfItems != 0)
  265. {
  266. DWORD nOffset = 0;
  267. DWORD nchWritten = 0;
  268. nchContext = pIntfContext->pwzcBList->NumberOfItems * 128;
  269. wszContext = (LPWSTR)MemCAlloc(nchContext * sizeof(WCHAR));
  270. if (wszContext == NULL)
  271. dwErr = GetLastError();
  272. if (dwErr == ERROR_SUCCESS)
  273. {
  274. UINT i;
  275. for (i = 0; i < pIntfContext->pwzcBList->NumberOfItems; i++)
  276. {
  277. nchWritten = nchContext - nOffset;
  278. dwErr = DbLogFmtWConfig(
  279. wszContext + nOffset,
  280. &nchWritten,
  281. &(pIntfContext->pwzcBList->Config[i]));
  282. if (dwErr != ERROR_SUCCESS)
  283. break;
  284. nOffset += nchWritten;
  285. }
  286. }
  287. if (dwErr == ERROR_SUCCESS)
  288. {
  289. DbRecord.context.pData = (LPBYTE)wszContext;
  290. DbRecord.context.dwDataLen = (wcslen(wszContext) + 1) * sizeof(WCHAR);
  291. }
  292. }
  293. dwErr = _DBRecord(
  294. eventID,
  295. &DbRecord,
  296. &argList);
  297. exit:
  298. MemFree(wszContext);
  299. return dwErr;
  300. }
  301. // Initializes the WZC_DB_RECORD
  302. DWORD DbLogInitDbRecord(
  303. DWORD dwCategory,
  304. PINTF_CONTEXT pIntfContext,
  305. PWZC_DB_RECORD pDbRecord)
  306. {
  307. DWORD dwErr = ERROR_SUCCESS;
  308. if (pDbRecord == NULL)
  309. {
  310. dwErr = ERROR_INVALID_PARAMETER;
  311. }
  312. else
  313. {
  314. ZeroMemory(pDbRecord, sizeof(WZC_DB_RECORD));
  315. pDbRecord->componentid = DBLOG_COMPID_WZCSVC;
  316. pDbRecord->category = dwCategory;
  317. if (pIntfContext != NULL)
  318. {
  319. pDbRecord->ssid.pData = (LPBYTE)DbLogFmtSSID(5,&(pIntfContext->wzcCurrent.Ssid));
  320. pDbRecord->ssid.dwDataLen = sizeof(WCHAR)*(wcslen((LPWSTR)pDbRecord->ssid.pData) + 1);
  321. pDbRecord->localmac.pData = (LPBYTE)DbLogFmtBSSID(6, pIntfContext->ndLocalMac);
  322. pDbRecord->localmac.dwDataLen = sizeof(WCHAR)*(wcslen((LPWSTR)pDbRecord->localmac.pData) + 1);
  323. pDbRecord->remotemac.pData = (LPBYTE)DbLogFmtBSSID(7, pIntfContext->wzcCurrent.MacAddress);
  324. pDbRecord->remotemac.dwDataLen = sizeof(WCHAR)*(wcslen((LPWSTR)pDbRecord->remotemac.pData) + 1);
  325. }
  326. }
  327. return dwErr;
  328. }
  329. // Formats an SSID in the given formatting buffer
  330. LPWSTR DbLogFmtSSID(
  331. UINT nBuff, // index of the format buffer to use (0 .. DBLOG_SZFMT_BUFFS)
  332. PNDIS_802_11_SSID pndSSid)
  333. {
  334. UINT nFmtLen;
  335. DbgAssert((nBuff < DBLOG_SZFMT_SIZE, "Illegal buffer index in DbLogFmtSSID"));
  336. nFmtLen = MultiByteToWideChar(
  337. CP_ACP,
  338. 0,
  339. pndSSid->Ssid,
  340. min (pndSSid->SsidLength, DBLOG_SZFMT_SIZE-1),
  341. g_wszDbLogBuffer[nBuff],
  342. DBLOG_SZFMT_SIZE-1);
  343. if (nFmtLen == DBLOG_SZFMT_SIZE-1)
  344. wcscpy(&(g_wszDbLogBuffer[nBuff][DBLOG_SZFMT_SIZE-3]), L"..");
  345. else
  346. g_wszDbLogBuffer[nBuff][nFmtLen] = '\0';
  347. return g_wszDbLogBuffer[nBuff];
  348. }
  349. // Formats a BSSID (MAC address) in the given formatting buffer
  350. LPWSTR DbLogFmtBSSID(
  351. UINT nBuff,
  352. NDIS_802_11_MAC_ADDRESS ndBSSID)
  353. {
  354. UINT i, j;
  355. BOOL bAllZero = TRUE;
  356. DbgAssert((nBuff < DBLOG_SZFMT_SIZE, "Illegal buffer index in DbLogFmtSSID"));
  357. g_wszDbLogBuffer[nBuff][0] = L'\0';
  358. for (j = 0, i = 0; i < sizeof(NDIS_802_11_MAC_ADDRESS); i++)
  359. {
  360. BYTE nHex;
  361. if (ndBSSID[i] != 0)
  362. bAllZero = FALSE;
  363. nHex = (ndBSSID[i] & 0xf0) >> 4;
  364. g_wszDbLogBuffer[nBuff][j++] = HEX2WCHAR(nHex);
  365. nHex = (ndBSSID[i] & 0x0f);
  366. g_wszDbLogBuffer[nBuff][j++] = HEX2WCHAR(nHex);
  367. g_wszDbLogBuffer[nBuff][j++] = MAC_SEPARATOR;
  368. }
  369. if (bAllZero)
  370. g_wszDbLogBuffer[nBuff][0] = L'\0';
  371. else if (j > 0)
  372. g_wszDbLogBuffer[nBuff][j-1] = L'\0';
  373. return g_wszDbLogBuffer[nBuff];
  374. }
  375. // Formats the INTF_CONTEXT::dwCtlFlags field for logging
  376. DWORD DbLogFmtFlags(
  377. LPWSTR wszBuffer, // buffer to place the result into
  378. LPDWORD pnchBuffer, // in: num of chars in the buffer; out: number of chars written to the buffer
  379. DWORD dwFlags) // interface flags to log
  380. {
  381. DWORD dwErr = ERROR_SUCCESS;
  382. UINT nchBuffer;
  383. LPVOID pvArgs[5];
  384. WCHAR wszArgs[5][33];
  385. if (pnchBuffer == NULL || *pnchBuffer == 0)
  386. {
  387. dwErr = ERROR_INVALID_PARAMETER;
  388. goto exit;
  389. }
  390. nchBuffer = (*pnchBuffer);
  391. *pnchBuffer = 0;
  392. if (g_hInstance == NULL)
  393. {
  394. dwErr = ERROR_DLL_INIT_FAILED;
  395. goto exit;
  396. }
  397. pvArgs[0] = _itow((dwFlags & INTFCTL_ENABLED) != 0, wszArgs[0], 10);
  398. pvArgs[1] = _itow((dwFlags & INTFCTL_FALLBACK) != 0, wszArgs[1], 10);
  399. pvArgs[2] = _itow((dwFlags & INTFCTL_CM_MASK), wszArgs[2], 10);
  400. pvArgs[3] = _itow((dwFlags & INTFCTL_VOLATILE) != 0, wszArgs[3], 10);
  401. pvArgs[4] = _itow((dwFlags & INTFCTL_POLICY) != 0, wszArgs[4], 10);
  402. // format the message
  403. *pnchBuffer = FormatMessageW(
  404. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  405. g_hInstance,
  406. WZCSVC_DETAILS_FLAGS,
  407. 0, // Default language
  408. wszBuffer,
  409. nchBuffer,
  410. (va_list*)pvArgs);
  411. if (*pnchBuffer == 0)
  412. dwErr = GetLastError();
  413. exit:
  414. return dwErr;
  415. }
  416. // Formats a WZC_WLAN_CONFIG structure for logging
  417. DWORD DbLogFmtWConfig(
  418. LPWSTR wszBuffer, // buffer to place the result into
  419. LPDWORD pnchBuffer, // in: num of chars in the buffer; out: number of chars written to the buffer
  420. PWZC_WLAN_CONFIG pWzcCfg) // WZC_WLAN_CONFIG object to log
  421. {
  422. DWORD dwErr = ERROR_SUCCESS;
  423. UINT nchBuffer;
  424. LPVOID pvArgs[5];
  425. WCHAR wszArgs[4][33];
  426. if (pnchBuffer == NULL || *pnchBuffer == 0)
  427. {
  428. dwErr = ERROR_INVALID_PARAMETER;
  429. goto exit;
  430. }
  431. nchBuffer = (*pnchBuffer);
  432. *pnchBuffer = 0;
  433. if (g_hInstance == NULL)
  434. {
  435. dwErr = ERROR_DLL_INIT_FAILED;
  436. goto exit;
  437. }
  438. pvArgs[0] = (LPVOID)DbLogFmtSSID(8, &(pWzcCfg->Ssid));
  439. pvArgs[1] = _itow(pWzcCfg->InfrastructureMode, wszArgs[0], 10);
  440. pvArgs[2] = _itow(pWzcCfg->Privacy, wszArgs[1], 10);
  441. pvArgs[3] = _itow((pWzcCfg->dwCtlFlags & WZCCTL_VOLATILE) != 0, wszArgs[2], 10);
  442. pvArgs[4] = _itow((pWzcCfg->dwCtlFlags & WZCCTL_POLICY) != 0, wszArgs[3], 10);
  443. // format the message
  444. *pnchBuffer = FormatMessageW(
  445. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  446. g_hInstance,
  447. WZCSVC_DETAILS_WCONFIG,
  448. 0, // Default language
  449. wszBuffer,
  450. nchBuffer,
  451. (va_list*)pvArgs);
  452. if (*pnchBuffer == 0)
  453. dwErr = GetLastError();
  454. exit:
  455. return dwErr;
  456. }