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 extern int yylex(void);
83 static void
84 exit_usage(char *name, int status)
85 {
86 printf(
87 "Usage: %s <options>\n"
89 "\nOptions:\n"
90 " -h display this help and exit\n"
91 " -V display the version number and copyright\n"
93 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
94 PACKAGE_URL"\n", basename(name));
95 exit(status);
96 } /* exit_usage */
98 static void
99 exit_version(void)
100 {
101 printf("SysDB version "SDB_CLIENT_VERSION_STRING
102 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
103 "using libsysdbclient version %s%s\n"
104 "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n"
106 "\nThis is free software under the terms of the BSD license, see "
107 "the source for\ncopying conditions. There is NO WARRANTY; not "
108 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
109 "PURPOSE.\n", sdb_client_version_string(),
110 sdb_client_version_extra());
111 exit(0);
112 } /* exit_version */
114 static const char *
115 get_current_user(void)
116 {
117 struct passwd pw_entry;
118 struct passwd *result = NULL;
120 uid_t uid;
122 /* needs to be static because we return a pointer into this buffer
123 * to the caller */
124 static char buf[1024];
126 int status;
128 uid = geteuid();
130 memset(&pw_entry, 0, sizeof(pw_entry));
131 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
133 if (status || (! result)) {
134 char errbuf[1024];
135 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
136 sdb_strerror(errno, errbuf, sizeof(errbuf)));
137 return NULL;
138 }
139 return result->pw_name;
140 } /* get_current_user */
142 static const char *
143 get_homedir(const char *username)
144 {
145 struct passwd pw_entry;
146 struct passwd *result = NULL;
148 /* needs to be static because we return a pointer into this buffer
149 * to the caller */
150 static char buf[1024];
152 int status;
154 memset(&pw_entry, 0, sizeof(pw_entry));
155 status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
157 if (status || (! result)) {
158 char errbuf[1024];
159 sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
160 "for user %s: %s", username,
161 sdb_strerror(errno, errbuf, sizeof(errbuf)));
162 return NULL;
163 }
164 return result->pw_dir;
165 } /* get_homedir */
167 int
168 main(int argc, char **argv)
169 {
170 sdb_client_t *client;
172 const char *host = NULL;
173 const char *user = NULL;
175 const char *homedir;
176 char hist_file[1024] = "";
178 sdb_input_t input = SDB_INPUT_INIT;
180 while (42) {
181 int opt = getopt(argc, argv, "H:U:hV");
183 if (-1 == opt)
184 break;
186 switch (opt) {
187 case 'H':
188 host = optarg;
189 break;
190 case 'U':
191 user = optarg;
192 break;
194 case 'h':
195 exit_usage(argv[0], 0);
196 break;
197 case 'V':
198 exit_version();
199 break;
200 default:
201 exit_usage(argv[0], 1);
202 }
203 }
205 if (optind < argc)
206 exit_usage(argv[0], 1);
208 if (! host)
209 host = DEFAULT_SOCKET;
210 if (! user) {
211 user = get_current_user();
212 if (! user)
213 exit(1);
214 }
216 client = sdb_client_create(host);
217 if (! client) {
218 sdb_log(SDB_LOG_ERR, "Failed to create client object");
219 exit(1);
220 }
221 if (sdb_client_connect(client, user)) {
222 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
223 sdb_client_destroy(client);
224 exit(1);
225 }
227 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
228 SDB_CLIENT_VERSION_EXTRA"\n");
230 using_history();
232 if ((homedir = get_homedir(user))) {
233 snprintf(hist_file, sizeof(hist_file) - 1,
234 "%s/.sysdb_history", homedir);
235 hist_file[sizeof(hist_file) - 1] = '\0';
237 errno = 0;
238 if (read_history(hist_file) && (errno != ENOENT)) {
239 char errbuf[1024];
240 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
241 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
242 }
243 }
245 input.buf = sdb_strbuf_create(2048);
246 sdb_input_set(&input);
247 yylex();
249 if (hist_file[0] != '\0') {
250 errno = 0;
251 if (write_history(hist_file)) {
252 char errbuf[1024];
253 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
254 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
255 }
256 }
258 sdb_client_destroy(client);
259 return 0;
260 } /* main */
262 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */