1 /*
   2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package build.tools.makejavasecurity;
  27 
  28 import java.io.*;
  29 import java.nio.file.Files;
  30 import java.nio.file.Paths;
  31 import java.util.*;
  32 
  33 /**
  34  * Builds the java.security file, including
  35  *
  36  * 1. Adds additional packages to the package.access and
  37  *    package.definition security properties.
  38  * 2. Filter out platform-unrelated parts
  39  *
  40  * In order to easily maintain platform-related entries, every item
  41  * (including the last line) in package.access and package.definition
  42  * MUST end with ',\'. A blank line MUST exist after the last line.
  43  */
  44 public class MakeJavaSecurity {
  45 
  46     private static final String PKG_ACC = "package.access";
  47     private static final String PKG_DEF = "package.definition";
  48     private static final int PKG_ACC_INDENT = 15;
  49     private static final int PKG_DEF_INDENT = 19;
  50 
  51     public static void main(String[] args) throws Exception {
  52 
  53         if (args.length < 3) {
  54             System.err.println("Usage: java MakeJavaSecurity " +
  55                                "[input java.security file name] " +
  56                                "[output java.security file name] " +
  57                                "[openjdk target os] " +
  58                                "[more restricted packages file name?]");
  59             System.exit(1);
  60         }
  61 
  62         // more restricted packages
  63         List<String> extraLines;
  64         if (args.length == 4) {
  65             extraLines = Files.readAllLines(Paths.get(args[3]));
  66         } else {
  67             extraLines = Collections.emptyList();
  68         }
  69 
  70         List<String> lines = new ArrayList<>();
  71 
  72         // read raw java.security and add more restricted packages
  73         try (FileReader fr = new FileReader(args[0]);
  74                 BufferedReader br = new BufferedReader(fr)) {
  75             // looking for pkg access properties
  76             String line = br.readLine();
  77             while (line != null) {
  78                 if (line.startsWith(PKG_ACC)) {
  79                     addPackages(br, lines, line, PKG_ACC_INDENT, extraLines);
  80                 } else if (line.startsWith(PKG_DEF)) {
  81                     addPackages(br, lines, line, PKG_DEF_INDENT, extraLines);
  82                 } else {
  83                     lines.add(line);
  84                 }
  85                 line = br.readLine();
  86             }
  87         }
  88 
  89         // Filter out platform-unrelated ones. We only support
  90         // #ifdef, #ifndef, and #endif.
  91         int mode = 0;   // 0: out of block, 1: in match, 2: in non-match
  92         Iterator<String> iter = lines.iterator();
  93         while (iter.hasNext()) {
  94             String line = iter.next();
  95             if (line.startsWith("#endif")) {
  96                 mode = 0;
  97                 iter.remove();
  98             } else if (line.startsWith("#ifdef ")) {
  99                 mode = line.endsWith(args[2])?1:2;
 100                 iter.remove();
 101             } else if (line.startsWith("#ifndef ")) {
 102                 mode = line.endsWith(args[2])?2:1;
 103                 iter.remove();
 104             } else {
 105                 if (mode == 2) iter.remove();
 106             }
 107         }
 108 
 109         // Update .tbd to .1, .2, etc.
 110         Map<String,Integer> count = new HashMap<>();
 111         for (int i=0; i<lines.size(); i++) {
 112             String line = lines.get(i);
 113             int index = line.indexOf(".tbd");
 114             if (index >= 0) {
 115                 String prefix = line.substring(0, index);
 116                 int n = count.getOrDefault(prefix, 1);
 117                 count.put(prefix, n+1);
 118                 lines.set(i, prefix + "." + n + line.substring(index+4));
 119             }
 120         }
 121 
 122         // Clean up the last line of PKG_ACC and PKG_DEF blocks.
 123         // Not really necessary since a blank line follows.
 124         boolean inBlock = false;
 125         for (int i=0; i<lines.size(); i++) {
 126             String line = lines.get(i);
 127             if (line.startsWith(PKG_ACC) || line.startsWith(PKG_DEF)) {
 128                 inBlock = true;
 129             }
 130             if (inBlock) {
 131                 if (line.isEmpty()) {
 132                     String lastLine = lines.get(i-1);
 133                     lines.set(i-1, lastLine.substring(0, lastLine.length()-2));
 134                     inBlock = false;
 135                 }
 136             }
 137         }
 138 
 139         Files.write(Paths.get(args[1]), lines);
 140     }
 141 
 142     private static void addPackages(BufferedReader br, List<String> lines,
 143                                     String line, int numSpaces,
 144                                     List<String> args) throws IOException {
 145         // parse property until EOL, not including line breaks
 146         boolean first = true;
 147         while (!line.isEmpty()) {
 148             if (!line.startsWith("#")) {
 149                 if (!line.endsWith(",\\") ||
 150                         (!first && line.contains("="))) {
 151                     throw new IOException("Invalid line: " + line);
 152                 }
 153             }
 154             lines.add(line);
 155             line = br.readLine();
 156             first = false;
 157         }
 158         // add new packages, one per line
 159         for (String arg: args) {
 160             if (arg.startsWith("#")) {
 161                 lines.add(arg);
 162             } else {
 163                 lines.add(String.format("%"+numSpaces+"s", "") + arg + ",\\");
 164             }
 165         }
 166         lines.add(line);
 167     }
 168 }