1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
* keydctl.c - A simple program to control a running keyd instance
*
* Copyright 2011 Jonathan McDowell <noodles@earth.li>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "keyd.h"
#include "onak-conf.h"
#include "version.h"
/* HACK: We need to stop onak-conf.o requiring this. */
void *DBINIT = NULL;
static int keyd_fd = -1;
static int verbose = 0;
static int keyd_do_command(enum keyd_ops cmd, void *buf, size_t len)
{
uint32_t tmp;
if (keyd_fd < 0) {
return -1;
}
tmp = cmd;
if (write(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
if (verbose >= 0) {
fprintf(stderr,
"Couldn't write keyd command %d: %s (%d)\n",
cmd, strerror(errno), errno);
}
exit(EXIT_FAILURE);
} else if (read(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
if (verbose >= 0) {
fprintf(stderr,
"Couldn't read keyd command %d reply: "
"%s (%d)\n",
cmd, strerror(errno), errno);
}
exit(EXIT_FAILURE);
} else if (tmp != KEYD_REPLY_OK) {
return -1;
} else if (buf == NULL) {
return 0;
} else if (read(keyd_fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
if (verbose >= 0) {
fprintf(stderr,
"Couldn't read keyd command %d reply length: "
"%s (%d)\n",
cmd, strerror(errno), errno);
}
exit(EXIT_FAILURE);
} else if (tmp > len) {
/* TODO: Read what we can into buf and skip the rest */
return -1;
} else {
return read(keyd_fd, buf, tmp);
}
}
static void keyd_connect(void)
{
struct sockaddr_un sock;
uint32_t reply = KEYD_REPLY_UNKNOWN_CMD;
keyd_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (keyd_fd < 0) {
if (verbose >= 0) {
fprintf(stderr,
"Couldn't open socket: %s (%d)\n",
strerror(errno),
errno);
}
exit(EXIT_FAILURE);
}
sock.sun_family = AF_UNIX;
snprintf(sock.sun_path, sizeof(sock.sun_path) - 1, "%s/%s",
config.db_dir,
KEYD_SOCKET);
if (connect(keyd_fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
if (verbose >= 0) {
fprintf(stderr,
"Couldn't connect to socket %s: %s (%d)\n",
sock.sun_path,
strerror(errno),
errno);
}
exit(EXIT_FAILURE);
}
keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply));
if (reply != keyd_version) {
if (verbose >= 0) {
fprintf(stderr, "Error! keyd protocol version "
"mismatch. (us = %d, it = %d)\n",
keyd_version, reply);
}
exit(EXIT_FAILURE);
}
return;
}
static void keyd_close(void)
{
uint32_t cmd = KEYD_CMD_CLOSE;
if (write(keyd_fd, &cmd, sizeof(cmd)) != sizeof(cmd) && verbose >= 0) {
fprintf(stderr, "Couldn't send close cmd: %s (%d)\n",
strerror(errno),
errno);
}
if (shutdown(keyd_fd, SHUT_RDWR) < 0 && verbose >= 0) {
fprintf(stderr, "Error shutting down socket: %d\n",
errno);
}
if (close(keyd_fd) < 0 && verbose >= 0) {
fprintf(stderr, "Error closing down socket: %d\n",
errno);
}
keyd_fd = -1;
return;
}
static void keyd_status(void)
{
uint32_t reply;
struct keyd_stats stats;
if (keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply)) == -1) {
printf("Got failure asking for keyd version.\n");
return;
}
printf("Using keyd protocol version %d.\n", reply);
if (keyd_do_command(KEYD_CMD_STATS, &stats, sizeof(stats)) == -1) {
printf("Got failure asking for keyd statistics.\n");
return;
}
printf("keyd running since %s", ctime(&stats.started));
printf("%d client connections received\n", stats.connects);
printf("Command statistics:\n");
printf(" Version: %d\n",
stats.command_stats[KEYD_CMD_VERSION]);
printf(" Get key by ID: %d\n",
stats.command_stats[KEYD_CMD_GET_ID]);
printf(" Get key by FP: %d\n",
stats.command_stats[KEYD_CMD_GET_FP]);
printf(" Store key: %d\n",
stats.command_stats[KEYD_CMD_STORE]);
printf(" Delete key: %d\n",
stats.command_stats[KEYD_CMD_DELETE]);
printf(" Search key: %d\n",
stats.command_stats[KEYD_CMD_GET_TEXT]);
printf(" Get full keyid: %d\n",
stats.command_stats[KEYD_CMD_GETFULLKEYID]);
printf(" Iterate all keys: %d\n",
stats.command_stats[KEYD_CMD_KEYITER]);
printf(" Close: %d\n",
stats.command_stats[KEYD_CMD_CLOSE]);
printf(" Quit: %d\n", stats.command_stats[KEYD_CMD_QUIT]);
printf(" Get statistics: %d\n",
stats.command_stats[KEYD_CMD_STATS]);
printf(" Unknown: %d\n",
stats.command_stats[KEYD_CMD_UNKNOWN]);
return;
}
static void usage(void)
{
puts("keydctl " ONAK_VERSION " - control an onak keyd instance.\n");
puts("Usage:\n");
puts("\tkeydctl [options] <command> <parameters>\n");
puts("\tCommands:\n");
puts("\tcheck - check if keyd is running");
puts("\tquit - request that keyd cleanly shuts down");
puts("\tstatus - display running keyd status");
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int optchar;
char *configfile = NULL;
while ((optchar = getopt(argc, argv, "c:h")) != -1 ) {
switch (optchar) {
case 'c':
configfile = strdup(optarg);
break;
case 'h':
default:
usage();
break;
}
}
readconfig(configfile);
free(configfile);
configfile = NULL;
if ((argc - optind) < 1) {
cleanupconfig();
usage();
} else if (!strcmp("check", argv[optind])) {
/* Just do the connect and close quietly */
verbose = -1;
keyd_connect();
keyd_close();
} else if (!strcmp("status", argv[optind])) {
keyd_connect();
keyd_status();
keyd_close();
} else if (!strcmp("quit", argv[optind])) {
keyd_connect();
keyd_do_command(KEYD_CMD_QUIT, NULL, 0);
keyd_close();
} else {
cleanupconfig();
usage();
}
cleanupconfig();
exit(EXIT_SUCCESS);
}
|