| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.data.update.task; |
| 19 | |
|
| 20 | |
import java.io.File; |
| 21 | |
import java.io.FileInputStream; |
| 22 | |
import java.io.FileNotFoundException; |
| 23 | |
import java.io.FileOutputStream; |
| 24 | |
import java.io.IOException; |
| 25 | |
import java.net.URL; |
| 26 | |
import java.util.concurrent.Callable; |
| 27 | |
import java.util.concurrent.ExecutorService; |
| 28 | |
import java.util.concurrent.Future; |
| 29 | |
import java.util.logging.Level; |
| 30 | |
import java.util.logging.Logger; |
| 31 | |
import java.util.zip.GZIPInputStream; |
| 32 | |
import org.apache.commons.io.FileUtils; |
| 33 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 34 | |
import org.owasp.dependencycheck.data.update.NvdCveInfo; |
| 35 | |
import org.owasp.dependencycheck.data.update.exception.UpdateException; |
| 36 | |
import org.owasp.dependencycheck.utils.DownloadFailedException; |
| 37 | |
import org.owasp.dependencycheck.utils.Downloader; |
| 38 | |
import org.owasp.dependencycheck.utils.Settings; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public class DownloadTask implements Callable<Future<ProcessTask>> { |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 1 | private static final Logger LOGGER = Logger.getLogger(DownloadTask.class.getName()); |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 1 | public DownloadTask(NvdCveInfo nvdCveInfo, ExecutorService processor, CveDB cveDB, Settings settings) throws UpdateException { |
| 63 | 1 | this.nvdCveInfo = nvdCveInfo; |
| 64 | 1 | this.processorService = processor; |
| 65 | 1 | this.cveDB = cveDB; |
| 66 | 1 | this.settings = settings; |
| 67 | |
|
| 68 | |
final File file1; |
| 69 | |
final File file2; |
| 70 | |
|
| 71 | |
try { |
| 72 | 1 | file1 = File.createTempFile("cve" + nvdCveInfo.getId() + "_", ".xml", Settings.getTempDirectory()); |
| 73 | 1 | file2 = File.createTempFile("cve_1_2_" + nvdCveInfo.getId() + "_", ".xml", Settings.getTempDirectory()); |
| 74 | 0 | } catch (IOException ex) { |
| 75 | 0 | throw new UpdateException("Unable to create temporary files", ex); |
| 76 | 1 | } |
| 77 | 1 | this.first = file1; |
| 78 | 1 | this.second = file2; |
| 79 | |
|
| 80 | 1 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
private CveDB cveDB; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private ExecutorService processorService; |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private NvdCveInfo nvdCveInfo; |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
private Settings settings; |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public NvdCveInfo getNvdCveInfo() { |
| 104 | 0 | return nvdCveInfo; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public void setNvdCveInfo(NvdCveInfo nvdCveInfo) { |
| 113 | 0 | this.nvdCveInfo = nvdCveInfo; |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private File first; |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public File getFirst() { |
| 126 | 0 | return first; |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public void setFirst(File first) { |
| 135 | 0 | this.first = first; |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
private File second; |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public File getSecond() { |
| 148 | 0 | return second; |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public void setSecond(File second) { |
| 157 | 0 | this.second = second; |
| 158 | 0 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | 1 | private Exception exception = null; |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
public Exception getException() { |
| 170 | 0 | return exception; |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public boolean hasException() { |
| 179 | 0 | return exception != null; |
| 180 | |
} |
| 181 | |
|
| 182 | |
@Override |
| 183 | |
public Future<ProcessTask> call() throws Exception { |
| 184 | |
try { |
| 185 | 1 | Settings.setInstance(settings); |
| 186 | 1 | final URL url1 = new URL(nvdCveInfo.getUrl()); |
| 187 | 1 | final URL url2 = new URL(nvdCveInfo.getOldSchemaVersionUrl()); |
| 188 | 1 | String msg = String.format("Download Started for NVD CVE - %s", nvdCveInfo.getId()); |
| 189 | 1 | LOGGER.log(Level.INFO, msg); |
| 190 | |
try { |
| 191 | 1 | Downloader.fetchFile(url1, first); |
| 192 | 1 | Downloader.fetchFile(url2, second); |
| 193 | 0 | } catch (DownloadFailedException ex) { |
| 194 | 0 | msg = String.format("Download Failed for NVD CVE - %s%nSome CVEs may not be reported.", nvdCveInfo.getId()); |
| 195 | 0 | LOGGER.log(Level.WARNING, msg); |
| 196 | 0 | if (Settings.getString(Settings.KEYS.PROXY_SERVER) == null) { |
| 197 | 0 | LOGGER.log(Level.INFO, |
| 198 | |
"If you are behind a proxy you may need to configure dependency-check to use the proxy."); |
| 199 | |
} |
| 200 | 0 | LOGGER.log(Level.FINE, null, ex); |
| 201 | 0 | return null; |
| 202 | 1 | } |
| 203 | 1 | if (url1.toExternalForm().endsWith(".xml.gz")) { |
| 204 | 1 | extractGzip(first); |
| 205 | |
} |
| 206 | 1 | if (url2.toExternalForm().endsWith(".xml.gz")) { |
| 207 | 1 | extractGzip(second); |
| 208 | |
} |
| 209 | |
|
| 210 | 1 | msg = String.format("Download Complete for NVD CVE - %s", nvdCveInfo.getId()); |
| 211 | 1 | LOGGER.log(Level.INFO, msg); |
| 212 | 1 | if (this.processorService == null) { |
| 213 | 1 | return null; |
| 214 | |
} |
| 215 | 0 | final ProcessTask task = new ProcessTask(cveDB, this, settings); |
| 216 | 0 | return this.processorService.submit(task); |
| 217 | |
|
| 218 | 0 | } catch (Throwable ex) { |
| 219 | 0 | final String msg = String.format("An exception occurred downloading NVD CVE - %s%nSome CVEs may not be reported.", nvdCveInfo.getId()); |
| 220 | 0 | LOGGER.log(Level.WARNING, msg); |
| 221 | 0 | LOGGER.log(Level.FINE, "Download Task Failed", ex); |
| 222 | |
} finally { |
| 223 | 1 | Settings.cleanup(false); |
| 224 | 0 | } |
| 225 | 0 | return null; |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public void cleanup() { |
| 232 | 0 | boolean deleted = false; |
| 233 | |
try { |
| 234 | 0 | if (first != null && first.exists()) { |
| 235 | 0 | deleted = first.delete(); |
| 236 | |
} |
| 237 | |
} finally { |
| 238 | 0 | if (first != null && (first.exists() || !deleted)) { |
| 239 | 0 | first.deleteOnExit(); |
| 240 | |
} |
| 241 | |
} |
| 242 | |
try { |
| 243 | 0 | deleted = false; |
| 244 | 0 | if (second != null && second.exists()) { |
| 245 | 0 | deleted = second.delete(); |
| 246 | |
} |
| 247 | |
} finally { |
| 248 | 0 | if (second != null && (second.exists() || !deleted)) { |
| 249 | 0 | second.deleteOnExit(); |
| 250 | |
} |
| 251 | |
} |
| 252 | 0 | } |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
private void extractGzip(File file) throws FileNotFoundException, IOException { |
| 263 | 2 | final String originalPath = file.getPath(); |
| 264 | 2 | final File gzip = new File(originalPath + ".gz"); |
| 265 | 2 | if (gzip.isFile() && !gzip.delete()) { |
| 266 | 0 | gzip.deleteOnExit(); |
| 267 | |
} |
| 268 | 2 | if (!file.renameTo(gzip)) { |
| 269 | 0 | throw new IOException("Unable to rename '" + file.getPath() + "'"); |
| 270 | |
} |
| 271 | 2 | final File newfile = new File(originalPath); |
| 272 | |
|
| 273 | 2 | final byte[] buffer = new byte[4096]; |
| 274 | |
|
| 275 | 2 | GZIPInputStream cin = null; |
| 276 | 2 | FileOutputStream out = null; |
| 277 | |
try { |
| 278 | 2 | cin = new GZIPInputStream(new FileInputStream(gzip)); |
| 279 | 2 | out = new FileOutputStream(newfile); |
| 280 | |
|
| 281 | |
int len; |
| 282 | 669 | while ((len = cin.read(buffer)) > 0) { |
| 283 | 667 | out.write(buffer, 0, len); |
| 284 | |
} |
| 285 | |
} finally { |
| 286 | 2 | if (cin != null) { |
| 287 | |
try { |
| 288 | 2 | cin.close(); |
| 289 | 0 | } catch (IOException ex) { |
| 290 | 0 | LOGGER.log(Level.FINEST, "ignore", ex); |
| 291 | 2 | } |
| 292 | |
} |
| 293 | 2 | if (out != null) { |
| 294 | |
try { |
| 295 | 2 | out.close(); |
| 296 | 0 | } catch (IOException ex) { |
| 297 | 0 | LOGGER.log(Level.FINEST, "ignore", ex); |
| 298 | 2 | } |
| 299 | |
} |
| 300 | 2 | if (gzip.isFile()) { |
| 301 | 2 | FileUtils.deleteQuietly(gzip); |
| 302 | |
} |
| 303 | |
} |
| 304 | 2 | } |
| 305 | |
} |