Files
postfixadmin/model/DkimsigningHandler.php
T
Kent Riboe 650a91bd05 Improve DKIM: auto-generate keypair, descriptive signing table (#1029)
Implements the improvements suggested by @fredkilbourn in #1024, cleaned up for current master.

**Changes:**

1. **Auto-generate RSA 2048 keypair** when creating a new DKIM key. Only
generates on the create form, not on list views or edits.

2. **Descriptive signing table dropdown** shows `domain:selector
(description) [id]` instead of just the numeric ID.

3. **Domain:Selector and Description columns** in the signing table list
view via LEFT JOIN.

4. **Order by domain:selector** instead of raw dkim_id.

**Technical notes:**
- Uses `$this->new` check to avoid key generation overhead on list/edit
views
- Handles MySQL (`CONCAT()`) vs SQLite/PostgreSQL (`||`) string
concatenation
- Uses named arguments for `pacol()` per current conventions

Two separate commits. Credit to @fredkilbourn for the original patch.
2026-05-23 14:41:13 +01:00

164 lines
5.9 KiB
PHP

<?php
# $Id$
/**
* Handler for domain key signing table
*/
class DkimsigningHandler extends PFAHandler
{
protected string $db_table = 'dkim_signing';
protected string $id_field = 'id';
protected string $order_by = '__dkim_domain_selector, author';
protected function initStruct()
{
// Get Domains as options for authors
$domain_handler = new DomainHandler(0, $this->admin_username);
$domain_handler->getList('1=1');
// Get Mailboxes as options for authors
$mail_handler = new MailboxHandler(0, $this->admin_username);
$mail_handler->getList('1=1');
// Get Domain Keys
$dkim_handler = new DkimHandler(0, $this->admin_username);
$dkim_handler->getList('1=1');
$authors = array_merge(
array_keys($domain_handler->result()),
array_keys($mail_handler->result())
);
// Build descriptive labels for the DKIM key dropdown: "domain:selector (description) [id]"
$dkim_options = array_map(
fn($v) => "{$v['domain_name']}:{$v['selector']} ({$v['description']}) [{$v['id']}]",
$dkim_handler->result()
);
// MySQL uses CONCAT(), SQLite and PostgreSQL use || for string concatenation
$dkim_table = table_by_key('dkim');
if (db_mysql()) {
$concat_expr = "CONCAT(domain_name, ':', selector)";
} else {
$concat_expr = "(domain_name || ':' || selector)";
}
$this->struct = array(
# field name allow display in... type $PALANG label $PALANG description default / options / ...
# edit? form list
'id' => self::pacol(0, 0, 1, 'num', 'pFetchmail_field_id', '', '', array(), 0, 1),
'dkim_id' => self::pacol(1, 1, 1, 'enma', 'pDkim_field_dkim_id', 'pDkim_field_dkim_id_desc', '', $dkim_options),
'__dkim_domain_selector' => self::pacol(0, 0, 1, 'text', 'pDkim_field_domain_and_selector', '',
dont_write_to_db: 1,
extrafrom: 'LEFT JOIN ('
. " SELECT id AS __dkim_id,"
. " $concat_expr AS __dkim_domain_selector,"
. " description AS __dkim_description"
. " FROM $dkim_table"
. ') AS __dkim ON __dkim_id = dkim_id'),
'__dkim_description' => self::pacol(0, 0, 1, 'text', 'pDkim_field_description', '', dont_write_to_db: 1),
'author' => self::pacol(1, 1, 1, 'enum', 'pDkim_field_author', 'pDkim_field_author_desc', '', $authors),
);
}
protected function initMsg()
{
$this->msg['error_already_exists'] = 'dkim_signing_already_exists';
$this->msg['error_does_not_exist'] = 'dkim_signing_does_not_exist';
$this->msg['confirm_delete'] = 'confirm_delete_dkim';
if ($this->new) {
$this->msg['logname'] = 'create_dkim_signing_entry';
$this->msg['store_error'] = 'pFetchmail_database_save_error';
$this->msg['successmessage'] = 'pFetchmail_database_save_success';
} else {
$this->msg['logname'] = 'edit_dkim_entry';
$this->msg['store_error'] = 'pFetchmail_database_save_error';
$this->msg['successmessage'] = 'pFetchmail_database_save_success';
}
}
public function webformConfig()
{
$required_role = 'global-admin';
if (Config::bool('dkim_all_admins')) {
$required_role = 'admin';
}
return array(
# $PALANG labels
'formtitle_create' => 'pDkim_new_sign',
'formtitle_edit' => 'pDkim_edit_sign',
'create_button' => 'pFetchmail_new_entry',
# various settings
'required_role' => $required_role,
'listview' => 'list.php?table=dkimsigning',
'early_init' => 0,
);
}
protected function validate_new_id()
{
# auto_increment - any non-empty ID is an error
if ($this->id != '') {
$this->errormsg[$this->id_field] = 'auto_increment value, you must pass an empty string!';
return false;
}
return true;
}
/**
* @return boolean
*/
public function delete()
{
if (! $this->view()) {
$this->errormsg[] = Config::Lang($this->msg['error_does_not_exist']);
return false;
}
db_delete($this->db_table, $this->id_field, $this->id);
db_log($this->result['author'], 'delete_dkim_signing_entry', $this->result['id']);
$this->infomsg[] = Config::Lang_f('pDelete_delete_success', $this->result['author']);
return true;
}
public function domain_from_id()
{
return '';
}
protected function no_domain_field()
{
$domain_handler = new DomainHandler(0, $this->admin_username);
$domain_handler->getList('1=1');
$this->allowed_domains = array_keys($domain_handler->result());
}
/**
* Filters to only allowed domains, as author can be either a mailbox or a domain
* @param $db_result
* @return array
*/
protected function read_from_db_postprocess($db_result)
{
return array_filter($db_result, function ($row) {
$domain = $row['author'];
$at_pos = strpos($domain, '@');
if ($at_pos) {
$domain = preg_split('/@/', $domain)[1];
}
return in_array($domain, $this->allowed_domains);
});
}
}
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */