|
| 1 | +/* |
| 2 | + * FB/Java plugin |
| 3 | + * |
| 4 | + * Distributable under LGPL license. |
| 5 | + * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * LGPL License for more details. |
| 11 | + * |
| 12 | + * This file was created by members of the Firebird development team. |
| 13 | + * All individual contributions remain the Copyright (C) of those |
| 14 | + * individuals. Contributors to this file are either listed here or |
| 15 | + * can be obtained from a git log command. |
| 16 | + * |
| 17 | + * All rights reserved. |
| 18 | + */ |
| 19 | +package org.firebirdsql.fbjava.impl; |
| 20 | + |
| 21 | +import java.lang.reflect.Constructor; |
| 22 | +import java.security.AccessController; |
| 23 | +import java.security.AllPermission; |
| 24 | +import java.security.Permission; |
| 25 | +import java.security.PermissionCollection; |
| 26 | +import java.security.Permissions; |
| 27 | +import java.security.Policy; |
| 28 | +import java.security.Principal; |
| 29 | +import java.security.PrivilegedAction; |
| 30 | +import java.security.ProtectionDomain; |
| 31 | +import java.sql.Connection; |
| 32 | +import java.sql.DriverManager; |
| 33 | +import java.sql.PreparedStatement; |
| 34 | +import java.sql.ResultSet; |
| 35 | +import java.sql.SQLException; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.HashSet; |
| 38 | + |
| 39 | +import javax.security.auth.Subject; |
| 40 | + |
| 41 | +import org.firebirdsql.gds.ISCConstants; |
| 42 | +import org.firebirdsql.gds.TransactionParameterBuffer; |
| 43 | +import org.firebirdsql.jdbc.FBConnection; |
| 44 | + |
| 45 | + |
| 46 | +/** |
| 47 | + * Security policy configured in a database. |
| 48 | + * |
| 49 | + * @author <a href="mailto:adrianosf@gmail.com">Adriano dos Santos Fernandes</a> |
| 50 | + */ |
| 51 | +final class DbPolicy extends Policy |
| 52 | +{ |
| 53 | + private static int count; |
| 54 | + private static String securityDatabase; |
| 55 | + private static FBConnection securityDb; |
| 56 | + private static PreparedStatement stmt; |
| 57 | + private static HashMap<String, Subject> userSubjects = new HashMap<String, Subject>(); |
| 58 | + |
| 59 | + public DbPolicy(String securityDatabase) |
| 60 | + { |
| 61 | + DbPolicy.securityDatabase = securityDatabase; |
| 62 | + } |
| 63 | + |
| 64 | + static void databaseOpened() throws SQLException |
| 65 | + { |
| 66 | + synchronized (securityDatabase) |
| 67 | + { |
| 68 | + if (count++ == 0) |
| 69 | + { |
| 70 | + assert securityDb == null; |
| 71 | + |
| 72 | + securityDb = (FBConnection) DriverManager.getConnection( |
| 73 | + "jdbc:firebirdsql:embedded:" + securityDatabase + "?charSet=UTF-8"); |
| 74 | + securityDb.setAutoCommit(false); |
| 75 | + securityDb.setReadOnly(true); |
| 76 | + |
| 77 | + TransactionParameterBuffer tpb = securityDb.createTransactionParameterBuffer(); |
| 78 | + tpb.addArgument(ISCConstants.isc_tpb_read_committed); |
| 79 | + tpb.addArgument(ISCConstants.isc_tpb_rec_version); |
| 80 | + tpb.addArgument(ISCConstants.isc_tpb_read); |
| 81 | + securityDb.setTransactionParameters(Connection.TRANSACTION_READ_COMMITTED, tpb); |
| 82 | + |
| 83 | + stmt = securityDb.prepareStatement( |
| 84 | + "select p.class_name, p.arg1, p.arg2\n" + |
| 85 | + " from database_permission_group dpg\n" + |
| 86 | + " join permission_group pg\n" + |
| 87 | + " on pg.id = dpg.permission_group\n" + |
| 88 | + " join permission p\n" + |
| 89 | + " on p.permission_group = pg.id\n" + |
| 90 | + " where cast(? as varchar(1024)) similar to dpg.database_pattern escape '\\' and\n" + |
| 91 | + " p.user_name in (?, 'PUBLIC')"); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static void databaseClosed() throws SQLException |
| 97 | + { |
| 98 | + synchronized (securityDatabase) |
| 99 | + { |
| 100 | + if (--count == 0) |
| 101 | + { |
| 102 | + stmt.close(); |
| 103 | + securityDb.close(); |
| 104 | + securityDb = null; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + static synchronized Subject getUserSubject(String databaseName, String userName) |
| 110 | + { |
| 111 | + synchronized (userSubjects) |
| 112 | + { |
| 113 | + Subject subj = userSubjects.get(userName); |
| 114 | + if (subj != null) |
| 115 | + return subj; |
| 116 | + |
| 117 | + final HashSet<Principal> principals = new HashSet<Principal>(); |
| 118 | + principals.add(new DbPrincipal(databaseName, userName)); |
| 119 | + subj = new Subject(true, principals, new HashSet<Object>(), new HashSet<Object>()); |
| 120 | + |
| 121 | + userSubjects.put(userName, subj); |
| 122 | + |
| 123 | + return subj; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public PermissionCollection getPermissions(final ProtectionDomain domain) |
| 129 | + { |
| 130 | + final Permissions permissions = new Permissions(); |
| 131 | + |
| 132 | + // Grant all permission to code stored at filesystem |
| 133 | + if ("file".equals(domain.getCodeSource().getLocation().getProtocol())) |
| 134 | + { |
| 135 | + permissions.add(new AllPermission()); |
| 136 | + return permissions; |
| 137 | + } |
| 138 | + |
| 139 | + return AccessController.doPrivileged(new PrivilegedAction<PermissionCollection>() { |
| 140 | + @Override |
| 141 | + public PermissionCollection run() |
| 142 | + { |
| 143 | + for (Principal principal : domain.getPrincipals()) |
| 144 | + { |
| 145 | + if (principal instanceof DbPrincipal) |
| 146 | + loadPermissions(((DbPrincipal) principal).getDatabaseName(), principal.getName(), permissions); |
| 147 | + } |
| 148 | + |
| 149 | + return permissions; |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean implies(final ProtectionDomain domain, final Permission permission) |
| 156 | + { |
| 157 | + // Grant all permission to code stored at filesystem |
| 158 | + if ("file".equals(domain.getCodeSource().getLocation().getProtocol())) |
| 159 | + return true; |
| 160 | + |
| 161 | + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { |
| 162 | + @Override |
| 163 | + public Boolean run() |
| 164 | + { |
| 165 | + PermissionCollection perms = getPermissions(domain); |
| 166 | + return perms.implies(permission); |
| 167 | + } |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + private void loadPermissions(String databaseName, String userName, PermissionCollection permissions) |
| 172 | + { |
| 173 | + //// TODO: cache |
| 174 | + synchronized (stmt) |
| 175 | + { |
| 176 | + try |
| 177 | + { |
| 178 | + stmt.setString(1, databaseName); |
| 179 | + stmt.setString(2, userName); |
| 180 | + ResultSet rs = stmt.executeQuery(); |
| 181 | + try |
| 182 | + { |
| 183 | + while (rs.next()) |
| 184 | + { |
| 185 | + try |
| 186 | + { |
| 187 | + Class<?> clazz = Class.forName(rs.getString(1)); |
| 188 | + String arg1 = rs.getString(2); |
| 189 | + String arg2 = rs.getString(3); |
| 190 | + |
| 191 | + if (arg2 != null) |
| 192 | + { |
| 193 | + Constructor<?> constr = clazz.getDeclaredConstructor( |
| 194 | + String.class, String.class); |
| 195 | + permissions.add((Permission) constr.newInstance(arg1, arg2)); |
| 196 | + } |
| 197 | + else if (arg1 != null) |
| 198 | + { |
| 199 | + Constructor<?> constr = clazz.getDeclaredConstructor(String.class); |
| 200 | + permissions.add((Permission) constr.newInstance(arg1)); |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + Constructor<?> constr = clazz.getDeclaredConstructor(); |
| 205 | + permissions.add((Permission) constr.newInstance()); |
| 206 | + } |
| 207 | + } |
| 208 | + catch (Exception e) |
| 209 | + { |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + finally |
| 214 | + { |
| 215 | + rs.close(); |
| 216 | + } |
| 217 | + } |
| 218 | + catch (SQLException e) |
| 219 | + { |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments