Skip to content

Commit

Permalink
SSL/TLS support and authentication for SMTP (#2940)
Browse files Browse the repository at this point in the history
* Added SSL/TLS support and authentication for SMTP

* Update timesketch/lib/utils.py

* Update data/timesketch.conf

* Update timesketch/lib/utils.py

* Update timesketch/lib/utils.py

---------

Co-authored-by: Johan Berggren <[email protected]>
  • Loading branch information
fazledyn-or and berggren authored Oct 25, 2023
1 parent c14672d commit c7f5a91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions data/timesketch.conf
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ EMAIL_RECIPIENTS = []
# Configuration to construct URLs for resources.
EXTERNAL_HOST_URL = 'https://localhost'

# SSL/TLS support for emails
EMAIL_TLS = False
EMAIL_SSL = False

# Email support for authentication
EMAIL_AUTH_USERNAME = ""
EMAIL_AUTH_PASSWORD = ""

#-------------------------------------------------------------------------------
# Sigma Settings

Expand Down
26 changes: 26 additions & 0 deletions timesketch/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ def send_email(subject, body, to_username, use_html=False):
email_smtp_server = current_app.config.get("EMAIL_SMTP_SERVER")
email_from_user = current_app.config.get("EMAIL_FROM_ADDRESS", "timesketch")
email_user_whitelist = current_app.config.get("EMAIL_USER_WHITELIST", [])
email_login_username = current_app.config.get("EMAIL_AUTH_USERNAME")
email_login_password = current_app.config.get("EMAIL_AUTH_PASSWORD")
email_ssl = current_app.config.get("EMAIL_SSL")
email_tls = current_app.config.get("EMAIL_TLS")

if not email_enabled:
raise RuntimeError("Email notifications are not enabled, aborting.")
Expand Down Expand Up @@ -626,6 +630,28 @@ def send_email(subject, body, to_username, use_html=False):
msg.add_header("Content-Type", email_content_type)
msg.set_payload(body)

# EMAIL_SSL in timesketch.conf must be set to True
if email_ssl:
smtp = smtplib.SMTP_SSL(email_smtp_server)
if email_login_username and email_login_password:
smtp.login(email_login_username, email_login_password)
smtp.sendmail(msg["From"], [msg["To"]], msg.as_string())
smtp.quit()
return
# EMAIL_TLS in timesketch.conf must be set to True
if email_tls:
smtp = smtplib.SMTP(email_smtp_server)
smtp.ehlo()
smtp.starttls()
if email_login_username and email_login_password:
smtp.login(email_login_username, email_login_password)
smtp.sendmail(msg["From"], [msg["To"]], msg.as_string())
smtp.quit()
return

# default - no SSL/TLS configured
smtp = smtplib.SMTP(email_smtp_server)
if email_login_username and email_login_password:
smtp.login(email_login_username, email_login_password)
smtp.sendmail(msg["From"], [msg["To"]], msg.as_string())
smtp.quit()

0 comments on commit c7f5a91

Please sign in to comment.