My/Job (개인작업물)
qmail vpopmail 사용자를 위한 pop3 동기화
알찬돌삐
2007. 4. 27. 11:52
저와 같은 방식으로 운영하는 사람들에게 해당됩니다.
각 파일명은 볼드체로 표기합니다.
운영방법
Qmail + vpopmail
발송 : 기존 Qmail smtp 로 발송
수신 : Qmail local 에서 수신후 각 사용자별 디렉토리 new/수신된메일파일
위의 방식은 Qmail + vpopmail 입니다.
이제 이걸 디비에 넣도록 하겠습니다..
.qmail-default
| /home/vpopmail/bin/vdelivermail '' delete
| /var/qmail/bin/qmail-mailtodb "${EXT}" "${SENDER}"
메일이 수신되자마자 /var/qmail/bin/qmail-mailtodb 파일에 인자를 전달해 줍니다.
${EXT} : 수신자
${SENDER} : 발신자
/var/qmail/bin/qmail-mailtodb 는 따로 만든 php 스크립트이며....
메일파일을 파싱후 디비에 insert 하는 작업을 수행합니다.
이건 공개하기 쪽팔려서 -;-;.......
그럼 이제 메일이 수신되면 디비에도 메일이 존재하고 실제 사용자디렉토리에도 메일파일이 존재합니다.
웹메일은 디비의 메일을 사용하여 기존 다람쥐메일(Squirrel Mail) imap 사용시보다 체감속도가 5~8 배 이상
상승하였습니다.
이때 pop3 도 사용하기 때문에.......
pop3 사용자가 pop3 로 접속하여 메일을 삭제하였을 경우.
실제 pop3 에는 메일이 없지만 웹메일(디비)에는 메일이 존재하는 경우가 발생합니다.
이를 위해 qmail-pop3d.c 와 vpopmail 의 vchkpw.c 파일을 수정토록 하겠습니다.
qmail 은 임은재님의 칵테일 패치가 되었을때 수정하였습니다.
글자색이 빨간색이 추가,수정한 부분입니다.
qmail-pop3d.c
#include <sys/types.h>
#include <sys/stat.h>
#include "commands.h"
#include "sig.h"
#include "getln.h"
#include "stralloc.h"
#include "substdio.h"
#include "alloc.h"
#include "open.h"
#include "prioq.h"
#include "scan.h"
#include "fmt.h"
#include "str.h"
#include "exit.h"
#include "maildir.h"
#include "readwrite.h"
#include "timeoutread.h"
#include "timeoutwrite.h"
void die() { _exit(0); }
int saferead(fd,buf,len) int fd; char *buf; int len;
{
int r;
r = timeoutread(1200,fd,buf,len);
if (r <= 0) die();
return r;
}
int safewrite(fd,buf,len) int fd; char *buf; int len;
{
int r;
r = timeoutwrite(1200,fd,buf,len);
if (r <= 0) die();
return r;
}
char ssoutbuf[1024];
substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf);
char ssinbuf[128];
substdio ssin = SUBSTDIO_FDBUF(saferead,0,ssinbuf,sizeof ssinbuf);
char *TheName[1];
void put(buf,len) char *buf; int len;
{
substdio_put(&ssout,buf,len);
}
void puts(s) char *s;
{
substdio_puts(&ssout,s);
}
void flush()
{
substdio_flush(&ssout);
}
void err(s) char *s;
{
puts("-ERR ");
puts(s);
puts("\r\n");
flush();
}
void die_nomem() { err("out of memory"); die(); }
void die_nomaildir() { err("this user has no $HOME/Maildir"); die(); }
void die_scan() { err("unable to scan $HOME/Maildir"); die(); }
void err_syntax() { err("syntax error"); }
void err_unimpl() { err("unimplemented"); }
void err_deleted() { err("already deleted"); }
void err_nozero() { err("messages are counted from 1"); }
void err_toobig() { err("not that many messages"); }
void err_nosuch() { err("unable to open that message"); }
void err_nounlink() { err("unable to unlink all deleted messages"); }
void err_logfileappend() { err("unable to open log file"); }
void okay() { puts("+OK \r\n"); flush(); }
void printfn(fn) char *fn;
{
fn += 4;
put(fn,str_chr(fn,'=>'));
}
char strnum[FMT_ULONG];
stralloc line = {0};
void blast(ssfrom,limit)
substdio *ssfrom;
unsigned long limit;
{
int match;
int inheaders = 1;
for (;;) {
if (getln(ssfrom,&line,&match,'\n') != 0) die();
if (!match && !line.len) break;
if (match) --line.len; /* no way to pass this info over POP */
if (limit) if (!inheaders) if (!--limit) break;
if (!line.len)
inheaders = 0;
else
if (line.s[0] == '.')
put(".",1);
put(line.s,line.len);
put("\r\n",2);
if (!match) break;
}
put("\r\n.\r\n",5);
flush();
}
stralloc filenames = {0};
prioq pq = {0};
struct message {
int flagdeleted;
unsigned long size;
char *fn;
} *m;
int numm;
int last = 0;
void getlist()
{
struct prioq_elt pe;
struct stat st;
int i;
maildir_clean(&line);
if (maildir_scan(&pq,&filenames,1,1) == -1) die_scan();
numm = pq.p ? pq.len : 0;
m = (struct message *) alloc(numm * sizeof(struct message));
if (!m) die_nomem();
for (i = 0;i < numm;++i) {
if (!prioq_min(&pq,&pe)) { numm = i; break; }
prioq_delmin(&pq);
m[i].fn = filenames.s + pe.id;
m[i].flagdeleted = 0;
if (stat(m[i].fn,&st) == -1)
m[i].size = 0;
else
m[i].size = st.st_size;
}
}
void pop3_stat()
{
int i;
unsigned long total;
total = 0;
for (i = 0;i < numm;++i) if (!m[i].flagdeleted) total += m[i].size;
puts("+OK ");
put(strnum,fmt_uint(strnum,numm));
puts(" ");
put(strnum,fmt_ulong(strnum,total));
puts("\r\n");
flush();
}
void pop3_rset()
{
int i;
for (i = 0;i < numm;++i) m[i].flagdeleted = 0;
last = 0;
okay();
}
void pop3_last()
{
puts("+OK ");
put(strnum,fmt_uint(strnum,last));
puts("\r\n");
flush();
}
void pop3_quit()
{
int i;
int fd;
int w;
char filename[255];
sprintf(filename, "/usr/xxxx/xxxx/logs/%s", TheName[0]);
// 위 부분에 해당 로그를 저장할 경로입니다.
// 위 경로에 각 사용자별로 로그가 기록됩니다.
for (i = 0;i < numm;++i)
if (m[i].flagdeleted) {
if (unlink(m[i].fn) == -1) err_nounlink();
fd = open_append(filename);
if (fd == -1) err_logfileappend();
w = write(fd, m[i].fn, strlen(m[i].fn));
w = write(fd, "\n", 1);
if (w <= 0) err_logfileappend();
close(fd);
}
else
if (str_start(m[i].fn,"new/")) {
if (!stralloc_copys(&line,"cur/")) die_nomem();
if (!stralloc_cats(&line,m[i].fn + 4)) die_nomem();
if (!stralloc_cats(&line,"=>2,")) die_nomem();
if (!stralloc_0(&line)) die_nomem();
rename(m[i].fn,line.s); /* if it fails, bummer */
}
okay();
die();
}
int msgno(arg) char *arg;
{
unsigned long u;
if (!scan_ulong(arg,&u)) { err_syntax(); return -1; }
if (!u) { err_nozero(); return -1; }
--u;
if (u >= numm) { err_toobig(); return -1; }
if (m[u].flagdeleted) { err_deleted(); return -1; }
return u;
}
void pop3_dele(arg) char *arg;
{
int i;
i = msgno(arg);
if (i == -1) return;
m[i].flagdeleted = 1;
if (i + 1 > last) last = i + 1;
okay();
}
void list(i,flaguidl)
int i;
int flaguidl;
{
put(strnum,fmt_uint(strnum,i + 1));
puts(" ");
if (flaguidl) printfn(m[i].fn);
else put(strnum,fmt_ulong(strnum,m[i].size));
puts("\r\n");
}
void dolisting(arg,flaguidl) char *arg; int flaguidl;
{
unsigned int i;
if (*arg) {
i = msgno(arg);
if (i == -1) return;
puts("+OK ");
list(i,flaguidl);
}
else {
okay();
for (i = 0;i < numm;++i)
if (!m[i].flagdeleted)
list(i,flaguidl);
puts(".\r\n");
}
flush();
}
void pop3_uidl(arg) char *arg; { dolisting(arg,1); }
void pop3_list(arg) char *arg; { dolisting(arg,0); }
substdio ssmsg; char ssmsgbuf[1024];
void pop3_top(arg) char *arg;
{
int i;
unsigned long limit;
int fd;
i = msgno(arg);
if (i == -1) return;
arg += scan_ulong(arg,&limit);
while (*arg == ' ') ++arg;
if (scan_ulong(arg,&limit)) ++limit; else limit = 0;
fd = open_read(m[i].fn);
if (fd == -1) { err_nosuch(); return; }
okay();
substdio_fdbuf(&ssmsg,read,fd,ssmsgbuf,sizeof(ssmsgbuf));
blast(&ssmsg,limit);
close(fd);
}
struct commands pop3commands[] = {
{ "quit", pop3_quit, 0 }
, { "stat", pop3_stat, 0 }
, { "list", pop3_list, 0 }
, { "uidl", pop3_uidl, 0 }
, { "dele", pop3_dele, 0 }
, { "retr", pop3_top, 0 }
, { "rset", pop3_rset, 0 }
, { "last", pop3_last, 0 }
, { "top", pop3_top, 0 }
, { "noop", okay, 0 }
, { 0, err_unimpl, 0 }
} ;
void main(argc,argv)
int argc;
char **argv;
{
sig_alarmcatch(die);
sig_alarmcatch(die);
sig_pipeignore();
TheName[0] = argv[2];
if (!argv[1]) die_nomaildir();
if (chdir(argv[1]) == -1) die_nomaildir();
getlist();
okay();
commands(&ssin,pop3commands);
die();
}
#include <sys/stat.h>
#include "commands.h"
#include "sig.h"
#include "getln.h"
#include "stralloc.h"
#include "substdio.h"
#include "alloc.h"
#include "open.h"
#include "prioq.h"
#include "scan.h"
#include "fmt.h"
#include "str.h"
#include "exit.h"
#include "maildir.h"
#include "readwrite.h"
#include "timeoutread.h"
#include "timeoutwrite.h"
void die() { _exit(0); }
int saferead(fd,buf,len) int fd; char *buf; int len;
{
int r;
r = timeoutread(1200,fd,buf,len);
if (r <= 0) die();
return r;
}
int safewrite(fd,buf,len) int fd; char *buf; int len;
{
int r;
r = timeoutwrite(1200,fd,buf,len);
if (r <= 0) die();
return r;
}
char ssoutbuf[1024];
substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf);
char ssinbuf[128];
substdio ssin = SUBSTDIO_FDBUF(saferead,0,ssinbuf,sizeof ssinbuf);
char *TheName[1];
void put(buf,len) char *buf; int len;
{
substdio_put(&ssout,buf,len);
}
void puts(s) char *s;
{
substdio_puts(&ssout,s);
}
void flush()
{
substdio_flush(&ssout);
}
void err(s) char *s;
{
puts("-ERR ");
puts(s);
puts("\r\n");
flush();
}
void die_nomem() { err("out of memory"); die(); }
void die_nomaildir() { err("this user has no $HOME/Maildir"); die(); }
void die_scan() { err("unable to scan $HOME/Maildir"); die(); }
void err_syntax() { err("syntax error"); }
void err_unimpl() { err("unimplemented"); }
void err_deleted() { err("already deleted"); }
void err_nozero() { err("messages are counted from 1"); }
void err_toobig() { err("not that many messages"); }
void err_nosuch() { err("unable to open that message"); }
void err_nounlink() { err("unable to unlink all deleted messages"); }
void err_logfileappend() { err("unable to open log file"); }
void okay() { puts("+OK \r\n"); flush(); }
void printfn(fn) char *fn;
{
fn += 4;
put(fn,str_chr(fn,'=>'));
}
char strnum[FMT_ULONG];
stralloc line = {0};
void blast(ssfrom,limit)
substdio *ssfrom;
unsigned long limit;
{
int match;
int inheaders = 1;
for (;;) {
if (getln(ssfrom,&line,&match,'\n') != 0) die();
if (!match && !line.len) break;
if (match) --line.len; /* no way to pass this info over POP */
if (limit) if (!inheaders) if (!--limit) break;
if (!line.len)
inheaders = 0;
else
if (line.s[0] == '.')
put(".",1);
put(line.s,line.len);
put("\r\n",2);
if (!match) break;
}
put("\r\n.\r\n",5);
flush();
}
stralloc filenames = {0};
prioq pq = {0};
struct message {
int flagdeleted;
unsigned long size;
char *fn;
} *m;
int numm;
int last = 0;
void getlist()
{
struct prioq_elt pe;
struct stat st;
int i;
maildir_clean(&line);
if (maildir_scan(&pq,&filenames,1,1) == -1) die_scan();
numm = pq.p ? pq.len : 0;
m = (struct message *) alloc(numm * sizeof(struct message));
if (!m) die_nomem();
for (i = 0;i < numm;++i) {
if (!prioq_min(&pq,&pe)) { numm = i; break; }
prioq_delmin(&pq);
m[i].fn = filenames.s + pe.id;
m[i].flagdeleted = 0;
if (stat(m[i].fn,&st) == -1)
m[i].size = 0;
else
m[i].size = st.st_size;
}
}
void pop3_stat()
{
int i;
unsigned long total;
total = 0;
for (i = 0;i < numm;++i) if (!m[i].flagdeleted) total += m[i].size;
puts("+OK ");
put(strnum,fmt_uint(strnum,numm));
puts(" ");
put(strnum,fmt_ulong(strnum,total));
puts("\r\n");
flush();
}
void pop3_rset()
{
int i;
for (i = 0;i < numm;++i) m[i].flagdeleted = 0;
last = 0;
okay();
}
void pop3_last()
{
puts("+OK ");
put(strnum,fmt_uint(strnum,last));
puts("\r\n");
flush();
}
void pop3_quit()
{
int i;
int fd;
int w;
char filename[255];
sprintf(filename, "/usr/xxxx/xxxx/logs/%s", TheName[0]);
// 위 부분에 해당 로그를 저장할 경로입니다.
// 위 경로에 각 사용자별로 로그가 기록됩니다.
for (i = 0;i < numm;++i)
if (m[i].flagdeleted) {
if (unlink(m[i].fn) == -1) err_nounlink();
fd = open_append(filename);
if (fd == -1) err_logfileappend();
w = write(fd, m[i].fn, strlen(m[i].fn));
w = write(fd, "\n", 1);
if (w <= 0) err_logfileappend();
close(fd);
}
else
if (str_start(m[i].fn,"new/")) {
if (!stralloc_copys(&line,"cur/")) die_nomem();
if (!stralloc_cats(&line,m[i].fn + 4)) die_nomem();
if (!stralloc_cats(&line,"=>2,")) die_nomem();
if (!stralloc_0(&line)) die_nomem();
rename(m[i].fn,line.s); /* if it fails, bummer */
}
okay();
die();
}
int msgno(arg) char *arg;
{
unsigned long u;
if (!scan_ulong(arg,&u)) { err_syntax(); return -1; }
if (!u) { err_nozero(); return -1; }
--u;
if (u >= numm) { err_toobig(); return -1; }
if (m[u].flagdeleted) { err_deleted(); return -1; }
return u;
}
void pop3_dele(arg) char *arg;
{
int i;
i = msgno(arg);
if (i == -1) return;
m[i].flagdeleted = 1;
if (i + 1 > last) last = i + 1;
okay();
}
void list(i,flaguidl)
int i;
int flaguidl;
{
put(strnum,fmt_uint(strnum,i + 1));
puts(" ");
if (flaguidl) printfn(m[i].fn);
else put(strnum,fmt_ulong(strnum,m[i].size));
puts("\r\n");
}
void dolisting(arg,flaguidl) char *arg; int flaguidl;
{
unsigned int i;
if (*arg) {
i = msgno(arg);
if (i == -1) return;
puts("+OK ");
list(i,flaguidl);
}
else {
okay();
for (i = 0;i < numm;++i)
if (!m[i].flagdeleted)
list(i,flaguidl);
puts(".\r\n");
}
flush();
}
void pop3_uidl(arg) char *arg; { dolisting(arg,1); }
void pop3_list(arg) char *arg; { dolisting(arg,0); }
substdio ssmsg; char ssmsgbuf[1024];
void pop3_top(arg) char *arg;
{
int i;
unsigned long limit;
int fd;
i = msgno(arg);
if (i == -1) return;
arg += scan_ulong(arg,&limit);
while (*arg == ' ') ++arg;
if (scan_ulong(arg,&limit)) ++limit; else limit = 0;
fd = open_read(m[i].fn);
if (fd == -1) { err_nosuch(); return; }
okay();
substdio_fdbuf(&ssmsg,read,fd,ssmsgbuf,sizeof(ssmsgbuf));
blast(&ssmsg,limit);
close(fd);
}
struct commands pop3commands[] = {
{ "quit", pop3_quit, 0 }
, { "stat", pop3_stat, 0 }
, { "list", pop3_list, 0 }
, { "uidl", pop3_uidl, 0 }
, { "dele", pop3_dele, 0 }
, { "retr", pop3_top, 0 }
, { "rset", pop3_rset, 0 }
, { "last", pop3_last, 0 }
, { "top", pop3_top, 0 }
, { "noop", okay, 0 }
, { 0, err_unimpl, 0 }
} ;
void main(argc,argv)
int argc;
char **argv;
{
sig_alarmcatch(die);
sig_alarmcatch(die);
sig_pipeignore();
TheName[0] = argv[2];
if (!argv[1]) die_nomaildir();
if (chdir(argv[1]) == -1) die_nomaildir();
getlist();
okay();
commands(&ssin,pop3commands);
die();
}
Qmail 의 Makefile
전체 파일을 올리지 않고 수정내역이 두군데이기 때문에 두군데만 올립니다.
qmail-pop3d: \
load qmail-pop3d.o commands.o case.a timeoutread.o timeoutwrite.o \
qsutil.o \
maildir.o prioq.o now.o env.a strerr.a sig.a open.a getln.a \
stralloc.a alloc.a substdio.a error.a str.a fs.a socket.lib
compile qmail-pop3d.c commands.h sig.h getln.h stralloc.h gen_alloc.h \
substdio.h alloc.h open.h prioq.h datetime.h gen_alloc.h scan.h fmt.h \
str.h exit.h maildir.h strerr.h readwrite.h timeoutread.h \
timeoutwrite.h qsutil.h
vpopmail 의 vchkpw.c
/*
* vchkpw
* This is a complete re-write of the 4.9 and below vchkpw versions
*
* Copyright (C) 1999,2001 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
*
* Purpose: vchkpw a pop authentication module for qmail-pop3d server
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <sys/wait.h>
#include <pwd.h>
#include <sys/types.h>
#include "config.h"
#include "vpopmail.h"
#include "vlog.h"
#include "vauth.h"
#ifdef HAS_SHADOW
#include <shadow.h>
#endif
/* Definitions */
#define VCHKPW_USER "USER="
#define VCHKPW_HOME "HOME="
#define VCHKPW_SHELL "SHELL=NOLOGIN"
#define VCHKPW_VPOPUSER "VPOPUSER="
/* For tracking ip of client asking for pop service */
char *IpAddr;
/* storage of authentication information */
#define AUTH_SIZE 156
#define AUTH_INC_SIZE 155
char TheName[AUTH_SIZE];
char TheUser[AUTH_SIZE];
char ThePass[AUTH_SIZE];
char TheCrypted[AUTH_SIZE];
char TheDomain[AUTH_SIZE];
/* log line buffer */
#define LOG_LINE_SIZE 500
char LogLine[LOG_LINE_SIZE];
/* environment variable buffers */
#define MAX_ENV_BUF 100
static char envbuf1[MAX_ENV_BUF];
static char envbuf2[MAX_ENV_BUF];
static char envbuf3[MAX_ENV_BUF];
static char envbuf4[MAX_ENV_BUF];
/* shared data */
uid_t pw_uid;
gid_t pw_gid;
char *pw_dir=NULL;
struct vqpasswd *vpw = NULL;
/* Forward declaration */
char *sysc(char *mess);
void login_virtual_user();
void login_system_user();
void read_user_pass();
void vlog(int verror, char *TheUser, char *TheDomain, char *ThePass, char *TheName, char *IpAddr, char *LogLine);
void vchkpw_exit(int err);
void run_command(char *prog);
int main( int argc, char **argv)
{
char *aRgu[4]; // execvp 에서 아이디를 같이 전달할 char
if ( (IpAddr = getenv("TCPREMOTEIP")) == NULL) IpAddr="";
/* read in the user name and password from file descriptor 3 */
read_user_pass();
if ( parse_email( TheName, TheUser, TheDomain, AUTH_SIZE) != 0 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: invalid user/domain characters %s:%s", TheName, IpAddr);
vlog(VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName,
IpAddr, LogLine);
vchkpw_exit(20);
}
/* check if this virtual domain is in the system
* we look in /var/qmail/users/cdb file
* and while we are at it, let's get the domains
* user id and group id.
*/
if ( (vpw = vauth_getpw(TheUser, TheDomain)) != NULL ) {
vget_assign(TheDomain,NULL,0,&pw_uid,&pw_gid);
login_virtual_user();
/* if it is not in the virtual domains
* then check the user in /etc/passwd
*/
#ifdef ENABLE_PASSWD
} else if ( ENABLE_PASSWD == 1 ) {
login_system_user();
#endif
} else {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: vpopmail user not found %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(3);
}
vclose();
/* The user is authenticated, now setup the environment
* for qmail-pop3d
*/
/* Set the programs effective group id */
if ( setgid(pw_gid) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: setgid %lu failed errno %d %s@%s:%s",
(long unsigned)pw_gid, errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(4);
}
/* Set the programs effective user id */
if ( setuid(pw_uid) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: setuid %lu failed errno %d %s@%s:%s",
(long unsigned)pw_uid, errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(5);
}
/* Change to the users Maildir directory */
if (chdir(pw_dir) == -1) {
if ( vpw!=NULL) {
if ( vmake_maildir(TheDomain, vpw->pw_dir )!= VA_SUCCESS ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: autocreate dir errno %d %s %s@%s:%s",
errno, pw_dir, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass,
TheName, IpAddr, LogLine);
vchkpw_exit(6);
}
chdir(pw_dir);
} else {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: chdir failed errno %d %s %s@%s:%s",
errno, pw_dir, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass,
TheName, IpAddr, LogLine);
vchkpw_exit(6);
}
}
/* The the USER variable */
snprintf (envbuf1, sizeof(envbuf1), "%s%s", VCHKPW_USER, TheUser);
if ( putenv(envbuf1) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(USER) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(7);
}
/* Now HOME */
snprintf (envbuf2, sizeof(envbuf2), "%s%s", VCHKPW_HOME, pw_dir);
if ( putenv(envbuf2) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(HOME) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(8);
}
/* Now SHELL */
strncpy(envbuf3,VCHKPW_SHELL,sizeof(envbuf3));
envbuf3[sizeof(envbuf3)-1] = 0; /* make sure it's NULL terminated */
if ( putenv(envbuf3) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(SHELL) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(9);
}
/* Now VPOPUSER */
snprintf (envbuf4, sizeof(envbuf4), "%s%s", VCHKPW_VPOPUSER, TheName);
if ( putenv(envbuf4) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(VPOPUSER) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(10);
}
/* close the log connection */
if ( ENABLE_LOGGING > 0 ) {
closelog();
}
/* And now a simple way to kick off the next program */
// execvp(argv[1],argv+1);
/* Modify BY crazyidol */
aRgu[0] = argv[1];
aRgu[1] = argv[2];
aRgu[2] = TheName;
aRgu[3] = 0;
execvp(argv[1],aRgu);
/* all done, time to release resources and go away */
exit(0);
}
/* clean a buffer for syslog */
char *sysc(char *mess)
{
char *ripper;
for(ripper=mess;*ripper!=0;++ripper) {
if ( *ripper=='%' ) {
*ripper = '#';
}
}
return(mess);
}
void read_user_pass()
{
int i,j,l;
/* Read the user and password from file descriptor 3
* use TheDomain variable as temporary storage of the
* full incoming line
*/
memset(TheDomain,0,AUTH_SIZE);
for(i=0;i<AUTH_SIZE;i+=j){
/* read a chunk */
j = read(3,&TheDomain[i],AUTH_SIZE-i-1);
/* on error exit out */
if ( j == -1 ) {
printf("vchkpw: what the hell are you doing running vchkpw on the command line!! It's only for talking with qmail-popup and qmail-pop3d.\n");
vchkpw_exit(11);
} else if ( j == 0 ) {
break;
}
}
/* close the user/pass file descriptor */
close(3);
/* parse out the name */
memset(TheName,0,AUTH_SIZE);
for(l=0;l<AUTH_INC_SIZE;++l){
TheName[l] = TheDomain[l];
if ( TheName[l] == 0 ) break;
if (l==i)break;
}
/* parse out the password */
memset(ThePass,0,AUTH_SIZE);
for(j=0,++l;l<AUTH_INC_SIZE;++j,++l){
ThePass[j] = TheDomain[l];
if ( ThePass[j] == 0 ) break;
if (l==i)break;
}
/* open the log if configured */
if ( ENABLE_LOGGING > 0 ) {
openlog(LOG_NAME,LOG_PID,LOG_MAIL);
}
if ( TheName[0] == 0 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: null user name given %s:%s", TheName, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(12);
}
if ( ThePass[0] == 0 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: null password given %s:%s", TheName, IpAddr);
vlog(VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(13);
}
}
void login_virtual_user()
{
/* If thier directory path is empty make them a new one */
if ( vpw->pw_dir == NULL || vpw->pw_dir[0]==0 ) {
if ( make_user_dir(vpw->pw_name, TheDomain, pw_uid, pw_gid)==NULL){
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: dir auto create failed %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(14);
}
}
#ifdef ENABLE_LEARN_PASSWORDS
/* check for a valid vpopmail passwd field */
if ( vpw->pw_passwd==NULL||vpw->pw_passwd[0]==0) {
mkpasswd3(ThePass,TheCrypted, AUTH_SIZE);
vpw->pw_passwd = TheCrypted;
vpw->pw_clear_passwd = ThePass;
vauth_setpw(vpw, TheDomain);
#ifdef POP_FETCH
if ( pop_init(vpw, "") {
while (pop_loop) == 1 );
}
#endif
}
#else
if ( vpw->pw_passwd==NULL||vpw->pw_passwd[0]==0) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: user has no password %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName,
IpAddr, LogLine);
vchkpw_exit(15);
}
#endif
/* Encrypt the clear text password using the crypted
* password as the salt then
* check if it matches the encrypted password
* If it does not match, log errors if requested
* and exit
*/
if ( strcmp(crypt(ThePass,vpw->pw_passwd),vpw->pw_passwd) != 0 ) {
if ( ENABLE_LOGGING==1||ENABLE_LOGGING==2){
snprintf(LogLine, LOG_LINE_SIZE, "vchkpw: password fail %s@%s:%s",
TheUser, TheDomain, IpAddr);
} else if ( ENABLE_LOGGING==3||ENABLE_LOGGING==4){
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: password fail %s %s@%s:%s",
ThePass, TheUser, TheDomain, IpAddr);
} else {
LogLine[0] = 0;
}
vlog( VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName,
IpAddr, LogLine);
vchkpw_exit(3);
}
#ifdef ENABLE_LEARN_PASSWORDS
#ifdef CLEAR_PASS
/* User with pw_clear_passwd unset but pw_passwd set
* should have the pw_clear_passwd field filled in
*/
if ( vpw->pw_clear_passwd==NULL||vpw->pw_clear_passwd[0]==0) {
vpw->pw_clear_passwd = ThePass;
vauth_setpw(vpw, TheDomain);
}
#endif
#endif
/* They are authenticated now, check for restrictions
* Check if they are allowed pop access
*/
if ( vpw->pw_gid & NO_POP ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: pop access denied %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_ACCESS, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(1);
}
/* They are authenticated, log the success if configured */
snprintf(LogLine, LOG_LINE_SIZE, "vchkpw: login success %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_AUTH, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
/* If authentication logging is enabled
* update the authentication time on the account
*/
#ifdef ENABLE_AUTH_LOGGING
vset_lastauth(TheUser,TheDomain,IpAddr);
#endif
#ifdef POP_AUTH_OPEN_RELAY
/* Check if we should open up relay for this account */
if ( (vpw->pw_gid & NO_RELAY) == 0 ) {
open_smtp_relay();
}
#endif
/* Save the directory pointer */
pw_dir = vpw->pw_dir;
}
#ifdef ENABLE_PASSWD
void login_system_user()
{
struct passwd *pw;
#ifdef HAS_SHADOW
struct spwd *spw;
#endif
if ((pw=getpwnam(TheUser)) == NULL ) {
snprintf(LogLine, LOG_LINE_SIZE, "vchkpw: system user not found %s:%s",
TheUser, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(21);
}
#ifdef HAS_SHADOW
if ((spw = getspnam(TheUser)) == NULL) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: system user shadow entry not found %s:%s",
TheName, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(22);
}
if ( strcmp(crypt(ThePass,spw->sp_pwdp),spw->sp_pwdp) != 0 ) {
#else
if ( strcmp(crypt(ThePass,pw->pw_passwd),pw->pw_passwd) != 0 ) {
#endif
if (ENABLE_LOGGING==1||ENABLE_LOGGING==2) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: system password fail %s:%s", TheName, IpAddr);
} else if (ENABLE_LOGGING==3||ENABLE_LOGGING==4) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: system password fail %s %s:%s",
ThePass, TheName, IpAddr);
} else {
LogLine[0] = 0;
}
vlog(VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(23);
}
pw_uid = pw->pw_uid;
pw_gid = pw->pw_gid;
pw_dir = pw->pw_dir;
#ifdef POP_AUTH_OPEN_RELAY
open_smtp_relay();
#endif
}
#endif
void vchkpw_exit(int err)
{
if ( ENABLE_LOGGING > 0 ) {
closelog();
}
vclose();
exit(err);
}
/* log messages and figure out what type they are and where they should go depending on configure options */
/* any one of the pointers can be null, i.e. the information is not available */
/* messages are autmatically cleaned for syslog if it is necessary */
void vlog(int verror, char *TheUser, char *TheDomain, char *ThePass, char *TheName, char *IpAddr, char *LogLine) {
/* always log to syslog if enabled */
if ( (verror == VLOG_ERROR_PASSWD) && ( ENABLE_LOGGING==1 || ENABLE_LOGGING==2 || ENABLE_LOGGING==3 || ENABLE_LOGGING==4 ) ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_ERROR_INTERNAL ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_ERROR_LOGON ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_ERROR_ACCESS ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_AUTH && ( ENABLE_LOGGING == 1 || ENABLE_LOGGING == 4 ) ) {
syslog(LOG_NOTICE,sysc(LogLine));
}
#ifdef ENABLE_MYSQL_LOGGING
/* always log to mysql if mysql logging is enabled and it
* is not internal error
*/
if ( (verror == VLOG_ERROR_PASSWD) && ( ENABLE_LOGGING==1 || ENABLE_LOGGING==2 || ENABLE_LOGGING==3 || ENABLE_LOGGING==4 ) ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_ERROR_INTERNAL ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_ERROR_LOGON ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_ERROR_ACCESS ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_AUTH && ( ENABLE_LOGGING == 1 || ENABLE_LOGGING == 4 ) ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
}
#endif
}
* vchkpw
* This is a complete re-write of the 4.9 and below vchkpw versions
*
* Copyright (C) 1999,2001 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
*
* Purpose: vchkpw a pop authentication module for qmail-pop3d server
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <sys/wait.h>
#include <pwd.h>
#include <sys/types.h>
#include "config.h"
#include "vpopmail.h"
#include "vlog.h"
#include "vauth.h"
#ifdef HAS_SHADOW
#include <shadow.h>
#endif
/* Definitions */
#define VCHKPW_USER "USER="
#define VCHKPW_HOME "HOME="
#define VCHKPW_SHELL "SHELL=NOLOGIN"
#define VCHKPW_VPOPUSER "VPOPUSER="
/* For tracking ip of client asking for pop service */
char *IpAddr;
/* storage of authentication information */
#define AUTH_SIZE 156
#define AUTH_INC_SIZE 155
char TheName[AUTH_SIZE];
char TheUser[AUTH_SIZE];
char ThePass[AUTH_SIZE];
char TheCrypted[AUTH_SIZE];
char TheDomain[AUTH_SIZE];
/* log line buffer */
#define LOG_LINE_SIZE 500
char LogLine[LOG_LINE_SIZE];
/* environment variable buffers */
#define MAX_ENV_BUF 100
static char envbuf1[MAX_ENV_BUF];
static char envbuf2[MAX_ENV_BUF];
static char envbuf3[MAX_ENV_BUF];
static char envbuf4[MAX_ENV_BUF];
/* shared data */
uid_t pw_uid;
gid_t pw_gid;
char *pw_dir=NULL;
struct vqpasswd *vpw = NULL;
/* Forward declaration */
char *sysc(char *mess);
void login_virtual_user();
void login_system_user();
void read_user_pass();
void vlog(int verror, char *TheUser, char *TheDomain, char *ThePass, char *TheName, char *IpAddr, char *LogLine);
void vchkpw_exit(int err);
void run_command(char *prog);
int main( int argc, char **argv)
{
char *aRgu[4]; // execvp 에서 아이디를 같이 전달할 char
if ( (IpAddr = getenv("TCPREMOTEIP")) == NULL) IpAddr="";
/* read in the user name and password from file descriptor 3 */
read_user_pass();
if ( parse_email( TheName, TheUser, TheDomain, AUTH_SIZE) != 0 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: invalid user/domain characters %s:%s", TheName, IpAddr);
vlog(VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName,
IpAddr, LogLine);
vchkpw_exit(20);
}
/* check if this virtual domain is in the system
* we look in /var/qmail/users/cdb file
* and while we are at it, let's get the domains
* user id and group id.
*/
if ( (vpw = vauth_getpw(TheUser, TheDomain)) != NULL ) {
vget_assign(TheDomain,NULL,0,&pw_uid,&pw_gid);
login_virtual_user();
/* if it is not in the virtual domains
* then check the user in /etc/passwd
*/
#ifdef ENABLE_PASSWD
} else if ( ENABLE_PASSWD == 1 ) {
login_system_user();
#endif
} else {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: vpopmail user not found %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(3);
}
vclose();
/* The user is authenticated, now setup the environment
* for qmail-pop3d
*/
/* Set the programs effective group id */
if ( setgid(pw_gid) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: setgid %lu failed errno %d %s@%s:%s",
(long unsigned)pw_gid, errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(4);
}
/* Set the programs effective user id */
if ( setuid(pw_uid) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: setuid %lu failed errno %d %s@%s:%s",
(long unsigned)pw_uid, errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(5);
}
/* Change to the users Maildir directory */
if (chdir(pw_dir) == -1) {
if ( vpw!=NULL) {
if ( vmake_maildir(TheDomain, vpw->pw_dir )!= VA_SUCCESS ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: autocreate dir errno %d %s %s@%s:%s",
errno, pw_dir, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass,
TheName, IpAddr, LogLine);
vchkpw_exit(6);
}
chdir(pw_dir);
} else {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: chdir failed errno %d %s %s@%s:%s",
errno, pw_dir, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass,
TheName, IpAddr, LogLine);
vchkpw_exit(6);
}
}
/* The the USER variable */
snprintf (envbuf1, sizeof(envbuf1), "%s%s", VCHKPW_USER, TheUser);
if ( putenv(envbuf1) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(USER) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(7);
}
/* Now HOME */
snprintf (envbuf2, sizeof(envbuf2), "%s%s", VCHKPW_HOME, pw_dir);
if ( putenv(envbuf2) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(HOME) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(8);
}
/* Now SHELL */
strncpy(envbuf3,VCHKPW_SHELL,sizeof(envbuf3));
envbuf3[sizeof(envbuf3)-1] = 0; /* make sure it's NULL terminated */
if ( putenv(envbuf3) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(SHELL) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(9);
}
/* Now VPOPUSER */
snprintf (envbuf4, sizeof(envbuf4), "%s%s", VCHKPW_VPOPUSER, TheName);
if ( putenv(envbuf4) == -1 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: putenv(VPOPUSER) failed errno %d %s@%s:%s",
errno, TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(10);
}
/* close the log connection */
if ( ENABLE_LOGGING > 0 ) {
closelog();
}
/* And now a simple way to kick off the next program */
// execvp(argv[1],argv+1);
/* Modify BY crazyidol */
aRgu[0] = argv[1];
aRgu[1] = argv[2];
aRgu[2] = TheName;
aRgu[3] = 0;
execvp(argv[1],aRgu);
/* all done, time to release resources and go away */
exit(0);
}
/* clean a buffer for syslog */
char *sysc(char *mess)
{
char *ripper;
for(ripper=mess;*ripper!=0;++ripper) {
if ( *ripper=='%' ) {
*ripper = '#';
}
}
return(mess);
}
void read_user_pass()
{
int i,j,l;
/* Read the user and password from file descriptor 3
* use TheDomain variable as temporary storage of the
* full incoming line
*/
memset(TheDomain,0,AUTH_SIZE);
for(i=0;i<AUTH_SIZE;i+=j){
/* read a chunk */
j = read(3,&TheDomain[i],AUTH_SIZE-i-1);
/* on error exit out */
if ( j == -1 ) {
printf("vchkpw: what the hell are you doing running vchkpw on the command line!! It's only for talking with qmail-popup and qmail-pop3d.\n");
vchkpw_exit(11);
} else if ( j == 0 ) {
break;
}
}
/* close the user/pass file descriptor */
close(3);
/* parse out the name */
memset(TheName,0,AUTH_SIZE);
for(l=0;l<AUTH_INC_SIZE;++l){
TheName[l] = TheDomain[l];
if ( TheName[l] == 0 ) break;
if (l==i)break;
}
/* parse out the password */
memset(ThePass,0,AUTH_SIZE);
for(j=0,++l;l<AUTH_INC_SIZE;++j,++l){
ThePass[j] = TheDomain[l];
if ( ThePass[j] == 0 ) break;
if (l==i)break;
}
/* open the log if configured */
if ( ENABLE_LOGGING > 0 ) {
openlog(LOG_NAME,LOG_PID,LOG_MAIL);
}
if ( TheName[0] == 0 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: null user name given %s:%s", TheName, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(12);
}
if ( ThePass[0] == 0 ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: null password given %s:%s", TheName, IpAddr);
vlog(VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(13);
}
}
void login_virtual_user()
{
/* If thier directory path is empty make them a new one */
if ( vpw->pw_dir == NULL || vpw->pw_dir[0]==0 ) {
if ( make_user_dir(vpw->pw_name, TheDomain, pw_uid, pw_gid)==NULL){
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: dir auto create failed %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(14);
}
}
#ifdef ENABLE_LEARN_PASSWORDS
/* check for a valid vpopmail passwd field */
if ( vpw->pw_passwd==NULL||vpw->pw_passwd[0]==0) {
mkpasswd3(ThePass,TheCrypted, AUTH_SIZE);
vpw->pw_passwd = TheCrypted;
vpw->pw_clear_passwd = ThePass;
vauth_setpw(vpw, TheDomain);
#ifdef POP_FETCH
if ( pop_init(vpw, "") {
while (pop_loop) == 1 );
}
#endif
}
#else
if ( vpw->pw_passwd==NULL||vpw->pw_passwd[0]==0) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: user has no password %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_INTERNAL, TheUser, TheDomain, ThePass, TheName,
IpAddr, LogLine);
vchkpw_exit(15);
}
#endif
/* Encrypt the clear text password using the crypted
* password as the salt then
* check if it matches the encrypted password
* If it does not match, log errors if requested
* and exit
*/
if ( strcmp(crypt(ThePass,vpw->pw_passwd),vpw->pw_passwd) != 0 ) {
if ( ENABLE_LOGGING==1||ENABLE_LOGGING==2){
snprintf(LogLine, LOG_LINE_SIZE, "vchkpw: password fail %s@%s:%s",
TheUser, TheDomain, IpAddr);
} else if ( ENABLE_LOGGING==3||ENABLE_LOGGING==4){
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: password fail %s %s@%s:%s",
ThePass, TheUser, TheDomain, IpAddr);
} else {
LogLine[0] = 0;
}
vlog( VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName,
IpAddr, LogLine);
vchkpw_exit(3);
}
#ifdef ENABLE_LEARN_PASSWORDS
#ifdef CLEAR_PASS
/* User with pw_clear_passwd unset but pw_passwd set
* should have the pw_clear_passwd field filled in
*/
if ( vpw->pw_clear_passwd==NULL||vpw->pw_clear_passwd[0]==0) {
vpw->pw_clear_passwd = ThePass;
vauth_setpw(vpw, TheDomain);
}
#endif
#endif
/* They are authenticated now, check for restrictions
* Check if they are allowed pop access
*/
if ( vpw->pw_gid & NO_POP ) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: pop access denied %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_ERROR_ACCESS, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(1);
}
/* They are authenticated, log the success if configured */
snprintf(LogLine, LOG_LINE_SIZE, "vchkpw: login success %s@%s:%s",
TheUser, TheDomain, IpAddr);
vlog(VLOG_AUTH, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
/* If authentication logging is enabled
* update the authentication time on the account
*/
#ifdef ENABLE_AUTH_LOGGING
vset_lastauth(TheUser,TheDomain,IpAddr);
#endif
#ifdef POP_AUTH_OPEN_RELAY
/* Check if we should open up relay for this account */
if ( (vpw->pw_gid & NO_RELAY) == 0 ) {
open_smtp_relay();
}
#endif
/* Save the directory pointer */
pw_dir = vpw->pw_dir;
}
#ifdef ENABLE_PASSWD
void login_system_user()
{
struct passwd *pw;
#ifdef HAS_SHADOW
struct spwd *spw;
#endif
if ((pw=getpwnam(TheUser)) == NULL ) {
snprintf(LogLine, LOG_LINE_SIZE, "vchkpw: system user not found %s:%s",
TheUser, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(21);
}
#ifdef HAS_SHADOW
if ((spw = getspnam(TheUser)) == NULL) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: system user shadow entry not found %s:%s",
TheName, IpAddr);
vlog(VLOG_ERROR_LOGON, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(22);
}
if ( strcmp(crypt(ThePass,spw->sp_pwdp),spw->sp_pwdp) != 0 ) {
#else
if ( strcmp(crypt(ThePass,pw->pw_passwd),pw->pw_passwd) != 0 ) {
#endif
if (ENABLE_LOGGING==1||ENABLE_LOGGING==2) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: system password fail %s:%s", TheName, IpAddr);
} else if (ENABLE_LOGGING==3||ENABLE_LOGGING==4) {
snprintf(LogLine, LOG_LINE_SIZE,
"vchkpw: system password fail %s %s:%s",
ThePass, TheName, IpAddr);
} else {
LogLine[0] = 0;
}
vlog(VLOG_ERROR_PASSWD, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine);
vchkpw_exit(23);
}
pw_uid = pw->pw_uid;
pw_gid = pw->pw_gid;
pw_dir = pw->pw_dir;
#ifdef POP_AUTH_OPEN_RELAY
open_smtp_relay();
#endif
}
#endif
void vchkpw_exit(int err)
{
if ( ENABLE_LOGGING > 0 ) {
closelog();
}
vclose();
exit(err);
}
/* log messages and figure out what type they are and where they should go depending on configure options */
/* any one of the pointers can be null, i.e. the information is not available */
/* messages are autmatically cleaned for syslog if it is necessary */
void vlog(int verror, char *TheUser, char *TheDomain, char *ThePass, char *TheName, char *IpAddr, char *LogLine) {
/* always log to syslog if enabled */
if ( (verror == VLOG_ERROR_PASSWD) && ( ENABLE_LOGGING==1 || ENABLE_LOGGING==2 || ENABLE_LOGGING==3 || ENABLE_LOGGING==4 ) ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_ERROR_INTERNAL ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_ERROR_LOGON ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_ERROR_ACCESS ) {
syslog(LOG_NOTICE,sysc(LogLine));
} else if ( verror == VLOG_AUTH && ( ENABLE_LOGGING == 1 || ENABLE_LOGGING == 4 ) ) {
syslog(LOG_NOTICE,sysc(LogLine));
}
#ifdef ENABLE_MYSQL_LOGGING
/* always log to mysql if mysql logging is enabled and it
* is not internal error
*/
if ( (verror == VLOG_ERROR_PASSWD) && ( ENABLE_LOGGING==1 || ENABLE_LOGGING==2 || ENABLE_LOGGING==3 || ENABLE_LOGGING==4 ) ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_ERROR_INTERNAL ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_ERROR_LOGON ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_ERROR_ACCESS ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
} else if ( verror == VLOG_AUTH && ( ENABLE_LOGGING == 1 || ENABLE_LOGGING == 4 ) ) {
if ( (logmysql(verror, TheUser, TheDomain, ThePass, TheName, IpAddr, LogLine) ) != 0 ) {
syslog(LOG_NOTICE,"vchkpw: can't write MySQL logs");
}
}
#endif
}
위 세파일을 수정하신후 컴파일 하시면 됩니다.
그러면 pop3 사용자가 접속하여.....
메일을 삭제하였을 경우
sprintf(filename, "/pop3사용자가 지운 메일 로그를 저장할 절대경로/%s", TheName[0]); // 추가
이 부분에서 지정한 곳에 사용자아이디 로 지운 파일명이 누적이 됩니다.
위와 같이 해 주신후.
웹에서 사용자 로그인시. 해당 로그를 분석하여 동기화시켜주시면 됩니다..
전 로그인시 + cron으로 매시간마다 체크하도록 하였습니다.
* 위 게시물을 이용하여 생긴 서버 장애 및 데이터 손실은 본인에게 책임이 없음을 알려드립니다. *
* 불안하시면 직접 만들어 쓰시길 권해드립니다. *
.
'My > Job (개인작업물)' 카테고리의 다른 글
현재 운영중인 메일서버의 POP3 흐름도 (0) | 2007.04.28 |
---|---|
현재 운영중인 메일서버의 메일 수신 흐름도. (2) | 2007.04.28 |
php 로 pop3 데몬 만들기 (1) | 2007.04.26 |
윈도우 응용프로그램처럼 메뉴를 만들기 (2) | 2007.03.17 |
Qmail 을 DB 에 저장하기 위한 순서도??? (0) | 2007.02.26 |
'My/Job (개인작업물)'의 다른글
- 현재글qmail vpopmail 사용자를 위한 pop3 동기화