extra.c
5.08 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
GNU Mailutils 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.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "pop3d.h"
static FILE *ifile;
static FILE *ofile;
#ifdef WITH_TLS
static gnutls_session sfile;
#endif /* WITH_TLS */
/* 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 *
pop3d_args (const char *cmd)
{
int space = -1, i = 0, len;
char *buf;
len = strlen (cmd) + 1;
buf = malloc (len * sizeof (char));
if (buf == NULL)
pop3d_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 *
pop3d_cmd (const char *cmd)
{
char *buf;
int i = 0, len;
len = strlen (cmd) + 1;
buf = malloc (len * sizeof (char));
if (buf == NULL)
pop3d_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;
}
/* This is called if GNU POP3 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
pop3d_abquit (int reason)
{
/* Unlock spool */
pop3d_unlock ();
mailbox_close (mbox);
mailbox_destroy (&mbox);
switch (reason)
{
case ERR_NO_MEM:
pop3d_outf ("-ERR Out of memory, quitting\r\n");
syslog (LOG_ERR, _("Out of memory"));
break;
case ERR_SIGNAL:
pop3d_outf ("-ERR Quitting on signal\r\n");
syslog (LOG_ERR, _("Quitting on signal"));
break;
case ERR_TIMEOUT:
pop3d_outf ("-ERR Session timed out\r\n");
if (state == TRANSACTION)
syslog (LOG_INFO, _("Session timed out for user: %s"), username);
else
syslog (LOG_INFO, _("Session timed out for no user"));
break;
case ERR_NO_OFILE:
syslog (LOG_INFO, _("No socket to send to"));
break;
case ERR_MBOX_SYNC:
syslog (LOG_ERR, _("Mailbox was updated by other party: %s"), username);
pop3d_outf
("-ERR [OUT-SYNC] Mailbox updated by other party or corrupt\r\n");
break;
default:
pop3d_outf ("-ERR Quitting (reason unknown)\r\n");
syslog (LOG_ERR, _("Unknown quit"));
break;
}
closelog ();
exit (EXIT_FAILURE);
}
void
pop3d_setio (FILE * in, FILE * out)
{
if (!in || !out)
pop3d_abquit (ERR_NO_OFILE);
ifile = in;
ofile = out;
}
#ifdef WITH_TLS
int
pop3d_init_tls_server ()
{
sfile =
(gnutls_session) mu_init_tls_server (fileno (ifile), fileno (ofile));
if (!sfile)
return 0;
return 1;
}
void
pop3d_deinit_tls_server ()
{
mu_deinit_tls_server (sfile);
}
#endif /* WITH_TLS */
void
pop3d_flush_output ()
{
fflush (ofile);
}
int
pop3d_is_master ()
{
return ofile == NULL;
}
void
pop3d_outf (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
if (daemon_param.transcript)
{
char *buf;
vasprintf (&buf, fmt, ap);
if (buf)
{
syslog (LOG_DEBUG, "sent: %s", buf);
free (buf);
}
}
#ifdef WITH_TLS
if (tls_done)
{
char *buf;
vasprintf (&buf, fmt, ap);
if (buf)
{
gnutls_record_send (sfile, buf, strlen (buf));
free (buf);
}
}
else
#endif /* WITH_TLS */
vfprintf (ofile, fmt, ap);
va_end (ap);
}
/* Gets a line of input from the client, caller should free() */
char *
pop3d_readline (char *buffer, size_t size)
{
char *ptr;
alarm (daemon_param.timeout);
#ifdef WITH_TLS
if (tls_done)
{
gnutls_record_recv (sfile, buffer, size - 1);
ptr = buffer;
}
else
#endif /* WITH_TLS */
ptr = fgets (buffer, size, ifile);
alarm (0);
/* We should probably check ferror() too, but if ptr is null we
are done anyway; if (!ptr && ferror(ifile)) */
if (!ptr)
pop3d_abquit (ERR_NO_OFILE);
if (daemon_param.transcript)
syslog (LOG_DEBUG, "recv: %s", ptr);
/* Caller should not free () this ... should we strdup() then? */
return ptr;
}