# Client-side OPAQUE registration tool over SSH.
#
# Pattern: ssh-copy-id analog — logs in with the user's existing SSH key
# (file or agent) first, then triggers a `gssapi-with-mic` userauth with
# our OPAQUE OID in register-mode. The server allows registration only
# after that publickey login, and only for the same user.
# The OPAQUE protocol's 3-message registration runs end-to-end via the
# SSH-tunneled GSSAPI tokens; the password never leaves the client.
#
# See SECURITY.md and GSSAPI_IMPLEMENTATION_PLAN.md §3.3 for rationale.

pkg_check_modules(LIBSSH REQUIRED IMPORTED_TARGET libssh)
message(STATUS "libssh: ${LIBSSH_VERSION}")

add_executable(opaque-register opaque-register.c)

# We do NOT statically link gssopaque_obj — instead we dlopen the
# installed gssopaque.so (the same one mechglue loads for libssh's
# GSSAPI calls). dlopen-deduplication on Linux ensures shared global
# state, so gssspi_opaque_set_password writes the slot that
# gss_acquire_cred (via mechglue) later reads.
target_link_libraries(opaque-register PRIVATE
    PkgConfig::LIBSSH
    PkgConfig::GSSAPI
    PkgConfig::SODIUM
    ${CMAKE_DL_LIBS})

target_compile_definitions(opaque-register PRIVATE
    _POSIX_C_SOURCE=200809L
    # Default plug-in path; the build can override it with
    # -DOPAQUE_PLUGIN_DEFAULT_PATH=/path/to/gssopaque.so or set the
    # OPAQUE_PLUGIN_PATH env var at runtime (path is not a secret).
    OPAQUE_PLUGIN_DEFAULT_PATH="${CMAKE_INSTALL_FULL_LIBDIR}/gssopaque/gssopaque.so")

install(TARGETS opaque-register
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
