Code

0639e5f74829042a4e3a8532b97f6ee74d853236
[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 "client/sysdb.h"
33 #include "client/sock.h"
34 #include "utils/error.h"
35 #include "utils/strbuf.h"
37 #include <errno.h>
39 #if HAVE_LIBGEN_H
40 #       include <libgen.h>
41 #else /* HAVE_LIBGEN_H */
42 #       define basename(path) (path)
43 #endif /* ! HAVE_LIBGEN_H */
45 #include <sys/stat.h>
46 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
52 #include <unistd.h>
54 #include <sys/types.h>
56 #include <pwd.h>
58 #if HAVE_EDITLINE_READLINE_H
59 #       include <editline/readline.h>
60 #       if HAVE_EDITLINE_HISTORY_H
61 #               include <editline/history.h>
62 #       endif
63 #elif HAVE_READLINE_READLINE_H
64 #       include <readline/readline.h>
65 #       if HAVE_READLINE_HISTORY_H
66 #               include <readline/history.h>
67 #       endif
68 #elif HAVE_READLINE_H
69 #       include <readline.h>
70 #       if HAVE_HISTORY_H
71 #               include <history.h>
72 #       endif
73 #endif /* READLINEs */
75 #ifndef DEFAULT_SOCKET
76 #       define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
77 #endif
79 static void
80 exit_usage(char *name, int status)
81 {
82         printf(
83 "Usage: %s <options>\n"
85 "\nOptions:\n"
86 "  -h        display this help and exit\n"
87 "  -V        display the version number and copyright\n"
89 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
90 PACKAGE_URL"\n", basename(name));
91         exit(status);
92 } /* exit_usage */
94 static void
95 exit_version(void)
96 {
97         printf("SysDB version "SDB_CLIENT_VERSION_STRING
98                         SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
99                         "using libsysdbclient version %s%s\n"
100                         "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n"
102                         "\nThis is free software under the terms of the BSD license, see "
103                         "the source for\ncopying conditions. There is NO WARRANTY; not "
104                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
105                         "PURPOSE.\n", sdb_client_version_string(),
106                         sdb_client_version_extra());
107         exit(0);
108 } /* exit_version */
110 static const char *
111 get_current_user(void)
113         struct passwd pw_entry;
114         struct passwd *result = NULL;
116         uid_t uid;
118         /* needs to be static because we return a pointer into this buffer
119          * to the caller */
120         static char buf[1024];
122         int status;
124         uid = geteuid();
126         memset(&pw_entry, 0, sizeof(pw_entry));
127         status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
129         if (status || (! result)) {
130                 char errbuf[1024];
131                 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
132                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
133                 return NULL;
134         }
135         return result->pw_name;
136 } /* get_current_user */
138 static const char *
139 get_homedir(const char *username)
141         struct passwd pw_entry;
142         struct passwd *result = NULL;
144         /* needs to be static because we return a pointer into this buffer
145          * to the caller */
146         static char buf[1024];
148         int status;
150         memset(&pw_entry, 0, sizeof(pw_entry));
151         status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
153         if (status || (! result)) {
154                 char errbuf[1024];
155                 sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
156                                 "for user %s: %s", username,
157                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
158                 return NULL;
159         }
160         return result->pw_dir;
161 } /* get_homedir */
163 int
164 main(int argc, char **argv)
166         sdb_client_t *client;
168         const char *host = NULL;
169         const char *user = NULL;
171         const char *homedir;
172         char hist_file[1024] = "";
174         sdb_strbuf_t *buf;
176         while (42) {
177                 int opt = getopt(argc, argv, "H:U:hV");
179                 if (-1 == opt)
180                         break;
182                 switch (opt) {
183                         case 'H':
184                                 host = optarg;
185                                 break;
186                         case 'U':
187                                 user = optarg;
188                                 break;
190                         case 'h':
191                                 exit_usage(argv[0], 0);
192                                 break;
193                         case 'V':
194                                 exit_version();
195                                 break;
196                         default:
197                                 exit_usage(argv[0], 1);
198                 }
199         }
201         if (optind < argc)
202                 exit_usage(argv[0], 1);
204         if (! host)
205                 host = DEFAULT_SOCKET;
206         if (! user) {
207                 user = get_current_user();
208                 if (! user)
209                         exit(1);
210         }
212         client = sdb_client_create(host);
213         if (! client) {
214                 sdb_log(SDB_LOG_ERR, "Failed to create client object");
215                 exit(1);
216         }
217         if (sdb_client_connect(client, user)) {
218                 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
219                 sdb_client_destroy(client);
220                 exit(1);
221         }
223         sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
224                         SDB_CLIENT_VERSION_EXTRA"\n");
226         using_history();
228         if ((homedir = get_homedir(user))) {
229                 snprintf(hist_file, sizeof(hist_file) - 1,
230                                 "%s/.sysdb_history", homedir);
231                 hist_file[sizeof(hist_file) - 1] = '\0';
233                 errno = 0;
234                 if (read_history(hist_file) && (errno != ENOENT)) {
235                         char errbuf[1024];
236                         sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
237                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
238                 }
239         }
241         buf = sdb_strbuf_create(1024);
243         while (42) {
244                 const char *prompt = "sysdb=> ";
245                 const char *query;
246                 char *input;
248                 if (sdb_strbuf_len(buf))
249                         prompt = "sysdb-> ";
251                 input = readline(prompt);
253                 if (! input)
254                         break;
256                 sdb_strbuf_append(buf, input);
257                 free(input);
259                 query = sdb_strbuf_string(buf);
260                 if (! strchr(query, (int)';'))
261                         continue;
263                 /* XXX */
264                 sdb_strbuf_clear(buf);
265         }
267         if (hist_file[0] != '\0') {
268                 errno = 0;
269                 if (write_history(hist_file)) {
270                         char errbuf[1024];
271                         sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
272                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
273                 }
274         }
276         sdb_client_destroy(client);
277         return 0;
278 } /* main */
280 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */