PageRenderTime 75ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jdk/src/share/classes/sun/security/tools/PolicyTool.java

https://bitbucket.org/nkabir/jdk-6
Java | 4251 lines | 3528 code | 304 blank | 419 comment | 169 complexity | 0a6ee0120998d09229f408df9771c2d3 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, BSD-3-Clause
  1. /*
  2. * Copyright (c) 1997, 2006, 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. package sun.security.tools;
  26. import java.io.*;
  27. import java.util.LinkedList;
  28. import java.util.ListIterator;
  29. import java.util.Vector;
  30. import java.util.Enumeration;
  31. import java.net.URL;
  32. import java.net.MalformedURLException;
  33. import java.lang.reflect.*;
  34. import java.text.Collator;
  35. import java.text.MessageFormat;
  36. import sun.misc.BASE64Decoder;
  37. import sun.security.provider.PolicyParser.PermissionEntry;
  38. import sun.security.util.PropertyExpander;
  39. import sun.security.util.PropertyExpander.ExpandException;
  40. import java.awt.*;
  41. import java.awt.event.*;
  42. import java.security.cert.Certificate;
  43. import java.security.cert.CertificateFactory;
  44. import java.security.cert.X509Certificate;
  45. import java.security.cert.CertificateException;
  46. import java.security.*;
  47. import sun.security.provider.*;
  48. import sun.security.util.PolicyUtil;
  49. import javax.security.auth.x500.X500Principal;
  50. import java.util.HashSet;
  51. /**
  52. * PolicyTool may be used by users and administrators to configure the
  53. * overall java security policy (currently stored in the policy file).
  54. * Using PolicyTool administators may add and remove policies from
  55. * the policy file. <p>
  56. *
  57. * @see java.security.Policy
  58. * @since 1.2
  59. */
  60. public class PolicyTool {
  61. // for i18n
  62. static final java.util.ResourceBundle rb =
  63. java.util.ResourceBundle.getBundle("sun.security.util.Resources");
  64. static final Collator collator = Collator.getInstance();
  65. static {
  66. // this is for case insensitive string comparisons
  67. collator.setStrength(Collator.PRIMARY);
  68. };
  69. // anyone can add warnings
  70. Vector<String> warnings;
  71. boolean newWarning = false;
  72. // set to true if policy modified.
  73. // this way upon exit we know if to ask the user to save changes
  74. boolean modified = false;
  75. private static final boolean testing = false;
  76. private static final Class[] TWOPARAMS = { String.class, String.class };
  77. private static final Class[] ONEPARAMS = { String.class };
  78. private static final Class[] NOPARAMS = {};
  79. /*
  80. * All of the policy entries are read in from the
  81. * policy file and stored here. Updates to the policy entries
  82. * using addEntry() and removeEntry() are made here. To ultimately save
  83. * the policy entries back to the policy file, the SavePolicy button
  84. * must be clicked.
  85. **/
  86. private static String policyFileName = null;
  87. private Vector<PolicyEntry> policyEntries = null;
  88. private PolicyParser parser = null;
  89. /* The public key alias information is stored here. */
  90. private KeyStore keyStore = null;
  91. private String keyStoreName = " ";
  92. private String keyStoreType = " ";
  93. private String keyStoreProvider = " ";
  94. private String keyStorePwdURL = " ";
  95. /* standard PKCS11 KeyStore type */
  96. private static final String P11KEYSTORE = "PKCS11";
  97. /* reserved word for PKCS11 KeyStores */
  98. private static final String NONE = "NONE";
  99. /**
  100. * default constructor
  101. */
  102. private PolicyTool() {
  103. policyEntries = new Vector<PolicyEntry>();
  104. parser = new PolicyParser();
  105. warnings = new Vector<String>();
  106. }
  107. /**
  108. * get the PolicyFileName
  109. */
  110. String getPolicyFileName() {
  111. return policyFileName;
  112. }
  113. /**
  114. * set the PolicyFileName
  115. */
  116. void setPolicyFileName(String policyFileName) {
  117. this.policyFileName = policyFileName;
  118. }
  119. /**
  120. * clear keyStore info
  121. */
  122. void clearKeyStoreInfo() {
  123. this.keyStoreName = null;
  124. this.keyStoreType = null;
  125. this.keyStoreProvider = null;
  126. this.keyStorePwdURL = null;
  127. this.keyStore = null;
  128. }
  129. /**
  130. * get the keyStore URL name
  131. */
  132. String getKeyStoreName() {
  133. return keyStoreName;
  134. }
  135. /**
  136. * get the keyStore Type
  137. */
  138. String getKeyStoreType() {
  139. return keyStoreType;
  140. }
  141. /**
  142. * get the keyStore Provider
  143. */
  144. String getKeyStoreProvider() {
  145. return keyStoreProvider;
  146. }
  147. /**
  148. * get the keyStore password URL
  149. */
  150. String getKeyStorePwdURL() {
  151. return keyStorePwdURL;
  152. }
  153. /**
  154. * Open and read a policy file
  155. */
  156. void openPolicy(String filename) throws FileNotFoundException,
  157. PolicyParser.ParsingException,
  158. KeyStoreException,
  159. CertificateException,
  160. InstantiationException,
  161. MalformedURLException,
  162. IOException,
  163. NoSuchAlgorithmException,
  164. IllegalAccessException,
  165. NoSuchMethodException,
  166. UnrecoverableKeyException,
  167. NoSuchProviderException,
  168. ClassNotFoundException,
  169. PropertyExpander.ExpandException,
  170. InvocationTargetException {
  171. newWarning = false;
  172. // start fresh - blow away the current state
  173. policyEntries = new Vector<PolicyEntry>();
  174. parser = new PolicyParser();
  175. warnings = new Vector<String>();
  176. setPolicyFileName(null);
  177. clearKeyStoreInfo();
  178. // see if user is opening a NEW policy file
  179. if (filename == null) {
  180. modified = false;
  181. return;
  182. }
  183. // Read in the policy entries from the file and
  184. // populate the parser vector table. The parser vector
  185. // table only holds the entries as strings, so it only
  186. // guarantees that the policies are syntactically
  187. // correct.
  188. setPolicyFileName(filename);
  189. parser.read(new FileReader(filename));
  190. // open the keystore
  191. openKeyStore(parser.getKeyStoreUrl(), parser.getKeyStoreType(),
  192. parser.getKeyStoreProvider(), parser.getStorePassURL());
  193. // Update the local vector with the same policy entries.
  194. // This guarantees that the policy entries are not only
  195. // syntactically correct, but semantically valid as well.
  196. Enumeration<PolicyParser.GrantEntry> enum_ = parser.grantElements();
  197. while (enum_.hasMoreElements()) {
  198. PolicyParser.GrantEntry ge = enum_.nextElement();
  199. // see if all the signers have public keys
  200. if (ge.signedBy != null) {
  201. String signers[] = parseSigners(ge.signedBy);
  202. for (int i = 0; i < signers.length; i++) {
  203. PublicKey pubKey = getPublicKeyAlias(signers[i]);
  204. if (pubKey == null) {
  205. newWarning = true;
  206. MessageFormat form = new MessageFormat(rb.getString
  207. ("Warning: A public key for alias " +
  208. "'signers[i]' does not exist. " +
  209. "Make sure a KeyStore is properly configured."));
  210. Object[] source = {signers[i]};
  211. warnings.addElement(form.format(source));
  212. }
  213. }
  214. }
  215. // check to see if the Principals are valid
  216. ListIterator<PolicyParser.PrincipalEntry> prinList =
  217. ge.principals.listIterator(0);
  218. while (prinList.hasNext()) {
  219. PolicyParser.PrincipalEntry pe = prinList.next();
  220. try {
  221. verifyPrincipal(pe.getPrincipalClass(),
  222. pe.getPrincipalName());
  223. } catch (ClassNotFoundException fnfe) {
  224. newWarning = true;
  225. MessageFormat form = new MessageFormat(rb.getString
  226. ("Warning: Class not found: class"));
  227. Object[] source = {pe.getPrincipalClass()};
  228. warnings.addElement(form.format(source));
  229. }
  230. }
  231. // check to see if the Permissions are valid
  232. Enumeration<PolicyParser.PermissionEntry> perms =
  233. ge.permissionElements();
  234. while (perms.hasMoreElements()) {
  235. PolicyParser.PermissionEntry pe = perms.nextElement();
  236. try {
  237. verifyPermission(pe.permission, pe.name, pe.action);
  238. } catch (ClassNotFoundException fnfe) {
  239. newWarning = true;
  240. MessageFormat form = new MessageFormat(rb.getString
  241. ("Warning: Class not found: class"));
  242. Object[] source = {pe.permission};
  243. warnings.addElement(form.format(source));
  244. } catch (InvocationTargetException ite) {
  245. newWarning = true;
  246. MessageFormat form = new MessageFormat(rb.getString
  247. ("Warning: Invalid argument(s) for constructor: arg"));
  248. Object[] source = {pe.permission};
  249. warnings.addElement(form.format(source));
  250. }
  251. // see if all the permission signers have public keys
  252. if (pe.signedBy != null) {
  253. String signers[] = parseSigners(pe.signedBy);
  254. for (int i = 0; i < signers.length; i++) {
  255. PublicKey pubKey = getPublicKeyAlias(signers[i]);
  256. if (pubKey == null) {
  257. newWarning = true;
  258. MessageFormat form = new MessageFormat(rb.getString
  259. ("Warning: A public key for alias " +
  260. "'signers[i]' does not exist. " +
  261. "Make sure a KeyStore is properly configured."));
  262. Object[] source = {signers[i]};
  263. warnings.addElement(form.format(source));
  264. }
  265. }
  266. }
  267. }
  268. PolicyEntry pEntry = new PolicyEntry(this, ge);
  269. policyEntries.addElement(pEntry);
  270. }
  271. // just read in the policy -- nothing has been modified yet
  272. modified = false;
  273. }
  274. /**
  275. * Save a policy to a file
  276. */
  277. void savePolicy(String filename)
  278. throws FileNotFoundException, IOException {
  279. // save the policy entries to a file
  280. parser.setKeyStoreUrl(keyStoreName);
  281. parser.setKeyStoreType(keyStoreType);
  282. parser.setKeyStoreProvider(keyStoreProvider);
  283. parser.setStorePassURL(keyStorePwdURL);
  284. parser.write(new FileWriter(filename));
  285. modified = false;
  286. }
  287. /**
  288. * Open the KeyStore
  289. */
  290. void openKeyStore(String name,
  291. String type,
  292. String provider,
  293. String pwdURL) throws KeyStoreException,
  294. NoSuchAlgorithmException,
  295. UnrecoverableKeyException,
  296. IOException,
  297. CertificateException,
  298. NoSuchProviderException,
  299. ExpandException {
  300. if (name == null && type == null &&
  301. provider == null && pwdURL == null) {
  302. // policy did not specify a keystore during open
  303. // or use wants to reset keystore values
  304. this.keyStoreName = null;
  305. this.keyStoreType = null;
  306. this.keyStoreProvider = null;
  307. this.keyStorePwdURL = null;
  308. // caller will set (tool.modified = true) if appropriate
  309. return;
  310. }
  311. URL policyURL = null;
  312. if (policyFileName != null) {
  313. File pfile = new File(policyFileName);
  314. policyURL = new URL("file:" + pfile.getCanonicalPath());
  315. }
  316. // although PolicyUtil.getKeyStore may properly handle
  317. // defaults and property expansion, we do it here so that
  318. // if the call is successful, we can set the proper values
  319. // (PolicyUtil.getKeyStore does not return expanded values)
  320. if (name != null && name.length() > 0) {
  321. name = PropertyExpander.expand(name).replace
  322. (File.separatorChar, '/');
  323. }
  324. if (type == null || type.length() == 0) {
  325. type = KeyStore.getDefaultType();
  326. }
  327. if (pwdURL != null && pwdURL.length() > 0) {
  328. pwdURL = PropertyExpander.expand(pwdURL).replace
  329. (File.separatorChar, '/');
  330. }
  331. try {
  332. this.keyStore = PolicyUtil.getKeyStore(policyURL,
  333. name,
  334. type,
  335. provider,
  336. pwdURL,
  337. null);
  338. } catch (IOException ioe) {
  339. // copied from sun.security.pkcs11.SunPKCS11
  340. String MSG = "no password provided, and no callback handler " +
  341. "available for retrieving password";
  342. Throwable cause = ioe.getCause();
  343. if (cause != null &&
  344. cause instanceof javax.security.auth.login.LoginException &&
  345. MSG.equals(cause.getMessage())) {
  346. // throw a more friendly exception message
  347. throw new IOException(MSG);
  348. } else {
  349. throw ioe;
  350. }
  351. }
  352. this.keyStoreName = name;
  353. this.keyStoreType = type;
  354. this.keyStoreProvider = provider;
  355. this.keyStorePwdURL = pwdURL;
  356. // caller will set (tool.modified = true)
  357. }
  358. /**
  359. * Add a Grant entry to the overall policy at the specified index.
  360. * A policy entry consists of a CodeSource.
  361. */
  362. boolean addEntry(PolicyEntry pe, int index) {
  363. if (index < 0) {
  364. // new entry -- just add it to the end
  365. policyEntries.addElement(pe);
  366. parser.add(pe.getGrantEntry());
  367. } else {
  368. // existing entry -- replace old one
  369. PolicyEntry origPe = policyEntries.elementAt(index);
  370. parser.replace(origPe.getGrantEntry(), pe.getGrantEntry());
  371. policyEntries.setElementAt(pe, index);
  372. }
  373. return true;
  374. }
  375. /**
  376. * Add a Principal entry to an existing PolicyEntry at the specified index.
  377. * A Principal entry consists of a class, and name.
  378. *
  379. * If the principal already exists, it is not added again.
  380. */
  381. boolean addPrinEntry(PolicyEntry pe,
  382. PolicyParser.PrincipalEntry newPrin,
  383. int index) {
  384. // first add the principal to the Policy Parser entry
  385. PolicyParser.GrantEntry grantEntry = pe.getGrantEntry();
  386. if (grantEntry.contains(newPrin) == true)
  387. return false;
  388. LinkedList<PolicyParser.PrincipalEntry> prinList =
  389. grantEntry.principals;
  390. if (index != -1)
  391. prinList.set(index, newPrin);
  392. else
  393. prinList.add(newPrin);
  394. modified = true;
  395. return true;
  396. }
  397. /**
  398. * Add a Permission entry to an existing PolicyEntry at the specified index.
  399. * A Permission entry consists of a permission, name, and actions.
  400. *
  401. * If the permission already exists, it is not added again.
  402. */
  403. boolean addPermEntry(PolicyEntry pe,
  404. PolicyParser.PermissionEntry newPerm,
  405. int index) {
  406. // first add the permission to the Policy Parser Vector
  407. PolicyParser.GrantEntry grantEntry = pe.getGrantEntry();
  408. if (grantEntry.contains(newPerm) == true)
  409. return false;
  410. Vector<PolicyParser.PermissionEntry> permList =
  411. grantEntry.permissionEntries;
  412. if (index != -1)
  413. permList.setElementAt(newPerm, index);
  414. else
  415. permList.addElement(newPerm);
  416. modified = true;
  417. return true;
  418. }
  419. /**
  420. * Remove a Permission entry from an existing PolicyEntry.
  421. */
  422. boolean removePermEntry(PolicyEntry pe,
  423. PolicyParser.PermissionEntry perm) {
  424. // remove the Permission from the GrantEntry
  425. PolicyParser.GrantEntry ppge = pe.getGrantEntry();
  426. modified = ppge.remove(perm);
  427. return modified;
  428. }
  429. /**
  430. * remove an entry from the overall policy
  431. */
  432. boolean removeEntry(PolicyEntry pe) {
  433. parser.remove(pe.getGrantEntry());
  434. modified = true;
  435. return (policyEntries.removeElement(pe));
  436. }
  437. /**
  438. * retrieve all Policy Entries
  439. */
  440. PolicyEntry[] getEntry() {
  441. if (policyEntries.size() > 0) {
  442. PolicyEntry entries[] = new PolicyEntry[policyEntries.size()];
  443. for (int i = 0; i < policyEntries.size(); i++)
  444. entries[i] = policyEntries.elementAt(i);
  445. return entries;
  446. }
  447. return null;
  448. }
  449. /**
  450. * Retrieve the public key mapped to a particular name.
  451. * If the key has expired, a KeyException is thrown.
  452. */
  453. PublicKey getPublicKeyAlias(String name) throws KeyStoreException {
  454. if (keyStore == null) {
  455. return null;
  456. }
  457. Certificate cert = keyStore.getCertificate(name);
  458. if (cert == null) {
  459. return null;
  460. }
  461. PublicKey pubKey = cert.getPublicKey();
  462. return pubKey;
  463. }
  464. /**
  465. * Retrieve all the alias names stored in the certificate database
  466. */
  467. String[] getPublicKeyAlias() throws KeyStoreException {
  468. int numAliases = 0;
  469. String aliases[] = null;
  470. if (keyStore == null) {
  471. return null;
  472. }
  473. Enumeration<String> enum_ = keyStore.aliases();
  474. // first count the number of elements
  475. while (enum_.hasMoreElements()) {
  476. enum_.nextElement();
  477. numAliases++;
  478. }
  479. if (numAliases > 0) {
  480. // now copy them into an array
  481. aliases = new String[numAliases];
  482. numAliases = 0;
  483. enum_ = keyStore.aliases();
  484. while (enum_.hasMoreElements()) {
  485. aliases[numAliases] = new String(enum_.nextElement());
  486. numAliases++;
  487. }
  488. }
  489. return aliases;
  490. }
  491. /**
  492. * This method parses a single string of signers separated by commas
  493. * ("jordan, duke, pippen") into an array of individual strings.
  494. */
  495. String[] parseSigners(String signedBy) {
  496. String signers[] = null;
  497. int numSigners = 1;
  498. int signedByIndex = 0;
  499. int commaIndex = 0;
  500. int signerNum = 0;
  501. // first pass thru "signedBy" counts the number of signers
  502. while (commaIndex >= 0) {
  503. commaIndex = signedBy.indexOf(',', signedByIndex);
  504. if (commaIndex >= 0) {
  505. numSigners++;
  506. signedByIndex = commaIndex + 1;
  507. }
  508. }
  509. signers = new String[numSigners];
  510. // second pass thru "signedBy" transfers signers to array
  511. commaIndex = 0;
  512. signedByIndex = 0;
  513. while (commaIndex >= 0) {
  514. if ((commaIndex = signedBy.indexOf(',', signedByIndex)) >= 0) {
  515. // transfer signer and ignore trailing part of the string
  516. signers[signerNum] =
  517. signedBy.substring(signedByIndex, commaIndex).trim();
  518. signerNum++;
  519. signedByIndex = commaIndex + 1;
  520. } else {
  521. // we are at the end of the string -- transfer signer
  522. signers[signerNum] = signedBy.substring(signedByIndex).trim();
  523. }
  524. }
  525. return signers;
  526. }
  527. /**
  528. * Check to see if the Principal contents are OK
  529. */
  530. void verifyPrincipal(String type, String name)
  531. throws ClassNotFoundException,
  532. InstantiationException
  533. {
  534. if (type.equals(PolicyParser.PrincipalEntry.WILDCARD_CLASS) ||
  535. type.equals(PolicyParser.REPLACE_NAME)) {
  536. return;
  537. };
  538. Class<?> PRIN = Class.forName("java.security.Principal");
  539. Class<?> pc = Class.forName(type, true,
  540. Thread.currentThread().getContextClassLoader());
  541. if (!PRIN.isAssignableFrom(pc)) {
  542. MessageFormat form = new MessageFormat(rb.getString
  543. ("Illegal Principal Type: type"));
  544. Object[] source = {type};
  545. throw new InstantiationException(form.format(source));
  546. }
  547. if (ToolDialog.X500_PRIN_CLASS.equals(pc.getName())) {
  548. // PolicyParser checks validity of X500Principal name
  549. // - PolicyTool needs to as well so that it doesn't store
  550. // an invalid name that can't be read in later
  551. //
  552. // this can throw an IllegalArgumentException
  553. X500Principal newP = new X500Principal(name);
  554. }
  555. }
  556. /**
  557. * Check to see if the Permission contents are OK
  558. */
  559. void verifyPermission(String type,
  560. String name,
  561. String actions)
  562. throws ClassNotFoundException,
  563. InstantiationException,
  564. IllegalAccessException,
  565. NoSuchMethodException,
  566. InvocationTargetException
  567. {
  568. //XXX we might want to keep a hash of created factories...
  569. Class<?> pc = Class.forName(type, true,
  570. Thread.currentThread().getContextClassLoader());
  571. Constructor<?> c = null;
  572. Vector<String> objects = new Vector<String>(2);
  573. if (name != null) objects.add(name);
  574. if (actions != null) objects.add(actions);
  575. switch (objects.size()) {
  576. case 0:
  577. try {
  578. c = pc.getConstructor(NOPARAMS);
  579. break;
  580. } catch (NoSuchMethodException ex) {
  581. // proceed to the one-param constructor
  582. objects.add(null);
  583. }
  584. case 1:
  585. try {
  586. c = pc.getConstructor(ONEPARAMS);
  587. break;
  588. } catch (NoSuchMethodException ex) {
  589. // proceed to the two-param constructor
  590. objects.add(null);
  591. }
  592. case 2:
  593. c = pc.getConstructor(TWOPARAMS);
  594. break;
  595. }
  596. Object parameters[] = objects.toArray();
  597. Permission p = (Permission)c.newInstance(parameters);
  598. }
  599. /*
  600. * Parse command line arguments.
  601. */
  602. static void parseArgs(String args[]) {
  603. /* parse flags */
  604. int n = 0;
  605. for (n=0; (n < args.length) && args[n].startsWith("-"); n++) {
  606. String flags = args[n];
  607. if (collator.compare(flags, "-file") == 0) {
  608. if (++n == args.length) usage();
  609. policyFileName = args[n];
  610. } else {
  611. MessageFormat form = new MessageFormat(rb.getString
  612. ("Illegal option: option"));
  613. Object[] source = { flags };
  614. System.err.println(form.format(source));
  615. usage();
  616. }
  617. }
  618. }
  619. static void usage() {
  620. System.out.println(rb.getString("Usage: policytool [options]"));
  621. System.out.println();
  622. System.out.println(rb.getString
  623. (" [-file <file>] policy file location"));
  624. System.out.println();
  625. System.exit(1);
  626. }
  627. /**
  628. * run the PolicyTool
  629. */
  630. public static void main(String args[]) {
  631. parseArgs(args);
  632. ToolWindow tw = new ToolWindow(new PolicyTool());
  633. tw.displayToolWindow(args);
  634. }
  635. // split instr to words according to capitalization,
  636. // like, AWTControl -> A W T Control
  637. // this method is for easy pronounciation
  638. static String splitToWords(String instr) {
  639. return instr.replaceAll("([A-Z])", " $1");
  640. }
  641. }
  642. /**
  643. * Each entry in the policy configuration file is represented by a
  644. * PolicyEntry object.
  645. *
  646. * A PolicyEntry is a (CodeSource,Permission) pair. The
  647. * CodeSource contains the (URL, PublicKey) that together identify
  648. * where the Java bytecodes come from and who (if anyone) signed
  649. * them. The URL could refer to localhost. The URL could also be
  650. * null, meaning that this policy entry is given to all comers, as
  651. * long as they match the signer field. The signer could be null,
  652. * meaning the code is not signed.
  653. *
  654. * The Permission contains the (Type, Name, Action) triplet.
  655. *
  656. */
  657. class PolicyEntry {
  658. private CodeSource codesource;
  659. private PolicyTool tool;
  660. private PolicyParser.GrantEntry grantEntry;
  661. private boolean testing = false;
  662. /**
  663. * Create a PolicyEntry object from the information read in
  664. * from a policy file.
  665. */
  666. PolicyEntry(PolicyTool tool, PolicyParser.GrantEntry ge)
  667. throws MalformedURLException, NoSuchMethodException,
  668. ClassNotFoundException, InstantiationException, IllegalAccessException,
  669. InvocationTargetException, CertificateException,
  670. IOException, NoSuchAlgorithmException, UnrecoverableKeyException {
  671. this.tool = tool;
  672. URL location = null;
  673. // construct the CodeSource
  674. if (ge.codeBase != null)
  675. location = new URL(ge.codeBase);
  676. this.codesource = new CodeSource(location,
  677. (java.security.cert.Certificate[]) null);
  678. if (testing) {
  679. System.out.println("Adding Policy Entry:");
  680. System.out.println(" CodeBase = " + location);
  681. System.out.println(" Signers = " + ge.signedBy);
  682. System.out.println(" with " + ge.principals.size() +
  683. " Principals");
  684. }
  685. this.grantEntry = ge;
  686. }
  687. /**
  688. * get the codesource associated with this PolicyEntry
  689. */
  690. CodeSource getCodeSource() {
  691. return codesource;
  692. }
  693. /**
  694. * get the GrantEntry associated with this PolicyEntry
  695. */
  696. PolicyParser.GrantEntry getGrantEntry() {
  697. return grantEntry;
  698. }
  699. /**
  700. * convert the header portion, i.e. codebase, signer, principals, of
  701. * this policy entry into a string
  702. */
  703. String headerToString() {
  704. String pString = principalsToString();
  705. if (pString.length() == 0) {
  706. return codebaseToString();
  707. } else {
  708. return codebaseToString() + ", " + pString;
  709. }
  710. }
  711. /**
  712. * convert the Codebase/signer portion of this policy entry into a string
  713. */
  714. String codebaseToString() {
  715. String stringEntry = new String();
  716. if (grantEntry.codeBase != null &&
  717. grantEntry.codeBase.equals("") == false)
  718. stringEntry = stringEntry.concat
  719. ("CodeBase \"" +
  720. grantEntry.codeBase +
  721. "\"");
  722. if (grantEntry.signedBy != null &&
  723. grantEntry.signedBy.equals("") == false)
  724. stringEntry = ((stringEntry.length() > 0) ?
  725. stringEntry.concat(", SignedBy \"" +
  726. grantEntry.signedBy +
  727. "\"") :
  728. stringEntry.concat("SignedBy \"" +
  729. grantEntry.signedBy +
  730. "\""));
  731. if (stringEntry.length() == 0)
  732. return new String("CodeBase <ALL>");
  733. return stringEntry;
  734. }
  735. /**
  736. * convert the Principals portion of this policy entry into a string
  737. */
  738. String principalsToString() {
  739. String result = "";
  740. if ((grantEntry.principals != null) &&
  741. (!grantEntry.principals.isEmpty())) {
  742. StringBuffer buffer = new StringBuffer(200);
  743. ListIterator<PolicyParser.PrincipalEntry> list =
  744. grantEntry.principals.listIterator();
  745. while (list.hasNext()) {
  746. PolicyParser.PrincipalEntry pppe = list.next();
  747. buffer.append(" Principal " + pppe.getDisplayClass() + " " +
  748. pppe.getDisplayName(true));
  749. if (list.hasNext()) buffer.append(", ");
  750. }
  751. result = buffer.toString();
  752. }
  753. return result;
  754. }
  755. /**
  756. * convert this policy entry into a PolicyParser.PermissionEntry
  757. */
  758. PolicyParser.PermissionEntry toPermissionEntry(Permission perm) {
  759. String actions = null;
  760. // get the actions
  761. if (perm.getActions() != null &&
  762. perm.getActions().trim() != "")
  763. actions = perm.getActions();
  764. PolicyParser.PermissionEntry pe = new PolicyParser.PermissionEntry
  765. (perm.getClass().getName(),
  766. perm.getName(),
  767. actions);
  768. return pe;
  769. }
  770. }
  771. /**
  772. * The main window for the PolicyTool
  773. */
  774. class ToolWindow extends Frame {
  775. // use serialVersionUID from JDK 1.2.2 for interoperability
  776. private static final long serialVersionUID = 5682568601210376777L;
  777. /* external paddings */
  778. public static final Insets TOP_PADDING = new Insets(25,0,0,0);
  779. public static final Insets BOTTOM_PADDING = new Insets(0,0,25,0);
  780. public static final Insets LITE_BOTTOM_PADDING = new Insets(0,0,10,0);
  781. public static final Insets LR_PADDING = new Insets(0,10,0,10);
  782. public static final Insets TOP_BOTTOM_PADDING = new Insets(15, 0, 15, 0);
  783. public static final Insets L_TOP_BOTTOM_PADDING = new Insets(5,10,15,0);
  784. public static final Insets LR_BOTTOM_PADDING = new Insets(0,10,5,10);
  785. public static final Insets L_BOTTOM_PADDING = new Insets(0,10,5,0);
  786. public static final Insets R_BOTTOM_PADDING = new Insets(0,0,5,10);
  787. /* buttons and menus */
  788. public static final String NEW_POLICY_FILE =
  789. PolicyTool.rb.getString("New");
  790. public static final String OPEN_POLICY_FILE =
  791. PolicyTool.rb.getString("Open");
  792. public static final String SAVE_POLICY_FILE =
  793. PolicyTool.rb.getString("Save");
  794. public static final String SAVE_AS_POLICY_FILE =
  795. PolicyTool.rb.getString("Save As");
  796. public static final String VIEW_WARNINGS =
  797. PolicyTool.rb.getString("View Warning Log");
  798. public static final String QUIT =
  799. PolicyTool.rb.getString("Exit");
  800. public static final String ADD_POLICY_ENTRY =
  801. PolicyTool.rb.getString("Add Policy Entry");
  802. public static final String EDIT_POLICY_ENTRY =
  803. PolicyTool.rb.getString("Edit Policy Entry");
  804. public static final String REMOVE_POLICY_ENTRY =
  805. PolicyTool.rb.getString("Remove Policy Entry");
  806. public static final String EDIT_KEYSTORE =
  807. PolicyTool.rb.getString("Edit");
  808. public static final String ADD_PUBKEY_ALIAS =
  809. PolicyTool.rb.getString("Add Public Key Alias");
  810. public static final String REMOVE_PUBKEY_ALIAS =
  811. PolicyTool.rb.getString("Remove Public Key Alias");
  812. /* gridbag index for components in the main window (MW) */
  813. public static final int MW_FILENAME_LABEL = 0;
  814. public static final int MW_FILENAME_TEXTFIELD = 1;
  815. public static final int MW_PANEL = 2;
  816. public static final int MW_ADD_BUTTON = 0;
  817. public static final int MW_EDIT_BUTTON = 1;
  818. public static final int MW_REMOVE_BUTTON = 2;
  819. public static final int MW_POLICY_LIST = 3; // follows MW_PANEL
  820. private PolicyTool tool;
  821. /**
  822. * Constructor
  823. */
  824. ToolWindow(PolicyTool tool) {
  825. this.tool = tool;
  826. }
  827. /**
  828. * Initialize the PolicyTool window with the necessary components
  829. */
  830. private void initWindow() {
  831. // create the top menu bar
  832. MenuBar menuBar = new MenuBar();
  833. // create a File menu
  834. Menu menu = new Menu(PolicyTool.rb.getString("File"));
  835. menu.add(NEW_POLICY_FILE);
  836. menu.add(OPEN_POLICY_FILE);
  837. menu.add(SAVE_POLICY_FILE);
  838. menu.add(SAVE_AS_POLICY_FILE);
  839. menu.add(VIEW_WARNINGS);
  840. menu.add(QUIT);
  841. menu.addActionListener(new FileMenuListener(tool, this));
  842. menuBar.add(menu);
  843. setMenuBar(menuBar);
  844. // create a KeyStore menu
  845. menu = new Menu(PolicyTool.rb.getString("KeyStore"));
  846. menu.add(EDIT_KEYSTORE);
  847. menu.addActionListener(new MainWindowListener(tool, this));
  848. menuBar.add(menu);
  849. setMenuBar(menuBar);
  850. // policy entry listing
  851. Label label = new Label(PolicyTool.rb.getString("Policy File:"));
  852. addNewComponent(this, label, MW_FILENAME_LABEL,
  853. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  854. TOP_BOTTOM_PADDING);
  855. TextField tf = new TextField(50);
  856. tf.getAccessibleContext().setAccessibleName(
  857. PolicyTool.rb.getString("Policy File:"));
  858. tf.setEditable(false);
  859. addNewComponent(this, tf, MW_FILENAME_TEXTFIELD,
  860. 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  861. TOP_BOTTOM_PADDING);
  862. // add ADD/REMOVE/EDIT buttons in a new panel
  863. Panel panel = new Panel();
  864. panel.setLayout(new GridBagLayout());
  865. Button button = new Button(ADD_POLICY_ENTRY);
  866. button.addActionListener(new MainWindowListener(tool, this));
  867. addNewComponent(panel, button, MW_ADD_BUTTON,
  868. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  869. LR_PADDING);
  870. button = new Button(EDIT_POLICY_ENTRY);
  871. button.addActionListener(new MainWindowListener(tool, this));
  872. addNewComponent(panel, button, MW_EDIT_BUTTON,
  873. 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  874. LR_PADDING);
  875. button = new Button(REMOVE_POLICY_ENTRY);
  876. button.addActionListener(new MainWindowListener(tool, this));
  877. addNewComponent(panel, button, MW_REMOVE_BUTTON,
  878. 2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  879. LR_PADDING);
  880. addNewComponent(this, panel, MW_PANEL,
  881. 0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  882. BOTTOM_PADDING);
  883. String policyFile = tool.getPolicyFileName();
  884. if (policyFile == null) {
  885. String userHome;
  886. userHome = java.security.AccessController.doPrivileged(
  887. new sun.security.action.GetPropertyAction("user.home"));
  888. policyFile = userHome + File.separatorChar + ".java.policy";
  889. }
  890. try {
  891. // open the policy file
  892. tool.openPolicy(policyFile);
  893. // display the policy entries via the policy list textarea
  894. List list = new List(40, false);
  895. list.addActionListener(new PolicyListListener(tool, this));
  896. PolicyEntry entries[] = tool.getEntry();
  897. if (entries != null) {
  898. for (int i = 0; i < entries.length; i++)
  899. list.add(entries[i].headerToString());
  900. }
  901. TextField newFilename = (TextField)
  902. getComponent(MW_FILENAME_TEXTFIELD);
  903. newFilename.setText(policyFile);
  904. initPolicyList(list);
  905. } catch (FileNotFoundException fnfe) {
  906. // add blank policy listing
  907. List list = new List(40, false);
  908. list.addActionListener(new PolicyListListener(tool, this));
  909. initPolicyList(list);
  910. tool.setPolicyFileName(null);
  911. tool.modified = false;
  912. setVisible(true);
  913. // just add warning
  914. tool.warnings.addElement(fnfe.toString());
  915. } catch (Exception e) {
  916. // add blank policy listing
  917. List list = new List(40, false);
  918. list.addActionListener(new PolicyListListener(tool, this));
  919. initPolicyList(list);
  920. tool.setPolicyFileName(null);
  921. tool.modified = false;
  922. setVisible(true);
  923. // display the error
  924. MessageFormat form = new MessageFormat(PolicyTool.rb.getString
  925. ("Could not open policy file: policyFile: e.toString()"));
  926. Object[] source = {policyFile, e.toString()};
  927. displayErrorDialog(null, form.format(source));
  928. }
  929. }
  930. /**
  931. * Add a component to the PolicyTool window
  932. */
  933. void addNewComponent(Container container, Component component,
  934. int index, int gridx, int gridy, int gridwidth, int gridheight,
  935. double weightx, double weighty, int fill, Insets is) {
  936. // add the component at the specified gridbag index
  937. container.add(component, index);
  938. // set the constraints
  939. GridBagLayout gbl = (GridBagLayout)container.getLayout();
  940. GridBagConstraints gbc = new GridBagConstraints();
  941. gbc.gridx = gridx;
  942. gbc.gridy = gridy;
  943. gbc.gridwidth = gridwidth;
  944. gbc.gridheight = gridheight;
  945. gbc.weightx = weightx;
  946. gbc.weighty = weighty;
  947. gbc.fill = fill;
  948. if (is != null) gbc.insets = is;
  949. gbl.setConstraints(component, gbc);
  950. }
  951. /**
  952. * Add a component to the PolicyTool window without external padding
  953. */
  954. void addNewComponent(Container container, Component component,
  955. int index, int gridx, int gridy, int gridwidth, int gridheight,
  956. double weightx, double weighty, int fill) {
  957. // delegate with "null" external padding
  958. addNewComponent(container, component, index, gridx, gridy,
  959. gridwidth, gridheight, weightx, weighty,
  960. fill, null);
  961. }
  962. /**
  963. * Init the policy_entry_list TEXTAREA component in the
  964. * PolicyTool window
  965. */
  966. void initPolicyList(List policyList) {
  967. // add the policy list to the window
  968. addNewComponent(this, policyList, MW_POLICY_LIST,
  969. 0, 3, 2, 1, 1.0, 1.0, GridBagConstraints.BOTH);
  970. }
  971. /**
  972. * Replace the policy_entry_list TEXTAREA component in the
  973. * PolicyTool window with an updated one.
  974. */
  975. void replacePolicyList(List policyList) {
  976. // remove the original list of Policy Entries
  977. // and add the new list of entries
  978. List list = (List)getComponent(MW_POLICY_LIST);
  979. list.removeAll();
  980. String newItems[] = policyList.getItems();
  981. for (int i = 0; i < newItems.length; i++)
  982. list.add(newItems[i]);
  983. }
  984. /**
  985. * display the main PolicyTool window
  986. */
  987. void displayToolWindow(String args[]) {
  988. setTitle(PolicyTool.rb.getString("Policy Tool"));
  989. setResizable(true);
  990. addWindowListener(new ToolWindowListener(this));
  991. setBounds(135, 80, 500, 500);
  992. setLayout(new GridBagLayout());
  993. initWindow();
  994. // display it
  995. setVisible(true);
  996. if (tool.newWarning == true) {
  997. displayStatusDialog(this, PolicyTool.rb.getString
  998. ("Errors have occurred while opening the " +
  999. "policy configuration. View the Warning Log " +
  1000. "for more information."));
  1001. }
  1002. }
  1003. /**
  1004. * displays a dialog box describing an error which occurred.
  1005. */
  1006. void displayErrorDialog(Window w, String error) {
  1007. ToolDialog ed = new ToolDialog
  1008. (PolicyTool.rb.getString("Error"), tool, this, true);
  1009. // find where the PolicyTool gui is
  1010. Point location = ((w == null) ?
  1011. getLocationOnScreen() : w.getLocationOnScreen());
  1012. ed.setBounds(location.x + 50, location.y + 50, 600, 100);
  1013. ed.setLayout(new GridBagLayout());
  1014. Label label = new Label(error);
  1015. addNewComponent(ed, label, 0,
  1016. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1017. Button okButton = new Button(PolicyTool.rb.getString("OK"));
  1018. okButton.addActionListener(new ErrorOKButtonListener(ed));
  1019. addNewComponent(ed, okButton, 1,
  1020. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
  1021. ed.pack();
  1022. ed.setVisible(true);
  1023. }
  1024. /**
  1025. * displays a dialog box describing an error which occurred.
  1026. */
  1027. void displayErrorDialog(Window w, Throwable t) {
  1028. if (t instanceof NoDisplayException) {
  1029. return;
  1030. }
  1031. displayErrorDialog(w, t.toString());
  1032. }
  1033. /**
  1034. * displays a dialog box describing the status of an event
  1035. */
  1036. void displayStatusDialog(Window w, String status) {
  1037. ToolDialog sd = new ToolDialog
  1038. (PolicyTool.rb.getString("Status"), tool, this, true);
  1039. // find the location of the PolicyTool gui
  1040. Point location = ((w == null) ?
  1041. getLocationOnScreen() : w.getLocationOnScreen());
  1042. sd.setBounds(location.x + 50, location.y + 50, 500, 100);
  1043. sd.setLayout(new GridBagLayout());
  1044. Label label = new Label(status);
  1045. addNewComponent(sd, label, 0,
  1046. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1047. Button okButton = new Button(PolicyTool.rb.getString("OK"));
  1048. okButton.addActionListener(new StatusOKButtonListener(sd));
  1049. addNewComponent(sd, okButton, 1,
  1050. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
  1051. sd.pack();
  1052. sd.setVisible(true);
  1053. }
  1054. /**
  1055. * display the warning log
  1056. */
  1057. void displayWarningLog(Window w) {
  1058. ToolDialog wd = new ToolDialog
  1059. (PolicyTool.rb.getString("Warning"), tool, this, true);
  1060. // find the location of the PolicyTool gui
  1061. Point location = ((w == null) ?
  1062. getLocationOnScreen() : w.getLocationOnScreen());
  1063. wd.setBounds(location.x + 50, location.y + 50, 500, 100);
  1064. wd.setLayout(new GridBagLayout());
  1065. TextArea ta = new TextArea();
  1066. ta.setEditable(false);
  1067. for (int i = 0; i < tool.warnings.size(); i++) {
  1068. ta.append(tool.warnings.elementAt(i));
  1069. ta.append(PolicyTool.rb.getString("\n"));
  1070. }
  1071. addNewComponent(wd, ta, 0,
  1072. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1073. BOTTOM_PADDING);
  1074. ta.setFocusable(false);
  1075. Button okButton = new Button(PolicyTool.rb.getString("OK"));
  1076. okButton.addActionListener(new CancelButtonListener(wd));
  1077. addNewComponent(wd, okButton, 1,
  1078. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1079. LR_PADDING);
  1080. wd.pack();
  1081. wd.setVisible(true);
  1082. }
  1083. char displayYesNoDialog(Window w, String title, String prompt, String yes, String no) {
  1084. final ToolDialog tw = new ToolDialog
  1085. (title, tool, this, true);
  1086. Point location = ((w == null) ?
  1087. getLocationOnScreen() : w.getLocationOnScreen());
  1088. tw.setBounds(location.x + 75, location.y + 100, 400, 150);
  1089. tw.setLayout(new GridBagLayout());
  1090. TextArea ta = new TextArea(prompt, 10, 50, TextArea.SCROLLBARS_VERTICAL_ONLY);
  1091. ta.setEditable(false);
  1092. addNewComponent(tw, ta, 0,
  1093. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1094. ta.setFocusable(false);
  1095. Panel panel = new Panel();
  1096. panel.setLayout(new GridBagLayout());
  1097. // StringBuffer to store button press. Must be final.
  1098. final StringBuffer chooseResult = new StringBuffer();
  1099. Button button = new Button(yes);
  1100. button.addActionListener(new ActionListener() {
  1101. public void actionPerformed(ActionEvent e) {
  1102. chooseResult.append('Y');
  1103. tw.setVisible(false);
  1104. tw.dispose();
  1105. }
  1106. });
  1107. addNewComponent(panel, button, 0,
  1108. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1109. LR_PADDING);
  1110. button = new Button(no);
  1111. button.addActionListener(new ActionListener() {
  1112. public void actionPerformed(ActionEvent e) {
  1113. chooseResult.append('N');
  1114. tw.setVisible(false);
  1115. tw.dispose();
  1116. }
  1117. });
  1118. addNewComponent(panel, button, 1,
  1119. 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1120. LR_PADDING);
  1121. addNewComponent(tw, panel, 1,
  1122. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
  1123. tw.pack();
  1124. tw.setVisible(true);
  1125. if (chooseResult.length() > 0) {
  1126. return chooseResult.charAt(0);
  1127. } else {
  1128. // I did encounter this once, don't why.
  1129. return 'N';
  1130. }
  1131. }
  1132. }
  1133. /**
  1134. * General dialog window
  1135. */
  1136. class ToolDialog extends Dialog {
  1137. // use serialVersionUID from JDK 1.2.2 for interoperability
  1138. private static final long serialVersionUID = -372244357011301190L;
  1139. /* necessary constants */
  1140. public static final int NOACTION = 0;
  1141. public static final int QUIT = 1;
  1142. public static final int NEW = 2;
  1143. public static final int OPEN = 3;
  1144. public static final String ALL_PERM_CLASS =
  1145. "java.security.AllPermission";
  1146. public static final String FILE_PERM_CLASS =
  1147. "java.io.FilePermission";
  1148. public static final String X500_PRIN_CLASS =
  1149. "javax.security.auth.x500.X500Principal";
  1150. /* popup menus */
  1151. public static final String PERM =
  1152. PolicyTool.rb.getString
  1153. ("Permission: ");
  1154. public static final String PRIN_TYPE =
  1155. PolicyTool.rb.getString("Principal Type:");
  1156. public static final String PRIN_NAME =
  1157. PolicyTool.rb.getString("Principal Name:");
  1158. /* more popu menus */
  1159. public static final String PERM_NAME =
  1160. PolicyTool.rb.getString
  1161. ("Target Name: ");
  1162. /* and more popup menus */
  1163. public static final String PERM_ACTIONS =
  1164. PolicyTool.rb.getString
  1165. ("Actions: ");
  1166. /* gridbag index for display OverWriteFile (OW) components */
  1167. public static final int OW_LABEL = 0;
  1168. public static final int OW_OK_BUTTON = 1;
  1169. public static final int OW_CANCEL_BUTTON = 2;
  1170. /* gridbag index for display PolicyEntry (PE) components */
  1171. public static final int PE_CODEBASE_LABEL = 0;
  1172. public static final int PE_CODEBASE_TEXTFIELD = 1;
  1173. public static final int PE_SIGNEDBY_LABEL = 2;
  1174. public static final int PE_SIGNEDBY_TEXTFIELD = 3;
  1175. public static final int PE_PANEL0 = 4;
  1176. public static final int PE_ADD_PRIN_BUTTON = 0;
  1177. public static final int PE_EDIT_PRIN_BUTTON = 1;
  1178. public static final int PE_REMOVE_PRIN_BUTTON = 2;
  1179. public static final int PE_PRIN_LABEL = 5;
  1180. public static final int PE_PRIN_LIST = 6;
  1181. public static final int PE_PANEL1 = 7;
  1182. public static final int PE_ADD_PERM_BUTTON = 0;
  1183. public static final int PE_EDIT_PERM_BUTTON = 1;
  1184. public static final int PE_REMOVE_PERM_BUTTON = 2;
  1185. public static final int PE_PERM_LIST = 8;
  1186. public static final int PE_PANEL2 = 9;
  1187. public static final int PE_CANCEL_BUTTON = 1;
  1188. public static final int PE_DONE_BUTTON = 0;
  1189. /* the gridbag index for components in the Principal Dialog (PRD) */
  1190. public static final int PRD_DESC_LABEL = 0;
  1191. public static final int PRD_PRIN_CHOICE = 1;
  1192. public static final int PRD_PRIN_TEXTFIELD = 2;
  1193. public static final int PRD_NAME_LABEL = 3;
  1194. public static final int PRD_NAME_TEXTFIELD = 4;
  1195. public static final int PRD_CANCEL_BUTTON = 6;
  1196. public static final int PRD_OK_BUTTON = 5;
  1197. /* the gridbag index for components in the Permission Dialog (PD) */
  1198. public static final int PD_DESC_LABEL = 0;
  1199. public static final int PD_PERM_CHOICE = 1;
  1200. public static final int PD_PERM_TEXTFIELD = 2;
  1201. public static final int PD_NAME_CHOICE = 3;
  1202. public static final int PD_NAME_TEXTFIELD = 4;
  1203. public static final int PD_ACTIONS_CHOICE = 5;
  1204. public static final int PD_ACTIONS_TEXTFIELD = 6;
  1205. public static final int PD_SIGNEDBY_LABEL = 7;
  1206. public static final int PD_SIGNEDBY_TEXTFIELD = 8;
  1207. public static final int PD_CANCEL_BUTTON = 10;
  1208. public static final int PD_OK_BUTTON = 9;
  1209. /* modes for KeyStore */
  1210. public static final int EDIT_KEYSTORE = 0;
  1211. /* the gridbag index for components in the Change KeyStore Dialog (KSD) */
  1212. public static final int KSD_NAME_LABEL = 0;
  1213. public static final int KSD_NAME_TEXTFIELD = 1;
  1214. public static final int KSD_TYPE_LABEL = 2;
  1215. public static final int KSD_TYPE_TEXTFIELD = 3;
  1216. public static final int KSD_PROVIDER_LABEL = 4;
  1217. public static final int KSD_PROVIDER_TEXTFIELD = 5;
  1218. public static final int KSD_PWD_URL_LABEL = 6;
  1219. public static final int KSD_PWD_URL_TEXTFIELD = 7;
  1220. public static final int KSD_CANCEL_BUTTON = 9;
  1221. public static final int KSD_OK_BUTTON = 8;
  1222. /* the gridbag index for components in the User Save Changes Dialog (USC) */
  1223. public static final int USC_LABEL = 0;
  1224. public static final int USC_PANEL = 1;
  1225. public static final int USC_YES_BUTTON = 0;
  1226. public static final int USC_NO_BUTTON = 1;
  1227. public static final int USC_CANCEL_BUTTON = 2;
  1228. /* gridbag index for the ConfirmRemovePolicyEntryDialog (CRPE) */
  1229. public static final int CRPE_LABEL1 = 0;
  1230. public static final int CRPE_LABEL2 = 1;
  1231. public static final int CRPE_PANEL = 2;
  1232. public static final int CRPE_PANEL_OK = 0;
  1233. public static final int CRPE_PANEL_CANCEL = 1;
  1234. /* some private static finals */
  1235. private static final int PERMISSION = 0;
  1236. private static final int PERMISSION_NAME = 1;
  1237. private static final int PERMISSION_ACTIONS = 2;
  1238. private static final int PERMISSION_SIGNEDBY = 3;
  1239. private static final int PRINCIPAL_TYPE = 4;
  1240. private static final int PRINCIPAL_NAME = 5;
  1241. public static java.util.ArrayList<Perm> PERM_ARRAY;
  1242. public static java.util.ArrayList<Prin> PRIN_ARRAY;
  1243. PolicyTool tool;
  1244. ToolWindow tw;
  1245. static {
  1246. // set up permission objects
  1247. PERM_ARRAY = new java.util.ArrayList<Perm>();
  1248. PERM_ARRAY.add(new AllPerm());
  1249. PERM_ARRAY.add(new AudioPerm());
  1250. PERM_ARRAY.add(new AuthPerm());
  1251. PERM_ARRAY.add(new AWTPerm());
  1252. PERM_ARRAY.add(new DelegationPerm());
  1253. PERM_ARRAY.add(new FilePerm());
  1254. PERM_ARRAY.add(new LogPerm());
  1255. PERM_ARRAY.add(new MgmtPerm());
  1256. PERM_ARRAY.add(new MBeanPerm());
  1257. PERM_ARRAY.add(new MBeanSvrPerm());
  1258. PERM_ARRAY.add(new MBeanTrustPerm());
  1259. PERM_ARRAY.add(new NetPerm());
  1260. PERM_ARRAY.add(new PrivCredPerm());
  1261. PERM_ARRAY.add(new PropPerm());
  1262. PERM_ARRAY.add(new ReflectPerm());
  1263. PERM_ARRAY.add(new RuntimePerm());
  1264. PERM_ARRAY.add(new SecurityPerm());
  1265. PERM_ARRAY.add(new SerialPerm());
  1266. PERM_ARRAY.add(new ServicePerm());
  1267. PERM_ARRAY.add(new SocketPerm());
  1268. PERM_ARRAY.add(new SQLPerm());
  1269. PERM_ARRAY.add(new SSLPerm());
  1270. PERM_ARRAY.add(new SubjDelegPerm());
  1271. // set up principal objects
  1272. PRIN_ARRAY = new java.util.ArrayList<Prin>();
  1273. PRIN_ARRAY.add(new KrbPrin());
  1274. PRIN_ARRAY.add(new X500Prin());
  1275. }
  1276. ToolDialog(String title, PolicyTool tool, ToolWindow tw, boolean modal) {
  1277. super(tw, modal);
  1278. setTitle(title);
  1279. this.tool = tool;
  1280. this.tw = tw;
  1281. addWindowListener(new ChildWindowListener(this));
  1282. }
  1283. /**
  1284. * get the Perm instance based on either the (shortened) class name
  1285. * or the fully qualified class name
  1286. */
  1287. static Perm getPerm(String clazz, boolean fullClassName) {
  1288. for (int i = 0; i < PERM_ARRAY.size(); i++) {
  1289. Perm next = PERM_ARRAY.get(i);
  1290. if (fullClassName) {
  1291. if (next.FULL_CLASS.equals(clazz)) {
  1292. return next;
  1293. }
  1294. } else {
  1295. if (next.CLASS.equals(clazz)) {
  1296. return next;
  1297. }
  1298. }
  1299. }
  1300. return null;
  1301. }
  1302. /**
  1303. * get the Prin instance based on either the (shortened) class name
  1304. * or the fully qualified class name
  1305. */
  1306. static Prin getPrin(String clazz, boolean fullClassName) {
  1307. for (int i = 0; i < PRIN_ARRAY.size(); i++) {
  1308. Prin next = PRIN_ARRAY.get(i);
  1309. if (fullClassName) {
  1310. if (next.FULL_CLASS.equals(clazz)) {
  1311. return next;
  1312. }
  1313. } else {
  1314. if (next.CLASS.equals(clazz)) {
  1315. return next;
  1316. }
  1317. }
  1318. }
  1319. return null;
  1320. }
  1321. /**
  1322. * ask user if they want to overwrite an existing file
  1323. */
  1324. void displayOverWriteFileDialog(String filename, int nextEvent) {
  1325. // find where the PolicyTool gui is
  1326. Point location = tw.getLocationOnScreen();
  1327. setBounds(location.x + 75, location.y + 100, 400, 150);
  1328. setLayout(new GridBagLayout());
  1329. // ask the user if they want to over write the existing file
  1330. MessageFormat form = new MessageFormat(PolicyTool.rb.getString
  1331. ("OK to overwrite existing file filename?"));
  1332. Object[] source = {filename};
  1333. Label label = new Label(form.format(source));
  1334. tw.addNewComponent(this, label, OW_LABEL,
  1335. 0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1336. tw.TOP_PADDING);
  1337. // OK button
  1338. Button button = new Button(PolicyTool.rb.getString("OK"));
  1339. button.addActionListener(new OverWriteFileOKButtonListener
  1340. (tool, tw, this, filename, nextEvent));
  1341. tw.addNewComponent(this, button, OW_OK_BUTTON,
  1342. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1343. tw.TOP_PADDING);
  1344. // Cancel button
  1345. // -- if the user hits cancel, do NOT go on to the next event
  1346. button = new Button(PolicyTool.rb.getString("Cancel"));
  1347. button.addActionListener(new CancelButtonListener(this));
  1348. tw.addNewComponent(this, button, OW_CANCEL_BUTTON,
  1349. 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1350. tw.TOP_PADDING);
  1351. setVisible(true);
  1352. }
  1353. /**
  1354. * pop up a dialog so the user can enter info to add a new PolicyEntry
  1355. * - if edit is TRUE, then the user is editing an existing entry
  1356. * and we should display the original info as well.
  1357. *
  1358. * - the other reason we need the 'edit' boolean is we need to know
  1359. * when we are adding a NEW policy entry. in this case, we can
  1360. * not simply update the existing entry, because it doesn't exist.
  1361. * we ONLY update the GUI listing/info, and then when the user
  1362. * finally clicks 'OK' or 'DONE', then we can collect that info
  1363. * and add it to the policy.
  1364. */
  1365. void displayPolicyEntryDialog(boolean edit) {
  1366. int listIndex = 0;
  1367. PolicyEntry entries[] = null;
  1368. TaggedList prinList = new TaggedList(3, false);
  1369. prinList.getAccessibleContext().setAccessibleName(
  1370. PolicyTool.rb.getString("Principal List"));
  1371. prinList.addActionListener
  1372. (new EditPrinButtonListener(tool, tw, this, edit));
  1373. TaggedList permList = new TaggedList(10, false);
  1374. permList.getAccessibleContext().setAccessibleName(
  1375. PolicyTool.rb.getString("Permission List"));
  1376. permList.addActionListener
  1377. (new EditPermButtonListener(tool, tw, this, edit));
  1378. // find where the PolicyTool gui is
  1379. Point location = tw.getLocationOnScreen();
  1380. setBounds(location.x + 75, location.y + 200, 650, 500);
  1381. setLayout(new GridBagLayout());
  1382. setResizable(true);
  1383. if (edit) {
  1384. // get the selected item
  1385. entries = tool.getEntry();
  1386. List policyList = (List)tw.getComponent(tw.MW_POLICY_LIST);
  1387. listIndex = policyList.getSelectedIndex();
  1388. // get principal list
  1389. LinkedList principals =
  1390. entries[listIndex].getGrantEntry().principals;
  1391. for (int i = 0; i < principals.size(); i++) {
  1392. String prinString = null;
  1393. PolicyParser.PrincipalEntry nextPrin =
  1394. (PolicyParser.PrincipalEntry)principals.get(i);
  1395. prinList.addTaggedItem(PrincipalEntryToUserFriendlyString(nextPrin), nextPrin);
  1396. }
  1397. // get permission list
  1398. Vector<PolicyParser.PermissionEntry> permissions =
  1399. entries[listIndex].getGrantEntry().permissionEntries;
  1400. for (int i = 0; i < permissions.size(); i++) {
  1401. String permString = null;
  1402. PolicyParser.PermissionEntry nextPerm =
  1403. permissions.elementAt(i);
  1404. permList.addTaggedItem(ToolDialog.PermissionEntryToUserFriendlyString(nextPerm), nextPerm);
  1405. }
  1406. }
  1407. // codebase label and textfield
  1408. Label label = new Label(PolicyTool.rb.getString("CodeBase:"));
  1409. tw.addNewComponent(this, label, PE_CODEBASE_LABEL,
  1410. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1411. TextField tf;
  1412. tf = (edit ?
  1413. new TextField(entries[listIndex].getGrantEntry().codeBase, 60) :
  1414. new TextField(60));
  1415. tf.getAccessibleContext().setAccessibleName(
  1416. PolicyTool.rb.getString("Code Base"));
  1417. tw.addNewComponent(this, tf, PE_CODEBASE_TEXTFIELD,
  1418. 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1419. // signedby label and textfield
  1420. label = new Label(PolicyTool.rb.getString("SignedBy:"));
  1421. tw.addNewComponent(this, label, PE_SIGNEDBY_LABEL,
  1422. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1423. tf = (edit ?
  1424. new TextField(entries[listIndex].getGrantEntry().signedBy, 60) :
  1425. new TextField(60));
  1426. tf.getAccessibleContext().setAccessibleName(
  1427. PolicyTool.rb.getString("Signed By:"));
  1428. tw.addNewComponent(this, tf, PE_SIGNEDBY_TEXTFIELD,
  1429. 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1430. // panel for principal buttons
  1431. Panel panel = new Panel();
  1432. panel.setLayout(new GridBagLayout());
  1433. Button button = new Button(PolicyTool.rb.getString("Add Principal"));
  1434. button.addActionListener
  1435. (new AddPrinButtonListener(tool, tw, this, edit));
  1436. tw.addNewComponent(panel, button, PE_ADD_PRIN_BUTTON,
  1437. 0, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
  1438. button = new Button(PolicyTool.rb.getString("Edit Principal"));
  1439. button.addActionListener(new EditPrinButtonListener
  1440. (tool, tw, this, edit));
  1441. tw.addNewComponent(panel, button, PE_EDIT_PRIN_BUTTON,
  1442. 1, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
  1443. button = new Button(PolicyTool.rb.getString("Remove Principal"));
  1444. button.addActionListener(new RemovePrinButtonListener
  1445. (tool, tw, this, edit));
  1446. tw.addNewComponent(panel, button, PE_REMOVE_PRIN_BUTTON,
  1447. 2, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
  1448. tw.addNewComponent(this, panel, PE_PANEL0,
  1449. 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.HORIZONTAL);
  1450. // principal label and list
  1451. label = new Label(PolicyTool.rb.getString("Principals:"));
  1452. tw.addNewComponent(this, label, PE_PRIN_LABEL,
  1453. 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1454. tw.BOTTOM_PADDING);
  1455. tw.addNewComponent(this, prinList, PE_PRIN_LIST,
  1456. 1, 3, 3, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1457. tw.BOTTOM_PADDING);
  1458. // panel for permission buttons
  1459. panel = new Panel();
  1460. panel.setLayout(new GridBagLayout());
  1461. button = new Button(PolicyTool.rb.getString(" Add Permission"));
  1462. button.addActionListener(new AddPermButtonListener
  1463. (tool, tw, this, edit));
  1464. tw.addNewComponent(panel, button, PE_ADD_PERM_BUTTON,
  1465. 0, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
  1466. button = new Button(PolicyTool.rb.getString(" Edit Permission"));
  1467. button.addActionListener(new EditPermButtonListener
  1468. (tool, tw, this, edit));
  1469. tw.addNewComponent(panel, button, PE_EDIT_PERM_BUTTON,
  1470. 1, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
  1471. button = new Button(PolicyTool.rb.getString("Remove Permission"));
  1472. button.addActionListener(new RemovePermButtonListener
  1473. (tool, tw, this, edit));
  1474. tw.addNewComponent(panel, button, PE_REMOVE_PERM_BUTTON,
  1475. 2, 0, 1, 1, 100.0, 0.0, GridBagConstraints.HORIZONTAL);
  1476. tw.addNewComponent(this, panel, PE_PANEL1,
  1477. 0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.HORIZONTAL,
  1478. tw.LITE_BOTTOM_PADDING);
  1479. // permission list
  1480. tw.addNewComponent(this, permList, PE_PERM_LIST,
  1481. 0, 5, 3, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1482. tw.BOTTOM_PADDING);
  1483. // panel for Done and Cancel buttons
  1484. panel = new Panel();
  1485. panel.setLayout(new GridBagLayout());
  1486. // Done Button
  1487. button = new Button(PolicyTool.rb.getString("Done"));
  1488. button.addActionListener
  1489. (new AddEntryDoneButtonListener(tool, tw, this, edit));
  1490. tw.addNewComponent(panel, button, PE_DONE_BUTTON,
  1491. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1492. tw.LR_PADDING);
  1493. // Cancel Button
  1494. button = new Button(PolicyTool.rb.getString("Cancel"));
  1495. button.addActionListener(new CancelButtonListener(this));
  1496. tw.addNewComponent(panel, button, PE_CANCEL_BUTTON,
  1497. 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1498. tw.LR_PADDING);
  1499. // add the panel
  1500. tw.addNewComponent(this, panel, PE_PANEL2,
  1501. 0, 6, 2, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
  1502. setVisible(true);
  1503. }
  1504. /**
  1505. * Read all the Policy information data in the dialog box
  1506. * and construct a PolicyEntry object with it.
  1507. */
  1508. PolicyEntry getPolicyEntryFromDialog()
  1509. throws InvalidParameterException, MalformedURLException,
  1510. NoSuchMethodException, ClassNotFoundException, InstantiationException,
  1511. IllegalAccessException, InvocationTargetException,
  1512. CertificateException, IOException, Exception {
  1513. // get the Codebase
  1514. TextField tf = (TextField)getComponent(PE_CODEBASE_TEXTFIELD);
  1515. String codebase = null;
  1516. if (tf.getText().trim().equals("") == false)
  1517. codebase = new String(tf.getText().trim());
  1518. // get the SignedBy
  1519. tf = (TextField)getComponent(PE_SIGNEDBY_TEXTFIELD);
  1520. String signedby = null;
  1521. if (tf.getText().trim().equals("") == false)
  1522. signedby = new String(tf.getText().trim());
  1523. // construct a new GrantEntry
  1524. PolicyParser.GrantEntry ge =
  1525. new PolicyParser.GrantEntry(signedby, codebase);
  1526. // get the new Principals
  1527. LinkedList<PolicyParser.PrincipalEntry> prins =
  1528. new LinkedList<PolicyParser.PrincipalEntry>();
  1529. TaggedList prinList = (TaggedList)getComponent(PE_PRIN_LIST);
  1530. for (int i = 0; i < prinList.getItemCount(); i++) {
  1531. prins.add((PolicyParser.PrincipalEntry)prinList.getObject(i));
  1532. }
  1533. ge.principals = prins;
  1534. // get the new Permissions
  1535. Vector<PolicyParser.PermissionEntry> perms =
  1536. new Vector<PolicyParser.PermissionEntry>();
  1537. TaggedList permList = (TaggedList)getComponent(PE_PERM_LIST);
  1538. for (int i = 0; i < permList.getItemCount(); i++) {
  1539. perms.addElement((PolicyParser.PermissionEntry)permList.getObject(i));
  1540. }
  1541. ge.permissionEntries = perms;
  1542. // construct a new PolicyEntry object
  1543. PolicyEntry entry = new PolicyEntry(tool, ge);
  1544. return entry;
  1545. }
  1546. /**
  1547. * display a dialog box for the user to enter KeyStore information
  1548. */
  1549. void keyStoreDialog(int mode) {
  1550. // find where the PolicyTool gui is
  1551. Point location = tw.getLocationOnScreen();
  1552. setBounds(location.x + 25, location.y + 100, 500, 300);
  1553. setLayout(new GridBagLayout());
  1554. if (mode == EDIT_KEYSTORE) {
  1555. // KeyStore label and textfield
  1556. Label label = new Label
  1557. (PolicyTool.rb.getString("KeyStore URL:"));
  1558. tw.addNewComponent(this, label, KSD_NAME_LABEL,
  1559. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1560. tw.BOTTOM_PADDING);
  1561. TextField tf = new TextField(tool.getKeyStoreName(), 30);
  1562. // URL to U R L, so that accessibility reader will pronounce well
  1563. tf.getAccessibleContext().setAccessibleName(
  1564. PolicyTool.rb.getString("KeyStore U R L:"));
  1565. tw.addNewComponent(this, tf, KSD_NAME_TEXTFIELD,
  1566. 1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1567. tw.BOTTOM_PADDING);
  1568. // KeyStore type and textfield
  1569. label = new Label(PolicyTool.rb.getString("KeyStore Type:"));
  1570. tw.addNewComponent(this, label, KSD_TYPE_LABEL,
  1571. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1572. tw.BOTTOM_PADDING);
  1573. tf = new TextField(tool.getKeyStoreType(), 30);
  1574. tf.getAccessibleContext().setAccessibleName(
  1575. PolicyTool.rb.getString("KeyStore Type:"));
  1576. tw.addNewComponent(this, tf, KSD_TYPE_TEXTFIELD,
  1577. 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1578. tw.BOTTOM_PADDING);
  1579. // KeyStore provider and textfield
  1580. label = new Label(PolicyTool.rb.getString
  1581. ("KeyStore Provider:"));
  1582. tw.addNewComponent(this, label, KSD_PROVIDER_LABEL,
  1583. 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1584. tw.BOTTOM_PADDING);
  1585. tf = new TextField(tool.getKeyStoreProvider(), 30);
  1586. tf.getAccessibleContext().setAccessibleName(
  1587. PolicyTool.rb.getString("KeyStore Provider:"));
  1588. tw.addNewComponent(this, tf, KSD_PROVIDER_TEXTFIELD,
  1589. 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1590. tw.BOTTOM_PADDING);
  1591. // KeyStore password URL and textfield
  1592. label = new Label(PolicyTool.rb.getString
  1593. ("KeyStore Password URL:"));
  1594. tw.addNewComponent(this, label, KSD_PWD_URL_LABEL,
  1595. 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1596. tw.BOTTOM_PADDING);
  1597. tf = new TextField(tool.getKeyStorePwdURL(), 30);
  1598. tf.getAccessibleContext().setAccessibleName(
  1599. PolicyTool.rb.getString("KeyStore Password U R L:"));
  1600. tw.addNewComponent(this, tf, KSD_PWD_URL_TEXTFIELD,
  1601. 1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1602. tw.BOTTOM_PADDING);
  1603. // OK button
  1604. Button okButton = new Button(PolicyTool.rb.getString("OK"));
  1605. okButton.addActionListener
  1606. (new ChangeKeyStoreOKButtonListener(tool, tw, this));
  1607. tw.addNewComponent(this, okButton, KSD_OK_BUTTON,
  1608. 0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
  1609. // cancel button
  1610. Button cancelButton = new Button(PolicyTool.rb.getString("Cancel"));
  1611. cancelButton.addActionListener(new CancelButtonListener(this));
  1612. tw.addNewComponent(this, cancelButton, KSD_CANCEL_BUTTON,
  1613. 1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL);
  1614. }
  1615. setVisible(true);
  1616. }
  1617. /**
  1618. * display a dialog box for the user to input Principal info
  1619. *
  1620. * if editPolicyEntry is false, then we are adding Principals to
  1621. * a new PolicyEntry, and we only update the GUI listing
  1622. * with the new Principal.
  1623. *
  1624. * if edit is true, then we are editing an existing Policy entry.
  1625. */
  1626. void displayPrincipalDialog(boolean editPolicyEntry, boolean edit) {
  1627. PolicyParser.PrincipalEntry editMe = null;
  1628. // get the Principal selected from the Principal List
  1629. TaggedList prinList = (TaggedList)getComponent(PE_PRIN_LIST);
  1630. int prinIndex = prinList.getSelectedIndex();
  1631. if (edit) {
  1632. editMe = (PolicyParser.PrincipalEntry)prinList.getObject(prinIndex);
  1633. }
  1634. ToolDialog newTD = new ToolDialog
  1635. (PolicyTool.rb.getString("Principals"), tool, tw, true);
  1636. newTD.addWindowListener(new ChildWindowListener(newTD));
  1637. // find where the PolicyTool gui is
  1638. Point location = getLocationOnScreen();
  1639. newTD.setBounds(location.x + 50, location.y + 100, 650, 190);
  1640. newTD.setLayout(new GridBagLayout());
  1641. newTD.setResizable(true);
  1642. // description label
  1643. Label label = (edit ?
  1644. new Label(PolicyTool.rb.getString(" Edit Principal:")) :
  1645. new Label(PolicyTool.rb.getString(" Add New Principal:")));
  1646. tw.addNewComponent(newTD, label, PRD_DESC_LABEL,
  1647. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1648. tw.TOP_BOTTOM_PADDING);
  1649. // principal choice
  1650. Choice choice = new Choice();
  1651. choice.add(PRIN_TYPE);
  1652. choice.getAccessibleContext().setAccessibleName(PRIN_TYPE);
  1653. for (int i = 0; i < PRIN_ARRAY.size(); i++) {
  1654. Prin next = PRIN_ARRAY.get(i);
  1655. choice.add(next.CLASS);
  1656. }
  1657. choice.addItemListener(new PrincipalTypeMenuListener(newTD));
  1658. if (edit) {
  1659. if (PolicyParser.PrincipalEntry.WILDCARD_CLASS.equals
  1660. (editMe.getPrincipalClass())) {
  1661. choice.select(PRIN_TYPE);
  1662. } else {
  1663. Prin inputPrin = getPrin(editMe.getPrincipalClass(), true);
  1664. if (inputPrin != null) {
  1665. choice.select(inputPrin.CLASS);
  1666. }
  1667. }
  1668. }
  1669. tw.addNewComponent(newTD, choice, PRD_PRIN_CHOICE,
  1670. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1671. tw.LR_PADDING);
  1672. // principal textfield
  1673. TextField tf;
  1674. tf = (edit ?
  1675. new TextField(editMe.getDisplayClass(), 30) :
  1676. new TextField(30));
  1677. tf.getAccessibleContext().setAccessibleName(PRIN_TYPE);
  1678. tw.addNewComponent(newTD, tf, PRD_PRIN_TEXTFIELD,
  1679. 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1680. tw.LR_PADDING);
  1681. // name label and textfield
  1682. label = new Label(PRIN_NAME);
  1683. tf = (edit ?
  1684. new TextField(editMe.getDisplayName(), 40) :
  1685. new TextField(40));
  1686. tf.getAccessibleContext().setAccessibleName(PRIN_NAME);
  1687. tw.addNewComponent(newTD, label, PRD_NAME_LABEL,
  1688. 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1689. tw.LR_PADDING);
  1690. tw.addNewComponent(newTD, tf, PRD_NAME_TEXTFIELD,
  1691. 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1692. tw.LR_PADDING);
  1693. // OK button
  1694. Button okButton = new Button(PolicyTool.rb.getString("OK"));
  1695. okButton.addActionListener(
  1696. new NewPolicyPrinOKButtonListener
  1697. (tool, tw, this, newTD, edit));
  1698. tw.addNewComponent(newTD, okButton, PRD_OK_BUTTON,
  1699. 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1700. tw.TOP_BOTTOM_PADDING);
  1701. // cancel button
  1702. Button cancelButton = new Button(PolicyTool.rb.getString("Cancel"));
  1703. cancelButton.addActionListener(new CancelButtonListener(newTD));
  1704. tw.addNewComponent(newTD, cancelButton, PRD_CANCEL_BUTTON,
  1705. 1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1706. tw.TOP_BOTTOM_PADDING);
  1707. newTD.setVisible(true);
  1708. }
  1709. /**
  1710. * display a dialog box for the user to input Permission info
  1711. *
  1712. * if editPolicyEntry is false, then we are adding Permissions to
  1713. * a new PolicyEntry, and we only update the GUI listing
  1714. * with the new Permission.
  1715. *
  1716. * if edit is true, then we are editing an existing Permission entry.
  1717. */
  1718. void displayPermissionDialog(boolean editPolicyEntry, boolean edit) {
  1719. PolicyParser.PermissionEntry editMe = null;
  1720. // get the Permission selected from the Permission List
  1721. TaggedList permList = (TaggedList)getComponent(PE_PERM_LIST);
  1722. int permIndex = permList.getSelectedIndex();
  1723. if (edit) {
  1724. editMe = (PolicyParser.PermissionEntry)permList.getObject(permIndex);
  1725. }
  1726. ToolDialog newTD = new ToolDialog
  1727. (PolicyTool.rb.getString("Permissions"), tool, tw, true);
  1728. newTD.addWindowListener(new ChildWindowListener(newTD));
  1729. // find where the PolicyTool gui is
  1730. Point location = getLocationOnScreen();
  1731. newTD.setBounds(location.x + 50, location.y + 100, 700, 250);
  1732. newTD.setLayout(new GridBagLayout());
  1733. newTD.setResizable(true);
  1734. // description label
  1735. Label label = (edit ?
  1736. new Label(PolicyTool.rb.getString(" Edit Permission:")) :
  1737. new Label(PolicyTool.rb.getString(" Add New Permission:")));
  1738. tw.addNewComponent(newTD, label, PD_DESC_LABEL,
  1739. 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1740. tw.TOP_BOTTOM_PADDING);
  1741. // permission choice (added in alphabetical order)
  1742. Choice choice = new Choice();
  1743. choice.add(PERM);
  1744. choice.getAccessibleContext().setAccessibleName(PERM);
  1745. for (int i = 0; i < PERM_ARRAY.size(); i++) {
  1746. Perm next = PERM_ARRAY.get(i);
  1747. choice.add(next.CLASS);
  1748. }
  1749. choice.addItemListener(new PermissionMenuListener(newTD));
  1750. tw.addNewComponent(newTD, choice, PD_PERM_CHOICE,
  1751. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1752. tw.LR_PADDING);
  1753. // permission textfield
  1754. TextField tf;
  1755. tf = (edit ? new TextField(editMe.permission, 30) : new TextField(30));
  1756. tf.getAccessibleContext().setAccessibleName(PERM);
  1757. if (edit) {
  1758. Perm inputPerm = getPerm(editMe.permission, true);
  1759. if (inputPerm != null) {
  1760. choice.select(inputPerm.CLASS);
  1761. }
  1762. }
  1763. tw.addNewComponent(newTD, tf, PD_PERM_TEXTFIELD,
  1764. 1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1765. tw.LR_PADDING);
  1766. // name label and textfield
  1767. choice = new Choice();
  1768. choice.add(PERM_NAME);
  1769. choice.getAccessibleContext().setAccessibleName(PERM_NAME);
  1770. choice.addItemListener(new PermissionNameMenuListener(newTD));
  1771. tf = (edit ? new TextField(editMe.name, 40) : new TextField(40));
  1772. tf.getAccessibleContext().setAccessibleName(PERM_NAME);
  1773. if (edit) {
  1774. setPermissionNames(getPerm(editMe.permission, true), choice, tf);
  1775. }
  1776. tw.addNewComponent(newTD, choice, PD_NAME_CHOICE,
  1777. 0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1778. tw.LR_PADDING);
  1779. tw.addNewComponent(newTD, tf, PD_NAME_TEXTFIELD,
  1780. 1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1781. tw.LR_PADDING);
  1782. // actions label and textfield
  1783. choice = new Choice();
  1784. choice.add(PERM_ACTIONS);
  1785. choice.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
  1786. choice.addItemListener(new PermissionActionsMenuListener(newTD));
  1787. tf = (edit ? new TextField(editMe.action, 40) : new TextField(40));
  1788. tf.getAccessibleContext().setAccessibleName(PERM_ACTIONS);
  1789. if (edit) {
  1790. setPermissionActions(getPerm(editMe.permission, true), choice, tf);
  1791. }
  1792. tw.addNewComponent(newTD, choice, PD_ACTIONS_CHOICE,
  1793. 0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1794. tw.LR_PADDING);
  1795. tw.addNewComponent(newTD, tf, PD_ACTIONS_TEXTFIELD,
  1796. 1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1797. tw.LR_PADDING);
  1798. // signedby label and textfield
  1799. label = new Label(PolicyTool.rb.getString("Signed By:"));
  1800. tw.addNewComponent(newTD, label, PD_SIGNEDBY_LABEL,
  1801. 0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1802. tw.LR_PADDING);
  1803. tf = (edit ? new TextField(editMe.signedBy, 40) : new TextField(40));
  1804. tf.getAccessibleContext().setAccessibleName(
  1805. PolicyTool.rb.getString("Signed By:"));
  1806. tw.addNewComponent(newTD, tf, PD_SIGNEDBY_TEXTFIELD,
  1807. 1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1808. tw.LR_PADDING);
  1809. // OK button
  1810. Button okButton = new Button(PolicyTool.rb.getString("OK"));
  1811. okButton.addActionListener(
  1812. new NewPolicyPermOKButtonListener
  1813. (tool, tw, this, newTD, edit));
  1814. tw.addNewComponent(newTD, okButton, PD_OK_BUTTON,
  1815. 0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1816. tw.TOP_BOTTOM_PADDING);
  1817. // cancel button
  1818. Button cancelButton = new Button(PolicyTool.rb.getString("Cancel"));
  1819. cancelButton.addActionListener(new CancelButtonListener(newTD));
  1820. tw.addNewComponent(newTD, cancelButton, PD_CANCEL_BUTTON,
  1821. 1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.VERTICAL,
  1822. tw.TOP_BOTTOM_PADDING);
  1823. newTD.setVisible(true);
  1824. }
  1825. /**
  1826. * construct a Principal object from the Principal Info Dialog Box
  1827. */
  1828. PolicyParser.PrincipalEntry getPrinFromDialog() throws Exception {
  1829. TextField tf = (TextField)getComponent(PRD_PRIN_TEXTFIELD);
  1830. String pclass = new String(tf.getText().trim());
  1831. tf = (TextField)getComponent(PRD_NAME_TEXTFIELD);
  1832. String pname = new String(tf.getText().trim());
  1833. if (pclass.equals("*")) {
  1834. pclass = PolicyParser.PrincipalEntry.WILDCARD_CLASS;
  1835. }
  1836. if (pname.equals("*")) {
  1837. pname = PolicyParser.PrincipalEntry.WILDCARD_NAME;
  1838. }
  1839. PolicyParser.PrincipalEntry pppe = null;
  1840. if ((pclass.equals(PolicyParser.PrincipalEntry.WILDCARD_CLASS)) &&
  1841. (!pname.equals(PolicyParser.PrincipalEntry.WILDCARD_NAME))) {
  1842. throw new Exception
  1843. (PolicyTool.rb.getString("Cannot Specify Principal " +
  1844. "with a Wildcard Class without a Wildcard Name"));
  1845. } else if (pname.equals("")) {
  1846. throw new Exception
  1847. (PolicyTool.rb.getString("Cannot Specify Principal " +
  1848. "without a Name"));
  1849. } else if (pclass.equals("")) {
  1850. // make this consistent with what PolicyParser does
  1851. // when it sees an empty principal class
  1852. pclass = PolicyParser.REPLACE_NAME;
  1853. tool.warnings.addElement(
  1854. "Warning: Principal name '" + pname +
  1855. "' specified without a Principal class.\n" +
  1856. "\t'" + pname + "' will be interpreted " +
  1857. "as a key store alias.\n" +
  1858. "\tThe final principal class will be " +
  1859. ToolDialog.X500_PRIN_CLASS + ".\n" +
  1860. "\tThe final principal name will be " +
  1861. "determined by the following:\n" +
  1862. "\n" +
  1863. "\tIf the key store entry identified by '"
  1864. + pname + "'\n" +
  1865. "\tis a key entry, then the principal name will be\n" +
  1866. "\tthe subject distinguished name from the first\n" +
  1867. "\tcertificate in the entry's certificate chain.\n" +
  1868. "\n" +
  1869. "\tIf the key store entry identified by '" +
  1870. pname + "'\n" +
  1871. "\tis a trusted certificate entry, then the\n" +
  1872. "\tprincipal name will be the subject distinguished\n" +
  1873. "\tname from the trusted public key certificate.");
  1874. tw.displayStatusDialog(this,
  1875. "'" + pname + "' will be interpreted as a key " +
  1876. "store alias. View Warning Log for details.");
  1877. }
  1878. return new PolicyParser.PrincipalEntry(pclass, pname);
  1879. }
  1880. /**
  1881. * construct a Permission object from the Permission Info Dialog Box
  1882. */
  1883. PolicyParser.PermissionEntry getPermFromDialog() {
  1884. TextField tf = (TextField)getComponent(PD_PERM_TEXTFIELD);
  1885. String permission = new String(tf.getText().trim());
  1886. tf = (TextField)getComponent(PD_NAME_TEXTFIELD);
  1887. String name = null;
  1888. if (tf.getText().trim().equals("") == false)
  1889. name = new String(tf.getText().trim());
  1890. if (permission.equals("") ||
  1891. (!permission.equals(ALL_PERM_CLASS) && name == null)) {
  1892. throw new InvalidParameterException(PolicyTool.rb.getString
  1893. ("Permission and Target Name must have a value"));
  1894. }
  1895. // When the permission is FilePermission, we need to check the name
  1896. // to make sure it's not escaped. We believe --
  1897. //
  1898. // String name.lastIndexOf("\\\\")
  1899. // ---------------- ------------------------
  1900. // c:\foo\bar -1, legal
  1901. // c:\\foo\\bar 2, illegal
  1902. // \\server\share 0, legal
  1903. // \\\\server\share 2, illegal
  1904. if (permission.equals(FILE_PERM_CLASS) && name.lastIndexOf("\\\\") > 0) {
  1905. char result = tw.displayYesNoDialog(this,
  1906. PolicyTool.rb.getString("Warning"),
  1907. PolicyTool.rb.getString(
  1908. "Warning: File name may include escaped backslash characters. " +
  1909. "It is not necessary to escape backslash characters " +
  1910. "(the tool escapes characters as necessary when writing " +
  1911. "the policy contents to the persistent store).\n\n" +
  1912. "Click on Retain to retain the entered name, or click on " +
  1913. "Edit to edit the name."),
  1914. PolicyTool.rb.getString("Retain"),
  1915. PolicyTool.rb.getString("Edit")
  1916. );
  1917. if (result != 'Y') {
  1918. // an invisible exception
  1919. throw new NoDisplayException();
  1920. }
  1921. }
  1922. // get the Actions
  1923. tf = (TextField)getComponent(PD_ACTIONS_TEXTFIELD);
  1924. String actions = null;
  1925. if (tf.getText().trim().equals("") == false)
  1926. actions = new String(tf.getText().trim());
  1927. // get the Signed By
  1928. tf = (TextField)getComponent(PD_SIGNEDBY_TEXTFIELD);
  1929. String signedBy = null;
  1930. if (tf.getText().trim().equals("") == false)
  1931. signedBy = new String(tf.getText().trim());
  1932. PolicyParser.PermissionEntry pppe = new PolicyParser.PermissionEntry
  1933. (permission, name, actions);
  1934. pppe.signedBy = signedBy;
  1935. // see if the signers have public keys
  1936. if (signedBy != null) {
  1937. String signers[] = tool.parseSigners(pppe.signedBy);
  1938. for (int i = 0; i < signers.length; i++) {
  1939. try {
  1940. PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
  1941. if (pubKey == null) {
  1942. MessageFormat form = new MessageFormat
  1943. (PolicyTool.rb.getString
  1944. ("Warning: A public key for alias " +
  1945. "'signers[i]' does not exist. " +
  1946. "Make sure a KeyStore is properly configured."));
  1947. Object[] source = {signers[i]};
  1948. tool.warnings.addElement(form.format(source));
  1949. tw.displayStatusDialog(this, form.format(source));
  1950. }
  1951. } catch (Exception e) {
  1952. tw.displayErrorDialog(this, e);
  1953. }
  1954. }
  1955. }
  1956. return pppe;
  1957. }
  1958. /**
  1959. * confirm that the user REALLY wants to remove the Policy Entry
  1960. */
  1961. void displayConfirmRemovePolicyEntry() {
  1962. // find the entry to be removed
  1963. List list = (List)tw.getComponent(tw.MW_POLICY_LIST);
  1964. int index = list.getSelectedIndex();
  1965. PolicyEntry entries[] = tool.getEntry();
  1966. // find where the PolicyTool gui is
  1967. Point location = tw.getLocationOnScreen();
  1968. setBounds(location.x + 25, location.y + 100, 600, 400);
  1969. setLayout(new GridBagLayout());
  1970. // ask the user do they really want to do this?
  1971. Label label = new Label
  1972. (PolicyTool.rb.getString("Remove this Policy Entry?"));
  1973. tw.addNewComponent(this, label, CRPE_LABEL1,
  1974. 0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  1975. tw.BOTTOM_PADDING);
  1976. // display the policy entry
  1977. label = new Label(entries[index].codebaseToString());
  1978. tw.addNewComponent(this, label, CRPE_LABEL2,
  1979. 0, 1, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1980. label = new Label(entries[index].principalsToString().trim());
  1981. tw.addNewComponent(this, label, CRPE_LABEL2+1,
  1982. 0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  1983. Vector<PolicyParser.PermissionEntry> perms =
  1984. entries[index].getGrantEntry().permissionEntries;
  1985. for (int i = 0; i < perms.size(); i++) {
  1986. PolicyParser.PermissionEntry nextPerm = perms.elementAt(i);
  1987. String permString = ToolDialog.PermissionEntryToUserFriendlyString(nextPerm);
  1988. label = new Label(" " + permString);
  1989. if (i == (perms.size()-1)) {
  1990. tw.addNewComponent(this, label, CRPE_LABEL2 + 2 + i,
  1991. 1, 3 + i, 1, 1, 0.0, 0.0,
  1992. GridBagConstraints.BOTH, tw.BOTTOM_PADDING);
  1993. } else {
  1994. tw.addNewComponent(this, label, CRPE_LABEL2 + 2 + i,
  1995. 1, 3 + i, 1, 1, 0.0, 0.0,
  1996. GridBagConstraints.BOTH);
  1997. }
  1998. }
  1999. // add OK/CANCEL buttons in a new panel
  2000. Panel panel = new Panel();
  2001. panel.setLayout(new GridBagLayout());
  2002. // OK button
  2003. Button okButton = new Button(PolicyTool.rb.getString("OK"));
  2004. okButton.addActionListener
  2005. (new ConfirmRemovePolicyEntryOKButtonListener(tool, tw, this));
  2006. tw.addNewComponent(panel, okButton, CRPE_PANEL_OK,
  2007. 0, 0, 1, 1, 0.0, 0.0,
  2008. GridBagConstraints.VERTICAL, tw.LR_PADDING);
  2009. // cancel button
  2010. Button cancelButton = new Button(PolicyTool.rb.getString("Cancel"));
  2011. cancelButton.addActionListener(new CancelButtonListener(this));
  2012. tw.addNewComponent(panel, cancelButton, CRPE_PANEL_CANCEL,
  2013. 1, 0, 1, 1, 0.0, 0.0,
  2014. GridBagConstraints.VERTICAL, tw.LR_PADDING);
  2015. tw.addNewComponent(this, panel, CRPE_LABEL2 + 2 + perms.size(),
  2016. 0, 3 + perms.size(), 2, 1, 0.0, 0.0,
  2017. GridBagConstraints.VERTICAL, tw.TOP_BOTTOM_PADDING);
  2018. pack();
  2019. setVisible(true);
  2020. }
  2021. /**
  2022. * perform SAVE AS
  2023. */
  2024. void displaySaveAsDialog(int nextEvent) {
  2025. // pop up a dialog box for the user to enter a filename.
  2026. FileDialog fd = new FileDialog
  2027. (tw, PolicyTool.rb.getString("Save As"), FileDialog.SAVE);
  2028. fd.addWindowListener(new WindowAdapter() {
  2029. public void windowClosing(WindowEvent e) {
  2030. e.getWindow().setVisible(false);
  2031. }
  2032. });
  2033. fd.setVisible(true);
  2034. // see if the user hit cancel
  2035. if (fd.getFile() == null ||
  2036. fd.getFile().equals(""))
  2037. return;
  2038. // get the entered filename
  2039. String filename = new String(fd.getDirectory() + fd.getFile());
  2040. fd.dispose();
  2041. // see if the file already exists
  2042. File saveAsFile = new File(filename);
  2043. if (saveAsFile.exists()) {
  2044. // display a dialog box for the user to enter policy info
  2045. ToolDialog td = new ToolDialog
  2046. (PolicyTool.rb.getString("Overwrite File"), tool, tw, true);
  2047. td.displayOverWriteFileDialog(filename, nextEvent);
  2048. } else {
  2049. try {
  2050. // save the policy entries to a file
  2051. tool.savePolicy(filename);
  2052. // display status
  2053. MessageFormat form = new MessageFormat(PolicyTool.rb.getString
  2054. ("Policy successfully written to filename"));
  2055. Object[] source = {filename};
  2056. tw.displayStatusDialog(null, form.format(source));
  2057. // display the new policy filename
  2058. TextField newFilename = (TextField)tw.getComponent
  2059. (tw.MW_FILENAME_TEXTFIELD);
  2060. newFilename.setText(filename);
  2061. tw.setVisible(true);
  2062. // now continue with the originally requested command
  2063. // (QUIT, NEW, or OPEN)
  2064. userSaveContinue(tool, tw, this, nextEvent);
  2065. } catch (FileNotFoundException fnfe) {
  2066. if (filename == null || filename.equals("")) {
  2067. tw.displayErrorDialog(null, new FileNotFoundException
  2068. (PolicyTool.rb.getString("null filename")));
  2069. } else {
  2070. tw.displayErrorDialog(null, fnfe);
  2071. }
  2072. } catch (Exception ee) {
  2073. tw.displayErrorDialog(null, ee);
  2074. }
  2075. }
  2076. }
  2077. /**
  2078. * ask user if they want to save changes
  2079. */
  2080. void displayUserSave(int select) {
  2081. if (tool.modified == true) {
  2082. // find where the PolicyTool gui is
  2083. Point location = tw.getLocationOnScreen();
  2084. setBounds(location.x + 75, location.y + 100, 400, 150);
  2085. setLayout(new GridBagLayout());
  2086. Label label = new Label
  2087. (PolicyTool.rb.getString("Save changes?"));
  2088. tw.addNewComponent(this, label, USC_LABEL,
  2089. 0, 0, 3, 1, 0.0, 0.0, GridBagConstraints.BOTH,
  2090. tw.L_TOP_BOTTOM_PADDING);
  2091. Panel panel = new Panel();
  2092. panel.setLayout(new GridBagLayout());
  2093. Button yesButton = new Button(PolicyTool.rb.getString("Yes"));
  2094. yesButton.addActionListener
  2095. (new UserSaveYesButtonListener(this, tool, tw, select));
  2096. tw.addNewComponent(panel, yesButton, USC_YES_BUTTON,
  2097. 0, 0, 1, 1, 0.0, 0.0,
  2098. GridBagConstraints.VERTICAL,
  2099. tw.LR_BOTTOM_PADDING);
  2100. Button noButton = new Button(PolicyTool.rb.getString("No"));
  2101. noButton.addActionListener
  2102. (new UserSaveNoButtonListener(this, tool, tw, select));
  2103. tw.addNewComponent(panel, noButton, USC_NO_BUTTON,
  2104. 1, 0, 1, 1, 0.0, 0.0,
  2105. GridBagConstraints.VERTICAL,
  2106. tw.LR_BOTTOM_PADDING);
  2107. Button cancelButton = new Button(PolicyTool.rb.getString("Cancel"));
  2108. cancelButton.addActionListener
  2109. (new UserSaveCancelButtonListener(this));
  2110. tw.addNewComponent(panel, cancelButton, USC_CANCEL_BUTTON,
  2111. 2, 0, 1, 1, 0.0, 0.0,
  2112. GridBagConstraints.VERTICAL,
  2113. tw.LR_BOTTOM_PADDING);
  2114. tw.addNewComponent(this, panel, USC_PANEL,
  2115. 0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.BOTH);
  2116. pack();
  2117. setVisible(true);
  2118. } else {
  2119. // just do the original request (QUIT, NEW, or OPEN)
  2120. userSaveContinue(tool, tw, this, select);
  2121. }
  2122. }
  2123. /**
  2124. * when the user sees the 'YES', 'NO', 'CANCEL' buttons on the
  2125. * displayUserSave dialog, and the click on one of them,
  2126. * we need to continue the originally requested action
  2127. * (either QUITting, opening NEW policy file, or OPENing an existing
  2128. * policy file. do that now.
  2129. */
  2130. void userSaveContinue(PolicyTool tool, ToolWindow tw,
  2131. ToolDialog us, int select) {
  2132. // now either QUIT, open a NEW policy file, or OPEN an existing policy
  2133. switch(select) {
  2134. case ToolDialog.QUIT:
  2135. tw.setVisible(false);
  2136. tw.dispose();
  2137. System.exit(0);
  2138. case ToolDialog.NEW:
  2139. try {
  2140. tool.openPolicy(null);
  2141. } catch (Exception ee) {
  2142. tool.modified = false;
  2143. tw.displayErrorDialog(null, ee);
  2144. }
  2145. // display the policy entries via the policy list textarea
  2146. List list = new List(40, false);
  2147. list.addActionListener(new PolicyListListener(tool, tw));
  2148. tw.replacePolicyList(list);
  2149. // display null policy filename and keystore
  2150. TextField newFilename = (TextField)
  2151. tw.getComponent(tw.MW_FILENAME_TEXTFIELD);
  2152. newFilename.setText("");
  2153. tw.setVisible(true);
  2154. break;
  2155. case ToolDialog.OPEN:
  2156. // pop up a dialog box for the user to enter a filename.
  2157. FileDialog fd = new FileDialog
  2158. (tw, PolicyTool.rb.getString("Open"), FileDialog.LOAD);
  2159. fd.addWindowListener(new WindowAdapter() {
  2160. public void windowClosing(WindowEvent e) {
  2161. e.getWindow().setVisible(false);
  2162. }
  2163. });
  2164. fd.setVisible(true);
  2165. // see if the user hit 'cancel'
  2166. if (fd.getFile() == null ||
  2167. fd.getFile().equals(""))
  2168. return;
  2169. // get the entered filename
  2170. String policyFile = new String(fd.getDirectory() + fd.getFile());
  2171. try {
  2172. // open the policy file
  2173. tool.openPolicy(policyFile);
  2174. // display the policy entries via the policy list textarea
  2175. list = new List(40, false);
  2176. list.addActionListener(new PolicyListListener(tool, tw));
  2177. PolicyEntry entries[] = tool.getEntry();
  2178. if (entries != null) {
  2179. for (int i = 0; i < entries.length; i++)
  2180. list.add(entries[i].headerToString());
  2181. }
  2182. tw.replacePolicyList(list);
  2183. tool.modified = false;
  2184. // display the new policy filename
  2185. newFilename = (TextField)
  2186. tw.getComponent(tw.MW_FILENAME_TEXTFIELD);
  2187. newFilename.setText(policyFile);
  2188. tw.setVisible(true);
  2189. // inform user of warnings
  2190. if (tool.newWarning == true) {
  2191. tw.displayStatusDialog(null, PolicyTool.rb.getString
  2192. ("Errors have occurred while opening the " +
  2193. "policy configuration. View the Warning Log " +
  2194. "for more information."));
  2195. }
  2196. } catch (Exception e) {
  2197. // add blank policy listing
  2198. list = new List(40, false);
  2199. list.addActionListener(new PolicyListListener(tool, tw));
  2200. tw.replacePolicyList(list);
  2201. tool.setPolicyFileName(null);
  2202. tool.modified = false;
  2203. // display a null policy filename
  2204. newFilename = (TextField)
  2205. tw.getComponent(tw.MW_FILENAME_TEXTFIELD);
  2206. newFilename.setText("");
  2207. tw.setVisible(true);
  2208. // display the error
  2209. MessageFormat form = new MessageFormat(PolicyTool.rb.getString
  2210. ("Could not open policy file: policyFile: e.toString()"));
  2211. Object[] source = {policyFile, e.toString()};
  2212. tw.displayErrorDialog(null, form.format(source));
  2213. }
  2214. break;
  2215. }
  2216. }
  2217. /**
  2218. * Return a Menu list of names for a given permission
  2219. *
  2220. * If inputPerm's TARGETS are null, then this means TARGETS are
  2221. * not allowed to be entered (and the TextField is set to be
  2222. * non-editable).
  2223. *
  2224. * If TARGETS are valid but there are no standard ones
  2225. * (user must enter them by hand) then the TARGETS array may be empty
  2226. * (and of course non-null).
  2227. */
  2228. void setPermissionNames(Perm inputPerm, Choice names, TextField field) {
  2229. names.removeAll();
  2230. names.add(PERM_NAME);
  2231. if (inputPerm == null) {
  2232. // custom permission
  2233. field.setEditable(true);
  2234. } else if (inputPerm.TARGETS == null) {
  2235. // standard permission with no targets
  2236. field.setEditable(false);
  2237. } else {
  2238. // standard permission with standard targets
  2239. field.setEditable(true);
  2240. for (int i = 0; i < inputPerm.TARGETS.length; i++) {
  2241. names.add(inputPerm.TARGETS[i]);
  2242. }
  2243. }
  2244. }
  2245. /**
  2246. * Return a Menu list of actions for a given permission
  2247. *
  2248. * If inputPerm's ACTIONS are null, then this means ACTIONS are
  2249. * not allowed to be entered (and the TextField is set to be
  2250. * non-editable). This is typically true for BasicPermissions.
  2251. *
  2252. * If ACTIONS are valid but there are no standard ones
  2253. * (user must enter them by hand) then the ACTIONS array may be empty
  2254. * (and of course non-null).
  2255. */
  2256. void setPermissionActions(Perm inputPerm, Choice actions, TextField field) {
  2257. actions.removeAll();
  2258. actions.add(PERM_ACTIONS);
  2259. if (inputPerm == null) {
  2260. // custom permission
  2261. field.setEditable(true);
  2262. } else if (inputPerm.ACTIONS == null) {
  2263. // standard permission with no actions
  2264. field.setEditable(false);
  2265. } else {
  2266. // standard permission with standard actions
  2267. field.setEditable(true);
  2268. for (int i = 0; i < inputPerm.ACTIONS.length; i++) {
  2269. actions.add(inputPerm.ACTIONS[i]);
  2270. }
  2271. }
  2272. }
  2273. static String PermissionEntryToUserFriendlyString(PolicyParser.PermissionEntry pppe) {
  2274. String result = pppe.permission;
  2275. if (pppe.name != null) {
  2276. result += " " + pppe.name;
  2277. }
  2278. if (pppe.action != null) {
  2279. result += ", \"" + pppe.action + "\"";
  2280. }
  2281. if (pppe.signedBy != null) {
  2282. result += ", signedBy " + pppe.signedBy;
  2283. }
  2284. return result;
  2285. }
  2286. static String PrincipalEntryToUserFriendlyString(PolicyParser.PrincipalEntry pppe) {
  2287. StringWriter sw = new StringWriter();
  2288. PrintWriter pw = new PrintWriter(sw);
  2289. pppe.write(pw);
  2290. return sw.toString();
  2291. }
  2292. }
  2293. /**
  2294. * Event handler for the PolicyTool window
  2295. */
  2296. class ToolWindowListener implements WindowListener {
  2297. private ToolWindow tw;
  2298. ToolWindowListener(ToolWindow tw) {
  2299. this.tw = tw;
  2300. }
  2301. public void windowOpened(WindowEvent we) {
  2302. }
  2303. public void windowClosing(WindowEvent we) {
  2304. // XXX
  2305. // should we ask user if they want to save changes?
  2306. // (we do if they choose the Menu->Exit)
  2307. // seems that if they kill the application by hand,
  2308. // we don't have to ask.
  2309. tw.setVisible(false);
  2310. tw.dispose();
  2311. System.exit(0);
  2312. }
  2313. public void windowClosed(WindowEvent we) {
  2314. System.exit(0);
  2315. }
  2316. public void windowIconified(WindowEvent we) {
  2317. }
  2318. public void windowDeiconified(WindowEvent we) {
  2319. }
  2320. public void windowActivated(WindowEvent we) {
  2321. }
  2322. public void windowDeactivated(WindowEvent we) {
  2323. }
  2324. }
  2325. /**
  2326. * Event handler for the Policy List
  2327. */
  2328. class PolicyListListener implements ActionListener {
  2329. private PolicyTool tool;
  2330. private ToolWindow tw;
  2331. PolicyListListener(PolicyTool tool, ToolWindow tw) {
  2332. this.tool = tool;
  2333. this.tw = tw;
  2334. }
  2335. public void actionPerformed(ActionEvent e) {
  2336. // display the permission list for a policy entry
  2337. ToolDialog td = new ToolDialog
  2338. (PolicyTool.rb.getString("Policy Entry"), tool, tw, true);
  2339. td.displayPolicyEntryDialog(true);
  2340. }
  2341. }
  2342. /**
  2343. * Event handler for the File Menu
  2344. */
  2345. class FileMenuListener implements ActionListener {
  2346. private PolicyTool tool;
  2347. private ToolWindow tw;
  2348. FileMenuListener(PolicyTool tool, ToolWindow tw) {
  2349. this.tool = tool;
  2350. this.tw = tw;
  2351. }
  2352. public void actionPerformed(ActionEvent e) {
  2353. if (PolicyTool.collator.compare(e.getActionCommand(), tw.QUIT) == 0) {
  2354. // ask user if they want to save changes
  2355. ToolDialog td = new ToolDialog
  2356. (PolicyTool.rb.getString("Save Changes"), tool, tw, true);
  2357. td.displayUserSave(td.QUIT);
  2358. // the above method will perform the QUIT as long as the
  2359. // user does not CANCEL the request
  2360. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2361. tw.NEW_POLICY_FILE) == 0) {
  2362. // ask user if they want to save changes
  2363. ToolDialog td = new ToolDialog
  2364. (PolicyTool.rb.getString("Save Changes"), tool, tw, true);
  2365. td.displayUserSave(td.NEW);
  2366. // the above method will perform the NEW as long as the
  2367. // user does not CANCEL the request
  2368. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2369. tw.OPEN_POLICY_FILE) == 0) {
  2370. // ask user if they want to save changes
  2371. ToolDialog td = new ToolDialog
  2372. (PolicyTool.rb.getString("Save Changes"), tool, tw, true);
  2373. td.displayUserSave(td.OPEN);
  2374. // the above method will perform the OPEN as long as the
  2375. // user does not CANCEL the request
  2376. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2377. tw.SAVE_POLICY_FILE) == 0) {
  2378. // get the previously entered filename
  2379. String filename = ((TextField)
  2380. tw.getComponent(tw.MW_FILENAME_TEXTFIELD)).getText();
  2381. // if there is no filename, do a SAVE_AS
  2382. if (filename == null || filename.length() == 0) {
  2383. // user wants to SAVE AS
  2384. ToolDialog td = new ToolDialog
  2385. (PolicyTool.rb.getString("Save As"), tool, tw, true);
  2386. td.displaySaveAsDialog(td.NOACTION);
  2387. } else {
  2388. try {
  2389. // save the policy entries to a file
  2390. tool.savePolicy(filename);
  2391. // display status
  2392. MessageFormat form = new MessageFormat
  2393. (PolicyTool.rb.getString
  2394. ("Policy successfully written to filename"));
  2395. Object[] source = {filename};
  2396. tw.displayStatusDialog(null, form.format(source));
  2397. } catch (FileNotFoundException fnfe) {
  2398. if (filename == null || filename.equals("")) {
  2399. tw.displayErrorDialog(null, new FileNotFoundException
  2400. (PolicyTool.rb.getString("null filename")));
  2401. } else {
  2402. tw.displayErrorDialog(null, fnfe);
  2403. }
  2404. } catch (Exception ee) {
  2405. tw.displayErrorDialog(null, ee);
  2406. }
  2407. }
  2408. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2409. tw.SAVE_AS_POLICY_FILE) == 0) {
  2410. // user wants to SAVE AS
  2411. ToolDialog td = new ToolDialog
  2412. (PolicyTool.rb.getString("Save As"), tool, tw, true);
  2413. td.displaySaveAsDialog(td.NOACTION);
  2414. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2415. tw.VIEW_WARNINGS) == 0) {
  2416. tw.displayWarningLog(null);
  2417. }
  2418. }
  2419. }
  2420. /**
  2421. * Event handler for the main window buttons and Edit Menu
  2422. */
  2423. class MainWindowListener implements ActionListener {
  2424. private PolicyTool tool;
  2425. private ToolWindow tw;
  2426. MainWindowListener(PolicyTool tool, ToolWindow tw) {
  2427. this.tool = tool;
  2428. this.tw = tw;
  2429. }
  2430. public void actionPerformed(ActionEvent e) {
  2431. if (PolicyTool.collator.compare(e.getActionCommand(),
  2432. tw.ADD_POLICY_ENTRY) == 0) {
  2433. // display a dialog box for the user to enter policy info
  2434. ToolDialog td = new ToolDialog
  2435. (PolicyTool.rb.getString("Policy Entry"), tool, tw, true);
  2436. td.displayPolicyEntryDialog(false);
  2437. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2438. tw.REMOVE_POLICY_ENTRY) == 0) {
  2439. // get the selected entry
  2440. List list = (List)tw.getComponent(tw.MW_POLICY_LIST);
  2441. int index = list.getSelectedIndex();
  2442. if (index < 0) {
  2443. tw.displayErrorDialog(null, new Exception
  2444. (PolicyTool.rb.getString("No Policy Entry selected")));
  2445. return;
  2446. }
  2447. // ask the user if they really want to remove the policy entry
  2448. ToolDialog td = new ToolDialog(PolicyTool.rb.getString
  2449. ("Remove Policy Entry"), tool, tw, true);
  2450. td.displayConfirmRemovePolicyEntry();
  2451. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2452. tw.EDIT_POLICY_ENTRY) == 0) {
  2453. // get the selected entry
  2454. List list = (List)tw.getComponent(tw.MW_POLICY_LIST);
  2455. int index = list.getSelectedIndex();
  2456. if (index < 0) {
  2457. tw.displayErrorDialog(null, new Exception
  2458. (PolicyTool.rb.getString("No Policy Entry selected")));
  2459. return;
  2460. }
  2461. // display the permission list for a policy entry
  2462. ToolDialog td = new ToolDialog
  2463. (PolicyTool.rb.getString("Policy Entry"), tool, tw, true);
  2464. td.displayPolicyEntryDialog(true);
  2465. } else if (PolicyTool.collator.compare(e.getActionCommand(),
  2466. tw.EDIT_KEYSTORE) == 0) {
  2467. // display a dialog box for the user to enter keystore info
  2468. ToolDialog td = new ToolDialog
  2469. (PolicyTool.rb.getString("KeyStore"), tool, tw, true);
  2470. td.keyStoreDialog(td.EDIT_KEYSTORE);
  2471. }
  2472. }
  2473. }
  2474. /**
  2475. * Event handler for OverWriteFileOKButton button
  2476. */
  2477. class OverWriteFileOKButtonListener implements ActionListener {
  2478. private PolicyTool tool;
  2479. private ToolWindow tw;
  2480. private ToolDialog td;
  2481. private String filename;
  2482. private int nextEvent;
  2483. OverWriteFileOKButtonListener(PolicyTool tool, ToolWindow tw,
  2484. ToolDialog td, String filename, int nextEvent) {
  2485. this.tool = tool;
  2486. this.tw = tw;
  2487. this.td = td;
  2488. this.filename = filename;
  2489. this.nextEvent = nextEvent;
  2490. }
  2491. public void actionPerformed(ActionEvent e) {
  2492. try {
  2493. // save the policy entries to a file
  2494. tool.savePolicy(filename);
  2495. // display status
  2496. MessageFormat form = new MessageFormat
  2497. (PolicyTool.rb.getString
  2498. ("Policy successfully written to filename"));
  2499. Object[] source = {filename};
  2500. tw.displayStatusDialog(null, form.format(source));
  2501. // display the new policy filename
  2502. TextField newFilename = (TextField)tw.getComponent
  2503. (tw.MW_FILENAME_TEXTFIELD);
  2504. newFilename.setText(filename);
  2505. tw.setVisible(true);
  2506. // now continue with the originally requested command
  2507. // (QUIT, NEW, or OPEN)
  2508. td.setVisible(false);
  2509. td.dispose();
  2510. td.userSaveContinue(tool, tw, td, nextEvent);
  2511. } catch (FileNotFoundException fnfe) {
  2512. if (filename == null || filename.equals("")) {
  2513. tw.displayErrorDialog(null, new FileNotFoundException
  2514. (PolicyTool.rb.getString("null filename")));
  2515. } else {
  2516. tw.displayErrorDialog(null, fnfe);
  2517. }
  2518. td.setVisible(false);
  2519. td.dispose();
  2520. } catch (Exception ee) {
  2521. tw.displayErrorDialog(null, ee);
  2522. td.setVisible(false);
  2523. td.dispose();
  2524. }
  2525. }
  2526. }
  2527. /**
  2528. * Event handler for AddEntryDoneButton button
  2529. *
  2530. * -- if edit is TRUE, then we are EDITing an existing PolicyEntry
  2531. * and we need to update both the policy and the GUI listing.
  2532. * if edit is FALSE, then we are ADDing a new PolicyEntry,
  2533. * so we only need to update the GUI listing.
  2534. */
  2535. class AddEntryDoneButtonListener implements ActionListener {
  2536. private PolicyTool tool;
  2537. private ToolWindow tw;
  2538. private ToolDialog td;
  2539. private boolean edit;
  2540. AddEntryDoneButtonListener(PolicyTool tool, ToolWindow tw,
  2541. ToolDialog td, boolean edit) {
  2542. this.tool = tool;
  2543. this.tw = tw;
  2544. this.td = td;
  2545. this.edit = edit;
  2546. }
  2547. public void actionPerformed(ActionEvent e) {
  2548. try {
  2549. // get a PolicyEntry object from the dialog policy info
  2550. PolicyEntry newEntry = td.getPolicyEntryFromDialog();
  2551. PolicyParser.GrantEntry newGe = newEntry.getGrantEntry();
  2552. // see if all the signers have public keys
  2553. if (newGe.signedBy != null) {
  2554. String signers[] = tool.parseSigners(newGe.signedBy);
  2555. for (int i = 0; i < signers.length; i++) {
  2556. PublicKey pubKey = tool.getPublicKeyAlias(signers[i]);
  2557. if (pubKey == null) {
  2558. MessageFormat form = new MessageFormat
  2559. (PolicyTool.rb.getString
  2560. ("Warning: A public key for alias " +
  2561. "'signers[i]' does not exist. " +
  2562. "Make sure a KeyStore is properly configured."));
  2563. Object[] source = {signers[i]};
  2564. tool.warnings.addElement(form.format(source));
  2565. tw.displayStatusDialog(td, form.format(source));
  2566. }
  2567. }
  2568. }
  2569. // add the entry
  2570. List policyList = (List)tw.getComponent(tw.MW_POLICY_LIST);
  2571. if (edit) {
  2572. int listIndex = policyList.getSelectedIndex();
  2573. tool.addEntry(newEntry, listIndex);
  2574. String newCodeBaseStr = newEntry.headerToString();
  2575. if (PolicyTool.collator.compare
  2576. (newCodeBaseStr, policyList.getItem(listIndex)) != 0)
  2577. tool.modified = true;
  2578. policyList.replaceItem(newCodeBaseStr, listIndex);
  2579. } else {
  2580. tool.addEntry(newEntry, -1);
  2581. policyList.add(newEntry.headerToString());
  2582. tool.modified = true;
  2583. }
  2584. td.setVisible(false);
  2585. td.dispose();
  2586. } catch (Exception eee) {
  2587. tw.displayErrorDialog(td, eee);
  2588. }
  2589. }
  2590. }
  2591. /**
  2592. * Event handler for ChangeKeyStoreOKButton button
  2593. */
  2594. class ChangeKeyStoreOKButtonListener implements ActionListener {
  2595. private PolicyTool tool;
  2596. private ToolWindow tw;
  2597. private ToolDialog td;
  2598. ChangeKeyStoreOKButtonListener(PolicyTool tool, ToolWindow tw,
  2599. ToolDialog td) {
  2600. this.tool = tool;
  2601. this.tw = tw;
  2602. this.td = td;
  2603. }
  2604. public void actionPerformed(ActionEvent e) {
  2605. String URLString = ((TextField)
  2606. td.getComponent(td.KSD_NAME_TEXTFIELD)).getText().trim();
  2607. String type = ((TextField)
  2608. td.getComponent(td.KSD_TYPE_TEXTFIELD)).getText().trim();
  2609. String provider = ((TextField)
  2610. td.getComponent(td.KSD_PROVIDER_TEXTFIELD)).getText().trim();
  2611. String pwdURL = ((TextField)
  2612. td.getComponent(td.KSD_PWD_URL_TEXTFIELD)).getText().trim();
  2613. try {
  2614. tool.openKeyStore
  2615. ((URLString.length() == 0 ? null : URLString),
  2616. (type.length() == 0 ? null : type),
  2617. (provider.length() == 0 ? null : provider),
  2618. (pwdURL.length() == 0 ? null : pwdURL));
  2619. tool.modified = true;
  2620. } catch (Exception ex) {
  2621. MessageFormat form = new MessageFormat(PolicyTool.rb.getString
  2622. ("Unable to open KeyStore: ex.toString()"));
  2623. Object[] source = {ex.toString()};
  2624. tw.displayErrorDialog(td, form.format(source));
  2625. return;
  2626. }
  2627. td.dispose();
  2628. }
  2629. }
  2630. /**
  2631. * Event handler for AddPrinButton button
  2632. */
  2633. class AddPrinButtonListener implements ActionListener {
  2634. private PolicyTool tool;
  2635. private ToolWindow tw;
  2636. private ToolDialog td;
  2637. private boolean editPolicyEntry;
  2638. AddPrinButtonListener(PolicyTool tool, ToolWindow tw,
  2639. ToolDialog td, boolean editPolicyEntry) {
  2640. this.tool = tool;
  2641. this.tw = tw;
  2642. this.td = td;
  2643. this.editPolicyEntry = editPolicyEntry;
  2644. }
  2645. public void actionPerformed(ActionEvent e) {
  2646. // display a dialog box for the user to enter principal info
  2647. td.displayPrincipalDialog(editPolicyEntry, false);
  2648. }
  2649. }
  2650. /**
  2651. * Event handler for AddPermButton button
  2652. */
  2653. class AddPermButtonListener implements ActionListener {
  2654. private PolicyTool tool;
  2655. private ToolWindow tw;
  2656. private ToolDialog td;
  2657. private boolean editPolicyEntry;
  2658. AddPermButtonListener(PolicyTool tool, ToolWindow tw,
  2659. ToolDialog td, boolean editPolicyEntry) {
  2660. this.tool = tool;
  2661. this.tw = tw;
  2662. this.td = td;
  2663. this.editPolicyEntry = editPolicyEntry;
  2664. }
  2665. public void actionPerformed(ActionEvent e) {
  2666. // display a dialog box for the user to enter permission info
  2667. td.displayPermissionDialog(editPolicyEntry, false);
  2668. }
  2669. }
  2670. /**
  2671. * Event handler for AddPrinOKButton button
  2672. */
  2673. class NewPolicyPrinOKButtonListener implements ActionListener {
  2674. private PolicyTool tool;
  2675. private ToolWindow tw;
  2676. private ToolDialog listDialog;
  2677. private ToolDialog infoDialog;
  2678. private boolean edit;
  2679. NewPolicyPrinOKButtonListener(PolicyTool tool,
  2680. ToolWindow tw,
  2681. ToolDialog listDialog,
  2682. ToolDialog infoDialog,
  2683. boolean edit) {
  2684. this.tool = tool;
  2685. this.tw = tw;
  2686. this.listDialog = listDialog;
  2687. this.infoDialog = infoDialog;
  2688. this.edit = edit;
  2689. }
  2690. public void actionPerformed(ActionEvent e) {
  2691. try {
  2692. // read in the new principal info from Dialog Box
  2693. PolicyParser.PrincipalEntry pppe =
  2694. infoDialog.getPrinFromDialog();
  2695. if (pppe != null) {
  2696. try {
  2697. tool.verifyPrincipal(pppe.getPrincipalClass(),
  2698. pppe.getPrincipalName());
  2699. } catch (ClassNotFoundException cnfe) {
  2700. MessageFormat form = new MessageFormat
  2701. (PolicyTool.rb.getString
  2702. ("Warning: Class not found: class"));
  2703. Object[] source = {pppe.getPrincipalClass()};
  2704. tool.warnings.addElement(form.format(source));
  2705. tw.displayStatusDialog(infoDialog, form.format(source));
  2706. }
  2707. // add the principal to the GUI principal list
  2708. TaggedList prinList =
  2709. (TaggedList)listDialog.getComponent(listDialog.PE_PRIN_LIST);
  2710. String prinString = ToolDialog.PrincipalEntryToUserFriendlyString(pppe);
  2711. if (edit) {
  2712. // if editing, replace the original principal
  2713. int index = prinList.getSelectedIndex();
  2714. prinList.replaceTaggedItem(prinString, pppe, index);
  2715. } else {
  2716. // if adding, just add it to the end
  2717. prinList.addTaggedItem(prinString, pppe);
  2718. }
  2719. }
  2720. infoDialog.dispose();
  2721. } catch (Exception ee) {
  2722. tw.displayErrorDialog(infoDialog, ee);
  2723. }
  2724. }
  2725. }
  2726. /**
  2727. * Event handler for AddPermOKButton button
  2728. */
  2729. class NewPolicyPermOKButtonListener implements ActionListener {
  2730. private PolicyTool tool;
  2731. private ToolWindow tw;
  2732. private ToolDialog listDialog;
  2733. private ToolDialog infoDialog;
  2734. private boolean edit;
  2735. NewPolicyPermOKButtonListener(PolicyTool tool,
  2736. ToolWindow tw,
  2737. ToolDialog listDialog,
  2738. ToolDialog infoDialog,
  2739. boolean edit) {
  2740. this.tool = tool;
  2741. this.tw = tw;
  2742. this.listDialog = listDialog;
  2743. this.infoDialog = infoDialog;
  2744. this.edit = edit;
  2745. }
  2746. public void actionPerformed(ActionEvent e) {
  2747. try {
  2748. // read in the new permission info from Dialog Box
  2749. PolicyParser.PermissionEntry pppe =
  2750. infoDialog.getPermFromDialog();
  2751. try {
  2752. tool.verifyPermission(pppe.permission, pppe.name, pppe.action);
  2753. } catch (ClassNotFoundException cnfe) {
  2754. MessageFormat form = new MessageFormat(PolicyTool.rb.getString
  2755. ("Warning: Class not found: class"));
  2756. Object[] source = {pppe.permission};
  2757. tool.warnings.addElement(form.format(source));
  2758. tw.displayStatusDialog(infoDialog, form.format(source));
  2759. }
  2760. // add the permission to the GUI permission list
  2761. TaggedList permList =
  2762. (TaggedList)listDialog.getComponent(listDialog.PE_PERM_LIST);
  2763. String permString = ToolDialog.PermissionEntryToUserFriendlyString(pppe);
  2764. if (edit) {
  2765. // if editing, replace the original permission
  2766. int which = permList.getSelectedIndex();
  2767. permList.replaceTaggedItem(permString, pppe, which);
  2768. } else {
  2769. // if adding, just add it to the end
  2770. permList.addTaggedItem(permString, pppe);
  2771. }
  2772. infoDialog.dispose();
  2773. } catch (InvocationTargetException ite) {
  2774. tw.displayErrorDialog(infoDialog, ite.getTargetException());
  2775. } catch (Exception ee) {
  2776. tw.displayErrorDialog(infoDialog, ee);
  2777. }
  2778. }
  2779. }
  2780. /**
  2781. * Event handler for RemovePrinButton button
  2782. */
  2783. class RemovePrinButtonListener implements ActionListener {
  2784. private PolicyTool tool;
  2785. private ToolWindow tw;
  2786. private ToolDialog td;
  2787. private boolean edit;
  2788. RemovePrinButtonListener(PolicyTool tool, ToolWindow tw,
  2789. ToolDialog td, boolean edit) {
  2790. this.tool = tool;
  2791. this.tw = tw;
  2792. this.td = td;
  2793. this.edit = edit;
  2794. }
  2795. public void actionPerformed(ActionEvent e) {
  2796. // get the Principal selected from the Principal List
  2797. TaggedList prinList = (TaggedList)td.getComponent(td.PE_PRIN_LIST);
  2798. int prinIndex = prinList.getSelectedIndex();
  2799. if (prinIndex < 0) {
  2800. tw.displayErrorDialog(td, new Exception
  2801. (PolicyTool.rb.getString("No principal selected")));
  2802. return;
  2803. }
  2804. // remove the principal from the display
  2805. prinList.removeTaggedItem(prinIndex);
  2806. }
  2807. }
  2808. /**
  2809. * Event handler for RemovePermButton button
  2810. */
  2811. class RemovePermButtonListener implements ActionListener {
  2812. private PolicyTool tool;
  2813. private ToolWindow tw;
  2814. private ToolDialog td;
  2815. private boolean edit;
  2816. RemovePermButtonListener(PolicyTool tool, ToolWindow tw,
  2817. ToolDialog td, boolean edit) {
  2818. this.tool = tool;
  2819. this.tw = tw;
  2820. this.td = td;
  2821. this.edit = edit;
  2822. }
  2823. public void actionPerformed(ActionEvent e) {
  2824. // get the Permission selected from the Permission List
  2825. TaggedList permList = (TaggedList)td.getComponent(td.PE_PERM_LIST);
  2826. int permIndex = permList.getSelectedIndex();
  2827. if (permIndex < 0) {
  2828. tw.displayErrorDialog(td, new Exception
  2829. (PolicyTool.rb.getString("No permission selected")));
  2830. return;
  2831. }
  2832. // remove the permission from the display
  2833. permList.removeTaggedItem(permIndex);
  2834. }
  2835. }
  2836. /**
  2837. * Event handler for Edit Principal button
  2838. *
  2839. * We need the editPolicyEntry boolean to tell us if the user is
  2840. * adding a new PolicyEntry at this time, or editing an existing entry.
  2841. * If the user is adding a new PolicyEntry, we ONLY update the
  2842. * GUI listing. If the user is editing an existing PolicyEntry, we
  2843. * update both the GUI listing and the actual PolicyEntry.
  2844. */
  2845. class EditPrinButtonListener implements ActionListener {
  2846. private PolicyTool tool;
  2847. private ToolWindow tw;
  2848. private ToolDialog td;
  2849. private boolean editPolicyEntry;
  2850. EditPrinButtonListener(PolicyTool tool, ToolWindow tw,
  2851. ToolDialog td, boolean editPolicyEntry) {
  2852. this.tool = tool;
  2853. this.tw = tw;
  2854. this.td = td;
  2855. this.editPolicyEntry = editPolicyEntry;
  2856. }
  2857. public void actionPerformed(ActionEvent e) {
  2858. // get the Principal selected from the Principal List
  2859. TaggedList list = (TaggedList)td.getComponent(td.PE_PRIN_LIST);
  2860. int prinIndex = list.getSelectedIndex();
  2861. if (prinIndex < 0) {
  2862. tw.displayErrorDialog(td, new Exception
  2863. (PolicyTool.rb.getString("No principal selected")));
  2864. return;
  2865. }
  2866. td.displayPrincipalDialog(editPolicyEntry, true);
  2867. }
  2868. }
  2869. /**
  2870. * Event handler for Edit Permission button
  2871. *
  2872. * We need the editPolicyEntry boolean to tell us if the user is
  2873. * adding a new PolicyEntry at this time, or editing an existing entry.
  2874. * If the user is adding a new PolicyEntry, we ONLY update the
  2875. * GUI listing. If the user is editing an existing PolicyEntry, we
  2876. * update both the GUI listing and the actual PolicyEntry.
  2877. */
  2878. class EditPermButtonListener implements ActionListener {
  2879. private PolicyTool tool;
  2880. private ToolWindow tw;
  2881. private ToolDialog td;
  2882. private boolean editPolicyEntry;
  2883. EditPermButtonListener(PolicyTool tool, ToolWindow tw,
  2884. ToolDialog td, boolean editPolicyEntry) {
  2885. this.tool = tool;
  2886. this.tw = tw;
  2887. this.td = td;
  2888. this.editPolicyEntry = editPolicyEntry;
  2889. }
  2890. public void actionPerformed(ActionEvent e) {
  2891. // get the Permission selected from the Permission List
  2892. List list = (List)td.getComponent(td.PE_PERM_LIST);
  2893. int permIndex = list.getSelectedIndex();
  2894. if (permIndex < 0) {
  2895. tw.displayErrorDialog(td, new Exception
  2896. (PolicyTool.rb.getString("No permission selected")));
  2897. return;
  2898. }
  2899. td.displayPermissionDialog(editPolicyEntry, true);
  2900. }
  2901. }
  2902. /**
  2903. * Event handler for Principal Popup Menu
  2904. */
  2905. class PrincipalTypeMenuListener implements ItemListener {
  2906. private ToolDialog td;
  2907. PrincipalTypeMenuListener(ToolDialog td) {
  2908. this.td = td;
  2909. }
  2910. public void itemStateChanged(ItemEvent e) {
  2911. Choice prin = (Choice)td.getComponent(td.PRD_PRIN_CHOICE);
  2912. TextField prinField =
  2913. (TextField)td.getComponent(td.PRD_PRIN_TEXTFIELD);
  2914. TextField nameField =
  2915. (TextField)td.getComponent(td.PRD_NAME_TEXTFIELD);
  2916. prin.getAccessibleContext().setAccessibleName(
  2917. PolicyTool.splitToWords((String)e.getItem()));
  2918. if (((String)e.getItem()).equals(td.PRIN_TYPE)) {
  2919. // ignore if they choose "Principal Type:" item
  2920. if (prinField.getText() != null &&
  2921. prinField.getText().length() > 0) {
  2922. Prin inputPrin = td.getPrin(prinField.getText(), true);
  2923. prin.select(inputPrin.CLASS);
  2924. }
  2925. return;
  2926. }
  2927. // if you change the principal, clear the name
  2928. if (prinField.getText().indexOf((String)e.getItem()) == -1) {
  2929. nameField.setText("");
  2930. }
  2931. // set the text in the textfield and also modify the
  2932. // pull-down choice menus to reflect the correct possible
  2933. // set of names and actions
  2934. Prin inputPrin = td.getPrin((String)e.getItem(), false);
  2935. if (inputPrin != null) {
  2936. prinField.setText(inputPrin.FULL_CLASS);
  2937. }
  2938. }
  2939. }
  2940. /**
  2941. * Event handler for Permission Popup Menu
  2942. */
  2943. class PermissionMenuListener implements ItemListener {
  2944. private ToolDialog td;
  2945. PermissionMenuListener(ToolDialog td) {
  2946. this.td = td;
  2947. }
  2948. public void itemStateChanged(ItemEvent e) {
  2949. Choice perms = (Choice)td.getComponent(td.PD_PERM_CHOICE);
  2950. Choice names = (Choice)td.getComponent(td.PD_NAME_CHOICE);
  2951. Choice actions = (Choice)td.getComponent(td.PD_ACTIONS_CHOICE);
  2952. TextField nameField =
  2953. (TextField)td.getComponent(td.PD_NAME_TEXTFIELD);
  2954. TextField actionsField =
  2955. (TextField)td.getComponent(td.PD_ACTIONS_TEXTFIELD);
  2956. TextField permField = (TextField)td.getComponent(td.PD_PERM_TEXTFIELD);
  2957. TextField signedbyField =
  2958. (TextField)td.getComponent(td.PD_SIGNEDBY_TEXTFIELD);
  2959. perms.getAccessibleContext().setAccessibleName(
  2960. PolicyTool.splitToWords((String)e.getItem()));
  2961. // ignore if they choose the 'Permission:' item
  2962. if (PolicyTool.collator.compare((String)e.getItem(), td.PERM) == 0) {
  2963. if (permField.getText() != null &&
  2964. permField.getText().length() > 0) {
  2965. Perm inputPerm = td.getPerm(permField.getText(), true);
  2966. if (inputPerm != null) {
  2967. perms.select(inputPerm.CLASS);
  2968. }
  2969. }
  2970. return;
  2971. }
  2972. // if you change the permission, clear the name, actions, and signedBy
  2973. if (permField.getText().indexOf((String)e.getItem()) == -1) {
  2974. nameField.setText("");
  2975. actionsField.setText("");
  2976. signedbyField.setText("");
  2977. }
  2978. // set the text in the textfield and also modify the
  2979. // pull-down choice menus to reflect the correct possible
  2980. // set of names and actions
  2981. Perm inputPerm = td.getPerm((String)e.getItem(), false);
  2982. if (inputPerm == null) {
  2983. permField.setText("");
  2984. } else {
  2985. permField.setText(inputPerm.FULL_CLASS);
  2986. }
  2987. td.setPermissionNames(inputPerm, names, nameField);
  2988. td.setPermissionActions(inputPerm, actions, actionsField);
  2989. }
  2990. }
  2991. /**
  2992. * Event handler for Permission Name Popup Menu
  2993. */
  2994. class PermissionNameMenuListener implements ItemListener {
  2995. private ToolDialog td;
  2996. PermissionNameMenuListener(ToolDialog td) {
  2997. this.td = td;
  2998. }
  2999. public void itemStateChanged(ItemEvent e) {
  3000. Choice names = (Choice)td.getComponent(td.PD_NAME_CHOICE);
  3001. names.getAccessibleContext().setAccessibleName(
  3002. PolicyTool.splitToWords((String)e.getItem()));
  3003. if (((String)e.getItem()).indexOf(td.PERM_NAME) != -1)
  3004. return;
  3005. TextField tf = (TextField)td.getComponent(td.PD_NAME_TEXTFIELD);
  3006. tf.setText((String)e.getItem());
  3007. }
  3008. }
  3009. /**
  3010. * Event handler for Permission Actions Popup Menu
  3011. */
  3012. class PermissionActionsMenuListener implements ItemListener {
  3013. private ToolDialog td;
  3014. PermissionActionsMenuListener(ToolDialog td) {
  3015. this.td = td;
  3016. }
  3017. public void itemStateChanged(ItemEvent e) {
  3018. Choice actions = (Choice)td.getComponent(td.PD_ACTIONS_CHOICE);
  3019. actions.getAccessibleContext().setAccessibleName((String)e.getItem());
  3020. if (((String)e.getItem()).indexOf(td.PERM_ACTIONS) != -1)
  3021. return;
  3022. TextField tf = (TextField)td.getComponent(td.PD_ACTIONS_TEXTFIELD);
  3023. if (tf.getText() == null || tf.getText().equals("")) {
  3024. tf.setText((String)e.getItem());
  3025. } else {
  3026. if (tf.getText().indexOf((String)e.getItem()) == -1)
  3027. tf.setText(tf.getText() + ", " + (String)e.getItem());
  3028. }
  3029. }
  3030. }
  3031. /**
  3032. * Event handler for all the children dialogs/windows
  3033. */
  3034. class ChildWindowListener implements WindowListener {
  3035. private ToolDialog td;
  3036. ChildWindowListener(ToolDialog td) {
  3037. this.td = td;
  3038. }
  3039. public void windowOpened(WindowEvent we) {
  3040. }
  3041. public void windowClosing(WindowEvent we) {
  3042. // same as pressing the "cancel" button
  3043. td.setVisible(false);
  3044. td.dispose();
  3045. }
  3046. public void windowClosed(WindowEvent we) {
  3047. }
  3048. public void windowIconified(WindowEvent we) {
  3049. }
  3050. public void windowDeiconified(WindowEvent we) {
  3051. }
  3052. public void windowActivated(WindowEvent we) {
  3053. }
  3054. public void windowDeactivated(WindowEvent we) {
  3055. }
  3056. }
  3057. /**
  3058. * Event handler for CancelButton button
  3059. */
  3060. class CancelButtonListener implements ActionListener {
  3061. private ToolDialog td;
  3062. CancelButtonListener(ToolDialog td) {
  3063. this.td = td;
  3064. }
  3065. public void actionPerformed(ActionEvent e) {
  3066. td.setVisible(false);
  3067. td.dispose();
  3068. }
  3069. }
  3070. /**
  3071. * Event handler for ErrorOKButton button
  3072. */
  3073. class ErrorOKButtonListener implements ActionListener {
  3074. private ToolDialog ed;
  3075. ErrorOKButtonListener(ToolDialog ed) {
  3076. this.ed = ed;
  3077. }
  3078. public void actionPerformed(ActionEvent e) {
  3079. ed.setVisible(false);
  3080. ed.dispose();
  3081. }
  3082. }
  3083. /**
  3084. * Event handler for StatusOKButton button
  3085. */
  3086. class StatusOKButtonListener implements ActionListener {
  3087. private ToolDialog sd;
  3088. StatusOKButtonListener(ToolDialog sd) {
  3089. this.sd = sd;
  3090. }
  3091. public void actionPerformed(ActionEvent e) {
  3092. sd.setVisible(false);
  3093. sd.dispose();
  3094. }
  3095. }
  3096. /**
  3097. * Event handler for UserSaveYes button
  3098. */
  3099. class UserSaveYesButtonListener implements ActionListener {
  3100. private ToolDialog us;
  3101. private PolicyTool tool;
  3102. private ToolWindow tw;
  3103. private int select;
  3104. UserSaveYesButtonListener(ToolDialog us, PolicyTool tool,
  3105. ToolWindow tw, int select) {
  3106. this.us = us;
  3107. this.tool = tool;
  3108. this.tw = tw;
  3109. this.select = select;
  3110. }
  3111. public void actionPerformed(ActionEvent e) {
  3112. // first get rid of the window
  3113. us.setVisible(false);
  3114. us.dispose();
  3115. try {
  3116. String filename = ((TextField)
  3117. tw.getComponent(tw.MW_FILENAME_TEXTFIELD)).getText();
  3118. if (filename == null || filename.equals("")) {
  3119. us.displaySaveAsDialog(select);
  3120. // the above dialog will continue with the originally
  3121. // requested command if necessary
  3122. } else {
  3123. // save the policy entries to a file
  3124. tool.savePolicy(filename);
  3125. // display status
  3126. MessageFormat form = new MessageFormat
  3127. (PolicyTool.rb.getString
  3128. ("Policy successfully written to filename"));
  3129. Object[] source = {filename};
  3130. tw.displayStatusDialog(null, form.format(source));
  3131. // now continue with the originally requested command
  3132. // (QUIT, NEW, or OPEN)
  3133. us.userSaveContinue(tool, tw, us, select);
  3134. }
  3135. } catch (Exception ee) {
  3136. // error -- just report it and bail
  3137. tw.displayErrorDialog(null, ee);
  3138. }
  3139. }
  3140. }
  3141. /**
  3142. * Event handler for UserSaveNoButton
  3143. */
  3144. class UserSaveNoButtonListener implements ActionListener {
  3145. private PolicyTool tool;
  3146. private ToolWindow tw;
  3147. private ToolDialog us;
  3148. private int select;
  3149. UserSaveNoButtonListener(ToolDialog us, PolicyTool tool,
  3150. ToolWindow tw, int select) {
  3151. this.us = us;
  3152. this.tool = tool;
  3153. this.tw = tw;
  3154. this.select = select;
  3155. }
  3156. public void actionPerformed(ActionEvent e) {
  3157. us.setVisible(false);
  3158. us.dispose();
  3159. // now continue with the originally requested command
  3160. // (QUIT, NEW, or OPEN)
  3161. us.userSaveContinue(tool, tw, us, select);
  3162. }
  3163. }
  3164. /**
  3165. * Event handler for UserSaveCancelButton
  3166. */
  3167. class UserSaveCancelButtonListener implements ActionListener {
  3168. private ToolDialog us;
  3169. UserSaveCancelButtonListener(ToolDialog us) {
  3170. this.us = us;
  3171. }
  3172. public void actionPerformed(ActionEvent e) {
  3173. us.setVisible(false);
  3174. us.dispose();
  3175. // do NOT continue with the originally requested command
  3176. // (QUIT, NEW, or OPEN)
  3177. }
  3178. }
  3179. /**
  3180. * Event handler for ConfirmRemovePolicyEntryOKButtonListener
  3181. */
  3182. class ConfirmRemovePolicyEntryOKButtonListener implements ActionListener {
  3183. private PolicyTool tool;
  3184. private ToolWindow tw;
  3185. private ToolDialog us;
  3186. ConfirmRemovePolicyEntryOKButtonListener(PolicyTool tool,
  3187. ToolWindow tw, ToolDialog us) {
  3188. this.tool = tool;
  3189. this.tw = tw;
  3190. this.us = us;
  3191. }
  3192. public void actionPerformed(ActionEvent e) {
  3193. // remove the entry
  3194. List list = (List)tw.getComponent(tw.MW_POLICY_LIST);
  3195. int index = list.getSelectedIndex();
  3196. PolicyEntry entries[] = tool.getEntry();
  3197. tool.removeEntry(entries[index]);
  3198. // redraw the window listing
  3199. list = new List(40, false);
  3200. list.addActionListener(new PolicyListListener(tool, tw));
  3201. entries = tool.getEntry();
  3202. if (entries != null) {
  3203. for (int i = 0; i < entries.length; i++)
  3204. list.add(entries[i].headerToString());
  3205. }
  3206. tw.replacePolicyList(list);
  3207. us.setVisible(false);
  3208. us.dispose();
  3209. }
  3210. }
  3211. /**
  3212. * Just a special name, so that the codes dealing with this exception knows
  3213. * it's special, and does not pop out a warning box.
  3214. */
  3215. class NoDisplayException extends RuntimeException {
  3216. }
  3217. /**
  3218. * This is a java.awt.List that bind an Object to each String it holds.
  3219. */
  3220. class TaggedList extends List {
  3221. private java.util.List<Object> data = new LinkedList<Object>();
  3222. public TaggedList(int i, boolean b) {
  3223. super(i, b);
  3224. }
  3225. public Object getObject(int index) {
  3226. return data.get(index);
  3227. }
  3228. @Override @Deprecated public void add(String string) {
  3229. throw new AssertionError("should not call add in TaggedList");
  3230. }
  3231. public void addTaggedItem(String string, Object object) {
  3232. super.add(string);
  3233. data.add(object);
  3234. }
  3235. @Override @Deprecated public void replaceItem(String string, int index) {
  3236. throw new AssertionError("should not call replaceItem in TaggedList");
  3237. }
  3238. public void replaceTaggedItem(String string, Object object, int index) {
  3239. super.replaceItem(string, index);
  3240. data.set(index, object);
  3241. }
  3242. @Override @Deprecated public void remove(int index) {
  3243. // Cannot throw AssertionError, because replaceItem() call remove() internally
  3244. super.remove(index);
  3245. }
  3246. public void removeTaggedItem(int index) {
  3247. super.remove(index);
  3248. data.remove(index);
  3249. }
  3250. }
  3251. /**
  3252. * Convenience Principal Classes
  3253. */
  3254. class Prin {
  3255. public final String CLASS;
  3256. public final String FULL_CLASS;
  3257. public Prin(String clazz, String fullClass) {
  3258. this.CLASS = clazz;
  3259. this.FULL_CLASS = fullClass;
  3260. }
  3261. }
  3262. class KrbPrin extends Prin {
  3263. public KrbPrin() {
  3264. super("KerberosPrincipal",
  3265. "javax.security.auth.kerberos.KerberosPrincipal");
  3266. }
  3267. }
  3268. class X500Prin extends Prin {
  3269. public X500Prin() {
  3270. super("X500Principal",
  3271. "javax.security.auth.x500.X500Principal");
  3272. }
  3273. }
  3274. /**
  3275. * Convenience Permission Classes
  3276. */
  3277. class Perm {
  3278. public final String CLASS;
  3279. public final String FULL_CLASS;
  3280. public final String[] TARGETS;
  3281. public final String[] ACTIONS;
  3282. public Perm(String clazz, String fullClass,
  3283. String[] targets, String[] actions) {
  3284. this.CLASS = clazz;
  3285. this.FULL_CLASS = fullClass;
  3286. this.TARGETS = targets;
  3287. this.ACTIONS = actions;
  3288. }
  3289. }
  3290. class AllPerm extends Perm {
  3291. public AllPerm() {
  3292. super("AllPermission", "java.security.AllPermission", null, null);
  3293. }
  3294. }
  3295. class AudioPerm extends Perm {
  3296. public AudioPerm() {
  3297. super("AudioPermission",
  3298. "javax.sound.sampled.AudioPermission",
  3299. new String[] {
  3300. "play",
  3301. "record"
  3302. },
  3303. null);
  3304. }
  3305. }
  3306. class AuthPerm extends Perm {
  3307. public AuthPerm() {
  3308. super("AuthPermission",
  3309. "javax.security.auth.AuthPermission",
  3310. new String[] {
  3311. "doAs",
  3312. "doAsPrivileged",
  3313. "getSubject",
  3314. "getSubjectFromDomainCombiner",
  3315. "setReadOnly",
  3316. "modifyPrincipals",
  3317. "modifyPublicCredentials",
  3318. "modifyPrivateCredentials",
  3319. "refreshCredential",
  3320. "destroyCredential",
  3321. "createLoginContext.<" + PolicyTool.rb.getString("name") + ">",
  3322. "getLoginConfiguration",
  3323. "setLoginConfiguration",
  3324. "createLoginConfiguration.<" +
  3325. PolicyTool.rb.getString("configuration type") + ">",
  3326. "refreshLoginConfiguration"
  3327. },
  3328. null);
  3329. }
  3330. }
  3331. class AWTPerm extends Perm {
  3332. public AWTPerm() {
  3333. super("AWTPermission",
  3334. "java.awt.AWTPermission",
  3335. new String[] {
  3336. "accessClipboard",
  3337. "accessEventQueue",
  3338. "accessSystemTray",
  3339. "createRobot",
  3340. "fullScreenExclusive",
  3341. "listenToAllAWTEvents",
  3342. "readDisplayPixels",
  3343. "replaceKeyboardFocusManager",
  3344. "setAppletStub",
  3345. "setWindowAlwaysOnTop",
  3346. "showWindowWithoutWarningBanner",
  3347. "toolkitModality",
  3348. "watchMousePointer"
  3349. },
  3350. null);
  3351. }
  3352. }
  3353. class DelegationPerm extends Perm {
  3354. public DelegationPerm() {
  3355. super("DelegationPermission",
  3356. "javax.security.auth.kerberos.DelegationPermission",
  3357. new String[] {
  3358. // allow user input
  3359. },
  3360. null);
  3361. }
  3362. }
  3363. class FilePerm extends Perm {
  3364. public FilePerm() {
  3365. super("FilePermission",
  3366. "java.io.FilePermission",
  3367. new String[] {
  3368. "<<ALL FILES>>"
  3369. },
  3370. new String[] {
  3371. "read",
  3372. "write",
  3373. "delete",
  3374. "execute"
  3375. });
  3376. }
  3377. }
  3378. class LogPerm extends Perm {
  3379. public LogPerm() {
  3380. super("LoggingPermission",
  3381. "java.util.logging.LoggingPermission",
  3382. new String[] {
  3383. "control"
  3384. },
  3385. null);
  3386. }
  3387. }
  3388. class MgmtPerm extends Perm {
  3389. public MgmtPerm() {
  3390. super("ManagementPermission",
  3391. "java.lang.management.ManagementPermission",
  3392. new String[] {
  3393. "control",
  3394. "monitor"
  3395. },
  3396. null);
  3397. }
  3398. }
  3399. class MBeanPerm extends Perm {
  3400. public MBeanPerm() {
  3401. super("MBeanPermission",
  3402. "javax.management.MBeanPermission",
  3403. new String[] {
  3404. // allow user input
  3405. },
  3406. new String[] {
  3407. "addNotificationListener",
  3408. "getAttribute",
  3409. "getClassLoader",
  3410. "getClassLoaderFor",
  3411. "getClassLoaderRepository",
  3412. "getDomains",
  3413. "getMBeanInfo",
  3414. "getObjectInstance",
  3415. "instantiate",
  3416. "invoke",
  3417. "isInstanceOf",
  3418. "queryMBeans",
  3419. "queryNames",
  3420. "registerMBean",
  3421. "removeNotificationListener",
  3422. "setAttribute",
  3423. "unregisterMBean"
  3424. });
  3425. }
  3426. }
  3427. class MBeanSvrPerm extends Perm {
  3428. public MBeanSvrPerm() {
  3429. super("MBeanServerPermission",
  3430. "javax.management.MBeanServerPermission",
  3431. new String[] {
  3432. "createMBeanServer",
  3433. "findMBeanServer",
  3434. "newMBeanServer",
  3435. "releaseMBeanServer"
  3436. },
  3437. null);
  3438. }
  3439. }
  3440. class MBeanTrustPerm extends Perm {
  3441. public MBeanTrustPerm() {
  3442. super("MBeanTrustPermission",
  3443. "javax.management.MBeanTrustPermission",
  3444. new String[] {
  3445. "register"
  3446. },
  3447. null);
  3448. }
  3449. }
  3450. class NetPerm extends Perm {
  3451. public NetPerm() {
  3452. super("NetPermission",
  3453. "java.net.NetPermission",
  3454. new String[] {
  3455. "setDefaultAuthenticator",
  3456. "requestPasswordAuthentication",
  3457. "specifyStreamHandler",
  3458. "setProxySelector",
  3459. "getProxySelector",
  3460. "setCookieHandler",
  3461. "getCookieHandler",
  3462. "setResponseCache",
  3463. "getResponseCache"
  3464. },
  3465. null);
  3466. }
  3467. }
  3468. class PrivCredPerm extends Perm {
  3469. public PrivCredPerm() {
  3470. super("PrivateCredentialPermission",
  3471. "javax.security.auth.PrivateCredentialPermission",
  3472. new String[] {
  3473. // allow user input
  3474. },
  3475. new String[] {
  3476. "read"
  3477. });
  3478. }
  3479. }
  3480. class PropPerm extends Perm {
  3481. public PropPerm() {
  3482. super("PropertyPermission",
  3483. "java.util.PropertyPermission",
  3484. new String[] {
  3485. // allow user input
  3486. },
  3487. new String[] {
  3488. "read",
  3489. "write"
  3490. });
  3491. }
  3492. }
  3493. class ReflectPerm extends Perm {
  3494. public ReflectPerm() {
  3495. super("ReflectPermission",
  3496. "java.lang.reflect.ReflectPermission",
  3497. new String[] {
  3498. "suppressAccessChecks"
  3499. },
  3500. null);
  3501. }
  3502. }
  3503. class RuntimePerm extends Perm {
  3504. public RuntimePerm() {
  3505. super("RuntimePermission",
  3506. "java.lang.RuntimePermission",
  3507. new String[] {
  3508. "createClassLoader",
  3509. "getClassLoader",
  3510. "setContextClassLoader",
  3511. "enableContextClassLoaderOverride",
  3512. "setSecurityManage",
  3513. "createSecurityManager",
  3514. "getenv.<" +
  3515. PolicyTool.rb.getString("environment variable name") + ">",
  3516. "exitVM",
  3517. "shutdownHooks",
  3518. "setFactory",
  3519. "setIO",
  3520. "modifyThread",
  3521. "stopThread",
  3522. "modifyThreadGroup",
  3523. "getProtectionDomain",
  3524. "readFileDescriptor",
  3525. "writeFileDescriptor",
  3526. "loadLibrary.<" +
  3527. PolicyTool.rb.getString("library name") + ">",
  3528. "accessClassInPackage.<" +
  3529. PolicyTool.rb.getString("package name")+">",
  3530. "defineClassInPackage.<" +
  3531. PolicyTool.rb.getString("package name")+">",
  3532. "accessDeclaredMembers",
  3533. "queuePrintJob",
  3534. "getStackTrace",
  3535. "setDefaultUncaughtExceptionHandler",
  3536. "preferences",
  3537. "usePolicy",
  3538. // "inheritedChannel"
  3539. },
  3540. null);
  3541. }
  3542. }
  3543. class SecurityPerm extends Perm {
  3544. public SecurityPerm() {
  3545. super("SecurityPermission",
  3546. "java.security.SecurityPermission",
  3547. new String[] {
  3548. "createAccessControlContext",
  3549. "getDomainCombiner",
  3550. "getPolicy",
  3551. "setPolicy",
  3552. "createPolicy.<" +
  3553. PolicyTool.rb.getString("policy type") + ">",
  3554. "getProperty.<" +
  3555. PolicyTool.rb.getString("property name") + ">",
  3556. "setProperty.<" +
  3557. PolicyTool.rb.getString("property name") + ">",
  3558. "insertProvider.<" +
  3559. PolicyTool.rb.getString("provider name") + ">",
  3560. "removeProvider.<" +
  3561. PolicyTool.rb.getString("provider name") + ">",
  3562. //"setSystemScope",
  3563. //"setIdentityPublicKey",
  3564. //"setIdentityInfo",
  3565. //"addIdentityCertificate",
  3566. //"removeIdentityCertificate",
  3567. //"printIdentity",
  3568. "clearProviderProperties.<" +
  3569. PolicyTool.rb.getString("provider name") + ">",
  3570. "putProviderProperty.<" +
  3571. PolicyTool.rb.getString("provider name") + ">",
  3572. "removeProviderProperty.<" +
  3573. PolicyTool.rb.getString("provider name") + ">",
  3574. //"getSignerPrivateKey",
  3575. //"setSignerKeyPair"
  3576. },
  3577. null);
  3578. }
  3579. }
  3580. class SerialPerm extends Perm {
  3581. public SerialPerm() {
  3582. super("SerializablePermission",
  3583. "java.io.SerializablePermission",
  3584. new String[] {
  3585. "enableSubclassImplementation",
  3586. "enableSubstitution"
  3587. },
  3588. null);
  3589. }
  3590. }
  3591. class ServicePerm extends Perm {
  3592. public ServicePerm() {
  3593. super("ServicePermission",
  3594. "javax.security.auth.kerberos.ServicePermission",
  3595. new String[] {
  3596. // allow user input
  3597. },
  3598. new String[] {
  3599. "initiate",
  3600. "accept"
  3601. });
  3602. }
  3603. }
  3604. class SocketPerm extends Perm {
  3605. public SocketPerm() {
  3606. super("SocketPermission",
  3607. "java.net.SocketPermission",
  3608. new String[] {
  3609. // allow user input
  3610. },
  3611. new String[] {
  3612. "accept",
  3613. "connect",
  3614. "listen",
  3615. "resolve"
  3616. });
  3617. }
  3618. }
  3619. class SQLPerm extends Perm {
  3620. public SQLPerm() {
  3621. super("SQLPermission",
  3622. "java.sql.SQLPermission",
  3623. new String[] {
  3624. "setLog"
  3625. },
  3626. null);
  3627. }
  3628. }
  3629. class SSLPerm extends Perm {
  3630. public SSLPerm() {
  3631. super("SSLPermission",
  3632. "javax.net.ssl.SSLPermission",
  3633. new String[] {
  3634. "setHostnameVerifier",
  3635. "getSSLSessionContext"
  3636. },
  3637. null);
  3638. }
  3639. }
  3640. class SubjDelegPerm extends Perm {
  3641. public SubjDelegPerm() {
  3642. super("SubjectDelegationPermission",
  3643. "javax.management.remote.SubjectDelegationPermission",
  3644. new String[] {
  3645. // allow user input
  3646. },
  3647. null);
  3648. }
  3649. }