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>
44 #include <time.h>
46 #if HAVE_LIBGEN_H
47 # include <libgen.h>
48 #else /* HAVE_LIBGEN_H */
49 # define basename(path) (path)
50 #endif /* ! HAVE_LIBGEN_H */
52 #include <sys/stat.h>
53 #include <fcntl.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
59 #include <unistd.h>
61 #include <sys/types.h>
63 #include <pwd.h>
65 #if HAVE_EDITLINE_READLINE_H
66 # include <editline/readline.h>
67 # if HAVE_EDITLINE_HISTORY_H
68 # include <editline/history.h>
69 # endif
70 #elif HAVE_READLINE_READLINE_H
71 # include <readline/readline.h>
72 # if HAVE_READLINE_HISTORY_H
73 # include <readline/history.h>
74 # endif
75 #elif HAVE_READLINE_H
76 # include <readline.h>
77 # if HAVE_HISTORY_H
78 # include <history.h>
79 # endif
80 #endif /* READLINEs */
82 #ifndef DEFAULT_SOCKET
83 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
84 #endif
86 static sdb_ssl_options_t ssl_options = {
87 /* ca_file */ SDB_SSL_CAFILE,
88 /* key_file */ "~/.config/sysdb/ssl/key.pem",
89 /* cert_file */ "~/.config/sysdb/ssl/cert.pem",
90 /* crl_file */ "~/.config/sysdb/ssl/crl.pem",
91 };
93 static void
94 canonicalize_ssl_options(void)
95 {
96 char *tmp;
97 if (ssl_options.ca_file) {
98 tmp = sdb_realpath(ssl_options.ca_file);
99 ssl_options.ca_file = tmp ? tmp : strdup(ssl_options.ca_file);
100 }
101 if (ssl_options.key_file) {
102 tmp = sdb_realpath(ssl_options.key_file);
103 ssl_options.key_file = tmp ? tmp : strdup(ssl_options.key_file);
104 }
105 if (ssl_options.cert_file) {
106 tmp = sdb_realpath(ssl_options.cert_file);
107 ssl_options.cert_file = tmp ? tmp : strdup(ssl_options.cert_file);
108 }
109 if (ssl_options.crl_file) {
110 tmp = sdb_realpath(ssl_options.crl_file);
111 ssl_options.crl_file = tmp ? tmp : strdup(ssl_options.crl_file);
112 }
113 } /* canonicalize_ssl_options */
115 static void
116 exit_usage(char *name, int status)
117 {
118 char *user = sdb_get_current_user();
119 printf(
120 "Usage: %s <options>\n"
122 "Connection options:\n"
123 " -H HOST the host to connect to\n"
124 " default: "DEFAULT_SOCKET"\n"
125 " -U USER the username to connect as\n"
126 " default: %s\n"
127 " -c CMD execute the specified command and then exit\n"
128 "\n"
129 "SSL options:\n"
130 " -K KEYFILE private key file name\n"
131 " default: %s\n"
132 " -C CERTFILE client certificate file name\n"
133 " default: %s\n"
134 " -A CAFILE CA certificates file name\n"
135 " default: %s\n"
136 "\n"
137 "General options:\n"
138 "\n"
139 " -h display this help and exit\n"
140 " -V display the version number and copyright\n"
142 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
143 PACKAGE_URL"\n", basename(name), user,
144 ssl_options.key_file, ssl_options.cert_file, ssl_options.ca_file);
146 free(user);
147 exit(status);
148 } /* exit_usage */
150 static void
151 exit_version(void)
152 {
153 printf("SysDB version "SDB_CLIENT_VERSION_STRING
154 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
155 "using libsysdbclient version %s%s\n"
156 "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
158 "\nThis is free software under the terms of the BSD license, see "
159 "the source for\ncopying conditions. There is NO WARRANTY; not "
160 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
161 "PURPOSE.\n", sdb_client_version_string(),
162 sdb_client_version_extra());
163 exit(0);
164 } /* exit_version */
166 static int
167 execute_commands(sdb_client_t *client, sdb_llist_t *commands)
168 {
169 sdb_llist_iter_t *iter;
170 int status = 0;
172 iter = sdb_llist_get_iter(commands);
173 if (! iter) {
174 sdb_log(SDB_LOG_ERR, "Failed to iterate commands");
175 return 1;
176 }
178 while (sdb_llist_iter_has_next(iter)) {
179 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
181 if (sdb_client_send(client, SDB_CONNECTION_QUERY,
182 (uint32_t)strlen(obj->name), obj->name) <= 0) {
183 sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
184 obj->name);
185 status = 1;
186 break;
187 }
189 /* Wait for server replies. We might get any number of log messages
190 * but eventually see the reply to the query, which is either DATA or
191 * ERROR. */
192 while (42) {
193 status = sdb_command_print_reply(client);
194 if (status < 0) {
195 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
196 break;
197 }
199 if ((status == SDB_CONNECTION_DATA)
200 || (status == SDB_CONNECTION_ERROR))
201 break;
202 if (status == SDB_CONNECTION_OK) {
203 /* pre 0.4 versions used OK instead of DATA */
204 sdb_log(SDB_LOG_WARNING, "Received unexpected OK status from "
205 "server in response to a QUERY (expected DATA); "
206 "assuming we're talking to an old server");
207 break;
208 }
209 }
211 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
212 break; /* error */
213 }
215 sdb_llist_iter_destroy(iter);
216 return status;
217 } /* execute_commands */
219 int
220 main(int argc, char **argv)
221 {
222 const char *host = NULL;
224 char *homedir;
225 char hist_file[1024] = "";
227 sdb_input_t input = SDB_INPUT_INIT;
228 sdb_llist_t *commands = NULL;
230 while (42) {
231 int opt = getopt(argc, argv, "H:U:c:C:K:A:hV");
233 if (-1 == opt)
234 break;
236 switch (opt) {
237 case 'H':
238 host = optarg;
239 break;
240 case 'U':
241 input.user = optarg;
242 break;
244 case 'c':
245 {
246 sdb_object_t *obj;
248 if (! commands)
249 commands = sdb_llist_create();
250 if (! commands) {
251 sdb_log(SDB_LOG_ERR, "Failed to create list object");
252 exit(1);
253 }
255 if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
256 sdb_log(SDB_LOG_ERR, "Failed to create object");
257 exit(1);
258 }
259 if (sdb_llist_append(commands, obj)) {
260 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
261 sdb_object_deref(obj);
262 exit(1);
263 }
264 sdb_object_deref(obj);
265 }
266 break;
268 case 'C':
269 ssl_options.cert_file = optarg;
270 break;
271 case 'K':
272 ssl_options.key_file = optarg;
273 break;
274 case 'A':
275 ssl_options.ca_file = optarg;
276 break;
278 case 'h':
279 exit_usage(argv[0], 0);
280 break;
281 case 'V':
282 exit_version();
283 break;
284 default:
285 exit_usage(argv[0], 1);
286 }
287 }
289 if (optind < argc)
290 exit_usage(argv[0], 1);
292 if (! host)
293 host = DEFAULT_SOCKET;
294 if (! input.user)
295 input.user = sdb_get_current_user();
296 else
297 input.user = strdup(input.user);
298 if (! input.user)
299 exit(1);
301 if (sdb_ssl_init())
302 exit(1);
304 input.client = sdb_client_create(host);
305 if (! input.client) {
306 sdb_log(SDB_LOG_ERR, "Failed to create client object");
307 sdb_input_reset(&input);
308 exit(1);
309 }
310 canonicalize_ssl_options();
311 if (sdb_client_set_ssl_options(input.client, &ssl_options)) {
312 sdb_log(SDB_LOG_ERR, "Failed to apply SSL options");
313 sdb_input_reset(&input);
314 sdb_ssl_free_options(&ssl_options);
315 exit(1);
316 }
317 sdb_ssl_free_options(&ssl_options);
318 if (sdb_client_connect(input.client, input.user)) {
319 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
320 sdb_input_reset(&input);
321 exit(1);
322 }
324 if (commands) {
325 int status = execute_commands(input.client, commands);
326 sdb_llist_destroy(commands);
327 sdb_input_reset(&input);
328 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
329 exit(1);
330 exit(0);
331 }
333 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
334 SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)",
335 sdb_client_version_string(), sdb_client_version_extra());
336 sdb_command_print_server_version(&input);
337 printf("\n");
339 using_history();
341 if ((homedir = sdb_get_homedir())) {
342 snprintf(hist_file, sizeof(hist_file) - 1,
343 "%s/.sysdb_history", homedir);
344 hist_file[sizeof(hist_file) - 1] = '\0';
345 free(homedir);
346 homedir = NULL;
348 errno = 0;
349 if (read_history(hist_file) && (errno != ENOENT)) {
350 char errbuf[1024];
351 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
352 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
353 }
354 }
356 input.input = sdb_strbuf_create(2048);
357 sdb_input_init(&input);
358 sdb_input_mainloop();
360 sdb_client_shutdown(input.client, SHUT_WR);
361 while (! sdb_client_eof(input.client)) {
362 /* wait for remaining data to arrive */
363 sdb_command_print_reply(input.client);
364 }
366 if (hist_file[0] != '\0') {
367 errno = 0;
368 if (write_history(hist_file)) {
369 char errbuf[1024];
370 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
371 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
372 }
373 }
375 sdb_input_reset(&input);
376 sdb_ssl_shutdown();
377 return 0;
378 } /* main */
380 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */