summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: b8dac59)
raw | patch | inline | side by side (parent: b8dac59)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 9 Dec 2014 14:18:04 +0000 (15:18 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 9 Dec 2014 14:18:04 +0000 (15:18 +0100) |
… copied from sysdb/main.c which now uses this new function.
src/Makefile.am | patch | blob | history | |
src/include/utils/os.h | patch | blob | history | |
src/tools/sysdb/main.c | patch | blob | history | |
src/utils/os.c | patch | blob | history |
diff --git a/src/Makefile.am b/src/Makefile.am
index 3472b6f527ed55068670db2cd99069e2f40ef0c7..ca558623722049c61fb8808d035aaf1867bdaa37 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
tools/sysdb/command.c tools/sysdb/command.h \
tools/sysdb/input.c tools/sysdb/input.h \
core/object.c include/core/object.h \
- utils/llist.c include/utils/llist.h
+ utils/llist.c include/utils/llist.h \
+ utils/os.c include/utils/os.h
sysdb_CFLAGS = -DBUILD_DATE="\"$$( date --utc '+%F %T' ) (UTC)\"" \
$(AM_CFLAGS) @READLINE_CFLAGS@
sysdb_LDADD = libsysdb_scanner.la libsysdbclient.la @READLINE_LIBS@
diff --git a/src/include/utils/os.h b/src/include/utils/os.h
index 2e6c54776ede89ba35d2f70aacda889df89b7d75..07328dd11a2b0d9aaf96c82c0f920e1d88082070 100644 (file)
--- a/src/include/utils/os.h
+++ b/src/include/utils/os.h
int
sdb_remove_all(const char *pathname);
+/*
+ * sdb_get_current_user:
+ * Returns the name of the current user. The string is allocated dynamically
+ * and has to be freed by the caller.
+ *
+ * Returns:
+ * - the username on success
+ * - NULL else
+ */
+char *
+sdb_get_current_user(void);
+
#ifdef __cplusplus
} /* extern "C" */
#endif
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/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;
- const char *user = NULL;
+ char *user = NULL;
const char *homedir;
char hist_file[1024] = "";
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");
+ 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);
+ free(user);
exit(1);
}
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);
hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
}
}
+ free(user);
input.input = sdb_strbuf_create(2048);
sdb_input_init(&input);
diff --git a/src/utils/os.c b/src/utils/os.c
index 7858825e1c889ad040b028284b9c4725c29a8068..e3f1fe2d42bb61d072940949a7ddee345836d4f6 100644 (file)
--- a/src/utils/os.c
+++ b/src/utils/os.c
#endif /* HAVE_CONFIG_H */
#include "utils/os.h"
+#include "utils/error.h"
#include <errno.h>
#include <unistd.h>
#include <libgen.h>
+#include <pwd.h>
/*
* public API
return remove(pathname);
} /* sdb_remove_all */
+char *
+sdb_get_current_user(void)
+{
+ struct passwd pw_entry;
+ struct passwd *result = NULL;
+
+ uid_t uid;
+
+ 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 strdup(result->pw_name);
+} /* sdb_get_current_user */
+
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */