1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/**
* @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.
*/
#include "common.h"
#include "config.h"
#include <open62541/client.h>
#include <open62541/client_config_default.h>
#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>
#include <string.h>
#include <time.h>
UA_Boolean running = true;
static void
stopHandler (int sign)
{
UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = false;
}
/* ========================================================================
* Main
* ======================================================================== */
int
main (int argc, char **argv)
{
signal (SIGINT, stopHandler);
signal (SIGTERM, stopHandler);
if (argc < 4 || argc > 5)
{
UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Usage: %s <server-config> <client-config> "
"<discovery-url> [log-level]",
argv[0]);
return EXIT_FAILURE;
}
const char *discoveryEndpoint = argv[3];
const char *logLevelStr = (argc == 5) ? argv[4] : "info";
int logLevel = parseLogLevel (logLevelStr);
if (logLevel < 0)
{
UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Unknown log level: %s "
"(expected trace, debug, info, warning, error, fatal)",
logLevelStr);
return EXIT_FAILURE;
}
/* ── Load server config ─────────────────────────────────────── */
Config serverCfg;
if (configLoad (argv[1], &serverCfg) != 0)
return EXIT_FAILURE;
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;
}
/* Security configuration (optional). When certificate, privateKey, and
trustStore are all omitted the server runs with SecurityPolicy#None
only. When any of the three is present, all three are required. */
const char *serverCertPath = configGet (&serverCfg, "certificate");
const char *serverKeyPath = configGet (&serverCfg, "privateKey");
const char *serverTrustStore = configGet (&serverCfg, "trustStore");
UA_Boolean serverSecure = (serverCertPath != NULL || serverKeyPath != NULL
|| serverTrustStore != NULL);
if (serverSecure && (!serverCertPath || !serverKeyPath || !serverTrustStore))
{
UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Incomplete server security config: certificate, "
"privateKey, and trustStore must all be set, or all "
"omitted");
configFree (&serverCfg);
return EXIT_FAILURE;
}
/* 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 (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;
}
/* ── Load client config ─────────────────────────────────────── */
Config clientCfg;
if (configLoad (argv[2], &clientCfg) != 0)
{
freeTrustStore (serverTrustPaths, serverTrustSize);
configFree (&serverCfg);
return EXIT_FAILURE;
}
const char *clientAppUri
= configRequire (&clientCfg, "applicationUri", "ServerRegister");
const char *clientCertPath
= configRequire (&clientCfg, "certificate", "ServerRegister");
const char *clientKeyPath
= configRequire (&clientCfg, "privateKey", "ServerRegister");
const char *securityModeStr
= 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;
}
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;
}
const char *securityPolicyUri = resolveSecurityPolicyUri (securityPolicyStr);
if (!securityPolicyUri)
{
UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Unknown security policy: %s", securityPolicyStr);
freeTrustStore (serverTrustPaths, serverTrustSize);
configFree (&clientCfg);
configFree (&serverCfg);
return EXIT_FAILURE;
}
/* 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;
}
const char *clientTrustStore
= configRequire (&clientCfg, "trustStore", "ServerRegister");
if (!clientTrustStore)
{
freeTrustStore (serverTrustPaths, serverTrustSize);
configFree (&clientCfg);
configFree (&serverCfg);
return EXIT_FAILURE;
}
char **clientTrustPaths = NULL;
size_t clientTrustSize = 0;
if (loadTrustStore (clientTrustStore, &clientTrustPaths, &clientTrustSize)
!= 0)
{
freeTrustStore (serverTrustPaths, serverTrustSize);
configFree (&clientCfg);
configFree (&serverCfg);
return EXIT_FAILURE;
}
/* ── 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);
}
UA_ServerConfig *serverConfig = UA_Server_getConfig (server);
serverConfig->logging->context = (void *)(uintptr_t)logLevel;
/* Configure access control after server creation because both
UA_ServerConfig_setDefaultWithSecurityPolicies and
UA_ServerConfig_setMinimal reset the access control plugin. The
credential list is deep-copied by UA_AccessControl_default. */
if (!serverAllowAnonymous)
{
UA_UsernamePasswordLogin logins[1];
logins[0].username = UA_STRING ((char *)serverUsername);
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;
}
}
serverConfig->applicationDescription.applicationType
= UA_APPLICATIONTYPE_SERVER;
/* Use run_startup + manual event loop (instead of UA_Server_run) so we
can periodically re-register with the LDS between iterations. */
UA_Server_run_startup (server);
/* UA_Server_registerDiscovery consumes (clears) the client config,
so a fresh zero-initialized config is needed for every call. */
UA_ClientConfig clientConfig;
memset (&clientConfig, 0, sizeof (UA_ClientConfig));
retval = createSecureClientConfig (
&clientConfig, clientAppUri, clientCertPath, clientKeyPath,
clientTrustPaths, clientTrustSize, securityMode, securityPolicyUri);
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;
}
clientConfig.logging->context = (void *)(uintptr_t)logLevel;
if (clientUsername)
UA_ClientConfig_setAuthenticationUsername (&clientConfig, clientUsername,
clientPassword);
UA_String discoveryUrl = UA_STRING_ALLOC (discoveryEndpoint);
retval = UA_Server_registerDiscovery (server, &clientConfig, discoveryUrl,
UA_STRING_NULL);
UA_String_clear (&discoveryUrl);
if (retval != UA_STATUSCODE_GOOD)
UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Initial register failed: %s",
UA_StatusCode_name (retval));
/* Periodic re-registration loop. Re-register with the LDS every
registerInterval seconds so the LDS does not purge us. */
time_t lastRegister = time (NULL);
while (running)
{
UA_Server_run_iterate (server, true);
time_t now = time (NULL);
if (now - lastRegister >= registerInterval)
{
memset (&clientConfig, 0, sizeof (UA_ClientConfig));
retval = createSecureClientConfig (&clientConfig, clientAppUri,
clientCertPath, clientKeyPath,
clientTrustPaths, clientTrustSize,
securityMode, securityPolicyUri);
if (retval == UA_STATUSCODE_GOOD)
{
clientConfig.logging->context = (void *)(uintptr_t)logLevel;
if (clientUsername)
UA_ClientConfig_setAuthenticationUsername (
&clientConfig, clientUsername, clientPassword);
UA_String reregUrl = UA_STRING_ALLOC (discoveryEndpoint);
retval = UA_Server_registerDiscovery (server, &clientConfig,
reregUrl, UA_STRING_NULL);
UA_String_clear (&reregUrl);
if (retval != UA_STATUSCODE_GOOD)
UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Re-register failed: %s",
UA_StatusCode_name (retval));
}
lastRegister = now;
}
}
/* Deregister from the LDS before shutting down so the LDS removes
our entry immediately rather than waiting for the cleanup timeout. */
memset (&clientConfig, 0, sizeof (UA_ClientConfig));
retval = createSecureClientConfig (
&clientConfig, clientAppUri, clientCertPath, clientKeyPath,
clientTrustPaths, clientTrustSize, securityMode, securityPolicyUri);
if (retval == UA_STATUSCODE_GOOD)
{
clientConfig.logging->context = (void *)(uintptr_t)logLevel;
if (clientUsername)
UA_ClientConfig_setAuthenticationUsername (
&clientConfig, clientUsername, clientPassword);
UA_String deregUrl = UA_STRING_ALLOC (discoveryEndpoint);
retval = UA_Server_deregisterDiscovery (server, &clientConfig, deregUrl);
UA_String_clear (&deregUrl);
if (retval != UA_STATUSCODE_GOOD)
UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Could not unregister from discovery server: %s",
UA_StatusCode_name (retval));
}
UA_Server_run_shutdown (server);
UA_Server_delete (server);
freeTrustStore (clientTrustPaths, clientTrustSize);
freeTrustStore (serverTrustPaths, serverTrustSize);
configFree (&clientCfg);
configFree (&serverCfg);
return EXIT_SUCCESS;
}
|