author | Sebastian Harl <sh@tokkee.org> | |
Fri, 12 Dec 2014 07:55:39 +0000 (08:55 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 12 Dec 2014 07:57:51 +0000 (08:57 +0100) |
1 | 2 | |||
---|---|---|---|---|
src/tools/sysdb/input.h | patch | | diff1 | | diff2 | | blob | history |
src/tools/sysdb/main.c | patch | | diff1 | | diff2 | | blob | history |
t/integration/query.sh | patch | | diff1 | | diff2 | | blob | history |
diff --combined src/tools/sysdb/input.h
index 0ba5c0a4dd385169fc439c1fe133b33464f609a8,b8501f472383cd454608c19aebcab8688e784c60..ba2b32d8d3553dad528bfda657d7f18f79b20234
+++ b/src/tools/sysdb/input.h
typedef struct {
sdb_client_t *client;
- const char *user;
++ char *user;
sdb_strbuf_t *input;
size_t tokenizer_pos;
size_t query_len;
+ /* indicates that we've had non-empty input */
+ bool have_input;
+
bool interactive;
bool eof;
} sdb_input_t;
- #define SDB_INPUT_INIT { NULL, NULL, 0, 0, 1, 0 }
+ #define SDB_INPUT_INIT { NULL, NULL, NULL, 0, 0, 0, 1, 0 }
/*
* sysdb_input:
int
sdb_input_exec_query(void);
+ /*
+ * sdb_input_reconnect:
+ * Let the client reconnect to the server using the settings stored in
+ * sysdb_input.
+ */
+ int
+ sdb_input_reconnect(void);
+
#endif /* SYSDB_INPUT_H */
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
diff --combined src/tools/sysdb/main.c
index b3a73c698e1e2507f9741b60a6692cb11ed5dd6c,6e10e8d9fd87d937a3d668566f9f4fccf7bbcd7d..357ef34827672d0033030eeefe8d85d059f98836
+++ b/src/tools/sysdb/main.c
#include "utils/error.h"
#include "utils/llist.h"
#include "utils/strbuf.h"
+#include "utils/os.h"
#include <errno.h>
# define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
#endif
-static const char *
-get_current_user(void)
-{
- struct passwd pw_entry;
- struct passwd *result = NULL;
-
- uid_t uid;
-
- /* needs to be static because we return a pointer into this buffer
- * to the caller */
- static char buf[1024];
-
- int status;
-
- uid = geteuid();
-
- memset(&pw_entry, 0, sizeof(pw_entry));
- status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
-
- if (status || (! result)) {
- char errbuf[1024];
- sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
- sdb_strerror(errno, errbuf, sizeof(errbuf)));
- return NULL;
- }
- return result->pw_name;
-} /* get_current_user */
-
static const char *
get_homedir(const char *username)
{
static void
exit_usage(char *name, int status)
{
+ char *user = sdb_get_current_user();
printf(
"Usage: %s <options>\n"
" -V display the version number and copyright\n"
"\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
-PACKAGE_URL"\n", basename(name), get_current_user());
+PACKAGE_URL"\n", basename(name), user);
+ free(user);
exit(status);
} /* exit_usage */
main(int argc, char **argv)
{
const char *host = NULL;
- char *user = NULL;
const char *homedir;
char hist_file[1024] = "";
host = optarg;
break;
case 'U':
- user = optarg;
+ input.user = optarg;
break;
case 'c':
if (! host)
host = DEFAULT_SOCKET;
- if (! user)
- user = sdb_get_current_user();
- if (! input.user) {
- input.user = get_current_user();
- if (! input.user)
- exit(1);
- }
++ if (! input.user)
++ input.user = sdb_get_current_user();
+ else
- user = strdup(user);
- if (! user)
++ input.user = strdup(input.user);
++ if (! input.user)
+ exit(1);
input.client = sdb_client_create(host);
if (! input.client) {
sdb_log(SDB_LOG_ERR, "Failed to create client object");
- free(user);
++ free(input.user);
exit(1);
}
- if (sdb_client_connect(input.client, user)) {
+ if (sdb_client_connect(input.client, input.user)) {
sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
sdb_client_destroy(input.client);
- free(user);
++ free(input.user);
exit(1);
}
int status = execute_commands(input.client, commands);
sdb_llist_destroy(commands);
sdb_client_destroy(input.client);
- free(user);
++ free(input.user);
if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
exit(1);
exit(0);
using_history();
- if ((homedir = get_homedir(user))) {
+ if ((homedir = get_homedir(input.user))) {
snprintf(hist_file, sizeof(hist_file) - 1,
"%s/.sysdb_history", homedir);
hist_file[sizeof(hist_file) - 1] = '\0';
hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
}
}
- free(user);
++ free(input.user);
input.input = sdb_strbuf_create(2048);
sdb_input_init(&input);
diff --combined t/integration/query.sh
index 5536c701c9c517eefd3882518e0e2eee1683a88a,f8287b45a645adac6798dbb611b092279e070d4f..eb4ccd8814771f058f6a9a357f1b669f5803b5e5
+++ b/t/integration/query.sh
wait_for_sysdbd
sleep 3
+# Invalid user.
+output="$( run_sysdb_nouser -H "$SOCKET_FILE" \
+ -U $SYSDB_USER-invalid -c 'LIST hosts' 2>&1 )" && exit 1
+echo "$output" | grep -F 'Access denied'
+
# On parse errors, expect a non-zero exit code.
- output="$( run_sysdb -H "$SOCKET_FILE" -c INVALID )" && exit 1
+ output="$( run_sysdb -H "$SOCKET_FILE" -c INVALID 2>&1 )" && exit 1
echo "$output" | grep "Failed to parse query 'INVALID'"
echo "$output" | grep "parse error: syntax error"
echo "$output" | grep -F 'some.host.name' && exit 1
output="$( run_sysdb -H "$SOCKET_FILE" \
- -c "FETCH host 'host1.example.com' FILTER last_update < 0" )" \
+ -c "FETCH host 'host1.example.com' FILTER last_update < 0" 2>&1 )" \
&& exit 1
echo "$output" | grep -F 'not found'
| run_sysdb -H "$SOCKET_FILE"
# When requesting information for unknown hosts, expect a non-zero exit code.
- output="$( run_sysdb -H "$SOCKET_FILE" -c "FETCH host 'does.not.exist'" )" \
- && exit 1
+ output="$( run_sysdb -H "$SOCKET_FILE" \
+ -c "FETCH host 'does.not.exist'" 2>&1 )" && exit 1
echo "$output" | grep -F 'not found'
run_sysdb -H "$SOCKET_FILE" \