ovsdb-server: Obtain SSL configuration from database.
[cascardo/ovs.git] / ovsdb / ovsdb-server.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb.h"
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 #include "column.h"
26 #include "command-line.h"
27 #include "daemon.h"
28 #include "file.h"
29 #include "json.h"
30 #include "jsonrpc.h"
31 #include "jsonrpc-server.h"
32 #include "leak-checker.h"
33 #include "list.h"
34 #include "ovsdb-data.h"
35 #include "ovsdb-types.h"
36 #include "ovsdb-error.h"
37 #include "poll-loop.h"
38 #include "process.h"
39 #include "row.h"
40 #include "stream-ssl.h"
41 #include "stream.h"
42 #include "svec.h"
43 #include "table.h"
44 #include "timeval.h"
45 #include "trigger.h"
46 #include "util.h"
47 #include "unixctl.h"
48
49 #include "vlog.h"
50 #define THIS_MODULE VLM_ovsdb_server
51
52 /* SSL configuration. */
53 static char *private_key_file;
54 static char *certificate_file;
55 static char *ca_cert_file;
56 static bool bootstrap_ca_cert;
57
58 static unixctl_cb_func ovsdb_server_exit;
59 static unixctl_cb_func ovsdb_server_compact;
60
61 static void parse_options(int argc, char *argv[], char **file_namep,
62                           struct shash *remotes, char **unixctl_pathp,
63                           char **run_command);
64 static void usage(void) NO_RETURN;
65
66 static void reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
67                                 const struct ovsdb *db, struct shash *remotes);
68
69 int
70 main(int argc, char *argv[])
71 {
72     char *unixctl_path = NULL;
73     char *run_command = NULL;
74     struct unixctl_server *unixctl;
75     struct ovsdb_jsonrpc_server *jsonrpc;
76     struct shash remotes;
77     struct ovsdb_error *error;
78     struct ovsdb_file *file;
79     struct ovsdb *db;
80     struct process *run_process;
81     char *file_name;
82     bool exiting;
83     int retval;
84
85     proctitle_init(argc, argv);
86     set_program_name(argv[0]);
87     time_init();
88     vlog_init();
89     signal(SIGPIPE, SIG_IGN);
90     process_init();
91
92     parse_options(argc, argv, &file_name, &remotes, &unixctl_path,
93                   &run_command);
94
95     die_if_already_running();
96     daemonize_start();
97
98     error = ovsdb_file_open(file_name, false, &db, &file);
99     if (error) {
100         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
101     }
102
103     jsonrpc = ovsdb_jsonrpc_server_create(db);
104     reconfigure_from_db(jsonrpc, db, &remotes);
105
106     retval = unixctl_server_create(unixctl_path, &unixctl);
107     if (retval) {
108         exit(EXIT_FAILURE);
109     }
110
111     if (run_command) {
112         char *run_argv[4];
113
114         run_argv[0] = "/bin/sh";
115         run_argv[1] = "-c";
116         run_argv[2] = run_command;
117         run_argv[3] = NULL;
118
119         retval = process_start(run_argv, NULL, 0, NULL, 0, &run_process);
120         if (retval) {
121             ovs_fatal(retval, "%s: process failed to start", run_command);
122         }
123     } else {
124         run_process = NULL;
125     }
126
127     daemonize_complete();
128
129     unixctl_command_register("exit", ovsdb_server_exit, &exiting);
130     unixctl_command_register("ovsdb-server/compact", ovsdb_server_compact,
131                              file);
132
133     exiting = false;
134     while (!exiting) {
135         reconfigure_from_db(jsonrpc, db, &remotes);
136         ovsdb_jsonrpc_server_run(jsonrpc);
137         unixctl_server_run(unixctl);
138         ovsdb_trigger_run(db, time_msec());
139         if (run_process && process_exited(run_process)) {
140             exiting = true;
141         }
142
143         ovsdb_jsonrpc_server_wait(jsonrpc);
144         unixctl_server_wait(unixctl);
145         ovsdb_trigger_wait(db, time_msec());
146         if (run_process) {
147             process_wait(run_process);
148         }
149         poll_block();
150     }
151     ovsdb_jsonrpc_server_destroy(jsonrpc);
152     ovsdb_destroy(db);
153     shash_destroy(&remotes);
154     unixctl_server_destroy(unixctl);
155
156     if (run_process && process_exited(run_process)) {
157         int status = process_status(run_process);
158         if (status) {
159             ovs_fatal(0, "%s: child exited, %s",
160                       run_command, process_status_msg(status));
161         }
162     }
163
164     return 0;
165 }
166
167 static void
168 parse_db_string_column(const struct ovsdb *db,
169                        const char *name_,
170                        const struct ovsdb_table **tablep,
171                        const struct ovsdb_column **columnp)
172 {
173     char *name, *table_name, *column_name;
174     const struct ovsdb_column *column;
175     const struct ovsdb_table *table;
176     char *save_ptr = NULL;
177
178     name = xstrdup(name_);
179     strtok_r(name, ":", &save_ptr); /* "db:" */
180     table_name = strtok_r(NULL, ",", &save_ptr);
181     column_name = strtok_r(NULL, ",", &save_ptr);
182     if (!table_name || !column_name) {
183         ovs_fatal(0, "\"%s\": invalid syntax", name_);
184     }
185
186     table = ovsdb_get_table(db, table_name);
187     if (!table) {
188         ovs_fatal(0, "\"%s\": no table named %s", name_, table_name);
189     }
190
191     column = ovsdb_table_schema_get_column(table->schema, column_name);
192     if (!column) {
193         ovs_fatal(0, "\"%s\": table \"%s\" has no column \"%s\"",
194                   name_, table_name, column_name);
195     }
196     free(name);
197
198     if (column->type.key.type != OVSDB_TYPE_STRING
199         || column->type.value.type != OVSDB_TYPE_VOID) {
200         ovs_fatal(0, "\"%s\": table \"%s\" column \"%s\" is "
201                   "not string or set of strings",
202                   name_, table->schema->name, column->name);
203     }
204
205     *columnp = column;
206     *tablep = table;
207 }
208
209 static const char *
210 query_db_string(const struct ovsdb *db, const char *name)
211 {
212     if (!name || strncmp(name, "db:", 3)) {
213         return name;
214     } else {
215         const struct ovsdb_column *column;
216         const struct ovsdb_table *table;
217         const struct ovsdb_row *row;
218
219         parse_db_string_column(db, name, &table, &column);
220
221         HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
222             const struct ovsdb_datum *datum;
223             size_t i;
224
225             datum = &row->fields[column->index];
226             for (i = 0; i < datum->n; i++) {
227                 if (datum->keys[i].string[0]) {
228                     return datum->keys[i].string;
229                 }
230             }
231         }
232         return NULL;
233     }
234 }
235
236 static void
237 query_db_remotes(const char *name, const struct ovsdb *db,
238                  struct shash *remotes)
239 {
240     const struct ovsdb_column *column;
241     const struct ovsdb_table *table;
242     const struct ovsdb_row *row;
243
244     parse_db_string_column(db, name, &table, &column);
245
246     HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
247         const struct ovsdb_datum *datum;
248         size_t i;
249
250         datum = &row->fields[column->index];
251         for (i = 0; i < datum->n; i++) {
252             shash_add_once(remotes, datum->keys[i].string, NULL);
253         }
254     }
255 }
256
257 /* Reconfigures ovsdb-server based on information in the database. */
258 static void
259 reconfigure_from_db(struct ovsdb_jsonrpc_server *jsonrpc,
260                     const struct ovsdb *db, struct shash *remotes)
261 {
262     struct shash resolved_remotes;
263     struct shash_node *node;
264
265     /* Configure remotes. */
266     shash_init(&resolved_remotes);
267     SHASH_FOR_EACH (node, remotes) {
268         const char *name = node->name;
269
270         if (!strncmp(name, "db:", 3)) {
271             query_db_remotes(name, db, &resolved_remotes);
272         } else {
273             shash_add_once(&resolved_remotes, name, NULL);
274         }
275     }
276     ovsdb_jsonrpc_server_set_remotes(jsonrpc, &resolved_remotes);
277     shash_destroy(&resolved_remotes);
278
279     /* Configure SSL. */
280     stream_ssl_set_private_key_file(query_db_string(db, private_key_file));
281     stream_ssl_set_certificate_file(query_db_string(db, certificate_file));
282     stream_ssl_set_ca_cert_file(query_db_string(db, ca_cert_file),
283                                 bootstrap_ca_cert);
284 }
285
286 static void
287 ovsdb_server_exit(struct unixctl_conn *conn, const char *args OVS_UNUSED,
288                   void *exiting_)
289 {
290     bool *exiting = exiting_;
291     *exiting = true;
292     unixctl_command_reply(conn, 200, NULL);
293 }
294
295 static void
296 ovsdb_server_compact(struct unixctl_conn *conn, const char *args OVS_UNUSED,
297                      void *file_)
298 {
299     struct ovsdb_file *file = file_;
300     struct ovsdb_error *error;
301
302     VLOG_INFO("compacting database by user request");
303     error = ovsdb_file_compact(file);
304     if (!error) {
305         unixctl_command_reply(conn, 200, NULL);
306     } else {
307         char *s = ovsdb_error_to_string(error);
308         ovsdb_error_destroy(error);
309         unixctl_command_reply(conn, 503, s);
310         free(s);
311     }
312 }
313
314 static void
315 parse_options(int argc, char *argv[], char **file_namep,
316               struct shash *remotes, char **unixctl_pathp,
317               char **run_command)
318 {
319     enum {
320         OPT_DUMMY = UCHAR_MAX + 1,
321         OPT_REMOTE,
322         OPT_UNIXCTL,
323         OPT_RUN,
324         OPT_BOOTSTRAP_CA_CERT,
325         VLOG_OPTION_ENUMS,
326         LEAK_CHECKER_OPTION_ENUMS
327     };
328     static struct option long_options[] = {
329         {"remote",      required_argument, 0, OPT_REMOTE},
330         {"unixctl",     required_argument, 0, OPT_UNIXCTL},
331         {"run",         required_argument, 0, OPT_RUN},
332         {"help",        no_argument, 0, 'h'},
333         {"version",     no_argument, 0, 'V'},
334         DAEMON_LONG_OPTIONS,
335         VLOG_LONG_OPTIONS,
336         LEAK_CHECKER_LONG_OPTIONS,
337 #ifdef HAVE_OPENSSL
338         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
339         {"private-key", required_argument, 0, 'p'},
340         {"certificate", required_argument, 0, 'c'},
341         {"ca-cert",     required_argument, 0, 'C'},
342 #endif
343         {0, 0, 0, 0},
344     };
345     char *short_options = long_options_to_short_options(long_options);
346
347     shash_init(remotes);
348     for (;;) {
349         int c;
350
351         c = getopt_long(argc, argv, short_options, long_options, NULL);
352         if (c == -1) {
353             break;
354         }
355
356         switch (c) {
357         case OPT_REMOTE:
358             shash_add_once(remotes, optarg, NULL);
359             break;
360
361         case OPT_UNIXCTL:
362             *unixctl_pathp = optarg;
363             break;
364
365         case OPT_RUN:
366             *run_command = optarg;
367             break;
368
369         case 'h':
370             usage();
371
372         case 'V':
373             OVS_PRINT_VERSION(0, 0);
374             exit(EXIT_SUCCESS);
375
376         VLOG_OPTION_HANDLERS
377         DAEMON_OPTION_HANDLERS
378         LEAK_CHECKER_OPTION_HANDLERS
379
380 #ifdef HAVE_OPENSSL
381         case 'p':
382             private_key_file = optarg;
383             break;
384
385         case 'c':
386             certificate_file = optarg;
387             break;
388
389         case 'C':
390             ca_cert_file = optarg;
391             bootstrap_ca_cert = false;
392             break;
393
394         case OPT_BOOTSTRAP_CA_CERT:
395             ca_cert_file = optarg;
396             bootstrap_ca_cert = true;
397             break;
398 #endif
399
400         case '?':
401             exit(EXIT_FAILURE);
402
403         default:
404             abort();
405         }
406     }
407     free(short_options);
408
409     argc -= optind;
410     argv += optind;
411
412     if (argc > 1) {
413         ovs_fatal(0, "database file is only non-option argument; "
414                 "use --help for usage");
415     } else if (argc < 1) {
416         ovs_fatal(0, "missing database file argument; use --help for usage");
417     }
418
419     *file_namep = argv[0];
420 }
421
422 static void
423 usage(void)
424 {
425     printf("%s: Open vSwitch database server\n"
426            "usage: %s [OPTIONS] DATABASE\n"
427            "where DATABASE is a database file in ovsdb format.\n",
428            program_name, program_name);
429     printf("\nJSON-RPC options (may be specified any number of times):\n"
430            "  --remote=REMOTE         connect or listen to REMOTE\n");
431     stream_usage("JSON-RPC", true, true, true);
432     daemon_usage();
433     vlog_usage();
434     printf("\nOther options:\n"
435            "  --run COMMAND           run COMMAND as subprocess then exit\n"
436            "  -h, --help              display this help message\n"
437            "  -V, --version           display version information\n");
438     leak_checker_usage();
439     exit(EXIT_SUCCESS);
440 }