| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.data.update; |
| 20 | |
|
| 21 | |
import java.io.File; |
| 22 | |
import java.io.IOException; |
| 23 | |
import java.net.MalformedURLException; |
| 24 | |
import java.sql.SQLException; |
| 25 | |
import java.util.List; |
| 26 | |
import java.util.Map; |
| 27 | |
import java.util.logging.Level; |
| 28 | |
import java.util.logging.Logger; |
| 29 | |
import javax.xml.parsers.ParserConfigurationException; |
| 30 | |
import javax.xml.parsers.SAXParser; |
| 31 | |
import javax.xml.parsers.SAXParserFactory; |
| 32 | |
import org.owasp.dependencycheck.data.UpdateException; |
| 33 | |
import org.owasp.dependencycheck.data.cpe.CpeIndexWriter; |
| 34 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 35 | |
import org.owasp.dependencycheck.utils.FileUtils; |
| 36 | |
import org.owasp.dependencycheck.utils.Settings; |
| 37 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 38 | |
import org.owasp.dependencycheck.data.nvdcve.NvdCve12Handler; |
| 39 | |
import org.owasp.dependencycheck.data.nvdcve.NvdCve20Handler; |
| 40 | |
import org.owasp.dependencycheck.dependency.VulnerableSoftware; |
| 41 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 42 | |
import org.xml.sax.SAXException; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public abstract class AbstractUpdateTask implements UpdateTask { |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 6 | public AbstractUpdateTask(DataStoreMetaInfo properties) throws MalformedURLException, DownloadFailedException, UpdateException { |
| 62 | 6 | this.properties = properties; |
| 63 | 6 | this.updateable = updatesNeeded(); |
| 64 | 6 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private Updateable updateable; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 6 | private DataStoreMetaInfo properties = null; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
protected DataStoreMetaInfo getProperties() { |
| 80 | 3 | return properties; |
| 81 | |
} |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 6 | private CveDB cveDB = null; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
protected CveDB getCveDB() { |
| 93 | 0 | return cveDB; |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | 6 | private CpeIndexWriter cpeIndex = null; |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
protected CpeIndexWriter getCpeIndex() { |
| 106 | 0 | return cpeIndex; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public boolean isUpdateNeeded() { |
| 115 | 0 | return updateable.isUpdateNeeded(); |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
protected Updateable getUpdateable() { |
| 124 | 0 | return updateable; |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
protected abstract Updateable updatesNeeded() throws MalformedURLException, DownloadFailedException, UpdateException; |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public abstract void update() throws UpdateException; |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | 6 | private boolean deleteAndRecreate = false; |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public boolean shouldDeleteAndRecreate() { |
| 159 | 1 | return deleteAndRecreate; |
| 160 | |
} |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
protected void setDeleteAndRecreate(boolean deleteAndRecreate) { |
| 168 | 1 | this.deleteAndRecreate = deleteAndRecreate; |
| 169 | 1 | } |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
protected void deleteExistingData() throws IOException { |
| 177 | 3 | File data = Settings.getFile(Settings.KEYS.CVE_DATA_DIRECTORY); |
| 178 | 3 | if (data.exists()) { |
| 179 | 1 | FileUtils.delete(data); |
| 180 | |
} |
| 181 | 3 | data = Settings.getFile(Settings.KEYS.CPE_DATA_DIRECTORY); |
| 182 | 3 | if (data.exists()) { |
| 183 | 1 | FileUtils.delete(data); |
| 184 | |
} |
| 185 | 3 | data = DataStoreMetaInfo.getPropertiesFile(); |
| 186 | 3 | if (data.exists()) { |
| 187 | 1 | FileUtils.delete(data); |
| 188 | |
} |
| 189 | 3 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
protected void closeDataStores() { |
| 195 | 1 | if (cveDB != null) { |
| 196 | |
try { |
| 197 | 1 | cveDB.close(); |
| 198 | 0 | } catch (Exception ignore) { |
| 199 | 0 | Logger.getLogger(AbstractUpdateTask.class.getName()).log(Level.FINEST, "Error closing the cveDB", ignore); |
| 200 | 1 | } |
| 201 | |
} |
| 202 | 1 | if (cpeIndex != null) { |
| 203 | |
try { |
| 204 | 1 | cpeIndex.close(); |
| 205 | 0 | } catch (Exception ignore) { |
| 206 | 0 | Logger.getLogger(AbstractUpdateTask.class.getName()).log(Level.FINEST, "Error closing the cpeIndex", ignore); |
| 207 | 1 | } |
| 208 | |
} |
| 209 | 1 | } |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
protected void openDataStores() throws UpdateException { |
| 217 | |
|
| 218 | |
try { |
| 219 | 1 | cveDB = new CveDB(); |
| 220 | 1 | cveDB.open(); |
| 221 | 1 | cpeIndex = new CpeIndexWriter(); |
| 222 | 1 | cpeIndex.open(); |
| 223 | 0 | } catch (IOException ex) { |
| 224 | 0 | closeDataStores(); |
| 225 | 0 | Logger.getLogger(AbstractUpdateTask.class.getName()).log(Level.FINE, "IO Error opening databases", ex); |
| 226 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 227 | 0 | } catch (SQLException ex) { |
| 228 | 0 | closeDataStores(); |
| 229 | 0 | Logger.getLogger(AbstractUpdateTask.class.getName()).log(Level.FINE, "SQL Exception opening databases", ex); |
| 230 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 231 | 0 | } catch (DatabaseException ex) { |
| 232 | 0 | closeDataStores(); |
| 233 | 0 | Logger.getLogger(AbstractUpdateTask.class.getName()).log(Level.FINE, "Database Exception opening databases", ex); |
| 234 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 235 | 0 | } catch (ClassNotFoundException ex) { |
| 236 | 0 | closeDataStores(); |
| 237 | 0 | Logger.getLogger(AbstractUpdateTask.class.getName()).log(Level.FINE, "Class not found exception opening databases", ex); |
| 238 | 0 | throw new UpdateException("Error updating the CPE/CVE data, please see the log file for more details."); |
| 239 | 1 | } |
| 240 | 1 | } |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
protected boolean withinRange(long date, long compareTo, int range) { |
| 254 | 2 | final double differenceInDays = (compareTo - date) / 1000.0 / 60.0 / 60.0 / 24.0; |
| 255 | 2 | return differenceInDays < range; |
| 256 | |
} |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
protected void importXML(File file, File oldVersion) |
| 273 | |
throws ParserConfigurationException, SAXException, IOException, SQLException, DatabaseException, ClassNotFoundException { |
| 274 | |
|
| 275 | 0 | final SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 276 | 0 | final SAXParser saxParser = factory.newSAXParser(); |
| 277 | |
|
| 278 | 0 | final NvdCve12Handler cve12Handler = new NvdCve12Handler(); |
| 279 | 0 | saxParser.parse(oldVersion, cve12Handler); |
| 280 | 0 | final Map<String, List<VulnerableSoftware>> prevVersionVulnMap = cve12Handler.getVulnerabilities(); |
| 281 | |
|
| 282 | 0 | final NvdCve20Handler cve20Handler = new NvdCve20Handler(); |
| 283 | 0 | cve20Handler.setCveDB(cveDB); |
| 284 | 0 | cve20Handler.setPrevVersionVulnMap(prevVersionVulnMap); |
| 285 | 0 | cve20Handler.setCpeIndex(cpeIndex); |
| 286 | 0 | saxParser.parse(file, cve20Handler); |
| 287 | 0 | } |
| 288 | |
} |