7283a21ae9ce42321e33885b17e6d3775894c307
1 /*
2 * SysDB - src/client/sysdb.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"
36 #include <errno.h>
38 #if HAVE_LIBGEN_H
39 # include <libgen.h>
40 #else /* HAVE_LIBGEN_H */
41 # define basename(path) (path)
42 #endif /* ! HAVE_LIBGEN_H */
44 #include <sys/stat.h>
45 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include <unistd.h>
53 #include <sys/types.h>
55 #include <pwd.h>
57 #if HAVE_EDITLINE_READLINE_H
58 # include <editline/readline.h>
59 # if HAVE_EDITLINE_HISTORY_H
60 # include <editline/history.h>
61 # endif
62 #elif HAVE_READLINE_READLINE_H
63 # include <readline/readline.h>
64 # if HAVE_READLINE_HISTORY_H
65 # include <readline/history.h>
66 # endif
67 #elif HAVE_READLINE_H
68 # include <readline.h>
69 # if HAVE_HISTORY_H
70 # include <history.h>
71 # endif
72 #endif /* READLINEs */
74 #ifndef DEFAULT_SOCKET
75 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
76 #endif
78 static void
79 exit_usage(char *name, int status)
80 {
81 printf(
82 "Usage: %s <options>\n"
84 "\nOptions:\n"
85 " -h display this help and exit\n"
86 " -V display the version number and copyright\n"
88 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
89 PACKAGE_URL"\n", basename(name));
90 exit(status);
91 } /* exit_usage */
93 static void
94 exit_version(void)
95 {
96 printf("SysDB version "SDB_CLIENT_VERSION_STRING
97 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
98 "using libsysdbclient version %s%s\n"
99 "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n"
101 "\nThis is free software under the terms of the BSD license, see "
102 "the source for\ncopying conditions. There is NO WARRANTY; not "
103 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
104 "PURPOSE.\n", sdb_client_version_string(),
105 sdb_client_version_extra());
106 exit(0);
107 } /* exit_version */
109 static const char *
110 get_current_user(void)
111 {
112 struct passwd pw_entry;
113 struct passwd *result = NULL;
115 uid_t uid;
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 uid = geteuid();
125 memset(&pw_entry, 0, sizeof(pw_entry));
126 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
128 if (status || (! result)) {
129 char errbuf[1024];
130 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
131 sdb_strerror(errno, errbuf, sizeof(errbuf)));
132 return NULL;
133 }
134 return result->pw_name;
135 } /* get_current_user */
137 static const char *
138 get_homedir(const char *username)
139 {
140 struct passwd pw_entry;
141 struct passwd *result = NULL;
143 /* needs to be static because we return a pointer into this buffer
144 * to the caller */
145 static char buf[1024];
147 int status;
149 memset(&pw_entry, 0, sizeof(pw_entry));
150 status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
152 if (status || (! result)) {
153 char errbuf[1024];
154 sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
155 "for user %s: %s", username,
156 sdb_strerror(errno, errbuf, sizeof(errbuf)));
157 return NULL;
158 }
159 return result->pw_dir;
160 } /* get_homedir */
162 int
163 main(int argc, char **argv)
164 {
165 sdb_client_t *client;
167 const char *host = NULL;
168 const char *user = NULL;
170 const char *homedir;
171 char hist_file[1024] = "";
173 while (42) {
174 int opt = getopt(argc, argv, "H:U:hV");
176 if (-1 == opt)
177 break;
179 switch (opt) {
180 case 'H':
181 host = optarg;
182 break;
183 case 'U':
184 user = optarg;
185 break;
187 case 'h':
188 exit_usage(argv[0], 0);
189 break;
190 case 'V':
191 exit_version();
192 break;
193 default:
194 exit_usage(argv[0], 1);
195 }
196 }
198 if (optind < argc)
199 exit_usage(argv[0], 1);
201 if (! host)
202 host = DEFAULT_SOCKET;
203 if (! user) {
204 user = get_current_user();
205 if (! user)
206 exit(1);
207 }
209 client = sdb_client_create(host);
210 if (! client) {
211 sdb_log(SDB_LOG_ERR, "Failed to create client object");
212 exit(1);
213 }
214 if (sdb_client_connect(client, user)) {
215 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
216 sdb_client_destroy(client);
217 exit(1);
218 }
220 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
221 SDB_CLIENT_VERSION_EXTRA"\n");
223 using_history();
225 if ((homedir = get_homedir(user))) {
226 snprintf(hist_file, sizeof(hist_file) - 1,
227 "%s/.sysdb_history", homedir);
228 hist_file[sizeof(hist_file) - 1] = '\0';
230 errno = 0;
231 if (read_history(hist_file)) {
232 char errbuf[1024];
233 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
234 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
235 }
236 }
238 while (42) {
239 char *line = readline("sysdb> ");
241 if (! line)
242 break;
243 if (*line == '\0')
244 continue;
246 if (*line != ' ')
247 add_history(line);
248 }
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(client);
260 return 0;
261 } /* main */
263 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */