policy.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. *
  3. * Copyright (c) 2016-2018, Suleyman POYRAZ (AquilaNipalensis)
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use, copy,
  9. * modify, merge, publish, distribute, sublicense, and/or sell copies
  10. * of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "log.h"
  27. #include "bus.h"
  28. #include "script.h"
  29. #include "policy.h"
  30. //! Check if sender is allowed to call method
  31. int
  32. policy_check(const char *sender, const char *action, int *result)
  33. {
  34. /*!
  35. *
  36. * @sender Bus name of the sender
  37. * @result PK result
  38. * @return 0 on success, 1 on error
  39. */
  40. DBusConnection *conn;
  41. DBusError err;
  42. int uid = -1;
  43. *result = POLICY_NO;
  44. dbus_error_init(&err);
  45. conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
  46. if (dbus_error_is_set(&err)) {
  47. log_error("Unable to open DBus connection to query PolicyKit: %s\n", err.message);
  48. dbus_error_free(&err);
  49. return -1;
  50. }
  51. // If UID is 0, don't query PolicyKit
  52. uid = dbus_bus_get_unix_user(conn, sender, &err);
  53. if (dbus_error_is_set(&err)) {
  54. log_error("Unable to get caller UID: %s\n", err.message);
  55. dbus_error_free(&err);
  56. return -1;
  57. }
  58. if (uid == 0) {
  59. *result = POLICY_YES;
  60. return 0;
  61. }
  62. PyObject *subject = PyTuple_New(2);
  63. PyTuple_SetItem(subject, 0, PyBytes_FromString("system-bus-name"));
  64. PyObject *details = PyDict_New();
  65. PyDict_SetItemString(details, "name", PyBytes_FromString(sender));
  66. PyTuple_SetItem(subject, 1, details);
  67. PyObject *details2 = PyDict_New();
  68. PyObject *obj = PyTuple_New(5);
  69. PyTuple_SetItem(obj, 0, subject);
  70. PyTuple_SetItem(obj, 1, PyBytes_FromString(action));
  71. PyTuple_SetItem(obj, 2, details2);
  72. PyTuple_SetItem(obj, 3, PyLong_FromLong((long) 1));
  73. PyTuple_SetItem(obj, 4, PyBytes_FromString("abc"));
  74. PyObject *ret = bus_execute2(conn, "org.freedesktop.PolicyKit1", "/org/freedesktop/PolicyKit1/Authority", "org.freedesktop.PolicyKit1.Authority", "CheckAuthorization", obj, -1, "(sa{sv})sa{ss}us");
  75. if (!ret) {
  76. char *eStr, *vStr;
  77. py_catch(&eStr, &vStr, 1);
  78. *result = POLICY_NO;
  79. return 0;
  80. }
  81. if (PyTuple_GetItem(ret, 0) == Py_True) {
  82. *result = POLICY_YES;
  83. }
  84. return 0;
  85. }