diff --git a/bbs.conf.sample b/bbs.conf.sample index aca1dee..a02cf3c 100644 --- a/bbs.conf.sample +++ b/bbs.conf.sample @@ -958,8 +958,10 @@ address = 127.0.0.1:2513 ; See docs/wiki/Yahoo-Messenger.md ;[ymsg] ;address = :5050 -; the web half, for Y!M 9 and later and the room list: point -; vcs1.msg.yahoo.com, vcs2.msg.yahoo.com and insider.msg.yahoo.com here +; the web half, for Y!M 9 and later, the room list and file transfer: point +; vcs1.msg.yahoo.com, vcs2.msg.yahoo.com, insider.msg.yahoo.com, +; relay.msg.yahoo.com and filetransfer.msg.yahoo.com here. File transfer +; needs it on port 80 ;http_address = :80 ; Y!M 9's login, its password over HTTPS: point login.yahoo.com here, with ; a certificate for it the client trusts @@ -973,6 +975,10 @@ address = 127.0.0.1:2513 ;max_per_ip = 4 ; the Yahoo ID the BBS's notices come from ;system = bbs +; the largest file users may send each other, in MB (0: no file transfer) +;max_file_mb = 16 +; give Y!M 6 to 8 Y!M 5's login instead of their own +;old_login = no ; Multi Relay Chat: the BBSes' chat network. Its rooms are the chat rooms ; beginning mrc- here (mrc-lobby is its lobby), on terminals and IRC alike. diff --git a/docs/wiki/Configuration-Reference.md b/docs/wiki/Configuration-Reference.md index 744ddc6..779f595 100644 --- a/docs/wiki/Configuration-Reference.md +++ b/docs/wiki/Configuration-Reference.md @@ -679,7 +679,7 @@ Yahoo! Messenger: the chat for Yahoo! Messenger clients (YMSG 9 to 16), beside I | key | default | live | what it does | |---|---|---|---| | `address` | | | See the section above. Examples: `:5050`. | -| `http_address` | | | the web half, for Y!M 9 and later and the room list: point vcs1.msg.yahoo.com, vcs2.msg.yahoo.com and insider.msg.yahoo.com here. Examples: `:80`. | +| `http_address` | | | the web half, for Y!M 9 and later, the room list and file transfer: point vcs1.msg.yahoo.com, vcs2.msg.yahoo.com, insider.msg.yahoo.com, relay.msg.yahoo.com and filetransfer.msg.yahoo.com here. File transfer needs it on port 80. Examples: `:80`. | | `https_address` | | | Y!M 9's login, its password over HTTPS: point login.yahoo.com here, with a certificate for it the client trusts. Examples: `:443`. | | `tls_cert` | | | Y!M 9's login, its password over HTTPS: point login.yahoo.com here, with a certificate for it the client trusts. Examples: `/etc/bbs/login.yahoo.com.crt`. | | `tls_key` | | | Y!M 9's login, its password over HTTPS: point login.yahoo.com here, with a certificate for it the client trusts. Examples: `/etc/bbs/login.yahoo.com.key`. | @@ -687,6 +687,8 @@ Yahoo! Messenger: the chat for Yahoo! Messenger clients (YMSG 9 to 16), beside I | `max_clients` | | | the pager's address the web half gives Y!M 9 (default: the address each one asked on; set it behind NAT) Examples: `256`. | | `max_per_ip` | | | the pager's address the web half gives Y!M 9 (default: the address each one asked on; set it behind NAT) Examples: `4`. | | `system` | | | the Yahoo ID the BBS's notices come from. Examples: `bbs`. | +| `max_file_mb` | | | the largest file users may send each other, in MB (0: no file transfer) Examples: `16`. | +| `old_login` | | | give Y!M 6 to 8 Y!M 5's login instead of their own. Examples: `no`. | ## `[mrc]` diff --git a/docs/wiki/Yahoo-Messenger.md b/docs/wiki/Yahoo-Messenger.md index e2dcf6e..9aaf95a 100644 --- a/docs/wiki/Yahoo-Messenger.md +++ b/docs/wiki/Yahoo-Messenger.md @@ -15,23 +15,30 @@ http_address = :80 | Setting | Default | What it sets | |---|---|---| | `address` | `:5050` | where the pager listens | -| `http_address` | none | the web half (the capacity lookup and the room list), plain HTTP | +| `http_address` | none | the web half (the capacity lookup, the room list and file transfer), plain HTTP; port 80 for file transfer | | `https_address` | none | the web half over HTTPS: the login of Y!M 9 and later | | `tls_cert`, `tls_key` | none | the certificate for `https_address` (for `login.yahoo.com`) | | `host` | the address each client asked on | the pager's address the capacity lookup gives; set it behind NAT | | `max_clients` | `256` | connections at once | | `max_per_ip` | `4` | from one address | | `system` | `bbs` | the Yahoo ID the BBS's notices come from | +| `max_file_mb` | `16` | the largest file users may send each other, in MB; `0` turns file transfer off | +| `old_login` | `no` | `yes` gives Y!M 6 to 8 the Y!M 5 login instead of their own | ## Versions served -YMSG 9 to 16. Two logins are served: +YMSG 9 to 16. Three logins are served: -- **The challenge login** (YMSG 9 to 15): Yahoo! Messenger 5.x, and - third-party clients of that time (Gaim, Kopete, libyahoo2's), which - take the login type the server offers. Yahoo! Messenger 6 to 8 used a - later challenge ("auth type 1") that is not served; whether they take - the older one when it is offered has not been tried yet. +- **The Y!M 5 challenge login** ("auth type 0", YMSG 9): Yahoo! + Messenger 5.x. +- **The Y!M 6 to 8 challenge login** ("auth type 1", YMSG 10 to 15): + Yahoo! Messenger 6, 7 and 8, and the third-party clients of that time + (Gaim, Kopete, libyahoo2's), which take whichever login the server + offers. Yahoo's version of this login ran the challenge through a set + of lookup tables; the BBS writes its challenges so that the client + runs through the tables zero times, so it needs no copy of them. If a + client turns out not to take these challenges, `old_login = yes` + offers it the Y!M 5 login instead. - **The token login** (YMSG 16): Yahoo! Messenger 9 and later, and the later Pidgin 2.x releases. The client sends the password over HTTPS to `login.yahoo.com`, gets a token, and proves it on the pager. This @@ -113,8 +120,30 @@ get it when they next log in. Lines kept for a Yahoo user come when they sign in, marked with when they were sent. The BBS's notices (new mail, a page for the sysop, errors) come from the `system` ID. -Sending files is not served: the client is told to use the BBS's file -areas. +## Sending files + +Files go between Yahoo! Messenger users, through the BBS. They are held +in a temporary file only until the receiver has fetched them (an hour +at most) and are never put in the file areas. A file to someone who +isn't on Yahoo! Messenger at the time is declined, and the sender is +told to use the BBS's [file areas](Files-and-FTP) instead. So is a file +over `max_file_mb`, or to someone who blocks the sender. + +This needs `http_address` on port 80, with these names sent to the BBS +(as for the other names above): + +- `relay.msg.yahoo.com` (and `relay.yahoo.com`): Yahoo! Messenger 7 and + later. The sender offers the file, the receiver accepts, and both + connect to the relay: the sender uploads, the receiver downloads. + Set `host` if the BBS is behind NAT, as the receiver is given that + address for the relay. +- `filetransfer.msg.yahoo.com`: Yahoo! Messenger 5 and 6. The sender + uploads the file and the receiver is sent a link to fetch it from. + +Neither way has been tried with the real clients yet, only with +scripted ones that do what libyahoo2 does. Direct connections between +two clients are not passed on, so clients that try one first fall back +to the relay or the upload. ## Chat rooms and conferences diff --git a/internal/server/ymsgconfig.go b/internal/server/ymsgconfig.go index a70eec6..4a646c4 100644 --- a/internal/server/ymsgconfig.go +++ b/internal/server/ymsgconfig.go @@ -54,6 +54,14 @@ func (c *Config) ymsgSection(cf *config.File) error { if y.MaxPerIP, err = s.Int("max_per_ip", 4, 0, 1000); err != nil { return err } + if y.OldLogin, err = s.Bool("old_login", false); err != nil { + return err + } + mb, err := s.Int("max_file_mb", 16, 0, 4096) + if err != nil { + return err + } + y.MaxFileSize = int64(mb) << 20 c.YMSG = y return nil }() diff --git a/internal/ymsg/auth1.go b/internal/ymsg/auth1.go new file mode 100644 index 0000000..dee0979 --- /dev/null +++ b/internal/ymsg/auth1.go @@ -0,0 +1,96 @@ +package ymsg + +import ( + "crypto/hmac" + "crypto/md5" + "crypto/rand" + "crypto/sha1" + "strings" + + "github.com/D-Modem/bbs/internal/store" +) + +// Auth type 1 is the challenge login of Y!M 6 to 8 (YMSG 10 to 15). +// +// The client turns the challenge into 20 bytes: a 4-byte key followed by +// MD5(key, depth, table). It searches for the depth and table that give +// that MD5, then runs the key through Yahoo's transform tables that many +// times. The answers are HMAC-SHA1 of the key, keyed with the two password +// digests auth type 0 already uses. +// +// The server picks the challenge, so it always picks depth 0 and table 0. +// The transform then runs no times and the key is used as it is, so the +// server needs none of the transform tables. + +const ( + chalLetters = "qzec2tb3um1olpar8whx4dfgijknsvy5" + chalOperands = "+|&%/*^-" + ansAlpha1 = "FBZDWAGHrJTLMNOPpRSKUVEXYChImkwQ" + ansAlpha2 = "F0E1D2C3B4A59687abcdefghijklmnop" +) + +// newChallenge1 returns an auth type 1 challenge and the 4-byte key the +// client will find in it. +func newChallenge1() (string, [4]byte) { + var key [4]byte + rand.Read(key[:]) + sum := md5.Sum(append(key[:], 0, 0, 0)) // depth 0, table 0 + + // The 20 bytes are ten 16-bit values, each written as three bytes: + // 0xe0|top 4 bits, then 6 bits, then 6 bits. + src := append(key[:], sum[:]...) + var r [2]byte + rand.Read(r[:]) + f := []byte{r[0]} // the client skips the first byte + for i := 0; i < len(src); i += 2 { + v := int(src[i])<<8 | int(src[i+1]) + f = append(f, byte(0xe0|v>>12), byte(v>>6&0x3f), byte(v&0x3f)) + } + f = append(f, r[1]) // the client stops one short of the end + + // The client mixes each byte with the one before it: + // f[i+1] = m[i+1] ^ m[i]*0xcd. Undo that so it gets f back. + m := make([]byte, len(f)) + m[0] = f[0] + for i := 1; i < len(f); i++ { + m[i] = f[i] ^ m[i-1]*0xcd + } + + // Each byte is a letter (top 5 bits) and an operator (low 3 bits). + // Brackets are ignored by the client; Yahoo's challenges had them. + var b strings.Builder + for i, c := range m { + if i%4 == 0 { + b.WriteByte('(') + } + b.WriteByte(chalLetters[c>>3]) + b.WriteByte(chalOperands[c&7]) + if i%4 == 3 || i == len(m)-1 { + b.WriteByte(')') + } + } + return b.String(), key +} + +// answer1 is one auth type 1 answer: HMAC-SHA1 of the key, keyed with a +// Y64 digest, written five characters per 16 bits. +func answer1(digest string, key [4]byte) string { + h := hmac.New(sha1.New, []byte(digest)) + h.Write(key[:]) + d := h.Sum(nil) + var b strings.Builder + for i := 0; i < len(d); i += 2 { + v := int(d[i])<<8 | int(d[i+1]) + b.WriteByte(ansAlpha1[v>>11&0x1f]) + b.WriteByte('=') + b.WriteByte(ansAlpha2[v>>6&0x1f]) + b.WriteByte(ansAlpha2[v>>1&0x1f]) + b.WriteByte(",;"[v&1]) + } + return b.String() +} + +// challengeAnswers1 are auth type 1's two answers (keys 6 and 96). +func challengeAnswers1(k store.YMSGKeys, key [4]byte) (string, string) { + return answer1(k.Pass, key), answer1(k.Crypt, key) +} diff --git a/internal/ymsg/auth1_test.go b/internal/ymsg/auth1_test.go new file mode 100644 index 0000000..557fee0 --- /dev/null +++ b/internal/ymsg/auth1_test.go @@ -0,0 +1,158 @@ +package ymsg + +import ( + "crypto/md5" + "crypto/sha1" + "strings" + "testing" + + "github.com/D-Modem/bbs/internal/store" +) + +// answers1 is the client side of auth type 1, step by step as libyahoo2's +// yahoo_process_auth_0x0b does it. It has no transform tables, so it fails +// the test unless the challenge needs none (depth 0). +func answers1(t *testing.T, pw, seed string) (string, string) { + t.Helper() + key := chalKey(t, seed) + p := md5.Sum([]byte(pw)) + c := md5.Sum([]byte(store.MD5Crypt(pw, "_2S43d5f"))) + return libAnswer(y64(p[:]), key), libAnswer(y64(c[:]), key) +} + +// chalKey reads a challenge as the client does: the 4-byte key, checking +// the depth is 0 and the table under 2 (above that, libyahoo2 changes the +// SHA-1 length, which Yahoo's client may not). +func chalKey(t *testing.T, seed string) [4]byte { + t.Helper() + // phase 1: letters set the high bits, operators add a byte + var magic []int + work := 0 + for i := 0; i < len(seed); i++ { + ch := seed[i] + switch { + case ch == '(' || ch == ')': + case ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9': + n := strings.IndexByte(chalLetters, ch) + if n < 0 { + t.Fatalf("challenge %q: %q would hang the client", seed, ch) + } + work = n << 3 + default: + n := strings.IndexByte(chalOperands, ch) + if n < 0 { + t.Fatalf("challenge %q: %q would hang the client", seed, ch) + } + if len(magic) < 64 { + magic = append(magic, work|n) + } + } + } + // phase 2 + for i := len(magic) - 2; i >= 0; i-- { + b1 := byte(magic[i]) + b1 *= 0xcd + b1 ^= byte(magic[i+1]) + magic[i+1] = int(b1) + } + // phase 3 + var src []byte + n := 1 + for len(src) < 20 { + cl := magic[n] + n++ + if n >= len(magic) { + t.Fatalf("challenge %q: too short", seed) + } + var bl int + if cl > 0x7f { + if cl < 0xe0 { + bl = (cl & 0x1f) << 6 + } else { + bl = magic[n] + n++ + bl = ((bl & 0x3f) + (cl&0x0f)<<6) << 6 + } + cl = magic[n] + n++ + bl = (cl & 0x3f) + bl + } else { + bl = cl + } + src = append(src, byte(bl>>8), byte(bl)) + } + var key [4]byte + copy(key[:], src) + for i := 0; i < 0xffff; i++ { + for j := 0; j < 5; j++ { + sum := md5.Sum([]byte{key[0], key[1], key[2], key[3], byte(i), byte(i >> 8), byte(j)}) + if string(sum[:]) == string(src[4:]) { + if i != 0 || j >= 2 { + t.Fatalf("challenge %q: depth %d, table %d", seed, i, j) + } + return key + } + } + if i > 4 { + break // it would be at depth 0 + } + } + t.Fatalf("challenge %q: no depth and table fit", seed) + return key +} + +// libAnswer is one answer: SHA-1 twice with the padded digest, written as +// libyahoo2 writes it. +func libAnswer(digest string, key [4]byte) string { + var x1, x2 [64]byte + for i := range x1 { + x1[i], x2[i] = 0x36, 0x5c + } + for i := 0; i < len(digest); i++ { + x1[i] = digest[i] ^ 0x36 + x2[i] = digest[i] ^ 0x5c + } + d1 := sha1.Sum(append(x1[:], key[:]...)) + d2 := sha1.Sum(append(x2[:], d1[:]...)) + var out string + for i := 0; i < 20; i += 2 { + v := int(d2[i])<<8 + int(d2[i+1]) + out += string(ansAlpha1[(v>>0x0b)&0x1f]) + "=" + string(ansAlpha2[(v>>0x06)&0x1f]) + + string(ansAlpha2[(v>>0x01)&0x1f]) + string(",;"[v&1]) + } + return out +} + +func TestChallenge1(t *testing.T) { + keys := store.YMSGDigests("password1") + for range 500 { + seed, key := newChallenge1() + if got := chalKey(t, seed); got != key { + t.Fatalf("challenge %q: key %x, want %x", seed, got, key) + } + w6, w96 := challengeAnswers1(keys, key) + g6, g96 := answers1(t, "password1", seed) + if w6 != g6 || w96 != g96 { + t.Fatalf("answers: %q %q, client %q %q", w6, w96, g6, g96) + } + if len(g6) != 50 { + t.Fatalf("answer %q", g6) + } + } +} + +func TestLoginTypes(t *testing.T) { + e := start(t) + for _, v := range []uint16{9, 11, 13, 15} { + if code, ok := dial(t, e, v).login("alice", "password1"); !ok { + t.Errorf("YMSG %d: refused (%s)", v, code) + } + if code, ok := dial(t, e, v).login("alice", "wrong"); ok || code != "13" { + t.Errorf("YMSG %d, wrong password: %q %v", v, code, ok) + } + } + e = startWith(t, func(s *Settings) { s.OldLogin = true }) + if code, ok := dial(t, e, 13).login("bob", "password1"); !ok { + t.Errorf("old_login: refused (%s)", code) + } +} diff --git a/internal/ymsg/filexfer.go b/internal/ymsg/filexfer.go new file mode 100644 index 0000000..bda9861 --- /dev/null +++ b/internal/ymsg/filexfer.go @@ -0,0 +1,569 @@ +package ymsg + +import ( + "bufio" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/url" + "os" + "strconv" + "strings" + "time" + + "github.com/D-Modem/bbs/internal/bbs" + "github.com/D-Modem/bbs/internal/store" +) + +// File transfer, with the BBS's web half as the go-between. Files go only +// between Yahoo! Messenger users, and pass through a temporary file. +// +// Y!M 7 and later (FILETRANSFER_15, 0xdc to 0xde, through a relay): +// +// 1. The sender offers the file (0xdc, 222=1); the BBS passes it on. +// 2. The receiver accepts (222=3) or declines (222=4); passed back. +// 3. The sender asks for a relay (0xdd); the BBS gives the receiver the +// relay's address (250, the web half) and a token for it (251). +// 4. The receiver says it's ready (0xde) and fetches +// http:///relay?token=... (HEAD, then GET); the BBS gives the +// sender the token and the sender POSTs the file to the same URL. +// +// Y!M 5 and 6 (FILETRANSFER, 0x46): the sender POSTs a YMSG packet and the +// file to http://filetransfer.msg.yahoo.com/notifyft. The BBS keeps the +// file and sends the receiver a 0x46 with a URL to fetch it from. + +const ( + xferWait = 15 * time.Minute // an offer, until its file is uploaded + xferKeep = time.Hour // an uploaded file, until it's fetched + xferIdle = 2 * time.Minute // the longest pause in an upload or download + y7Files = 12 // the first YMSG version with FILETRANSFER_15 +) + +// xfer is a file on its way from one user to another. +type xfer struct { + id string // the sender's ID for it (key 265; Y!M 7 and later) + token string // the relay's or the download URL's + from, to *store.User + src, dst *session + name string + size int64 + + expires time.Time // s.mu + started bool // s.mu: an upload began + + done chan struct{} // closed when the upload ends + path string // the temporary file, once uploaded ("": it failed) + n int64 +} + +// filesOff says why files can't be sent ("": they can). +func (s *Server) filesOff() string { + if s.S.HTTPAddress == "" || s.S.MaxFileSize <= 0 { + return "Sending files by Yahoo! Messenger isn't set up here: upload the file to the BBS's file areas instead." + } + return "" +} + +// newXfer registers a transfer and gives it a token. +func (s *Server) newXfer(x *xfer) { + x.token = randomString(32) + x.done = make(chan struct{}) + s.mu.Lock() + defer s.mu.Unlock() + s.expireXfers() + x.expires = time.Now().Add(xferWait) + s.xfers[x.token] = x +} + +// xferByToken is a transfer by its token (nil: none, or expired). +func (s *Server) xferByToken(t string) *xfer { + s.mu.Lock() + defer s.mu.Unlock() + s.expireXfers() + return s.xfers[t] +} + +// xferByID is a Y!M 7 transfer a session is sending or receiving. +func (s *Server) xferByID(id string, ss *session) *xfer { + s.mu.Lock() + defer s.mu.Unlock() + for _, x := range s.xfers { + if x.id == id && (x.src == ss || x.dst == ss) { + return x + } + } + return nil +} + +// dropXfer forgets a transfer and removes its file, once any upload of it +// has ended. +func (s *Server) dropXfer(x *xfer) { + s.mu.Lock() + _, ok := s.xfers[x.token] + delete(s.xfers, x.token) + started := x.started + x.started = true // no upload may begin now + s.mu.Unlock() + if !ok { + return + } + if !started { + close(x.done) + return + } + go func() { + <-x.done + if x.path != "" { + os.Remove(x.path) + } + }() +} + +// expireXfers drops transfers past their time (s.mu held). +func (s *Server) expireXfers() { + now := time.Now() + for t, x := range s.xfers { + if now.After(x.expires) { + delete(s.xfers, t) + if !x.started { + x.started = true + close(x.done) + continue + } + go func() { + <-x.done + if x.path != "" { + os.Remove(x.path) + } + }() + } + } +} + +// receiver is the session a user gets files on: their newest signed-in +// Yahoo session that understands FILETRANSFER_15 (y7) or any (!y7). why +// is the reason there's none. +func (s *Server) receiver(t *store.User, y7 bool) (dst *session, why string) { + any := false + for _, o := range s.sessionsOf(t.ID) { + if !o.isOnline() { + continue + } + any = true + if !y7 || o.ver >= y7Files { + dst = o + } + } + switch { + case dst != nil: + return dst, "" + case any: + return nil, idOf(t) + "'s Yahoo! Messenger is too old to take files from yours." + default: + return nil, idOf(t) + " isn't on Yahoo! Messenger now. Files go only between Yahoo! Messenger " + + "users: upload it to the BBS's file areas instead." + } +} + +// fileCheck says why ss may not send a file of size bytes to who ("": it +// may), with the user, or nil when who is ss's own. +func (ss *session) fileCheck(who string, size int64) (*store.User, string) { + s := ss.s + if why := s.filesOff(); why != "" { + return nil, why + } + t := s.resolve(who) + if t == nil { + return nil, who + " is not a user here." + } + if t.ID == ss.u.ID { + return nil, "" + } + if s.B.Blocks(t, ss.u) { + return nil, bbs.ErrBlocked.Error() + "." + } + if size > s.S.MaxFileSize { + return nil, fmt.Sprintf("That file is too big to send here: the most is %s.", sizeText(s.S.MaxFileSize)) + } + return t, "" +} + +// sizeText is a size in bytes for people. +func sizeText(n int64) string { + if n >= 1<<20 && n%(1<<20) == 0 { + return strconv.FormatInt(n>>20, 10) + " MB" + } + if n >= 1<<10 { + return strconv.FormatInt(n>>10, 10) + " KB" + } + return strconv.FormatInt(n, 10) + " bytes" +} + +// fileTrans15 is FILETRANSFER_15: an offer, or the answer to one. +func (ss *session) fileTrans15(p *packet) { + s := ss.s + id := p.get(265) + if id == "" { + return + } + if p.get(222) == "1" { + ss.offerFile(p, id) + return + } + x := s.xferByID(id, ss) + if x == nil || x.dst != ss { + return + } + ans := p.get(222) + if ans != "3" { + s.dropXfer(x) + } + x.src.k.send(svcFileTrans15, stOK, kv(4, ss.id), kv(5, x.src.id), kv(265, id), kv(222, ans)) +} + +// offerFile passes a Y!M 7 offer to the receiver, or declines it for them. +func (ss *session) offerFile(p *packet, id string) { + s := ss.s + who := p.get(5) + size, _ := strconv.ParseInt(p.get(28), 10, 64) + decline := func(why string) { + ss.k.send(svcFileTrans15, stOK, kv(4, who), kv(5, ss.id), kv(265, id), kv(222, "4")) + ss.system(why) + } + t, why := ss.fileCheck(who, size) + if why != "" { + decline(why) + return + } + if t == nil || s.xferByID(id, ss) != nil { + return + } + dst, why := s.receiver(t, true) + if dst == nil { + decline(why) + return + } + x := &xfer{id: id, from: ss.u, to: t, src: ss, dst: dst, name: p.get(27), size: size} + s.newXfer(x) + out := &packet{service: svcFileTrans15, status: stOK} + out.add(4, ss.id) + out.add(5, dst.id) + out.add(265, id) + out.add(222, "1") + for _, kv := range p.pairs { + switch kv.k { + case 1, 4, 5, 222, 265: + case 14: + out.add(14, dst.toClient(fromYahoo(kv.v, p.get(97) == "1"))) + default: + out.add(kv.k, kv.v) + } + } + dst.k.write(out) +} + +// fileTransInfo15 is the sender asking for a relay: the receiver is told +// where it is and the token for it. +func (ss *session) fileTransInfo15(p *packet) { + x := ss.s.xferByID(p.get(265), ss) + if x == nil || x.src != ss { + return + } + relay := ss.s.S.Host + if relay == "" { + relay = p.get(250) + } + if relay == "" { + relay, _, _ = net.SplitHostPort(ss.k.c.LocalAddr().String()) + } + x.dst.k.send(svcFileTransInfo15, stOK, kv(4, ss.id), kv(5, x.dst.id), kv(265, x.id), kv(27, x.name), + kv(249, "3"), kv(250, relay), kv(251, x.token)) +} + +// fileTransAcc15 is the receiver ready at the relay: the sender is given +// the token to upload with. +func (ss *session) fileTransAcc15(p *packet) { + x := ss.s.xferByID(p.get(265), ss) + if x == nil || x.dst != ss { + return + } + x.src.k.send(svcFileTransAcc15, stOK, kv(4, ss.id), kv(5, x.src.id), kv(265, x.id), kv(27, x.name), + kv(249, "3"), kv(251, x.token)) +} + +// The web half's file transfer URLs. + +// relay is /relay: the Y!M 7 relay. The sender POSTs, the receiver HEADs +// and GETs. +func (s *Server) relay(w http.ResponseWriter, r *http.Request) { + q := r.URL.Query() + x := s.xferByToken(q.Get("token")) + if x == nil || x.id == "" { + http.NotFound(w, r) + return + } + if !s.is(q.Get("sender"), x.from) || !s.is(q.Get("recver"), x.to) { + http.Error(w, "not yours", http.StatusForbidden) + return + } + switch r.Method { + case http.MethodPost: + if err := s.upload(x, r.Body, w); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + w.Write([]byte("OK")) + case http.MethodHead, http.MethodGet: + s.download(x, w, r) + default: + http.Error(w, "no", http.StatusMethodNotAllowed) + } +} + +// is says whether a Yahoo ID in a URL is u's ("" is anyone's). +func (s *Server) is(id string, u *store.User) bool { + if id == "" { + return true + } + o := s.resolve(id) + return o != nil && o.ID == u.ID +} + +// upload stores a transfer's file from r, once. +func (s *Server) upload(x *xfer, r io.Reader, w http.ResponseWriter) (err error) { + s.mu.Lock() + if x.started { + s.mu.Unlock() + return errors.New("already sent") + } + x.started = true + s.mu.Unlock() + defer func() { + s.mu.Lock() + x.expires = time.Now().Add(xferKeep) + s.mu.Unlock() + close(x.done) + }() + f, err := os.CreateTemp("", "bbs-ymsg-*") + if err != nil { + s.B.Log.Printf("ymsg: file transfer: %v", err) + return errors.New("no room") + } + rc := http.NewResponseController(w) + n, err := io.Copy(f, &idle{r: io.LimitReader(r, s.S.MaxFileSize+1), rc: rc}) + if err == nil && n > s.S.MaxFileSize { + err = errors.New("too big") + } + if cerr := f.Close(); err == nil { + err = cerr + } + if err != nil { + os.Remove(f.Name()) + return err + } + x.path, x.n = f.Name(), n + return nil +} + +// download serves a transfer's file once it's uploaded, then drops it. +func (s *Server) download(x *xfer, w http.ResponseWriter, r *http.Request) { + rc := http.NewResponseController(w) + rc.SetWriteDeadline(time.Now().Add(xferWait + xferIdle)) + t := time.NewTimer(xferWait) + defer t.Stop() + select { + case <-x.done: + case <-t.C: + http.NotFound(w, r) + return + case <-r.Context().Done(): + return + } + if x.path == "" { + http.NotFound(w, r) + return + } + f, err := os.Open(x.path) + if err != nil { + http.NotFound(w, r) + return + } + defer f.Close() + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Length", strconv.FormatInt(x.n, 10)) + if r.Method == http.MethodHead { + return + } + n, err := io.Copy(&idle{w: w, rc: rc}, f) + if err == nil && n == x.n { + s.dropXfer(x) + } +} + +// idle reads or writes, giving each step xferIdle. +type idle struct { + r io.Reader + w io.Writer + rc *http.ResponseController +} + +func (d *idle) Read(b []byte) (int, error) { + t := time.Now().Add(xferIdle) + d.rc.SetReadDeadline(t) + d.rc.SetWriteDeadline(t) // for the answer, after a long upload + return d.r.Read(b) +} + +func (d *idle) Write(b []byte) (int, error) { + d.rc.SetWriteDeadline(time.Now().Add(xferIdle)) + return d.w.Write(b) +} + +// notifyFT is /notifyft: a Y!M 5 or 6 upload. The body is a FILETRANSFER +// packet (0 the sender, 5 the receiver, 14 a message, 27 the file's name, +// 28 its size) ending with key 29, then the file. +func (s *Server) notifyFT(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "no", http.StatusMethodNotAllowed) + return + } + ss := s.byCookie(r.Header.Get("Cookie")) + if ss == nil { + http.Error(w, "not signed in", http.StatusForbidden) + return + } + br := bufio.NewReader(r.Body) + p, err := readFTHeader(br) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + who := p.get(5) + size, _ := strconv.ParseInt(p.get(28), 10, 64) + t, why := ss.fileCheck(who, size) + var dst *session + if why == "" && t != nil { + dst, why = s.receiver(t, false) + } + if why != "" || t == nil { + if why != "" { + ss.system(why) + } + http.Error(w, "not sent", http.StatusForbidden) + return + } + x := &xfer{from: ss.u, to: t, src: ss, dst: dst, name: p.get(27), size: size} + s.newXfer(x) + if err := s.upload(x, br, w); err != nil { + s.dropXfer(x) + ss.system("Your file " + x.name + " didn't reach " + who + ": " + err.Error() + ".") + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + host := r.Host + if host == "" { + host = s.pagerHost(r) + } + u := "http://" + host + "/ft/" + x.token + "/" + url.PathEscape(x.name) + exp := time.Now().Add(xferKeep).Unix() + dst.k.send(svcFileTransfer, stOK, kv(4, ss.id), kv(5, dst.id), + kv(14, dst.toClient(fromYahoo(p.get(14), p.get(97) == "1"))), kv(20, u), kn(38, exp), + kv(27, x.name), kn(28, x.n)) + w.Write([]byte("OK")) +} + +// readFTHeader reads /notifyft's packet, up to and including key 29. +func readFTHeader(br *bufio.Reader) (*packet, error) { + var h [20]byte + if _, err := io.ReadFull(br, h[:]); err != nil || string(h[:4]) != "YMSG" { + return nil, errors.New("not a YMSG packet") + } + p := &packet{} + var total int + field := func() (string, error) { + var b []byte + for { + c, err := br.ReadByte() + if err != nil { + return "", err + } + if total++; total > 8192 { + return "", errors.New("packet too long") + } + if c == 0x80 && len(b) > 0 && b[len(b)-1] == 0xc0 { + return string(b[:len(b)-1]), nil + } + b = append(b, c) + } + } + for { + k, err := field() + if err != nil { + return nil, err + } + if k == "29" { + return p, nil + } + v, err := field() + if err != nil { + return nil, err + } + n, err := strconv.Atoi(k) + if err != nil { + return nil, errors.New("bad key") + } + p.add(n, v) + } +} + +// ft is /ft//: a Y!M 5 or 6 upload's file, for its receiver. +func (s *Server) ft(w http.ResponseWriter, r *http.Request) { + rest := strings.TrimPrefix(r.URL.Path, "/ft/") + tok, _, _ := strings.Cut(rest, "/") + x := s.xferByToken(tok) + if x == nil || x.id != "" || (r.Method != http.MethodGet && r.Method != http.MethodHead) { + http.NotFound(w, r) + return + } + s.download(x, w, r) +} + +// byCookie is the signed-in session a Cookie header's T cookie was given to. +func (s *Server) byCookie(h string) *session { + var t string + for _, part := range strings.Split(h, ";") { + part = strings.TrimSpace(part) + if v, ok := strings.CutPrefix(part, "T="); ok { + t = v + } + } + if t == "" { + return nil + } + for _, ss := range s.all() { + ss.mu.Lock() + ok := false + for _, c := range ss.tcookies { + if c == t { + ok = true + } + } + ss.mu.Unlock() + if ok && ss.isOnline() { + return ss + } + } + return nil +} + +// noteCookie remembers a T cookie a session was given (the last few). +func (ss *session) noteCookie(t string) { + ss.mu.Lock() + defer ss.mu.Unlock() + ss.tcookies = append(ss.tcookies, t) + if len(ss.tcookies) > 4 { + ss.tcookies = ss.tcookies[1:] + } +} diff --git a/internal/ymsg/filexfer_test.go b/internal/ymsg/filexfer_test.go new file mode 100644 index 0000000..293affd --- /dev/null +++ b/internal/ymsg/filexfer_test.go @@ -0,0 +1,191 @@ +package ymsg + +import ( + "bytes" + "io" + "net/http" + "net/url" + "strings" + "testing" +) + +// web makes a request of the web half: the status and body. +func web(t *testing.T, method, u, cookie string, body []byte) (int, string, http.Header) { + t.Helper() + req, err := http.NewRequest(method, u, bytes.NewReader(body)) + if err != nil { + t.Fatal(err) + } + if body == nil { + req.Body = nil + } + if cookie != "" { + req.Header.Set("Cookie", cookie) + } + r, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + defer r.Body.Close() + b, _ := io.ReadAll(r.Body) + return r.StatusCode, string(b), r.Header +} + +// offer is a Y!M 7 file offer as libyahoo2 sends it. +func offer(cl *client, to, id, name, size string) { + cl.send(svcFileTrans15, 0, kv(1, cl.id), kv(5, to), kv(265, id), kv(222, "1"), kv(266, "1"), + kv(302, "268"), kv(300, "268"), kv(27, name), kv(28, size), kv(301, "268"), kv(303, "268")) +} + +func TestFileRelay(t *testing.T) { + e := start(t) + alice := signedIn(t, e, "alice", 15) + bob := signedIn(t, e, "bob", 15) + const id = "abcdefghijklmnopqrstuv$$" + + offer(alice, "bob", id, "hello.txt", "11") + o := bob.expect(svcFileTrans15, nil) + if o.get(4) != "alice" || o.get(5) != "bob" || o.get(265) != id || o.get(222) != "1" || + o.get(27) != "hello.txt" || o.get(28) != "11" { + t.Fatalf("offer: %v", o.pairs) + } + bob.send(svcFileTrans15, 0, kv(1, "bob"), kv(5, "alice"), kv(265, id), kv(222, "3")) + if a := alice.expect(svcFileTrans15, nil); a.get(4) != "bob" || a.get(222) != "3" || a.get(265) != id { + t.Fatalf("accept: %v", a.pairs) + } + alice.send(svcFileTransInfo15, 0, kv(1, "alice"), kv(5, "bob"), kv(265, id), kv(27, "hello.txt"), + kv(249, "3"), kv(250, "192.0.2.1")) + info := bob.expect(svcFileTransInfo15, nil) + tok := info.get(251) + if info.get(4) != "alice" || info.get(250) != "127.0.0.1" || len(tok) < 20 { + t.Fatalf("info: %v", info.pairs) + } + bob.send(svcFileTransAcc15, 0, kv(1, "bob"), kv(5, "alice"), kv(265, id), kv(27, "hello.txt"), + kv(249, "3"), kv(251, tok)) + if acc := alice.expect(svcFileTransAcc15, nil); acc.get(251) != tok || acc.get(4) != "bob" { + t.Fatalf("ready: %v", acc.pairs) + } + + relay := "http://" + e.web + "/relay?token=" + url.QueryEscape(tok) + if code, _, _ := web(t, "GET", relay+"&sender=alice&recver=carolann", "", nil); code != http.StatusForbidden { + t.Errorf("someone else's: %d", code) + } + // the receiver's HEAD comes first, and waits for the upload + head := make(chan int, 1) + go func() { + code, _, h := web(t, "HEAD", relay+"&sender=alice&recver=bob", "", nil) + if h.Get("Content-Length") != "11" { + code = -1 + } + head <- code + }() + if code, _, _ := web(t, "POST", relay+"&sender=alice&recver=bob", "T=x; Y=y", []byte("hello there")); code != 200 { + t.Fatalf("upload: %d", code) + } + if code := <-head; code != 200 { + t.Fatalf("head: %d", code) + } + if code, body, _ := web(t, "GET", relay+"&sender=alice&recver=bob", "", nil); code != 200 || body != "hello there" { + t.Fatalf("download: %d %q", code, body) + } + if code, _, _ := web(t, "GET", relay, "", nil); code != http.StatusNotFound { + t.Errorf("fetched twice: %d", code) + } + if code, _, _ := web(t, "POST", relay, "", []byte("again")); code != http.StatusNotFound { + t.Errorf("uploaded again: %d", code) + } + + // declined + offer(alice, "bob", "second", "b.txt", "3") + bob.expect(svcFileTrans15, func(p *packet) bool { return p.get(265) == "second" }) + bob.send(svcFileTrans15, 0, kv(1, "bob"), kv(5, "alice"), kv(265, "second"), kv(222, "4")) + alice.expect(svcFileTrans15, func(p *packet) bool { return p.get(265) == "second" && p.get(222) == "4" }) + if x := e.srv.xferByID("second", alice.sessionFor(t, e)); x != nil { + t.Error("declined offer kept") + } +} + +// sessionFor is the server's session for a client's user. +func (cl *client) sessionFor(t *testing.T, e *env) *session { + t.Helper() + u, _ := e.st.UserByName(cl.id) + ss := e.srv.sessionsOf(u.ID) + if len(ss) == 0 { + t.Fatal("no session") + } + return ss[0] +} + +func TestFileRefused(t *testing.T) { + e := start(t) + alice := signedIn(t, e, "alice", 15) + declined := func(to, id, why string) { + t.Helper() + alice.expect(svcFileTrans15, func(p *packet) bool { + return p.get(265) == id && p.get(222) == "4" && p.get(4) == to + }) + m := alice.expect(svcMessage, nil) + if m.get(4) != "bbs" || !strings.Contains(m.get(14), why) { + t.Errorf("%s: %q", id, m.get(14)) + } + } + offer(alice, "carolann", "1", "a.txt", "10") // not on Yahoo + declined("carolann", "1", "isn't on Yahoo! Messenger") + offer(alice, "nobody", "2", "a.txt", "10") + declined("nobody", "2", "not a user") + signedIn(t, e, "bob", 10) // too old for 0xdc + offer(alice, "bob", "3", "a.txt", "10") + declined("bob", "3", "too old") + signedIn(t, e, "bob", 15) + offer(alice, "bob", "4", "a.txt", "2000000") + declined("bob", "4", "too big") +} + +func TestFileUpload(t *testing.T) { + e := start(t) + alice := signedIn(t, e, "alice", 10) + bob := signedIn(t, e, "bob", 10) + var cookie string + for _, c := range alice.list.all(59) { + if v, ok := strings.CutPrefix(c, "T\t"); ok { + cookie = "Y=whatever; T=" + strings.SplitN(v, ";", 2)[0] + } + } + if cookie == "" { + t.Fatalf("no T cookie: %v", alice.list.pairs) + } + + // as libyahoo2 0.7 sends it: a FILETRANSFER packet, key 29, the file + body := func(to string, file []byte) []byte { + p := &packet{version: 10, service: svcFileTransfer} + p.add(0, "alice").add(5, to).add(14, "for you").add(27, "notes.txt").add(28, "9") + b := p.encode() + b = append(b, '2', '9', 0xc0, 0x80) + return append(b, file...) + } + ft := "http://" + e.web + "/notifyft" + if code, _, _ := web(t, "POST", ft, "Y=a; T=z=forged", body("bob", []byte("some text"))); code != http.StatusForbidden { + t.Errorf("not signed in: %d", code) + } + if code, _, _ := web(t, "POST", ft, cookie, body("bob", []byte("some text"))); code != 200 { + t.Fatalf("upload: %d", code) + } + m := bob.expect(svcFileTransfer, nil) + u := m.get(20) + if m.get(4) != "alice" || m.get(14) != "for you" || m.get(27) != "notes.txt" || m.get(28) != "9" || + !strings.HasPrefix(u, "http://"+e.web+"/ft/") || m.get(38) == "" { + t.Fatalf("notice: %v", m.pairs) + } + if code, b, _ := web(t, "GET", u, "", nil); code != 200 || b != "some text" { + t.Fatalf("download: %d %q", code, b) + } + if code, _, _ := web(t, "GET", u, "", nil); code != http.StatusNotFound { + t.Errorf("fetched twice: %d", code) + } + + // to someone not on Yahoo: refused, and alice told why + if code, _, _ := web(t, "POST", ft, cookie, body("carolann", []byte("some text"))); code != http.StatusForbidden { + t.Errorf("to carolann: %d", code) + } + alice.expect(svcMessage, func(p *packet) bool { return strings.Contains(p.get(14), "isn't on Yahoo") }) +} diff --git a/internal/ymsg/http.go b/internal/ymsg/http.go index c57c433..fcd1d08 100644 --- a/internal/ymsg/http.go +++ b/internal/ymsg/http.go @@ -23,6 +23,8 @@ import ( // /config/pwtoken_get, /config/pwtoken_login (login.yahoo.com, HTTPS): // the YMSG 16 login: the password, then the token it gave // /ycontent/ (insider.msg.yahoo.com): the chat room list +// /relay (relay.msg.yahoo.com): Y!M 7's file transfer relay +// /notifyft, /ft/ (filetransfer.msg.yahoo.com): Y!M 5 and 6's file uploads // serveWeb serves the web half until ctx is done. func (s *Server) serveWeb(ctx context.Context) error { @@ -105,6 +107,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.pwtokenLogin(w, r) case strings.HasPrefix(r.URL.Path, "/ycontent"): s.roomList(w, r) + case r.URL.Path == "/relay": + s.relay(w, r) + case r.URL.Path == "/notifyft": + s.notifyFT(w, r) + case strings.HasPrefix(r.URL.Path, "/ft/"): + s.ft(w, r) default: http.NotFound(w, r) } diff --git a/internal/ymsg/lists.go b/internal/ymsg/lists.go index 0823ea5..7a27d86 100644 --- a/internal/ymsg/lists.go +++ b/internal/ymsg/lists.go @@ -101,6 +101,7 @@ func (ss *session) sendLists() { } } y, t := ss.cookies() + ss.noteCookie(t) p.add(89, ss.id).add(59, "Y\t"+y+"; expires=Thu, 15 Apr 2030 20:00:00 GMT; path=/; domain=.yahoo.com"). add(59, "T\t"+t+"; expires=Thu, 15 Apr 2030 20:00:00 GMT; path=/; domain=.yahoo.com"). add(90, "1").add(100, "0").add(101, "").add(102, "").add(93, "86400").add(3, ss.id) diff --git a/internal/ymsg/server.go b/internal/ymsg/server.go index 47237c8..3a589af 100644 --- a/internal/ymsg/server.go +++ b/internal/ymsg/server.go @@ -56,6 +56,12 @@ type Settings struct { MaxPerIP int // System is the Yahoo ID the BBS's notices come from System string + // OldLogin offers Y!M 6 to 8 (YMSG 10 to 15) the Y!M 5 challenge + // login (auth type 0) instead of their own (auth type 1) + OldLogin bool + // MaxFileSize is the largest file users may send each other (0: no + // file transfer). It needs the web half on port 80. + MaxFileSize int64 } // Server serves YMSG. @@ -68,6 +74,7 @@ type Server struct { byNode map[int]*session tokens map[string]*token // pwtoken_get's, by token cookies map[string]*token // pwtoken_login's, by T cookie + xfers map[string]*xfer // files on their way, by token } // token is a YMSG 16 login in progress: the user whose password was @@ -91,7 +98,7 @@ func New(b *bbs.BBS, s Settings) (*Server, error) { s.MaxClients = 256 } return &Server{B: b, S: s, sessions: map[*session]bool{}, byNode: map[int]*session{}, - tokens: map[string]*token{}, cookies: map[string]*token{}}, nil + tokens: map[string]*token{}, cookies: map[string]*token{}, xfers: map[string]*xfer{}}, nil } // Run listens until ctx is done: the pager, and the web half if set. diff --git a/internal/ymsg/session.go b/internal/ymsg/session.go index 17da594..de3e626 100644 --- a/internal/ymsg/session.go +++ b/internal/ymsg/session.go @@ -62,6 +62,7 @@ type session struct { chats map[string]string // its chat rooms: BBS room -> name as the client calls it confs map[string]string // its conferences: BBS room -> conference name keptSent bool + tcookies []string // T cookies it was given, for /notifyft } // handle serves a pager connection: the sign-in, then its packets. @@ -126,6 +127,7 @@ func (ss *session) signIn() (initial uint32, ok bool) { s := ss.s k := ss.k var seed string + var key1 [4]byte authType := 0 for range 8 { p, err := k.read() @@ -143,10 +145,14 @@ func (ss *session) signIn() (initial uint32, ok bool) { return 0, false } k.session = randomSession() - if ss.ver >= 16 { + switch { + case ss.ver >= 16: authType = 2 seed = randomString(24) - } else { + case ss.ver >= 10 && !s.S.OldLogin: + authType = 1 + seed, key1 = newChallenge1() + default: seed = randomString(24) } k.send(svcAuth, stServerAck, kv(1, ss.id), kv(94, seed), kn(13, int64(authType))) @@ -156,10 +162,17 @@ func (ss *session) signIn() (initial uint32, ok bool) { } var u *store.User var err error - if authType == 2 { + switch authType { + case 2: u, err = ss.tokenLogin(p, seed) - } else { - u, err = ss.challengeLogin(p, seed) + case 1: + u, err = ss.challengeLogin(p, func(k store.YMSGKeys, _ string) (string, string) { + return challengeAnswers1(k, key1) + }) + default: + u, err = ss.challengeLogin(p, func(k store.YMSGKeys, user string) (string, string) { + return challengeAnswers(k, user, seed) + }) } if err == nil { err = s.may(u) @@ -184,7 +197,9 @@ func (ss *session) signIn() (initial uint32, ok bool) { // challengeLogin checks auth type 0's answers (keys 6 and 96) against the // account's kept digests. -func (ss *session) challengeLogin(p *packet, seed string) (*store.User, error) { +// challengeLogin checks an auth type 0 or 1 answer. answers gives the +// expected keys 6 and 96 for an account's digests and the ID as typed. +func (ss *session) challengeLogin(p *packet, answers func(store.YMSGKeys, string) (string, string)) (*store.User, error) { s := ss.s user := p.get(0) if user == "" { @@ -211,7 +226,7 @@ func (ss *session) challengeLogin(p *packet, seed string) (*store.User, error) { stale = true return nil, store.ErrNotFound } - want6, want96 := challengeAnswers(keys, user, seed) + want6, want96 := answers(keys, user) got6, got96 := p.get(6), p.get(96) if got6 == "" || subtle.ConstantTimeCompare([]byte(want6), []byte(got6)) != 1 { return nil, store.ErrNotFound @@ -421,11 +436,15 @@ func (ss *session) packet(p *packet) { ss.chatLogout() case svcChatAddInvite: ss.chatInvite(p) - case svcFileTransfer, svcP2PFileXfer, svcFileTrans15, svcFileTransInfo15, svcFileTransAcc15, svcPeerToPeer: - if to := p.get(5); to != "" && p.service != svcPeerToPeer { - ss.system("Sending files by Yahoo! Messenger isn't available here: upload the file to " + - "the BBS's file areas instead.") - } + case svcFileTrans15: + ss.fileTrans15(p) + case svcFileTransInfo15: + ss.fileTransInfo15(p) + case svcFileTransAcc15: + ss.fileTransAcc15(p) + case svcP2PFileXfer, svcPeerToPeer: + // direct connections between clients: not passed on, so the + // clients fall back to the relay or the upload } } diff --git a/internal/ymsg/ymsg_test.go b/internal/ymsg/ymsg_test.go index 6377fc2..3fd3aa9 100644 --- a/internal/ymsg/ymsg_test.go +++ b/internal/ymsg/ymsg_test.go @@ -63,7 +63,10 @@ func freeAddr(t *testing.T) string { // start is a BBS (alice, bob, Carol Ann and sysop, all password1; old, // made before Yahoo digests were kept) with a Yahoo server. -func start(t *testing.T) *env { +func start(t *testing.T) *env { t.Helper(); return startWith(t, nil) } + +// startWith is start, with settings changed by set. +func startWith(t *testing.T, set func(*Settings)) *env { t.Helper() store.BcryptCost = 4 st, err := store.Open(filepath.Join(t.TempDir(), "bbs.db")) @@ -85,7 +88,11 @@ func start(t *testing.T) *env { logs := &syncBuf{} b := bbs.New(s, st, log.New(logs, "", 0)) e := &env{b: b, st: st, addr: freeAddr(t), web: freeAddr(t)} - srv, err := New(b, Settings{Address: e.addr, HTTPAddress: e.web, MaxPerIP: 50, Host: "127.0.0.1"}) + ss := Settings{Address: e.addr, HTTPAddress: e.web, MaxPerIP: 50, Host: "127.0.0.1", MaxFileSize: 1 << 20} + if set != nil { + set(&ss) + } + srv, err := New(b, ss) if err != nil { t.Fatal(err) } @@ -124,6 +131,9 @@ type client struct { session uint32 id string ch chan *packet + + oldLogin bool // the server offers auth type 0 to all + list *packet // the LIST it was sent at sign-in } func dial(t *testing.T, e *env, ver uint16) *client { @@ -133,7 +143,8 @@ func dial(t *testing.T, e *env, ver uint16) *client { t.Fatal(err) } t.Cleanup(func() { c.Close() }) - cl := &client{t: t, c: c, r: bufio.NewReader(c), ver: ver, ch: make(chan *packet, 256)} + cl := &client{t: t, c: c, r: bufio.NewReader(c), ver: ver, ch: make(chan *packet, 256), + oldLogin: e.srv.S.OldLogin} go func() { defer close(cl.ch) for { @@ -235,10 +246,19 @@ func (cl *client) login(user, pw string) (code string, ok bool) { cl.expect(svcVerify, nil) cl.send(svcAuth, 0, kv(1, user)) a := cl.expect(svcAuth, nil) - if a.get(13) != "0" || len(a.get(94)) < 16 { + want := "0" + if cl.ver >= 10 && !cl.oldLogin { + want = "1" + } + if a.get(13) != want || len(a.get(94)) < 16 { cl.t.Fatalf("auth: %v", a.pairs) } - r6, r96 := answers(user, pw, a.get(94)) + var r6, r96 string + if want == "1" { + r6, r96 = answers1(cl.t, pw, a.get(94)) + } else { + r6, r96 = answers(user, pw, a.get(94)) + } cl.send(svcAuthResp, 0, kv(0, user), kv(6, r6), kv(96, r96), kv(1, user)) for { select { @@ -250,6 +270,7 @@ func (cl *client) login(user, pw string) (code string, ok bool) { return p.get(66), false } if p.service == svcList { + cl.list = p return "", true } case <-time.After(3 * time.Second):