?? vqmaillocal.c
字號:
/* * $Id: vqmaillocal.c,v 1.3 2003/12/17 03:39:50 tomcollins Exp $ * Copyright (C) 2002 Inter7 Internet Technologies, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA *//* include files */#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/wait.h>#include <fcntl.h>#include <dirent.h>#include <errno.h>#include <time.h>#include <signal.h>#include "config.h"#include "vpopmail.h"#include "vauth.h"/* Globals */#define AUTH_SIZE 300char TheUser[AUTH_SIZE];char TheHomeDir[AUTH_SIZE];char TheLocal[AUTH_SIZE];char TheDash[AUTH_SIZE];char TheExt[AUTH_SIZE];char TheSender[AUTH_SIZE];char TheDefaultDelivery[AUTH_SIZE];char TheUserFull[AUTH_SIZE];char TheDomain[AUTH_SIZE];char TheDir[AUTH_SIZE];char CurrentDir[AUTH_SIZE];char DeliveredTo[AUTH_SIZE];struct vqpasswd *vpw;off_t message_size = 0;char bounce[AUTH_SIZE];int CurrentQuotaSizeFd;#ifdef QMAIL_EXTchar TheUserExt[AUTH_SIZE]; /* the User with '-' and following chars out if any */#endif#define FILE_SIZE 156char hostname[FILE_SIZE];char loop_buf[FILE_SIZE];#define MSG_BUF_SIZE 5000char msgbuf[MSG_BUF_SIZE];#define BUFF_SIZE 300int fdm;static char *binqqargs[4];/* Forward declarations */int process_valias(void);int is_delete(char *deliverto);int is_bounce(char *deliverto);void get_arguments(int argc, char **argv);off_t get_message_size();int deliver_mail(char *address, char *quota);int user_over_quota(char *address, char *quota);int check_forward_deliver(char *dir);off_t count_dir(char *dir_name);int is_looping( char *address );void run_command(char *prog);void checkuser(void);void usernotfound(void);static char local_file[156];static char local_file_new[156];/* * The email message comes in on file descriptor 0 - stanard in * The user to deliver the email to is in the EXT environment variable * The domain to deliver the email to is in the HOST environment variable */int main(int argc, char **argv){ /* get the arguments to the program and setup things */ get_arguments(argc, argv);#ifdef VALIAS /* process valiases if configured */ if ( process_valias() == 1 ) { printf("vdelivermail: valiases processed\n"); vexit(0); }#endif /* get the user from vpopmail database */ if ((vpw=vauth_getpw(TheUser, TheDomain)) != NULL ) { checkuser(); } #ifdef QMAIL_EXT /* try and find user that matches the QmailEXT address if: no user found, */ /* and the QmailEXT address is different, meaning there was an extension */ else if ( strncmp(TheUser, TheUserExt, AUTH_SIZE) != 0 ) { /* get the user from vpopmail database */ if ((vpw=vauth_getpw(TheUserExt, TheDomain)) != NULL ) { checkuser(); } else { usernotfound(); } }#endif else { if ( verrori != 0 ) { vexit(111); } usernotfound(); } /* exit successfully and have qmail delete the email */ return(vexit(0)); }/* * Get the command line arguments and the environment variables. * Force addresses to be lower case and set the default domain */void get_arguments(int argc, char **argv){#ifdef QMAIL_EXT int i;#endif if (argc != 10) { printf("vqmaillocal: wrong number of parameters\n"); vexit(0); } strncpy(TheHomeDir, argv[3], sizeof(TheHomeDir)); strncpy(TheUser, argv[6], sizeof(TheHomeDir)); strncpy(TheDomain, argv[7], sizeof(TheHomeDir)); printf("%s,%s,%s\n", TheHomeDir, TheUser, TheDomain); chdir(TheHomeDir); lowerit(TheUser); lowerit(TheDomain); strncpy(TheUserFull, TheUser, AUTH_SIZE);#ifdef QMAIL_EXT /* delete the '-' and following chars if any and store in TheUserExt */ for(i = 0; TheUser[i] != 0; i++) { if (TheUser[i] == '-' ) { break; } TheUserExt[i] = TheUser[i]; } TheUserExt[i] = 0;#endif}#ifdef VALIAS/* * Process any valiases for this user@domain * * This will look up any valiases in vpopmail and * deliver the email to the entries * * Return 1 if aliases found * Return 0 if no aliases found */int process_valias(void){ int found = 0; char *tmpstr; /* Get the first alias for this user@domain */ tmpstr = valias_select( TheUser, TheDomain ); /* tmpstr will be NULL if there are no more aliases */ while (tmpstr != NULL ) { /* We found one */ found = 1; /* deliver the mail */ deliver_mail(tmpstr, "NOQUOTA"); /* Get the next alias for this user@domain */ tmpstr = valias_select_next(); }#ifdef QMAIL_EXT /* try and find alias that matches the QmailEXT address * if: no alias found, * and the QmailEXT address is different, meaning there was an extension */ if ( (!found) && ( strncmp(TheUser, TheUserExt, AUTH_SIZE) != 0 ) ) { /* Get the first alias for this user@domain */ tmpstr = valias_select( TheUserExt, TheDomain ); /* tmpstr will be NULL if there are no more aliases */ while (tmpstr != NULL ) { /* We found one */ found = 1; /* deliver the mail */ deliver_mail(tmpstr, "NOQUOTA"); /* Get the next alias for this user@domain */ tmpstr = valias_select_next(); } } #endif /* Return whether we found an alias or not */ return(found);}#endif/* If the .qmail-default file has bounce all in it * Then return 1 * otherwise return 0 */int is_bounce(char *deliverto){ if ( strcmp( deliverto, BOUNCE_ALL ) == 0 ) return(1); return(0);}/* If the .qmail-default file has delete all in it * Then return 1 * otherwise return 0 */int is_delete(char *deliverto){ if ( strcmp( deliverto, DELETE_ALL ) == 0 ) return(1); return(0);}/* * Assumes the current working directory is user/Maildir * * We go off to look at cur and tmp dirs * * return size of files * */ssize_t check_quota(char *maildir){ ssize_t mail_size = 0; char tmpbuf[156]; snprintf(tmpbuf, 156, "%s.current_size", maildir); if ((CurrentQuotaSizeFd=open(tmpbuf,O_CREAT|O_RDWR,S_IWUSR|S_IRUSR))==-1){ return(mail_size); } read(CurrentQuotaSizeFd, tmpbuf, 100); mail_size = (off_t)atoi(tmpbuf); return(mail_size);}off_t recalc_quota(char *dir_name){ off_t mail_size = 0; char tmpbuf[100]; getcwd(CurrentDir, AUTH_SIZE); mail_size = count_dir(dir_name); chdir(CurrentDir); snprintf(tmpbuf, 100, "%d\n", (int)mail_size); lseek(CurrentQuotaSizeFd, 0L, SEEK_SET); write(CurrentQuotaSizeFd, tmpbuf, strlen(tmpbuf)); return(mail_size);}void update_quota(off_t new_size){ char tmpbuf[100]; snprintf(tmpbuf, 100, "%d\n", (int)new_size); lseek(CurrentQuotaSizeFd, 0L, SEEK_SET); write(CurrentQuotaSizeFd, tmpbuf, strlen(tmpbuf)); close(CurrentQuotaSizeFd);}off_t count_dir(char *dir_name){ DIR *mydir; struct dirent *mydirent; struct stat statbuf; off_t file_size = 0; char *tmpstr; if ( dir_name == NULL ) return(0); if (chdir(dir_name) == -1) { return(0); } if ( (mydir = opendir(".")) == NULL ) { return(0); } while( (mydirent=readdir(mydir)) != NULL ) { if ( strcmp( mydirent->d_name, "..") == 0 ) continue; if ( strcmp( mydirent->d_name, ".") == 0 ) continue; if ( (tmpstr=strstr(mydirent->d_name, ",S="))!=NULL) { tmpstr += 3; file_size += atoi(tmpstr); } else if (stat(mydirent->d_name,&statbuf)==0 && (statbuf.st_mode & S_IFDIR) ) { file_size += count_dir(mydirent->d_name); } } closedir(mydir); if ( dir_name != NULL && strcmp(dir_name, ".." )!=0 && strcmp(dir_name, "." )!=0) { chdir(".."); } return(file_size);}long unsigned qmail_inject_open(char *address){ int pim[2]; long unsigned pid; static char *in_address; in_address = malloc(strlen(address)+1); strcpy( in_address, address); /* skip over an & sign if there */ if (*in_address == '&') ++in_address; if ( pipe(pim) == -1) return(-1); switch(pid=vfork()){ case -1: close(pim[0]); close(pim[1]); return(-1); case 0: close(pim[1]); if (vfd_move(0,pim[0]) == -1 ) _exit(-1); binqqargs[0] = QMAILINJECT; binqqargs[1] = in_address; execv(*binqqargs, binqqargs); } fdm = pim[1]; close(pim[0]); free(in_address); return(pid);}/* * Deliver an email to an address * Return 0 on success * Return less than zero on failure * * -1 = user is over quota * -2 and below are system failures * -3 mail is looping */int deliver_mail(char *address, char *quota){ time_t tm; off_t file_count; long unsigned pid; int write_fd; int inject = 0; /* check if the email is looping to this user */ if ( is_looping( address ) == 1 ) { printf("message is looping %s\n", address ); return(-3); } /* This is a directory/Maildir location */ if ( *address == '/' ) { /* if the user has a quota set */ if ( strncmp(quota, "NOQUOTA", 2) != 0 ) { /* If the message is greater than 1000 bytes and * the user is over thier quota, return it back * to the sender. We allow messages less than 1000 bytes * to go through. This is so system admins can send a * user over quota message */ if (user_over_quota(address, quota)==1 && message_size>1000 ) { printf("user is over quota\n"); return(-1); } } /* Format the email file name */ gethostname(hostname,sizeof(hostname)); pid=getpid(); time (&tm); snprintf(local_file, 156, "%stmp/%lu.%lu.%s,S=%lu", address,(long unsigned)tm,(long unsigned)pid, hostname, (long unsigned)message_size); snprintf(local_file_new, 156, "%snew/%lu.%lu.%s,S=%lu", address,(long unsigned)tm,(long unsigned)pid,hostname, (long unsigned)message_size); /* open the new email file */ if ((write_fd=open(local_file,O_CREAT|O_RDWR,S_IRUSR|S_IWUSR))== -1) { printf("can not open new email file errno=%d file=%s\n", errno, local_file); return(-2); } if ( strcmp( address, bounce) == 0 ) { snprintf(DeliveredTo, AUTH_SIZE, "%s%s", getenv("RPLINE"), getenv("DTLINE")); } else { snprintf(DeliveredTo, AUTH_SIZE, "%sDelivered-To: %s\n", getenv("RPLINE"), maildir_to_email(address)); } /* This is an command */ } else if ( *address == '|' ) { /* run the command */ run_command(address); return(0); /* must be an email address */ } else { char *dtline; char *tstr; qmail_inject_open(address); write_fd = fdm; inject = 1; /* use the DTLINE variable, but skip past the dash in * domain-user@domain
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -