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);
187 if (sdb_client_send(client, CONNECTION_QUERY,
188 (uint32_t)strlen(obj->name), obj->name) <= 0) {
189 sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
190 obj->name);
191 status = 1;
192 break;
193 }
194 if (sdb_command_print_reply(client)) {
195 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
196 status = 1;
197 break;
198 }
199 }
201 sdb_llist_iter_destroy(iter);
202 return status;
203 } /* execute_commands */
205 int
206 main(int argc, char **argv)
207 {
208 const char *host = NULL;
209 const char *user = NULL;
211 const char *homedir;
212 char hist_file[1024] = "";
214 sdb_input_t input = SDB_INPUT_INIT;
215 sdb_llist_t *commands = NULL;
217 while (42) {
218 int opt = getopt(argc, argv, "H:U:c:hV");
220 if (-1 == opt)
221 break;
223 switch (opt) {
224 case 'H':
225 host = optarg;
226 break;
227 case 'U':
228 user = optarg;
229 break;
231 case 'c':
232 {
233 sdb_object_t *obj;
235 if (! commands)
236 commands = sdb_llist_create();
237 if (! commands) {
238 sdb_log(SDB_LOG_ERR, "Failed to create list object");
239 exit(1);
240 }
242 if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
243 sdb_log(SDB_LOG_ERR, "Failed to create object");
244 exit(1);
245 }
246 if (sdb_llist_append(commands, obj)) {
247 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
248 sdb_object_deref(obj);
249 exit(1);
250 }
251 sdb_object_deref(obj);
252 }
253 break;
255 case 'h':
256 exit_usage(argv[0], 0);
257 break;
258 case 'V':
259 exit_version();
260 break;
261 default:
262 exit_usage(argv[0], 1);
263 }
264 }
266 if (optind < argc)
267 exit_usage(argv[0], 1);
269 if (! host)
270 host = DEFAULT_SOCKET;
271 if (! user) {
272 user = get_current_user();
273 if (! user)
274 exit(1);
275 }
277 input.client = sdb_client_create(host);
278 if (! input.client) {
279 sdb_log(SDB_LOG_ERR, "Failed to create client object");
280 exit(1);
281 }
282 if (sdb_client_connect(input.client, user)) {
283 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
284 sdb_client_destroy(input.client);
285 exit(1);
286 }
288 if (commands) {
289 int status = execute_commands(input.client, commands);
290 sdb_llist_destroy(commands);
291 sdb_client_destroy(input.client);
292 exit(status);
293 }
295 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
296 SDB_CLIENT_VERSION_EXTRA"\n");
298 using_history();
300 if ((homedir = get_homedir(user))) {
301 snprintf(hist_file, sizeof(hist_file) - 1,
302 "%s/.sysdb_history", homedir);
303 hist_file[sizeof(hist_file) - 1] = '\0';
305 errno = 0;
306 if (read_history(hist_file) && (errno != ENOENT)) {
307 char errbuf[1024];
308 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
309 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
310 }
311 }
313 input.input = sdb_strbuf_create(2048);
314 sdb_input_init(&input);
315 sdb_input_mainloop();
317 if (hist_file[0] != '\0') {
318 errno = 0;
319 if (write_history(hist_file)) {
320 char errbuf[1024];
321 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
322 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
323 }
324 }
326 sdb_client_destroy(input.client);
327 sdb_strbuf_destroy(input.input);
328 return 0;
329 } /* main */
331 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */