imv_os_database.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2012-2017 Andreas Steffen
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #define _GNU_SOURCE /* for stdndup() */
  16. #include <string.h>
  17. #include "imv_os_database.h"
  18. #include <utils/debug.h>
  19. typedef struct private_imv_os_database_t private_imv_os_database_t;
  20. /**
  21. * Private data of a imv_os_database_t object.
  22. *
  23. */
  24. struct private_imv_os_database_t {
  25. /**
  26. * Public imv_os_database_t interface.
  27. */
  28. imv_os_database_t public;
  29. /**
  30. * database instance
  31. */
  32. database_t *db;
  33. };
  34. METHOD(imv_os_database_t, check_packages, status_t,
  35. private_imv_os_database_t *this, imv_os_state_t *os_state,
  36. enumerator_t *package_enumerator)
  37. {
  38. imv_state_t *state;
  39. imv_session_t *session;
  40. imv_os_info_t *os_info;
  41. os_type_t os_type;
  42. char *product, *package, *release, *cur_release;
  43. chunk_t name, version;
  44. int pid, gid, security, blacklist;
  45. int count = 0, count_ok = 0, count_security = 0, count_blacklist = 0;
  46. enumerator_t *e;
  47. status_t status = SUCCESS;
  48. bool found, match;
  49. state = &os_state->interface;
  50. session = state->get_session(state);
  51. session->get_session_id(session, &pid, NULL);
  52. os_info = session->get_os_info(session);
  53. os_type = os_info->get_type(os_info);
  54. product = os_info->get_info(os_info);
  55. if (os_type == OS_TYPE_ANDROID)
  56. {
  57. /*no package dependency on Android version */
  58. product = enum_to_name(os_type_names, os_type);
  59. /* Get primary key of product */
  60. e = this->db->query(this->db,
  61. "SELECT id FROM products WHERE name = ?",
  62. DB_TEXT, product, DB_INT);
  63. if (!e)
  64. {
  65. return FAILED;
  66. }
  67. if (!e->enumerate(e, &pid))
  68. {
  69. e->destroy(e);
  70. return NOT_FOUND;
  71. }
  72. e->destroy(e);
  73. }
  74. DBG1(DBG_IMV, "processing installed '%s' packages", product);
  75. while (package_enumerator->enumerate(package_enumerator, &name, &version))
  76. {
  77. /* Convert package name chunk to a string */
  78. package = strndup(name.ptr, name.len);
  79. count++;
  80. /* Get primary key of package */
  81. e = this->db->query(this->db,
  82. "SELECT id FROM packages WHERE name = ?",
  83. DB_TEXT, package, DB_INT);
  84. if (!e)
  85. {
  86. free(package);
  87. return FAILED;
  88. }
  89. if (!e->enumerate(e, &gid))
  90. {
  91. /* package not present in database for any product - skip */
  92. DBG2(DBG_IMV, "package '%s' (%.*s) not found",
  93. package, version.len, version.ptr);
  94. free(package);
  95. e->destroy(e);
  96. continue;
  97. }
  98. e->destroy(e);
  99. /* Convert package version chunk to a string */
  100. release = strndup(version.ptr, version.len);
  101. /* Enumerate over all acceptable versions */
  102. e = this->db->query(this->db,
  103. "SELECT release, security, blacklist FROM versions "
  104. "WHERE product = ? AND package = ?",
  105. DB_INT, pid, DB_INT, gid, DB_TEXT, DB_INT, DB_INT);
  106. if (!e)
  107. {
  108. free(package);
  109. free(release);
  110. return FAILED;
  111. }
  112. found = FALSE;
  113. match = FALSE;
  114. while (e->enumerate(e, &cur_release, &security, &blacklist))
  115. {
  116. found = TRUE;
  117. if (streq(release, cur_release) || streq("*", cur_release))
  118. {
  119. match = TRUE;
  120. break;
  121. }
  122. }
  123. e->destroy(e);
  124. if (found)
  125. {
  126. if (match)
  127. {
  128. if (blacklist)
  129. {
  130. DBG1(DBG_IMV, "package '%s' (%s) is blacklisted",
  131. package, release);
  132. count_blacklist++;
  133. os_state->add_bad_package(os_state, package,
  134. OS_PACKAGE_STATE_BLACKLIST);
  135. }
  136. else if (security)
  137. {
  138. DBG1(DBG_IMV, "package '%s' (%s) is vulnerable",
  139. package, release);
  140. os_state->add_bad_package(os_state, package,
  141. OS_PACKAGE_STATE_SECURITY);
  142. count_security++;
  143. }
  144. else
  145. {
  146. DBG2(DBG_IMV, "package '%s' (%s) is ok",
  147. package, release);
  148. count_ok++;
  149. }
  150. }
  151. else
  152. {
  153. DBG1(DBG_IMV, "package '%s' (%s) no match", package, release);
  154. }
  155. }
  156. else
  157. {
  158. DBG2(DBG_IMV, "package '%s' (%s) unknown", package, release);
  159. }
  160. free(package);
  161. free(release);
  162. }
  163. os_state->set_count(os_state, count, count_security, count_blacklist,
  164. count_ok);
  165. return status;
  166. }
  167. METHOD(imv_os_database_t, destroy, void,
  168. private_imv_os_database_t *this)
  169. {
  170. free(this);
  171. }
  172. /**
  173. * See header
  174. */
  175. imv_os_database_t *imv_os_database_create(imv_database_t *imv_db)
  176. {
  177. private_imv_os_database_t *this;
  178. if (!imv_db)
  179. {
  180. return NULL;
  181. }
  182. INIT(this,
  183. .public = {
  184. .check_packages = _check_packages,
  185. .destroy = _destroy,
  186. },
  187. .db = imv_db->get_database(imv_db),
  188. );
  189. return &this->public;
  190. }