Skip to content

Commit

Permalink
list-modules: Do not embed module-name and module-path in URL
Browse files Browse the repository at this point in the history
module-name and module-path are query attributes, which are to modify
the default settings (RFC 7512 section 2.4).  As p11-kit manages the
default configuration, it shouldn't print them as part of the token
URL.  This instead prints the absolute path of each module in a
separate field of the output.

Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Sep 18, 2023
1 parent 03652e2 commit 89c2115
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 20 deletions.
4 changes: 2 additions & 2 deletions common/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ p11_list_printer_write_array (p11_list_printer *printer,
print_indent (printer->fp, printer->depth);

if (printer->use_color) {
fprintf (printer->fp, "\033[0;1m%s\033[0m: \n", name);
fprintf (printer->fp, "\033[0;1m%s\033[0m:\n", name);
} else {
fprintf (printer->fp, "%s: \n", name);
fprintf (printer->fp, "%s:\n", name);
}

for (i = 0; i < array->num; i++) {
Expand Down
1 change: 1 addition & 0 deletions p11-kit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ c_tests += test-server
sh_tests += \
p11-kit/test-profiles.sh \
p11-kit/test-objects.sh \
p11-kit/test-lists.sh \
p11-kit/test-server.sh \
$(NULL)
endif
Expand Down
33 changes: 15 additions & 18 deletions p11-kit/lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ print_token_info (p11_list_printer *printer, CK_FUNCTION_LIST_PTR module, P11Kit
ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_TOKEN, &value);
if (ret != P11_KIT_URI_OK) {
p11_message (_("couldn't format URI into string: %s"), p11_kit_uri_message (ret));
return;
goto out;
}
p11_list_printer_write_value (printer, "uri", "%s", value);
free (value);
Expand Down Expand Up @@ -158,6 +158,7 @@ print_token_info (p11_list_printer *printer, CK_FUNCTION_LIST_PTR module, P11Kit
p11_list_printer_write_array (printer, "flags", flags);
p11_array_free (flags);

out:
p11_list_printer_end_section (printer);
}

Expand All @@ -177,15 +178,23 @@ print_module_info (p11_list_printer *printer, CK_FUNCTION_LIST_PTR module, P11Ki
return;
}

value = p11_kit_module_get_name (module);
p11_list_printer_start_section (printer, "module", "%s", value);
free (value);

memcpy (p11_kit_uri_get_module_info (uri), &info, sizeof (info));
ret = p11_kit_uri_format (uri, P11_KIT_URI_FOR_MODULE, &value);
if (ret != P11_KIT_URI_OK) {
p11_message (_("couldn't format URI into string: %s"), p11_kit_uri_message (ret));
return;
goto out;
}
p11_list_printer_write_value (printer, "uri", "%s", value);
free (value);

value = p11_kit_module_get_filename (module);
p11_list_printer_write_value (printer, "path", "%s", value);
free (value);

value = p11_kit_space_strdup (info.libraryDescription, sizeof (info.libraryDescription));
p11_list_printer_write_value (printer, "library-description", "%s", value);
free (value);
Expand All @@ -201,19 +210,20 @@ print_module_info (p11_list_printer *printer, CK_FUNCTION_LIST_PTR module, P11Ki
rv = (module->C_GetSlotList) (CK_TRUE, slot_list, &count);
if (rv != CKR_OK) {
p11_message (_("couldn't load module info: %s"), p11_kit_strerror (rv));
return;
goto out;
}

for (i = 0; i < count; i++)
print_token_info (printer, module, uri, slot_list[i]);

out:
p11_list_printer_end_section (printer);
}

static int
print_modules (void)
{
CK_FUNCTION_LIST_PTR *module_list;
char *name;
char *path;
p11_list_printer printer;
P11KitUri *uri;
int i;
Expand All @@ -231,20 +241,7 @@ print_modules (void)
p11_list_printer_init (&printer, stdout, 0);

for (i = 0; module_list[i]; i++) {
name = p11_kit_module_get_name (module_list[i]);
path = p11_kit_config_option (module_list[i], "module");

p11_kit_uri_set_module_name (uri, name);
p11_kit_uri_set_module_path (uri, path);

p11_list_printer_start_section (&printer,
name ? name : "(null)",
"%s", path ? path : "(null)");
print_module_info (&printer, module_list[i], uri);
p11_list_printer_end_section (&printer);

free (name);
free (path);
}

p11_kit_uri_free (uri);
Expand Down
4 changes: 4 additions & 0 deletions p11-kit/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ if get_option('test')
find_program('test-objects.sh'),
env: p11_kit_tests_env)

test('test-lists.sh',
find_program('test-lists.sh'),
env: p11_kit_tests_env)

test('test-messages.sh',
find_program('test-messages.sh'),
env: p11_kit_tests_env)
Expand Down
105 changes: 105 additions & 0 deletions p11-kit/test-lists.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/sh

test "${abs_top_builddir+set}" = set || {
echo "set abs_top_builddir" 1>&2
exit 1
}

. "$abs_top_builddir/common/test-init.sh"

setup() {
testdir=$PWD/test-objects-$$
test -d "$testdir" || mkdir "$testdir"
cd "$testdir"
}

teardown() {
rm -rf "$testdir"
}

test_list_modules() {
cat > list.exp <<EOF
module: four
uri: pkcs11:library-description=MOCK%20LIBRARY;library-manufacturer=MOCK%20MANUFACTURER
library-description: MOCK LIBRARY
library-manufacturer: MOCK MANUFACTURER
library-version: 45.145
token: TEST LABEL
uri: pkcs11:model=TEST%20MODEL;manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL;token=TEST%20LABEL
manufacturer: TEST MANUFACTURER
model: TEST MODEL
serial-number: TEST SERIAL
hardware-version: 75.175
firmware-version: 85.185
flags:
login-required
user-pin-initialized
clock-on-token
token-initialized
module: eleven
uri: pkcs11:library-description=MOCK%20LIBRARY;library-manufacturer=MOCK%20MANUFACTURER
library-description: MOCK LIBRARY
library-manufacturer: MOCK MANUFACTURER
library-version: 45.145
token: TEST LABEL
uri: pkcs11:model=TEST%20MODEL;manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL;token=TEST%20LABEL
manufacturer: TEST MANUFACTURER
model: TEST MODEL
serial-number: TEST SERIAL
hardware-version: 75.175
firmware-version: 85.185
flags:
login-required
user-pin-initialized
clock-on-token
token-initialized
module: one
uri: pkcs11:library-description=MOCK%20LIBRARY;library-manufacturer=MOCK%20MANUFACTURER
library-description: MOCK LIBRARY
library-manufacturer: MOCK MANUFACTURER
library-version: 45.145
token: TEST LABEL
uri: pkcs11:model=TEST%20MODEL;manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL;token=TEST%20LABEL
manufacturer: TEST MANUFACTURER
model: TEST MODEL
serial-number: TEST SERIAL
hardware-version: 75.175
firmware-version: 85.185
flags:
login-required
user-pin-initialized
clock-on-token
token-initialized
module: two-duplicate
uri: pkcs11:library-description=MOCK%20LIBRARY;library-manufacturer=MOCK%20MANUFACTURER
library-description: MOCK LIBRARY
library-manufacturer: MOCK MANUFACTURER
library-version: 45.145
token: TEST LABEL
uri: pkcs11:model=TEST%20MODEL;manufacturer=TEST%20MANUFACTURER;serial=TEST%20SERIAL;token=TEST%20LABEL
manufacturer: TEST MANUFACTURER
model: TEST MODEL
serial-number: TEST SERIAL
hardware-version: 75.175
firmware-version: 85.185
flags:
login-required
user-pin-initialized
clock-on-token
token-initialized
EOF

# Since the path is absolute, it may contain user's current working
# directory; strip it before taking a diff.
if ! "$abs_top_builddir"/p11-kit/p11-kit-testable list-modules -q | sed '/^ *path: /d' > list.out; then
assert_fail "unable to run: p11-kit list-modules"
fi

: ${DIFF=diff}
if ! ${DIFF} list.exp list.out > list.diff; then
sed 's/^/# /' list.diff
assert_fail "output contains incorrect result"
fi
}

run test_list_modules

0 comments on commit 89c2115

Please sign in to comment.