/* * pop3d - IP/TCP/POP3 server for UNIX 4.3BSD * Post Office Protocol - Version 3 (RFC1225) * * (C) Copyright 1991 Regents of the University of California * * Permission to use, copy, modify, and distribute this program * for any purpose and without fee is hereby granted, provided * that this copyright and permission notice appear on all copies * and supporting documentation, the name of University of California * not be used in advertising or publicity pertaining to distribution * of the program without specific prior permission, and notice be * given in supporting documentation that copying and distribution is * by permission of the University of California. * The University of California makes no representations about * the suitability of this software for any purpose. It is provided * "as is" without express or implied warranty. * * Katie Stevens * dkstevens@ucdavis.edu * Information Technology -- Campus Access Point * University of California, Davis * ************************************** * * folder.c * * REVISIONS: * 02-27-90 [ks] original implementation * 1.000 03-04-90 [ks] * 1.001 06-24-90 [ks] allow TRANS state if 0 msgs in folder * implement optional TOP command * 1.002 07-22-91 [ks] reset index counter after folder rewind * in fld_release (Thanks to John Briggs, * vaxs09@vitro.com, Vitro Corporation, * Silver Spring, MD for finding this bug!) * 1.004 11-13-91 [ks] leave original mailbox intact during POP * session (Thanks to Dave Cooley, * dwcooley@colby.edu for suggesting this) */ #include #include #include #include #include #include #include #include #include #include #include "pop3.h" #ifdef LINUX # include #endif /* In main.c */ extern char *svr_hostname; extern char svr_buf[]; extern char cli_user[]; /* In util.c */ extern char flash_buf[]; #ifdef DEBUG extern FILE *logfp; #endif /* Constants used when closing a folder after POP session */ #define REMOVE_MBOX 1 #define SAVE_NEW 2 #define SAVE_ALL 3 static char *svr_nomsg = "-ERR no messages in mailbox\r\n"; static char *cli_mbox = NULL; /* Filename of mailbox folder */ static char fld_fname[32]; /* Filename for working folder */ static FILE *fld_fp = NULL; /* Stream pointer for mailbox */ static struct fld_item *fld_msg; /* Struct for mailbox stats */ static int fld_max = -1; /* Actual # msgs in mailbox */ static int fld_highest = -1; /* Max msg accessed by client */ static int fld_hostmbox = 0; /* 0=FromSP mailbox; 1=BSMTP */ static int fld_write_ok = -1; /* -1=client doesnt have write privs */ static long fld_orig_size; /* [1.004] size of mbox when loaded */ static time_t fld_orig_mod_time; /* [1.004] timestamp on mbox file */ #define isfromsp_start(buf) (strncmp(buf,"From ",5) == 0) #define isbsmtp_helo(buf) (strncmp(buf,"helo",4) == 0) #define isbsmtp_end(buf) ((buf[0] == DOT_CHAR)&&(strlen(buf) == 2)) static char path_lnk[PATH_MAX]; static void del_pop_session_lock () { unlink (path_lnk); } static int put_pop_session_lock (const char *mbox) { int ret = -1; int i; int pid = getpid(); char pidstr[10]; sprintf (pidstr,"%d",pid); sprintf (path_lnk,"%s.lock-vpop3d",mbox); for (i=0; i<5; i++){ if (symlink (pidstr,path_lnk)==-1){ char tmp[PATH_MAX]; if (readlink(path_lnk,tmp,sizeof(tmp)-1)!=-1){ int other_pid = atoi(tmp); if (kill(other_pid,0)==-1){ const char *user = strrchr (mbox,'/'); if (user != NULL) user++; syslog (LOG_ERR,"Removing stale vpop3d lock for user %s",user); unlink (path_lnk); }else{ sleep(1); /* Try again later */ } } }else{ atexit(del_pop_session_lock); ret = 0; break; } } return ret; } /* Load messages from a mailbox delimited by FromSPACE */ static int msg_fromsp(FILE *infp, FILE *outfp) { bool inheader = false; int i = 0; register struct fld_item *mp; /* Get an array for storing info about messages in folder */ get_e_array(fld_msg, FLD_ENTRY_BLOCK); if (fld_msg == NULL) fail(FAIL_OUT_OF_MEMORY); mp = &fld_msg[0]; /* Load messages from mailbox folder to temporary folder */ while (fgetl(svr_buf,SVR_BUFSIZ,infp) != NULL) { fputs(svr_buf, outfp); if (ferror(outfp)) return -1; if (isfromsp_start(svr_buf)) { /* Make sure there is room in array for this entry */ chk_e_size(fld_msg, FLD_ENTRY_BLOCK, i); if (fld_msg == NULL) fail(FAIL_OUT_OF_MEMORY); /* Reset stats for this message */ mp = &fld_msg[i]; mp->fmsg_entry = ftell(outfp); mp->status = 0; mp->uid = NULL; sprintf(flash_buf,"%s %s@%s\r\n",POP3_RCPT_HDR, cli_user,svr_hostname); mp->pop_hdr = (char*)malloc(strlen(flash_buf)+1); if (mp->pop_hdr == NULL) fail(FAIL_OUT_OF_MEMORY); strcpy(mp->pop_hdr,flash_buf); mp->bcount = strlen(mp->pop_hdr); inheader = true; ++i; }else{ mp->bcount += ((long)strlen(svr_buf) + 1L); if (inheader && strncasecmp(svr_buf,"Message-Id:",11)==0) { // Keep the message id, but strip spaces at the end char *pt = svr_buf+11; char *end = pt + strlen(pt)-1; while (*pt == ' ') pt++; while (end > pt && end[-1] <= ' ') end--; *end = '\0'; mp->uid = strdup (pt); }else if (svr_buf[0] == '\n' || svr_buf[0] == '\r'){ inheader = false; } } } if (ferror(infp)) return -1; if (i == 0) free((char *)fld_msg); return(i); } /************************************************/ /* Isolate a recipient address in a BSMTP RCPT TO: command */ static char *bsmtp_rcpt(char *inbuf) { char *cp; cp = strchr(inbuf,RANKLE_CHAR); if (cp == NULL) return("postmaster"); *cp = NULL_CHAR; cp = strchr(inbuf,LANKLE_CHAR); if (cp == NULL) return("postmaster"); return(cp+1); } /**************************************************************************/ /* Load messages from a mailbox wrapped in BSMTP format */ /* Assume BSMTP mailbox starts with a HELO command. */ /* Assume BSMTP mailbox doesnt end with a QUIT command. */ static int msg_bsmtp(FILE *infp, FILE *outfp) { register struct fld_item *mp; int i = 0; int mbox_state = BSMTP_HELO_STATE; /* Get an array for storing info about messages in folder */ get_e_array(fld_msg, FLD_ENTRY_BLOCK); if (fld_msg == NULL) fail(FAIL_OUT_OF_MEMORY); mp = &fld_msg[0]; mp->pop_hdr = NULL; /* Load messages from mailbox folder to temporary folder */ while (fgetl(svr_buf,SVR_BUFSIZ,infp) != NULL) { fputs(svr_buf, outfp); if (ferror(outfp)) return -1; switch(mbox_state) { case BSMTP_HELO_STATE: /* Look for HELO command */ cmd_prepare(svr_buf); if (strncmp(svr_buf,"helo",4) == 0) mbox_state = BSMTP_MAIL_STATE; break; case BSMTP_MAIL_STATE: /* Process MAIL command */ cmd_prepare(svr_buf); if (strncmp(svr_buf,"mail",4) == 0) mbox_state = BSMTP_RCPT_STATE; break; case BSMTP_RCPT_STATE: /* Process RCPT command(s) */ cmd_prepare(svr_buf); if (strncmp(svr_buf,"rcpt",4) == 0) { /* Save recipient for POP3 client */ sprintf(flash_buf,"%s %s\r\n", POP3_RCPT_HDR,bsmtp_rcpt(svr_buf)); if (mp->pop_hdr == NULL) { mp->bcount = 0L; mp->status = 0; mp->pop_hdr = (char*)malloc(strlen(flash_buf)+1); if (mp->pop_hdr == NULL) fail(FAIL_OUT_OF_MEMORY); strcpy(mp->pop_hdr,flash_buf); } else { mp->pop_hdr = (char*) realloc(mp->pop_hdr, (strlen(mp->pop_hdr)+strlen(flash_buf)+1)); if (mp->pop_hdr == NULL) fail(FAIL_OUT_OF_MEMORY); strcat(mp->pop_hdr,flash_buf); } mp->bcount += (long)strlen(flash_buf); } else if (strncmp(svr_buf,"data",4) == 0) { mbox_state = BSMTP_DATA_STATE; /* Save entry point of message text */ mp->fmsg_entry = ftell(outfp); } break; case BSMTP_DATA_STATE: /* Reading mail message */ if (isbsmtp_end(svr_buf)) { /* Prepare for next message in mailbox */ mbox_state = BSMTP_MAIL_STATE; ++i; /* Resize message array, if needed */ chk_e_size(fld_msg, FLD_ENTRY_BLOCK, i); if (fld_msg == NULL) fail(FAIL_OUT_OF_MEMORY); mp = &fld_msg[i]; mp->pop_hdr = NULL; } else { mp->bcount += ((long)strlen(svr_buf) + 1L); if (svr_buf[0] == DOT_CHAR) --mp->bcount; } break; default: /* Shouldnt happen */ fail(FAIL_CONFUSION); break; } } if (ferror(infp)) return -1; if (i == 0) free((char *)fld_msg); return(i); } /**************************************************************************/ /* Attempt to load a mailbox folder */ static int fld_select(char *mbox, int host_fld) { struct stat stat_buf; FILE *mboxfp; int lock; int fd; /* Reset folder variables */ fld_hostmbox = host_fld; fld_fp = NULL; fld_write_ok = -1; fld_highest = -1; /* Make sure mailbox is present and non-zero size */ if (stat(mbox,&stat_buf) == -1) return 0; if (stat_buf.st_size == 0L) return 0; if (put_pop_session_lock(mbox) == -1) fail (FAIL_ACCESS); /* Save current mailbox size and last-modified-timestamp */ fld_orig_size = stat_buf.st_size; /* [1.004] */ fld_orig_mod_time = stat_buf.st_mtime; /* [1.004] */ /* Create/save mailbox names */ strcpy(fld_fname, POP3_TMPFILE); if ((fd = mkstemp(fld_fname)) == -1) return -1; /* Secure mailbox for POP3 session; copy to temporary file */ if ((mboxfp = fopen(mbox, "r")) == NULL) return -1; if ((lock = dup(fileno(mboxfp))) == -1) { fclose(mboxfp); return -1; } if (flock(lock, LOCK_EX) == -1) { fclose(mboxfp); close(lock); return -1; } if ((fld_fp = fdopen(fd,"w")) == NULL) { fclose(mboxfp); flock(lock, LOCK_UN); close(lock); return -1; } /* Load messages from folder */ if (fld_hostmbox == 0) fld_max = msg_fromsp(mboxfp, fld_fp); else fld_max = msg_bsmtp(mboxfp, fld_fp); if (fclose(mboxfp) == EOF) fld_max = -1; if (fclose(fld_fp) == EOF) fld_max = -1; #ifdef notdef /* [1.004] */ /* Zero out the mailbox so other processes can use it */ /* while we are using the temporary copy just made. */ fld_write_ok = access(mbox,W_OK); if ((fld_max > 0) && (fld_write_ok != -1)) { if ((mboxfp = fopen(mbox,"w")) == NULL) { fld_max = -1; } else if (fclose(mboxfp) == EOF) { fld_max = -1; } } #endif /* [1.004] */ /* Unlock mailbox */ flock(lock, LOCK_UN); close(lock); /* Prepare to use temporary file for POP3 session */ if (fld_max > 0) { if ((fld_fp = fopen(fld_fname,"r")) == NULL) { unlink(fld_fname); fld_max = -1; } } else { /* Either zero messages or error */ unlink(fld_fname); fld_fp = NULL; } return(fld_max); } /**************************************************************************/ /* Open a BSMTP wrapped mailbox */ int fld_bsmtp(char *hostname) { int cnt; /* Release previously opened mailbox */ if (fld_fp != NULL) fld_release(); /* Construct filename for new mailbox */ cli_mbox = (char*)malloc(strlen(hostname)+strlen(DEF_POP3_DIR)+1); if (cli_mbox == NULL) fail(FAIL_OUT_OF_MEMORY); strcpy(cli_mbox,DEF_POP3_DIR); strcat(cli_mbox,hostname); /* Open mailbox */ if ((cnt = fld_select(cli_mbox,1)) == -1) { sprintf(svr_buf,"-ERR cannot open mailbox %s\r\n", cli_mbox); free(cli_mbox); cli_mbox = NULL; return(SVR_FOLD_STATE); } else { sprintf(svr_buf,"+OK %d messages ready for %s in %s\r\n", cnt,cli_user,cli_mbox); return(SVR_TRANS_STATE); } } /* Open a FromSpace delimited mailbox */ int fld_fromsp(char *fname) { int cnt; /* Release previously opened mailbox */ if (fld_fp != NULL) fld_release(); /* Construct filename for new mailbox */ cli_mbox = (char*)malloc(strlen(fname)+1); if (cli_mbox == NULL) fail(FAIL_OUT_OF_MEMORY); strcpy(cli_mbox,fname); /* Open mailbox */ if ((cnt = fld_select(cli_mbox,0)) == -1) { sprintf(svr_buf,"-ERR cannot open mailbox %s\r\n", cli_mbox); free(cli_mbox); cli_mbox = NULL; return(SVR_FOLD_STATE); } else { sprintf(svr_buf,"+OK %d messages ready for %s in %s\r\n", cnt,cli_user,cli_mbox); return(SVR_TRANS_STATE); } } /**************************************************************************/ /* Mark a message for deletion */ void fld_delete(int msgnum) { if (fld_fp == NULL) { strcpy(svr_buf, svr_nomsg); return; } if ((msgnum < 1)||(msgnum > fld_max)) { sprintf(svr_buf,"-ERR invalid message; number out of range\r\n"); } else { fld_msg[msgnum-1].status |= MSG_DELETED; sprintf(svr_buf,"+OK message %d marked for deletion\r\n", msgnum); if ((msgnum-1) > fld_highest) fld_highest =(msgnum-1); } } /* Report the highest access number for this mailbox */ void fld_last() { sprintf(svr_buf,"+OK %d\r\n",(fld_highest+1)); } /* Give information about messages in mailbox folder */ void fld_list(int msgnum) { if (msgnum == -1) { if (fld_fp == NULL) { sprintf(svr_buf,"+OK 0 messages; msg# and size (in octets) for undeleted messages:\r\n"); svr_data_out(svr_buf); }else{ sprintf(svr_buf,"+OK %d messages; msg# and size (in octets) for undeleted messages:\r\n",fld_max); svr_data_out(svr_buf); for (int i=0; i fld_max)) sprintf(svr_buf,"-ERR invalid message; number out of range\r\n"); else if (fld_msg[msgnum-1].status & MSG_DELETED) sprintf(svr_buf,"-ERR message %d has been marked for deletion\r\n", msgnum); else sprintf(svr_buf,"+OK %d %ld\r\n", msgnum,fld_msg[msgnum-1].bcount); } } static const char *fld_getuidl (int msgnum) { const char *ret = fld_msg[msgnum-1].uid; if (ret == NULL) ret = "none"; return ret; } /* Give unique identifier about messages in mailbox folder */ void fld_uidl(int msgnum) { int i; if (fld_fp == NULL) { strcpy(svr_buf, svr_nomsg); return; } if (msgnum == -1) { sprintf(svr_buf,"+OK Unique-ID listing follows\r\n"); svr_data_out(svr_buf); for (i=0; i fld_max)) sprintf(svr_buf,"-ERR invalid message; number out of range\r\n"); else if (fld_msg[msgnum-1].status & MSG_DELETED) sprintf(svr_buf,"-ERR message %d has been marked for deletion\r\n", msgnum); else sprintf(svr_buf,"+OK %d %s\r\n", msgnum,fld_getuidl(msgnum)); } } /* Reset deleted messages and highest access number */ void fld_reset() { int i; if (fld_fp == NULL) { strcpy(svr_buf, svr_nomsg); return; } /* Reset messages marked for deletion */ for (i=0; i fld_max)) { sprintf(svr_buf,"-ERR invalid message; number out of range\r\n"); } else if (fld_msg[msgnum-1].status & MSG_DELETED) { sprintf(svr_buf,"-ERR message %d has been marked for deletion\r\n", msgnum); } else { sprintf(svr_buf,"+OK message %d (%ld octets):\r\n", msgnum,fld_msg[msgnum-1].bcount); svr_data_out(svr_buf); if (fld_hostmbox == 0) retr_fromsp(--msgnum,linecnt); else retr_bsmtp(--msgnum,linecnt); sprintf(svr_buf,".\r\n"); if ((linecnt != -1)&&(msgnum > fld_highest)) fld_highest = msgnum; } } /* Give message count and total size (in octets) of a mailbox folder */ void fld_stat() { int i; long total_cnt = 0L; if (fld_fp == NULL) { strcpy(svr_buf, "+OK 0 0\r\n"); return; } for (i=0; i fld_orig_size) { /* More messages were added to end of mbox */ zap_orig_mbox = SAVE_NEW; /* If no messages were deleted, just remove */ /* the working mailbox file. */ for (i=0; i