pop3d.c
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "pop3d.h"
mailbox_t mbox;
size_t timeout;
int state;
char *username;
FILE *ifile;
FILE *ofile;
char *md5shared;
/* Number of child processes. */
volatile size_t children;
static struct option long_options[] =
{
{"daemon", optional_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{"inetd", no_argument, 0, 'i'},
{"port", required_argument, 0, 'p'},
{"timeout", required_argument, 0, 't'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
const char *short_options ="d::hip:t:v";
static int syslog_error_printer __P ((const char *fmt, va_list ap));
static int pop3d_mainloop __P ((int, int));
static void pop3d_daemon_init __P ((void));
static void pop3d_daemon __P ((unsigned int, unsigned int));
static void pop3d_usage __P ((char *));
#ifndef DEFMAXCHILDREN
# define DEFMAXCHILDREN 10 /* Default maximum number of children */
#endif
int
main (int argc, char **argv)
{
struct group *gr;
static int mode = INTERACTIVE;
size_t maxchildren = DEFMAXCHILDREN;
int c = 0;
int status = OK;
unsigned int port;
port = 110; /* Default POP3 port. */
timeout = 600; /* Default timeout of 600. */
while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
!= -1)
{
switch (c)
{
case 'd':
mode = DAEMON;
if (optarg)
maxchildren = strtoul (optarg, NULL, 10);
if (maxchildren == 0)
maxchildren = DEFMAXCHILDREN;
break;
case 'h':
pop3d_usage (argv[0]);
break;
case 'i':
mode = INTERACTIVE;
break;
case 'p':
mode = DAEMON;
port = strtoul (optarg, NULL, 10);
break;
case 't':
timeout = strtoul (optarg, NULL, 10);
break;
case 'v':
printf (IMPL " ("PACKAGE " " VERSION ")\n");
exit (EXIT_SUCCESS);
break;
default:
break;
}
}
/* First we want our group to be mail so we can access the spool. */
gr = getgrnam ("mail");
if (gr == NULL)
{
perror ("Error getting mail group");
exit (EXIT_FAILURE);
}
if (setgid (gr->gr_gid) == -1)
{
perror ("Error setting mail group");
exit (EXIT_FAILURE);
}
/* Register the desire formats. We only need Mbox mail format. */
{
list_t bookie;
registrar_get_list (&bookie);
/* list_append (bookie, mbox_record); */
list_append (bookie, path_record);
}
/* Set the signal handlers. */
signal (SIGINT, pop3d_signal);
signal (SIGQUIT, pop3d_signal);
signal (SIGILL, pop3d_signal);
signal (SIGBUS, pop3d_signal);
signal (SIGFPE, pop3d_signal);
signal (SIGSEGV, pop3d_signal);
signal (SIGTERM, pop3d_signal);
signal (SIGSTOP, pop3d_signal);
signal (SIGPIPE, pop3d_signal);
signal (SIGABRT, pop3d_signal);
if (mode == DAEMON)
pop3d_daemon_init ();
/* Make sure we are in the root directory. */
chdir ("/");
/* Set up for syslog. */
openlog ("gnu-pop3d", LOG_PID, LOG_FACILITY);
/* Redirect any stdout error from the library to syslog, they
should not go to the client. */
mu_error_set_print (syslog_error_printer);
umask (S_IROTH | S_IWOTH | S_IXOTH); /* 007 */
/* Actually run the daemon. */
if (mode == DAEMON)
pop3d_daemon (maxchildren, port);
/* exit (EXIT_SUCCESS) -- no way out of daemon except a signal. */
else
status = pop3d_mainloop (fileno (stdin), fileno (stdout));
/* Close the syslog connection and exit. */
closelog ();
return (OK != status);
}
/* Sets things up for daemon mode. */
static void
pop3d_daemon_init (void)
{
pid_t pid;
pid = fork ();
if (pid == -1)
{
perror ("fork failed:");
exit (EXIT_FAILURE);
}
else if (pid > 0)
exit (EXIT_SUCCESS); /* Parent exits. */
setsid (); /* Become session leader. */
signal (SIGHUP, SIG_IGN); /* Ignore SIGHUP. */
/* The second fork is to guarantee that the daemon cannot acquire a
controlling terminal. */
pid = fork ();
if (pid == -1)
{
perror("fork failed:");
exit (EXIT_FAILURE);
}
else if (pid > 0)
exit (EXIT_SUCCESS); /* Parent exits. */
/* Close inherited file descriptors. */
{
size_t i;
#if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
size_t fdlimit = sysconf(_SC_OPEN_MAX);
#else
size_t fdlimit = 64;
#endif
for (i = 0; i < fdlimit; ++i)
close (i);
}
/* SIGCHLD is not ignore but rather use to do some simple load balancing. */
#ifdef HAVE_SIGACTION
{
struct sigaction act;
act.sa_handler = pop3d_sigchld;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
sigaction (SIGCHLD, &act, NULL);
}
#else
signal (SIGCHLD, pop3d_sigchld);
#endif
}
/* The main part of the daemon. This function reads input from the client and
executes the proper functions. Also handles the bulk of error reporting. */
static int
pop3d_mainloop (int infile, int outfile)
{
int status = OK;
/* Reset hup to exit. */
signal (SIGHUP, pop3d_signal);
/* Timeout alarm. */
signal (SIGALRM, pop3d_signal);
ifile = fdopen (infile, "r");
ofile = fdopen (outfile, "w");
if (!ifile || !ofile)
pop3d_abquit (ERR_NO_OFILE);
state = AUTHORIZATION;
syslog (LOG_INFO, "Incoming connection opened");
/* log information on the connecting client */
{
struct sockaddr_in cs;
int len = sizeof cs;
if (getpeername (infile, (struct sockaddr*)&cs, &len) < 0)
syslog (LOG_ERR, "can't obtain IP address of client: %s",
strerror (errno));
else
syslog (LOG_INFO, "connect from %s", inet_ntoa(cs.sin_addr));
}
/* Prepare the shared secret for APOP. */
{
char *local_hostname;
local_hostname = malloc (MAXHOSTNAMELEN + 1);
if (local_hostname == NULL)
pop3d_abquit (ERR_NO_MEM);
/* Get our canonical hostname. */
{
struct hostent *htbuf;
gethostname (local_hostname, MAXHOSTNAMELEN);
htbuf = gethostbyname (local_hostname);
if (htbuf)
{
free (local_hostname);
local_hostname = strdup (htbuf->h_name);
}
}
md5shared = malloc (strlen (local_hostname) + 51);
if (md5shared == NULL)
pop3d_abquit (ERR_NO_MEM);
snprintf (md5shared, strlen (local_hostname) + 50, "<%u.%u@%s>", getpid (),
(unsigned)time (NULL), local_hostname);
free (local_hostname);
}
/* Lets boogie. */
fprintf (ofile, "+OK POP3 Ready %s\r\n", md5shared);
while (state != UPDATE)
{
char *buf, *arg, *cmd;
fflush (ofile);
status = OK;
buf = pop3d_readline (ifile);
cmd = pop3d_cmd (buf);
arg = pop3d_args (buf);
/* The mailbox size needs to be check to make sure that we are in
sync. Some other applications may not respect the *.lock or
the lock may be stale because downloading on slow modem.
We rely on the size of the mailbox for the check and bail if out
of sync. */
if (state == TRANSACTION && !mailbox_is_updated (mbox))
{
static off_t mailbox_size;
off_t newsize = 0;
mailbox_get_size (mbox, &newsize);
/* Did we shrink? First time save the size. */
if (!mailbox_size)
mailbox_size = newsize;
else if (newsize < mailbox_size) /* FIXME: Should it be a != ? */
pop3d_abquit (ERR_MBOX_SYNC); /* Out of sync, Bail out. */
}
/* Refresh the Lock. */
pop3d_touchlock ();
if (strlen (arg) > POP_MAXCMDLEN || strlen (cmd) > POP_MAXCMDLEN)
status = ERR_TOO_LONG;
else if (strlen (cmd) > 4)
status = ERR_BAD_CMD;
else if (strncasecmp (cmd, "RETR", 4) == 0)
status = pop3d_retr (arg);
else if (strncasecmp (cmd, "DELE", 4) == 0)
status = pop3d_dele (arg);
else if (strncasecmp (cmd, "USER", 4) == 0)
status = pop3d_user (arg);
else if (strncasecmp (cmd, "QUIT", 4) == 0)
status = pop3d_quit (arg);
else if (strncasecmp (cmd, "APOP", 4) == 0)
status = pop3d_apop (arg);
else if (strncasecmp (cmd, "AUTH", 4) == 0)
status = pop3d_auth (arg);
else if (strncasecmp (cmd, "STAT", 4) == 0)
status = pop3d_stat (arg);
else if (strncasecmp (cmd, "LIST", 4) == 0)
status = pop3d_list (arg);
else if (strncasecmp (cmd, "NOOP", 4) == 0)
status = pop3d_noop (arg);
else if (strncasecmp (cmd, "RSET", 4) == 0)
status = pop3d_rset (arg);
else if ((strncasecmp (cmd, "TOP", 3) == 0) && (strlen (cmd) == 3))
status = pop3d_top (arg);
else if (strncasecmp (cmd, "UIDL", 4) == 0)
status = pop3d_uidl (arg);
else if (strncasecmp (cmd, "CAPA", 4) == 0)
status = pop3d_capa (arg);
else
status = ERR_BAD_CMD;
if (status == OK)
; /* Everything is good. */
else if (status == ERR_WRONG_STATE)
fprintf (ofile, "-ERR " BAD_STATE "\r\n");
else if (status == ERR_BAD_ARGS)
fprintf (ofile, "-ERR " BAD_ARGS "\r\n");
else if (status == ERR_NO_MESG)
fprintf (ofile, "-ERR " NO_MESG "\r\n");
else if (status == ERR_MESG_DELE)
fprintf (ofile, "-ERR " MESG_DELE "\r\n");
else if (status == ERR_NOT_IMPL)
fprintf (ofile, "-ERR " NOT_IMPL "\r\n");
else if (status == ERR_BAD_CMD)
fprintf (ofile, "-ERR " BAD_COMMAND "\r\n");
else if (status == ERR_BAD_LOGIN)
fprintf (ofile, "-ERR " BAD_LOGIN "\r\n");
else if (status == ERR_MBOX_LOCK)
fprintf (ofile, "-ERR [IN-USE] " MBOX_LOCK "\r\n");
else if (status == ERR_TOO_LONG)
fprintf (ofile, "-ERR " TOO_LONG "\r\n");
else if (status == ERR_FILE)
fprintf (ofile, "-ERR " FILE_EXP "\r\n");
else
fprintf (ofile, "-ERR unknown error\r\n");
free (cmd);
free (arg);
}
return (status != OK);
}
/* Runs GNU POP3 in standalone daemon mode. This opens and binds to a port
(default 110) then executes a pop3d_mainloop() upon accepting a connection.
It starts maxchildren child processes to listen to and accept socket
connections. */
static void
pop3d_daemon (unsigned int maxchildren, unsigned int port)
{
struct sockaddr_in server, client;
pid_t pid;
int listenfd, connfd;
size_t size;
listenfd = socket (AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
{
syslog (LOG_ERR, "socket: %s", strerror(errno));
exit (EXIT_FAILURE);
}
size = 1; /* Use size here to avoid making a new variable. */
setsockopt (listenfd, SOL_SOCKET, SO_REUSEADDR, &size, sizeof(size));
size = sizeof (server);
memset (&server, 0, size);
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl (INADDR_ANY);
server.sin_port = htons (port);
if (bind (listenfd, (struct sockaddr *)&server, size) == -1)
{
syslog (LOG_ERR, "bind: %s", strerror (errno));
exit (EXIT_FAILURE);
}
if (listen (listenfd, 128) == -1)
{
syslog (LOG_ERR, "listen: %s", strerror (errno));
exit (EXIT_FAILURE);
}
for (;;)
{
if (children > maxchildren)
{
syslog (LOG_ERR, "too many children (%d)", children);
pause ();
continue;
}
connfd = accept (listenfd, (struct sockaddr *)&client, &size);
if (connfd == -1)
{
if (errno == EINTR)
continue;
syslog (LOG_ERR, "accept: %s", strerror (errno));
exit (EXIT_FAILURE);
}
pid = fork ();
if (pid == -1)
syslog(LOG_ERR, "fork: %s", strerror (errno));
else if (pid == 0) /* Child. */
{
int status;
close (listenfd);
status = pop3d_mainloop (connfd, connfd);
closelog ();
exit (status);
}
else
{
++children;
}
close (connfd);
}
}
/* Prints out usage information and exits the program */
static void
pop3d_usage (char *argv0)
{
printf ("Usage: %s [OPTIONS]\n", argv0);
printf ("Runs the GNU POP3 daemon.\n\n");
printf (" -d, --daemon=MAXCHILDREN runs in daemon mode with a maximum\n");
printf (" of MAXCHILDREN child processes\n");
printf (" -h, --help display this help and exit\n");
printf (" -i, --inetd runs in inetd mode (default)\n");
printf (" -p, --port=PORT specifies port to listen on, implies -d\n"
);
printf (" defaults to 110, which need not be specifi
ed\n");
printf (" -t, --timeout=TIMEOUT sets idle timeout to TIMEOUT seconds\n");
printf (" TIMEOUT default is 600 (10 minutes)\n");
printf (" -v, --version display version information and exit\n");
printf ("\nReport bugs to bug-mailutils@gnu.org\n");
exit (EXIT_SUCCESS);
}
static int
syslog_error_printer (const char *fmt, va_list ap)
{
vsyslog (LOG_CRIT, fmt, ap);
return 0;
}