2 * keydctl.c - A simple program to control a running keyd instance
4 * Copyright 2011 Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <https://www.gnu.org/licenses/>.
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include "onak-conf.h"
33 /* HACK: We need to stop onak-conf.o requiring this. */
36 static int keyd_fd = -1;
37 static int verbose = 0;
39 static int keyd_do_command(enum keyd_ops cmd, void *buf, size_t len)
48 if (write(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
51 "Couldn't write keyd command %d: %s (%d)\n",
52 cmd, strerror(errno), errno);
55 } else if (read(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
58 "Couldn't read keyd command %d reply: "
60 cmd, strerror(errno), errno);
63 } else if (tmp != KEYD_REPLY_OK) {
65 } else if (buf == NULL) {
67 } else if (read(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
70 "Couldn't read keyd command %d reply length: "
72 cmd, strerror(errno), errno);
75 } else if (tmp > len) {
76 /* TODO: Read what we can into buf and skip the rest */
79 return read(keyd_fd, buf, tmp);
83 static void keyd_connect(void)
85 struct sockaddr_un sock;
86 uint32_t reply = KEYD_REPLY_UNKNOWN_CMD;
88 keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
92 "Couldn't open socket: %s (%d)\n",
99 sock.sun_family = AF_UNIX;
100 snprintf(sock.sun_path, sizeof(sock.sun_path) - 1, "%s/%s",
103 if (connect(keyd_fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
106 "Couldn't connect to socket %s: %s (%d)\n",
114 keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply));
115 if (reply != keyd_version) {
117 fprintf(stderr, "Error! keyd protocol version "
118 "mismatch. (us = %d, it = %d)\n",
119 keyd_version, reply);
127 static void keyd_close(void)
129 uint32_t cmd = KEYD_CMD_CLOSE;
131 if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd) && verbose >= 0) {
132 fprintf(stderr, "Couldn't send close cmd: %s (%d)\n",
137 if (shutdown(keyd_fd, SHUT_RDWR) < 0 && verbose >= 0) {
138 fprintf(stderr, "Error shutting down socket: %d\n",
141 if (close(keyd_fd) < 0 && verbose >= 0) {
142 fprintf(stderr, "Error closing down socket: %d\n",
151 static void keyd_status(void)
154 struct keyd_stats stats;
156 if (keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply)) == -1) {
157 printf("Got failure asking for keyd version.\n");
160 printf("Using keyd protocol version %d.\n", reply);
162 if (keyd_do_command(KEYD_CMD_STATS, &stats, sizeof(stats)) == -1) {
163 printf("Got failure asking for keyd statistics.\n");
167 printf("keyd running since %s", ctime(&stats.started));
168 printf("%d client connections received\n", stats.connects);
170 printf("Command statistics:\n");
171 printf(" Version: %d\n",
172 stats.command_stats[KEYD_CMD_VERSION]);
173 printf(" Get key by ID: %d\n",
174 stats.command_stats[KEYD_CMD_GET_ID]);
175 printf(" Get key by FP: %d\n",
176 stats.command_stats[KEYD_CMD_GET_FP]);
177 printf(" Get key by hash: %d\n",
178 stats.command_stats[KEYD_CMD_GET_SKSHASH]);
179 printf(" Store key: %d\n",
180 stats.command_stats[KEYD_CMD_STORE]);
181 printf(" Delete key: %d\n",
182 stats.command_stats[KEYD_CMD_DELETE]);
183 printf(" Update key: %d\n",
184 stats.command_stats[KEYD_CMD_UPDATE]);
185 printf(" Search key: %d\n",
186 stats.command_stats[KEYD_CMD_GET_TEXT]);
187 printf(" Get full keyid: %d\n",
188 stats.command_stats[KEYD_CMD_GETFULLKEYID]);
189 printf(" Iterate all keys: %d\n",
190 stats.command_stats[KEYD_CMD_KEYITER]);
191 printf(" Close: %d\n",
192 stats.command_stats[KEYD_CMD_CLOSE]);
193 printf(" Quit: %d\n", stats.command_stats[KEYD_CMD_QUIT]);
194 printf(" Get statistics: %d\n",
195 stats.command_stats[KEYD_CMD_STATS]);
196 printf(" Unknown: %d\n",
197 stats.command_stats[KEYD_CMD_UNKNOWN]);
202 static void usage(void)
204 puts("keydctl " ONAK_VERSION " - control an onak keyd instance.\n");
206 puts("\tkeydctl [options] <command> <parameters>\n");
207 puts("\tCommands:\n");
208 puts("\tcheck - check if keyd is running");
209 puts("\tquit - request that keyd cleanly shuts down");
210 puts("\tstatus - display running keyd status");
214 int main(int argc, char *argv[])
217 char *configfile = NULL;
219 while ((optchar = getopt(argc, argv, "c:h")) != -1 ) {
222 configfile = strdup(optarg);
231 readconfig(configfile);
235 if ((argc - optind) < 1) {
238 } else if (!strcmp("check", argv[optind])) {
239 /* Just do the connect and close quietly */
243 } else if (!strcmp("status", argv[optind])) {
247 } else if (!strcmp("quit", argv[optind])) {
249 keyd_do_command(KEYD_CMD_QUIT, NULL, 0);