patch-main_c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. $OpenBSD: patch-main_c,v 1.2 2005/12/14 06:02:09 jolan Exp $
  2. --- main.c.orig Sun Aug 3 00:54:56 2003
  3. +++ main.c Mon Nov 28 11:25:21 2005
  4. @@ -23,26 +23,34 @@
  5. #include "ssl.h"
  6. #include "strlcpy.h"
  7. +#ifndef DEFAULT_GROUP_NAME
  8. +# define DEFAULT_GROUP_NAME "_akpop3d"
  9. +#endif
  10. +
  11. static char * port = NULL;
  12. static int become_daemon = 0;
  13. const char * pidfile = "/var/run/akpop3d.pid";
  14. -const char * ssl_certfile = "/etc/akpop3d/cert.pem";
  15. -const char * ssl_keyfile = "/etc/akpop3d/key.pem";
  16. +const char * ssl_certfile = SYSCONFDIR "/akpop3d/cert.pem";
  17. +const char * ssl_keyfile = SYSCONFDIR "/akpop3d/key.pem";
  18. +const char * group_name = DEFAULT_GROUP_NAME;
  19. const char * authfile = NULL;
  20. const char * local_mbox = NULL;
  21. const char * tmp_dir = "/tmp";
  22. char * mailspool = "/var/mail/";
  23. int use_pop3_allow_deny = 0;
  24. int enable_mysql = 0;
  25. -unsigned int timeout = 30;
  26. +unsigned int timeout = 600;
  27. +extern int *socks, maxs;
  28. +extern fd_set deffds;
  29. -
  30. +#ifdef HAVE_LIBMYSQLCLIENT
  31. /* mysql pointers */
  32. extern char *HOSTNAME;
  33. extern char *USERNAME;
  34. extern char *PASSWORD;
  35. extern char *DATABASE;
  36. extern char *TABLE;
  37. +#endif /* HAVE_LIBMYSQLCLIENT */
  38. int daemonize(void);
  39. void pop3_session(int fd);
  40. @@ -68,14 +76,15 @@ static void usage(char * argv0) {
  41. " -s use SSL for all connections\n"
  42. " -c <certfile> use <certfile> for SSL certificate [%s]\n"
  43. " -k <keyfile> use <keyfile> for SSL RSA key [%s]\n"
  44. - " -p <port> listen on <port> [default: 110, or 995 if SSL]\n",
  45. - ssl_certfile, ssl_keyfile
  46. + " -p <port> listen on <port> [default: 110, or 995 if SSL]\n"
  47. + " -g <group> use GID <group> to access spool directory [default: %s]\n",
  48. + ssl_certfile, ssl_keyfile, group_name
  49. );
  50. printf(
  51. " -l <address> listen on <address> [default: any]\n"
  52. " -a <authfile> use text file <authfile> for authentication\n"
  53. " -m <spooldir> use directory <spooldir> as mail spool\n"
  54. - " -D use /etc/pop3.{allow,deny} files\n"
  55. + " -D use " SYSCONFDIR "/pop3.{allow,deny} files\n"
  56. " -L <mbox> use ~/<mbox> as mail spool\n"
  57. " -t <timeout> use <timeout> seconds as r/w timeout\n"
  58. #ifdef HAVE_LIBMYSQLCLIENT
  59. @@ -107,21 +116,25 @@ static void delete_pid(void) {
  60. }
  61. int main(int argc, char * argv[]) {
  62. - int listenfd, connfd;
  63. + int connfd, maxfd;
  64. + int *socks;
  65. + fd_set fds;
  66. pid_t childpid;
  67. struct stat sbuf;
  68. - socklen_t addrlen;
  69. #ifdef HAVE_LIBMYSQLCLIENT
  70. - const char * optstring = "df:sp:c:k:l:a:m:hvDL:MH:U:P:I:T:t:x:";
  71. + const char * optstring = "df:sp:c:k:l:a:m:hvDL:MH:U:P:I:T:t:x:g:";
  72. int len = 0;
  73. #else
  74. - const char * optstring = "df:sp:c:k:l:a:m:hvDL:t:x:";
  75. + const char * optstring = "df:sp:c:k:l:a:m:hvDL:t:x:g:";
  76. #endif
  77. char * listenhost = NULL;
  78. char * progname;
  79. - int c;
  80. + int c,i;
  81. + struct sockaddr_storage client;
  82. socklen_t clen;
  83. - struct sockaddr_in client;
  84. + uint16_t sin_port_number;
  85. + const char *sin_addr_text;
  86. + char sin_addr_text_buffer[64];
  87. #ifdef HAVE_LIBMYSQLCLIENT
  88. struct rlimit memlim, cpulim;
  89. @@ -159,6 +172,7 @@ int main(int argc, char * argv[]) {
  90. case 'k': ssl_keyfile = optarg; break;
  91. case 'l': listenhost = optarg; break;
  92. case 'a': authfile = optarg; break;
  93. + case 'g': group_name = optarg; break;
  94. case 't': sscanf(optarg,"%u",&timeout); break;
  95. case 'x': tmp_dir = optarg; break;
  96. case 'm': if (stat(optarg,&sbuf)!=0) {
  97. @@ -253,8 +267,13 @@ int main(int argc, char * argv[]) {
  98. }
  99. openlog(progname, LOG_PID, LOG_MAIL);
  100. - listenfd = Tcp_listen(listenhost, port, &addrlen);
  101. -
  102. + socks = Tcp_listen(listenhost, port);
  103. + for (maxfd = -1, i=0; i<maxs; i++) {
  104. + if (maxfd < socks[i])
  105. + maxfd = socks[i];
  106. + }
  107. + clen = sizeof(client);
  108. +
  109. Signal(SIGCHLD,sig_chld);
  110. Signal(SIGTERM,sig_term);
  111. @@ -263,9 +282,21 @@ int main(int argc, char * argv[]) {
  112. Signal(SIGQUIT,sig_term);
  113. for (;;) {
  114. - clen = sizeof(struct sockaddr *);
  115. -
  116. - if ((connfd = accept(listenfd,(struct sockaddr *) &client,&clen)) < 0) {
  117. +#ifdef FD_COPY
  118. + FD_COPY(&deffds, &fds);
  119. +#else
  120. + fds = deffds;
  121. +#endif
  122. + if (select(maxfd+1, &fds, NULL, NULL, NULL) != 1)
  123. + continue;
  124. + connfd = -1;
  125. + for (i=0; i<maxs; i++) {
  126. + if (FD_ISSET(socks[i], &fds)) {
  127. + connfd = accept(socks[i],(struct sockaddr *) &client,&clen);
  128. + break;
  129. + }
  130. + }
  131. + if (connfd < 0) {
  132. if (errno == EINTR) {
  133. continue;
  134. } else {
  135. @@ -275,11 +306,20 @@ int main(int argc, char * argv[]) {
  136. }
  137. getpeername( connfd, (struct sockaddr *) &client, &clen );
  138. + if (client.ss_family==AF_INET6)
  139. + {
  140. + sin_addr_text=inet_ntop(((struct sockaddr_in6 *)&client)->sin6_family,&(((struct sockaddr_in6 *)&client)->sin6_addr),sin_addr_text_buffer,64);
  141. + sin_port_number=ntohs(((struct sockaddr_in6 *)&client)->sin6_port);
  142. + }
  143. + else
  144. + {
  145. + sin_addr_text=inet_ntoa(((struct sockaddr_in *)&client)->sin_addr); // IPv4
  146. + sin_port_number=ntohs(((struct sockaddr_in *)&client)->sin_port);
  147. + }
  148. + syslog( LOG_INFO, "Connection from %s:%u",sin_addr_text,sin_port_number);
  149. - syslog( LOG_INFO, "Connection from %s:%u", inet_ntoa( client.sin_addr ), ntohs( client.sin_port ) );
  150. -
  151. if ((childpid = fork()) == 0) {
  152. - close(listenfd);
  153. +/* close(listenfd); */
  154. pop3_session(connfd);
  155. syslog( LOG_INFO, "Connection closed" );
  156. exit(0);