|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.fs.store.commands; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import org.apache.hadoop.fs.FileUtil; |
| 28 | +import org.apache.hadoop.fs.store.diag.DiagnosticsEntryPoint; |
| 29 | +import org.apache.hadoop.util.ToolRunner; |
| 30 | + |
| 31 | +import static org.apache.hadoop.fs.store.CommonParameters.DEFINE; |
| 32 | +import static org.apache.hadoop.fs.store.CommonParameters.VERBOSE; |
| 33 | +import static org.apache.hadoop.fs.store.CommonParameters.XMLFILE; |
| 34 | +import static org.apache.hadoop.fs.store.StoreExitCodes.E_USAGE; |
| 35 | + |
| 36 | +/** |
| 37 | + * Checks to see if the tar command is hardened by taking a command line param and trying |
| 38 | + * to untar it. |
| 39 | + */ |
| 40 | +public class TarHardened extends DiagnosticsEntryPoint { |
| 41 | + |
| 42 | + private static final Logger LOG = LoggerFactory.getLogger(TarHardened.class); |
| 43 | + |
| 44 | + public static final String USAGE |
| 45 | + = "Usage: tarhardened [filename]\n" |
| 46 | + + optusage(DEFINE, "key=value", "Define a property") |
| 47 | + + optusage(XMLFILE, "file", "XML config file to load") |
| 48 | + + optusage(VERBOSE, "verbose output"); |
| 49 | + |
| 50 | + public TarHardened() { |
| 51 | + createCommandFormat(1, 1, VERBOSE); |
| 52 | + addValueOptions(XMLFILE, DEFINE); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int run(String[] args) throws Exception { |
| 57 | + List<String> paths = parseArgs(args); |
| 58 | + if (paths.size() > 1) { |
| 59 | + // too many entries |
| 60 | + errorln(USAGE); |
| 61 | + return E_USAGE; |
| 62 | + } |
| 63 | + String filename; |
| 64 | + if (paths.isEmpty()) { |
| 65 | + File tar = File.createTempFile("tarhardened", ".tgz"); |
| 66 | + tar.delete(); |
| 67 | + filename = tar.getAbsolutePath() + "; true"; |
| 68 | + } else { |
| 69 | + filename = paths.get(0); |
| 70 | + } |
| 71 | + File tmpdir = File.createTempFile("tarhardened-dir", ""); |
| 72 | + tmpdir.delete(); |
| 73 | + final File source = new File(filename); |
| 74 | + println("Attempting to untar file with name \"%s\"", source); |
| 75 | + FileUtil.unTar(source, tmpdir); |
| 76 | + println("untar operation reported success"); |
| 77 | + println(); |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Execute the command, return the result or throw an exception, |
| 83 | + * as appropriate. |
| 84 | + * @param args argument varags. |
| 85 | + * @return return code |
| 86 | + * @throws Exception failure |
| 87 | + */ |
| 88 | + public static int exec(String... args) throws Exception { |
| 89 | + return ToolRunner.run(new TarHardened(), args); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Main entry point. Calls {@code System.exit()} on all execution paths. |
| 94 | + * @param args argument list |
| 95 | + */ |
| 96 | + public static void main(String[] args) { |
| 97 | + try { |
| 98 | + exit(exec(args), ""); |
| 99 | + } catch (Throwable e) { |
| 100 | + exitOnThrowable(e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments