| 1 | // Collects email reminders set by users for special occasions and moves |
| 2 | // them to the email-reminder spool directory. |
| 3 | // |
| 4 | // Email-reminder allows users to define events that they want to be |
| 5 | // reminded of by email. |
| 6 | // |
| 7 | // This program is meant to be invoked every day by a cron job or as the |
| 8 | // root user. It collects the reminder files from each user. |
| 9 | // |
| 10 | // # Copyright (C) 2023-2025 Francois Marier |
| 11 | // |
| 12 | // Email-Reminder is free software; you can redistribute it and/or |
| 13 | // modify it under the terms of the GNU General Public License as |
| 14 | // published by the Free Software Foundation; either version 3 of the |
| 15 | // License, or (at your option) any later version. |
| 16 | // |
| 17 | // Email-Reminder is distributed in the hope that it will be useful, |
| 18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | // General Public License for more details. |
| 21 | // |
| 22 | // You should have received a copy of the GNU General Public License |
| 23 | // along with Email-Reminder; if not, write to the Free Software |
| 24 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 25 | // 02110-1301, USA. |
| 26 | package main |
| 27 | |
| 28 | import ( |
| 29 | "flag" |
| 30 | "fmt" |
| 31 | "io" |
| 32 | "log" |
| 33 | "os" |
| 34 | "os/user" |
| 35 | "path/filepath" |
| 36 | "slices" |
| 37 | "strconv" |
| 38 | "strings" |
| 39 | |
| 40 | "launchpad.net/email-reminder/internal/config" |
| 41 | "launchpad.net/email-reminder/internal/util" |
| 42 | ) |
| 43 | |
| 44 | const etcPasswd = "/etc/passwd" |
| 45 | const ( |
| 46 | FieldUsername = iota |
| 47 | FieldPassword |
| 48 | FieldUid |
| 49 | FieldGid |
| 50 | FieldGECOS |
| 51 | FieldHomeDir |
| 52 | FieldShell |
| 53 | ) |
| 54 | |
| 55 | var nonInteractiveShells = []string{"/bin/false", "/sbin/nologin", "/usr/bin/git-shell", "/usr/sbin/nologin"} |
| 56 | |
| 57 | type HomeDir struct { |
| 58 | Directory string |
| 59 | Uid int |
| 60 | } |
| 61 | |
| 62 | func copyReminders(homeDir HomeDir, conf config.Config) { |
| 63 | configFilename, errors := config.GetUserConfigFilename(homeDir.Directory) |
| 64 | if configFilename == "" { |
| 65 | if conf.Verbose { |
| 66 | log.Printf("No reminders in '%s'\n", homeDir.Directory) |
| 67 | for _, e := range errors { |
| 68 | log.Println(e) |
| 69 | } |
| 70 | } |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | // OpenInRoot prevents symlink escapes outside homeDir.Directory |
| 75 | src, err := os.OpenInRoot(homeDir.Directory, configFilename) |
| 76 | if err != nil { |
| 77 | configFilePath := filepath.Join(homeDir.Directory, configFilename) |
| 78 | log.Printf("Could not read from '%s': %s\n", configFilePath, err) |
| 79 | return |
| 80 | } |
| 81 | defer src.Close() |
| 82 | |
| 83 | // Validate ownership and file type on the *open* file descriptor to avoid TOCTOU races |
| 84 | if err := util.ValidateOpenFile(src, uint32(homeDir.Uid)); err != nil { |
| 85 | log.Printf("Skipping config file '%s': %s\n", src.Name(), err) |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | destinationFilePath := filepath.Join(config.SpoolDir, strconv.Itoa(homeDir.Uid)) |
| 90 | if conf.Simulate { |
| 91 | log.Printf("Simulating, not actually copying '%s' to '%s'\n", src.Name(), destinationFilePath) |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | dst, err := os.Create(destinationFilePath) |
| 96 | if err != nil { |
| 97 | log.Printf("Could not write to '%s': %s\n", destinationFilePath, err) |
| 98 | return |
| 99 | } |
| 100 | defer dst.Close() |
| 101 | |
| 102 | // Set the reader limit higher than the max file size to detect files that are too large |
| 103 | written, err := io.Copy(dst, io.LimitReader(src, int64(conf.MaxXMLSizeBytes)+1)) |
| 104 | if err != nil { |
| 105 | log.Printf("Could not copy '%s' to '%s': %s\n", src.Name(), dst.Name(), err) |
| 106 | os.Remove(destinationFilePath) |
| 107 | return |
| 108 | } |
| 109 | if written > int64(conf.MaxXMLSizeBytes) { |
| 110 | log.Printf("Skipping config file '%s': XML file too large (max %d bytes)\n", src.Name(), conf.MaxXMLSizeBytes) |
| 111 | os.Remove(destinationFilePath) |
| 112 | return |
| 113 | } |
| 114 | if conf.Verbose { |
| 115 | log.Printf("Copied '%s' to '%s'\n", src.Name(), dst.Name()) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func localHomeDirs(passwd string, verbose bool) []HomeDir { |
| 120 | homeDirs := make([]HomeDir, 0, 2) |
| 121 | data, err := os.ReadFile(passwd) |
| 122 | if err != nil { |
| 123 | log.Printf("Could not open %s: %s\n", etcPasswd, err) |
| 124 | return homeDirs |
| 125 | } |
| 126 | for _, line := range strings.Split(string(data), "\n") { |
| 127 | fields := strings.Split(line, ":") |
| 128 | if len(fields) == FieldShell+1 { |
| 129 | uid, err := strconv.Atoi(fields[FieldUid]) |
| 130 | if err != nil { |
| 131 | if verbose { |
| 132 | log.Printf("Skipping invalid user %s with invalid UID %s\n", fields[FieldUsername], fields[FieldUid]) |
| 133 | } |
| 134 | continue |
| 135 | } |
| 136 | if uid < 1000 || uid >= 60000 || slices.Contains(nonInteractiveShells, fields[FieldShell]) { |
| 137 | // This is likely a system user account, not a normal user account. |
| 138 | continue |
| 139 | } |
| 140 | if verbose { |
| 141 | log.Printf("Found %s (%d:%s) with homedir '%s'\n", fields[FieldUsername], uid, fields[FieldShell], fields[FieldHomeDir]) |
| 142 | } |
| 143 | |
| 144 | if err := util.ValidateHomeDirectory(fields[FieldHomeDir], uint32(uid)); err != nil { |
| 145 | if verbose { |
| 146 | log.Printf("Skipping user %s: %s\n", fields[FieldUsername], err) |
| 147 | } |
| 148 | continue |
| 149 | } |
| 150 | |
| 151 | homeDirs = append(homeDirs, HomeDir{fields[FieldHomeDir], uid}) |
| 152 | } |
| 153 | } |
| 154 | return homeDirs |
| 155 | } |
| 156 | |
| 157 | func requireRoot(verbose bool) { |
| 158 | currentUser, err := user.Current() |
| 159 | if err != nil { |
| 160 | log.Fatalln(err) |
| 161 | } |
| 162 | |
| 163 | if currentUser.Uid != "0" { |
| 164 | if verbose { |
| 165 | log.Printf("Current user is %s (%s)\n", currentUser.Username, currentUser.Uid) |
| 166 | } |
| 167 | log.Fatalln("This script must be run as root.") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func main() { |
| 172 | log.SetFlags(0) |
| 173 | |
| 174 | simulate := flag.Bool("simulate", false, "do not actually copy any files") |
| 175 | verbose := flag.Bool("verbose", false, "print out information about what the program is doing") |
| 176 | version := flag.Bool("version", false, "display the version number") |
| 177 | // TODO: add short names too, either hacking it like in https://www.antoniojgutierrez.com/posts/2021-05-14-short-and-long-options-in-go-flags-pkg/ |
| 178 | // or using another package like golang-github-pborman-getopt-dev in Debian |
| 179 | flag.Parse() |
| 180 | if *version { |
| 181 | fmt.Println("collect-reminders " + config.VersionNumber) |
| 182 | os.Exit(0) |
| 183 | } |
| 184 | |
| 185 | requireRoot(*verbose) |
| 186 | conf := config.ReadSystemWideConfig(*simulate, *verbose) |
| 187 | |
| 188 | homeDirs := localHomeDirs(etcPasswd, *verbose) |
| 189 | if *verbose { |
| 190 | log.Printf("Found these local home directories: %v\n", homeDirs) |
| 191 | } |
| 192 | for _, homeDir := range homeDirs { |
| 193 | copyReminders(homeDir, conf) |
| 194 | } |
| 195 | } |