server_access_control.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include <open62541/plugin/accesscontrol_default.h>
  4. #include <open62541/plugin/log_stdout.h>
  5. #include <open62541/server.h>
  6. #include <open62541/server_config_default.h>
  7. #include <signal.h>
  8. #include <stdlib.h>
  9. static UA_Boolean
  10. allowAddNode(UA_Server *server, UA_AccessControl *ac,
  11. const UA_NodeId *sessionId, void *sessionContext,
  12. const UA_AddNodesItem *item) {
  13. printf("Called allowAddNode\n");
  14. return UA_TRUE;
  15. }
  16. static UA_Boolean
  17. allowAddReference(UA_Server *server, UA_AccessControl *ac,
  18. const UA_NodeId *sessionId, void *sessionContext,
  19. const UA_AddReferencesItem *item) {
  20. printf("Called allowAddReference\n");
  21. return UA_TRUE;
  22. }
  23. static UA_Boolean
  24. allowDeleteNode(UA_Server *server, UA_AccessControl *ac,
  25. const UA_NodeId *sessionId, void *sessionContext,
  26. const UA_DeleteNodesItem *item) {
  27. printf("Called allowDeleteNode\n");
  28. return UA_FALSE; // Do not allow deletion from client
  29. }
  30. static UA_Boolean
  31. allowDeleteReference(UA_Server *server, UA_AccessControl *ac,
  32. const UA_NodeId *sessionId, void *sessionContext,
  33. const UA_DeleteReferencesItem *item) {
  34. printf("Called allowDeleteReference\n");
  35. return UA_TRUE;
  36. }
  37. UA_Boolean running = true;
  38. static void stopHandler(int sign) {
  39. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  40. running = false;
  41. }
  42. static UA_UsernamePasswordLogin logins[2] = {
  43. {UA_STRING_STATIC("peter"), UA_STRING_STATIC("peter123")},
  44. {UA_STRING_STATIC("paula"), UA_STRING_STATIC("paula123")}
  45. };
  46. int main(void) {
  47. signal(SIGINT, stopHandler);
  48. signal(SIGTERM, stopHandler);
  49. UA_Server *server = UA_Server_new();
  50. UA_ServerConfig *config = UA_Server_getConfig(server);
  51. UA_ServerConfig_setDefault(config);
  52. /* Disable anonymous logins, enable two user/password logins */
  53. config->accessControl.deleteMembers(&config->accessControl);
  54. UA_StatusCode retval = UA_AccessControl_default(config, false,
  55. &config->securityPolicies[config->securityPoliciesSize-1].policyUri, 2, logins);
  56. if(retval != UA_STATUSCODE_GOOD)
  57. goto cleanup;
  58. /* Set accessControl functions for nodeManagement */
  59. config->accessControl.allowAddNode = allowAddNode;
  60. config->accessControl.allowAddReference = allowAddReference;
  61. config->accessControl.allowDeleteNode = allowDeleteNode;
  62. config->accessControl.allowDeleteReference = allowDeleteReference;
  63. retval = UA_Server_run(server, &running);
  64. cleanup:
  65. UA_Server_delete(server);
  66. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  67. }