-static void
-deltmp(void)
-{
- struct stritem *t;
-
- SLIST_FOREACH(t, &tmpfs, next) {
- unlink(t->str);
- }
-}
-
-int
-open_locked(const char *fname, int flags, ...)
-{
- int mode = 0;
-
- if (flags & O_CREAT) {
- va_list ap;
- va_start(ap, flags);
- mode = va_arg(ap, int);
- va_end(ap);
- }
-
-#ifndef O_EXLOCK
- int fd, save_errno;
-
- fd = open(fname, flags, mode);
- if (fd < 0)
- return(fd);
- if (flock(fd, LOCK_EX|((flags & O_NONBLOCK)? LOCK_NB: 0)) < 0) {
- save_errno = errno;
- close(fd);
- errno = save_errno;
- return(-1);
- }
- return(fd);
-#else
- return(open(fname, flags|O_EXLOCK, mode));
-#endif
-}
-
-static char *
-rfc822date(void)
-{
- static char str[50];
- size_t error;
- time_t now;
-
- now = time(NULL);
- error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
- localtime(&now));
- if (error == 0)
- strcpy(str, "(date fail)");
- return (str);
-}
-
-static int
-strprefixcmp(const char *str, const char *prefix)
-{
- return (strncasecmp(str, prefix, strlen(prefix)));
-}
-
-static int
-readmail(struct queue *queue, const char *sender, int nodot)
-{
- char line[1000]; /* by RFC2822 */
- size_t linelen;
- int error;
- int had_headers = 0;
- int had_from = 0;
- int had_messagid = 0;
- int had_date = 0;
-
- error = snprintf(line, sizeof(line),
- "Received: from %s (uid %d)\n"
- "\t(envelope-from %s)\n"
- "\tid %s\n"
- "\tby %s (%s)\n"
- "\t%s\n",
- username, uid,
- sender,
- queue->id,
- hostname(), VERSION,
- rfc822date());
- if (error < 0 || (size_t)error >= sizeof(line))
- return (-1);
- if (write(queue->mailfd, line, error) != error)
- return (-1);
-
- while (!feof(stdin)) {
- if (fgets(line, sizeof(line), stdin) == NULL)
- break;
- linelen = strlen(line);
- if (linelen == 0 || line[linelen - 1] != '\n') {
- errno = EINVAL; /* XXX mark permanent errors */
- return (-1);
- }
- if (!had_headers) {
- if (strprefixcmp(line, "Date:") == 0)
- had_date = 1;
- else if (strprefixcmp(line, "Message-Id:") == 0)
- had_messagid = 1;
- else if (strprefixcmp(line, "From:") == 0)
- had_from = 1;
- }
- if (strcmp(line, "\n") == 0 && !had_headers) {
- had_headers = 1;
- while (!had_date || !had_messagid || !had_from) {
- if (!had_date) {
- had_date = 1;
- snprintf(line, sizeof(line), "Date: %s\n", rfc822date());
- } else if (!had_messagid) {
- /* XXX better msgid, assign earlier and log? */
- had_messagid = 1;
- snprintf(line, sizeof(line), "Message-Id: <%s@%s>\n",
- queue->id, hostname());
- } else if (!had_from) {
- had_from = 1;
- snprintf(line, sizeof(line), "From: <%s>\n", sender);
- }
- if ((size_t)write(queue->mailfd, line, strlen(line)) != strlen(line))
- return (-1);
- }
- strcpy(line, "\n");
- }
- if (!nodot && linelen == 2 && line[0] == '.')
- break;
- if ((size_t)write(queue->mailfd, line, linelen) != linelen)
- return (-1);
- }
- if (fsync(queue->mailfd) != 0)
- return (-1);
-
- syslog(LOG_INFO, "new mail from user=%s uid=%d envelope_from=<%s>",
- username, uid, sender);
-
- return (0);
-}
-