/** * Check the server for greeting code 220 and then send a QUIT and * check for code 221. If alive return TRUE, else, return FALSE. * * @file */ int check_ftp(Socket_T socket) { int status; char buf[STRLEN]; ASSERT(socket); do { if (! socket_readln(socket, buf, STRLEN)) { socket_setError(socket, "FTP: error receiving data -- %s", STRERROR); return FALSE; } Str_chomp(buf); } while(buf[3] == '-'); // Discard multi-line response if (sscanf(buf, "%d", &status) != 1 || status != 220) { socket_setError(socket, "FTP greeting error: %s", buf); return FALSE; } if (socket_print(socket, "QUIT\r\n") < 0) { socket_setError(socket, "FTP: error sending data -- %s", STRERROR); return FALSE; } if (! socket_readln(socket, buf, STRLEN)) { socket_setError(socket, "FTP: error receiving data -- %s", STRERROR); return FALSE; } Str_chomp(buf); if (sscanf(buf, "%d", &status) != 1 || status != 221) { socket_setError(socket, "FTP quit error: %s", buf); return FALSE; } return TRUE; }
/** * Check the server for greeting code '* OK' and then send LOGOUT and * check for code '* BYE'. If alive return TRUE, else, return FALSE. * * @file */ int check_imap(Socket_T socket) { char buf[STRLEN]; const char *ok = "* OK"; const char *bye = "* BYE"; ASSERT(socket); // Read and check IMAP greeting if (! socket_readln(socket, buf, sizeof(buf))) { socket_setError(socket, "IMAP: greeting read error -- %s", STRERROR); return FALSE; } Str_chomp(buf); if (strncasecmp(buf, ok, strlen(ok)) != 0) { socket_setError(socket, "IMAP: invalid greeting -- %s", buf); return FALSE; } // Logout and check response if (socket_print(socket, "001 LOGOUT\r\n") < 0) { socket_setError(socket, "IMAP: logout command error -- %s", STRERROR); return FALSE; } if (! socket_readln(socket, buf, sizeof(buf))) { socket_setError(socket, "IMAP: logout response read error -- %s", STRERROR); return FALSE; } Str_chomp(buf); if (strncasecmp(buf, bye, strlen(bye)) != 0) { socket_setError(socket, "IMAP: invalid logout response: %s", buf); return FALSE; } return TRUE; }
/** * A simple DWP (database wire protocol) test. * * We send the following request to the server: * 'HEAD / HTTP/1.1' * and check the server's status code. * * If the status code is >= 400, an error has occurred. * Return TRUE if the status code is 200, otherwise FALSE. * * @file */ int check_dwp(Socket_T socket) { #define REQ_LENGTH 1024 int n; int status; char buf[STRLEN]; char proto[STRLEN]; ASSERT(socket); if(socket_print(socket, "HEAD / HTTP/1.1\r\n" "Connection: close\r\n\r\n") < 0) { socket_setError(socket, "DWP: error sending data -- %s\n", STRERROR); return FALSE; } if(! socket_readln(socket, buf, sizeof(buf))) { socket_setError(socket, "DWP: error receiving data -- %s\n", STRERROR); return FALSE; } Str_chomp(buf); n= sscanf(buf, "%255s %d", proto, &status); if(n!=2 || (status >= 400)) { socket_setError(socket, "DWP error: %s\n", buf); return FALSE; } return TRUE; }
/** * A simple Postfix SMTP access policy delegation protocol test * * To not affect real traffic, we send the following request with * fixed virtual triplet values to the server: * request=smtpd_access_policy * protocol_state=RCPT * protocol_name=SMTP * [email protected] * [email protected] * client_address=1.2.3.4 * client_name=mx.foo.tld * and check that the server replies with some action. * * @file */ int check_postfix_policy(Socket_T socket) { char buf[STRLEN]; ASSERT(socket); if(socket_print(socket, "request=smtpd_access_policy\n" "protocol_state=RCPT\n" "protocol_name=SMTP\n" "[email protected]\n" "[email protected]\n" "client_address=1.2.3.4\n" "client_name=mx.foo.tld\n" "\n") < 0) { socket_setError(socket, "POSTFIX-POLICY: error sending data -- %s", STRERROR); return FALSE; } if(! socket_readln(socket, buf, sizeof(buf))) { socket_setError(socket, "POSTFIX-POLICY: error receiving data -- %s", STRERROR); return FALSE; } Str_chomp(buf); if( (strlen(buf) <= 7) || strncasecmp(buf, "action=", 7) ) { socket_setError(socket, "POSTFIX-POLICY error: %s", *buf?buf:"no action returned"); return FALSE; } return TRUE; }
/** * Returns a new HttpRequest object wrapping the client request */ static HttpRequest create_HttpRequest(Socket_T S) { HttpRequest req= NULL; char url[REQ_STRLEN]; char line[REQ_STRLEN]; char protocol[STRLEN]; char method[REQ_STRLEN]; if(socket_readln(S, line, REQ_STRLEN) == NULL) { internal_error(S, SC_BAD_REQUEST, "No request found"); return NULL; } Str_chomp(line); if(sscanf(line, "%1023s %1023s HTTP/%3[1.0]", method, url, protocol) != 3) { internal_error(S, SC_BAD_REQUEST, "Cannot parse request"); return NULL; } if(strlen(url) >= MAX_URL_LENGTH) { internal_error(S, SC_BAD_REQUEST, "[error] URL too long"); return NULL; } NEW(req); req->S= S; Util_urlDecode(url); req->url= Str_dup(url); req->method= Str_dup(method); req->protocol= Str_dup(protocol); create_headers(req); if(!create_parameters(req)) { destroy_HttpRequest(req); internal_error(S, SC_BAD_REQUEST, "Cannot parse Request parameters"); return NULL; } return req; }
/** * Create HTTP headers for the given request */ static void create_headers(HttpRequest req) { Socket_T S; char *value; HttpHeader header= NULL; char line[REQ_STRLEN]; S= req->S; while(1) { if(! socket_readln(S, line, sizeof(line))) break; if(!strcasecmp(line, "\r\n") || !strcasecmp(line, "\n")) break; if(NULL != (value= strchr(line, ':'))) { NEW(header); *value++= 0; Str_trim(line); Str_trim(value); Str_chomp(value); header->name= Str_dup(line); header->value= Str_dup(value); header->next= req->headers; req->headers= header; } } }
/** * Send PING and check for PONG. * If alive return TRUE, else, return FALSE. * * @file */ int check_clamav(Socket_T socket) { char buf[STRLEN]; const char *ok = "PONG"; ASSERT(socket); if(socket_print(socket, "PING\r\n") < 0) { socket_setError(socket, "CLAMAV: error sending data -- %s", STRERROR); return FALSE; } if(!socket_readln(socket, buf, sizeof(buf))) { socket_setError(socket, "CLAMAV: error receiving data -- %s", STRERROR); return FALSE; } Str_chomp(buf); if(strncasecmp(buf, ok, strlen(ok)) != 0) { socket_setError(socket, "CLAMAV error: %s", buf); return FALSE; } return TRUE; }
static void escape(Mail_T m) { // replace bare linefeed Util_replaceString(&m->message, "\r\n", "\n"); Util_replaceString(&m->message, "\n", "\r\n"); // escape ^. Util_replaceString(&m->message, "\n.", "\n.."); // drop any CR|LF from the subject Str_chomp(m->subject); }
/** * Check the server for greeting code 220 and then send a QUIT and check for code 221 * * @file */ void check_ftp(Socket_T socket) { int status; char buf[STRLEN]; ASSERT(socket); do { if (! Socket_readLine(socket, buf, STRLEN)) THROW(IOException, "FTP: error receiving data -- %s", STRERROR); Str_chomp(buf); } while (buf[3] == '-'); // Discard multi-line response if (sscanf(buf, "%d", &status) != 1 || status != 220) THROW(IOException, "FTP greeting error: %s", buf); if (Socket_print(socket, "QUIT\r\n") < 0) THROW(IOException, "FTP: error sending data -- %s", STRERROR); if (! Socket_readLine(socket, buf, STRLEN)) THROW(IOException, "FTP: error receiving data -- %s", STRERROR); Str_chomp(buf); if (sscanf(buf, "%d", &status) != 1 || status != 221) THROW(IOException, "FTP quit error: %s", buf); }
static void do_status(SendMail_T *S) { int status = 0; StringBuffer_clear(S->status_message); char buf[STRLEN]; do { if (! Socket_readLine(S->socket, buf, sizeof(buf))) THROW(IOException, "Error receiving data from the mailserver '%s' -- %s", S->server, STRERROR); StringBuffer_append(S->status_message, "%s", buf); } while (buf[3] == '-'); // multi-line response Str_chomp(buf); if (sscanf(buf, "%d", &status) != 1 || status < 200 || status >= 400) THROW(IOException, "%s", buf); }
static void _receive(T S, int code, void (*callback)(T S, const char *line)) { int status = 0; char line[STRLEN]; do { if (! Socket_readLine(S->socket, line, sizeof(line))) THROW(IOException, "Error receiving data from the mailserver -- %s", STRERROR); Str_chomp(line); if (strlen(line) < 4 || sscanf(line, "%d", &status) != 1 || status != code) THROW(IOException, "Mailserver response error -- %s", line); if (callback) callback(S, line); } while (line[3] == '-'); // multi-line response }
/** * Check the server for greeting code +OK, then send QUIT and check for code +OK * * @file */ void check_pop(Socket_T socket) { ASSERT(socket); char buf[STRLEN]; const char *ok = "+OK"; // Read and check POP greeting if (! Socket_readLine(socket, buf, sizeof(buf))) THROW(IOException, "POP: greeting read error -- %s", errno ? STRERROR : "no data"); Str_chomp(buf); if (strncasecmp(buf, ok, strlen(ok)) != 0) THROW(IOException, "POP: invalid greeting -- %s", buf); // QUIT and check response if (Socket_print(socket, "QUIT\r\n") < 0) THROW(IOException, "POP: QUIT command error -- %s", STRERROR); if (! Socket_readLine(socket, buf, sizeof(buf))) THROW(IOException, "POP: QUIT response read error -- %s", errno ? STRERROR : "no data"); Str_chomp(buf); if (strncasecmp(buf, ok, strlen(ok)) != 0) THROW(IOException, "POP: invalid QUIT response -- %s", buf); }
/** * Check that the server returns a valid HTTP response as well as checksum * or content regex if required * @param s A socket * @return TRUE if the response is valid otherwise FALSE */ static int check_request(Socket_T socket, Port_T P) { int status, content_length = -1; char buf[LINE_SIZE]; if (! socket_readln(socket, buf, LINE_SIZE)) { socket_setError(socket, "HTTP: Error receiving data -- %s\n", STRERROR); return FALSE; } Str_chomp(buf); if (! sscanf(buf, "%*s %d", &status)) { socket_setError(socket, "HTTP error: Cannot parse HTTP status in response: %s\n", buf); return FALSE; } if (status >= 400) { socket_setError(socket, "HTTP error: Server returned status %d\n", status); return FALSE; } /* Get Content-Length header value */ while (socket_readln(socket, buf, LINE_SIZE)) { if ((buf[0] == '\r' && buf[1] == '\n') || (buf[0] == '\n')) break; Str_chomp(buf); if (Str_startsWith(buf, "Content-Length")) { if (! sscanf(buf, "%*s%*[: ]%d", &content_length)) { socket_setError(socket, "HTTP error: Parsing Content-Length response header '%s'\n", buf); return FALSE; } if (content_length < 0) { socket_setError(socket, "HTTP error: Illegal Content-Length response header '%s'\n", buf); return FALSE; } } } if (P->url_request && P->url_request->regex && ! do_regex(socket, content_length, P->url_request)) return FALSE; if (P->request_checksum) return check_request_checksum(socket, content_length, P->request_checksum, P->request_hashtype); return TRUE; }
/** * Simple redis RESP protocol ping test: * * 1. send a PING command * 2. expect a PONG response * 3. send a QUIT command * * @see http://redis.io/topics/protocol * * @file */ void check_redis(Socket_T socket) { ASSERT(socket); char buf[STRLEN]; if (Socket_print(socket, "*1\r\n$4\r\nPING\r\n") < 0) THROW(IOException, "REDIS: PING command error -- %s", STRERROR); if (! Socket_readLine(socket, buf, sizeof(buf))) THROW(IOException, "REDIS: PING response error -- %s", STRERROR); Str_chomp(buf); if (! Str_isEqual(buf, "+PONG") && ! Str_startsWith(buf, "-NOAUTH")) // We accept authentication error (-NOAUTH Authentication required): redis responded to request, but requires authentication => we assume it works THROW(IOException, "REDIS: PING error -- %s", buf); if (Socket_print(socket, "*1\r\n$4\r\nQUIT\r\n") < 0) THROW(IOException, "REDIS: QUIT command error -- %s", STRERROR); }
/** * Check that the server returns a valid HTTP response * @param C An mmonit object * @return TRUE if the response is valid otherwise FALSE */ static int data_check(Socket_T socket, Mmonit_T C) { int status; char buf[STRLEN]; if (! socket_readln(socket, buf, sizeof(buf))) { LogError("M/Monit: error receiving data from %s -- %s\n", C->url->url, STRERROR); return FALSE; } Str_chomp(buf); int n = sscanf(buf, "%*s %d", &status); if (n != 1 || (status >= 400)) { LogError("M/Monit: message sending failed to %s -- %s\n", C->url->url, buf); return FALSE; } return TRUE; }
/** * Check the server for greeting "@RSYNCD: XX, then send this greeting back to server, send command '#list' to get a listing of modules. * * @file */ void check_rsync(Socket_T socket) { char buf[64]; char header[11]; int rc, version_major, version_minor; char *rsyncd = "@RSYNCD:"; char *rsyncd_exit = "@RSYNCD: EXIT"; ASSERT(socket); /* Read and check the greeting */ if (! Socket_readLine(socket, buf, sizeof(buf))) THROW(IOException, "RSYNC: did not see server greeting -- %s", STRERROR); Str_chomp(buf); rc = sscanf(buf, "%10s %d.%d", header, &version_major, &version_minor); if ((rc == EOF) || (rc != 3)) THROW(IOException, "RSYNC: server greeting parse error %s", buf); if (strncasecmp(header, rsyncd, strlen(rsyncd)) != 0) THROW(IOException, "RSYNC: server sent unexpected greeting -- %s", buf); /* Send back the greeting */ if (Socket_print(socket, "%s\n", buf) <= 0) THROW(IOException, "RSYNC: identification string send failed -- %s", STRERROR); /* Send #list command */ if (Socket_print(socket, "#list\n") < 0) THROW(IOException, "RSYNC: #list command failed -- %s", STRERROR); /* Read response: discard list output and check that we've received successful exit */ do { if (! Socket_readLine(socket, buf, sizeof(buf))) THROW(IOException, "RSYNC: error receiving data -- %s", STRERROR); Str_chomp(buf); } while (strncasecmp(buf, rsyncd, strlen(rsyncd))); if (strncasecmp(buf, rsyncd_exit, strlen(rsyncd_exit)) != 0) THROW(IOException, "RSYNC: server sent unexpected response -- %s", buf); }
static int expect(Socket_T socket, int expect) { int status; char buf[STRLEN]; do { if (! socket_readln(socket, buf, STRLEN)) { socket_setError(socket, "LMTP: error receiving data -- %s", STRERROR); return FALSE; } Str_chomp(buf); } while (buf[3] == '-'); // Discard multi-line response if (sscanf(buf, "%d", &status) != 1 || status != expect) { socket_setError(socket, "LMTP error: %s", buf); return FALSE; } return TRUE; }
static void do_status(SendMail_T *S) { int status; char buf[STRLEN]; if(!socket_readln(S->socket, buf, sizeof(buf))) { LogError("Sendmail: error receiving data from the mailserver '%s' -- %s\n", S->server, STRERROR); siglongjmp(S->error, TRUE); } Str_chomp(buf); sscanf(buf, "%d", &status); if(status >= 400) { LogError("Sendmail error: %s\n", buf); siglongjmp(S->error, TRUE); } }
static int expect(Socket_T socket, int expect, int log) { int status; char buf[STRLEN]; if(!socket_readln(socket, buf, STRLEN)) { socket_setError(socket, "LMTP: error receiving data -- %s\n", STRERROR); return FALSE; } Str_chomp(buf); sscanf(buf, "%d%*s", &status); if(status != expect) { if(log) socket_setError(socket, "LMTP error: %s\n", buf); return FALSE; } return TRUE; }
void check_sip(Socket_T socket) { ASSERT(socket); Port_T P = Socket_getPort(socket); ASSERT(P); const char *target = P->parameters.sip.target ? P->parameters.sip.target : "*****@*****.**"; int port = Socket_getLocalPort(socket); char *proto = Socket_isSecure(socket) ? "sips" : "sip"; char *transport = ""; char *rport = ""; switch (Socket_getType(socket)) { case Socket_Udp: transport = "UDP"; rport = ";rport"; break; case Socket_Tcp: transport = "TCP"; break; default: THROW(IOException, "Unsupported socket type, only TCP and UDP are supported"); break; } char buf[STRLEN]; const char *myip = Socket_getLocalHost(socket, buf, sizeof(buf)); if (Socket_print(socket, "OPTIONS %s:%s SIP/2.0\r\n" "Via: SIP/2.0/%s %s:%d;branch=z9hG4bKh%ld%s\r\n" "Max-Forwards: %d\r\n" "To: <%s:%s>\r\n" "From: monit <%s:monit@%s>;tag=%ld\r\n" "Call-ID: %ld\r\n" "CSeq: 63104 OPTIONS\r\n" "Contact: <%s:%s:%d>\r\n" "Accept: application/sdp\r\n" "Content-Length: 0\r\n" "User-Agent: Monit/%s\r\n\r\n", proto, // protocol target, // to transport, // via transport udp|tcp myip, // who its from port, // our port random(), // branch rport, // rport option P->parameters.sip.maxforward ? P->parameters.sip.maxforward : 70, // maximum forwards proto, // protocol target, // to proto, // protocol myip, // from host random(), // tag random(), // call id proto, // protocol myip, // contact host port, // contact port VERSION // user agent ) < 0) { THROW(IOException, "SIP: error sending data -- %s", STRERROR); } if (! Socket_readLine(socket, buf, sizeof(buf))) THROW(IOException, "SIP: error receiving data -- %s", STRERROR); Str_chomp(buf); DEBUG("Response from SIP server: %s\n", buf); int status; if (! sscanf(buf, "%*s %d", &status)) THROW(IOException, "SIP error: cannot parse SIP status in response: %s", buf); if (status >= 400) THROW(IOException, "SIP error: Server returned status %d", status); if (status >= 300 && status < 400) THROW(IOException, "SIP info: Server redirection. Returned status %d", status); if (status > 100 && status < 200) THROW(IOException, "SIP error: Provisional response . Returned status %d", status); }
int check_sip(Socket_T socket) { int status; char buf[STRLEN]; int port; char *transport; Port_T P; const char *request; const char *myip; char *rport= ""; char *proto; ASSERT(socket); P= socket_get_Port(socket); ASSERT(P); request= P->request?P->request:"*****@*****.**"; port = socket_get_local_port(socket); proto = socket_is_secure(socket) ? "sips" : "sip"; switch(socket_get_type(socket)) { case SOCK_DGRAM: { transport="UDP"; rport=";rport"; break; } case SOCK_STREAM: { transport="TCP"; break; } default: { socket_setError(socket, "Unsupported socket type, only TCP and UDP are supported\n"); return TRUE; } } myip= socket_get_local_host(socket); if(socket_print(socket, "OPTIONS %s:%s SIP/2.0\r\n" "Via: SIP/2.0/%s %s:%d;branch=z9hG4bKh%u%s\r\n" "Max-Forwards: %d\r\n" "To: <%s:%s>\r\n" "From: monit <%s:monit@%s>;tag=%d\r\n" "Call-ID: %u\r\n" "CSeq: 63104 OPTIONS\r\n" "Contact: <%s:%s:%d>\r\n" "Accept: application/sdp\r\n" "Content-Length: 0\r\n" "User-Agent: %s/%s\r\n\r\n", proto, // protocol request, // to transport, // via transport udp|tcp myip, // who its from port, // our port random(), // branch rport, // rport option P->maxforward, // maximum forwards proto, // protocol request, // to proto, // protocol myip, // from host random(), // tag random(), // call id proto, // protocol myip, // contact host port, // contact port prog, VERSION // user agent ) < 0) { socket_setError(socket, "SIP: error sending data -- %s\n", STRERROR); return FALSE; } if(! socket_readln(socket, buf, sizeof(buf))) { socket_setError(socket, "SIP: error receiving data -- %s\n", STRERROR); return FALSE; } Str_chomp(buf); DEBUG("Response from SIP server: %s\n", buf); if(! sscanf(buf, "%*s %d", &status)) { socket_setError(socket, "SIP error: cannot parse SIP status in response: %s\n", buf); return FALSE; } if(status >= 400) { socket_setError(socket, "SIP error: Server returned status %d\n", status); return FALSE; } if(status >= 300 && status < 400) { socket_setError(socket, "SIP info: Server redirection. Returned status %d\n", status); return FALSE; } if(status > 100 && status < 200) { socket_setError(socket, "SIP error: Provisional response . Returned status %d\n", status); return FALSE; } return TRUE; }
/** * Extract the Scoreboard line from the mod_status response. * Count the active apache child processes, and those which are * in other states. If each percentage exceeds the corresponding * limit, then return FALSE. * @param s A socket * @param limit The maximum percentage of logging processes * @return TRUE if logging is OK otherwise FALSE */ static int check_apache_stat(Socket_T socket) { int scored = 0; int errors = 0; char line[READ_SIZE]; char search_string[READ_SIZE + 1]; int loglimit= 0; int closelimit= 0; int dnslimit= 0; int keepalivelimit= 0; int replylimit= 0; int requestlimit= 0; int startlimit= 0; int waitlimit= 0; int gracefullimit= 0; int cleanuplimit= 0; int no_logging = 0; int no_close = 0; int no_dns = 0; int no_keepalive = 0; int no_reply = 0; int no_request = 0; int no_start = 0; int no_wait = 0; int no_graceful = 0; int no_cleanup = 0; int active_servers = 0; char *p; Port_T myPort= (Port_T)socket_get_Port(socket); ASSERT(myPort); loglimit= myPort->ApacheStatus.loglimit; closelimit= myPort->ApacheStatus.closelimit; dnslimit= myPort->ApacheStatus.dnslimit; keepalivelimit= myPort->ApacheStatus.keepalivelimit; replylimit= myPort->ApacheStatus.replylimit; requestlimit= myPort->ApacheStatus.requestlimit; startlimit= myPort->ApacheStatus.startlimit; waitlimit= myPort->ApacheStatus.waitlimit; gracefullimit= myPort->ApacheStatus.gracefullimit; cleanuplimit= myPort->ApacheStatus.cleanuplimit; while(NULL != socket_readln(socket, line, READ_SIZE)) { if(Str_startsWith(line, "Scoreboard")) { if(1 != sscanf(line, "%*s%*[: ]%1024s", search_string)) { Str_chomp(line); socket_setError(socket, "APACHE-STATUS error: parsing Apache status response '%s'\n", line); return FALSE; } else { scored = 1; } } } DEBUG("Scoreboard: %s\n", search_string); /* Check that some scoreboard line was found, if not return an error */ if(!scored){ socket_setError(socket, "APACHE-STATUS error: no scoreboard line returned by Apache\n"); return FALSE; } /* Total each of the status messages in the scoreboard */ for(p = search_string ; *p ; p++){ active_servers++; switch(*p){ case 'S': no_start++; break; case 'R': no_request++; break; case 'W': no_reply++; break; case 'K': no_keepalive++; break; case 'D': no_dns++; break; case 'C': no_close++; break; case 'L': no_logging++; break; case 'G': no_graceful++; break; case 'I': no_cleanup++; break; case '_': no_wait++; break; case '.': active_servers--; break; } } if(active_servers <= 0){ socket_setError(socket, "APACHE-STATUS warning: No idle server or threads found\n"); /* This is not really an error, only a very bussy server */ return TRUE; } /* * Conditions are only tested if the limit parameter is greater than zero. */ if(loglimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.loglimitOP, (100 * no_logging / active_servers), loglimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are logging\n", loglimit); errors++; } } if(startlimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.startlimitOP, (100 * no_start / active_servers), startlimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are starting\n", startlimit); errors++; } } if(requestlimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.requestlimitOP, (100 * no_request / active_servers), requestlimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are reading requests\n", requestlimit); errors++; } } if(replylimit > 0 ){ if(Util_evalQExpression(myPort->ApacheStatus.replylimitOP, (100 * no_reply / active_servers), replylimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are sending a reply\n", replylimit); errors++; } } if(keepalivelimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.keepalivelimitOP, (100 * no_keepalive / active_servers), keepalivelimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are in keepalive\n", keepalivelimit); errors++; } } if(dnslimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.dnslimitOP, (100 * no_dns / active_servers), dnslimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are waiting for DNS\n", dnslimit); errors++; } } if(closelimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.closelimitOP, (100 * no_close / active_servers), closelimit)){ socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are closing connections\n", closelimit); errors++; } } if(gracefullimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.gracefullimitOP, (100 * no_graceful / active_servers), gracefullimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are finishing gracefully\n", gracefullimit); errors++; } } if(cleanuplimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.cleanuplimitOP, (100 * no_cleanup / active_servers), cleanuplimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are in idle cleanup\n", cleanuplimit); errors++; } } if(waitlimit > 0){ if(Util_evalQExpression(myPort->ApacheStatus.waitlimitOP, (100 * no_wait / active_servers), waitlimit)) { socket_setError(socket, "APACHE-STATUS error:" " %i percent of Apache processes are waiting for a connection\n", waitlimit); errors++; } } return (errors==0); }
int main(void) { Bootstrap(); // Need to initialize library printf("============> Start Str Tests\n\n"); printf("=> Test1: copy\n"); { char s3[STRLEN]; printf("\tResult: %s\n", Str_copy(s3, "The abc house", 7)); assert(Str_isEqual(s3, "The abc")); printf("\tTesting for NULL argument\n"); assert(!Str_copy(NULL, NULL, 7)); } printf("=> Test1: OK\n\n"); printf("=> Test2: dup\n"); { char *s4 = Str_dup("abc123"); printf("\tResult: %s\n", s4); assert(Str_isEqual(s4, "abc123")); printf("\tTesting for NULL argument\n"); assert(!Str_dup(NULL)); FREE(s4); } printf("=> Test2: OK\n\n"); printf("=> Test3: ndup\n"); { char *s5 = Str_ndup("abc123", 3); printf("\tResult: %s\n", s5); assert(Str_isEqual(s5, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_ndup(NULL, 3)); FREE(s5); } printf("=> Test3: OK\n\n"); printf("=> Test4: Str_cat & Str_vcat\n"); { char *s6; s6 = Str_cat("%s://%s%s?%s", "https", "foo.bar", "/uri", "abc=123"); printf("\tResult: %s\n", s6); assert(Str_isEqual(s6, "https://foo.bar/uri?abc=123")); FREE(s6); printf("\tTesting for NULL arguments\n"); s6 = Str_cat(NULL); assert(s6==NULL); FREE(s6); } printf("=> Test4: OK\n\n"); printf("=> Test5: chomp\n"); { char s3[] = "abc\r\n123"; printf("\tResult: %s\n", Str_chomp(s3)); assert(Str_isEqual(s3, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_chomp(NULL)); } printf("=> Test5: OK\n\n"); printf("=> Test6: trim\n"); { char e[] = " "; char o[] = " a "; char s[] = " abcdef"; char s4[] = " \t abc \r\n\t "; assert(Str_isEqual(Str_ltrim(s), "abcdef")); printf("\tResult: %s\n", Str_trim(s4)); assert(Str_isEqual(s4, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_trim(NULL)); assert(Str_isEqual(Str_ltrim(e), "")); memcpy(e, " ", sizeof(" ") - 1); assert(Str_isEqual(Str_rtrim(e), "")); memcpy(e, " ", sizeof(" ") - 1); assert(Str_isEqual(Str_trim(e), "")); assert(Str_isEqual(Str_ltrim(o), "a ")); memcpy(o, " a ", sizeof(" a ") - 1); assert(Str_isEqual(Str_rtrim(o), " a")); memcpy(o, " a ", sizeof(" a ") - 1); assert(Str_isEqual(Str_trim(o), "a")); assert(Str_isEqual(Str_trim(o), "a")); } printf("=> Test6: OK\n\n"); printf("=> Test7: trim quotes\n"); { char s5[] = "\"'abc'\""; char s5a[] = "\"'abc"; char s5b[] = "abc'\""; char s5c[] = "'\""; char s5d[] = " \t abc def '\" "; printf("\tResult: %s\n", Str_unquote(s5)); assert(Str_isEqual(s5, "abc")); printf("\tResult: %s\n", Str_unquote(s5a)); assert(Str_isEqual(s5, "abc")); printf("\tResult: %s\n", Str_unquote(s5b)); assert(Str_isEqual(s5, "abc")); printf("\tResult: %s\n", Str_unquote(s5b)); assert(Str_isEqual(s5, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_unquote(NULL)); printf("\tTesting for quotes-only argument\n"); assert(Str_isEqual("", Str_unquote(s5c))); printf("\tTesting for quotes and white-space removal\n"); assert(Str_isEqual("abc def", Str_unquote(s5d))); } printf("=> Test7: OK\n\n"); printf("=> Test8: toLowerCase\n"); { char s6[] = "AbC"; printf("\tResult: %s\n", Str_toLower(s6)); assert(Str_isEqual(s6, "abc")); printf("\tTesting for NULL argument\n"); assert(!Str_toLower(NULL)); } printf("=> Test8: OK\n\n"); printf("=> Test9: toUpperCase\n"); { char s7[] = "aBc"; printf("\tResult: %s\n", Str_toUpper(s7)); assert(Str_isEqual(s7, "ABC")); printf("\tTesting for NULL argument\n"); assert(!Str_toUpper(NULL)); } printf("=> Test9: OK\n\n"); printf("=> Test10: parseInt, parseLLong, parseDouble\n"); { char i[STRLEN] = " -2812 bla"; char ll[STRLEN] = " 2147483642 blabla"; char d[STRLEN] = " 2.718281828 this is e"; char de[STRLEN] = "1.495E+08 kilometer = An Astronomical Unit"; char ie[STRLEN] = " 9999999999999999999999999999999999999999"; printf("\tResult:\n"); printf("\tParsed int = %d\n", Str_parseInt(i)); assert(Str_parseInt(i)==-2812); printf("\tParsed long long = %lld\n", Str_parseLLong(ll)); assert(Str_parseLLong(ll)==2147483642); printf("\tParsed double = %.9f\n", Str_parseDouble(d)); assert(Str_parseDouble(d)==2.718281828); printf("\tParsed double exp = %.3e\n", Str_parseDouble(de)); assert(Str_parseDouble(de)==1.495e+08); TRY Str_parseInt(ie); assert(false); CATCH(NumberFormatException) printf("=> Test11: OK\n\n"); END_TRY; } printf("=> Test10: OK\n\n"); printf("=> Test11: replace\n"); { char s9[] = "abccba"; printf("\tResult: %s\n", Str_replaceChar(s9, 'b', 'X')); assert(Str_isEqual(s9, "aXccXa")); printf("\tTesting for NULL argument\n"); assert(!Str_replaceChar(NULL, 'b', 'X')); } printf("=> Test11: OK\n\n"); printf("=> Test12: startsWith\n"); { char *a = "mysql://*****:*****@ ]+@([-a-zA-Z0-9]+\\.)+[a-zA-Z]{2,}$"; char *valid_phone1 = "+4797141255"; char *valid_phone2 = "(47)-97-14-12-55"; char *invalid_phone1 = "141255"; char *invalid_phone2 = "(47)971412551234567890123456789012345678901234567890"; char *invalid_phone3 = ""; char *invalid_phone4 = "abc123"; char *valid_email1 = "*****@*****.**"; char *valid_email2 = "*****@*****.**"; char *invalid_email1 = "hauktildeslash.com"; char *invalid_email2 = ""; char *invalid_email3 = "hauk@tildeslashcom"; char *invalid_email4 = "hauk@æøåtildeslash.com"; // phone printf("\tResult: match(%s, %s)\n", phone_pattern, valid_phone1); assert(Str_match(phone_pattern, valid_phone1)); printf("\tResult: match(%s, %s)\n", phone_pattern, valid_phone2); assert(Str_match(phone_pattern, valid_phone2)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone1); assert(! Str_match(phone_pattern, invalid_phone1)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone2); assert(! Str_match(phone_pattern, invalid_phone2)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone3); assert(! Str_match(phone_pattern, invalid_phone3)); printf("\tResult: match(%s, %s)\n", phone_pattern, invalid_phone4); assert(! Str_match(phone_pattern, invalid_phone4)); // email printf("\tResult: match(%s, %s)\n", email_pattern, valid_email1); assert(Str_match(email_pattern, valid_email1)); printf("\tResult: match(%s, %s)\n", email_pattern, valid_email2); assert(Str_match(email_pattern, valid_email2)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email1); assert(! Str_match(email_pattern, invalid_email1)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email2); assert(! Str_match(email_pattern, invalid_email2)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email3); assert(! Str_match(email_pattern, invalid_email3)); printf("\tResult: match(%s, %s)\n", email_pattern, invalid_email4); assert(! Str_match(email_pattern, invalid_email4)); } printf("=> Test17: OK\n\n"); printf("=> Test18: lim\n"); { char *zero = ""; char *two = "12"; char *ten = "1234567890"; assert(! Str_lim(zero, 0)); assert(!Str_lim(zero, 1)); assert(Str_lim(two, 0)); assert(Str_lim(two, 1)); assert(!Str_lim(two, 2)); assert(Str_lim(ten, 0)); assert(Str_lim(ten, 5)); assert(Str_lim(ten, 9)); assert(!Str_lim(ten, 10)); assert(! Str_lim(ten, 100)); } printf("=> Test18: OK\n\n"); printf("=> Test19: substring\n"); { assert(Str_sub("foo bar baz", "bar")); assert(! Str_sub("foo bar baz", "barx")); assert(Str_isEqual(Str_sub("foo bar baz", "baz"), "baz")); assert(Str_sub("foo bar baz", "foo bar baz")); assert(Str_sub("a", "a")); assert(! Str_sub("a", "b")); assert(! Str_sub("", "")); assert(! Str_sub("foo", "")); assert(! Str_sub("abc", "abcdef")); assert(! Str_sub("foo", "foo bar")); assert(Str_isEqual(Str_sub("foo foo bar", "foo bar"), "foo bar")); assert(Str_sub("foo foo bar foo bar baz fuu", "foo bar baz")); assert(Str_isEqual(Str_sub("abcd abcc", "abcc"), "abcc")); } printf("=> Test19: OK\n\n"); printf("=> Test20: Str_join\n"); { char *p = NULL; char dest[10+1] = "xxxxxxxxx"; char a[] = "abc"; char *b = "def"; char *c = "xxx123"; assert(Str_isEqual(Str_join(dest, 10, a, b, "ghi"), "abcdefghi")); assert(Str_isEqual(Str_join(dest, 10, p), "")); assert(Str_isEqual(Str_join(dest, 10), "")); assert(Str_isEqual(Str_join(dest, 10, "012", "3456789", "0123456789"), "0123456789")); assert(Str_isEqual(Str_join(dest, 4, "a", "b", "cd", "ghi", "jklmnopq"), "abcd")); assert(Str_isEqual(Str_join(dest, 10, a, c + 3), "abc123")); Str_join(dest, 0); assert(dest[0]==0); } printf("=> Test20: OK\n\n"); printf("=> Test21: Str_has\n"); { char *foo = "'bar' (baz)"; assert(Str_has("(')", foo)); assert(! Str_has(",;", foo)); } printf("=> Test21: OK\n\n"); printf("=> Test22: Str_curtail\n"); { char s[] = "<text>Hello World</text>"; assert(Str_isByteEqual(Str_curtail(s, "</text>"), "<text>Hello World")); assert(Str_isByteEqual(Str_curtail(s, ">"), "<text")); assert(Str_isByteEqual(Str_curtail(s, "@"), "<text")); } printf("=> Test22: OK\n\n"); printf("=> Test23: Str_ton\n"); { char str[43]; long l1 = 42, l2 = 0, l3 = -42, l4 = LONG_MAX, l5 = LONG_MIN; assert(Str_isByteEqual("42", Str_ton(42, (char[43]){0}))); assert(Str_isByteEqual("42", Str_ton(l1, str))); assert(Str_isByteEqual("0", Str_ton(l2, str))); assert(Str_isByteEqual("-42", Str_ton(l3, str))); assert(l4 == Str_parseLLong(Str_ton(l4, str))); assert(l5 == Str_parseLLong(Str_ton(l5, str))); }