~ari-tczew/ubuntu/dapper/fetchmail/fix-CVE-2008-2711

« back to all changes in this revision

Viewing changes to smtp.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-02-07 12:12:13 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060207121213-onurwfrzdlzlzxnt
Tags: 6.3.2-2ubuntu1
* Resynchronise with Debian. This brings the new upstream version to dapper
  since upstream support for 6.2 was dropped.
* Drop debian/patches/CVE-2005-4348.dpatch, upstream now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 * For license terms, see the file COPYING in this directory.
9
9
 */
10
10
 
 
11
#include "config.h"
 
12
#include "fetchmail.h"
 
13
 
11
14
#include <stdio.h>
12
15
#include <unistd.h>
13
16
#include <string.h>
14
17
#include <signal.h>
15
 
#include "fetchmail.h"
16
18
#include "socket.h"
17
19
#include "smtp.h"
18
 
#include "config.h"
19
20
#include "i18n.h"
20
21
 
21
22
struct opt
38
39
 
39
40
char smtp_response[MSGBUFSIZE];
40
41
 
41
 
static char smtp_mode = 'S';
42
 
 
43
 
void SMTP_setmode(char sl)
44
 
/* set whether we are speaking SMTP or LMTP */
45
 
{
46
 
    smtp_mode = sl;
47
 
}
48
 
 
49
 
int SMTP_helo(int sock,const char *host)
 
42
int SMTP_helo(int sock, char smtp_mode, const char *host)
50
43
/* send a "HELO" message to the SMTP listener */
51
44
{
52
45
  int ok;
53
46
 
54
47
  SockPrintf(sock,"HELO %s\r\n", host);
55
48
  if (outlevel >= O_MONITOR)
56
 
      report(stdout, "SMTP> HELO %s\n", host);
57
 
  ok = SMTP_ok(sock);
 
49
      report(stdout, "%cMTP> HELO %s\n", smtp_mode, host);
 
50
  ok = SMTP_ok(sock, smtp_mode);
58
51
  return ok;
59
52
}
60
53
 
61
 
static void SMTP_auth_error(int sock, char *msg)
 
54
static void SMTP_auth_error(int sock, const char *msg)
62
55
{
63
56
    SockPrintf(sock, "*\r\n");
64
57
    SockRead(sock, smtp_response, sizeof(smtp_response) - 1);
65
58
    if (outlevel >= O_MONITOR) report(stdout, msg);
66
59
}
67
60
 
68
 
static void SMTP_auth(int sock, char *username, char *password, char *buf)
 
61
static void SMTP_auth(int sock, char smtp_mode, char *username, char *password, char *buf)
69
62
/* ESMTP Authentication support for fetchmail by Wojciech Polak */
70
63
{       
71
64
        int c;
105
98
                        report(stdout, GT_("Challenge decoded: %s\n"), b64buf);
106
99
                hmac_md5(password, strlen(password),
107
100
                         b64buf, strlen(b64buf), digest, sizeof(digest));
108
 
#ifdef HAVE_SNPRINTF
109
101
                snprintf(tmp, sizeof(tmp),
110
 
#else
111
 
                sprintf(tmp,
112
 
#endif /* HAVE_SNPRINTF */
113
102
                "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
114
103
                username,  digest[0], digest[1], digest[2], digest[3],
115
104
                digest[4], digest[5], digest[6], digest[7], digest[8],
118
107
 
119
108
                to64frombits(b64buf, tmp, strlen(tmp));
120
109
                SockPrintf(sock, "%s\r\n", b64buf);
121
 
                SMTP_ok(sock);
 
110
                SMTP_ok(sock, smtp_mode);
122
111
        }
123
112
        else if (strstr(buf, "PLAIN")) {
124
113
                int len;
125
114
                if (outlevel >= O_MONITOR)
126
115
                        report(stdout, GT_("ESMTP PLAIN Authentication...\n"));
127
 
#ifdef HAVE_SNPRINTF
128
 
                snprintf(tmp, sizeof(tmp),
129
 
#else
130
 
                sprintf(tmp,
131
 
#endif /* HAVE_SNPRINTF */
132
 
                "^%s^%s", username, password);
 
116
                snprintf(tmp, sizeof(tmp), "^%s^%s", username, password);
133
117
 
134
118
                len = strlen(tmp);
135
119
                for (c = len - 1; c >= 0; c--)
139
123
                }
140
124
                to64frombits(b64buf, tmp, len);
141
125
                SockPrintf(sock, "AUTH PLAIN %s\r\n", b64buf);
142
 
                SMTP_ok(sock);
 
126
                SMTP_ok(sock, smtp_mode);
143
127
        }
144
128
        else if (strstr(buf, "LOGIN")) {
145
129
                if (outlevel >= O_MONITOR)
178
162
                }
179
163
                to64frombits(b64buf, password, strlen(password));
180
164
                SockPrintf(sock, "%s\r\n", b64buf);
181
 
                SMTP_ok(sock);
 
165
                SMTP_ok(sock, smtp_mode);
182
166
        }
183
167
        return;
184
168
}
185
169
 
186
 
int SMTP_ehlo(int sock, const char *host, char *name, char *password, int *opt)
 
170
int SMTP_ehlo(int sock, char smtp_mode, const char *host, char *name, char *password, int *opt)
187
171
/* send a "EHLO" message to the SMTP listener, return extension status bits */
188
172
{
189
173
  struct opt *hp;
193
177
  if (outlevel >= O_MONITOR)
194
178
      report(stdout, "%cMTP> %cHLO %s\n", 
195
179
            smtp_mode, (smtp_mode == 'S') ? 'E' : smtp_mode, host);
196
 
  
 
180
 
197
181
  *opt = 0;
198
182
  while ((SockRead(sock, smtp_response, sizeof(smtp_response)-1)) != -1)
199
183
  {
207
191
          return SM_ERROR;
208
192
      smtp_response[n] = '\0';
209
193
      if (outlevel >= O_MONITOR)
210
 
          report(stdout, "SMTP< %s\n", smtp_response);
 
194
          report(stdout, "%cMTP< %s\n", smtp_mode, smtp_response);
211
195
      for (hp = extensions; hp->name; hp++)
212
196
          if (!strncasecmp(hp->name, smtp_response+4, strlen(hp->name))) {
213
197
              *opt |= hp->value;
214
198
              if (strncmp(hp->name, "AUTH ", 5) == 0)
215
 
                strncpy(auth_response, smtp_response, sizeof(auth_response));
 
199
                strncpy(auth_response, smtp_response, sizeof(auth_response));
216
200
                auth_response[sizeof(auth_response)-1] = '\0';
217
201
          }
218
202
      if ((smtp_response[0] == '1' || smtp_response[0] == '2' || smtp_response[0] == '3') && smtp_response[3] == ' ') {
219
203
          if (*opt & ESMTP_AUTH)
220
 
                SMTP_auth(sock, name, password, auth_response);
 
204
                SMTP_auth(sock, smtp_mode, name, password, auth_response);
221
205
          return SM_OK;
222
206
      }
223
207
      else if (smtp_response[3] != '-')
226
210
  return SM_UNRECOVERABLE;
227
211
}
228
212
 
229
 
int SMTP_from(int sock, const char *from, const char *opts)
 
213
int SMTP_from(int sock, char smtp_mode, const char *from, const char *opts)
230
214
/* send a "MAIL FROM:" message to the SMTP listener */
231
215
{
232
216
    int ok;
233
217
    char buf[MSGBUFSIZE];
234
218
 
235
219
    if (from[0]=='<')
236
 
#ifdef HAVE_SNPRINTF
237
 
        snprintf(buf, sizeof(buf),
238
 
#else
239
 
        sprintf(buf,
240
 
#endif /* HAVE_SNPRINTF */
241
 
                "MAIL FROM:%s", from);
 
220
        snprintf(buf, sizeof(buf), "MAIL FROM:%s", from);
242
221
    else
243
 
#ifdef HAVE_SNPRINTF
244
 
    snprintf(buf, sizeof(buf),
245
 
#else
246
 
    sprintf(buf,
247
 
#endif /* HAVE_SNPRINTF */
248
 
            "MAIL FROM:<%s>", from);
 
222
        snprintf(buf, sizeof(buf), "MAIL FROM:<%s>", from);
249
223
    if (opts)
250
 
#ifdef HAVE_SNPRINTF
251
224
        snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%s", opts);
252
 
#else
253
 
        strcat(buf, opts);
254
 
#endif /* HAVE_SNPRINTF */
255
225
    SockPrintf(sock,"%s\r\n", buf);
256
226
    if (outlevel >= O_MONITOR)
257
227
        report(stdout, "%cMTP> %s\n", smtp_mode, buf);
258
 
    ok = SMTP_ok(sock);
 
228
    ok = SMTP_ok(sock, smtp_mode);
259
229
    return ok;
260
230
}
261
231
 
262
 
int SMTP_rcpt(int sock, const char *to)
 
232
int SMTP_rcpt(int sock, char smtp_mode, const char *to)
263
233
/* send a "RCPT TO:" message to the SMTP listener */
264
234
{
265
235
  int ok;
267
237
  SockPrintf(sock,"RCPT TO:<%s>\r\n", to);
268
238
  if (outlevel >= O_MONITOR)
269
239
      report(stdout, "%cMTP> RCPT TO:<%s>\n", smtp_mode, to);
270
 
  ok = SMTP_ok(sock);
 
240
  ok = SMTP_ok(sock, smtp_mode);
271
241
  return ok;
272
242
}
273
243
 
274
 
int SMTP_data(int sock)
 
244
int SMTP_data(int sock, char smtp_mode)
275
245
/* send a "DATA" message to the SMTP listener */
276
246
{
277
247
  int ok;
279
249
  SockPrintf(sock,"DATA\r\n");
280
250
  if (outlevel >= O_MONITOR)
281
251
      report(stdout, "%cMTP> DATA\n", smtp_mode);
282
 
  ok = SMTP_ok(sock);
 
252
  ok = SMTP_ok(sock, smtp_mode);
283
253
  return ok;
284
254
}
285
255
 
286
 
int SMTP_rset(int sock)
 
256
int SMTP_rset(int sock, char smtp_mode)
287
257
/* send a "RSET" message to the SMTP listener */
288
258
{
289
259
  int ok;
291
261
  SockPrintf(sock,"RSET\r\n");
292
262
  if (outlevel >= O_MONITOR)
293
263
      report(stdout, "%cMTP> RSET\n", smtp_mode);
294
 
  ok = SMTP_ok(sock);
 
264
  ok = SMTP_ok(sock, smtp_mode);
295
265
  return ok;
296
266
}
297
267
 
298
 
int SMTP_quit(int sock)
 
268
int SMTP_quit(int sock, char smtp_mode)
299
269
/* send a "QUIT" message to the SMTP listener */
300
270
{
301
271
  int ok;
303
273
  SockPrintf(sock,"QUIT\r\n");
304
274
  if (outlevel >= O_MONITOR)
305
275
      report(stdout, "%cMTP> QUIT\n", smtp_mode);
306
 
  ok = SMTP_ok(sock);
 
276
  ok = SMTP_ok(sock, smtp_mode);
307
277
  return ok;
308
278
}
309
279
 
310
 
int SMTP_eom(int sock)
 
280
int SMTP_eom(int sock, char smtp_mode)
311
281
/* send a message data terminator to the SMTP listener */
312
282
{
313
283
  int ok;
320
290
   * When doing LMTP, must process many of these at the outer level. 
321
291
   */
322
292
  if (smtp_mode == 'S')
323
 
      ok = SMTP_ok(sock);
 
293
      ok = SMTP_ok(sock, smtp_mode);
324
294
  else
325
295
      ok = SM_OK;
326
296
 
329
299
 
330
300
time_t last_smtp_ok = 0;
331
301
 
332
 
int SMTP_ok(int sock)
 
302
int SMTP_ok(int sock, char smtp_mode)
333
303
/* returns status of SMTP connection */
334
304
{
335
305
    SIGHANDLERTYPE alrmsave;