|
| 1 | +// Copyright 2018-2020 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef RCL__SECURITY_H_ |
| 16 | +#define RCL__SECURITY_H_ |
| 17 | + |
| 18 | +#ifdef __cplusplus |
| 19 | +extern "C" |
| 20 | +{ |
| 21 | +#endif |
| 22 | + |
| 23 | +#include <stdbool.h> |
| 24 | + |
| 25 | +#include "rcl/allocator.h" |
| 26 | +#include "rcl/types.h" |
| 27 | +#include "rcl/visibility_control.h" |
| 28 | +#include "rmw/security_options.h" |
| 29 | + |
| 30 | +#ifndef ROS_SECURITY_DIRECTORY_OVERRIDE |
| 31 | +# define ROS_SECURITY_DIRECTORY_OVERRIDE "ROS_SECURITY_DIRECTORY_OVERRIDE" |
| 32 | +#endif |
| 33 | + |
| 34 | +#ifndef ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME |
| 35 | +# define ROS_SECURITY_ROOT_DIRECTORY_VAR_NAME "ROS_SECURITY_ROOT_DIRECTORY" |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifndef ROS_SECURITY_STRATEGY_VAR_NAME |
| 39 | +# define ROS_SECURITY_STRATEGY_VAR_NAME "ROS_SECURITY_STRATEGY" |
| 40 | +#endif |
| 41 | + |
| 42 | +#ifndef ROS_SECURITY_ENABLE_VAR_NAME |
| 43 | +# define ROS_SECURITY_ENABLE_VAR_NAME "ROS_SECURITY_ENABLE" |
| 44 | +#endif |
| 45 | + |
| 46 | +/// Initialize security options from values in the environment variables and given names. |
| 47 | +/** |
| 48 | + * Initialize the given security options based on the environment. |
| 49 | + * For more details: |
| 50 | + * \sa rcl_security_enabled |
| 51 | + * \sa rcl_get_enforcement_policy |
| 52 | + * \sa rcl_get_secure_root |
| 53 | + * |
| 54 | + * \param[in] name name used to find the securiy root path. |
| 55 | + * \param[in] allocator used to do allocations. |
| 56 | + * \param[out] security_options security options that will be configured according to |
| 57 | + * the environment. |
| 58 | + */ |
| 59 | +RCL_PUBLIC |
| 60 | +rcl_ret_t |
| 61 | +rcl_get_security_options_from_environment( |
| 62 | + const char * name, |
| 63 | + const rcutils_allocator_t * allocator, |
| 64 | + rmw_security_options_t * security_options); |
| 65 | + |
| 66 | +/// Check if security has to be used, according to the environment. |
| 67 | +/** |
| 68 | + * If `ROS_SECURITY_ENABLE` environment variable is set to "true", `use_security` will be set to |
| 69 | + * true. |
| 70 | + * |
| 71 | + * \param[out] use_security Must not be NULL. |
| 72 | + * \returns RCL_RET_INVALID_ARGUMENT if an argument is not valid, or |
| 73 | + * \returns RCL_RET_ERROR if an unexpected error happened, or |
| 74 | + * \returns RCL_RET_OK. |
| 75 | + */ |
| 76 | +RCL_PUBLIC |
| 77 | +rcl_ret_t |
| 78 | +rcl_security_enabled(bool * use_security); |
| 79 | + |
| 80 | +/// Get security enforcement policy from the environment. |
| 81 | +/** |
| 82 | + * Sets `policy` based on the value of `ROS_SECURITY_STRATEGY` environment variable. |
| 83 | + * If `ROS_SECURITY_STRATEGY` is "Enforce", `policy` will be `RMW_SECURITY_ENFORCEMENT_ENFORCE`. |
| 84 | + * If not, `policy` will be `RMW_SECURITY_ENFORCEMENT_PERMISSIVE`. |
| 85 | + * |
| 86 | + * \param[out] policy Must not be NULL. |
| 87 | + * \returns RCL_RET_INVALID_ARGUMENT if an argument is not valid, or |
| 88 | + * \returns RCL_RET_ERROR if an unexpected error happened, or |
| 89 | + * \returns RCL_RET_OK. |
| 90 | + */ |
| 91 | +RCL_PUBLIC |
| 92 | +rcl_ret_t |
| 93 | +rcl_get_enforcement_policy(rmw_security_enforcement_policy_t * policy); |
| 94 | + |
| 95 | +/// Return the secure root given a security context name. |
| 96 | +/** |
| 97 | + * Return the security directory associated with the security context name. |
| 98 | + * |
| 99 | + * The value of the environment variable `ROS_SECURITY_ROOT_DIRECTORY` is used as a root. |
| 100 | + * The specific directory to be used, is found from that root using the `name` passed. |
| 101 | + * E.g. for a context named "/a/b/c" and root "/r", the secure root path will be |
| 102 | + * "/r/a/b/c", where the delimiter "/" is native for target file system (e.g. "\\" for _WIN32). |
| 103 | + * |
| 104 | + * However, this expansion can be overridden by setting the secure directory override environment |
| 105 | + * (`ROS_SECURITY_DIRECTORY_OVERRIDE`) variable, allowing users to explicitly specify the exact secure |
| 106 | + * root directory to be utilized. |
| 107 | + * Such an override is useful for applications where the security context is non-deterministic |
| 108 | + * before runtime, or when testing and using additional tools that may not otherwise be easily |
| 109 | + * provisioned. |
| 110 | + * |
| 111 | + * \param[in] name validated name (a single token) |
| 112 | + * \param[in] allocator the allocator to use for allocation |
| 113 | + * \returns Machine specific (absolute) secure root path or NULL on failure. |
| 114 | + * Returned pointer must be deallocated by the caller of this function |
| 115 | + */ |
| 116 | +RCL_PUBLIC |
| 117 | +char * |
| 118 | +rcl_get_secure_root(const char * name, const rcl_allocator_t * allocator); |
| 119 | + |
| 120 | +#ifdef __cplusplus |
| 121 | +} |
| 122 | +#endif |
| 123 | + |
| 124 | +#endif // RCL__SECURITY_H_ |
0 commit comments