1 /*
   2  * Copyright (c) 1999, 2015, 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 com.sun.media.sound;
  27 
  28 import sun.misc.ManagedLocalsThread;
  29 
  30 import java.io.BufferedInputStream;
  31 import java.io.InputStream;
  32 import java.io.File;
  33 import java.io.FileInputStream;
  34 
  35 import java.util.ArrayList;
  36 import java.util.Iterator;
  37 import java.util.List;
  38 import java.util.Properties;
  39 import java.util.ServiceLoader;
  40 
  41 import java.security.AccessController;
  42 import java.security.PrivilegedAction;
  43 
  44 import javax.sound.sampled.AudioPermission;
  45 
  46 /** Managing security in the Java Sound implementation.
  47  * This class contains all code that uses and is used by
  48  * SecurityManager.doPrivileged().
  49  *
  50  * @author Matthias Pfisterer
  51  */
  52 final class JSSecurityManager {
  53 
  54     /** Prevent instantiation.
  55      */
  56     private JSSecurityManager() {
  57     }
  58 
  59     /** Checks if the VM currently has a SecurityManager installed.
  60      * Note that this may change over time. So the result of this method
  61      * should not be cached.
  62      *
  63      * @return true if a SecurityManger is installed, false otherwise.
  64      */
  65     private static boolean hasSecurityManager() {
  66         return (System.getSecurityManager() != null);
  67     }
  68 
  69 
  70     static void checkRecordPermission() throws SecurityException {
  71         if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
  72         SecurityManager sm = System.getSecurityManager();
  73         if (sm != null) {
  74             sm.checkPermission(new AudioPermission("record"));
  75         }
  76     }
  77 
  78     /** Load properties from a file.
  79         This method tries to load properties from the filename give into
  80         the passed properties object.
  81         If the file cannot be found or something else goes wrong,
  82         the method silently fails.
  83         @param properties The properties bundle to store the values of the
  84         properties file.
  85         @param filename The filename of the properties file to load. This
  86         filename is interpreted as relative to the subdirectory "conf" in
  87         the JRE directory.
  88      */
  89     static void loadProperties(final Properties properties,
  90                                final String filename) {
  91         if(hasSecurityManager()) {
  92             try {
  93                 // invoke the privileged action using 1.2 security
  94                 PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
  95                         public Void run() {
  96                             loadPropertiesImpl(properties, filename);
  97                             return null;
  98                         }
  99                     };
 100                 AccessController.doPrivileged(action);
 101                 if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
 102             } catch (Exception e) {
 103                 if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
 104                 // try without using JDK 1.2 security
 105                 loadPropertiesImpl(properties, filename);
 106             }
 107         } else {
 108             // not JDK 1.2 security, assume we already have permission
 109             loadPropertiesImpl(properties, filename);
 110         }
 111     }
 112 
 113 
 114     private static void loadPropertiesImpl(Properties properties,
 115                                            String filename) {
 116         if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
 117         String fname = System.getProperty("java.home");
 118         try {
 119             if (fname == null) {
 120                 throw new Error("Can't find java.home ??");
 121             }
 122             File f = new File(fname, "conf");
 123             f = new File(f, filename);
 124             fname = f.getCanonicalPath();
 125             InputStream in = new FileInputStream(fname);
 126             BufferedInputStream bin = new BufferedInputStream(in);
 127             try {
 128                 properties.load(bin);
 129             } finally {
 130                 if (in != null) {
 131                     in.close();
 132                 }
 133             }
 134         } catch (Throwable t) {
 135             if (Printer.trace) {
 136                 System.err.println("Could not load properties file \"" + fname + "\"");
 137                 t.printStackTrace();
 138             }
 139         }
 140         if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed");
 141     }
 142 
 143     /** Create a Thread in the current ThreadGroup.
 144      */
 145     static Thread createThread(final Runnable runnable,
 146                                final String threadName,
 147                                final boolean isDaemon, final int priority,
 148                                final boolean doStart) {
 149         Thread thread = new ManagedLocalsThread(runnable);
 150 
 151         if (threadName != null) {
 152             thread.setName(threadName);
 153         }
 154         thread.setDaemon(isDaemon);
 155         if (priority >= 0) {
 156             thread.setPriority(priority);
 157         }
 158         if (doStart) {
 159             thread.start();
 160         }
 161         return thread;
 162     }
 163 
 164     static synchronized <T> List<T> getProviders(final Class<T> providerClass) {
 165         List<T> p = new ArrayList<>(7);
 166         // ServiceLoader creates "lazy" iterator instance, but it ensures that
 167         // next/hasNext run with permissions that are restricted by whatever
 168         // creates the ServiceLoader instance, so it requires to be called from
 169         // privileged section
 170         final PrivilegedAction<Iterator<T>> psAction =
 171                 new PrivilegedAction<Iterator<T>>() {
 172                     @Override
 173                     public Iterator<T> run() {
 174                         return ServiceLoader.load(providerClass).iterator();
 175                     }
 176                 };
 177         final Iterator<T> ps = AccessController.doPrivileged(psAction);
 178 
 179         // the iterator's hasNext() method looks through classpath for
 180         // the provider class names, so it requires read permissions
 181         PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
 182             public Boolean run() {
 183                 return ps.hasNext();
 184             }
 185         };
 186 
 187         while (AccessController.doPrivileged(hasNextAction)) {
 188             try {
 189                 // the iterator's next() method creates instances of the
 190                 // providers and it should be called in the current security
 191                 // context
 192                 T provider = ps.next();
 193                 if (providerClass.isInstance(provider)) {
 194                     // $$mp 2003-08-22
 195                     // Always adding at the beginning reverses the
 196                     // order of the providers. So we no longer have
 197                     // to do this in AudioSystem and MidiSystem.
 198                     p.add(0, provider);
 199                 }
 200             } catch (Throwable t) {
 201                 //$$fb 2002-11-07: do not fail on SPI not found
 202                 if (Printer.err) t.printStackTrace();
 203             }
 204         }
 205         return p;
 206     }
 207 }