~ubuntu-branches/ubuntu/jaunty/monit/jaunty

« back to all changes in this revision

Viewing changes to protocols/smtp.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Alfredsson
  • Date: 2008-08-24 23:44:46 UTC
  • mfrom: (4.1.1 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080824234446-a3k8n02f52nio5dy
Tags: 1:4.10.1-4
* Patch for config file location was not applied (Closes: #479357)
  (thanks to DVZ-Team <dv-zentrum@fh-giessen.de>)
* The relocation patch created an unneccessary "file.c.orig", this
  was removed from the patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
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.
4
4
 *
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.
9
 
 *
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.
14
 
 * 
 
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.
 
9
 *
 
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.
 
14
 *
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/>.
18
17
 */
19
18
 
20
19
#include <config.h>
33
32
 
34
33
#include "protocol.h"
35
34
 
 
35
 
 
36
/* Private prototypes */
 
37
static int send(Socket_T s, char *msg);
 
38
static int expect(Socket_T s, int expect, int log);
 
39
 
 
40
 
36
41
/**
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.
39
45
 *
40
46
 *  @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
41
47
 *  @author Michael Amster, <mamster@webeasy.com>
42
48
 *
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 $
44
50
 *
45
51
 *  @file
46
52
 */
47
53
int check_smtp(Socket_T s) {
 
54
    
 
55
  ASSERT(s);
 
56
  
 
57
  if(!expect(s, 220, TRUE))
 
58
    return FALSE;
 
59
  
 
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)))
 
63
      return FALSE;
 
64
  }
 
65
 
 
66
  if(!(send(s, "QUIT\r\n") && expect(s, 221, TRUE)))
 
67
    return FALSE;
 
68
    
 
69
  return TRUE;
 
70
  
 
71
}
 
72
 
 
73
 
 
74
/* --------------------------------------------------------------- Private */
 
75
 
 
76
 
 
77
static int send(Socket_T s, char *msg) {
 
78
  
 
79
  if(socket_write(s, msg, strlen(msg)) < 0) {
 
80
    LogError("SMTP: error sending data -- %s\n", STRERROR);
 
81
    return FALSE;
 
82
  }
 
83
  
 
84
  return TRUE;
 
85
  
 
86
}
 
87
 
 
88
 
 
89
static int expect(Socket_T s, int expect, int log) {
48
90
  
49
91
  int status;
50
92
  char buf[STRLEN];
51
 
  
52
 
  ASSERT(s);
53
 
  
54
 
  if(!socket_readln(s, buf, sizeof(buf))) {
55
 
    LogError("SMTP: error receiving data -- %s\n", STRERROR);
56
 
    return FALSE;
57
 
  }
58
 
  
59
 
  Util_chomp(buf);
60
 
  
61
 
  sscanf(buf, "%d%*s", &status);
62
 
  if(status != 220) {
63
 
    LogError("SMTP error: %s\n", buf);
64
 
    return FALSE;
65
 
  }
66
 
  
67
 
  /* Terminate the session */
68
 
  if(socket_print(s, "QUIT\r\n") < 0) {
69
 
    LogError("SMTP: error sending data -- %s\n", STRERROR);
70
 
    return FALSE;
71
 
  }
72
 
  
73
 
  /* Check the reply */
74
 
  if(!socket_readln(s, buf, sizeof(buf))) {
75
 
    LogError("SMTP: error receiving data -- %s\n", STRERROR);
76
 
    return FALSE;
77
 
  }
78
 
  
79
 
  Util_chomp(buf);
80
 
  
81
 
  sscanf(buf, "%d%*s", &status);
82
 
  if(status != 221) {
83
 
    LogError("SMTP error: %s\n", buf);
 
93
 
 
94
  if(!socket_readln(s, buf, STRLEN)) {
 
95
    LogError("SMTP: error receiving data -- %s\n", STRERROR);
 
96
    return FALSE;
 
97
  }
 
98
  
 
99
  Util_chomp(buf);
 
100
  
 
101
  sscanf(buf, "%d%*s", &status);
 
102
  if(status != expect) {
 
103
    if(log) 
 
104
      LogError("SMTP error: %s\n", buf);
84
105
    return FALSE;
85
106
  }
86
107