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 const char *
88 get_homedir(void)
89 {
90 char *username = sdb_get_current_user();
92 struct passwd pw_entry;
93 struct passwd *result = NULL;
95 /* needs to be static because we return a pointer into this buffer
96 * to the caller */
97 static char buf[1024];
99 int status;
101 if (username) {
102 memset(&pw_entry, 0, sizeof(pw_entry));
103 status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
104 }
105 else
106 status = -1;
108 if (status || (! result)) {
109 char errbuf[1024];
110 sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
111 "for user %s: %s", username,
112 sdb_strerror(errno, errbuf, sizeof(errbuf)));
113 free(username);
114 return NULL;
115 }
116 free(username);
117 return result->pw_dir;
118 } /* get_homedir */
120 static void
121 exit_usage(char *name, int status)
122 {
123 char *user = sdb_get_current_user();
124 printf(
125 "Usage: %s <options>\n"
127 "\nOptions:\n"
128 " -H HOST the host to connect to\n"
129 " default: "DEFAULT_SOCKET"\n"
130 " -U USER the username to connect as\n"
131 " default: %s\n"
132 " -c CMD execute the specified command and then exit\n"
133 "\n"
134 " -h display this help and exit\n"
135 " -V display the version number and copyright\n"
137 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
138 PACKAGE_URL"\n", basename(name), user);
139 free(user);
140 exit(status);
141 } /* exit_usage */
143 static void
144 exit_version(void)
145 {
146 printf("SysDB version "SDB_CLIENT_VERSION_STRING
147 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
148 "using libsysdbclient version %s%s\n"
149 "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
151 "\nThis is free software under the terms of the BSD license, see "
152 "the source for\ncopying conditions. There is NO WARRANTY; not "
153 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
154 "PURPOSE.\n", sdb_client_version_string(),
155 sdb_client_version_extra());
156 exit(0);
157 } /* exit_version */
159 static int
160 execute_commands(sdb_client_t *client, sdb_llist_t *commands)
161 {
162 sdb_llist_iter_t *iter;
163 int status = 0;
165 iter = sdb_llist_get_iter(commands);
166 if (! iter) {
167 sdb_log(SDB_LOG_ERR, "Failed to iterate commands");
168 return 1;
169 }
171 while (sdb_llist_iter_has_next(iter)) {
172 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
174 if (sdb_client_send(client, SDB_CONNECTION_QUERY,
175 (uint32_t)strlen(obj->name), obj->name) <= 0) {
176 sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
177 obj->name);
178 status = 1;
179 break;
180 }
182 /* Wait for server replies. We might get any number of log messages
183 * but eventually see the reply to the query, which is either DATA or
184 * ERROR. */
185 while (42) {
186 status = sdb_command_print_reply(client);
187 if (status < 0) {
188 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
189 break;
190 }
192 if ((status == SDB_CONNECTION_DATA)
193 || (status == SDB_CONNECTION_ERROR))
194 break;
195 if (status == SDB_CONNECTION_OK) {
196 /* pre 0.4 versions used OK instead of DATA */
197 sdb_log(SDB_LOG_WARNING, "Received unexpected OK status from "
198 "server in response to a QUERY (expected DATA); "
199 "assuming we're talking to an old server");
200 break;
201 }
202 }
204 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
205 break; /* error */
206 }
208 sdb_llist_iter_destroy(iter);
209 return status;
210 } /* execute_commands */
212 int
213 main(int argc, char **argv)
214 {
215 const char *host = NULL;
217 const char *homedir;
218 char hist_file[1024] = "";
220 sdb_input_t input = SDB_INPUT_INIT;
221 sdb_llist_t *commands = NULL;
223 while (42) {
224 int opt = getopt(argc, argv, "H:U:c:hV");
226 if (-1 == opt)
227 break;
229 switch (opt) {
230 case 'H':
231 host = optarg;
232 break;
233 case 'U':
234 input.user = optarg;
235 break;
237 case 'c':
238 {
239 sdb_object_t *obj;
241 if (! commands)
242 commands = sdb_llist_create();
243 if (! commands) {
244 sdb_log(SDB_LOG_ERR, "Failed to create list object");
245 exit(1);
246 }
248 if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
249 sdb_log(SDB_LOG_ERR, "Failed to create object");
250 exit(1);
251 }
252 if (sdb_llist_append(commands, obj)) {
253 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
254 sdb_object_deref(obj);
255 exit(1);
256 }
257 sdb_object_deref(obj);
258 }
259 break;
261 case 'h':
262 exit_usage(argv[0], 0);
263 break;
264 case 'V':
265 exit_version();
266 break;
267 default:
268 exit_usage(argv[0], 1);
269 }
270 }
272 if (optind < argc)
273 exit_usage(argv[0], 1);
275 if (! host)
276 host = DEFAULT_SOCKET;
277 if (! input.user)
278 input.user = sdb_get_current_user();
279 else
280 input.user = strdup(input.user);
281 if (! input.user)
282 exit(1);
284 SSL_load_error_strings();
285 OpenSSL_add_ssl_algorithms();
287 input.client = sdb_client_create(host);
288 if (! input.client) {
289 sdb_log(SDB_LOG_ERR, "Failed to create client object");
290 free(input.user);
291 exit(1);
292 }
293 if (sdb_client_connect(input.client, input.user)) {
294 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
295 sdb_client_destroy(input.client);
296 free(input.user);
297 exit(1);
298 }
300 if (commands) {
301 int status = execute_commands(input.client, commands);
302 sdb_llist_destroy(commands);
303 sdb_client_destroy(input.client);
304 free(input.user);
305 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
306 exit(1);
307 exit(0);
308 }
310 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
311 SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
312 sdb_client_version_string(), sdb_client_version_extra());
314 using_history();
316 if ((homedir = get_homedir())) {
317 snprintf(hist_file, sizeof(hist_file) - 1,
318 "%s/.sysdb_history", homedir);
319 hist_file[sizeof(hist_file) - 1] = '\0';
321 errno = 0;
322 if (read_history(hist_file) && (errno != ENOENT)) {
323 char errbuf[1024];
324 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
325 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
326 }
327 }
328 free(input.user);
330 input.input = sdb_strbuf_create(2048);
331 sdb_input_init(&input);
332 sdb_input_mainloop();
334 sdb_client_shutdown(input.client, SHUT_WR);
335 while (! sdb_client_eof(input.client)) {
336 /* wait for remaining data to arrive */
337 sdb_command_print_reply(input.client);
338 }
340 if (hist_file[0] != '\0') {
341 errno = 0;
342 if (write_history(hist_file)) {
343 char errbuf[1024];
344 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
345 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
346 }
347 }
349 sdb_client_destroy(input.client);
350 sdb_strbuf_destroy(input.input);
352 ERR_free_strings();
353 return 0;
354 } /* main */
356 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */