ada0ec3ae30f5dba633265eee73a2db937118a3c
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"
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)
112 {
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)
140 {
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)
165 {
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 while (42) {
175 int opt = getopt(argc, argv, "H:U:hV");
177 if (-1 == opt)
178 break;
180 switch (opt) {
181 case 'H':
182 host = optarg;
183 break;
184 case 'U':
185 user = optarg;
186 break;
188 case 'h':
189 exit_usage(argv[0], 0);
190 break;
191 case 'V':
192 exit_version();
193 break;
194 default:
195 exit_usage(argv[0], 1);
196 }
197 }
199 if (optind < argc)
200 exit_usage(argv[0], 1);
202 if (! host)
203 host = DEFAULT_SOCKET;
204 if (! user) {
205 user = get_current_user();
206 if (! user)
207 exit(1);
208 }
210 client = sdb_client_create(host);
211 if (! client) {
212 sdb_log(SDB_LOG_ERR, "Failed to create client object");
213 exit(1);
214 }
215 if (sdb_client_connect(client, user)) {
216 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
217 sdb_client_destroy(client);
218 exit(1);
219 }
221 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
222 SDB_CLIENT_VERSION_EXTRA"\n");
224 using_history();
226 if ((homedir = get_homedir(user))) {
227 snprintf(hist_file, sizeof(hist_file) - 1,
228 "%s/.sysdb_history", homedir);
229 hist_file[sizeof(hist_file) - 1] = '\0';
231 errno = 0;
232 if (read_history(hist_file) && (errno != ENOENT)) {
233 char errbuf[1024];
234 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
235 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
236 }
237 }
239 while (42) {
240 char *line = readline("sysdb> ");
242 if (! line)
243 break;
244 if (*line == '\0') {
245 free(line);
246 continue;
247 }
249 if (*line != ' ')
250 add_history(line);
252 free(line);
253 }
255 if (hist_file[0] != '\0') {
256 errno = 0;
257 if (write_history(hist_file)) {
258 char errbuf[1024];
259 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
260 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
261 }
262 }
264 sdb_client_destroy(client);
265 return 0;
266 } /* main */
268 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */