Skip to content

Commit

Permalink
chasquid-util: Fix creating the directory on user-add
Browse files Browse the repository at this point in the history
`chasquid-util user-add` is meant to create the domain directory if it
doesn't exist; however there's a bug that makes this not happen, and
instead the command fails with:

  Error writing database: open <path>: no such file or directory

This patch fixes the issue and adds a test to ensure we don't have any
regressions on this behaviour.

Thanks to raspbeguy (https://github.com/raspbeguy) for reporting this
issue (on IRC).
  • Loading branch information
albertito committed Jan 20, 2025
1 parent 2f17f57 commit ad03885
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 11 additions & 6 deletions cmd/chasquid-util/chasquid-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package main

import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -126,19 +128,22 @@ func userDBFromArgs(create bool) (string, string, *userdb.DB) {
Fatalf("Domain missing, username should be of the form 'user@domain'")
}

db, err := userdb.Load(userDBForDomain(domain))
if err != nil {
if create && os.IsNotExist(err) {
if create {
dbDir := filepath.Dir(userDBForDomain(domain))
if _, err := os.Stat(dbDir); errors.Is(err, fs.ErrNotExist) {
fmt.Println("Creating database")
err = os.MkdirAll(filepath.Dir(userDBForDomain(domain)), 0755)
err = os.MkdirAll(dbDir, 0755)
if err != nil {
Fatalf("Error creating database dir: %v", err)
}
} else {
Fatalf("Error loading database: %v", err)
}
}

db, err := userdb.Load(userDBForDomain(domain))
if err != nil {
Fatalf("Error loading database: %v", err)
}

user, err = normalize.User(user)
if err != nil {
Fatalf("Error normalizing user: %v", err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/chasquid-util/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ function check_userdb() {


rm -rf .config/
mkdir -p .config/domains/domain/ .data/domaininfo
mkdir -p .config/ .data/domaininfo
echo 'data_dir: ".data"' >> .config/chasquid.conf

if ! r print-config > /dev/null; then
fail print-config
fi

# We intentionally run this when the domain directory doesn't exist, as we
# want to confirm it creates it.
if ! r user-add interactive@domain --password=passwd > /dev/null; then
fail user-add
fi
Expand Down

0 comments on commit ad03885

Please sign in to comment.