server_access_control.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 <ua_server.h>
  4. #include <ua_config_default.h>
  5. #include <ua_plugin_access_control.h>
  6. #include <ua_accesscontrol_default.h>
  7. #include <ua_log_stdout.h>
  8. #include <signal.h>
  9. #include <stdlib.h>
  10. static UA_Boolean
  11. allowAddNode(UA_Server *server, UA_AccessControl *ac,
  12. const UA_NodeId *sessionId, void *sessionContext,
  13. const UA_AddNodesItem *item) {
  14. printf("Called allowAddNode\n");
  15. return UA_TRUE;
  16. }
  17. static UA_Boolean
  18. allowAddReference(UA_Server *server, UA_AccessControl *ac,
  19. const UA_NodeId *sessionId, void *sessionContext,
  20. const UA_AddReferencesItem *item) {
  21. printf("Called allowAddReference\n");
  22. return UA_TRUE;
  23. }
  24. static UA_Boolean
  25. allowDeleteNode(UA_Server *server, UA_AccessControl *ac,
  26. const UA_NodeId *sessionId, void *sessionContext,
  27. const UA_DeleteNodesItem *item) {
  28. printf("Called allowDeleteNode\n");
  29. return UA_FALSE; // Do not allow deletion from client
  30. }
  31. static UA_Boolean
  32. allowDeleteReference(UA_Server *server, UA_AccessControl *ac,
  33. const UA_NodeId *sessionId, void *sessionContext,
  34. const UA_DeleteReferencesItem *item) {
  35. printf("Called allowDeleteReference\n");
  36. return UA_TRUE;
  37. }
  38. UA_Boolean running = true;
  39. static void stopHandler(int sign) {
  40. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  41. running = false;
  42. }
  43. static UA_UsernamePasswordLogin logins[2] = {
  44. {UA_STRING_STATIC("peter"), UA_STRING_STATIC("peter123")},
  45. {UA_STRING_STATIC("paula"), UA_STRING_STATIC("paula123")}
  46. };
  47. int main(void) {
  48. signal(SIGINT, stopHandler);
  49. signal(SIGTERM, stopHandler);
  50. UA_ServerConfig *config = UA_ServerConfig_new_default();
  51. // disable anonymous logins, enable two user/password logins
  52. config->accessControl.deleteMembers(&config->accessControl);
  53. if (UA_AccessControl_default(&config->accessControl, false, 2, logins) != UA_STATUSCODE_GOOD) {
  54. return EXIT_FAILURE;
  55. }
  56. // Set accessControl functions for nodeManagement
  57. config->accessControl.allowAddNode = allowAddNode;
  58. config->accessControl.allowAddReference = allowAddReference;
  59. config->accessControl.allowDeleteNode = allowDeleteNode;
  60. config->accessControl.allowDeleteReference = allowDeleteReference;
  61. UA_Server *server = UA_Server_new(config);
  62. UA_StatusCode retval = UA_Server_run(server, &running);
  63. UA_Server_delete(server);
  64. UA_ServerConfig_delete(config);
  65. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  66. }