atom feed2 messages in net.sourceforge.lists.courier-usersRe: [courier-users] Automatic mailbox...
FromSent OnAttachments
Brian CandlerOct 1, 2002 2:33 pm 
Brian CandlerOct 2, 2002 8:38 am.patch, .patch, .patch, 1 more
Subject:Re: [courier-users] Automatic mailbox migration
From:Brian Candler (B.Ca@pobox.com)
Date:Oct 2, 2002 8:38:28 am
List:net.sourceforge.lists.courier-users
Attachments:
maildirfetch.c.gz - 4k

On Tue, Oct 01, 2002 at 10:27:58PM +0100, Brian Candler wrote:

I've been using Courier-imap/sqwebmail for quite a while. I'm now about to start migrating a number of legacy systems to it. To make my life easier, what I would like to do is to have a facility to automatically transfer mailbox contents from an old mail server using POP3 (initially) or IMAP (later), triggered when a user first logs in to the new system. This should minimise the amount of disk space used on the target machine, since inactive accounts will not be copied, and spread the work of moving mailboxes over time.

I've gone ahead and implemented this, and it turned out to be simpler than I thought.

The attached small patches enable the functionality described before: if an executable file called "loginexec" exists in the maildir when the user logs in, it is executed. If it returns with zero status code, it is deleted. (Just thought: it would probably be a good idea to get an exclusive lock on the file before executing it, but that is easily added)

The loginexec file typically would contain something like this:

#!/bin/sh /usr/local/bin/maildirfetch pop3 oldhost oldusername oldpassword

which connects to the old host and downloads the messages. 'maildirfetch' is a small C program I wrote which is also attached.

I am happy to contribute this code to the Courier project if it's felt to be of use.

Regards,

Brian Candler.

--- sqwebmail-3.3.7/sqwebmail/auth.c.orig Fri Dec 7 12:55:39 2001 +++ sqwebmail-3.3.7/sqwebmail/auth.c Tue Oct 1 18:54:41 2002 @@ -38,6 +38,7 @@ #include "authlib/auth.h" #include "authlib/authmod.h" #include "authlib/authstaticlist.h" +#include "maildir/loginexec.h"

#include "auth.h" #include "htmllibdir.h" @@ -80,6 +81,7 @@ if (!maildir || !*maildir) maildir="Maildir"; if (chdir(maildir)) return (-1); + maildir_loginexec(); return (0); }

diff -uNr --exclude *.html --exclude *.orig
courier-imap-1.5.3.orig/imap/pop3dserver.c courier-imap-1.5.3/imap/pop3dserver.c --- courier-imap-1.5.3.orig/imap/pop3dserver.c Tue Jul 2 21:01:37 2002 +++ courier-imap-1.5.3/imap/pop3dserver.c Tue Oct 1 18:48:00 2002 @@ -54,6 +54,7 @@ #include "maildir/maildirmisc.h" #include "maildir/maildirquota.h" #include "maildir/maildirgetquota.h" +#include "maildir/loginexec.h"

extern void pop3dcapa();

@@ -667,6 +668,8 @@ printf("-ERR Maildir: %s\r\n",strerror(errno)); exit(1); } + + maildir_loginexec();

fprintf(stderr, "INFO: LOGIN, user=%s, ip=[%s]\n", authaddr, --- courier-imap-1.5.3.orig/imap/imapd.c Tue Jul 2 21:01:37 2002 +++ courier-imap-1.5.3/imap/imapd.c Wed Oct 2 12:11:16 2002 @@ -71,6 +71,7 @@ #include "maildir/maildirgetquota.h" #include "maildir/maildirquota.h" #include "maildir/maildirmisc.h" +#include "maildir/loginexec.h"

#include "unicode/unicode.h"

@@ -3015,6 +3016,7 @@ fprintf(stderr, "chdir %s: %s\n", p, strerror(errno)); write_error_exit(p); } + maildir_loginexec();

p=getenv("HOSTNAME"); if (!p)

diff -uNr --exclude *.html --exclude *.orig
courier-imap-1.5.3.orig/maildir/Makefile.am
courier-imap-1.5.3/maildir/Makefile.am --- courier-imap-1.5.3.orig/maildir/Makefile.am Tue Mar 19 01:32:47 2002 +++ courier-imap-1.5.3/maildir/Makefile.am Wed Oct 2 11:57:26 2002 @@ -28,7 +28,8 @@ maildirgetquota.c maildirgetquota.h maildiropen.c maildirparsequota.c \ maildirpath.c maildirpurgetmp.c maildirmisc.h \ maildirshared.c maildirshared2.c maildirdelfolder.c\ - maildirquota.c maildirquota.h maildirrequota.c maildirrequota.h + maildirquota.c maildirquota.h maildirrequota.c maildirrequota.h \ + loginexec.c loginexec.h

noinst_PROGRAMS=deliverquota maildirmake testmaildirfilter

diff -uNr --exclude *.html --exclude *.orig
courier-imap-1.5.3.orig/maildir/Makefile.in
courier-imap-1.5.3/maildir/Makefile.in --- courier-imap-1.5.3.orig/maildir/Makefile.in Thu Aug 8 04:47:53 2002 +++ courier-imap-1.5.3/maildir/Makefile.in Wed Oct 2 11:58:00 2002 @@ -100,7 +100,8 @@ maildirgetquota.c maildirgetquota.h maildiropen.c maildirparsequota.c \ maildirpath.c maildirpurgetmp.c maildirmisc.h \ maildirshared.c maildirshared2.c maildirdelfolder.c\ - maildirquota.c maildirquota.h maildirrequota.c maildirrequota.h + maildirquota.c maildirquota.h maildirrequota.c maildirrequota.h \ + loginexec.c loginexec.h

noinst_PROGRAMS = deliverquota maildirmake testmaildirfilter @@ -147,7 +148,7 @@ maildirfilter2.o maildirflags.o maildirmkdir.o maildirgetquota.o \ maildiropen.o maildirparsequota.o maildirpath.o maildirpurgetmp.o \ maildirshared.o maildirshared2.o maildirdelfolder.o maildirquota.o \ -maildirrequota.o +maildirrequota.o loginexec.o AR = ar PROGRAMS = $(noinst_PROGRAMS)

diff -uNr --exclude *.html --exclude *.orig
courier-imap-1.5.3.orig/maildir/loginexec.c
courier-imap-1.5.3/maildir/loginexec.c --- courier-imap-1.5.3.orig/maildir/loginexec.c Thu Jan 1 01:00:00 1970 +++ courier-imap-1.5.3/maildir/loginexec.c Wed Oct 2 12:06:08 2002 @@ -0,0 +1,42 @@ +#include "loginexec.h" +#include <sys/types.h> +#if HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif +#include <string.h> +#include <stdio.h> +#if HAVE_UNISTD_H +#include <unistd.h> +#endif +#if HAVE_SYS_WAIT_H +#include <sys/wait.h> +#endif + +#define LOGINEXEC_FILE "loginexec" + +void maildir_loginexec(void) +{ +#ifdef LOGINEXEC_FILE +struct stat buf; +pid_t pid; +int waitstat; + + if (stat(LOGINEXEC_FILE, &buf) != 0 || (buf.st_mode & S_IXUSR) == 0) + return; + + while ((pid=fork()) == -1) + { + sleep(5); + } + if (pid == 0) + { + execl("./" LOGINEXEC_FILE, LOGINEXEC_FILE, (char *)0); + perror("Failed to exec " LOGINEXEC_FILE); + exit(1); + } + while (wait(&waitstat) != pid) + ; + if (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0) + unlink(LOGINEXEC_FILE); +#endif +} diff -uNr --exclude *.html --exclude *.orig
courier-imap-1.5.3.orig/maildir/loginexec.h
courier-imap-1.5.3/maildir/loginexec.h --- courier-imap-1.5.3.orig/maildir/loginexec.h Thu Jan 1 01:00:00 1970 +++ courier-imap-1.5.3/maildir/loginexec.h Tue Oct 1 18:46:14 2002 @@ -0,0 +1,26 @@ +#ifndef loginexec_h +#define loginexec_h + +/* +** Copyright 1998 - 1999 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* Check for a 'loginexec' executable in the current directory, if so + run it, then delete it if exit code zero */ + +void maildir_loginexec(void); + +#ifdef __cplusplus +} +#endif + +#endif