Code

a8dbf27fa55a7e0315d4fa95c88383cd957d5695
[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 "\nOptions:\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 "  -h        display this help and exit\n"
131 "  -V        display the version number and copyright\n"
133 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
134 PACKAGE_URL"\n", basename(name), user);
135         free(user);
136         exit(status);
137 } /* exit_usage */
139 static void
140 exit_version(void)
142         printf("SysDB version "SDB_CLIENT_VERSION_STRING
143                         SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
144                         "using libsysdbclient version %s%s\n"
145                         "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
147                         "\nThis is free software under the terms of the BSD license, see "
148                         "the source for\ncopying conditions. There is NO WARRANTY; not "
149                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
150                         "PURPOSE.\n", sdb_client_version_string(),
151                         sdb_client_version_extra());
152         exit(0);
153 } /* exit_version */
155 static int
156 execute_commands(sdb_client_t *client, sdb_llist_t *commands)
158         sdb_llist_iter_t *iter;
159         int status = 0;
161         iter = sdb_llist_get_iter(commands);
162         if (! iter) {
163                 sdb_log(SDB_LOG_ERR, "Failed to iterate commands");
164                 return 1;
165         }
167         while (sdb_llist_iter_has_next(iter)) {
168                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
170                 if (sdb_client_send(client, SDB_CONNECTION_QUERY,
171                                         (uint32_t)strlen(obj->name), obj->name) <= 0) {
172                         sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
173                                         obj->name);
174                         status = 1;
175                         break;
176                 }
178                 /* Wait for server replies. We might get any number of log messages
179                  * but eventually see the reply to the query, which is either DATA or
180                  * ERROR. */
181                 while (42) {
182                         status = sdb_command_print_reply(client);
183                         if (status < 0) {
184                                 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
185                                 break;
186                         }
188                         if ((status == SDB_CONNECTION_DATA)
189                                         || (status == SDB_CONNECTION_ERROR))
190                                 break;
191                         if (status == SDB_CONNECTION_OK) {
192                                 /* pre 0.4 versions used OK instead of DATA */
193                                 sdb_log(SDB_LOG_WARNING, "Received unexpected OK status from "
194                                                 "server in response to a QUERY (expected DATA); "
195                                                 "assuming we're talking to an old server");
196                                 break;
197                         }
198                 }
200                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
201                         break; /* error */
202         }
204         sdb_llist_iter_destroy(iter);
205         return status;
206 } /* execute_commands */
208 int
209 main(int argc, char **argv)
211         const char *host = NULL;
213         char *homedir;
214         char hist_file[1024] = "";
216         sdb_input_t input = SDB_INPUT_INIT;
217         sdb_llist_t *commands = NULL;
219         while (42) {
220                 int opt = getopt(argc, argv, "H:U:c:hV");
222                 if (-1 == opt)
223                         break;
225                 switch (opt) {
226                         case 'H':
227                                 host = optarg;
228                                 break;
229                         case 'U':
230                                 input.user = optarg;
231                                 break;
233                         case 'c':
234                                 {
235                                         sdb_object_t *obj;
237                                         if (! commands)
238                                                 commands = sdb_llist_create();
239                                         if (! commands) {
240                                                 sdb_log(SDB_LOG_ERR, "Failed to create list object");
241                                                 exit(1);
242                                         }
244                                         if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
245                                                 sdb_log(SDB_LOG_ERR, "Failed to create object");
246                                                 exit(1);
247                                         }
248                                         if (sdb_llist_append(commands, obj)) {
249                                                 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
250                                                 sdb_object_deref(obj);
251                                                 exit(1);
252                                         }
253                                         sdb_object_deref(obj);
254                                 }
255                                 break;
257                         case 'h':
258                                 exit_usage(argv[0], 0);
259                                 break;
260                         case 'V':
261                                 exit_version();
262                                 break;
263                         default:
264                                 exit_usage(argv[0], 1);
265                 }
266         }
268         if (optind < argc)
269                 exit_usage(argv[0], 1);
271         if (! host)
272                 host = DEFAULT_SOCKET;
273         if (! input.user)
274                 input.user = sdb_get_current_user();
275         else
276                 input.user = strdup(input.user);
277         if (! input.user)
278                 exit(1);
280         SSL_load_error_strings();
281         OpenSSL_add_ssl_algorithms();
283         input.client = sdb_client_create(host);
284         if (! input.client) {
285                 sdb_log(SDB_LOG_ERR, "Failed to create client object");
286                 sdb_input_reset(&input);
287                 exit(1);
288         }
289         canonicalize_ssl_options();
290         if (sdb_client_set_ssl_options(input.client, &ssl_options)) {
291                 sdb_log(SDB_LOG_ERR, "Failed to apply SSL options");
292                 sdb_input_reset(&input);
293                 sdb_ssl_free_options(&ssl_options);
294                 exit(1);
295         }
296         sdb_ssl_free_options(&ssl_options);
297         if (sdb_client_connect(input.client, input.user)) {
298                 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
299                 sdb_input_reset(&input);
300                 exit(1);
301         }
303         if (commands) {
304                 int status = execute_commands(input.client, commands);
305                 sdb_llist_destroy(commands);
306                 sdb_input_reset(&input);
307                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
308                         exit(1);
309                 exit(0);
310         }
312         sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
313                         SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
314                         sdb_client_version_string(), sdb_client_version_extra());
316         using_history();
318         if ((homedir = sdb_get_homedir())) {
319                 snprintf(hist_file, sizeof(hist_file) - 1,
320                                 "%s/.sysdb_history", homedir);
321                 hist_file[sizeof(hist_file) - 1] = '\0';
322                 free(homedir);
323                 homedir = NULL;
325                 errno = 0;
326                 if (read_history(hist_file) && (errno != ENOENT)) {
327                         char errbuf[1024];
328                         sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
329                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
330                 }
331         }
333         input.input = sdb_strbuf_create(2048);
334         sdb_input_init(&input);
335         sdb_input_mainloop();
337         sdb_client_shutdown(input.client, SHUT_WR);
338         while (! sdb_client_eof(input.client)) {
339                 /* wait for remaining data to arrive */
340                 sdb_command_print_reply(input.client);
341         }
343         if (hist_file[0] != '\0') {
344                 errno = 0;
345                 if (write_history(hist_file)) {
346                         char errbuf[1024];
347                         sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
348                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
349                 }
350         }
352         sdb_input_reset(&input);
354         ERR_free_strings();
355         return 0;
356 } /* main */
358 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */