2026-11-23 22:41:57 +01:00
|
|
|
from datetime import timedelta
|
2026-11-23 23:27:02 +01:00
|
|
|
from pathlib import Path
|
2026-11-23 22:41:57 +01:00
|
|
|
|
2026-11-23 23:27:02 +01:00
|
|
|
from django import forms
|
2026-11-23 22:41:57 +01:00
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
|
from django.db import transaction
|
|
|
|
|
from django.http import Http404
|
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
from django.views.decorators.cache import never_cache
|
|
|
|
|
from django.views.decorators.debug import sensitive_variables
|
2026-11-23 23:27:02 +01:00
|
|
|
from django.views.decorators.http import require_http_methods
|
2026-11-23 22:41:57 +01:00
|
|
|
|
|
|
|
|
from coulobre.domain_names import canonical_domain
|
2026-11-23 23:27:02 +01:00
|
|
|
from coulobre.operator_node import SOCKET
|
2026-11-23 22:41:57 +01:00
|
|
|
from coulobre.web_certificates import ACTIONS
|
|
|
|
|
from coulobre.web_node import WebSite
|
|
|
|
|
|
|
|
|
|
from .access import customer_for_user
|
|
|
|
|
from .certificate_uploads import CertificateUploadForm, seal_upload
|
|
|
|
|
from .independent_websites import active_websites
|
|
|
|
|
from .installation_features import panel_feature_allowed
|
2026-11-23 23:27:02 +01:00
|
|
|
from .models import HostingAccount, OperatorOperation, WebsiteCertificateJob
|
2026-11-23 22:41:57 +01:00
|
|
|
from .resource_requests import lock_hosting_account, require_active_services
|
|
|
|
|
from .sftp import site_is_active
|
|
|
|
|
|
|
|
|
|
|
2026-11-23 23:27:02 +01:00
|
|
|
class AcmePreferencesForm(forms.Form):
|
|
|
|
|
email = forms.EmailField(
|
|
|
|
|
required=False,
|
|
|
|
|
label="ACME registration contact",
|
|
|
|
|
max_length=254,
|
|
|
|
|
help_text="Leave blank to use the web node's existing registration.",
|
|
|
|
|
)
|
|
|
|
|
wildcard = forms.BooleanField(
|
|
|
|
|
required=False,
|
|
|
|
|
label="Include a wildcard name",
|
|
|
|
|
help_text="Use the web node's configured DNS-01 hooks.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.fields[
|
|
|
|
|
"wildcard"
|
|
|
|
|
].disabled = not settings.WEBSITE_WILDCARD_CERTIFICATES
|
|
|
|
|
|
|
|
|
|
|
2026-11-23 22:41:57 +01:00
|
|
|
def account_certificate_sites(account):
|
|
|
|
|
sites = [
|
|
|
|
|
website.spec
|
|
|
|
|
for website in active_websites(account).filter(suspended=False)
|
|
|
|
|
]
|
|
|
|
|
if site_is_active(account):
|
|
|
|
|
sites.insert(0, WebSite(account.pk, account.primary_domain))
|
|
|
|
|
return {site.domain: site for site in sites}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
|
def queue_certificate(
|
2026-11-23 23:27:02 +01:00
|
|
|
account,
|
|
|
|
|
site,
|
|
|
|
|
actor,
|
|
|
|
|
action,
|
|
|
|
|
customer_id,
|
|
|
|
|
*,
|
|
|
|
|
encrypted_upload="",
|
|
|
|
|
acme=None,
|
2026-11-23 22:41:57 +01:00
|
|
|
):
|
|
|
|
|
account = lock_hosting_account(account.pk, customer_id)
|
|
|
|
|
require_active_services(account)
|
|
|
|
|
if account_certificate_sites(account).get(site.domain) != site:
|
|
|
|
|
raise ValidationError("This website is no longer active.")
|
|
|
|
|
recent = account.certificate_jobs.filter(site=site.manifest())
|
|
|
|
|
if recent.filter(finished_at__isnull=True).exists():
|
|
|
|
|
raise ValidationError("Wait for the current certificate operation.")
|
2026-11-23 23:27:02 +01:00
|
|
|
if (action == "reissue" or acme) and recent.filter(
|
|
|
|
|
action__in=("reissue", "issue"),
|
|
|
|
|
requested_at__gte=timezone.now() - timedelta(hours=12),
|
|
|
|
|
).exists():
|
2026-11-23 22:41:57 +01:00
|
|
|
raise ValidationError(
|
|
|
|
|
"Wait 12 hours before requesting another reissue."
|
|
|
|
|
)
|
|
|
|
|
WebsiteCertificateJob.objects.create(
|
|
|
|
|
account=account,
|
|
|
|
|
site=site.manifest(),
|
|
|
|
|
actor=actor,
|
|
|
|
|
requested_customer_id=customer_id,
|
|
|
|
|
action=action,
|
|
|
|
|
encrypted_upload=encrypted_upload,
|
2026-11-23 23:27:02 +01:00
|
|
|
acme=acme or {},
|
2026-11-23 22:41:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@never_cache
|
|
|
|
|
@sensitive_variables("encrypted_upload", "upload_form")
|
|
|
|
|
@require_http_methods(("GET", "HEAD", "POST"))
|
|
|
|
|
def customer_certificates(request, pk):
|
|
|
|
|
customer = customer_for_user(request.user)
|
|
|
|
|
if customer is None:
|
|
|
|
|
raise PermissionDenied
|
|
|
|
|
account = get_object_or_404(
|
|
|
|
|
HostingAccount, pk=pk, customer=customer, retired_at__isnull=True
|
|
|
|
|
)
|
|
|
|
|
sites = account_certificate_sites(account)
|
|
|
|
|
try:
|
|
|
|
|
domain = canonical_domain(
|
|
|
|
|
request.GET.get(
|
|
|
|
|
"domain", next(iter(sites), account.primary_domain)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except ValueError as error:
|
|
|
|
|
raise Http404 from error
|
|
|
|
|
site = sites.get(domain)
|
|
|
|
|
if "domain" in request.GET and site is None:
|
|
|
|
|
raise Http404
|
|
|
|
|
tab = request.GET.get("tab", "manage")
|
|
|
|
|
if tab not in {"manage", "acme", "history"}:
|
|
|
|
|
raise Http404
|
|
|
|
|
enabled = panel_feature_allowed("LOCAL_WEB_HTTPS")
|
|
|
|
|
problem = ""
|
|
|
|
|
is_upload = (
|
|
|
|
|
request.method == "POST" and request.POST.get("action") == "upload"
|
|
|
|
|
)
|
|
|
|
|
upload_form = CertificateUploadForm(
|
|
|
|
|
request.POST if is_upload else None,
|
|
|
|
|
request.FILES if is_upload else None,
|
|
|
|
|
)
|
2026-11-23 23:27:02 +01:00
|
|
|
is_acme = (
|
|
|
|
|
request.method == "POST"
|
|
|
|
|
and tab == "acme"
|
|
|
|
|
and request.POST.get("action") in {"issue", "reissue"}
|
|
|
|
|
)
|
|
|
|
|
acme_form = AcmePreferencesForm(request.POST if is_acme else None)
|
|
|
|
|
if (
|
|
|
|
|
request.method == "POST"
|
|
|
|
|
and (not is_upload or upload_form.is_valid())
|
|
|
|
|
and (not is_acme or acme_form.is_valid())
|
|
|
|
|
):
|
2026-11-23 22:41:57 +01:00
|
|
|
if not site or not enabled:
|
|
|
|
|
raise Http404
|
|
|
|
|
action = request.POST.get("action")
|
|
|
|
|
if action not in ACTIONS:
|
|
|
|
|
raise Http404
|
|
|
|
|
try:
|
|
|
|
|
encrypted_upload = (
|
|
|
|
|
seal_upload(site, customer.pk, upload_form)
|
|
|
|
|
if is_upload
|
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
queue_certificate(
|
|
|
|
|
account,
|
|
|
|
|
site,
|
|
|
|
|
request.user,
|
|
|
|
|
action,
|
|
|
|
|
customer.pk,
|
|
|
|
|
encrypted_upload=encrypted_upload,
|
2026-11-23 23:27:02 +01:00
|
|
|
acme=acme_form.cleaned_data if is_acme else None,
|
2026-11-23 22:41:57 +01:00
|
|
|
)
|
|
|
|
|
except HostingAccount.DoesNotExist:
|
|
|
|
|
raise Http404 from None
|
|
|
|
|
except ValidationError as error:
|
|
|
|
|
problem = " ".join(error.messages)
|
|
|
|
|
else:
|
|
|
|
|
return redirect(
|
|
|
|
|
reverse(
|
|
|
|
|
"panel:customer_certificates",
|
|
|
|
|
args=[account.pk],
|
|
|
|
|
query={"domain": domain, "tab": "history"},
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
history = []
|
|
|
|
|
certificate = None
|
|
|
|
|
if site:
|
|
|
|
|
jobs = account.certificate_jobs.filter(
|
|
|
|
|
site=site.manifest(),
|
|
|
|
|
requested_customer_id=customer.pk,
|
|
|
|
|
)
|
|
|
|
|
history = list(jobs.order_by("-pk")[:25])
|
|
|
|
|
certificate = jobs.exclude(certificate={}).order_by("-pk").first()
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|
"hosting/customer_certificates.html",
|
|
|
|
|
{
|
|
|
|
|
"account": account,
|
|
|
|
|
"sites": sites,
|
|
|
|
|
"domain": domain,
|
|
|
|
|
"tab": tab,
|
|
|
|
|
"certificate": certificate,
|
|
|
|
|
"upload_form": upload_form,
|
2026-11-23 23:27:02 +01:00
|
|
|
"acme_form": acme_form,
|
2026-11-23 22:41:57 +01:00
|
|
|
"history": history,
|
|
|
|
|
"enabled": enabled,
|
|
|
|
|
"problem": problem,
|
|
|
|
|
"site": site,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@never_cache
|
2026-11-23 23:27:02 +01:00
|
|
|
@require_http_methods(("GET", "HEAD", "POST"))
|
2026-11-23 22:41:57 +01:00
|
|
|
def operator_certificates(request):
|
|
|
|
|
if not (request.user.is_superuser and request.user.is_verified()):
|
|
|
|
|
raise PermissionDenied
|
|
|
|
|
mode = request.GET.get("tab", "websites")
|
|
|
|
|
if mode not in {"websites", "server"}:
|
|
|
|
|
raise Http404
|
2026-11-23 23:27:02 +01:00
|
|
|
latest = OperatorOperation.objects.filter(action="server-tls").order_by(
|
|
|
|
|
"-pk"
|
|
|
|
|
)
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
if mode != "server" or not Path(SOCKET).is_socket():
|
|
|
|
|
raise Http404
|
|
|
|
|
from .tasks import run_operator_operation
|
|
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
|
type(request.user).objects.select_for_update().get(
|
|
|
|
|
pk=request.user.pk
|
|
|
|
|
)
|
|
|
|
|
if not latest.filter(finished_at__isnull=True).exists():
|
|
|
|
|
job = OperatorOperation.objects.create(
|
|
|
|
|
actor=request.user, action="server-tls"
|
|
|
|
|
)
|
|
|
|
|
transaction.on_commit(
|
|
|
|
|
lambda: run_operator_operation.delay(job.pk)
|
|
|
|
|
)
|
|
|
|
|
return redirect(
|
|
|
|
|
reverse("panel:operator_certificates", query={"tab": "server"})
|
|
|
|
|
)
|
2026-11-23 22:41:57 +01:00
|
|
|
accounts = HostingAccount.objects.filter(retired_at__isnull=True).order_by(
|
|
|
|
|
"primary_domain"
|
|
|
|
|
)
|
|
|
|
|
page_obj = Paginator(accounts, 30).get_page(request.GET.get("page"))
|
|
|
|
|
sites = (
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
site.domain,
|
|
|
|
|
account,
|
|
|
|
|
account.certificate_jobs.filter(site=site.manifest())
|
|
|
|
|
.exclude(certificate={})
|
|
|
|
|
.order_by("-pk")
|
|
|
|
|
.first(),
|
|
|
|
|
)
|
|
|
|
|
for account in page_obj
|
|
|
|
|
for site in account_certificate_sites(account).values()
|
|
|
|
|
]
|
|
|
|
|
if mode == "websites"
|
|
|
|
|
else []
|
|
|
|
|
)
|
|
|
|
|
return render(
|
|
|
|
|
request,
|
|
|
|
|
"hosting/operator_certificates.html",
|
|
|
|
|
{
|
|
|
|
|
"mode": mode,
|
|
|
|
|
"sites": sites,
|
|
|
|
|
"page_obj": page_obj,
|
|
|
|
|
"is_paginated": page_obj.has_other_pages,
|
|
|
|
|
"panel_origin": settings.PASSWORD_RESET_ORIGIN,
|
2026-11-23 23:27:02 +01:00
|
|
|
"tls_job": latest.first(),
|
|
|
|
|
"tls_available": Path(SOCKET).is_socket(),
|
|
|
|
|
"tls_result": latest.filter(
|
|
|
|
|
finished_at__isnull=False, problem=""
|
|
|
|
|
).first(),
|
2026-11-23 22:41:57 +01:00
|
|
|
"imap_host": settings.MAIL_IMAP_HOST,
|
|
|
|
|
"submission_host": settings.MAIL_SUBMISSION_HOST,
|
|
|
|
|
},
|
|
|
|
|
)
|