diff options
Diffstat (limited to 'plugins/utils.c')
| -rw-r--r-- | plugins/utils.c | 72 |
1 files changed, 42 insertions, 30 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index aaa9fe5a..0d250677 100644 --- a/plugins/utils.c +++ b/plugins/utils.c | |||
| @@ -17,6 +17,8 @@ | |||
| 17 | #include <stdarg.h> | 17 | #include <stdarg.h> |
| 18 | #include <limits.h> | 18 | #include <limits.h> |
| 19 | 19 | ||
| 20 | #include <arpa/inet.h> | ||
| 21 | |||
| 20 | extern int timeout_interval; | 22 | extern int timeout_interval; |
| 21 | extern const char *progname; | 23 | extern const char *progname; |
| 22 | 24 | ||
| @@ -27,7 +29,10 @@ void terminate (int, const char *fmt, ...); | |||
| 27 | RETSIGTYPE timeout_alarm_handler (int); | 29 | RETSIGTYPE timeout_alarm_handler (int); |
| 28 | 30 | ||
| 29 | int is_host (char *); | 31 | int is_host (char *); |
| 30 | int is_dotted_quad (char *); | 32 | int is_addr (char *); |
| 33 | int resolve_host_or_addr (char *, int); | ||
| 34 | int is_inet_addr (char *); | ||
| 35 | int is_inet6_addr (char *); | ||
| 31 | int is_hostname (char *); | 36 | int is_hostname (char *); |
| 32 | 37 | ||
| 33 | int is_integer (char *); | 38 | int is_integer (char *); |
| @@ -58,7 +63,7 @@ char *strpcat (char *dest, const char *src, const char *str); | |||
| 58 | #define TXTBLK 128 | 63 | #define TXTBLK 128 |
| 59 | 64 | ||
| 60 | /* ************************************************************************** | 65 | /* ************************************************************************** |
| 61 | * max_state(STATE_x, STATE_y) | 66 | /* max_state(STATE_x, STATE_y) |
| 62 | * compares STATE_x to STATE_y and returns result based on the following | 67 | * compares STATE_x to STATE_y and returns result based on the following |
| 63 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL | 68 | * STATE_UNKNOWN < STATE_OK < STATE_WARNING < STATE_CRITICAL |
| 64 | * | 69 | * |
| @@ -167,28 +172,50 @@ timeout_alarm_handler (int signo) | |||
| 167 | int | 172 | int |
| 168 | is_host (char *address) | 173 | is_host (char *address) |
| 169 | { | 174 | { |
| 170 | if (is_dotted_quad (address) || is_hostname (address)) | 175 | if (is_addr (address) || is_hostname (address)) |
| 171 | return (TRUE); | 176 | return (TRUE); |
| 177 | |||
| 172 | return (FALSE); | 178 | return (FALSE); |
| 173 | } | 179 | } |
| 174 | 180 | ||
| 175 | int | 181 | int |
| 176 | is_dotted_quad (char *address) | 182 | is_addr (char *address) |
| 177 | { | 183 | { |
| 178 | int o1, o2, o3, o4; | 184 | if (is_inet_addr (address) || is_inet6_addr (address)) |
| 179 | char c[1]; | 185 | return (TRUE); |
| 180 | 186 | ||
| 181 | if (!address) | 187 | return (FALSE); |
| 182 | return FALSE; | 188 | } |
| 183 | 189 | ||
| 184 | if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4) | 190 | int |
| 185 | return FALSE; | 191 | resolve_host_or_addr (char *address, int family) |
| 186 | else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255) | 192 | { |
| 187 | return FALSE; | 193 | struct addrinfo hints; |
| 188 | else if (o1 < 0 || o2 < 0 || o3 < 0 || o4 < 0) | 194 | struct addrinfo *res; |
| 195 | int retval; | ||
| 196 | |||
| 197 | memset (&hints, 0, sizeof (hints)); | ||
| 198 | hints.ai_family = family; | ||
| 199 | retval = getaddrinfo (address, NULL, &hints, &res); | ||
| 200 | |||
| 201 | if (retval != 0) | ||
| 189 | return FALSE; | 202 | return FALSE; |
| 190 | else | 203 | else { |
| 204 | freeaddrinfo (res); | ||
| 191 | return TRUE; | 205 | return TRUE; |
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | int | ||
| 210 | is_inet_addr (char *address) | ||
| 211 | { | ||
| 212 | return resolve_host_or_addr (address, AF_INET); | ||
| 213 | } | ||
| 214 | |||
| 215 | int | ||
| 216 | is_inet6_addr (char *address) | ||
| 217 | { | ||
| 218 | return resolve_host_or_addr (address, AF_INET6); | ||
| 192 | } | 219 | } |
| 193 | 220 | ||
| 194 | /* from RFC-1035 | 221 | /* from RFC-1035 |
| @@ -201,22 +228,7 @@ is_dotted_quad (char *address) | |||
| 201 | int | 228 | int |
| 202 | is_hostname (char *s1) | 229 | is_hostname (char *s1) |
| 203 | { | 230 | { |
| 204 | if (!s1 || strlen (s1) > 63) { | 231 | return resolve_host_or_addr (s1, AF_UNSPEC); |
| 205 | return FALSE; | ||
| 206 | } | ||
| 207 | if (strcspn (s1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") != 0) { | ||
| 208 | return FALSE; | ||
| 209 | } | ||
| 210 | if (strspn (s1, "0123456789-.") == 1) { | ||
| 211 | return FALSE; | ||
| 212 | } | ||
| 213 | while ((s1 = index (s1, '.'))) { | ||
| 214 | s1++; | ||
| 215 | if (strspn (s1, "0123456789-.") == 1) { | ||
| 216 | return FALSE; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | return TRUE; | ||
| 220 | } | 232 | } |
| 221 | 233 | ||
| 222 | int | 234 | int |
