Code

sysdb: Hide implementation details in the "input" module.
[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/input.h"
34 #include "client/sysdb.h"
35 #include "client/sock.h"
36 #include "utils/error.h"
37 #include "utils/strbuf.h"
39 #include <errno.h>
41 #if HAVE_LIBGEN_H
42 #       include <libgen.h>
43 #else /* HAVE_LIBGEN_H */
44 #       define basename(path) (path)
45 #endif /* ! HAVE_LIBGEN_H */
47 #include <sys/stat.h>
48 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
54 #include <unistd.h>
56 #include <sys/types.h>
58 #include <pwd.h>
60 #if HAVE_EDITLINE_READLINE_H
61 #       include <editline/readline.h>
62 #       if HAVE_EDITLINE_HISTORY_H
63 #               include <editline/history.h>
64 #       endif
65 #elif HAVE_READLINE_READLINE_H
66 #       include <readline/readline.h>
67 #       if HAVE_READLINE_HISTORY_H
68 #               include <readline/history.h>
69 #       endif
70 #elif HAVE_READLINE_H
71 #       include <readline.h>
72 #       if HAVE_HISTORY_H
73 #               include <history.h>
74 #       endif
75 #endif /* READLINEs */
77 #ifndef DEFAULT_SOCKET
78 #       define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
79 #endif
81 static void
82 exit_usage(char *name, int status)
83 {
84         printf(
85 "Usage: %s <options>\n"
87 "\nOptions:\n"
88 "  -h        display this help and exit\n"
89 "  -V        display the version number and copyright\n"
91 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
92 PACKAGE_URL"\n", basename(name));
93         exit(status);
94 } /* exit_usage */
96 static void
97 exit_version(void)
98 {
99         printf("SysDB version "SDB_CLIENT_VERSION_STRING
100                         SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
101                         "using libsysdbclient version %s%s\n"
102                         "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n"
104                         "\nThis is free software under the terms of the BSD license, see "
105                         "the source for\ncopying conditions. There is NO WARRANTY; not "
106                         "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
107                         "PURPOSE.\n", sdb_client_version_string(),
108                         sdb_client_version_extra());
109         exit(0);
110 } /* exit_version */
112 static const char *
113 get_current_user(void)
115         struct passwd pw_entry;
116         struct passwd *result = NULL;
118         uid_t uid;
120         /* needs to be static because we return a pointer into this buffer
121          * to the caller */
122         static char buf[1024];
124         int status;
126         uid = geteuid();
128         memset(&pw_entry, 0, sizeof(pw_entry));
129         status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
131         if (status || (! result)) {
132                 char errbuf[1024];
133                 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
134                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
135                 return NULL;
136         }
137         return result->pw_name;
138 } /* get_current_user */
140 static const char *
141 get_homedir(const char *username)
143         struct passwd pw_entry;
144         struct passwd *result = NULL;
146         /* needs to be static because we return a pointer into this buffer
147          * to the caller */
148         static char buf[1024];
150         int status;
152         memset(&pw_entry, 0, sizeof(pw_entry));
153         status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
155         if (status || (! result)) {
156                 char errbuf[1024];
157                 sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
158                                 "for user %s: %s", username,
159                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
160                 return NULL;
161         }
162         return result->pw_dir;
163 } /* get_homedir */
165 int
166 main(int argc, char **argv)
168         const char *host = NULL;
169         const char *user = NULL;
171         const char *homedir;
172         char hist_file[1024] = "";
174         sdb_input_t input = SDB_INPUT_INIT;
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         input.client = sdb_client_create(host);
213         if (! input.client) {
214                 sdb_log(SDB_LOG_ERR, "Failed to create client object");
215                 exit(1);
216         }
217         if (sdb_client_connect(input.client, user)) {
218                 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
219                 sdb_client_destroy(input.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         input.input = sdb_strbuf_create(2048);
242         sdb_input_init(&input);
243         sdb_input_mainloop();
245         if (hist_file[0] != '\0') {
246                 errno = 0;
247                 if (write_history(hist_file)) {
248                         char errbuf[1024];
249                         sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
250                                         hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
251                 }
252         }
254         sdb_client_destroy(input.client);
255         sdb_strbuf_destroy(input.input);
256         return 0;
257 } /* main */
259 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */