From fc4e289617f3e40452ae661962e900eb96512ff1 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Mon, 18 Apr 2016 17:21:17 -0400 Subject: [PATCH] swtpm_bios: Add support for UnixIO socket Add support for UnixIO socker using --unixio command line option. Signed-off-by: Stefan Berger --- man/man8/swtpm_bios.8 | 11 +++++++++-- man/man8/swtpm_bios.pod | 9 +++++++++ src/swtpm_bios/tpm_bios.c | 41 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/man/man8/swtpm_bios.8 b/man/man8/swtpm_bios.8 index 1d5b185..c0ebefc 100644 --- a/man/man8/swtpm_bios.8 +++ b/man/man8/swtpm_bios.8 @@ -1,4 +1,4 @@ -.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) +.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.31) .\" .\" Standard preamble: .\" ======================================================================== @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "swtpm_bios 8" -.TH swtpm_bios 8 "2016-04-15" "swtpm" "" +.TH swtpm_bios 8 "2016-04-18" "swtpm" "" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -190,6 +190,13 @@ The following options are supported: .IX Item "--tpm-device " Use the given device rather than the default /dev/tpm0. This option overrides the \s-1TPM_DEVICE\s0 environment variable. +.IP "\fB\-\-tcp :\fR" 4 +.IX Item "--tcp :" +Connect to the given server and port; if no server is given, 127.0.0.1 is used; +if port is not given, the default port 6545 is used. +.IP "\fB\-\-unixio \fR" 4 +.IX Item "--unixio " +Connect to the given UnixIO path. .IP "\fB\-c\fR" 4 .IX Item "-c" Send TPM_Startup(\s-1ST_CLEAR\s0) (default). This instructs the \s-1TPM\s0 to start diff --git a/man/man8/swtpm_bios.pod b/man/man8/swtpm_bios.pod index 81afe41..69d4f6c 100644 --- a/man/man8/swtpm_bios.pod +++ b/man/man8/swtpm_bios.pod @@ -59,6 +59,15 @@ The following options are supported: Use the given device rather than the default /dev/tpm0. This option overrides the TPM_DEVICE environment variable. +=item B<--tcp EserverE:EportE> + +Connect to the given server and port; if no server is given, 127.0.0.1 is used; +if port is not given, the default port 6545 is used. + +=item B<--unixio EpathE> + +Connect to the given UnixIO path. + =item B<-c> Send TPM_Startup(ST_CLEAR) (default). This instructs the TPM to start diff --git a/src/swtpm_bios/tpm_bios.c b/src/swtpm_bios/tpm_bios.c index 50cf493..f4bd11e 100644 --- a/src/swtpm_bios/tpm_bios.c +++ b/src/swtpm_bios/tpm_bios.c @@ -69,6 +69,8 @@ static char *tpm_device; /* e.g., /dev/tpm0 */ static char *tcp_hostname; static int tcp_port = DEFAULT_TCP_PORT; +static char *unix_path; + static int parse_tcp_optarg(char *optarg, char **tcp_hostname, int *tcp_port) { char *pos = strchr(optarg, ':'); @@ -127,7 +129,7 @@ static int parse_tcp_optarg(char *optarg, char **tcp_hostname, int *tcp_port) } static int open_connection(char *devname, char *tcp_device_hostname, - int tcp_device_port) + int tcp_device_port, const char *unix_path) { int fd = -1; char *tcp_device_port_string = NULL; @@ -138,6 +140,32 @@ static int open_connection(char *devname, char *tcp_device_hostname, if (tcp_device_hostname) goto use_tcp; + if (unix_path) { + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd > 0) { + struct sockaddr_un addr; + + if (strlen(unix_path) + 1 > sizeof(addr.sun_path)) { + fprintf(stderr, "Socket path is too long.\n"); + return -1; + } + + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, unix_path); + + if (connect(fd, + (struct sockaddr*)&addr, sizeof(addr)) < 0) { + close(fd); + fd = -1; + } + } + + if (fd < 0) { + fprintf(stderr, "Could not connect using UnixIO socket.\n"); + } + return fd; + } + if (getenv("TCSD_USE_TCP_DEVICE")) { if ((tcp_device_hostname = getenv("TCSD_TCP_DEVICE_HOSTNAME")) == NULL) tcp_device_hostname = "localhost"; @@ -204,7 +232,7 @@ static int talk(const struct tpm_header *hdr, size_t count, int *tpm_errcode, }; fd_set rfds; - fd = open_connection(tpm_device, tcp_hostname, tcp_port); + fd = open_connection(tpm_device, tcp_hostname, tcp_port, unix_path); if (fd < 0) { goto err_exit; } @@ -374,6 +402,7 @@ static void print_usage(const char *prgname) "\t--tpm-device use the given device; default is /dev/tpm0\n" "\t--tcp []:[] connect to TPM on give host and port;\n" "\t default host is 127.0.0.1, default port is %u\n" +"\t--unix connect to TPM using UnixIO socket\n" "\t-c startup clear (default)\n" "\t-s startup state\n" "\t-d startup deactivate\n" @@ -403,6 +432,7 @@ int main(int argc, char *argv[]) static struct option long_options[] = { {"tpm-device", required_argument, NULL, 'D'}, {"tcp", required_argument, NULL, 'T'}, + {"unix", required_argument, NULL, 'U'}, {"c", no_argument, NULL, 'c'}, {"d", no_argument, NULL, 'd'}, {"h", no_argument, NULL, 'h'}, @@ -432,6 +462,13 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } break; + case 'U': + unix_path = strdup(optarg); + if (!unix_path) { + fprintf(stderr, "Out of memory.\n"); + return EXIT_FAILURE; + } + break; case 'c': startupparm = TPM_ST_CLEAR; do_more = 1; -- 2.39.5