root/src/stratcond.c

Revision 229c7f9e18cc5409c42a955a656fdb31df1f7462, 7.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 months ago)

use 0.0.2 APIs to apply ACLs to reverse connections

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007-2010, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  * Copyright (c) 2015, Circonus, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials provided
15  *       with the distribution.
16  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
17  *       of its contributors may be used to endorse or promote products
18  *       derived from this software without specific prior written
19  *       permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <mtev_defines.h>
35 #include "noit_version.h"
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <limits.h>
44
45 #include <eventer/eventer.h>
46 #include <mtev_memory.h>
47 #include <mtev_log.h>
48 #include <mtev_hash.h>
49 #include <mtev_security.h>
50 #include <mtev_watchdog.h>
51 #include <mtev_lockfile.h>
52 #include <mtev_main.h>
53 #include <mtev_listener.h>
54 #include <mtev_console.h>
55 #include <mtev_conf.h>
56 #include <mtev_rest.h>
57 #include <mtev_reverse_socket.h>
58 #include <mtev_events_rest.h>
59 #include <mtev_capabilities_listener.h>
60
61 #include "noit_mtev_bridge.h"
62 #include "noit_config.h"
63 #include "noit_module.h"
64 #include "stratcon_jlog_streamer.h"
65 #include "stratcon_datastore.h"
66 #include "stratcon_iep.h"
67 #include "stratcon_realtime_http.h"
68
69 #define APPNAME "stratcon"
70 static char *config_file = ETC_DIR "/" APPNAME ".conf";
71 static const char *droptouser = NULL;
72 static const char *droptogroup = NULL;
73 static const char *chrootpath = NULL;
74 static int foreground = 0;
75 static int debug = 0;
76 static int strict_module_load = 0;
77 static char *glider = NULL;
78
79 #include "man/stratcond.usage.h"
80 static void usage(const char *progname) {
81   printf("Usage for %s:\n", progname);
82 #ifdef STRATCOND_USAGE
83   assert(write(STDOUT_FILENO,
84               STRATCOND_USAGE,
85               sizeof(STRATCOND_USAGE)-1) == sizeof(STRATCOND_USAGE)-1);
86 #else
87   printf("\nError in usage, build problem.\n");
88 #endif
89   return;
90 }
91
92 void parse_clargs(int argc, char **argv) {
93   int c;
94   while((c = getopt(argc, argv, "Mrshc:dDu:g:n:t:l:L:G:")) != EOF) {
95     switch(c) {
96       case 'M':
97         strict_module_load = 1;
98         break;
99       case 'G':
100         glider = strdup(optarg);
101         break;
102       case 'l':
103         mtev_main_enable_log(optarg);
104         break;
105       case 'L':
106         mtev_main_disable_log(optarg);
107         break;
108       case 'n':
109         {
110           char *cp = strchr(optarg, ':');
111           if(!cp) mtev_listener_skip(optarg, 0);
112           else {
113             if(cp == optarg) {
114               *cp++ = '\0';
115               mtev_listener_skip(NULL, atoi(cp));
116             }
117             else {
118               *cp++ = '\0';
119               mtev_listener_skip(optarg, atoi(cp));
120             }
121           }
122         }
123         break;
124       case 'r':
125         stratcon_iep_set_enabled(0);
126         break;
127       case 's':
128         stratcon_datastore_set_enabled(0);
129         break;
130       case 'h':
131         usage(argv[0]);
132         exit(1);
133         break;
134       case 'u':
135         droptouser = strdup(optarg);
136         break;
137       case 'g':
138         droptogroup = strdup(optarg);
139         break;
140       case 't':
141         chrootpath = strdup(optarg);
142         break;
143       case 'c':
144         config_file = strdup(optarg);
145         break;
146       case 'd':
147         debug++;
148         break;
149       case 'D':
150         foreground++;
151         break;
152       default:
153         break;
154     }
155   }
156 }
157
158 static
159 int configure_eventer() {
160   int rv = 0;
161   mtev_hash_table *table;
162   table = mtev_conf_get_hash(NULL, "/" APPNAME "/eventer/config");
163   if(table) {
164     mtev_hash_iter iter = MTEV_HASH_ITER_ZERO;
165     const char *key, *value;
166     int klen;
167     while(mtev_hash_next_str(table, &iter, &key, &klen, &value)) {
168       int subrv;
169       if((subrv = eventer_propset(key, value)) != 0)
170         rv = subrv;
171     }
172     mtev_hash_destroy(table, free, free);
173     free(table);
174   }
175   return rv;
176 }
177
178 const char *reverse_prefix = "noit/"; /* namespace out connections */
179 const char *reverse_prefix_cns[] = { "noit/", NULL };
180
181 static int child_main() {
182   char conf_str[1024];
183   char stratcon_version[80];
184
185   mtev_watchdog_child_heartbeat();
186
187   /* Next (re)load the configs */
188   if(mtev_conf_load(config_file) == -1) {
189     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
190     exit(2);
191   }
192
193   mtev_log_reopen_all();
194   mtevL(noit_notice, "process starting: %d\n", (int)getpid());
195   mtev_log_go_asynch();
196
197   /* Lastly, run through all other system inits */
198   if(!mtev_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
199                               conf_str, sizeof(conf_str))) {
200     mtevL(noit_stderr, "Cannot find '%s' in configuration\n",
201           "/" APPNAME "/eventer/@implementation");
202     exit(2);
203   }
204   if(eventer_choose(conf_str) == -1) {
205     mtevL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
206     exit(2);
207   }
208   if(configure_eventer() != 0) {
209     mtevL(noit_stderr, "Cannot configure eventer\n");
210     exit(2);
211   }
212   if(eventer_init() == -1) {
213     mtevL(noit_stderr, "Cannot init eventer %s\n", conf_str);
214     exit(2);
215   }
216   /* rotation init requires, eventer_init() */
217   mtev_conf_log_init_rotate(APPNAME, mtev_false);
218
219   mtev_watchdog_child_eventer_heartbeat();
220
221   mtev_console_init(APPNAME);
222   mtev_console_conf_init();
223   mtev_http_rest_init();
224   mtev_reverse_socket_init(reverse_prefix, reverse_prefix_cns);
225   mtev_reverse_socket_acl(mtev_reverse_socket_denier);
226   mtev_events_rest_init();
227   stratcon_realtime_http_init(APPNAME);
228   mtev_capabilities_listener_init();
229   noit_build_version(stratcon_version, sizeof(stratcon_version));
230   mtev_capabilities_add_feature("stratcon", stratcon_version);
231   mtev_listener_init(APPNAME);
232
233   mtev_dso_init();
234   mtev_dso_post_init();
235   if(strict_module_load && mtev_dso_load_failures() > 0) {
236     mtevL(noit_stderr, "Failed to load some modules and -M given.\n");
237     exit(2);
238   }
239
240   if(stratcon_datastore_get_enabled())
241     stratcon_datastore_init();
242
243   /* Drop privileges */
244   mtev_conf_security_init(APPNAME, droptouser, droptogroup, chrootpath);
245
246   stratcon_jlog_streamer_init(APPNAME);
247
248   if(stratcon_iep_get_enabled())
249     stratcon_iep_init();
250   if(stratcon_datastore_get_enabled()) {
251     /* Write our log out, and setup a watchdog to write it out on change. */
252     stratcon_datastore_saveconfig(NULL);
253     mtev_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
254   }
255   else
256     mtev_conf_coalesce_changes(INT_MAX);
257
258   mtev_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL);
259
260   eventer_loop();
261   return 0;
262 }
263
264 int main(int argc, char **argv) {
265   noit_mtev_bridge_init();
266   mtev_memory_init();
267   parse_clargs(argc, argv);
268   return mtev_main(APPNAME, config_file, debug, foreground,
269                    MTEV_LOCK_OP_LOCK, glider, droptouser, droptogroup,
270                    child_main);
271 }
Note: See TracBrowser for help on using the browser.