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.

695 lines
15 KiB

  1. /********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1987-1990 **/
  4. /********************************************************************/
  5. #ifndef _MSRV_INCLUDED
  6. #define _MSRV_INCLUDED
  7. #include <nt.h> // for ntrtl.h
  8. #include <ntrtl.h> // DbgPrint prototypes
  9. #include <nturtl.h> // needed for windows.h when I have nt.h
  10. #define WINMM_H
  11. #include <windows.h> // ExitThread prototype
  12. #include <lmcons.h>
  13. #include <lmerr.h>
  14. #include <nb30.h> // NetBios Prototypes and constants
  15. #include <winsta.h> // Winstation functions (for HYDRA)
  16. #include "heap.h"
  17. #ifdef LINT
  18. #define near
  19. #define far
  20. #define void int
  21. #endif // LINT
  22. #define clearncb(x) memset((char *)x,'\0',sizeof(NCB))
  23. //
  24. // Constant definitions
  25. //
  26. #define BUFLEN 200 // Length of NCB_BUF
  27. #define TXTMAX 128 // Maximum bytes of text per block
  28. #define MAXHEAD 80 // Maximum message header length
  29. #define MAXEND 60 // Maximum message end length
  30. #define MAXGRPMSGLEN 128 // Max domain message length
  31. #define MAX_SIZMESSBUF 62000 // The max size for the message buffer
  32. #define MIN_SIZMESSBUF 512 // The min size for the message buffer
  33. #define MSNGR_MAX_NETS MAX_LANA // The maximum number of nets the messenger
  34. // can handle. (Currently 12)
  35. #define TIME_BUF_SIZE 128 // Size of the buffer to hold the message time
  36. #define MSGFILENAMLEN PATHLEN*sizeof(TCHAR)
  37. //
  38. // Messaging name end bytes
  39. //
  40. #define NAME_LOCAL_END '\003' // 16th byte in local NCB name
  41. //
  42. // Messenger Thread Manager States (used as return codes)
  43. //
  44. #define UPDATE_ONLY 0 // no change in state - just send current status.
  45. #define STARTING 1 // the messenger is initializing.
  46. #define RUNNING 2 // initialization completed normally - now running
  47. #define STOPPING 3 // uninstall pending
  48. #define STOPPED 4 // uninstalled
  49. //
  50. // Forced Shutdown PendingCodes
  51. //
  52. #define PENDING TRUE
  53. #define IMMEDIATE FALSE
  54. //
  55. // Message transfer states
  56. //
  57. #define MESSTART 0 // Message start state
  58. #define MESSTOP 1 // Message stop state
  59. #define MESCONT 2 // Message continued state
  60. #define MESERR 3 // Message error state
  61. //
  62. // Alert Size
  63. //
  64. #define ALERT_MAX_DISPLAYED_MSG_SIZE 4096
  65. //
  66. // Special Session Id = -1 (used to indicate that the message has to be broadcast to every session)
  67. //
  68. #define EVERYBODY_SESSION_ID -1
  69. // Structure definitions
  70. //
  71. // ncb worker function type
  72. //
  73. typedef VOID (*PNCBIFCN) (
  74. DWORD NetIndex, // Network Index
  75. DWORD NcbIndex, // Network Control Block Index
  76. CHAR RetVal // value returned by net bios
  77. );
  78. typedef PNCBIFCN LPNCBIFCN;
  79. typedef struct _NCB_STATUS {
  80. int this_immediate;
  81. int last_immediate;
  82. unsigned char this_final;
  83. unsigned char last_final;
  84. unsigned char rep_count;
  85. unsigned char align; // *ALIGNMENT*
  86. }NCB_STATUS, *PNCB_STATUS, *LPNCB_STATUS;
  87. // structure used for keeping the session id list for each alias
  88. typedef struct _MSG_SESSION_ID_ITEM
  89. {
  90. LIST_ENTRY List;
  91. ULONG SessionId;
  92. }
  93. MSG_SESSION_ID_ITEM, *PMSG_SESSION_ID_ITEM;
  94. // Per NCB Information
  95. typedef struct _NCB_DATA {
  96. DWORD MsgPtr;
  97. LPNCBIFCN IFunc;
  98. NCB_STATUS Status;
  99. NCB Ncb; // Structure passed to Netbios
  100. CHAR Buffer[BUFLEN];
  101. CHAR Name[NCBNAMSZ + 4];
  102. CHAR Fname[NCBNAMSZ + 4];
  103. SHORT mgid;
  104. CHAR State;
  105. UCHAR NameFlags;
  106. UCHAR NameNum;
  107. LIST_ENTRY SessionList;
  108. UCHAR Pad[3];
  109. } NCB_DATA, *PNCB_DATA;
  110. // Per Network Information
  111. typedef struct _NET_DATA {
  112. ULONG NumNcbs;
  113. PNCB_DATA *NcbList;
  114. UCHAR net_lana_num;
  115. UCHAR Pad[3];
  116. } NET_DATA, *PNET_DATA;
  117. // Global Information
  118. typedef struct _GLOBAL_DATA
  119. {
  120. ULONG NumNets;
  121. DWORD LogStatus;
  122. DWORD BufSize;
  123. PNET_DATA NetData;
  124. PCHAR Buffer;
  125. } GLOBAL_DATA, *PGLOBAL_DATA;
  126. extern GLOBAL_DATA GlobalData;
  127. #define NCB_INIT_ENTRIES 16 // Initial number of NCB allocated per network
  128. // For Multi-user systems, we allow up to 256 NCBs per network
  129. #define NCB_MAX_ENTRIES 256 // Maximum number of NCBs per network
  130. #define SESSION_MAX 256
  131. //
  132. // Name Flag definitions
  133. //
  134. #define NFNEW 0x01 // New name
  135. #define NFDEL 0x02 // Name deleted
  136. #define NFFOR 0x04 // Messages forwarded
  137. // #define NFFWDNAME 0x10 // Forward-name
  138. #define NFMACHNAME 0x20 // Machine name (undeletable)
  139. #define NFLOCK 0x40 // Name entry locked
  140. #define NFDEL_PENDING 0x80 // Delete name issued but not complete*/
  141. //
  142. // The messenger mailslot for domain messaging
  143. //
  144. #define MESSNGR_MS_NAME "\\\\.\\mailslot\\messngr"
  145. //
  146. // Structure and macro definitions
  147. //
  148. #ifdef INULL // If heap structures defined
  149. //
  150. // Multi-block message header
  151. //
  152. typedef struct {
  153. HEAPHDR mbb_hp; // Heap block header
  154. DWORD mbb_next; // Link to next message
  155. SYSTEMTIME mbb_bigtime; // Date of message
  156. DWORD mbb_btext; // Link to last text block
  157. DWORD mbb_ftext; // Link to first text block
  158. DWORD mbb_state; // State flag
  159. }MBB;
  160. #define MBB_CODE(x) HP_FLAG((x).mbb_hp)
  161. #define MBB_NEXT(x) (x).mbb_next
  162. #define MBB_BIGTIME(x) (x).mbb_bigtime
  163. #define MBB_BTEXT(x) (x).mbb_btext
  164. #define MBB_FTEXT(x) (x).mbb_ftext
  165. #define MBB_STATE(x) (x).mbb_state
  166. #define MBBPTR(x) ((MBB far *) &heap[(x)])
  167. //
  168. // Multi-block message text
  169. //
  170. typedef struct {
  171. HEAPHDR mbt_hp; // Heap block header
  172. DWORD mbt_next; // Link to next block (offset)
  173. DWORD mbt_bytecount; // *ALIGNMENT2*
  174. }MBT, *PMBT, *LPMBT;
  175. #define MBT_CODE(x) HP_FLAG((x).mbt_hp)
  176. #define MBT_NEXT(x) (x).mbt_next
  177. #define MBT_COUNT(x) (x).mbt_bytecount // *ALIGNMENT2*
  178. #define MBTPTR(x) ((LPMBT) &heap[(x)])
  179. #endif // INULL - End heap access macros
  180. //
  181. // A one session/name status structure
  182. //
  183. typedef struct _MSG_SESSION_STATUS{
  184. SESSION_HEADER SessHead;
  185. SESSION_BUFFER SessBuffer[SESSION_MAX];
  186. }MSG_SESSION_STATUS, *PMSG_SESSION_STATUS, *LPMSG_SESSION_STATUS;
  187. //
  188. // Shared data access macros
  189. //
  190. #define GETNCBDATA(n, x) GlobalData.NetData[(n)].NcbList[(x)]
  191. #define GETNCB(n, x) &GlobalData.NetData[(n)].NcbList[(x)]->Ncb
  192. #define GETNETLANANUM(n) GlobalData.NetData[(n)].net_lana_num
  193. #define NETLANANUM GETNETLANANUM
  194. #define GETNETDATA(n) &GlobalData.NetData[(n)]
  195. #define SD_NUMNETS() GlobalData.NumNets
  196. #define SD_NAMEFLAGS(n, x) GlobalData.NetData[(n)].NcbList[(x)]->NameFlags
  197. #define SD_NAMENUMS(n, x) GlobalData.NetData[(n)].NcbList[(x)]->NameNum
  198. #define SD_NAMES(n, x) GlobalData.NetData[(n)].NcbList[(x)]->Name
  199. #define SD_BUFLEN() GlobalData.BufSize
  200. #define SD_MESLOG() GlobalData.LogStatus
  201. #define SD_MESPTR(n, x) GlobalData.NetData[(n)].NcbList[(x)]->MsgPtr
  202. #define SD_BUFFER() GlobalData.Buffer
  203. #define SD_SIDLIST(n,x) GlobalData.NetData[(n)].NcbList[(x)]->SessionList
  204. #define NCBMAX(n) GlobalData.NetData[(n)].NumNcbs
  205. //
  206. // No. of repeated consectutive NCB errors required to abort the
  207. // message server.
  208. //
  209. #define SHUTDOWN_THRESHOLD 10
  210. //
  211. // Database Lock requests for the MsgDatabaseLock function.
  212. //
  213. typedef enum _MSG_LOCK_REQUEST
  214. {
  215. MSG_INITIALIZE,
  216. MSG_GET_SHARED,
  217. MSG_GET_EXCLUSIVE,
  218. MSG_RELEASE
  219. }
  220. MSG_LOCK_REQUEST, *PMSG_LOCK_REQUEST, *LPMSG_LOCK_REQUEST;
  221. //
  222. // Macros to deregister a thread pool item and close
  223. // a handle once and only once
  224. //
  225. #define DEREGISTER_WORK_ITEM(g_hWorkItem) \
  226. { \
  227. HANDLE hTemp = InterlockedExchangePointer(&g_hWorkItem, NULL); \
  228. \
  229. if (hTemp != NULL) \
  230. { \
  231. NTSTATUS Status = RtlDeregisterWait(hTemp); \
  232. \
  233. if (!NT_SUCCESS(Status)) \
  234. { \
  235. MSG_LOG2(ERROR, \
  236. "RtlDeregisterWait on %p failed %x\n", \
  237. hTemp, \
  238. Status); \
  239. } \
  240. } \
  241. }
  242. #define CLOSE_HANDLE(HandleToClose, InvalidHandleValue) \
  243. { \
  244. HANDLE hTemp = InterlockedExchangePointer(&HandleToClose, InvalidHandleValue); \
  245. \
  246. if (hTemp != InvalidHandleValue) \
  247. { \
  248. CloseHandle(hTemp); \
  249. } \
  250. }
  251. //
  252. // global variables
  253. //
  254. extern BOOL g_IsTerminalServer;
  255. // WinStationQueryInformationW
  256. typedef BOOLEAN (*PWINSTATION_QUERY_INFORMATION) (
  257. HANDLE hServer,
  258. ULONG SessionId,
  259. WINSTATIONINFOCLASS WinStationInformationClass,
  260. PVOID pWinStationInformation,
  261. ULONG WinStationInformationLength,
  262. PULONG pReturnLength
  263. );
  264. extern PWINSTATION_QUERY_INFORMATION gpfnWinStationQueryInformation;
  265. // WinStationSendMessageW
  266. typedef BOOLEAN (*PWINSTATION_SEND_MESSAGE) (
  267. HANDLE hServer,
  268. ULONG SessionId,
  269. LPWSTR pTitle,
  270. ULONG TitleLength,
  271. LPWSTR pMessage,
  272. ULONG MessageLength,
  273. ULONG Style,
  274. ULONG Timeout,
  275. PULONG pResponse,
  276. BOOLEAN DoNotWait
  277. );
  278. extern PWINSTATION_SEND_MESSAGE gpfnWinStationSendMessage;
  279. // WinStationFreeMemory
  280. typedef BOOLEAN (*PWINSTATION_FREE_MEMORY) (
  281. PVOID pBuffer
  282. );
  283. extern PWINSTATION_FREE_MEMORY gpfnWinStationFreeMemory;
  284. // WinStationEnumerateW
  285. typedef BOOLEAN (*PWINSTATION_ENUMERATE) (
  286. HANDLE hServer,
  287. PLOGONIDW *ppLogonId,
  288. PULONG pEntries
  289. );
  290. extern PWINSTATION_ENUMERATE gpfnWinStationEnumerate;
  291. //
  292. // Function Prototypes
  293. //
  294. DWORD
  295. GetMsgrState(
  296. VOID
  297. );
  298. VOID
  299. MsgrBlockStateChange(
  300. VOID
  301. );
  302. VOID
  303. MsgrUnblockStateChange(
  304. VOID
  305. );
  306. NET_API_STATUS
  307. MsgAddName(
  308. LPTSTR Name,
  309. ULONG SessionId
  310. );
  311. VOID
  312. MsgAddUserNames(
  313. VOID
  314. );
  315. VOID
  316. MsgAddAlreadyLoggedOnUserNames(
  317. VOID
  318. );
  319. DWORD
  320. MsgBeginForcedShutdown(
  321. IN BOOL PendingCode,
  322. IN DWORD ExitCode
  323. );
  324. BOOL
  325. MsgDatabaseLock(
  326. IN MSG_LOCK_REQUEST request,
  327. IN LPSTR idString
  328. );
  329. BOOL
  330. MsgConfigurationLock(
  331. IN MSG_LOCK_REQUEST request,
  332. IN LPSTR idString
  333. );
  334. NTSTATUS
  335. MsgInitCriticalSection(
  336. PRTL_CRITICAL_SECTION pCritsec
  337. );
  338. NTSTATUS
  339. MsgInitResource(
  340. PRTL_RESOURCE pResource
  341. );
  342. DWORD
  343. MsgDisplayInit(
  344. VOID
  345. );
  346. BOOL
  347. MsgDisplayQueueAdd(
  348. IN LPSTR pMsgBuffer,
  349. IN DWORD MsgSize,
  350. IN ULONG SessionId,
  351. IN SYSTEMTIME BigTime
  352. );
  353. VOID
  354. MsgDisplayThreadWakeup(
  355. VOID
  356. );
  357. VOID
  358. MsgDisplayEnd(
  359. VOID
  360. );
  361. NET_API_STATUS
  362. MsgInitializeMsgr(
  363. IN DWORD argc,
  364. IN LPTSTR *argv
  365. );
  366. NET_API_STATUS
  367. MsgrInitializeMsgrInternal1(
  368. void
  369. );
  370. NET_API_STATUS
  371. MsgrInitializeMsgrInternal2(
  372. void
  373. );
  374. NET_API_STATUS
  375. MsgNewName(
  376. IN DWORD neti,
  377. IN DWORD ncbi
  378. );
  379. VOID
  380. MsgrShutdown(
  381. );
  382. VOID
  383. MsgThreadWakeup(
  384. VOID
  385. );
  386. VOID
  387. MsgStatusInit(
  388. VOID
  389. );
  390. DWORD
  391. MsgStatusUpdate(
  392. IN DWORD NewState
  393. );
  394. VOID
  395. MsgThreadCloseAll(
  396. VOID
  397. );
  398. DWORD
  399. MsgThreadManagerInit(
  400. VOID
  401. );
  402. NET_API_STATUS
  403. MsgInit_NetBios(
  404. VOID
  405. );
  406. BOOL
  407. MsgServeNCBs(
  408. DWORD net // Which network am I serving?
  409. );
  410. VOID
  411. MsgServeNameReqs(
  412. IN DWORD net
  413. );
  414. VOID
  415. MsgReadGroupMailslot(
  416. VOID
  417. );
  418. NET_API_STATUS
  419. MsgServeGroupMailslot(
  420. VOID
  421. );
  422. NET_API_STATUS
  423. MsgFmtNcbName(
  424. OUT PCHAR DestBuf,
  425. IN LPTSTR Name,
  426. IN DWORD Type);
  427. DWORD
  428. Msghdrprint(
  429. int action, // Where to log the header to.
  430. LPSTR from, // Name of sender
  431. LPSTR to, // Name of recipient
  432. SYSTEMTIME bigtime, // Bigtime of message
  433. HANDLE file_handle // Output file handle*/
  434. );
  435. DWORD
  436. Msglogmbb(
  437. LPSTR from, // Name of sender
  438. LPSTR to, // Name of recipient
  439. DWORD net, // Which network ?
  440. DWORD ncbi // Network Control Block index
  441. );
  442. UCHAR
  443. Msglogmbe(
  444. DWORD state, // Final state of message
  445. DWORD net, // Which network?
  446. DWORD ncbi // Network Control Block index
  447. );
  448. DWORD
  449. Msglogmbt(
  450. LPSTR text, // Text of message
  451. DWORD net, // Which network?
  452. DWORD ncbi // Network Control Block index
  453. );
  454. DWORD
  455. Msglogsbm(
  456. LPSTR from, // Name of sender
  457. LPSTR to, // Name of recipient
  458. LPSTR text, // Text of message
  459. ULONG SessionId // Session Id
  460. );
  461. VOID
  462. Msgmbmfree(
  463. DWORD mesi
  464. );
  465. DWORD
  466. Msgmbmprint(
  467. int action,
  468. DWORD mesi,
  469. HANDLE file_handle,
  470. LPDWORD pdwAlertFlag
  471. );
  472. DWORD
  473. MsgrCtrlHandler(
  474. IN DWORD dwControl,
  475. IN DWORD dwEventType,
  476. IN LPVOID lpEventData,
  477. IN LPVOID lpContext
  478. );
  479. UCHAR
  480. Msgsendncb(
  481. PNCB NcbPtr,
  482. DWORD neti
  483. );
  484. int
  485. Msgsmbcheck(
  486. LPBYTE buffer,
  487. USHORT size,
  488. UCHAR func,
  489. int parms,
  490. LPSTR fields
  491. );
  492. NET_API_STATUS
  493. MsgStartListen(
  494. DWORD net,
  495. DWORD ncbi
  496. );
  497. DWORD
  498. Msgtxtprint(
  499. int action, // Alert, File, or Alert and file
  500. LPSTR text, // Pointer to text
  501. DWORD length, // Length of text
  502. HANDLE file_handle // Log file handle
  503. );
  504. NET_API_STATUS
  505. MsgInitSupportSeg(
  506. VOID
  507. );
  508. VOID
  509. MsgFreeSupportSeg(
  510. VOID
  511. );
  512. VOID
  513. MsgFreeSharedData(
  514. VOID
  515. );
  516. BOOL
  517. MsgCreateWakeupSems(
  518. DWORD NumNets
  519. );
  520. BOOL
  521. MsgCreateWakeupEvent(
  522. void
  523. );
  524. VOID
  525. MsgCloseWakeupSems(
  526. VOID
  527. );
  528. VOID
  529. MsgCloseWakeupEvent(
  530. VOID
  531. );
  532. NET_API_STATUS
  533. MsgInitGroupSupport(
  534. DWORD iGrpMailslotWakeupSem
  535. );
  536. VOID
  537. MsgGrpThreadShutdown(
  538. VOID
  539. );
  540. DWORD
  541. MsgGetNumNets(
  542. VOID
  543. );
  544. NET_API_STATUS
  545. MultiUserInitMessage(
  546. VOID
  547. );
  548. VOID
  549. MsgArrivalBeep(
  550. ULONG SessionId
  551. );
  552. INT
  553. DisplayMessage(
  554. LPWSTR pMessage,
  555. LPWSTR pTitle,
  556. ULONG SessionId
  557. );
  558. NET_API_STATUS
  559. MsgGetClientSessionId(
  560. OUT PULONG pSessionId
  561. );
  562. VOID
  563. MsgNetEventCompletion(
  564. PVOID pvContext, // This passed in as context.
  565. BOOLEAN fWaitStatus
  566. );
  567. #endif // MSRV_INCLUDED