$OpenBSD: patch-pop3_session_c,v 1.1.1.1 2004/11/08 21:57:05 naddy Exp $
--- pop3_session.c.orig	Sun Aug 17 19:44:55 2003
+++ pop3_session.c	Fri Aug 27 01:45:07 2004
@@ -35,6 +35,7 @@
 
 extern const char * ssl_certfile;
 extern const char * ssl_keyfile;
+extern const char * group_name;
 extern const char * local_mbox;
 extern char real_username[MAXLINE+1];
 extern char real_maildrop[MAXLINE+1];
@@ -45,8 +46,17 @@ extern const char * mailspool;
 int authenticate(char * username, char * password);
 void show_uidl(int fd, char * line);
 
-static void do_remove_lock(void) {
+static void do_remove_lock(int fd) {
   do_cleanup();
+  if (remove_lock(mdl)) {
+    syslog(LOG_ERR,"unable to unlink lock file %s : %m",mdl);
+    write_line(fd,"unable to unlink lock file - see syslog\r\n"); 
+    exit(EXIT_FAILURE);
+  }
+}
+
+static void cleanup_before_exit(void) {
+  do_cleanup();
   remove_lock(mdl);
 }
 
@@ -64,8 +74,9 @@ static void print_capa(int fd) {
 
 
 static void sig_handler(int signo) {
-  remove_lock(mdl);
   syslog(LOG_INFO,"%s: %u", "caught signal",signo);
+  if (remove_lock(mdl))
+    syslog(LOG_ERR,"unable to unlink lock file for user %s : %m",mdl);
   exit(EXIT_FAILURE);
 }
 
@@ -227,21 +238,21 @@ void pop3_session(int fd) {
 
   mdl = maildrop;
 
-  g_inf = getgrnam("mail");
+  g_inf = getgrnam(group_name);
   if (g_inf==NULL) {
-    syslog(LOG_ERR,"%s","group 'mail' not found");
-    write_line(fd,"-ERR [SYS/TEMP] group 'mail' not found\r\n");
+    syslog(LOG_ERR,"group '%s' not found",group_name);
+    write_line(fd,"-ERR [SYS/TEMP] group not found\r\n");
     exit(EXIT_FAILURE);
   }
   if (setegid(g_inf->gr_gid)!=0 && real_username[0] == 0) {
     syslog(LOG_ERR,"%s: %u: %s","setegid() failed",g_inf->gr_gid,strerror(errno));
-    write_line(fd,"-ERR [SYS/TEMP] failed to join 'mail' group (setegid)\r\n");
+    write_line(fd,"-ERR [SYS/TEMP] failed to join group (setegid)\r\n");
     exit(EXIT_FAILURE);
   }
 
   if (setgid(g_inf->gr_gid)!=0 && real_username[0] == 0) {
     syslog(LOG_ERR,"%s: %u: %s","setgid() failed",g_inf->gr_gid,strerror(errno));
-    write_line(fd,"-ERR [SYS/TEMP] failed to join 'mail' group (setgid)\r\n");
+    write_line(fd,"-ERR [SYS/TEMP] failed to join group (setgid)\r\n");
     exit(EXIT_FAILURE);
   }
 
@@ -258,18 +269,18 @@ void pop3_session(int fd) {
   if (setuid(u_inf->pw_uid)!=0) {
     syslog(LOG_ERR,"%s: %u: %s","setuid() failed",u_inf->pw_uid,strerror(errno));
     write_line(fd,"-ERR [SYS/TEMP] failed to set user identity\r\n");
-    do_remove_lock();
+    do_remove_lock(fd);
     exit(EXIT_FAILURE);
   }
 
   if (seteuid(u_inf->pw_uid)!=0) {
     syslog(LOG_ERR,"%s: %u: %s","seteuid() failed",u_inf->pw_uid,strerror(errno));
     write_line(fd,"-ERR [SYS/TEMP] failed to set effective user identity\r\n");
-    do_remove_lock();
+    do_remove_lock(fd);
     exit(EXIT_FAILURE);
   }
 
-  if (atexit(do_remove_lock)!=0) {
+  if (atexit(cleanup_before_exit)!=0) {
     syslog(LOG_WARNING,"%s: %s","atexit() failed; lock files may fail to expire",strerror(errno));
   }
 
@@ -278,7 +289,7 @@ void pop3_session(int fd) {
   if (process_mails(maildrop)==0) {
     write_line(fd,"-ERR [SYS/PERM] failed to scan maildrop contents\r\n");
     do_cleanup();
-    do_remove_lock();
+    do_remove_lock(fd);
     exit(EXIT_FAILURE);
   }
 
@@ -334,5 +345,8 @@ void pop3_session(int fd) {
     do_update(maildrop);
   }
   do_cleanup();
-  remove_lock(maildrop);
+  if (remove_lock(maildrop)) {
+    syslog(LOG_ERR,"unable to unlink lock file %s : %m",maildrop);
+    exit(EXIT_FAILURE);
+  }
 }
