home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / osdep / amiga / unix.c < prev    next >
C/C++ Source or Header  |  1999-01-22  |  61KB  |  1,791 lines

  1. /*
  2.  * Program:    UNIX mail routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    20 December 1989
  13.  * Last Edited:    22 January 1999
  14.  *
  15.  * Copyright 1999 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36.  
  37. /*                DEDICATION
  38.  *
  39.  *  This file is dedicated to my dog, Unix, also known as Yun-chan and
  40.  * Unix J. Terwilliker Jehosophat Aloysius Monstrosity Animal Beast.  Unix
  41.  * passed away at the age of 11 1/2 on September 14, 1996, 12:18 PM PDT, after
  42.  * a two-month bout with cirrhosis of the liver.
  43.  *
  44.  *  He was a dear friend, and I miss him terribly.
  45.  *
  46.  *  Lift a leg, Yunie.  Luv ya forever!!!!
  47.  */
  48.  
  49. #include <stdio.h>
  50. #include <ctype.h>
  51. #include <errno.h>
  52. extern int errno;        /* just in case */
  53. #include <signal.h>
  54. #include "mail.h"
  55. #include "osdep.h"
  56. #include <time.h>
  57. #include <sys/stat.h>
  58. #include "unix.h"
  59. #include "pseudo.h"
  60. #include "fdstring.h"
  61. #include "misc.h"
  62. #include "dummy.h"
  63.  
  64. /* UNIX mail routines */
  65.  
  66.  
  67. /* Driver dispatch used by MAIL */
  68.  
  69. DRIVER unixdriver = {
  70.   "unix",            /* driver name */
  71.   DR_LOCAL|DR_MAIL,        /* driver flags */
  72.   (DRIVER *) NIL,        /* next driver */
  73.   unix_valid,            /* mailbox is valid for us */
  74.   unix_parameters,        /* manipulate parameters */
  75.   unix_scan,            /* scan mailboxes */
  76.   unix_list,            /* list mailboxes */
  77.   unix_lsub,            /* list subscribed mailboxes */
  78.   NIL,                /* subscribe to mailbox */
  79.   NIL,                /* unsubscribe from mailbox */
  80.   unix_create,            /* create mailbox */
  81.   unix_delete,            /* delete mailbox */
  82.   unix_rename,            /* rename mailbox */
  83.   NIL,                /* status of mailbox */
  84.   unix_open,            /* open mailbox */
  85.   unix_close,            /* close mailbox */
  86.   NIL,                /* fetch message "fast" attributes */
  87.   NIL,                /* fetch message flags */
  88.   NIL,                /* fetch overview */
  89.   NIL,                /* fetch message envelopes */
  90.   unix_header,            /* fetch message header */
  91.   unix_text,            /* fetch message text */
  92.   NIL,                /* fetch partial message text */
  93.   NIL,                /* unique identifier */
  94.   NIL,                /* message number */
  95.   NIL,                /* modify flags */
  96.   unix_flagmsg,            /* per-message modify flags */
  97.   NIL,                /* search for message based on criteria */
  98.   NIL,                /* sort messages */
  99.   NIL,                /* thread messages */
  100.   unix_ping,            /* ping mailbox to see if still alive */
  101.   unix_check,            /* check for new messages */
  102.   unix_expunge,            /* expunge deleted messages */
  103.   unix_copy,            /* copy messages to another mailbox */
  104.   unix_append,            /* append string message to mailbox */
  105.   NIL                /* garbage collect stream */
  106. };
  107.  
  108.                 /* prototype stream */
  109. MAILSTREAM unixproto = {&unixdriver};
  110.  
  111.                 /* driver parameters */
  112. static long unix_fromwidget = T;
  113.  
  114. /* UNIX mail validate mailbox
  115.  * Accepts: mailbox name
  116.  * Returns: our driver if name is valid, NIL otherwise
  117.  */
  118.  
  119. DRIVER *unix_valid (char *name)
  120. {
  121.   char tmp[MAILTMPLEN];
  122.   return unix_isvalid (name,tmp) ? &unixdriver : NIL;
  123. }
  124.  
  125.  
  126. /* UNIX mail test for valid mailbox name
  127.  * Accepts: mailbox name
  128.  *        scratch buffer
  129.  * Returns: T if valid, NIL otherwise
  130.  */
  131.  
  132. long unix_isvalid (char *name,char *tmp)
  133. {
  134.   int fd;
  135.   int ret = NIL;
  136.   char *t,file[MAILTMPLEN];
  137.   struct stat sbuf;
  138.   time_t tp[2];
  139.   errno = EINVAL;        /* assume invalid argument */
  140.                 /* must be non-empty file */
  141.   if ((t = dummy_file (file,name)) && !stat (t,&sbuf)) {
  142.     if (!sbuf.st_size)errno = 0;/* empty file */
  143.     else if ((fd = open (file,O_RDONLY,NIL)) >= 0) {
  144.                 /* error -1 for invalid format */
  145.       if (!(ret = unix_isvalid_fd (fd,tmp))) errno = -1;
  146.       close (fd);        /* close the file */
  147.       tp[0] = sbuf.st_atime;    /* preserve atime and mtime */
  148.       tp[1] = sbuf.st_mtime;
  149.       utime (file,tp);        /* set the times */
  150.     }
  151.   }
  152.                 /* in case INBOX but not unix format */
  153.   else if ((errno == ENOENT) && ((name[0] == 'I') || (name[0] == 'i')) &&
  154.        ((name[1] == 'N') || (name[1] == 'n')) &&
  155.        ((name[2] == 'B') || (name[2] == 'b')) &&
  156.        ((name[3] == 'O') || (name[3] == 'o')) &&
  157.        ((name[4] == 'X') || (name[4] == 'x')) && !name[5]) errno = -1;
  158.   return ret;            /* return what we should */
  159. }
  160.  
  161. /* UNIX mail test for valid mailbox
  162.  * Accepts: file descriptor
  163.  *        scratch buffer
  164.  * Returns: T if valid, NIL otherwise
  165.  */
  166.  
  167. long unix_isvalid_fd (int fd,char *tmp)
  168. {
  169.   int zn;
  170.   int ret = NIL;
  171.   char *s,*t,c = '\n';
  172.   memset (tmp,'\0',MAILTMPLEN);
  173.   if (read (fd,tmp,MAILTMPLEN-1) >= 0) {
  174.     for (s = tmp; (*s == '\n') || (*s == ' ') || (*s == '\t');) c = *s++;
  175.     if (c == '\n') VALID (s,t,ret,zn);
  176.   }
  177.   return ret;            /* return what we should */
  178. }
  179.  
  180.  
  181. /* UNIX manipulate driver parameters
  182.  * Accepts: function code
  183.  *        function-dependent value
  184.  * Returns: function-dependent return value
  185.  */
  186.  
  187. void *unix_parameters (long function,void *value)
  188. {
  189.   switch ((int) function) {
  190.   case SET_FROMWIDGET:
  191.     unix_fromwidget = (long) value;
  192.     break;
  193.   case GET_FROMWIDGET:
  194.     value = (void *) unix_fromwidget;
  195.     break;
  196.   default:
  197.     value = NIL;        /* error case */
  198.     break;
  199.   }
  200.   return value;
  201. }
  202.  
  203. /* UNIX mail scan mailboxes
  204.  * Accepts: mail stream
  205.  *        reference
  206.  *        pattern to search
  207.  *        string to scan
  208.  */
  209.  
  210. void unix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
  211. {
  212.   if (stream) dummy_scan (NIL,ref,pat,contents);
  213. }
  214.  
  215.  
  216. /* UNIX mail list mailboxes
  217.  * Accepts: mail stream
  218.  *        reference
  219.  *        pattern to search
  220.  */
  221.  
  222. void unix_list (MAILSTREAM *stream,char *ref,char *pat)
  223. {
  224.   if (stream) dummy_list (NIL,ref,pat);
  225. }
  226.  
  227.  
  228. /* UNIX mail list subscribed mailboxes
  229.  * Accepts: mail stream
  230.  *        reference
  231.  *        pattern to search
  232.  */
  233.  
  234. void unix_lsub (MAILSTREAM *stream,char *ref,char *pat)
  235. {
  236.   if (stream) dummy_lsub (NIL,ref,pat);
  237. }
  238.  
  239. /* UNIX mail create mailbox
  240.  * Accepts: MAIL stream
  241.  *        mailbox name to create
  242.  * Returns: T on success, NIL on failure
  243.  */
  244.  
  245. long unix_create (MAILSTREAM *stream,char *mailbox)
  246. {
  247.   char *s,*t,mbx[MAILTMPLEN],tmp[MAILTMPLEN];
  248.   long ret = NIL;
  249.   int i,fd;
  250.   time_t ti = time (0);
  251.   if (!(s = dummy_file (mbx,mailbox))) {
  252.     sprintf (tmp,"Can't create %.80s: invalid name",mailbox);
  253.     mm_log (tmp,ERROR);
  254.   }
  255.                 /* create underlying file */
  256.   else if (dummy_create_path (stream,s)) {
  257.                 /* done if made directory */
  258.     if ((s = strrchr (s,'/')) && !s[1]) ret = T;
  259.     else if ((fd = open (mbx,O_WRONLY,
  260.              (int) mail_parameters(NIL,GET_MBXPROTECTION,NIL)))<0){
  261.       sprintf (tmp,"Can't reopen mailbox node %.80s: %s",mbx,strerror (errno));
  262.       mm_log (tmp,ERROR);
  263.       unlink (mbx);        /* delete the file */
  264.     }
  265.                 /* in case a whiner with no life */
  266.     else if (mail_parameters (NIL,GET_USERHASNOLIFE,NIL)) ret = T;
  267.     else {            /* initialize header */
  268.       memset (tmp,'\0',MAILTMPLEN);
  269.       sprintf (tmp,"From %s %sDate: ",pseudo_from,ctime (&ti));
  270.       rfc822_date (s = tmp + strlen (tmp));
  271.                 /* write the pseudo-header */
  272.       sprintf (s += strlen (s),
  273.            "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu 0000000000",
  274.            pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,ti);
  275.       for (i = 0; i < NUSERFLAGS; ++i) if (default_user_flag (i))
  276.     sprintf (s += strlen (s)," %s",default_user_flag (i));
  277.       sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n\n",pseudo_msg);
  278.       if ((write (fd,tmp,strlen (tmp)) < 0) || close (fd)) {
  279.     sprintf (tmp,"Can't initialize mailbox node %.80s: %s",mbx,
  280.          strerror (errno));
  281.     mm_log (tmp,ERROR);
  282.     unlink (mbx);        /* delete the file */
  283.       }
  284.       else ret = T;        /* success */
  285.     }
  286.   }
  287.   return ret ? set_mbx_protections (mailbox,mbx) : NIL;
  288. }
  289.  
  290.  
  291. /* UNIX mail delete mailbox
  292.  * Accepts: MAIL stream
  293.  *        mailbox name to delete
  294.  * Returns: T on success, NIL on failure
  295.  */
  296.  
  297. long unix_delete (MAILSTREAM *stream,char *mailbox)
  298. {
  299.   return unix_rename (stream,mailbox,NIL);
  300. }
  301.  
  302. /* UNIX mail rename mailbox
  303.  * Accepts: MAIL stream
  304.  *        old mailbox name
  305.  *        new mailbox name (or NIL for delete)
  306.  * Returns: T on success, NIL on failure
  307.  */
  308.  
  309. long unix_rename (MAILSTREAM *stream,char *old,char *newname)
  310. {
  311.   long ret = NIL;
  312.   char c,*s;
  313.   char tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN],lockx[MAILTMPLEN];
  314.   int fd,ld;
  315.   struct stat sbuf;
  316.   mm_critical (stream);        /* get the c-client lock */
  317.   if (newname && !((s = dummy_file (tmp,newname)) && *s))
  318.     sprintf (tmp,"Can't rename mailbox %.80s to %.80s: invalid name",
  319.          old,newname);
  320.                 /* lock out other c-clients */
  321.   else if ((ld = lockname (lock,dummy_file (file,old),LOCK_EX|LOCK_NB)) < 0)
  322.     sprintf (tmp,"Mailbox %.80s is in use by another process",old);
  323.   else {
  324.     if ((fd = unix_lock (file,O_RDWR,S_IREAD|S_IWRITE,lockx,LOCK_EX)) < 0)
  325.       sprintf (tmp,"Can't lock mailbox %.80s: %s",old,strerror (errno));
  326.     else {
  327.       if (newname) {        /* want rename? */
  328.                 /* found superior to destination name? */
  329.     if (s = strrchr (s,'/')) {
  330.       c = *++s;        /* remember first character of inferior */
  331.       *s = '\0';        /* tie off to get just superior */
  332.                 /* name doesn't exist, create it */
  333.       if ((stat (tmp,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
  334.           !dummy_create (stream,tmp)) {
  335.         unix_unlock (fd,NIL,lockx);
  336.         flock (ld,LOCK_UN);    /* release c-client lock */
  337.         close (ld);        /* close c-client lock */
  338.         unlink (lock);    /* and delete it */
  339.         mm_nocritical (stream);
  340.         return ret;        /* return success or failure */
  341.       }
  342.       *s = c;        /* restore full name */
  343.     }
  344.     if (rename (file,tmp))
  345.       sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %s",old,newname,
  346.            strerror (errno));
  347.     else ret = T;        /* set success */
  348.       }
  349.       else if (unlink (file))
  350.     sprintf (tmp,"Can't delete mailbox %.80s: %s",old,strerror (errno));
  351.       else ret = T;        /* set success */
  352.       unix_unlock (fd,NIL,lockx);
  353.     }
  354.     flock (ld,LOCK_UN);        /* release c-client lock */
  355.     close (ld);            /* close c-client lock */
  356.     unlink (lock);        /* and delete it */
  357.   }
  358.   mm_nocritical (stream);    /* no longer critical */
  359.   if (!ret) mm_log (tmp,ERROR);    /* log error */
  360.   return ret;            /* return success or failure */
  361. }
  362.  
  363. /* UNIX mail open
  364.  * Accepts: Stream to open
  365.  * Returns: Stream on success, NIL on failure
  366.  */
  367.  
  368. MAILSTREAM *unix_open (MAILSTREAM *stream)
  369. {
  370.   long i;
  371.   int fd;
  372.   char tmp[MAILTMPLEN];
  373.   struct stat sbuf;
  374.   long retry;
  375.                 /* return prototype for OP_PROTOTYPE call */
  376.   if (!stream) return user_flags (&unixproto);
  377.   retry = stream->silent ? 1 : KODRETRY;
  378.   if (stream->local) fatal ("unix recycle stream");
  379.   stream->local = memset (fs_get (sizeof (UNIXLOCAL)),0,sizeof (UNIXLOCAL));
  380.                 /* canonicalize the stream mailbox name */
  381.   dummy_file (tmp,stream->mailbox);
  382.                 /* flush old name */
  383.   fs_give ((void **) &stream->mailbox);
  384.                 /* save canonical name */
  385.   stream->mailbox = cpystr (tmp);
  386.   LOCAL->fd = LOCAL->ld = -1;    /* no file or state locking yet */
  387.   LOCAL->buf = (char *) fs_get ((LOCAL->buflen = CHUNK) + 1);
  388.   stream->sequence++;        /* bump sequence number */
  389.  
  390.                 /* make lock for read/write access */
  391.   if (!stream->rdonly) while (retry) {
  392.                 /* try to lock file */
  393.     if ((fd = lockname (tmp,stream->mailbox,LOCK_EX|LOCK_NB)) < 0) {
  394.       if (retry-- == KODRETRY) {/* no, first time through? */
  395.                 /* yes, get other process' PID */
  396.     if (!fstat (fd,&sbuf) && (i = min (sbuf.st_size,MAILTMPLEN)) &&
  397.         (read (fd,tmp,i) == i) && !(tmp[i] = 0) && (i = atol (tmp))) {
  398.       kill ((int) i,SIGUSR2);
  399.       sprintf (tmp,"Trying to get mailbox lock from process %lu",i);
  400.       mm_log (tmp,WARN);
  401.     }
  402.     else retry = 0;        /* give up */
  403.       }
  404.       if (!stream->silent) {    /* nothing if silent stream */
  405.     if (retry) sleep (1);    /* wait a second before trying again */
  406.     else mm_log ("Mailbox is open by another process, access is readonly",
  407.              WARN);
  408.       }
  409.     }
  410.     else {            /* got the lock, nobody else can alter state */
  411.       LOCAL->ld = fd;        /* note lock's fd and name */
  412.       LOCAL->lname = cpystr (tmp);
  413.                 /* make sure mode OK (don't use fchmod()) */
  414.       chmod (LOCAL->lname,(int) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
  415.       if (stream->silent) i = 0;/* silent streams won't accept KOD */
  416.       else {            /* note our PID in the lock */
  417.     sprintf (tmp,"%d",getpid ());
  418.     write (fd,tmp,(i = strlen (tmp))+1);
  419.       }
  420.       ftruncate (fd,i);        /* make sure tied off */
  421.       fsync (fd);        /* make sure it's available */
  422.       retry = 0;        /* no more need to try */
  423.     }
  424.   }
  425.  
  426.                 /* parse mailbox */
  427.   stream->nmsgs = stream->recent = 0;
  428.                 /* will we be able to get write access? */
  429.   if ((LOCAL->ld >= 0) && access (stream->mailbox,W_OK) && (errno == EACCES)) {
  430.     mm_log ("Can't get write access to mailbox, access is readonly",WARN);
  431.     flock (LOCAL->ld,LOCK_UN);    /* release the lock */
  432.     close (LOCAL->ld);        /* close the lock file */
  433.     LOCAL->ld = -1;        /* no more lock fd */
  434.     unlink (LOCAL->lname);    /* delete it */
  435.   }
  436.                 /* reset UID validity */
  437.   stream->uid_validity = stream->uid_last = 0;
  438.   if (stream->silent && !stream->rdonly && (LOCAL->ld < 0))
  439.     unix_abort (stream);    /* abort if can't get RW silent stream */
  440.                 /* parse mailbox */
  441.   else if (unix_parse (stream,tmp,LOCK_SH)) {
  442.     unix_unlock (LOCAL->fd,stream,tmp);
  443.     mail_unlock (stream);
  444.     mm_nocritical (stream);    /* done with critical */
  445.   }
  446.   if (!LOCAL) return NIL;    /* failure if stream died */
  447.                 /* make sure upper level knows readonly */
  448.   stream->rdonly = (LOCAL->ld < 0);
  449.                 /* notify about empty mailbox */
  450.   if (!(stream->nmsgs || stream->silent)) mm_log ("Mailbox is empty",NIL);
  451.   if (!stream->rdonly) {    /* flags stick if readwrite */
  452.     stream->perm_seen = stream->perm_deleted =
  453.       stream->perm_flagged = stream->perm_answered = stream->perm_draft = T;
  454.     if (!stream->uid_nosticky) {/* users with lives get permanent keywords */
  455.       stream->perm_user_flags = 0xffffffff;
  456.                 /* and maybe can create them too! */
  457.       stream->kwd_create = stream->user_flags[NUSERFLAGS-1] ? NIL : T;
  458.     }
  459.   }
  460.   return stream;        /* return stream alive to caller */
  461. }
  462.  
  463.  
  464. /* UNIX mail close
  465.  * Accepts: MAIL stream
  466.  *        close options
  467.  */
  468.  
  469. void unix_close (MAILSTREAM *stream,long options)
  470. {
  471.   int silent = stream->silent;
  472.   stream->silent = T;        /* go silent */
  473.                 /* expunge if requested */
  474.   if (options & CL_EXPUNGE) unix_expunge (stream);
  475.                 /* else dump final checkpoint */
  476.   else if (LOCAL->dirty) unix_check (stream);
  477.   stream->silent = NIL;        /* restore old silence state */
  478.   unix_abort (stream);        /* now punt the file and local data */
  479. }
  480.  
  481. /* UNIX mail fetch message header
  482.  * Accepts: MAIL stream
  483.  *        message # to fetch
  484.  *        pointer to returned header text length
  485.  *        option flags
  486.  * Returns: message header in RFC822 format
  487.  */
  488.  
  489.                 /* lines to filter from header */
  490. static STRINGLIST *unix_hlines = NIL;
  491.  
  492. char *unix_header (MAILSTREAM *stream,unsigned long msgno,
  493.            unsigned long *length,long flags)
  494. {
  495.   MESSAGECACHE *elt;
  496.   char *s;
  497.   *length = 0;            /* default to empty */
  498.   if (flags & FT_UID) return "";/* UID call "impossible" */
  499.   elt = mail_elt (stream,msgno);/* get cache */
  500.   if (!unix_hlines) {        /* once only code */
  501.     STRINGLIST *lines = unix_hlines = mail_newstringlist ();
  502.     lines->text.size = strlen ((char *) (lines->text.data =
  503.                      (unsigned char *) "Status"));
  504.     lines = lines->next = mail_newstringlist ();
  505.     lines->text.size = strlen ((char *) (lines->text.data =
  506.                      (unsigned char *) "X-Status"));
  507.     lines = lines->next = mail_newstringlist ();
  508.     lines->text.size = strlen ((char *) (lines->text.data =
  509.                      (unsigned char *) "X-Keywords"));
  510.     lines = lines->next = mail_newstringlist ();
  511.     lines->text.size = strlen ((char *) (lines->text.data =
  512.                      (unsigned char *) "X-UID"));
  513.   }
  514.                 /* go to header position */
  515.   lseek (LOCAL->fd,elt->private.special.offset +
  516.      elt->private.msg.header.offset,L_SET);
  517.   if (flags & FT_INTERNAL) {    /* initial data OK? */
  518.     if (elt->private.msg.header.text.size > LOCAL->buflen) {
  519.       fs_give ((void **) &LOCAL->buf);
  520.       LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
  521.                      elt->private.msg.header.text.size) + 1);
  522.     }
  523.                 /* read message */
  524.     read (LOCAL->fd,LOCAL->buf,elt->private.msg.header.text.size);
  525.                 /* got text, tie off string */
  526.     LOCAL->buf[*length = elt->private.msg.header.text.size] = '\0';
  527.   }
  528.   else {            /* need to make a CRLF version */
  529.     read (LOCAL->fd,s = (char *) fs_get (elt->private.msg.header.text.size+1),
  530.       elt->private.msg.header.text.size);
  531.                 /* tie off string, and convert to CRLF */
  532.     s[elt->private.msg.header.text.size] = '\0';
  533.     *length = strcrlfcpy (&LOCAL->buf,&LOCAL->buflen,s,
  534.               elt->private.msg.header.text.size);
  535.     fs_give ((void **) &s);    /* free readin buffer */
  536.   }
  537.   *length = mail_filter (LOCAL->buf,*length,unix_hlines,FT_NOT);
  538.   return LOCAL->buf;        /* return processed copy */
  539. }
  540.  
  541. /* UNIX mail fetch message text
  542.  * Accepts: MAIL stream
  543.  *        message # to fetch
  544.  *        pointer to returned stringstruct
  545.  *        option flags
  546.  * Returns: T on success, NIL if failure
  547.  */
  548.  
  549. long unix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
  550. {
  551.   char *s;
  552.   unsigned long i;
  553.   MESSAGECACHE *elt;
  554.                 /* UID call "impossible" */
  555.   if (flags & FT_UID) return NIL;
  556.   elt = mail_elt (stream,msgno);/* get cache element */
  557.                 /* if message not seen */
  558.   if (!(flags & FT_PEEK) && !elt->seen) {
  559.     elt->seen = T;        /* mark message as seen */
  560.     LOCAL->dirty = T;        /* note stream is now dirty */
  561.     mm_flags (stream,msgno);
  562.   }
  563.   s = unix_text_work (stream,elt,&i,flags);
  564.   INIT (bs,mail_string,s,i);    /* set up stringstruct */
  565.   return T;            /* success */
  566. }
  567.  
  568. /* UNIX mail fetch message text worker routine
  569.  * Accepts: MAIL stream
  570.  *        message cache element
  571.  *        pointer to returned header text length
  572.  *        option flags
  573.  */
  574.  
  575. char *unix_text_work (MAILSTREAM *stream,MESSAGECACHE *elt,
  576.               unsigned long *length,long flags)
  577. {
  578.   FDDATA d;
  579.   STRING bs;
  580.   char *s,tmp[CHUNK];
  581.                 /* go to text position */
  582.   lseek (LOCAL->fd,elt->private.special.offset +
  583.      elt->private.msg.text.offset,L_SET);
  584.   if (flags & FT_INTERNAL) {    /* initial data OK? */
  585.     if (elt->private.msg.text.text.size > LOCAL->buflen) {
  586.       fs_give ((void **) &LOCAL->buf);
  587.       LOCAL->buf = (char *) fs_get ((LOCAL->buflen =
  588.                      elt->private.msg.text.text.size) + 1);
  589.     }
  590.                 /* read message */
  591.     read (LOCAL->fd,LOCAL->buf,elt->private.msg.text.text.size);
  592.                 /* got text, tie off string */
  593.     LOCAL->buf[*length = elt->private.msg.text.text.size] = '\0';
  594.   }
  595.   else {            /* need to make a CRLF version */
  596.     if (elt->rfc822_size > LOCAL->buflen) {
  597.       /* excessively conservative, but the right thing is too hard to do */
  598.       fs_give ((void **) &LOCAL->buf);
  599.       LOCAL->buf = (char *) fs_get ((LOCAL->buflen = elt->rfc822_size) + 1);
  600.     }
  601.     d.fd = LOCAL->fd;        /* yes, set up file descriptor */
  602.     d.pos = elt->private.special.offset + elt->private.msg.text.offset;
  603.     d.chunk = tmp;        /* initial buffer chunk */
  604.     d.chunksize = CHUNK;    /* file chunk size */
  605.     INIT (&bs,fd_string,&d,elt->private.msg.text.text.size);
  606.     for (s = LOCAL->buf; SIZE (&bs);) switch (CHR (&bs)) {
  607.     case '\015':        /* carriage return seen */
  608.       *s++ = SNX (&bs);        /* copy it and any succeeding LF */
  609.       if (SIZE (&bs) && (CHR (&bs) == '\012')) *s++ = SNX (&bs);
  610.       break;
  611.     case '\012':
  612.       *s++ = '\015';        /* insert a CR */
  613.     default:
  614.       *s++ = SNX (&bs);        /* copy characters */
  615.     }
  616.     *s = '\0';            /* tie off buffer */
  617.     *length = s - LOCAL->buf;    /* calculate length */
  618.   }
  619.   return LOCAL->buf;
  620. }
  621.  
  622. /* UNIX per-message modify flag
  623.  * Accepts: MAIL stream
  624.  *        message cache element
  625.  */
  626.  
  627. void unix_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt)
  628. {
  629.                 /* only after finishing */
  630.   if (elt->valid) LOCAL->dirty = T;
  631. }
  632.  
  633.  
  634. /* UNIX mail ping mailbox
  635.  * Accepts: MAIL stream
  636.  * Returns: T if stream alive, else NIL
  637.  */
  638.  
  639. long unix_ping (MAILSTREAM *stream)
  640. {
  641.   char lock[MAILTMPLEN];
  642.   struct stat sbuf;
  643.                 /* big no-op if not readwrite */
  644.   if (LOCAL && (LOCAL->ld >= 0) && !stream->lock) {
  645.     if (stream->rdonly) {    /* does he want to give up readwrite? */
  646.                 /* checkpoint if we changed something */
  647.       if (LOCAL->dirty) unix_check (stream);
  648.       flock (LOCAL->ld,LOCK_UN);/* release readwrite lock */
  649.       close (LOCAL->ld);    /* close the readwrite lock file */
  650.       LOCAL->ld = -1;        /* no more readwrite lock fd */
  651.       unlink (LOCAL->lname);    /* delete the readwrite lock file */
  652.     }
  653.     else {            /* get current mailbox size */
  654.       if (LOCAL->fd >= 0) fstat (LOCAL->fd,&sbuf);
  655.       else stat (stream->mailbox,&sbuf);
  656.                 /* parse if mailbox changed */
  657.       if ((sbuf.st_size != LOCAL->filesize) &&
  658.       unix_parse (stream,lock,LOCK_SH)) {
  659.                 /* unlock mailbox */
  660.     unix_unlock (LOCAL->fd,stream,lock);
  661.     mail_unlock (stream);    /* and stream */
  662.     mm_nocritical (stream);    /* done with critical */
  663.       }
  664.     }
  665.   }
  666.   return LOCAL ? LONGT : NIL;    /* return if still alive */
  667. }
  668.  
  669. /* UNIX mail check mailbox
  670.  * Accepts: MAIL stream
  671.  */
  672.  
  673. void unix_check (MAILSTREAM *stream)
  674. {
  675.   char lock[MAILTMPLEN];
  676.                 /* parse and lock mailbox */
  677.   if (LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
  678.       unix_parse (stream,lock,LOCK_EX)) {
  679.                 /* any unsaved changes? */
  680.     if (LOCAL->dirty && unix_rewrite (stream,NIL)) {
  681.       unlink (lock);        /* flush the lock file */
  682.       if (!stream->silent) mm_log ("Checkpoint completed",NIL);
  683.     }
  684.                 /* no checkpoint needed, just unlock */
  685.     else unix_unlock (LOCAL->fd,stream,lock);
  686.     mail_unlock (stream);    /* unlock the stream */
  687.     mm_nocritical (stream);    /* done with critical */
  688.   }
  689. }
  690.  
  691.  
  692. /* UNIX mail expunge mailbox
  693.  * Accepts: MAIL stream
  694.  */
  695.  
  696. void unix_expunge (MAILSTREAM *stream)
  697. {
  698.   unsigned long i;
  699.   char lock[MAILTMPLEN];
  700.   char *msg = NIL;
  701.                 /* parse and lock mailbox */
  702.   if (LOCAL && (LOCAL->ld >= 0) && !stream->lock &&
  703.       unix_parse (stream,lock,LOCK_EX)) {
  704.                 /* count expunged messages if not dirty */
  705.     if (!LOCAL->dirty) for (i = 1; i <= stream->nmsgs; i++)
  706.       if (mail_elt (stream,i)->deleted) LOCAL->dirty = T;
  707.     if (!LOCAL->dirty) {    /* not dirty and no expunged messages */
  708.       unix_unlock (LOCAL->fd,stream,lock);
  709.       msg = "No messages deleted, so no update needed";
  710.     }
  711.     else if (unix_rewrite (stream,&i)) {
  712.       unlink (lock);        /* flush the lock file */
  713.       if (i) sprintf (msg = LOCAL->buf,"Expunged %lu messages",i);
  714.       else msg = "Mailbox checkpointed, but no messages expunged";
  715.     }
  716.                 /* rewrite failed */
  717.     else unix_unlock (LOCAL->fd,stream,lock);
  718.     mail_unlock (stream);    /* unlock the stream */
  719.     mm_nocritical (stream);    /* done with critical */
  720.     if (msg && !stream->silent) mm_log (msg,NIL);
  721.   }
  722.   else if (!stream->silent) mm_log("Expunge ignored on readonly mailbox",WARN);
  723. }
  724.  
  725. /* UNIX mail copy message(s)
  726.  * Accepts: MAIL stream
  727.  *        sequence
  728.  *        destination mailbox
  729.  *        copy options
  730.  * Returns: T if copy successful, else NIL
  731.  */
  732.  
  733. long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
  734. {
  735.   struct stat sbuf;
  736.   int fd;
  737.   char *s,file[MAILTMPLEN],lock[MAILTMPLEN];
  738.   time_t tp[2];
  739.   unsigned long i,j;
  740.   MESSAGECACHE *elt;
  741.   long ret = T;
  742.   mailproxycopy_t pc =
  743.     (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
  744.   if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
  745.     mail_sequence (stream,sequence))) return NIL;
  746.                 /* make sure valid mailbox */
  747.   if (!unix_isvalid (mailbox,file)) switch (errno) {
  748.   case ENOENT:            /* no such file? */
  749.     mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
  750.     return NIL;
  751.   case 0:            /* merely empty file? */
  752.     break;
  753.   case EINVAL:
  754.     if (pc) return (*pc) (stream,sequence,mailbox,options);
  755.     sprintf (LOCAL->buf,"Invalid UNIX-format mailbox name: %.80s",mailbox);
  756.     mm_log (LOCAL->buf,ERROR);
  757.     return NIL;
  758.   default:
  759.     if (pc) return (*pc) (stream,sequence,mailbox,options);
  760.     sprintf (LOCAL->buf,"Not a UNIX-format mailbox: %.80s",mailbox);
  761.     mm_log (LOCAL->buf,ERROR);
  762.     return NIL;
  763.   }
  764.   LOCAL->buf[0] = '\0';
  765.   mm_critical (stream);        /* go critical */
  766.   if ((fd = unix_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND|O_CREAT,
  767.                S_IREAD|S_IWRITE,lock,LOCK_EX)) < 0) {
  768.     mm_nocritical (stream);    /* done with critical */
  769.     sprintf (LOCAL->buf,"Can't open destination mailbox: %s",strerror (errno));
  770.     mm_log (LOCAL->buf,ERROR);    /* log the error */
  771.     return NIL;            /* failed */
  772.   }
  773.   fstat (fd,&sbuf);        /* get current file size */
  774.  
  775.                 /* write all requested messages to mailbox */
  776.   for (i = 1; ret && (i <= stream->nmsgs); i++)
  777.     if ((elt = mail_elt (stream,i))->sequence) {
  778.       lseek (LOCAL->fd,elt->private.special.offset,L_SET);
  779.       read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
  780.       if (write (fd,LOCAL->buf,elt->private.special.text.size) < 0) ret = NIL;
  781.       else {            /* internal header succeeded */
  782.     s = unix_header (stream,i,&j,FT_INTERNAL);
  783.                 /* header size, sans trailing newline */
  784.     if (j && (s[j - 2] == '\n')) j--;
  785.     if (write (fd,s,j) < 0) ret = NIL;
  786.     else {            /* message header succeeded */
  787.       j = unix_xstatus (stream,LOCAL->buf,elt,NIL);
  788.       if (write (fd,LOCAL->buf,j) < 0) ret = NIL;
  789.       else {        /* message status succeeded */
  790.         s = unix_text_work (stream,elt,&j,FT_INTERNAL);
  791.         if ((write (fd,s,j) < 0) || (write (fd,"\n",1) < 0)) ret = NIL;
  792.       }
  793.     }
  794.       }
  795.     }
  796.   if (!ret || fsync (fd)) {    /* force out the update */
  797.     sprintf (LOCAL->buf,"Message copy failed: %s",strerror (errno));
  798.     ftruncate (fd,sbuf.st_size);
  799.     ret = NIL;
  800.   }
  801.   tp[0] = sbuf.st_atime;    /* preserve atime */
  802.   tp[1] = time (0);        /* set mtime to now */
  803.   utime (file,tp);        /* set the times */
  804.   unix_unlock (fd,NIL,lock);    /* unlock and close mailbox */
  805.   mm_nocritical (stream);    /* release critical */
  806.                 /* log the error */
  807.   if (!ret) mm_log (LOCAL->buf,ERROR);
  808.                 /* delete if requested message */
  809.   else if (options & CP_MOVE) for (i = 1; i <= stream->nmsgs; i++)
  810.     if ((elt = mail_elt (stream,i))->sequence) {
  811.       elt->deleted = T;        /* mark message deleted */
  812.       LOCAL->dirty = T;        /* note stream is now dirty */
  813.     }
  814.   return ret;
  815. }
  816.  
  817. /* UNIX mail append message from stringstruct
  818.  * Accepts: MAIL stream
  819.  *        destination mailbox
  820.  *        initial flags
  821.  *        internal date
  822.  *        stringstruct of messages to append
  823.  * Returns: T if append successful, else NIL
  824.  */
  825.  
  826. #define BUFLEN 8*MAILTMPLEN
  827.  
  828. long unix_append (MAILSTREAM *stream,char *mailbox,char *flags,char *date,
  829.           STRING *message)
  830. {
  831.   MESSAGECACHE elt;
  832.   struct stat sbuf;
  833.   int fd,ti,zn;
  834.   long f,i,ok;
  835.   unsigned long j,n,uf,size;
  836.   char c,*x,buf[BUFLEN],tmp[MAILTMPLEN],file[MAILTMPLEN],lock[MAILTMPLEN];
  837.   time_t tp[2],t = time (0);
  838.                 /* default stream to prototype */
  839.   if (!stream) stream = user_flags (&unixproto);
  840.                 /* get flags */
  841.   f = mail_parse_flags (stream,flags,&uf);
  842.                 /* parse date */
  843.   if (!date) rfc822_date (date = tmp);
  844.   if (!mail_parse_date (&elt,date)) {
  845.     sprintf (buf,"Bad date in append: %.80s",date);
  846.     mm_log (buf,ERROR);
  847.     return NIL;
  848.   }
  849.                 /* make sure valid mailbox */
  850.   if (!unix_isvalid (mailbox,buf)) switch (errno) {
  851.   case ENOENT:            /* no such file? */
  852.     if (((mailbox[0] == 'I') || (mailbox[0] == 'i')) &&
  853.     ((mailbox[1] == 'N') || (mailbox[1] == 'n')) &&
  854.     ((mailbox[2] == 'B') || (mailbox[2] == 'b')) &&
  855.     ((mailbox[3] == 'O') || (mailbox[3] == 'o')) &&
  856.     ((mailbox[4] == 'X') || (mailbox[4] == 'x')) && !mailbox[5])
  857.       unix_create (NIL,"INBOX");
  858.     else {
  859.       mm_notify (stream,"[TRYCREATE] Must create mailbox before append",NIL);
  860.       return NIL;
  861.     }
  862.                 /* falls through */
  863.   case 0:            /* INBOX ENOENT or empty file? */
  864.     break;
  865.   case EINVAL:
  866.     sprintf (buf,"Invalid UNIX-format mailbox name: %.80s",mailbox);
  867.     mm_log (buf,ERROR);
  868.     return NIL;
  869.   default:
  870.     sprintf (buf,"Not a UNIX-format mailbox: %.80s",mailbox);
  871.     mm_log (buf,ERROR);
  872.     return NIL;
  873.   }
  874.  
  875.   mm_critical (stream);        /* go critical */
  876.   if ((fd = unix_lock (dummy_file (file,mailbox),O_WRONLY|O_APPEND|O_CREAT,
  877.                S_IREAD|S_IWRITE,lock,LOCK_EX)) < 0) {
  878.     mm_nocritical (stream);    /* done with critical */
  879.     sprintf (buf,"Can't open append mailbox: %s",strerror (errno));
  880.     mm_log (buf,ERROR);
  881.     return NIL;
  882.   }
  883.   fstat (fd,&sbuf);        /* get current file size */
  884.   sprintf (buf,"From %s@%s ",myusername (),mylocalhost ());
  885.                 /* write the date given */
  886.   mail_cdate (buf + strlen (buf),&elt);
  887.   sprintf (buf + strlen (buf),"Status: %s\nX-Status: %s%s%s%s\nX-Keywords:",
  888.        f&fSEEN ? "R" : "",f&fDELETED ? "D" : "",f&fFLAGGED ? "F" : "",
  889.        f&fANSWERED ? "A" : "",f&fDRAFT ? "T" : "");
  890.   while (uf)            /* write user flags */
  891.     sprintf(buf+strlen(buf)," %s",stream->user_flags[find_rightmost_bit(&uf)]);
  892.   strcat (buf,"\n");        /* tie off flags */
  893.                 /* copy text, tossing out CRs */
  894.   for (i = strlen (buf), ok = T, size = SIZE (message); ok && size; ) {
  895.     for (j = 0, c = '\0'; size && (c != '\n') && (j < MAILTMPLEN); size--)
  896.       if ((c = SNX (message)) != '\015') tmp[j++] = c;
  897.                 /* possible "From " line? */
  898.     if ((j > 4) && (tmp[0] == 'F') && (tmp[1] == 'r') && (tmp[2] == 'o') &&
  899.     (tmp[3] == 'm') && (tmp[4] == ' ')) {
  900.                 /* see if need to write a widget */
  901.       if (!(ti = unix_fromwidget || (c != '\n'))) VALID (tmp,x,ti,zn);
  902.       if (ti) ok = unix_append_putc (fd,buf,&i,'>');
  903.     }
  904.                 /* write the line */
  905.     for (n = 0; ok && (n < j); n++) ok = unix_append_putc (fd,buf,&i,tmp[n]);
  906.                 /* handle pathologically long line */
  907.     if (ok && (c != '\n') && size) do {
  908.       if ((c = SNX (message)) != '\015') ok = unix_append_putc (fd,buf,&i,c);
  909.     } while (ok && --size && (c != '\n'));
  910.   }
  911.                 /* write trailing newline */
  912.   if (!(ok && (ok = unix_append_putc (fd,buf,&i,'\n') &&
  913.            (i ? (write (fd,buf,i) >= 0) : T) && !fsync (fd)))) {
  914.     sprintf (buf,"Message append failed: %s",strerror (errno));
  915.     mm_log (buf,ERROR);
  916.     ftruncate (fd,sbuf.st_size);
  917.   }
  918.   tp[0] = sbuf.st_atime;    /* preserve atime */
  919.   tp[1] = time (0);        /* set mtime to now */
  920.   utime (file,tp);        /* set the times */
  921.   unix_unlock (fd,NIL,lock);    /* unlock and close mailbox */
  922.   mm_nocritical (stream);    /* release critical */
  923.   return ok;            /* return success */
  924. }
  925.  
  926. /* UNIX mail append character
  927.  * Accepts: file descriptor
  928.  *        output buffer
  929.  *        pointer to current size of output buffer
  930.  *        character to append
  931.  * Returns: T if append successful, else NIL
  932.  */
  933.  
  934. long unix_append_putc (int fd,char *s,long *i,char c)
  935. {
  936.   s[(*i)++] = c;
  937.   if (*i == BUFLEN) {        /* dump if buffer filled */
  938.     if (write (fd,s,*i) < 0) return NIL;
  939.     *i = 0;            /* reset */
  940.   }
  941.   return T;
  942. }
  943.  
  944. /* Internal routines */
  945.  
  946.  
  947. /* UNIX mail abort stream
  948.  * Accepts: MAIL stream
  949.  */
  950.  
  951. void unix_abort (MAILSTREAM *stream)
  952. {
  953.   if (LOCAL) {            /* only if a file is open */
  954.     if (LOCAL->fd >= 0) close (LOCAL->fd);
  955.     if (LOCAL->ld >= 0) {    /* have a mailbox lock? */
  956.       flock (LOCAL->ld,LOCK_UN);/* yes, release the lock */
  957.       close (LOCAL->ld);    /* close the lock file */
  958.       unlink (LOCAL->lname);    /* and delete it */
  959.     }
  960.     if (LOCAL->lname) fs_give ((void **) &LOCAL->lname);
  961.                 /* free local text buffers */
  962.     if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
  963.     if (LOCAL->line) fs_give ((void **) &LOCAL->line);
  964.                 /* nuke the local data */
  965.     fs_give ((void **) &stream->local);
  966.     stream->dtb = NIL;        /* log out the DTB */
  967.   }
  968. }
  969.  
  970. /* UNIX open and lock mailbox
  971.  * Accepts: file name to open/lock
  972.  *        file open mode
  973.  *        destination buffer for lock file name
  974.  *        type of locking operation (LOCK_SH or LOCK_EX)
  975.  */
  976.  
  977. int unix_lock (char *file,int flags,int mode,char *lock,int op)
  978. {
  979.   int fd,ld,j;
  980.   int i = LOCKTIMEOUT * 60 - 1;
  981.   char hitch[MAILTMPLEN],tmp[MAILTMPLEN];
  982.   time_t t;
  983.   struct stat sb;
  984.   sprintf (lock,"%s.lock",file);/* build lock filename */
  985.   if (chk_notsymlink (lock)) do{/* until OK or out of tries */
  986.     t = time (0);        /* get the time now */
  987. #ifdef NFSKLUDGE
  988.   /* SUN-OS had an NFS, As kludgy as an albatross;
  989.    * And everywhere that it was installed, It was a total loss.  -- MRC 9/25/91
  990.    */
  991.                 /* build hitching post file name */
  992.     sprintf (hitch,"%s.%d.%d.",lock,time (0),getpid ());
  993.     j = strlen (hitch);        /* append local host name */
  994.     gethostname (hitch + j,(MAILTMPLEN - j) - 1);
  995.                 /* try to get hitching-post file */
  996.     if ((ld = open (hitch,O_WRONLY|O_CREAT|O_EXCL,
  997.             (int) mail_parameters (NIL,GET_LOCKPROTECTION,NIL))) < 0) {
  998.       switch (errno) {        /* what happened? */
  999.       case EEXIST:        /* file already exists? */
  1000.     break;            /* oops, just try again */
  1001.       case EACCES:        /* protection failure */
  1002.     if (stat (hitch,&sb)) {    /* try again if file exists(?) */
  1003.                 /* punt silently if paranoid site */
  1004.       if (mail_parameters (NIL,GET_LOCKEACCESERROR,NIL))
  1005.         mm_log ("Mailbox vulnerable - directory must have 1777 protection",
  1006.             WARN);
  1007.       *lock = '\0';        /* give up on lock file */
  1008.     }
  1009.     break;
  1010.       default:            /* some other error */
  1011.     sprintf (tmp,"Mailbox vulnerable - error creating %.80s: %s",
  1012.          hitch,strerror (errno));
  1013.     mm_log (tmp,WARN);    /* this is probably not good */
  1014.     *lock = '\0';        /* give up on lock file */
  1015.     break;
  1016.       }
  1017.     }
  1018.     else {            /* got a hitching-post */
  1019.                 /* make sure others can break the lock */
  1020.       chmod (hitch,(int) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
  1021.       close (ld);        /* close the hitching-post */
  1022.       link (hitch,lock);    /* tie hitching-post to lock, ignore failure */
  1023.       stat (hitch,&sb);        /* get its data */
  1024.       unlink (hitch);        /* flush hitching post */
  1025.       /* If link count .ne. 2, hitch failed.  Set ld to -1 as if open() failed
  1026.      so we try again.  If extant lock file and time now is .gt. file time
  1027.      plus timeout interval, flush the lock so can win next time around. */
  1028.       if ((ld = (sb.st_nlink != 2) ? -1 : 0) && (!stat (lock,&sb)) &&
  1029.       (t > sb.st_ctime + LOCKTIMEOUT * 60)) unlink (lock);
  1030.     }
  1031.  
  1032. #else
  1033.   /* This works on modern Unix systems which are not afflicted with NFS mail.
  1034.    * "Modern" means that O_EXCL works.  I think that NFS mail is a terrible
  1035.    * idea -- that's what IMAP is for -- but some people insist upon losing...
  1036.    */
  1037.                 /* try to get the lock */
  1038.     if ((ld = open (lock,O_WRONLY|O_CREAT|O_EXCL,
  1039.             (int) mail_parameters (NIL,GET_LOCKPROTECTION,NIL))) < 0)
  1040.       switch (errno) {        /* what happened? */
  1041.       case EEXIST:        /* if extant and old, grab it for ourselves */
  1042.     if ((!stat (lock,&sb)) && (t > sb.st_ctime + LOCKTIMEOUT * 60))
  1043.       ld = open (lock,O_WRONLY|O_CREAT,
  1044.              (int) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
  1045.     break;
  1046.       case EACCES:        /* protection fail, ignore if non-ex or old */
  1047.     if (stat (lock,&sb)) {    /* try again if file exists(?) */
  1048.                 /* punt silently if paranoid site */
  1049.       if (mail_parameters (NIL,GET_LOCKEACCESERROR,NIL))
  1050.         mm_log ("Mailbox vulnerable - directory must have 1777 protection",
  1051.             WARN);
  1052.       *lock = '\0';        /* give up on lock file */
  1053.     }
  1054.     else if (t > sb.st_ctime + LOCKTIMEOUT * 60) unlink (lock);
  1055.     break;
  1056.       default:            /* some other failure */
  1057.     sprintf (tmp,"Mailbox vulnerable - error creating %.80s: %s",
  1058.            lock,strerror (errno));
  1059.     mm_log (tmp,WARN);    /* this is probably not good */
  1060.     *lock = '\0';        /* don't use lock files */
  1061.     break;
  1062.       }
  1063.     if (ld >= 0) {        /* if made a lock file */
  1064.                 /* make sure others can break the lock */
  1065.       chmod (lock,(int) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));
  1066.       close (ld);        /* close the lock file */
  1067.     }
  1068. #endif
  1069.     if ((ld < 0) && *lock) {    /* if failed to make lock file and retry OK */
  1070.       if (!(i%15)) {
  1071.     sprintf (tmp,"Mailbox %.80s is locked, will override in %d seconds...",
  1072.          file,i);
  1073.     mm_log (tmp,WARN);
  1074.       }
  1075.       sleep (1);        /* wait 1 second before next try */
  1076.     }
  1077.   } while (i-- && ld < 0 && *lock);
  1078.                 /* open file */
  1079.   if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
  1080.   else {            /* open failed */
  1081.     j = errno;            /* preserve error code */
  1082.     if (*lock) unlink (lock);    /* flush the lock file if any */
  1083.     errno = j;            /* restore error code */
  1084.   }
  1085.   return fd;
  1086. }
  1087.  
  1088. /* UNIX unlock and close mailbox
  1089.  * Accepts: file descriptor
  1090.  *        (optional) mailbox stream to check atime/mtime
  1091.  *        (optional) lock file name
  1092.  */
  1093.  
  1094. void unix_unlock (int fd,MAILSTREAM *stream,char *lock)
  1095. {
  1096.   struct stat sbuf;
  1097.   time_t tp[2];
  1098.   fstat (fd,&sbuf);        /* get file times */
  1099.                 /* if stream and csh would think new mail */
  1100.   if (stream && (sbuf.st_atime <= sbuf.st_mtime)) {
  1101.     tp[0] = time (0);        /* set atime to now */
  1102.                 /* set mtime to (now - 1) if necessary */
  1103.     tp[1] = tp[0] > sbuf.st_mtime ? sbuf.st_mtime : tp[0] - 1;
  1104.                 /* set the times, note change */
  1105.     if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
  1106.   }
  1107.   flock (fd,LOCK_UN);        /* release flock'ers */
  1108.   if (!stream) close (fd);    /* close the file if no stream */
  1109.                 /* flush the lock file if any */
  1110.   if (lock && *lock) unlink (lock);
  1111. }
  1112.  
  1113. /* UNIX mail parse and lock mailbox
  1114.  * Accepts: MAIL stream
  1115.  *        space to write lock file name
  1116.  *        type of locking operation
  1117.  * Returns: T if parse OK, critical & mailbox is locked shared; NIL if failure
  1118.  */
  1119.  
  1120. int unix_parse (MAILSTREAM *stream,char *lock,int op)
  1121. {
  1122.   int zn;
  1123.   unsigned long i,j,k;
  1124.   char c,*s,*t,*u,tmp[MAILTMPLEN],date[30];
  1125.   int ti = 0,pseudoseen = NIL;
  1126.   unsigned long nmsgs = stream->nmsgs;
  1127.   unsigned long prevuid = nmsgs ? mail_elt (stream,nmsgs)->private.uid : 0;
  1128.   unsigned long recent = stream->recent;
  1129.   unsigned long oldnmsgs = stream->nmsgs;
  1130.   short silent = stream->silent;
  1131.   struct stat sbuf;
  1132.   STRING bs;
  1133.   FDDATA d;
  1134.   MESSAGECACHE *elt;
  1135.   mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
  1136.   mail_lock (stream);        /* guard against recursion or pingers */
  1137.                 /* toss out previous descriptor */
  1138.   if (LOCAL->fd >= 0) close (LOCAL->fd);
  1139.   mm_critical (stream);        /* open and lock mailbox (shared OK) */
  1140.   if ((LOCAL->fd = unix_lock (stream->mailbox,(LOCAL->ld >= 0) ?
  1141.                   O_RDWR : O_RDONLY,NIL,lock,op)) < 0) {
  1142.     sprintf (tmp,"Mailbox open failed, aborted: %s",strerror (errno));
  1143.     mm_log (tmp,ERROR);
  1144.     unix_abort (stream);
  1145.     mail_unlock (stream);
  1146.     mm_nocritical (stream);    /* done with critical */
  1147.     return NIL;
  1148.   }
  1149.   fstat (LOCAL->fd,&sbuf);    /* get status */
  1150.                 /* validate change in size */
  1151.   if (sbuf.st_size < LOCAL->filesize) {
  1152.     sprintf (tmp,"Mailbox shrank from %lu to %lu bytes, aborted",
  1153.          LOCAL->filesize,sbuf.st_size);
  1154.     mm_log (tmp,ERROR);        /* this is pretty bad */
  1155.     unix_unlock (LOCAL->fd,stream,lock);
  1156.     unix_abort (stream);
  1157.     mail_unlock (stream);
  1158.     mm_nocritical (stream);    /* done with critical */
  1159.     return NIL;
  1160.   }
  1161.  
  1162.                 /* new data? */
  1163.   else if (i = sbuf.st_size - LOCAL->filesize) {
  1164.     d.fd = LOCAL->fd;        /* yes, set up file descriptor */
  1165.     d.pos = LOCAL->filesize;    /* get to that position in the file */
  1166.     d.chunk = LOCAL->buf;    /* initial buffer chunk */
  1167.     d.chunksize = CHUNK;    /* file chunk size */
  1168.     INIT (&bs,fd_string,&d,i);    /* initialize stringstruct */
  1169.                 /* skip leading whitespace for broken MTAs */
  1170.     while (((c = CHR (&bs)) == '\n') || (c == ' ') || (c == '\t')) SNX (&bs);
  1171.     if (SIZE (&bs)) {        /* read new data */
  1172.                 /* remember internal header position */
  1173.       j = LOCAL->filesize + GETPOS (&bs);
  1174.       s = unix_mbxline (stream,&bs,&i);
  1175.       if (i) VALID (s,t,ti,zn);    /* see if valid From line */
  1176.       if (!ti) {        /* someone pulled the rug from under us */
  1177.     sprintf(tmp,"Unexpected changes to mailbox (try restarting): %.20s",s);
  1178.     mm_log (tmp,ERROR);
  1179.     unix_unlock (LOCAL->fd,stream,lock);
  1180.     unix_abort (stream);
  1181.     mail_unlock (stream);
  1182.     mm_nocritical (stream);    /* done with critical */
  1183.     return NIL;
  1184.       }
  1185.       stream->silent = T;    /* quell main program new message events */
  1186.       do {            /* found a message */
  1187.                 /* instantiate first new message */
  1188.     mail_exists (stream,++nmsgs);
  1189.     (elt = mail_elt (stream,nmsgs))->valid = T;
  1190.     recent++;        /* assume recent by default */
  1191.     elt->recent = T;
  1192.                 /* note position/size of internal header */
  1193.     elt->private.special.offset = j;
  1194.     elt->private.msg.header.offset = elt->private.special.text.size = i;
  1195.  
  1196.                 /* generate plausible IMAPish date string */
  1197.     date[2] = date[6] = date[20] = '-'; date[11] = ' ';
  1198.     date[14] = date[17] = ':';
  1199.                 /* dd */
  1200.     date[0] = t[ti - 2]; date[1] = t[ti - 1];
  1201.                 /* mmm */
  1202.     date[3] = t[ti - 6]; date[4] = t[ti - 5]; date[5] = t[ti - 4];
  1203.                 /* hh */
  1204.     date[12] = t[ti + 1]; date[13] = t[ti + 2];
  1205.                 /* mm */
  1206.     date[15] = t[ti + 4]; date[16] = t[ti + 5];
  1207.     if (t[ti += 6] == ':') {/* ss */
  1208.       date[18] = t[++ti]; date[19] = t[++ti];
  1209.       ti++;            /* move to space */
  1210.     }
  1211.     else date[18] = date[19] = '0';
  1212.                 /* yy -- advance over timezone if necessary */
  1213.     if (zn == ti) ti += (((t[zn+1] == '+') || (t[zn+1] == '-')) ? 6 : 4);
  1214.     date[7] = t[ti + 1]; date[8] = t[ti + 2];
  1215.     date[9] = t[ti + 3]; date[10] = t[ti + 4];
  1216.                 /* zzz */
  1217.     t = zn ? (t + zn + 1) : "LCL";
  1218.     date[21] = *t++; date[22] = *t++; date[23] = *t++;
  1219.     if ((date[21] != '+') && (date[21] != '-')) date[24] = '\0';
  1220.     else {            /* numeric time zone */
  1221.       date[24] = *t++; date[25] = *t++;
  1222.       date[26] = '\0'; date[20] = ' ';
  1223.     }
  1224.                 /* set internal date */
  1225.     if (!mail_parse_date (elt,date)) {
  1226.       sprintf (tmp,"Unable to parse internal date: %s",date);
  1227.       mm_log (tmp,WARN);
  1228.     }
  1229.  
  1230.     do {            /* look for message body */
  1231.       s = t = unix_mbxline (stream,&bs,&i);
  1232.       if (i) switch (*s) {    /* check header lines */
  1233.       case 'X':        /* possible X-???: line */
  1234.         if (s[1] == '-') {    /* must be immediately followed by hyphen */
  1235.                 /* X-Status: becomes Status: in S case */
  1236.           if (s[2] == 'S' && s[3] == 't' && s[4] == 'a' && s[5] == 't' &&
  1237.           s[6] == 'u' && s[7] == 's' && s[8] == ':') s += 2;
  1238.                 /* possible X-Keywords */
  1239.           else if (s[2] == 'K' && s[3] == 'e' && s[4] == 'y' &&
  1240.                s[5] == 'w' && s[6] == 'o' && s[7] == 'r' &&
  1241.                s[8] == 'd' && s[9] == 's' && s[10] == ':') {
  1242.         char uf[MAILTMPLEN];
  1243.         s += 11;    /* flush leading whitespace */
  1244.         while (*s && (*s != '\n')) {
  1245.           while (*s == ' ') s++;
  1246.                 /* find end of keyword */
  1247.           if (!(u = strpbrk (s," \n"))) u = s + strlen (s);
  1248.                 /* got a keyword? */
  1249.           if ((k = (u - s)) && (k < MAILTMPLEN)) {
  1250.                 /* copy keyword */
  1251.             strncpy (uf,s,k);
  1252.             uf[k] = '\0';    /* make sure tied off */
  1253.             ucase (uf);    /* coerce upper case */
  1254.             for (j = 0; (j<NUSERFLAGS) && stream->user_flags[j]; ++j)
  1255.               if (!strcmp(uf,
  1256.                   ucase (strcpy(tmp,stream->user_flags[j])))) {
  1257.             elt->user_flags |= ((long) 1) << j;
  1258.             break;
  1259.               }
  1260.                 /* need to create it? */
  1261.             if (!stream->rdonly && (j < NUSERFLAGS) &&
  1262.             !stream->user_flags[j]) {
  1263.                 /* set the bit */
  1264.               *uf |= 1 << j;
  1265.               stream->user_flags[j] = (char *) fs_get (k + 1);
  1266.               strncpy (stream->user_flags[j],s,k);
  1267.               stream->user_flags[j][k] = '\0';
  1268.                 /* if now out of user flags */
  1269.               if (j == NUSERFLAGS - 1) stream->kwd_create = NIL;
  1270.             }
  1271.           }
  1272.           s = u;    /* advance to next keyword */
  1273.         }
  1274.         break;
  1275.           }
  1276.  
  1277.                 /* possible X-IMAP */
  1278.           else if (s[2] == 'I' && s[3] == 'M' && s[4] == 'A' &&
  1279.                s[5] == 'P' && s[6] == ':') {
  1280.         if ((nmsgs == 1) && !stream->uid_validity) {
  1281.           s += 7;    /* advance to data */
  1282.                 /* flush whitespace */
  1283.           while (*s == ' ') s++;
  1284.           j = 0;    /* slurp UID validity */
  1285.                 /* found a digit? */
  1286.           while (isdigit (*s)) {
  1287.             j *= 10;    /* yes, add it in */
  1288.             j += *s++ - '0';
  1289.           }
  1290.           if (!j) break; /* punt if invalid UID validity */
  1291.           stream->uid_validity = j;
  1292.                 /* flush whitespace */
  1293.           while (*s == ' ') s++;
  1294.                 /* must have UID last too */
  1295.           if (isdigit (*s)) {
  1296.             j = 0;    /* slurp UID last */
  1297.             while (isdigit (*s)) {
  1298.               j *= 10;    /* yes, add it in */
  1299.               j += *s++ - '0';
  1300.             }
  1301.             stream->uid_last = j;
  1302.                 /* process keywords */
  1303.             for (j = 0; *s != '\n'; j++) {
  1304.                 /* flush leading whitespace */
  1305.               while (*s == ' ') s++;
  1306.               u = strpbrk (s," \n");
  1307.                 /* got a keyword? */
  1308.               if ((k = (u - s)) && j < NUSERFLAGS) {
  1309.             if (stream->user_flags[j])
  1310.               fs_give ((void **) &stream->user_flags[j]);
  1311.             stream->user_flags[j] = (char *) fs_get (k + 1);
  1312.             strncpy (stream->user_flags[j],s,k);
  1313.             stream->user_flags[j][k] = '\0';
  1314.               }
  1315.               s = u;    /* advance to next keyword */
  1316.             }
  1317.                 /* pseudo-header seen */
  1318.             pseudoseen = T;
  1319.           }
  1320.         }
  1321.         break;
  1322.           }
  1323.  
  1324.                 /* possible X-UID */
  1325.           else if (s[2] == 'U' && s[3] == 'I' && s[4] == 'D' &&
  1326.                s[5] == ':') {
  1327.                 /* only believe if have a UID validity */
  1328.         if (stream->uid_validity && (nmsgs > 1)) {
  1329.           s += 6;    /* advance to UID value */
  1330.                 /* flush whitespace */
  1331.           while (*s == ' ') s++;
  1332.           j = 0;
  1333.                 /* found a digit? */
  1334.           while (isdigit (*s)) {
  1335.             j *= 10;    /* yes, add it in */
  1336.             j += *s++ - '0';
  1337.           }
  1338.                 /* flush remainder of line */
  1339.           while (*s != '\n') s++;
  1340.                 /* make sure not duplicated */
  1341.           if (elt->private.uid)
  1342.             sprintf (tmp,"Message %lu UID %lu already has UID %lu",
  1343.                  pseudoseen ? elt->msgno - 1 : elt->msgno,
  1344.                  j,elt->private.uid);
  1345.                 /* make sure UID doesn't go backwards */
  1346.           else if (j <= prevuid)
  1347.             sprintf (tmp,"Message %lu UID %lu less than %lu",
  1348.                  pseudoseen ? elt->msgno - 1 : elt->msgno,
  1349.                  j,prevuid + 1);
  1350.                 /* or skip by mailbox's recorded last */
  1351.           else if (j > stream->uid_last)
  1352.             sprintf (tmp,"Message %lu UID %lu greater than last %lu",
  1353.                  pseudoseen ? elt->msgno - 1 : elt->msgno,
  1354.                  j,stream->uid_last);
  1355.           else {    /* normal UID case */
  1356.             prevuid = elt->private.uid = j;
  1357.             break;        /* exit this cruft */
  1358.           }
  1359.           mm_log (tmp,WARN);
  1360.                 /* invalidate UID validity */
  1361.           stream->uid_validity = 0;
  1362.           elt->private.uid = 0;
  1363.         }
  1364.         break;
  1365.           }
  1366.         }
  1367.                 /* otherwise fall into S case */
  1368.  
  1369.       case 'S':        /* possible Status: line */
  1370.         if (s[0] == 'S' && s[1] == 't' && s[2] == 'a' && s[3] == 't' &&
  1371.         s[4] == 'u' && s[5] == 's' && s[6] == ':') {
  1372.           s += 6;        /* advance to status flags */
  1373.           do switch (*s++) {/* parse flags */
  1374.           case 'R':        /* message read */
  1375.         elt->seen = T;
  1376.         break;
  1377.           case 'O':        /* message old */
  1378.         if (elt->recent) {
  1379.           elt->recent = NIL;
  1380.           recent--;    /* it really wasn't recent */
  1381.         }
  1382.         break;
  1383.           case 'D':        /* message deleted */
  1384.         elt->deleted = T;
  1385.         break;
  1386.           case 'F':        /* message flagged */
  1387.         elt->flagged = T;
  1388.         break;
  1389.           case 'A':        /* message answered */
  1390.         elt->answered = T;
  1391.         break;
  1392.           case 'T':        /* message is a draft */
  1393.         elt->draft = T;
  1394.         break;
  1395.           default:        /* some other crap */
  1396.         break;
  1397.           } while (*s && *s != '\n');
  1398.           break;        /* all done */
  1399.         }
  1400.                 /* otherwise fall into default case */
  1401.       default:        /* ordinary header line */
  1402.         elt->rfc822_size += i + 1;
  1403.         break;
  1404.       }
  1405.     } while (i && (*t != '\n'));
  1406.                 /* assign a UID if none found */
  1407.     if (((nmsgs > 1) || !pseudoseen) && !elt->private.uid)
  1408.       prevuid = elt->private.uid = ++stream->uid_last;
  1409.  
  1410.                 /* note size of header, location of text */
  1411.     elt->private.msg.header.text.size = 
  1412.       (elt->private.msg.text.offset =
  1413.        (LOCAL->filesize + GETPOS (&bs)) - elt->private.special.offset) -
  1414.          elt->private.special.text.size;
  1415.     k = 0;            /* no previous line size yet */
  1416.                 /* note current position */
  1417.     j = LOCAL->filesize + GETPOS (&bs);
  1418.     if (i) do {        /* look for next message */
  1419.       s = unix_mbxline (stream,&bs,&i);
  1420.       if (i) {        /* got new data? */
  1421.         VALID (s,t,ti,zn);    /* yes, parse line */
  1422.         if (!ti) {        /* not a header line, add it to message */
  1423.           elt->rfc822_size += 
  1424.         k = i + (((i > 1) && s[i - 2] == '\015') ? 0 : 1);
  1425.                 /* update current position */
  1426.           j = LOCAL->filesize + GETPOS (&bs);
  1427.         }
  1428.       }
  1429.     } while (i && !ti);    /* until found a header */
  1430.     elt->private.msg.text.text.size = j -
  1431.       (elt->private.special.offset + elt->private.msg.text.offset);
  1432.     if (k == 2) {        /* last line was blank? */
  1433.       elt->private.msg.text.text.size--;
  1434.       elt->rfc822_size -= 2;
  1435.     }
  1436.       } while (i);        /* until end of buffer */
  1437.       if (pseudoseen) {        /* flush pseudo-message if present */
  1438.                 /* decrement recent count */
  1439.     if (mail_elt (stream,1)->recent) recent--;
  1440.                 /* and the exists count */
  1441.     mail_exists (stream,nmsgs--);
  1442.     mail_expunged(stream,1);/* fake an expunge of that message */
  1443.       }
  1444.                 /* need to start a new UID validity? */
  1445.       if (!stream->uid_validity) {
  1446.     stream->uid_validity = time (0);
  1447.                 /* in case a whiner with no life */
  1448.     if (mail_parameters (NIL,GET_USERHASNOLIFE,NIL))
  1449.       stream->uid_nosticky = T;
  1450.     else LOCAL->dirty = T;    /* make dirty to create pseudo-message */
  1451.       }
  1452.       stream->nmsgs = oldnmsgs;    /* whack it back down */
  1453.       stream->silent = silent;    /* restore old silent setting */
  1454.                 /* notify upper level of new mailbox sizes */
  1455.       mail_exists (stream,nmsgs);
  1456.       mail_recent (stream,recent);
  1457.                 /* mark dirty so O flags are set */
  1458.       if (recent) LOCAL->dirty = T;
  1459.     }
  1460.   }
  1461.                 /* no change, don't babble if never got time */
  1462.   else if (LOCAL->filetime && LOCAL->filetime != sbuf.st_mtime)
  1463.     mm_log ("New mailbox modification time but apparently no changes",WARN);
  1464.                 /* update parsed file size and time */
  1465.   LOCAL->filesize = sbuf.st_size;
  1466.   LOCAL->filetime = sbuf.st_mtime;
  1467.   return T;            /* return the winnage */
  1468. }
  1469.  
  1470. /* UNIX read line from mailbox
  1471.  * Accepts: mail stream
  1472.  *        stringstruct
  1473.  *        pointer to line size
  1474.  * Returns: pointer to input line
  1475.  */
  1476.  
  1477. char *unix_mbxline (MAILSTREAM *stream,STRING *bs,unsigned long *size)
  1478. {
  1479.   unsigned long i,j,k,m;
  1480.   char p1[CHUNK];
  1481.   char *ret = "";
  1482.                 /* flush old buffer */
  1483.   if (LOCAL->line) fs_give ((void **) &LOCAL->line);
  1484.                 /* if buffer needs refreshing */
  1485.   if (!bs->cursize) SETPOS (bs,GETPOS (bs));
  1486.   if (SIZE (bs)) {        /* find newline */
  1487.     for (i = 0; (i < bs->cursize) && (bs->curpos[i] != '\n'); i++);
  1488.     if (i == bs->cursize) {    /* difficult case if line spans buffer */
  1489.       memcpy (p1,bs->curpos,i);    /* remember what we have so far */
  1490.                 /* load next buffer */
  1491.       SETPOS (bs,k = GETPOS (bs) + i);
  1492.       for (j = 0; (j < bs->cursize) && (bs->curpos[j] != '\n'); j++);
  1493.       if (j == bs->cursize) {    /* huge line? */
  1494.     SETPOS (bs,GETPOS (bs) + j);
  1495.                 /* look for end of line */
  1496.     for (m = SIZE (bs); m && (SNX (bs) != '\n'); --m,++j);
  1497.     SETPOS (bs,k);        /* go back to where it started */
  1498.       }
  1499.       ret = LOCAL->line = (char *) fs_get (i + j + 2);
  1500.       memcpy (ret,p1,i);    /* copy first chunk */
  1501.       while (j) {        /* copy remainder */
  1502.     if (!bs->cursize) SETPOS (bs,GETPOS (bs));
  1503.     memcpy (ret + i,bs->curpos,k = min (j,bs->cursize));
  1504.     i += k;            /* account for this much read in */
  1505.     j -= k;
  1506.     bs->curpos += k;    /* increment new position */
  1507.     bs->cursize -= k;    /* eat that many bytes */
  1508.       }
  1509.       if (SIZE (bs)) SNX (bs);    /* skip over newline if one seen */
  1510.       ret[i++] = '\n';        /* make sure newline at end */
  1511.       ret[i] = '\0';        /* makes debugging easier */
  1512.     }
  1513.     else {            /* this is easy */
  1514.       ret = bs->curpos;        /* string it at this position */
  1515.       bs->curpos += ++i;    /* increment new position */
  1516.       bs->cursize -= i;        /* eat that many bytes */
  1517.     }
  1518.     *size = i;            /* return that to user */
  1519.   }
  1520.   else *size = 0;        /* end of data, return empty */
  1521.   return ret;
  1522. }
  1523.  
  1524. /* UNIX make pseudo-header
  1525.  * Accepts: MAIL stream
  1526.  *        buffer to write pseudo-header
  1527.  * Returns: length of pseudo-header
  1528.  */
  1529.  
  1530. unsigned long unix_pseudo (MAILSTREAM *stream,char *hdr)
  1531. {
  1532.   int i;
  1533.   char *s;
  1534.   time_t t = time(0);
  1535.   sprintf (hdr,"From %s %sDate: ",pseudo_from,ctime (&t));
  1536.   rfc822_date (s = hdr + strlen (hdr));
  1537.   sprintf (s += strlen (s),    /* write the pseudo-header */
  1538.        "\nFrom: %s <%s@%s>\nSubject: %s\nX-IMAP: %010lu %010lu",
  1539.        pseudo_name,pseudo_from,mylocalhost (),pseudo_subject,
  1540.        stream->uid_validity,stream->uid_last);
  1541.   for (i = 0; i < NUSERFLAGS; ++i) if (stream->user_flags[i])
  1542.     sprintf (s += strlen (s)," %s",stream->user_flags[i]);
  1543.   sprintf (s += strlen (s),"\nStatus: RO\n\n%s\n\n",pseudo_msg);
  1544.   return strlen (hdr);
  1545. }
  1546.  
  1547. /* UNIX make status string
  1548.  * Accepts: MAIL stream
  1549.  *        destination string to write
  1550.  *        message cache entry
  1551.  *        non-zero flag to write UID as well
  1552.  * Returns: length of string
  1553.  */
  1554.  
  1555. unsigned long unix_xstatus (MAILSTREAM *stream,char *status,MESSAGECACHE *elt,
  1556.                 long flag)
  1557. {
  1558.   char *t;
  1559.   char *s = status;
  1560.   unsigned long uf = elt->user_flags;
  1561.   /* This used to be an sprintf(), but thanks to certain cretinous C libraries
  1562.      with horribly slow implementations of sprintf() I had to change it to this
  1563.      mess.  At least it should be fast. */
  1564.   *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't'; *s++ = 'u'; *s++ = 's';
  1565.   *s++ = ':'; *s++ = ' ';
  1566.   if (elt->seen) *s++ = 'R';
  1567.   *s++ = 'O'; *s++ = '\n';
  1568.   *s++ = 'X'; *s++ = '-'; *s++ = 'S'; *s++ = 't'; *s++ = 'a'; *s++ = 't';
  1569.   *s++ = 'u'; *s++ = 's'; *s++ = ':'; *s++ = ' ';
  1570.   if (elt->deleted) *s++ = 'D';
  1571.   if (elt->flagged) *s++ = 'F';
  1572.   if (elt->answered) *s++ = 'A';
  1573.   if (elt->draft) *s++ = 'T';
  1574.   *s++ = '\n';
  1575.   if (!stream->uid_nosticky) {    /* cretins with no life can't use this */
  1576.     *s++ = 'X'; *s++ = '-'; *s++ = 'K'; *s++ = 'e'; *s++ = 'y'; *s++ = 'w';
  1577.     *s++ = 'o'; *s++ = 'r'; *s++ = 'd'; *s++ = 's'; *s++ = ':';
  1578.     while (uf) {
  1579.       *s++ = ' ';
  1580.       for (t = stream->user_flags[find_rightmost_bit (&uf)]; *t; *s++ = *t++);
  1581.     }
  1582.     *s++ = '\n';
  1583.     if (flag) {            /* want to include UID? */
  1584.       char stack[64];
  1585.       char *p = stack;
  1586.                 /* push UID digits on the stack */
  1587.       unsigned long n = elt->private.uid;
  1588.       do *p++ = (char) (n % 10) + '0';
  1589.       while (n /= 10);
  1590.       *s++ = 'X'; *s++ = '-'; *s++ = 'U'; *s++ = 'I'; *s++ = 'D'; *s++ = ':';
  1591.       *s++ = ' ';
  1592.                 /* pop UID from stack */
  1593.       while (p > stack) *s++ = *--p;
  1594.       *s++ = '\n';
  1595.     }
  1596.   }
  1597.   *s++ = '\n'; *s = '\0';    /* end of extended message status */
  1598.   return s - status;        /* return size of resulting string */
  1599. }
  1600.  
  1601. /* Rewrite mailbox file
  1602.  * Accepts: MAIL stream, must be critical and locked
  1603.  *        return pointer to number of expunged messages if want expunge
  1604.  * Returns: T if success and mailbox unlocked, NIL if failure
  1605.  */
  1606.  
  1607. long unix_rewrite (MAILSTREAM *stream,unsigned long *nexp)
  1608. {
  1609.   unsigned long i,j;
  1610.   int e,retry;
  1611.   time_t tp[2];
  1612.   struct stat sbuf;
  1613.   FILE *f;
  1614.   MESSAGECACHE *elt;
  1615.   unsigned long recent = stream->recent;
  1616.   unsigned long size = 0;    /* initially nothing done */
  1617.   if (nexp) *nexp = 0;        /* initially nothing expunged */
  1618.                 /* open scratch file */
  1619.   if (!(f = tmpfile ())) return unix_punt_scratch (NIL);
  1620.   if (!(stream->uid_nosticky ||    /* write pseudo-header */
  1621.     unix_fwrite (f,LOCAL->buf,unix_pseudo (stream,LOCAL->buf),&size)))
  1622.     return unix_punt_scratch (f);
  1623.   if (nexp) {            /* expunging */
  1624.     for (i = 1; i <= stream->nmsgs; i++)
  1625.       if (!(elt = mail_elt (stream,i))->deleted &&
  1626.       !unix_write_message (f,stream,elt,&size))
  1627.     return unix_punt_scratch (f);
  1628.   }
  1629.   else for (i = 1; i <= stream->nmsgs; i++)
  1630.     if (!unix_write_message (f,stream,mail_elt (stream,i),&size))
  1631.       return unix_punt_scratch (f);
  1632.                 /* write remaining data */
  1633.   if (fflush (f) || fstat (fileno (f),&sbuf)) return unix_punt_scratch (f);
  1634.   if (size != sbuf.st_size) {    /* make damn sure stdio isn't lying */
  1635.     char tmp[MAILTMPLEN];
  1636.     sprintf (tmp,"Checkpoint file size mismatch (%lu != %lu)",size,
  1637.          sbuf.st_size);
  1638.     mm_log (tmp,ERROR);
  1639.     fclose (f);            /* flush the output file */
  1640.     return NIL;
  1641.   }
  1642.   if (size > LOCAL->filesize) {    /* does the mailbox need to grow? */
  1643.                 /* am I paranoid or what? */
  1644.     if ((i = size - LOCAL->filesize) > LOCAL->buflen) {
  1645.                 /* this user won the lottery all right */
  1646.       fs_give ((void **) &LOCAL->buf);
  1647.       LOCAL->buf = (char *) fs_get ((LOCAL->buflen = i) + 1);
  1648.     }
  1649.     memset (LOCAL->buf,'\0',i);    /* get a block of nulls */
  1650.     while (i) {            /* until write successful or punt */
  1651.       lseek (LOCAL->fd,LOCAL->filesize,L_SET);
  1652.       if (write (LOCAL->fd,LOCAL->buf,i) < 0) {
  1653.     j = errno;        /* note error before doing ftrunctate */
  1654.     ftruncate (LOCAL->fd,LOCAL->filesize);
  1655.     fsync (LOCAL->fd);
  1656.     if (mm_diskerror (stream,j,NIL)) {
  1657.       sprintf (LOCAL->buf,"Unable to extend mailbox: %s",strerror (j));
  1658.       mm_log (LOCAL->buf,ERROR);
  1659.       fclose (f);        /* flush the output file */
  1660.       return NIL;
  1661.     }
  1662.       }
  1663.       else i = 0;        /* write was successful */
  1664.     }
  1665.   }
  1666.  
  1667.                 /* update the cache */
  1668.   for (i = 1; i <= stream->nmsgs;) {
  1669.     elt = mail_elt (stream,i);    /* get cache */
  1670.     if (nexp && elt->deleted) {    /* expunge this message? */
  1671.       if (elt->recent) recent--;/* one less recent message */
  1672.       mail_expunged (stream,i);    /* notify upper levels */
  1673.       ++*nexp;            /* count up one more expunged message */
  1674.     }
  1675.     else {            /* update file pointers from kludgey places */
  1676.       elt->private.special.offset = elt->private.msg.full.offset;
  1677.       elt->private.msg.text.offset = elt->private.msg.full.text.size;
  1678.                 /* in case header grew */
  1679.       elt->private.msg.header.text.size = elt->private.msg.text.offset -
  1680.     elt->private.msg.header.offset;
  1681.                 /* stomp on these two kludges */
  1682.       elt->private.msg.full.offset = elt->private.msg.full.text.size = 0;
  1683.       i++;            /* preserved message */
  1684.     }
  1685.   }
  1686.   do {                /* restart point if failure */
  1687.     retry = NIL;        /* no need to retry yet */
  1688.     fseek (f,0,L_SET);        /* rewind files */
  1689.     lseek (LOCAL->fd,0,L_SET);
  1690.     for (i = size; i; i -= j)
  1691.       if (!((j = fread (LOCAL->buf,1,min ((long) CHUNK,i),f)) &&
  1692.         (write (LOCAL->fd,LOCAL->buf,j) >= 0))) {
  1693.     sprintf (LOCAL->buf,"Mailbox rewrite error: %s",strerror (e = errno));
  1694.     mm_notify (stream,LOCAL->buf,WARN);
  1695.     mm_diskerror (stream,e,T);
  1696.     retry = T;        /* must retry */
  1697.     break;
  1698.       }
  1699.   } while (retry);        /* in case need to retry */
  1700.   fclose (f);            /* finished with scratch file */
  1701.                 /* make sure tied off */
  1702.   ftruncate (LOCAL->fd,LOCAL->filesize = size);
  1703.   fsync (LOCAL->fd);        /* make sure the updates take */
  1704.   LOCAL->dirty = NIL;        /* no longer dirty */
  1705.                   /* notify upper level of new mailbox sizes */
  1706.   mail_exists (stream,stream->nmsgs);
  1707.   mail_recent (stream,recent);
  1708.                 /* set atime to now, mtime a second earlier */
  1709.   tp[1] = (tp[0] = time (0)) - 1;
  1710.                 /* set the times, note change */
  1711.   if (!utime (stream->mailbox,tp)) LOCAL->filetime = tp[1];
  1712.   close (LOCAL->fd);        /* close and reopen file */
  1713.   if ((LOCAL->fd = open (stream->mailbox,O_RDWR,NIL)) < 0) {
  1714.     sprintf (LOCAL->buf,"Mailbox open failed, aborted: %s",strerror (errno));
  1715.     mm_log (LOCAL->buf,ERROR);
  1716.     unix_abort (stream);
  1717.   }
  1718.   return T;            /* looks good */
  1719. }
  1720.  
  1721. /* Write message
  1722.  * Accepts: destination file
  1723.  *        MAIL stream
  1724.  *        message number
  1725.  *        pointer to current filesize tally
  1726.  * Returns: T if success, NIL if failure
  1727.  */
  1728.  
  1729. long unix_write_message (FILE *f,MAILSTREAM *stream,MESSAGECACHE *elt,
  1730.              unsigned long *size)
  1731. {
  1732.   char *s;
  1733.   unsigned long i;
  1734.                 /* (kludge alert) note new message offset */
  1735.   elt->private.msg.full.offset = *size;
  1736.                 /* internal header */
  1737.   lseek (LOCAL->fd,elt->private.special.offset,L_SET);
  1738.   read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
  1739.   if (unix_fwrite (f,LOCAL->buf,elt->private.special.text.size,size)) {
  1740.                 /* get header */
  1741.     s = unix_header (stream,elt->msgno,&i,FT_INTERNAL);
  1742.                 /* header size, sans trailing newline */
  1743.     if (i && (s[i - 2] == '\n')) i--;
  1744.                 /* write header */
  1745.     if (unix_fwrite (f,s,i,size) &&
  1746.     unix_fwrite (f,LOCAL->buf,unix_xstatus(stream,LOCAL->buf,elt,T),size)){
  1747.                 /* (kludge alert) note new text offset */
  1748.       elt->private.msg.full.text.size = *size - elt->private.msg.full.offset;
  1749.                 /* get text */
  1750.       s = unix_text_work (stream,elt,&i,FT_INTERNAL);
  1751.                 /* write text and trailing newline */
  1752.       if (unix_fwrite (f,s,i,size) && unix_fwrite (f,"\n",1,size)) return T;
  1753.     }
  1754.   }
  1755.   return NIL;            /* failed */
  1756. }
  1757.  
  1758. /* Safely write buffer
  1759.  * Accepts: destination file
  1760.  *        buffer pointer
  1761.  *        number of octets
  1762.  *        pointer to current filesize tally
  1763.  * Returns: T if successful, NIL if failure
  1764.  */
  1765.  
  1766. long unix_fwrite (FILE *f,char *s,unsigned long i,unsigned long *size)
  1767. {
  1768.   unsigned long j;
  1769.   while (i && ((j = fwrite (s,1,i,f)) || (errno == EINTR))) {
  1770.     *size += j;
  1771.     s += j;
  1772.     i -= j;
  1773.   }
  1774.   return i ? NIL : T;        /* success if wrote all requested data */
  1775. }
  1776.  
  1777.  
  1778. /* Punt scratch file
  1779.  * Accepts: file pointer
  1780.  * Returns: NIL, always
  1781.  */
  1782.  
  1783. long unix_punt_scratch (FILE *f)
  1784. {
  1785.   char tmp[MAILTMPLEN];
  1786.   sprintf (tmp,"Checkpoint file failure: %s",strerror (errno));
  1787.   mm_log (tmp,ERROR);
  1788.   if (f) fclose (f);        /* flush the output file */
  1789.   return NIL;
  1790. }
  1791.