From c4b96658a0883d94186ce2b64305dcafaf567922 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 4 Jan 2025 21:01:08 +0800 Subject: Use Open_file_at instead of Open --- config.go | 17 +++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- incoming.go | 8 ++++---- main.go | 5 +++++ mta_recv.go | 2 +- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index f965c24..1ec7207 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "os" "sync" + "go.lindenii.runxiyu.org/lindenii-common/misc" "go.lindenii.runxiyu.org/lindenii-common/scfg" ) @@ -33,3 +34,19 @@ func load_config(path string) error { } return nil } + +var directories struct { + inbox misc.Dir_t +} +var directories_mutex sync.RWMutex + +func prepare_dirs() error { + directories_mutex.Lock() + defer directories_mutex.Unlock() + var err error + directories.inbox, err = misc.Open_directory_readonly(config.Inbox_path) + if err != nil { + return err + } + return nil +} diff --git a/go.mod b/go.mod index cbbf3f2..ad6be5f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module go.lindenii.runxiyu.org/maild go 1.23.4 -require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20241229193139-603ef6227aca +require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9 diff --git a/go.sum b/go.sum index dbcd25c..bccd500 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -go.lindenii.runxiyu.org/lindenii-common v0.0.0-20241229193139-603ef6227aca h1:iyxIKtyBNe5RlIMVu+whX2r2JGK9JCo6RotHrHstb0U= -go.lindenii.runxiyu.org/lindenii-common v0.0.0-20241229193139-603ef6227aca/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= +go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9 h1:9H+rOwTeOYBoi11NSXn4IN4j0/3zo5nT8HA2Ktrdur0= +go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= diff --git a/incoming.go b/incoming.go index e264262..35aa006 100644 --- a/incoming.go +++ b/incoming.go @@ -2,10 +2,10 @@ package main import ( "os" - "path" "time" "go.lindenii.runxiyu.org/lindenii-common/clog" + "go.lindenii.runxiyu.org/lindenii-common/misc" ) func deliver_incoming(envelope_from string, envelope_recipients []string, data []byte) error { @@ -16,14 +16,14 @@ func deliver_incoming(envelope_from string, envelope_recipients []string, data [ "data", string(data), ) t := time.Now() - fd, err := os.OpenFile(path.Join(config.Inbox_path, envelope_from+" "+t.Format(time.RFC3339Nano)+".eml"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o660) // TODO: vulnerability + fd, err := misc.Open_file_at(directories.inbox, envelope_from+" "+t.Format(time.RFC3339Nano)+".eml", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) if err != nil { // TODO: handle fs.ErrExist - return err_deliver_write + return misc.Wrap_one_error(err_deliver_write, err) } _, err = fd.Write(data) if err != nil { - return err_deliver_write + return misc.Wrap_one_error(err_deliver_write, err) } return nil } diff --git a/main.go b/main.go index 94f195e..2d6b706 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,11 @@ func main() { panic(err) } + err = prepare_dirs() + if err != nil { + panic(err) + } + listener, err := net.Listen("tcp", ":25") if err != nil { panic(err) diff --git a/mta_recv.go b/mta_recv.go index 1d2c4f3..fe6139f 100644 --- a/mta_recv.go +++ b/mta_recv.go @@ -133,7 +133,7 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write if err == nil { _, _ = writer.WriteString("250 2.0.0 Ok: Accepted\r\n") } else { - _, _ = writer.WriteString("500 2.0.0 Funderscore\r\n") + _, _ = writer.WriteString("500 2.0.0 Error: "+err.Error()+"\r\n") } _ = writer.Flush() server_state = server_state_helo -- cgit v1.2.3