Skip to content

Commit

Permalink
utils: Implement find_program() to try BINDIR path before PATH
Browse files Browse the repository at this point in the history
To support install paths that are not covered by $PATH, and still allow
swtpm_setup and swtpm_localca to find swtpm and swtpm_cert executables,
implement find_program() to prepend the install path if only the program
name is given and otherwise fall back to g_find_program_in_path().

Update the man page stating that swtpm from the installation directory
(BINDIR) is tried to be used before one is attempted to be found in
the PATH.

Resolves: #795
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
  • Loading branch information
stefanberger committed Apr 17, 2023
1 parent fd2c9fd commit ee0bce1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions man/man8/swtpm_setup.pod
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ Prefix with dir:// to use directory backend, or file:// to use linear file.

=item B<--tpm <path to executable>>

Path to the TPM executable; this is an optional argument and
by default the swtpm executable found in the PATH will be used.
Path to the TPM executable; this is an optional argument and by default the
swtpm executable found in the installation directory (BINDIR) will be used
before swtpm is tried to be found in the PATH.

=item B<--tpm2>

Expand Down
2 changes: 1 addition & 1 deletion src/swtpm_localca/swtpm_localca.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ static int create_cert(unsigned long flags, const gchar *typ, const gchar *direc
int ret = 1;
size_t i, j;

swtpm_cert_path = g_find_program_in_path("swtpm_cert");
swtpm_cert_path = find_program("swtpm_cert");
if (swtpm_cert_path == NULL) {
logerr(gl_LOGFILE, "Could not find swtpm_cert in PATH.\n");
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/swtpm_setup/swtpm_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ int main(int argc, char *argv[])
if (init(&config_file) < 0)
goto error;

swtpm_prg = g_find_program_in_path("swtpm");
swtpm_prg = find_program("swtpm");
if (swtpm_prg) {
tmp = g_strconcat(swtpm_prg, " socket", NULL);
g_free(swtpm_prg);
Expand Down
20 changes: 20 additions & 0 deletions src/utils/swtpm_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <glib.h>

#include "swtpm_conf.h"
#include "swtpm_utils.h"

void append_to_file(const char *pathname, const char *str)
Expand Down Expand Up @@ -440,3 +441,22 @@ int check_directory_access(const gchar *directory, int mode, const struct passwd
}
return 0;
}

/* A program that is only described by the name of the executable is searched
* for in the BINDIR path and only then in $PATH
*/
gchar *find_program(const gchar *program)
{
g_autofree gchar *dirname = g_path_get_dirname(program);
gchar *path;

if (g_strcmp0(".", dirname) == 0) {
path = g_strdup_printf(BINDIR "/%s", program);
if (g_file_test(path, G_FILE_TEST_IS_EXECUTABLE))
return path;

g_free(path);
}

return g_find_program_in_path(program);
}
2 changes: 2 additions & 0 deletions src/utils/swtpm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ gchar *str_replace(const char *in, const char *torep, const char *rep);

int check_directory_access(const gchar *directory, int mode, const struct passwd *curr_user);

gchar *find_program(const gchar *program);

#endif /* SWTPM_UTILS_H */

0 comments on commit ee0bce1

Please sign in to comment.