X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Ftools%2Fsysdb%2Fmain.c;h=357ef34827672d0033030eeefe8d85d059f98836;hb=13fe0f9ec3d161fab7a015054649910541d75f5e;hp=6e75ef9b6c7687484848739fda8eeeaf3c85a233;hpb=4d93f084bf26672e322b82ce192c8beb5765e1d0;p=sysdb.git diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c index 6e75ef9..357ef34 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,6 @@ int main(int argc, char **argv) { const char *host = NULL; - const char *user = NULL; const char *homedir; char hist_file[1024] = ""; @@ -246,7 +220,7 @@ main(int argc, char **argv) host = optarg; break; case 'U': - user = optarg; + input.user = optarg; break; case 'c': @@ -289,20 +263,23 @@ main(int argc, char **argv) if (! host) host = DEFAULT_SOCKET; - if (! user) { - user = get_current_user(); - if (! user) - exit(1); - } + if (! input.user) + input.user = sdb_get_current_user(); + else + input.user = strdup(input.user); + if (! input.user) + exit(1); input.client = sdb_client_create(host); if (! input.client) { sdb_log(SDB_LOG_ERR, "Failed to create client object"); + 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(input.user); exit(1); } @@ -310,6 +287,7 @@ main(int argc, char **argv) int status = execute_commands(input.client, commands); sdb_llist_destroy(commands); sdb_client_destroy(input.client); + free(input.user); if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA)) exit(1); exit(0); @@ -321,7 +299,7 @@ main(int argc, char **argv) using_history(); - if ((homedir = get_homedir(user))) { + if ((homedir = get_homedir(input.user))) { snprintf(hist_file, sizeof(hist_file) - 1, "%s/.sysdb_history", homedir); hist_file[sizeof(hist_file) - 1] = '\0'; @@ -333,6 +311,7 @@ main(int argc, char **argv) hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf))); } } + free(input.user); input.input = sdb_strbuf_create(2048); sdb_input_init(&input);