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"
41 #include "utils/ssl.h"
43 #include <errno.h>
45 #if HAVE_LIBGEN_H
46 # include <libgen.h>
47 #else /* HAVE_LIBGEN_H */
48 # define basename(path) (path)
49 #endif /* ! HAVE_LIBGEN_H */
51 #include <sys/stat.h>
52 #include <fcntl.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
58 #include <unistd.h>
60 #include <sys/types.h>
62 #include <pwd.h>
64 #if HAVE_EDITLINE_READLINE_H
65 # include <editline/readline.h>
66 # if HAVE_EDITLINE_HISTORY_H
67 # include <editline/history.h>
68 # endif
69 #elif HAVE_READLINE_READLINE_H
70 # include <readline/readline.h>
71 # if HAVE_READLINE_HISTORY_H
72 # include <readline/history.h>
73 # endif
74 #elif HAVE_READLINE_H
75 # include <readline.h>
76 # if HAVE_HISTORY_H
77 # include <history.h>
78 # endif
79 #endif /* READLINEs */
81 #ifndef DEFAULT_SOCKET
82 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
83 #endif
85 static sdb_ssl_options_t ssl_options = {
86 /* ca_file */ SDB_SSL_CAFILE,
87 /* key_file */ "~/.config/sysdb/ssl/key.pem",
88 /* cert_file */ "~/.config/sysdb/ssl/cert.pem",
89 /* crl_file */ "~/.config/sysdb/ssl/crl.pem",
90 };
92 static void
93 canonicalize_ssl_options(void)
94 {
95 char *tmp;
96 if (ssl_options.ca_file) {
97 tmp = sdb_realpath(ssl_options.ca_file);
98 ssl_options.ca_file = tmp ? tmp : strdup(ssl_options.ca_file);
99 }
100 if (ssl_options.key_file) {
101 tmp = sdb_realpath(ssl_options.key_file);
102 ssl_options.key_file = tmp ? tmp : strdup(ssl_options.key_file);
103 }
104 if (ssl_options.cert_file) {
105 tmp = sdb_realpath(ssl_options.cert_file);
106 ssl_options.cert_file = tmp ? tmp : strdup(ssl_options.cert_file);
107 }
108 if (ssl_options.crl_file) {
109 tmp = sdb_realpath(ssl_options.crl_file);
110 ssl_options.crl_file = tmp ? tmp : strdup(ssl_options.crl_file);
111 }
112 } /* canonicalize_ssl_options */
114 static void
115 exit_usage(char *name, int status)
116 {
117 char *user = sdb_get_current_user();
118 printf(
119 "Usage: %s <options>\n"
121 "Connection options:\n"
122 " -H HOST the host to connect to\n"
123 " default: "DEFAULT_SOCKET"\n"
124 " -U USER the username to connect as\n"
125 " default: %s\n"
126 " -c CMD execute the specified command and then exit\n"
127 "\n"
128 "SSL options:\n"
129 " -K KEYFILE private key file name\n"
130 " default: %s\n"
131 " -C CERTFILE client certificate file name\n"
132 " default: %s\n"
133 " -A CAFILE CA certificates 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, ssl_options.ca_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:A: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;
273 case 'A':
274 ssl_options.ca_file = optarg;
275 break;
277 case 'h':
278 exit_usage(argv[0], 0);
279 break;
280 case 'V':
281 exit_version();
282 break;
283 default:
284 exit_usage(argv[0], 1);
285 }
286 }
288 if (optind < argc)
289 exit_usage(argv[0], 1);
291 if (! host)
292 host = DEFAULT_SOCKET;
293 if (! input.user)
294 input.user = sdb_get_current_user();
295 else
296 input.user = strdup(input.user);
297 if (! input.user)
298 exit(1);
300 sdb_ssl_init();
302 input.client = sdb_client_create(host);
303 if (! input.client) {
304 sdb_log(SDB_LOG_ERR, "Failed to create client object");
305 sdb_input_reset(&input);
306 exit(1);
307 }
308 canonicalize_ssl_options();
309 if (sdb_client_set_ssl_options(input.client, &ssl_options)) {
310 sdb_log(SDB_LOG_ERR, "Failed to apply SSL options");
311 sdb_input_reset(&input);
312 sdb_ssl_free_options(&ssl_options);
313 exit(1);
314 }
315 sdb_ssl_free_options(&ssl_options);
316 if (sdb_client_connect(input.client, input.user)) {
317 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
318 sdb_input_reset(&input);
319 exit(1);
320 }
322 if (commands) {
323 int status = execute_commands(input.client, commands);
324 sdb_llist_destroy(commands);
325 sdb_input_reset(&input);
326 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
327 exit(1);
328 exit(0);
329 }
331 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
332 SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
333 sdb_client_version_string(), sdb_client_version_extra());
335 using_history();
337 if ((homedir = sdb_get_homedir())) {
338 snprintf(hist_file, sizeof(hist_file) - 1,
339 "%s/.sysdb_history", homedir);
340 hist_file[sizeof(hist_file) - 1] = '\0';
341 free(homedir);
342 homedir = NULL;
344 errno = 0;
345 if (read_history(hist_file) && (errno != ENOENT)) {
346 char errbuf[1024];
347 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
348 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
349 }
350 }
352 input.input = sdb_strbuf_create(2048);
353 sdb_input_init(&input);
354 sdb_input_mainloop();
356 sdb_client_shutdown(input.client, SHUT_WR);
357 while (! sdb_client_eof(input.client)) {
358 /* wait for remaining data to arrive */
359 sdb_command_print_reply(input.client);
360 }
362 if (hist_file[0] != '\0') {
363 errno = 0;
364 if (write_history(hist_file)) {
365 char errbuf[1024];
366 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
367 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
368 }
369 }
371 sdb_input_reset(&input);
372 sdb_ssl_shutdown();
373 return 0;
374 } /* main */
376 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */