diff --git a/admin/database_updates/2.8.3.php b/admin/database_updates/2.8.3.php new file mode 100644 index 000000000..6233d84f8 --- /dev/null +++ b/admin/database_updates/2.8.3.php @@ -0,0 +1,32 @@ + 0 AND `domain_id` IS NULL"); diff --git a/admin/modals/tag/tag_add.php b/admin/modals/tag/tag_add.php index 473f61791..9c17f5e6d 100644 --- a/admin/modals/tag/tag_add.php +++ b/admin/modals/tag/tag_add.php @@ -19,6 +19,8 @@ if (isset($_GET['type'])) { $type_display = "Asset"; } elseif ($type === 6) { $type_display = "Vendor"; + } elseif ($type === 7) { + $type_display = "Domain"; } } ob_start(); diff --git a/admin/modals/tag/tag_edit.php b/admin/modals/tag/tag_edit.php index f7cfc8a5d..48a266bb5 100644 --- a/admin/modals/tag/tag_edit.php +++ b/admin/modals/tag/tag_edit.php @@ -24,6 +24,8 @@ if ($tag_type == 1) { $tag_type_display = "Asset"; } elseif ($tag_type == 6) { $tag_type_display = "Vendor"; +} elseif ($tag_type == 7) { + $tag_type_display = "Domain"; } else { $tag_type_display = "Unknown"; } diff --git a/admin/post/demo_data_model.php b/admin/post/demo_data_model.php index c1b9ee85f..92ae28bb9 100644 --- a/admin/post/demo_data_model.php +++ b/admin/post/demo_data_model.php @@ -1283,28 +1283,63 @@ function demoBuildClient($mysqli, $profile, $index, $context, &$counts) { demoBuildNetworkIps($mysqli, $profile, $assets, $network_ids, $counts); demoBuildPatchPanel($mysqli, $profile, $assets, $network_ids, $months, $counts); - // Domain, DNS and the certificate on it + // Domain, DNS and the certificate on it. Half renew by themselves; the + // email records vary so the Email checks have something to say - DMARC + // enforced, monitoring only, or missing + $domain_auto_renew = $index % 2; + $domain_m365 = strpos($profile['mail_servers'], 'google') === false; $domain_id = starterInsert($mysqli, 'domains', [ 'domain_name' => $profile['domain'], 'domain_description' => 'Primary domain - website and email', 'domain_expire' => demoDate(-1 * (60 + ($index * 21))), + 'domain_auto_renew' => $domain_auto_renew, 'domain_ip' => '203.0.113.' . (10 + $index), - 'domain_name_servers' => 'ns1.' . $profile['dns_host'] . ', ns2.' . $profile['dns_host'], - 'domain_raw_whois' => 'Registrar: Harbour Registrar Services. Status: clientTransferProhibited. Registrant: ' . $profile['name'] . '.', - 'domain_mail_servers' => $profile['mail_servers'], + 'domain_name_servers' => 'ns1.' . $profile['dns_host'] . "\nns2." . $profile['dns_host'], + 'domain_raw_whois' => "Registrar: Harbour Registrar Services\nRegistered: " . demoDate(365 * (6 + $index % 9) + $index * 11) . "\nExpires: " . demoDate(-1 * (60 + ($index * 21))) . "\nStatus: client transfer prohibited\nNameservers: ns1." . $profile['dns_host'] . ', ns2.' . $profile['dns_host'], + 'domain_mail_servers' => '0 ' . $profile['mail_servers'], + 'domain_dmarc' => [0 => 'v=DMARC1; p=reject; rua=mailto:dmarc@' . $profile['domain'], 1 => 'v=DMARC1; p=none; rua=mailto:dmarc@' . $profile['domain'], 2 => ''][$index % 3], + 'domain_dkim' => $index % 4 === 3 ? '' : ($domain_m365 ? "selector1\nselector2" : 'google'), + 'domain_refreshed_at' => demoDateTime(1 + $index % 6, 4, 0), // These three are vendor IDs, not names - who hosts the site, who runs // DNS, and who the mail lands with 'domain_webhost' => intval($profile['build'] === 1 ? ($context['org_vendors']['Nextcloud Hosting Partner'] ?? 0) : $primary_vendor_id), 'domain_dnshost' => intval($primary_vendor_id), 'domain_mailhost' => intval($context['org_vendors']['Cascade Cloud Licensing'] ?? 0), - 'domain_txt' => 'v=spf1 include:' . $profile['dns_host'] . ' -all', - 'domain_notes' => 'Registrar login is held in the credential vault. Renewal is set to manual so it cannot lapse without a ticket.', + 'domain_txt' => 'v=spf1 include:' . $profile['dns_host'] . ' -all' . ($domain_m365 ? "\nMS=ms" . substr(demoSerial($profile['domain'], 8), 0, 8) : "\ngoogle-site-verification=" . strtolower(demoSerial($profile['domain'], 20))), + 'domain_notes' => 'Registrar login is held in the credential vault. ' . ($domain_auto_renew ? 'Auto-renew is on and charged to the card on file - check it before the card expires.' : 'Renewal is set to manual so it cannot lapse without a ticket.'), 'domain_registrar' => $primary_vendor_id, 'domain_client_id' => $client_id, 'domain_created_at' => demoMonthDateTime($months, 7, 9, 45), 'domain_accessed_at' => demoDateTime($index % 13, 15, 5), ]); + demoAttachTags($mysqli, 'domain_tags', 'domain_id', 'tag_id', $domain_id, 7, ['Primary', 'Managed DNS']); + + // Every third client also keeps a campaign domain nobody remembers - parked, + // no mail, and close to lapsing or already lapsed + if ($index % 3 === 0) { + $campaign_days = $index % 2 ? -12 : 20; + $campaign_domain_id = starterInsert($mysqli, 'domains', [ + 'domain_name' => 'get' . $profile['domain'], + 'domain_description' => 'Old campaign domain - forwards to the main site', + 'domain_expire' => demoDate(-1 * $campaign_days), + 'domain_auto_renew' => 0, + 'domain_ip' => '203.0.113.' . (10 + $index), + 'domain_name_servers' => 'ns1.' . $profile['dns_host'] . "\nns2." . $profile['dns_host'], + 'domain_txt' => 'v=spf1 -all', + 'domain_dmarc' => 'v=DMARC1; p=reject', + 'domain_raw_whois' => "Registrar: Harbour Registrar Services\nRegistered: " . demoDate(365 * 3 + $index) . "\nStatus: client transfer prohibited", + 'domain_notes' => 'Ask the client whether to keep it before it renews.', + 'domain_registrar' => $primary_vendor_id, + 'domain_dnshost' => intval($primary_vendor_id), + 'domain_client_id' => $client_id, + 'domain_created_at' => demoMonthDateTime($months, 7, 10, 5), + 'domain_refreshed_at' => demoDateTime(2 + $index % 5, 4, 0), + ]); + demoAttachTags($mysqli, 'domain_tags', 'domain_id', 'tag_id', $campaign_domain_id, 7, ['Parked', 'Marketing']); + $counts['documentation']++; + } + $certificate_id = starterInsert($mysqli, 'certificates', [ 'certificate_name' => 'www.' . $profile['domain'], 'certificate_description' => $profile['cert_issuer'] === "Let's Encrypt" ? 'Public website certificate, renewed automatically by the reverse proxy' : 'Public website certificate, renewed manually at expiry', diff --git a/admin/post/starter_content_model.php b/admin/post/starter_content_model.php index e578e67bb..849732924 100644 --- a/admin/post/starter_content_model.php +++ b/admin/post/starter_content_model.php @@ -30,7 +30,7 @@ function starterContentPacks() { 'tags' => [ 'label' => 'Tags', 'icon' => 'fa-tags', - 'description' => 'Client, location, contact, credential, asset and vendor tags.', + 'description' => 'Client, location, contact, credential, asset, vendor and domain tags.', ], 'ticket_templates' => [ 'label' => 'Ticket Templates', @@ -675,6 +675,14 @@ function starterContentTags() { [6, 'Hosting', '#17a2b8', 'globe'], [6, 'Professional Services', '#343a40', 'briefcase'], [6, 'Authorised Contacts Only', '#d81b60', 'user-lock'], + + // Domain + [7, 'Primary', '#007bff', 'star'], + [7, 'Client-owned', '#6f42c1', 'user-tie'], + [7, 'Managed DNS', '#20c997', 'sitemap'], + [7, 'Parked', '#adb5bd', 'parking'], + [7, 'Marketing', '#fd7e14', 'bullhorn'], + [7, 'Do Not Renew', '#dc3545', 'ban'], ]; return $tags; diff --git a/admin/tags.php b/admin/tags.php index 8d2320d86..7b5826a98 100644 --- a/admin/tags.php +++ b/admin/tags.php @@ -20,6 +20,7 @@ $tag_types = [ 4 => ['label' => 'Credential', 'icon' => 'fa-key'], 5 => ['label' => 'Asset', 'icon' => 'fa-desktop'], 6 => ['label' => 'Vendor', 'icon' => 'fa-building'], + 7 => ['label' => 'Domain', 'icon' => 'fa-globe'], ]; // Label for the selected type diff --git a/agent/ajax.php b/agent/ajax.php index fa9921bb2..bba15665c 100644 --- a/agent/ajax.php +++ b/agent/ajax.php @@ -1780,24 +1780,63 @@ if (isset($_GET['ai_ticket_summary'])) { } // Stops people trying to use sub-domains in the domains tracker -if (isset($_GET['apex_domain_check'])) { +/* + * The domain name in a New / Edit Domain form, checked as it is typed: what it + * is saved as, whether DNS answers for it, whether it looks like a subdomain + * of one that does, and whether the client has it already. + */ +if (isset($_GET['domain_check'])) { enforceUserPermission('module_support', 2); - $domain = escapeSql($_GET['domain']); + $input = (string) ($_GET['domain'] ?? ''); + $name = domainNormalizeName($input); + $check_client_id = intval($_GET['client_id'] ?? 0); + $check_domain_id = intval($_GET['domain_id'] ?? 0); + $response = ['state' => '', 'message' => '', 'name' => $name]; - $response['message'] = ""; // default + if (trim($input) === '') { + echo json_encode($response); + exit; + } + if ($name === '') { + echo json_encode(['state' => 'bad', 'message' => "That isn't a domain name - something like example.com.", 'name' => '']); + exit; + } - if (strlen($domain) >= 4) { + $saved_as = strcasecmp(trim($input), $name) !== 0 ? "Saved as $name. " : ''; - // SOA record check - // This isn't 100%, as sub-domains can have their own SOA but will capture 99% - if (!checkdnsrr($domain, 'SOA')) { - $response['message'] = " Domain name is invalid."; + // Already on this client? + if ($check_client_id) { + $client_id = $check_client_id; + enforceClientAccess(); + $name_sql = escapeSql($name); + if (mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM domains WHERE domain_name = '$name_sql' AND domain_client_id = $check_client_id AND domain_id <> $check_domain_id LIMIT 1"))) { + echo json_encode(['state' => 'bad', 'message' => $saved_as . "$name is already on this client.", 'name' => $name]); + exit; } + } + if (@checkdnsrr($name, 'SOA')) { + $response = ['state' => 'ok', 'message' => $saved_as . 'Found in DNS - its records and expiry are looked up when it is saved.', 'name' => $name]; + } else { + // Nothing for the name itself - is it a subdomain of one that does answer? + $parts = explode('.', $name); + $parent = ''; + while (count($parts) > 2) { + array_shift($parts); + $candidate = implode('.', $parts); + if (@checkdnsrr($candidate, 'SOA')) { + $parent = $candidate; + break; + } + } + $response = $parent + ? ['state' => 'warn', 'message' => $saved_as . "That looks like a subdomain of $parent - add $parent instead, unless it is registered on its own.", 'name' => $name] + : ['state' => 'warn', 'message' => $saved_as . "Nothing answers for it in DNS yet - check the spelling. It can still be saved.", 'name' => $name]; } echo json_encode($response); + exit; } // Get internal users/techs diff --git a/agent/domains.php b/agent/domains.php index 5af3510c0..962656db1 100644 --- a/agent/domains.php +++ b/agent/domains.php @@ -1,5 +1,13 @@ ['label' => 'All', 'where' => ''], + 'starred' => ['label' => 'Starred', 'icon' => 'fa-star', 'where' => 'domains.domain_favorite = 1'], + 'soon' => ['label' => "Expiring in $soon_days days", 'icon' => 'fa-hourglass-half', 'where' => "domains.domain_expire BETWEEN CURDATE() AND CURDATE() + INTERVAL $soon_days DAY"], + 'expired' => ['label' => 'Expired', 'icon' => 'fa-calendar-times', 'where' => 'domains.domain_expire < CURDATE()'], + 'noauto' => ['label' => 'No auto-renew', 'icon' => 'fa-hand-paper', 'where' => 'domains.domain_auto_renew = 0'], + 'mail' => ['label' => 'Email gaps', 'icon' => 'fa-envelope-open-text', 'where' => domainMailGapSql()], +]; +$show = isset($shows[$_GET['show'] ?? '']) ? ($_GET['show'] ?? '') : ''; +$show_query = $shows[$show]['where'] ? 'AND ' . $shows[$show]['where'] : ''; + +$expire_days = $_GET['expire_days'] ?? ''; +$expire_query = ''; +if ($expire_days === 'expired') { + $expire_query = 'AND domains.domain_expire < CURDATE()'; +} elseif (intval($expire_days) > 0) { + $expire_query = 'AND domains.domain_expire BETWEEN CURDATE() AND CURDATE() + INTERVAL ' . intval($expire_days) . ' DAY'; } -$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS domains.*, clients.*, - registrar.vendor_id AS registrar_id, - registrar.vendor_name AS registrar_name, - dnshost.vendor_id AS dnshost_id, - dnshost.vendor_name AS dnshost_name, - mailhost.vendor_id AS mailhost_id, - mailhost.vendor_name AS mailhost_name, - webhost.vendor_id AS webhost_id, - webhost.vendor_name AS webhost_name - FROM domains - LEFT JOIN clients ON client_id = domain_client_id - LEFT JOIN vendors AS registrar ON domains.domain_registrar = registrar.vendor_id - LEFT JOIN vendors AS dnshost ON domains.domain_dnshost = dnshost.vendor_id - LEFT JOIN vendors AS mailhost ON domains.domain_mailhost = mailhost.vendor_id - LEFT JOIN vendors AS webhost ON domains.domain_webhost = webhost.vendor_id - WHERE (domains.domain_name LIKE '%$q%' OR domains.domain_description LIKE '%$q%' OR registrar.vendor_name LIKE '%$q%' OR dnshost.vendor_name LIKE '%$q%' OR mailhost.vendor_name LIKE '%$q%' OR webhost.vendor_name LIKE '%$q%' OR client_name LIKE '%$q%') - AND $archive_query - " . clientScopeSql('domain_client_id') . " - $client_query - $expire_query - ORDER BY $sort $order LIMIT $record_from, $record_to"); +// Only these sort - filter_header.php will hand through any lowercase word, and +// an unknown column takes the page down with a mysqli exception +$sortable_columns = ['domain_name' => 'domains.domain_name', 'domain_expire' => 'domains.domain_expire', 'registrar_name' => 'registrar.vendor_name', + 'client_name' => 'client_name', 'domain_created_at' => 'domains.domain_created_at', 'domain_refreshed_at' => 'domains.domain_refreshed_at']; +if (!isset($sortable_columns[$sort])) { + $sort = 'domain_name'; +} +// Domains without an expiry go last whichever way the expiry is sorted +$sort_sql = ($sort === 'domain_expire' ? 'domains.domain_expire IS NULL, ' : '') . $sortable_columns[$sort] . " $order"; + +[$domain_columns, $domain_joins] = domainSelectSql(); +$search_query = "AND (domains.domain_name LIKE '%$q%' OR domains.domain_description LIKE '%$q%' OR domains.domain_notes LIKE '%$q%' + OR registrar.vendor_name LIKE '%$q%' OR dnshost.vendor_name LIKE '%$q%' OR mailhost.vendor_name LIKE '%$q%' OR webhost.vendor_name LIKE '%$q%' OR client_name LIKE '%$q%' + OR domains.domain_id IN (SELECT domain_id FROM domain_tags JOIN tags USING (tag_id) WHERE tag_name LIKE '%$q%'))"; +$scope_query = clientScopeSql('domain_client_id'); +$filters = "$archive_query $tag_query $vendor_query $search_query $expire_query $scope_query $client_query"; + +$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS $domain_columns + FROM domains $domain_joins + WHERE $filters $show_query + ORDER BY domains.domain_favorite DESC, $sort_sql, domains.domain_name ASC LIMIT $record_from, $record_to"); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); +$domains = []; +while ($row = mysqli_fetch_assoc($sql)) { + $domains[intval($row['domain_id'])] = $row; +} +$domains_tags = tagsForRecords('domain', array_keys($domains)); + +// How many of each, for the pills - everything in scope, before the pill itself +$count_sql = []; +foreach ($shows as $key => $s) { + $count_sql[] = $key === '' ? 'COUNT(*)' : "COALESCE(SUM({$s['where']}), 0)"; +} +$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . " FROM domains $domain_joins WHERE $filters")); +$counts = []; +foreach (array_keys($shows) as $i => $key) { + $counts[$key] = intval($row[$i]); +} + +$filter_url = function ($changes) { + $params = array_merge($_GET, $changes); + unset($params['page']); + return '?' . http_build_query(array_filter($params, function ($v) { return $v !== '' && $v !== null; })); +}; + +// Vendors for the filter - the ones registering or hosting a domain in this scope +$sql_vendors_filter = mysqli_query($mysqli, "SELECT DISTINCT vendor_id, vendor_name, vendor_description, vendor_client_id FROM vendors + JOIN domains ON vendor_id IN (domain_registrar, domain_dnshost, domain_webhost, domain_mailhost) + LEFT JOIN clients ON client_id = domain_client_id + WHERE $archive_query $scope_query $client_query + ORDER BY vendor_name ASC"); + +// Vendors the bulk "set registrar or host" offers - the client's and ours, or only ours across clients +$bulk_vendor_options = $can_edit ? domainVendorChoices($client_url ? $client_id : 0) : []; + ?>