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.

214 lines
7.2 KiB

  1. #ifndef __NmLdap_h__
  2. #define __NmLdap_h__
  3. #include <cstring.hpp>
  4. #include "ldap.h"
  5. class CKeepAlive;
  6. class CNmLDAP
  7. {
  8. private:
  9. typedef CSTRING string_type;
  10. enum eState {
  11. Uninitialized,
  12. Idle, // Idle means we are not logged in, or connected
  13. Binding, // We are waiting for bind_s to complete in helper thread
  14. Bound, // The bind operation is complete
  15. AddingUser, // We are waiting for the result to ldap_add
  16. UserAdded, // ldap_add completed successfully
  17. SettingAppInfo, // We are waiting for result to ldap_modify( app attributes )
  18. ModifyingAttrs, // We are waiting for result to ldap_modify( some attrs )
  19. LoggedIn, // We are logged in to ldap server and m_ldap session is closed ( we are connectionless )
  20. LoggingOff // We are waiting for result to ldap_delete
  21. };
  22. enum eCurrentOp {
  23. Op_NoOp, // We are not in the middle of multi-state operation
  24. Op_Logon, // Logon is: ldap_bind, ldap_add, ldap_modify
  25. Op_Logoff, // Logoff is ldap_bind, ldap_delete
  26. Op_Refresh_Logoff, // Refresh is ldap_bind, ldap_delete, ldap_unbind, Op_Logon
  27. Op_Modifying_InCallAttr,// Modify in-call attrs is ldap_bind, ldap_modify
  28. };
  29. enum eTimerEvent {
  30. PollForResultTimer = 666, // This is the timer ID passed to WM_TIMER
  31. WaitForLogoffTimer
  32. };
  33. enum { RESULT_POLL_INTERVAL = 1000 }; // We poll ldap_result for this many milliseconds
  34. enum { LOGOFF_WAIT_INTERVAL = 5000 }; // Max amound of time we will wait for logof to complete in the destructor...
  35. /////////////////////////////////////////////////
  36. /// Data
  37. CKeepAlive *m_pKeepAlive;
  38. // This indicates weather we have loaded wldap32.dll and the ldap functions
  39. static bool ms_bLdapDLLLoaded;
  40. HWND m_hWndHidden; // Hidden window for handling WM_TIMER and custom messages
  41. eState m_State; // The current state we are in
  42. eCurrentOp m_CurrentOp; // The current multi-state operation we are performing
  43. LDAP* m_pLdap; // The current ldap session ( kept for multi-state and multi-state operations )
  44. INT m_uMsgID; // Current async operation message id ( or INVALID_MSG_ID )
  45. UINT_PTR m_ResultPollTimer; // Timer ID given to us by SetTimer
  46. UINT_PTR m_LogoffTimer;
  47. HANDLE m_hEventWaitForLogoffDone; // We attempt to logoff asynchronously
  48. HANDLE m_hBindThread;
  49. string_type m_strCurrentServer; // If we are logged in, we are logged in to this server
  50. string_type m_strCurrentDN; // If we are logged in, this is our current DN
  51. // User attributes
  52. string_type m_strServer;
  53. string_type m_strSurName;
  54. string_type m_strGivenName;
  55. string_type m_strEmailName;
  56. string_type m_strComment;
  57. string_type m_strLocation;
  58. string_type m_strSecurityToken;
  59. bool m_bVisible;
  60. bool m_bAudioHardware;
  61. bool m_bVideoHardware;
  62. bool m_bInCall;
  63. bool m_bDirty;
  64. bool m_bRefreshAfterBindCompletes;
  65. bool m_bLogoffAfterBindCompletes;
  66. bool m_bSendInCallAttrWhenDone;
  67. int m_iPort;
  68. public:
  69. CNmLDAP();
  70. ~CNmLDAP();
  71. HRESULT Initialize(HINSTANCE hInst); // Initialize the CNmLDAP object
  72. HRESULT LogonAsync(LPCTSTR pcszServer = NULL); // Logon to the specified server ( or default if NULL )
  73. HRESULT Logoff(); // Logoff from the current surver
  74. HRESULT OnSettingsChanged(); // Refresh our information on the server
  75. HRESULT OnCallStarted(); // Update server information about our call state
  76. HRESULT OnCallEnded(); // Update server information about our call state
  77. bool IsLoggedOn() const; // Are we logged on?
  78. bool IsLoggingOn() const; // Are we logged on?
  79. bool IsBusy() const; // Are we in the middle of an async operation?
  80. HRESULT GetStatusText(LPTSTR psz, int cch, UINT *idIcon=NULL) const; // Status text for status bar for example
  81. // Static fns used for resolving users, etc.
  82. static HRESULT ResolveUser(LPCTSTR pcszName, LPCTSTR pcszServer, LPTSTR pszIPAddr, DWORD cchMax, int port = DEFAULT_LDAP_PORT);
  83. static bool IsInitialized() { return ms_bLdapDLLLoaded; }
  84. private:
  85. // Window Procedure and helpers
  86. LRESULT WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  87. static LRESULT _sWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  88. static DWORD _sAsyncBindThreadFn(LPVOID lpParameter);
  89. void _AddUser();
  90. void _DeleteUser();
  91. void _SetAppInfo();
  92. void _ModifyInCallAttr();
  93. HRESULT _BindAsync();
  94. HRESULT _bCallChangedHelper();
  95. HRESULT _GetUserSettingsFromRegistryAndGlobals();
  96. void _OnLoggedOn();
  97. void _OnLoggedOff();
  98. void _OnLoggingOn();
  99. void _OnLoggingOff();
  100. void _AbandonAllAndSetState(eState new_state);
  101. void _AbandonAllAndSetToIdle() { _AbandonAllAndSetState(Idle); }
  102. void _AbandonAllAndRecoverState(void) { _AbandonAllAndSetState((ModifyingAttrs == m_State && NULL != m_pKeepAlive) ? LoggedIn : Idle); }
  103. void _AbandonAllAndRecoverState(eCurrentOp op) { _AbandonAllAndSetState((Op_Modifying_InCallAttr == op && NULL != m_pKeepAlive) ? LoggedIn : Idle); }
  104. void _MakeStatusText(UINT uResID, LPTSTR psz, UINT cch) const;
  105. HRESULT _InternalLogoff(bool bRefreshLogoff);
  106. HRESULT _RefreshServer();
  107. void _OnAddingUserResult(int Result);
  108. void _OnSettingAppInfoOrModifyingAttrsResult(int Result);
  109. void _OnLoggingOffResult(int Result);
  110. void _OnUserBindComplete(INT LdapResult, DWORD LastError );
  111. void _OnTimer(UINT_PTR TimerID);
  112. void _GetIpAddressOfLdapSession( LPTSTR szIpAddr, int cchMax, DWORD *pdwIPAddr );
  113. static HRESULT _LoadLdapDLL();
  114. HRESULT OnReLogon(void);
  115. };
  116. void InitNmLdapAndLogon();
  117. extern CNmLDAP* g_pLDAP;
  118. extern CPing* g_pPing;
  119. enum { INITIAL_REFRESH_INTERVAL_MINUTES = 2 }; // Initial time before we send a message to the server to reset the TTL
  120. enum { MIN_REFRESH_TIMEOUT_INTERVAL_MINUTES = 1 }; // Minimum timeout interval
  121. enum { REFRESH_TIMEOUT_MARGIN = 2 }; // We send a refresh REFRESH_TIMEOUT_MARGIN minutes before the server TTL
  122. enum { PING_TIMEOUT_INTERVAL = (10 * 1000) };
  123. enum { PING_RETRIES = 9 };
  124. enum { LDAP_TIMEOUT_IN_SECONDS = 45 };
  125. class CKeepAlive
  126. {
  127. friend DWORD KeepAliveThreadProc(LPVOID);
  128. public:
  129. // called in the main thread
  130. CKeepAlive(BOOL *pfRet, HWND hwndMainThread,
  131. DWORD dwLocalIPAddress,
  132. const TCHAR * const pcszServerName, UINT nPort,
  133. LPTSTR pszKeepAliveFilter);
  134. BOOL Start(void);
  135. BOOL End(BOOL fSync = FALSE);
  136. protected:
  137. // called in the worker thread
  138. ~CKeepAlive(void);
  139. BOOL SetServerIPAddress(void);
  140. DWORD GetLocalIPAddress(LDAP *ld);
  141. BOOL Ping(void);
  142. BOOL Bind(LDAP *ld);
  143. BOOL KeepAlive(LDAP *ld, UINT *pnRefreshInterval);
  144. DWORD GetLocalIPAddress(void) { return m_dwLocalIPAddress; }
  145. void SetLocalIPAddress(DWORD dwLocalIPAddress) { m_dwLocalIPAddress = dwLocalIPAddress; }
  146. LPTSTR GetServerName(void) { return m_pszServerName; }
  147. DWORD GetServerIPAddress(void) { return m_dwServerIPAddress; }
  148. UINT GetServerPortNumber(void) { return m_nPort; }
  149. void UpdateIPAddressOnServer(void);
  150. private:
  151. // called in the worker thread
  152. void GetNewInterval(LDAP *ld, LDAPMessage *pMsg, UINT *pnRefreshInternval);
  153. void ReLogon(void);
  154. private:
  155. HWND m_hwndMainThread;
  156. DWORD m_dwLocalIPAddress;
  157. DWORD m_dwServerIPAddress;
  158. LPTSTR m_pszServerName;
  159. UINT m_nPort;
  160. LPTSTR m_pszKeepAliveFilter;
  161. HANDLE m_hThread;
  162. DWORD m_dwThreadID;
  163. BOOL m_fAborted;
  164. };
  165. #endif // __NmLdap_h__
  166.