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