2023-04-08 10:22:09 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import imaplib
|
|
|
|
import poplib
|
|
|
|
import smtplib
|
2023-04-20 16:14:01 +02:00
|
|
|
import sys
|
|
|
|
import managesieve
|
2023-04-08 10:22:09 +02:00
|
|
|
|
|
|
|
SERVER='localhost'
|
2023-05-31 11:17:59 +02:00
|
|
|
USERNAME='user_UTF8@mailu.io'
|
|
|
|
PASSWORD='password€'
|
|
|
|
#https://github.com/python/cpython/issues/73936
|
|
|
|
#SMTPlib does not support UTF8 passwords.
|
|
|
|
USERNAME_ASCII='user@mailu.io'
|
|
|
|
PASSWORD_ASCII='password'
|
|
|
|
|
2023-04-08 10:22:09 +02:00
|
|
|
|
|
|
|
def test_imap(server, username, password):
|
2023-05-31 11:17:59 +02:00
|
|
|
auth = lambda data : f'\x00{username}\x00{password}'
|
2023-04-08 10:26:22 +02:00
|
|
|
print(f'Authenticating to imaps://{username}:{password}@{server}:993/')
|
2023-04-08 10:22:09 +02:00
|
|
|
with imaplib.IMAP4_SSL(server) as conn:
|
2023-05-31 11:17:59 +02:00
|
|
|
conn.authenticate('PLAIN', auth)
|
2023-04-08 10:26:22 +02:00
|
|
|
conn.noop()
|
|
|
|
print('OK')
|
|
|
|
print(f'Authenticating to imaps://{username}:{password}@{server}:143/')
|
2023-04-08 10:22:09 +02:00
|
|
|
with imaplib.IMAP4(server) as conn:
|
|
|
|
conn.starttls()
|
2023-05-31 11:17:59 +02:00
|
|
|
conn.authenticate('PLAIN', auth)
|
2023-04-08 10:26:22 +02:00
|
|
|
conn.noop()
|
|
|
|
print('OK')
|
|
|
|
print(f'Authenticating to imap://{username}:{password}@{server}:143/')
|
2023-04-08 10:22:09 +02:00
|
|
|
try:
|
|
|
|
with imaplib.IMAP4(server) as conn:
|
2023-05-31 11:17:59 +02:00
|
|
|
conn.authenticate('PLAIN', auth)
|
2023-04-08 10:22:09 +02:00
|
|
|
print(f'Authenticating to imap://{username}:{password}@{server}:143/ worked without STARTTLS!')
|
2023-04-20 16:14:01 +02:00
|
|
|
sys.exit(102)
|
2023-04-08 10:26:22 +02:00
|
|
|
except imaplib.IMAP4.error:
|
2023-04-08 12:47:41 +02:00
|
|
|
print('NOK - expected')
|
2023-04-08 10:22:09 +02:00
|
|
|
|
|
|
|
def test_pop3(server, username, password):
|
|
|
|
print(f'Authenticating to pop3s://{username}:{password}@{server}:995/')
|
|
|
|
conn = poplib.POP3_SSL(server)
|
|
|
|
conn.capa()
|
|
|
|
conn.user(username)
|
|
|
|
conn.pass_(password)
|
|
|
|
conn.close()
|
2023-04-08 10:26:22 +02:00
|
|
|
print('OK')
|
2023-04-08 10:22:09 +02:00
|
|
|
print(f'Authenticating to pop3s://{username}:{password}@{server}:110/')
|
|
|
|
conn = poplib.POP3(server)
|
|
|
|
conn.stls()
|
|
|
|
conn.capa()
|
|
|
|
conn.user(username)
|
|
|
|
conn.pass_(password)
|
|
|
|
conn.close()
|
2023-04-08 10:26:22 +02:00
|
|
|
print('OK')
|
2023-04-08 10:22:09 +02:00
|
|
|
print(f'Authenticating to pop3://{username}:{password}@{server}:110/')
|
|
|
|
try:
|
|
|
|
conn = poplib.POP3(server)
|
|
|
|
conn.capa()
|
|
|
|
conn.user(username)
|
|
|
|
conn.pass_(password)
|
|
|
|
conn.close()
|
|
|
|
print(f'Authenticating to pop3://{username}:{password}@{server}:110/ worked without STARTTLS!')
|
2023-04-20 16:14:01 +02:00
|
|
|
sys.exit(103)
|
2023-04-08 10:22:09 +02:00
|
|
|
except poplib.error_proto:
|
2023-04-08 12:47:41 +02:00
|
|
|
print('NOK - expected')
|
2023-04-08 10:22:09 +02:00
|
|
|
|
|
|
|
def test_SMTP(server, username, password):
|
2023-04-08 10:26:22 +02:00
|
|
|
print(f'Authenticating to smtps://{username}:{password}@{server}:465/')
|
2023-04-08 10:22:09 +02:00
|
|
|
with smtplib.SMTP_SSL(server) as conn:
|
|
|
|
conn.ehlo()
|
|
|
|
conn.login(username, password)
|
2023-04-08 10:26:22 +02:00
|
|
|
print('OK')
|
|
|
|
print(f'Authenticating to smtps://{username}:{password}@{server}:587/')
|
|
|
|
with smtplib.SMTP(server, 587) as conn:
|
2023-04-08 10:22:09 +02:00
|
|
|
conn.ehlo()
|
|
|
|
conn.starttls()
|
|
|
|
conn.ehlo()
|
|
|
|
conn.login(username, password)
|
2023-04-08 10:26:22 +02:00
|
|
|
print('OK')
|
2023-04-08 10:22:09 +02:00
|
|
|
try:
|
2023-04-08 10:26:22 +02:00
|
|
|
print(f'Authenticating to smtp://{username}:{password}@{server}:587/')
|
|
|
|
with smtplib.SMTP(server, 587) as conn:
|
|
|
|
conn.ehlo()
|
|
|
|
conn.login(username, password)
|
2023-04-08 12:47:41 +02:00
|
|
|
print(f'Authenticating to smtp://{username}:{password}@{server}:587/ worked!')
|
2023-04-20 16:14:01 +02:00
|
|
|
sys.exit(104)
|
2023-04-08 10:26:22 +02:00
|
|
|
except smtplib.SMTPNotSupportedError:
|
2023-04-08 12:47:41 +02:00
|
|
|
print('NOK - expected')
|
2023-04-08 10:26:22 +02:00
|
|
|
#port 25 should fail
|
|
|
|
try:
|
|
|
|
print(f'Authenticating to smtps://{username}:{password}@{server}:25/')
|
|
|
|
with smtplib.SMTP(server) as conn:
|
|
|
|
conn.ehlo()
|
|
|
|
conn.starttls()
|
|
|
|
conn.ehlo()
|
|
|
|
conn.login(username, password)
|
2023-04-08 12:47:41 +02:00
|
|
|
print(f'Authenticating to smtps://{username}:{password}@{server}:25/ worked!')
|
2023-04-20 16:14:01 +02:00
|
|
|
sys.exit(105)
|
2023-04-08 10:26:22 +02:00
|
|
|
except smtplib.SMTPNotSupportedError:
|
2023-04-08 12:47:41 +02:00
|
|
|
print('NOK - expected')
|
2023-04-08 10:26:22 +02:00
|
|
|
try:
|
|
|
|
print(f'Authenticating to smtp://{username}:{password}@{server}:25/')
|
2023-04-08 10:22:09 +02:00
|
|
|
with smtplib.SMTP(server) as conn:
|
|
|
|
conn.ehlo()
|
|
|
|
conn.login(username, password)
|
2023-04-08 12:47:41 +02:00
|
|
|
print(f'Authenticating to smtp://{username}:{password}@{server}:25/ worked without STARTTLS!')
|
2023-04-20 16:14:01 +02:00
|
|
|
sys.exit(106)
|
2023-04-08 10:22:09 +02:00
|
|
|
except smtplib.SMTPNotSupportedError:
|
2023-04-08 12:47:41 +02:00
|
|
|
print('NOK - expected')
|
2023-04-08 10:22:09 +02:00
|
|
|
|
2023-04-20 16:14:01 +02:00
|
|
|
def test_managesieve(server, username, password):
|
|
|
|
print(f'Authenticating to sieve://{username}:{password}@{server}:4190/')
|
|
|
|
m=managesieve.MANAGESIEVE(server)
|
|
|
|
try:
|
|
|
|
m.login('PLAIN', username, password)
|
|
|
|
print(f'Worked without STARTTLS!')
|
|
|
|
sys.exit(107)
|
|
|
|
except managesieve.MANAGESIEVE.abort:
|
|
|
|
pass
|
|
|
|
|
|
|
|
m=managesieve.MANAGESIEVE(server, use_tls=True)
|
|
|
|
if m.login('', username, 'wrongpass') != 'NO':
|
|
|
|
print(f'Authenticating to sieve://{username}:{password}@{server}:4190/ with wrong creds has worked!')
|
|
|
|
sys.exit(108)
|
|
|
|
|
|
|
|
if m.login('', username, password) != 'OK':
|
|
|
|
print(f'Authenticating to sieve://{username}:{password}@{server}:4190/ has failed!')
|
|
|
|
sys.exit(109)
|
2023-05-31 11:17:59 +02:00
|
|
|
|
2023-04-20 16:14:01 +02:00
|
|
|
if m.listscripts()[0] != 'OK':
|
|
|
|
print(f'Listing scripts failed!')
|
|
|
|
sys.exit(110)
|
|
|
|
print('OK')
|
|
|
|
|
2023-04-08 10:22:09 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_imap(SERVER, USERNAME, PASSWORD)
|
|
|
|
test_pop3(SERVER, USERNAME, PASSWORD)
|
2023-05-31 11:17:59 +02:00
|
|
|
test_SMTP(SERVER, USERNAME_ASCII, PASSWORD_ASCII)
|
2023-04-20 16:14:01 +02:00
|
|
|
test_managesieve(SERVER, USERNAME, PASSWORD)
|
2023-05-31 11:17:59 +02:00
|
|
|
#https://github.com/python/cpython/issues/73936
|
|
|
|
#SMTPlib does not support UTF8 passwords.
|