| 1 | // # Copyright (C) 2025 Francois Marier |
| 2 | // |
| 3 | // Email-Reminder is free software; you can redistribute it and/or |
| 4 | // modify it under the terms of the GNU General Public License as |
| 5 | // published by the Free Software Foundation; either version 3 of the |
| 6 | // License, or (at your option) any later version. |
| 7 | // |
| 8 | // Email-Reminder is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | // General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Email-Reminder; if not, write to the Free Software |
| 15 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | // 02110-1301, USA. |
| 17 | package configscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/ |
| 18 | |
| 19 | import ( |
| 20 | "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/" |
| 21 | _ "embedscip-go gomod github.com/golang/go/src go1.25.0 embed/" |
| 22 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 23 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 24 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 25 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 26 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 27 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 28 | |
| 29 | "launchpad.net/email-reminder/internal/utilscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/util`/" |
| 30 | ) |
| 31 | |
| 32 | //go:embed version |
| 33 | var VersionNumberscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/VersionNumber. string |
| 34 | |
| 35 | const ( |
| 36 | LegacyUserConfigFilescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/LegacyUserConfigFile. = ".email-reminders" |
| 37 | UserConfigDirscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/UserConfigDir. = ".config/email-reminder" |
| 38 | UserConfigFilescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/UserConfigFile. = "reminders.xml" |
| 39 | SpoolDirscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/SpoolDir. = "/var/spool/email-reminder" |
| 40 | systemWideConfigFilescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/systemWideConfigFile. = "email-reminder.conf" // relative to /etc |
| 41 | ) |
| 42 | |
| 43 | type Configscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config# struct { |
| 44 | SmtpServerscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpServer. string |
| 45 | SmtpUsernamescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpUsername. string |
| 46 | SmtpPasswordscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpPassword. string |
| 47 | MailFromscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MailFrom. string |
| 48 | SendRemindersscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SendReminders. bool |
| 49 | SmtpSslscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpSsl. bool |
| 50 | |
| 51 | // Limits to prevent resource exhaustion |
| 52 | MaxEventsPerUserscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxEventsPerUser. int |
| 53 | MaxRecipientsPerEventscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxRecipientsPerEvent. int |
| 54 | MaxXMLSizeBytesscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxXMLSizeBytes. int |
| 55 | |
| 56 | // Runtime flags |
| 57 | Simulatescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#Simulate. bool |
| 58 | Verbosescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#Verbose. bool |
| 59 | } |
| 60 | |
| 61 | var smtpServerRegexscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/smtpServerRegex. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`^([A-Za-z0-9][A-Za-z0-9.\-]*[A-Za-z0-9])$`) |
| 62 | |
| 63 | func readConfigscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/readConfig().(dir string, filename string) Configscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config# { |
| 64 | // Use the local machine's hostname so that the server name matches the self-signed cert that Debian creates for STARTTLS purposes. |
| 65 | hostname, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Hostnamescip-go gomod github.com/golang/go/src go1.25.0 os/Hostname().() |
| 66 | if err != nil { |
| 67 | hostname = "localhost" |
| 68 | } |
| 69 | |
| 70 | // Default values |
| 71 | config := Configscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#{ |
| 72 | SendRemindersscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SendReminders.: true, |
| 73 | SmtpServerscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpServer.: hostname, |
| 74 | SmtpSslscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpSsl.: false, |
| 75 | SmtpUsernamescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpUsername.: "", |
| 76 | SmtpPasswordscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpPassword.: "", |
| 77 | MailFromscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MailFrom.: "root@localhost", |
| 78 | |
| 79 | MaxEventsPerUserscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxEventsPerUser.: 10000, |
| 80 | MaxRecipientsPerEventscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxRecipientsPerEvent.: 200, |
| 81 | MaxXMLSizeBytesscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxXMLSizeBytes.: 50 * 1024 * 1024, // 50MB |
| 82 | } |
| 83 | |
| 84 | file, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.OpenInRootscip-go gomod github.com/golang/go/src go1.25.0 os/OpenInRoot().(dir, filename) |
| 85 | if err != nil { |
| 86 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("failed to open system-wide config file in /etc: %v", err) |
| 87 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printlnscip-go gomod github.com/golang/go/src go1.25.0 log/Println().("Using default configuration values") |
| 88 | return config |
| 89 | } |
| 90 | defer file.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() |
| 91 | |
| 92 | if info, err := file.Statscip-go gomod github.com/golang/go/src go1.25.0 os/File#Stat().(); err == nil { |
| 93 | permissions := info.Modescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#Mode().().Permscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileMode#Perm().() |
| 94 | if permissions&0024 != 0 { |
| 95 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Warning: '%s' has insecure permissions: %o. This file could contain sensitive credentials and should be 640 at most.", file.Namescip-go gomod github.com/golang/go/src go1.25.0 os/File#Name().(), permissions) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | scanner := bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.25.0 bufio/NewScanner().(file) |
| 100 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Scan().() { |
| 101 | // Strip out comments |
| 102 | tokens := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitNscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitN().(scanner.Textscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Text().(), "#", 2) |
| 103 | line := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(tokens[0]) |
| 104 | |
| 105 | if line == "" { |
| 106 | // Skip blank lines |
| 107 | continue |
| 108 | } |
| 109 | |
| 110 | tokens = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(line, "=") |
| 111 | if len(tokens) != 2 { |
| 112 | // Stop processing invalid file |
| 113 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid line in %s: %s", filename, line) |
| 114 | continue |
| 115 | } |
| 116 | |
| 117 | key := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(tokens[0]) |
| 118 | value := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(tokens[1]) |
| 119 | switch key { |
| 120 | case "send_reminders": |
| 121 | b, err := utilscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/util`/.ParseBoolscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/util`/ParseBool().(value) |
| 122 | if err == nil { |
| 123 | config.SendRemindersscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SendReminders. = b |
| 124 | } |
| 125 | case "smtp_server": |
| 126 | if smtpServerRegexscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/smtpServerRegex..MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString().(value) { |
| 127 | config.SmtpServerscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpServer. = value |
| 128 | } else { |
| 129 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid SMTP server '%s'", value) |
| 130 | } |
| 131 | case "smtp_ssl": |
| 132 | b, err := utilscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/util`/.ParseBoolscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/util`/ParseBool().(value) |
| 133 | if err == nil { |
| 134 | config.SmtpSslscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpSsl. = b |
| 135 | } |
| 136 | case "smtp_username": |
| 137 | if len(value) <= 128 { |
| 138 | config.SmtpUsernamescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpUsername. = value |
| 139 | } else { |
| 140 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid SMTP username: length exceeds 128 characters") |
| 141 | } |
| 142 | case "smtp_password": |
| 143 | if len(value) <= 128 { |
| 144 | config.SmtpPasswordscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#SmtpPassword. = value |
| 145 | } else { |
| 146 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid SMTP password: length exceeds 128 characters") |
| 147 | } |
| 148 | case "mail_from": |
| 149 | if err := utilscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/util`/.ValidateEmailscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/util`/ValidateEmail().(value); err != nil { |
| 150 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid mail_from '%s': %v", value, err) |
| 151 | } else { |
| 152 | config.MailFromscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MailFrom. = value |
| 153 | } |
| 154 | case "max_events_per_user": |
| 155 | if n, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(value); err != nil { |
| 156 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid max_events_per_user '%s': %v", value, err) |
| 157 | } else if n <= 0 { |
| 158 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid max_events_per_user '%s': must be positive and non-zero", value) |
| 159 | } else { |
| 160 | config.MaxEventsPerUserscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxEventsPerUser. = n |
| 161 | } |
| 162 | case "max_recipients_per_event": |
| 163 | if n, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(value); err != nil { |
| 164 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid max_recipients_per_event '%s': %v", value, err) |
| 165 | } else if n <= 0 || n > 10_000 { |
| 166 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid max_recipients_per_event '%s': must be between 1 and 10000 inclusively", value) |
| 167 | } else { |
| 168 | config.MaxRecipientsPerEventscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxRecipientsPerEvent. = n |
| 169 | } |
| 170 | case "max_xml_size_mb": |
| 171 | if n, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(value); err != nil { |
| 172 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid max_xml_size_mb '%s': %v", value, err) |
| 173 | } else if n <= 0 || n > 1_000_000 { |
| 174 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Ignoring invalid max_xml_size_mb '%s': must be between 1 and 1M inclusively", value) |
| 175 | } else { |
| 176 | config.MaxXMLSizeBytesscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#MaxXMLSizeBytes. = n * 1024 * 1024 |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if err := scanner.Errscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Err().(); err != nil { |
| 182 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("Invalid system-wide config file %s: %s\n", filename, err) |
| 183 | } |
| 184 | return config |
| 185 | } |
| 186 | |
| 187 | func ReadSystemWideConfigscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/ReadSystemWideConfig().(simulate bool, verbose bool) Configscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config# { |
| 188 | config := readConfigscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/readConfig().("/etc", systemWideConfigFilescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/systemWideConfigFile.) |
| 189 | config.Simulatescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#Simulate. = simulate |
| 190 | config.Verbosescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/Config#Verbose. = verbose |
| 191 | return config |
| 192 | } |
| 193 | |
| 194 | // Returns the name of the user's config file relative to homeDir or empty string if there isn't one |
| 195 | func GetUserConfigFilenamescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/GetUserConfigFilename().(homeDir string) (string, []error) { |
| 196 | errors := make([]error, 0, 2) |
| 197 | preferredFilename := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(UserConfigDirscip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/UserConfigDir., UserConfigFilescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/UserConfigFile.) |
| 198 | if _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(homeDir, preferredFilename)); err == nil { |
| 199 | return preferredFilename, nil |
| 200 | } else { |
| 201 | errors = append(errors, err) |
| 202 | } |
| 203 | |
| 204 | if _, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(homeDir, LegacyUserConfigFilescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/LegacyUserConfigFile.)); err == nil { |
| 205 | return LegacyUserConfigFilescip-go gomod launchpad.net/email-reminder . `launchpad.net/email-reminder/internal/config`/LegacyUserConfigFile., nil |
| 206 | } else { |
| 207 | errors = append(errors, err) |
| 208 | } |
| 209 | |
| 210 | return "", errors |
| 211 | } |