pop3_connect.c
4.3 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
/* 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 Library 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <mailutils/sys/pop3.h>
static int pop3_sleep (int seconds);
/* Open the connection to the server. The server sends an affirmative greeting
that may contain a timestamp for APOP. */
static int
pop3_connect0 (pop3_t pop3, const char *host, unsigned int port)
{
int status;
/* Default is 110. */
if (!port)
port = 110;
/* Enter the pop state machine, and boogy: AUTHORISATION State. */
switch (pop3->state)
{
default:
/* __Fallthrough__, they want to clear an error. */
case POP3_NO_STATE:
/* Create the networking stack. */
if (pop3->stream == NULL)
{
status = stream_tcp_create (&(pop3->stream));
POP3_CHECK_ERROR (pop3, status);
/* Using the awkward stream_t buffering. */
/*stream_setbufsiz (pop3->stream, 1024); */
}
else
{
/* This is sudden death: for many pop servers, it is important to
let them time to remove locks or move the .user.pop files. This
happen when we do close() and immediately open(). For example,
the user does not want to read the entire file, and wants to start
to read a new message, closing the connection and immediately
contacting the server again, and he'll end up having
"-ERR Mail Lock busy" or something similar. To prevent this race
condition we sleep 2 seconds. This really obvious for in
environment where QPopper is use, the user as a big mailbox. */
pop3_disconnect (pop3);
pop3_sleep (2);
}
pop3->state = POP3_CONNECT;
case POP3_CONNECT:
/* Establish the connection. */
status = stream_open (pop3->stream, host, port,
MU_STREAM_READ|MU_STREAM_WRITE);
POP3_CHECK_EAGAIN (pop3, status);
pop3->acknowledge = 0;
pop3->state = POP3_GREETINGS;
case POP3_GREETINGS:
/* Get the greetings. */
{
size_t len = 0;
char *right, *left;
status = pop3_response (pop3, NULL, 0, &len);
POP3_CHECK_EAGAIN (pop3, status);
if (strncasecmp (pop3->ack.buf, "+OK", 3) != 0)
{
stream_close (pop3->stream);
pop3->state = POP3_NO_STATE;
return MU_ERROR_OPERATION_DENIED;
}
/* Get the timestamp. */
right = memchr (pop3->ack.buf, '<', len);
if (right)
{
len = len - (right - pop3->ack.buf);
left = memchr (right, '>', len);
if (left)
{
len = left - right;
pop3->timestamp = calloc (len + 1, 1);
if (pop3->timestamp == NULL)
{
stream_close (pop3->stream);
POP3_CHECK_ERROR (pop3, MU_ERROR_NO_MEMORY);
}
/* Do not copy the surrounding '<>'. */
memcpy (pop3->timestamp, right + 1, len - 1);
}
}
pop3->state = POP3_NO_STATE;
}
} /* End AUTHORISATION state. */
return status;
}
/* GRRRRR!! We can not use sleep in the library since this we'll
muck up any alarm() done by the user. */
static int
pop3_sleep (int seconds)
{
struct timeval tval;
tval.tv_sec = seconds;
tval.tv_usec = 0;
return select (1, NULL, NULL, NULL, &tval);
}
int
pop3_connect (pop3_t pop3, const char *host, unsigned int port)
{
int status;
/* Sanity checks. */
if (pop3 == NULL || host == NULL)
return MU_ERROR_INVALID_PARAMETER;
monitor_lock (pop3->lock);
monitor_cleanup_push (pop3_cleanup, pop3);
status = pop3_connect0 (pop3, host, port);
monitor_unlock (pop3->lock);
monitor_cleanup_pop (0);
return status;
}