Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add SMTP service settings to environment variables so it can be exposed via Docker (#105)

Leendert de Borstb3ddf9408936
diff --git a/.env.example b/.env.example
index 67eed3107..6f97da333 100644
--- a/.env.example
+++ b/.env.example
@@ -1 +1,3 @@
JWT_KEY=
+SMTP_ALLOWED_DOMAINS=example.tld
+SMTP_TLS_ENABLED=false
diff --git a/README.md b/README.md
index 9b531f440..7c04840fe 100644
--- a/README.md
+++ b/README.md
@@ -48,7 +48,7 @@ $ cd AliasVault
# Make init script executable
$ chmod +x init.sh
-# Run the init script
+# Run the init script and follow the steps
$ ./init.sh
```
diff --git a/docker-compose.yml b/docker-compose.yml
index 743b5130a..3e0e51f75 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -31,4 +31,6 @@ services:
ports:
- "25:25"
- "587:587"
+ env_file:
+ - .env
restart: always
diff --git a/init.sh b/init.sh
index 4e01c35c1..560b99198 100755
--- a/init.sh
+++ b/init.sh
@@ -49,6 +49,44 @@ populate_jwt_key() {
fi
}
+# Function to ask the user for SMTP_ALLOWED_DOMAINS
+set_smtp_allowed_domains() {
+ if ! grep -q "^SMTP_ALLOWED_DOMAINS=" "$ENV_FILE" || [ -z "$(grep "^SMTP_ALLOWED_DOMAINS=" "$ENV_FILE" | cut -d '=' -f2)" ]; then
+ printf "${YELLOW}Please enter the domains that should be allowed to send email, separated by commas:${NC}\n"
+ read -r smtp_allowed_domains
+ if grep -q "^SMTP_ALLOWED_DOMAINS=" "$ENV_FILE"; then
+ awk -v domains="$smtp_allowed_domains" '/^SMTP_ALLOWED_DOMAINS=/ {$0="SMTP_ALLOWED_DOMAINS="domains} 1' "$ENV_FILE" > "$ENV_FILE.tmp" && mv "$ENV_FILE.tmp" "$ENV_FILE"
+ else
+ printf "SMTP_ALLOWED_DOMAINS=${smtp_allowed_domains}\n" >> "$ENV_FILE"
+ fi
+ printf "${GREEN}> SMTP_ALLOWED_DOMAINS has been set in $ENV_FILE.${NC}\n"
+ else
+ printf "${CYAN}> SMTP_ALLOWED_DOMAINS already exists and has a value in $ENV_FILE.${NC}\n"
+ fi
+}
+
+# Function to ask the user if TLS should be enabled for email
+set_smtp_tls_enabled() {
+ if ! grep -q "^SMTP_TLS_ENABLED=" "$ENV_FILE" || [ -z "$(grep "^SMTP_TLS_ENABLED=" "$ENV_FILE" | cut -d '=' -f2)" ]; then
+ printf "${YELLOW}Do you want TLS enabled for email? (yes/no):${NC}\n"
+ read -r tls_enabled
+ tls_enabled=$(echo "$tls_enabled" | tr '[:upper:]' '[:lower:]')
+ if [ "$tls_enabled" = "yes" ] || [ "$tls_enabled" = "y" ]; then
+ tls_enabled="true"
+ else
+ tls_enabled="false"
+ fi
+ if grep -q "^SMTP_TLS_ENABLED=" "$ENV_FILE"; then
+ awk -v tls="$tls_enabled" '/^SMTP_TLS_ENABLED=/ {$0="SMTP_TLS_ENABLED="tls} 1' "$ENV_FILE" > "$ENV_FILE.tmp" && mv "$ENV_FILE.tmp" "$ENV_FILE"
+ else
+ printf "SMTP_TLS_ENABLED=${tls_enabled}\n" >> "$ENV_FILE"
+ fi
+ printf "${GREEN}> SMTP_TLS_ENABLED has been set to ${tls_enabled} in $ENV_FILE.${NC}\n"
+ else
+ printf "${CYAN}> SMTP_TLS_ENABLED already exists and has a value in $ENV_FILE.${NC}\n"
+ fi
+}
+
# Function to print the CLI logo
print_logo() {
printf "${MAGENTA}\n"
@@ -69,6 +107,8 @@ print_logo
printf "${BLUE}Initializing AliasVault...${NC}\n"
create_env_file
populate_jwt_key
+set_smtp_allowed_domains
+set_smtp_tls_enabled
printf "${BLUE}Initialization complete.${NC}\n"
printf "\n"
printf "To build the images and start the containers, run the following command:\n"
diff --git a/src/Services/AliasVault.SmtpService/DatabaseMessageStore.cs b/src/Services/AliasVault.SmtpService/DatabaseMessageStore.cs
index a8fba6e73..fe071411e 100644
--- a/src/Services/AliasVault.SmtpService/DatabaseMessageStore.cs
+++ b/src/Services/AliasVault.SmtpService/DatabaseMessageStore.cs
@@ -200,16 +200,16 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c
if (email.MessagePlain != null && !String.IsNullOrEmpty(email.MessagePlain) && email.MessagePlain.Length > 3)
{
// Replace any newline characters with a space
- string plainToPlainText = Regex.Replace(email.MessagePlain, @"\t|\n|\r", " ");
+ string plainToPlainText = Regex.Replace(email.MessagePlain, @"\t|\n|\r", " ", RegexOptions.NonBacktracking);
// Remove all "-" or "=" characters if there are 3 or more in a row
- plainToPlainText = Regex.Replace(plainToPlainText, @"-{3,}|\={3,}", "");
+ plainToPlainText = Regex.Replace(plainToPlainText, @"-{3,}|\={3,}", "", RegexOptions.NonBacktracking);
// Remove any non-printable characters
- plainToPlainText = Regex.Replace(plainToPlainText, @"[^\u0020-\u007E]", "");
+ plainToPlainText = Regex.Replace(plainToPlainText, @"[^\u0020-\u007E]", "", RegexOptions.NonBacktracking);
// Replace multiple spaces with a single space
- plainToPlainText = Regex.Replace(plainToPlainText, @"\s+", " ");
+ plainToPlainText = Regex.Replace(plainToPlainText, @"\s+", " ", RegexOptions.NonBacktracking);
// Trim start and end of string
plainToPlainText = plainToPlainText.Trim();
@@ -223,16 +223,16 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c
string htmlToPlainText = Uglify.HtmlToText(email.MessageHtml).ToString();
// Replace any newline characters with a space
- htmlToPlainText = Regex.Replace(htmlToPlainText, @"\t|\n|\r", " ");
+ htmlToPlainText = Regex.Replace(htmlToPlainText, @"\t|\n|\r", " ", RegexOptions.NonBacktracking);
// Remove all "-" or "=" characters if there are 3 or more in a row
- htmlToPlainText = Regex.Replace(htmlToPlainText, @"-{3,}|\={3,}", "");
+ htmlToPlainText = Regex.Replace(htmlToPlainText, @"-{3,}|\={3,}", "", RegexOptions.NonBacktracking);
// Remove any non-printable characters
- htmlToPlainText = Regex.Replace(htmlToPlainText, @"[^\u0020-\u007E]", "");
+ htmlToPlainText = Regex.Replace(htmlToPlainText, @"[^\u0020-\u007E]", "", RegexOptions.NonBacktracking);
// Replace multiple spaces with a single space
- htmlToPlainText = Regex.Replace(htmlToPlainText, @"\s+", " ");
+ htmlToPlainText = Regex.Replace(htmlToPlainText, @"\s+", " ", RegexOptions.NonBacktracking);
// Trim start and end of string
htmlToPlainText = htmlToPlainText.Trim();
diff --git a/src/Services/AliasVault.SmtpService/Program.cs b/src/Services/AliasVault.SmtpService/Program.cs
index adb267a8e..b12d71ce5 100644
--- a/src/Services/AliasVault.SmtpService/Program.cs
+++ b/src/Services/AliasVault.SmtpService/Program.cs
@@ -16,9 +16,15 @@ using SmtpServer.Storage;
var builder = Host.CreateApplicationBuilder(args);
-// Read settings from appsettings.json.
-ConfigurationManager configuration = builder.Configuration;
-Config config = configuration.GetSection("Config").Get<Config>()!;
+// Create global config object, get values from environment variables.
+Config config = new Config();
+var emailDomains = Environment.GetEnvironmentVariable("SMTP_ALLOWED_DOMAINS")
+ ?? throw new KeyNotFoundException("SMTP_ALLOWED_DOMAINS environment variable is not set.");
+config.AllowedToDomains = emailDomains.Split(',').ToList();
+
+var tlsEnabled = Environment.GetEnvironmentVariable("SMTP_TLS_ENABLED")
+ ?? throw new KeyNotFoundException("SMTP_TLS_ENABLED environment variable is not set.");
+config.SmtpTlsEnabled = tlsEnabled;
builder.Services.AddSingleton(config);
builder.Services.AddSingleton<DbConnection>(container =>
diff --git a/src/Services/AliasVault.SmtpService/Properties/launchSettings.json b/src/Services/AliasVault.SmtpService/Properties/launchSettings.json
index a6513c699..4f7c9c76e 100644
--- a/src/Services/AliasVault.SmtpService/Properties/launchSettings.json
+++ b/src/Services/AliasVault.SmtpService/Properties/launchSettings.json
@@ -3,7 +3,9 @@
"AliasVault.SmtpService": {
"commandName": "Project",
"environmentVariables": {
- "DOTNET_ENVIRONMENT": "Development"
+ "DOTNET_ENVIRONMENT": "Development",
+ "SMTP_ALLOWED_DOMAINS": "example.tld",
+ "SMTP_TLS_ENABLED": "false"
},
"dotnetRunMessages": true
},
@@ -12,4 +14,4 @@
}
},
"$schema": "http://json.schemastore.org/launchsettings.json"
-}
\ No newline at end of file
+}
diff --git a/src/Services/AliasVault.SmtpService/appsettings.json b/src/Services/AliasVault.SmtpService/appsettings.json
index 258416faf..c74fb8b2a 100644
--- a/src/Services/AliasVault.SmtpService/appsettings.json
+++ b/src/Services/AliasVault.SmtpService/appsettings.json
@@ -7,11 +7,5 @@
},
"ConnectionStrings": {
"AliasServerDbContext": "Data Source=../../../database/AliasServerDb.sqlite"
- },
- "Config": {
- "SmtpTlsEnabled": "false",
- "AllowedToDomains": [
- "example.tld"
- ]
}
}