1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-16 14:27:01 +00:00

chasquid-util: Use server for aliases-resolve and domaininfo-remove

This patch makes chasquid-util's aliases-resolve and domaininfo-remove
commands talk to the chasquid server (via the new localrpc server).

For aliases-resolve, currently has fairly hacky logic which reimplements
a bunch of the servers', and is also incomplete because it does not
support hooks.

In this patch we fix that by having it talk to the server, where we get
authoritative responses and have no issues with aliases hooks. This
resolves https://github.com/albertito/chasquid/issues/18.

For domaininfo-remove, currently its implementation is also very hacky
since it manipulates files behind the servers' back and without even
using the internal library.

In this patch we fix that by doing the operation through the server,
avoiding the need for those hacks, and also remove the need to manually
reload the server afterwards.
This commit is contained in:
Alberto Bertogli
2023-07-29 22:49:50 +01:00
parent ddd1b6d96e
commit e6c6df457d
13 changed files with 147 additions and 72 deletions

View File

@@ -9,9 +9,9 @@ package main
import (
"bytes"
"fmt"
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"syscall"
@@ -19,6 +19,7 @@ import (
"blitiri.com.ar/go/chasquid/internal/aliases"
"blitiri.com.ar/go/chasquid/internal/config"
"blitiri.com.ar/go/chasquid/internal/envelope"
"blitiri.com.ar/go/chasquid/internal/localrpc"
"blitiri.com.ar/go/chasquid/internal/normalize"
"blitiri.com.ar/go/chasquid/internal/trace"
"blitiri.com.ar/go/chasquid/internal/userdb"
@@ -223,45 +224,26 @@ func aliasesResolve() {
if err != nil {
Fatalf("Error loading config: %v", err)
}
_ = os.Chdir(configDir)
r := aliases.NewResolver(allUsersExist)
r.SuffixSep = *conf.SuffixSeparators
r.DropChars = *conf.DropCharacters
domainDirs, err := os.ReadDir("domains/")
if err != nil {
Fatalf("Error reading domains/ directory: %v", err)
}
if len(domainDirs) == 0 {
Fatalf("No domains found in config")
}
for _, entry := range domainDirs {
name := entry.Name()
aliasfile := "domains/" + name + "/aliases"
r.AddDomain(name)
err := r.AddAliasesFile(name, aliasfile)
if err == nil {
fmt.Printf("%s: loaded %q\n", name, aliasfile)
} else if err != nil && os.IsNotExist(err) {
fmt.Printf("%s: no aliases file\n", name)
} else {
fmt.Printf("%s: error loading %q: %v\n", name, aliasfile, err)
}
}
tr := trace.New("chasquid-util", "aliasesResolve")
defer tr.Finish()
rcpts, err := r.Resolve(tr, args["$2"])
c := localrpc.NewClient(conf.DataDir + "/localrpc-v1")
vs, err := c.Call("AliasResolve", "Address", args["$2"])
if err != nil {
Fatalf("Error resolving: %v", err)
}
for _, rcpt := range rcpts {
fmt.Printf("%v %s\n", rcpt.Type, rcpt.Addr)
}
// Result is a map of type -> []addresses.
// Sort the types for deterministic output.
ts := []string{}
for t := range vs {
ts = append(ts, t)
}
sort.Strings(ts)
for _, t := range ts {
for _, a := range vs[t] {
fmt.Printf("%v %s\n", t, a)
}
}
}
// chasquid-util print-config
@@ -276,20 +258,15 @@ func printConfig() {
// chasquid-util domaininfo-remove <domain>
func domaininfoRemove() {
domain := args["$2"]
conf, err := config.Load(configDir+"/chasquid.conf", "")
if err != nil {
Fatalf("Error loading config: %v", err)
}
// File for the corresponding domain.
// Note this is making some assumptions about the data layout and
// protoio's storage structure, so it will need adjustment if they change.
file := conf.DataDir + "/domaininfo/s:" + url.QueryEscape(domain)
err = os.Remove(file)
c := localrpc.NewClient(conf.DataDir + "/localrpc-v1")
_, err = c.Call("DomaininfoClear", "Domain", args["$2"])
if err != nil {
Fatalf("Error removing file: %v", err)
Fatalf("Error removing domaininfo entry: %v", err)
}
}

View File

@@ -71,24 +71,6 @@ if r authenticate user@domain --password=passwd > /dev/null; then
exit 1
fi
touch '.data/domaininfo/s:dom%C3%A1in'
if ! r domaininfo-remove domáin; then
echo domaininfo-remove failed
exit 1
fi
if [ -f '.data/domaininfo/s:dom%C3%A1in' ]; then
echo domaininfo-remove did not remove file
exit 1
fi
echo "alias: user@somewhere" > .config/domains/domain/aliases
A=$(r aliases-resolve alias@domain | grep somewhere)
if [ "$A" != "(email) user@somewhere" ]; then
echo aliases-resolve failed
echo output: "$A"
exit 1
fi
C=$(r print-config | grep hostname)
if ! ( echo "$C" | grep -E -q "hostname:.*\"$HOSTNAME\"" ); then
echo print-config failed
@@ -120,4 +102,13 @@ if r aliases-add alias4@domain > /dev/null; then
exit 1
fi
# Run all the chamuyero tests.
for i in *.cmy; do
if ! chamuyero "$i" > "$i.log" 2>&1 ; then
echo "# Test $i failed, log follows"
cat "$i.log"
exit 1
fi
done
success

View File

@@ -0,0 +1,21 @@
# Test success.
server unix_listen .data/localrpc-v1
c = ./chasquid-util -C=.config aliases-resolve test@test.com
server <- AliasResolve Address=test%40test.com
server -> 200 %28email%29=r1%40r1.com&%28pipe%29=cmd%20args
c <- (email) r1@r1.com
c <- (pipe) cmd args
c wait 0
# Test error.
server unix_listen .data/localrpc-v1
c = ./chasquid-util -C=.config aliases-resolve test@test.com
server <- AliasResolve Address=test%40test.com
server -> 500 This is a test error
c <- Error resolving: This is a test error
c wait 1

View File

@@ -0,0 +1,19 @@
# Test success.
server unix_listen .data/localrpc-v1
c = ./chasquid-util -C=.config domaininfo-remove domain.com
server <- DomaininfoClear Domain=domain.com
server -> 200
c wait 0
# Test error.
server unix_listen .data/localrpc-v1
c = ./chasquid-util -C=.config domaininfo-remove domain.com
server <- DomaininfoClear Domain=domain.com
server -> 500 This is a test error
c <- Error removing domaininfo entry: This is a test error
c wait 1

View File

@@ -1,3 +0,0 @@
*.log
dovecot-auth-cli
dovecot-auth-cli.test