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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
/* Copyright 2011-2014 Red Hat, Inc.
* All rights reserved.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* Author(s): Peter Jones <pjones@redhat.com>
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <popt.h>
#include <pwd.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include "pesign.h"
#define NO_FLAGS 0x00
#define UNLOCK_TOKEN 0x01
#define KILL_DAEMON 0x02
#define SIGN_BINARY 0x04
#define IS_TOKEN_UNLOCKED 0x08
#define FLAG_LIST_END 0x10
static struct {
int flag;
const char *name;
} flag_names[] = {
{UNLOCK_TOKEN, "unlock"},
{KILL_DAEMON, "kill"},
{SIGN_BINARY, "sign"},
{IS_TOKEN_UNLOCKED, "is-unlocked"},
{FLAG_LIST_END, NULL},
};
static void
print_flag_name(FILE *f, int flag)
{
for (int i = 0; flag_names[i].flag != FLAG_LIST_END; i++) {
if (flag_names[i].flag == flag)
fprintf(f, "%s ", flag_names[i].name);
}
}
static int
connect_to_server(void)
{
int rc = access(SOCKPATH, R_OK);
if (rc != 0) {
fprintf(stderr, "pesign-client: could not connect to server: "
"%m\n");
exit(1);
}
struct sockaddr_un addr_un = {
.sun_family = AF_UNIX,
.sun_path = SOCKPATH,
};
int sd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sd < 0) {
fprintf(stderr, "pesign-client: could not open socket: %m\n");
exit(1);
}
socklen_t len = strlen(addr_un.sun_path) +
sizeof(addr_un.sun_family);
rc = connect(sd, (struct sockaddr *)&addr_un, len);
if (rc < 0) {
fprintf(stderr, "pesign-client: could not connect to daemon: "
"%m\n");
exit(1);
}
return sd;
}
static int32_t
check_response(int sd, char **srvmsg);
static void
check_cmd_version(int sd, uint32_t command, char *name, int32_t version)
{
struct msghdr msg;
struct iovec iov[1];
pesignd_msghdr pm;
pm.version = PESIGND_VERSION;
pm.command = CMD_GET_CMD_VERSION;
pm.size = sizeof(command);
iov[0].iov_base = ±
iov[0].iov_len = sizeof(pm);
memset(&msg, '\0', sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
ssize_t n;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "check-cmd-version: kill daemon failed: %m\n");
exit(1);
}
iov[0].iov_base = &command;
iov[0].iov_len = sizeof(command);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
n = sendmsg(sd, &msg, 0);
if (n < 0)
err(1, "check-cmd-version: sendmsg failed");
char *srvmsg = NULL;
int32_t rc = check_response(sd, &srvmsg);
if (rc < 0)
errx(1, "command \"%s\" not known by server", name);
if (rc != version)
errx(1, "command \"%s\": client version %d, server version %d",
name, version, rc);
}
static void
send_kill_daemon(int sd)
{
struct msghdr msg;
struct iovec iov;
pesignd_msghdr pm;
check_cmd_version(sd, CMD_KILL_DAEMON, "kill-daemon", 0);
pm.version = PESIGND_VERSION;
pm.command = CMD_KILL_DAEMON;
pm.size = 0;
iov.iov_base = ±
iov.iov_len = sizeof(pm);
memset(&msg, '\0', sizeof(msg));
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
ssize_t n;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "pesign-client: kill daemon failed: %m\n");
exit(1);
}
}
static int32_t
check_response(int sd, char **srvmsg)
{
ssize_t n;
struct msghdr msg;
struct iovec iov;
char buffer[1024];
pesignd_msghdr *pm;
msg.msg_name = NULL;
msg.msg_namelen = 0;
memset(&msg, '\0', sizeof(msg));
memset(buffer, '\0', sizeof(buffer));
iov.iov_base = buffer;
iov.iov_len = 1023;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
n = recvmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "pesign-client: could not get response from "
"server: %m\n");
exit(1);
}
pm = (pesignd_msghdr *)buffer;
if (pm->version != PESIGND_VERSION) {
fprintf(stderr, "pesign-client: got version %d, "
"expected version %d\n", pm->version, PESIGND_VERSION);
exit(1);
}
if (pm->command != CMD_RESPONSE) {
fprintf(stderr, "pesign-client: got unexpected response: %d\n",
pm->command);
exit(1);
}
pesignd_cmd_response *resp = (pesignd_cmd_response *)((uint8_t *)pm +
offsetof(pesignd_msghdr, size) +
sizeof(pm->size));
if (resp->rc == 0)
return 0;
*srvmsg = strdup((char *)resp->errmsg);
return resp->rc;
}
static char *
get_token_pin(int pinfd, char *pinfile, char *envname)
{
char *pin = NULL;
FILE *pinf = NULL;
errno = 0;
/* validate that the fd we got is real... */
if (pinfd >= 0) {
pinf = fdopen(pinfd, "r");
if (!pinf) {
if (errno != EBADF)
close(pinfd);
return NULL;
}
ssize_t n = getline(&pin, 0, pinf);
if (n < 0 || !pin) {
fclose(pinf);
close(pinfd);
return NULL;
}
char *c = strchrnul(pin, '\n');
*c = '\0';
fclose(pinf);
close(pinfd);
return pin;
} else if (pinfile) {
pinf = fopen(pinfile, "r");
if (!pinf)
return NULL;
size_t len;
ssize_t n = getline(&pin, &len, pinf);
if (n < 0 || !pin) {
fclose(pinf);
return NULL;
}
char *c = strchrnul(pin, '\n');
*c = '\0';
fclose(pinf);
return pin;
} else {
pin = getenv(envname);
if (pin)
return strdup(pin);
}
pin = readpw(NULL, PR_FALSE, NULL);
return pin;
}
static void
unlock_token(int sd, char *tokenname, char *pin)
{
struct msghdr msg;
struct iovec iov[2];
pesignd_msghdr pm;
uint32_t size0 = pesignd_string_size(tokenname);
uint32_t size1 = pesignd_string_size(pin);
check_cmd_version(sd, CMD_UNLOCK_TOKEN, "unlock-token", 0);
pm.version = PESIGND_VERSION;
pm.command = CMD_UNLOCK_TOKEN;
pm.size = size0 + size1;
iov[0].iov_base = ±
iov[0].iov_len = sizeof (pm);
memset(&msg, '\0', sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
ssize_t n;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "pesign-client: unlock token: sendmsg failed: "
"%m\n");
exit(1);
}
uint8_t *buffer = NULL;
buffer = calloc(1, size0 + size1);
if (!buffer) {
fprintf(stderr, "pesign-client: could not allocate memory: "
"%m\n");
exit(1);
}
pesignd_string *tn = (pesignd_string *)buffer;
pesignd_string_set(tn, tokenname);
iov[0].iov_base = tn;
iov[0].iov_len = size0;
pesignd_string *tp = pesignd_string_next(tn);
pesignd_string_set(tp, pin);
iov[1].iov_base = tp;
iov[1].iov_len = size1;
msg.msg_iov = iov;
msg.msg_iovlen = 2;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "pesign-client: unlock token: sendmsg failed: "
"%m\n");
exit(1);
}
char *srvmsg = NULL;
int rc = check_response(sd, &srvmsg);
if (rc < 0) {
fprintf(stderr, "pesign-client: %s\n",
srvmsg);
exit(1);
}
free(buffer);
}
static void
is_token_unlocked(int sd, char *tokenname)
{
struct msghdr msg;
struct iovec iov[1];
pesignd_msghdr pm;
uint32_t size0 = pesignd_string_size(tokenname);
check_cmd_version(sd, CMD_IS_TOKEN_UNLOCKED, "is-token-unlocked", 0);
pm.version = PESIGND_VERSION;
pm.command = CMD_IS_TOKEN_UNLOCKED;
pm.size = size0;
iov[0].iov_base = ±
iov[0].iov_len = sizeof (pm);
memset(&msg, '\0', sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
ssize_t n;
n = sendmsg(sd, &msg, 0);
if (n < 0)
err(1, "is_token_unlocked: sendmsg failed");
uint8_t *buffer = NULL;
buffer = calloc(1, size0);
if (!buffer)
err(1, "is_token_unlocked: Could not allocate memory");
pesignd_string *tn = (pesignd_string *)buffer;
pesignd_string_set(tn, tokenname);
iov[0].iov_base = tn;
iov[0].iov_len = size0;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
n = sendmsg(sd, &msg, 0);
if (n < 0)
err(1, "is_token_unlocked: sendmsg failed");
char *srvmsg = NULL;
int rc = check_response(sd, &srvmsg);
if (rc < 0)
errx(1, "%s", srvmsg);
printf("token \"%s\" is %slocked\n", tokenname, rc == 1 ? "" : "un");
free(buffer);
}
static void
send_fd(int sd, int fd)
{
struct msghdr msg;
struct iovec iov;
char buf[2] = "\0";
memset(&msg, '\0', sizeof(msg));
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
size_t controllen = CMSG_SPACE(sizeof(int));
struct cmsghdr *cm = malloc(controllen);
if (!cm) {
fprintf(stderr, "pesign-client: could not allocate memory: "
"%m\n");
exit(1);
}
msg.msg_control = cm;
msg.msg_controllen = controllen;
struct cmsghdr *cme;
cme = CMSG_FIRSTHDR(&msg);
cme->cmsg_len = CMSG_LEN(sizeof(int));
cme->cmsg_level = SOL_SOCKET;
cme->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cme) = fd;
ssize_t n;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "pesign-client: sign: sendmsg failed: "
"%m\n");
exit(1);
}
}
static void
sign(int sd, char *infile, char *outfile, char *tokenname, char *certname,
int attached)
{
int infd = open(infile, O_RDONLY);
if (infd < 0) {
fprintf(stderr, "pesign-client: could not open input file "
"\"%s\": %m\n", infile);
exit(1);
}
int outfd = open(outfile, O_RDWR|O_CREAT, 0600);
if (outfd < 0) {
fprintf(stderr, "pesign-client: could not open output file "
"\"%s\": %m\n", outfile);
exit(1);
}
struct msghdr msg;
struct iovec iov[2];
uint32_t size0 = pesignd_string_size(tokenname);
uint32_t size1 = pesignd_string_size(certname);
pesignd_msghdr *pm;
pm = calloc(1, sizeof(*pm));
if (!pm) {
oom:
fprintf(stderr, "pesign-client: could not allocate memory: "
"%m\n");
exit(1);
}
check_cmd_version(sd, attached ? CMD_SIGN_ATTACHED : CMD_SIGN_DETACHED,
attached ? "sign-attached" : "sign-detached", 0);
pm->version = PESIGND_VERSION;
pm->command = attached ? CMD_SIGN_ATTACHED : CMD_SIGN_DETACHED;
pm->size = size0 + size1;
iov[0].iov_base = pm;
iov[0].iov_len = sizeof (*pm);
memset(&msg, '\0', sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;
ssize_t n;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "pesign-client: sign: sendmsg failed: "
"%m\n");
exit(1);
}
char *buffer;
buffer = malloc(size0 + size1);
if (!buffer)
goto oom;
pesignd_string *tn = (pesignd_string *)buffer;
pesignd_string_set(tn, tokenname);
iov[0].iov_base = tn;
iov[0].iov_len = size0;
pesignd_string *cn = pesignd_string_next(tn);
pesignd_string_set(cn, certname);
iov[1].iov_base = cn;
iov[1].iov_len = size1;
msg.msg_iov = iov;
msg.msg_iovlen = 2;
n = sendmsg(sd, &msg, 0);
if (n < 0) {
fprintf(stderr, "pesign-client: sign: sendmsg failed: "
"%m\n");
exit(1);
}
free(buffer);
send_fd(sd, infd);
send_fd(sd, outfd);
char *srvmsg = NULL;
int rc = check_response(sd, &srvmsg);
if (rc < 0) {
fprintf(stderr, "pesign-client: signing failed: \"%s\"\n",
srvmsg);
exit(1);
}
close(infd);
close(outfd);
return;
}
int
main(int argc, char *argv[])
{
char *tokenname = "NSS Certificate DB";
char *certname = NULL;
poptContext optCon;
int rc;
int action = NO_FLAGS;
char *infile = NULL;
char *outfile = NULL;
char *exportfile = NULL;
int attached = 1;
int pinfd = -1;
char *pinfile = NULL;
char *tokenpin = NULL;
struct poptOption options[] = {
{.argInfo = POPT_ARG_INTL_DOMAIN,
.arg = "pesign" },
{.longName = "token",
.shortName = 't',
.argInfo = POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT,
.arg = &tokenname,
.descrip = "NSS token holding signing key",
.argDescrip = "<token>" },
{.longName = "certificate",
.shortName = 'c',
.argInfo = POPT_ARG_STRING,
.arg = &certname,
.descrip = "NSS certificate name",
.argDescrip = "<nickname>" },
{.longName = "unlock",
.shortName = 'u',
.argInfo = POPT_ARG_VAL|POPT_ARGFLAG_OR,
.arg = &action,
.val = UNLOCK_TOKEN,
.descrip = "unlock nss token" },
{.longName = "is-unlocked",
.shortName = 'q',
.argInfo = POPT_ARG_VAL|POPT_ARGFLAG_OR,
.arg = &action,
.val = IS_TOKEN_UNLOCKED,
.descrip = "query if an nss token is unlocked" },
{.longName = "kill",
.shortName = 'k',
.argInfo = POPT_ARG_VAL|POPT_ARGFLAG_OR,
.arg = &action,
.val = KILL_DAEMON,
.descrip = "kill running daemon" },
{.longName = "sign",
.shortName = 's',
.argInfo = POPT_ARG_VAL|POPT_ARGFLAG_OR,
.arg = &action,
.val = SIGN_BINARY,
.descrip = "sign binary" },
{.longName = "infile",
.shortName = 'i',
.argInfo = POPT_ARG_STRING,
.arg = &infile,
.descrip = "input filename",
.argDescrip = "<infile>" },
{.longName = "outfile",
.shortName = 'o',
.argInfo = POPT_ARG_STRING,
.arg = &outfile,
.descrip = "output filename",
.argDescrip = "<outfile>" },
{.longName = "export",
.shortName = 'e',
.argInfo = POPT_ARG_STRING,
.arg = &exportfile,
.descrip = "create detached signature",
.argDescrip = "<outfile>" },
{.longName = "pinfd",
.shortName = 'f',
.argInfo = POPT_ARG_INT,
.arg = &pinfd,
.val = -1,
.descrip = "read file descriptor for pin information",
.argDescrip = "<file descriptor>" },
{.longName = "pinfile",
.shortName = 'F',
.argInfo = POPT_ARG_STRING,
.arg = &pinfile,
.descrip = "read named file for pin information",
.argDescrip = "<pin file name>" },
POPT_AUTOALIAS
POPT_AUTOHELP
POPT_TABLEEND
};
optCon = poptGetContext("pesign", argc, (const char **)argv, options,0);
rc = poptReadDefaultConfig(optCon, 0);
if (rc < 0 && !(rc == POPT_ERROR_ERRNO && errno == ENOENT)) {
fprintf(stderr,
"pesign-client: poptReadDefaultConfig failed: %s\n",
poptStrerror(rc));
exit(1);
}
while ((rc = poptGetNextOpt(optCon)) > 0)
;
if (rc < -1) {
fprintf(stderr, "pesign-client: Invalid argument: %s: %s\n",
poptBadOption(optCon, 0), poptStrerror(rc));
exit(1);
}
if (poptPeekArg(optCon)) {
fprintf(stderr, "pesign-client: Invalid Argument: \"%s\"\n",
poptPeekArg(optCon));
exit(1);
}
if (action == NO_FLAGS) {
poptPrintUsage(optCon, stdout, 0);
poptFreeContext(optCon);
exit(0);
}
if (action & SIGN_BINARY && (!outfile && !exportfile)) {
fprintf(stderr, "pesign-client: neither --outfile nor --export "
"specified\n");
exit(1);
}
if (outfile && exportfile) {
fprintf(stderr, "pesign-client: both --outfile and --export "
"specified\n");
exit(1);
}
if (exportfile) {
outfile = exportfile;
attached = 0;
}
poptFreeContext(optCon);
int sd = -1;
switch (action) {
case UNLOCK_TOKEN:
tokenpin = get_token_pin(pinfd, pinfile, "PESIGN_TOKEN_PIN");
if (tokenpin == NULL) {
if (errno)
fprintf(stderr, "pesign-client: could not "
"get token pin: %m\n");
else
fprintf(stderr, "pesign-client: no token pin "
"specified");
exit(1);
}
sd = connect_to_server();
unlock_token(sd, tokenname, tokenpin);
free(tokenpin);
break;
case IS_TOKEN_UNLOCKED:
sd = connect_to_server();
is_token_unlocked(sd, tokenname);
break;
case KILL_DAEMON:
sd = connect_to_server();
send_kill_daemon(sd);
break;
case SIGN_BINARY:
if (!infile) {
fprintf(stderr, "pesign-client: no input file "
"specified\n");
exit(1);
}
if (!outfile) {
fprintf(stderr, "pesign-client: no output file "
"specified\n");
exit(1);
}
if (!certname) {
fprintf(stderr, "pesign-client: no certificate name "
"spefified\n");
exit(1);
}
sd = connect_to_server();
sign(sd, infile, outfile, tokenname, certname, attached);
break;
default:
fprintf(stderr, "Incompatible flags (0x%08x): ", action);
for (int i = 1; i < FLAG_LIST_END; i <<= 1) {
if (action & i)
print_flag_name(stderr, i);
}
fprintf(stderr, "\n");
exit(1);
}
return 0;
}
|