X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftools%2Fsysdb%2Fmain.c;h=b22e11f0f89da462596783a000ffdcb2d70835fa;hb=1d4f883090c6e081041d8909256a393727a2ecf1;hp=b3a73c698e1e2507f9741b60a6692cb11ed5dd6c;hpb=a9ae0d1a4c9e2992d932489c96cd63b2ce2a0c56;p=sysdb.git diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c index b3a73c6..b22e11f 100644 --- a/src/tools/sysdb/main.c +++ b/src/tools/sysdb/main.c @@ -77,34 +77,13 @@ # endif #endif /* READLINEs */ +#include +#include + #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) @@ -202,9 +181,8 @@ int main(int argc, char **argv) { const char *host = NULL; - char *user = NULL; - const char *homedir; + char *homedir; char hist_file[1024] = ""; sdb_input_t input = SDB_INPUT_INIT; @@ -221,7 +199,7 @@ main(int argc, char **argv) host = optarg; break; case 'U': - user = optarg; + input.user = optarg; break; case 'c': @@ -264,23 +242,26 @@ main(int argc, char **argv) if (! host) host = DEFAULT_SOCKET; - if (! user) - user = sdb_get_current_user(); + if (! input.user) + input.user = sdb_get_current_user(); else - user = strdup(user); - if (! user) + input.user = strdup(input.user); + 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"); - free(user); + free(input.user); exit(1); } - if (sdb_client_connect(input.client, user)) { + if (sdb_client_connect(input.client, input.user)) { sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd"); sdb_client_destroy(input.client); - free(user); + free(input.user); exit(1); } @@ -288,7 +269,7 @@ main(int argc, char **argv) int status = execute_commands(input.client, commands); sdb_llist_destroy(commands); sdb_client_destroy(input.client); - free(user); + free(input.user); if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA)) exit(1); exit(0); @@ -300,10 +281,12 @@ main(int argc, char **argv) using_history(); - if ((homedir = get_homedir(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)) { @@ -312,7 +295,7 @@ main(int argc, char **argv) hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf))); } } - free(user); + free(input.user); input.input = sdb_strbuf_create(2048); sdb_input_init(&input); @@ -335,6 +318,8 @@ main(int argc, char **argv) sdb_client_destroy(input.client); sdb_strbuf_destroy(input.input); + + ERR_free_strings(); return 0; } /* main */