Code

sysdb: Add -A option to specify a CA certificates file.
[sysdb.git] / src / tools / sysdb / main.c
1 /*
2  * SysDB - src/tools/sysdb/main.c
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * 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
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "tools/sysdb/command.h"
33 #include "tools/sysdb/input.h"
35 #include "client/sysdb.h"
36 #include "client/sock.h"
37 #include "utils/error.h"
38 #include "utils/llist.h"
39 #include "utils/strbuf.h"
40 #include "utils/os.h"
42 #include <errno.h>
44 #if HAVE_LIBGEN_H
45 #       include <libgen.h>
46 #else /* HAVE_LIBGEN_H */
47 #       define basename(path) (path)
48 #endif /* ! HAVE_LIBGEN_H */
50 #include <sys/stat.h>
51 #include <fcntl.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
57 #include <unistd.h>
59 #include <sys/types.h>
61 #include <pwd.h>
63 #if HAVE_EDITLINE_READLINE_H
64 #       include <editline/readline.h>
65 #       if HAVE_EDITLINE_HISTORY_H
66 #               include <editline/history.h>
67 #       endif
68 #elif HAVE_READLINE_READLINE_H
69 #       include <readline/readline.h>
70 #       if HAVE_READLINE_HISTORY_H
71 #               include <readline/history.h>
72 #       endif
73 #elif HAVE_READLINE_H
74 #       include <readline.h>
75 #       if HAVE_HISTORY_H
76 #               include <history.h>
77 #       endif
78 #endif /* READLINEs */
80 #include <openssl/ssl.h>
81 #include <openssl/err.h>
83 #ifndef DEFAULT_SOCKET
84 #       define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
85 #endif
87 static sdb_ssl_options_t ssl_options = {
88         /* ca_file */   SDB_SSL_CAFILE,
89         /* key_file */  "~/.config/sysdb/ssl/key.pem",
90         /* cert_file */ "~/.config/sysdb/ssl/cert.pem",
91         /* crl_file */  "~/.config/sysdb/ssl/crl.pem",
92 };
94 static void
95 canonicalize_ssl_options(void)
96 {
97         char *tmp;
98         if (ssl_options.ca_file) {
99                 tmp = sdb_realpath(ssl_options.ca_file);
100                 ssl_options.ca_file = tmp ? tmp : strdup(ssl_options.ca_file);
101         }
102         if (ssl_options.key_file) {
103                 tmp = sdb_realpath(ssl_options.key_file);
104                 ssl_options.key_file = tmp ? tmp : strdup(ssl_options.key_file);
105         }
106         if (ssl_options.cert_file) {
107                 tmp = sdb_realpath(ssl_options.cert_file);
108                 ssl_options.cert_file = tmp ? tmp : strdup(ssl_options.cert_file);
109         }
110         if (ssl_options.crl_file) {
111                 tmp = sdb_realpath(ssl_options.crl_file);
112                 ssl_options.crl_file = tmp ? tmp : strdup(ssl_options.crl_file);
113         }
114 } /* canonicalize_ssl_options */
116 static void
117 exit_usage(char *name, int status)
119         char *user = sdb_get_current_user();
120         printf(
121 "Usage: %s <options>\n"
123 "Connection options:\n"
124 "  -H HOST      the host to connect to\n"
125 "               default: "DEFAULT_SOCKET"\n"
126 "  -U USER      the username to connect as\n"
127 "               default: %s\n"
128 "  -c CMD       execute the specified command and then exit\n"
129 "\n"
130 "SSL options:\n"
131 "  -K KEYFILE   private key file name\n"
132 "               default: %s\n"
133 "  -C CERTFILE  client certificate file name\n"
134 "               default: %s\n"
135 "  -A CAFILE    CA certificates file name\n"
136 "               default: %s\n"
137 "\n"
138 "General options:\n"
139 "\n"
140 "  -h           display this help and exit\n"
141 "  -V           display the version number and copyright\n"
143 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
144 PACKAGE_URL"\n", basename(name), user,
145                         ssl_options.key_file, ssl_options.cert_file, ssl_options.ca_file);
147         free(user);
148         exit(status);
149 } /* exit_usage */
151 static void
152 exit_version(void)
154         printf("SysDB version "SDB_CLIENT_VERSION_STRING
155                         SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
156                         "using libsysdbclient version %s%s\n"
157                         "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
159                         "\nThis is free software under the terms of the BSD license, see "
160                         "the source for\ncopying conditions. There is NO WARRANTY; not "
161                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
162                         "PURPOSE.\n", sdb_client_version_string(),
163                         sdb_client_version_extra());
164         exit(0);
165 } /* exit_version */
167 static int
168 execute_commands(sdb_client_t *client, sdb_llist_t *commands)
170         sdb_llist_iter_t *iter;
171         int status = 0;
173         iter = sdb_llist_get_iter(commands);
174         if (! iter) {
175                 sdb_log(SDB_LOG_ERR, "Failed to iterate commands");
176                 return 1;
177         }
179         while (sdb_llist_iter_has_next(iter)) {
180                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
182                 if (sdb_client_send(client, SDB_CONNECTION_QUERY,
183                                         (uint32_t)strlen(obj->name), obj->name) <= 0) {
184                         sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
185                                         obj->name);
186                         status = 1;
187                         break;
188                 }
190                 /* Wait for server replies. We might get any number of log messages
191                  * but eventually see the reply to the query, which is either DATA or
192                  * ERROR. */
193                 while (42) {
194                         status = sdb_command_print_reply(client);
195                         if (status < 0) {
196                                 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
197                                 break;
198                         }
200                         if ((status == SDB_CONNECTION_DATA)
201                                         || (status == SDB_CONNECTION_ERROR))
202                                 break;
203                         if (status == SDB_CONNECTION_OK) {
204                                 /* pre 0.4 versions used OK instead of DATA */
205                                 sdb_log(SDB_LOG_WARNING, "Received unexpected OK status from "
206                                                 "server in response to a QUERY (expected DATA); "
207                                                 "assuming we're talking to an old server");
208                                 break;
209                         }
210                 }
212                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
213                         break; /* error */
214         }
216         sdb_llist_iter_destroy(iter);
217         return status;
218 } /* execute_commands */
220 int
221 main(int argc, char **argv)
223         const char *host = NULL;
225         char *homedir;
226         char hist_file[1024] = "";
228         sdb_input_t input = SDB_INPUT_INIT;
229         sdb_llist_t *commands = NULL;
231         while (42) {
232                 int opt = getopt(argc, argv, "H:U:c:C:K:A:hV");
234                 if (-1 == opt)
235                         break;
237                 switch (opt) {
238                         case 'H':
239                                 host = optarg;
240                                 break;
241                         case 'U':
242                                 input.user = optarg;
243                                 break;
245                         case 'c':
246                                 {
247                                         sdb_object_t *obj;
249                                         if (! commands)
250                                                 commands = sdb_llist_create();
251                                         if (! commands) {
252                                                 sdb_log(SDB_LOG_ERR, "Failed to create list object");
253                                                 exit(1);
254                                         }
256                                         if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
257                                                 sdb_log(SDB_LOG_ERR, "Failed to create object");
258                                                 exit(1);
259                                         }
260                                         if (sdb_llist_append(commands, obj)) {
261                                                 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
262                                                 sdb_object_deref(obj);
263                                                 exit(1);
264                                         }
265                                         sdb_object_deref(obj);
266                                 }
267                                 break;
269                         case 'C':
270                                 ssl_options.cert_file = optarg;
271                                 break;
272                         case 'K':
273                                 ssl_options.key_file = optarg;
274                                 break;
275                         case 'A':
276                                 ssl_options.ca_file = optarg;
277                                 break;
279                         case 'h':
280                                 exit_usage(argv[0], 0);
281                                 break;
282                         case 'V':
283                                 exit_version();
284                                 break;
285                         default:
286                                 exit_usage(argv[0], 1);
287                 }
288         }
290         if (optind < argc)
291                 exit_usage(argv[0], 1);
293         if (! host)
294                 host = DEFAULT_SOCKET;
295         if (! input.user)
296                 input.user = sdb_get_current_user();
297         else
298                 input.user = strdup(input.user);
299         if (! input.user)
300                 exit(1);
302         SSL_load_error_strings();
303         OpenSSL_add_ssl_algorithms();
305         input.client = sdb_client_create(host);
306         if (! input.client) {
307                 sdb_log(SDB_LOG_ERR, "Failed to create client object");
308                 sdb_input_reset(&input);
309                 exit(1);
310         }
311         canonicalize_ssl_options();
312         if (sdb_client_set_ssl_options(input.client, &ssl_options)) {
313                 sdb_log(SDB_LOG_ERR, "Failed to apply SSL options");
314                 sdb_input_reset(&input);
315                 sdb_ssl_free_options(&ssl_options);
316                 exit(1);
317         }
318         sdb_ssl_free_options(&ssl_options);
319         if (sdb_client_connect(input.client, input.user)) {
320                 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
321                 sdb_input_reset(&input);
322                 exit(1);
323         }
325         if (commands) {
326                 int status = execute_commands(input.client, commands);
327                 sdb_llist_destroy(commands);
328                 sdb_input_reset(&input);
329                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
330                         exit(1);
331                 exit(0);
332         }
334         sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
335                         SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
336                         sdb_client_version_string(), sdb_client_version_extra());
338         using_history();
340         if ((homedir = sdb_get_homedir())) {
341                 snprintf(hist_file, sizeof(hist_file) - 1,
342                                 "%s/.sysdb_history", homedir);
343                 hist_file[sizeof(hist_file) - 1] = '\0';
344                 free(homedir);
345                 homedir = NULL;
347                 errno = 0;
348                 if (read_history(hist_file) && (errno != ENOENT)) {
349                         char errbuf[1024];
350                         sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
351                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
352                 }
353         }
355         input.input = sdb_strbuf_create(2048);
356         sdb_input_init(&input);
357         sdb_input_mainloop();
359         sdb_client_shutdown(input.client, SHUT_WR);
360         while (! sdb_client_eof(input.client)) {
361                 /* wait for remaining data to arrive */
362                 sdb_command_print_reply(input.client);
363         }
365         if (hist_file[0] != '\0') {
366                 errno = 0;
367                 if (write_history(hist_file)) {
368                         char errbuf[1024];
369                         sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
370                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
371                 }
372         }
374         sdb_input_reset(&input);
376         ERR_free_strings();
377         return 0;
378 } /* main */
380 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */