Code

Renamed CONNECTION_* constants to SDB_CONNECTION_*.
[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, SDB_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 == SDB_CONNECTION_DATA)
207                                         || (status == SDB_CONNECTION_ERROR))
208                                 break;
209                         if (status == SDB_CONNECTION_OK) {
210                                 /* pre 0.4 versions used OK instead of DATA */
211                                 sdb_log(SDB_LOG_WARNING, "Received unexpected OK status from "
212                                                 "server in response to a QUERY (expected DATA); "
213                                                 "assuming we're talking to an old server");
214                                 break;
215                         }
216                 }
218                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
219                         break; /* error */
220         }
222         sdb_llist_iter_destroy(iter);
223         return status;
224 } /* execute_commands */
226 int
227 main(int argc, char **argv)
229         const char *host = NULL;
230         const char *user = NULL;
232         const char *homedir;
233         char hist_file[1024] = "";
235         sdb_input_t input = SDB_INPUT_INIT;
236         sdb_llist_t *commands = NULL;
238         while (42) {
239                 int opt = getopt(argc, argv, "H:U:c:hV");
241                 if (-1 == opt)
242                         break;
244                 switch (opt) {
245                         case 'H':
246                                 host = optarg;
247                                 break;
248                         case 'U':
249                                 user = optarg;
250                                 break;
252                         case 'c':
253                                 {
254                                         sdb_object_t *obj;
256                                         if (! commands)
257                                                 commands = sdb_llist_create();
258                                         if (! commands) {
259                                                 sdb_log(SDB_LOG_ERR, "Failed to create list object");
260                                                 exit(1);
261                                         }
263                                         if (! (obj = sdb_object_create_T(optarg, sdb_object_t))) {
264                                                 sdb_log(SDB_LOG_ERR, "Failed to create object");
265                                                 exit(1);
266                                         }
267                                         if (sdb_llist_append(commands, obj)) {
268                                                 sdb_log(SDB_LOG_ERR, "Failed to append command to list");
269                                                 sdb_object_deref(obj);
270                                                 exit(1);
271                                         }
272                                         sdb_object_deref(obj);
273                                 }
274                                 break;
276                         case 'h':
277                                 exit_usage(argv[0], 0);
278                                 break;
279                         case 'V':
280                                 exit_version();
281                                 break;
282                         default:
283                                 exit_usage(argv[0], 1);
284                 }
285         }
287         if (optind < argc)
288                 exit_usage(argv[0], 1);
290         if (! host)
291                 host = DEFAULT_SOCKET;
292         if (! user) {
293                 user = get_current_user();
294                 if (! user)
295                         exit(1);
296         }
298         input.client = sdb_client_create(host);
299         if (! input.client) {
300                 sdb_log(SDB_LOG_ERR, "Failed to create client object");
301                 exit(1);
302         }
303         if (sdb_client_connect(input.client, user)) {
304                 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
305                 sdb_client_destroy(input.client);
306                 exit(1);
307         }
309         if (commands) {
310                 int status = execute_commands(input.client, commands);
311                 sdb_llist_destroy(commands);
312                 sdb_client_destroy(input.client);
313                 if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
314                         exit(1);
315                 exit(0);
316         }
318         sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
319                         SDB_CLIENT_VERSION_EXTRA" (libsysdbclient %s%s)\n",
320                         sdb_client_version_string(), sdb_client_version_extra());
322         using_history();
324         if ((homedir = get_homedir(user))) {
325                 snprintf(hist_file, sizeof(hist_file) - 1,
326                                 "%s/.sysdb_history", homedir);
327                 hist_file[sizeof(hist_file) - 1] = '\0';
329                 errno = 0;
330                 if (read_history(hist_file) && (errno != ENOENT)) {
331                         char errbuf[1024];
332                         sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
333                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
334                 }
335         }
337         input.input = sdb_strbuf_create(2048);
338         sdb_input_init(&input);
339         sdb_input_mainloop();
341         sdb_client_shutdown(input.client, SHUT_WR);
342         while (! sdb_client_eof(input.client)) {
343                 /* wait for remaining data to arrive */
344                 sdb_command_print_reply(input.client);
345         }
347         if (hist_file[0] != '\0') {
348                 errno = 0;
349                 if (write_history(hist_file)) {
350                         char errbuf[1024];
351                         sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
352                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
353                 }
354         }
356         sdb_client_destroy(input.client);
357         sdb_strbuf_destroy(input.input);
358         return 0;
359 } /* main */
361 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */