Пример #1
0
        /// <summary>
        /// Is called when new POP3 server session has created.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event data.</param>
        private void m_pImapServer_SessionCreated(object sender,TCP_ServerSessionEventArgs<IMAP_Session> e)
        {            
            e.Session.Started += new EventHandler<IMAP_e_Started>(m_pImapServer_Session_Started);
            e.Session.Login += new EventHandler<IMAP_e_Login>(m_pImapServer_Session_Login);
            //--- Authenticated state
            e.Session.Namespace += new EventHandler<IMAP_e_Namespace>(m_pImapServer_Session_Namespace);
            e.Session.LSub += new EventHandler<IMAP_e_LSub>(m_pImapServer_Session_LSub);
            e.Session.Subscribe += new EventHandler<IMAP_e_Folder>(m_pImapServer_Session_Subscribe);
            e.Session.Unsubscribe += new EventHandler<IMAP_e_Folder>(m_pImapServer_Session_Unsubscribe);
            e.Session.List += new EventHandler<IMAP_e_List>(m_pImapServer_Session_List);
            e.Session.Create += new EventHandler<IMAP_e_Folder>(m_pImapServer_Session_Create);
            e.Session.Delete += new EventHandler<IMAP_e_Folder>(m_pImapServer_Session_Delete);
            e.Session.Rename += new EventHandler<IMAP_e_Rename>(m_pImapServer_Session_Rename);
            e.Session.GetQuotaRoot += new EventHandler<IMAP_e_GetQuotaRoot>(m_pImapServer_Session_GetQuotaRoot);
            e.Session.GetQuota += new EventHandler<IMAP_e_GetQuota>(m_pImapServer_Session_GetQuota);
            e.Session.GetAcl += new EventHandler<IMAP_e_GetAcl>(m_pImapServer_Session_GetAcl);
            e.Session.SetAcl += new EventHandler<IMAP_e_SetAcl>(m_pImapServer_Session_SetAcl);
            e.Session.DeleteAcl += new EventHandler<IMAP_e_DeleteAcl>(m_pImapServer_Session_DeleteAcl);
            e.Session.ListRights += new EventHandler<IMAP_e_ListRights>(m_pImapServer_Session_ListRights);
            e.Session.MyRights += new EventHandler<IMAP_e_MyRights>(m_pImapServer_Session_MyRights);
            e.Session.Select += new EventHandler<IMAP_e_Select>(m_pImapServer_Session_Select);
            e.Session.Append += new EventHandler<IMAP_e_Append>(m_pImapServer_Session_Append);
            //--- Selected state
            e.Session.GetMessagesInfo += new EventHandler<IMAP_e_MessagesInfo>(m_pImapServer_Session_GetMessagesInfo);
            e.Session.Search += new EventHandler<IMAP_e_Search>(m_pImapServer_Session_Search);
            e.Session.Fetch += new EventHandler<IMAP_e_Fetch>(m_pImapServer_Session_Fetch);
            e.Session.Expunge += new EventHandler<IMAP_e_Expunge>(m_pImapServer_Session_Expunge);
            e.Session.Store += new EventHandler<IMAP_e_Store>(m_pImapServer_Session_Store);
            e.Session.Copy += new EventHandler<IMAP_e_Copy>(m_pImapServer_Session_Copy);
            
            // Add session supported authentications.
            if(m_AuthType == MailServerAuthType_enum.Windows || m_AuthType == MailServerAuthType_enum.Ldap){
                // For windows or LDAP auth, we can allow only plain text authentications, because otherwise 
                // we can't do auth against windows (it requires user name and password).

                #region PLAIN

                AUTH_SASL_ServerMechanism_Plain auth_plain = new AUTH_SASL_ServerMechanism_Plain(false);
                auth_plain.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.IMAP) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_plain.Name,auth_plain);

                #endregion

                #region LOGIN

                AUTH_SASL_ServerMechanism_Login auth_login = new AUTH_SASL_ServerMechanism_Login(false);
                auth_login.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.IMAP) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_login.Name,auth_login);

                #endregion
            }
            else{
                #region DIGEST-MD5

                AUTH_SASL_ServerMechanism_DigestMd5 auth_digestmMd5 = new AUTH_SASL_ServerMechanism_DigestMd5(false);                
                auth_digestmMd5.Realm = e.Session.LocalHostName;
                auth_digestmMd5.GetUserInfo += new EventHandler<AUTH_e_UserInfo>(delegate(object s,AUTH_e_UserInfo e1){
                    // Check that user is allowed to access this service.
                    if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.IMAP) == 0){
                        e1.UserExists = false;

                        return;
                    }

                    FillUserInfo(e1);
                });
                e.Session.Authentications.Add(auth_digestmMd5.Name,auth_digestmMd5);

                #endregion

                #region CRAM-MD5

                AUTH_SASL_ServerMechanism_CramMd5 auth_cramMd5 = new AUTH_SASL_ServerMechanism_CramMd5(false);
                auth_cramMd5.GetUserInfo += new EventHandler<AUTH_e_UserInfo>(delegate(object s,AUTH_e_UserInfo e1){
                    // Check that user is allowed to access this service.
                    if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.IMAP) == 0){
                        e1.UserExists = false;

                        return;
                    }

                    FillUserInfo(e1);
                });
                e.Session.Authentications.Add(auth_cramMd5.Name,auth_cramMd5);

                #endregion

                #region PLAIN

                AUTH_SASL_ServerMechanism_Plain auth_plain = new AUTH_SASL_ServerMechanism_Plain(false);
                auth_plain.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.IMAP) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_plain.Name,auth_plain);

                #endregion

                #region LOGIN

                AUTH_SASL_ServerMechanism_Login auth_login = new AUTH_SASL_ServerMechanism_Login(false);
                auth_login.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.IMAP) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_login.Name,auth_login);

                #endregion                                
            }
        }
Пример #2
0
        /// <summary>
        /// Is called when new SMTP server session has created.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event data.</param>
        private void m_pSmtpServer_SessionCreated(object sender,TCP_ServerSessionEventArgs<SMTP_Session> e)
        {            
            e.Session.Started += new EventHandler<SMTP_e_Started>(m_pSmtpServer_Session_Started);
            e.Session.Ehlo += new EventHandler<SMTP_e_Ehlo>(m_pSmtpServer_Session_Ehlo);
            e.Session.MailFrom += new EventHandler<SMTP_e_MailFrom>(m_pSmtpServer_Session_MailFrom);
            e.Session.RcptTo += new EventHandler<SMTP_e_RcptTo>(m_pSmtpServer_Session_RcptTo);
            e.Session.GetMessageStream += new EventHandler<SMTP_e_Message>(m_pSmtpServer_Session_GetMessageStream);
            e.Session.MessageStoringCanceled += new EventHandler(m_pSmtpServer_Session_MessageStoringCanceled);
            e.Session.MessageStoringCompleted += new EventHandler<SMTP_e_MessageStored>(m_pSmtpServer_Session_MessageStoringCompleted);

            // Add session supported authentications.
            if(m_AuthType == MailServerAuthType_enum.Windows || m_AuthType == MailServerAuthType_enum.Ldap){
                // For windows or LDAP auth, we can allow only plain text authentications, because otherwise 
                // we can't do auth against windows (it requires user name and password).

                #region PLAIN

                AUTH_SASL_ServerMechanism_Plain auth_plain = new AUTH_SASL_ServerMechanism_Plain(false);
                auth_plain.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_plain.Name,auth_plain);

                #endregion

                #region LOGIN

                AUTH_SASL_ServerMechanism_Login auth_login = new AUTH_SASL_ServerMechanism_Login(false);
                auth_login.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_login.Name,auth_login);

                #endregion
            }
            else{
                #region DIGEST-MD5

                AUTH_SASL_ServerMechanism_DigestMd5 auth_digestmMd5 = new AUTH_SASL_ServerMechanism_DigestMd5(false);                
                auth_digestmMd5.Realm = e.Session.LocalHostName;
                auth_digestmMd5.GetUserInfo += new EventHandler<AUTH_e_UserInfo>(delegate(object s,AUTH_e_UserInfo e1){
            
                    FillUserInfo(e1);
                });
                e.Session.Authentications.Add(auth_digestmMd5.Name,auth_digestmMd5);

                #endregion

                #region CRAM-MD5

                AUTH_SASL_ServerMechanism_CramMd5 auth_cramMd5 = new AUTH_SASL_ServerMechanism_CramMd5(false);
                auth_cramMd5.GetUserInfo += new EventHandler<AUTH_e_UserInfo>(delegate(object s,AUTH_e_UserInfo e1){
                    FillUserInfo(e1);
                });
                e.Session.Authentications.Add(auth_cramMd5.Name,auth_cramMd5);

                #endregion

                #region PLAIN

                AUTH_SASL_ServerMechanism_Plain auth_plain = new AUTH_SASL_ServerMechanism_Plain(false);
                auth_plain.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_plain.Name,auth_plain);

                #endregion

                #region LOGIN

                AUTH_SASL_ServerMechanism_Login auth_login = new AUTH_SASL_ServerMechanism_Login(false);
                auth_login.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_login.Name,auth_login);

                #endregion                                
            }
        }
Пример #3
0
        /// <summary>
        /// Is called when new POP3 server session has created.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">Event data.</param>
        private void m_pPop3Server_SessionCreated(object sender,TCP_ServerSessionEventArgs<POP3_Session> e)
        {
            e.Session.Started += new EventHandler<POP3_e_Started>(m_pPop3Server_Session_Started);
            e.Session.Authenticate += new EventHandler<POP3_e_Authenticate>(m_pPop3Server_Session_Authenticate);
            e.Session.GetMessagesInfo += new EventHandler<POP3_e_GetMessagesInfo>(m_pPop3Server_Session_GetMessagesInfo);
            e.Session.GetTopOfMessage += new EventHandler<POP3_e_GetTopOfMessage>(m_pPop3Server_Session_GetTopOfMessage);
            e.Session.GetMessageStream += new EventHandler<POP3_e_GetMessageStream>(m_pPop3Server_Session_GetMessageStream);
            e.Session.DeleteMessage += new EventHandler<POP3_e_DeleteMessage>(m_pPop3Server_Session_DeleteMessage);
                        
            // Add session supported authentications.
            if(m_AuthType == MailServerAuthType_enum.Windows || m_AuthType == MailServerAuthType_enum.Ldap){
                // For windows or LDAP auth, we can allow only plain text authentications, because otherwise 
                // we can't do auth against windows (it requires user name and password).

                #region PLAIN

                AUTH_SASL_ServerMechanism_Plain auth_plain = new AUTH_SASL_ServerMechanism_Plain(false);
                auth_plain.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.POP3) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_plain.Name,auth_plain);

                #endregion

                #region LOGIN

                AUTH_SASL_ServerMechanism_Login auth_login = new AUTH_SASL_ServerMechanism_Login(false);
                auth_login.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.POP3) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_login.Name,auth_login);

                #endregion
            }
            else{
                #region DIGEST-MD5

                AUTH_SASL_ServerMechanism_DigestMd5 auth_digestmMd5 = new AUTH_SASL_ServerMechanism_DigestMd5(false);                
                auth_digestmMd5.Realm = e.Session.LocalHostName;
                auth_digestmMd5.GetUserInfo += new EventHandler<AUTH_e_UserInfo>(delegate(object s,AUTH_e_UserInfo e1){
                    // Check that user is allowed to access this service.
                    if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.POP3) == 0){
                        e1.UserExists = false;

                        return;
                    }

                    FillUserInfo(e1);
                });
                e.Session.Authentications.Add(auth_digestmMd5.Name,auth_digestmMd5);

                #endregion

                #region CRAM-MD5

                AUTH_SASL_ServerMechanism_CramMd5 auth_cramMd5 = new AUTH_SASL_ServerMechanism_CramMd5(false);
                auth_cramMd5.GetUserInfo += new EventHandler<AUTH_e_UserInfo>(delegate(object s,AUTH_e_UserInfo e1){
                    // Check that user is allowed to access this service.
                    if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.POP3) == 0){
                        e1.UserExists = false;

                        return;
                    }

                    FillUserInfo(e1);
                });
                e.Session.Authentications.Add(auth_cramMd5.Name,auth_cramMd5);

                #endregion

                #region PLAIN

                AUTH_SASL_ServerMechanism_Plain auth_plain = new AUTH_SASL_ServerMechanism_Plain(false);
                auth_plain.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.POP3) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_plain.Name,auth_plain);

                #endregion

                #region LOGIN

                AUTH_SASL_ServerMechanism_Login auth_login = new AUTH_SASL_ServerMechanism_Login(false);
                auth_login.Authenticate += new EventHandler<AUTH_e_Authenticate>(delegate(object s,AUTH_e_Authenticate e1){
                    try{
                        // Check that user is allowed to access this service.
                        if((m_pApi.GetUserPermissions(e1.UserName) & UserPermissions_enum.POP3) == 0){
                            e1.IsAuthenticated = false;

                            return;
                        }

                        e1.IsAuthenticated = Authenticate(e.Session.RemoteEndPoint.Address,e1.UserName,e1.Password);
                    }
                    catch(Exception x){
                        OnError(x);
                        e1.IsAuthenticated = false;
                    }
                });
                e.Session.Authentications.Add(auth_login.Name,auth_login);

                #endregion                                
            }
        }