From a9ae0d1a4c9e2992d932489c96cd63b2ce2a0c56 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 9 Dec 2014 15:18:04 +0100 Subject: [PATCH] utils/os: Added sdb_get_current_user(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit … copied from sysdb/main.c which now uses this new function. --- src/Makefile.am | 3 ++- src/include/utils/os.h | 12 ++++++++++ src/tools/sysdb/main.c | 50 +++++++++++++----------------------------- src/utils/os.c | 26 ++++++++++++++++++++++ 4 files changed, 55 insertions(+), 36 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 3472b6f..ca55862 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -119,7 +119,8 @@ sysdb_SOURCES = tools/sysdb/main.c include/client/sysdb.h \ 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 2e6c547..07328dd 100644 --- a/src/include/utils/os.h +++ b/src/include/utils/os.h @@ -57,6 +57,18 @@ sdb_mkdir_all(const char *pathname, mode_t mode); 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 6e75ef9..b3a73c6 100644 --- a/src/tools/sysdb/main.c +++ b/src/tools/sysdb/main.c @@ -37,6 +37,7 @@ #include "utils/error.h" #include "utils/llist.h" #include "utils/strbuf.h" +#include "utils/os.h" #include @@ -80,34 +81,6 @@ # 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) { @@ -136,6 +109,7 @@ get_homedir(const char *username) static void exit_usage(char *name, int status) { + char *user = sdb_get_current_user(); printf( "Usage: %s \n" @@ -150,7 +124,8 @@ exit_usage(char *name, int status) " -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 */ @@ -227,7 +202,7 @@ int main(int argc, char **argv) { const char *host = NULL; - const char *user = NULL; + char *user = NULL; const char *homedir; char hist_file[1024] = ""; @@ -289,20 +264,23 @@ main(int argc, char **argv) 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); } @@ -310,6 +288,7 @@ main(int argc, char **argv) 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); @@ -333,6 +312,7 @@ main(int argc, char **argv) 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 7858825..e3f1fe2 100644 --- a/src/utils/os.c +++ b/src/utils/os.c @@ -30,6 +30,7 @@ #endif /* HAVE_CONFIG_H */ #include "utils/os.h" +#include "utils/error.h" #include @@ -44,6 +45,7 @@ #include #include +#include /* * public API @@ -140,5 +142,29 @@ sdb_remove_all(const char *pathname) 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 : */ -- 2.30.2