Code

frontend, proto: Include the response data type in query replies.
[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);
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 DATA 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_DATA) || (status == CONNECTION_ERROR))
207                                 break;
208                         if (status == CONNECTION_OK) {
209                                 /* pre 0.4 versions used OK instead of DATA */
210                                 sdb_log(SDB_LOG_WARNING, "Received unexpected OK status from "
211                                                 "server in response to a QUERY (expected DATA); "
212                                                 "assuming we're talking to an old server");
213                                 break;
214                         }
215                 }
217                 if ((status != CONNECTION_OK) && (status != CONNECTION_DATA))
218                         break; /* error */
219         }
221         sdb_llist_iter_destroy(iter);
222         return status;
223 } /* execute_commands */
225 int
226 main(int argc, char **argv)
228         const char *host = NULL;
229         const char *user = NULL;
231         const char *homedir;
232         char hist_file[1024] = "";
234         sdb_input_t input = SDB_INPUT_INIT;
235         sdb_llist_t *commands = NULL;
237         while (42) {
238                 int opt = getopt(argc, argv, "H:U:c:hV");
240                 if (-1 == opt)
241                         break;
243                 switch (opt) {
244                         case 'H':
245                                 host = optarg;
246                                 break;
247                         case 'U':
248                                 user = optarg;
249                                 break;
251                         case 'c':
252                                 {
253                                         sdb_object_t *obj;
255                                         if (! commands)
256                                                 commands = sdb_llist_create();
257                                         if (! commands) {
258                                                 sdb_log(SDB_LOG_ERR, "Failed to create list object");
259                                                 exit(1);
260                                         }
262                                         if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
263                                                 sdb_log(SDB_LOG_ERR, "Failed to create object");
264                                                 exit(1);
265                                         }
266                                         if (sdb_llist_append(commands, obj)) {
267                                                 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
268                                                 sdb_object_deref(obj);
269                                                 exit(1);
270                                         }
271                                         sdb_object_deref(obj);
272                                 }
273                                 break;
275                         case 'h':
276                                 exit_usage(argv[0], 0);
277                                 break;
278                         case 'V':
279                                 exit_version();
280                                 break;
281                         default:
282                                 exit_usage(argv[0], 1);
283                 }
284         }
286         if (optind < argc)
287                 exit_usage(argv[0], 1);
289         if (! host)
290                 host = DEFAULT_SOCKET;
291         if (! user) {
292                 user = get_current_user();
293                 if (! user)
294                         exit(1);
295         }
297         input.client = sdb_client_create(host);
298         if (! input.client) {
299                 sdb_log(SDB_LOG_ERR, "Failed to create client object");
300                 exit(1);
301         }
302         if (sdb_client_connect(input.client, user)) {
303                 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
304                 sdb_client_destroy(input.client);
305                 exit(1);
306         }
308         if (commands) {
309                 int status = execute_commands(input.client, commands);
310                 sdb_llist_destroy(commands);
311                 sdb_client_destroy(input.client);
312                 if ((status != CONNECTION_OK) && (status != CONNECTION_DATA))
313                         exit(1);
314                 exit(0);
315         }
317         sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
318                         SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
319                         sdb_client_version_string(), sdb_client_version_extra());
321         using_history();
323         if ((homedir = get_homedir(user))) {
324                 snprintf(hist_file, sizeof(hist_file) - 1,
325                                 "%s/.sysdb_history", homedir);
326                 hist_file[sizeof(hist_file) - 1] = '\0';
328                 errno = 0;
329                 if (read_history(hist_file) && (errno != ENOENT)) {
330                         char errbuf[1024];
331                         sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
332                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
333                 }
334         }
336         input.input = sdb_strbuf_create(2048);
337         sdb_input_init(&input);
338         sdb_input_mainloop();
340         sdb_client_shutdown(input.client, SHUT_WR);
341         while (! sdb_client_eof(input.client)) {
342                 /* wait for remaining data to arrive */
343                 sdb_command_print_reply(input.client);
344         }
346         if (hist_file[0] != '\0') {
347                 errno = 0;
348                 if (write_history(hist_file)) {
349                         char errbuf[1024];
350                         sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
351                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
352                 }
353         }
355         sdb_client_destroy(input.client);
356         sdb_strbuf_destroy(input.input);
357         return 0;
358 } /* main */
360 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */