b20e0ca4d65e3f0de245e358459402de54c77bb0
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 const char *
82 get_current_user(void)
83 {
84 struct passwd pw_entry;
85 struct passwd *result = NULL;
87 uid_t uid;
89 /* needs to be static because we return a pointer into this buffer
90 * to the caller */
91 static char buf[1024];
93 int status;
95 uid = geteuid();
97 memset(&pw_entry, 0, sizeof(pw_entry));
98 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
100 if (status || (! result)) {
101 char errbuf[1024];
102 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
103 sdb_strerror(errno, errbuf, sizeof(errbuf)));
104 return NULL;
105 }
106 return result->pw_name;
107 } /* get_current_user */
109 static const char *
110 get_homedir(const char *username)
111 {
112 struct passwd pw_entry;
113 struct passwd *result = NULL;
115 /* needs to be static because we return a pointer into this buffer
116 * to the caller */
117 static char buf[1024];
119 int status;
121 memset(&pw_entry, 0, sizeof(pw_entry));
122 status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
124 if (status || (! result)) {
125 char errbuf[1024];
126 sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
127 "for user %s: %s", username,
128 sdb_strerror(errno, errbuf, sizeof(errbuf)));
129 return NULL;
130 }
131 return result->pw_dir;
132 } /* get_homedir */
134 static void
135 exit_usage(char *name, int status)
136 {
137 printf(
138 "Usage: %s <options>\n"
140 "\nOptions:\n"
141 " -H HOST the host to connect to\n"
142 " default: "DEFAULT_SOCKET"\n"
143 " -U USER the username to connect as\n"
144 " default: %s\n"
145 "\n"
146 " -h display this help and exit\n"
147 " -V display the version number and copyright\n"
149 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
150 PACKAGE_URL"\n", basename(name), get_current_user());
151 exit(status);
152 } /* exit_usage */
154 static void
155 exit_version(void)
156 {
157 printf("SysDB version "SDB_CLIENT_VERSION_STRING
158 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
159 "using libsysdbclient version %s%s\n"
160 "Copyright (C) 2012-2014 "PACKAGE_MAINTAINER"\n"
162 "\nThis is free software under the terms of the BSD license, see "
163 "the source for\ncopying conditions. There is NO WARRANTY; not "
164 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
165 "PURPOSE.\n", sdb_client_version_string(),
166 sdb_client_version_extra());
167 exit(0);
168 } /* exit_version */
170 int
171 main(int argc, char **argv)
172 {
173 const char *host = NULL;
174 const char *user = NULL;
176 const char *homedir;
177 char hist_file[1024] = "";
179 sdb_input_t input = SDB_INPUT_INIT;
181 while (42) {
182 int opt = getopt(argc, argv, "H:U:hV");
184 if (-1 == opt)
185 break;
187 switch (opt) {
188 case 'H':
189 host = optarg;
190 break;
191 case 'U':
192 user = optarg;
193 break;
195 case 'h':
196 exit_usage(argv[0], 0);
197 break;
198 case 'V':
199 exit_version();
200 break;
201 default:
202 exit_usage(argv[0], 1);
203 }
204 }
206 if (optind < argc)
207 exit_usage(argv[0], 1);
209 if (! host)
210 host = DEFAULT_SOCKET;
211 if (! user) {
212 user = get_current_user();
213 if (! user)
214 exit(1);
215 }
217 input.client = sdb_client_create(host);
218 if (! input.client) {
219 sdb_log(SDB_LOG_ERR, "Failed to create client object");
220 exit(1);
221 }
222 if (sdb_client_connect(input.client, user)) {
223 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
224 sdb_client_destroy(input.client);
225 exit(1);
226 }
228 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
229 SDB_CLIENT_VERSION_EXTRA"\n");
231 using_history();
233 if ((homedir = get_homedir(user))) {
234 snprintf(hist_file, sizeof(hist_file) - 1,
235 "%s/.sysdb_history", homedir);
236 hist_file[sizeof(hist_file) - 1] = '\0';
238 errno = 0;
239 if (read_history(hist_file) && (errno != ENOENT)) {
240 char errbuf[1024];
241 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
242 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
243 }
244 }
246 input.input = sdb_strbuf_create(2048);
247 sdb_input_init(&input);
248 sdb_input_mainloop();
250 if (hist_file[0] != '\0') {
251 errno = 0;
252 if (write_history(hist_file)) {
253 char errbuf[1024];
254 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
255 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
256 }
257 }
259 sdb_client_destroy(input.client);
260 sdb_strbuf_destroy(input.input);
261 return 0;
262 } /* main */
264 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */