19 #include "rcl/error_handling.h"
21 #include "rcutils/env.h"
22 #include "rcutils/filesystem.h"
23 #include "rcutils/logging_macros.h"
24 #include "rcutils/strdup.h"
26 #include "rmw/security_options.h"
31 const rcutils_allocator_t * allocator,
32 rmw_security_options_t * security_options)
34 bool use_security =
false;
40 RCUTILS_LOG_DEBUG_NAMED(
41 ROS_PACKAGE_NAME,
"Using security: %s", use_security ?
"true" :
"false");
44 security_options->enforce_security = RMW_SECURITY_ENFORCEMENT_PERMISSIVE;
56 RCUTILS_LOG_INFO_NAMED(ROS_PACKAGE_NAME,
"Found security directory: %s", secure_root);
57 security_options->security_root_path = secure_root;
59 if (RMW_SECURITY_ENFORCEMENT_ENFORCE == security_options->enforce_security) {
69 const char * ros_security_enable = NULL;
70 const char * get_env_error_str = NULL;
75 if (NULL != get_env_error_str) {
76 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
82 *use_security = (0 == strcmp(ros_security_enable,
"true"));
89 const char * ros_enforce_security = NULL;
90 const char * get_env_error_str = NULL;
95 if (NULL != get_env_error_str) {
96 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
102 *policy = (0 == strcmp(ros_enforce_security,
"Enforce")) ?
103 RMW_SECURITY_ENFORCEMENT_ENFORCE : RMW_SECURITY_ENFORCEMENT_PERMISSIVE;
107 char * exact_match_lookup(
109 const char * ros_secure_keystore_env,
113 char * secure_root = NULL;
114 char * enclaves_dir = NULL;
115 enclaves_dir = rcutils_join_path(ros_secure_keystore_env,
"enclaves", *allocator);
117 if (0 == strcmp(name,
"/")) {
118 secure_root = enclaves_dir;
120 char * relative_path = NULL;
123 relative_path = rcutils_to_native_path(name + 1, *allocator);
124 secure_root = rcutils_join_path(enclaves_dir, relative_path, *allocator);
125 allocator->deallocate(relative_path, allocator->state);
126 allocator->deallocate(enclaves_dir, allocator->state);
132 dupenv(
const char * name,
const rcl_allocator_t * allocator,
char ** value)
134 const char * buffer = NULL;
135 const char * error = rcutils_get_env(name, &buffer);
140 if (0 != strcmp(
"", buffer)) {
141 *value = rcutils_strdup(buffer, *allocator);
142 if (NULL == *value) {
143 return "string duplication failed";
153 RCL_CHECK_ARGUMENT_FOR_NULL(name, NULL);
156 char * secure_root = NULL;
157 char * ros_secure_keystore_env = NULL;
158 char * ros_secure_enclave_override_env = NULL;
164 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
169 if (NULL == ros_secure_keystore_env) {
176 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
178 goto leave_rcl_get_secure_root;
182 if (NULL != ros_secure_enclave_override_env) {
183 secure_root = exact_match_lookup(
184 ros_secure_enclave_override_env,
185 ros_secure_keystore_env,
188 secure_root = exact_match_lookup(
190 ros_secure_keystore_env,
194 if (NULL == secure_root) {
195 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
196 "SECURITY ERROR: unable to find a folder matching the name '%s' in '%s'. ",
197 name, ros_secure_keystore_env);
198 goto leave_rcl_get_secure_root;
201 if (!rcutils_is_directory(secure_root)) {
202 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
203 "SECURITY ERROR: directory '%s' does not exist.", secure_root);
204 allocator->deallocate(secure_root, allocator->state);
208 leave_rcl_get_secure_root:
209 allocator->deallocate(ros_secure_enclave_override_env, allocator->state);
210 allocator->deallocate(ros_secure_keystore_env, allocator->state);
#define RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement)
Check that the given allocator is initialized, or fail with a message.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
#define ROS_SECURITY_ENABLE_VAR_NAME
The name of the environment variable controlling whether security is enabled.
RCL_PUBLIC rcl_ret_t rcl_get_enforcement_policy(rmw_security_enforcement_policy_t *policy)
Get security enforcement policy from the environment.
RCL_PUBLIC char * rcl_get_secure_root(const char *name, const rcl_allocator_t *allocator)
Return the secure root given a enclave name.
#define ROS_SECURITY_KEYSTORE_VAR_NAME
The name of the environment variable containing the path to the keystore.
#define ROS_SECURITY_STRATEGY_VAR_NAME
The name of the environment variable containing the security strategy.
#define ROS_SECURITY_ENCLAVE_OVERRIDE
The name of the environment variable containing the security enclave override.
RCL_PUBLIC rcl_ret_t rcl_security_enabled(bool *use_security)
Check if security has to be used, according to the environment.
RCL_PUBLIC rcl_ret_t rcl_get_security_options_from_environment(const char *name, const rcutils_allocator_t *allocator, rmw_security_options_t *security_options)
Initialize security options from values in the environment variables and given names.
#define RCL_RET_OK
Success return code.
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
#define RCL_RET_ERROR
Unspecified error return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.