06f4de2f0fb858152a74849300173627281de1cb
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 const char *host = NULL;
171 const char *user = NULL;
173 const char *homedir;
174 char hist_file[1024] = "";
176 sdb_input_t input = SDB_INPUT_INIT;
178 while (42) {
179 int opt = getopt(argc, argv, "H:U:hV");
181 if (-1 == opt)
182 break;
184 switch (opt) {
185 case 'H':
186 host = optarg;
187 break;
188 case 'U':
189 user = optarg;
190 break;
192 case 'h':
193 exit_usage(argv[0], 0);
194 break;
195 case 'V':
196 exit_version();
197 break;
198 default:
199 exit_usage(argv[0], 1);
200 }
201 }
203 if (optind < argc)
204 exit_usage(argv[0], 1);
206 if (! host)
207 host = DEFAULT_SOCKET;
208 if (! user) {
209 user = get_current_user();
210 if (! user)
211 exit(1);
212 }
214 input.client = sdb_client_create(host);
215 if (! input.client) {
216 sdb_log(SDB_LOG_ERR, "Failed to create client object");
217 exit(1);
218 }
219 if (sdb_client_connect(input.client, user)) {
220 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
221 sdb_client_destroy(input.client);
222 exit(1);
223 }
225 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
226 SDB_CLIENT_VERSION_EXTRA"\n");
228 using_history();
230 if ((homedir = get_homedir(user))) {
231 snprintf(hist_file, sizeof(hist_file) - 1,
232 "%s/.sysdb_history", homedir);
233 hist_file[sizeof(hist_file) - 1] = '\0';
235 errno = 0;
236 if (read_history(hist_file) && (errno != ENOENT)) {
237 char errbuf[1024];
238 sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
239 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
240 }
241 }
243 input.input = sdb_strbuf_create(2048);
244 sdb_input_set(&input);
245 yylex();
247 if (hist_file[0] != '\0') {
248 errno = 0;
249 if (write_history(hist_file)) {
250 char errbuf[1024];
251 sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
252 hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
253 }
254 }
256 sdb_client_destroy(input.client);
257 sdb_strbuf_destroy(input.input);
258 return 0;
259 } /* main */
261 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */