import {readFileSync} from 'node:fs'; // Administrator-owned transport settings; never accept these values from HTTP input. export function mailBrokerCommand({file=process.env.DUNE_MAIL_BROKER_SETTINGS||'/etc/dune-mail-broker/settings.json'}={}){ const config=JSON.parse(readFileSync(file,'utf8')); if(config.version!==1)throw Error('Unsupported mail broker settings version'); const transport=config.transport; if(transport?.mode==='local'){ const command=transport.local?.command; if(typeof command!=='string'||!command.startsWith('/')||/[\r\n\0]/.test(command))throw Error('Invalid local mail broker path'); return transport.local.sudo===false?{command,args:[]}:{command:'/usr/bin/sudo',args:['-n',command]}; } if(transport?.mode!=='ssh')throw Error('Choose local or ssh mail broker transport'); const ssh=transport.ssh; if(!ssh||typeof ssh.host!=='string'||!/^[a-z_][a-z0-9_-]*@[a-z0-9][a-z0-9.-]*$/i.test(ssh.host))throw Error('Invalid mail broker SSH host'); for(const key of ['identityFile','knownHostsFile'])if(typeof ssh[key]!=='string'||!ssh[key].startsWith('/')||/[\r\n\0]/.test(ssh[key]))throw Error('Invalid mail broker SSH path'); return {command:'/usr/bin/ssh',args:['-T','-i',ssh.identityFile,'-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','StrictHostKeyChecking=yes','-o','ConnectTimeout=7','-o','UserKnownHostsFile='+ssh.knownHostsFile,ssh.host]}; }