home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / MAILBOX.C < prev    next >
C/C++ Source or Header  |  1991-06-18  |  45KB  |  1,800 lines

  1. /* There are only two functions in this mailbox code that depend on the
  2.  * underlying protocol, namely mbx_getname() and dochat(). All the other
  3.  * functions can hopefully be used without modification on other stream
  4.  * oriented protocols than AX.25 or NET/ROM.
  5.  *
  6.  * SM0RGV 890506, most work done previously by W9NK
  7.  *
  8.  *** Changed 900114 by KA9Q to use newline mapping features in stream socket
  9.  *    interface code; everything here uses C eol convention (\n)
  10.  *
  11.  *    Numerous new commands and other changes by SM0RGV, 900120
  12.  */
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <ctype.h>
  16. #ifdef    UNIX
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #endif
  20.  
  21. #include "global.h"
  22. #include "config.h"
  23. #include "timer.h"
  24. #include "proc.h"
  25. #include "socket.h"
  26. #include "usock.h"
  27. #include "session.h"
  28. #include "smtp.h"
  29. #include "dirutil.h"
  30. #include "telnet.h"
  31. #include "ftp.h"
  32. #include "ftpserv.h"
  33. #include "commands.h"
  34. #include "netuser.h"
  35. #include "files.h"
  36. #include "bm.h"
  37. #include "mailbox.h"
  38. #include "ax25mail.h"
  39. #include "nr4mail.h"
  40. #include "cmdparse.h"
  41.  
  42. /*
  43. #define MBDEBUG
  44. */
  45.  
  46. struct mbx *Mbox[NUMMBX];
  47. static char *Motd = NULLCHAR;
  48. static int Attended = TRUE;    /* default to attended mode */
  49. unsigned Maxlet = BM_NLET;
  50.  
  51. char Noperm[] = "Permission denied.\n";
  52. char Nosock[] = "Can't create socket\n";
  53.  
  54. static char Mbbanner[] = "[NET-H$]\nWelcome %s to the %s TCP/IP Mailbox (%s)\n%s";
  55. static char Mbmenu[] = "Current msg# %d : A,B,C,D,E,F,G,H,I,J,K,L,N,R,S,T,U,V,W,Z,? >\n";
  56. static char Longmenu1[] = "(?)help    (A)rea     (B)ye      (C)hat     (D)ownload (E)scape   (F)inger\n";
  57. static char Longmenu2[] = "(G)ateway  (H)elp     (I)nfo     (J)heard   (K)ill     (L)ist     (N)etrom\n";
  58. static char Longmenu3[] = "(R)ead     (S)end     (T)elnet   (U)pload   (V)erbose  (W)hat     (Z)ap\n";
  59. static char Loginbanner[] = "\nKA9Q NOS (%s)\n\n";
  60. static char Howtoend[] = "Terminate with /EX or ^Z in first column (^A aborts):\n";
  61.  
  62. static int doarea __ARGS((int argc,char *argv[],void *p));
  63. static int mbx_getname __ARGS((struct mbx *m));
  64.  
  65. /************************************************************************/
  66. /*            C O M M A N D S                    */
  67. /************************************************************************/
  68.  
  69. static int doattend __ARGS((int argc,char *argv[],void *p));
  70. static int domaxmsg __ARGS((int argc,char *argv[],void *p));
  71. static int domotd __ARGS((int argc,char *argv[],void *p));
  72. static int dotimeout __ARGS((int argc,char *argv[],void *p));
  73.  
  74. /* mbox subcommand table */
  75. static struct cmds Mbtab[] = {
  76.     "attend",    doattend,    0, 0, NULLCHAR,
  77. #ifdef    AX25
  78.     "kick",        dombkick,    0, 0, NULLCHAR,
  79. #endif
  80.     "maxmsg",    domaxmsg,    0, 0, NULLCHAR,
  81.     "motd",        domotd,        0, 0, NULLCHAR,
  82.     "status",    domboxdisplay,    0, 0, NULLCHAR,
  83. #ifdef    AX25
  84.     "timer",    dombtimer,    0, 0, NULLCHAR,
  85. #endif
  86.     "tiptimeout",    dotimeout,    0, 0, NULLCHAR,
  87.     NULLCHAR,
  88. };
  89.  
  90.  
  91. int
  92. dombox(argc,argv,p)
  93. int argc;
  94. char *argv[];
  95. void *p;
  96. {
  97.     if(argc == 1)
  98.         return domboxdisplay(argc,argv,p);
  99.     return subcmd(Mbtab,argc,argv,p);
  100. }
  101.  
  102. /* if unattended mode is set, ax25, telnet and maybe other sessions will
  103.  * be restricted.
  104.  */
  105. static int
  106. doattend(argc,argv,p)
  107. int argc;
  108. char *argv[];
  109. void *p;
  110. {
  111.     return setbool(&Attended,"Attended flag",argc,argv);
  112. }
  113.  
  114. static int
  115. domaxmsg(argc,argv,p)
  116. int argc;
  117. char *argv[];
  118. void *p;
  119. {
  120.     return setuns(&Maxlet,"Maximum messages per area",argc,argv);
  121. }
  122.  
  123. static int
  124. domotd(argc,argv,p)
  125. int argc;
  126. char *argv[];
  127. void *p;
  128. {
  129.     if(argc > 2) {
  130.         tprintf("Usage: mbox motd \"<your message>\"\n");
  131.         return 0;
  132.     }
  133.  
  134.     if(argc < 2) {
  135.         if(Motd != NULLCHAR)
  136.             tputs(Motd);
  137.     }
  138.     else {
  139.         if(Motd != NULLCHAR){
  140.             free(Motd);
  141.             Motd = NULLCHAR;    /* reset the pointer */
  142.         }
  143.         if(!strlen(argv[1]))
  144.             return 0;        /* clearing the buffer */
  145.         Motd = mallocw(strlen(argv[1])+5);/* allow for the EOL char */
  146.         strcpy(Motd, argv[1]);
  147.         strcat(Motd, "\n");        /* add the EOL char */
  148.     }
  149.     return 0;
  150. }
  151.  
  152. int
  153. domboxdisplay(argc,argv,p)
  154. int argc;
  155. char *argv[];
  156. void *p;
  157. {
  158.     int i, j, len;
  159.     struct mbx *m;
  160.     char fsocket[MAXSOCKSIZE];
  161.     static char *states[] = {"LOGIN","CMD","SUBJ","DATA","REVFWD",
  162.                 "TRYING","FORWARD"};
  163.  
  164.     tprintf("User       State    S#  Where\n");
  165.  
  166.     for (i = 0; i < NUMMBX; i++){
  167.         if((m = Mbox[i]) != NULLMBX){
  168.             len = MAXSOCKSIZE;
  169.             j = getpeername(m->user,fsocket,&len);
  170.             if(tprintf("%-11s%-9s%-4u%s\n",m->name,
  171.                    states[m->state],m->user,
  172.                    j != -1 ? psocket(fsocket): "") == EOF)
  173.                 break;
  174.         }
  175.     }
  176.     return 0;
  177. }
  178.  
  179. static int
  180. dotimeout(argc,argv,p)
  181. int argc;
  182. char *argv[];
  183. void *p;
  184. {
  185.     return setuns(&Tiptimeout,"Tip connection timeout",argc,argv);
  186. }
  187.  
  188.  
  189. /**********************************************************************/
  190.  
  191. void
  192. listusers(s)
  193. int s;
  194. {
  195.     int outsave;
  196.  
  197.     usprintf(s,"\nCurrent remote users:\n");
  198.     outsave = Curproc->output;
  199.     Curproc->output = s;
  200.     domboxdisplay(0,NULLCHARP,NULL);
  201.     Curproc->output = outsave;
  202. }
  203.  
  204. struct mbx *
  205. newmbx()
  206. {
  207.     int i;
  208.     struct mbx *m;
  209.  
  210.     for(i = 0; i < NUMMBX; i++){
  211.         if(Mbox[i] == NULLMBX){
  212.             m = Mbox[i] = (struct mbx *)callocw(1,sizeof(struct mbx));
  213.             m->mbnum = i;
  214.             return m;
  215.         }
  216.     }
  217.     /* If we get here, there are no free mailbox sessions */
  218.     return NULLMBX;
  219. }
  220.  
  221. static int
  222. mbx_getname(m)
  223. struct mbx *m;
  224. {
  225. #ifdef    AX25
  226.     char *cp;
  227. #endif
  228.     union sp sp;
  229.     char tmp[MAXSOCKSIZE];
  230.     char buf[MBXLINE];
  231.     int len = MAXSOCKSIZE;
  232.     int anony = 0;
  233.     int oldmode;
  234.  
  235.     sp.p = tmp;
  236.     sp.sa->sa_family = AF_LOCAL;    /* default to AF_LOCAL */
  237.     getpeername(m->user,tmp,&len);
  238.     m->path = mallocw(MBXLINE);
  239.     /* This is one of the two parts of the mbox code that depends on the
  240.      * underlying protocol. We have to figure out the name of the
  241.      * calling station. This is only practical when AX.25 or NET/ROM is
  242.      * used. Telnet users have to identify themselves by a login procedure.
  243.      */
  244.     switch(sp.sa->sa_family){
  245. #ifdef    AX25
  246.     case AF_NETROM:
  247.     case AF_AX25:
  248.         /* NETROM and AX25 socket address structures are "compatible" */
  249.         pax25(m->name,sp.ax->ax25_addr);
  250.         cp = strchr(m->name,'-');
  251.         if(cp != NULLCHAR)            /* get rid of SSID */
  252.             *cp = '\0';
  253.         /* SMTP wants the name to be in lower case */
  254.         cp = m->name;
  255.         while(*cp){
  256.             if(isupper(*cp))
  257.                 *cp = tolower(*cp);
  258.             ++cp;
  259.         }
  260.         anony = 1;
  261.         /* Try to find the privileges of this user from the userfile */
  262.         if((m->privs = userlogin(m->name,buf,&m->path,MBXLINE,&anony)) == -1)
  263.             if((m->privs = userlogin("bbs",buf,&m->path,MBXLINE,&anony)) == -1)
  264.                 if((m->privs = userlogin("anonymous",buf,&m->path,MBXLINE,
  265.                      &anony)) == -1){
  266.                         m->privs = 0;
  267.                         free(m->path);
  268.                         m->path = NULLCHAR;
  269.                 }
  270.         if(m->privs & EXCLUDED_CMD)
  271.             return -1;
  272.         return 0;
  273. #endif
  274.     case AF_LOCAL:
  275.     case AF_INET:
  276.         m->state = MBX_LOGIN;
  277.         tprintf(Loginbanner,Hostname);
  278.         for(;;){
  279.             tputs("login: ");
  280.             usflush(m->user);
  281.             if(mbxrecvline(m->user,m->name,sizeof(m->name),-1) == EOF)
  282.                 return -1;
  283.             if(*m->name == '\0')
  284.                 continue;
  285.             tprintf("Password: %c%c%c",IAC,WILL,TN_ECHO);
  286.             usflush(m->user);
  287.             oldmode = sockmode(m->user,SOCK_BINARY);
  288.             if(mbxrecvline(m->user,buf,MBXLINE,-1) == EOF)
  289.                 return -1;
  290.             tprintf("%c%c%c",IAC,WONT,TN_ECHO);
  291.             sockmode(m->user,oldmode);
  292.             tputc('\n');
  293.             usflush(m->user);
  294.             /* This is needed if the password was send before the
  295.              * telnet no-echo options were receied. We neeed to
  296.              * flush the eold sequence from the input buffers, sigh
  297.              */
  298.             if(socklen(m->user,0))/* discard any remaining input */
  299.                 recv_mbuf(m->user,NULL,0,NULLCHAR,0);
  300.             if((m->privs = userlogin(m->name,buf,&m->path,MBXLINE,&anony))
  301.              != -1){
  302.                 if(anony)
  303.                     log(m->user,"MBOX login: %s Password: %s",m->name,buf);
  304.                 else
  305.                     log(m->user,"MBOX login: %s",m->name);
  306.                 if(m->privs & EXCLUDED_CMD)
  307.                     return -1;
  308.                 return 0;
  309.             }
  310.             tprintf("Login incorrect\n");
  311.             *m->name = '\0';    /* wipe any garbage */
  312.         }
  313.     }
  314.     return 0;
  315. }
  316.  
  317. /* Incoming mailbox session */
  318. void
  319. mbx_incom(s,t,p)
  320. int s;
  321. void *t;
  322. void *p;
  323. {
  324.     struct mbx *m;
  325.     struct usock *up;
  326.     char *buf[3];
  327.     int rval;
  328.  
  329.     sockmode(s,SOCK_ASCII);
  330.     sockowner(s,Curproc);    /* We own it now */
  331.     /* Secede from the parent's sockets, and use the network socket that
  332.      * was passed to us for both input and output. The reference
  333.      * count on this socket will still be 1; this allows the domboxbye()
  334.      * command to work by closing that socket with a single call.
  335.      * If we return, the socket will be closed automatically.
  336.      */
  337.     close_s(Curproc->output);
  338.     close_s(Curproc->input);
  339.     Curproc->output = Curproc->input = s;
  340.  
  341.     /* We'll do our own flushing right before we read input */
  342.     setflush(s,-1);
  343.  
  344.     log(s,"open MBOX");
  345.     if((m = newmbx()) == NULLMBX){
  346.         tprintf("Too many mailbox sessions\n");
  347.         return;
  348.     }
  349.     m->user = s;
  350.     m->escape = 24;        /* default escape character is Ctrl-X */
  351.     m->type = (int) t;
  352.     /* get the name of the remote station */
  353.     if(mbx_getname(m) == -1) {
  354.         exitbbs(m);
  355.         return;
  356.     }
  357.  
  358.     m->state = MBX_CMD;    /* start in command state */
  359.  
  360.     /* Now say hi */
  361.     tprintf(Mbbanner,m->name,Hostname,Version,
  362.         Motd != NULLCHAR ? Motd : "");
  363.     /* Enable our local message area */
  364.     buf[1] = m->name;
  365.     doarea(2,buf,m);
  366.     tprintf(Mbmenu,m->current);
  367.     while(mbxrecvline(s,m->line,MBXLINE,-1) != EOF){
  368.         if((rval = mbx_parse(m)) == -2)
  369.             break;
  370.         if(rval == 1)
  371.             tprintf("Bad syntax.\n");
  372.         if(!(m->sid & MBX_SID) && isnewprivmail(m) > 0L)
  373.             tprintf("You have new mail.\n");
  374.         scanmail(m);
  375.         tprintf((m->sid & MBX_SID) ? ">\n" : Mbmenu, m->current);
  376.         m->state = MBX_CMD;
  377.     }
  378.     exitbbs(m);
  379.     /* nasty hack! we may have screwed up reference count */
  380.     /* by invoking newproc("smtp_send",....); Fudge it!   */
  381.     if((up = itop(Curproc->output)) != NULLUSOCK)
  382.         up->refcnt = 1;
  383.     close_s(Curproc->output);
  384. }
  385.  
  386. void
  387. exitbbs(m)
  388. struct mbx *m;
  389. {
  390.     closenotes(m);
  391.     free(m->to);
  392.     free(m->tofrom);
  393.     free(m->origto);
  394.     free(m->tomsgid);
  395.     free(m->path);
  396.     free((char *)m->mbox);
  397.     Mbox[m->mbnum] = NULLMBX;
  398.     free((char *)m);
  399. }
  400.  
  401. /**********************************************************************/
  402.  
  403. static int dochat __ARGS((int argc,char *argv[],void *p));
  404. static int dodownload __ARGS((int argc,char *argv[],void *p));
  405. static int dombupload __ARGS((int argc,char *argv[],void *p));
  406. static int dowhat __ARGS((int argc,char *argv[],void *p));
  407. static int dozap __ARGS((int argc,char *argv[],void *p));
  408. static int dosend __ARGS((int argc,char *argv[],void *p));
  409. static int dosid __ARGS((int argc,char *argv[],void *p));
  410. static int dosysop __ARGS((int argc,char *argv[],void *p));
  411. static int dostars __ARGS((int argc,char *argv[],void *p));
  412. static int dombhelp __ARGS((int argc,char *argv[],void *p));
  413. static int dombtelnet __ARGS((int argc,char *argv[],void *p));
  414. static int dombfinger __ARGS((int argc,char *argv[],void *p));
  415. static void gw_alarm __ARGS((void *p));
  416. static void gw_input __ARGS((int s,void *notused,void *p));
  417. static void gw_superv __ARGS((int null,void *proc,void *p));
  418. static int mbx_to __ARGS((int argc,char *argv[],void *p));
  419. static int mbx_data __ARGS((struct mbx *m,struct list *cclist,char *extra));
  420. static int msgidcheck __ARGS((char *string));
  421. static int uuencode __ARGS((FILE *infile,int s,char *infilename));
  422.  
  423. static struct cmds Mbcmds[] = {
  424.     "",        doreadnext,    0, 0, NULLCHAR,
  425.     "area",        doarea,        0, 0, NULLCHAR,
  426.     "send",        dosend,        0, 0, NULLCHAR,
  427.     "read",        doreadmsg,    0, 2, "R numbers",
  428.     "verbose",    doreadmsg,    0, 2, "V numbers",
  429. #ifdef    AX25
  430.     "jheard",    doaxheard,    0, 0, NULLCHAR,
  431. #endif
  432.     "kill",        dodelmsg,    0, 2, "K numbers",
  433.     "list",        dolistnotes,    0, 0, NULLCHAR,
  434.     "escape",    dombescape,    0, 0, NULLCHAR,
  435.     "download",    dodownload,    0, 2, "D[U] filename",
  436.     "upload",    dombupload,    0, 2, "U filename",
  437.     "what",        dowhat,        0, 0, NULLCHAR,
  438.     "zap",        dozap,        0, 2, "Z filename",
  439. #ifdef AX25
  440.     "gateway",    dogateway,    0, 3, "G interface callsigns",
  441. #endif
  442.     "telnet",    dombtelnet,    0, 2, "T hostname",
  443.     "finger",    dombfinger,    0, 0, NULLCHAR,
  444. #ifdef    NETROM
  445.     "netrom",    dombnetrom,    0, 0, NULLCHAR,
  446. #endif
  447.     "chat",        dochat,        0, 0, NULLCHAR,
  448.     "bye",        domboxbye,    0, 0, NULLCHAR,
  449.     "help",        dombhelp,    0, 0, NULLCHAR,
  450.     "info",        dombhelp,    0, 0, NULLCHAR,
  451.     "?",        dombhelp,    0, 0, NULLCHAR,
  452.     "[",        dosid,        0, 0, NULLCHAR,
  453. #ifdef    AX25
  454.     "f>",        dorevfwd,    0, 0, NULLCHAR,
  455. #endif
  456.     "@",        dosysop,    0, 0, NULLCHAR,
  457.     "***",        dostars,    0, 0, NULLCHAR,
  458.     NULLCHAR,    NULLFP,        0, 0, "Huh?",
  459. };
  460.  
  461. /* "twocmds" defines the MBL/RLI two-letter commands, eg. "SB", "SP" and so on.
  462.  * They have to be treated specially since cmdparse() wants a space between
  463.  * the actual command and its arguments.
  464.  * "SP FOO" is converted to "s  foo" and the second command letter is saved
  465.  * in m->stype. Longer commands like "SEND" are unaffected, except for
  466.  * commands starting with "[", i.e. the SID, since we don't know what it will
  467.  * look like.
  468.  */
  469. static char twocmds[] = "slrd[";    /* S,L,R,D are two-letter commands */
  470. int
  471. mbx_parse(m)
  472. struct mbx *m;
  473. {
  474.     char *cp;
  475.     int i;
  476.     char *newargv[2];
  477.     /* Translate entire buffer to lower case */
  478.     for (cp = m->line; *cp != '\0'; ++cp)
  479.         if(isupper(*cp))
  480.             *cp = tolower(*cp);
  481.     /* Skip any spaces at the begining */
  482.     for(cp = m->line;isspace(*cp);++cp)
  483.         ;
  484.     m->stype = ' ';
  485.     if(*cp != '\0' && *(cp+1) != '\0')
  486.     for(i=0; i<strlen(twocmds); ++i){
  487.         if(*cp == twocmds[i] && (isspace(*(cp+2)) || *(cp+2) == '\0'
  488.          || *cp == '[')){
  489.             if(islower(*(++cp)))
  490.                 m->stype = toupper(*cp); /* Save the second character */
  491.             else
  492.                 m->stype = *cp;
  493.             *cp = ' ';
  494.             break;
  495.         }
  496.     }
  497.     /* See if the input line consists solely of digits */
  498.     cp = m->line;
  499.     for(cp = m->line;isspace(*cp);++cp)
  500.         ;
  501.     newargv[1] = cp;
  502.     for(;*cp != '\0' && isdigit(*cp);++cp)
  503.         ;
  504.     if(*cp == '\0' && strlen(newargv[1]) > 0) {
  505.         newargv[0] = "read";
  506.         return doreadmsg(2,newargv,(void *)m);
  507.     }
  508.     else
  509.         return cmdparse(Mbcmds,m->line,(void *)m);
  510. }
  511.  
  512. /* This works like recvline(), but telnet options are answered and the
  513.  * terminating newline character is not put into the buffer. If the
  514.  * incoming character equals the value of escape, any queued input is
  515.  * flushed and -2 returned.
  516.  */
  517. int
  518. mbxrecvline(s,buf,len,escape)
  519. int s;
  520. char *buf;
  521. int len;
  522. int escape;
  523. {
  524.     int c, cnt = 0, opt;
  525.     if(buf == NULLCHAR)
  526.         return 0;
  527.     usflush(Curproc->output);
  528.     while((c = recvchar(s)) != EOF){
  529.         if(c == IAC){        /* Telnet command escape */
  530.             if((c = recvchar(s)) == EOF)
  531.                 break;
  532.             if(c > 250 && c < 255 && (opt = recvchar(s)) != EOF){
  533. #ifdef    foo
  534.                 switch(c){
  535.                 case WILL:
  536.                     tprintf("%c%c%c",IAC,DONT,opt);
  537.                     break;
  538.                 case WONT:
  539.                     tprintf("%c%c%c",IAC,DONT,opt);
  540.                     break;
  541.                 case DO:
  542.                     tprintf("%c%c%c",IAC,WONT,opt);
  543.                     break;
  544.                 case DONT:
  545.                     tprintf("%c%c%c",IAC,WONT,opt);
  546.                 }
  547. #endif
  548. /* to be fixed             usflush(Curproc->output);*/
  549.                 continue;
  550.             }
  551.             if(c != IAC && (c = recvchar(s)) == EOF)
  552.                 break;
  553.         }
  554.         /* ordinary character */
  555.         if(c == '\r' || c == '\n')
  556.             break;
  557.         if(uchar(c) == escape){
  558.             if(socklen(s,0)) /* discard any remaining input */
  559.                 recv_mbuf(s,NULL,0,NULLCHAR,0);
  560.             cnt = -2;
  561.             break;
  562.         }
  563.         *buf++ = c;
  564.         ++cnt;
  565.         if(cnt == len - 1)
  566.             break;
  567.     }
  568.     if(c == EOF && cnt == 0)
  569.         return -1;
  570.     *buf = '\0';
  571.     return cnt;
  572. }
  573.  
  574. int
  575. domboxbye(argc,argv,p)
  576. int argc;
  577. char *argv[];
  578. void *p;
  579. {
  580.     struct mbx *m;
  581.  
  582.     m = (struct mbx *)p;
  583.     /* Now say goodbye */
  584.     tprintf("Thank you %s, for calling the %s Tcp/Ip Mailbox.\n",m->name,
  585.         Hostname);
  586.     if(m->type == TIP)
  587.         tprintf("Please hang up now.\n");
  588.  
  589.     return -2;    /* signal that exitbbs() should be called */
  590. }
  591. static int
  592. dombhelp(argc,argv,p)
  593. int argc;
  594. char *argv[];
  595. void *p;
  596. {
  597.     char buf[255];
  598.     int i;
  599.     FILE *fp;
  600.     if(*argv[0] == '?') {
  601.         tputs(Longmenu1);
  602.         tputs(Longmenu2);
  603.         tputs(Longmenu3);
  604.         return 0;
  605.     }
  606.     buf[0] = '\0';
  607.     if(argc > 1)
  608.         for(i=0; Mbcmds[i].name != NULLCHAR; ++i)
  609.             if(!strncmp(Mbcmds[i].name,argv[1],strlen(argv[1]))) {
  610.                 sprintf(buf,"%s/%s.hlp",Helpdir,Mbcmds[i].name);
  611.                 break;
  612.             }
  613.     if(buf[0] == '\0')
  614.         if(*argv[0] == 'i')            /* INFO command */
  615.             sprintf(buf,"%s/info.hlp",Helpdir);
  616.         else
  617.             sprintf(buf,"%s/help.hlp",Helpdir);
  618.     if((fp = fopen(buf,READ_TEXT)) != NULLFILE) {
  619.         sendfile(fp,Curproc->output,ASCII_TYPE,0);
  620.         fclose(fp);
  621.     }
  622.     else
  623.         tprintf("No help available. (%s not found)\n",buf);
  624.     return 0;
  625. }
  626.  
  627. static int
  628. dochat(argc,argv,p)
  629. int argc;
  630. char *argv[];
  631. void *p;
  632. {
  633.     char buf[8], *newargv[3];
  634.  
  635.     if(Attended){
  636.         newargv[0] = "telnet";
  637.         newargv[1] = Hostname;
  638.         sprintf(buf,"%d",IPPORT_TTYLINK);
  639.         newargv[2] = buf;
  640.         return dombtelnet(3,newargv,p);
  641.     }
  642.     else {
  643.         tprintf("Sorry - the system is unattended.\007\n");
  644.     }
  645.     /* It returns only after a disconnect or refusal */
  646.     return 0;
  647. }
  648.  
  649. static int
  650. dosend(argc,argv,p)
  651. int argc;
  652. char *argv[];
  653. void *p;
  654. {
  655.     int cccnt = 0, fail = 0;
  656.     char *host, *cp, fullfrom[MBXLINE], sigwork[LINELEN], *rhdr = NULLCHAR;
  657.     struct list *ap, *cclist = NULLLIST;
  658.     struct mbx *m;
  659.     FILE *fp;
  660.  
  661.     m = (struct mbx *)p;
  662.     if((m->stype != 'R' || (m->sid & MBX_SID)) && mbx_to(argc,argv,m)
  663.        == -1){
  664.         if(m->sid & MBX_SID)
  665.             tprintf("NO - syntax error\n");
  666.         else {
  667.             tprintf("S command syntax error - format is:\n");
  668.             tprintf("  S[F] name [@ host] [< from_addr] [$bulletin_id]\n");
  669.             tprintf("  SR [number]\n");
  670.         }
  671.         return 0;
  672.     }
  673.     if(m->stype != 'R' && msgidcheck(m->tomsgid)) {
  674.         if(m->sid & MBX_SID)
  675.             tputs("NO - ");
  676.         tprintf("Already have %s\n",m->tomsgid);
  677.         return 0;
  678.     }
  679.     if(m->stype == 'R' && !(m->sid & MBX_SID) &&
  680.        mbx_reply(argc,argv,m,&cclist,&rhdr) == -1)
  681.         return 0;
  682.     if((cp = rewrite_address(m->to)) != NULLCHAR)
  683.          if(strcmp(m->to,cp) != 0){
  684.           m->origto = m->to;
  685.           m->to = cp;
  686.          }
  687.          else
  688.           free(cp);
  689.     if((m->origto != NULLCHAR || m->stype == 'R') && !(m->sid & MBX_SID))
  690.         tprintf("To: %s\n", m->to);
  691.     if(validate_address(m->to) == 0){
  692.         if(m->sid & MBX_SID)
  693.             tprintf("NO - bad address\n");
  694.         else
  695.             tprintf("Bad user or host name\n");
  696.         free(rhdr);
  697.         del_list(cclist);
  698.         /* We don't free any more buffers here. They are freed upon
  699.          * the next call to mbx_to() or to domboxbye()
  700.          */
  701.         return 0;
  702.     }
  703.     /* Display the Cc: line (during SR command) */
  704.     for(ap = cclist; ap != NULLLIST; ap = ap->next) {
  705.         if(cccnt == 0){
  706.             tprintf("%s",Hdrs[CC]);
  707.             cccnt = 4;
  708.         }
  709.         else {
  710.             tputs(", ");
  711.             cccnt += 2;
  712.         }
  713.         if(cccnt + strlen(ap->val) > 80 - 3) {
  714.             tputs("\n    ");
  715.             cccnt = 4;
  716.         }
  717.         tputs(ap->val);
  718.         cccnt += strlen(ap->val);
  719.     }
  720.     if(cccnt)
  721.         tputc('\n');
  722.     m->state = MBX_SUBJ;
  723.     if(m->stype != 'R' || (m->sid & MBX_SID) != 0) {
  724.         tprintf((m->sid & MBX_SID) ? "OK\n" : "Subject: ");
  725.         if(mbxrecvline(m->user,m->line,MBXLINE,-1) == -1)
  726.             return 0;
  727.     }
  728.     else                /* Replying to a message */
  729.         tprintf("Subject: %s\n",m->line);
  730.     if(mbx_data(m,cclist,rhdr) == -1){
  731.         free(rhdr);
  732.         del_list(cclist);
  733.         tputs("Can't create temp file for mail\n");
  734.         return 0;
  735.     }
  736.     free(rhdr);
  737.     m->state = MBX_DATA;
  738.     if((m->sid & MBX_SID) == 0 && m->stype != 'F')
  739.         tprintf("Enter message.  %s",Howtoend);
  740.  
  741.     if(m->stype != 'F' || (m->sid & MBX_SID) != 0)
  742.         while(mbxrecvline(m->user,m->line,MBXLINE,-1) != -1){
  743.             if(m->line[0] == 0x01){  /* CTRL-A */
  744.                 fclose(m->tfile);
  745.                 tputs("Aborted.\n");
  746.                 del_list(cclist);
  747.                 return 0;
  748.             }
  749.             if(m->line[0] != CTLZ && stricmp(m->line, "/ex"))
  750.                 fprintf(m->tfile,"%s\n",m->line);
  751.             else
  752.                 break;    /* all done */
  753.         }
  754.     else {
  755.         fprintf(m->tfile,"----- Forwarded message -----\n\n");
  756.         msgtofile(m,m->current,m->tfile,0);
  757.         fprintf(m->tfile,"----- End of forwarded message -----\n");
  758.     }
  759.  
  760.     /* Insert customised signature if one is found */
  761.     if(!(m->sid & MBX_SID)) {    /* not a forwarding BBS */
  762.          sprintf(sigwork,"%s/%s.sig",Signature,
  763.              m->tofrom ? m->tofrom : m->name);
  764.          if((fp = fopen(sigwork,READ_TEXT)) != NULLFILE){
  765.           while(fgets(sigwork,LINELEN,fp) != NULLCHAR)
  766.             fputs(sigwork,m->tfile);
  767.           fclose(fp);
  768.          }
  769.     }
  770.  
  771.     if((host = strrchr(m->to,'@')) == NULLCHAR) {
  772.         host = Hostname;    /* use our hostname */
  773.         if(m->origto != NULLCHAR) {
  774.             /* rewrite_address() will be called again by our
  775.              * SMTP server, so revert to the original address.
  776.              */
  777.              free(m->to);
  778.             m->to = m->origto;
  779.             m->origto = NULLCHAR;
  780.         }
  781.     }
  782.     else
  783.         host++;    /* use the host part of address */
  784.  
  785.     /* make up full from name for work file */
  786.     if(m->tofrom != NULLCHAR)
  787.         sprintf(fullfrom,"%s%%%s.bbs@%s",m->tofrom, m->name, Hostname);
  788.     else
  789.         sprintf(fullfrom,"%s@%s",m->name,Hostname);
  790.     if(cclist != NULLLIST && stricmp(host,Hostname) != 0) {
  791.         fseek(m->tfile,0L,0);    /* reset to beginning */
  792.         fail = queuejob(m->tfile,Hostname,cclist,fullfrom);
  793.         del_list(cclist);
  794.         cclist = NULLLIST;
  795.     }
  796.     addlist(&cclist,m->to,0);
  797.     fseek(m->tfile,0L,0);
  798.     fail += queuejob(m->tfile,host,cclist,fullfrom);
  799.     del_list(cclist);
  800.     fclose(m->tfile);
  801.     if(fail)
  802.          tputs("Couldn't queue message for delivery\n");
  803.     else
  804.          if(m->tomsgid != NULLCHAR &&
  805.         (fp = fopen(Historyfile,APPEND_TEXT)) != NULLFILE) {
  806.           fprintf(fp,"%s\n",m->tomsgid); /* Save BID in history file */
  807.           fclose(fp);
  808.          }
  809.     smtptick(NULL);        /* wake SMTP to send that mail */
  810.     return 0;
  811. }
  812.  
  813. static int
  814. dosid(argc,argv,p)
  815. int argc;
  816. char *argv[];
  817. void *p;
  818. {
  819.     struct mbx *m;
  820.     char *cp;
  821.  
  822.     m = (struct mbx *)p;
  823.     if(argc == 1)
  824.         return 1;
  825.     if(argv[1][strlen(argv[1]) - 1] != ']') /* must be an SID */
  826.         return 1;
  827.     m->sid = MBX_SID;
  828.     /* Now check to see if this is an RLI board.
  829.      * As usual, Hank does it a bit differently from
  830.      * the rest of the world.
  831.      */
  832.     if(m->stype == 'R' && strncmp(argv[1],"li",2) == 0)/* [RLI] at a minimum */
  833.         m->sid |= MBX_RLI_SID;
  834.     /* Check to see if the BBS supports a kludge called "hierarchical
  835.      * routing designators."
  836.      *
  837.      * No need to check for ']' -- it must be there or this is not
  838.      * a valid mbox id -- it is checked earlier (fix de OH3LKU)
  839.      */
  840.     if((cp = strchr(argv[1],'-')) != NULLCHAR
  841.      && (cp=strchr(cp+1,'h')) != NULLCHAR
  842.      && strchr(cp+1,'$'))
  843.         m->sid |= MBX_HIER_SID;    
  844.     return 0;
  845. }
  846.  
  847. int
  848. dombescape(argc,argv,p)
  849. int argc;
  850. char *argv[];
  851. void *p;
  852. {
  853.     struct mbx *m;
  854.  
  855.     m = (struct mbx *)p;
  856.     if(argc < 2){
  857.         tprintf("The escape character is: ");
  858.         if(m->escape < 32)
  859.             tprintf("CTRL-%c\n",m->escape+'A'-1);
  860.         else
  861.             tprintf("'%c'\n",m->escape);
  862.         return 0;
  863.     }
  864.     if(strlen(argv[1]) > 1)
  865.         if(isdigit(*argv[1]))
  866.             m->escape = (char) atoi(argv[1]);
  867.         else
  868.             return 1;
  869.     else
  870.         m->escape = *argv[1];
  871.     return 0;
  872. }
  873.  
  874. static int
  875. dodownload(argc,argv,p)
  876. int argc;
  877. char *argv[];
  878. void *p;
  879. {
  880.     struct mbx *m;
  881.     FILE *fp;
  882.     char *file;
  883.  
  884.     m = (struct mbx *)p;
  885.     file = pathname(m->path,argv[1]);
  886.     if(!permcheck(m->path,m->privs,RETR_CMD,file)){
  887.         tprintf(Noperm);
  888.         return 0;
  889.     }
  890.     if((fp = fopen(file,READ_TEXT)) == NULLFILE)
  891.         tprintf("Can't open \"%s\": %s\n",file,sys_errlist[errno]);
  892.     else
  893.         if(m->stype == 'U'){            /* uuencode ? */
  894.             fclose(fp);
  895.             fp = fopen(file,READ_BINARY);    /* assume non-ascii */
  896.             uuencode(fp,m->user,file);
  897.         } else
  898.             sendfile(fp,m->user,ASCII_TYPE,0);
  899.     free(file);
  900.     fclose(fp);
  901.     return 0;
  902. }
  903.  
  904. static int
  905. dombupload(argc,argv,p)
  906. int argc;
  907. char *argv[];
  908. void *p;
  909. {
  910.     struct mbx *m;
  911.     FILE *fp;
  912.     char *file, buf[LINELEN];
  913.  
  914.     m = (struct mbx *)p;
  915.     file = pathname(m->path,argv[1]);
  916.     if(!permcheck(m->path,m->privs,STOR_CMD,file)){
  917.         tprintf(Noperm);
  918.         return 0;
  919.     }
  920.     if((fp = fopen(file,WRITE_TEXT)) == NULLFILE){
  921.         tprintf("Can't create \"%s\": %s\n",file,sys_errlist[errno]);
  922.         free(file);
  923.         return 0;
  924.     }
  925.     log(m->user,"MBOX upload: %s",file);
  926.     tprintf("Send file,  %s",Howtoend);
  927.     for(;;){
  928.         if(mbxrecvline(m->user,buf,LINELEN,-1) == -1){
  929.             unlink(file);
  930.             break;
  931.         }
  932.         if(buf[0] == 0x01){  /* CTRL-A */
  933.             unlink(file);
  934.             tprintf("Aborted.\n");
  935.             break;
  936.         }
  937.         if(buf[0] == CTLZ || !stricmp("/ex",buf))
  938.             break;
  939.         fputs(buf,fp);
  940. #if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
  941.         /* Needed only if the OS uses a CR/LF
  942.          * convention and putc doesn't do
  943.          * an automatic translation
  944.          */
  945.         if(putc('\r',fp) == EOF)
  946.             break;
  947. #endif
  948.         if(putc('\n',fp) == EOF)
  949.             break;
  950.     }
  951.     free(file);
  952.     fclose(fp);
  953.     return 0;
  954. }
  955.  
  956. static int
  957. dowhat(argc,argv,p)
  958. int argc;
  959. char *argv[];
  960. void *p;
  961. {
  962.     struct mbx *m;
  963.     FILE *fp;
  964.     char *file;
  965.  
  966.     m = (struct mbx *)p;
  967.     if(argc < 2)
  968.         file = strdup(m->path);
  969.     else
  970.         file = pathname(m->path,argv[1]);
  971.     if(!permcheck(m->path,m->privs,RETR_CMD,file)){
  972.         tprintf(Noperm);
  973.         return 0;
  974.     }
  975.     if((fp = dir(file,1)) == NULLFILE)
  976.         tprintf("Can't read directory: \"%s\": %s\n",file,sys_errlist[errno]);
  977.     else
  978.         sendfile(fp,m->user,ASCII_TYPE,0);
  979.     free(file);
  980.     fclose(fp);
  981.     return 0;
  982. }
  983.  
  984. static int
  985. dozap(argc,argv,p)
  986. int argc;
  987. char *argv[];
  988. void *p;
  989. {
  990.     struct mbx *m;
  991.     char *file;
  992.  
  993.     m = (struct mbx *)p;
  994.     file = pathname(m->path,argv[1]);
  995.     if(!permcheck(m->path,m->privs,DELE_CMD,file)){
  996.         tprintf(Noperm);
  997.         return 0;
  998.     }
  999.     if(unlink(file))
  1000.         tprintf("Zap failed: %s\n",sys_errlist[errno]);
  1001.     log(m->user,"MBOX Zap: %s",file);
  1002.     free(file);
  1003.     return 0;
  1004. }
  1005.  
  1006. static int
  1007. dosysop(argc,argv,p)
  1008. int argc;
  1009. char *argv[];
  1010. void *p;
  1011. {
  1012.     struct mbx *m;
  1013.     int c;
  1014.     extern struct cmds Cmds[];
  1015.  
  1016.     m = (struct mbx *) p;
  1017.     if(!(m->privs & SYSOP_CMD)){
  1018.         tprintf(Noperm);
  1019.         return 0;
  1020.     }
  1021.     dombescape(1,NULLCHARP,p);
  1022.     for(;;){
  1023.         tprintf("Net> ");
  1024.         usflush(Curproc->output);
  1025.         c = mbxrecvline(Curproc->input,m->line,MBXLINE,m->escape);
  1026.         if(c == EOF || c == -2)
  1027.             break;
  1028.         log(m->user,"MBOX sysop: %s",m->line);
  1029.         cmdparse(Cmds,m->line,NULL);
  1030.     }
  1031.     return 0;
  1032. }
  1033.  
  1034. /* Handle the "*** Done" command when reverse forwarding ends or the
  1035.  * "*** LINKED to" command.
  1036.  */
  1037. static int
  1038. dostars(argc,argv,p)
  1039. int argc;
  1040. char *argv[];
  1041. void *p;
  1042. {
  1043.     struct mbx *m;
  1044.     int anony = 1;
  1045.     m = (struct mbx *)p;
  1046.     /* The "*** LINKED to" command is only allowed to stations with
  1047.      * SYSOP privileges to prevent others from obtaining the same.
  1048.      */
  1049.     if((m->privs & SYSOP_CMD) && argc == 4 && !strcmp(argv[1],"linked")) {
  1050.         strcpy(m->name,argv[3]);
  1051.         /* Try to find the privileges of this user from the userfile */
  1052.         if((m->privs = userlogin(m->name,NULLCHAR,&m->path,MBXLINE,
  1053.                      &anony)) == -1)
  1054.              if((m->privs = userlogin("bbs",NULLCHAR,&m->path,
  1055.                       MBXLINE,&anony)) == -1)
  1056.               if((m->privs = userlogin("anonymous",NULLCHAR,
  1057.                        &m->path,MBXLINE,&anony)) == -1){
  1058.                         m->privs = 0;
  1059.                         free(m->path);
  1060.                         m->path = NULLCHAR;
  1061.               }
  1062.         tprintf("Oh, hello %s.\n",m->name);
  1063.         if(m->privs & EXCLUDED_CMD)
  1064.             return domboxbye(0,NULLCHARP,p);
  1065.         changearea(m,m->name);
  1066.         return 0;
  1067.     }
  1068.     if(argc > 1 && (m->sid & MBX_SID))    /* "*** Done" or similar */
  1069.         return 2;
  1070.     return -1;
  1071. }
  1072.  
  1073. static int
  1074. doarea(argc,argv,p)
  1075. int argc;
  1076. char *argv[];
  1077. void *p;
  1078. {
  1079.     struct mbx *m;
  1080.     FILE *fp;
  1081.  
  1082.     m = (struct mbx *) p;
  1083.     if(argc < 2){
  1084.         tprintf("Current message area is: %s\n",m->area);
  1085.         tprintf("Available areas are:\n%-15s  Your private mail area\n",
  1086.           m->name);
  1087.         if((fp = fopen(Arealist,READ_TEXT)) == NULLFILE)
  1088.             return 0;
  1089.         sendfile(fp,m->user,ASCII_TYPE,0);
  1090.         fclose(fp);
  1091.         return 0;
  1092.     }
  1093.     if((m->privs & SYSOP_CMD) || strcmp(m->name,argv[1]) == 0){
  1094.         changearea(m,argv[1]);
  1095.         if(m->nmsgs){
  1096.             if(!strcmp(m->name,m->area))
  1097.                 tprintf("You have ");
  1098.             else
  1099.                 tprintf("%s: ",m->area);
  1100.             tprintf("%d message%s -  %d new.\n", m->nmsgs,
  1101.               m->nmsgs == 1 ? " " : "s ", m->newmsgs);
  1102.         }
  1103.         return 0;
  1104.     }
  1105.     if(isarea(argv[1])) {
  1106.         changearea(m,argv[1]);
  1107.         tprintf("%s: %d message%s.\n", m->area, m->nmsgs,
  1108.           m->nmsgs == 1 ? "" : "s");
  1109.     }
  1110.     else
  1111.         tprintf("No such message area: %s\n",argv[1]);
  1112.     return 0;
  1113. }
  1114.  
  1115. /* subroutine to do the actual switch from one area to another */
  1116. void
  1117. changearea(m,area)
  1118. struct mbx *m;
  1119. char *area;
  1120. {
  1121.     closenotes(m);
  1122.     m->nmsgs = m->newmsgs = m->current = 0;
  1123.     strcpy(m->area,area);
  1124.     scanmail(m);
  1125. }
  1126.  
  1127. static int
  1128. dombtelnet(argc,argv,p)
  1129. int argc;
  1130. char *argv[];
  1131. void *p;
  1132. {
  1133.     struct mbx *m;
  1134.     int s, len, i;
  1135.     char dsocket[MAXSOCKSIZE];
  1136.     struct sockaddr_in fsocket;
  1137.  
  1138.     m = (struct mbx *) p;
  1139.     fsocket.sin_family = AF_INET;
  1140.     if(argc < 3)
  1141.         fsocket.sin_port = IPPORT_TELNET;
  1142.     else
  1143.         fsocket.sin_port = atoi(argv[2]);
  1144.  
  1145.     if((fsocket.sin_addr.s_addr = resolve(argv[1])) == 0){
  1146.         tprintf(Badhost,argv[1]);
  1147.         return 0;
  1148.     }
  1149.     /* Only local telnets are are allowed to the unprivileged user */
  1150.     if(!(m->privs & TELNET_CMD) && !ismyaddr(fsocket.sin_addr.s_addr)){
  1151.         tprintf(Noperm);
  1152.         return 0;
  1153.     }
  1154.     if((s = socket(AF_INET,SOCK_STREAM,0)) == -1){
  1155.         tprintf(Nosock);
  1156.         return 0;
  1157.     }
  1158.     if(fsocket.sin_port == IPPORT_TTYLINK) {
  1159.         m->startmsg = mallocw(80);
  1160.         len = MAXSOCKSIZE;
  1161.         i = getpeername(m->user,dsocket,&len);
  1162.         sprintf(m->startmsg,"*** Incoming call from %s@%s ***\n",
  1163.             m->name,i != -1 ? psocket(dsocket): Hostname);
  1164.     }
  1165.     return gw_connect(m,s,(char *)&fsocket,SOCKSIZE);
  1166. }
  1167.  
  1168. static int
  1169. dombfinger(argc,argv,p)
  1170. int argc;
  1171. char *argv[];
  1172. void *p;
  1173. {
  1174.     struct mbx *m;
  1175.     char *host, *user = NULLCHAR, buf[8], *newargv[3];
  1176.  
  1177.     if(argc > 2){
  1178.         tprintf("Usage: F user@host  or  F @host  or  F user.\n");
  1179.         return 0;
  1180.     }
  1181.     host = Hostname;
  1182.     if(argc == 2){
  1183.         if((host = strchr(argv[1], '@')) != NULLCHAR){
  1184.             *host = '\0';
  1185.             host++;
  1186.         } else
  1187.             host = Hostname;
  1188.         user = argv[1];
  1189.     }
  1190.     m = (struct mbx *) p;
  1191.     m->startmsg = mallocw(80);
  1192.     if(user != NULLCHAR)
  1193.         sprintf(m->startmsg,"%s\n",user);
  1194.     else
  1195.         strcpy(m->startmsg,"\n");
  1196.     newargv[0] = "telnet";
  1197.     newargv[1] = host;
  1198.     sprintf(buf,"%d",IPPORT_FINGER);
  1199.     newargv[2] = buf;
  1200.     return dombtelnet(3,newargv,p);
  1201. }
  1202.  
  1203. /* Generic mbox gateway code. It sends and frees the contents of m->startmsg
  1204.  * when the connection has been established unless it a null pointer.
  1205.  */
  1206. int
  1207. gw_connect(m,s,fsocket,len)
  1208. struct mbx *m;
  1209. int s;
  1210. char *fsocket;
  1211. int len;
  1212. {
  1213.     int c;
  1214.     char *cp;
  1215.     struct proc *child;
  1216.     struct gwalarm *gwa;
  1217.  
  1218.     sockmode(s,SOCK_ASCII);
  1219.     child = newproc("gateway supervisor",256,gw_superv,0,Curproc,m,0);
  1220.     tprintf("Trying %s...  ",psocket((struct sockaddr *)fsocket));
  1221.     dombescape(0,NULLCHARP,(void *)m);
  1222.     usflush(Curproc->output);
  1223.     if(connect(s,fsocket,len) == -1){
  1224.         cp = sockerr(s);
  1225.         tprintf("Connection failed: ");
  1226.         if(cp != NULLCHAR)
  1227.             tprintf("%s errno %d\n",cp,errno);
  1228.         else
  1229.             tprintf("Escape character sent.\n");
  1230.         free(m->startmsg);
  1231.         m->startmsg = NULLCHAR;
  1232.         killproc(child);
  1233.         close_s(s);
  1234.         return 0;
  1235.     }
  1236.     /* The user did not type the escape character */
  1237.     killproc(child);
  1238.     tputs("Connected.\n");
  1239.     
  1240.     if(m->startmsg != NULLCHAR){
  1241.         usputs(s,m->startmsg);
  1242.         free(m->startmsg);
  1243.         m->startmsg = NULLCHAR;
  1244.     }
  1245.     /* Since NOS does not flush the output socket after a certain
  1246.      * period of time, we have to arrange that ourselves.
  1247.      */
  1248.     gwa = (struct gwalarm *) mallocw(sizeof(struct gwalarm));
  1249.     gwa->s1 = Curproc->output;
  1250.     gwa->s2 = s;
  1251.     set_timer(&gwa->t,240L);
  1252.     gwa->t.func = gw_alarm;
  1253.     gwa->t.arg = (void *) gwa;
  1254.     start_timer(&gwa->t);
  1255.     /* Fork off the receive process */
  1256.     child = newproc("gateway in",1024,gw_input,s,NULL,Curproc,0);
  1257.     
  1258.     for(;;){
  1259.         if((c = recvchar(Curproc->input)) == EOF)
  1260.             break;
  1261.         if(c == m->escape){
  1262.             tputs("Disconnecting.");
  1263.             if(socklen(Curproc->input,0))
  1264.                 recv_mbuf(Curproc->input,NULL,0,NULLCHAR,0);
  1265.             break;
  1266.         }
  1267.         if(usputc(s,(char)c) == EOF)
  1268.             break;
  1269.     }
  1270.     stop_timer(&gwa->t);
  1271.     free((char *)gwa);
  1272.     close_s(s);
  1273.     killproc(child); /* get rid of the receive process */
  1274.     tprintf("%c%c%c\n",IAC,WONT,TN_ECHO);
  1275.     return 0;
  1276. }
  1277.  
  1278. static void
  1279. gw_input(s,notused,p)
  1280. int s;
  1281. void *notused;
  1282. void *p;
  1283. {
  1284.     int c;
  1285.     char *cp;
  1286.     struct proc *parent;
  1287.  
  1288.     parent = (struct proc *) p;
  1289.     while((c = recvchar(s)) != EOF)
  1290.         tputc((char)c);
  1291.     tprintf("Disconnected ");
  1292.     cp = sockerr(s);
  1293.     if(cp != NULLCHAR)
  1294.         tputs(cp);
  1295.     /* Tell the parent that we are no longer connected */
  1296.     alert(parent,ENOTCONN);
  1297.     pwait(Curproc); /* Now wait to be killed */
  1298. }
  1299.  
  1300. /* Check if the escape character is typed while the parent process is busy
  1301.  * doing other things. 
  1302.  */
  1303. static void
  1304. gw_superv(null,proc,p)
  1305. int null;
  1306. void *proc;
  1307. void *p;
  1308. {
  1309.     struct proc *parent;
  1310.     struct mbx *m;
  1311.     int c;
  1312.     parent = (struct proc *) proc;
  1313.     m = (struct mbx *) p;
  1314.     while((c = recvchar(Curproc->input)) != EOF)
  1315.         if(c == m->escape){
  1316.             /* flush anything in the input queue */
  1317.             if(socklen(Curproc->input,0))
  1318.                 recv_mbuf(Curproc->input,NULL,0,NULLCHAR,0);
  1319.             break;
  1320.         }
  1321.     alert(parent,EINTR);     /* Tell the parent to quit */
  1322.     pwait(Curproc);         /* Please kill me */
  1323. }
  1324.  
  1325. static void
  1326. gw_alarm(p)
  1327. void *p;
  1328. {
  1329.     struct gwalarm *gwa = (struct gwalarm *)p;
  1330.     char oldbl;
  1331.     struct usock *up;
  1332.  
  1333.     /* Flush sockets s1 and s2, but first make sure that the socket
  1334.      * is set to non-blocking mode, to prevent the flush from blocking
  1335.      * if the high water mark has been reached.
  1336.      */
  1337.     if((up = itop(gwa->s1)) != NULLUSOCK) {
  1338.         oldbl = up->noblock;
  1339.         up->noblock = 1;
  1340.         usflush(gwa->s1);
  1341.         up->noblock = oldbl;
  1342.     }
  1343.     if((up = itop(gwa->s2)) != NULLUSOCK) {
  1344.         oldbl = up->noblock;
  1345.         up->noblock = 1;
  1346.         usflush(gwa->s2);
  1347.         up->noblock = oldbl;
  1348.     }
  1349.     start_timer(&gwa->t);
  1350. }
  1351.  
  1352. /* States for send line parser state machine */
  1353. #define        LOOK_FOR_USER        2
  1354. #define        IN_USER            3
  1355. #define        AFTER_USER        4
  1356. #define        LOOK_FOR_HOST        5
  1357. #define        IN_HOST            6
  1358. #define        AFTER_HOST        7
  1359. #define        LOOK_FOR_FROM        8
  1360. #define        IN_FROM            9
  1361. #define        AFTER_FROM        10
  1362. #define        LOOK_FOR_MSGID        11
  1363. #define        IN_MSGID        12
  1364. #define        FINAL_STATE        13
  1365. #define        ERROR_STATE        14
  1366.  
  1367. /* Prepare the addressee.  If the address is bad, return -1, otherwise
  1368.  * return 0
  1369.  */
  1370. static int
  1371. mbx_to(argc,argv,p)
  1372. int argc;
  1373. char *argv[];
  1374. void *p;
  1375. {
  1376.     register char *cp;
  1377.     int state, i;
  1378.     char *user, *host, *from, *msgid;
  1379.     int userlen = 0, hostlen = 0, fromlen = 0, msgidlen = 0;
  1380.     struct mbx *m;
  1381.  
  1382.     m = (struct mbx *)p;
  1383.     /* Free anything that might be allocated
  1384.      * since the last call to mbx_to() or mbx_reply()
  1385.      */
  1386.     free(m->to);
  1387.     m->to = NULLCHAR;
  1388.     free(m->tofrom);
  1389.     m->tofrom = NULLCHAR;
  1390.     free(m->tomsgid);
  1391.     m->tomsgid = NULLCHAR;
  1392.     free(m->origto);
  1393.     m->origto = NULLCHAR;
  1394.  
  1395.     if(argc == 1)
  1396.         return -1;
  1397.     i = 1;
  1398.     cp = argv[i];
  1399.     state = LOOK_FOR_USER;
  1400.     while(state < FINAL_STATE){
  1401. #ifdef MBDEBUG
  1402.         tprintf("State is %d, char is %c\n", state, *cp);
  1403. #endif
  1404.         switch(state){
  1405.         case LOOK_FOR_USER:
  1406.             if(*cp == '@' || *cp == '<' || *cp == '$'){
  1407.                 state = ERROR_STATE;        /* no user */
  1408.             } else {
  1409.                 user = cp;            /* point at start */
  1410.                 userlen++;            /* start counting */
  1411.                 state = IN_USER;
  1412.             }
  1413.             break;
  1414.         case IN_USER:
  1415.             switch(*cp){
  1416.             case '\0':
  1417.                 state = AFTER_USER;        /* done with username */
  1418.                 break;
  1419.             case '@':
  1420.                 state = LOOK_FOR_HOST;        /* hostname should follow */
  1421.                 break;
  1422.             case '<':
  1423.                 state = LOOK_FOR_FROM;        /* from name should follow */
  1424.                 break;
  1425.             case '$':
  1426.                 state = LOOK_FOR_MSGID;    /* message id should follow */
  1427.                 break;
  1428.             default:
  1429.                 userlen++;            /* part of username */
  1430.             }
  1431.             break;
  1432.         case AFTER_USER:
  1433.             switch(*cp){
  1434.             case '@':
  1435.                 state = LOOK_FOR_HOST;        /* hostname follows */
  1436.                 break;
  1437.             case '<':
  1438.                 state = LOOK_FOR_FROM;        /* fromname follows */
  1439.                 break;
  1440.             case '$':
  1441.             state = LOOK_FOR_MSGID;    /* message id follows */
  1442.                 break;
  1443.             default:
  1444.                 state = ERROR_STATE;
  1445.             }
  1446.             break;
  1447.         case LOOK_FOR_HOST:
  1448.             if(*cp == '@' || *cp == '<' || *cp == '$'){
  1449.                 state = ERROR_STATE;
  1450.                 break;
  1451.             }
  1452.             if(*cp == '\0')
  1453.                 break;
  1454.             host = cp;
  1455.             hostlen++;
  1456.             state = IN_HOST;
  1457.             break;
  1458.         case IN_HOST:
  1459.             switch(*cp){
  1460.             case '\0':
  1461.                 state = AFTER_HOST;        /* found user@host */
  1462.                 break;
  1463.             case '@':
  1464.                 state = ERROR_STATE;        /* user@host@? */
  1465.                 break;
  1466.             case '<':
  1467.                 state = LOOK_FOR_FROM;        /* fromname follows */
  1468.                 break;
  1469.             case '$':
  1470.                 state = LOOK_FOR_MSGID;    /* message id follows */
  1471.                 break;
  1472.             default:
  1473.                 hostlen++;
  1474.             }
  1475.             break;
  1476.         case AFTER_HOST:
  1477.             switch(*cp){
  1478.             case '@':
  1479.                 state = ERROR_STATE;        /* user@host @ */
  1480.                 break;
  1481.             case '<':
  1482.                 state = LOOK_FOR_FROM;        /* user@host < */
  1483.                 break;
  1484.             case '$':
  1485.                 state = LOOK_FOR_MSGID;    /* user@host $ */
  1486.                 break;
  1487.             default:
  1488.                 state = ERROR_STATE;        /* user@host foo */
  1489.             }
  1490.             break;
  1491.         case LOOK_FOR_FROM:
  1492.             if(*cp == '@' || *cp == '<' || *cp == '$'){
  1493.                 state = ERROR_STATE;
  1494.                 break;
  1495.             }
  1496.             if(*cp == '\0')
  1497.                 break;
  1498.             from = cp;
  1499.             fromlen++;
  1500.             state = IN_FROM;
  1501.             break;
  1502.         case IN_FROM:
  1503.             switch(*cp){
  1504.             case '\0':
  1505.                 state = AFTER_FROM;        /* user@host <foo */
  1506.                 break;
  1507.             case '<':
  1508.                 state = ERROR_STATE;        /* user@host <foo< */
  1509.                 break;
  1510.             case '$':
  1511.                 state = LOOK_FOR_MSGID;    /* message id follows */
  1512.                 break;
  1513.             default:
  1514.                 fromlen++;
  1515.             }
  1516.             break;
  1517.         case AFTER_FROM:
  1518.             switch(*cp){
  1519.             case '@':                /* user@host <foo @ */
  1520.             case '<':                /* user@host <foo < */
  1521.                 state = ERROR_STATE;
  1522.                 break;
  1523.             case '$':
  1524.                 state = LOOK_FOR_MSGID;    /* user@host <foo $ */
  1525.                 break;
  1526.             default:
  1527.                 state = ERROR_STATE;        /* user@host foo */
  1528.             }
  1529.             break;
  1530.         case LOOK_FOR_MSGID:
  1531.             if(*cp == '\0')
  1532.                 break;
  1533.             msgid = cp;
  1534.             msgidlen++;
  1535.             state = IN_MSGID;
  1536.             break;
  1537.         case IN_MSGID:
  1538.             if(*cp == '\0')
  1539.                 state = FINAL_STATE;
  1540.             else
  1541.                 msgidlen++;
  1542.             break;
  1543.         default:
  1544.             /* what are we doing in this state? */
  1545.             state = ERROR_STATE;
  1546.         }
  1547.         if(*(cp) == '\0'){
  1548.             ++i;
  1549.             if(i < argc)
  1550.             cp = argv[i];
  1551.             else break;
  1552.         } else
  1553.             ++cp;
  1554.     }
  1555.     if(state == ERROR_STATE || state == LOOK_FOR_HOST
  1556.      || state == LOOK_FOR_FROM || state == LOOK_FOR_MSGID)
  1557.         return -1;        /* syntax error */
  1558.  
  1559.     m->to = mallocw(userlen + hostlen + 2);
  1560.  
  1561.     strncpy(m->to, user, userlen);
  1562.     m->to[userlen] = '\0';
  1563.  
  1564.     if(hostlen){
  1565.         m->to[userlen] = '@';
  1566.         strncpy(m->to + userlen + 1, host, hostlen);
  1567.         m->to[userlen + hostlen + 1] = '\0';
  1568.     }
  1569.     if(fromlen){
  1570.         m->tofrom = mallocw(fromlen + 1);
  1571.         strncpy(m->tofrom, from, fromlen);
  1572.         m->tofrom[fromlen] = '\0';
  1573.     }
  1574.     if(msgidlen){
  1575.         m->tomsgid = mallocw(msgidlen + 1);
  1576.         strncpy(m->tomsgid, msgid, msgidlen);
  1577.         m->tomsgid[msgidlen] = '\0';
  1578.     }
  1579.     return 0;
  1580. }
  1581.  
  1582. /* This opens the data file and writes the mail header into it.
  1583.  * Returns 0 if OK, and -1 if not.
  1584.  */
  1585. static int
  1586. mbx_data(m,cclist,extra)
  1587. struct mbx *m;
  1588. struct list *cclist;    /* list of carbon copy recipients */
  1589. char *extra;        /* optional extra header lines */
  1590. {
  1591.     time_t t;
  1592.     struct list *ap;
  1593.     int cccnt = 0;
  1594.     
  1595.     if((m->tfile = tmpfile()) == NULLFILE)
  1596.         return -1;
  1597.     time(&t);
  1598.     fprintf(m->tfile,Hdrs[RECEIVED]);
  1599.     if(m->tofrom != NULLCHAR)
  1600.         fprintf(m->tfile,"from %s.bbs ",m->name);
  1601.     fprintf(m->tfile,"by %s (%s)\n\tid AA%ld ; %s",
  1602.         Hostname, Version, get_msgid(), ptime(&t));
  1603.     fprintf(m->tfile,"%s%s",Hdrs[DATE],ptime(&t));
  1604.     fprintf(m->tfile,Hdrs[MSGID]);
  1605.     if(m->tomsgid)
  1606.         fprintf(m->tfile,"<%s@%s.bbs>\n", m->tomsgid, m->name);
  1607.     else
  1608.         fprintf(m->tfile,"<%ld@%s>\n",get_msgid(), Hostname);
  1609.     fprintf(m->tfile,Hdrs[FROM]);
  1610.     if(m->tofrom)
  1611.         fprintf(m->tfile,"%s%%%s.bbs@%s\n",
  1612.             m->tofrom, m->name, Hostname);
  1613.     else
  1614.         fprintf(m->tfile,"%s@%s\n", m->name, Hostname);
  1615.     fprintf(m->tfile,"%s%s\n",Hdrs[TO],m->origto != NULLCHAR ? m->origto : m->to);
  1616.     /* Write Cc: line */
  1617.     for(ap = cclist; ap != NULLLIST; ap = ap->next) {
  1618.         if(cccnt == 0){
  1619.             fprintf(m->tfile,"%s",Hdrs[CC]);
  1620.             cccnt = 4;
  1621.         }
  1622.         else {
  1623.                fprintf(m->tfile,", ");
  1624.                cccnt += 2;
  1625.         }
  1626.         if(cccnt + strlen(ap->val) > 80 - 3) {
  1627.                fprintf(m->tfile,"\n    ");
  1628.                cccnt = 4;
  1629.         }
  1630.         fputs(ap->val,m->tfile);
  1631.         cccnt += strlen(ap->val);
  1632.     }
  1633.     if(cccnt)
  1634.         fputc('\n',m->tfile);
  1635.     fprintf(m->tfile,"%s%s\n",Hdrs[SUBJECT],m->line);
  1636.     if(!isspace(m->stype) && ((m->stype != 'R' && m->stype != 'F') ||
  1637.       (m->sid & MBX_SID) !=0))
  1638.           fprintf(m->tfile,"%s%c\n", Hdrs[BBSTYPE],m->stype);
  1639.     if(extra != NULLCHAR)
  1640.         fprintf(m->tfile,extra);
  1641.     fprintf(m->tfile,"\n");
  1642.  
  1643.     return 0;
  1644. }
  1645.  
  1646. /* Returns true if string is in history file or if string appears to be a
  1647.  * message id generated by our system.
  1648.  */
  1649. static int
  1650. msgidcheck(string)
  1651. char *string;
  1652. {
  1653.      FILE *fp;
  1654.      char buf[LINELEN], *cp;
  1655.      if(string == NULLCHAR)
  1656.       return 0;
  1657.      /* BID's that we have generated ourselves are not kept in the history
  1658.       * file. Such BID's are in the nnnn_hhhh form, where hhhh is a part of
  1659.       * our hostname, truncated so that the BID is no longer than 11
  1660.       * characters.
  1661.       */
  1662.      if((cp = strchr(string,'_')) != NULLCHAR && *(cp+1) != '\0' && 
  1663.     strnicmp(cp+1,Hostname,strlen(cp+1)) == 0)
  1664.       return 1;
  1665.  
  1666.      if((fp = fopen(Historyfile,READ_TEXT)) == NULLFILE)
  1667.       return 0;
  1668.      while(fgets(buf,LINELEN,fp) != NULLCHAR) {
  1669.       rip(buf);
  1670.       if(stricmp(string,buf) == 0) {    /* found */
  1671.            fclose(fp);
  1672.            return 1;
  1673.       }
  1674.      }
  1675.      fclose(fp);
  1676.      return 0;
  1677. }
  1678.      
  1679. /* Read the rewrite file for lines where the first word is a regular
  1680.  * expression and the second word are rewriting rules. The special
  1681.  * character '$' followed by a digit denotes the string that matched
  1682.  * a '*' character. The '*' characters are numbered from 1 to 9.
  1683.  * Example: the line "*@*.* $2@$1.ampr.org" would rewrite the address
  1684.  * "foo@bar.xxx" to "bar@foo.ampr.org".
  1685.  * $H is replaced by our hostname, and $$ is an escaped $ character.
  1686.  * If the third word on the line has an 'r' character in it, the function
  1687.  * will recurse with the new address.
  1688.  */
  1689. char *
  1690. rewrite_address(addr)
  1691. char *addr;
  1692. {
  1693.     char *argv[10], buf[MBXLINE], *cp, *cp2, *retstr;
  1694.     int cnt;
  1695.     FILE *fp;
  1696.     if ((fp = fopen(Rewritefile,READ_TEXT)) == NULLFILE)
  1697.         return NULLCHAR;
  1698.     memset((char *)argv,0,10*sizeof(char *));
  1699.     while(fgets(buf,MBXLINE,fp) != NULLCHAR) {
  1700.         if(*buf == '#')        /* skip commented lines */
  1701.             continue;
  1702.         if((cp = strchr(buf,' ')) == NULLCHAR) /* get the first word */
  1703.             if((cp = strchr(buf,'\t')) == NULLCHAR)
  1704.                 continue;
  1705.         *cp = '\0';
  1706.         if((cp2 = strchr(buf,'\t')) != NULLCHAR){
  1707.             *cp = ' ';
  1708.             cp = cp2;
  1709.             *cp = '\0';
  1710.         }
  1711.         if(!wildmat(addr,buf,argv))
  1712.             continue;        /* no match */
  1713.         rip(++cp);
  1714.         cp2 = retstr = (char *) callocw(1,MBXLINE);
  1715.         while(*cp != '\0' && *cp != ' ' && *cp != '\t')
  1716.             if(*cp == '$') {
  1717.                 if(isdigit(*(++cp)))
  1718.                     if(argv[*cp - '0'-1] != '\0')
  1719.                         strcat(cp2,argv[*cp - '0'-1]);
  1720.                 if(*cp == 'h' || *cp == 'H') /* Our hostname */
  1721.                     strcat(cp2,Hostname);
  1722.                 if(*cp == '$')    /* Escaped $ character */
  1723.                     strcat(cp2,"$");
  1724.                 cp2 = retstr + strlen(retstr);
  1725.                 cp++;
  1726.             }
  1727.             else
  1728.                 *cp2++ = *cp++;
  1729.         for(cnt=0; argv[cnt] != NULLCHAR; ++cnt)
  1730.             free(argv[cnt]);
  1731.         fclose(fp);
  1732.         /* If there remains an 'r' character on the line, repeat
  1733.          * everything by recursing.
  1734.          */
  1735.         if(strchr(cp,'r') != NULLCHAR || strchr(cp,'R') != NULLCHAR) {
  1736.             if((cp2 = rewrite_address(retstr)) != NULLCHAR) {
  1737.                 free(retstr);
  1738.                 return cp2;
  1739.             }
  1740.         }
  1741.         return retstr;
  1742.     }
  1743.     fclose(fp);
  1744.     return NULLCHAR;
  1745. }
  1746.  
  1747. /* uuencode a file -- translated from C++; both versions copyright 1990
  1748.    by David R. Evans, G4AMJ/NQ0I
  1749. */
  1750.  
  1751. static int
  1752. uuencode(infile,s,infilename)
  1753. FILE *infile;
  1754. int s;            /* output socket */
  1755. char *infilename;
  1756. {
  1757.   int n_read_so_far = 0, n_written_so_far = 0, in_chars, n, mode = 0755;
  1758.   unsigned int32 cnt = 0;
  1759.   unsigned char in[3], out[4], line[100];
  1760. #ifdef UNIX
  1761.   struct stat stb;
  1762.   
  1763.   if(stat(infilename,&stb) != -1)
  1764.        mode = stb.st_mode & 0777;    /* get real file protection mode */
  1765. #endif
  1766.   usprintf(s, "begin %03o %s\n", mode, infilename);
  1767.  
  1768.   /* do the encode */
  1769.   for(;;) {
  1770.     in_chars = fread(in, 1, 3, infile);
  1771.     out[0] = in[0] >> 2;
  1772.     out[1] = in[0] << 6;
  1773.     out[1] = out[1] >> 2;
  1774.     out[1] = out[1] | (in[1] >> 4);
  1775.     out[2] = in[1] << 4;
  1776.     out[2] = out[2] >> 2;
  1777.     out[2] = out[2] | (in[2] >> 6);
  1778.     out[3] = in[2] << 2;
  1779.     out[3] = out[3] >> 2;
  1780.     for (n = 0; n < 4; n++)
  1781.       out[n] += ' ';
  1782.     n_read_so_far += in_chars;
  1783.     for (n = 0; n < 4; n++)
  1784.       line[n_written_so_far++] = out[n];
  1785.     if (((in_chars != 3) || (n_written_so_far == 60)) && n_read_so_far > 0) {
  1786.       line[(n_read_so_far + 2) / 3 * 4] = '\0';
  1787.       
  1788.       usprintf(s,"%c%s\n",n_read_so_far + ' ', line);
  1789.       cnt += n_read_so_far;
  1790.       n_read_so_far = 0;
  1791.       n_written_so_far = 0;
  1792.     }
  1793.     if (in_chars == 0)
  1794.       break;
  1795.   }
  1796.   if (usprintf(s," \nend\nsize %lu\n", cnt) == EOF)
  1797.     return 1;
  1798.   return 0;
  1799. }
  1800.