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

19
ctl.go
View File

@ -25,6 +25,7 @@ import (
"github.com/mjl-/mox/admin"
"github.com/mjl-/mox/config"
"github.com/mjl-/mox/dns"
"github.com/mjl-/mox/imapserver"
"github.com/mjl-/mox/message"
"github.com/mjl-/mox/metrics"
"github.com/mjl-/mox/mlog"
@ -277,7 +278,7 @@ func (s *ctlreader) xcheck(err error, msg string) {
}
// servectl handles requests on the unix domain socket "ctl", e.g. for graceful shutdown, local mail delivery.
func servectl(ctx context.Context, log mlog.Log, conn net.Conn, shutdown func()) {
func servectl(ctx context.Context, cid int64, log mlog.Log, conn net.Conn, shutdown func()) {
log.Debug("ctl connection")
var stop = struct{}{} // Sentinel value for panic and recover.
@ -296,7 +297,7 @@ func servectl(ctx context.Context, log mlog.Log, conn net.Conn, shutdown func())
ctl.xwrite("ctlv0")
for {
servectlcmd(ctx, ctl, shutdown)
servectlcmd(ctx, ctl, cid, shutdown)
}
}
@ -307,7 +308,7 @@ func xparseJSON(ctl *ctl, s string, v any) {
ctl.xcheck(err, "parsing from ctl as json")
}
func servectlcmd(ctx context.Context, ctl *ctl, shutdown func()) {
func servectlcmd(ctx context.Context, ctl *ctl, cid int64, shutdown func()) {
log := ctl.log
cmd := ctl.xread()
ctl.cmd = cmd
@ -1824,6 +1825,18 @@ func servectlcmd(ctx context.Context, ctl *ctl, shutdown func()) {
case "backup":
backupctl(ctx, ctl)
case "imapserve":
/* protocol:
> "imapserve"
> address
< "ok or error"
imap protocol
*/
address := ctl.xread()
ctl.xwriteok()
imapserver.ServeConnPreauth("(imapserve)", cid, ctl.conn, address)
ctl.log.Debug("imap connection finished")
default:
log.Info("unrecognized command", slog.String("cmd", cmd))
ctl.xwrite("unrecognized command")