2 ba97b2d7 2024-03-20 stsp * Copyright (c) 2024 Stefan Sperling <stsp@openbsd.org>
4 ba97b2d7 2024-03-20 stsp * Permission to use, copy, modify, and distribute this software for any
5 ba97b2d7 2024-03-20 stsp * purpose with or without fee is hereby granted, provided that the above
6 ba97b2d7 2024-03-20 stsp * copyright notice and this permission notice appear in all copies.
8 ba97b2d7 2024-03-20 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ba97b2d7 2024-03-20 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ba97b2d7 2024-03-20 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ba97b2d7 2024-03-20 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ba97b2d7 2024-03-20 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ba97b2d7 2024-03-20 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ba97b2d7 2024-03-20 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ba97b2d7 2024-03-20 stsp #include <sys/types.h>
18 ba97b2d7 2024-03-20 stsp #include <sys/socket.h>
20 939d3016 2024-04-23 op #include <ctype.h>
21 39910b63 2024-03-20 op #include <errno.h>
22 09486e84 2024-03-21 op #include <poll.h>
23 ba97b2d7 2024-03-20 stsp #include <stdio.h>
24 ba97b2d7 2024-03-20 stsp #include <stdlib.h>
25 ba97b2d7 2024-03-20 stsp #include <string.h>
26 ba97b2d7 2024-03-20 stsp #include <stdarg.h>
27 939d3016 2024-04-23 op #include <stdint.h>
28 7e03b468 2024-04-19 stsp #include <syslog.h>
29 ba97b2d7 2024-03-20 stsp #include <getopt.h>
30 ba97b2d7 2024-03-20 stsp #include <err.h>
31 ba97b2d7 2024-03-20 stsp #include <pwd.h>
32 ba97b2d7 2024-03-20 stsp #include <netdb.h>
33 ba97b2d7 2024-03-20 stsp #include <time.h>
34 ba97b2d7 2024-03-20 stsp #include <unistd.h>
36 7e03b468 2024-04-19 stsp #include "log.h"
38 ba97b2d7 2024-03-20 stsp #include "got_error.h"
40 ba97b2d7 2024-03-20 stsp #include "got_lib_poll.h"
42 09486e84 2024-03-21 op #define SMTP_LINE_MAX 65535
44 100d3e4b 2024-03-20 op static int smtp_timeout = 60; /* in seconds */
45 09486e84 2024-03-21 op static char smtp_buf[SMTP_LINE_MAX];
46 09486e84 2024-03-21 op static size_t smtp_buflen;
48 ba97b2d7 2024-03-20 stsp __dead static void
51 dfa6ae4c 2024-03-20 op fprintf(stderr, "usage: %s [-f sender] [-r responder] "
52 ba97b2d7 2024-03-20 stsp "[-s subject] [-h hostname] [-p port] recipient\n", getprogname());
57 39910b63 2024-03-20 op dial(const char *host, const char *port)
59 39910b63 2024-03-20 op struct addrinfo hints, *res, *res0;
60 39910b63 2024-03-20 op const char *cause = NULL;
61 39910b63 2024-03-20 op int s, error, save_errno;
63 39910b63 2024-03-20 op memset(&hints, 0, sizeof(hints));
64 39910b63 2024-03-20 op hints.ai_family = AF_UNSPEC;
65 39910b63 2024-03-20 op hints.ai_socktype = SOCK_STREAM;
66 39910b63 2024-03-20 op error = getaddrinfo(host, port, &hints, &res0);
68 7e03b468 2024-04-19 stsp fatalx("failed to resolve %s:%s: %s", host, port,
69 39910b63 2024-03-20 op gai_strerror(error));
72 39910b63 2024-03-20 op for (res = res0; res; res = res->ai_next) {
73 39910b63 2024-03-20 op s = socket(res->ai_family, res->ai_socktype,
74 39910b63 2024-03-20 op res->ai_protocol);
75 39910b63 2024-03-20 op if (s == -1) {
76 39910b63 2024-03-20 op cause = "socket";
80 39910b63 2024-03-20 op if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
81 39910b63 2024-03-20 op cause = "connect";
82 39910b63 2024-03-20 op save_errno = errno;
84 39910b63 2024-03-20 op errno = save_errno;
92 39910b63 2024-03-20 op freeaddrinfo(res0);
94 7e03b468 2024-04-19 stsp fatal("%s", cause);
98 ba97b2d7 2024-03-20 stsp static char *
99 ba97b2d7 2024-03-20 stsp set_default_fromaddr(void)
101 ba97b2d7 2024-03-20 stsp struct passwd *pw = NULL;
103 ba97b2d7 2024-03-20 stsp char hostname[255];
105 ba97b2d7 2024-03-20 stsp pw = getpwuid(getuid());
106 ba97b2d7 2024-03-20 stsp if (pw == NULL) {
107 7e03b468 2024-04-19 stsp fatalx("my UID %d was not found in password database",
111 ba97b2d7 2024-03-20 stsp if (gethostname(hostname, sizeof(hostname)) == -1)
112 7e03b468 2024-04-19 stsp fatal("gethostname");
114 ba97b2d7 2024-03-20 stsp if (asprintf(&s, "%s@%s", pw->pw_name, hostname) == -1)
115 7e03b468 2024-04-19 stsp fatal("asprintf");
121 ba97b2d7 2024-03-20 stsp read_smtp_code(int s, const char *code)
123 ba97b2d7 2024-03-20 stsp const struct got_error *error;
125 09486e84 2024-03-21 op size_t linelen;
129 09486e84 2024-03-21 op endl = memmem(smtp_buf, smtp_buflen, "\r\n", 2);
130 09486e84 2024-03-21 op if (endl != NULL)
133 09486e84 2024-03-21 op if (smtp_buflen == sizeof(smtp_buf))
134 7e03b468 2024-04-19 stsp fatalx("line too long");
136 09486e84 2024-03-21 op error = got_poll_fd(s, POLLIN, smtp_timeout);
138 7e03b468 2024-04-19 stsp fatalx("poll: %s", error->msg);
140 09486e84 2024-03-21 op r = read(s, smtp_buf + smtp_buflen,
141 09486e84 2024-03-21 op sizeof(smtp_buf) - smtp_buflen);
143 7e03b468 2024-04-19 stsp fatal("read");
145 7e03b468 2024-04-19 stsp fatalx("unexpected EOF");
146 09486e84 2024-03-21 op smtp_buflen += r;
149 09486e84 2024-03-21 op linelen = endl - smtp_buf;
150 09486e84 2024-03-21 op if (linelen < 3)
151 7e03b468 2024-04-19 stsp fatalx("invalid SMTP response");
153 09486e84 2024-03-21 op if (strncmp(code, smtp_buf, 3) != 0) {
154 09486e84 2024-03-21 op smtp_buf[3] = '\0';
155 7e03b468 2024-04-19 stsp log_warnx("unexpected SMTP message code: %s", smtp_buf);
160 09486e84 2024-03-21 op * Normally we would get just one reply, but the regress doesn't
161 09486e84 2024-03-21 op * use a real SMTP server and queues all the replies upfront.
163 09486e84 2024-03-21 op linelen += 2;
164 09486e84 2024-03-21 op memmove(smtp_buf, smtp_buf + linelen, smtp_buflen - linelen);
165 09486e84 2024-03-21 op smtp_buflen -= linelen;
171 ba97b2d7 2024-03-20 stsp send_smtp_msg(int s, const char *fmt, ...)
173 ba97b2d7 2024-03-20 stsp const struct got_error *error;
174 ba97b2d7 2024-03-20 stsp char buf[512];
176 ba97b2d7 2024-03-20 stsp va_list ap;
178 ba97b2d7 2024-03-20 stsp va_start(ap, fmt);
179 ba97b2d7 2024-03-20 stsp len = vsnprintf(buf, sizeof(buf), fmt, ap);
180 ba97b2d7 2024-03-20 stsp va_end(ap);
181 ba97b2d7 2024-03-20 stsp if (len < 0) {
182 7e03b468 2024-04-19 stsp log_warn("vsnprintf");
185 ba97b2d7 2024-03-20 stsp if (len >= sizeof(buf)) {
186 7e03b468 2024-04-19 stsp log_warnx("%s: buffer too small for message '%s...'",
187 ba97b2d7 2024-03-20 stsp __func__, buf);
191 ba97b2d7 2024-03-20 stsp error = got_poll_write_full(s, buf, len);
192 ba97b2d7 2024-03-20 stsp if (error) {
193 7e03b468 2024-04-19 stsp log_warnx("write: %s", error->msg);
200 ba97b2d7 2024-03-20 stsp static char *
201 ba97b2d7 2024-03-20 stsp get_datestr(time_t *time, char *datebuf)
203 ba97b2d7 2024-03-20 stsp struct tm mytm, *tm;
204 ba97b2d7 2024-03-20 stsp char *p, *s;
206 ba97b2d7 2024-03-20 stsp tm = gmtime_r(time, &mytm);
207 ba97b2d7 2024-03-20 stsp if (tm == NULL)
208 ba97b2d7 2024-03-20 stsp return NULL;
209 ba97b2d7 2024-03-20 stsp s = asctime_r(tm, datebuf);
210 ba97b2d7 2024-03-20 stsp if (s == NULL)
211 ba97b2d7 2024-03-20 stsp return NULL;
212 ba97b2d7 2024-03-20 stsp p = strchr(s, '\n');
218 939d3016 2024-04-23 op static const struct got_error *
219 939d3016 2024-04-23 op print_date(int s, char *date, int shortfmt)
221 939d3016 2024-04-23 op const struct got_error *error;
222 939d3016 2024-04-23 op struct tm tm;
223 939d3016 2024-04-23 op char *t, datebuf[26];
224 939d3016 2024-04-23 op const char *errstr;
227 939d3016 2024-04-23 op date[strcspn(date, " \n")] = '\0';
229 939d3016 2024-04-23 op ts = strtonum(date, INT64_MIN, INT64_MAX, &errstr);
231 939d3016 2024-04-23 op return got_error_set_errno(EINVAL, errstr);
232 939d3016 2024-04-23 op if (gmtime_r(&ts, &tm) == NULL)
233 939d3016 2024-04-23 op return got_error_set_errno(EINVAL, "gmtime_r");
235 939d3016 2024-04-23 op if (!shortfmt) {
236 939d3016 2024-04-23 op t = asctime_r(&tm, datebuf);
237 939d3016 2024-04-23 op if (t == NULL)
238 939d3016 2024-04-23 op return got_error_set_errno(EINVAL, "invalid timestamp");
239 939d3016 2024-04-23 op t[strcspn(t, "\n")] = '\0';
240 939d3016 2024-04-23 op error = got_poll_write_full(s, t, strlen(t));
242 939d3016 2024-04-23 op return error;
243 939d3016 2024-04-23 op return got_poll_write_full(s, " UTC\n", 5);
246 939d3016 2024-04-23 op if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
247 939d3016 2024-04-23 op return got_error_set_errno(EINVAL, "invalid timestamp");
248 939d3016 2024-04-23 op return got_poll_write_full(s, datebuf, strlen(datebuf));
251 ba97b2d7 2024-03-20 stsp static void
252 39910b63 2024-03-20 op send_email(int s, const char *myfromaddr, const char *fromaddr,
253 ba97b2d7 2024-03-20 stsp const char *recipient, const char *replytoaddr,
254 39910b63 2024-03-20 op const char *subject)
256 ba97b2d7 2024-03-20 stsp const struct got_error *error;
257 ba97b2d7 2024-03-20 stsp char *line = NULL;
258 ba97b2d7 2024-03-20 stsp size_t linesize = 0;
259 ba97b2d7 2024-03-20 stsp ssize_t linelen;
260 ba97b2d7 2024-03-20 stsp time_t now;
261 939d3016 2024-04-23 op int firstline = 1, shortfmt = 0;
262 ba97b2d7 2024-03-20 stsp char datebuf[26];
263 ba97b2d7 2024-03-20 stsp char *datestr;
265 ba97b2d7 2024-03-20 stsp now = time(NULL);
266 ba97b2d7 2024-03-20 stsp datestr = get_datestr(&now, datebuf);
268 ba97b2d7 2024-03-20 stsp if (read_smtp_code(s, "220"))
269 7e03b468 2024-04-19 stsp fatalx("unexpected SMTP greeting received");
271 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "HELO localhost\r\n"))
272 7e03b468 2024-04-19 stsp fatalx("could not send HELO");
273 ba97b2d7 2024-03-20 stsp if (read_smtp_code(s, "250"))
274 7e03b468 2024-04-19 stsp fatalx("unexpected SMTP response received");
276 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "MAIL FROM:<%s>\r\n", myfromaddr))
277 7e03b468 2024-04-19 stsp fatalx("could not send MAIL FROM");
278 ba97b2d7 2024-03-20 stsp if (read_smtp_code(s, "250"))
279 7e03b468 2024-04-19 stsp fatalx("unexpected SMTP response received");
281 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "RCPT TO:<%s>\r\n", recipient))
282 7e03b468 2024-04-19 stsp fatalx("could not send MAIL FROM");
283 ba97b2d7 2024-03-20 stsp if (read_smtp_code(s, "250"))
284 7e03b468 2024-04-19 stsp fatalx("unexpected SMTP response received");
286 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "DATA\r\n"))
287 7e03b468 2024-04-19 stsp fatalx("could not send MAIL FROM");
288 ba97b2d7 2024-03-20 stsp if (read_smtp_code(s, "354"))
289 7e03b468 2024-04-19 stsp fatalx("unexpected SMTP response received");
291 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "From: %s\r\n", fromaddr))
292 7e03b468 2024-04-19 stsp fatalx("could not send From header");
293 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "To: %s\r\n", recipient))
294 7e03b468 2024-04-19 stsp fatalx("could not send To header");
295 ba97b2d7 2024-03-20 stsp if (replytoaddr) {
296 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "Reply-To: %s\r\n", replytoaddr))
297 7e03b468 2024-04-19 stsp fatalx("could not send Reply-To header");
299 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "Date: %s +0000 (UTC)\r\n", datestr))
300 7e03b468 2024-04-19 stsp fatalx("could not send Date header");
302 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "Subject: %s\r\n", subject))
303 7e03b468 2024-04-19 stsp fatalx("could not send Subject header");
305 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "\r\n"))
306 7e03b468 2024-04-19 stsp fatalx("could not send body delimiter");
308 ba97b2d7 2024-03-20 stsp while ((linelen = getline(&line, &linesize, stdin)) != -1) {
309 939d3016 2024-04-23 op if (firstline && isdigit((unsigned char)line[0]))
310 939d3016 2024-04-23 op shortfmt = 1;
311 939d3016 2024-04-23 op firstline = 0;
313 ba97b2d7 2024-03-20 stsp if (line[0] == '.') { /* dot stuffing */
314 ba97b2d7 2024-03-20 stsp error = got_poll_write_full(s, ".", 1);
316 7e03b468 2024-04-19 stsp fatalx("write: %s", error->msg);
319 939d3016 2024-04-23 op if (shortfmt) {
321 939d3016 2024-04-23 op t = strchr(line, ' ');
322 939d3016 2024-04-23 op if (t != NULL) {
324 939d3016 2024-04-23 op error = print_date(s, line, shortfmt);
326 939d3016 2024-04-23 op fatalx("write: %s", error->msg);
327 939d3016 2024-04-23 op error = got_poll_write_full(s, t, strlen(t));
332 939d3016 2024-04-23 op if (!shortfmt && !strncmp(line, "date: ", 6)) {
333 939d3016 2024-04-23 op error = got_poll_write_full(s, line, 6);
335 939d3016 2024-04-23 op fatalx("write: %s", error->msg);
336 939d3016 2024-04-23 op error = print_date(s, line + 6, shortfmt);
338 939d3016 2024-04-23 op fatalx("write: %s", error->msg);
342 ba97b2d7 2024-03-20 stsp error = got_poll_write_full(s, line, linelen);
344 7e03b468 2024-04-19 stsp fatalx("write: %s", error->msg);
347 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "\r\n.\r\n"))
348 7e03b468 2024-04-19 stsp fatalx("could not send data terminator");
349 ba97b2d7 2024-03-20 stsp if (read_smtp_code(s, "250"))
350 7e03b468 2024-04-19 stsp fatalx("unexpected SMTP response received");
352 ba97b2d7 2024-03-20 stsp if (send_smtp_msg(s, "QUIT\r\n"))
353 7e03b468 2024-04-19 stsp fatalx("could not send QUIT");
355 ba97b2d7 2024-03-20 stsp if (read_smtp_code(s, "221"))
356 7e03b468 2024-04-19 stsp fatalx("unexpected SMTP response received");
359 ba97b2d7 2024-03-20 stsp free(line);
363 ba97b2d7 2024-03-20 stsp main(int argc, char *argv[])
365 ba97b2d7 2024-03-20 stsp char *default_fromaddr = NULL;
366 ba97b2d7 2024-03-20 stsp const char *fromaddr = NULL, *recipient = NULL, *replytoaddr = NULL;
367 ba97b2d7 2024-03-20 stsp const char *subject = "gotd notification";
368 ba97b2d7 2024-03-20 stsp const char *hostname = "127.0.0.1";
369 ba97b2d7 2024-03-20 stsp const char *port = "25";
370 ba97b2d7 2024-03-20 stsp const char *errstr;
371 ba97b2d7 2024-03-20 stsp char *timeoutstr;
374 7e03b468 2024-04-19 stsp log_init(0, LOG_DAEMON);
375 ba97b2d7 2024-03-20 stsp while ((ch = getopt(argc, argv, "f:r:s:h:p:")) != -1) {
376 ba97b2d7 2024-03-20 stsp switch (ch) {
378 ba97b2d7 2024-03-20 stsp hostname = optarg;
381 ba97b2d7 2024-03-20 stsp fromaddr = optarg;
384 ba97b2d7 2024-03-20 stsp port = optarg;
387 ba97b2d7 2024-03-20 stsp replytoaddr = optarg;
390 ba97b2d7 2024-03-20 stsp subject = optarg;
394 ba97b2d7 2024-03-20 stsp /* NOTREACHED */
399 ba97b2d7 2024-03-20 stsp argc -= optind;
400 ba97b2d7 2024-03-20 stsp argv += optind;
402 ba97b2d7 2024-03-20 stsp if (argc != 1)
405 ba97b2d7 2024-03-20 stsp /* used by the regression test suite */
406 ba97b2d7 2024-03-20 stsp timeoutstr = getenv("GOT_NOTIFY_EMAIL_TIMEOUT");
407 ba97b2d7 2024-03-20 stsp if (timeoutstr) {
408 8bffa129 2024-04-09 op smtp_timeout = strtonum(timeoutstr, 0, 600, &errstr);
409 ba97b2d7 2024-03-20 stsp if (errstr != NULL)
410 7e03b468 2024-04-19 stsp fatalx("timeout in seconds is %s: %s",
411 ba97b2d7 2024-03-20 stsp errstr, timeoutstr);
414 ba97b2d7 2024-03-20 stsp #ifndef PROFILE
415 ba97b2d7 2024-03-20 stsp if (pledge("stdio dns inet getpw", NULL) == -1)
416 ba97b2d7 2024-03-20 stsp err(1, "pledge");
418 ba97b2d7 2024-03-20 stsp default_fromaddr = set_default_fromaddr();
420 ba97b2d7 2024-03-20 stsp #ifndef PROFILE
421 ba97b2d7 2024-03-20 stsp if (pledge("stdio dns inet", NULL) == -1)
422 ba97b2d7 2024-03-20 stsp err(1, "pledge");
425 ba97b2d7 2024-03-20 stsp recipient = argv[0];
426 ba97b2d7 2024-03-20 stsp if (fromaddr == NULL)
427 ba97b2d7 2024-03-20 stsp fromaddr = default_fromaddr;
429 39910b63 2024-03-20 op s = dial(hostname, port);
431 39910b63 2024-03-20 op #ifndef PROFILE
432 39910b63 2024-03-20 op if (pledge("stdio", NULL) == -1)
433 39910b63 2024-03-20 op err(1, "pledge");
436 39910b63 2024-03-20 op send_email(s, default_fromaddr, fromaddr, recipient, replytoaddr,
439 ba97b2d7 2024-03-20 stsp free(default_fromaddr);