2
* Copyright (C), 2000-2006 by the monit project group.
2
* Copyright (C), 2000-2007 by the monit project group.
3
3
* All Rights Reserved.
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License as
7
* published by the Free Software Foundation; either version 2 of the
8
* License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software Foundation,
17
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
19
#include <config.h>
34
33
#include "protocol.h"
36
/* Private prototypes */
37
static int send(Socket_T s, char *msg);
38
static int expect(Socket_T s, int expect, int log);
37
* Check the server for greeting code 220 and then send QUIT and
38
* check for code 221. If alive return TRUE, else, return FALSE.
42
* Check the server for greeting code 220 and send EHLO. If that failed
43
* try HELO and test for return code 250 and finally send QUIT and check
44
* for return code 221. If alive return TRUE else return FALSE.
40
46
* @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
41
47
* @author Michael Amster, <mamster@webeasy.com>
43
* @version \$Id: smtp.c,v 1.27 2006/04/27 20:16:03 martinp Exp $
49
* @version \$Id: smtp.c,v 1.33 2007/07/25 12:54:33 hauk Exp $
47
53
int check_smtp(Socket_T s) {
57
if(!expect(s, 220, TRUE))
60
if(!(send(s, "EHLO localhost\r\n") && expect(s, 250, FALSE))) {
61
/* Try HELO also before giving up as of rfc2821 4.1.1.1 */
62
if(!(send(s, "HELO localhost\r\n") && expect(s, 250, TRUE)))
66
if(!(send(s, "QUIT\r\n") && expect(s, 221, TRUE)))
74
/* --------------------------------------------------------------- Private */
77
static int send(Socket_T s, char *msg) {
79
if(socket_write(s, msg, strlen(msg)) < 0) {
80
LogError("SMTP: error sending data -- %s\n", STRERROR);
89
static int expect(Socket_T s, int expect, int log) {
54
if(!socket_readln(s, buf, sizeof(buf))) {
55
LogError("SMTP: error receiving data -- %s\n", STRERROR);
61
sscanf(buf, "%d%*s", &status);
63
LogError("SMTP error: %s\n", buf);
67
/* Terminate the session */
68
if(socket_print(s, "QUIT\r\n") < 0) {
69
LogError("SMTP: error sending data -- %s\n", STRERROR);
74
if(!socket_readln(s, buf, sizeof(buf))) {
75
LogError("SMTP: error receiving data -- %s\n", STRERROR);
81
sscanf(buf, "%d%*s", &status);
83
LogError("SMTP error: %s\n", buf);
94
if(!socket_readln(s, buf, STRLEN)) {
95
LogError("SMTP: error receiving data -- %s\n", STRERROR);
101
sscanf(buf, "%d%*s", &status);
102
if(status != expect) {
104
LogError("SMTP error: %s\n", buf);