X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fclient%2Fsysdb.c;h=fa71eed9d064b04d86037412988e5b10a5c1c7b6;hp=073e40ef6c0386709a2a487274543d341e8d8d89;hb=50f0c90249b043acd6dd72979a319f81f2e48f74;hpb=55f329ab567091e3203809ca35af766e45966b45 diff --git a/src/client/sysdb.c b/src/client/sysdb.c index 073e40e..fa71eed 100644 --- a/src/client/sysdb.c +++ b/src/client/sysdb.c @@ -31,6 +31,9 @@ #include "client/sysdb.h" #include "client/sock.h" +#include "utils/error.h" + +#include #if HAVE_LIBGEN_H # include @@ -47,6 +50,10 @@ #include +#include + +#include + #ifndef DEFAULT_SOCKET # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock" #endif @@ -82,16 +89,56 @@ exit_version(void) exit(0); } /* exit_version */ +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 */ + int main(int argc, char **argv) { + sdb_client_t *client; + + const char *host = NULL; + const char *user = NULL; + while (42) { - int opt = getopt(argc, argv, "hV"); + int opt = getopt(argc, argv, "H:U:hV"); if (-1 == opt) break; switch (opt) { + case 'H': + host = optarg; + break; + case 'U': + user = optarg; + break; + case 'h': exit_usage(argv[0], 0); break; @@ -106,8 +153,26 @@ main(int argc, char **argv) if (optind < argc) exit_usage(argv[0], 1); - printf("SysDB client "SDB_CLIENT_VERSION_STRING + if (! host) + host = DEFAULT_SOCKET; + if (! user) + user = get_current_user(); + + client = sdb_client_create(host); + if (! client) { + sdb_log(SDB_LOG_ERR, "Failed to create client object"); + exit(1); + } + if (sdb_client_connect(client, user)) { + sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd"); + sdb_client_destroy(client); + exit(1); + } + + sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA"\n"); + + sdb_client_destroy(client); return 0; } /* main */