Code

Move sdb_get_homedir() from tools/sysdb to utils/os.
[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"
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 #include <openssl/ssl.h>
81 #include <openssl/err.h>
83 #ifndef DEFAULT_SOCKET
84 #       define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
85 #endif
88 static void
89 exit_usage(char *name, int status)
90 {
91         char *user = sdb_get_current_user();
92         printf(
93 "Usage: %s <options>\n"
95 "\nOptions:\n"
96 "  -H HOST   the host to connect to\n"
97 "            default: "DEFAULT_SOCKET"\n"
98 "  -U USER   the username to connect as\n"
99 "            default: %s\n"
100 "  -c CMD    execute the specified command and then exit\n"
101 "\n"
102 "  -h        display this help and exit\n"
103 "  -V        display the version number and copyright\n"
105 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
106 PACKAGE_URL"\n", basename(name), user);
107         free(user);
108         exit(status);
109 } /* exit_usage */
111 static void
112 exit_version(void)
114         printf("SysDB version "SDB_CLIENT_VERSION_STRING
115                         SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
116                         "using libsysdbclient version %s%s\n"
117                         "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
119                         "\nThis is free software under the terms of the BSD license, see "
120                         "the source for\ncopying conditions. There is NO WARRANTY; not "
121                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
122                         "PURPOSE.\n", sdb_client_version_string(),
123                         sdb_client_version_extra());
124         exit(0);
125 } /* exit_version */
127 static int
128 execute_commands(sdb_client_t *client, sdb_llist_t *commands)
130         sdb_llist_iter_t *iter;
131         int status = 0;
133         iter = sdb_llist_get_iter(commands);
134         if (! iter) {
135                 sdb_log(SDB_LOG_ERR, "Failed to iterate commands");
136                 return 1;
137         }
139         while (sdb_llist_iter_has_next(iter)) {
140                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
142                 if (sdb_client_send(client, SDB_CONNECTION_QUERY,
143                                         (uint32_t)strlen(obj->name), obj->name) <= 0) {
144                         sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
145                                         obj->name);
146                         status = 1;
147                         break;
148                 }
150                 /* Wait for server replies. We might get any number of log messages
151                  * but eventually see the reply to the query, which is either DATA or
152                  * ERROR. */
153                 while (42) {
154                         status = sdb_command_print_reply(client);
155                         if (status < 0) {
156                                 sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
157                                 break;
158                         }
160                         if ((status == SDB_CONNECTION_DATA)
161                                         || (status == SDB_CONNECTION_ERROR))
162                                 break;
163                         if (status == SDB_CONNECTION_OK) {
164                                 /* pre 0.4 versions used OK instead of DATA */
165                                 sdb_log(SDB_LOG_WARNING, "Received unexpected OK status from "
166                                                 "server in response to a QUERY (expected DATA); "
167                                                 "assuming we're talking to an old server");
168                                 break;
169                         }
170                 }
172                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
173                         break; /* error */
174         }
176         sdb_llist_iter_destroy(iter);
177         return status;
178 } /* execute_commands */
180 int
181 main(int argc, char **argv)
183         const char *host = NULL;
185         char *homedir;
186         char hist_file[1024] = "";
188         sdb_input_t input = SDB_INPUT_INIT;
189         sdb_llist_t *commands = NULL;
191         while (42) {
192                 int opt = getopt(argc, argv, "H:U:c:hV");
194                 if (-1 == opt)
195                         break;
197                 switch (opt) {
198                         case 'H':
199                                 host = optarg;
200                                 break;
201                         case 'U':
202                                 input.user = optarg;
203                                 break;
205                         case 'c':
206                                 {
207                                         sdb_object_t *obj;
209                                         if (! commands)
210                                                 commands = sdb_llist_create();
211                                         if (! commands) {
212                                                 sdb_log(SDB_LOG_ERR, "Failed to create list object");
213                                                 exit(1);
214                                         }
216                                         if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
217                                                 sdb_log(SDB_LOG_ERR, "Failed to create object");
218                                                 exit(1);
219                                         }
220                                         if (sdb_llist_append(commands, obj)) {
221                                                 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
222                                                 sdb_object_deref(obj);
223                                                 exit(1);
224                                         }
225                                         sdb_object_deref(obj);
226                                 }
227                                 break;
229                         case 'h':
230                                 exit_usage(argv[0], 0);
231                                 break;
232                         case 'V':
233                                 exit_version();
234                                 break;
235                         default:
236                                 exit_usage(argv[0], 1);
237                 }
238         }
240         if (optind < argc)
241                 exit_usage(argv[0], 1);
243         if (! host)
244                 host = DEFAULT_SOCKET;
245         if (! input.user)
246                 input.user = sdb_get_current_user();
247         else
248                 input.user = strdup(input.user);
249         if (! input.user)
250                 exit(1);
252         SSL_load_error_strings();
253         OpenSSL_add_ssl_algorithms();
255         input.client = sdb_client_create(host);
256         if (! input.client) {
257                 sdb_log(SDB_LOG_ERR, "Failed to create client object");
258                 free(input.user);
259                 exit(1);
260         }
261         if (sdb_client_connect(input.client, input.user)) {
262                 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
263                 sdb_client_destroy(input.client);
264                 free(input.user);
265                 exit(1);
266         }
268         if (commands) {
269                 int status = execute_commands(input.client, commands);
270                 sdb_llist_destroy(commands);
271                 sdb_client_destroy(input.client);
272                 free(input.user);
273                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
274                         exit(1);
275                 exit(0);
276         }
278         sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
279                         SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
280                         sdb_client_version_string(), sdb_client_version_extra());
282         using_history();
284         if ((homedir = sdb_get_homedir())) {
285                 snprintf(hist_file, sizeof(hist_file) - 1,
286                                 "%s/.sysdb_history", homedir);
287                 hist_file[sizeof(hist_file) - 1] = '\0';
288                 free(homedir);
289                 homedir = NULL;
291                 errno = 0;
292                 if (read_history(hist_file) && (errno != ENOENT)) {
293                         char errbuf[1024];
294                         sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
295                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
296                 }
297         }
298         free(input.user);
300         input.input = sdb_strbuf_create(2048);
301         sdb_input_init(&input);
302         sdb_input_mainloop();
304         sdb_client_shutdown(input.client, SHUT_WR);
305         while (! sdb_client_eof(input.client)) {
306                 /* wait for remaining data to arrive */
307                 sdb_command_print_reply(input.client);
308         }
310         if (hist_file[0] != '\0') {
311                 errno = 0;
312                 if (write_history(hist_file)) {
313                         char errbuf[1024];
314                         sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
315                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
316                 }
317         }
319         sdb_client_destroy(input.client);
320         sdb_strbuf_destroy(input.input);
322         ERR_free_strings();
323         return 0;
324 } /* main */
326 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */