001: /*
002: * @(#)JAASPnutsImpl.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.security;
010:
011: import java.security.AccessController;
012: import java.security.CodeSource;
013: import java.security.PermissionCollection;
014: import java.security.Permissions;
015: import java.security.Policy;
016: import java.security.Principal;
017: import java.security.PrivilegedAction;
018: import java.security.ProtectionDomain;
019: import java.util.Set;
020:
021: import javax.security.auth.Subject;
022:
023: import pnuts.lang.Implementation;
024: import pnuts.lang.PnutsImpl;
025:
026: /**
027: * A SecurePnutsImpl subclass that executes scripts with permissions constructed from
028: * codesource-based policy and subject-based policy.
029: *
030: * <pre>e.g.
031: * context.setImplementation(new JAASPnutsImpl(context.Implementation(), codesource, subject))
032: * </pre>
033: */
034: public class JAASPnutsImpl extends SecurePnutsImpl {
035:
036: private Subject subject;
037:
038: /**
039: * A Constructor
040: *
041: * @param impl a PnutsImpl object
042: *
043: * @deprecated replaced by JAASPnutsImpl(Implementation)
044: */
045: public JAASPnutsImpl(PnutsImpl impl) {
046: this (impl, null);
047: }
048:
049: /**
050: * A Constructor
051: *
052: * @param impl a Implementation object
053: */
054: public JAASPnutsImpl(Implementation impl) {
055: this (impl, null);
056: }
057:
058: /**
059: * A Constructor
060: *
061: * @param impl the base implementation
062: * @param codeSource a CodeSource object which indicates the source of the expression
063: * execute by eval(String, Context).
064: *
065: * @deprecated replaced byte JAASPnutsImpl(Implementation, CodeSource)
066: */
067: public JAASPnutsImpl(PnutsImpl impl, CodeSource codeSource) {
068: this (impl, codeSource, null);
069: }
070:
071: /**
072: * A Constructor
073: *
074: * @param impl the base implementation
075: * @param codeSource a CodeSource object which indicates the source of the expression
076: * execute by eval(String, Context).
077: */
078: public JAASPnutsImpl(Implementation impl, CodeSource codeSource) {
079: this (impl, codeSource, null);
080: }
081:
082: /**
083: * A Constructor
084: *
085: * @param impl the base implementation
086: * @param codeSource a CodeSource object which indicates the source of the expression
087: * execute by eval(String, Context).
088: * @param subject a Subject
089: *
090: * @deprecated replaced by JAASPnutsImpl(Implementation, CodeSource, Subject)
091: */
092: public JAASPnutsImpl(PnutsImpl impl, CodeSource codeSource,
093: Subject subject) {
094: super (impl, codeSource);
095: this .subject = subject;
096: }
097:
098: /**
099: * A Constructor
100: *
101: * @param impl the base implementation
102: * @param codeSource a CodeSource object which indicates the source of the expression
103: * execute by eval(String, Context).
104: * @param subject a Subject
105: */
106: public JAASPnutsImpl(Implementation impl, CodeSource codeSource,
107: Subject subject) {
108: super (impl, codeSource);
109: this .subject = subject;
110: }
111:
112: protected PermissionCollection getPermissions(
113: final CodeSource codesource) {
114: if (subject != null) {
115: PermissionCollection perms = (PermissionCollection) AccessController
116: .doPrivileged(new PrivilegedAction() {
117: public Object run() {
118: Policy policy = Policy.getPolicy();
119: if (policy != null) {
120: Set s = subject.getPrincipals();
121: Principal[] principals = new Principal[s
122: .size()];
123: s.toArray(principals);
124: return policy
125: .getPermissions(new ProtectionDomain(
126: codesource, null, null,
127: principals));
128: } else {
129: return null;
130: }
131: }
132: });
133: if (perms == null) {
134: perms = new Permissions();
135: }
136: return perms;
137: } else {
138: return super .getPermissions(codesource);
139: }
140: }
141:
142: public String toString() {
143: return getClass().getName() + "[" + getBaseImpl() + ", "
144: + getCodeSource() + "]";
145: }
146: }
|