Code

Need to add in the libgnu.la for Solaris and other platforms
[nagiosplug.git] / lib / utils_base.c
index 18fb3f25143559134128036c286555f0d0505d56..d1453c6717c0f11ccc05107df8f485d53f64d86a 100644 (file)
@@ -1,19 +1,34 @@
 /*****************************************************************************
- *
- * utils_base.c
- *
- * Library of useful functions for plugins
- * These functions are tested with libtap. See tests/ directory
- *
- * Copyright (c) 2006 Nagios Plugin Development Team
- * License: GPL
- *
- * $Revision$
- * $Date$
- ****************************************************************************/
+*
+* utils_base.c
+*
+* License: GPL
+* Copyright (c) 2006 Nagios Plugins Development Team
+*
+* Last Modified: $Date$
+*
+* Library of useful functions for plugins
+* 
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+* 
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+* 
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+* 
+* $Id$
+*
+*****************************************************************************/
 
-#include <stdarg.h>
 #include "common.h"
+#include <stdarg.h>
 #include "utils_base.h"
 
 void
@@ -105,10 +120,6 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
                }
        }
 
-       if (*my_thresholds > 0) {       /* Not sure why, but sometimes could be -1 */
-               /* printf("Freeing here: %d\n", *my_thresholds); */
-               free(*my_thresholds);
-       }
        *my_thresholds = temp_thresholds;
 
        return 0;
@@ -151,34 +162,34 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
 int
 check_range(double value, range *my_range)
 {
-       int false = FALSE;
-       int true = TRUE;
+       int no = FALSE;
+       int yes = TRUE;
        
        if (my_range->alert_on == INSIDE) {
-               false = TRUE;
-               true = FALSE;
+               no = TRUE;
+               yes = FALSE;
        }
 
        if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
                if ((my_range->start <= value) && (value <= my_range->end)) {
-                       return false;
+                       return no;
                } else {
-                       return true;
+                       return yes;
                }
        } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
                if (my_range->start <= value) {
-                       return false;
+                       return no;
                } else {
-                       return true;
+                       return yes;
                }
        } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
                if (value <= my_range->end) {
-                       return false;
+                       return no;
                } else {
-                       return true;
+                       return yes;
                }
        } else {
-               return false;
+               return no;
        }
 }
 
@@ -228,3 +239,18 @@ char *np_escaped_string (const char *string) {
        data[j] = '\0';
        return data;
 }
+
+int np_check_if_root(void) { return (geteuid() == 0); }
+
+int np_warn_if_not_root(void) {
+       int status = np_check_if_root();
+       if(!status) {
+               printf(_("Warning: "));
+               printf(_("This plugin must be either run as root or setuid root.\n"));
+               printf(_("To run as root, you can use a tool like sudo.\n"));
+               printf(_("To set the setuid permissions, use the command:\n"));
+               /* XXX could we use something like progname? */
+               printf("\tchmod u+s yourpluginfile\n");
+       }
+       return status;
+}