22 #include "rcl/error_handling.h"
25 #include "rcutils/allocator.h"
26 #include "rcutils/env.h"
27 #include "rcutils/filesystem.h"
28 #include "rcutils/logging_macros.h"
29 #include "rcutils/strdup.h"
31 #include "rmw/security_options.h"
36 const rcutils_allocator_t * allocator,
37 rmw_security_options_t * security_options)
39 bool use_security =
false;
45 RCUTILS_LOG_DEBUG_NAMED(
46 ROS_PACKAGE_NAME,
"Using security: %s", use_security ?
"true" :
"false");
49 security_options->enforce_security = RMW_SECURITY_ENFORCEMENT_PERMISSIVE;
61 RCUTILS_LOG_INFO_NAMED(ROS_PACKAGE_NAME,
"Found security directory: %s", secure_root);
62 security_options->security_root_path = secure_root;
64 if (RMW_SECURITY_ENFORCEMENT_ENFORCE == security_options->enforce_security) {
74 const char * ros_security_enable = NULL;
75 const char * get_env_error_str = NULL;
80 if (NULL != get_env_error_str) {
81 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
87 *use_security = (0 == strcmp(ros_security_enable,
"true"));
94 const char * ros_enforce_security = NULL;
95 const char * get_env_error_str = NULL;
100 if (NULL != get_env_error_str) {
101 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
107 *policy = (0 == strcmp(ros_enforce_security,
"Enforce")) ?
108 RMW_SECURITY_ENFORCEMENT_ENFORCE : RMW_SECURITY_ENFORCEMENT_PERMISSIVE;
112 char * exact_match_lookup(
114 const char * ros_secure_keystore_env,
118 char * secure_root = NULL;
119 char * enclaves_dir = NULL;
120 enclaves_dir = rcutils_join_path(ros_secure_keystore_env,
"enclaves", *allocator);
122 if (0 == strcmp(name,
"/")) {
123 secure_root = enclaves_dir;
125 char * relative_path = NULL;
128 relative_path = rcutils_to_native_path(name + 1, *allocator);
129 secure_root = rcutils_join_path(enclaves_dir, relative_path, *allocator);
130 allocator->deallocate(relative_path, allocator->state);
131 allocator->deallocate(enclaves_dir, allocator->state);
137 dupenv(
const char * name,
const rcl_allocator_t * allocator,
char ** value)
139 const char * buffer = NULL;
140 const char * error = rcutils_get_env(name, &buffer);
145 if (0 != strcmp(
"", buffer)) {
146 *value = rcutils_strdup(buffer, *allocator);
147 if (NULL == *value) {
148 return "string duplication failed";
158 RCL_CHECK_ARGUMENT_FOR_NULL(name, NULL);
161 char * secure_root = NULL;
162 char * ros_secure_keystore_env = NULL;
163 char * ros_secure_enclave_override_env = NULL;
169 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
174 if (NULL == ros_secure_keystore_env) {
181 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
183 goto leave_rcl_get_secure_root;
187 if (NULL != ros_secure_enclave_override_env) {
188 secure_root = exact_match_lookup(
189 ros_secure_enclave_override_env,
190 ros_secure_keystore_env,
193 secure_root = exact_match_lookup(
195 ros_secure_keystore_env,
199 if (NULL == secure_root) {
200 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
201 "SECURITY ERROR: unable to find a folder matching the name '%s' in '%s'. ",
202 name, ros_secure_keystore_env);
203 goto leave_rcl_get_secure_root;
206 if (!rcutils_is_directory(secure_root)) {
207 RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
208 "SECURITY ERROR: directory '%s' does not exist.", secure_root);
209 allocator->deallocate(secure_root, allocator->state);
213 leave_rcl_get_secure_root:
214 allocator->deallocate(ros_secure_enclave_override_env, allocator->state);
215 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.