"Fossies" - the Fresh Open Source Software Archive

Member "monit-5.28.0/src/http/client.c" (28 Mar 2021, 8727 Bytes) of package /linux/privat/monit-5.28.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "client.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.27.2_vs_5.28.0.

    1 /*
    2  * Copyright (C) Tildeslash Ltd. All rights reserved.
    3  *
    4  * This program is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU Affero General Public License version 3.
    6  *
    7  * This program is distributed in the hope that it will be useful,
    8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10  * GNU General Public License for more details.
   11  *
   12  * You should have received a copy of the GNU Affero General Public License
   13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   14  *
   15  * In addition, as a special exception, the copyright holders give
   16  * permission to link the code of portions of this program with the
   17  * OpenSSL library under certain conditions as described in each
   18  * individual source file, and distribute linked combinations
   19  * including the two.
   20  *
   21  * You must obey the GNU Affero General Public License in all respects
   22  * for all of the code used other than OpenSSL.
   23  */
   24 
   25 #include "config.h"
   26 
   27 #ifdef HAVE_STDIO_H
   28 #include <stdio.h>
   29 #endif
   30 
   31 #ifdef HAVE_STDLIB_H
   32 #include <stdlib.h>
   33 #endif
   34 
   35 #ifdef HAVE_SYS_TYPES_H
   36 #include <sys/types.h>
   37 #endif
   38 
   39 #ifdef HAVE_SYS_SOCKET_H
   40 #include <sys/socket.h>
   41 #endif
   42 
   43 #ifdef HAVE_STRING_H
   44 #include <string.h>
   45 #endif
   46 
   47 #ifdef HAVE_STRINGS_H
   48 #include <strings.h>
   49 #endif
   50 
   51 #ifdef HAVE_UNISTD_H
   52 #include <unistd.h>
   53 #endif
   54 
   55 #include "monit.h"
   56 #include "ProcessTree.h"
   57 #include "device.h"
   58 #include "TextColor.h"
   59 #include "TextBox.h"
   60 #include "client.h"
   61 
   62 // libmonit
   63 #include "exceptions/AssertException.h"
   64 #include "exceptions/IOException.h"
   65 
   66 /**
   67  *  The monit HTTP GUI client
   68  *
   69  *  @file
   70  */
   71 
   72 
   73 /* ----------------------------------------------------------------- Private */
   74 
   75 
   76 static void _argument(StringBuffer_T data, const char *name, const char *value) {
   77         char *_value = Util_urlEncode((char *)value, true);
   78         StringBuffer_append(data, "%s%s=%s", StringBuffer_length(data) ? "&" : "", name, _value);
   79         FREE(_value);
   80 }
   81 
   82 
   83 static char *_getBasicAuthHeader(void) {
   84         Auth_T auth = NULL;
   85         // Find the first cleartext credential for authorization
   86         for (Auth_T c = Run.httpd.credentials; c; c = c->next) {
   87                 if (c->digesttype == Digest_Cleartext) {
   88                         if (! auth || auth->is_readonly) {
   89                                 auth = c;
   90                         }
   91                 }
   92         }
   93         if (auth)
   94                 return Util_getBasicAuthHeader(auth->uname, auth->passwd);
   95         return NULL;
   96 }
   97 
   98 
   99 static void _parseHttpResponse(Socket_T S) {
  100         char buf[1024];
  101         if (! Socket_readLine(S, buf, sizeof(buf)))
  102                 THROW(IOException, "Error receiving data -- %s", STRERROR);
  103         Str_chomp(buf);
  104         int status;
  105         if (! sscanf(buf, "%*s %d", &status))
  106                 THROW(IOException, "Cannot parse status in response: %s", buf);
  107         if (status >= 300) {
  108                 int content_length = 0;
  109                 // Read HTTP headers
  110                 while (Socket_readLine(S, buf, sizeof(buf))) {
  111                         if (! strncmp(buf, "\r\n", sizeof(buf)))
  112                                 break;
  113                         if (Str_startsWith(buf, "Content-Length") && ! sscanf(buf, "%*s%*[: ]%d", &content_length))
  114                                 THROW(IOException, "Invalid Content-Length header: %s", buf);
  115                 }
  116                 // Parse error response
  117                 char *message = NULL;
  118                 if (content_length > 0 && content_length < 1024 && Socket_readLine(S, buf, sizeof(buf))) {
  119                         char token[] = "</h2>";
  120                         message = strstr(buf, token);
  121                         if (message && strlen(message) > strlen(token)) {
  122                                 message += strlen(token);
  123                                 char *footer = NULL;
  124                                 if ((footer = strstr(message, "<p>")) || (footer = strstr(message, "<hr>")))
  125                                         *footer = 0;
  126                         }
  127                 }
  128                 THROW(AssertException, "%s", message ? message : "cannot parse response");
  129         } else {
  130                 // Skip HTTP headers
  131                 while (Socket_readLine(S, buf, sizeof(buf))) {
  132                          if (! strncmp(buf, "\r\n", sizeof(buf)))
  133                                 break;
  134                 }
  135         }
  136 }
  137 
  138 
  139 static void _send(Socket_T S, const char *request, StringBuffer_T data) {
  140         _argument(data, "format", "text");
  141         char *_auth = _getBasicAuthHeader();
  142         MD_T token;
  143         StringBuffer_append(data, "%ssecuritytoken=%s", StringBuffer_length(data) > 0 ? "&" : "", Util_getToken(token));
  144         int rv = Socket_print(S,
  145                 "POST %s HTTP/1.0\r\n"
  146                 "Content-Type: application/x-www-form-urlencoded\r\n"
  147                 "Cookie: securitytoken=%s\r\n"
  148                 "Content-Length: %d\r\n"
  149                  "%s"
  150                  "\r\n"
  151                  "%s",
  152                 request,
  153                 token,
  154                 StringBuffer_length(data),
  155                 _auth ? _auth : "",
  156                 StringBuffer_toString(data));
  157         FREE(_auth);
  158         if (rv < 0)
  159                 THROW(IOException, "Monit: cannot send command to the monit daemon -- %s", STRERROR);
  160 }
  161 
  162 
  163 static void _receive(Socket_T S) {
  164         char buf[1024];
  165         _parseHttpResponse(S);
  166         bool strip = (Run.flags & Run_Batch || ! TextColor_support()) ? true : false;
  167         while (Socket_readLine(S, buf, sizeof(buf))) {
  168                 if (strip)
  169                         TextColor_strip(TextBox_strip(buf));
  170                 printf("%s", buf);
  171         }
  172 }
  173 
  174 
  175 static bool _client(const char *request, StringBuffer_T data) {
  176         volatile bool status = false;
  177         if (! exist_daemon()) {
  178                 Log_error("Monit: the monit daemon is not running\n");
  179                 return status;
  180         }
  181         Socket_T S = NULL;
  182         if (Run.httpd.flags & Httpd_Net) {
  183                 S = Socket_create(Run.httpd.socket.net.address ? Run.httpd.socket.net.address : "localhost", Run.httpd.socket.net.port, Socket_Tcp, Socket_Ip, &(Run.httpd.socket.net.ssl), Run.limits.networkTimeout);
  184         } else if (Run.httpd.flags & Httpd_Unix) {
  185                 S = Socket_createUnix(Run.httpd.socket.unix.path, Socket_Tcp, Run.limits.networkTimeout);
  186         } else {
  187                 Log_error("Monit: the monit HTTP interface is not enabled, please add the 'set httpd' statement and use the 'allow' option to allow monit to connect\n");
  188         }
  189         if (S) {
  190                 TRY
  191                 {
  192                         _send(S, request, data);
  193                         _receive(S);
  194                         status = true;
  195                 }
  196                 ELSE
  197                 {
  198                         Log_error("%s\n", Exception_frame.message);
  199                 }
  200                 END_TRY;
  201                 Socket_free(&S);
  202         }
  203         return status;
  204 }
  205 
  206 
  207 /* ------------------------------------------------------------------ Public */
  208 
  209 
  210 bool HttpClient_action(const char *action, List_T services) {
  211         ASSERT(services);
  212         ASSERT(action);
  213         if (Util_getAction(action) == Action_Ignored) {
  214                 Log_error("Invalid action %s\n", action);
  215                 return false;
  216         }
  217         StringBuffer_T data = StringBuffer_create(64);
  218         _argument(data, "action", action);
  219         for (list_t s = services->head; s; s = s->next)
  220                 _argument(data, "service", s->e);
  221         bool rv = _client("/_doaction", data);
  222         StringBuffer_free(&data);
  223         return rv;
  224 }
  225 
  226 
  227 bool HttpClient_report(const char *type) {
  228         StringBuffer_T data = StringBuffer_create(64);
  229         if (STR_DEF(type))
  230                 _argument(data, "type", type);
  231         bool rv = _client("/_report", data);
  232         StringBuffer_free(&data);
  233         return rv;
  234 }
  235 
  236 
  237 bool HttpClient_status(const char *group, const char *service) {
  238         StringBuffer_T data = StringBuffer_create(64);
  239         if (STR_DEF(service))
  240                 _argument(data, "service", service);
  241         if (STR_DEF(group))
  242                 _argument(data, "group", group);
  243         bool rv = _client("/_status", data);
  244         StringBuffer_free(&data);
  245         return rv;
  246 }
  247 
  248 
  249 bool HttpClient_summary(const char *group, const char *service) {
  250         StringBuffer_T data = StringBuffer_create(64);
  251         if (STR_DEF(service))
  252                 _argument(data, "service", service);
  253         if (STR_DEF(group))
  254                 _argument(data, "group", group);
  255         bool rv = _client("/_summary", data);
  256         StringBuffer_free(&data);
  257         return rv;
  258 }
  259