aboutsummaryrefslogtreecommitdiffstats
path: root/src/server_register.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/server_register.c')
-rw-r--r--src/server_register.c217
1 files changed, 46 insertions, 171 deletions
diff --git a/src/server_register.c b/src/server_register.c
index cea7124..80bed20 100644
--- a/src/server_register.c
+++ b/src/server_register.c
@@ -2,10 +2,10 @@
* @file server_register.c
* @brief OPC UA Server that registers with a Local Discovery Server.
*
- * This program runs an OPC UA server configured with security and periodically
- * registers itself with a remote LDS using the RegisterServer2 service. It
- * uses separate certificate pairs for the server and for the client connection
- * to the LDS. On shutdown, it deregisters from the LDS.
+ * This program runs an OPC UA server that periodically registers itself with
+ * a remote LDS using the RegisterServer2 service. Encryption is optional for
+ * the server; the client connection to the LDS uses a separate certificate
+ * pair. On shutdown, it deregisters from the LDS.
*/
#include "common.h"
@@ -16,7 +16,6 @@
#include <open62541/plugin/accesscontrol_default.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
-#include <open62541/server_config_default.h>
#include <signal.h>
#include <stdlib.h>
@@ -66,23 +65,26 @@ main (int argc, char **argv)
/* ── Load server config ─────────────────────────────────────── */
- Config serverCfg;
+ int rc = EXIT_FAILURE;
+ Config serverCfg = { 0 };
+ Config clientCfg = { 0 };
+ char **serverTrustPaths = NULL;
+ size_t serverTrustSize = 0;
+ char **clientTrustPaths = NULL;
+ size_t clientTrustSize = 0;
+ UA_Server *server = NULL;
+
if (configLoad (argv[1], &serverCfg) != 0)
- return EXIT_FAILURE;
+ goto cleanup;
int port = configRequireInt (&serverCfg, "port", "ServerRegister");
const char *applicationUri
= configRequire (&serverCfg, "applicationUri", "ServerRegister");
int registerInterval
= configRequireInt (&serverCfg, "registerInterval", "ServerRegister");
- const char *serverAuthMode
- = configRequire (&serverCfg, "authMode", "ServerRegister");
- if (!applicationUri || !serverAuthMode || port < 0 || registerInterval < 0)
- {
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ if (!applicationUri || port < 0 || registerInterval < 0)
+ goto cleanup;
/* Security configuration (optional). When certificate, privateKey, and
trustStore are all omitted the server runs with SecurityPolicy#None
@@ -99,62 +101,25 @@ main (int argc, char **argv)
"Incomplete server security config: certificate, "
"privateKey, and trustStore must all be set, or all "
"omitted");
- configFree (&serverCfg);
- return EXIT_FAILURE;
+ goto cleanup;
}
- /* Parse server-side auth mode (what clients connecting to this server
- need). "anonymous" allows unauthenticated sessions; "user" requires
- a username/password pair. */
UA_Boolean serverAllowAnonymous;
const char *serverUsername = NULL, *serverPassword = NULL;
+ if (parseAuthConfig (&serverCfg, "ServerRegister", &serverAllowAnonymous,
+ &serverUsername, &serverPassword)
+ != 0)
+ goto cleanup;
- if (strcmp (serverAuthMode, "anonymous") == 0)
- {
- serverAllowAnonymous = true;
- }
- else if (strcmp (serverAuthMode, "user") == 0)
- {
- serverAllowAnonymous = false;
- serverUsername
- = configRequire (&serverCfg, "username", "ServerRegister");
- serverPassword
- = configRequire (&serverCfg, "password", "ServerRegister");
- if (!serverUsername || !serverPassword)
- {
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
- }
- else
- {
- UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Unknown server auth mode: %s "
- "(expected 'anonymous' or 'user')",
- serverAuthMode);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
-
- char **serverTrustPaths = NULL;
- size_t serverTrustSize = 0;
if (serverSecure
&& loadTrustStore (serverTrustStore, &serverTrustPaths, &serverTrustSize)
!= 0)
- {
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ goto cleanup;
/* ── Load client config ─────────────────────────────────────── */
- Config clientCfg;
if (configLoad (argv[2], &clientCfg) != 0)
- {
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ goto cleanup;
const char *clientAppUri
= configRequire (&clientCfg, "applicationUri", "ServerRegister");
@@ -166,27 +131,17 @@ main (int argc, char **argv)
= configRequire (&clientCfg, "securityMode", "ServerRegister");
const char *securityPolicyStr
= configRequire (&clientCfg, "securityPolicy", "ServerRegister");
- const char *clientAuthMode
- = configRequire (&clientCfg, "authMode", "ServerRegister");
if (!clientAppUri || !clientCertPath || !clientKeyPath || !securityModeStr
- || !securityPolicyStr || !clientAuthMode)
- {
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ || !securityPolicyStr)
+ goto cleanup;
UA_MessageSecurityMode securityMode = parseSecurityMode (securityModeStr);
if (securityMode == UA_MESSAGESECURITYMODE_INVALID)
{
UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Unknown security mode: %s", securityModeStr);
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
+ goto cleanup;
}
const char *securityPolicyUri = resolveSecurityPolicyUri (securityPolicyStr);
@@ -194,104 +149,32 @@ main (int argc, char **argv)
{
UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Unknown security policy: %s", securityPolicyStr);
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
+ goto cleanup;
}
- /* Parse client-side auth mode (how this server authenticates to the
- LDS when registering). */
const char *clientUsername = NULL, *clientPassword = NULL;
-
- if (strcmp (clientAuthMode, "anonymous") == 0)
- {
- /* No credentials needed. */
- }
- else if (strcmp (clientAuthMode, "user") == 0)
- {
- clientUsername
- = configRequire (&clientCfg, "username", "ServerRegister");
- clientPassword
- = configRequire (&clientCfg, "password", "ServerRegister");
- if (!clientUsername || !clientPassword)
- {
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
- }
- else
- {
- UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
- "Unknown client auth mode: %s "
- "(expected 'anonymous' or 'user')",
- clientAuthMode);
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ if (parseAuthConfig (&clientCfg, "ServerRegister", NULL, &clientUsername,
+ &clientPassword)
+ != 0)
+ goto cleanup;
const char *clientTrustStore
= configRequire (&clientCfg, "trustStore", "ServerRegister");
if (!clientTrustStore)
- {
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ goto cleanup;
- char **clientTrustPaths = NULL;
- size_t clientTrustSize = 0;
if (loadTrustStore (clientTrustStore, &clientTrustPaths, &clientTrustSize)
!= 0)
- {
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ goto cleanup;
/* ── Create and configure server ────────────────────────────── */
UA_StatusCode retval;
- UA_Server *server;
-
- if (serverSecure)
- {
- server = createSecureServer ((UA_UInt16)port, applicationUri,
- serverCertPath, serverKeyPath,
- serverTrustPaths, serverTrustSize, &retval);
- if (!server)
- {
- freeTrustStore (clientTrustPaths, clientTrustSize);
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
- }
- else
- {
- server = UA_Server_new ();
- UA_ServerConfig *config = UA_Server_getConfig (server);
- retval = UA_ServerConfig_setMinimal (config, (UA_UInt16)port, NULL);
- if (retval != UA_STATUSCODE_GOOD)
- {
- UA_Server_delete (server);
- freeTrustStore (clientTrustPaths, clientTrustSize);
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
- UA_String_clear (&config->applicationDescription.applicationUri);
- config->applicationDescription.applicationUri
- = UA_String_fromChars (applicationUri);
- }
+ server = createServer ((UA_UInt16)port, applicationUri, serverCertPath,
+ serverKeyPath, serverTrustPaths, serverTrustSize,
+ &retval);
+ if (!server)
+ goto cleanup;
UA_ServerConfig *serverConfig = UA_Server_getConfig (server);
serverConfig->logging->context = (void *)(uintptr_t)logLevel;
@@ -307,14 +190,7 @@ main (int argc, char **argv)
logins[0].password = UA_STRING ((char *)serverPassword);
retval = UA_AccessControl_default (serverConfig, false, NULL, 1, logins);
if (retval != UA_STATUSCODE_GOOD)
- {
- UA_Server_delete (server);
- freeTrustStore (clientTrustPaths, clientTrustSize);
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
- }
+ goto cleanup;
}
serverConfig->applicationDescription.applicationType
@@ -334,12 +210,7 @@ main (int argc, char **argv)
if (retval != UA_STATUSCODE_GOOD)
{
UA_Server_run_shutdown (server);
- UA_Server_delete (server);
- freeTrustStore (clientTrustPaths, clientTrustSize);
- freeTrustStore (serverTrustPaths, serverTrustSize);
- configFree (&clientCfg);
- configFree (&serverCfg);
- return EXIT_FAILURE;
+ goto cleanup;
}
clientConfig.logging->context = (void *)(uintptr_t)logLevel;
if (clientUsername)
@@ -412,10 +283,14 @@ main (int argc, char **argv)
}
UA_Server_run_shutdown (server);
- UA_Server_delete (server);
+ rc = EXIT_SUCCESS;
+
+cleanup:
+ if (server)
+ UA_Server_delete (server);
freeTrustStore (clientTrustPaths, clientTrustSize);
freeTrustStore (serverTrustPaths, serverTrustSize);
configFree (&clientCfg);
configFree (&serverCfg);
- return EXIT_SUCCESS;
+ return rc;
}