1 /*
2 * SysDB - src/client/sysdb.c
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "client/sysdb.h"
33 #include "client/sock.h"
34 #include "utils/error.h"
36 #include <errno.h>
38 #if HAVE_LIBGEN_H
39 # include <libgen.h>
40 #else /* HAVE_LIBGEN_H */
41 # define basename(path) (path)
42 #endif /* ! HAVE_LIBGEN_H */
44 #include <sys/stat.h>
45 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include <unistd.h>
53 #include <sys/types.h>
55 #include <pwd.h>
57 #ifndef DEFAULT_SOCKET
58 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
59 #endif
61 static void
62 exit_usage(char *name, int status)
63 {
64 printf(
65 "Usage: %s <options>\n"
67 "\nOptions:\n"
68 " -h display this help and exit\n"
69 " -V display the version number and copyright\n"
71 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
72 PACKAGE_URL"\n", basename(name));
73 exit(status);
74 } /* exit_usage */
76 static void
77 exit_version(void)
78 {
79 printf("SysDB version "SDB_CLIENT_VERSION_STRING
80 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
81 "using libsysdbclient version %s%s\n"
82 "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n"
84 "\nThis is free software under the terms of the BSD license, see "
85 "the source for\ncopying conditions. There is NO WARRANTY; not "
86 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
87 "PURPOSE.\n", sdb_client_version_string(),
88 sdb_client_version_extra());
89 exit(0);
90 } /* exit_version */
92 static const char *
93 get_current_user(void)
94 {
95 struct passwd pw_entry;
96 struct passwd *result = NULL;
98 uid_t uid;
100 /* needs to be static because we return a pointer into this buffer
101 * to the caller */
102 static char buf[1024];
104 int status;
106 uid = geteuid();
108 memset(&pw_entry, 0, sizeof(pw_entry));
109 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
111 if (status || (! result)) {
112 char errbuf[1024];
113 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
114 sdb_strerror(errno, errbuf, sizeof(errbuf)));
115 return NULL;
116 }
117 return result->pw_name;
118 } /* get_current_user */
120 int
121 main(int argc, char **argv)
122 {
123 sdb_client_t *client;
125 const char *host = NULL;
126 const char *user = NULL;
128 while (42) {
129 int opt = getopt(argc, argv, "H:U:hV");
131 if (-1 == opt)
132 break;
134 switch (opt) {
135 case 'H':
136 host = optarg;
137 break;
138 case 'U':
139 user = optarg;
140 break;
142 case 'h':
143 exit_usage(argv[0], 0);
144 break;
145 case 'V':
146 exit_version();
147 break;
148 default:
149 exit_usage(argv[0], 1);
150 }
151 }
153 if (optind < argc)
154 exit_usage(argv[0], 1);
156 if (! host)
157 host = DEFAULT_SOCKET;
158 if (! user)
159 user = get_current_user();
161 client = sdb_client_create(host);
162 if (! client) {
163 sdb_log(SDB_LOG_ERR, "Failed to create client object");
164 exit(1);
165 }
166 if (sdb_client_connect(client, user)) {
167 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDBd");
168 sdb_client_destroy(client);
169 exit(1);
170 }
172 sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
173 SDB_CLIENT_VERSION_EXTRA"\n");
175 sdb_client_destroy(client);
176 return 0;
177 } /* main */
179 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */