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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
|
/*
Copyright (C) 1999-2004 IC & S dbmail@ic-s.nl
Copyright (c) 2005-2006 NFG Net Facilities Group BV support@nfg.nl
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 of the License, 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.
*/
/*
*
* implementation for pop3 commands according to RFC 1081 */
#include "dbmail.h"
#define THIS_MODULE "pop3"
#define INCOMING_BUFFER_SIZE 512
#define APOP_STAMP_SIZE 255
#define MAX_USERID_SIZE 100
/* default timeout for server daemon */
#define DEFAULT_SERVER_TIMEOUT 300
/* max_errors defines the maximum number of allowed failures */
#define MAX_ERRORS 3
/* max_in_buffer defines the maximum number of bytes that are allowed to be
* in the incoming buffer */
#define MAX_IN_BUFFER 255
extern int pop_before_smtp;
extern volatile sig_atomic_t alarm_occured;
int pop3_error(PopSession_t * session, void *stream,
const char *formatstring, ...) PRINTF_ARGS(3, 4);
/* allowed pop3 commands */
const char *commands[] = {
"quit", /**< POP3_QUIT */
"user", /**< POP3_USER */
"pass", /**< POP3_PASS */
"stat", /**< POP3_STAT */
"list", /**< POP3_LIST */
"retr", /**< POP3_RETR */
"dele", /**< POP3_DELE */
"noop", /**< POP3_NOOP */
"last", /**< POP3_LAST */
"rset", /**< POP3_RSET */
"uidl", /**< POP3_UIDL */
"apop", /**< POP3_APOP */
"auth", /**< POP3_AUTH */
"top", /**< POP3_TOP */
"capa" /**< POP3_CAPA */
};
int pop3_handle_connection(clientinfo_t * ci)
{
/*
Handles connection and calls
pop command handler
*/
int done = 1; /* loop state */
char *buffer = NULL; /* connection buffer */
char myhostname[64];
int cnt; /* counter */
char unique_id[UID_SIZE];
PopSession_t session; /* current connection session */
/* setting Session variables */
memset(&session,0,sizeof(session));
/* clear the message list */
dm_list_init(&session.messagelst);
/* getting hostname */
gethostname(myhostname, 64);
myhostname[63] = '\0'; /* make sure string is terminated */
/* create an unique timestamp + processid for APOP authentication */
session.apop_stamp = g_new0(char,APOP_STAMP_SIZE);
create_unique_id(unique_id, 0, 0);
snprintf(session.apop_stamp, APOP_STAMP_SIZE, "<%s@%s>", unique_id, myhostname);
if (ci->tx) {
/* sending greeting */
field_t banner;
GETCONFIGVALUE("banner", "POP", banner);
if (strlen(banner) > 0) {
ci_write(ci->tx, "+OK %s %s\r\n",
banner, session.apop_stamp);
} else {
ci_write(ci->tx, "+OK DBMAIL pop3 server ready to rock %s\r\n",
session.apop_stamp);
}
fflush(ci->tx);
} else {
TRACE(TRACE_MESSAGE, "TX stream is null!");
g_free(session.apop_stamp);
return 0;
}
/* set authorization state */
session.state = POP3_AUTHORIZATION_STATE;
/* setup the read buffer */
buffer = g_new0(char,INCOMING_BUFFER_SIZE);
/* lets start handling commands */
while (done > 0) {
if (db_check_connection())
break;
/* set the timeout counter */
if (session.state == POP3_AUTHORIZATION_STATE)
alarm(ci->login_timeout);
else
alarm(ci->timeout);
/* clear the buffer */
memset(buffer, 0, INCOMING_BUFFER_SIZE);
for (cnt = 0; cnt < INCOMING_BUFFER_SIZE - 1; cnt++) {
do {
clearerr(ci->rx);
fread(&buffer[cnt], 1, 1, ci->rx);
if (alarm_occured) {
alarm_occured = 0;
done = -2;
session.SessionResult = 1;
break;
}
} while (ferror(ci->rx) && errno == EINTR);
if (buffer[cnt] == '\n' || feof(ci->rx) || ferror(ci->rx)) {
buffer[cnt + 1] = '\0';
break;
}
}
if (done < 0) {
break;
} else if (feof(ci->rx) || ferror(ci->rx))
done = -1;
else {
/* reset function handle timeout */
alarm(0);
/* handle pop3 commands */
done = pop3(ci, buffer, &session);
}
fflush(ci->tx);
}
g_free(buffer);
buffer = NULL;
/* We're done with this client. The rest is cleanup */
session.state = POP3_UPDATE_STATE;
/* memory cleanup */
g_free(session.apop_stamp);
session.apop_stamp = NULL;
if (session.username != NULL && (session.was_apop || session.password != NULL)) {
switch (session.SessionResult) {
case 0:
TRACE(TRACE_MESSAGE, "user %s logging out [messages=%" U64_T_FORMAT ", octets=%" U64_T_FORMAT "]",
session.username,
session.virtual_totalmessages,
session.virtual_totalsize);
/* if everything went well, write down everything and do a cleanup */
if (db_update_pop(&session) == DM_SUCCESS)
ci_write(ci->tx, "+OK see ya later\r\n");
else
ci_write(ci->tx, "-ERR some deleted messages not removed\r\n");
fflush(ci->tx);
break;
case 1:
ci_write(ci->tx, "-ERR I'm leaving, you're too slow\r\n");
TRACE(TRACE_ERROR, "client timed out, connection closed");
break;
case 2:
TRACE(TRACE_ERROR, "alert! possible flood attempt, closing connection");
break;
case 3:
TRACE(TRACE_ERROR, "authorization layer failure");
break;
case 4:
TRACE(TRACE_ERROR, "storage layer failure");
break;
}
} else if (done==0) { // QUIT issued before AUTH phase completed
ci_write(ci->tx, "+OK see ya later\r\n");
fflush(ci->tx);
} else if (done==-2) {
ci_write(ci->tx, "-ERR I'm leaving, you're too slow\r\n");
TRACE(TRACE_ERROR, "client timed out before AUTH, connection closed");
} else {
TRACE(TRACE_ERROR, "error, incomplete session");
}
/* remove session info like messagelist etc. */
db_session_cleanup(&session);
/* username cleanup */
if (session.username != NULL) {
g_free(session.username);
session.username = NULL;
}
/* password cleanup */
if (session.password != NULL) {
g_free(session.password);
session.password = NULL;
}
/* reset timers */
alarm(0);
return 0;
}
int pop3_error(PopSession_t * session, void *stream,
const char *formatstring, ...)
{
va_list argp;
if (session->error_count >= MAX_ERRORS) {
TRACE(TRACE_MESSAGE, "too many errors (MAX_ERRORS is %d)",
MAX_ERRORS);
ci_write((FILE *) stream, "-ERR loser, go play somewhere else\r\n");
session->SessionResult = 2; /* possible flood */
return -3;
} else {
va_start(argp, formatstring);
vfprintf((FILE *) stream, formatstring, argp);
va_end(argp);
}
TRACE(TRACE_DEBUG, "an invalid command was issued");
session->error_count++;
return 1;
}
int pop3(clientinfo_t *ci, char *buffer, PopSession_t * session)
{
/* returns a 0 on a quit
* -1 on a failure
* 1 on a success
*/
char *command, *value;
Pop3Cmd_t cmdtype;
int found = 0;
int indx = 0;
u64_t result;
int validate_result;
u64_t top_lines, top_messageid;
struct element *tmpelement;
unsigned char *md5_apop_he;
char *searchptr;
u64_t user_idnr;
struct message *msg;
char *client_ip = ci->src_ip;
FILE *stream = ci->tx;
/* buffer overflow attempt */
if (strlen(buffer) > MAX_IN_BUFFER) {
TRACE(TRACE_DEBUG, "buffer overflow attempt");
return -3;
}
/* check for command issued */
while (strchr(ValidNetworkChars, buffer[indx]))
indx++;
/* end buffer */
buffer[indx] = '\0';
TRACE(TRACE_DEBUG, "incoming buffer: [%s]",
buffer);
command = buffer;
value = strstr(command, " "); /* look for the separator */
if (value != NULL) {
*value = '\0'; /* set a \0 on the command end */
value++; /* skip space */
if (strlen(value) == 0)
value = NULL; /* no value specified */
else {
TRACE(TRACE_DEBUG, "command issued :cmd [%s], value [%s]\n",
command, value);
}
}
/* find command that was issued */
for (cmdtype = POP3_QUIT; cmdtype <= POP3_CAPA; cmdtype++)
if (strcasecmp(command, commands[cmdtype]) == 0) {
session->was_apop = 1;
break;
}
TRACE(TRACE_DEBUG, "command looked up as commandtype %d",
cmdtype);
/* commands that are allowed to have no arguments */
if (value == NULL) {
switch (cmdtype) {
case POP3_QUIT:
case POP3_LIST:
case POP3_STAT:
case POP3_RSET:
case POP3_NOOP:
case POP3_LAST:
case POP3_UIDL:
case POP3_AUTH:
case POP3_CAPA:
break;
default:
return pop3_error(session, stream,
"-ERR your command does not compute\r\n");
break;
}
}
switch (cmdtype) {
case POP3_QUIT:
/* We return 0 here, and then pop3_handle_connection cleans up
* the connection, commits all changes, and sends the final
* "OK" message indicating that QUIT has completed. */
return 0;
case POP3_USER:
if (session->state != POP3_AUTHORIZATION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
if (session->username != NULL) {
/* reset username */
g_free(session->username);
session->username = NULL;
}
if (session->username == NULL) {
if (strlen(value) > MAX_USERID_SIZE)
return pop3_error(session, stream, "-ERR userid is too long\r\n");
/* create memspace for username */
session->username = g_new0(char,strlen(value) + 1);
strncpy(session->username, value, strlen(value) + 1);
}
ci_write((FILE *) stream, "+OK Password required for %s\r\n", session->username);
return 1;
case POP3_PASS:
if (session->state != POP3_AUTHORIZATION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
if (session->password != NULL) {
g_free(session->password);
session->password = NULL;
}
if (session->password == NULL) {
/* create memspace for password */
session->password = g_new0(char,strlen(value) + 1);
strncpy(session->password, value, strlen(value) + 1);
}
/* check in authorization layer if these credentials are correct */
validate_result = auth_validate(ci, session->username, session->password, &result);
switch (validate_result) {
case -1:
session->SessionResult = 3;
return -1;
case 0:
TRACE(TRACE_ERROR, "user [%s] coming from [%s] tried to login with wrong password",
session->username, ci->src_ip);
g_free(session->username);
session->username = NULL;
g_free(session->password);
session->password = NULL;
return pop3_error(session, stream, "-ERR username/password incorrect\r\n");
default:
/* user logged in OK */
session->state = POP3_TRANSACTION_STATE;
/* now we're going to build up a session for this user */
TRACE(TRACE_DEBUG, "validation OK, building a session for user [%s]",
session->username);
/* if pop_before_smtp is active, log this ip */
if (pop_before_smtp)
db_log_ip(client_ip);
child_reg_connected_user(session->username);
result = db_createsession(result, session);
if (result == 1) {
ci_write((FILE *) stream, "+OK %s has %" U64_T_FORMAT " messages (%" U64_T_FORMAT " octets)\r\n",
session->username,
session->virtual_totalmessages,
session->virtual_totalsize);
TRACE(TRACE_MESSAGE, "user %s logged in [messages=%" U64_T_FORMAT ", octets=%" U64_T_FORMAT "]",
session->username,
session->virtual_totalmessages,
session->virtual_totalsize);
} else
session->SessionResult = 4; /* Database error. */
return result;
}
return 1;
case POP3_LIST:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
tmpelement = dm_list_getstart(&session->messagelst);
if (value != NULL) {
/* they're asking for a specific message */
while (tmpelement != NULL) {
msg = (struct message *)tmpelement->data;
if (msg->messageid == strtoull(value,NULL, 10) && msg->virtual_messagestatus < MESSAGE_STATUS_DELETE) {
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT " %" U64_T_FORMAT "\r\n", msg->messageid,msg->msize);
found = 1;
}
tmpelement = tmpelement->nextnode;
}
if (!found)
return pop3_error(session, stream, "-ERR [%s] no such message\r\n", value);
else
return 1;
}
/* just drop the list */
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT " messages (%" U64_T_FORMAT " octets)\r\n",
session->virtual_totalmessages,
session->virtual_totalsize);
if (session->virtual_totalmessages > 0) {
/* traversing list */
while (tmpelement != NULL) {
msg = (struct message *)tmpelement->data;
if (msg->virtual_messagestatus < MESSAGE_STATUS_DELETE)
ci_write((FILE *) stream, "%" U64_T_FORMAT " %" U64_T_FORMAT "\r\n", msg->messageid,msg->msize);
tmpelement = tmpelement->nextnode;
}
}
ci_write((FILE *) stream, ".\r\n");
return 1;
case POP3_STAT:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT " %" U64_T_FORMAT "\r\n",
session->virtual_totalmessages,
session->virtual_totalsize);
return 1;
case POP3_RETR:
TRACE(TRACE_DEBUG, "RETR command, retrieving message");
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
tmpelement = dm_list_getstart(&(session->messagelst));
/* selecting a message */
TRACE(TRACE_DEBUG, "RETR command, selecting message");
while (tmpelement != NULL) {
msg = (struct message *) tmpelement->data;
if (msg->messageid == strtoull(value, NULL, 10) && msg->virtual_messagestatus < MESSAGE_STATUS_DELETE) { /* message is not deleted */
msg->virtual_messagestatus = MESSAGE_STATUS_SEEN;
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT " octets\r\n", msg->msize);
return db_send_message_lines((void *) stream, session->mailbox_idnr, msg->realmessageid, -2, 0);
}
tmpelement = tmpelement->nextnode;
}
return pop3_error(session, stream, "-ERR [%s] no such message\r\n", value);
case POP3_DELE:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
tmpelement = dm_list_getstart(&(session->messagelst));
/* selecting a message */
while (tmpelement != NULL) {
msg = (struct message *) tmpelement->data;
if (msg->messageid == strtoull(value, NULL, 10) && msg->virtual_messagestatus < MESSAGE_STATUS_DELETE) { /* message is not deleted */
msg->virtual_messagestatus = MESSAGE_STATUS_DELETE;
session->virtual_totalsize -= msg->msize;
session->virtual_totalmessages -= 1;
ci_write((FILE *) stream, "+OK message %" U64_T_FORMAT " deleted\r\n", msg->messageid);
return 1;
}
tmpelement = tmpelement->nextnode;
}
return pop3_error(session, stream, "-ERR [%s] no such message\r\n", value);
case POP3_RSET:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
tmpelement = dm_list_getstart(&(session->messagelst));
session->virtual_totalsize = session->totalsize;
session->virtual_totalmessages = session->totalmessages;
while (tmpelement != NULL) {
msg = (struct message *) tmpelement->data;
msg->virtual_messagestatus = msg->messagestatus;
tmpelement = tmpelement->nextnode;
}
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT " messages (%" U64_T_FORMAT " octets)\r\n", session->virtual_totalmessages, session->virtual_totalsize);
return 1;
case POP3_LAST:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
tmpelement = dm_list_getstart(&(session->messagelst));
while (tmpelement != NULL) {
msg = (struct message *) tmpelement->data;
if (msg->virtual_messagestatus == MESSAGE_STATUS_NEW) {
/* we need the last message that has been accessed */
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT "\r\n", msg->messageid - 1);
return 1;
}
tmpelement = tmpelement->nextnode;
}
/* all old messages */
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT "\r\n", session->virtual_totalmessages);
return 1;
case POP3_NOOP:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
ci_write((FILE *) stream, "+OK\r\n");
return 1;
case POP3_UIDL:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
tmpelement = dm_list_getstart(&(session->messagelst));
if (value != NULL) {
/* they're asking for a specific message */
while (tmpelement != NULL) {
msg = (struct message *)tmpelement->data;
if (msg->messageid == strtoull(value,NULL, 10) && msg->virtual_messagestatus < MESSAGE_STATUS_DELETE) {
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT " %s\r\n", msg->messageid,msg->uidl);
found = 1;
}
tmpelement = tmpelement->nextnode;
}
if (!found)
return pop3_error(session, stream, "-ERR [%s] no such message\r\n", value);
else
return 1;
}
/* just drop the list */
ci_write((FILE *) stream, "+OK Some very unique numbers for you\r\n");
if (session->virtual_totalmessages > 0) {
/* traversing list */
while (tmpelement != NULL) {
msg = (struct message *)tmpelement->data;
if (msg->virtual_messagestatus < MESSAGE_STATUS_DELETE)
ci_write((FILE *) stream, "%" U64_T_FORMAT " %s\r\n", msg->messageid, msg->uidl);
tmpelement = tmpelement->nextnode;
}
}
ci_write((FILE *) stream, ".\r\n");
return 1;
case POP3_APOP:
if (session->state != POP3_AUTHORIZATION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
/* find out where the md5 hash starts */
searchptr = strstr(value, " ");
if (searchptr == NULL)
return pop3_error(session, stream, "-ERR your command does not compute\r\n");
/* skip the space */
searchptr = searchptr + 1;
/* value should now be the username */
value[searchptr - value - 1] = '\0';
if (strlen(searchptr) != 32)
return pop3_error(session, stream, "-ERR not a valid md5 hash\r\n");
if (strlen(value) > MAX_USERID_SIZE)
return pop3_error(session, stream, "-ERR userid is too long\r\n");
md5_apop_he = g_new0(unsigned char,strlen(searchptr) + 1);
session->username = g_new0(char,strlen(value) + 1);
strncpy((char *)md5_apop_he, searchptr, strlen(searchptr) + 1);
strncpy(session->username, value, strlen(value) + 1);
/*
* check the encryption used for this user
* note that if the user does not exist it is not noted
* by db_getencryption()
*/
if (auth_user_exists(session->username, &user_idnr) == -1) {
TRACE(TRACE_ERROR, "error finding if user exists. username = [%s]",
session->username);
return -1;
}
if (strcasecmp(auth_getencryption(user_idnr), "") != 0) {
/* it should be clear text */
g_free(md5_apop_he);
g_free(session->username);
session->username = NULL;
md5_apop_he = NULL;
return pop3_error(session, stream, "-ERR APOP command is not supported for this user\r\n");
}
TRACE(TRACE_DEBUG, "APOP auth, username [%s], md5_hash [%s]", session->username, md5_apop_he);
result = auth_md5_validate(ci,session->username, md5_apop_he, session->apop_stamp);
g_free(md5_apop_he);
md5_apop_he = 0;
switch (result) {
case -1:
session->SessionResult = 3;
return -1;
case 0:
TRACE(TRACE_ERROR, "user [%s] tried to login with wrong password", session->username);
g_free(session->username);
session->username = NULL;
g_free(session->password);
session->password = NULL;
return pop3_error(session, stream, "-ERR authentication attempt is invalid\r\n");
default:
/* user logged in OK */
session->state = POP3_TRANSACTION_STATE;
/* user seems to be valid, let's build a session */
TRACE(TRACE_DEBUG, "validation OK, building a session for user [%s]",
session->username);
/* if pop_before_smtp is active, log this ip */
if (pop_before_smtp)
db_log_ip(client_ip);
child_reg_connected_user(session->username);
result = db_createsession(result, session);
if (result == 1) {
ci_write((FILE *) stream, "+OK %s has %" U64_T_FORMAT " messages (%" U64_T_FORMAT " octets)\r\n",
session->username,
session->virtual_totalmessages,
session->virtual_totalsize);
TRACE(TRACE_MESSAGE, "user %s logged in [messages=%" U64_T_FORMAT ", octets=%" U64_T_FORMAT "]",
session->username,
session->virtual_totalmessages,
session->virtual_totalsize);
} else
session->SessionResult = 4; /* Database error. */
return result;
}
return 1;
case POP3_AUTH:
if (session->state != POP3_AUTHORIZATION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
return pop3_error(session, stream, "-ERR no AUTH mechanisms supported\r\n");
case POP3_TOP:
if (session->state != POP3_TRANSACTION_STATE)
return pop3_error(session, stream, "-ERR wrong command mode\r\n");
/* find out how many lines they want */
searchptr = strstr(value, " ");
/* insufficient parameters */
if (searchptr == NULL)
return pop3_error(session, stream, "-ERR your command does not compute\r\n");
/* skip the space */
searchptr = searchptr + 1;
/* value should now be the the message that needs to be retrieved */
value[searchptr - value - 1] = '\0';
/* check if searchptr or value are negative. If so return an
error. This is done by only letting the strings contain
digits (0-9) */
if (strspn(searchptr, "0123456789") != strlen(searchptr))
return pop3_error(session, stream, "-ERR wrong parameter\r\n");
if (strspn(value, "0123456789") != strlen(value))
return pop3_error(session, stream, "-ERR wrong parameter\r\n");
top_lines = strtoull(searchptr, NULL, 10);
top_messageid = strtoull(value, NULL, 10);
if (top_messageid < 1)
return pop3_error(session, stream, "-ERR wrong parameter\r\n");
TRACE(TRACE_DEBUG, "TOP command (partially) retrieving message");
tmpelement = dm_list_getstart(&(session->messagelst));
/* selecting a message */
TRACE(TRACE_DEBUG, "TOP command, selecting message");
while (tmpelement != NULL) {
msg = (struct message *) tmpelement->data;
if (msg->messageid == top_messageid && msg->virtual_messagestatus < MESSAGE_STATUS_DELETE) { /* message is not deleted */
ci_write((FILE *) stream, "+OK %" U64_T_FORMAT " lines of message %" U64_T_FORMAT "\r\n", top_lines, top_messageid);
return db_send_message_lines(stream, session->mailbox_idnr, msg->realmessageid, top_lines, 0);
}
tmpelement = tmpelement->nextnode;
}
return pop3_error(session, stream, "-ERR no such message\r\n");
case POP3_CAPA:
ci_write((FILE *) stream, "+OK Capability list follows\r\nTOP\r\nUSER\r\nUIDL\r\n.\r\n");
return 1;
default:
return pop3_error(session, stream, "-ERR command not understood\r\n");
}
return 1;
}
|