added .gitignore master
[mini_sendmail.git] / mini_sendmail.c
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/* mini_sendmail - accept email on behalf of real sendmail
**
** Copyright  1999 by Jef Poskanzer <jef@mail.acme.com>.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 1. Redistributions of source code must retain the above copyright
**    notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
**    notice, this list of conditions and the following disclaimer in the
**    documentation and/or other materials provided with the distribution.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
** SUCH DAMAGE.
*/
 
 
/* These defines control some optional features of the program.  If you
** don't want the features you can undef the symbols; some of them mean
** a significant savings in executable size.
*/
/* whether to add a "Received:" header */
#undef DO_RECEIVED
#define DO_GETPWUID     /* whether to try a getpwuid() if getlogin() fails */
#define DO_MINUS_SP     /* whether to implement the -s and -p flags */
#define DO_DNS          /* whether to do a name lookup on -s, or just IP# */
 
 
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
 
#ifdef DO_RECEIVED
#include <time.h>
#endif /* DO_RECEIVED */
#ifdef DO_GETPWUID
#include <pwd.h>
#endif /* DO_GETPWUID */
 
#include "version.h"
 
 
/* Defines. */
#define SMTP_PORT 25
#define DEFAULT_TIMEOUT 60
 
 
/* Globals. */
static char* argv0;
static char* fake_from;
static int parse_message, verbose;
#ifdef DO_MINUS_SP
static char* server;
static short port;
#endif /* DO_MINUS_SP */
static int timeout;
static int sockfd1, sockfd2;
static FILE* sockrfp;
static FILE* sockwfp;
static int got_a_recipient;
 
 
/* Forwards. */
static void usage( void );
static char* slurp_message( void );
#ifdef DO_RECEIVED
static char* make_received( char* from, char* username, char* hostname );
#endif /* DO_RECEIVED */
static void parse_for_recipients( char* message );
static void add_recipient( char* recipient, int len );
static int open_client_socket( void );
static int read_response( void );
static void send_command( char* command );
static void send_data( char* data );
static void send_done( void );
static void sigcatch( int sig );
static void show_error( char* cause );
 
 
int
main( int argc, char** argv )
    {
    int argn;
    char* message;
#ifdef DO_RECEIVED
    char* received;
#endif /* DO_RECEIVED */
    char* username;
    char hostname[500];
    char from[1000];
    int status;
    char buf[2000];
 
    /* Parse args. */
    argv0 = argv[0];
    fake_from = (char*) 0;
    parse_message = 0;
#ifdef DO_MINUS_SP
    server = "127.0.0.1";
    port = SMTP_PORT;
#endif /* DO_MINUS_SP */
    verbose = 0;
    timeout = DEFAULT_TIMEOUT;
    argn = 1;
    while ( argn < argc && argv[argn][0] == '-' )
        {
        if ( strncmp( argv[argn], "-f", 2 ) == 0 && argv[argn][2] != '\0' )
            fake_from = &(argv[argn][2]);
                else if ( strncmp( argv[argn], "-f", 2) == 0)
           fake_from = &(argv[++argn][0]);
        else if ( strcmp( argv[argn], "-t" ) == 0 )
            parse_message = 1;
#ifdef DO_MINUS_SP
        else if ( strncmp( argv[argn], "-s", 2 ) == 0 && argv[argn][2] != '\0' )
            server = &(argv[argn][2]);
        else if ( strncmp( argv[argn], "-p", 2 ) == 0 && argv[argn][2] != '\0' )
            port = atoi( &(argv[argn][2]) );
#endif /* DO_MINUS_SP */
        else if ( strncmp( argv[argn], "-T", 2 ) == 0 && argv[argn][2] != '\0' )
            timeout = atoi( &(argv[argn][2]) );
        else if ( strcmp( argv[argn], "-v" ) == 0 )
            verbose = 1;
        else if ( strcmp( argv[argn], "-i" ) == 0 )
            ;   /* ignore */
        else if ( strcmp( argv[argn], "-oi" ) == 0 )
            ;   /* ignore */
        else if ( strcmp( argv[argn], "--" ) == 0 )
            ;   /* ignore */
        else
            usage();
        ++argn;
        }
 
        if ( fake_from == (char*) 0 ) {
       username = getlogin();
       if ( username == (char*) 0 ) {
#ifdef DO_GETPWUID
        struct passwd* pw = getpwuid( getuid() );
           if ( pw == (struct passwd*) 0 ) {
               (void) fprintf( stderr, "%s: can't determine username\n", argv0 );
               exit( 1 );
            }
        username = pw->pw_name;
#else /* DO_GETPWUID */
        (void) fprintf( stderr, "%s: can't determine username\n", argv0 );
        exit( 1 );
#endif /* DO_GETPWUID */
        }
 
        (void) snprintf( from, sizeof(from), "%s@%s", username, hostname );
  } else {
        if ( strchr( fake_from, '@' ) == (char*) 0 )
                (void) snprintf( from, sizeof(from), "%s@%s", fake_from, hostname );
        else
            (void) snprintf( from, sizeof(from), "%s", fake_from );
        }
 
    if ( gethostname( hostname, sizeof(hostname) - 1 ) < 0 )
       show_error( "gethostname" );
 
 
    /* Strip off any angle brackets in the from address. */
    while ( from[0] == '<' )
        (void) strcpy( from, &from[1] );
    while ( from[strlen(from)-1] == '>' )
        from[strlen(from)-1] = '\0';
 
    message = slurp_message();
#ifdef DO_RECEIVED
    received = make_received( from, username, hostname );
#endif /* DO_RECEIVED */
 
    (void) signal( SIGALRM, sigcatch );
 
    (void) alarm( timeout );
    sockfd1 = open_client_socket();
 
    sockfd2 = dup( sockfd1 );
    sockrfp = fdopen( sockfd1, "r" );
    sockwfp = fdopen( sockfd2, "w" );
 
    /* The full SMTP protocol is spelled out in RFC821, available at
    ** http://www.faqs.org/rfcs/rfc821.html
    ** The only non-obvious wrinkles:
    **  - The commands are terminated with CRLF, not newline.
    **  - Newlines in the data file get turned into CRLFs.
    **  - Any data lines beginning with a period get an extra period prepended.
    */
 
    status = read_response();
    if ( status != 220 )
        {
        (void) fprintf(
            stderr,  "%s: unexpected initial greeting %d\n", argv0, status );
        exit( 1 );
        }
 
    (void) snprintf( buf, sizeof(buf), "HELO %s", hostname );
    send_command( buf );
    status = read_response();
    if ( status != 250 )
        {
        (void) fprintf(
            stderr,  "%s: unexpected response %d to HELO command\n",
            argv0, status );
        exit( 1 );
        }
 
    (void) snprintf( buf, sizeof(buf), "MAIL FROM:<%s>", from );
    send_command( buf );
    status = read_response();
    if ( status != 250 )
        {
        (void) fprintf(
            stderr,  "%s: unexpected response %d to MAIL FROM command\n",
            argv0, status );
        exit( 1 );
        }
 
    got_a_recipient = 0;
    for ( ; argn < argc; ++argn )
        add_recipient( argv[argn], strlen( argv[argn] ) );
    if ( parse_message )
        parse_for_recipients( message );
    if ( ! got_a_recipient )
        {
        (void) fprintf( stderr,  "%s: no recipients found\n", argv0 );
        exit( 1 );
        }
 
    send_command( "DATA" );
    status = read_response();
    if ( status != 354 )
        {
        (void) fprintf(
            stderr,  "%s: unexpected response %d to DATA command\n",
            argv0, status );
        exit( 1 );
        }
 
#ifdef DO_RECEIVED
    send_data( received );
#endif /* DO_RECEIVED */
    send_data( message );
    send_done();
    status = read_response();
    if ( status != 250 )
        {
        (void) fprintf(
            stderr,  "%s: unexpected response %d to DATA\n", argv0, status );
        exit( 1 );
        }
 
    send_command( "QUIT" );
    status = read_response();
    if ( status != 221 )
        (void) fprintf(
            stderr,  "%s: unexpected response %d to QUIT command - ignored\n",
            argv0, status );
 
    (void) close( sockfd1 );
    (void) close( sockfd2 );
 
    exit( 0 );
    }
 
 
static void
usage( void )
    {
#ifdef DO_MINUS_SP
#ifdef DO_DNS
    char* spflag = "[-s<server>] [-p<port>] ";
#else /* DO_DNS */
    char* spflag = "[-s<server_ip>] [-p<port>] ";
#endif /* DO_DNS */
#else /* DO_MINUS_SP */
    char* spflag = "";
#endif /* DO_MINUS_SP */
    (void) fprintf( stderr, "usage:  %s [-f<name>] [-t] %s[-T<timeout>] [-v] [address ...]\n", argv0, spflag );
    exit( 1 );
    }
 
 
static char*
slurp_message( void )
    {
    char* message;
    int message_size, message_len;
    int c;
 
    message_size = 5000;
    message = (char*) malloc( message_size );
    if ( message == (char*) 0 )
        {
        (void) fprintf( stderr, "%s: out of memory\n", argv0 );
        exit( 1 );
        }
    message_len = 0;
 
    for (;;)
        {
        c = getchar();
        if ( c == EOF )
            break;
        if ( message_len + 1 >= message_size )
            {
            message_size *= 2;
            message = (char*) realloc( (void*) message, message_size );
            if ( message == (char*) 0 )
                {
                (void) fprintf( stderr, "%s: out of memory\n", argv0 );
                exit( 1 );
                }
            }
        message[message_len++] = c;
        }
    message[message_len] = '\0';
 
    return message;
    }
 
 
#ifdef DO_RECEIVED
static char*
make_received( char* from, char* username, char* hostname )
    {
    int received_size;
    char* received;
    time_t t;
    struct tm* tmP;
    char timestamp[100];
 
    t = time( (time_t*) 0 );
    tmP = localtime( &t );
    (void) strftime( timestamp, sizeof(timestamp), "%a, %d %b %Y %T %Z", tmP );
    received_size =
        500 + strlen( from ) + strlen( hostname ) * 2 + strlen( VERSION ) +
        strlen( timestamp ) + strlen( username );
    received = (char*) malloc( received_size );
    if ( received == (char*) 0 )
        {
        (void) fprintf( stderr, "%s: out of memory\n", argv0 );
        exit( 1 );
        }
    (void) snprintf(
        received, received_size,
        "Received: (from %s)\n\tby %s (%s);\n\t%s\n\t(sender %s@%s)\n",
        from, hostname, VERSION, timestamp, username, hostname );
    return received;
    }
#endif /* DO_RECEIVED */
 
 
static void
parse_for_recipients( char* message )
    {
    /* A little finite-state machine that parses the message, looking
    ** for To:, Cc:, and Bcc: recipients.
    */
    int state;
#define ST_BOL          0
#define ST_OTHERHEAD    1
#define ST_T            2
#define ST_C            3
#define ST_B            4
#define ST_BC           5
#define ST_RECIPHEAD    6
#define ST_RECIPS       7
    char* cp;
    char* bcc;
    char* recip;
 
    state = ST_BOL;
    bcc = (char*) 0;
    for ( cp = message; *cp != '\0'; ++cp )
        {
        switch ( state )
            {
            case ST_BOL:
            switch ( *cp )
                {
                case '\n':
                return;
                case 'T':
                case 't':
                state = ST_T;
                break;
                case 'C':
                case 'c':
                state = ST_C;
                break;
                case 'B':
                case 'b':
                state = ST_B;
                bcc = cp;
                break;
                default:
                state = ST_OTHERHEAD;
                break;
                }
            break;
            case ST_OTHERHEAD:
            switch ( *cp )
                {
                case '\n':
                state = ST_BOL;
                break;
                }
            break;
            case ST_T:
            switch ( *cp )
                {
                case '\n':
                state = ST_BOL;
                break;
                case 'O':
                case 'o':
                state = ST_RECIPHEAD;
                break;
                default:
                state = ST_OTHERHEAD;
                break;
                }
            break;
            case ST_C:
            switch ( *cp )
                {
                case '\n':
                state = ST_BOL;
                break;
                case 'C':
                case 'c':
                state = ST_RECIPHEAD;
                break;
                default:
                state = ST_OTHERHEAD;
                break;
                }
            break;
            case ST_B:
            switch ( *cp )
                {
                case '\n':
                state = ST_BOL;
                bcc = (char*) 0;
                break;
                case 'C':
                case 'c':
                state = ST_BC;
                break;
                default:
                state = ST_OTHERHEAD;
                bcc = (char*) 0;
                break;
                }
            break;
            case ST_BC:
            switch ( *cp )
                {
                case '\n':
                state = ST_BOL;
                bcc = (char*) 0;
                break;
                case 'C':
                case 'c':
                state = ST_RECIPHEAD;
                break;
                default:
                state = ST_OTHERHEAD;
                bcc = (char*) 0;
                break;
                }
            break;
            case ST_RECIPHEAD:
            switch ( *cp )
                {
                case '\n':
                state = ST_BOL;
                bcc = (char*) 0;
                break;
                case ':':
                state = ST_RECIPS;
                recip = cp + 1;
                break;
                default:
                state = ST_OTHERHEAD;
                bcc = (char*) 0;
                break;
                }
            break;
            case ST_RECIPS:
            switch ( *cp )
                {
                case '\n':
                add_recipient( recip, ( cp - recip ) );
                state = ST_BOL;
                if ( bcc != (char*) 0 )
                    {
                    /* Elide the Bcc: line, and reset cp. */
                    (void) strcpy( bcc, cp + 1 );
                    cp = bcc - 1;
                    bcc = (char*) 0;
                    }
                break;
                case ',':
                add_recipient( recip, ( cp - recip ) );
                recip = cp + 1;
                break;
                }
            break;
            }
        }
    }
 
 
static void
add_recipient( char* recipient, int len )
    {
    char buf[1000];
    int status;
 
    /* Skip leading whitespace. */
    while ( len > 0 && ( *recipient == ' ' || *recipient == '\t' ) )
        {
        ++recipient;
        --len;
        }
 
    /* Strip off any angle brackets. */
    while ( len > 0 && *recipient == '<' )
        {
        ++recipient;
        --len;
        }
    while ( len > 0 && recipient[len-1] == '>' )
        --len;
 
    (void) snprintf( buf, sizeof(buf), "RCPT TO:<%.*s>", len, recipient );
    send_command( buf );
    status = read_response();
    if ( status != 250  && status != 251 )
        {
        (void) fprintf(
            stderr,  "%s: unexpected response %d to RCPT TO command\n",
            argv0, status );
        exit( 1 );
        }
    got_a_recipient = 1;
    }
 
 
#if defined(AF_INET6) && defined(IN6_IS_ADDR_V4MAPPED)
#define USE_IPV6
#endif
 
static int
open_client_socket( void )
    {
#ifdef USE_IPV6
    struct sockaddr_in6 sa;
#else /* USE_IPV6 */
    struct sockaddr_in sa;
#endif /* USE_IPV6 */
    int sa_len, sock_family, sock_type, sock_protocol;
    int sockfd;
 
    sock_type = SOCK_STREAM;
    sock_protocol = 0;
    sa_len = sizeof(sa);
    (void) memset( (void*) &sa, 0, sa_len );
 
#ifdef USE_IPV6
 
    {
#ifdef DO_MINUS_SP
    struct sockaddr_in sa4;
    struct addrinfo hints;
    char portstr[10];
    int gaierr;
    struct addrinfo* ai;
    struct addrinfo* ai2;
    struct addrinfo* aiv4;
    struct addrinfo* aiv6;
#endif /* DO_MINUS_SP */
 
    sock_family = PF_INET6;
 
#ifdef DO_MINUS_SP
    (void) memset( (void*) &sa4, 0, sizeof(sa4) );
    if ( inet_pton( AF_INET, server, (void*) &sa4.sin_addr ) == 1 )
        {
        sock_family = PF_INET;
        sa4.sin_port = htons( port );
        sa_len = sizeof(sa4);
        (void) memmove( &sa, &sa4, sa_len );
        }
    else if ( inet_pton( AF_INET6, server, (void*) &sa.sin6_addr ) != 1 )
        {
#ifdef DO_DNS
        (void) memset( &hints, 0, sizeof(hints) );
        hints.ai_family = PF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        (void) snprintf( portstr, sizeof(portstr), "%d", port );
        if ( (gaierr = getaddrinfo( server, portstr, &hints, &ai )) != 0 )
            {
            (void) fprintf(
                stderr, "%s: getaddrinfo %s - %s\n", argv0, server,
                gai_strerror( gaierr ) );
            exit( 1 );
            }
 
        /* Find the first IPv4 and IPv6 entries. */
        aiv4 = (struct addrinfo*) 0;
        aiv6 = (struct addrinfo*) 0;
        for ( ai2 = ai; ai2 != (struct addrinfo*) 0; ai2 = ai2->ai_next )
            {
            switch ( ai2->ai_family )
                {
                case PF_INET:
                if ( aiv4 == (struct addrinfo*) 0 )
                    aiv4 = ai2;
                break;
                case PF_INET6:
                if ( aiv6 == (struct addrinfo*) 0 )
                    aiv6 = ai2;
                break;
                }
            }
 
        /* If there's an IPv4 address, use that, otherwise try IPv6. */
        if ( aiv4 != (struct addrinfo*) 0 )
            {
            if ( sizeof(sa) < aiv4->ai_addrlen )
                {
                (void) fprintf(
                    stderr, "%s - sockaddr too small (%lu < %lu)\n",
                    server, (unsigned long) sizeof(sa),
                    (unsigned long) aiv4->ai_addrlen );
                exit( 1 );
                }
            sock_family = aiv4->ai_family;
            sock_type = aiv4->ai_socktype;
            sock_protocol = aiv4->ai_protocol;
            sa_len = aiv4->ai_addrlen;
            (void) memmove( &sa, aiv4->ai_addr, sa_len );
            goto ok;
            }
        if ( aiv6 != (struct addrinfo*) 0 )
            {
            if ( sizeof(sa) < aiv6->ai_addrlen )
                {
                (void) fprintf(
                    stderr, "%s - sockaddr too small (%lu < %lu)\n",
                    server, (unsigned long) sizeof(sa),
                    (unsigned long) aiv6->ai_addrlen );
                exit( 1 );
                }
            sock_family = aiv6->ai_family;
            sock_type = aiv6->ai_socktype;
            sock_protocol = aiv6->ai_protocol;
            sa_len = aiv6->ai_addrlen;
            (void) memmove( &sa, aiv6->ai_addr, sa_len );
            goto ok;
            }
 
        (void) fprintf(
            stderr, "%s: no valid address found for host %s\n", argv0, server );
        exit( 1 );
 
        ok:
        freeaddrinfo( ai );
#else /* DO_DNS */
        (void) fprintf(
            stderr, "%s: bad server IP address %s\n", argv0, server );
        exit( 1 );
#endif /* DO_DNS */
        }
#else /* DO_MINUS_SP */
    sa.sin6_addr = in6addr_any;
    sa.sin6_port = htons( SMTP_PORT );
#endif /* DO_MINUS_SP */
 
    sa.sin6_family = sock_family;
 
    }
 
#else /* USE_IPV6 */
 
    {
#ifdef DO_MINUS_SP
    struct hostent *he;
#else /* DO_MINUS_SP */
    char local_addr[4] = { 127, 0, 0, 1 };
#endif /* DO_MINUS_SP */
 
    sock_family = PF_INET;
 
#ifdef DO_MINUS_SP
    sa.sin_addr.s_addr = inet_addr( server );
    sa.sin_port = htons( port );
    if ( (int32_t) sa.sin_addr.s_addr == -1 )
        {
#ifdef DO_DNS
        he = gethostbyname( server );
        if ( he == (struct hostent*) 0 )
            {
            (void) fprintf(
                stderr, "%s: server name lookup of '%s' failed - %s\n",
                argv0, server, hstrerror( h_errno ) );
            exit( 1 );
            }
        sock_family = he->h_addrtype;
        (void) memmove( &sa.sin_addr, he->h_addr, he->h_length );
#else /* DO_DNS */
        (void) fprintf(
            stderr, "%s: bad server IP address %s\n", argv0, server );
        exit( 1 );
#endif /* DO_DNS */
        }
#else /* DO_MINUS_SP */
    (void) memmove( &sa.sin_addr, local_addr, sizeof(local_addr) );
    sa.sin_port = htons( SMTP_PORT );
#endif /* DO_MINUS_SP */
 
    sa.sin_family = sock_family;
    }
 
#endif /* USE_IPV6 */
 
    sockfd = socket( sock_family, sock_type, sock_protocol );
    if ( sockfd < 0 )
        show_error( "socket" );
 
    if ( connect( sockfd, (struct sockaddr*) &sa, sa_len ) < 0 )
        show_error( "connect" );
 
    return sockfd;
    }
 
 
static int
read_response( void )
    {
    char buf[10000];
    char* cp;
    int status;
 
    for (;;)
        {
        (void) alarm( timeout );
        if ( fgets( buf, sizeof(buf), sockrfp ) == (char*) 0 )
            {
            (void) fprintf( stderr, "%s: unexpected EOF\n", argv0 );
            exit( 1 );
            }
        if ( verbose )
            (void) fprintf( stderr, "<<<< %s", buf );
        for ( status = 0, cp = buf; *cp >= '0' && *cp <= '9'; ++cp )
            status = 10 * status + ( *cp - '0' );
        if ( *cp == ' ' )
            break;
        if ( *cp != '-' )
            {
            (void) fprintf(
                stderr,  "%s: bogus reply syntax - '%s'\n", argv0, buf );
            exit( 1 );
            }
        }
    return status;
    }
 
 
static void
send_command( char* command )
    {
    (void) alarm( timeout );
    if ( verbose )
        (void) fprintf( stderr, ">>>> %s\n", command );
    (void) fprintf( sockwfp, "%s\r\n", command );
    (void) fflush( sockwfp );
    }
 
 
static void
send_data( char* data )
    {
    int bol;
 
    for ( bol = 1; *data != '\0'; ++data )
        {
        if ( bol && *data == '.' )
            putc( '.', sockwfp );
        bol = 0;
        if ( *data == '\n' )
            {
            putc( '\r', sockwfp );
            bol = 1;
            }
        putc( *data, sockwfp );
        }
    if ( ! bol )
        (void) fputs( "\r\n", sockwfp );
    }
 
 
static void
send_done( void )
    {
    (void) fputs( ".\r\n", sockwfp );
    (void) fflush( sockwfp );
    }
 
 
static void
sigcatch( int sig )
    {
    (void) fprintf( stderr, "%s: timed out\n", argv0 );
    exit( 1 );
    }
 
 
static void
show_error( char* cause )
    {
    char buf[5000];
 
    (void) snprintf( buf, sizeof(buf), "%s: %s", argv0, cause );
    perror( buf );
    exit( 1 );
    }
 
comments