also looking at this
feat: mailbox recovery addresses and password reset by email
#13
21 files
+1503
-10
| @@ -352,6 +352,37 @@ pub const Database = struct { | ||
| 352 | 352 | // not be bundled with the quota migration above). |
| 353 | 353 | self.exec("ALTER TABLE users ADD COLUMN digest_ha1 TEXT;") catch {}; |
| 354 | 354 | |
| 355 | // Migration: a mailbox's recovery address, where a password reset link | |
| 356 | // is sent. `recovery_source` records who set it ('operator' for the CLI | |
| 357 | // and deploy provisioning, 'user' for the mailbox owner in webmail), so | |
| 358 | // provisioning can leave an owner's own choice alone. One statement | |
| 359 | // each: sqlite3_exec stops at the first error, and an existing column | |
| 360 | // is that error. | |
| 361 | self.exec("ALTER TABLE users ADD COLUMN recovery_email TEXT;") catch {}; | |
| 362 | self.exec("ALTER TABLE users ADD COLUMN recovery_source TEXT;") catch {}; | |
| 363 | self.exec("ALTER TABLE users ADD COLUMN recovery_updated_at INTEGER;") catch {}; | |
| 364 | ||
| 365 | // Single-use password reset tokens (see src/api/webmail_recovery.zig). | |
| 366 | // Only the SHA-256 of a token is stored. Rows outlive their use for an | |
| 367 | // hour so the per-mailbox send limit can count them. | |
| 368 | // | |
| 369 | // No foreign key to users(username), deliberately: a username is | |
| 370 | // rewritten by `domain migrate` and by deploy-time username migrations, | |
| 371 | // and a referencing row would make that UPDATE fail. deleteUser and | |
| 372 | // renameDomain remove a mailbox's tokens explicitly instead. | |
| 373 | try self.exec( | |
| 374 | \\CREATE TABLE IF NOT EXISTS mailbox_reset_tokens ( | |
| 375 | \\ token_hash TEXT PRIMARY KEY, | |
| 376 | \\ username TEXT NOT NULL, | |
| 377 | \\ recovery_email TEXT NOT NULL, | |
| 378 | \\ created_at INTEGER NOT NULL, | |
| 379 | \\ expires_at INTEGER NOT NULL, | |
| 380 | \\ used_at INTEGER, | |
| 381 | \\ ip_address TEXT | |
| 382 | \\); | |
| 383 | \\CREATE INDEX IF NOT EXISTS idx_mailbox_reset_tokens_user ON mailbox_reset_tokens(username, created_at); | |
| 384 | ); | |
| 385 | ||
| 355 | 386 | // IMAP UID persistence tables |
| 356 | 387 | try self.exec( |
| 357 | 388 | \\CREATE TABLE IF NOT EXISTS imap_mailboxes ( |
| @@ -621,6 +652,7 @@ pub const Database = struct { | ||
| 621 | 652 | defer self.mutex.unlock(); |
| 622 | 653 | |
| 623 | 654 | try self.deleteUserSessionsLocked(username); |
| 655 | try self.deleteUserResetTokensLocked(username); | |
| 624 | 656 | |
| 625 | 657 | const sql = "DELETE FROM users WHERE username = ?1"; |
| 626 | 658 | |
| @@ -841,6 +873,23 @@ pub const Database = struct { | ||
| 841 | 873 | return self.deleteUserSessionsLocked(username); |
| 842 | 874 | } |
| 843 | 875 | |
| 876 | /// Void every password reset link for a mailbox. Called when its password | |
| 877 | /// or recovery address changes, so an older link cannot outlive either. | |
| 878 | pub fn deleteUserResetTokens(self: *Database, username: []const u8) !void { | |
| 879 | self.mutex.lock(); | |
| 880 | defer self.mutex.unlock(); | |
| 881 | return self.deleteUserResetTokensLocked(username); | |
| 882 | } | |
| 883 | ||
| 884 | fn deleteUserResetTokensLocked(self: *Database, username: []const u8) !void { | |
| 885 | var raw: ?*sqlite.sqlite3_stmt = null; | |
| 886 | if (sqlite.sqlite3_prepare_v2(self.db, "DELETE FROM mailbox_reset_tokens WHERE username = ?1", -1, &raw, null) != sqlite.SQLITE_OK) return DatabaseError.PrepareFailed; | |
| 887 | const stmt = Statement{ .stmt = raw.?, .allocator = self.allocator }; | |
| 888 | defer stmt.finalize(); | |
| 889 | try stmt.bind(1, username); | |
| 890 | _ = try stmt.step(); | |
| 891 | } | |
| 892 | ||
| 844 | 893 | fn deleteUserSessionsLocked(self: *Database, username: []const u8) !void { |
| 845 | 894 | var raw: ?*sqlite.sqlite3_stmt = null; |
| 846 | 895 | if (sqlite.sqlite3_prepare_v2(self.db, "DELETE FROM webmail_sessions WHERE username = ?1", -1, &raw, null) != sqlite.SQLITE_OK) return DatabaseError.PrepareFailed; |
| @@ -1639,8 +1688,12 @@ pub const Database = struct { | ||
| 1639 | 1688 | if (i == 0) moved = @intCast(sqlite.sqlite3_changes(self.db)); |
| 1640 | 1689 | } |
| 1641 | 1690 | |
| 1642 | { | |
| 1643 | const sql: [:0]const u8 = "DELETE FROM webmail_sessions WHERE username LIKE ?1 ESCAPE '\\'"; | |
| 1691 | // Sessions and reset links carry the old address, so they are voided | |
| 1692 | // rather than rewritten. Both are cheap to obtain again. | |
| 1693 | for ([_][:0]const u8{ | |
| 1694 | "DELETE FROM webmail_sessions WHERE username LIKE ?1 ESCAPE '\\'", | |
| 1695 | "DELETE FROM mailbox_reset_tokens WHERE username LIKE ?1 ESCAPE '\\'", | |
| 1696 | }) |sql| { | |
| 1644 | 1697 | var stmt: ?*sqlite.sqlite3_stmt = null; |
| 1645 | 1698 | if (sqlite.sqlite3_prepare_v2(self.db, sql.ptr, -1, &stmt, null) != sqlite.SQLITE_OK) |
| 1646 | 1699 | return DatabaseError.PrepareFailed; |