cleanups
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
@@ -11,6 +10,7 @@ import (
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"path"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -26,31 +26,26 @@ import (
|
||||
"nxcaldav/internal/config"
|
||||
)
|
||||
|
||||
// for ICS access
|
||||
type publicInfo struct {
|
||||
InternalPath string
|
||||
Mode string // e.g., "future-only"
|
||||
}
|
||||
|
||||
// DBBackend implements the caldav.Backend interface using a PostgreSQL database.
|
||||
// It manages users, calendars, and their events/tasks with built-in support for
|
||||
// access control, privacy redaction, and aggregate (virtual) calendars.
|
||||
// DBBackend = caldav.Backend interface (PostgreSQL)
|
||||
type DBBackend struct {
|
||||
pool *pgxpool.Pool // Connection pool to PostgreSQL
|
||||
prefix string // Public URL base path prefix
|
||||
publicURL string // Full public URL base (e.g. http://nxc.nx2.site/)
|
||||
publicURL string // Full public URL base (e.g. http://dav.example.com/)
|
||||
redactionText string // Text used to hide confidential event details (e.g. "[REDACED]")
|
||||
defaultClass string // Class assumed if non is set ("PUBLIC", "PRIVATE", "CONFIDENTIAL")
|
||||
emailDomain string // Domain for email addresses (e.g., "nx2.site")
|
||||
smtp config.SMTPConfig // SMTP server configuration
|
||||
aggregates map[string]*config.Aggregate // In-memory map of path -> virtual calendar definitions
|
||||
userAggs map[string][]string // In-memory map of user -> list of aggregate paths they can see
|
||||
publicAccess map[string]publicInfo // In-memory map of public path -> internal info
|
||||
emailDomain string // Domain for email addresses (e.g., "example.com" if email is tony@example.com)
|
||||
smtp config.SMTPConfig
|
||||
aggregates map[string]*config.Aggregate // In-memory maps...
|
||||
userAggs map[string][]string
|
||||
publicAccess map[string]publicInfo
|
||||
}
|
||||
|
||||
// NewDBBackend creates a new database-backed CalDAV provider.
|
||||
// It is called once during main.go startup.
|
||||
// It initializes the database connection, ensures the tables exist (Schema),
|
||||
// and synchronizes the config.yaml data into the database tables.
|
||||
func NewDBBackend(ctx context.Context, cfg *config.Config) (*DBBackend, error) {
|
||||
pool, err := pgxpool.New(ctx, cfg.Database.URL)
|
||||
if err != nil {
|
||||
@@ -84,8 +79,7 @@ func NewDBBackend(ctx context.Context, cfg *config.Config) (*DBBackend, error) {
|
||||
}
|
||||
|
||||
// initSchema runs the DDL queries to create the necessary tables if they don't exist.
|
||||
// We use ON DELETE CASCADE extensively so that deleting a user or calendar
|
||||
// automatically wipes all related events and access rules in the DB.
|
||||
// it uses ON DELETE CASCADE extensively
|
||||
func (b *DBBackend) initSchema(ctx context.Context) error {
|
||||
queries := []string{
|
||||
`CREATE TABLE IF NOT EXISTS users (
|
||||
@@ -108,35 +102,35 @@ func (b *DBBackend) initSchema(ctx context.Context) error {
|
||||
PRIMARY KEY (calendar_id, user_id)
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS calendar_objects (
|
||||
id SERIAL PRIMARY KEY,
|
||||
calendar_id INTEGER REFERENCES calendars(id) ON DELETE CASCADE,
|
||||
path TEXT NOT NULL,
|
||||
data TEXT NOT NULL,
|
||||
etag TEXT NOT NULL,
|
||||
UNIQUE (calendar_id, path)
|
||||
id SERIAL PRIMARY KEY,
|
||||
calendar_id INTEGER REFERENCES calendars(id) ON DELETE CASCADE,
|
||||
path TEXT NOT NULL,
|
||||
data TEXT NOT NULL,
|
||||
etag TEXT NOT NULL,
|
||||
UNIQUE (calendar_id, path)
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS addressbooks (
|
||||
id SERIAL PRIMARY KEY,
|
||||
owner_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
path TEXT UNIQUE NOT NULL,
|
||||
name TEXT,
|
||||
description TEXT
|
||||
id SERIAL PRIMARY KEY,
|
||||
owner_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
path TEXT UNIQUE NOT NULL,
|
||||
name TEXT,
|
||||
description TEXT
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS addressbook_access (
|
||||
addressbook_id INTEGER REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
mode TEXT NOT NULL,
|
||||
PRIMARY KEY (addressbook_id, user_id)
|
||||
addressbook_id INTEGER REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
mode TEXT NOT NULL,
|
||||
PRIMARY KEY (addressbook_id, user_id)
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS addressbook_objects (
|
||||
id SERIAL PRIMARY KEY,
|
||||
addressbook_id INTEGER REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
path TEXT NOT NULL,
|
||||
data TEXT NOT NULL,
|
||||
etag TEXT NOT NULL,
|
||||
UNIQUE (addressbook_id, path)
|
||||
id SERIAL PRIMARY KEY,
|
||||
addressbook_id INTEGER REFERENCES addressbooks(id) ON DELETE CASCADE,
|
||||
path TEXT NOT NULL,
|
||||
data TEXT NOT NULL,
|
||||
etag TEXT NOT NULL,
|
||||
UNIQUE (addressbook_id, path)
|
||||
)`,
|
||||
}
|
||||
}
|
||||
for _, q := range queries {
|
||||
if _, err := b.pool.Exec(ctx, q); err != nil {
|
||||
return fmt.Errorf("failed to execute schema query: %v", err)
|
||||
@@ -145,14 +139,10 @@ func (b *DBBackend) initSchema(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolvePassword prepares a password for storage.
|
||||
// It is called by syncConfig for every user.
|
||||
// 1. If PasswordCmd is set, it runs the bash command to get the password.
|
||||
// 2. If the password is not already a bcrypt hash, it hashes it.
|
||||
func (b *DBBackend) resolvePassword(u config.User) (string, error) {
|
||||
var raw string
|
||||
if u.PasswordCmd != "" {
|
||||
cmd := exec.Command("bash", "-c", u.PasswordCmd)
|
||||
cmd := exec.Command("sh", "-c", u.PasswordCmd)
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to run password command for %s: %v", u.Name, err)
|
||||
@@ -175,12 +165,6 @@ func (b *DBBackend) resolvePassword(u config.User) (string, error) {
|
||||
return string(hash), nil
|
||||
}
|
||||
|
||||
// syncConfig reconciles the YAML configuration with the PostgreSQL state.
|
||||
// It is called once at server startup.
|
||||
// 1. Inserts/Updates all users and their hashed passwords.
|
||||
// 2. Inserts/Updates all calendars and their access/group rules.
|
||||
// 3. Builds the in-memory Aggregate maps for fast lookup during requests.
|
||||
// 4. Performs an 'Orphan Check' to warn the user about DB entries not in config.
|
||||
func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
tx, err := b.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
@@ -199,6 +183,7 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
prefix := cfg.Server.BasePath()
|
||||
|
||||
// --- Phase 1: User Sync ---
|
||||
// Inserts/Updates all users and their hashed passwords.
|
||||
for _, u := range cfg.Users {
|
||||
configUserNames[u.Name] = true
|
||||
for _, g := range u.Groups {
|
||||
@@ -219,6 +204,7 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
}
|
||||
|
||||
// --- Phase 2: Calendar & Access Sync ---
|
||||
// Inserts/Updates all calendars and their access/group rules.
|
||||
for _, c := range cfg.Calendars {
|
||||
path := prefix + fmt.Sprintf("/%s/calendars/%s/", c.Owner, c.ID)
|
||||
configCalendarPaths[path] = true
|
||||
@@ -353,10 +339,10 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
if configCalendarPaths[p_] {
|
||||
return fmt.Errorf("aggregate %s collides with real calendar path", p_)
|
||||
}
|
||||
|
||||
|
||||
aggCopy := agg
|
||||
b.aggregates[p_] = &aggCopy
|
||||
|
||||
|
||||
aggAccess := make(map[string]bool)
|
||||
aggAccess[agg.Owner] = true
|
||||
for _, a := range agg.Access {
|
||||
@@ -409,7 +395,6 @@ func (b *DBBackend) syncConfig(ctx context.Context, cfg *config.Config) error {
|
||||
}
|
||||
calRows.Close()
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
@@ -448,9 +433,8 @@ func (b *DBBackend) getUsername(ctx context.Context) (string, error) {
|
||||
|
||||
// --- Internal Security & Privacy Helpers ---
|
||||
|
||||
// checkAccess verifies if the current user has the required permission for a real calendar.
|
||||
// It returns the internal database ID of the calendar and the granted mode if access is granted.
|
||||
// It is called before any read (PROPFIND, GET, REPORT) or write (PUT, DELETE) operation.
|
||||
// for a real calendars
|
||||
// returns internal database ID if granted
|
||||
func (b *DBBackend) checkAccess(ctx context.Context, calendarPath string, requiredMode string) (int, string, error) {
|
||||
username, err := b.getUsername(ctx)
|
||||
if err != nil {
|
||||
@@ -501,13 +485,7 @@ func (b *DBBackend) checkAccess(ctx context.Context, calendarPath string, requir
|
||||
return calID, mode, nil
|
||||
}
|
||||
|
||||
// filterCalendar is the privacy enforcement engine of the server.
|
||||
// It is called every time a calendar object is retrieved from the database
|
||||
// for a user who is NOT the owner (unless they have read-write access).
|
||||
// It applies the following rules based on the 'CLASS' property of an event/task:
|
||||
// 1. PRIVATE: Removes the object entirely.
|
||||
// 2. CONFIDENTIAL: Redacts the summary to the configured 'redactionText' while keeping time data.
|
||||
// 3. PUBLIC: Passes the object through unchanged.
|
||||
// Privacy engine
|
||||
func (b *DBBackend) filterCalendar(ctx context.Context, ownerName string, mode string, original *ical.Calendar) (*ical.Calendar, error) {
|
||||
username, err := b.getUsername(ctx)
|
||||
if err != nil {
|
||||
@@ -529,7 +507,7 @@ func (b *DBBackend) filterCalendar(ctx context.Context, ownerName string, mode s
|
||||
continue
|
||||
}
|
||||
|
||||
class := b.defaultClass;
|
||||
class := b.defaultClass
|
||||
if prop := child.Props.Get("CLASS"); prop != nil {
|
||||
class = strings.ToUpper(prop.Value)
|
||||
}
|
||||
@@ -793,10 +771,10 @@ func (b *DBBackend) listCalendarObjectsRaw(ctx context.Context, calID int, owner
|
||||
func (b *DBBackend) listAggregateObjects(ctx context.Context, aggPath string, agg *config.Aggregate) ([]caldav.CalendarObject, error) {
|
||||
var res []caldav.CalendarObject
|
||||
username, _ := b.getUsername(ctx)
|
||||
|
||||
|
||||
for _, sourceID := range agg.Sources {
|
||||
sourcePath := b.prefix + fmt.Sprintf("/%s/calendars/%s/", agg.Owner, sourceID)
|
||||
|
||||
|
||||
var calID int
|
||||
var ownerName string
|
||||
err := b.pool.QueryRow(ctx, `
|
||||
@@ -825,7 +803,7 @@ func (b *DBBackend) listAggregateObjects(ctx context.Context, aggPath string, ag
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
for _, obj := range objs {
|
||||
// Prepend source name so Diane knows this is a "[calendar]" event
|
||||
for _, child := range obj.Data.Children {
|
||||
@@ -872,7 +850,7 @@ func (b *DBBackend) GetCalendarObject(ctx context.Context, p string, req *caldav
|
||||
}
|
||||
sourceID := parts[0]
|
||||
realFileName := parts[1]
|
||||
|
||||
|
||||
sourcePath := b.prefix + fmt.Sprintf("/%s/calendars/%s/", agg.Owner, sourceID)
|
||||
var calID int
|
||||
var ownerName string
|
||||
@@ -973,7 +951,7 @@ func (b *DBBackend) GetCalendarObject(ctx context.Context, p string, req *caldav
|
||||
// Called during PUT requests when a user saves a change in their calendar app.
|
||||
func (b *DBBackend) PutCalendarObject(ctx context.Context, p string, calendar *ical.Calendar, opts *caldav.PutCalendarObjectOptions) (*caldav.CalendarObject, error) {
|
||||
dirPath := path.Dir(p) + "/"
|
||||
|
||||
|
||||
// Aggregates are read-only
|
||||
if _, ok := b.aggregates[dirPath]; ok {
|
||||
return nil, webdav.NewHTTPError(http.StatusForbidden, errors.New("aggregates are read-only"))
|
||||
@@ -1012,11 +990,11 @@ func (b *DBBackend) PutCalendarObject(ctx context.Context, p string, calendar *i
|
||||
}
|
||||
|
||||
// --- Case A: User is the ATTENDEE updating their status ---
|
||||
// If the user is NOT the organizer, but is an attendee, find the organizer's
|
||||
// If the user is NOT the organizer, but is an attendee, find the organizer's
|
||||
// original event and update the status there.
|
||||
if orgEmail != "" && orgEmail != userEmail {
|
||||
log.Printf("[scheduling] Attendee %s updated event. Propagating to organizer %s", userEmail, orgEmail)
|
||||
|
||||
|
||||
// Find the attendee's status in this version
|
||||
myStatus := "NEEDS-ACTION"
|
||||
for _, att := range event.Props["ATTENDEE"] {
|
||||
@@ -1120,7 +1098,7 @@ func (b *DBBackend) propagateStatusToOrganizer(orgEmail, attendeeEmail, uid, sta
|
||||
|
||||
// 1. Find the organizer's user ID
|
||||
var orgUserID int
|
||||
err := b.pool.QueryRow(ctx, "SELECT id FROM users WHERE name = $1 OR name = $2",
|
||||
err := b.pool.QueryRow(ctx, "SELECT id FROM users WHERE name = $1 OR name = $2",
|
||||
strings.Split(orgEmail, "@")[0], orgEmail).Scan(&orgUserID)
|
||||
if err != nil {
|
||||
log.Printf("[scheduling] Could not find organizer user %s: %v", orgEmail, err)
|
||||
@@ -1132,7 +1110,7 @@ func (b *DBBackend) propagateStatusToOrganizer(orgEmail, attendeeEmail, uid, sta
|
||||
SELECT co.path, co.data, co.calendar_id
|
||||
FROM calendar_objects co
|
||||
JOIN calendars c ON co.calendar_id = c.id
|
||||
WHERE c.owner_id = $1 AND co.data LIKE '%' || $2 || '%'`,
|
||||
WHERE c.owner_id = $1 AND co.data LIKE '%' || $2 || '%'`,
|
||||
orgUserID, uid)
|
||||
if err != nil {
|
||||
log.Printf("[scheduling] Error searching for organizer's copy: %v", err)
|
||||
@@ -1143,11 +1121,15 @@ func (b *DBBackend) propagateStatusToOrganizer(orgEmail, attendeeEmail, uid, sta
|
||||
for rows.Next() {
|
||||
var p, dataStr string
|
||||
var calID int
|
||||
if err := rows.Scan(&p, &dataStr, &calID); err != nil { continue }
|
||||
if err := rows.Scan(&p, &dataStr, &calID); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Verify UID (LIKE is just a hint)
|
||||
calendar, err := ical.NewDecoder(strings.NewReader(dataStr)).Decode()
|
||||
if err != nil { continue }
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, event := range calendar.Events() {
|
||||
@@ -1264,87 +1246,87 @@ func (b *DBBackend) GetColor(ctx context.Context, p string) string {
|
||||
return color
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// --- CardDAV Backend Implementation ---
|
||||
// --- CardDAV ---
|
||||
|
||||
func (b *DBBackend) AddressBookHomeSetPath(ctx context.Context) (string, error) {
|
||||
func (b *DBBackend) AddressBookHomeSetPath(ctx context.Context) (string, error) {
|
||||
username, err := b.getUsername(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", err
|
||||
}
|
||||
return b.prefix + fmt.Sprintf("/%s/addressbooks/", username), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DBBackend) ListAddressBooks(ctx context.Context) ([]carddav.AddressBook, error) {
|
||||
func (b *DBBackend) ListAddressBooks(ctx context.Context) ([]carddav.AddressBook, error) {
|
||||
username, err := b.getUsername(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := b.pool.Query(ctx, `
|
||||
SELECT path, name, COALESCE(description, '') FROM addressbooks
|
||||
WHERE owner_id = (SELECT id FROM users WHERE name = $1)
|
||||
OR id IN (SELECT addressbook_id FROM addressbook_access WHERE user_id = (SELECT id FROM users WHERE name = $1))`,
|
||||
username)
|
||||
username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var res []carddav.AddressBook
|
||||
for rows.Next() {
|
||||
var ab carddav.AddressBook
|
||||
if err := rows.Scan(&ab.Path, &ab.Name, &ab.Description); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ab.MaxResourceSize = 1000000
|
||||
ab.SupportedAddressData = []carddav.AddressDataType{
|
||||
{ContentType: "text/vcard", Version: "3.0"},
|
||||
{ContentType: "text/vcard", Version: "4.0"},
|
||||
}
|
||||
res = append(res, ab)
|
||||
var ab carddav.AddressBook
|
||||
if err := rows.Scan(&ab.Path, &ab.Name, &ab.Description); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ab.MaxResourceSize = 1000000
|
||||
ab.SupportedAddressData = []carddav.AddressDataType{
|
||||
{ContentType: "text/vcard", Version: "3.0"},
|
||||
{ContentType: "text/vcard", Version: "4.0"},
|
||||
}
|
||||
res = append(res, ab)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DBBackend) GetAddressBook(ctx context.Context, p string) (*carddav.AddressBook, error) {
|
||||
func (b *DBBackend) GetAddressBook(ctx context.Context, p string) (*carddav.AddressBook, error) {
|
||||
if !strings.HasSuffix(p, "/") {
|
||||
p += "/"
|
||||
p += "/"
|
||||
}
|
||||
|
||||
var ab carddav.AddressBook
|
||||
err := b.pool.QueryRow(ctx, "SELECT path, name, COALESCE(description, '') FROM addressbooks WHERE path = $1", p).Scan(&ab.Path, &ab.Name, &ab.Description)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, webdav.NewHTTPError(http.StatusNotFound, errors.New("address book not found"))
|
||||
}
|
||||
return nil, err
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, webdav.NewHTTPError(http.StatusNotFound, errors.New("address book not found"))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
ab.MaxResourceSize = 1000000
|
||||
ab.SupportedAddressData = []carddav.AddressDataType{
|
||||
{ContentType: "text/vcard", Version: "3.0"},
|
||||
{ContentType: "text/vcard", Version: "4.0"},
|
||||
{ContentType: "text/vcard", Version: "3.0"},
|
||||
{ContentType: "text/vcard", Version: "4.0"},
|
||||
}
|
||||
return &ab, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DBBackend) CreateAddressBook(ctx context.Context, addressBook *carddav.AddressBook) error {
|
||||
func (b *DBBackend) CreateAddressBook(ctx context.Context, addressBook *carddav.AddressBook) error {
|
||||
return webdav.NewHTTPError(http.StatusForbidden, errors.New("address book creation only via config"))
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DBBackend) DeleteAddressBook(ctx context.Context, p string) error {
|
||||
func (b *DBBackend) DeleteAddressBook(ctx context.Context, p string) error {
|
||||
return webdav.NewHTTPError(http.StatusForbidden, errors.New("address book deletion only via config"))
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DBBackend) checkAddressBookAccess(ctx context.Context, abPath string, requiredMode string) (int, string, error) {
|
||||
func (b *DBBackend) checkAddressBookAccess(ctx context.Context, abPath string, requiredMode string) (int, string, error) {
|
||||
username, err := b.getUsername(ctx)
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(abPath, "/") {
|
||||
abPath += "/"
|
||||
abPath += "/"
|
||||
}
|
||||
|
||||
var abID int
|
||||
@@ -1355,140 +1337,139 @@ func (b *DBBackend) GetColor(ctx context.Context, p string) string {
|
||||
JOIN users u ON a.owner_id = u.id
|
||||
WHERE a.path = $1`, abPath).Scan(&abID, &ownerName)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, "", webdav.NewHTTPError(http.StatusNotFound, errors.New("address book not found"))
|
||||
}
|
||||
return 0, "", err
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, "", webdav.NewHTTPError(http.StatusNotFound, errors.New("address book not found"))
|
||||
}
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
if ownerName == username {
|
||||
return abID, "owner", nil
|
||||
return abID, "owner", nil
|
||||
}
|
||||
|
||||
var mode string
|
||||
err = b.pool.QueryRow(ctx, `
|
||||
SELECT mode FROM addressbook_access
|
||||
WHERE addressbook_id = $1 AND user_id = (SELECT id FROM users WHERE name = $2)`,
|
||||
abID, username).Scan(&mode)
|
||||
abID, username).Scan(&mode)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, "", webdav.NewHTTPError(http.StatusForbidden, errors.New("access denied"))
|
||||
}
|
||||
return 0, "", err
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, "", webdav.NewHTTPError(http.StatusForbidden, errors.New("access denied"))
|
||||
}
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
if requiredMode == "write" && mode != "read-write" {
|
||||
return 0, "", webdav.NewHTTPError(http.StatusForbidden, errors.New("read-only access"))
|
||||
return 0, "", webdav.NewHTTPError(http.StatusForbidden, errors.New("read-only access"))
|
||||
}
|
||||
return abID, mode, nil
|
||||
}
|
||||
|
||||
func (b *DBBackend) ListAddressObjects(ctx context.Context, p string, req *carddav.AddressDataRequest) ([]carddav.AddressObject, error) {
|
||||
if !strings.HasSuffix(p, "/") {
|
||||
p += "/"
|
||||
}
|
||||
|
||||
func (b *DBBackend) ListAddressObjects(ctx context.Context, p string, req *carddav.AddressDataRequest) ([]carddav.AddressObject, error) {
|
||||
if !strings.HasSuffix(p, "/") {
|
||||
p += "/"
|
||||
}
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, p, "read")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, p, "read")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := b.pool.Query(ctx, "SELECT path, data, etag FROM addressbook_objects WHERE addressbook_id = $1", abID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
rows, err := b.pool.Query(ctx, "SELECT path, data, etag FROM addressbook_objects WHERE addressbook_id = $1", abID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var res []carddav.AddressObject
|
||||
for rows.Next() {
|
||||
var obj carddav.AddressObject
|
||||
var dataStr string
|
||||
if err := rows.Scan(&obj.Path, &dataStr, &obj.ETag); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var res []carddav.AddressObject
|
||||
for rows.Next() {
|
||||
var obj carddav.AddressObject
|
||||
var dataStr string
|
||||
if err := rows.Scan(&obj.Path, &dataStr, &obj.ETag); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
card, err := vcard.NewDecoder(strings.NewReader(dataStr)).Decode()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
obj.Card = card
|
||||
res = append(res, obj)
|
||||
}
|
||||
return res, nil
|
||||
card, err := vcard.NewDecoder(strings.NewReader(dataStr)).Decode()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
obj.Card = card
|
||||
res = append(res, obj)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (b *DBBackend) GetAddressObject(ctx context.Context, p string, req *carddav.AddressDataRequest) (*carddav.AddressObject, error) {
|
||||
dirPath := path.Dir(p) + "/"
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, dirPath, "read")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dirPath := path.Dir(p) + "/"
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, dirPath, "read")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var obj carddav.AddressObject
|
||||
var dataStr string
|
||||
err = b.pool.QueryRow(ctx, "SELECT path, data, etag FROM addressbook_objects WHERE addressbook_id = $1 AND path = $2", abID, p).Scan(&obj.Path, &dataStr, &obj.ETag)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, webdav.NewHTTPError(http.StatusNotFound, errors.New("address book object not found"))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
var obj carddav.AddressObject
|
||||
var dataStr string
|
||||
err = b.pool.QueryRow(ctx, "SELECT path, data, etag FROM addressbook_objects WHERE addressbook_id = $1 AND path = $2", abID, p).Scan(&obj.Path, &dataStr, &obj.ETag)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, webdav.NewHTTPError(http.StatusNotFound, errors.New("address book object not found"))
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
card, err := vcard.NewDecoder(strings.NewReader(dataStr)).Decode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
obj.Card = card
|
||||
return &obj, nil
|
||||
card, err := vcard.NewDecoder(strings.NewReader(dataStr)).Decode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
obj.Card = card
|
||||
return &obj, nil
|
||||
}
|
||||
|
||||
func (b *DBBackend) PutAddressObject(ctx context.Context, p string, card vcard.Card, opts *carddav.PutAddressObjectOptions) (*carddav.AddressObject, error) {
|
||||
dirPath := path.Dir(p) + "/"
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, dirPath, "write")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dirPath := path.Dir(p) + "/"
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, dirPath, "write")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := vcard.NewEncoder(&buf).Encode(card); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataStr := buf.String()
|
||||
etag := fmt.Sprintf(`"%d"`, len(dataStr))
|
||||
var buf bytes.Buffer
|
||||
if err := vcard.NewEncoder(&buf).Encode(card); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dataStr := buf.String()
|
||||
etag := fmt.Sprintf(`"%d"`, len(dataStr))
|
||||
|
||||
_, err = b.pool.Exec(ctx, `
|
||||
_, err = b.pool.Exec(ctx, `
|
||||
INSERT INTO addressbook_objects (addressbook_id, path, data, etag) VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (addressbook_id, path) DO UPDATE SET data = EXCLUDED.data, etag = EXCLUDED.etag`,
|
||||
abID, p, dataStr, etag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
abID, p, dataStr, etag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &carddav.AddressObject{
|
||||
Path: p,
|
||||
Card: card,
|
||||
ETag: etag,
|
||||
}, nil
|
||||
return &carddav.AddressObject{
|
||||
Path: p,
|
||||
Card: card,
|
||||
ETag: etag,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (b *DBBackend) DeleteAddressObject(ctx context.Context, p string) error {
|
||||
dirPath := path.Dir(p) + "/"
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, dirPath, "write")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dirPath := path.Dir(p) + "/"
|
||||
abID, _, err := b.checkAddressBookAccess(ctx, dirPath, "write")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
commandTag, err := b.pool.Exec(ctx, "DELETE FROM addressbook_objects WHERE addressbook_id = $1 AND path = $2", abID, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if commandTag.RowsAffected() == 0 {
|
||||
return webdav.NewHTTPError(http.StatusNotFound, errors.New("address book object not found"))
|
||||
}
|
||||
return nil
|
||||
commandTag, err := b.pool.Exec(ctx, "DELETE FROM addressbook_objects WHERE addressbook_id = $1 AND path = $2", abID, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if commandTag.RowsAffected() == 0 {
|
||||
return webdav.NewHTTPError(http.StatusNotFound, errors.New("address book object not found"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *DBBackend) QueryAddressObjects(ctx context.Context, p string, query *carddav.AddressBookQuery) ([]carddav.AddressObject, error) {
|
||||
return b.ListAddressObjects(ctx, p, nil)
|
||||
return b.ListAddressObjects(ctx, p, nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -18,20 +18,19 @@ type Access struct {
|
||||
}
|
||||
|
||||
type Calendar struct {
|
||||
ID string `yaml:"id"`
|
||||
Owner string `yaml:"owner"`
|
||||
Color string `yaml:"color,omitempty"`
|
||||
Access []Access `yaml:"access,omitempty"`
|
||||
ID string `yaml:"id"`
|
||||
Owner string `yaml:"owner"`
|
||||
Color string `yaml:"color,omitempty"`
|
||||
Access []Access `yaml:"access,omitempty"`
|
||||
}
|
||||
|
||||
type AddressBook struct {
|
||||
ID string `yaml:"id"`
|
||||
Owner string `yaml:"owner"`
|
||||
Access []Access `yaml:"access,omitempty"`
|
||||
ID string `yaml:"id"`
|
||||
Owner string `yaml:"owner"`
|
||||
Access []Access `yaml:"access,omitempty"`
|
||||
}
|
||||
|
||||
type Aggregate struct {
|
||||
|
||||
ID string `yaml:"id"`
|
||||
Owner string `yaml:"owner"`
|
||||
Color string `yaml:"color,omitempty"`
|
||||
@@ -54,18 +53,16 @@ type ServerConfig struct {
|
||||
BindAddress string `yaml:"bind_address"`
|
||||
PublicURL string `yaml:"public_url"`
|
||||
EmailDomain string `yaml:"email_domain"`
|
||||
Redaction string `yaml:"redaction_text"`
|
||||
DefaultClass string `yaml:"default_class"`
|
||||
Redaction string `yaml:"redaction_text"` // "[-]"
|
||||
DefaultClass string `yaml:"default_class"` // CONFIDENTIAL/PRIVATE/PUBLIC
|
||||
}
|
||||
|
||||
func (s ServerConfig) BasePath() string {
|
||||
u, err := url.Parse(s.PublicURL)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if err != nil { return "" }
|
||||
return strings.TrimSuffix(u.Path, "/")
|
||||
}
|
||||
|
||||
|
||||
type SMTPConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
@@ -74,14 +71,29 @@ type SMTPConfig struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig `yaml:"server"`
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
SMTP SMTPConfig `yaml:"smtp"`
|
||||
Users []User `yaml:"users"`
|
||||
Calendars []Calendar `yaml:"calendars"`
|
||||
AddressBooks []AddressBook `yaml:"address_books"`
|
||||
Aggregates []Aggregate `yaml:"aggregates"`
|
||||
Server ServerConfig `yaml:"server"`
|
||||
Database DatabaseConfig `yaml:"database"`
|
||||
SMTP SMTPConfig `yaml:"smtp"`
|
||||
Users []User `yaml:"users"`
|
||||
Calendars []Calendar `yaml:"calendars"`
|
||||
AddressBooks []AddressBook `yaml:"address_books"`
|
||||
Aggregates []Aggregate `yaml:"aggregates"`
|
||||
}
|
||||
func (c *Config) setDefaults() {
|
||||
if c.Server.BindAddress == "" { c.Server.BindAddress = ":8080" }
|
||||
if c.Server.Redaction == "" { c.Server.Redaction = "Busy" }
|
||||
if c.Server.DefaultClass == "" { c.Server.DefaultClass = "CONFIDENTIAL" }
|
||||
if c.Server.EmailDomain == "" { c.Server.EmailDomain = "nx2.site" }
|
||||
if c.Database.URL == "" { c.Database.URL = "postgres://nxcaldav@localhost:5432/nxcaldav?sslmode=disable" }
|
||||
if c.SMTP.Host == "" { c.SMTP.Host = "localhost" }
|
||||
if c.SMTP.Port == 0 { c.SMTP.Port = 25 }
|
||||
}
|
||||
func (c *Config) checkConfig() {
|
||||
if !(slices.Contains([]string{"PUBLIC", "PRIVATE", "CONFIDENTIAL"}, c.Server.DefaultClass)) {
|
||||
panic("Invaldi Config, default_class")
|
||||
}
|
||||
}
|
||||
|
||||
func Load(path string) (*Config, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -100,32 +112,3 @@ func Load(path string) (*Config, error) {
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) checkConfig() {
|
||||
if !(slices.Contains([]string{"PUBLIC", "PRIVATE", "CONFIDENTIAL"}, c.Server.DefaultClass)) {
|
||||
panic("Invaldi Config, default_class")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) setDefaults() {
|
||||
if c.Server.BindAddress == "" {
|
||||
c.Server.BindAddress = ":8080"
|
||||
}
|
||||
if c.Server.Redaction == "" {
|
||||
c.Server.Redaction = "Busy"
|
||||
}
|
||||
if c.Server.DefaultClass == "" {
|
||||
c.Server.DefaultClass = "CONFIDENTIAL"
|
||||
}
|
||||
if c.Server.EmailDomain == "" {
|
||||
c.Server.EmailDomain = "nx2.site"
|
||||
}
|
||||
if c.Database.URL == "" {
|
||||
c.Database.URL = "postgres://nxcaldav@localhost:5432/nxcaldav?sslmode=disable"
|
||||
}
|
||||
if c.SMTP.Host == "" {
|
||||
c.SMTP.Host = "localhost"
|
||||
}
|
||||
if c.SMTP.Port == 0 {
|
||||
c.SMTP.Port = 25
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,8 @@ func (rw *responseWriter) WriteHeader(status int) {
|
||||
rw.status = status
|
||||
}
|
||||
|
||||
func AddColorToCalendarPropfind(r *http.Request, ctx context.Context, handler http.Handler, w http.ResponseWriter, be *backend.DBBackend) {
|
||||
// Add Color To Calendar Propfind
|
||||
func InjectColor(r *http.Request, ctx context.Context, handler http.Handler, w http.ResponseWriter, be *backend.DBBackend) {
|
||||
reqBody, _ := io.ReadAll(r.Body)
|
||||
r.Body = io.NopCloser(bytes.NewBuffer(reqBody))
|
||||
|
||||
@@ -38,16 +39,17 @@ func AddColorToCalendarPropfind(r *http.Request, ctx context.Context, handler ht
|
||||
|
||||
body := buf.Bytes()
|
||||
|
||||
// this models after the Radicale Response, largely AI code
|
||||
// 1. Add namespaces to the root multistatus tag
|
||||
reMultistatus := regexp.MustCompile(`(<[a-zA-Z0-9]*:?multistatus)`)
|
||||
body = reMultistatus.ReplaceAll(body, []byte(`$1 xmlns:ICAL="http://apple.com/ns/ical/" xmlns:C="urn:ietf:params:xml:ns:caldav" xmlns:CS="http://calendarserver.org/ns/"`))
|
||||
|
||||
// 2. Response processing
|
||||
reResponse := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?response.*?>.*?</[a-zA-Z0-9]*:?response>`)
|
||||
reHref := regexp.MustCompile(`<[a-zA-Z0-9]*:?href.*?>(.*?)</[a-zA-Z0-9]*:?href>`)
|
||||
rePropstat := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?propstat.*?>.*?</[a-zA-Z0-9]*:?propstat>`)
|
||||
reStatusOk := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?status.*?>HTTP/1.1 200 OK</[a-zA-Z0-9]*:?status>`)
|
||||
reProp := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?prop.*?>.*?</[a-zA-Z0-9]*:?prop>`)
|
||||
reResponse := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?response.*?>.*?</[a-zA-Z0-9]*:?response>`)
|
||||
reHref := regexp.MustCompile(`<[a-zA-Z0-9]*:?href.*?>(.*?)</[a-zA-Z0-9]*:?href>`)
|
||||
rePropstat := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?propstat.*?>.*?</[a-zA-Z0-9]*:?propstat>`)
|
||||
reStatusOk := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?status.*?>HTTP/1.1 200 OK</[a-zA-Z0-9]*:?status>`)
|
||||
reProp := regexp.MustCompile(`(?s)<[a-zA-Z0-9]*:?prop.*?>.*?</[a-zA-Z0-9]*:?prop>`)
|
||||
rePropClose := regexp.MustCompile(`</[a-zA-Z0-9]*:?prop>`)
|
||||
|
||||
body = reResponse.ReplaceAllFunc(body, func(resp []byte) []byte {
|
||||
@@ -89,7 +91,7 @@ func AddColorToCalendarPropfind(r *http.Request, ctx context.Context, handler ht
|
||||
return ps
|
||||
})
|
||||
|
||||
// 3. If no 200 OK propstat was found, create one!
|
||||
// 3. If no 200 OK propstat was found, create one
|
||||
if !has200 {
|
||||
newPropstat := fmt.Sprintf("<propstat xmlns=\"DAV:\"><prop>%s</prop><status>HTTP/1.1 200 OK</status></propstat>", props)
|
||||
reResponseClose := regexp.MustCompile(`</[a-zA-Z0-9]*:?response>`)
|
||||
@@ -106,6 +108,10 @@ func AddColorToCalendarPropfind(r *http.Request, ctx context.Context, handler ht
|
||||
w.Write(body)
|
||||
}
|
||||
|
||||
// modify caledar probfind
|
||||
//
|
||||
// this again modeled after the Radicale response
|
||||
// Largly AI generated agian
|
||||
func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.Handler, w http.ResponseWriter, be *backend.DBBackend) {
|
||||
reqBody, _ := io.ReadAll(r.Body)
|
||||
r.Body = io.NopCloser(bytes.NewBuffer(reqBody))
|
||||
@@ -158,6 +164,7 @@ func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.
|
||||
return resp
|
||||
}
|
||||
|
||||
// 2. Try to inject into an existing 200 OK propstat
|
||||
has200 := false
|
||||
resp = rePropstat.ReplaceAllFunc(resp, func(ps []byte) []byte {
|
||||
if reStatusOk.Match(ps) {
|
||||
@@ -171,6 +178,7 @@ func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.
|
||||
return ps
|
||||
})
|
||||
|
||||
// 3. If no 200 OK propstat was found, create one
|
||||
if !has200 {
|
||||
newPropstat := fmt.Sprintf("<propstat xmlns=\"DAV:\"><prop>%s</prop><status>HTTP/1.1 200 OK</status></propstat>", props)
|
||||
reResponseClose := regexp.MustCompile(`</[a-zA-Z0-9]*:?response>`)
|
||||
@@ -178,7 +186,6 @@ func HandleDiscoveryPropfind(r *http.Request, ctx context.Context, handler http.
|
||||
return append([]byte(newPropstat), closeTag...)
|
||||
})
|
||||
}
|
||||
|
||||
return resp
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user