#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "signal.h"
#include "getline.h"
#include "stralloc.h"
#include "substdio.h"
#include "subfd.h"
#include "scan.h"
#include "case.h"
#include "error.h"
#include "conf-home.h"
#include "control.h"
#include "dns.h"
#include "alloc.h"
#include "quote.h"
#include "ip.h"
#include "ipalloc.h"
#include "ipme.h"
#include "gen_alloc.h"
#include "gen_allocdefs.h"
#include "str.h"
#include "now.h"
#include "exit.h"
#include "tcpto.h"

#define HUGESMTPTEXT 5000

GEN_ALLOC_typedef(saa,stralloc,sa,len,a)
GEN_ALLOC_readyplus(saa,stralloc,sa,len,a,i,n,x,10,saa_readyplus)
static stralloc sauninit = {0};

stralloc helohost = {0};
stralloc localnet = {0};
int localnetok;
stralloc smarthost = {0};
int smarthostok;
stralloc host = {0};
stralloc sender = {0};

saa reciplist = {0};

struct ip_address partner;

void out(s) char *s; { if (substdio_puts(subfdout,s) == -1) _exit(0); }
void zero() { if (substdio_put(subfdout,"\0",1) == -1) _exit(0); }
void zerodie() { zero(); substdio_flush(subfdout); _exit(0); }
void temp_nomem() { out("ZOut of memory. (#4.3.0)\n"); zerodie(); }
void temp_oserr() { out("Z\
System resources temporarily unavailable. (#4.3.0)\n"); zerodie(); }
void temp_noconn() { out("Z\
Sorry, I wasn't able to establish an SMTP connection. (#4.4.1)\n"); zerodie(); }
void temp_read() { out("ZUnable to read message. (#4.3.0)\n"); zerodie(); }
void temp_dnscanon() { out("Z\
Temporarily unable to canonicalize address. (#4.4.3)\n"); zerodie(); }
void temp_dns() { out("Z\
Sorry, I couldn't find any host by that name. (#4.1.2)\n"); zerodie(); }
void temp_chdir() { out("Z\
Unable to switch to home directory. (#4.3.0)\n"); zerodie(); }
void temp_control() { out("Z\
Unable to read control files. (#4.3.0)\n"); zerodie(); }
void perm_usage() { out("D\
I (qmail-remote) was invoked improperly. (#5.3.5)\n"); zerodie(); }
void perm_dns() { out("D\
Sorry, I couldn't find any host by that name. (#5.1.2)\n"); zerodie(); }
void perm_nomx() { out("D\
Sorry, I couldn't find a mail exchanger or IP address. (#5.4.4)\n");
zerodie(); }
void perm_ambigmx() { out("D\
Sorry. Although I'm listed as a best-preference MX or A for that host,\n\
it isn't in my control/locals file, so I don't treat it as local. (#5.4.6)\n");
zerodie(); }

int timeout = 1200;

void getcontrols()
{
 int r;
 if (control_init() == -1)
  { if (errno == error_nomem) temp_nomem(); temp_control(); }

 if (control_readint(&timeout,"control/timeoutremote") == -1)
  { if (errno == error_nomem) temp_nomem(); temp_control(); }

 smarthostok = control_readline(&smarthost,"control/smarthost");
 if (smarthostok == -1)
  { if (errno == error_nomem) temp_nomem(); temp_control(); }

 localnetok = control_readline(&localnet,"control/localnet");
 if (localnetok == -1)
  { if (errno == error_nomem) temp_nomem(); temp_control(); }

 r = control_rldef(&helohost,"control/helohost",1,(char *) 0);
 if (r == -1) if (errno == error_nomem) temp_nomem();
 if (r != 1) temp_control();
}

char smtptobuf[1024];
char smtpfrombuf[128];
stralloc smtpline = {0};
stralloc smtptext = {0};

void outsmtptext()
{
 int i; 
 if (smtptext.s) if (smtptext.len) if (smtptext.len < HUGESMTPTEXT)
  {
   if (substdio_puts(subfdout,"Remote host said: ") == -1) _exit(0);
   for (i = 0;i < smtptext.len;++i)
     if (!smtptext.s[i]) smtptext.s[i] = '?';
   if (substdio_put(subfdout,smtptext.s,smtptext.len) == -1) _exit(0);
  }
}

unsigned long smtpcode(ss)
substdio *ss;
{
 int match;
 unsigned long code;

 if (!stralloc_copys(&smtptext,"")) return 421;
 do
  {
   if (getline2(ss,&smtpline,&match,'\n') != 0) return 421;
   if (!match) return 421;
   if ((smtpline.len >= 2) && (smtpline.s[smtpline.len - 2] == '\r'))
    {
     smtpline.s[smtpline.len - 2] = '\n';
     --smtpline.len;
    }
   if (!stralloc_cat(&smtptext,&smtpline)) return 421;
   if (scan_nbblong(smtpline.s,smtpline.len,10,0,&code) != 3) return 421;
   if (smtpline.len == 3) return code;
  }
 while (smtpline.s[3] == '-');

 return code;
}

void outhost()
{
 char x[IPFMT];

 x[ip_fmt(x,&partner)] = 0;
 out(x);
}

void writeerr()
{
 out("ZConnected to "); outhost();
 out(" but communications failed. (#4.4.2)\n");
 zerodie();
}

void quit(ssto,ssfrom)
substdio *ssto;
substdio *ssfrom;
{
 outsmtptext();
 if (substdio_putsflush(ssto,"QUIT\r\n") != -1)
   smtpcode(ssfrom); /* protocol design stupidity */
 zerodie();
}

stralloc dataline = {0};

void blast(ssto,ssfrom)
substdio *ssto;
substdio *ssfrom;
{
 int match;

 for (;;)
  {
   if (getline2(ssfrom,&dataline,&match,'\n') != 0) temp_read();
   if (!match && !dataline.len) break;
   if (match) --dataline.len; /* no way to pass this info over SMTP */
   if (dataline.len && (dataline.s[0] == '.'))
     substdio_put(ssto,".",1);
   if (substdio_put(ssto,dataline.s,dataline.len) == -1) writeerr();
   if (substdio_put(ssto,"\r\n",2) == -1) writeerr();
   if (!match) break;
  }
 if (substdio_put(ssto,".\r\n",3) == -1) writeerr();
 if (substdio_flush(ssto) == -1) writeerr();
}

int flagtimedout = 0;
void sigalrm()
{
 flagtimedout = 1;
}
int timeoutread(fd,buf,n) int fd; char *buf; int n;
{
 int r; int saveerrno;
 if (flagtimedout) { errno = error_io; return -1; }
 alarm(timeout);
 r = read(fd,buf,n); saveerrno = errno;
 alarm(0);
 if (flagtimedout) { errno = error_io; return -1; }
 errno = saveerrno;
 return r;
}
int timeoutwrite(fd,buf,n) int fd; char *buf; int n;
{
 int r; int saveerrno;
 if (flagtimedout) { errno = error_io; return -1; }
 alarm(timeout);
 r = write(fd,buf,n); saveerrno = errno;
 alarm(0);
 if (flagtimedout) { errno = error_io; return -1; }
 errno = saveerrno;
 return r;
}

stralloc recip = {0};

void smtp(fd)
int fd;
{
 substdio ssto;
 substdio ssfrom;
 unsigned long code;
 int i;

 substdio_fdbuf(&ssto,timeoutwrite,fd,smtptobuf,sizeof(smtptobuf));
 substdio_fdbuf(&ssfrom,timeoutread,fd,smtpfrombuf,sizeof(smtpfrombuf));

 if (smtpcode(&ssfrom) != 220)
  {
   out("ZConnected to "); outhost(); out(" but greeting failed.\n");
   quit(&ssto,&ssfrom);
  }

 if (substdio_puts(&ssto,"HELO ") == -1) writeerr();
 if (substdio_put(&ssto,helohost.s,helohost.len) == -1) writeerr();
 if (substdio_puts(&ssto,"\r\n") == -1) writeerr();
 if (substdio_flush(&ssto) == -1) writeerr();

 if (smtpcode(&ssfrom) != 250)
  {
   out("ZConnected to "); outhost(); out(" but my name was rejected.\n");
   quit(&ssto,&ssfrom);
  }

 if (substdio_puts(&ssto,"MAIL FROM:<") == -1) writeerr();
 if (substdio_put(&ssto,sender.s,sender.len) == -1) writeerr();
 if (substdio_puts(&ssto,">\r\n") == -1) writeerr();
 if (substdio_flush(&ssto) == -1) writeerr();

 code = smtpcode(&ssfrom);
 if (code >= 500)
  {
   out("DConnected to "); outhost(); out(" but sender was rejected.\n");
   quit(&ssto,&ssfrom);
  }
 if (code >= 400)
  {
   out("ZConnected to "); outhost(); out(" but sender was rejected.\n");
   quit(&ssto,&ssfrom);
  }

 for (i = 0;i < reciplist.len;++i)
  {
   if (substdio_puts(&ssto,"RCPT TO:<") == -1) writeerr();
   if (substdio_put(&ssto,reciplist.sa[i].s,reciplist.sa[i].len) == -1) writeerr();
   if (substdio_puts(&ssto,">\r\n") == -1) writeerr();
   if (substdio_flush(&ssto) == -1) writeerr();

   code = smtpcode(&ssfrom);
   if (code == 421)
    {
     out("ZConnected to "); outhost(); out(" but connection died.\n");
     quit(&ssto,&ssfrom);
    }
   if (code >= 500)
    {
     out("h"); outhost(); out(" does not like recipient.\n");
     outsmtptext(); zero();
    }
   else if (code >= 400)
    {
     out("s"); outhost(); out(" does not like recipient.\n");
     outsmtptext(); zero();
    }
   else
    {
     out("r"); zero();
    }
  }

 if (substdio_putsflush(&ssto,"DATA\r\n") == -1) writeerr();

 code = smtpcode(&ssfrom);
 if (code == 421)
  {
   out("ZConnected to "); outhost(); out(" but connection died.\n");
   quit(&ssto,&ssfrom);
  }
 if (code >= 500)
  {
   out("D"); outhost(); out(" failed on DATA command.\n");
   quit(&ssto,&ssfrom);
  }
 if (code >= 400)
  {
   out("Z"); outhost(); out(" failed on DATA command.\n");
   quit(&ssto,&ssfrom);
  }

 blast(&ssto,subfdin);

 code = smtpcode(&ssfrom);
 if (code == 421)
  {
   out("ZConnected to "); outhost(); out(" but connection died.\n");
   quit(&ssto,&ssfrom);
  }
 if (code >= 500)
  {
   out("D"); outhost(); out(" failed after I sent the message.\n");
   quit(&ssto,&ssfrom);
  }
 if (code >= 400)
  {
   out("Z"); outhost(); out(" failed after I sent the message.\n");
   quit(&ssto,&ssfrom);
  }

 out("K");
 quit(&ssto,&ssfrom);
}

stralloc canonhost = {0};
stralloc canonbox = {0};

void addrmangle(saout,s,flagalias)
stralloc *saout; /* host has to be canonical, box has to be quoted */
char *s;
int *flagalias;
{
 int j;

 *flagalias = 1;

 j = str_rchr(s,'@');
 if (!s[j])
  {
   if (!stralloc_copys(saout,s)) temp_nomem();
   return;
  }
 if (!stralloc_copys(&canonbox,s)) temp_nomem();
 canonbox.len = j;
 if (!quote(saout,&canonbox)) temp_nomem();
 if (!stralloc_cats(saout,"@")) temp_nomem();

 if (!stralloc_copys(&canonhost,s + j + 1)) temp_nomem();
 if (!stralloc_0(&canonhost)) temp_nomem();
 switch(dns_cname(&canonhost))
  {
   case 0: *flagalias = 0; break;
   case DNS_MEM: temp_nomem();
   case DNS_SOFT: temp_dnscanon();
   case DNS_HARD: ; /* alias loop, not our problem */
  }

 if (!stralloc_0(&canonhost)) temp_nomem();
 if (!stralloc_cats(saout,canonhost.s)) temp_nomem();
}

void main(argc,argv)
int argc;
char **argv;
{
 static ipalloc ip = {0};
 int i;
 unsigned long random;
 char **recips;
 unsigned long prefme;
 int flagallaliases;
 int flagalias;

 signal_init();
 signal_catchalarm(sigalrm);
 if (argc < 4) perm_usage();
 if (chdir(CONF_HOME) == -1) temp_chdir();
 getcontrols();

 addrmangle(&sender,argv[2],&flagalias);

 if (!saa_readyplus(&reciplist,0)) temp_nomem();
 if (!ipme_init()) temp_nomem();

 flagallaliases = 1;
 recips = argv + 3;
 while (*recips)
  {
   if (!saa_readyplus(&reciplist,1)) temp_nomem();
   reciplist.sa[reciplist.len] = sauninit;
   addrmangle(reciplist.sa + reciplist.len,*recips,&flagalias);
   if (!flagalias) flagallaliases = 0;
   ++reciplist.len;
   ++recips;
  }

 if (!stralloc_copys(&host,argv[1])) temp_nomem();
 if (localnetok)
  {
   if (host.len == localnet.len)
     if (!case_diffb(host.s,localnet.s,localnet.len))
       smarthostok = 0;
   if (host.len > localnet.len)
     if (host.s[host.len - localnet.len - 1] == '.')
       if (!case_diffb(host.s+host.len - localnet.len,localnet.s,localnet.len))
	 smarthostok = 0;
  }
 if (!stralloc_0(&host)) temp_nomem();

 random = now() + (getpid() << 16);
 switch (smarthostok ? dns_ip(&ip,&smarthost) : dns_mxip(&ip,&host,random))
  {
   case DNS_MEM: temp_nomem();
   case DNS_SOFT: temp_dns();
   case DNS_HARD: perm_dns();
   case 1:
     if (ip.len <= 0) temp_dns();
  }

 if (ip.len <= 0) perm_nomx();

 prefme = 100000;
 for (i = 0;i < ip.len;++i)
   if (ipme_is(&ip.ix[i].ip))
     if (ip.ix[i].pref < prefme)
       prefme = ip.ix[i].pref;

 if (smarthostok) prefme = 300000;
 if (flagallaliases) prefme = 500000;

 for (i = 0;i < ip.len;++i)
   if (ip.ix[i].pref < prefme)
     break;

 if (i >= ip.len)
   perm_ambigmx();

 for (i = 0;i < ip.len;++i) if (ip.ix[i].pref < prefme)
  {
   struct sockaddr_in sin;
   int s;
   char *x;

   if (tcpto(&ip.ix[i].ip)) continue;

   for (x = (char *) &sin;x < (char *) &sin + sizeof(sin);++x) *x = 0;
   x = (char *) &sin.sin_addr;
   x[0] = ip.ix[i].ip.d[0]; x[1] = ip.ix[i].ip.d[1];
   x[2] = ip.ix[i].ip.d[2]; x[3] = ip.ix[i].ip.d[3];
   x = (char *) &sin.sin_port;
   x[0] = 0; x[1] = 25; /* silly rabbit, /etc/services is for users */
   sin.sin_family = AF_INET;

   s = socket(AF_INET,SOCK_STREAM,0);
   if (s == -1) temp_oserr();

   if (connect(s,(struct sockaddr *) &sin,sizeof(sin)) == 0)
    {
     tcpto_err(&ip.ix[i].ip,0);
     partner = ip.ix[i].ip;
     smtp(s); /* does not return */
    }
   tcpto_err(&ip.ix[i].ip,(errno == error_timeout));
   close(s);
  }
 
 temp_noconn();
}
