2 * Copyright (c) 2024 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/socket.h>
33 #include "got_error.h"
35 #include "got_lib_poll.h"
37 #define SMTP_LINE_MAX 65535
39 static int smtp_timeout = 60; /* in seconds */
40 static char smtp_buf[SMTP_LINE_MAX];
41 static size_t smtp_buflen;
46 fprintf(stderr, "usage: %s [-f sender] [-r responder] "
47 "[-s subject] [-h hostname] [-p port] recipient\n", getprogname());
52 dial(const char *host, const char *port)
54 struct addrinfo hints, *res, *res0;
55 const char *cause = NULL;
56 int s, error, save_errno;
58 memset(&hints, 0, sizeof(hints));
59 hints.ai_family = AF_UNSPEC;
60 hints.ai_socktype = SOCK_STREAM;
61 error = getaddrinfo(host, port, &hints, &res0);
63 errx(1, "failed to resolve %s:%s: %s", host, port,
67 for (res = res0; res; res = res->ai_next) {
68 s = socket(res->ai_family, res->ai_socktype,
75 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
94 set_default_fromaddr(void)
96 struct passwd *pw = NULL;
100 pw = getpwuid(getuid());
102 errx(1, "my UID %d was not found in password database",
106 if (gethostname(hostname, sizeof(hostname)) == -1)
107 err(1, "gethostname");
109 if (asprintf(&s, "%s@%s", pw->pw_name, hostname) == -1)
116 read_smtp_code(int s, const char *code)
118 const struct got_error *error;
124 endl = memmem(smtp_buf, smtp_buflen, "\r\n", 2);
128 if (smtp_buflen == sizeof(smtp_buf))
129 errx(1, "line too long");
131 error = got_poll_fd(s, POLLIN, smtp_timeout);
133 errx(1, "poll: %s", error->msg);
135 r = read(s, smtp_buf + smtp_buflen,
136 sizeof(smtp_buf) - smtp_buflen);
140 errx(1, "unexpected EOF");
144 linelen = endl - smtp_buf;
146 errx(1, "invalid SMTP response");
148 if (strncmp(code, smtp_buf, 3) != 0) {
150 warnx("unexpected SMTP message code: %s", smtp_buf);
155 * Normally we would get just one reply, but the regress doesn't
156 * use a real SMTP server and queues all the replies upfront.
159 memmove(smtp_buf, smtp_buf + linelen, smtp_buflen - linelen);
160 smtp_buflen -= linelen;
166 send_smtp_msg(int s, const char *fmt, ...)
168 const struct got_error *error;
174 len = vsnprintf(buf, sizeof(buf), fmt, ap);
180 if (len >= sizeof(buf)) {
181 warnx("%s: buffer too small for message '%s...'",
186 error = got_poll_write_full(s, buf, len);
188 warnx("write: %s", error->msg);
196 get_datestr(time_t *time, char *datebuf)
201 tm = gmtime_r(time, &mytm);
204 s = asctime_r(tm, datebuf);
214 send_email(int s, const char *myfromaddr, const char *fromaddr,
215 const char *recipient, const char *replytoaddr,
218 const struct got_error *error;
227 datestr = get_datestr(&now, datebuf);
229 if (read_smtp_code(s, "220"))
230 errx(1, "unexpected SMTP greeting received");
232 if (send_smtp_msg(s, "HELO localhost\r\n"))
233 errx(1, "could not send HELO");
234 if (read_smtp_code(s, "250"))
235 errx(1, "unexpected SMTP response received");
237 if (send_smtp_msg(s, "MAIL FROM:<%s>\r\n", myfromaddr))
238 errx(1, "could not send MAIL FROM");
239 if (read_smtp_code(s, "250"))
240 errx(1, "unexpected SMTP response received");
242 if (send_smtp_msg(s, "RCPT TO:<%s>\r\n", recipient))
243 errx(1, "could not send MAIL FROM");
244 if (read_smtp_code(s, "250"))
245 errx(1, "unexpected SMTP response received");
247 if (send_smtp_msg(s, "DATA\r\n"))
248 errx(1, "could not send MAIL FROM");
249 if (read_smtp_code(s, "354"))
250 errx(1, "unexpected SMTP response received");
252 if (send_smtp_msg(s, "From: %s\r\n", fromaddr))
253 errx(1, "could not send From header");
254 if (send_smtp_msg(s, "To: %s\r\n", recipient))
255 errx(1, "could not send To header");
257 if (send_smtp_msg(s, "Reply-To: %s\r\n", replytoaddr))
258 errx(1, "could not send Reply-To header");
260 if (send_smtp_msg(s, "Date: %s +0000 (UTC)\r\n", datestr))
261 errx(1, "could not send Date header");
263 if (send_smtp_msg(s, "Subject: %s\r\n", subject))
264 errx(1, "could not send Subject header");
266 if (send_smtp_msg(s, "\r\n"))
267 errx(1, "could not send body delimiter");
269 while ((linelen = getline(&line, &linesize, stdin)) != -1) {
270 if (line[0] == '.') { /* dot stuffing */
271 error = got_poll_write_full(s, ".", 1);
273 errx(1, "write: %s", error->msg);
275 error = got_poll_write_full(s, line, linelen);
277 errx(1, "write: %s", error->msg);
280 if (send_smtp_msg(s, "\r\n.\r\n"))
281 errx(1, "could not send data terminator");
282 if (read_smtp_code(s, "250"))
283 errx(1, "unexpected SMTP response received");
285 if (send_smtp_msg(s, "QUIT\r\n"))
286 errx(1, "could not send QUIT");
288 if (read_smtp_code(s, "221"))
289 errx(1, "unexpected SMTP response received");
296 main(int argc, char *argv[])
298 char *default_fromaddr = NULL;
299 const char *fromaddr = NULL, *recipient = NULL, *replytoaddr = NULL;
300 const char *subject = "gotd notification";
301 const char *hostname = "127.0.0.1";
302 const char *port = "25";
307 while ((ch = getopt(argc, argv, "f:r:s:h:p:")) != -1) {
319 replytoaddr = optarg;
337 /* used by the regression test suite */
338 timeoutstr = getenv("GOT_NOTIFY_EMAIL_TIMEOUT");
340 smtp_timeout = strtonum(timeoutstr, 0, 600, &errstr);
342 errx(1, "timeout in seconds is %s: %s",
347 if (pledge("stdio dns inet getpw", NULL) == -1)
350 default_fromaddr = set_default_fromaddr();
353 if (pledge("stdio dns inet", NULL) == -1)
358 if (fromaddr == NULL)
359 fromaddr = default_fromaddr;
361 s = dial(hostname, port);
364 if (pledge("stdio", NULL) == -1)
368 send_email(s, default_fromaddr, fromaddr, recipient, replytoaddr,
371 free(default_fromaddr);