1 /*
   2  * Copyright (c) 2016, 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.customizesecurityfile;
  27 
  28 import java.io.*;
  29 
  30 /**
  31  * Alters the crypto.policy security property
  32  * if --enable-unlimited-crypto is enabled.
  33  */
  34 public class CryptoLevel {
  35 
  36     private static final String PROP_NAME = "crypto.policy";
  37 
  38     public static void main(String[] args) throws Exception {
  39         boolean fileModified = false;
  40 
  41         if (args.length < 3) {
  42             System.err.println("Usage: java CryptoLevel" +
  43                                "[input java.security file name] " +
  44                                "[output java.security file name] " +
  45                                "[unlimited|limited]");
  46             System.exit(1);
  47         }
  48         if (!args[2].equals("unlimited") && !args[2].equals("limited")) {
  49             System.err.println("CryptoLevel error: Unexpected " +
  50                 "input: " + args[2]);
  51             System.exit(1);
  52         }
  53 
  54         try (FileReader fr = new FileReader(args[0]);
  55              BufferedReader br = new BufferedReader(fr);
  56              FileWriter fw = new FileWriter(args[1]);
  57              BufferedWriter bw = new BufferedWriter(fw))
  58         {
  59             // parse the file line-by-line, looking for crypto.policy
  60             String line = br.readLine();
  61             while (line != null) {
  62                 if (line.startsWith('#' + PROP_NAME) ||
  63                     line.startsWith(PROP_NAME)) {
  64                     writeLine(bw, PROP_NAME + "=" + args[2]);
  65                     fileModified = true;
  66                 } else {
  67                     writeLine(bw, line);
  68                 }
  69                 line = br.readLine();
  70             }
  71             if (!fileModified) {
  72                 //no previous setting seen. Insert at end
  73                 writeLine(bw, PROP_NAME + "=" + args[2]);
  74             }
  75             bw.flush();
  76         }
  77     }
  78 
  79     private static void writeLine(BufferedWriter bw, String line)
  80         throws IOException
  81     {
  82         bw.write(line);
  83         bw.newLine();
  84     }
  85 }