|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/mail" |
| 10 | + "net/textproto" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// Exit codes as defined in <sysexits.h> |
| 17 | +const ( |
| 18 | + // The input data was incorrect in some way. This |
| 19 | + // should only be used for user's data and not system |
| 20 | + // files. |
| 21 | + ExDataErr = 65 |
| 22 | + // Temporary failure, indicating something that is not |
| 23 | + // really an error. In sendmail, this means that a |
| 24 | + // mailer (e.g.) could not create a connection, and |
| 25 | + // the request should be reattempted later. |
| 26 | + ExTempFail = 75 |
| 27 | +) |
| 28 | + |
| 29 | +var srsAddr = flag.String("srs-addr", "localhost:10001", "TCP address for SRS lookups") |
| 30 | +var rpHeader = flag.String("rp-header", "Return-Path", "header name containing the return-path (MAIL FROM) value") |
| 31 | +var sendmailPath = flag.String("sendmail-path", "sendmail", "path to the sendmail binary") |
| 32 | +var dryRun = flag.Bool("dry-run", false, "show what would be done, don't actually forward mail") |
| 33 | + |
| 34 | +// lookupTCP performs a TCP table lookup for the specified key against the |
| 35 | +// given address. |
| 36 | +func lookupTCP(addr, key string) (string, error) { |
| 37 | + c, err := textproto.Dial("tcp", addr) |
| 38 | + if err != nil { |
| 39 | + return "", err |
| 40 | + } |
| 41 | + |
| 42 | + id, err := c.Cmd("get " + key) |
| 43 | + if err != nil { |
| 44 | + return "", err |
| 45 | + } |
| 46 | + c.StartResponse(id) |
| 47 | + defer c.EndResponse(id) |
| 48 | + |
| 49 | + code, msg, err := c.ReadCodeLine(-1) |
| 50 | + if err != nil { |
| 51 | + return "", err |
| 52 | + } |
| 53 | + switch code { |
| 54 | + case 200: |
| 55 | + return msg, nil |
| 56 | + case 500: |
| 57 | + fmt.Fprintf(os.Stderr, "warning: srs: returncode 500 (%v)\n", msg) |
| 58 | + return key, nil |
| 59 | + default: |
| 60 | + return "", fmt.Errorf("srs: unexpected returncode %d (%v)", code, msg) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// die writes msg to stderr and aborts the program with the given status code. |
| 65 | +func die(msg string, code int) { |
| 66 | + fmt.Fprintln(os.Stderr, msg) |
| 67 | + os.Exit(code) |
| 68 | +} |
| 69 | + |
| 70 | +// headerRewriter wraps the given reader and performs header rewriting on read |
| 71 | +// data. Specifically, this strips the "From sender time_stamp" envelope header |
| 72 | +// inserted by Postfix and adds supplied headers. |
| 73 | +// |
| 74 | +// Note that the Return-Path header is left intact. Postfix (specifically, |
| 75 | +// the cleanup daemon) will replace this header automatically. |
| 76 | +func headerRewriter(in io.Reader, headers []string) io.Reader { |
| 77 | + buffer := bytes.Buffer{} |
| 78 | + scanner := bufio.NewScanner(in) |
| 79 | + linenum := 0 |
| 80 | + for scanner.Scan() { |
| 81 | + linenum++ |
| 82 | + line := scanner.Bytes() |
| 83 | + if linenum == 1 { |
| 84 | + for _, header := range headers { |
| 85 | + buffer.WriteString(header + "\r\n") |
| 86 | + } |
| 87 | + |
| 88 | + if bytes.HasPrefix(line, []byte("From ")) { |
| 89 | + continue |
| 90 | + } |
| 91 | + } |
| 92 | + buffer.Write(line) |
| 93 | + buffer.Write([]byte("\r\n")) |
| 94 | + } |
| 95 | + return &buffer |
| 96 | +} |
| 97 | + |
| 98 | +func main() { |
| 99 | + flag.Parse() |
| 100 | + hostname, _ := os.Hostname() |
| 101 | + |
| 102 | + buffer := bytes.Buffer{} |
| 103 | + message, err := mail.ReadMessage(io.TeeReader(os.Stdin, &buffer)) |
| 104 | + if err != nil { |
| 105 | + die(fmt.Sprintf("Parse error: %s", err), ExDataErr) |
| 106 | + } |
| 107 | + |
| 108 | + returnPath := message.Header.Get(*rpHeader) |
| 109 | + if returnPath == "" { |
| 110 | + die("Parse error: Missing return-path header in message", ExDataErr) |
| 111 | + } |
| 112 | + |
| 113 | + extraHeaders := []string{ |
| 114 | + fmt.Sprintf("Received: by %s (Postforward); %s", |
| 115 | + hostname, time.Now().Format("Mon, 2 Jan 2006 15:04:05 -0700")), |
| 116 | + fmt.Sprintf("X-Original-Return-Path: %s", returnPath)} |
| 117 | + |
| 118 | + returnPath = returnPath[1 : len(returnPath)-1] // Remove <> brackets |
| 119 | + returnPath, err = lookupTCP(*srsAddr, returnPath) |
| 120 | + if err != nil { |
| 121 | + die(fmt.Sprintf("SRS lookup error: %s", err), ExTempFail) |
| 122 | + } |
| 123 | + |
| 124 | + mailreader := io.MultiReader(headerRewriter(&buffer, extraHeaders), os.Stdin) |
| 125 | + args := append([]string{"-f", returnPath}, flag.Args()...) |
| 126 | + sendmail := exec.Command(*sendmailPath, args...) |
| 127 | + sendmail.Stdin = mailreader |
| 128 | + sendmail.Stdout = os.Stdout |
| 129 | + sendmail.Stderr = os.Stderr |
| 130 | + |
| 131 | + if *dryRun { |
| 132 | + fmt.Printf("Would call sendmail with args: %v\n", args) |
| 133 | + fmt.Print("Would pipe the following data into sendmail:\n\n") |
| 134 | + io.Copy(os.Stdout, mailreader) |
| 135 | + os.Exit(0) |
| 136 | + } |
| 137 | + |
| 138 | + if err = sendmail.Run(); err != nil { |
| 139 | + die(fmt.Sprintf("Error delivering message to sendmail: %s", err), ExTempFail) |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments