Code

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