add cli command "mox admin imapserve $preauthaddress"

for admins to open an imap connection preauthenticated for an account (by address), also when
it is disabled for logins.

useful for migrations. the admin typically doesn't know the password of the
account, so couldn't get an imap session (for synchronizing) before.

tested with "mox localserve" and running:

	mutt -e 'set tunnel="MOXCONF=/home/mjl/.config/mox-localserve/mox.conf ./mox admin imapserve mox@localhost"'

may also work with interimap, but untested.

i initially assumed imap would be done fully on file descriptor 0, but mutt
expects imap output on fd 1. that's the default now. flag -fd0 is for others
that expect it on fd0.

for issue #175, suggested by DanielG
This commit is contained in:
Mechiel Lukkien
2025-01-25 22:18:26 +01:00
parent 2d3d726f05
commit 49e2eba52b
11 changed files with 141 additions and 14 deletions

54
main.go
View File

@ -171,6 +171,8 @@ var commands = []struct {
{"config ensureacmehostprivatekeys", cmdConfigEnsureACMEHostprivatekeys},
{"config example", cmdConfigExample},
{"admin imapserve", cmdIMAPServe},
{"checkupdate", cmdCheckupdate},
{"cid", cmdCid},
{"clientconfig", cmdClientConfig},
@ -3720,6 +3722,58 @@ func ctlcmdReassignthreads(ctl *ctl, account string) {
ctl.xstreamto(os.Stdout)
}
func cmdIMAPServe(c *cmd) {
c.params = "preauth-address"
c.help = `Initiate a preauthenticated IMAP connection on file descriptor 0.
For use with tools that can do IMAP over tunneled connections, e.g. with SSH
during migrations. TLS is not possible on the connection, and authentication
does not require TLS.
`
var fd0 bool
c.flag.BoolVar(&fd0, "fd0", false, "write IMAP to file descriptor 0 instead of stdout")
args := c.Parse()
if len(args) != 1 {
c.Usage()
}
address := args[0]
output := os.Stdout
if fd0 {
output = os.Stdout
}
ctlcmdIMAPServe(xctl(), address, os.Stdin, output)
}
func ctlcmdIMAPServe(ctl *ctl, address string, input io.ReadCloser, output io.WriteCloser) {
ctl.xwrite("imapserve")
ctl.xwrite(address)
ctl.xreadok()
done := make(chan struct{}, 1)
go func() {
defer func() {
done <- struct{}{}
}()
_, err := io.Copy(output, ctl.conn)
if err == nil {
err = io.EOF
}
log.Printf("reading from imap: %v", err)
}()
go func() {
defer func() {
done <- struct{}{}
}()
_, err := io.Copy(ctl.conn, input)
if err == nil {
err = io.EOF
}
log.Printf("writing to imap: %v", err)
}()
<-done
}
func cmdReadmessages(c *cmd) {
c.unlisted = true
c.params = "datadir account ..."