| 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 org.owasp.dependencycheck.data.nvdcve.InvalidDataException; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileNotFoundException; |
| 24 | |
import java.io.IOException; |
| 25 | |
import javax.xml.parsers.ParserConfigurationException; |
| 26 | |
import org.xml.sax.SAXException; |
| 27 | |
import java.net.MalformedURLException; |
| 28 | |
import java.net.URL; |
| 29 | |
import java.sql.SQLException; |
| 30 | |
import java.util.Calendar; |
| 31 | |
import java.util.Date; |
| 32 | |
import java.util.logging.Level; |
| 33 | |
import java.util.logging.Logger; |
| 34 | |
import org.owasp.dependencycheck.data.UpdateException; |
| 35 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 36 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 37 | |
import org.owasp.dependencycheck.utils.Downloader; |
| 38 | |
import org.owasp.dependencycheck.utils.Settings; |
| 39 | |
import org.owasp.dependencycheck.data.nvdcve.DatabaseException; |
| 40 | |
import org.owasp.dependencycheck.utils.InvalidSettingException; |
| 41 | |
import static org.owasp.dependencycheck.data.update.DataStoreMetaInfo.MODIFIED; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class StandardUpdateTask extends AbstractUpdateTask { |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public StandardUpdateTask(DataStoreMetaInfo properties) throws MalformedURLException, DownloadFailedException, UpdateException { |
| 61 | 0 | super(properties); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
@Override |
| 72 | |
public void update() throws UpdateException { |
| 73 | |
try { |
| 74 | 0 | int maxUpdates = 0; |
| 75 | 0 | for (NvdCveInfo cve : getUpdateable()) { |
| 76 | 0 | if (cve.getNeedsUpdate()) { |
| 77 | 0 | maxUpdates += 1; |
| 78 | |
} |
| 79 | |
} |
| 80 | 0 | if (maxUpdates > 3) { |
| 81 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, |
| 82 | |
"NVD CVE requires several updates; this could take a couple of minutes."); |
| 83 | |
} |
| 84 | 0 | if (maxUpdates > 0) { |
| 85 | 0 | openDataStores(); |
| 86 | |
} |
| 87 | |
|
| 88 | 0 | int count = 0; |
| 89 | 0 | for (NvdCveInfo cve : getUpdateable()) { |
| 90 | 0 | if (cve.getNeedsUpdate()) { |
| 91 | 0 | count += 1; |
| 92 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, |
| 93 | |
"Updating NVD CVE ({0} of {1})", new Object[]{count, maxUpdates}); |
| 94 | 0 | URL url = new URL(cve.getUrl()); |
| 95 | 0 | File outputPath = null; |
| 96 | 0 | File outputPath12 = null; |
| 97 | |
try { |
| 98 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, |
| 99 | |
"Downloading {0}", cve.getUrl()); |
| 100 | 0 | outputPath = File.createTempFile("cve" + cve.getId() + "_", ".xml"); |
| 101 | 0 | Downloader.fetchFile(url, outputPath); |
| 102 | |
|
| 103 | 0 | url = new URL(cve.getOldSchemaVersionUrl()); |
| 104 | 0 | outputPath12 = File.createTempFile("cve_1_2_" + cve.getId() + "_", ".xml"); |
| 105 | 0 | Downloader.fetchFile(url, outputPath12); |
| 106 | |
|
| 107 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, |
| 108 | |
"Processing {0}", cve.getUrl()); |
| 109 | |
|
| 110 | 0 | importXML(outputPath, outputPath12); |
| 111 | |
|
| 112 | 0 | getCveDB().commit(); |
| 113 | 0 | getCpeIndex().commit(); |
| 114 | 0 | getProperties().save(cve); |
| 115 | |
|
| 116 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.INFO, |
| 117 | |
"Completed update {0} of {1}", new Object[]{count, maxUpdates}); |
| 118 | 0 | } catch (FileNotFoundException ex) { |
| 119 | 0 | throw new UpdateException(ex); |
| 120 | 0 | } catch (ParserConfigurationException ex) { |
| 121 | 0 | throw new UpdateException(ex); |
| 122 | 0 | } catch (SAXException ex) { |
| 123 | 0 | throw new UpdateException(ex); |
| 124 | 0 | } catch (IOException ex) { |
| 125 | 0 | throw new UpdateException(ex); |
| 126 | 0 | } catch (SQLException ex) { |
| 127 | 0 | throw new UpdateException(ex); |
| 128 | 0 | } catch (DatabaseException ex) { |
| 129 | 0 | throw new UpdateException(ex); |
| 130 | 0 | } catch (ClassNotFoundException ex) { |
| 131 | 0 | throw new UpdateException(ex); |
| 132 | |
} finally { |
| 133 | 0 | boolean deleted = false; |
| 134 | |
try { |
| 135 | 0 | if (outputPath != null && outputPath.exists()) { |
| 136 | 0 | deleted = outputPath.delete(); |
| 137 | |
} |
| 138 | |
} finally { |
| 139 | 0 | if (outputPath != null && (outputPath.exists() || !deleted)) { |
| 140 | 0 | outputPath.deleteOnExit(); |
| 141 | |
} |
| 142 | |
} |
| 143 | |
try { |
| 144 | 0 | deleted = false; |
| 145 | 0 | if (outputPath12 != null && outputPath12.exists()) { |
| 146 | 0 | deleted = outputPath12.delete(); |
| 147 | |
} |
| 148 | |
} finally { |
| 149 | 0 | if (outputPath12 != null && (outputPath12.exists() || !deleted)) { |
| 150 | 0 | outputPath12.deleteOnExit(); |
| 151 | |
} |
| 152 | |
} |
| 153 | 0 | } |
| 154 | 0 | } |
| 155 | |
} |
| 156 | 0 | if (maxUpdates >= 1) { |
| 157 | 0 | getProperties().save(getUpdateable().get(MODIFIED)); |
| 158 | 0 | getCveDB().cleanupDatabase(); |
| 159 | |
} |
| 160 | 0 | } catch (MalformedURLException ex) { |
| 161 | 0 | throw new UpdateException(ex); |
| 162 | |
} finally { |
| 163 | 0 | closeDataStores(); |
| 164 | 0 | } |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
@Override |
| 182 | |
protected Updateable updatesNeeded() throws MalformedURLException, DownloadFailedException, UpdateException { |
| 183 | 0 | Updateable updates = null; |
| 184 | |
try { |
| 185 | 0 | updates = retrieveCurrentTimestampsFromWeb(); |
| 186 | 0 | } catch (InvalidDataException ex) { |
| 187 | 0 | final String msg = "Unable to retrieve valid timestamp from nvd cve downloads page"; |
| 188 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.FINE, msg, ex); |
| 189 | 0 | throw new DownloadFailedException(msg, ex); |
| 190 | 0 | } catch (InvalidSettingException ex) { |
| 191 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.FINE, "Invalid setting found when retrieving timestamps", ex); |
| 192 | 0 | throw new DownloadFailedException("Invalid settings", ex); |
| 193 | 0 | } |
| 194 | |
|
| 195 | 0 | if (updates == null) { |
| 196 | 0 | throw new DownloadFailedException("Unable to retrieve the timestamps of the currently published NVD CVE data"); |
| 197 | |
} |
| 198 | 0 | final DataStoreMetaInfo properties = getProperties(); |
| 199 | 0 | if (!properties.isEmpty()) { |
| 200 | |
try { |
| 201 | |
float version; |
| 202 | |
|
| 203 | 0 | if (properties.getProperty("version") == null) { |
| 204 | 0 | setDeleteAndRecreate(true); |
| 205 | |
} else { |
| 206 | |
try { |
| 207 | 0 | version = Float.parseFloat(properties.getProperty("version")); |
| 208 | 0 | final float currentVersion = Float.parseFloat(CveDB.DB_SCHEMA_VERSION); |
| 209 | 0 | if (currentVersion > version) { |
| 210 | 0 | setDeleteAndRecreate(true); |
| 211 | |
} |
| 212 | 0 | } catch (NumberFormatException ex) { |
| 213 | 0 | setDeleteAndRecreate(true); |
| 214 | 0 | } |
| 215 | |
} |
| 216 | |
|
| 217 | 0 | if (shouldDeleteAndRecreate()) { |
| 218 | 0 | return updates; |
| 219 | |
} |
| 220 | |
|
| 221 | 0 | final long lastUpdated = Long.parseLong(properties.getProperty(DataStoreMetaInfo.LAST_UPDATED, "0")); |
| 222 | 0 | final Date now = new Date(); |
| 223 | 0 | final int days = Settings.getInt(Settings.KEYS.CVE_MODIFIED_VALID_FOR_DAYS, 7); |
| 224 | 0 | if (lastUpdated == updates.getTimeStamp(MODIFIED)) { |
| 225 | 0 | updates.clear(); |
| 226 | 0 | } else if (withinRange(lastUpdated, now.getTime(), days)) { |
| 227 | 0 | for (NvdCveInfo entry : updates) { |
| 228 | 0 | if (MODIFIED.equals(entry.getId())) { |
| 229 | 0 | entry.setNeedsUpdate(true); |
| 230 | |
} else { |
| 231 | 0 | entry.setNeedsUpdate(false); |
| 232 | |
} |
| 233 | |
} |
| 234 | |
} else { |
| 235 | 0 | for (NvdCveInfo entry : updates) { |
| 236 | 0 | if (MODIFIED.equals(entry.getId())) { |
| 237 | 0 | entry.setNeedsUpdate(true); |
| 238 | |
} else { |
| 239 | 0 | long currentTimestamp = 0; |
| 240 | |
try { |
| 241 | 0 | currentTimestamp = Long.parseLong(properties.getProperty(DataStoreMetaInfo.LAST_UPDATED_BASE + entry.getId(), "0")); |
| 242 | 0 | } catch (NumberFormatException ex) { |
| 243 | 0 | final String msg = String.format("Error parsing '%s' '%s' from nvdcve.lastupdated", |
| 244 | |
DataStoreMetaInfo.LAST_UPDATED_BASE, entry.getId()); |
| 245 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.FINE, msg, ex); |
| 246 | 0 | } |
| 247 | 0 | if (currentTimestamp == entry.getTimestamp()) { |
| 248 | 0 | entry.setNeedsUpdate(false); |
| 249 | |
} |
| 250 | 0 | } |
| 251 | |
} |
| 252 | |
} |
| 253 | 0 | } catch (NumberFormatException ex) { |
| 254 | 0 | final String msg = "An invalid schema version or timestamp exists in the data.properties file."; |
| 255 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.WARNING, msg); |
| 256 | 0 | Logger.getLogger(StandardUpdateTask.class.getName()).log(Level.FINE, null, ex); |
| 257 | 0 | } |
| 258 | |
} |
| 259 | 0 | return updates; |
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
private Updateable retrieveCurrentTimestampsFromWeb() |
| 275 | |
throws MalformedURLException, DownloadFailedException, InvalidDataException, InvalidSettingException { |
| 276 | |
|
| 277 | 0 | final Updateable updates = new Updateable(); |
| 278 | 0 | updates.add(MODIFIED, Settings.getString(Settings.KEYS.CVE_MODIFIED_20_URL), |
| 279 | |
Settings.getString(Settings.KEYS.CVE_MODIFIED_12_URL), |
| 280 | |
false); |
| 281 | |
|
| 282 | 0 | final int start = Settings.getInt(Settings.KEYS.CVE_START_YEAR); |
| 283 | 0 | final int end = Calendar.getInstance().get(Calendar.YEAR); |
| 284 | 0 | final String baseUrl20 = Settings.getString(Settings.KEYS.CVE_SCHEMA_2_0); |
| 285 | 0 | final String baseUrl12 = Settings.getString(Settings.KEYS.CVE_SCHEMA_1_2); |
| 286 | 0 | for (int i = start; i <= end; i++) { |
| 287 | 0 | updates.add(Integer.toString(i), String.format(baseUrl20, i), |
| 288 | |
String.format(baseUrl12, i), |
| 289 | |
true); |
| 290 | |
} |
| 291 | |
|
| 292 | 0 | return updates; |
| 293 | |
} |
| 294 | |
} |