Code

utils/os: Added sdb_get_current_user().
authorSebastian Harl <sh@tokkee.org>
Tue, 9 Dec 2014 14:18:04 +0000 (15:18 +0100)
committerSebastian 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
src/include/utils/os.h
src/tools/sysdb/main.c
src/utils/os.c

index 3472b6f527ed55068670db2cd99069e2f40ef0c7..ca558623722049c61fb8808d035aaf1867bdaa37 100644 (file)
@@ -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@
index 2e6c54776ede89ba35d2f70aacda889df89b7d75..07328dd11a2b0d9aaf96c82c0f920e1d88082070 100644 (file)
@@ -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
index 6e75ef9b6c7687484848739fda8eeeaf3c85a233..b3a73c698e1e2507f9741b60a6692cb11ed5dd6c 100644 (file)
@@ -37,6 +37,7 @@
 #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)
 {
@@ -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 <options>\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);
index 7858825e1c889ad040b028284b9c4725c29a8068..e3f1fe2d42bb61d072940949a7ddee345836d4f6 100644 (file)
@@ -30,6 +30,7 @@
 #endif /* HAVE_CONFIG_H */
 
 #include "utils/os.h"
+#include "utils/error.h"
 
 #include <errno.h>
 
@@ -44,6 +45,7 @@
 #include <unistd.h>
 
 #include <libgen.h>
+#include <pwd.h>
 
 /*
  * 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 : */