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"
41 #include <errno.h>
43 #if HAVE_LIBGEN_H
44 # include <libgen.h>
45 #else /* HAVE_LIBGEN_H */
46 # define basename(path) (path)
47 #endif /* ! HAVE_LIBGEN_H */
49 #include <sys/stat.h>
50 #include <fcntl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
56 #include <unistd.h>
58 #include <sys/types.h>
60 #include <pwd.h>
62 #if HAVE_EDITLINE_READLINE_H
63 # include <editline/readline.h>
64 # if HAVE_EDITLINE_HISTORY_H
65 # include <editline/history.h>
66 # endif
67 #elif HAVE_READLINE_READLINE_H
68 # include <readline/readline.h>
69 # if HAVE_READLINE_HISTORY_H
70 # include <readline/history.h>
71 # endif
72 #elif HAVE_READLINE_H
73 # include <readline.h>
74 # if HAVE_HISTORY_H
75 # include <history.h>
76 # endif
77 #endif /* READLINEs */
79 #ifndef DEFAULT_SOCKET
80 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
81 #endif
83 static const char *
84 get_current_user(void)
85 {
86 struct passwd pw_entry;
87 struct passwd *result = NULL;
89 uid_t uid;
91 /* needs to be static because we return a pointer into this buffer
92 * to the caller */
93 static char buf[1024];
95 int status;
97 uid = geteuid();
99 memset(&pw_entry, 0, sizeof(pw_entry));
100 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
102 if (status || (! result)) {
103 char errbuf[1024];
104 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
105 sdb_strerror(errno, errbuf, sizeof(errbuf)));
106 return NULL;
107 }
108 return result->pw_name;
109 } /* get_current_user */
111 static const char *
112 get_homedir(const char *username)
113 {
114 struct passwd pw_entry;
115 struct passwd *result = NULL;
117 /* needs to be static because we return a pointer into this buffer
118 * to the caller */
119 static char buf[1024];
121 int status;
123 memset(&pw_entry, 0, sizeof(pw_entry));
124 status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
126 if (status || (! result)) {
127 char errbuf[1024];
128 sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
129 "for user %s: %s", username,
130 sdb_strerror(errno, errbuf, sizeof(errbuf)));
131 return NULL;
132 }
133 return result->pw_dir;
134 } /* get_homedir */
136 static void
137 exit_usage(char *name, int status)
138 {
139 printf(
140 "Usage: %s <options>\n"
142 "\nOptions:\n"
143 " -H HOST the host to connect to\n"
144 " default: "DEFAULT_SOCKET"\n"
145 " -U USER the username to connect as\n"
146 " default: %s\n"
147 " -c CMD execute the specified command and then exit\n"
148 "\n"
149 " -h display this help and exit\n"
150 " -V display the version number and copyright\n"
152 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
153 PACKAGE_URL"\n", basename(name), get_current_user());
154 exit(status);
155 } /* exit_usage */
157 static void
158 exit_version(void)
159 {
160 printf("SysDB version "SDB_CLIENT_VERSION_STRING
161 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
162 "using libsysdbclient version %s%s\n"
163 "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
165 "\nThis is free software under the terms of the BSD license, see "
166 "the source for\ncopying conditions. There is NO WARRANTY; not "
167 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
168 "PURPOSE.\n", sdb_client_version_string(),
169 sdb_client_version_extra());
170 exit(0);
171 } /* exit_version */
173 static int
174 execute_commands(sdb_client_t *client, sdb_llist_t *commands)
175 {
176 sdb_llist_iter_t *iter;
177 int status = 0;
179 iter = sdb_llist_get_iter(commands);
180 if (! iter) {
181 sdb_log(SDB_LOG_ERR, "Failed to iterate commands");
182 return 1;
183 }
185 while (sdb_llist_iter_has_next(iter)) {
186 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
188 if (sdb_client_send(client, CONNECTION_QUERY,
189 (uint32_t)strlen(obj->name), obj->name) <= 0) {
190 sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
191 obj->name);
192 status = 1;
193 break;
194 }
196 /* Wait for server replies. We might get any number of log messages
197 * but eventually see the reply to the query, which is either OK or
198 * ERROR. */
199 while (42) {
200 status = sdb_command_print_reply(client);
201 if (status < 0) {
202 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
203 break;
204 }
206 if ((status == CONNECTION_OK) || (status == CONNECTION_ERROR))
207 break;
208 }
210 if (status)
211 break;
212 }
214 sdb_llist_iter_destroy(iter);
215 return status;
216 } /* execute_commands */
218 int
219 main(int argc, char **argv)
220 {
221 const char *host = NULL;
222 const char *user = NULL;
224 const 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:hV");
233 if (-1 == opt)
234 break;
236 switch (opt) {
237 case 'H':
238 host = optarg;
239 break;
240 case 'U':
241 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 'h':
269 exit_usage(argv[0], 0);
270 break;
271 case 'V':
272 exit_version();
273 break;
274 default:
275 exit_usage(argv[0], 1);
276 }
277 }
279 if (optind < argc)
280 exit_usage(argv[0], 1);
282 if (! host)
283 host = DEFAULT_SOCKET;
284 if (! user) {
285 user = get_current_user();
286 if (! user)
287 exit(1);
288 }
290 input.client = sdb_client_create(host);
291 if (! input.client) {
292 sdb_log(SDB_LOG_ERR, "Failed to create client object");
293 exit(1);
294 }
295 if (sdb_client_connect(input.client, user)) {
296 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
297 sdb_client_destroy(input.client);
298 exit(1);
299 }
301 if (commands) {
302 int status = execute_commands(input.client, commands);
303 sdb_llist_destroy(commands);
304 sdb_client_destroy(input.client);
305 exit(status);
306 }
308 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
309 SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
310 sdb_client_version_string(), sdb_client_version_extra());
312 using_history();
314 if ((homedir = get_homedir(user))) {
315 snprintf(hist_file, sizeof(hist_file) - 1,
316 "%s/.sysdb_history", homedir);
317 hist_file[sizeof(hist_file) - 1] = '\0';
319 errno = 0;
320 if (read_history(hist_file) && (errno != ENOENT)) {
321 char errbuf[1024];
322 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
323 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
324 }
325 }
327 input.input = sdb_strbuf_create(2048);
328 sdb_input_init(&input);
329 sdb_input_mainloop();
331 sdb_client_shutdown(input.client, SHUT_WR);
332 while (! sdb_client_eof(input.client)) {
333 /* wait for remaining data to arrive */
334 sdb_command_print_reply(input.client);
335 }
337 if (hist_file[0] != '\0') {
338 errno = 0;
339 if (write_history(hist_file)) {
340 char errbuf[1024];
341 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
342 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
343 }
344 }
346 sdb_client_destroy(input.client);
347 sdb_strbuf_destroy(input.input);
348 return 0;
349 } /* main */
351 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */