示例#1
0
文件: sendmail.c 项目: Nejuf/monit
void do_send(SendMail_T *S, const char *s, ...) {
        va_list ap;
        va_start(ap,s);
        char *msg = Str_vcat(s, ap);
        va_end(ap);
        int rv = Socket_write(S->socket, msg, strlen(msg));
        FREE(msg);
        if (rv <= 0)
                THROW(IOException, "Error sending data to the server '%s' -- %s", S->server, STRERROR);
}
示例#2
0
文件: SMTP.c 项目: GaiaMagic/monit
static void _send(T S, const char *data, ...) {
    va_list ap;
    va_start(ap, data);
    char *msg = Str_vcat(data, ap);
    va_end(ap);
    int rv = Socket_write(S->socket, msg, strlen(msg));
    FREE(msg);
    if (rv <= 0)
        THROW(IOException, "Error sending data to the mailserver -- %s", STRERROR);
}
示例#3
0
void check_tns(Socket_T socket) {

        unsigned char  buf[STRLEN];

        unsigned char requestPing[] = {
                0x00, 0x57,                           /** Packet Length */
                0x00, 0x00,                           /** Packet Checksum */
                0x01,                                 /** Packet Type: CONNECT */
                0x00,                                 /** Reserved */
                0x00, 0x00,                           /** Header Checksum */
                0x01, 0x36,                           /** Version */
                0x01, 0x2c,                           /** Compatible */
                0x00, 0x00,                           /** Service Options */
                0x08, 0x00,                           /** Session Data Unit Size */
                0x7f, 0xff,                           /** Maximum Transmission Data Unit Size */
                0xa3, 0x0a,                           /** NT Protocol Characteristics */
                0x00, 0x00,                           /** Line Turnaround Value */
                0x01, 0x00,                           /** Value of 1 in Hardware */
                0x00, 0x1d,                           /** Length of Connect Data */
                0x00, 0x3a,                           /** Offset of Connect Data */
                0x00, 0x00, 0x00, 0x00,               /** Maximum Receivable Connect Data */
                0x00,                                 /** Connect flags 0 */
                0x00,                                 /** Connect flags 1 */
                0x00, 0x00, 0x00, 0x00,               /** Trace Cross Facility Item 1 */
                0x00, 0x00, 0x00, 0x00,               /** Trace Cross Facility Item 2 */
                0x00, 0x00, 0x0b, 0x1c,               /** Trace Unique Connection ID */
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x28, 0x43, 0x4f, 0x4e,               /** Connect Data */
                0x4e, 0x45, 0x43, 0x54,               /** (CONNECT_DATA=(COMMAND=ping)) */
                0x5f, 0x44, 0x41, 0x54,
                0x41, 0x3d, 0x28, 0x43,
                0x4f, 0x4d, 0x4d, 0x41,
                0x4e, 0x44, 0x3d, 0x70,
                0x69, 0x6e, 0x67, 0x29,
                0x29
        };

        ASSERT(socket);

        if (Socket_write(socket, (unsigned char *)requestPing, sizeof(requestPing)) < 0)
                THROW(IOException, "TNS: error sending ping -- %s", STRERROR);

        /* read just first few bytes which contains enough information */
        if (Socket_read(socket, (unsigned char *)buf, 5) < 5)
                THROW(IOException, "TNS: error receiving ping response -- %s", STRERROR);

        /* compare packet type */
        if (buf[4] != TNS_TYPE_REFUSED)
                THROW(IOException, "TNS: invalid ping response");
}
示例#4
0
/**
 * int Frame_send(Frame_T, Socket_T, const char* uint16_t)
 * @param frame: To serialize and Send
 * @param socket: use to send
 * @param ip, port: Send to
 * @return: @see Socket_write(), same return value
 **/
int Frame_send(Frame_T frame, Socket_T socket, const char* ip, uint16_t port) {
  uint8_t* buf;
  assert(frame != NULL);
  assert(socket != NULL);

  buf = calloc(HEADER + frame->sHeader.length, sizeof(char));
  if (buf == NULL) return -1;

  memcpy(buf, &(frame->sHeader), HEADER);
  memcpy(buf + HEADER, frame->payload, frame->sHeader.length);

  return Socket_write(socket, ip, port, buf, HEADER + frame->sHeader.length);
}
示例#5
0
static bool
sendBuffer(CotpConnection* self)
{
    int writeBufferPosition = ByteBuffer_getSize(self->writeBuffer);

    bool retVal = false;

    if (Socket_write(self->socket, ByteBuffer_getBuffer(self->writeBuffer), writeBufferPosition) == writeBufferPosition)
        retVal = true;

    ByteBuffer_setSize(self->writeBuffer, 0);

    return retVal;
}
示例#6
0
int
ByteStream_sendBuffer(ByteStream self)
{
    int writeBufferPosition = ByteBuffer_getSize(self->writeBuffer);

    if (Socket_write(self->socket, ByteBuffer_getBuffer(self->writeBuffer), writeBufferPosition)
            == writeBufferPosition)
    {
        ByteBuffer_setSize(self->writeBuffer, 0);
        return 1;
    }
    else
        return -1;
}
示例#7
0
/**
 *  A simple 'SSH protocol version exchange' implemetation based on RFC (http://www.openssh.com/txt/draft-ietf-secsh-transport-14.txt)
 *
 *  @file
 */
void check_ssh(Socket_T socket) {

        char  buf[STRLEN];

        ASSERT(socket);

        if (! Socket_readLine(socket, buf, sizeof(buf)))
                THROW(IOException, "SSH: error receiving identification string -- %s", STRERROR);

        if (! Str_startsWith(buf, "SSH-"))
                THROW(IOException, "SSH: protocol error %s", buf);

        /* send identification string back to server */
        if (Socket_write(socket, buf, strlen(buf)) <= 0)
                THROW(IOException, "SSH: error sending identification string -- %s", STRERROR);

        /* Read one extra line to prevent the "Read from socket failed" warning */
        Socket_readLine(socket, buf, sizeof(buf));
}
示例#8
0
/**
 *  Generic service test.
 *
 *  @file
 */
void check_generic(Socket_T socket) {
        ASSERT(socket);

        Generic_T g = NULL;
        if (Socket_getPort(socket))
                g = ((Port_T)(Socket_getPort(socket)))->parameters.generic.sendexpect;

        char *buf = CALLOC(sizeof(char), Run.limits.sendExpectBuffer + 1);

        while (g != NULL) {

                if (g->send != NULL) {
                        /* Unescape any \0x00 escaped chars in g's send string to allow sending a string containing \0 bytes also */
                        char *X = Str_dup(g->send);
                        int l = Util_handle0Escapes(X);

                        if (Socket_write(socket, X, l) < 0) {
                                FREE(X);
                                FREE(buf);
                                THROW(IOException, "GENERIC: error sending data -- %s", STRERROR);
                        } else {
                                DEBUG("GENERIC: successfully sent: '%s'\n", g->send);
                        }
                        FREE(X);
                } else if (g->expect != NULL) {
                        /* Since the protocol is unknown we need to wait on EOF. To avoid waiting
                         timeout seconds on EOF we first read one byte to fill the socket's read
                         buffer and then set a low timeout on next read which reads remaining bytes
                         as well as wait on EOF */
                        int first_byte = Socket_readByte(socket);
                        if (first_byte < 0) {
                                FREE(buf);
                                THROW(IOException, "GENERIC: error receiving data -- %s", STRERROR);
                        }
                        *buf = first_byte;

                        int timeout = Socket_getTimeout(socket);
                        Socket_setTimeout(socket, 200);
                        int n = Socket_read(socket, buf + 1, Run.limits.sendExpectBuffer - 1) + 1;
                        buf[n] = 0;
                        if (n > 0)
                                _escapeZeroInExpectBuffer(buf, n);
                        Socket_setTimeout(socket, timeout); // Reset back original timeout for next send/expect
#ifdef HAVE_REGEX_H
                        int regex_return = regexec(g->expect, buf, 0, NULL, 0);
                        if (regex_return != 0) {
                                char e[STRLEN];
                                regerror(regex_return, g->expect, e, STRLEN);
                                FREE(buf);
                                THROW(IOException, "GENERIC: received unexpected data -- %s", e);
                        } else {
                                DEBUG("GENERIC: successfully received: '%s'\n", Str_trunc(buf, STRLEN - 4));
                        }

#else
                        /* w/o regex support */

                        if (strncmp(buf, g->expect, strlen(g->expect)) != 0) {
                                FREE(buf);
                                THROW(IOException, "GENERIC: received unexpected data");
                        } else {
                                DEBUG("GENERIC: successfully received: '%s'\n", Str_trunc(buf, STRLEN - 4));
                        }

#endif

                } else {
                        /* This should not happen */
                        FREE(buf);
                        THROW(IOException, "GENERIC: unexpected strangeness");
                }
                g = g->next;
        }
        FREE(buf);
}
示例#9
0
/**
 *  PostgreSQL test.
 *
 *  @file
 */
void check_pgsql(Socket_T socket) {

        unsigned char buf[STRLEN];

        unsigned char requestLogin[33] = {
                0x00,                              /** Length */
                0x00,
                0x00,
                0x21,

                0x00,                              /** ProtoVer 3.0 */
                0x03,
                0x00,
                0x00,

                0x75, 0x73, 0x65, 0x72, 0x00,      /** user */
                0x72, 0x6f, 0x6f, 0x74, 0x00,      /** root */

                0x64, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x00,  /** database */
                0x72, 0x6f, 0x6f, 0x74, 0x00,                          /** root */

                0x00
        };

        /** Doing this is too suspicious maybe.
         * Type Q, Length 19 and QUERY select 1 as a; */
        /**
         unsigned char requestQuery[20] = {
         0x51,

         0x00,
         0x00,
         0x00,
         0x13,

         0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x20,
         0x31, 0x20, 0x61, 0x73, 0x20, 0x61, 0x3b,

         0x00
         };
         */

        unsigned char requestTerm[5] = {
                0x58,                              /** Type X */

                0x00,                              /** Length */
                0x00,
                0x00,
                0x04
        };

        unsigned char responseAuthOk[9] = {
                0x52,                              /** Type R */

                0x00,                              /** Length */
                0x00,
                0x00,
                0x08,

                0x00,                              /** OK code 0 */
                0x00,
                0x00,
                0x00
        };

        ASSERT(socket);

        if (Socket_write(socket, (unsigned char *)requestLogin, sizeof(requestLogin)) <= 0)
                THROW(IOException, "PGSQL: error sending data -- %s", STRERROR);

        /** Nine-byte is enough to hold Auth-Ok */
        if (Socket_read(socket, buf, 9) <= 0)
                THROW(IOException, "PGSQL: error receiving data -- %s", STRERROR);

        /** If server insists on auth error it is working anyway */
        if (*buf == 'E')
                return;

        /** Successful connection */
        if (! memcmp((unsigned char *)buf, (unsigned char *)responseAuthOk, 9)) {
                /** This is where suspicious people can do SELECT query that I dont */
                Socket_write(socket, (unsigned char *)requestTerm, sizeof(requestTerm));
                return;
        }

        /** The last possibility must be that server is demanding password */
        if (*buf == 'R')
                return;

        THROW(IOException, "PGSQL: unknown error");
}
示例#10
0
// Set payload length and send the request to the server
static void _sendRequest(mysql_t *mysql) {
        mysql->request.len = (uint32_t)(mysql->request.cursor - mysql->request.buf);
        // Send request
        if (Socket_write(mysql->socket, &mysql->request, mysql->request.len + 4) < 0) // Note: mysql->request.len value is just payload size + need to add 4 bytes for the header itself (len + seq)
                THROW(IOException, "Cannot send handshake response -- %s\n", STRERROR);
}
示例#11
0
/**
 *  Simple LDAPv3 protocol test.
 *
 *  Try anonymous bind to the server.
 *
 *  BindRequest based on RFC2251. Request and response are ASN.1
 *  BER encoded strings. To make the test as simple as possible
 *  we work with BER encoded data.
 *
 *  The test checks only if the bind was successfull - in the
 *  case of failure it don't provide any erroneous message
 *  analysis.
 *
 *  @file
 */
void check_ldap3(Socket_T socket) {

        unsigned char buf[STRLEN];

        unsigned char request[14] = {
                0x30,                         /** Universal Sequence TAG */
                0x0c,               /** Length of the packet's data part */

                0x02,                          /** Universal Integer TAG */
                0x01,                                    /** Integer length */
                0x00,                                         /** MessageID */

                0x60,                    /** Application BindRequest TAG */
                0x07,                        /** Length of the data part */

                0x02,                         /** Universal Integer TAG */
                0x01,                                 /** Integer length */
                0x03,                               /** Protocol version */

                0x04,                    /** Universal Octet string TAG */
                0x00,                                  /** Octet string length */
                /* NULL */                                 /** Anonymous BindDN */

                0x80,                /** Context specific SimpleAuth TAG */
                0x00                    /** SimpleAuth (octet string) length */
                /* NULL */                            /** Anonymous Credentials */
        };

        unsigned char response[14] = {
                0x30,                         /** Universal Sequence TAG */
                0x0c,               /** Length of the packet's data part */

                0x02,                          /** Universal Integer TAG */
                0x01,                                    /** Integer length */
                0x00,                                         /** MessageID */

                0x61,                   /** Application BindResponse TAG */
                0x07,                        /** Length of the data part */

                0x0a,                      /** Universal Enumerated TAG */
                0x01,                              /** Enumerated length */
                0x00,                                        /** Success */

                0x04,                    /** Universal Octet string TAG */
                0x00,                                  /** Octet string length */
                /* NULL */                                        /** MatchedDN */

                0x04,                    /** Universal Octet string TAG */
                0x00                                  /** Octet string length */
                /* NULL */                                     /** ErrorMessage */
        };

        unsigned char unbind[7] = {
                0x30,                         /** Universal Sequence TAG */
                0x05,               /** Length of the packet's data part */

                0x02,                          /** Universal Integer TAG */
                0x01,                                    /** Integer length */
                0x01,                                         /** MessageID */

                0x42,                  /** Application UnbindRequest TAG */
                0x00                        /** Length of the data part */
                /* NULL */

        };

        ASSERT(socket);


        if (Socket_write(socket, (unsigned char *)request, sizeof(request)) < 0)
                THROW(IOException, "LDAP: error sending data -- %s", STRERROR);

        if (Socket_read(socket, (unsigned char *)buf, sizeof(response)) <= 0)
                THROW(IOException, "LDAP: error receiving data -- %s", STRERROR);

        if (memcmp((unsigned char *)buf, (unsigned char *)response, sizeof(response)))
                THROW(IOException, "LDAP: anonymous bind failed");

        if (Socket_write(socket, (unsigned char *)unbind, sizeof(unbind)) < 0)
                THROW(IOException, "LDAP: error sending data -- %s", STRERROR);
}