ftp.nice.ch/pub/next/unix/mail/popper.s.tar.gz#/popper/qpopper2.1.1/pop_dropcopy.c

This is pop_dropcopy.c in view mode; [Download] [Up]

/*
 * Copyright (c) 1989 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#ifndef lint
static char copyright[] = "Copyright (c) 1990 Regents of the University of California.\nAll rights reserved.\n";
static char SccsId[] = "@(#)@(#)pop_dropcopy.c	2.6  2.6 4/3/91";
#endif

#if defined(SOLARIS2) || defined(AIX)
# define SYSV
#endif

#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#ifdef SYSV
# include	<string.h>
# include 	<unistd.h>	/* Maybe /sys/unistd.h on HPUX */
# include	"flock.h"
#else
# include <strings.h>
#endif
#include <sys/stat.h>
#include <sys/file.h>
#include <pwd.h>
#include "popper.h"

#if defined(SYSV) && !defined(L_XTND)
# define L_XTND SEEK_END
#endif

extern int      errno;
extern int      sys_nerr;
extern char    *sys_errlist[];

/*
 *  A more accurate From header checker extracted from /usr/bin/mail src.
 */

/*
 * Test to see if the passed string is a ctime(3) generated
 * date string as documented in the manual.  The template
 * below is used as the criterion of correctness.
 * Also, we check for a possible trailing time zone using
 * the tmztype template.
 */

/*
 * 'A'	An upper case char
 * 'a'	A lower case char
 * ' '	A space
 * '0'	A digit
 * 'O'	An optional digit or space
 * ':'	A colon
 * 'N'	A new line
 */
char ctype[] = "Aaa Aaa O0 00:00:00 0000";
char tmztype[] = "Aaa Aaa O0 00:00:00 AAA 0000";
char ctype2[] = "Aaa Aaa O0 00:00 0000";
char tmztype2[] = "Aaa Aaa O0 00:00 AAA 0000";

isdate(date)
	char date[];
{

	return cmatch(date, ctype) || cmatch(date, tmztype) ||
		cmatch(date, ctype2) || cmatch(date, tmztype2) ;
}

/*
 * Match the given string (cp) against the given template (tp).
 * Return 1 if they match, 0 if they don't
 */
cmatch(cp, tp)
	register char *cp, *tp;
{

	while (*cp && *tp)
		switch (*tp++) {
		case 'a':
			if (!islower(*cp++))
				return 0;
			break;
		case 'A':
			if (!isupper(*cp++))
				return 0;
			break;
		case ' ':
			if (*cp++ != ' ')
				return 0;
			break;
		case '0':
			if (!isdigit(*cp++))
				return 0;
			break;
		case 'O':
			if (*cp != ' ' && !isdigit(*cp))
				return 0;
			cp++;
			break;
		case ':':
			if (*cp++ != ':')
				return 0;
			break;
		case 'N':
			if (*cp++ != '\n')
				return 0;
			break;
		}
	if ((*cp != '\r' && *cp != '\n' && *cp) || *tp)
		return 0;
	return (1);
}


/*
 *  0 for not a from line
 *  1 for is a from line
 */

int
isfromline(cp)
char	*cp;
{
    if (!cp || (strlen(cp) < 5) || *cp++ != 'F' || *cp++ != 'r' || 
	*cp++ != 'o' || *cp++ != 'm' || *cp != ' ')
	return(0);

    /* Skip over spaces and tabs */
    while (*cp && (*cp == ' ' || *cp == '\t'))
	cp++;

    /* Skip over the from address */
    while (*cp && *cp != ' ' && *cp != '\t')
	cp++;

    /* Skip over more spaces and tabs */
    while (*cp && (*cp == ' ' || *cp == '\t'))
	cp++;

    /* We should now be at either ttysomething or the date */
    if (cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
	/* skip over the tty name to the next word */
	while (*cp && (*cp == ' ' || *cp == '\t'))
	    cp++;
	while (*cp && *cp != ' ' && *cp != '\t')
	    cp++;
    }

    return (isdate(cp));
}


/* 
 *  use to be dropinfo:   Extract information about the POP maildrop and store 
 *  it for use by the other POP routines.
 *
 *  Now, the copy and info collection are done at the same time.
 */

do_drop_copy(p, mfd, dfd)
int	mfd, dfd;
POP	*p;
{
    char                    buffer[BUFSIZ];         /*  Read buffer */
    MsgInfoList         *   mp;                     /*  Pointer to message 
                                                        info list */
    register int            msg_num;                /*  Current message 
                                                        counter */
    int                     nchar;                  /*  Bytes written/read */
    int			    inheader;		    /*  Header section flag */
    int			    uidlfound;		    /*  UIDL header flag */
    int			    uidl_seq;		    /*  session sequence */
    time_t		    session_time;	    /*  POP time value */

    FILE		    *mail_drop, *pop_drop;  /*  Streams for fids */

    /*  Initialize maildrop status variables in the POP parameter block */
    p->msg_count = 0;
    p->msgs_deleted = 0;
    p->last_msg = 0;
    p->bytes_deleted = 0;
    p->drop_size = 0;

    /*  Allocate memory for message information structures */
    p->msg_count = ALLOC_MSGS;
    p->mlp = (MsgInfoList *)calloc((unsigned)p->msg_count,sizeof(MsgInfoList));
    if (p->mlp == NULL){
        (void)close (mfd);
        (void)close (dfd);
        p->msg_count = 0;
        return pop_msg (p,POP_FAILURE,
            "Can't build message list for '%s': Out of memory", p->user);
    }

    /*  Acquire a stream pointer for the temporary maildrop */
    if ( (pop_drop = fdopen(dfd,"a+")) == NULL ) {
        (void)close(dfd) ;
        return pop_msg(p,POP_FAILURE,"Cannot assign stream for %s",
            p->temp_drop);
    }

    /*  Acquire a stream pointer for the maildrop */
    if ( (mail_drop = fdopen(mfd,"r")) == NULL ) {
        (void)close(mfd) ;
        return pop_msg(p,POP_FAILURE,"Cannot assign stream for %s",
            p->drop_name);
    }

    rewind (mail_drop);

    /* Is the first line a recognizable From line? */
    fgets(buffer,MAXMSGLINELEN,mail_drop);
    if (! isfromline(buffer))
      simple_from = TRUE;
    else
      simple_from = FALSE;

    rewind (mail_drop);

    /*  Scan the file, loading the message information list with 
        information about each message */

    inheader = 0;
    uidl_seq = 0;
    session_time = time((time_t *)0);

    for (msg_num = p->drop_size = 0, mp = p->mlp - 1;
				      fgets(buffer,MAXMSGLINELEN,mail_drop);) {
	nchar = strlen(buffer);

	if (inheader && *buffer == '\n') {
	    inheader = 0;

	    if (!uidlfound) {
	        sprintf(buffer,"%s %ld.%03d\n",MSGUIDL,session_time,uidl_seq++);
		fputs(buffer, pop_drop);

	        mp->length += strlen(buffer);
	        p->drop_size += strlen(buffer);
	        mp->lines++;

	        strcpy(buffer, "\n");
	        nchar = 1;
	    }
	}

	fputs(buffer, pop_drop);

	if (simple_from ? (strncmp(buffer,"From ",5) == 0) : isfromline(buffer))
	{
            if (++msg_num > p->msg_count) {
                p->mlp=(MsgInfoList *) realloc(p->mlp,
                    (p->msg_count+=ALLOC_MSGS)*sizeof(MsgInfoList));
                if (p->mlp == NULL){
                    (void)close (mfd);
                    (void)close (dfd);
                    p->msg_count = 0;
                    return pop_msg (p,POP_FAILURE,
                        "Can't build message list for '%s': Out of memory",
                            p->user);
                }
                mp = p->mlp + msg_num - 2;
            }
#ifdef DEBUG
            if(p->debug && msg_num != 1)
                pop_log(p,POP_DEBUG,
                    "Msg %d at offset %d is %d octets long and has %u lines.",
                        mp->number,mp->offset,mp->length,mp->lines);
#endif
            ++mp;
            mp->number = msg_num;
            mp->length = 0;
            mp->lines = 0;
            mp->offset = ftell(pop_drop) - nchar;
            mp->del_flag = FALSE;
            mp->retr_flag = FALSE;
#ifdef DEBUG
            if(p->debug)
                pop_log(p,POP_DEBUG, "Msg %d being added to list", mp->number);
#endif
	    inheader = 1;
	    uidlfound = 0;
        } else if (inheader && !strncasecmp(buffer, MSGUIDL, strlen(MSGUIDL))) {
	    uidlfound = 1;
	}

        mp->length += nchar;
        p->drop_size += nchar;
        mp->lines++;
    }

    p->msg_count = msg_num;

#ifdef DEBUG
    if(p->debug && msg_num > 0) {
        register    i;
        for (i = 0, mp = p->mlp; i < p->msg_count; i++, mp++)
            pop_log(p,POP_DEBUG,
                "Msg %d at offset %d is %d octets long and has %u lines.",
                    mp->number,mp->offset,mp->length,mp->lines);
    }
#endif

    fflush(pop_drop);

    return(POP_SUCCESS);
}

/* 
 *  dropcopy:   Make a temporary copy of the user's mail drop and 
 *  save a stream pointer for it.
 */

pop_dropcopy(p,pwp)
POP     *   p;
struct passwd	*	pwp;
{
    int                     mfd;                    /*  File descriptor for 
                                                        the user's maildrop */
    int                     dfd;                    /*  File descriptor for 
                                                        the SERVER maildrop */
    FILE		    *tf;		    /*  The temp file */
    char		    template[POP_TMPSIZE];  /*  Temp name holder */
    char                    buffer[BUFSIZ];         /*  Read buffer */
    long                    offset;                 /*  Old/New boundary */
    int                     nchar;                  /*  Bytes written/read */
    struct stat             mybuf;                  /*  For lstat() */

    /*  Create a temporary maildrop into which to copy the updated maildrop */
    (void)sprintf(p->temp_drop,POP_DROP,p->user);

#ifdef DEBUG
    if(p->debug)
        pop_log(p,POP_DEBUG,"Creating temporary maildrop '%s'",
            p->temp_drop);
#endif

    /* Here we work to make sure the user doesn't cause us to remove or
     * write over existing files by limiting how much work we do while
     * running as root.
     */

    /* First create a unique file.  Would prefer mkstemp, but Ultrix...*/
    strcpy(template,POP_TMPDROP);
    (void) mktemp(template);
    if ( (tf=fopen(template,"w+")) == NULL ) {	/* failure, bail out	*/
        pop_log(p,POP_PRIORITY,
            "Unable to create temporary temporary maildrop '%s': %s",template,
                (errno < sys_nerr) ? sys_errlist[errno] : "") ;
        return pop_msg(p,POP_FAILURE,
		"System error, can't create temporary file.");
    }

    /* Now give this file to the user	*/
#ifdef HPUX
    (void) fchown(template,pwp->pw_uid, pwp->pw_gid);
#else
    (void) chown(template,pwp->pw_uid, pwp->pw_gid);
#endif
    (void) chmod(template,0600);

    /* Now link this file to the temporary maildrop.  If this fails it
     * is probably because the temporary maildrop already exists.  If so,
     * this is ok.  We can just go on our way, because by the time we try
     * to write into the file we will be running as the user.
     */
    (void) link(template,p->temp_drop);
    (void) fclose(tf);
    (void) unlink(template);

    /* Now we run as the user. */
    (void) setuid(pwp->pw_uid);
    (void) setgid(pwp->pw_gid);

#ifdef DEBUG
    if(p->debug)pop_log(p,POP_DEBUG,"uid = %d, gid = %d",getuid(),getgid());
#endif

    /* Open for append,  this solves the crash recovery problem */
    if ((dfd = open(p->temp_drop,O_RDWR|O_APPEND|O_CREAT,0600)) == -1){
        pop_log(p,POP_PRIORITY,
            "Unable to open temporary maildrop '%s': %s",p->temp_drop,
                (errno < sys_nerr) ? sys_errlist[errno] : "") ;
        return pop_msg(p,POP_FAILURE,
		"System error, can't open temporary file, do you own it?");
    }

    /*  Lock the temporary maildrop */
    if ( flock (dfd, LOCK_EX|LOCK_NB) == -1 )
    {
    switch(errno) {
        case EWOULDBLOCK:
            return pop_msg(p,POP_FAILURE,
                 "%s lock busy!  Is another session active?",p->temp_drop);
            /* NOTREACHED */
        default:
            return pop_msg(p,POP_FAILURE,"flock: '%s': %s", p->temp_drop,
                (errno < sys_nerr) ? sys_errlist[errno] : "");
            /* NOTREACHED */
        }
    
     }
    /* check for race conditions involving unlink.  See pop_updt.c */
    /* s-dorner@uiuc.edu, 12/91 */
    {
      struct stat byName, byFd;
      if (stat(p->temp_drop,&byName) || fstat(dfd,&byFd) ||
	  byName.st_ino!=byFd.st_ino)
      {
        return pop_msg(p,POP_FAILURE,
		"Maildrop being unlocked; try again.");
      }
    }
    
    /* May have grown or shrunk between open and lock! */
    offset = lseek(dfd,0,L_XTND);

    /*  Open the user's maildrop, If this fails,  no harm in assuming empty */
    if ((mfd = open(p->drop_name,O_RDWR)) > 0) {

        /*  Lock the maildrop */
        if (flock (mfd,LOCK_EX) == -1)
	{
            (void)close(mfd) ;
            return pop_msg(p,POP_FAILURE, "flock: '%s': %s", p->temp_drop,
                (errno < sys_nerr) ? sys_errlist[errno] : "");
        }

	/* New routine to copy and get dropbox info all at the same time */
	nchar = do_drop_copy(p, mfd, dfd);

/* Now copy and pop_info occur at the same time because of uidl
        while ( (nchar=read(mfd,buffer,BUFSIZ)) > 0 )
            if ( nchar != write(dfd,buffer,nchar) ) {
                nchar = -1 ;
                break ;
            }
*/

        if ( nchar != POP_SUCCESS ) {
            /* Error adding new mail.  Truncate to original size,
               and leave the maildrop as is.  The user will not 
               see the new mail until the error goes away.
               Should let them process the current backlog,  in case
               the error is a quota problem requiring deletions! */
            (void)ftruncate(dfd,(int)offset) ;
        } else {
            /* Mail transferred!  Zero the mail drop NOW,  that we
               do not have to do gymnastics to figure out what's new
               and what is old later */
            (void)ftruncate(mfd,0) ;
        }

        /*  Close the actual mail drop */
        (void)close (mfd);
    }

    /*  Acquire a stream pointer for the temporary maildrop */
    if ( (p->drop = fdopen(dfd,"a+")) == NULL ) {
        (void)close(dfd) ;
        return pop_msg(p,POP_FAILURE,"Cannot assign stream for %s",
            p->temp_drop);
    }

    /*  If enabled, copy any new bulletins to the temporary mail drop */
    if (p->bulldir != NULL) pop_bull(p, pwp);

    rewind (p->drop);

    return(POP_SUCCESS);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.