root/src/noit_check_tools_shared.c

Revision b553f9a4d73487ee85e5cfedf2cd67352b4d5e6b, 6.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

refs #340

This is a rather major change. Targets can now be hostnames in addition
to IP addresses so long as //checks/@resolve_targets is not false.

If a target is entered that does not look like an IP (inet_pton fails)
then the check is marked as needing resolution NP_RESOLVE.

A passive look-aside dns cache has been implemented in noit_check_resolver.c
that is used to power the whole system and some reasonably simply console
command have been provided:

show dns_cache [fqdn1 [fqdn2]] -- shows the state
dns_cache <fqdn> [fqdn2 [fqdn3]] -- submits for lookup
no dns_cache <fqdn> [fqdn2 [fqdn3]] -- purges from cache

The big change is that modules that relied on check->target to be an IP
address are going to explode when names are provided. Instead, modules
should now use target for the provided target (possibly a FQDN) and use
target_ip (check->target_ip or check.target_ip) for a resolved IP address
and also check for the case of empty string: (check->target_ip[0] == '\0')
for the case that resolution has failed. In lua, the target_ip will be
presented as nil in the case of failed name resolution.

I believe I've updated all necessary components of the system for this to
"just work" but people that have implemented their own check should update
them before they elect to use non-IP addresses as targets.

The dns subsystem supports both IPv4 and IPv6, but currently prefers IPv4
addresses if any are present.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "noit_check_tools.h"
35 #include "utils/noit_str.h"
36
37 #include <assert.h>
38
39 static noit_hash_table interpolation_operators = NOIT_HASH_EMPTY;
40
41 static int
42 interpolate_oper_copy(char *buff, int len, const char *replacement) {
43   strlcpy(buff, replacement, len);
44   return strlen(buff);
45 }
46 static int
47 interpolate_oper_ccns(char *buff, int len, const char *replacement) {
48   char *start;
49   start = strstr(replacement, "::");
50   return interpolate_oper_copy(buff, len, start ? (start + 2) : replacement);
51 }
52
53 int
54 noit_check_interpolate_register_oper_fn(const char *name,
55                                         intperpolate_oper_fn f) {
56   noit_hash_replace(&interpolation_operators,
57                     strdup(name), strlen(name),
58                     (void *)f,
59                     free, NULL);
60   return 0;
61 }
62
63 int
64 noit_check_interpolate(char *buff, int len, const char *fmt,
65                        noit_hash_table *attrs,
66                        noit_hash_table *config) {
67   char *copy = NULL;
68   char closer;
69   const char *fmte, *key;
70   int replaced_something = 1;
71   int iterations = 3;
72
73   while(replaced_something && iterations > 0) {
74     char *cp = buff, * const end = buff + len;
75     iterations--;
76     replaced_something = 0;
77     while(*fmt && cp < end) {
78       switch(*fmt) {
79         case '%':
80           if(fmt[1] == '{' || fmt[1] == '[') {
81             closer = (fmt[1] == '{') ? '}' : ']';
82             fmte = fmt + 2;
83             key = fmte;
84             while(*fmte && *fmte != closer) fmte++;
85             if(*fmte == closer) {
86               /* We have a full key here */
87               const char *replacement, *oper, *nkey;
88               intperpolate_oper_fn oper_sprint;
89
90               /* keys can be of the form: :operator:key */
91               oper = key;
92               if(*oper == ':' &&
93                  (nkey = strnstrn(":", 1, oper + 1, fmte - key - 1)) != NULL) {
94                 void *voper;
95                 oper++;
96                 /* find oper, nkey-oper */
97                 if(!noit_hash_retrieve(&interpolation_operators,
98                                        oper, nkey - oper,
99                                        &voper)) {
100                   /* else oper <- copy */
101                   oper_sprint = interpolate_oper_copy;
102                 }
103                 else
104                   oper_sprint = (intperpolate_oper_fn)voper;
105                 nkey++;
106               }
107               else {
108                 oper_sprint = interpolate_oper_copy;
109                 nkey = key;
110               }
111               if(!noit_hash_retr_str((closer == '}') ?  config : attrs,
112                                      nkey, fmte - nkey, &replacement))
113                 replacement = "";
114               fmt = fmte + 1; /* Format points just after the end of the key */
115               cp += oper_sprint(cp, end-cp, replacement);
116               *(end-1) = '\0'; /* In case the oper_sprint didn't teminate */
117               replaced_something = 1;
118               break;
119             }
120           }
121         default:
122           *cp++ = *fmt++;
123       }
124     }
125     *cp = '\0';
126     if(copy) free(copy);
127     if(replaced_something)
128       copy = strdup(buff);
129     fmt = copy;
130   }
131   return strlen(buff);
132 }
133
134 void
135 noit_check_extended_id_split(const char *in, int len,
136                              char *target, int target_len,
137                              char *module, int module_len,
138                              char *name, int name_len,
139                              char *uuid, int uuid_len) {
140   if(target) *target = '\0';
141   if(module) *module = '\0';
142   if(name) *name = '\0';
143   if(uuid) *uuid = '\0';
144   if(len >= UUID_STR_LEN) {
145     memcpy(uuid, in + len - UUID_STR_LEN, UUID_STR_LEN);
146     uuid[UUID_STR_LEN] = '\0';
147   }
148   if(len > UUID_STR_LEN) {
149     const char *tcp = in;
150     const char *mcp, *ncp, *ucp;
151     /* find the end of the target */
152     mcp = strchr(tcp,'`');
153     if(!mcp) return;
154     /* copy in the target */
155     if(target_len > mcp-tcp) {
156       memcpy(target,tcp,mcp-tcp);
157       target[mcp-tcp] = '\0';
158     }
159     mcp++;
160     ncp = strchr(mcp,'`');
161     if(!ncp) return;
162     /* copy in the module */
163     if(module_len > ncp-mcp) {
164       memcpy(module,mcp,ncp-mcp);
165       module[ncp-mcp] = '\0';
166     }
167     ncp++;
168     /* copy in the name */
169     ucp = in + len - UUID_STR_LEN - 1;
170     if(ncp < ucp) {
171       if(name_len > ucp-ncp) {
172         memcpy(name, ncp, ucp-ncp);
173         name[ucp-ncp] = '\0';
174       }
175     }
176   }
177 }
178
179 void
180 noit_check_make_attrs(noit_check_t *check, noit_hash_table *attrs) {
181 #define CA_STORE(a,b) noit_hash_store(attrs, a, strlen(a), b)
182   CA_STORE("target", check->target);
183   CA_STORE("target_ip", check->target_ip);
184   CA_STORE("name", check->name);
185   CA_STORE("module", check->module);
186 }
187 void
188 noit_check_release_attrs(noit_hash_table *attrs) {
189   noit_hash_destroy(attrs, NULL, NULL);
190 }
191
192 void
193 noit_check_tools_shared_init() {
194   noit_check_interpolate_register_oper_fn("copy", interpolate_oper_copy);
195   noit_check_interpolate_register_oper_fn("ccns", interpolate_oper_ccns);
196 }
197
Note: See TracBrowser for help on using the browser.