Code

Move sdb_get_homedir() from tools/sysdb to utils/os.
[sysdb.git] / src / tools / sysdb / main.c
index 357ef34827672d0033030eeefe8d85d059f98836..b22e11f0f89da462596783a000ffdcb2d70835fa 100644 (file)
 #      endif
 #endif /* READLINEs */
 
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+
 #ifndef DEFAULT_SOCKET
 #      define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
 #endif
 
-static const char *
-get_homedir(const char *username)
-{
-       struct passwd pw_entry;
-       struct passwd *result = NULL;
-
-       /* needs to be static because we return a pointer into this buffer
-        * to the caller */
-       static char buf[1024];
-
-       int status;
-
-       memset(&pw_entry, 0, sizeof(pw_entry));
-       status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
-
-       if (status || (! result)) {
-               char errbuf[1024];
-               sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
-                               "for user %s: %s", username,
-                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
-               return NULL;
-       }
-       return result->pw_dir;
-} /* get_homedir */
 
 static void
 exit_usage(char *name, int status)
@@ -203,7 +182,7 @@ main(int argc, char **argv)
 {
        const char *host = NULL;
 
-       const char *homedir;
+       char *homedir;
        char hist_file[1024] = "";
 
        sdb_input_t input = SDB_INPUT_INIT;
@@ -270,6 +249,9 @@ main(int argc, char **argv)
        if (! input.user)
                exit(1);
 
+       SSL_load_error_strings();
+       OpenSSL_add_ssl_algorithms();
+
        input.client = sdb_client_create(host);
        if (! input.client) {
                sdb_log(SDB_LOG_ERR, "Failed to create client object");
@@ -299,10 +281,12 @@ main(int argc, char **argv)
 
        using_history();
 
-       if ((homedir = get_homedir(input.user))) {
+       if ((homedir = sdb_get_homedir())) {
                snprintf(hist_file, sizeof(hist_file) - 1,
                                "%s/.sysdb_history", homedir);
                hist_file[sizeof(hist_file) - 1] = '\0';
+               free(homedir);
+               homedir = NULL;
 
                errno = 0;
                if (read_history(hist_file) && (errno != ENOENT)) {
@@ -334,6 +318,8 @@ main(int argc, char **argv)
 
        sdb_client_destroy(input.client);
        sdb_strbuf_destroy(input.input);
+
+       ERR_free_strings();
        return 0;
 } /* main */