swtpm: Use logprintf wherever possible

Use logprintf wherever possible. While the logging is not set up,
print error messages to stderr and prefix them with 'swtpm: '. After
the logging has been set up all error messages go into the log file.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
This commit is contained in:
Stefan Berger 2017-03-31 08:41:08 -04:00
parent 93f4a38940
commit cba815690c
9 changed files with 244 additions and 200 deletions

View File

@ -193,8 +193,8 @@ handle_log_options(char *options)
ovs = options_parse(options, logging_opt_desc, &error);
if (!ovs) {
fprintf(stderr, "Error parsing logging options: %s\n",
error);
logprintf(STDERR_FILENO, "Error parsing logging options: %s\n",
error);
return -1;
}
logfile = option_get_string(ovs, "file", NULL);
@ -202,27 +202,27 @@ handle_log_options(char *options)
loglevel = option_get_uint(ovs, "level", 0);
logprefix = option_get_string(ovs, "prefix", NULL);
if (logfile && (log_init(logfile) < 0)) {
fprintf(stderr,
"Could not open logfile for writing: %s\n",
strerror(errno));
logprintf(STDERR_FILENO,
"Could not open logfile for writing: %s\n",
strerror(errno));
goto error;
} else if (logfd >= 0 && (log_init_fd(logfd) < 0)) {
fprintf(stderr,
"Could not access logfile using fd %d: %s\n",
logfd, strerror(errno));
logprintf(STDERR_FILENO,
"Could not access logfile using fd %d: %s\n",
logfd, strerror(errno));
goto error;
}
if ((logfile || logfd) && !loglevel)
loglevel = 1;
if (log_set_prefix(logprefix) < 0) {
fprintf(stderr,
"Could not set logging prefix. Out of memory?\n");
logprintf(STDERR_FILENO,
"Could not set logging prefix. Out of memory?\n");
goto error;
}
if (log_set_level(loglevel) < 0) {
fprintf(stderr,
"Could not set log level. Out of memory?");
logprintf(STDERR_FILENO,
"Could not set log level. Out of memory?");
goto error;
}
@ -262,15 +262,15 @@ parse_key_options(char *options, unsigned char *key, size_t maxkeylen,
ovs = options_parse(options, key_opt_desc, &error);
if (!ovs) {
fprintf(stderr, "Error parsing key options: %s\n",
error);
logprintf(STDERR_FILENO, "Error parsing key options: %s\n",
error);
goto error;
}
keyfile = option_get_string(ovs, "file", NULL);
pwdfile = option_get_string(ovs, "pwdfile", NULL);
if (!keyfile && !pwdfile) {
fprintf(stderr, "Either --key or --pwdfile is required\n");
logprintf(STDERR_FILENO, "Either --key or --pwdfile is required\n");
goto error;
}
@ -387,20 +387,21 @@ parse_pid_options(char *options, char **pidfile)
ovs = options_parse(options, pid_opt_desc, &error);
if (!ovs) {
fprintf(stderr, "Error parsing pid options: %s\n",
logprintf(STDERR_FILENO, "Error parsing pid options: %s\n",
error);
goto error;
}
filename = option_get_string(ovs, "file", NULL);
if (!filename) {
fprintf(stderr, "The file parameter is required for the pid option.\n");
logprintf(STDERR_FILENO,
"The file parameter is required for the pid option.\n");
goto error;
}
*pidfile = strdup(filename);
if (!*pidfile) {
fprintf(stderr, "Out of memory.");
logprintf(STDERR_FILENO, "Out of memory.");
goto error;
}
@ -460,21 +461,21 @@ parse_tpmstate_options(char *options, char **tpmstatedir)
ovs = options_parse(options, tpmstate_opt_desc, &error);
if (!ovs) {
fprintf(stderr, "Error parsing tpmstate options: %s\n",
error);
logprintf(STDERR_FILENO, "Error parsing tpmstate options: %s\n",
error);
goto error;
}
directory = option_get_string(ovs, "dir", NULL);
if (!directory) {
fprintf(stderr,
"The file parameter is required for the tpmstate option.\n");
logprintf(STDERR_FILENO,
"The file parameter is required for the tpmstate option.\n");
goto error;
}
*tpmstatedir = strdup(directory);
if (!*tpmstatedir) {
fprintf(stderr, "Out of memory.");
logprintf(STDERR_FILENO, "Out of memory.");
goto error;
}
@ -531,11 +532,11 @@ static int unixio_open_socket(const char *path, mode_t perm)
len = sizeof(su.sun_path);
n = snprintf(su.sun_path, len, "%s", path);
if (n < 0) {
fprintf(stderr, "Could not nsprintf path to UnixIO socket\n");
logprintf(STDERR_FILENO, "Could not nsprintf path to UnixIO socket\n");
return -1;
}
if (n >= (int)len) {
fprintf(stderr, "Path for UnioIO socket is too long\n");
logprintf(STDERR_FILENO, "Path for UnioIO socket is too long\n");
return -1;
}
@ -543,29 +544,29 @@ static int unixio_open_socket(const char *path, mode_t perm)
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr, "Could not open UnixIO socket\n");
logprintf(STDERR_FILENO, "Could not open UnixIO socket\n");
return -1;
}
len = strlen(su.sun_path) + sizeof(su.sun_family);
n = bind(fd, (struct sockaddr *)&su, len);
if (n < 0) {
fprintf(stderr, "Could not open UnixIO socket: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Could not open UnixIO socket: %s\n",
strerror(errno));
goto error;
}
if (chmod(su.sun_path, perm) < 0) {
fprintf(stderr,
"Could not change permssions on UnixIO socket: %s\n",
strerror(errno));
logprintf(STDERR_FILENO,
"Could not change permssions on UnixIO socket: %s\n",
strerror(errno));
goto error;
}
n = listen(fd, 1);
if (n < 0) {
fprintf(stderr, "Cannot listen on UnixIO socket: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Cannot listen on UnixIO socket: %s\n",
strerror(errno));
goto error;
}
@ -617,23 +618,24 @@ static int tcp_open_socket(unsigned short port, const char *bindaddr,
n = inet_pton(af, bindaddr, dst);
if (n <= 0) {
fprintf(stderr, "Could not parse the bind address '%s'\n",
bindaddr);
logprintf(STDERR_FILENO, "Could not parse the bind address '%s'\n",
bindaddr);
return -1;
}
if (af == AF_INET6) {
if (IN6_IS_ADDR_LINKLOCAL(&si6.sin6_addr)) {
if (!ifname) {
fprintf(stderr,
"Missing interface name for link local address\n");
logprintf(STDERR_FILENO,
"Missing interface name for link local address\n");
return -1;
}
n = if_nametoindex(ifname);
if (!n) {
fprintf(stderr,
"Could not convert interface name '%s' to index: %s\n",
ifname, strerror(errno));
logprintf(STDERR_FILENO,
"Could not convert interface name '%s' to "
"index: %s\n",
ifname, strerror(errno));
return -1;
}
si6.sin6_scope_id = n;
@ -642,29 +644,30 @@ static int tcp_open_socket(unsigned short port, const char *bindaddr,
fd = socket(af, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr, "Could not open TCP socket\n");
logprintf(STDERR_FILENO, "Could not open TCP socket\n");
return -1;
}
opt = 1;
n = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (n < 0) {
fprintf(stderr, "Could not set socket option SO_REUSEADDR: %s\n",
strerror(errno));
logprintf(STDERR_FILENO,
"Could not set socket option SO_REUSEADDR: %s\n",
strerror(errno));
goto error;
}
n = bind(fd, sa, sa_len);
if (n < 0) {
fprintf(stderr, "Could not open TCP socket: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Could not open TCP socket: %s\n",
strerror(errno));
goto error;
}
n = listen(fd, 1);
if (n < 0) {
fprintf(stderr, "Cannot listen on TCP socket: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Cannot listen on TCP socket: %s\n",
strerror(errno));
goto error;
}
@ -694,13 +697,14 @@ static int parse_ctrlchannel_options(char *options, struct ctrlchannel **cc)
ovs = options_parse(options, ctrl_opt_desc, &error);
if (!ovs) {
fprintf(stderr, "Error parsing ctrl options: %s\n", error);
logprintf(STDERR_FILENO, "Error parsing ctrl options: %s\n", error);
goto error;
}
type = option_get_string(ovs, "type", NULL);
if (!type) {
fprintf(stderr, "Missing type parameter for control channel\n");
logprintf(STDERR_FILENO,
"Missing type parameter for control channel\n");
goto error;
}
@ -710,18 +714,18 @@ static int parse_ctrlchannel_options(char *options, struct ctrlchannel **cc)
clientfd = option_get_int(ovs, "clientfd", -1);
if (fd >= 0) {
if (fstat(fd, &stat) < 0 || !S_ISSOCK(stat.st_mode)) {
fprintf(stderr,
"Bad filedescriptor %d for UnixIO control channel\n",
fd);
logprintf(STDERR_FILENO,
"Bad filedescriptor %d for UnixIO control channel\n",
fd);
goto error;
}
*cc = ctrlchannel_new(fd, false);
} else if (clientfd >= 0) {
if (fstat(clientfd, &stat) < 0 || !S_ISSOCK(stat.st_mode)) {
fprintf(stderr,
"Bad filedescriptor %d for UnixIO client control"
" channel\n", clientfd);
logprintf(STDERR_FILENO,
"Bad filedescriptor %d for UnixIO client control"
" channel\n", clientfd);
goto error;
}
@ -733,8 +737,9 @@ static int parse_ctrlchannel_options(char *options, struct ctrlchannel **cc)
*cc = ctrlchannel_new(fd, false);
} else {
fprintf(stderr,
"Missing path and fd options for UnixIO control channel\n");
logprintf(STDERR_FILENO,
"Missing path and fd options for UnixIO "
"control channel\n");
goto error;
}
} else if (!strcmp(type, "tcp")) {
@ -742,16 +747,16 @@ static int parse_ctrlchannel_options(char *options, struct ctrlchannel **cc)
fd = option_get_int(ovs, "fd", -1);
if (fd >= 0) {
if (fstat(fd, &stat) < 0 || !S_ISSOCK(stat.st_mode)) {
fprintf(stderr,
"Bad filedescriptor %d for TCP control channel\n", fd);
logprintf(STDERR_FILENO,
"Bad filedescriptor %d for TCP control channel\n", fd);
goto error;
}
*cc = ctrlchannel_new(fd, false);
} else if (port >= 0) {
if (port >= 0x10000) {
fprintf(stderr,
"TCP control channel port outside valid range\n");
logprintf(STDERR_FILENO,
"TCP control channel port outside valid range\n");
goto error;
}
@ -764,12 +769,12 @@ static int parse_ctrlchannel_options(char *options, struct ctrlchannel **cc)
*cc = ctrlchannel_new(fd, false);
} else {
fprintf(stderr,
"Missing port and fd options for TCP control channel\n");
logprintf(STDERR_FILENO,
"Missing port and fd options for TCP control channel\n");
goto error;
}
} else {
fprintf(stderr, "Unsupport control channel type: %s\n", type);
logprintf(STDERR_FILENO, "Unsupport control channel type: %s\n", type);
goto error;
}
@ -825,7 +830,7 @@ static int parse_server_options(char *options, struct server **c)
ovs = options_parse(options, server_opt_desc, &error);
if (!ovs) {
fprintf(stderr, "Error parsing server options: %s\n", error);
logprintf(STDERR_FILENO, "Error parsing server options: %s\n", error);
goto error;
}
@ -839,9 +844,9 @@ static int parse_server_options(char *options, struct server **c)
fd = option_get_int(ovs, "fd", -1);
if (fd >= 0) {
if (fstat(fd, &stat) < 0 || !S_ISSOCK(stat.st_mode)) {
fprintf(stderr,
"Bad filedescriptor %d for UnixIO control channel\n",
fd);
logprintf(STDERR_FILENO,
"Bad filedescriptor %d for UnixIO control channel\n",
fd);
goto error;
}
@ -853,8 +858,9 @@ static int parse_server_options(char *options, struct server **c)
*c = server_new(fd, flags);
} else {
fprintf(stderr,
"Missing path and file descriptor option for UnixIO socket\n");
logprintf(STDERR_FILENO,
"Missing path and file descriptor option for UnixIO "
"socket\n");
goto error;
}
} else if (!strcmp(type, "tcp")) {
@ -862,8 +868,8 @@ static int parse_server_options(char *options, struct server **c)
fd = option_get_int(ovs, "fd", -1);
if (fd >= 0) {
if (fstat(fd, &stat) < 0 || !S_ISSOCK(stat.st_mode)) {
fprintf(stderr,
"Bad filedescriptor %d for TCP socket\n", fd);
logprintf(STDERR_FILENO,
"Bad filedescriptor %d for TCP socket\n", fd);
goto error;
}
@ -872,8 +878,8 @@ static int parse_server_options(char *options, struct server **c)
*c = server_new(fd, flags);
} else if (port >= 0) {
if (port >= 0x10000) {
fprintf(stderr,
"TCP socket port outside valid range\n");
logprintf(STDERR_FILENO,
"TCP socket port outside valid range\n");
goto error;
}
@ -886,12 +892,12 @@ static int parse_server_options(char *options, struct server **c)
*c = server_new(fd, flags);
} else {
fprintf(stderr,
"Missing port and fd options for TCP socket\n");
logprintf(STDERR_FILENO,
"Missing port and fd options for TCP socket\n");
goto error;
}
} else {
fprintf(stderr, "Unsupport socket type: %s\n", type);
logprintf(STDERR_FILENO, "Unsupport socket type: %s\n", type);
goto error;
}

View File

@ -1263,6 +1263,8 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
memset(&cinfo, 0, sizeof(cinfo));
memset(&param, 0, sizeof(param));
log_set_prefix("swtpm: ");
while (true) {
opt = getopt_long(argc, argv, "M:m:n:r:hv", longopts, &longindex);
@ -1272,22 +1274,24 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
switch (opt) {
case 'M': /* major */
if (sscanf(optarg, "%u", &num) != 1) {
fprintf(stderr, "Could not parse major number\n");
logprintf(STDERR_FILENO, "Could not parse major number\n");
return -1;
}
if (num > 65535) {
fprintf(stderr, "Major number outside valid range [0 - 65535]\n");
logprintf(STDERR_FILENO,
"Major number outside valid range [0 - 65535]\n");
return -1;
}
cinfo.dev_major = num;
break;
case 'm': /* minor */
if (sscanf(optarg, "%u", &num) != 1) {
fprintf(stderr, "Could not parse major number\n");
logprintf(STDERR_FILENO, "Could not parse major number\n");
return -1;
}
if (num > 65535) {
fprintf(stderr, "Major number outside valid range [0 - 65535]\n");
logprintf(STDERR_FILENO,
"Major number outside valid range [0 - 65535]\n");
return -1;
}
cinfo.dev_minor = num;
@ -1296,7 +1300,7 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
if (!cinfo.dev_info_argc) {
cinfo_argv[0] = calloc(1, strlen("DEVNAME=") + strlen(optarg) + 1);
if (!cinfo_argv[0]) {
fprintf(stderr, "Out of memory\n");
logprintf(STDERR_FILENO, "Out of memory\n");
return -1;
}
devname = optarg;
@ -1340,7 +1344,7 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
}
if (!cinfo.dev_info_argv) {
fprintf(stderr, "Error: device name missing\n");
logprintf(STDERR_FILENO, "Error: device name missing\n");
return -2;
}
@ -1352,44 +1356,45 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac
return -3;
if (setuid(0)) {
fprintf(stderr, "Error: Unable to setuid root. uid = %d, "
"euid = %d, gid = %d\n", getuid(), geteuid(), getgid());
logprintf(STDERR_FILENO, "Error: Unable to setuid root. uid = %d, "
"euid = %d, gid = %d\n", getuid(), geteuid(), getgid());
return -4;
}
if (param.runas) {
if (!(passwd = getpwnam(param.runas))) {
fprintf(stderr, "User '%s' does not exist\n",
param.runas);
logprintf(STDERR_FILENO, "User '%s' does not exist\n",
param.runas);
return -5;
}
}
tpmdir = tpmstate_get_dir();
if (tpmdir == NULL) {
fprintf(stderr,
"Error: No TPM state directory is defined; TPM_PATH is not set\n");
logprintf(STDERR_FILENO,
"Error: No TPM state directory is defined; "
"TPM_PATH is not set\n");
return -1;
}
n = snprintf(path, sizeof(path), "/dev/%s", devname);
if (n < 0) {
fprintf(stderr,
"Error: Could not create device file name\n");
logprintf(STDERR_FILENO,
"Error: Could not create device file name\n");
return -1;
}
if (n >= (int)sizeof(path)) {
fprintf(stderr,
"Error: Buffer too small to create device file name\n");
logprintf(STDERR_FILENO,
"Error: Buffer too small to create device file name\n");
return -1;
}
tpmfd = open(path, O_RDWR);
if (tpmfd >= 0) {
close(tpmfd);
fprintf(stderr,
"Error: A device '%s' already exists.\n",
path);
logprintf(STDERR_FILENO,
"Error: A device '%s' already exists.\n",
path);
return -1;
}

View File

@ -57,7 +57,7 @@
#include <sys/stat.h>
#include "key.h"
#include "logging.h"
/*
* key_format_from_string:
@ -74,7 +74,7 @@ key_format_from_string(const char *format)
} else if (!strcmp(format, "binary")) {
return KEY_FORMAT_BINARY;
}
fprintf(stderr, "Unknown key format '%s'.\n", format);
logprintf(STDERR_FILENO, "Unknown key format '%s'.\n", format);
return KEY_FORMAT_UNKNOWN;
}
@ -151,14 +151,16 @@ key_parse_as_hexkey(const char *rawkey,
digits = key_stream_to_bin(&rawkey[offset], key, maxkeylen);
if (digits < 0) {
fprintf(stderr, "Could not parse key hex string into %zu byte buffer.\n",
maxkeylen);
logprintf(STDERR_FILENO,
"Could not parse key hex string into %zu byte buffer.\n",
maxkeylen);
return -1;
} else if (digits == 128/4) {
*keylen = 128/8;
} else {
fprintf(stderr, "Unsupported key length with %zu digits.\n",
digits);
logprintf(STDERR_FILENO,
"Unsupported key length with %zu digits.\n",
digits);
return -1;
}
@ -188,15 +190,15 @@ key_load_key(const char *filename, enum key_format keyformat,
fd = open(filename, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open file %s: %s\n",
filename, strerror(errno));
logprintf(STDERR_FILENO, "Unable to open file %s: %s\n",
filename, strerror(errno));
return -1;
}
len = read(fd, filebuffer, sizeof(filebuffer) - 1);
close(fd);
if (len < 0) {
fprintf(stderr, "Unable to read key: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Unable to read key: %s\n",
strerror(errno));
return -1;
}
filebuffer[len] = 0;
@ -205,8 +207,9 @@ key_load_key(const char *filename, enum key_format keyformat,
case KEY_FORMAT_BINARY:
*keylen = len;
if (maxkeylen < (size_t)len) {
fprintf(stderr, "Key is larger than buffer (%zu > %zu).\n",
len, maxkeylen);
logprintf(STDERR_FILENO,
"Key is larger than buffer (%zu > %zu).\n",
len, maxkeylen);
return -1;
}
memcpy(key, filebuffer, len);
@ -251,29 +254,32 @@ key_from_pwdfile(const char *filename, unsigned char *key, size_t *keylen,
#endif
if (maxkeylen > sizeof(hashbuf)) {
fprintf(stderr, "Request keylength is too big (%zu > %zu)\n",
maxkeylen, sizeof(hashbuf));
logprintf(STDERR_FILENO,
"Request keylength is too big (%zu > %zu)\n",
maxkeylen, sizeof(hashbuf));
return -1;
}
fd = open(filename, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open file %s : %s\n",
filename, strerror(errno));
logprintf(STDERR_FILENO,
"Unable to open file %s : %s\n",
filename, strerror(errno));
return -1;
}
len = read(fd, filebuffer, sizeof(filebuffer));
close(fd);
if (len < 0) {
fprintf(stderr, "Unable to read passphrase: %s\n",
strerror(errno));
logprintf(STDERR_FILENO,
"Unable to read passphrase: %s\n",
strerror(errno));
return -1;
}
#ifdef USE_FREEBL_CRYPTO_LIBRARY
if (SHA512_HashBuf(hashbuf, filebuffer, len) != SECSuccess) {
fprintf(stderr, "Could not hash the passphrase");
logprintf(STDERR_FILENO, "Could not hash the passphrase");
return -1;
}
#else

View File

@ -59,7 +59,13 @@
static int logfd = CONSOLE_LOGGING;
static unsigned int log_level = 1;
static char *log_prefix = NULL;
static char *log_prefix;
static void log_prefix_clear(void)
{
free(log_prefix);
log_prefix = NULL;
}
/*
* log_init:
@ -79,6 +85,8 @@ int log_init(const char *filename)
if (logfd < 0)
return -1;
log_prefix_clear();
return 0;
}
@ -107,6 +115,8 @@ int log_init_fd(int fd)
}
}
log_prefix_clear();
return 0;
}

View File

@ -135,8 +135,8 @@ int mainLoop(struct mainLoopParams *mlp,
rc = TPM_Malloc(&command, max_command_length);
if (rc != TPM_SUCCESS) {
fprintf(stderr, "Could not allocate %u bytes for buffer.\n",
max_command_length);
logprintf(STDERR_FILENO, "Could not allocate %u bytes for buffer.\n",
max_command_length);
return rc;
}

View File

@ -189,6 +189,8 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
{NULL , 0 , 0, 0 },
};
log_set_prefix("swtpm: ");
while (TRUE) {
opt = getopt_long(argc, argv, "dhp:f:tr:", longopts, &longindex);
@ -204,17 +206,19 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
errno = 0;
val = strtoul(optarg, &end_ptr, 0);
if (val != (unsigned int)val || errno || end_ptr[0] != '\0') {
fprintf(stderr, "Cannot parse socket port number '%s'.\n",
optarg);
logprintf(STDERR_FILENO,
"Cannot parse socket port number '%s'.\n",
optarg);
exit(1);
}
if (val >= 0x10000) {
fprintf(stderr, "Port is outside valid range.\n");
logprintf(STDERR_FILENO, "Port is outside valid range.\n");
exit(1);
}
snprintf(buf, sizeof(buf), "%lu", val);
if (setenv("TPM_PORT", buf, 1) != 0) {
fprintf(stderr, "Could not set port: %s\n", strerror(errno));
logprintf(STDERR_FILENO,
"Could not set port: %s\n", strerror(errno));
exit(1);
}
break;
@ -223,13 +227,14 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
errno = 0;
val = strtoul(optarg, &end_ptr, 10);
if (val != (unsigned int)val || errno || end_ptr[0] != '\0') {
fprintf(stderr, "Cannot parse socket file descriptor.\n");
logprintf(STDERR_FILENO,
"Cannot parse socket file descriptor.\n");
exit(1);
}
mlp.fd = val;
if (fstat(mlp.fd, &statbuf) != 0) {
fprintf(stderr, "Cannot stat file descriptor: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Cannot stat file descriptor: %s\n",
strerror(errno));
exit(1);
}
/*
@ -238,8 +243,8 @@ int swtpm_main(int argc, char **argv, const char *prgname, const char *iface)
*/
if (S_ISREG(statbuf.st_mode) || S_ISDIR(statbuf.st_mode) || S_ISBLK(statbuf.st_mode)
|| S_ISLNK(statbuf.st_mode)) {
fprintf(stderr,
"Given file descriptor type is not supported.\n");
logprintf(STDERR_FILENO,
"Given file descriptor type is not supported.\n");
exit(1);
}
mlp.flags |= MAIN_LOOP_FLAG_TERMINATE | MAIN_LOOP_FLAG_USE_FD |

View File

@ -99,14 +99,15 @@ static int create_vtpm_proxy(struct vtpm_proxy_new_dev *vtpm_new_dev)
fd = open("/dev/vtpmx", O_RDWR);
if (fd < 0) {
fprintf(stderr, "Could not open /dev/vtpmx: %s\n", strerror(errno));
logprintf(STDERR_FILENO, "Could not open /dev/vtpmx: %s\n",
strerror(errno));
return -1;
}
n = ioctl(fd, VTPM_PROXY_IOC_NEW_DEV, vtpm_new_dev);
if (n) {
fprintf(stderr, "Ioctl to create vtpm proxy failed: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Ioctl to create vtpm proxy failed: %s\n",
strerror(errno));
ret = -1;
}
close(fd);
@ -208,6 +209,8 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
{NULL , 0 , 0, 0 },
};
log_set_prefix("swtpm: ");
while (TRUE) {
opt = getopt_long(argc, argv, "dhc:f:r:", longopts, &longindex);
@ -225,8 +228,8 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
mlp.fd = open(optarg, O_RDWR);
if (mlp.fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n",
optarg, strerror(errno));
logprintf(STDERR_FILENO, "Cannot open %s: %s\n",
optarg, strerror(errno));
exit(1);
}
mlp.flags |= MAIN_LOOP_FLAG_TERMINATE | MAIN_LOOP_FLAG_USE_FD |
@ -241,13 +244,14 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
errno = 0;
val = strtoul(optarg, &end_ptr, 10);
if (val != (unsigned int)val || errno || end_ptr[0] != '\0') {
fprintf(stderr, "Cannot parse character device file descriptor.\n");
logprintf(STDERR_FILENO,
"Cannot parse character device file descriptor.\n");
exit(1);
}
mlp.fd = val;
if (fstat(mlp.fd, &statbuf) != 0) {
fprintf(stderr, "Cannot stat file descriptor: %s\n",
strerror(errno));
logprintf(STDERR_FILENO, "Cannot stat file descriptor: %s\n",
strerror(errno));
exit(1);
}
/*
@ -256,8 +260,8 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
*/
if (S_ISREG(statbuf.st_mode) || S_ISDIR(statbuf.st_mode) || S_ISBLK(statbuf.st_mode)
|| S_ISLNK(statbuf.st_mode)) {
fprintf(stderr,
"Given file descriptor type is not supported.\n");
logprintf(STDERR_FILENO,
"Given file descriptor type is not supported.\n");
exit(1);
}
mlp.flags |= MAIN_LOOP_FLAG_TERMINATE | MAIN_LOOP_FLAG_USE_FD |
@ -313,7 +317,8 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
};
if (mlp.fd >= 0) {
fprintf(stderr, "Cannot use vTPM proxy with a provided device.\n");
logprintf(STDERR_FILENO,
"Cannot use vTPM proxy with a provided device.\n");
exit(1);
}
if (create_vtpm_proxy(&vtpm_new_dev))
@ -331,7 +336,8 @@ int swtpm_chardev_main(int argc, char **argv, const char *prgname, const char *i
#endif
if (mlp.fd < 0) {
logprintf(STDERR_FILENO, "Error: Missing character device or file descriptor\n");
logprintf(STDERR_FILENO,
"Error: Missing character device or file descriptor\n");
return EXIT_FAILURE;
}

View File

@ -64,6 +64,7 @@
#include <libtpms/tpm_error.h>
#include <libtpms/tpm_types.h>
#include "logging.h"
#include "swtpm_debug.h"
#include "swtpm_io.h"
@ -204,9 +205,9 @@ TPM_RESULT SWTPM_IO_Init(void)
if (rc == 0) {
port_str = getenv("TPM_PORT");
if (port_str == NULL) {
fprintf(stderr,
"SWTPM_IO_Init: Error, TPM_PORT environment variable not "
"set\n");
logprintf(STDERR_FILENO,
"SWTPM_IO_Init: Error, TPM_PORT environment variable not "
"set\n");
rc = TPM_IOERROR;
}
}
@ -214,9 +215,9 @@ TPM_RESULT SWTPM_IO_Init(void)
if (rc == 0) {
irc = sscanf(port_str, "%hu", &port);
if (irc != 1) {
fprintf(stderr,
"SWTPM_IO_Init: Error, TPM_PORT environment variable "
"invalid\n");
logprintf(STDERR_FILENO,
"SWTPM_IO_Init: Error, TPM_PORT environment variable "
"invalid\n");
rc = TPM_IOERROR;
}
}
@ -226,8 +227,9 @@ TPM_RESULT SWTPM_IO_Init(void)
port,
INADDR_ANY);
if (rc != 0) {
fprintf(stderr, "SWTPM_IO_Init: Warning, could not open TCP/IP "
"server socket.\n");
logprintf(STDERR_FILENO,
"SWTPM_IO_Init: Warning, could not open TCP/IP "
"server socket.\n");
}
}
@ -275,9 +277,9 @@ static TPM_RESULT SWTPM_IO_ServerSocket_Open(int *sock_fd,
irc = setsockopt(*sock_fd, SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof(opt));
if (irc != 0) {
fprintf(stderr,
"SWTPM_IO_ServerSocket_Open: Error, server setsockopt() "
"%d %s\n", errno, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_IO_ServerSocket_Open: Error, server setsockopt() "
"%d %s\n", errno, strerror(errno));
rc = TPM_IOERROR;
}
}
@ -288,9 +290,9 @@ static TPM_RESULT SWTPM_IO_ServerSocket_Open(int *sock_fd,
if (irc != 0) {
close(*sock_fd);
*sock_fd = -1;
fprintf(stderr,
"SWTPM_IO_ServerSocket_Open: Error, server bind() %d "
"%s\n", errno, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_IO_ServerSocket_Open: Error, server bind() %d "
"%s\n", errno, strerror(errno));
rc = TPM_IOERROR;
}
}
@ -300,9 +302,9 @@ static TPM_RESULT SWTPM_IO_ServerSocket_Open(int *sock_fd,
if (irc != 0) {
close(*sock_fd);
*sock_fd = -1;
fprintf(stderr,
"SWTPM_IO_ServerSocket_Open: Error, server listen() %d "
"%s\n", errno, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_IO_ServerSocket_Open: Error, server listen() %d "
"%s\n", errno, strerror(errno));
rc = TPM_IOERROR;
}
}
@ -351,9 +353,9 @@ TPM_RESULT SWTPM_IO_Connect(TPM_CONNECTION_FD *connection_fd, /* read/write
TPM_DEBUG("\n SWTPM_IO_Connect: Accepting connection from port %s ...\n", port_str);
connection_fd->fd = accept(sock_fd, (struct sockaddr *)&cli_addr, &cli_len);
if (connection_fd->fd < 0) {
fprintf(stderr,
"SWTPM_IO_Connect: Error, accept() %d %s\n",
errno, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_IO_Connect: Error, accept() %d %s\n",
errno, strerror(errno));
rc = TPM_IOERROR;
}
break;
@ -436,9 +438,9 @@ TPM_RESULT SWTPM_IO_Write(TPM_CONNECTION_FD *connection_fd, /* read/write
/* test that connection is open to write */
if (rc == 0) {
if (connection_fd->fd < 0) {
fprintf(stderr,
"SWTPM_IO_Write: Error, connection not open, fd %d\n",
connection_fd->fd);
logprintf(STDERR_FILENO,
"SWTPM_IO_Write: Error, connection not open, fd %d\n",
connection_fd->fd);
rc = TPM_IOERROR;
}
}
@ -449,8 +451,8 @@ TPM_RESULT SWTPM_IO_Write(TPM_CONNECTION_FD *connection_fd, /* read/write
buffer += nwritten;
}
else {
fprintf(stderr, "SWTPM_IO_Write: Error, write() %d %s\n",
errno, strerror(errno));
logprintf(STDERR_FILENO, "SWTPM_IO_Write: Error, write() %d %s\n",
errno, strerror(errno));
rc = TPM_IOERROR;
}
}

View File

@ -167,9 +167,9 @@ TPM_RESULT SWTPM_NVRAM_Init(void)
if (rc == 0) {
tpm_state_path = tpmstate_get_dir();
if (tpm_state_path == NULL) {
fprintf(stderr,
"SWTPM_NVRAM_Init: Error (fatal), TPM_PATH environment "
"variable not set\n");
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_Init: Error (fatal), TPM_PATH environment "
"variable not set\n");
rc = TPM_FAIL;
}
}
@ -178,9 +178,9 @@ TPM_RESULT SWTPM_NVRAM_Init(void)
if (rc == 0) {
length = strlen(tpm_state_path);
if ((length + TPM_FILENAME_MAX) > FILENAME_MAX) {
fprintf(stderr,
"SWTPM_NVRAM_Init: Error (fatal), TPM state path name "
"%s too large\n", tpm_state_path);
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_Init: Error (fatal), TPM state path name "
"%s too large\n", tpm_state_path);
rc = TPM_FAIL;
}
}
@ -237,8 +237,9 @@ SWTPM_NVRAM_LoadData_Intern(unsigned char **data, /* freed by caller */
rc = TPM_RETRY; /* first time start up */
}
else {
fprintf(stderr, "SWTPM_NVRAM_LoadData: Error (fatal) opening "
"%s for read, %s\n", filename, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_LoadData: Error (fatal) opening "
"%s for read, %s\n", filename, strerror(errno));
rc = TPM_FAIL;
}
}
@ -247,18 +248,18 @@ SWTPM_NVRAM_LoadData_Intern(unsigned char **data, /* freed by caller */
if (rc == 0) {
irc = fseek(file, 0L, SEEK_END); /* seek to end of file */
if (irc == -1L) {
fprintf(stderr,
"SWTPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
filename, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
filename, strerror(errno));
rc = TPM_FAIL;
}
}
if (rc == 0) {
lrc = ftell(file); /* get position in the stream */
if (lrc == -1L) {
fprintf(stderr,
"SWTPM_NVRAM_LoadData: Error (fatal) ftell'ing %s, %s\n",
filename, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_LoadData: Error (fatal) ftell'ing %s, %s\n",
filename, strerror(errno));
rc = TPM_FAIL;
}
else {
@ -268,9 +269,9 @@ SWTPM_NVRAM_LoadData_Intern(unsigned char **data, /* freed by caller */
if (rc == 0) {
irc = fseek(file, 0L, SEEK_SET); /* seek back to the beginning of the file */
if (irc == -1L) {
fprintf(stderr,
"SWTPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
filename, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_LoadData: Error (fatal) fseek'ing %s, %s\n",
filename, strerror(errno));
rc = TPM_FAIL;
}
}
@ -279,9 +280,9 @@ SWTPM_NVRAM_LoadData_Intern(unsigned char **data, /* freed by caller */
TPM_DEBUG(" SWTPM_NVRAM_LoadData: Reading %u bytes of data\n", *length);
rc = TPM_Malloc(data, *length);
if (rc != 0) {
fprintf(stderr,
"SWTPM_NVRAM_LoadData: Error (fatal) allocating %u "
"bytes\n", *length);
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_LoadData: Error (fatal) allocating %u "
"bytes\n", *length);
rc = TPM_FAIL;
}
}
@ -289,9 +290,9 @@ SWTPM_NVRAM_LoadData_Intern(unsigned char **data, /* freed by caller */
if ((rc == 0) && *length != 0) {
src = fread(*data, 1, *length, file);
if (src != *length) {
fprintf(stderr,
"SWTPM_NVRAM_LoadData: Error (fatal), data read of %u "
"only read %lu\n", *length, (unsigned long)src);
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_LoadData: Error (fatal), data read of %u "
"only read %lu\n", *length, (unsigned long)src);
rc = TPM_FAIL;
}
}
@ -300,9 +301,9 @@ SWTPM_NVRAM_LoadData_Intern(unsigned char **data, /* freed by caller */
TPM_DEBUG(" SWTPM_NVRAM_LoadData: Closing file %s\n", filename);
irc = fclose(file); /* @1 */
if (irc != 0) {
fprintf(stderr,
"SWTPM_NVRAM_LoadData: Error (fatal) closing file %s\n",
filename);
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_LoadData: Error (fatal) closing file %s\n",
filename);
rc = TPM_FAIL;
}
else {
@ -371,9 +372,9 @@ SWTPM_NVRAM_StoreData_Intern(const unsigned char *data,
TPM_DEBUG(" SWTPM_NVRAM_StoreData: Opening file %s\n", filename);
file = fopen(filename, "wb"); /* closed @1 */
if (file == NULL) {
fprintf(stderr,
"SWTPM_NVRAM_StoreData: Error (fatal) opening %s for "
"write failed, %s\n", filename, strerror(errno));
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_StoreData: Error (fatal) opening %s for "
"write failed, %s\n", filename, strerror(errno));
rc = TPM_FAIL;
}
}
@ -394,8 +395,9 @@ SWTPM_NVRAM_StoreData_Intern(const unsigned char *data,
lrc = fwrite(encrypt_data ? encrypt_data : data, 1,
length, file);
if (lrc != length) {
fprintf(stderr, "TPM_NVRAM_StoreData: Error (fatal), data write "
"of %u only wrote %u\n", length, lrc);
logprintf(STDERR_FILENO,
"TPM_NVRAM_StoreData: Error (fatal), data write "
"of %u only wrote %u\n", length, lrc);
rc = TPM_FAIL;
}
}
@ -403,8 +405,8 @@ SWTPM_NVRAM_StoreData_Intern(const unsigned char *data,
TPM_DEBUG(" SWTPM_NVRAM_StoreData: Closing file %s\n", filename);
irc = fclose(file); /* @1 */
if (irc != 0) {
fprintf(stderr, "SWTPM_NVRAM_StoreData: Error (fatal) closing "
"file\n");
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_StoreData: Error (fatal) closing file\n");
rc = TPM_FAIL;
}
else {
@ -482,8 +484,9 @@ TPM_RESULT SWTPM_NVRAM_DeleteName(uint32_t tpm_number,
if ((irc != 0) && /* if the remove failed */
(mustExist || /* if any error is a failure, or */
(errno != ENOENT))) { /* if error other than no such file */
fprintf(stderr, "SWTPM_NVRAM_DeleteName: Error, (fatal) file "
"remove failed, errno %d\n", errno);
logprintf(STDERR_FILENO,
"SWTPM_NVRAM_DeleteName: Error, (fatal) file "
"remove failed, errno %d\n", errno);
rc = TPM_FAIL;
}
}
@ -776,7 +779,8 @@ SWTPM_NVRAM_CheckHeader(unsigned char *data, uint32_t length,
return TPM_BAD_PARAMETER;
if (bh->min_version > BLOB_HEADER_VERSION) {
logprintf(STDERR_FILENO, "Minimum required version for the blob is %d, we "
logprintf(STDERR_FILENO,
"Minimum required version for the blob is %d, we "
"only support version %d\n", bh->min_version,
BLOB_HEADER_VERSION);
return TPM_BAD_VERSION;