Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autodiscover file #10

Merged
merged 8 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
app.mailbox_username: '%env(MAILBOX_USERNAME)%'

services:
# default configuration for services in *this* file
Expand Down
36 changes: 36 additions & 0 deletions migrations/Version20231014141450.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231014141450 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function isTransactional(): bool
{
return false;
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE domains ADD mailhost VARCHAR(255) NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE domains DROP mailhost');
}
}
4 changes: 0 additions & 4 deletions src/Command/CheckmailboxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$mailresult = $this->open_mailbox($this->imap);
$stats['new_emails'] = $mailresult['num_emails'];

// dump($mailresult['reports']['dmarc_reports']);
// dump($mailresult['reports']['smtptls_reports']);
// dd();

foreach($mailresult['reports']['dmarc_reports'] as $dmarcreport){
$stats['new_dmarc_reports']++;

Expand Down
7 changes: 7 additions & 0 deletions src/Controller/DomainsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,16 @@ public function edit(Domains $domain, Request $request): Response
}
$setup['users_form'] = $form->createView();

$dns_info = array(
'now' => new \DateTime('now'),
'ip' => $request->server->get('SERVER_ADDR'),
'email' => $this->getParameter('app.mailbox_username'),
);

return $this->render('domains/edit.html.twig', [
'menuactive' => 'domains',
'domain' => $domain,
'dns_info' => $dns_info,
'form' => $form,
'breadcrumbs' => array(
array('name' => $this->translator->trans("Domains"), 'url' => $this->router->generate('app_domains')),
Expand Down
31 changes: 31 additions & 0 deletions src/Controller/PolicyFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,35 @@ public function policyfile(Request $request, EntityManagerInterface $em): Respon
$response->headers->set('Content-Type', 'text/plain');
return $response;
}

#[Route('/autodiscover/autodiscover.xml', name: 'app_autodiscover_file')]
public function autodiscoverfile(Request $request, EntityManagerInterface $em): Response
{
$domain = str_replace("autodiscover.", "",$request->getHost());
$domain = str_replace("autoconfig.", "",$domain);
$repository = $this->em->getRepository(Domains::class);
$domain = $repository->findOneBy(array('fqdn' => $domain));
if($domain){
preg_match("/\<EMailAddress\>(.*?)\<\/EMailAddress\>/", file_get_contents("php://input"), $matches);
if(!array_key_exists('1', $matches)){
$matches[1] = "";
}

$response = $this->render('policy_file/autodiscover.xml.twig', [
'loginname' => $matches[1],
'mailsubdomain' => $domain->getMailhost(),
]);
}
else {
$response = $this->render('policy_file/autodiscover.xml.twig', [
'loginname' => "",
'mailsubdomain' => ""
]);
}


$response->headers->set('Content-Type', 'application/xml');

return $response;
}
}
15 changes: 15 additions & 0 deletions src/Entity/Domains.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Domains
#[ORM\Column(options: ['default' => '86400'])]
private ?int $sts_maxage = null;

#[ORM\Column(length: 255)]
private ?string $mailhost = null;

public function __construct()
{
$this->reports = new ArrayCollection();
Expand Down Expand Up @@ -185,4 +188,16 @@ public function setStsMaxage(int $sts_maxage): static

return $this;
}

public function getMailhost(): ?string
{
return $this->mailhost;
}

public function setMailhost(string $mailhost): static
{
$this->mailhost = $mailhost;

return $this;
}
}
3 changes: 3 additions & 0 deletions src/Form/DomainFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('fqdn', TextType::class, [
'label' => 'Domain name',
])
->add('mailhost', TextType::class, [
'label' => 'Mailhost',
])
->add('sts_version', ChoiceType::class, [
'choices' => [
'STSv1' => 'STSv1'
Expand Down
132 changes: 97 additions & 35 deletions templates/domains/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
{% block body %}
{{ form_start(form) }}
<div class="row my-4">
<div class="col-6 col-md-6 col-lg-6 mb-4 mb-lg-0">
<div class="col-12 col-lg-6 mb-4 mb-lg-0">
<div class="card">
<h5 class="card-header">Edit domain</h5>
<div class="card-body">
<p class="card-text">
{{ form_row(form.fqdn) }}
{{ form_row(form.mailhost) }}
</p>
</div>
</div>
Expand All @@ -52,45 +53,38 @@
<br>
<span class="text-muted">You can not remove MX-Records that are bound to an MTA-TLS report. Trying to do so will result in a 500 error.</span>
<div class="js-mxrecord-wrapper"
data-prototype="<div class='col-xs-4 clearfix js-mxrecord-item'>
<span class='float-start w-75'>
data-prototype="<div class='col-xs-4 js-mxrecord-item row'>
<div class='col-9'>
{{ form_row(form.mx_records.vars.prototype.name)|e('html_attr') }}
</span>
<span class='float-end'>
<span class='clearfix'>
<span class='float-start'>
{{ form_row(form.mx_records.vars.prototype.in_sts)|e('html_attr') }}
</span>
<span class='float-end'>
<a href='#' class='js-remove-mxrecord'>
<svg height='20px' width='20px' xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke-width='1.5' stroke='currentColor' class='w-6 h-6'>
<path stroke-linecap='round' stroke-linejoin='round' d='M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0' />
</svg>
</a>
</span>
</span>
</div>
<div class='col-2'>
{{ form_row(form.mx_records.vars.prototype.in_sts)|e('html_attr') }}
</div>
<div class='col-1'>
<a href='#' class='js-remove-mxrecord'>
<svg height='20px' width='20px' xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke-width='1.5' stroke='currentColor' class='w-6 h-6'>
<path stroke-linecap='round' stroke-linejoin='round' d='M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0' />
</svg>
</a>
</div>
</div>"
data-index="{{ form.mx_records|length }}">
{% for mxrecordFrom in form.mx_records %}
<div class="col-xs-4 clearfix js-mxrecord-item">
<span class="float-start w-75">
<div class="col-xs-4 js-mxrecord-item row">
<div class="col-9">
{{ form_errors(mxrecordFrom) }}
{{ form_row(mxrecordFrom.name) }}
</span>
<span class="float-end">
<span class="clearfix">
<span class="float-start">
{{ form_row(mxrecordFrom.in_sts) }}
</span>
<span class="float-end">
<a href="#" class="js-remove-mxrecord">
<svg height="20px" width="20px" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
</svg>
</a>
</span>
</span>
</span>
</div>
<div class="col-2">
{{ form_row(mxrecordFrom.in_sts) }}
</div>
<div class="col-1">
<a href="#" class="js-remove-mxrecord">
<svg height="20px" width="20px" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
</svg>
</a>
</div>
</div>
{% endfor %}
<a href="#" class="js-mxrecord-add">
Expand All @@ -106,7 +100,7 @@
</div>
</div>
</div>
<div class="col-6 col-md-6 col-lg-6 mb-4 mb-lg-0">
<div class="col-12 col-lg-6 mb-4 mb-lg-0">
<div class="card">
<h5 class="card-header">STS Policy</h5>
<div class="card-body">
Expand All @@ -117,6 +111,74 @@
</p>
</div>
</div>
<p></p>
<div class="card">
<h5 class="card-header">DNS Settings</h5>
<div class="card-body">
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#collapseDNS" aria-expanded="false" aria-controls="collapseDNS">
Show Example DNS Records
</button>
<div class="collapse" id="collapseDNS">
<p class="card-text">
<br>
You can create the following DNS Records for this domain:<br>
Please note that you need to update the STSv1 with a higher number each time you update the policy.<br>
This will enable MTA-STS, TLS-RPT, DMARC and Outlook Autoconfig.
<table class="table">
<thead>
<tr>
<th scope="col">Type</th>
<th scope="col">Name</th>
<th scope="col">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>autoconfig.{{ domain.fqdn }}</td>
<td>{{ dns_info.ip }}</td>
</tr>
<tr>
<td>A</td>
<td>autodiscover.{{ domain.fqdn }}</td>
<td>{{ dns_info.ip }}</td>
</tr>
<tr>
<td>A</td>
<td>mta-sts.{{ domain.fqdn }}</td>
<td>{{ dns_info.ip }}</td>
</tr>
<tr>
<td>A</td>
<td>_autodiscover._tcp.{{ domain.fqdn }}</td>
<td>{{ dns_info.ip }}</td>
</tr>
<tr>
<td>TXT</td>
<td>_mta-sts.{{ domain.fqdn }}</td>
<td>"v=STSv1; id={{ dns_info.now|date("YmdHis") }};"</td>
</tr>
<tr>
<td>TXT</td>
<td>_smtp._tls.{{ domain.fqdn }}</td>
<td>"v=TLSRPTv1; rua=mailto:{{ dns_info.email }}"</td>
</tr>
<tr>
<td>TXT</td>
<td>_dmarc.{{ domain.fqdn }}</td>
<td>"v=DMARC1; p=reject; rua=mailto:{{ dns_info.email }}; ruf=mailto:{{ dns_info.email }}; fo=1"</td>
</tr>
<tr>
<td>TXT</td>
<td>default._domainkey.{{ domain.fqdn }}</td>
<td>[Key from your DKIM installation]</td>
</tr>
</tbody>
</table>
</p>
</div>
</div>
</div>
</div>
</div>
<p></p>
Expand Down
2 changes: 2 additions & 0 deletions templates/domains/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<tr>
<th scope="col">Id</th>
<th scope="col">Domain FQDN</th>
<th scope="col">Mailhost</th>
<th scope="col">MX Records</th>
<th scope="col">Total DMARC reports</th>
<th scope="col">Total SMTP-TLS policies</th>
Expand All @@ -25,6 +26,7 @@
<tr>
<th scope="row">{{ domain.id }}</th>
<td>{{ domain.fqdn }}</td>
<td>{{ domain.mailhost }}</td>
<td>
{% for mx in domain.mxrecords %}
{{ mx.name }}<br>
Expand Down
41 changes: 41 additions & 0 deletions templates/policy_file/autodiscover.xml.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8" ?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
<Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
<Account>
<AccountType>email</AccountType>
<Action>settings</Action>
<Protocol>
<Type>IMAP</Type>
<Server>{{ mailsubdomain }}</Server>
<Port>993</Port>
<DomainRequired>off</DomainRequired>
<LoginName>{{ loginname }}</LoginName>
<SPA>off</SPA>
<SSL>on</SSL>
<AuthRequired>on</AuthRequired>
</Protocol>
<Protocol>
<Type>SMTP</Type>
<Server>{{ mailsubdomain }}</Server>
<Port>587</Port>
<DomainRequired>off</DomainRequired>
<LoginName>{{ loginname }}</LoginName>
<SPA>off</SPA>
<Encryption>TLS</Encryption>
<AuthRequired>on</AuthRequired>
<UsePOPAuth>off</UsePOPAuth>
<SMTPLast>off</SMTPLast>
</Protocol>
</Account>
</Response>
</Autodiscover>
{# https://apache.tutorials24x7.com/blog/outlook-autodiscover-using-apache-virtual-host #}
{# Input is the following
<Autodiscover xmlns="https://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006">
<Request>
<EMailAddress>[email protected]</EMailAddress>
<AcceptableResponseSchema>https://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>
</Request>
</Autodiscover>
source: https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/pox-autodiscover-request-for-exchange
#}
Loading