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