1// Package webapisrv implements the server-side of the webapi.
2package webapisrv
3
4// In a separate package from webapi, so webapi.Client can be used and imported
5// without including all mox internals. Documentation for the functions is in
6// ../webapi/client.go.
7
8import (
9 "bytes"
10 "context"
11 cryptorand "crypto/rand"
12 "encoding/base64"
13 "encoding/json"
14 "errors"
15 "fmt"
16 htmltemplate "html/template"
17 "io"
18 "log/slog"
19 "mime"
20 "mime/multipart"
21 "net/http"
22 "net/textproto"
23 "os"
24 "reflect"
25 "runtime/debug"
26 "slices"
27 "strings"
28 "time"
29
30 "github.com/prometheus/client_golang/prometheus"
31 "github.com/prometheus/client_golang/prometheus/promauto"
32
33 "github.com/mjl-/bstore"
34
35 "github.com/mjl-/mox/dkim"
36 "github.com/mjl-/mox/dns"
37 "github.com/mjl-/mox/message"
38 "github.com/mjl-/mox/metrics"
39 "github.com/mjl-/mox/mlog"
40 "github.com/mjl-/mox/mox-"
41 "github.com/mjl-/mox/moxio"
42 "github.com/mjl-/mox/moxvar"
43 "github.com/mjl-/mox/queue"
44 "github.com/mjl-/mox/smtp"
45 "github.com/mjl-/mox/store"
46 "github.com/mjl-/mox/webapi"
47 "github.com/mjl-/mox/webauth"
48 "github.com/mjl-/mox/webops"
49)
50
51var pkglog = mlog.New("webapi", nil)
52
53var (
54 // Similar between ../webmail/webmail.go:/metricSubmission and ../smtpserver/server.go:/metricSubmission and ../webapisrv/server.go:/metricSubmission
55 metricSubmission = promauto.NewCounterVec(
56 prometheus.CounterOpts{
57 Name: "mox_webapi_submission_total",
58 Help: "Webapi message submission results, known values (those ending with error are server errors): ok, badfrom, messagelimiterror, recipientlimiterror, queueerror, storesenterror, domaindisabled.",
59 },
60 []string{
61 "result",
62 },
63 )
64 metricServerErrors = promauto.NewCounterVec(
65 prometheus.CounterOpts{
66 Name: "mox_webapi_errors_total",
67 Help: "Webapi server errors, known values: dkimsign, submit.",
68 },
69 []string{
70 "error",
71 },
72 )
73 metricResults = promauto.NewCounterVec(
74 prometheus.CounterOpts{
75 Name: "mox_webapi_results_total",
76 Help: "HTTP webapi results by method and result.",
77 },
78 []string{"method", "result"}, // result: "badauth", "ok", or error code
79 )
80 metricDuration = promauto.NewHistogramVec(
81 prometheus.HistogramOpts{
82 Name: "mox_webapi_duration_seconds",
83 Help: "HTTP webhook call duration.",
84 Buckets: []float64{0.01, 0.05, 0.1, 0.5, 1, 5, 10, 20, 30},
85 },
86 []string{"method"},
87 )
88)
89
90// We pass the request to the handler so the TLS info can be used for
91// the Received header in submitted messages. Most API calls need just the
92// account name.
93type ctxKey string
94
95var requestInfoCtxKey ctxKey = "requestInfo"
96
97type requestInfo struct {
98 Log mlog.Log
99 LoginAddress string
100 Account *store.Account
101 Response http.ResponseWriter // For setting headers for non-JSON responses.
102 Request *http.Request // For Proto and TLS connection state during message submit.
103}
104
105// todo: show a curl invocation on the method pages
106
107var docsMethodTemplate = htmltemplate.Must(htmltemplate.New("method").Parse(`<!doctype html>
108 <head>
109 <meta charset="utf-8" />
110 <meta name="robots" content="noindex,nofollow" />
111 <title>Method {{ .Method }} - WebAPI - Mox</title>
112 <style>
113body, html { padding: 1em; font-size: 16px; }
114* { font-size: inherit; font-family: ubuntu, lato, sans-serif; margin: 0; padding: 0; box-sizing: border-box; }
115h1, h2, h3, h4 { margin-bottom: 1ex; }
116h1 { font-size: 1.2rem; }
117h2 { font-size: 1.1rem; }
118h3, h4 { font-size: 1rem; }
119ul { padding-left: 1rem; }
120p { margin-bottom: 1em; max-width: 50em; }
121[title] { text-decoration: underline; text-decoration-style: dotted; }
122fieldset { border: 0; }
123textarea { width: 100%; max-width: 50em; }
124 </style>
125 </head>
126 <body>
127 <h1><a href="../">WebAPI</a> - Method {{ .Method }}</h1>
128 <form id="webapicall" method="POST">
129 <fieldset id="webapifieldset">
130 <h2>Request JSON</h2>
131 <div><textarea id="webapirequest" name="request" required rows="20">{{ .Request }}</textarea></div>
132 <br/>
133 <div>
134 <button type="reset">Reset</button>
135 <button type="submit">Call</button>
136 </div>
137 <br/>
138{{ if .ReturnsBytes }}
139 <p>Method has a non-JSON response.</p>
140{{ else }}
141 <h2>Response JSON</h2>
142 <div><textarea id="webapiresponse" rows="20">{{ .Response }}</textarea></div>
143{{ end }}
144 </fieldset>
145 </form>
146 <script>
147window.addEventListener('load', () => {
148 window.webapicall.addEventListener('submit', async (e) => {
149 const stop = () => {
150 e.stopPropagation()
151 e.preventDefault()
152 }
153
154 let req
155 try {
156 req = JSON.parse(window.webapirequest.value)
157 } catch (err) {
158 window.alert('Error parsing request: ' + err.message)
159 stop()
160 return
161 }
162 if (!req) {
163 window.alert('Empty request')
164 stop()
165 return
166 }
167
168 if ({{ .ReturnsBytes }}) {
169 // Just POST to this URL.
170 return
171 }
172
173 stop()
174 // Do call ourselves, get response and put it in the response textarea.
175 window.webapifieldset.disabled = true
176 let data = new window.FormData()
177 data.append("request", window.webapirequest.value)
178 try {
179 const response = await fetch("{{ .Method }}", {body: data, method: "POST"})
180 const text = await response.text()
181 try {
182 window.webapiresponse.value = JSON.stringify(JSON.parse(text), undefined, '\t')
183 } catch (err) {
184 window.webapiresponse.value = text
185 }
186 } catch (err) {
187 window.alert('Error: ' + err.message)
188 } finally {
189 window.webapifieldset.disabled = false
190 }
191 })
192})
193 </script>
194 </body>
195</html>
196`))
197
198var docsIndex []byte
199
200func init() {
201 var methods []string
202 mt := reflect.TypeFor[webapi.Methods]()
203 n := mt.NumMethod()
204 for i := range n {
205 methods = append(methods, mt.Method(i).Name)
206 }
207 docsIndexTmpl := htmltemplate.Must(htmltemplate.New("index").Parse(`<!doctype html>
208<html>
209 <head>
210 <meta charset="utf-8" />
211 <meta name="robots" content="noindex,nofollow" />
212 <title>Webapi - Mox</title>
213 <style>
214body, html { padding: 1em; font-size: 16px; }
215* { font-size: inherit; font-family: ubuntu, lato, sans-serif; margin: 0; padding: 0; box-sizing: border-box; }
216h1, h2, h3, h4 { margin-bottom: 1ex; }
217h1 { font-size: 1.2rem; }
218h2 { font-size: 1.1rem; }
219h3, h4 { font-size: 1rem; }
220ul { padding-left: 1rem; }
221p { margin-bottom: 1em; max-width: 50em; }
222[title] { text-decoration: underline; text-decoration-style: dotted; }
223fieldset { border: 0; }
224 </style>
225 </head>
226 <body>
227 <h1>Webapi and webhooks</h1>
228 <p>The mox webapi is a simple HTTP/JSON-based API for sending messages and processing incoming messages.</p>
229 <p>Configure webhooks in mox to receive notifications about outgoing delivery event, and/or incoming deliveries of messages.</p>
230 <p>Documentation and examples:</p>
231 <p><a href="{{ .WebapiDocsURL }}">{{ .WebapiDocsURL }}</a></p>
232 <h2>Methods</h2>
233 <p>The methods below are available in this version of mox. Follow a link for an example request/response JSON, and a button to make an API call.</p>
234 <ul>
235{{ range $i, $method := .Methods }}
236 <li><a href="{{ $method }}">{{ $method }}</a></li>
237{{ end }}
238 </ul>
239 </body>
240</html>
241`))
242 webapiDocsURL := "https://pkg.go.dev/github.com/mjl-/mox@" + moxvar.VersionBare + "/webapi/"
243 webhookDocsURL := "https://pkg.go.dev/github.com/mjl-/mox@" + moxvar.VersionBare + "/webhook/"
244 indexArgs := struct {
245 WebapiDocsURL string
246 WebhookDocsURL string
247 Methods []string
248 }{webapiDocsURL, webhookDocsURL, methods}
249 var b bytes.Buffer
250 err := docsIndexTmpl.Execute(&b, indexArgs)
251 if err != nil {
252 panic("executing api docs index template: " + err.Error())
253 }
254 docsIndex = b.Bytes()
255
256 mox.NewWebapiHandler = func(maxMsgSize int64, basePath string, isForwarded bool) http.Handler {
257 return NewServer(maxMsgSize, basePath, isForwarded)
258 }
259}
260
261// NewServer returns a new http.Handler for a webapi server.
262func NewServer(maxMsgSize int64, path string, isForwarded bool) http.Handler {
263 return server{maxMsgSize, path, isForwarded}
264}
265
266// server implements the webapi methods.
267type server struct {
268 maxMsgSize int64 // Of outgoing messages.
269 path string // Path webapi is configured under, typically /webapi/, with methods at /webapi/v0/<method>.
270 isForwarded bool // Whether incoming requests are reverse-proxied. Used for getting remote IPs for rate limiting.
271}
272
273var _ webapi.Methods = server{}
274
275// ServeHTTP implements http.Handler.
276func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
277 log := pkglog.WithContext(r.Context()) // Take cid from webserver.
278
279 // Send requests to /webapi/ to /webapi/v0/.
280 if r.URL.Path == "/" {
281 if r.Method != "GET" {
282 http.Error(w, "405 - method not allow", http.StatusMethodNotAllowed)
283 return
284 }
285 http.Redirect(w, r, s.path+"v0/", http.StatusSeeOther)
286 return
287 }
288 // Serve short introduction and list to methods at /webapi/v0/.
289 if r.URL.Path == "/v0/" {
290 w.Header().Set("Content-Type", "text/html; charset=utf-8")
291 w.Write(docsIndex)
292 return
293 }
294
295 // Anything else must be a method endpoint.
296 if !strings.HasPrefix(r.URL.Path, "/v0/") {
297 http.NotFound(w, r)
298 return
299 }
300 fn := r.URL.Path[len("/v0/"):]
301 log = log.With(slog.String("method", fn))
302 rfn := reflect.ValueOf(s).MethodByName(fn)
303 var zero reflect.Value
304 if rfn == zero || rfn.Type().NumIn() != 2 || rfn.Type().NumOut() != 2 {
305 log.Debug("unknown webapi method")
306 http.NotFound(w, r)
307 return
308 }
309
310 // GET on method returns an example request JSON, a button to call the method,
311 // which either fills a textarea with the response (in case of JSON) or posts to
312 // the URL letting the browser handle the response (e.g. raw message or part).
313 if r.Method == "GET" {
314 formatJSON := func(v any) (string, error) {
315 var b bytes.Buffer
316 enc := json.NewEncoder(&b)
317 enc.SetIndent("", "\t")
318 enc.SetEscapeHTML(false)
319 err := enc.Encode(v)
320 return string(b.String()), err
321 }
322
323 req, err := formatJSON(mox.FillExample(nil, reflect.New(rfn.Type().In(1))).Interface())
324 if err != nil {
325 log.Errorx("formatting request as json", err)
326 http.Error(w, "500 - internal server error - marshal request: "+err.Error(), http.StatusInternalServerError)
327 return
328 }
329 // todo: could check for io.ReadCloser, but we don't return other interfaces than that one.
330 returnsBytes := rfn.Type().Out(0).Kind() == reflect.Interface
331 var resp string
332 if !returnsBytes {
333 resp, err = formatJSON(mox.FillExample(nil, reflect.New(rfn.Type().Out(0))).Interface())
334 if err != nil {
335 log.Errorx("formatting response as json", err)
336 http.Error(w, "500 - internal server error - marshal response: "+err.Error(), http.StatusInternalServerError)
337 return
338 }
339 }
340 args := struct {
341 Method string
342 Request string
343 Response string
344 ReturnsBytes bool
345 }{fn, req, resp, returnsBytes}
346 w.Header().Set("Content-Type", "text/html; charset=utf-8")
347 err = docsMethodTemplate.Execute(w, args)
348 log.Check(err, "executing webapi method template")
349 return
350 } else if r.Method != "POST" {
351 http.Error(w, "405 - method not allowed - use get or post", http.StatusMethodNotAllowed)
352 return
353 }
354
355 // Account is available during call, but we close it before we start writing a
356 // response, to prevent slow readers from holding a reference for a long time.
357 var acc *store.Account
358 closeAccount := func() {
359 if acc != nil {
360 err := acc.Close()
361 log.Check(err, "closing account")
362 acc = nil
363 }
364 }
365 defer closeAccount()
366
367 email, password, aok := r.BasicAuth()
368 if !aok {
369 metricResults.WithLabelValues(fn, "badauth").Inc()
370 log.Debug("missing http basic authentication credentials")
371 w.Header().Set("WWW-Authenticate", "Basic realm=webapi")
372 http.Error(w, "401 - unauthorized - use http basic auth with email address as username", http.StatusUnauthorized)
373 return
374 }
375 log = log.With(slog.String("username", email))
376
377 t0 := time.Now()
378
379 // If remote IP/network resulted in too many authentication failures, refuse to serve.
380 remoteIP := webauth.RemoteIP(log, s.isForwarded, r)
381 if remoteIP == nil {
382 metricResults.WithLabelValues(fn, "internal").Inc()
383 log.Debug("cannot find remote ip for rate limiter")
384 http.Error(w, "500 - internal server error - cannot find remote ip", http.StatusInternalServerError)
385 return
386 }
387 if !mox.LimiterFailedAuth.CanAdd(remoteIP, t0, 1) {
388 metrics.AuthenticationRatelimitedInc("webapi")
389 log.Debug("refusing connection due to many auth failures", slog.Any("remoteip", remoteIP))
390 http.Error(w, "429 - too many auth attempts", http.StatusTooManyRequests)
391 return
392 }
393
394 writeError := func(err webapi.Error) {
395 closeAccount()
396 metricResults.WithLabelValues(fn, err.Code).Inc()
397
398 if err.Code == "server" {
399 log.Errorx("webapi call result", err, slog.String("resultcode", err.Code))
400 } else {
401 log.Infox("webapi call result", err, slog.String("resultcode", err.Code))
402 }
403
404 w.Header().Set("Content-Type", "application/json; charset=utf-8")
405 w.WriteHeader(http.StatusBadRequest)
406 enc := json.NewEncoder(w)
407 enc.SetEscapeHTML(false)
408 werr := enc.Encode(err)
409 log.Check(werr, "writing error response")
410 }
411
412 // Called for all successful JSON responses, not non-JSON responses.
413 writeResponse := func(resp any) {
414 closeAccount()
415 metricResults.WithLabelValues(fn, "ok").Inc()
416 log.Debug("webapi call result", slog.String("resultcode", "ok"))
417 w.Header().Set("Content-Type", "application/json; charset=utf-8")
418 enc := json.NewEncoder(w)
419 enc.SetEscapeHTML(false)
420 werr := enc.Encode(resp)
421 log.Check(werr, "writing error response")
422 }
423
424 la := loginAttempt(remoteIP.String(), r, "webapi", "httpbasic")
425 la.LoginAddress = email
426 defer func() {
427 store.LoginAttemptAdd(context.Background(), log, la)
428 metricDuration.WithLabelValues(fn).Observe(float64(time.Since(t0)) / float64(time.Second))
429 }()
430
431 var err error
432 acc, la.AccountName, err = store.OpenEmailAuth(log, email, password, true)
433 if err != nil {
434 mox.LimiterFailedAuth.Add(remoteIP, t0, 1)
435 if errors.Is(err, mox.ErrDomainNotFound) || errors.Is(err, mox.ErrAddressNotFound) || errors.Is(err, store.ErrUnknownCredentials) || errors.Is(err, store.ErrLoginDisabled) {
436 log.Debug("bad http basic authentication credentials")
437 metricResults.WithLabelValues(fn, "badauth").Inc()
438 la.Result = store.AuthBadCredentials
439 msg := "use http basic auth with email address as username"
440 if errors.Is(err, store.ErrLoginDisabled) {
441 la.Result = store.AuthLoginDisabled
442 msg = "login is disabled for this account"
443 }
444 w.Header().Set("WWW-Authenticate", "Basic realm=webapi")
445 http.Error(w, "401 - unauthorized - "+msg, http.StatusUnauthorized)
446 return
447 }
448 writeError(webapi.Error{Code: "server", Message: "error verifying credentials"})
449 return
450 }
451 la.AccountName = acc.Name
452 la.Result = store.AuthSuccess
453 mox.LimiterFailedAuth.Reset(remoteIP, t0)
454
455 ct := r.Header.Get("Content-Type")
456 ct, _, err = mime.ParseMediaType(ct)
457 if err != nil {
458 writeError(webapi.Error{Code: "protocol", Message: "unknown content-type " + r.Header.Get("Content-Type")})
459 return
460 }
461 if ct == "multipart/form-data" {
462 err = r.ParseMultipartForm(200 * 1024)
463 } else {
464 err = r.ParseForm()
465 }
466 if err != nil {
467 writeError(webapi.Error{Code: "protocol", Message: "parsing form: " + err.Error()})
468 return
469 }
470
471 reqstr := r.PostFormValue("request")
472 if reqstr == "" {
473 writeError(webapi.Error{Code: "protocol", Message: "missing/empty request"})
474 return
475 }
476
477 defer func() {
478 x := recover()
479 if x == nil {
480 return
481 }
482 if err, eok := x.(webapi.Error); eok {
483 writeError(err)
484 return
485 }
486 log.Error("unhandled panic in webapi call", slog.Any("x", x), slog.String("resultcode", "server"))
487 metrics.PanicInc(metrics.Webapi)
488 debug.PrintStack()
489 writeError(webapi.Error{Code: "server", Message: "unhandled error"})
490 }()
491 req := reflect.New(rfn.Type().In(1))
492 dec := json.NewDecoder(strings.NewReader(reqstr))
493 dec.DisallowUnknownFields()
494 if err := dec.Decode(req.Interface()); err != nil {
495 writeError(webapi.Error{Code: "protocol", Message: fmt.Sprintf("parsing request: %s", err)})
496 return
497 }
498
499 reqInfo := requestInfo{log, email, acc, w, r}
500 nctx := context.WithValue(r.Context(), requestInfoCtxKey, reqInfo)
501 resp := rfn.Call([]reflect.Value{reflect.ValueOf(nctx), req.Elem()})
502 if !resp[1].IsZero() {
503 var e webapi.Error
504 err := resp[1].Interface().(error)
505 if x, eok := err.(webapi.Error); eok {
506 e = x
507 } else {
508 e = webapi.Error{Code: "error", Message: err.Error()}
509 }
510 writeError(e)
511 return
512 }
513 rc, ok := resp[0].Interface().(io.ReadCloser)
514 if !ok {
515 rv, _ := mox.FillNil(resp[0])
516 writeResponse(rv.Interface())
517 return
518 }
519 closeAccount()
520 log.Debug("webapi call result", slog.String("resultcode", "ok"))
521 metricResults.WithLabelValues(fn, "ok").Inc()
522 defer func() {
523 err := rc.Close()
524 log.Check(err, "closing readcloser")
525 }()
526 _, err = io.Copy(w, rc)
527 log.Check(err, "writing response to client")
528}
529
530// loginAttempt initializes a store.LoginAttempt, for adding to the store after
531// filling in the results and other details.
532func loginAttempt(remoteIP string, r *http.Request, protocol, authMech string) store.LoginAttempt {
533 return store.LoginAttempt{
534 RemoteIP: remoteIP,
535 TLS: store.LoginAttemptTLS(r.TLS),
536 Protocol: protocol,
537 AuthMech: authMech,
538 UserAgent: r.UserAgent(),
539 Result: store.AuthError, // Replaced by caller.
540 }
541}
542
543func xcheckf(err error, format string, args ...any) {
544 if err != nil {
545 msg := fmt.Sprintf(format, args...)
546 panic(webapi.Error{Code: "server", Message: fmt.Sprintf("%s: %s", msg, err)})
547 }
548}
549
550func xcheckuserf(err error, format string, args ...any) {
551 if err != nil {
552 msg := fmt.Sprintf(format, args...)
553 panic(webapi.Error{Code: "user", Message: fmt.Sprintf("%s: %s", msg, err)})
554 }
555}
556
557func xdbwrite(ctx context.Context, acc *store.Account, fn func(tx *bstore.Tx)) {
558 err := acc.DB.Write(ctx, func(tx *bstore.Tx) error {
559 fn(tx)
560 return nil
561 })
562 xcheckf(err, "transaction")
563}
564
565func xdbread(ctx context.Context, acc *store.Account, fn func(tx *bstore.Tx)) {
566 err := acc.DB.Read(ctx, func(tx *bstore.Tx) error {
567 fn(tx)
568 return nil
569 })
570 xcheckf(err, "transaction")
571}
572
573func xcheckcontrol(s string) {
574 for _, c := range s {
575 if c < 0x20 {
576 xcheckuserf(errors.New("control characters not allowed"), "checking header values")
577 }
578 }
579}
580
581func xparseAddress(addr string) smtp.Address {
582 a, err := smtp.ParseAddress(addr)
583 if err != nil {
584 panic(webapi.Error{Code: "badAddress", Message: fmt.Sprintf("parsing address %q: %s", addr, err)})
585 }
586 return a
587}
588
589func xparseAddresses(l []webapi.NameAddress) ([]message.NameAddress, []smtp.Path) {
590 r := make([]message.NameAddress, len(l))
591 paths := make([]smtp.Path, len(l))
592 for i, a := range l {
593 xcheckcontrol(a.Name)
594 addr := xparseAddress(a.Address)
595 r[i] = message.NameAddress{DisplayName: a.Name, Address: addr}
596 paths[i] = addr.Path()
597 }
598 return r, paths
599}
600
601func xrandomID(n int) string {
602 return base64.RawURLEncoding.EncodeToString(xrandom(n))
603}
604
605func xrandom(n int) []byte {
606 buf := make([]byte, n)
607 x, err := cryptorand.Read(buf)
608 if err != nil {
609 panic("read random")
610 } else if x != n {
611 panic("short random read")
612 }
613 return buf
614}
615
616func (s server) Send(ctx context.Context, req webapi.SendRequest) (resp webapi.SendResult, err error) {
617 // Similar between ../smtpserver/server.go:/submit\( and ../webmail/api.go:/MessageSubmit\( and ../webapisrv/server.go:/Send\(
618
619 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
620 log := reqInfo.Log
621 acc := reqInfo.Account
622
623 m := req.Message
624
625 accConf, _ := acc.Conf()
626
627 if m.Text == "" && m.HTML == "" {
628 return resp, webapi.Error{Code: "missingBody", Message: "at least text or html body required"}
629 }
630
631 if len(m.From) == 0 {
632 m.From = []webapi.NameAddress{{Name: accConf.FullName, Address: reqInfo.LoginAddress}}
633 } else if len(m.From) > 1 {
634 return resp, webapi.Error{Code: "multipleFrom", Message: "multiple from-addresses not allowed"}
635 }
636 froms, fromPaths := xparseAddresses(m.From)
637 from, fromPath := froms[0], fromPaths[0]
638 to, toPaths := xparseAddresses(m.To)
639 cc, ccPaths := xparseAddresses(m.CC)
640 bcc, bccPaths := xparseAddresses(m.BCC)
641
642 recipients := append(append(toPaths, ccPaths...), bccPaths...)
643 addresses := append(append(m.To, m.CC...), m.BCC...)
644
645 // Check if from address is allowed for account.
646 if ok, disabled := mox.AllowMsgFrom(acc.Name, from.Address); disabled {
647 metricSubmission.WithLabelValues("domaindisabled").Inc()
648 return resp, webapi.Error{Code: "domainDisabled", Message: "domain of from-address is temporarily disabled"}
649 } else if !ok {
650 metricSubmission.WithLabelValues("badfrom").Inc()
651 return resp, webapi.Error{Code: "badFrom", Message: "from-address not configured for account"}
652 }
653
654 if len(recipients) == 0 {
655 return resp, webapi.Error{Code: "noRecipients", Message: "no recipients"}
656 }
657
658 // Check outgoing message rate limit.
659 xdbread(ctx, acc, func(tx *bstore.Tx) {
660 msglimit, rcptlimit, err := acc.SendLimitReached(tx, recipients)
661 if msglimit >= 0 {
662 metricSubmission.WithLabelValues("messagelimiterror").Inc()
663 panic(webapi.Error{Code: "messageLimitReached", Message: "outgoing message rate limit reached"})
664 } else if rcptlimit >= 0 {
665 metricSubmission.WithLabelValues("recipientlimiterror").Inc()
666 panic(webapi.Error{Code: "recipientLimitReached", Message: "outgoing new recipient rate limit reached"})
667 }
668 xcheckf(err, "checking send limit")
669 })
670
671 // If we have a non-ascii localpart, we will be sending with smtputf8. We'll go
672 // full utf-8 then.
673 intl := func(l []smtp.Path) bool {
674 for _, p := range l {
675 if p.Localpart.IsInternational() {
676 return true
677 }
678 }
679 return false
680 }
681 smtputf8 := intl([]smtp.Path{fromPath}) || intl(toPaths) || intl(ccPaths) || intl(bccPaths)
682
683 replyTos, replyToPaths := xparseAddresses(m.ReplyTo)
684 for _, rt := range replyToPaths {
685 if rt.Localpart.IsInternational() {
686 smtputf8 = true
687 }
688 }
689
690 // Create file to compose message into.
691 dataFile, err := store.CreateMessageTemp(log, "webapi-submit")
692 xcheckf(err, "creating temporary file for message")
693 defer store.CloseRemoveTempFile(log, dataFile, "message to submit")
694
695 // If writing to the message file fails, we abort immediately.
696 xc := message.NewComposer(dataFile, s.maxMsgSize, smtputf8)
697 defer func() {
698 x := recover()
699 if x == nil {
700 return
701 }
702 if err, ok := x.(error); ok && errors.Is(err, message.ErrMessageSize) {
703 panic(webapi.Error{Code: "messageTooLarge", Message: "message too large"})
704 } else if ok && errors.Is(err, message.ErrCompose) {
705 xcheckf(err, "making message")
706 }
707 panic(x)
708 }()
709
710 // Each queued message gets a Received header.
711 // We cannot use VIA, because there is no registered method. We would like to use
712 // it to add the ascii domain name in case of smtputf8 and IDNA host name.
713 // We don't add the IP address of the submitter. Exposing likely not desirable.
714 recvFrom := message.HeaderCommentDomain(mox.Conf.Static.HostnameDomain, smtputf8)
715 recvBy := mox.Conf.Static.HostnameDomain.XName(smtputf8)
716 recvID := mox.ReceivedID(mox.CidFromCtx(ctx))
717 recvHdrFor := func(rcptTo string) string {
718 recvHdr := &message.HeaderWriter{}
719 // For additional Received-header clauses, see:
720 // https://www.iana.org/assignments/mail-parameters/mail-parameters.xhtml#table-mail-parameters-8
721 // Note: we don't have "via" or "with", there is no registered for webmail.
722 recvHdr.Add(" ", "Received:", "from", recvFrom, "by", recvBy, "id", recvID) // ../rfc/5321:3158
723 if reqInfo.Request.TLS != nil {
724 recvHdr.Add(" ", mox.TLSReceivedComment(log, *reqInfo.Request.TLS)...)
725 }
726 recvHdr.Add(" ", "for", "<"+rcptTo+">;", time.Now().Format(message.RFC5322Z))
727 return recvHdr.String()
728 }
729
730 // Outer message headers.
731 xc.HeaderAddrs("From", []message.NameAddress{from})
732 if len(replyTos) > 0 {
733 xc.HeaderAddrs("Reply-To", replyTos)
734 }
735 xc.HeaderAddrs("To", to)
736 xc.HeaderAddrs("Cc", cc)
737 // We prepend Bcc headers to the message when adding to the Sent mailbox.
738 if m.Subject != "" {
739 xcheckcontrol(m.Subject)
740 xc.Subject(m.Subject)
741 }
742
743 var date time.Time
744 if m.Date != nil {
745 date = *m.Date
746 } else {
747 date = time.Now()
748 }
749 xc.Header("Date", date.Format(message.RFC5322Z))
750
751 if m.MessageID == "" {
752 m.MessageID = fmt.Sprintf("<%s>", mox.MessageIDGen(smtputf8))
753 } else if !strings.HasPrefix(m.MessageID, "<") || !strings.HasSuffix(m.MessageID, ">") {
754 return resp, webapi.Error{Code: "malformedMessageID", Message: "missing <> in message-id"}
755 }
756 xcheckcontrol(m.MessageID)
757 xc.Header("Message-Id", m.MessageID)
758
759 if len(m.References) > 0 {
760 for _, ref := range m.References {
761 xcheckcontrol(ref)
762 // We don't check for <>'s. If caller just puts in what they got, we don't want to
763 // reject the message.
764 }
765 xc.Header("References", strings.Join(m.References, "\r\n\t"))
766 xc.Header("In-Reply-To", m.References[len(m.References)-1])
767 }
768 xc.Header("MIME-Version", "1.0")
769
770 var haveUserAgent bool
771 for _, kv := range req.Headers {
772 xcheckcontrol(kv[0])
773 xcheckcontrol(kv[1])
774 xc.Header(kv[0], kv[1])
775 if strings.EqualFold(kv[0], "User-Agent") || strings.EqualFold(kv[0], "X-Mailer") {
776 haveUserAgent = true
777 }
778 }
779 if !haveUserAgent {
780 xc.Header("User-Agent", "mox/"+moxvar.Version)
781 }
782
783 // Whether we have additional separately alternative/inline/attached file(s).
784 mpf := reqInfo.Request.MultipartForm
785 formAlternative := mpf != nil && len(mpf.File["alternativefile"]) > 0
786 formInline := mpf != nil && len(mpf.File["inlinefile"]) > 0
787 formAttachment := mpf != nil && len(mpf.File["attachedfile"]) > 0
788
789 // MIME structure we'll build:
790 // - multipart/mixed (in case of attached files)
791 // - multipart/related (in case of inline files, we assume they are relevant both text and html part if present)
792 // - multipart/alternative (in case we have both text and html bodies)
793 // - text/plain (optional)
794 // - text/html (optional)
795 // - alternative file, ...
796 // - inline file, ...
797 // - attached file, ...
798
799 // We keep track of cur, which is where we add new parts to, whether the text or
800 // html part, or the inline or attached files.
801 var cur, mixed, related, alternative *multipart.Writer
802 xcreateMultipart := func(subtype string) *multipart.Writer {
803 mp := multipart.NewWriter(xc)
804 if cur == nil {
805 xc.Header("Content-Type", fmt.Sprintf(`multipart/%s; boundary="%s"`, subtype, mp.Boundary()))
806 xc.Line()
807 } else {
808 _, err := cur.CreatePart(textproto.MIMEHeader{"Content-Type": []string{fmt.Sprintf(`multipart/%s; boundary="%s"`, subtype, mp.Boundary())}})
809 xcheckf(err, "adding multipart")
810 }
811 return mp
812 }
813 xcreatePart := func(header textproto.MIMEHeader) io.Writer {
814 if cur == nil {
815 for k, vl := range header {
816 for _, v := range vl {
817 xc.Header(k, v)
818 }
819 }
820 xc.Line()
821 return xc
822 }
823 p, err := cur.CreatePart(header)
824 xcheckf(err, "adding part")
825 return p
826 }
827 // We create multiparts from outer structure to inner. Then for each we add its
828 // inner parts and close the multipart.
829 if len(req.AttachedFiles) > 0 || formAttachment {
830 mixed = xcreateMultipart("mixed")
831 cur = mixed
832 }
833 if len(req.InlineFiles) > 0 || formInline {
834 related = xcreateMultipart("related")
835 cur = related
836 }
837 if m.Text != "" && m.HTML != "" || len(req.AlternativeFiles) > 0 || formAlternative {
838 alternative = xcreateMultipart("alternative")
839 cur = alternative
840 }
841 if m.Text != "" {
842 textBody, ct, cte := xc.TextPart("plain", m.Text)
843 tp := xcreatePart(textproto.MIMEHeader{"Content-Type": []string{ct}, "Content-Transfer-Encoding": []string{cte}})
844 _, err := tp.Write([]byte(textBody))
845 xcheckf(err, "write text part")
846 }
847 if m.HTML != "" {
848 htmlBody, ct, cte := xc.TextPart("html", m.HTML)
849 tp := xcreatePart(textproto.MIMEHeader{"Content-Type": []string{ct}, "Content-Transfer-Encoding": []string{cte}})
850 _, err := tp.Write([]byte(htmlBody))
851 xcheckf(err, "write html part")
852 }
853
854 xaddFileBase64 := func(ct string, inline bool, filename string, cid string, base64Data string) {
855 h := textproto.MIMEHeader{}
856 disp := "attachment"
857 if inline {
858 disp = "inline"
859 }
860 cd := mime.FormatMediaType(disp, map[string]string{"filename": filename})
861
862 h.Set("Content-Type", ct)
863 h.Set("Content-Disposition", cd)
864 if cid != "" {
865 h.Set("Content-ID", cid)
866 }
867 h.Set("Content-Transfer-Encoding", "base64")
868 p := xcreatePart(h)
869
870 for len(base64Data) > 0 {
871 line := base64Data
872 n := min(len(line), 76) // ../rfc/2045:1372
873 line, base64Data = base64Data[:n], base64Data[n:]
874 _, err := p.Write([]byte(line))
875 xcheckf(err, "writing attachment")
876 _, err = p.Write([]byte("\r\n"))
877 xcheckf(err, "writing attachment")
878 }
879 }
880 xaddJSONFiles := func(l []webapi.File, inline bool) {
881 for _, f := range l {
882 if f.ContentType == "" {
883 buf, _ := io.ReadAll(io.LimitReader(base64.NewDecoder(base64.StdEncoding, strings.NewReader(f.Data)), 512))
884 f.ContentType = http.DetectContentType(buf)
885 if f.ContentType == "application/octet-stream" {
886 f.ContentType = ""
887 }
888 }
889
890 // Ensure base64 is valid, then we'll write the original string.
891 _, err := io.Copy(io.Discard, base64.NewDecoder(base64.StdEncoding, strings.NewReader(f.Data)))
892 xcheckuserf(err, "parsing attachment as base64")
893
894 xaddFileBase64(f.ContentType, inline, f.Name, f.ContentID, f.Data)
895 }
896 }
897 xaddFile := func(fh *multipart.FileHeader, inline bool) {
898 f, err := fh.Open()
899 xcheckf(err, "open uploaded file")
900 defer func() {
901 err := f.Close()
902 log.Check(err, "closing uploaded file")
903 }()
904
905 ct := fh.Header.Get("Content-Type")
906 if ct == "" {
907 buf, err := io.ReadAll(io.LimitReader(f, 512))
908 if err == nil {
909 ct = http.DetectContentType(buf)
910 }
911 _, err = f.Seek(0, 0)
912 xcheckf(err, "rewind uploaded file after content-detection")
913 if ct == "application/octet-stream" {
914 ct = ""
915 }
916 }
917
918 h := textproto.MIMEHeader{}
919 disp := "attachment"
920 if inline {
921 disp = "inline"
922 }
923 cd := mime.FormatMediaType(disp, map[string]string{"filename": fh.Filename})
924
925 if ct != "" {
926 h.Set("Content-Type", ct)
927 }
928 h.Set("Content-Disposition", cd)
929 cid := fh.Header.Get("Content-ID")
930 if cid != "" {
931 h.Set("Content-ID", cid)
932 }
933 h.Set("Content-Transfer-Encoding", "base64")
934 p := xcreatePart(h)
935 bw := moxio.Base64Writer(p)
936 _, err = io.Copy(bw, f)
937 xcheckf(err, "adding uploaded file")
938 err = bw.Close()
939 xcheckf(err, "flushing uploaded file")
940 }
941
942 cur = alternative
943 xaddJSONFiles(req.AlternativeFiles, true)
944 if mpf != nil {
945 for _, fh := range mpf.File["alternativefile"] {
946 xaddFile(fh, true)
947 }
948 }
949 if alternative != nil {
950 err := alternative.Close()
951 xcheckf(err, "closing alternative part")
952 alternative = nil
953 }
954
955 cur = related
956 xaddJSONFiles(req.InlineFiles, true)
957 if mpf != nil {
958 for _, fh := range mpf.File["inlinefile"] {
959 xaddFile(fh, true)
960 }
961 }
962 if related != nil {
963 err := related.Close()
964 xcheckf(err, "closing related part")
965 related = nil
966 }
967 cur = mixed
968 xaddJSONFiles(req.AttachedFiles, false)
969 if mpf != nil {
970 for _, fh := range mpf.File["attachedfile"] {
971 xaddFile(fh, false)
972 }
973 }
974 if mixed != nil {
975 err := mixed.Close()
976 xcheckf(err, "closing mixed part")
977 mixed = nil
978 }
979 cur = nil
980 xc.Flush()
981
982 // Add DKIM-Signature headers.
983 var msgPrefix string
984 fd := from.Address.Domain
985 confDom, _ := mox.Conf.Domain(fd)
986 if confDom.Disabled {
987 xcheckuserf(mox.ErrDomainDisabled, "checking domain")
988 }
989 selectors := mox.DKIMSelectors(confDom.DKIM)
990 if len(selectors) > 0 {
991 dkimHeaders, err := dkim.Sign(ctx, log.Logger, from.Address.Localpart, fd, selectors, smtputf8, dataFile)
992 if err != nil {
993 metricServerErrors.WithLabelValues("dkimsign").Inc()
994 }
995 xcheckf(err, "sign dkim")
996
997 msgPrefix = dkimHeaders
998 }
999
1000 loginAddr, err := smtp.ParseAddress(reqInfo.LoginAddress)
1001 xcheckf(err, "parsing login address")
1002 useFromID := slices.Contains(accConf.ParsedFromIDLoginAddresses, loginAddr)
1003 var localpartBase string
1004 if useFromID {
1005 localpartBase = strings.SplitN(string(fromPath.Localpart), confDom.LocalpartCatchallSeparatorsEffective[0], 2)[0]
1006 }
1007 fromIDs := make([]string, len(recipients))
1008 qml := make([]queue.Msg, len(recipients))
1009 now := time.Now()
1010 for i, rcpt := range recipients {
1011 fp := fromPath
1012 if useFromID {
1013 fromIDs[i] = xrandomID(16)
1014 fp.Localpart = smtp.Localpart(localpartBase + confDom.LocalpartCatchallSeparatorsEffective[0] + fromIDs[i])
1015 }
1016
1017 // Don't use per-recipient unique message prefix when multiple recipients are
1018 // present, we want to keep the message identical.
1019 var recvRcpt string
1020 if len(recipients) == 1 {
1021 recvRcpt = rcpt.XString(smtputf8)
1022 }
1023 rcptMsgPrefix := recvHdrFor(recvRcpt) + msgPrefix
1024 msgSize := int64(len(rcptMsgPrefix)) + xc.Size
1025 qm := queue.MakeMsg(fp, rcpt, xc.Has8bit, xc.SMTPUTF8, msgSize, m.MessageID, []byte(rcptMsgPrefix), req.RequireTLS, now, m.Subject)
1026 qm.FromID = fromIDs[i]
1027 qm.Extra = req.Extra
1028 if req.FutureRelease != nil {
1029 ival := time.Until(*req.FutureRelease)
1030 if ival > queue.FutureReleaseIntervalMax {
1031 xcheckuserf(fmt.Errorf("date/time can not be further than %v in the future", queue.FutureReleaseIntervalMax), "scheduling delivery")
1032 }
1033 qm.NextAttempt = *req.FutureRelease
1034 qm.FutureReleaseRequest = "until;" + req.FutureRelease.Format(time.RFC3339)
1035 // todo: possibly add a header to the message stored in the Sent mailbox to indicate it was scheduled for later delivery.
1036 }
1037 qml[i] = qm
1038 }
1039 err = queue.Add(ctx, log, acc.Name, dataFile, qml...)
1040 if err != nil {
1041 metricSubmission.WithLabelValues("queueerror").Inc()
1042 }
1043 xcheckf(err, "adding messages to the delivery queue")
1044 metricSubmission.WithLabelValues("ok").Inc()
1045
1046 // Message has been added to the queue. Ensure we finish the work.
1047 ctx = context.WithoutCancel(ctx)
1048
1049 if req.SaveSent {
1050 // Append message to Sent mailbox and mark original messages as answered/forwarded.
1051 acc.WithRLock(func() {
1052 var changes []store.Change
1053
1054 var sentID int64
1055 metricked := false
1056 defer func() {
1057 if sentID != 0 {
1058 p := acc.MessagePath(sentID)
1059 err := os.Remove(p)
1060 log.Check(err, "removing sent message file after error", slog.String("path", p))
1061 }
1062
1063 if x := recover(); x != nil {
1064 if !metricked {
1065 metricServerErrors.WithLabelValues("submit").Inc()
1066 }
1067 panic(x)
1068 }
1069 }()
1070 xdbwrite(ctx, reqInfo.Account, func(tx *bstore.Tx) {
1071 sentmb, err := bstore.QueryTx[store.Mailbox](tx).FilterEqual("Expunged", false).FilterEqual("Sent", true).Get()
1072 if err == bstore.ErrAbsent {
1073 // There is no mailbox designated as Sent mailbox, so we're done.
1074 return
1075 }
1076 xcheckf(err, "message submitted to queue, adding to Sent mailbox")
1077
1078 modseq, err := acc.NextModSeq(tx)
1079 xcheckf(err, "next modseq")
1080
1081 // If there were bcc headers, prepend those to the stored message only, before the
1082 // DKIM signature. The DKIM-signature oversigns the bcc header, so this stored message
1083 // won't validate with DKIM anymore, which is fine.
1084 if len(bcc) > 0 {
1085 var sb strings.Builder
1086 xbcc := message.NewComposer(&sb, 100*1024, smtputf8)
1087 xbcc.HeaderAddrs("Bcc", bcc)
1088 xbcc.Flush()
1089 msgPrefix = sb.String() + msgPrefix
1090 }
1091
1092 sentm := store.Message{
1093 CreateSeq: modseq,
1094 ModSeq: modseq,
1095 MailboxID: sentmb.ID,
1096 MailboxOrigID: sentmb.ID,
1097 Flags: store.Flags{Notjunk: true, Seen: true},
1098 Size: int64(len(msgPrefix)) + xc.Size,
1099 MsgPrefix: []byte(msgPrefix),
1100 }
1101
1102 err = acc.MessageAdd(log, tx, &sentmb, &sentm, dataFile, store.AddOpts{})
1103 if err != nil && errors.Is(err, store.ErrOverQuota) {
1104 panic(webapi.Error{Code: "sentOverQuota", Message: fmt.Sprintf("message was sent, but not stored in sent mailbox: %v", err)})
1105 } else if err != nil {
1106 metricSubmission.WithLabelValues("storesenterror").Inc()
1107 metricked = true
1108 }
1109 xcheckf(err, "message submitted to queue, appending message to Sent mailbox")
1110 sentID = sentm.ID
1111
1112 err = tx.Update(&sentmb)
1113 xcheckf(err, "updating mailbox")
1114
1115 changes = append(changes, sentm.ChangeAddUID(sentmb), sentmb.ChangeCounts())
1116 })
1117 sentID = 0 // Commit.
1118
1119 store.BroadcastChanges(acc, changes)
1120 })
1121 }
1122
1123 submissions := make([]webapi.Submission, len(qml))
1124 for i, qm := range qml {
1125 submissions[i] = webapi.Submission{
1126 Address: addresses[i].Address,
1127 QueueMsgID: qm.ID,
1128 FromID: fromIDs[i],
1129 }
1130 }
1131 resp = webapi.SendResult{
1132 MessageID: m.MessageID,
1133 Submissions: submissions,
1134 }
1135 return resp, nil
1136}
1137
1138func (s server) SuppressionList(ctx context.Context, req webapi.SuppressionListRequest) (resp webapi.SuppressionListResult, err error) {
1139 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1140 resp.Suppressions, err = queue.SuppressionList(ctx, reqInfo.Account.Name)
1141 return
1142}
1143
1144func (s server) SuppressionAdd(ctx context.Context, req webapi.SuppressionAddRequest) (resp webapi.SuppressionAddResult, err error) {
1145 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1146 addr := xparseAddress(req.EmailAddress)
1147 sup := webapi.Suppression{
1148 Account: reqInfo.Account.Name,
1149 Manual: req.Manual,
1150 Reason: req.Reason,
1151 }
1152 err = queue.SuppressionAdd(ctx, addr.Path(), &sup)
1153 return resp, err
1154}
1155
1156func (s server) SuppressionRemove(ctx context.Context, req webapi.SuppressionRemoveRequest) (resp webapi.SuppressionRemoveResult, err error) {
1157 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1158 addr := xparseAddress(req.EmailAddress)
1159 err = queue.SuppressionRemove(ctx, reqInfo.Account.Name, addr.Path())
1160 return resp, err
1161}
1162
1163func (s server) SuppressionPresent(ctx context.Context, req webapi.SuppressionPresentRequest) (resp webapi.SuppressionPresentResult, err error) {
1164 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1165 addr := xparseAddress(req.EmailAddress)
1166 xcheckuserf(err, "parsing address %q", req.EmailAddress)
1167 sup, err := queue.SuppressionLookup(ctx, reqInfo.Account.Name, addr.Path())
1168 if sup != nil {
1169 resp.Present = true
1170 }
1171 return resp, err
1172}
1173
1174func xwebapiAddresses(l []message.Address) (r []webapi.NameAddress) {
1175 r = make([]webapi.NameAddress, len(l))
1176 for i, ma := range l {
1177 dom, err := dns.ParseDomain(ma.Host)
1178 xcheckf(err, "parsing host %q for address", ma.Host)
1179 lp, err := smtp.ParseLocalpart(ma.User)
1180 xcheckf(err, "parsing localpart %q for address", ma.User)
1181 path := smtp.Path{Localpart: lp, IPDomain: dns.IPDomain{Domain: dom}}
1182 r[i] = webapi.NameAddress{Name: ma.Name, Address: path.XString(true)}
1183 }
1184 return r
1185}
1186
1187// caller should hold account lock.
1188func xmessageGet(ctx context.Context, acc *store.Account, msgID int64) (store.Message, store.Mailbox) {
1189 m := store.Message{ID: msgID}
1190 var mb store.Mailbox
1191 err := acc.DB.Read(ctx, func(tx *bstore.Tx) error {
1192 if err := tx.Get(&m); err == bstore.ErrAbsent || err == nil && m.Expunged {
1193 panic(webapi.Error{Code: "messageNotFound", Message: "message not found"})
1194 }
1195 var err error
1196 mb, err = store.MailboxID(tx, m.MailboxID)
1197 if err != nil {
1198 return fmt.Errorf("get mailbox: %v", err)
1199 }
1200 return nil
1201 })
1202 xcheckf(err, "get message")
1203 return m, mb
1204}
1205
1206func (s server) MessageGet(ctx context.Context, req webapi.MessageGetRequest) (resp webapi.MessageGetResult, err error) {
1207 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1208 log := reqInfo.Log
1209 acc := reqInfo.Account
1210
1211 var m store.Message
1212 var mb store.Mailbox
1213 var msgr *store.MsgReader
1214 acc.WithRLock(func() {
1215 m, mb = xmessageGet(ctx, acc, req.MsgID)
1216 msgr = acc.MessageReader(m)
1217 })
1218 defer func() {
1219 if err != nil {
1220 err := msgr.Close()
1221 log.Check(err, "cleaning up message reader")
1222 }
1223 }()
1224
1225 p, err := m.LoadPart(msgr)
1226 xcheckf(err, "load parsed message")
1227
1228 var env message.Envelope
1229 if p.Envelope != nil {
1230 env = *p.Envelope
1231 }
1232 text, html, _, err := webops.ReadableParts(p, 1*1024*1024)
1233 if err != nil {
1234 log.Debugx("looking for text and html content in message", err)
1235 }
1236 date := &env.Date
1237 if date.IsZero() {
1238 date = nil
1239 }
1240
1241 // Parse References message header.
1242 h, err := p.Header()
1243 if err != nil {
1244 log.Debugx("parsing headers for References", err)
1245 }
1246 var refs []string
1247 for _, s := range h.Values("References") {
1248 s = strings.ReplaceAll(s, "\t", " ")
1249 for _, w := range strings.Split(s, " ") {
1250 if w != "" {
1251 refs = append(refs, w)
1252 }
1253 }
1254 }
1255 if env.InReplyTo != "" && !slices.Contains(refs, env.InReplyTo) {
1256 // References are ordered, most recent first. In-Reply-To is less powerful/older.
1257 // So if both are present, give References preference, prepending the In-Reply-To
1258 // header.
1259 refs = append([]string{env.InReplyTo}, refs...)
1260 }
1261
1262 msg := webapi.Message{
1263 From: xwebapiAddresses(env.From),
1264 To: xwebapiAddresses(env.To),
1265 CC: xwebapiAddresses(env.CC),
1266 BCC: xwebapiAddresses(env.BCC),
1267 ReplyTo: xwebapiAddresses(env.ReplyTo),
1268 MessageID: env.MessageID,
1269 References: refs,
1270 Date: date,
1271 Subject: env.Subject,
1272 Text: strings.ReplaceAll(text, "\r\n", "\n"),
1273 HTML: strings.ReplaceAll(html, "\r\n", "\n"),
1274 }
1275
1276 var msgFrom string
1277 if d, err := dns.ParseDomain(m.MsgFromDomain); err == nil {
1278 msgFrom = smtp.NewAddress(m.MsgFromLocalpart, d).Pack(true)
1279 }
1280 var rcptTo string
1281 if m.RcptToDomain != "" {
1282 rcptTo = m.RcptToLocalpart.String() + "@" + m.RcptToDomain
1283 }
1284 meta := webapi.MessageMeta{
1285 Size: m.Size,
1286 DSN: m.DSN,
1287 Flags: append(m.Flags.Strings(), m.Keywords...),
1288 MailFrom: m.MailFrom,
1289 MailFromValidated: m.MailFromValidated,
1290 RcptTo: rcptTo,
1291 MsgFrom: msgFrom,
1292 MsgFromValidated: m.MsgFromValidated,
1293 DKIMVerifiedDomains: m.DKIMDomains,
1294 RemoteIP: m.RemoteIP,
1295 MailboxName: mb.Name,
1296 }
1297
1298 structure, err := queue.PartStructure(log, &p)
1299 xcheckf(err, "parsing structure")
1300
1301 result := webapi.MessageGetResult{
1302 Message: msg,
1303 Structure: structure,
1304 Meta: meta,
1305 }
1306 return result, nil
1307}
1308
1309func (s server) MessageRawGet(ctx context.Context, req webapi.MessageRawGetRequest) (resp io.ReadCloser, err error) {
1310 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1311 acc := reqInfo.Account
1312
1313 var m store.Message
1314 var msgr *store.MsgReader
1315 acc.WithRLock(func() {
1316 m, _ = xmessageGet(ctx, acc, req.MsgID)
1317 msgr = acc.MessageReader(m)
1318 })
1319
1320 reqInfo.Response.Header().Set("Content-Type", "text/plain")
1321 return msgr, nil
1322}
1323
1324func (s server) MessagePartGet(ctx context.Context, req webapi.MessagePartGetRequest) (resp io.ReadCloser, err error) {
1325 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1326 log := reqInfo.Log
1327 acc := reqInfo.Account
1328
1329 var m store.Message
1330 var msgr *store.MsgReader
1331 acc.WithRLock(func() {
1332 m, _ = xmessageGet(ctx, acc, req.MsgID)
1333 msgr = acc.MessageReader(m)
1334 })
1335 defer func() {
1336 if err != nil {
1337 err := msgr.Close()
1338 log.Check(err, "cleaning up message reader")
1339 }
1340 }()
1341
1342 p, err := m.LoadPart(msgr)
1343 xcheckf(err, "load parsed message")
1344
1345 for i, index := range req.PartPath {
1346 if index < 0 || index >= len(p.Parts) {
1347 return nil, webapi.Error{Code: "partNotFound", Message: fmt.Sprintf("part %d at index %d not found", index, i)}
1348 }
1349 p = p.Parts[index]
1350 }
1351 return struct {
1352 io.Reader
1353 io.Closer
1354 }{Reader: p.Reader(), Closer: msgr}, nil
1355}
1356
1357var xops = webops.XOps{
1358 DBWrite: xdbwrite,
1359 Checkf: func(ctx context.Context, err error, format string, args ...any) {
1360 xcheckf(err, format, args...)
1361 },
1362 Checkuserf: func(ctx context.Context, err error, format string, args ...any) {
1363 if err != nil && errors.Is(err, webops.ErrMessageNotFound) {
1364 msg := fmt.Sprintf("%s: %s", fmt.Sprintf(format, args...), err)
1365 panic(webapi.Error{Code: "messageNotFound", Message: msg})
1366 }
1367 xcheckuserf(err, format, args...)
1368 },
1369}
1370
1371func (s server) MessageDelete(ctx context.Context, req webapi.MessageDeleteRequest) (resp webapi.MessageDeleteResult, err error) {
1372 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1373 xops.MessageDelete(ctx, reqInfo.Log, reqInfo.Account, []int64{req.MsgID})
1374 return
1375}
1376
1377func (s server) MessageFlagsAdd(ctx context.Context, req webapi.MessageFlagsAddRequest) (resp webapi.MessageFlagsAddResult, err error) {
1378 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1379 xops.MessageFlagsAdd(ctx, reqInfo.Log, reqInfo.Account, []int64{req.MsgID}, req.Flags)
1380 return
1381}
1382
1383func (s server) MessageFlagsRemove(ctx context.Context, req webapi.MessageFlagsRemoveRequest) (resp webapi.MessageFlagsRemoveResult, err error) {
1384 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1385 xops.MessageFlagsClear(ctx, reqInfo.Log, reqInfo.Account, []int64{req.MsgID}, req.Flags)
1386 return
1387}
1388
1389func (s server) MessageMove(ctx context.Context, req webapi.MessageMoveRequest) (resp webapi.MessageMoveResult, err error) {
1390 reqInfo := ctx.Value(requestInfoCtxKey).(requestInfo)
1391 xops.MessageMove(ctx, reqInfo.Log, reqInfo.Account, []int64{req.MsgID}, req.DestMailboxName, 0)
1392 return
1393}
1394