mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
[general] database = takes SQLite's file as before, or a server's address: postgres://... (pgx) or mysql://... / mysql:DSN (go-sql-driver). The store's SQL is written once; internal/store's dialect gives each engine its forms of what differs: ? placeholders ($1 for PostgreSQL), ids returned by RETURNING or LastInsertId, upserts (ON DUPLICATE KEY UPDATE), INSERT OR IGNORE, GREATEST, booleans, unique violations, column checks for migrations, indexes. The schema is one list with types per engine: names match in any case (NOCASE, an ICU collation made on PostgreSQL, utf8mb4_0900_as_ci), MySQL keeps indexed text in VARCHARs and connects with the SQL mode the store wants (ANSI quotes for "read" and "key", strict, rows matched not changed). Existing SQLite databases are kept as they are. bbs db copy DATABASE copies every table into a new database of any engine, keeping ids (PostgreSQL's sequences set after). Logs and bbs check never show a server's password. Under go test, BBS_TEST_DATABASE puts every test's store on a PostgreSQL or MySQL server (a schema or database each, dropped after): the whole suite passes on PostgreSQL 16 and MySQL 8.4 as on SQLite. The store's own tests check what the engines do differently, and copying from SQLite into each. Also: the FTP tests' readiness probe now waits to be counted out, so the per-address limit test no longer fails at random. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// SSHKey is a public key a user logs in with by SSH.
|
|
type SSHKey struct {
|
|
ID int64
|
|
UserID int64
|
|
User string // the user's name (read)
|
|
Type string // ssh-ed25519, ssh-rsa, ...
|
|
Fingerprint string // SHA256:...
|
|
Key string // the authorized_keys line: type, base64, comment
|
|
Comment string
|
|
Created time.Time
|
|
LastUsed time.Time
|
|
}
|
|
|
|
// ErrKeyTaken: the key is someone's already (a key logs in one user).
|
|
var ErrKeyTaken = errors.New("that key is already in use")
|
|
|
|
// AddSSHKey records a user's key.
|
|
func (s *Store) AddSSHKey(k *SSHKey) (int64, error) {
|
|
var n int
|
|
if err := s.db.QueryRow(`SELECT COUNT(*) FROM ssh_keys WHERE fingerprint = ?`, k.Fingerprint).Scan(&n); err != nil {
|
|
return 0, err
|
|
}
|
|
if n > 0 {
|
|
return 0, ErrKeyTaken
|
|
}
|
|
id, err := s.db.insertID(`INSERT INTO ssh_keys (user_id, type, fingerprint, "key", comment, created)
|
|
VALUES (?, ?, ?, ?, ?, ?)`, k.UserID, k.Type, k.Fingerprint, k.Key, k.Comment, now())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
const sshKeyCols = `k.id, k.user_id, u.name, k.type, k.fingerprint, k."key", k.comment, k.created, k.last_used`
|
|
|
|
func scanSSHKey(row interface{ Scan(...any) error }) (*SSHKey, error) {
|
|
var k SSHKey
|
|
var created, last int64
|
|
err := row.Scan(&k.ID, &k.UserID, &k.User, &k.Type, &k.Fingerprint, &k.Key, &k.Comment, &created, &last)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
k.Created, k.LastUsed = unix(created), unix(last)
|
|
return &k, err
|
|
}
|
|
|
|
// SSHKeyByFingerprint finds a key.
|
|
func (s *Store) SSHKeyByFingerprint(fp string) (*SSHKey, error) {
|
|
return scanSSHKey(s.db.QueryRow(`SELECT `+sshKeyCols+` FROM ssh_keys k JOIN users u ON u.id = k.user_id
|
|
WHERE k.fingerprint = ?`, fp))
|
|
}
|
|
|
|
// TouchSSHKey notes a key's use.
|
|
func (s *Store) TouchSSHKey(id int64) error {
|
|
_, err := s.db.Exec(`UPDATE ssh_keys SET last_used = ? WHERE id = ?`, now(), id)
|
|
return err
|
|
}
|
|
|
|
// SSHKeys lists the keys (of a user, or all if user is 0).
|
|
func (s *Store) SSHKeys(user int64) ([]*SSHKey, error) {
|
|
q := `SELECT ` + sshKeyCols + ` FROM ssh_keys k JOIN users u ON u.id = k.user_id`
|
|
args := []any{}
|
|
if user != 0 {
|
|
q += ` WHERE k.user_id = ?`
|
|
args = append(args, user)
|
|
}
|
|
rows, err := s.db.Query(q+` ORDER BY k.id`, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []*SSHKey
|
|
for rows.Next() {
|
|
k, err := scanSSHKey(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, k)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// RemoveSSHKey deletes a key (of a user, or anyone's if user is 0).
|
|
func (s *Store) RemoveSSHKey(id, user int64) error {
|
|
if user == 0 {
|
|
return s.exec1(`DELETE FROM ssh_keys WHERE id = ?`, id)
|
|
}
|
|
return s.exec1(`DELETE FROM ssh_keys WHERE id = ? AND user_id = ?`, id, user)
|
|
}
|