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