smtp.c
19 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
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 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/types.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <mailutils/stream.h>
#include <mailer0.h>
#include <registrar0.h>
#include <bio.h>
#include <misc.h>
static int smtp_init (mailer_t);
static struct mailer_entry _smtp_entry =
{
url_smtp_init, smtp_init
};
static struct _record _smtp_record =
{
MU_SMTP_SCHEME,
NULL, /* Mailbox entry. */
&_smtp_entry, /* Mailer entry. */
NULL, /* Mailer entry. */
0, /* Not malloc()ed. */
NULL, /* No need for an owner. */
NULL, /* is_scheme method. */
NULL, /* get_mailbox method. */
NULL, /* get_mailer method. */
NULL /* get_folder method. */
};
/* We export two functions: url parsing and the initialisation of
the mailbox, via the register entry/record. */
record_t smtp_record = &_smtp_record;
mailer_entry_t smtp_entry = &_smtp_entry;
struct _smtp
{
char *mailhost;
char *localhost;
bio_t bio;
char *ptr;
char *nl;
char *buffer;
size_t buflen;
enum smtp_state
{
SMTP_NO_STATE, SMTP_OPEN, SMTP_GREETINGS, SMTP_EHLO, SMTP_EHLO_ACK,
SMTP_HELO, SMTP_HELO_ACK, SMTP_QUIT, SMTP_QUIT_ACK, SMTP_ENV_FROM,
SMTP_ENV_RCPT, SMTP_MAIL_FROM, SMTP_MAIL_FROM_ACK, SMTP_RCPT_TO,
SMTP_RCPT_TO_ACK, SMTP_DATA, SMTP_DATA_ACK, SMTP_SEND, SMTP_SEND_ACK,
SMTP_SEND_DOT
} state;
int extended;
char *from;
char *to;
off_t offset;
int dsn;
message_t message;
};
typedef struct _smtp * smtp_t;
/* Usefull little Macros, since these are very repetitive. */
#define CLEAR_STATE(smtp) \
smtp->state = SMTP_NO_STATE
/* Clear the state and close the stream. */
#define CHECK_ERROR_CLOSE(mailer, smtp, status) \
do \
{ \
if (status != 0) \
{ \
stream_close (mailer->stream); \
CLEAR_STATE (smtp); \
return status; \
} \
} \
while (0)
/* Clear the state. */
#define CHECK_ERROR(smtp, status) \
do \
{ \
if (status != 0) \
{ \
CLEAR_STATE (smtp); \
return status; \
} \
} \
while (0)
/* Clear the state for non recoverable error. */
#define CHECK_EAGAIN(smtp, status) \
do \
{ \
if (status != 0) \
{ \
if (status != EAGAIN && status != EINPROGRESS && status != EINTR) \
{ \
CLEAR_STATE (smtp); \
} \
return status; \
} \
} \
while (0)
static void smtp_destroy (mailer_t);
static int smtp_open (mailer_t, int);
static int smtp_close (mailer_t);
static int smtp_send_message (mailer_t, const char *from, const char *rcpt,
int dsn, message_t);
static int smtp_writeline (smtp_t smtp, const char *format, ...);
static int smtp_readline (smtp_t);
static int smtp_read_ack (smtp_t);
static int smtp_write (smtp_t);
int
smtp_init (mailer_t mailer)
{
smtp_t smtp;
/* Allocate memory specific to smtp mailer. */
smtp = mailer->data = calloc (1, sizeof (*smtp));
if (mailer->data == NULL)
return ENOMEM;
mailer->_init = smtp_init;
mailer->_destroy = smtp_destroy;
mailer->_open = smtp_open;
mailer->_close = smtp_close;
mailer->_send_message = smtp_send_message;
return 0;
}
static void
smtp_destroy(mailer_t mailer)
{
smtp_t smtp = mailer->data;
smtp_close (mailer);
if (smtp->mailhost)
free (smtp->mailhost);
if (smtp->localhost)
free (smtp->localhost);
if (smtp->bio)
bio_destroy (&(smtp->bio));
if (smtp->buffer)
free (smtp->buffer);
if (smtp->from)
free (smtp->from);
if (smtp->to)
free (smtp->to);
free (smtp);
mailer->data = NULL;
}
static int
smtp_open (mailer_t mailer, int flags)
{
smtp_t smtp = mailer->data;
int status;
long port;
size_t buf_len = 0;
/* Sanity checks. */
if (smtp == NULL)
return EINVAL;
mailer->flags = flags | MU_STREAM_SMTP;
/* Fetch the mailer server name and the port in the url_t. */
if ((status = url_get_host (mailer->url, NULL, 0, &buf_len)) != 0
|| buf_len == 0 || (status = url_get_port (mailer->url, &port)) != 0)
return status;
switch (smtp->state)
{
case SMTP_NO_STATE:
/* Get the mailhost. */
if (smtp->mailhost)
{
free (smtp->mailhost);
smtp->mailhost = NULL;
}
smtp->mailhost = calloc (buf_len + 1, sizeof (char));
if (smtp->mailhost == NULL)
return ENOMEM;
url_get_host (mailer->url, smtp->mailhost, buf_len + 1, NULL);
if (smtp->localhost)
{
free (smtp->localhost);
smtp->localhost = NULL;
}
/* Fetch our localhost name. */
buf_len = 64;
do
{
char *tmp;
errno = 0;
buf_len *= 2; /* Initial guess */
tmp = realloc (smtp->localhost, buf_len);
if (tmp == NULL)
{
if (smtp->localhost)
free (smtp->localhost);
smtp->localhost = NULL;
free (smtp->mailhost);
smtp->mailhost = NULL;
return ENOMEM;
}
smtp->localhost = tmp;
}
while (((status = gethostname(smtp->localhost, buf_len)) == 0
&& !memchr (smtp->localhost, '\0', buf_len))
#ifdef ENAMETOOLONG
|| errno == ENAMETOOLONG
#endif
);
if (status != 0 && errno != 0)
{
/* gethostname failed, abort. */
free (smtp->localhost);
smtp->localhost = NULL;
free (smtp->mailhost);
smtp->mailhost = NULL;
return EINVAL;
}
/* Many SMTP servers prefer a FQDN. */
if (strchr (smtp->localhost, '.') == NULL)
{
struct hostent *hp = gethostbyname (smtp->localhost);
if (hp == NULL)
{
/* Don't flag it as an error some SMTP servers can get the FQDN
by themselves even if the client is lying, probably
with getpeername(). */
// return EINVAL;
}
else
{
free (smtp->localhost);
smtp->localhost = strdup (hp->h_name);
if (smtp->localhost == NULL)
{
free (smtp->mailhost);
smtp->mailhost = NULL;
return ENOMEM;
}
}
}
/* allocate a working io buffer. */
if (smtp->buffer == NULL)
{
smtp->buflen = 255; /* Initial guess. */
smtp->buffer = malloc (smtp->buflen + 1);
if (smtp->buffer == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
smtp->ptr = smtp->buffer;
}
/* Create a TCP stack if one is not given. */
if (mailer->stream == NULL)
{
status = tcp_stream_create (&(mailer->stream));
CHECK_ERROR (smtp, status);
}
status = bio_create (&(smtp->bio), mailer->stream);
CHECK_ERROR (smtp, status);
smtp->state = SMTP_OPEN;
case SMTP_OPEN:
MAILER_DEBUG2 (mailer, MU_DEBUG_PROT, "smtp_open (%s:%d)\n", smtp->localhost, port);
status = stream_open (mailer->stream, smtp->mailhost, port,
mailer->flags);
CHECK_EAGAIN (smtp, status);
smtp->state = SMTP_GREETINGS;
case SMTP_GREETINGS:
/* Swallow the greetings. */
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
return EACCES;
}
status = smtp_writeline (smtp, "EHLO %s\r\n", smtp->localhost);
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_EHLO;
case SMTP_EHLO:
/* We first try Extended SMTP. */
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
smtp->state = SMTP_EHLO_ACK;
case SMTP_EHLO_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
smtp->extended = 0;
status = smtp_writeline (smtp, "HELO %s\r\n", smtp->localhost);
CHECK_ERROR (smtp, status);
smtp->state = SMTP_HELO;
}
else
{
smtp->extended = 1;
break;
}
case SMTP_HELO:
if (!smtp->extended)
{
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
}
smtp->state = SMTP_HELO_ACK;
case SMTP_HELO_ACK:
if (!smtp->extended)
{
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
CLEAR_STATE (smtp);
return EACCES;
}
}
default:
break;
}
smtp->state = SMTP_NO_STATE;
return 0;
}
static int
smtp_close (mailer_t mailer)
{
smtp_t smtp = mailer->data;
int status;
switch (smtp->state)
{
case SMTP_NO_STATE:
status = smtp_writeline (smtp, "Quit\r\n");
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_QUIT;
case SMTP_QUIT:
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
smtp->state = SMTP_QUIT_ACK;
case SMTP_QUIT_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
default:
break;
}
return stream_close (mailer->stream);
}
static int
smtp_send_message(mailer_t mailer, const char *from, const char *rcpt,
int dsn, message_t msg)
{
smtp_t smtp = mailer->data;
int status;
if (smtp == NULL || msg == NULL)
return EINVAL;
switch (smtp->state)
{
case SMTP_NO_STATE:
smtp->dsn = dsn;
smtp->state = SMTP_ENV_FROM;
case SMTP_ENV_FROM:
if (smtp->from)
{
free (smtp->from);
smtp->from = NULL;
}
/* Try to fetch it from the header. */
if (from == NULL)
{
header_t header;
size_t size;
message_get_header (msg, &header);
status = header_get_value (header, MU_HEADER_FROM, NULL, 0, &size);
if (status == EAGAIN)
return status;
/* If it's not in the header create one form the passwd. */
if (status != 0)
{
struct passwd *pwd = getpwuid (getuid ());
/* Not in the passwd ???? We have a problem. */
if (pwd == 0 || pwd->pw_name == NULL)
{
size_t len = 10 + strlen (smtp->localhost) + 1;
smtp->from = calloc (len, sizeof (char));
if (smtp->from == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
snprintf (smtp->from, len, "%d@%s", getuid(),
smtp->localhost);
}
else
{
smtp->from = calloc (strlen (pwd->pw_name) + 1
+ strlen (smtp->localhost) + 1,
sizeof (char));
if (smtp->from == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
sprintf(smtp->from, "%s@%s", pwd->pw_name, smtp->localhost);
}
}
else
{
smtp->from = calloc (size + 1, sizeof (char));
if (smtp->from == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
status = header_get_value (header, MU_HEADER_FROM, smtp->from,
size + 1, NULL);
CHECK_EAGAIN (smtp, status);
}
}
else
{
smtp->from = strdup (from);
if (smtp->from == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
}
/* Check if a Fully Qualified Name, some smtp servers
notably sendmail insists on it, for good reasons. */
if (strchr (smtp->from, '@') == NULL)
{
char *tmp;
tmp = malloc (strlen (smtp->from) + 1 +strlen (smtp->localhost) + 1);
if (tmp == NULL)
{
free (smtp->from);
smtp->from = NULL;
CHECK_ERROR (smtp, ENOMEM);
}
sprintf (tmp, "%s@%s", smtp->from, smtp->localhost);
free (smtp->from);
smtp->from = tmp;
}
smtp->state = SMTP_ENV_RCPT;
case SMTP_ENV_RCPT:
if (smtp->to)
{
free (smtp->to);
smtp->to = NULL;
}
if (rcpt == NULL)
{
header_t header;
size_t size;
message_get_header (msg, &header);
status = header_get_value (header, MU_HEADER_TO, NULL, 0, &size);
CHECK_EAGAIN (smtp, status);
smtp->to = calloc (size + 1, sizeof (char));
if (smtp->to == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
status = header_get_value (header, MU_HEADER_TO, smtp->to,
size + 1, NULL);
CHECK_EAGAIN (smtp, status);
}
else
{
smtp->to = strdup (rcpt);
if (smtp->to == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
}
status = smtp_writeline (smtp, "MAIL FROM: %s\r\n", smtp->from);
free (smtp->from);
smtp->from = NULL;
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_MAIL_FROM;
case SMTP_MAIL_FROM:
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
smtp->state = SMTP_MAIL_FROM_ACK;
case SMTP_MAIL_FROM_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
CLEAR_STATE (smtp);
return EACCES;
}
/* We use a goto, since we may have multiple recipients,
we come back here and doit all over again ... Not pretty. */
RCPT_TO:
{
char *buf;
size_t len = strlen (smtp->to) + 1;
buf = calloc (len, sizeof (char));
if (buf == NULL)
{
CHECK_ERROR (smtp, ENOMEM);
}
if (parseaddr (smtp->to, buf, len) != 0)
{
free (buf);
CHECK_ERROR (smtp, EINVAL);
}
status = smtp_writeline (smtp, "RCPT TO: %s\r\n", buf);
free (buf);
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_RCPT_TO;
}
case SMTP_RCPT_TO:
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
smtp->state = SMTP_RCPT_TO_ACK;
case SMTP_RCPT_TO_ACK:
{
char *p;
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
CLEAR_STATE (smtp);
return EACCES;
}
/* Do we have multiple recipients ? */
p = strchr (smtp->to, ',');
if (p != NULL)
{
char *tmp = smtp->to;
smtp->to = strdup (p++);
if (smtp->to == NULL)
{
free (tmp);
CHECK_ERROR (smtp, ENOMEM);
}
free (tmp);
goto RCPT_TO;
}
/* We are done with the rcpt. */
free (smtp->to);
smtp->to = NULL;
status = smtp_writeline (smtp, "DATA\r\n");
CHECK_ERROR (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
smtp->state = SMTP_DATA;
}
case SMTP_DATA:
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
smtp->state = SMTP_DATA_ACK;
case SMTP_DATA_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '3')
{
stream_close (mailer->stream);
CLEAR_STATE (smtp);
return EACCES;
}
smtp->offset = 0;
smtp->state = SMTP_SEND;
case SMTP_SEND:
{
stream_t stream;
size_t n = 0;
char data[256] = "";
/* We may be here after an EAGAIN so check if we have something
in the buffer and flush it. */
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
message_get_stream (msg, &stream);
while ((status = stream_readline (stream, data, sizeof (data) - 1,
smtp->offset, &n)) == 0 && n > 0)
{
if (data [n - 1] == '\n')
data [n -1] = '\0';
if (data[0] == '.')
{
status = smtp_writeline (smtp, ".%s\r\n", data);
CHECK_ERROR (smtp, status);
}
else
{
status = smtp_writeline (smtp, "%s\r\n", data);
CHECK_ERROR (smtp, status);
}
smtp->offset += n;
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
}
smtp->offset = 0;
status = smtp_writeline (smtp, ".\r\n");
CHECK_ERROR (smtp, status);
smtp->state = SMTP_SEND_DOT;
}
case SMTP_SEND_DOT:
status = smtp_write (smtp);
CHECK_EAGAIN (smtp, status);
smtp->state = SMTP_SEND_ACK;
case SMTP_SEND_ACK:
status = smtp_read_ack (smtp);
CHECK_EAGAIN (smtp, status);
MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
if (smtp->buffer[0] != '2')
{
stream_close (mailer->stream);
CLEAR_STATE (smtp);
return EACCES;
}
default:
break;
}
CLEAR_STATE (smtp);
return 0;
}
static int
smtp_writeline (smtp_t smtp, const char *format, ...)
{
int len;
va_list ap;
va_start(ap, format);
do
{
len = vsnprintf (smtp->buffer, smtp->buflen - 1, format, ap);
if (len >= (int)smtp->buflen)
{
smtp->buflen *= 2;
smtp->buffer = realloc (smtp->buffer, smtp->buflen);
if (smtp->buffer == NULL)
return ENOMEM;
}
}
while (len > (int)smtp->buflen);
va_end(ap);
smtp->ptr = smtp->buffer + len;
return 0;
}
static int
smtp_write (smtp_t smtp)
{
int status = 0;
size_t len;
if (smtp->ptr > smtp->buffer)
{
len = smtp->ptr - smtp->buffer;
status = bio_write (smtp->bio, smtp->buffer, len, &len);
if (status == 0)
{
memmove (smtp->buffer, smtp->buffer + len, len);
smtp->ptr -= len;
}
}
else
{
smtp->ptr = smtp->buffer;
len = 0;
}
return status;
}
static int
smtp_read_ack (smtp_t smtp)
{
int status;
int multi;
do
{
multi = 0;
status = smtp_readline (smtp);
if ((smtp->ptr - smtp->buffer) > 4
&& smtp->buffer[3] == '-')
multi = 1;
if (status == 0)
smtp->ptr = smtp->buffer;
}
while (multi && status == 0);
if (status == 0)
smtp->ptr = smtp->buffer;
return status;
}
/* Read a complete line form the pop server. Transform CRLF to LF,
put a null in the buffer when done. */
static int
smtp_readline (smtp_t smtp)
{
size_t n = 0;
size_t total = smtp->ptr - smtp->buffer;
int status;
/* Must get a full line before bailing out. */
do
{
status = bio_readline (smtp->bio, smtp->buffer + total,
smtp->buflen - total, &n);
if (status != 0)
return status;
total += n;
smtp->nl = memchr (smtp->buffer, '\n', total);
if (smtp->nl == NULL) /* Do we have a full line. */
{
/* Allocate a bigger buffer ? */
if (total >= smtp->buflen -1)
{
smtp->buflen *= 2;
smtp->buffer = realloc (smtp->buffer, smtp->buflen + 1);
if (smtp->buffer == NULL)
return ENOMEM;
}
}
smtp->ptr = smtp->buffer + total;
}
while (smtp->nl == NULL);
/* \r\n --> \n\0 */
if (smtp->nl > smtp->buffer)
{
*(smtp->nl - 1) = '\n';
*(smtp->nl) = '\0';
smtp->ptr = smtp->nl;
}
return 0;
}