From cce9bfbf9f114b6d694ea4e3f5f7a900fd3e0b04 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 10 Oct 2026 18:09:22 +0000 Subject: [PATCH] IND$FILE file transfer for 3270 callers A 3270 file area's screen has a command line, the cursor on it, for an emulator's file transfer to type IND$FILE into: PUT uploads into the area (through the file store's checks, size limit and allowance), GET downloads a file of it, in DFT mode with go3270-sec's new IND$FILE support; the transfer ends with IND$FILE's own message (TRANS03, or TRANS13-18 for what went wrong). An emulator without DFT is told so. go3270-sec moves to the commit with IND$FILE. A test runs s3270 against the BBS: a binary upload, its download, and a GET of a file that is not there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr --- README.md | 43 ++++++++++++- go.mod | 2 +- go.sum | 4 +- internal/server/server_test.go | 59 +++++++++++++++++- internal/ui3270/files.go | 71 +++++++++++++++++----- internal/ui3270/indfile.go | 108 +++++++++++++++++++++++++++++++++ 6 files changed, 266 insertions(+), 21 deletions(-) create mode 100644 internal/ui3270/indfile.go diff --git a/README.md b/README.md index f3bbb8f..582507a 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,9 @@ What there is: with its rate), the user list, system info - your profile: where you call from, your password, ANSI and UTF-8 - file areas: files listed with their size, uploader, downloads and - SHA-256, uploaded and downloaded by FTP (FTPS), deleted by their - uploader or the sysop + SHA-256, moved by XMODEM, YMODEM, ZMODEM or Kermit on the text BBS, by + IND$FILE on a 3270, or by FTP (FTPS); deleted by their uploader or the + sysop - new users sign themselves up (or not: `new_users = no`) - SQLite storage, one file @@ -303,6 +304,39 @@ Open the FTP port and the passive range in the firewall. An FTP session is not a node: it does not show in who's online or count against `max_nodes`; `[ftp] max_clients` and `max_per_ip` limit it. +## File transfers on the BBS + +Besides FTP, files move over the caller's own line. + +**Text callers** (telnet, modem, rlogin) pick a file's details and press +*D* to download, or *U* in an area to upload, then a protocol: + +| key | protocol | | +|---|---|---| +| X | XMODEM | CRC, or checksum if the terminal program asks; 128-byte blocks | +| 1 | XMODEM-1K | 1K blocks | +| Y | YMODEM | batch: names and exact sizes | +| Z | ZMODEM | batch, CRC-32, error recovery; most terminal programs start it by themselves | +| K | Kermit | for 7-bit lines and anything else that fails | + +XMODEM sends no name, so the BBS asks for one. Names a terminal program +sends lose any path (`C:\DOS\GAME.ZIP` becomes `GAME.ZIP`) and characters +the file areas do not take; an upload goes through the same checks, size +limit and daily allowance as one by FTP, and is kept only if it all came. +X/Y/ZMODEM need an 8-bit clean line: a modem, rlogin, or telnet in binary +mode (the BBS asks for it; a client that refuses is warned). + +**3270 callers** use their emulator's file transfer (IND$FILE): in a file +area (8 on the main menu, then S by an area), with the cursor on the +command line, start a transfer: *send* to upload into the area, *receive* +to download a file of it, the host file name being the file's name. The +emulator types `IND$FILE PUT name` or `IND$FILE GET name` and the BBS plays +the host's part in DFT mode (structured fields), as TSO's IND$FILE does. +Binary mode keeps a file as it is; ASCII with CR/LF stores text with CR LF +line ends and gives it back as the PC wants it. TSO or VM/CMS host type +both work. The emulator must do DFT (x3270, c3270, wc3270 and PCOMM do; +*Your terminal* shows reply 95, Distributed Data Management). + ### Sixel graphics Telnet callers whose terminal has sixel graphics (xterm `-ti vt340`, @@ -380,6 +414,7 @@ and Greg Price's [3270 Graphics](https://www.prycroft6.com.au/misc/3270grfx.html | `internal/sixel` | the sixel encoder | | `internal/files` | file areas' storage: names, uploads, limits | | `internal/ftp` | the FTP server: the file areas as a file system | +| `internal/xfer` | XMODEM, YMODEM, ZMODEM and Kermit, both ways | | `internal/logo` | the logo: a PNG, or the name in a pixel font | | `internal/bbs` | the BBS itself: nodes, logins, new users, posting, cleaning text | | `internal/store` | the SQLite database | @@ -394,6 +429,10 @@ modemmenu header, one whose header is not trusted, a login and a post, a new user, rlogin with a right and a wrong password and trusted, throttling by caller ID, all lines busy, a telnet ANSI client, a TN3270 client taken through go3270's negotiation to the logon screen and out with PF3, +the transfer protocols against lrzsz (`sx`/`rx`, `sb`/`rb`, `sz`/`rz`) and +C-Kermit both ways, and ZMODEM over a line that damages bytes; uploads and +downloads through the BBS by ZMODEM, Kermit, YMODEM and XMODEM-1K; +IND$FILE with a real s3270 (upload, download, a file that is not there); the file store (names, limits, the daily allowance, sysop-only areas), the FTP server with a real FTP client (uploads, downloads and their count, every refused operation, path traversal, wrong logins, anonymous diff --git a/go.mod b/go.mod index 0dacda8..7ae7f5c 100644 --- a/go.mod +++ b/go.mod @@ -34,4 +34,4 @@ require ( modernc.org/token v1.1.0 // indirect ) -replace github.com/racingmars/go3270 => github.com/Neo-Desktop/go3270-sec v0.9.14-0.20261010172004-e50b7be8095a +replace github.com/racingmars/go3270 => github.com/Neo-Desktop/go3270-sec v0.9.14-0.20261010180804-750b72ee6b2f diff --git a/go.sum b/go.sum index 67682a3..5c684e9 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/Neo-Desktop/go3270-sec v0.9.14-0.20261010172004-e50b7be8095a h1:isnFUPxBt4xLDpigvInRsaKPAW1dc3+OgjGKaunxCBw= -github.com/Neo-Desktop/go3270-sec v0.9.14-0.20261010172004-e50b7be8095a/go.mod h1:JCzKbsCGdevsd+2iLMRw3Cd+Wk7vmBeGlnfHmeJEcsU= +github.com/Neo-Desktop/go3270-sec v0.9.14-0.20261010180804-750b72ee6b2f h1:Bt6G332gAcwKuDK+WBYMx6MgAsAbjEwBPb0fSxwQch8= +github.com/Neo-Desktop/go3270-sec v0.9.14-0.20261010180804-750b72ee6b2f/go.mod h1:JCzKbsCGdevsd+2iLMRw3Cd+Wk7vmBeGlnfHmeJEcsU= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 622d054..4054d02 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -7,6 +7,7 @@ import ( "log" "net" "os" + "os/exec" "path/filepath" "strings" "testing" @@ -73,7 +74,9 @@ func startGraphics(t *testing.T, g ui3270.Graphics, tweak func(*bbs.Settings), l t.Fatal(err) } for _, l := range ls { - l.Address = filepath.Join(dir, l.Address) + if !strings.Contains(l.Address, ":") { // host:port stays TCP + l.Address = filepath.Join(dir, l.Address) + } l.SocketMode = 0600 if l.HeaderTimeout == 0 { l.HeaderTimeout = 2 * time.Second @@ -102,6 +105,10 @@ func startGraphics(t *testing.T, g ui3270.Graphics, tweak func(*bbs.Settings), l if _, err := os.Stat(l.Address); err == nil { break } + if c, err := net.Dial("tcp", l.Address); err == nil { + c.Close() + break + } if i > 200 { t.Fatalf("%s never listened", l.Address) } @@ -736,3 +743,53 @@ func TestFileTransfers(t *testing.T) { t.Errorf("record %+v", f) } } + +// IND$FILE with a real 3270 emulator: s3270 uploads a binary file into +// the file area, downloads it again, and asks for one that is not there +func TestIndFile(t *testing.T) { + if _, err := exec.LookPath("s3270"); err != nil { + t.Skip("no s3270") + } + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + addr := ln.Addr().String() + ln.Close() + e := start(t, nil, &Listener{Name: "telnet", Kind: "telnet", Address: addr, TN3270: true}) + dir := t.TempDir() + up, down := filepath.Join(dir, "up.bin"), filepath.Join(dir, "down.bin") + want := make([]byte, 40000) + for i := range want { + want[i] = byte(i * 7) + } + os.WriteFile(up, want, 0600) + script := strings.Join([]string{ + "Connect(" + addr + ")", "Wait(10,InputField)", + `String("alice")`, "Tab()", `String("alicepass1")`, "Enter()", "Wait(5,InputField)", + `String("8")`, "Enter()", "Wait(5,InputField)", + `String("s")`, "Enter()", "Wait(5,InputField)", + // s3270 4.1's Transfer returns before the transfer ends: wait + "Transfer(Direction=send,Host=tso,LocalFile=" + up + ",HostFile=up.bin,Mode=binary)", "Wait(3,Seconds)", + "Transfer(Direction=receive,Host=tso,LocalFile=" + down + ",HostFile=up.bin,Mode=binary,Exist=replace)", "Wait(3,Seconds)", + "Transfer(Direction=receive,Host=tso,LocalFile=" + filepath.Join(dir, "x") + ",HostFile=nosuch,Mode=binary,Exist=replace)", "Wait(3,Seconds)", + "Ascii()", "Quit()", ""}, "\n") + cmd := exec.Command("s3270", "-model", "3279-2") + cmd.Stdin = strings.NewReader(script) + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("s3270: %v\n%s", err, out) + } + a, _ := e.b.Store.FileAreaByName("Uploads") + f, err := e.b.Store.FileByName(a.ID, "up.bin") + if err != nil || f.Size != int64(len(want)) || f.Downloads != 1 { + t.Fatalf("stored: %+v, %v\n%s", f, err, out) + } + got, _ := os.ReadFile(down) + if !bytes.Equal(got, want) { + t.Errorf("downloaded %d bytes", len(got)) + } + if !strings.Contains(string(out), "File not found") { + t.Errorf("no not-found message:\n%s", out) + } +} diff --git a/internal/ui3270/files.go b/internal/ui3270/files.go index df28e3b..8ac5735 100644 --- a/internal/ui3270/files.go +++ b/internal/ui3270/files.go @@ -2,6 +2,7 @@ package ui3270 import ( "fmt" + "strings" "github.com/racingmars/go3270" @@ -45,36 +46,76 @@ func (u *ui) fileAreas() error { } } +// fileArea is an area's files, and the command line an emulator's file +// transfer types IND$FILE into. func (u *ui) fileArea(a *store.FileArea) error { top := 0 + msg := "" + if u.b.FTPAddress != "" && files.CanUpload(u.user, a) { + msg = "Upload by FTP into /" + a.Name + "/, or with your emulator's file transfer" + } for { u.n.Doing("Files in " + a.Name) list, err := u.b.Store.Files(a.ID) if err != nil { return err } - tb := &table{title: a.Name, top: top, - heading: fmt.Sprintf("%-28s %9s %-10s %s", "Name", "Size", "Date", "Description"), - keys: "Type S by a file, Enter PF3=Back PF7=Up PF8=Down"} - for _, f := range list { - tb.lines = append(tb.lines, fmt.Sprintf("%s %9s %s %s", bbs.Fit(f.Name, 28), - files.Size(f.Size), f.Created.Format("2006-01-02"), f.Description)) + per := u.rows - 8 + scr := u.frame(a.Name, "S+Enter=Details PF3=Back PF7=Up PF8=Down", msg) + msg = "" + // the command line first: the cursor goes to it, where an + // emulator types IND$FILE + scr = append(scr, u.text(u.rows-3, 0, "Command ===>", go3270.Turquoise, false)) + scr = append(scr, u.input(u.rows-3, 13, u.cols-15, "cmd", false)...) + scr = append(scr, u.text(u.rows-4, 0, + "File transfer (IND$FILE): GET name downloads, PUT name uploads here.", go3270.Blue, false)) + scr = append(scr, u.text(2, 0, fmt.Sprintf(" %-28s %9s %-10s %s", "Name", "Size", "Date", "Description"), + go3270.Turquoise, false)) + for i := 0; i < per && top+i < len(list); i++ { + f := list[top+i] + scr = append(scr, go3270.Field{Row: 3 + i, Col: 0, Write: true, Name: fmt.Sprintf("s%d", i), + Color: go3270.Green, Highlighting: go3270.Underscore}) + scr = append(scr, u.text(3+i, 2, fmt.Sprintf("%s %9s %s %s", bbs.Fit(f.Name, 28), + files.Size(f.Size), f.Created.Format("2006-01-02"), f.Description), go3270.Green, false)) } - msg := "" - if u.b.FTPAddress != "" && files.CanUpload(u.user, a) { - msg = "Upload by FTP into /" + a.Name + "/" + if len(list) == 0 { + scr = append(scr, u.text(4, 2, "No files here yet.", go3270.White, false)) } - i, aid, err := u.pickFrom(tb, msg) - top = tb.top + resp, v, err := u.show(scr, nil) if err != nil { return err } - if aid == go3270.AIDPF3 { + switch resp.AID { + case go3270.AIDPF3: return nil + case go3270.AIDPF7: + top = max(0, top-per) + continue + case go3270.AIDPF8: + if top+per < len(list) { + top += per + } + continue + case go3270.AIDEnter: + default: + continue } - if i >= 0 { - if err := u.fileDetail(a, list[i]); err != nil { - return err + if cmd := strings.TrimSpace(v["cmd"]); cmd != "" { + if ind, ok := go3270.ParseIndFile(cmd); ok { + if msg, err = u.indFile(a, ind); err != nil { + return err + } + } else { + msg = "Commands here are IND$FILE GET and PUT, from your emulator's file transfer." + } + continue + } + for i := 0; i < per && top+i < len(list); i++ { + if v[fmt.Sprintf("s%d", i)] != "" { + if err := u.fileDetail(a, list[top+i]); err != nil { + return err + } + break } } } diff --git a/internal/ui3270/indfile.go b/internal/ui3270/indfile.go new file mode 100644 index 0000000..a2c77df --- /dev/null +++ b/internal/ui3270/indfile.go @@ -0,0 +1,108 @@ +package ui3270 + +import ( + "errors" + "strings" + "time" + + "github.com/racingmars/go3270" + + "github.com/Neo-Desktop/bbs-sec/internal/files" + "github.com/Neo-Desktop/bbs-sec/internal/store" +) + +// dftTimeout is how long an emulator may take to answer in a transfer. +const dftTimeout = 60 * time.Second + +// IND$FILE's messages +const ( + transOK = "TRANS03 File transfer complete" + transNoFile = "TRANS14 File not found: file transfer canceled" + transBadName = "TRANS17 Incorrect file name: file transfer canceled" + transBadParm = "TRANS18 Incorrect parameter: file transfer canceled" + transWrite = "TRANS13 Error writing file to host: file transfer canceled" + transRead = "TRANS16 Error reading file from host: file transfer canceled" + transDenied = "TRANS15 Not permitted: file transfer canceled" +) + +// indFile runs an IND$FILE transfer the emulator started: GET sends a file +// of the area, PUT receives one into it. The message is for the screen +// after. +func (u *ui) indFile(a *store.FileArea, ind *go3270.IndFile) (string, error) { + conn := u.g.Conn // the transfer's records are not screens: no guard + if go3270.DFTBufferSize(u.dev) == 0 { + // the emulator waits for the transfer to start, and gives up + return "Your emulator does not do DFT file transfers.", nil + } + name := strings.Trim(ind.Name, `'"`) + if i := strings.LastIndexAny(name, `/\:`); i >= 0 { + name = name[i+1:] + } + end := func(msg string) (string, error) { + if err := go3270.DFTMessage(conn, dftTimeout, msg); err != nil { + u.b.Log.Printf("node %d: IND$FILE: %v", u.n.Num, err) + } + if msg == transOK { + return "Transfer complete.", nil + } + return msg[8:], nil + } + if ind.Get { + f, err := u.b.Store.FileByName(a.ID, name) + if err != nil { + return end(transNoFile) + } + r, err := u.b.Files.OpenFile(f) + if err != nil { + return end(transRead) + } + defer r.Close() + u.n.Doing("Downloading " + f.Name) + start := time.Now() + n, err := go3270.SendFileDFT(conn, u.dev, r, dftTimeout) + if err != nil { + u.b.Log.Printf("node %d: %s: IND$FILE GET %q failed: %v", u.n.Num, u.user.Name, f.Name, err) + if errors.Is(err, go3270.ErrDFTAborted) { + return "The transfer was cancelled.", nil + } + return end(transRead) + } + u.b.Store.CountDownload(f.ID) + u.b.Log.Printf("node %d: %s downloaded %q by IND$FILE (%d bytes, %v)", u.n.Num, u.user.Name, + f.Name, n, time.Since(start).Round(time.Second)) + return end(transOK) + } + + if ind.Append { + return end(transBadParm) + } + if files.ValidName(name) != "" { + return end(transBadName) + } + up, err := u.b.Files.Upload(u.user, a, name, "") + switch { + case errors.Is(err, files.ErrDenied), errors.Is(err, files.ErrQuota): + return end(transDenied) + case errors.Is(err, files.ErrExists): + return end("TRANS17 A file of that name is there already: file transfer canceled") + case err != nil: + return end(transWrite) + } + u.n.Doing("Uploading to " + a.Name) + n, err := go3270.ReceiveFileDFT(conn, u.dev, up, u.b.Files.MaxSize, dftTimeout) + if err != nil { + up.Abort() + u.b.Log.Printf("node %d: %s: IND$FILE PUT %q failed after %d bytes: %v", u.n.Num, u.user.Name, name, n, err) + if errors.Is(err, go3270.ErrDFTAborted) { + return "The transfer was cancelled.", nil + } + return end(transWrite) + } + f, err := up.Commit() + if err != nil { + return end(transWrite) + } + u.b.Log.Printf("node %d: %s uploaded %q to %s by IND$FILE (%d bytes, sha256 %s)", u.n.Num, + u.user.Name, f.Name, a.Name, f.Size, f.SHA256) + return end(transOK) +}