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

« back to all changes in this revision

Viewing changes to protocols/mysql.c

  • Committer: Bazaar Package Importer
  • Author(s): Steinar H. Gunderson
  • Date: 2006-11-13 13:38:47 UTC
  • mfrom: (3.1.2 edgy)
  • Revision ID: james.westby@ubuntu.com-20061113133847-s552y2xbe1tiiqgy
Tags: 1:4.8.1-2.1
* Non-maintainer upload.
* monit-lfs.patch: Add LFS support; patch adapted from upstream CVS by
  Michael Mende. (Closes: #395164)
* Build-depend on automake and set some magical cdbs option to enable
  re-automaking, as monit-lfs.patch touches configure.ac.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C), 2000-2006 by the monit project group.
 
3
 * All Rights Reserved.
 
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
 * 
 
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
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#ifdef HAVE_STDIO_H
 
23
#include <stdio.h>
 
24
#endif
 
25
 
 
26
#ifdef HAVE_ERRNO_H
 
27
#include <errno.h>
 
28
#endif
 
29
 
 
30
#ifdef HAVE_STRING_H
 
31
#include <string.h>
 
32
#endif
 
33
 
 
34
#include "protocol.h"
 
35
 
 
36
/**
 
37
 *  Simple MySQL test.
 
38
 *
 
39
 *  In the case that the anonymous login is possible,
 
40
 *  we will perform MySQL ping. If authentication failed
 
41
 *  we suppose the anonymous login is denied and we will
 
42
 *  return success, because the server at least performed
 
43
 *  authentication => it seems it works.
 
44
 *
 
45
 *  @author Martin Pala, <martinp@tildeslash.com>
 
46
 *
 
47
 *  @version \$Id: mysql.c,v 1.9 2006/04/27 20:16:03 martinp Exp $
 
48
 *
 
49
 *  @file
 
50
 */
 
51
int check_mysql(Socket_T s) {
 
52
 
 
53
  unsigned char buf[STRLEN];
 
54
 
 
55
  unsigned char requestLogin[10] = {
 
56
    0x06,                                 /** Packet Length */
 
57
    0x00,
 
58
    0x00,
 
59
 
 
60
    0x01,                                 /** Packet Number */
 
61
 
 
62
    0x00,                                         /** Flags */
 
63
 
 
64
    0x00,                                    /** Max Packet */
 
65
    0x00,
 
66
    0x00,
 
67
 
 
68
    0x00,                                       /** Username*/
 
69
 
 
70
    0x00                                        /** Password*/
 
71
  };
 
72
 
 
73
  unsigned char requestPing[5] = {
 
74
    0x01,                                 /** Packet Length */
 
75
    0x00,
 
76
    0x00,
 
77
 
 
78
    0x00,                                 /** Packet Number */
 
79
 
 
80
    0x0E                                   /** Command Ping */
 
81
  };
 
82
 
 
83
  unsigned char responsePing[5] = {
 
84
    0x03,                                 /** Packet Length */
 
85
    0x00,
 
86
    0x00,
 
87
 
 
88
    0x01,                                 /** Packet Number */
 
89
 
 
90
    0x00                               /** Response Code OK */
 
91
 
 
92
                                        /** Padding Ignored */
 
93
  };
 
94
 
 
95
  unsigned char requestQuit[5] = {
 
96
    0x01,                                 /** Packet Length */
 
97
    0x00,
 
98
    0x00,
 
99
 
 
100
    0x00,                                 /** Packet Number */
 
101
 
 
102
    0x01                                   /** Command Quit */
 
103
  };
 
104
 
 
105
  ASSERT(s);
 
106
 
 
107
  if(!socket_readln(s, (char *)buf, sizeof(buf))) {
 
108
    LogError("MYSQL: error receiving greeting -- %s\n", STRERROR);
 
109
    return FALSE;
 
110
  }
 
111
 
 
112
  if(socket_write(s, requestLogin, sizeof(requestLogin)) < 0) {
 
113
    LogError("MYSQL: error sending login -- %s\n", STRERROR);
 
114
    return FALSE;
 
115
  }
 
116
 
 
117
  /* read just first few bytes  which contains enough information */
 
118
  if(socket_read(s, buf, 7) <= 6) {
 
119
    LogError("MYSQL: error receiving login response -- %s\n", STRERROR);
 
120
    return FALSE;
 
121
  }
 
122
 
 
123
  /* Compare Packet Number: */
 
124
  if(buf[3] != 0x02) {
 
125
    LogError("MYSQL: invalid response packet number\n");
 
126
    return FALSE;
 
127
  }
 
128
 
 
129
  /* Compare Response Code: */
 
130
 
 
131
  /* If OK, we are loged in and will perform MySQL ping */
 
132
  if(buf[4] == 0x00) {
 
133
    if(socket_write(s, (unsigned char *)requestPing, sizeof(requestPing)) < 0) {
 
134
      LogError("MYSQL: error sending ping -- %s\n", STRERROR);
 
135
      return FALSE;
 
136
    }
 
137
 
 
138
    if(socket_read(s, buf, sizeof(responsePing)) <= 0) {
 
139
      LogError("MYSQL: error receiving ping response -- %s\n", STRERROR);
 
140
      return FALSE;
 
141
    }
 
142
 
 
143
    if(memcmp((unsigned char *)buf,
 
144
              (unsigned char *)responsePing, sizeof(responsePing))) {
 
145
      LogError("MYSQL: ping failed\n");
 
146
      return FALSE;
 
147
    }
 
148
 
 
149
    if(socket_write(s, (unsigned char *)requestQuit, sizeof(requestQuit)) < 0) {
 
150
      LogError("MYSQL: error sending quit -- %s\n", STRERROR);
 
151
      return FALSE;
 
152
    }
 
153
 
 
154
    return TRUE;
 
155
  }
 
156
  /* If Authentication Failed, return success immediately */
 
157
  else if((buf[4] == 0xFF) && (buf[5] == 0x15 && buf[6] == 0x04)) {
 
158
    return TRUE;
 
159
  }
 
160
 
 
161
  LogError("MYSQL: login failed\n");
 
162
 
 
163
  return FALSE;
 
164
}
 
165