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