summaryrefslogtreecommitdiff
path: root/nullpop.c
blob: 3be7da5805475ebb14cb9566205e3208ef3f8dde (plain)
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
/* nullpop: A NOOP POP3 Server. Based on:

   virtualmail-pop3d - a POP3 server with virtual domains support
   This code is licensed under the GPL; it has several authors.
   vm-pop3d is based on:
   GNU POP3 - a small, fast, and efficient POP3 daemon
   Copyright (C) 1999 Jakob 'sparky' Kaivo <jkaivo@nodomainname.net>

   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.  */


#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pwd.h>
#include <grp.h>
#include <syslog.h>
#include <ctype.h>

/* A command that doesn't exist */
#define BAD_COMMAND	"Invalid command"

/* Incorrect number of arguments passed to a command */
#define BAD_ARGS	"Invalid arguments"

/* An action on a message that doesn't exist */
#define NO_MESG		"No such message"

/* A command that is known but not implemented */
#define NOT_IMPL	"Not implemented"

/* The command argument was > 40 characters */
#define TOO_LONG	"Argument too long"

/* Longest legal POP command */
#define POP_MAXCMDLEN	255



/* Maximum length of a hostname (is this defined somewhere else?) */
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN        64
#endif

#define OK		0
#define ERR_BAD_ARGS	2
#define ERR_NO_MESG	4
#define ERR_NOT_IMPL	5
#define ERR_BAD_CMD	6
#define ERR_TOO_LONG	8
#define ERR_NO_MEM	9
#define ERR_DEAD_SOCK	10
#define ERR_SIGNAL	11
#define ERR_FILE        12
#define ERR_NO_OFILE    13
#define ERR_TIMEOUT	14

unsigned int timeout;
int ifile;
FILE *ofile;

/* Prints out usage information and exits the program */
void
pop3_usage(char *argv0)
{
  printf("Usage: %s [OPTIONS]\n", argv0);
  printf("Runs the nullpop POP3 daemon.\n\n");
  printf("  -t      sets idle timeout to TIMEOUT seconds\n");
  exit(2);
}

/* This is called if it needs to quit without going to the UPDATE stage.
   This is used for conditions such as out of memory, a broken socket, or
   being killed on a signal */
int
pop3_abquit(int reason)
{
  switch (reason) {
  case ERR_NO_MEM:
    if (NULL != ofile)
      fprintf(ofile, "-ERR Out of memory, quitting\r\n");
    syslog(LOG_ERR, "Out of memory");
    break;
  case ERR_DEAD_SOCK:
    if (NULL != ofile)  /* should this even print to socket? */
      fprintf(ofile, "-ERR Socket closed, quitting\r\n");
    syslog(LOG_ERR, "Socket closed");
    break;
  case ERR_SIGNAL:
    if (NULL != ofile)
      fprintf(ofile, "-ERR Quitting on signal\r\n");
    break;
  case ERR_TIMEOUT:
    if (NULL != ofile)
      fprintf(ofile, "-ERR Session timed out\r\n");
    else
      syslog(LOG_INFO, "Session timed out for no user");
    break;
  case ERR_NO_OFILE:
    syslog(LOG_ERR, "Quitting - couldn't open stream");
    break;
  default:
    if (NULL != ofile)
      fprintf(ofile, "-ERR Quitting (reason unknown)\r\n");
    syslog(LOG_ERR, "Unknown quit");
    break;
  }
  fflush(ofile);
  exit(1);
}

/* Default signal handler to call the pop3_abquit() function */
void
pop3_signal(int signal)
{
  syslog(LOG_ERR, "Quitting on signal: %d", signal);
  pop3_abquit(ERR_SIGNAL);
}


/* Gets a line of input from the client */
char *
pop3_readline(int fd)
{
  fd_set rfds;
  struct timeval tv;
  char buf[1024], *ret = NULL;
  int available;

  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  tv.tv_sec = timeout;
  tv.tv_usec = 0;
  memset(buf, '\0', 1024);

  while (strchr(buf, '\n') == NULL) {
    if (timeout > 0) {
      available = select(fd + 1, &rfds, NULL, NULL, &tv);
      if (!available)
	pop3_abquit(ERR_TIMEOUT);
    }
    if (read(fd, buf, 1024) < 1)
      pop3_abquit(ERR_DEAD_SOCK);

    if (ret == NULL) {
      ret = malloc((strlen(buf) + 1) * sizeof(char));
      strcpy(ret, buf);
    } else {
      ret = realloc(ret, (strlen(ret) + strlen(buf) + 1) * sizeof(char));
      strcat(ret, buf);
    }
  }
  return ret;
}

/* Takes a string as input and returns either the remainder of the string
   after the first space, or a zero length string if no space */
char *
pop3_args(const char *cmd)
{
  int space = -1, i = 0, len;
  char *buf;

  len = strlen(cmd) + 1;
  buf = malloc(len * sizeof(char));
  if (buf == NULL)
    pop3_abquit(ERR_NO_MEM);

  while (space < 0 && i < len) {
    if (cmd[i] == ' ')
      space = i + 1;
    else if (cmd[i] == '\0' || cmd[i] == '\r' || cmd[i] == '\n')
      len = i;
    i++;
  }

  if (space < 0)
    buf[0] = '\0';
  else {
    for (i = space; i < len; i++)
      if (cmd[i] == '\0' || cmd[i] == '\r' || cmd[i] == '\n')
	buf[i - space] = '\0';
      else
	buf[i - space] = cmd[i];
  }

  return buf;
}

/* This takes a string and returns the string up to the first space or end of
   the string, whichever occurs first */
char *
pop3_cmd(const char *cmd)
{
  char *buf;
  int i = 0, len;

  len = strlen(cmd) + 1;
  buf = malloc(len * sizeof(char));
  if (buf == NULL)
    pop3_abquit(ERR_NO_MEM);

  for (i = 0; i < len; i++) {
    if (cmd[i] == ' ' || cmd[i] == '\0' || cmd[i] == '\r' || cmd[i] == '\n')
      len = i;
    else
      buf[i] = cmd[i];
  }
  buf[i - 1] = '\0';
  return buf;
}

/* 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. */
int
pop3_mainloop(int infile, int outfile)
{
  int status = OK;
  char *buf, *arg, *cmd;
  struct sockaddr_in name;
  int namelen = sizeof(name);
  char *temp_domain;

  ifile = infile;
  ofile = fdopen(outfile, "w+");
  if (ofile == NULL)
    pop3_abquit(ERR_NO_OFILE);

  if (getpeername(infile,
        (struct sockaddr *) & name, (socklen_t *) & namelen) == 0) {
    if ((temp_domain = (char *) inet_ntoa(name.sin_addr)))
      syslog(LOG_INFO,"Connect from %s", temp_domain);
    else
      syslog(LOG_INFO, "Connection opened");
  }
  else
    syslog(LOG_INFO, "Incoming connection opened");

  fflush(ofile);
  fprintf(ofile, "+OK POP3 \r\n");

  for (;;) {
    fflush(ofile);
    status = OK;
    buf = pop3_readline(ifile);
    cmd = pop3_cmd(buf);
    arg = pop3_args(buf);

    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, "USER", 4) == 0) {
      fprintf(ofile, "+OK\r\n");
      status = OK;
    } else if (strncasecmp(cmd, "PASS", 4) == 0) {
      fprintf(ofile, "+OK\r\n");
      status = OK;
    } else if (strncasecmp(cmd, "QUIT", 4) == 0) {
      fprintf(ofile, "+OK\r\n");
      fflush(ofile);
      break;
    } else if (strncasecmp(cmd, "AUTH", 4) == 0) {
      status = ERR_NOT_IMPL;
    } else if (strncasecmp(cmd, "STAT", 4) == 0) {
      fprintf(ofile, "+OK %d %d\r\n", 0, 0);
      status = OK;
    } else if (strncasecmp(cmd, "LIST", 4) == 0) {
      if (strchr(arg, ' ') != NULL) {
        status = ERR_BAD_ARGS;
      } else if (strlen(arg) == 0) {
        fprintf(ofile, "+OK\r\n");
        fprintf(ofile, ".\r\n");
        status = OK;
      } else {
        status = ERR_NO_MESG;
      }
    } else if (strncasecmp(cmd, "RETR", 4) == 0) {
      if ((strlen(arg) == 0) || (strchr(arg, ' ') != NULL))
        status = ERR_BAD_ARGS;
      else
        status = ERR_NO_MESG;
    } else if (strncasecmp(cmd, "DELE", 4) == 0) {
      if ((strlen(arg) == 0) || (strchr(arg, ' ') != NULL))
        status = ERR_BAD_ARGS;
      else
        status = ERR_NO_MESG;
    } else if (strncasecmp(cmd, "NOOP", 4) == 0) {
      if (strlen(arg) != 0)
        status = ERR_BAD_ARGS;
      else {
        fprintf(ofile, "+OK\r\n");
        status = OK;
      }
    } else if (strncasecmp(cmd, "RSET", 4) == 0) {
      if (strlen(arg) != 0)
        status = ERR_BAD_ARGS;
      else {
        fprintf(ofile, "+OK\r\n");
        status = OK;
      }
    } else if ((strncasecmp(cmd, "TOP", 3) == 0) && (strlen(cmd) == 3)) {
      status = ERR_NO_MESG;
    } else if (strncasecmp(cmd, "UIDL", 4) == 0) {
      if (strchr(arg, ' ') != NULL)
        status = ERR_BAD_ARGS;
      else
        status = ERR_NO_MESG;
    } else if (strncasecmp(cmd, "CAPA", 4) == 0) {
      if (strlen(arg) != 0)
        status = ERR_BAD_ARGS;
      else {
        fprintf(ofile, "+OK Capability list follows\r\n");
        fprintf(ofile, "TOP\r\n");
        fprintf(ofile, "USER\r\n");
        fprintf(ofile, "RESP-CODES\r\n");
        fprintf(ofile, "UIDL\r\n");
        fprintf(ofile, ".\r\n");
        status = OK;
      }
    } else {
      status = ERR_BAD_CMD;
    }

    if (status == OK)
      fflush(ofile);
    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_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_TOO_LONG)
      fprintf(ofile, "-ERR " TOO_LONG "\r\n");

    free(buf);
    free(cmd);
    free(arg);
  }

  fflush(ofile);
  return OK;
}

int
main(int argc, char **argv)
{
  int c = 0;
  timeout = 0;			/* Timeout turned off */

  /* Set the signal handlers */
  signal(SIGINT, pop3_signal);
  signal(SIGQUIT, pop3_signal);
  signal(SIGTERM, pop3_signal);

/* Don't die when a process goes away unexpectedly.
   Ignore write on a pipe with no reader.  */
  signal(SIGPIPE, SIG_IGN);

  while ((c = getopt(argc, argv, "t:") && c)) {
    switch (c) {
    case 't':
      timeout = atoi(optarg);
      break;
    default:
      pop3_usage(argv[0]);
      break;
    }
  }

  /* Set up for syslog */
  openlog("nullpop", LOG_PID, LOG_MAIL);

  pop3_mainloop(fileno(stdin), fileno(stdout));

  /* Close the syslog connection and exit */
  closelog();
  return OK;
}