PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/helpers/creds-self/creds-self.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 125 lines | 87 code | 16 blank | 22 comment | 17 complexity | 43d4f479646c0940c5c2322a5a639c33 MD5 | raw file
  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file src/helpers/creds-self/creds-self.cpp
  18. * @author Zofia Abramowska <z.abramowska@samsung.com>
  19. * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  20. * @version 1.0
  21. * @brief Implementation of external libcynara-creds-self API
  22. */
  23. #include <cstring>
  24. #include <fstream>
  25. #include <string>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <attributes/attributes.h>
  29. #include <exceptions/TryCatch.h>
  30. #include <log/log.h>
  31. #include <cynara-creds-self.h>
  32. namespace {
  33. int copyStr(char **client, const std::string &str) {
  34. char *clientTmp = strdup(str.c_str());
  35. if (!clientTmp) {
  36. LOGE("strdup failed");
  37. return CYNARA_API_OUT_OF_MEMORY;
  38. }
  39. *client = clientTmp;
  40. return CYNARA_API_SUCCESS;
  41. }
  42. int getSelfSmackLabel(char **client) {
  43. return Cynara::tryCatch([&client](){
  44. std::ifstream current("/proc/self/attr/current");
  45. if (!current) {
  46. LOGE("Couldn't open proc file");
  47. return CYNARA_API_UNKNOWN_ERROR;
  48. }
  49. std::string label;
  50. current >> label;
  51. if (!current) {
  52. LOGE("Couldn't read label from file");
  53. return CYNARA_API_UNKNOWN_ERROR;
  54. }
  55. return copyStr(client, label);
  56. });
  57. }
  58. int getSelfPid(char **client) {
  59. return Cynara::tryCatch([&client](){
  60. return copyStr(client, std::to_string(getpid()));
  61. });
  62. }
  63. int getSelfUid(char **client) {
  64. return Cynara::tryCatch([&client](){
  65. return copyStr(client, std::to_string(getuid()));
  66. });
  67. }
  68. int getSelfGid(char **client) {
  69. return Cynara::tryCatch([&client](){
  70. return copyStr(client, std::to_string(getgid()));
  71. });
  72. }
  73. } // namespace anonymous
  74. CYNARA_API
  75. int cynara_creds_self_get_client(enum cynara_client_creds method, char **client) {
  76. if (client == nullptr)
  77. return CYNARA_API_INVALID_PARAM;
  78. if (method == cynara_client_creds::CLIENT_METHOD_DEFAULT) {
  79. int ret = cynara_creds_get_default_client_method(&method);
  80. if (ret != CYNARA_API_SUCCESS)
  81. return ret;
  82. }
  83. switch (method) {
  84. case cynara_client_creds::CLIENT_METHOD_SMACK:
  85. return getSelfSmackLabel(client);
  86. case cynara_client_creds::CLIENT_METHOD_PID:
  87. return getSelfPid(client);
  88. default:
  89. return CYNARA_API_METHOD_NOT_SUPPORTED;
  90. }
  91. }
  92. CYNARA_API
  93. int cynara_creds_self_get_user(enum cynara_user_creds method, char **user) {
  94. if (user == nullptr)
  95. return CYNARA_API_INVALID_PARAM;
  96. if (method == cynara_user_creds::USER_METHOD_DEFAULT) {
  97. int ret = cynara_creds_get_default_user_method(&method);
  98. if (ret != CYNARA_API_SUCCESS)
  99. return ret;
  100. }
  101. switch (method) {
  102. case cynara_user_creds::USER_METHOD_UID:
  103. return getSelfUid(user);
  104. case cynara_user_creds::USER_METHOD_GID:
  105. return getSelfGid(user);
  106. default:
  107. return CYNARA_API_METHOD_NOT_SUPPORTED;
  108. }
  109. }