Source code of Windows XP (NT5)
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.

422 lines
8.6 KiB

  1. /*--
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. nlmon.h
  5. Abstract:
  6. Trusted Domain monitor program.
  7. Author:
  8. 10-May-1993 (madana)
  9. Environment:
  10. User mode only.
  11. Contains NT-specific code.
  12. Requires ANSI C extensions: slash-slash comments, long external names.
  13. Revision History:
  14. --*/
  15. #include <nt.h>
  16. #include <ntlsa.h>
  17. #include <ntsam.h>
  18. #include <ntrtl.h>
  19. #include <nturtl.h>
  20. #include <windef.h>
  21. #include <winbase.h>
  22. #include <stdio.h>
  23. #include <conio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <lmcons.h>
  27. #include <lmerr.h>
  28. #include <lmwksta.h>
  29. #include <lmserver.h>
  30. #include <lmaccess.h>
  31. #include <lmapibuf.h>
  32. #include <netlib.h>
  33. #include <netlibnt.h>
  34. #include <icanon.h>
  35. #include <netlogon.h>
  36. #include <netdebug.h>
  37. #include <logonp.h>
  38. #ifdef GLOBAL_DEF
  39. #define EXTERN
  40. #else
  41. #define EXTERN extern
  42. #endif
  43. #ifdef DBG
  44. #define NlMonDbgPrint(_x_) printf _x_
  45. #else
  46. #define NlMonDbgPrint(_x_)
  47. #endif
  48. #define DOMAIN_PARAM "/DOMAINLIST:"
  49. #define MONTRUST_PARAM "/MONTRUST:"
  50. #define UPDATE_PARAM "/UPDATE:"
  51. #define DEBUG_PARAM "/DEBUG:"
  52. #define YES_PARAM "YES"
  53. #define NO_PARAM "NO"
  54. #define DEBUG_MONITOR 0x00000001
  55. #define DEBUG_INIT 0x00000002
  56. #define DEBUG_UPDATE 0x00000004
  57. #define DEBUG_TRUST 0x00000008
  58. #define DEBUG_VERBOSE 0x80000000
  59. #define IF_DEBUG(Function) if (GlobalTrace & DEBUG_ ## Function)
  60. #define INIT_OBJ_ATTR(Obj) \
  61. InitializeObjectAttributes( &(Obj), NULL, 0, NULL, NULL )
  62. #define SERVERS_GROUP L"SERVERS"
  63. #define DCSTATE_ONLINE L"DCOnline"
  64. #define DCSTATE_OFFLINE L"DCOffLine"
  65. #define TYPE_NTPDC L"NTPDC"
  66. #define TYPE_NTBDC L"NTBDC"
  67. #define TYPE_LMBDC L"LMBDC"
  68. #define REPL_STATE_SYNC L"InSync"
  69. #define REPL_STATE_REQ L"ReplRequired"
  70. #define REPL_STATE_PROGRESS L"InProgress"
  71. #define UNKNOWN L"Unknown"
  72. #define CONST_GLOBALTRACE 0x00000000
  73. #define CONST_GLOBALMONITORTRUST FALSE
  74. #define CONST_GLOBALUPDATETIME 15
  75. #define RETRY_COUNT 5
  76. #define UNKNOWN_REPLICATION_STATE 0x80000000
  77. //
  78. // update flags.
  79. //
  80. #define UPDATE_DCS_FROM_SERVER_ENUM 0x00000001
  81. #define UPDATE_DCS_FROM_DATABASE 0x00000002
  82. #define UPDATE_TRUST_DOMAINS_FROM_DATABASE 0x00000004
  83. #define UPDATE_TRUST_DCS_FROM_SERVER_ENUM 0x00000008
  84. #define UPDATE_TRUST_DCS_FROM_DATABASE 0x00000010
  85. #define VALIDATE_DCS 0x00000020
  86. #define VALIDATE_TRUST_DCS 0x00000040
  87. #define VALIDATE_TRUST_CONNECTIONS 0x00000080
  88. #define STANDARD_UPDATE \
  89. UPDATE_DCS_FROM_SERVER_ENUM | \
  90. VALIDATE_DCS | \
  91. UPDATE_TRUST_DCS_FROM_SERVER_ENUM | \
  92. VALIDATE_TRUST_DCS \
  93. #define UPDATE_FROM_DATABASE \
  94. STANDARD_UPDATE | \
  95. UPDATE_DCS_FROM_DATABASE | \
  96. UPDATE_TRUST_DCS_FROM_DATABASE
  97. #define UPDATE_TRUST_CONNECTIONS_STATUS \
  98. STANDARD_UPDATE | \
  99. VALIDATE_TRUST_CONNECTIONS
  100. #define UPDATE_ALL \
  101. STANDARD_UPDATE | \
  102. UPDATE_FROM_DATABASE | \
  103. UPDATE_TRUST_CONNECTIONS_STATUS | \
  104. UPDATE_TRUST_DOMAINS_FROM_DATABASE
  105. #define DOMAINLINE "............................................................................"
  106. #define SESSLINE "****************************************************************************"
  107. #define THREAD_STACKSIZE 1024 * 16 // 16K.
  108. #define MACHINES_PER_PASS 250 // SAM machine acct. query size.
  109. #define THREAD_WAIT_TIME 1 * 60 * 1000 // 1 min.
  110. typedef enum _DC_STATE {
  111. DCOnLine, // DC currently on running.
  112. DCOffLine // DC currently down.
  113. } DC_STATE;
  114. typedef enum _DC_TYPE {
  115. NTPDC, // NT Primary DC.
  116. NTBDC, // NT Backup DC.
  117. LMBDC, // Downlevel Backup DC.
  118. } DC_TYPE;
  119. typedef enum _DOMAIN_STATE {
  120. DomainSuccess,
  121. DomainProblem,
  122. DomainSick,
  123. DomainDown,
  124. DomainUnknown,
  125. } DOMAIN_STATE;
  126. //
  127. // generic entry
  128. //
  129. typedef struct _ENTRY {
  130. LIST_ENTRY NextEntry;
  131. UNICODE_STRING Name;
  132. } ENTRY, *PENTRY;
  133. //
  134. // DC entry.
  135. //
  136. typedef struct _DC_ENTRY {
  137. LIST_ENTRY NextEntry; // don't move this field
  138. UNICODE_STRING DCName; // don't move this field
  139. DC_STATE State;
  140. DC_TYPE Type;
  141. DWORD DCStatus; // DC Status
  142. DWORD ReplicationStatus; // boolean flag bit array.
  143. DWORD PDCLinkStatus; // To its PDC link status.
  144. LIST_ENTRY TrustedDCs;
  145. BOOL TDCLinkState; // health of the trust connections.
  146. DWORD RetryCount;
  147. BOOL DeleteFlag;
  148. } DC_ENTRY, *PDC_ENTRY;
  149. //
  150. // Trust Link entry.
  151. //
  152. typedef struct _TD_LINK {
  153. LIST_ENTRY NextEntry; // don't move this field
  154. UNICODE_STRING TDName; // don't move this field
  155. UNICODE_STRING DCName;
  156. DWORD SecureChannelStatus;
  157. BOOL DeleteFlag;
  158. } TD_LINK, *PTD_LINK;
  159. typedef struct _DOMAIN_ENTRY {
  160. LIST_ENTRY NextEntry; // don't move this field
  161. UNICODE_STRING Name; // don't move this field
  162. LIST_ENTRY DCList;
  163. LIST_ENTRY TrustedDomainList;
  164. DOMAIN_STATE DomainState;
  165. LONG ReferenceCount;
  166. BOOL IsMonitoredDomain;
  167. DWORD UpdateFlags;
  168. HANDLE ThreadHandle;
  169. BOOL ThreadTerminateFlag;
  170. DWORD LastUpdateTime;
  171. } DOMAIN_ENTRY, *PDOMAIN_ENTRY;
  172. typedef struct _DOMAIN_PRIVATE_ENTRY{
  173. LIST_ENTRY NextEntry; // don't move this field
  174. UNICODE_STRING Name; // don't move this field
  175. PDOMAIN_ENTRY DomainEntry;
  176. BOOL DeleteFlag;
  177. } MONITORED_DOMAIN_ENTRY, *PMONITORED_DOMAIN_ENTRY,
  178. TRUSTED_DOMAIN_ENTRY, *PTRUSTED_DOMAIN_ENTRY;
  179. //
  180. // Global variables
  181. //
  182. EXTERN DWORD GlobalTrace;
  183. EXTERN BOOL GlobalMonitorTrust;
  184. EXTERN DWORD GlobalUpdateTimeMSec; // UpdateTime in micro secs.
  185. EXTERN LIST_ENTRY GlobalDomains;
  186. EXTERN LIST_ENTRY GlobalDomainsMonitored;
  187. EXTERN CRITICAL_SECTION GlobalListCritSect;
  188. EXTERN CRITICAL_SECTION GlobalDomainUpdateThreadCritSect;
  189. EXTERN HANDLE GlobalWorkerThreadHandle;
  190. EXTERN HANDLE GlobalCmdProcessThreadHandle;
  191. EXTERN HANDLE GlobalTerminateEvent;
  192. EXTERN BOOL GlobalTerminateFlag;
  193. EXTERN HANDLE GlobalUpdateEvent;
  194. EXTERN BOOL GlobalInitialized;
  195. EXTERN HANDLE GlobalRefreshEvent;
  196. EXTERN HANDLE GlobalRefreshDoneEvent;
  197. //
  198. // This lock is a very simple lock. The list is updated (ie,
  199. // add/delete/update an entry) after locking it using this lock.
  200. // The reader of the list may lock the list if they don't want it
  201. // updated while reading.
  202. //
  203. #define LOCK_LISTS() EnterCriticalSection( &GlobalListCritSect )
  204. #define UNLOCK_LISTS() LeaveCriticalSection( &GlobalListCritSect )
  205. //
  206. // proto types.
  207. //
  208. VOID
  209. DomainUpdateThread(
  210. PDOMAIN_ENTRY DomainEntry
  211. );
  212. BOOL
  213. StartDomainUpdateThread(
  214. PDOMAIN_ENTRY DomainEntry,
  215. DWORD UpdateFlags
  216. );
  217. BOOL
  218. IsDomainUpdateThreadRunning(
  219. HANDLE *ThreadHandle
  220. );
  221. VOID
  222. StopDomainUpdateThread(
  223. HANDLE *ThreadHandle,
  224. BOOL *ThreadTerminateFlag
  225. );
  226. PMONITORED_DOMAIN_ENTRY
  227. AddToMonitoredDomainList(
  228. PUNICODE_STRING DomainName
  229. );
  230. PTRUSTED_DOMAIN_ENTRY
  231. AddToTrustedDomainList(
  232. PLIST_ENTRY List,
  233. PUNICODE_STRING DomainName
  234. );
  235. NTSTATUS
  236. QueryLsaInfo(
  237. PUNICODE_STRING ServerName,
  238. ACCESS_MASK DesiredAccess,
  239. POLICY_INFORMATION_CLASS InformationClass,
  240. PVOID *Info,
  241. PLSA_HANDLE ReturnHandle //optional
  242. );
  243. VOID
  244. CleanupLists(
  245. VOID
  246. );
  247. DWORD
  248. InitGlobals(
  249. VOID
  250. );
  251. VOID
  252. UpdateAndValidateDomain(
  253. PDOMAIN_ENTRY DomainEntry,
  254. DWORD UpdateFlags
  255. );
  256. VOID
  257. UpdateAndValidateLists(
  258. DWORD UpdateFlags,
  259. BOOL ForceFlag
  260. );
  261. VOID
  262. WorkerThread(
  263. VOID
  264. );
  265. DWORD
  266. StartMonitor(
  267. LPWSTR DomainList,
  268. DWORD interval,
  269. BOOL MonitorTD
  270. );
  271. DOMAIN_STATE
  272. QueryHealth(
  273. const LPWSTR DomainName
  274. );
  275. VOID
  276. StopMonitor(
  277. VOID
  278. );
  279. LPWSTR
  280. QueryPDC(
  281. const LPWSTR DomainName
  282. );
  283. PLIST_ENTRY
  284. QueryTrustedDomain(
  285. const LPWSTR DomainName
  286. );
  287. PLIST_ENTRY
  288. QueryDCList(
  289. const LPWSTR DomainName
  290. );
  291. PLIST_ENTRY
  292. QueryTDLink(
  293. const LPWSTR DomainName,
  294. const LPWSTR DCName
  295. );
  296. PLIST_ENTRY
  297. QueryTDCList(
  298. const LPWSTR DomainName,
  299. const LPWSTR TrustedDomainName);
  300. DWORD
  301. DisConnect(
  302. const LPWSTR DomainName,
  303. const LPWSTR DCName,
  304. const LPWSTR TrustedDomainName
  305. );
  306. VOID
  307. AddDomainToList(
  308. const LPWSTR DomainName
  309. );
  310. VOID
  311. RemoveDomainFromList(
  312. const LPWSTR DomainName
  313. );
  314. BOOL
  315. InitDomainListW(
  316. LPWSTR DomainList
  317. );
  318. PLIST_ENTRY
  319. FindNamedEntry(
  320. PLIST_ENTRY List,
  321. PUNICODE_STRING Name
  322. );
  323. VOID
  324. CleanupDomainEntry(
  325. PDOMAIN_ENTRY DomainEntry
  326. );
  327. VOID
  328. UpdateDomainState(
  329. PDOMAIN_ENTRY DomainEntry
  330. );
  331. NET_API_STATUS
  332. IsValidNTDC(
  333. PUNICODE_STRING ServerName,
  334. PUNICODE_STRING DomainName
  335. );