annotate src/checkwait_smtpreply.c @ 775:d0bf2135ab34

Check the server gives a reply before reading it to avoid segfaulting
author Ben Schmidt
date Fri, 12 Nov 2010 02:27:50 +1100
parents a38cde48669f
children 04d916168efb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
1 /* Copyright (C) 2002, 2003 Mads Martin Joergensen <mmj at mmj.dk>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
2 *
21ce01de8109 Initial revision
mmj
parents:
diff changeset
3 * $Id$
21ce01de8109 Initial revision
mmj
parents:
diff changeset
4 *
225
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
6 * of this software and associated documentation files (the "Software"), to
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
7 * deal in the Software without restriction, including without limitation the
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
9 * sell copies of the Software, and to permit persons to whom the Software is
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
10 * furnished to do so, subject to the following conditions:
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
11 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
12 * The above copyright notice and this permission notice shall be included in
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
13 * all copies or substantial portions of the Software.
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
14 *
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
3f177909efc8 Goodbye GPL license, Welcome MIT
mmj
parents: 180
diff changeset
21 * IN THE SOFTWARE.
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
22 */
21ce01de8109 Initial revision
mmj
parents:
diff changeset
23
21ce01de8109 Initial revision
mmj
parents:
diff changeset
24 #include <stdio.h>
21ce01de8109 Initial revision
mmj
parents:
diff changeset
25 #include <unistd.h>
110
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
26 #include <string.h>
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
27
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
28 #include "checkwait_smtpreply.h"
21ce01de8109 Initial revision
mmj
parents:
diff changeset
29 #include "config.h"
245
00eadc106b34 changed to use the new memory wrappers
mortenp
parents: 225
diff changeset
30 #include "memory.h"
591
29728cd732e7 Fixed EOF handling in checkwait_smtpreply()
mortenp
parents: 506
diff changeset
31 #include "mygetline.h"
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
32
110
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
33
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
34 char *checkwait_smtpreply(int sockfd, int replytype)
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
35 {
591
29728cd732e7 Fixed EOF handling in checkwait_smtpreply()
mortenp
parents: 506
diff changeset
36 char *smtpreply;
110
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
37
591
29728cd732e7 Fixed EOF handling in checkwait_smtpreply()
mortenp
parents: 506
diff changeset
38 smtpreply = mygetline(sockfd);
775
d0bf2135ab34 Check the server gives a reply before reading it to avoid segfaulting
Ben Schmidt
parents: 595
diff changeset
39 if(smtpreply == NULL) {
d0bf2135ab34 Check the server gives a reply before reading it to avoid segfaulting
Ben Schmidt
parents: 595
diff changeset
40 /* This will never be a valid SMTP response so will always be returned,
d0bf2135ab34 Check the server gives a reply before reading it to avoid segfaulting
Ben Schmidt
parents: 595
diff changeset
41 * but is more descriptive than an empty string. */
d0bf2135ab34 Check the server gives a reply before reading it to avoid segfaulting
Ben Schmidt
parents: 595
diff changeset
42 smtpreply = mystrdup("none / error / closed connection");
d0bf2135ab34 Check the server gives a reply before reading it to avoid segfaulting
Ben Schmidt
parents: 595
diff changeset
43 }
d0bf2135ab34 Check the server gives a reply before reading it to avoid segfaulting
Ben Schmidt
parents: 595
diff changeset
44
99
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 0
diff changeset
45 #if 0
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
46 printf("replytype = [%d], smtpreply = [%s]\n", replytype, smtpreply);
110
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
47 fprintf(stderr, "%s", smtpreply);
180
8b9850c25d94 Remove fprintf(stderr, "%s", smtpreply) from checkwait_smtpreply. No need to
mmj
parents: 110
diff changeset
48 #endif
99
6089f38bb228 Send our hostname in HELO and change the mlmmj-send output from
mmj
parents: 0
diff changeset
49
110
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
50 /* This case might seem like (and is ATM) total overkill. But it's
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
51 * easy for us to extend it later on if needed.
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
52 */
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
53 switch(replytype) {
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
54 case MLMMJ_CONNECT:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
55 if(smtpreply[0] != '2' || smtpreply[1] != '2')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
56 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
57 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
58 case MLMMJ_HELO:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
59 if(smtpreply[0] != '2' || smtpreply[1] != '5')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
60 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
61 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
62 case MLMMJ_FROM:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
63 if(smtpreply[0] != '2' || smtpreply[1] != '5')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
64 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
65 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
66 case MLMMJ_RCPTTO:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
67 if(smtpreply[0] != '2' || smtpreply[1] != '5')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
68 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
69 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
70 case MLMMJ_DATA:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
71 if(smtpreply[0] != '3' || smtpreply[1] != '5')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
72 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
73 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
74 case MLMMJ_DOT:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
75 if(smtpreply[0] != '2' || smtpreply[1] != '5')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
76 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
77 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
78 case MLMMJ_QUIT:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
79 if(smtpreply[0] != '2' || smtpreply[1] != '2')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
80 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
81 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
82 case MLMMJ_RSET:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
83 if(smtpreply[0] != '2' || smtpreply[1] != '5')
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
84 return smtpreply;
504
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
85 break;
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
86 default:
a07174f00a1b *** empty log message ***
mmj
parents: 426
diff changeset
87 break;
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
88 }
21ce01de8109 Initial revision
mmj
parents:
diff changeset
89
595
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
90 myfree(smtpreply);
a38cde48669f Fixed memory leak in checkwait_smtpreply()
mortenp
parents: 591
diff changeset
91
110
6bbaadfb1c43 Make checkwait_smtpreply return the string that it gets from the mailserver
mmj
parents: 107
diff changeset
92 return NULL;
0
21ce01de8109 Initial revision
mmj
parents:
diff changeset
93 }