76e084a2108c541db054d3d914150d09c5b09bf1
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"
35 #if HAVE_LIBGEN_H
36 # include <libgen.h>
37 #else /* HAVE_LIBGEN_H */
38 # define basename(path) (path)
39 #endif /* ! HAVE_LIBGEN_H */
41 #include <sys/stat.h>
42 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include <unistd.h>
50 #include <sys/types.h>
52 #include <pwd.h>
54 #ifndef DEFAULT_SOCKET
55 # define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
56 #endif
58 static void
59 exit_usage(char *name, int status)
60 {
61 printf(
62 "Usage: %s <options>\n"
64 "\nOptions:\n"
65 " -h display this help and exit\n"
66 " -V display the version number and copyright\n"
68 "\nSysDB client "SDB_CLIENT_VERSION_STRING SDB_CLIENT_VERSION_EXTRA", "
69 PACKAGE_URL"\n", basename(name));
70 exit(status);
71 } /* exit_usage */
73 static void
74 exit_version(void)
75 {
76 printf("SysDB version "SDB_CLIENT_VERSION_STRING
77 SDB_CLIENT_VERSION_EXTRA", built "BUILD_DATE"\n"
78 "using libsysdbclient version %s%s\n"
79 "Copyright (C) 2012-2013 "PACKAGE_MAINTAINER"\n"
81 "\nThis is free software under the terms of the BSD license, see "
82 "the source for\ncopying conditions. There is NO WARRANTY; not "
83 "even for MERCHANTABILITY or\nFITNESS FOR A PARTICULAR "
84 "PURPOSE.\n", sdb_client_version_string(),
85 sdb_client_version_extra());
86 exit(0);
87 } /* exit_version */
89 static const char *
90 get_current_user(void)
91 {
92 struct passwd pw_entry;
93 struct passwd *result = NULL;
95 uid_t uid;
97 /* needs to be static because we return a pointer into this buffer
98 * to the caller */
99 static char buf[1024];
101 int status;
103 uid = geteuid();
105 memset(&pw_entry, 0, sizeof(pw_entry));
106 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
108 if (status || (! result)) {
109 fprintf(stderr, "Failed to determine current username\n");
110 return NULL;
111 }
112 return result->pw_name;
113 } /* get_current_user */
115 int
116 main(int argc, char **argv)
117 {
118 sdb_client_t *client;
120 const char *host = NULL;
121 const char *user = NULL;
123 while (42) {
124 int opt = getopt(argc, argv, "H:U:hV");
126 if (-1 == opt)
127 break;
129 switch (opt) {
130 case 'H':
131 host = optarg;
132 break;
133 case 'U':
134 user = optarg;
135 break;
137 case 'h':
138 exit_usage(argv[0], 0);
139 break;
140 case 'V':
141 exit_version();
142 break;
143 default:
144 exit_usage(argv[0], 1);
145 }
146 }
148 if (optind < argc)
149 exit_usage(argv[0], 1);
151 if (! host)
152 host = DEFAULT_SOCKET;
153 if (! user)
154 user = get_current_user();
156 client = sdb_client_create(host);
157 if (! client) {
158 fprintf(stderr, "Failed to create client object\n");
159 exit(1);
160 }
161 if (sdb_client_connect(client, user)) {
162 fprintf(stderr, "Failed to connect to SysDBd\n");
163 sdb_client_destroy(client);
164 exit(1);
165 }
167 printf("SysDB client "SDB_CLIENT_VERSION_STRING
168 SDB_CLIENT_VERSION_EXTRA"\n");
170 sdb_client_destroy(client);
171 return 0;
172 } /* main */
174 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */