mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
MSN Messenger 1.0 to 4.7 and Windows Messenger 4.x sign in with BBS names and passwords on port 1863. The contact list is the friends list, the block list a new blocked list shared by every route (chat's /block and /unblock, MSN's Block); private messages to and from IRC, AIM and terminal users pass through switchboards, and kept messages are rung in at sign-in. - internal/msn: notification server and switchboards on one port - store: msn_keys (MD5 digests kept like AIM's) and blocks tables - bbs: blocks refused in TellUser and Message; /block, /unblock - oscar: IMs to a user who blocks the sender are refused - [msn] config section, docs/wiki/MSN-Messenger.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRTgFq9vbzTWgK3XYQL
92 lines
3.1 KiB
Go
92 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"errors"
|
|
)
|
|
|
|
// MSN Messenger's MD5 login (MSNP2 to MSNP7: MSN 1.0 to 4.7) proves the
|
|
// password by MD5(salt + password), the salt the server's, which a bcrypt
|
|
// hash cannot check. So each account gets a fixed salt and the digest of
|
|
// its password with it, kept whenever the password is known in the clear
|
|
// and bound to the password hash it was made with, as the AIM digests are
|
|
// (oscar.go). A fixed salt makes the digest replayable: anyone who sees
|
|
// a login can log in again. MSN 1.0 to 4.7 had no better way.
|
|
|
|
// MSNDigest is an MD5 login's answer: MD5(salt + password), in lowercase
|
|
// hex.
|
|
func MSNDigest(salt, pw string) string {
|
|
h := md5.Sum([]byte(salt + pw))
|
|
return hex.EncodeToString(h[:])
|
|
}
|
|
|
|
// SetKeepMSN says whether to keep the digests: only with an MSN server,
|
|
// as they would let anyone who read the database log in by MSN.
|
|
func (s *Store) SetKeepMSN(on bool) { s.keepMSN.Store(on) }
|
|
|
|
// MSNKey is the account's MD5 login salt and digest; ok false: no current
|
|
// digest (none kept, or the password changed since).
|
|
func (s *Store) MSNKey(id int64) (salt, digest string, ok bool, err error) {
|
|
var bound, hash string
|
|
err = s.db.QueryRow(`SELECT m.salt, m.digest, m.bound, u.password FROM msn_keys m
|
|
JOIN users u ON u.id = m.user_id WHERE m.user_id = ?`, id).Scan(&salt, &digest, &bound, &hash)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", "", false, nil
|
|
}
|
|
if err != nil {
|
|
return "", "", false, err
|
|
}
|
|
return salt, digest, bound == hash && digest != "", nil
|
|
}
|
|
|
|
// MSNSalt is the salt an MD5 login gives the account: its kept one, or a
|
|
// new one kept for it (with no digest yet).
|
|
func (s *Store) MSNSalt(id int64) (string, error) {
|
|
var salt string
|
|
err := s.db.QueryRow(`SELECT salt FROM msn_keys WHERE user_id = ?`, id).Scan(&salt)
|
|
if err == nil {
|
|
return salt, nil
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return "", err
|
|
}
|
|
b := make([]byte, 10)
|
|
rand.Read(b)
|
|
salt = hex.EncodeToString(b)
|
|
if _, err := s.db.Exec(`INSERT OR IGNORE INTO msn_keys (user_id, salt, digest, bound) VALUES (?, ?, '', '')`, id, salt); err != nil {
|
|
return "", err
|
|
}
|
|
// another login may have made one meanwhile
|
|
err = s.db.QueryRow(`SELECT salt FROM msn_keys WHERE user_id = ?`, id).Scan(&salt)
|
|
return salt, err
|
|
}
|
|
|
|
// KeepMSNKey makes the account's MD5 login digest of pw (its password,
|
|
// just checked or set), bound to its password hash now, if the store keeps
|
|
// them (SetKeepMSN). Nothing is written if it is current already.
|
|
func (s *Store) KeepMSNKey(id int64, pw string) error {
|
|
if !s.keepMSN.Load() || pw == "" {
|
|
return nil
|
|
}
|
|
var hash string
|
|
if err := s.db.QueryRow(`SELECT password FROM users WHERE id = ?`, id).Scan(&hash); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
salt, err := s.MSNSalt(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d := MSNDigest(salt, pw)
|
|
if _, old, ok, err := s.MSNKey(id); err == nil && ok && old == d {
|
|
return nil
|
|
}
|
|
_, err = s.db.Exec(`UPDATE msn_keys SET digest = ?, bound = ? WHERE user_id = ?`, d, hash, id)
|
|
return err
|
|
}
|