From 7648a256d97abda40edbdc0d7bf59edd0a09fb95 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Tue, 17 Feb 2026 23:52:06 +0100 Subject: Extract createServer and parseAuthConfig, simplify programs Rename createSecureServer to createServer and add an unsecure path (UA_ServerConfig_setMinimal) when certPath is NULL, eliminating the if/else server creation blocks in server_lds.c and server_register.c. Add parseAuthConfig() to common.c to replace four near-identical authMode parsing blocks across the three programs. Restructure server_register.c error handling with goto cleanup, removing ~20 duplicated cleanup sequences. Rename the CMake library target from DiscoveryCommon to common. --- src/common.h | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'src/common.h') diff --git a/src/common.h b/src/common.h index 7290181..b4bd323 100644 --- a/src/common.h +++ b/src/common.h @@ -15,6 +15,8 @@ #include +#include "config.h" + /** * @brief Loads a DER-encoded certificate or key file into a UA_ByteString. * @@ -47,26 +49,26 @@ int loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize); void freeTrustStore (char **paths, size_t size); /** - * @brief Creates a UA_Server configured with security policies and encryption. + * @brief Creates a UA_Server, optionally configured with security policies. * - * The server is initialized with the specified port, certificate, private key, - * and trustlist. The applicationUri is set in the server's application - * description. + * When @p certPath is non-NULL the server is initialized with encryption + * (certificate, private key, trustlist). When @p certPath is NULL the server + * runs with SecurityPolicy#None only (keyPath and trustPaths are ignored). + * The applicationUri is set in both cases. * * @param port Server port number. * @param applicationUri OPC UA application URI. - * @param certPath Path to server certificate (.der). - * @param keyPath Path to private key (.der). - * @param trustPaths Array of trustlist file paths (may be NULL if trustSize is - * 0). + * @param certPath Path to server certificate (.der), or NULL for unsecure. + * @param keyPath Path to private key (.der), or NULL when certPath is NULL. + * @param trustPaths Array of trustlist file paths (may be NULL). * @param trustSize Number of entries in trustPaths. * @param retval Output parameter set to the status code on failure. * @return A configured UA_Server, or NULL on error. */ -UA_Server *createSecureServer (UA_UInt16 port, const char *applicationUri, - const char *certPath, const char *keyPath, - char **trustPaths, size_t trustSize, - UA_StatusCode *retval); +UA_Server *createServer (UA_UInt16 port, const char *applicationUri, + const char *certPath, const char *keyPath, + char **trustPaths, size_t trustSize, + UA_StatusCode *retval); /** * @brief Parses a log-level name into the corresponding UA_LogLevel value. @@ -79,6 +81,26 @@ UA_Server *createSecureServer (UA_UInt16 port, const char *applicationUri, */ int parseLogLevel (const char *name); +/** + * @brief Parses the authMode key from a configuration file. + * + * When authMode is "anonymous", sets *allowAnonymous to true and + * *username / *password to NULL. When authMode is "user", sets + * *allowAnonymous to false and loads the username/password keys. + * Logs errors internally. + * + * @param cfg Parsed configuration. + * @param program Program name (for error messages). + * @param allowAnonymous Output: true for anonymous, false for user. + * May be NULL (ignored — useful for client callers). + * @param username Output: username string (owned by cfg), or NULL. + * @param password Output: password string (owned by cfg), or NULL. + * @return 0 on success, -1 on error. + */ +int parseAuthConfig (const Config *cfg, const char *program, + UA_Boolean *allowAnonymous, const char **username, + const char **password); + /** * @brief Parses a security mode name into the corresponding enum value. * -- cgit v1.2.3