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.

280 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. Misc.hxx
  5. Abstract:
  6. Header for random helpers functions.
  7. Author:
  8. Mario Goertzel [MarioGo]
  9. Revision History:
  10. MarioGo 02-11-95 Bits 'n pieces
  11. --*/
  12. #ifndef __MISC_HXX
  13. #define __MISC_HXX
  14. #include <memapi.hxx>
  15. #include <list.hxx>
  16. #define RPC_CHAR WCHAR
  17. // This is the new W2K server principal name prefix; to form a full SPN
  18. // you construct a string of the form "RPCSS/machine". For W2K RTM such
  19. // a string will simply be mapped to HOST (ie local system account) but
  20. // doing it this way gives us more flexibility for the future.
  21. #define RPCSS_SPN_PREFIX L"RPCSS/"
  22. extern ORSTATUS StartListeningIfNecessary();
  23. extern RPC_STATUS CopyMyOrBindings(DUALSTRINGARRAY **ppdsaOrBindings, DWORD64* pdwBindingsID);
  24. extern void PushCurrentBindings();
  25. extern RPC_STATUS ComputeNewResolverBindings(void);
  26. extern BOOL AllocateId(ID* pID);
  27. extern error_status_t ResolveClientOXID(
  28. handle_t hClient,
  29. OXID *poxidServer,
  30. DUALSTRINGARRAY *pdsaServerBindings,
  31. LONG fApartment,
  32. USHORT wProtseqId,
  33. WCHAR *pMachineName,
  34. OXID_INFO *poxidInfo,
  35. MID *pDestinationMid,
  36. BOOL fUnsecure,
  37. USHORT wAuthnSvc,
  38. unsigned long ulMarshaledTargetInfoLength,
  39. unsigned char *pMarshaledTargetInfo,
  40. BOOL *pIsLocalOxid,
  41. unsigned long *pulMarshaledTargetInfoLength,
  42. unsigned char **pucMarshaledTargetInfo,
  43. USHORT *pusAuthnSvc);
  44. //
  45. // Magic constants used in rpc security callback code:
  46. //
  47. #define INITIAL_NON_AUTHNSVC_VALUE 0xFFBB
  48. #define ERROR_AUTHNSVC_VALUE 0xFFBC
  49. //+-------------------------------------------------------------------------
  50. //
  51. // CRpcSecurityCallback
  52. //
  53. // This class represents a single rpc security callback for a single calling
  54. // thread.
  55. //
  56. class CRpcSecurityCallback : public CListElement
  57. {
  58. public:
  59. // Ctor
  60. CRpcSecurityCallback(handle_t hRpc, DWORD dwRegisteredThreadId)
  61. {
  62. _usAuthSvc = INITIAL_NON_AUTHNSVC_VALUE; // anything but a valid RPC_C_AUTHN_xxx value
  63. _dwThreadId = dwRegisteredThreadId;
  64. _hRpcBindingHandle = hRpc; // this is just a numeric copy! I am assuming that no two
  65. // binding handles will ever have the same value
  66. _ulMarshaledTargetInfoLength = 0;
  67. _pMarshaledTargetInfo = NULL;
  68. }
  69. ~CRpcSecurityCallback();
  70. DWORD GetRegisteredThreadId() { return _dwThreadId; };
  71. void SetAuthSvc(USHORT usAuthSvc) { _usAuthSvc = usAuthSvc; };
  72. void SetMarshaledTargetInfo(unsigned long ulMarshaledTargetInfoLength, unsigned char *pMarshaledTargetInfo);
  73. BOOL WasAuthSvcSet() { return _usAuthSvc != INITIAL_NON_AUTHNSVC_VALUE; };
  74. USHORT GetAuthSvcResult() { return _usAuthSvc; };
  75. ORSTATUS GetMarshaledTargetInfo(unsigned long *pulMarshaledTargetInfoLength, unsigned char **MarshaledTargetInfo);
  76. handle_t RegisteredHandle() { return _hRpcBindingHandle; };
  77. private:
  78. DWORD _dwThreadId;
  79. handle_t _hRpcBindingHandle;
  80. USHORT _usAuthSvc;
  81. unsigned long _ulMarshaledTargetInfoLength;
  82. unsigned char *_pMarshaledTargetInfo;
  83. };
  84. //+-------------------------------------------------------------------------
  85. //
  86. // Class for registering/receiving/storing rpc security
  87. // callback information; see bug 406902
  88. //
  89. class CRpcSecurityCallbackManager
  90. {
  91. public:
  92. // ctor
  93. CRpcSecurityCallbackManager(ORSTATUS& status)
  94. {
  95. _plistlock = new CSharedLock(status);
  96. if (!_plistlock)
  97. status = OR_NOMEM;
  98. };
  99. ~CRpcSecurityCallbackManager() {};
  100. // Registration function; a thread calls this before making an rpc call
  101. BOOL RegisterForRpcAuthSvcCallBack(handle_t hRpc);
  102. // Retrieval function; a thread calls this after making the rpc call
  103. BOOL GetSecurityContextDetailsAndTurnOffCallback(handle_t hRpc, USHORT* pusAuthSvc,
  104. unsigned long *pulMarshaledTargetInfoLength, unsigned char **pucMarshaledTargetInfo);
  105. private:
  106. // Function to just turn off the callback
  107. BOOL TurnOffCallback(handle_t hRpc);
  108. // Static callback function; has same signature as
  109. // RPC_SECURITY_CALLBACK_FN which is defined in rpcdce.h
  110. static void RPC_ENTRY RpcSecurityCallbackFunction(void* pvContext);
  111. // Helper function for storing results
  112. void StoreCallbackResult(USHORT usAuthSvc);
  113. void StoreMarshaledTargetInfo(unsigned long ulMarshaledTargetInfoLength, unsigned char *pMarshaledTargetInfo);
  114. CList _CallbackList; // list for holding the callbacks
  115. CSharedLock* _plistlock; // the lock
  116. };
  117. // external defn of the single instance of this class
  118. extern CRpcSecurityCallbackManager* gpCRpcSecurityCallbackMgr;
  119. //+-------------------------------------------------------------------------
  120. //
  121. // CUserPingSetCount
  122. //
  123. // Holds the current # of ping sets per SID
  124. //
  125. class CUserPingSetCount : public CListElement
  126. {
  127. public:
  128. // Ctor
  129. CUserPingSetCount(ORSTATUS &status, PSID pSid)
  130. {
  131. status = OR_OK;
  132. _dwCount = 0;
  133. _pSid = NULL; // NULL pSid is for unsecure user.
  134. if (pSid)
  135. {
  136. status = OR_NOMEM;
  137. DWORD dwLenSid = GetLengthSid(pSid);
  138. _pSid = (PSID*)new BYTE[dwLenSid];
  139. if (_pSid)
  140. {
  141. BOOL b = CopySid(dwLenSid, _pSid, pSid);
  142. ASSERT(b == TRUE);
  143. if (b)
  144. {
  145. status = OR_OK;
  146. }
  147. }
  148. }
  149. }
  150. ~CUserPingSetCount()
  151. {
  152. if (_pSid)
  153. delete []_pSid;
  154. }
  155. void Increment() { InterlockedIncrement((PLONG)&_dwCount); }
  156. void Decrement() { InterlockedDecrement((PLONG)&_dwCount); }
  157. DWORD GetCount() { return _dwCount;}
  158. BOOL IsEqual (PSID pSid) {
  159. if (pSid && _pSid)
  160. return EqualSid(pSid, _pSid);
  161. else
  162. return (pSid == _pSid);
  163. }
  164. private:
  165. DWORD _dwCount;
  166. PSID _pSid;
  167. };
  168. //+-------------------------------------------------------------------------
  169. //
  170. //
  171. // Mgr object for CUserPingSetCount
  172. //
  173. class CPingSetQuotaManager
  174. {
  175. public:
  176. // ctor
  177. CPingSetQuotaManager(ORSTATUS& status)
  178. {
  179. _plistlock = new CSharedLock(status);
  180. if (!_plistlock)
  181. status = OR_NOMEM;
  182. };
  183. ~CPingSetQuotaManager() { delete _plistlock; }
  184. void SetPerUserPingSetQuota(DWORD dwQuota);
  185. BOOL ManageQuotaForUser(PSID pSid, BOOL fAlloc);
  186. BOOL IsUserQuotaExceeded (PSID pSid);
  187. private:
  188. CList _UserPingSetCountList; // list for holding the callbacks
  189. CSharedLock* _plistlock; // the lock
  190. static DWORD _dwPerUserPingSetQuota;
  191. };
  192. extern CPingSetQuotaManager* gpPingSetQuotaManager;
  193. //
  194. // CRandomNumberGenerator -- class for handling rc4 random # generation. Guts
  195. // of this are shamelessly stolen from UuidCreate.
  196. //
  197. class CRandomNumberGenerator
  198. {
  199. public:
  200. CRandomNumberGenerator();
  201. ~CRandomNumberGenerator();
  202. // Initialize is not threadsafe
  203. BOOL Initialize();
  204. // GenerateRandomNumber is not threadsafe
  205. HRESULT GenerateRandomNumber(PVOID pBuffer, ULONG ulBufSize);
  206. private:
  207. void* _rc4SafeCtx;
  208. };
  209. extern CRandomNumberGenerator gRNG;
  210. //
  211. // Memory allocation
  212. //
  213. // Inside of the object exporter, new and delete go to PrivMemAlloc.
  214. inline void* __cdecl operator new(size_t cbSize)
  215. {
  216. return PrivMemAlloc(cbSize);
  217. }
  218. inline void* __cdecl operator new(size_t cbSize, size_t cbExtra)
  219. {
  220. return PrivMemAlloc(cbSize + cbExtra);
  221. }
  222. inline void __cdecl operator delete(void *pvMem)
  223. {
  224. PrivMemFree(pvMem);
  225. }
  226. #endif // __MISC_HXX