Revision: 66
http://svn.sourceforge.net/nmailserver/?rev=66&view=rev
Author: tmyroadctfig
Date: 2006-10-24 05:21:38 -0700 (Tue, 24 Oct 2006)
Log Message:
-----------
Work on authentication and mail domain management.
Modified Paths:
--------------
NMail/branches/luke-dev/NMail/Authentication/BasicAuthToken.cs
NMail/branches/luke-dev/NMail/Authentication/IAuthenticationProvider.cs
NMail/branches/luke-dev/NMail/Authentication/IAuthenticationToken.cs
NMail/branches/luke-dev/NMail/Authentication/NullAuthentication.cs
NMail/branches/luke-dev/NMail/DataTypes/LocalStore/ILocalStore.cs
NMail/branches/luke-dev/NMail/DataTypes/LocalStore/MailDomain.cs
NMail/branches/luke-dev/NMail/NMail.csproj
NMail/branches/luke-dev/NMail.Administration.Web/CreateMailDomain.aspx.cs
NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStore.sql
NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs
NMail/branches/luke-dev/NMail.LocalStoreData.MySql/NMail.LocalStoreData.MySql.csproj
NMail/branches/luke-dev/NMail.Server.Console/NMail.config
Modified: NMail/branches/luke-dev/NMail/Authentication/BasicAuthToken.cs
===================================================================
--- NMail/branches/luke-dev/NMail/Authentication/BasicAuthToken.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail/Authentication/BasicAuthToken.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -28,9 +28,12 @@
/// </summary>
/// <param name="username">The username associated with then authentication token.</param>
/// <param name="credentialsExpiry">The date and time that the user's credentials expire.</param>
- public BasicAuthToken(string username, DateTime credentialsExpiry) {
+ /// <param name="credentialsExpiry">The date and time that the token will expire.</param>
+ public BasicAuthToken(string username, DateTime credentialsExpiry, DateTime tokenExpiry) {
this.username = username;
this.credentialsExpiry = credentialsExpiry;
+ this.tokenExpiry = tokenExpiry;
+ this.tokenId = new Guid();
}
private Guid tokenId;
@@ -63,6 +66,12 @@
return this.credentialsExpiry;
}
}
+
+ private DateTime tokenExpiry;
+
+ public DateTime TokenExpiry {
+ get { return tokenExpiry; }
+ }
#endregion
}
}
Modified: NMail/branches/luke-dev/NMail/Authentication/IAuthenticationProvider.cs
===================================================================
--- NMail/branches/luke-dev/NMail/Authentication/IAuthenticationProvider.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail/Authentication/IAuthenticationProvider.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -67,20 +67,39 @@
bool ValidToken(IAuthenticationToken user);
/// <summary>
- /// Checks if the user's account is currently locked out.
+ /// Checks if the user's account is currently locked out. This situation
+ /// is triggered when the user attempts too many incorrect logins.
/// </summary>
/// <param name="user">The user to check.</param>
/// <returns>True if their account is locked out.</returns>
bool IsLockedOut(IAuthenticationToken user);
/// <summary>
+ /// Resets a locked out account restoring access.
+ /// </summary>
+ /// <param name="username">The username for the account to reset.</param>
+ void ResetLockOut(string username);
+
+ /// <summary>
+ /// Checks if the user's account is enabled.
+ /// </summary>
+ /// <param name="user">The user to check.</param>
+ /// <returns>True if their account is enabled.</returns>
+ bool IsEnabled(IAuthenticationToken user);
+
+ /// <summary>
+ /// Sets whether an account is enabled.
+ /// </summary>
+ /// <param name="username">The username to alter.</param>
+ /// <param name="enabled">True if the account is enabled, false for disabled.</param>
+ void SetEnabled(string username, bool enabled);
+
+ /// <summary>
/// Changes the user's password.
/// </summary>
/// <param name="user">The user to change the password for.</param>
/// <param name="newPassword">The new password.</param>
/// <returns>True on success, false otherwise.</returns>
bool ChangePassword(IAuthenticationToken user, string newPassword);
-
- // TODO: reset password & others
}
}
Modified: NMail/branches/luke-dev/NMail/Authentication/IAuthenticationToken.cs
===================================================================
--- NMail/branches/luke-dev/NMail/Authentication/IAuthenticationToken.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail/Authentication/IAuthenticationToken.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -32,5 +32,10 @@
/// password expires and needs changing.
/// </summary>
DateTime CredentialsExpiry { get; }
+
+ /// <summary>
+ /// Gets the date and time that this token will expire.
+ /// </summary>
+ DateTime TokenExpiry { get; }
}
}
Modified: NMail/branches/luke-dev/NMail/Authentication/NullAuthentication.cs
===================================================================
--- NMail/branches/luke-dev/NMail/Authentication/NullAuthentication.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail/Authentication/NullAuthentication.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -28,11 +28,11 @@
#region IAuthenticationProvider Members
public IAuthenticationToken Authenticate(string username, string password) {
- return new BasicAuthToken(username, DateTime.MaxValue);
+ return new BasicAuthToken(username, DateTime.MaxValue, DateTime.MaxValue);
}
public IAuthenticationToken CreateAuthToken(string username) {
- return new BasicAuthToken(username, DateTime.MaxValue);
+ return new BasicAuthToken(username, DateTime.MaxValue, DateTime.MaxValue);
}
public DateTime GetTokenExpiry(IAuthenticationToken user) {
@@ -54,6 +54,14 @@
public bool ChangePassword(IAuthenticationToken user, string newPassword) {
return true;
}
+
+ public void ResetLockOut(string username) { }
+
+ public void SetEnabled(string username, bool enabled) { }
+
+ public bool IsEnabled(IAuthenticationToken user) {
+ return true;
+ }
#endregion
}
}
Modified: NMail/branches/luke-dev/NMail/DataTypes/LocalStore/ILocalStore.cs
===================================================================
--- NMail/branches/luke-dev/NMail/DataTypes/LocalStore/ILocalStore.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail/DataTypes/LocalStore/ILocalStore.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -412,22 +412,6 @@
/// <returns>The user's email addresses.</returns>
EmailAddress[] GetUserAddresses(IAuthenticationToken authToken, int userId);
- ///// <summary>
- ///// Adds the given address the to list of addresses for the user.
- ///// </summary>
- ///// <param name="authToken">The authentication credentials.</param>
- ///// <param name="userId">The Id of the user to add the address for.</param>
- ///// <param name="address">The address to add.</param>
- //void AddUserAddress(IAuthenticationToken authToken, int userId, EmailAddress address);
-
- ///// <summary>
- ///// Removes the given address from the list of addresses for the user.
- ///// </summary>
- ///// <param name="authToken">The authentication credentials.</param>
- ///// <param name="userId">The Id of the user to remove the address from.</param>
- ///// <param name="address">The address to remove.</param>
- //void RemoveUserAddress(IAuthenticationToken authToken, int userId, EmailAddress address);
-
/// <summary>
/// Gets a list of folder that belong to the user with the given user Id.
/// </summary>
Modified: NMail/branches/luke-dev/NMail/DataTypes/LocalStore/MailDomain.cs
===================================================================
--- NMail/branches/luke-dev/NMail/DataTypes/LocalStore/MailDomain.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail/DataTypes/LocalStore/MailDomain.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -107,11 +107,6 @@
}
}
- public void AddMailboxMapping(Type mappingType) {
- ILocalStoreUserMap mapping = (ILocalStoreUserMap) Activator.CreateInstance(mappingType);
- this.mailboxMappings.Add(mapping);
- }
-
private List<ILocalStoreDeliveryAction> allowedActions = new List<ILocalStoreDeliveryAction>();
/// <summary>
@@ -167,7 +162,7 @@
}
// TODO: should also check group membership
-
+
return result;
}
Modified: NMail/branches/luke-dev/NMail/NMail.csproj
===================================================================
--- NMail/branches/luke-dev/NMail/NMail.csproj 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail/NMail.csproj 2006-10-24 12:21:38 UTC (rev 66)
@@ -106,6 +106,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Authentication\IHashAuthProvider.cs" />
+ <Compile Include="Authentication\ILockOutPolicy.cs" />
<Compile Include="Authentication\NullAuthentication.cs">
<SubType>Code</SubType>
</Compile>
Modified: NMail/branches/luke-dev/NMail.Administration.Web/CreateMailDomain.aspx.cs
===================================================================
--- NMail/branches/luke-dev/NMail.Administration.Web/CreateMailDomain.aspx.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail.Administration.Web/CreateMailDomain.aspx.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -30,16 +30,11 @@
if (mailDomain != null)
{
- Type mappingType = typeof(MySqlLocalStoreData);
+ Type mappingType = typeof(MySqlUserMap);
- //ILocalStoreUserMap mapping = MailDomainDataSource.CreateMailboxMapping(mappingType);
- //ILocalStoreUserMap mapping = Activator.CreateInstance
- //mailDomain.MailboxMappings.Add(mapping);
- mailDomain.AddMailboxMapping(mappingType);
+ ILocalStoreUserMap mapping = (ILocalStoreUserMap) Activator.CreateInstance(mappingType);
+ mailDomain.MailboxMappings.Add(mapping);
result = MailDomainDataSource.UpdateMailDomain(mailDomain);
-
-
- //MailDomainDataSource.AddMailboxMapping(mailDomain.MailDomainId, mappingType);
}
}
Modified: NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStore.sql
===================================================================
--- NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStore.sql 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStore.sql 2006-10-24 12:21:38 UTC (rev 66)
@@ -40,6 +40,9 @@
QuotaHardLimit INT,
QuotaWarnLimit INT,
Password VARCHAR(255),
+ Disabled BOOL NOT NULL,
+ LockedOutData LONGBLOB,
+ PasswordExpiry DATETIME NOT NULL,
PRIMARY KEY(UserId)
) TYPE=InnoDB;
@@ -868,6 +871,7 @@
DROP PROCEDURE IF EXISTS UpdateMailDomain ;
CREATE PROCEDURE UpdateMailDomain
(
+ MailDomainId INT,
PrimaryHost VARCHAR(255),
ObjectData LONGBLOB,
OUT Result INT
@@ -875,17 +879,25 @@
BEGIN
START TRANSACTION;
- /* Check if the name is valid. */
- IF NOT EXISTS (SELECT m.MailDomainId FROM MailDomain m WHERE m.PrimaryHost LIKE PrimaryHost AND m.MailDomainId != MailDomainId) THEN
-
- UPDATE MailDomain m SET m.PrimaryHost = PrimaryHost, m.ObjectData = ObjectData WHERE m.MailDomainId = MailDomainId;
+ /* Check if the Id is valid. */
+ IF EXISTS (SELECT m.PrimaryHost FROM MailDomain m WHERE m.MailDomainId = MailDomainId) THEN
+
+ /* Check if the name is valid. */
+ IF NOT EXISTS (SELECT m.MailDomainId FROM MailDomain m WHERE m.PrimaryHost LIKE PrimaryHost AND m.MailDomainId != MailDomainId) THEN
+
+ UPDATE MailDomain m SET m.PrimaryHost = PrimaryHost, m.ObjectData = ObjectData WHERE m.MailDomainId = MailDomainId;
+
+ /* Ok successful. */
+ SELECT 0 INTO Result;
- /* Ok successful. */
- SELECT 0 INTO Result;
+ ELSE
+ /* Already exists. */
+ SELECT 1 INTO Result;
+ END IF;
ELSE
- /* Already exists. */
- SELECT 1 INTO Result;
+ /* No such mail domain. */
+ SELECT 2 INTO Result;
END IF;
COMMIT;
Modified: NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs
===================================================================
--- NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail.LocalStoreData.MySql/MySqlLocalStoreData.cs 2006-10-24 12:21:38 UTC (rev 66)
@@ -22,33 +22,22 @@
using System.Security.Cryptography;
using System.Text;
-using Mono.Security.Protocol.Ntlm;
using MySql.Data.MySqlClient;
-using NMail.Authentication;
using NMail.Configuration;
using NMail.DataTypes;
using NMail.DataTypes.ACLs;
using NMail.DataTypes.LocalStore;
using NMail.Helper;
-using NMail.LocalStoreData.MySql.Configuration;
// TODO: implement quotas
-// TODO: password expiry
-// TODO: account lockout if too many bad attempts
namespace NMail.LocalStoreData.MySql {
/// <summary>
/// A MySql based local store data provider.
/// </summary>
[Serializable]
- public class MySqlLocalStoreData : MarshalByRefObject, ILocalStoreData, IHashAuthProvider, ILocalStoreUserMap {
- private MySqlConnection GetConnection() {
- MySqlConnection cnn = new MySqlConnection(MySqlLocalStoreDataConfiguration.Current.ConnectionString);
- cnn.Open();
- return cnn;
- }
-
+ public class MySqlLocalStoreData : MarshalByRefObject, ILocalStoreData {
#region ILocalStoreData Members
#region Message Delivery
@@ -63,7 +52,7 @@
}
public void DeliverMessage(Message message, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
MySqlTransaction transaction = null;
try {
@@ -201,13 +190,13 @@
}
public StoreFolder GetStoreFolder(Folder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
return getStoreFolder(folder, cnn, null);
}
}
public StoreFolder GetStoreFolder(int folderId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetFolder";
@@ -262,7 +251,7 @@
/// <param name="newFolder">The folder to create.</param>
/// <returns>The result of the attempt to create the folder.</returns>
public LocalStoreFolderResult CreateFolder(int userId, StoreFolder parent, Folder newFolder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
int? parentFolderId = (parent == null) ? null : (int?) parent.FolderId;
@@ -295,7 +284,7 @@
/// <param name="folderId">The Id of the folder to delete.</param>
/// <returns>The result of the delete attempt.</returns>
public LocalStoreFolderResult DeleteFolder(int folderId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "DeleteFolder";
@@ -344,7 +333,7 @@
}
// Only the last part of the folder name has changed, proceed with the rename
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
MySqlTransaction transaction = cnn.BeginTransaction();
// Check for a folder with the same name
@@ -363,7 +352,7 @@
#region GetChildren
public StoreFolder[] GetChildren(StoreFolder parent) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetFolderChildIds";
@@ -401,7 +390,7 @@
/// </summary>
/// <returns>The list of folders.</returns>
public StoreFolder[] GetFolders() {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetFolders";
@@ -431,7 +420,7 @@
#region GetSubscribedFolders
public StoreFolder[] GetSubscribedFolders(string userName) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT s.FolderId AS FolderId FROM User u, Subscription s WHERE s.UserId = u.UserId AND u.UserName = ?UserName";
cmd.Parameters.Add("UserName", userName);
@@ -459,7 +448,7 @@
#region Subscribe
private bool folderSubscribed(int userId, int folderId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT COUNT(*) FROM Subscription WHERE UserId = ?UserId AND FolderId = ?FolderId";
cmd.Parameters.Add("UserId", userId);
@@ -472,7 +461,7 @@
}
public void Subscribe(string userName, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT UserId FROM User WHERE UserName = ?UserName";
cmd.Parameters.Add("UserName", userName);
@@ -494,7 +483,7 @@
#region UnSubscribe
public void UnSubscribe(string userName, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT UserId FROM User WHERE Username = ?Username";
cmd.Parameters.Add("Username", userName);
@@ -518,7 +507,7 @@
#region GetNextMessageId
public int GetNextMessageId(int messageId, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT FolderMessageId FROM Message WHERE FolderId = ?FolderId AND FolderMessageId > ?MessageId ORDER BY FolderMessageId LIMIT 1";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -531,7 +520,7 @@
}
public int GetNextMessageId(StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT NextMessageId FROM Folder WHERE FolderId = ?FolderId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -545,7 +534,7 @@
#region GetMessageId
public int GetMessageId(int messageOffset, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT FolderMessageId FROM Message WHERE FolderId = ?FolderId ORDER BY FolderMessageId LIMIT 1 OFFSET ?Offset";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -558,7 +547,7 @@
#region GetMessageOffset
public int GetMessageOffset(int messageId, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT COUNT(*) FROM Message WHERE FolderId = ?FolderId AND FolderMessageId <= ?FolderMessageId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -577,7 +566,7 @@
public StoreMessageFlags GetMessageFlags(int messageId, StoreFolder folder) {
StoreMessageFlags flags = StoreMessageFlags.None;
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT SeenFlag, AnsweredFlag, FlaggedFlag, DeletedFlag, DraftFlag, RecentFlag FROM Message WHERE FolderId = ?FolderId AND FolderMessageId = ?FolderMessageId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -614,7 +603,7 @@
#region SetMessageFlags
public void SetMessageFlags(int messageId, StoreFolder folder, StoreMessageFlags flags) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "UPDATE Message SET SeenFlag = ?SeenFlag, AnsweredFlag = ?AnsweredFlag, FlaggedFlag = ?FlaggedFlag, DeletedFlag = ?DeletedFlag, DraftFlag = ?DraftFlag, RecentFlag = ?RecentFlag WHERE FolderId = ?FolderId AND FolderMessageId = ?FolderMessageId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -640,7 +629,7 @@
#region GetInternalDate
public DateTime GetInternalDate(int messageId, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT InternalDate FROM Message WHERE FolderId = ?FolderId AND FolderMessageId = ?FolderMessageId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -653,7 +642,7 @@
#region GetMessageSize
public int GetMessageSize(int messageId, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT Size FROM Message WHERE FolderId = ?FolderId AND FolderMessageId = ?FolderMessageId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -666,7 +655,7 @@
#region GetMessageHeaders
public MessageHeaders GetMessageHeaders(int messageId, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT Headers FROM Message WHERE FolderId = ?FolderId AND FolderMessageId = ?FolderMessageId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -685,7 +674,7 @@
#region GetMessageMimePart
public IMessageBodyPart GetMessageMimePart(int messageId, StoreFolder folder, int messagePart) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT md.Data FROM Message m, MessageData md WHERE m.MessageId = md.MessageId AND m.FolderId = ?FolderId AND m.FolderMessageId = ?FolderMessageId AND md.Part = ?Part";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -700,7 +689,7 @@
#region GetMessage
public Message GetMessage(int messageId, StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT MimeMessage FROM Message WHERE FolderId = ?FolderId AND FolderMessageId = ?FolderMessageId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -765,7 +754,7 @@
#region GetMessageCount
public int GetMessageCount(StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT COUNT(*) as MessageCount FROM Message WHERE FolderId = ?FolderId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -777,7 +766,7 @@
#region GetRecentMessageCount
public int GetRecentMessageCount(StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT COUNT(*) FROM Message WHERE FolderId = ?FolderId AND RecentFlag = true";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -791,7 +780,7 @@
#region PurgeDeletedMessages
public int[] PurgeDeletedMessages(StoreFolder folder) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
ArrayList folderMsgIds = new ArrayList();
MySqlTransaction transaction = cnn.BeginTransaction();
@@ -843,7 +832,7 @@
/// <param name="key">A key to identify which object to get.</param>
/// <returns>The object.</returns>
public object LoadObject(string key) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT Data FROM AuxData WHERE `Key` = ?Key";
cmd.Parameters.Add("Key", key);
@@ -859,7 +848,7 @@
/// </summary>
/// <param name="key">A key to identify the object at load time.</param>
public void SaveObject(object o, string key) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
MySqlTransaction trans = cnn.BeginTransaction();
MySqlCommand cmd = cnn.CreateCommand();
@@ -892,7 +881,7 @@
/// <param name="username">The username to look up.</param>
/// <returns>The matching user or null if non is found.</returns>
public LocalStoreUser GetUser(string name) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
return getUser(name, cnn, null);
}
}
@@ -948,7 +937,7 @@
/// <param name="userId">The Id of the user to look up.</param>
/// <returns>The matching user or null if non is found.</returns>
public LocalStoreUser GetUser(int userId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
return getUser(userId, cnn, null);
}
}
@@ -1005,7 +994,7 @@
/// </summary>
/// <returns>The list of users.</returns>
public LocalStoreUser[] GetUsers() {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
MySqlTransaction transaction = cnn.BeginTransaction();
try {
@@ -1051,7 +1040,7 @@
/// <param name="hardQuota">The hard quota.</param>
/// <returns>The result of the attemp to create a new user.</returns>
public LocalStoreUserResult CreateUser(string username, int? warnQuota, int? hardQuota) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "CreateUser";
@@ -1080,7 +1069,7 @@
/// <param name="userId">The Id of the user to delete.</param>
/// <returns>The result of the attempt to delete a user.</returns>
public LocalStoreUserResult DeleteUser(int userId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "DeleteUser";
@@ -1105,7 +1094,7 @@
/// <param name="user">The user details to update.</param>
/// <returns>The result of the attempt to update the user.</returns>
public LocalStoreUserResult UpdateUser(LocalStoreUser user) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UpdateUser";
@@ -1132,7 +1121,7 @@
/// <param name="userId">The Id of the user to get the folder for.</param>
/// <returns>The list of folders or null if the user Id is invalid.</returns>
public StoreFolder[] GetUserFolders(int userId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetUserFolders";
@@ -1167,7 +1156,7 @@
/// <param name="name">The name to look up.</param>
/// <returns>The matching group or null if non is found.</returns>
public LocalStoreGroup GetGroup(string name) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
return getGroup(name, cnn, null);
}
}
@@ -1221,7 +1210,7 @@
/// <param name="groupId">The Id of the group to look up.</param>
/// <returns>The matching group or null if non is found.</returns>
public LocalStoreGroup GetGroup(int groupId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
return getGroup(groupId, cnn, null);
}
}
@@ -1276,7 +1265,7 @@
/// </summary>
/// <returns>The list of groups.</returns>
public LocalStoreGroup[] GetGroups() {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
MySqlTransaction transaction = cnn.BeginTransaction();
try {
@@ -1318,7 +1307,7 @@
/// <param name="name">The name of the new group.</param>
/// <returns>The result of the attemp to create a new group.</returns>
public LocalStoreGroupResult CreateGroup(string name) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "CreateGroup";
@@ -1345,7 +1334,7 @@
/// <param name="groupId">The Id of the group to delete.</param>
/// <returns>The result of the attempt to delete a group.</returns>
public LocalStoreGroupResult DeleteGroup(int groupId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "DeleteGroup";
@@ -1370,7 +1359,7 @@
/// <param name="group">The updated group.</param>
/// <returns>The result of the attempt to update the group.</returns>
public LocalStoreGroupResult UpdateGroup(LocalStoreGroup group) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UpdateGroup";
@@ -1394,7 +1383,7 @@
#region Folder ACLs
#region Set StoreFolder ACE
public void SetStoreFolderAce(StoreFolder folder, GenericAce<StoreFolderPrivilege> ace) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
setStoreFolderAce(folder, ace, cnn, null);
}
}
@@ -1417,7 +1406,7 @@
#region Remove StoreFolder ACE
public void RemoveStoreFolderAce(StoreFolder folder, string identifier) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "DELETE FROM FolderAcl WHERE FolderId = ?FolderId AND Identifier = ?Identifier";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -1436,7 +1425,7 @@
StoreFolder folder = GetStoreFolder(folderId);
StoreFolderAcl result = new StoreFolderAcl(folder);
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandText = "SELECT Identifier, Allow, Privilege FROM FolderAcl WHERE FolderId = ?FolderId";
cmd.Parameters.Add("FolderId", folder.FolderId);
@@ -1488,7 +1477,7 @@
/// </summary>
/// <returns>The mail domains.</returns>
public MailDomain[] GetMailDomains() {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
MySqlTransaction transaction = cnn.BeginTransaction();
try {
@@ -1531,7 +1520,7 @@
/// <param name="host">The primary host for the mail domain.</param>
/// <returns>The result of the attempt to add the mail domain.</returns>
public LocalStoreMailDomainResult CreateMailDomain(Host host) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
MailDomain mailDomain = new MailDomain(-1, host);
@@ -1561,7 +1550,7 @@
/// <param name="mailDomainId">The Id of the mail domain to delete.</param>
/// <returns>The result of the attempt to delete the domain.</returns>
public LocalStoreMailDomainResult DeleteMailDomain(int mailDomainId) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "DeleteMailDomain";
@@ -1586,11 +1575,12 @@
/// <param name="updatedMailDomain">The updated mail domain.</param>
/// <returns>The result of the attempt to update the mail domain.</returns>
public LocalStoreMailDomainResult UpdateMailDomain(MailDomain updatedMailDomain) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UpdateMailDomain";
cmd.Parameters.Add("PrimaryHost", updatedMailDomain.PrimaryHost.ToString());
+ cmd.Parameters.Add("MailDomainId", updatedMailDomain.MailDomainId);
cmd.Parameters.Add("ObjectData", SerializationHelper.Serialize(updatedMailDomain));
cmd.Parameters.Add("Result", MySqlDbType.Int32);
cmd.Parameters["Result"].Direction = ParameterDirection.Output;
@@ -1613,7 +1603,7 @@
/// </summary>
/// <param name="ace">The privileges and the identifier to put.</param>
public void SetSystemPrivilegeAce(GenericAce<SystemPrivilege> ace) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SetSystemAce";
@@ -1630,7 +1620,7 @@
/// </summary>
/// <param name="identifier">The identifier to remove from the ACL.</param>
public void RemoveSystemPrivilegeAce(string identifier) {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "RemoveSystemAce";
@@ -1645,7 +1635,7 @@
/// </summary>
/// <returns>The system ACL.</returns>
public GenericAcl<SystemPrivilege> GetSystemPrivilegeAcl() {
- using (MySqlConnection cnn = GetConnection()) {
+ using (MySqlConnection cnn = MySqlHelper.GetConnection()) {
using (MySqlCommand cmd = cnn.CreateCommand()) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetSystemAcl";
@@ -1669,181 +1659,5 @@
}
#endregion
#endregion
-
- #region Auth Provider
- #region IAuthProvider
-
- public DateTime GetTokenExpiry(IAuthenticationToken user) {
- // TODO: fix this!
- return DateTime.MaxValue;
- }
-
- public bool TokenExpired(IAuthenticationToken user) {
- // TODO: fix this!!
- return false;
- }
-
- public bool ValidToken(IAuthenticationToken user) {
- // TODO: fix this!!
- return true;
- }
-
- public bool IsLockedOut(IAuthenticationToken user) {
- // TODO: fix this!
- return false;
- }
-
- public bool ChangePassword(IAuthenticationToken user, string newPassword) {
- using (MySqlConnection cnn = GetConnection()) {
- using (MySqlCommand cmd = cnn.CreateCommand()) {
- cmd.CommandText = "UPDATE `User` SET Password = ?Password WHERE Username = ?Username";
- cmd.Parameters.Add("Username", user.Username);
- cmd.Parameters.Add("Password", newPassword);
- int count = cmd.ExecuteNonQuery();
-
- return (count == 1);
- }
- }
- }
- #endregion
-
- #region IHashAuthProvider Members
-
- public IAuthenticationToken Authenticate(string username, string hash, HashAuthType authType) {
- if ((authType & HashAuthType.Ntlm) == HashAuthType.Ntlm) {
- // Get the hash stored in the database.
- using (MySqlConnection cnn = GetConnection()) {
- using (MySqlCommand cmd = cnn.CreateCommand()) {
- cmd.CommandText = "SELECT Password FROM User WHERE Username = ?Username";
- cmd.Parameters.Add("Username", username);
- string dbPassword = (string)cmd.ExecuteScalar();
-
- // Generate a NTLM hash
- Type3Message type3Msg = new Type3Message();
- type3Msg.Password = dbPassword;
- string dbHash = Convert.ToBase64String(type3Msg.NT);
-
- if (dbHash == hash) {
- // Hashes match, return a valid token
- // TODO: get the password expiry details
- return new BasicAuthToken(username, DateTime.MaxValue);
- }
- }
- }
- }
-
- return null;
- }
-
- public HashAuthType HashAuthType {
- get { return HashAuthType.Ntlm; }
- }
-
- #endregion
-
- #region IAuthenticationProvider Members
-
- public IAuthenticationToken Authenticate(string username, string password) {
- // Get the hash stored in the database.
- using (MySqlConnection cnn = GetConnection()) {
- using (MySqlCommand cmd = cnn.CreateCommand()) {
- cmd.CommandText = "SELECT Password FROM User WHERE Username = ?Username";
- cmd.Parameters.Add("Username", username);
- string dbHash = cmd.ExecuteScalar() as string;
-
- if (dbHash == password) {
- // Hashes match, return a valid token
- // TODO: get the password expiry details
- return new BasicAuthToken(username, DateTime.MaxValue);
- }
- }
- }
-
- // Hashes do not match
- return null;
- }
-
- public IAuthenticationToken CreateAuthToken(string username) {
- // Get the hash stored in the database.
- using (MySqlConnection cnn = GetConnection()) {
- using (MySqlCommand cmd = cnn.CreateCommand()) {
- cmd.CommandText = "SELECT COUNT(*) FROM User WHERE Username = ?Username";
- cmd.Parameters.Add("Username", username);
- long count = (long) cmd.ExecuteScalar();
-
- if (count == 1) {
- // TODO: get the password expiry details
- return new BasicAuthToken(username, DateTime.MaxValue);
- }
- }
- }
-
- // No such user
- return null;
- }
- #endregion
- #endregion
-
- #region ILocalStoreUserMap Members
- /// <summary>
- /// Maps the given mailbox to a user.
- /// </summary>
- /// <param name="mailbox">The mailbox to map.</param>
- /// <returns>The user or null.</returns>
- public LocalStoreUser MapMailboxToUser(string mailbox) {
- using (MySqlConnection cnn = GetConnection()) {
- using (MySqlCommand cmd = cnn.CreateCommand()) {
- // First check if the username matches the mailbox
- cmd.CommandText = "SELECT u.UserId FROM User u WHERE u.Username LIKE ?Mailbox";
- cmd.Parameters.Add("Mailbox", mailbox);
- object o = cmd.ExecuteScalar();
-
- if (o is int) {
- return GetUser((int) o);
- }
-
- // Next check if the mailbox maps to a user
- cmd.CommandText = "SELECT u.UserId FROM User u, MailboxMapping m WHERE u.UserId = m.UserId AND m.Mailbox LIKE ?Mailbox";
- cmd.Parameters.Add("Mailbox", mailbox);
- o = cmd.ExecuteScalar();
-
- if (o is int) {
- return GetUser((int) o);
- }
-
- // No such mailbox
- return null;
- }
- }
- }
-
- public Mailbox[] GetUserMailboxes(string username) {
- using (MySqlConnection cnn = GetConnection()) {
- using (MySqlCommand cmd = cnn.CreateCommand()) {
- cmd.CommandText = "SELECT m.Mailbox FROM User u, MailboxMapping m WHERE u.UserId = m.UserId AND u.Username LIKE ?Username";
- cmd.Parameters.Add("Username", username);
-
- using (MySqlDataReader reader = cmd.ExecuteReader()) {
- List<Mailbox> mailboxes = new List<Mailbox>();
-
- while (reader.Read()) {
- string mailboxName = (string)reader["Mailbox"];
- mailboxes.Add(new Mailbox(mailboxName));
- }
-
- return mailboxes.ToArray();
- }
- }
- }
- }
-
- public void AddUserMailbox(string username, Mailbox mailbox) {
- throw new NotImplementedException();
- }
-
- public void RemoveUserMailbox(string username, Mailbox mailbox) {
- throw new NotImplementedException();
- }
- #endregion
}
}
Modified: NMail/branches/luke-dev/NMail.LocalStoreData.MySql/NMail.LocalStoreData.MySql.csproj
===================================================================
--- NMail/branches/luke-dev/NMail.LocalStoreData.MySql/NMail.LocalStoreData.MySql.csproj 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail.LocalStoreData.MySql/NMail.LocalStoreData.MySql.csproj 2006-10-24 12:21:38 UTC (rev 66)
@@ -105,9 +105,12 @@
<Compile Include="Configuration\MySqlLocalStoreDataConfiguration.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="MySqlAuthProvider.cs" />
+ <Compile Include="MySqlHelper.cs" />
<Compile Include="MySqlLocalStoreData.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="MySqlUserMap.cs" />
<Content Include="MySqlLocalStore.sql" />
<Content Include="NMail.LocalStoreData.Mysql.build" />
</ItemGroup>
Modified: NMail/branches/luke-dev/NMail.Server.Console/NMail.config
===================================================================
--- NMail/branches/luke-dev/NMail.Server.Console/NMail.config 2006-10-19 11:08:29 UTC (rev 65)
+++ NMail/branches/luke-dev/NMail.Server.Console/NMail.config 2006-10-24 12:21:38 UTC (rev 66)
@@ -21,7 +21,7 @@
</Services>
<NamedServices>
- <NamedService Name="AuthProvider" Type="NMail.LocalStoreData.MySql.MySqlLocalStoreData, NMail.LocalStoreData.MySql" />
+ <NamedService Name="AuthProvider" Type="NMail.LocalStoreData.MySql.MySqlAuthProvider, NMail.LocalStoreData.MySql" />
<NamedService Name="DnsClient" Type="NMail.DnsClient.DnsClient, NMail.DnsClient" />
<NamedService Name="MessageRouter" Type="NMail.MessageRouter.MessageRouter, NMail.MessageRouter" />
<NamedService Name="LocalStore" Type="NMail.LocalStore.LocalStore, NMail.LocalStore" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|