1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// nullmailer -- a simple relay-only MTA
// Copyright (C) 2012 Bruce Guenter <bruce@untroubled.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// You can contact me at <bruce@untroubled.org>. There is also a mailing list
// available to discuss this package. To subscribe, send an email to
// <nullmailer-subscribe@lists.untroubled.org>.
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "itoa.h"
#include "defines.h"
#include "mystring/mystring.h"
#include "fdbuf/fdbuf.h"
#include "configio.h"
#include "hostname.h"
#define fail(MSG) do{ fout << "nullmailer-queue: " << MSG << endl; return false; }while(0)
pid_t pid = getpid();
uid_t uid = getuid();
time_t timesecs = time(0);
mystring adminaddr;
bool remapadmin = false;
bool is_dir(const char* path)
{
struct stat buf;
return !stat(path, &buf) && S_ISDIR(buf.st_mode);
}
bool is_exist(const char* path)
{
struct stat buf;
return !stat(path, &buf);
}
int fsyncdir(const char* path)
{
int fd = open(path, O_RDONLY);
if(fd == -1)
return 0;
int result = fsync(fd);
if(result == -1 && errno != EIO)
result = 0;
close(fd);
return result;
}
void trigger()
{
int fd = open(QUEUE_TRIGGER, O_WRONLY|O_NONBLOCK, 0666);
if(fd == -1)
return;
char x = 0;
write(fd, &x, 1);
close(fd);
}
bool validate_addr(mystring& addr, bool doremap)
{
int i = addr.find_last('@');
if(i < 0)
return false;
mystring hostname = addr.right(i+1);
if(doremap && remapadmin) {
if(hostname == me || hostname == "localhost")
addr = adminaddr;
}
// else if(hostname.find_first('.') < 0)
// return false;
return true;
}
bool copyenv(fdobuf& out)
{
mystring str;
if(!fin.getline(str))
fail("Could not read envelope sender.");
if(!validate_addr(str, false) && str != "")
fail("Envelope sender address is invalid.");
if(!(out << str << endl))
fail("Could not write envelope sender.");
unsigned count=0;
while(fin.getline(str) && !!str) {
if(!validate_addr(str, true))
fail("Envelope recipient address is invalid.");
if(!(out << str << endl))
fail("Could not write envelope recipient.");
++count;
}
if(count == 0)
fail("No envelope recipients read.");
if(!(out << "\n"))
fail("Could not write extra blank line to destination.");
return true;
}
bool makereceived(fdobuf& out)
{
mystring line("Received: (nullmailer pid ");
line += itoa(pid);
line += " invoked by uid ";
line += itoa(uid);
line += ");\n\t";
char buf[100];
if(!strftime(buf, 100, "%a, %d %b %Y %H:%M:%S -0000\n", gmtime(×ecs)))
fail("Error generating a date string.");
line += buf;
if(!(out << line))
fail("Could not write received line to message.");
return true;
}
bool dump(int fd)
{
fdobuf out(fd);
if(!copyenv(out))
return false;
if(!makereceived(out))
return false;
if(!fdbuf_copy(fin, out))
fail("Error copying the message to the queue file.");
if(!out.sync())
fail("Error flushing the output file.");
if(!out.close())
fail("Error closing the output file.");
return true;
}
bool deliver()
{
if(!is_dir(QUEUE_MSG_DIR) || !is_dir(QUEUE_TMP_DIR))
fail("Installation error: queue directory is invalid.");
// Notes:
// - temporary file name is unique to the currently running
// nullmailer-queue program
// - destination file name is unique to the system
// - if the temporary file previously existed, it did so because
// the previous nullmailer-queue process crashed, and it can be
// safely overwritten
const mystring pidstr = itoa(pid);
const mystring timestr = itoa(timesecs);
const mystring tmpfile = QUEUE_TMP_DIR + pidstr;
const mystring newfile = QUEUE_MSG_DIR + timestr + "." + pidstr;
int out = open(tmpfile.c_str(), O_CREAT|O_WRONLY|O_TRUNC, 0600);
if(out < 0)
fail("Could not open temporary file for writing");
if(!dump(out)) {
unlink(tmpfile.c_str());
return false;
}
if(link(tmpfile.c_str(), newfile.c_str()))
fail("Error linking the temp file to the new file.");
if(fsyncdir(QUEUE_MSG_DIR))
fail("Error syncing the new directory.");
if(unlink(tmpfile.c_str()))
fail("Error unlinking the temp file.");
return true;
}
int main(int, char*[])
{
umask(077);
if(config_read("adminaddr", adminaddr) && !!adminaddr) {
adminaddr = adminaddr.subst(',', '\n');
remapadmin = true;
read_hostnames();
}
if(!deliver())
return 1;
trigger();
return 0;
}
|