diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c
index 6e75ef9b6c7687484848739fda8eeeaf3c85a233..b3a73c698e1e2507f9741b60a6692cb11ed5dd6c 100644 (file)
--- a/src/tools/sysdb/main.c
+++ b/src/tools/sysdb/main.c
#include "utils/error.h"
#include "utils/llist.h"
#include "utils/strbuf.h"
#include "utils/error.h"
#include "utils/llist.h"
#include "utils/strbuf.h"
+#include "utils/os.h"
#include <errno.h>
#include <errno.h>
# define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
#endif
# 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 const char *
get_homedir(const char *username)
{
static void
exit_usage(char *name, int status)
{
static void
exit_usage(char *name, int status)
{
+ char *user = sdb_get_current_user();
printf(
"Usage: %s <options>\n"
printf(
"Usage: %s <options>\n"
" -V display the version number and copyright\n"
"\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
" -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 */
exit(status);
} /* exit_usage */
main(int argc, char **argv)
{
const char *host = NULL;
main(int argc, char **argv)
{
const char *host = NULL;
- const char *user = NULL;
+ char *user = NULL;
const char *homedir;
char hist_file[1024] = "";
const char *homedir;
char hist_file[1024] = "";
if (! host)
host = DEFAULT_SOCKET;
if (! host)
host = DEFAULT_SOCKET;
- if (! user) {
- user = get_current_user();
- if (! user)
- exit(1);
- }
+ if (! user)
+ user = sdb_get_current_user();
+ else
+ user = strdup(user);
+ if (! user)
+ exit(1);
input.client = sdb_client_create(host);
if (! input.client) {
sdb_log(SDB_LOG_ERR, "Failed to create client object");
input.client = sdb_client_create(host);
if (! input.client) {
sdb_log(SDB_LOG_ERR, "Failed to create client object");
+ free(user);
exit(1);
}
if (sdb_client_connect(input.client, user)) {
sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
sdb_client_destroy(input.client);
exit(1);
}
if (sdb_client_connect(input.client, user)) {
sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
sdb_client_destroy(input.client);
+ free(user);
exit(1);
}
exit(1);
}
int status = execute_commands(input.client, commands);
sdb_llist_destroy(commands);
sdb_client_destroy(input.client);
int status = execute_commands(input.client, commands);
sdb_llist_destroy(commands);
sdb_client_destroy(input.client);
+ free(user);
if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
exit(1);
exit(0);
if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
exit(1);
exit(0);
hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
}
}
hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
}
}
+ free(user);
input.input = sdb_strbuf_create(2048);
sdb_input_init(&input);
input.input = sdb_strbuf_create(2048);
sdb_input_init(&input);