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;
206 const char *homedir;
207 char hist_file[1024] = "";
209 sdb_input_t input = SDB_INPUT_INIT;
210 sdb_llist_t *commands = NULL;
212 while (42) {
213 int opt = getopt(argc, argv, "H:U:c:hV");
215 if (-1 == opt)
216 break;
218 switch (opt) {
219 case 'H':
220 host = optarg;
221 break;
222 case 'U':
223 input.user = optarg;
224 break;
226 case 'c':
227 {
228 sdb_object_t *obj;
230 if (! commands)
231 commands = sdb_llist_create();
232 if (! commands) {
233 sdb_log(SDB_LOG_ERR, "Failed to create list object");
234 exit(1);
235 }
237 if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
238 sdb_log(SDB_LOG_ERR, "Failed to create object");
239 exit(1);
240 }
241 if (sdb_llist_append(commands, obj)) {
242 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
243 sdb_object_deref(obj);
244 exit(1);
245 }
246 sdb_object_deref(obj);
247 }
248 break;
250 case 'h':
251 exit_usage(argv[0], 0);
252 break;
253 case 'V':
254 exit_version();
255 break;
256 default:
257 exit_usage(argv[0], 1);
258 }
259 }
261 if (optind < argc)
262 exit_usage(argv[0], 1);
264 if (! host)
265 host = DEFAULT_SOCKET;
266 if (! input.user)
267 input.user = sdb_get_current_user();
268 else
269 input.user = strdup(input.user);
270 if (! input.user)
271 exit(1);
273 input.client = sdb_client_create(host);
274 if (! input.client) {
275 sdb_log(SDB_LOG_ERR, "Failed to create client object");
276 free(input.user);
277 exit(1);
278 }
279 if (sdb_client_connect(input.client, input.user)) {
280 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
281 sdb_client_destroy(input.client);
282 free(input.user);
283 exit(1);
284 }
286 if (commands) {
287 int status = execute_commands(input.client, commands);
288 sdb_llist_destroy(commands);
289 sdb_client_destroy(input.client);
290 free(input.user);
291 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
292 exit(1);
293 exit(0);
294 }
296 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
297 SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
298 sdb_client_version_string(), sdb_client_version_extra());
300 using_history();
302 if ((homedir = get_homedir(input.user))) {
303 snprintf(hist_file, sizeof(hist_file) - 1,
304 "%s/.sysdb_history", homedir);
305 hist_file[sizeof(hist_file) - 1] = '\0';
307 errno = 0;
308 if (read_history(hist_file) && (errno != ENOENT)) {
309 char errbuf[1024];
310 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
311 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
312 }
313 }
314 free(input.user);
316 input.input = sdb_strbuf_create(2048);
317 sdb_input_init(&input);
318 sdb_input_mainloop();
320 sdb_client_shutdown(input.client, SHUT_WR);
321 while (! sdb_client_eof(input.client)) {
322 /* wait for remaining data to arrive */
323 sdb_command_print_reply(input.client);
324 }
326 if (hist_file[0] != '\0') {
327 errno = 0;
328 if (write_history(hist_file)) {
329 char errbuf[1024];
330 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
331 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
332 }
333 }
335 sdb_client_destroy(input.client);
336 sdb_strbuf_destroy(input.input);
337 return 0;
338 } /* main */
340 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */