"Fossies" - the Fresh Open Source Software Archive

Member "monit-5.28.0/src/util.h" (28 Mar 2021, 9092 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 "util.h" 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 
   26 #ifndef MONIT_UTIL_H
   27 #define MONIT_UTIL_H
   28 
   29 
   30 /**
   31  *  General purpose utility methods.
   32  *
   33  *  @file
   34  */
   35 
   36 
   37 /**
   38  * Replace all occurrences of the sub-string old in the string src
   39  * with the sub-string new. The method is case sensitive for the
   40  * sub-strings new and old. The string parameter src must be an
   41  * allocated string, not a character array.
   42  * @param src An allocated string reference (e.g. &string)
   43  * @param old The old sub-string
   44  * @param new The new sub-string
   45  * @return src where all occurrences of the old sub-string are
   46  * replaced with the new sub-string.
   47  */
   48 char *Util_replaceString(char **src, const char *old, const char *new);
   49 
   50 
   51 /**
   52  * Count the number the sub-string word occurs in s.
   53  * @param s The String to search for word in
   54  * @param word The sub-string to count in s
   55  */
   56 int Util_countWords(char *s, const char *word);
   57 
   58 
   59 /**
   60  * Exchanges \escape sequences in a string
   61  * @param buf A string
   62  */
   63 void Util_handleEscapes(char *buf);
   64 
   65 
   66 /**
   67  * Variant of Util_handleEscapes() which only handle \0x00 escape sequences
   68  * in a string
   69  * @param buf A string
   70  * @return The new length of buf
   71  */
   72 int Util_handle0Escapes(char *buf);
   73 
   74 
   75 /**
   76  * @param name A service name as stated in the config file
   77  * @return the named service or NULL if not found
   78  */
   79 Service_T Util_getService(const char *name);
   80 
   81 
   82 /**
   83  * @param name A service name as stated in the config file
   84  * @return true if the service name exist in the
   85  * servicelist, otherwise false
   86  */
   87 bool Util_existService(const char *name);
   88 
   89 
   90 /**
   91  * Get the length of the service list, that is; the number of services
   92  * managed by monit
   93  * @return The number of services monitored
   94  */
   95 int Util_getNumberOfServices(void);
   96 
   97 
   98 /**
   99  * Print the Runtime object
  100  */
  101 void Util_printRunList(void);
  102 
  103 
  104 /**
  105  * Print a service object
  106  * @param p A Service_T object
  107  */
  108 void Util_printService(Service_T s);
  109 
  110 
  111 /**
  112  * Print all the services in the servicelist
  113  */
  114 void Util_printServiceList(void);
  115 
  116 
  117 /**
  118  * Get a random token
  119  * @param token buffer to store the MD digest
  120  * @return pointer to token buffer
  121  */
  122 char *Util_getToken(MD_T token);
  123 
  124 
  125 /**
  126  * Open and read the id from the given idfile. If the idfile doesn't exist,
  127  * generate new id and store it in the id file.
  128  * @param idfile An idfile with full path
  129  * @return the id or NULL
  130  */
  131 char *Util_monitId(char *idfile);
  132 
  133 
  134 /**
  135  * Open and read the pid from the given pidfile.
  136  * @param pidfile A pidfile with full path
  137  * @return the pid or 0 if the pid could
  138  * not be read from the file
  139  */
  140 pid_t Util_getPid(char *pidfile);
  141 
  142 
  143 /**
  144  * Returns true if url contains url safe characters otherwise false
  145  * @param url an url string to test
  146  * @return true if url is url safe otherwise false
  147  */
  148 bool Util_isurlsafe(const char *url);
  149 
  150 /**
  151  * Escape an url string converting unsafe characters to a hex (%xx)
  152  * representation.  The caller must free the returned string.
  153  * @param string a string to encode
  154  * @param isParameterValue true if the string is url parameter value
  155  * @return the escaped string
  156  */
  157 char *Util_urlEncode(char *string, bool isParameterValue);
  158 
  159 
  160 /**
  161  * Unescape an url string. The <code>url</code> parameter is modified
  162  * by this method.
  163  * @param url an escaped url string
  164  * @return A pointer to the unescaped <code>url</code>string
  165  */
  166 char *Util_urlDecode(char *url);
  167 
  168 
  169 /**
  170  * @return a Basic Authentication Authorization string (RFC 2617),
  171  * NULL if username is not defined.
  172  */
  173 char *Util_getBasicAuthHeader(char *username, char *password);
  174 
  175 
  176 /**
  177  * Redirect the standard file descriptors to /dev/null and route any
  178  * error messages to the log file.
  179  */
  180 void Util_redirectStdFds(void);
  181 
  182 
  183 /*
  184  * Close all filedescriptors except standard.
  185  */
  186 void Util_closeFds(void);
  187 
  188 
  189 /*
  190  * Check if monit does have credentials for this user.  If successful
  191  * a pointer to the password is returned.
  192  */
  193 Auth_T Util_getUserCredentials(char *uname);
  194 
  195 
  196 /**
  197  * Check if the given password match the registered password for the
  198  * given username.
  199  * @param uname Username
  200  * @param outside The password to test
  201  * @return true if the passwords match for the given uname otherwise
  202  * false
  203  */
  204 bool Util_checkCredentials(char *uname, char *outside);
  205 
  206 
  207 /**
  208  * Reset the service information structure
  209  * @param s A Service_T object
  210  */
  211 void Util_resetInfo(Service_T s);
  212 
  213 
  214 /**
  215  * Are service status data available?
  216  * @param s The service to test
  217  * @return true if available otherwise false
  218  */
  219 bool Util_hasServiceStatus(Service_T s);
  220 
  221 
  222 /**
  223  * Construct a HTTP/1.1 Host header utilizing information from the
  224  * socket. The returned hostBuf is set to "hostname:port" or to the
  225  * empty string if information is not available or not applicable.
  226  * @param s A connected socket
  227  * @param hostBuf the buffer to write the host-header to
  228  * @param len Length of the hostBuf
  229  * @return the hostBuffer
  230  */
  231 char *Util_getHTTPHostHeader(Socket_T s, char *hostBuf, int len);
  232 
  233 
  234 /**
  235  * Evaluate a qualification expression.
  236  * @param operator The qualification operator
  237  * @param left Expression lval
  238  * @param rightExpression rval
  239  * @return the boolean value of the expression
  240  */
  241 bool Util_evalQExpression(Operator_Type operator, long long left, long long right);
  242 
  243 
  244 /**
  245  * Evaluate a qualification expression.
  246  * @param operator The qualification operator
  247  * @param left Expression lval
  248  * @param rightExpression rval
  249  * @return the boolean value of the expression
  250  */
  251 bool Util_evalDoubleQExpression(Operator_Type operator, double left, double right);
  252 
  253 
  254 /*
  255  * This will enable service monitoring in the case that it was disabled.
  256  * @param s A Service_T object
  257  */
  258 void Util_monitorSet(Service_T s);
  259 
  260 
  261 /*
  262  * This will disable service monitoring in the case that it is enabled
  263  * @param s A Service_T object
  264  */
  265 void Util_monitorUnset(Service_T s);
  266 
  267 
  268 /*
  269  * Return appropriate action id for string
  270  * @param action A action string
  271  * @return the action id
  272  */
  273 int Util_getAction(const char *action);
  274 
  275 
  276 /*
  277  * Append full action description to given string buffer
  278  * @param action An action object
  279  * @param buf StringBuffer
  280  * @return StringBuffer reference
  281  */
  282 StringBuffer_T Util_printAction(Action_T action, StringBuffer_T buf);
  283 
  284 
  285 /**
  286  * Append event ratio needed to trigger the action to given string buffer
  287  * @param action A action string
  288  * @param buf StringBuffer
  289  * @return StringBuffer reference
  290  */
  291 StringBuffer_T Util_printEventratio(Action_T action, StringBuffer_T buf);
  292 
  293 
  294 /**
  295  * Append a rule description to the given StringBuffer. The description
  296  * consists of the formatted string given by the rule argument and constant
  297  * part which describes rule actions based on the action argument.
  298  * @param inverse Inverse test flag
  299  * @param buf StringBuffer
  300  * @param action An EventAction object
  301  * @param rule Rule description
  302  * @return StringBuffer reference
  303  */
  304 StringBuffer_T Util_printRule(bool inverse, StringBuffer_T buf, EventAction_T action, const char *rule, ...) __attribute__((format (printf, 4, 5)));
  305 
  306 
  307 /**
  308  * Print port IP version description
  309  * @param p A port structure
  310  * @return the socket IP version description
  311  */
  312 const char *Util_portIpDescription(Port_T p);
  313 
  314 
  315 /**
  316  * Print port type description
  317  * @param p A port structure
  318  * @return the socket type description
  319  */
  320 const char *Util_portTypeDescription(Port_T p);
  321 
  322 
  323 /**
  324  * Print port request description
  325  * @param p A port structure
  326  * @return the request description
  327  */
  328 const char *Util_portRequestDescription(Port_T p);
  329 
  330 
  331 /**
  332  * Print full port description \[<host>\]:<port>[request][ via TCP|TCPSSL|UDP]
  333  * @param p A port structure
  334  * @param buf Buffer
  335  * @param bufsize Buffer size
  336  * @return the buffer
  337  */
  338 char *Util_portDescription(Port_T p, char *buf, int bufsize);
  339 
  340 
  341 /**
  342  * Print a command description
  343  * @param command Command object
  344  * @param s A result buffer, must be large enough to hold STRLEN chars
  345  * @return A pointer to s
  346  */
  347 char *Util_commandDescription(command_t command, char s[STRLEN]);
  348 
  349 
  350 /**
  351  * Return string presentation of TIME_* unit
  352  *  @param time The TIME_* unit (see monit.h)
  353  *  @return string
  354  */
  355 const char *Util_timestr(int time);
  356 
  357 
  358 #endif
  359