PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/helpers/creds-gdbus/creds-gdbus.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 134 lines | 95 code | 18 blank | 21 comment | 41 complexity | 335a9c1c9f3e464f511ce785f150149b MD5 | raw file
  1. /*
  2. * Copyright (c) 2015 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-gdbus/creds-gdbus.cpp
  18. * @author Jacek Bukarewicz <j.bukarewicz@samsung.com>
  19. * @version 1.0
  20. * @brief Implementation of external libcynara-creds-gdbus API
  21. */
  22. #include <attributes/attributes.h>
  23. #include <cynara-creds-commons.h>
  24. #include <cynara-creds-gdbus.h>
  25. #include <cynara-error.h>
  26. namespace {
  27. int call_dbus_daemon_method_str(GDBusConnection *connection, const gchar *methodName,
  28. const gchar *arg, gchar **result) {
  29. GVariant *reply = g_dbus_connection_call_sync(connection,
  30. "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
  31. methodName, g_variant_new("(s)", arg), G_VARIANT_TYPE("(s)"),
  32. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
  33. if (reply != NULL) {
  34. g_variant_get(reply, "(s)", result);
  35. g_variant_unref(reply);
  36. return CYNARA_API_SUCCESS;
  37. } else {
  38. return CYNARA_API_UNKNOWN_ERROR;
  39. }
  40. }
  41. int call_dbus_daemon_method_u32(GDBusConnection *connection, const gchar *methodName,
  42. const gchar *arg, guint32 *result) {
  43. GVariant *reply = g_dbus_connection_call_sync(connection,
  44. "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
  45. methodName, g_variant_new("(s)", arg), G_VARIANT_TYPE("(u)"),
  46. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
  47. if (reply != NULL) {
  48. g_variant_get(reply, "(u)", result);
  49. g_variant_unref(reply);
  50. return CYNARA_API_SUCCESS;
  51. } else {
  52. return CYNARA_API_UNKNOWN_ERROR;
  53. }
  54. }
  55. }
  56. CYNARA_API
  57. int cynara_creds_gdbus_get_client(GDBusConnection *connection, const gchar *uniqueName,
  58. enum cynara_client_creds method, gchar **client) {
  59. int ret;
  60. if (connection == nullptr || uniqueName == nullptr || client == nullptr)
  61. return CYNARA_API_INVALID_PARAM;
  62. if (method == cynara_client_creds::CLIENT_METHOD_DEFAULT) {
  63. int ret = cynara_creds_get_default_client_method(&method);
  64. if (ret != CYNARA_API_SUCCESS)
  65. return ret;
  66. }
  67. switch (method) {
  68. case cynara_client_creds::CLIENT_METHOD_SMACK:
  69. ret = call_dbus_daemon_method_str(connection, "GetConnectionSmackContext", uniqueName,
  70. client);
  71. break;
  72. case cynara_client_creds::CLIENT_METHOD_PID:
  73. {
  74. guint32 pid;
  75. ret = call_dbus_daemon_method_u32(connection, "GetConnectionUnixProcessID",
  76. uniqueName, &pid);
  77. if (ret == CYNARA_API_SUCCESS)
  78. *client = g_strdup_printf("%u", pid);
  79. break;
  80. }
  81. default:
  82. return CYNARA_API_METHOD_NOT_SUPPORTED;
  83. }
  84. return ret;
  85. }
  86. CYNARA_API
  87. int cynara_creds_gdbus_get_user(GDBusConnection *connection, const gchar *uniqueName,
  88. enum cynara_user_creds method, gchar **user) {
  89. if (connection == nullptr || uniqueName == nullptr || user == nullptr)
  90. return CYNARA_API_INVALID_PARAM;
  91. if (method == cynara_user_creds::USER_METHOD_DEFAULT) {
  92. int ret = cynara_creds_get_default_user_method(&method);
  93. if (ret != CYNARA_API_SUCCESS)
  94. return ret;
  95. }
  96. if (method != cynara_user_creds::USER_METHOD_UID)
  97. return CYNARA_API_METHOD_NOT_SUPPORTED;
  98. guint32 uid;
  99. int ret = call_dbus_daemon_method_u32(connection, "GetConnectionUnixUser", uniqueName, &uid);
  100. if (ret == CYNARA_API_SUCCESS) {
  101. *user = g_strdup_printf("%u", uid);
  102. }
  103. return ret;
  104. }
  105. CYNARA_API
  106. int cynara_creds_gdbus_get_pid(GDBusConnection *connection, const char *uniqueName, pid_t *pid) {
  107. if (connection == nullptr || uniqueName == nullptr || pid == nullptr)
  108. return CYNARA_API_INVALID_PARAM;
  109. guint32 pidU32;
  110. int ret = call_dbus_daemon_method_u32(connection, "GetConnectionUnixProcessID", uniqueName,
  111. &pidU32);
  112. if (ret == CYNARA_API_SUCCESS) {
  113. *pid = static_cast<pid_t>(pidU32);
  114. }
  115. return ret;
  116. }