xref: /titanic_44/usr/src/cmd/krb5/kadmin/gui/dataclasses/Policy.java (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 import java.util.ResourceBundle;
30 import java.text.NumberFormat;
31 import java.text.ParseException;
32 import java.util.MissingResourceException;
33 
34 /**
35  * Class representing a Kerberos V5 policy
36  * Class data items correspond to fields in struct _kadm5_policy_ent_t
37  */
38 class Policy {
39     String PolicyName;	// char		*policy;
40     Integer PwMinLife;	// long		pw_min_life;
41     Integer PwMaxLife;	// long		pw_max_life;
42     Integer PwMinLength;	// long		pw_min_length;
43     Integer PwMinClasses;	// long		pw_min_classes;
44     Integer PwSaveCount;	// long		pw_history_num;
45     Integer RefCount;	// long		policy_refcnt;
46     Kadmin Kadmin;
47     boolean isNew;
48     boolean dummy;
49 
50     // For I18N
51     private static ResourceBundle rb =
52         ResourceBundle.getBundle("GuiResource" /* NOI18N */);
53     private static NumberFormat nf = NumberFormat.getInstance();
54 
55     /**
56      * Initialize new policy to defaults - this one is for new creations
57      */
58     public Policy() {
59 	dummy = true;
60 	isNew = true;
61 	PolicyName = new String("");
62 	PwMinLife = new Integer(0);
63 	PwMaxLife = new Integer(30 * 24 * 60  * 60);   /* 30 days */
64 	PwMinLength = new Integer(4);
65 	PwMinClasses = new Integer(2);
66 	PwSaveCount = new Integer(3);
67 	RefCount = new Integer(0);
68     }
69 
70     /*
71      * This is used for loading an existing principal
72      */
73     public Policy(String Pname) {
74 	/* Get some specific data from somewhere */
75 	this();
76 	dummy = true;
77 	isNew = false;
78 	PolicyName = Pname;
79 	loadPolicy(Pname);
80     }
81 
82     /*
83      * This is used for duplicating a new principal from an old one
84      */
85     public Policy(Policy old) {
86 	/* Copy old principal to new one */
87 	this();
88 	dummy = true;
89 	copyPolicy(old, this);
90     }
91 
92     /*
93      * For real data, use Kadmin as a first argument
94      */
95     public Policy(Kadmin session) {
96 	this();
97 	dummy = false;
98 	Kadmin = session;
99     }
100 
101     public Policy(Kadmin session, String Pname) {
102 	this();
103 	isNew = false;
104 	dummy = false;
105 	Kadmin = session;
106 	PolicyName = Pname;
107 	loadPolicy(Pname);
108     }
109 
110     public Policy(Kadmin session, Policy old) {
111 	this(old);
112 	dummy = false;
113 	Kadmin = session;
114     }
115 
116     /**
117      * Copy relevant fields from old policy, overriding as necessary
118      */
119     public void copyPolicy(Policy old, Policy curr) {
120 	curr.PolicyName = new String("");	/* override */
121 	curr.PwMinLife = new Integer(old.PwMinLife.intValue());
122 	curr.PwMaxLife = new Integer(old.PwMaxLife.intValue());
123 	curr.PwMinLength = new Integer(old.PwMinLength.intValue());
124 	curr.PwMinClasses = new Integer(old.PwMinClasses.intValue());
125 	curr.PwSaveCount = new Integer(old.PwSaveCount.intValue());
126 	curr.RefCount = new Integer(0);		/* override */
127     }
128 
129     public boolean loadPolicy(String name) {
130 	if (dummy)
131 	    return true;
132 	boolean b = Kadmin.loadPolicy(name, this);
133 	// System.out.println(this.toString());
134 	return b;
135     }
136 
137     public boolean savePolicy() {
138 	// System.out.println(this.toString());
139 	if (dummy)
140 	    return true;
141 	if (this.isNew)
142 	    return Kadmin.createPolicy(this);
143 	else
144 	    return Kadmin.savePolicy(this);
145     }
146 
147     public boolean setName(String name) {
148 	  // xxx: see where this gets called from to determine if a new Policy
149 	  // just added can have a duplicate name or whether that would have
150 	  // been screened out earlier.
151 	  PolicyName = name;
152 	  return true;
153     }
154 
155     /**
156      * @param val Contains one number representing the length.
157      */
158     public boolean setPolPwLength(String val) {
159 	  try {
160 		PwMinLength = new Integer(nf.parse(val).intValue());
161 	  } catch (ParseException e) {
162 		return false;
163 	  }
164 	  return true;
165     }
166 
167     /**
168      * @param val Contains one number representing the number of classes
169      */
170     public boolean setPolPwClasses(String val) {
171         try {
172 	    PwMinClasses = new Integer(nf.parse(val).intValue());
173         } catch (ParseException e) {
174     	    return false;
175         }
176         return true;
177     }
178 
179     /**
180      * @param val Contains one number representing the save count.
181      */
182     public boolean setPolPwHistory(String val) {
183       // xxx: Is pwHistory the same as pwSaveCount?
184         try {
185 	    PwSaveCount = new Integer(nf.parse(val).intValue());
186         } catch (ParseException e) {
187 	  return false;
188         }
189         return true;
190     }
191 
192     /**
193      * @param val Contains one number representing the lifetime in seconds.
194      */
195     public boolean setPolMinlife(String val) {
196         try {
197   	    PwMinLife =  new Integer(nf.parse(val.trim()).intValue());
198         } catch (ParseException e) {
199 	    return false;
200         }
201         return true;
202     }
203 
204     /**
205      * @param val Contains one number representing the lifetime in seconds.
206      */
207     public boolean setPolMaxlife(String val) {
208         try {
209 	    PwMaxLife = new Integer(nf.parse(val.trim()).intValue());
210         } catch (ParseException e) {
211 	    return false;
212         }
213 	    return true;
214     }
215 
216     /*
217      * Obtain a string representation of this policy.
218      * @return a String containing the following information about this policy:
219      * <br><ul>
220      * <li>policy name
221      * <li>password minimum life
222      * <li>password maximum life
223      * <li>password minimum length
224      * <li>password minimum classes
225      * <li>password save count
226      * <li>reference count
227      *</ul>
228      */
229     public String toString() {
230 
231         StringBuffer sb = new StringBuffer();
232 
233         sb.append(getString("Policy Name:") + "  " + PolicyName).append('\n');
234         sb.append(getString("Reference Count:") + "  "
235               +  RefCount).append("\n");
236         sb.append(getString("Minimum Password Lifetime (seconds):")
237 	      + "  " +  PwMinLife).append("\t");
238         sb.append(getString("Maximum Password Lifetime (seconds):")
239 	      + "  " + PwMaxLife).append("\n");
240         sb.append(getString("Minimum Password Length:") + "  "
241               + PwMinLength).append("\t");
242         sb.append(getString("Minimum Password Classes:") + "  "
243 	      + PwMinClasses).append("\n");
244         sb.append(getString("Password Save Count:") + "  "
245 	      + PwSaveCount).append("\n");
246 
247         return sb.toString();
248     }
249 
250     /**
251      * Call rb.getString(), but catch exception and return English
252      * key so that small spelling errors don't cripple the GUI
253      *
254      */
255     private static final String getString(String key) {
256         try {
257     	    String res = rb.getString(key);
258 	    return res;
259         } catch (MissingResourceException e) {
260 	    System.out.println("Missing resource "+key+", using English.");
261 	    return key;
262         }
263     }
264 
265 }
266