Andrzej Szeszo
2013-06-04 453bf5a3004107204e1858532a38b3e703e32995
commit | author | age
c4768d 1 /*
DL 2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
05f426 23  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
c4768d 24  */
DL 25
26 package com.oracle.solaris.vp.panels.usermgr.client.swing;
27
28 import java.util.*;
29 import java.util.logging.Level;
30 import javax.management.ObjectName;
31 import javax.swing.Icon;
32 import com.oracle.solaris.adr.Stability;
33 import com.oracle.solaris.rad.ObjectException;
34 import com.oracle.solaris.vp.panel.common.*;
35 import com.oracle.solaris.vp.panel.common.action.*;
36 import com.oracle.solaris.vp.panel.common.api.panel.MBeanUtil;
37 import com.oracle.solaris.vp.panel.common.control.*;
38 import com.oracle.solaris.vp.panel.common.model.*;
39 import com.oracle.solaris.vp.panel.swing.control.PanelFrameControl;
40 import com.oracle.solaris.vp.panel.swing.model.SwingPanelDescriptor;
41 import com.oracle.solaris.rad.usermgr.*;
42 import com.oracle.solaris.vp.util.misc.*;
43 import com.oracle.solaris.vp.util.misc.finder.Finder;
44 import com.oracle.solaris.vp.util.misc.property.*;
45 import com.oracle.solaris.vp.util.swing.HasIcons;
46
47 public class UserMgrPanelDescriptor
48     extends AbstractPanelDescriptor<UserManagedObject>
47baea 49     implements SwingPanelDescriptor<UserManagedObject>, HasIcons,
LD 50     ConnectionListener {
c4768d 51
DL 52     //
53     // Static data
54     //
55
56     // Constants needed for registering the MBean
57     public static final String INTERFACE_NAME =
58         "com.oracle.solaris.rad.usermgr";
59     public static final ObjectName OBJECT_NAME =
60         MBeanUtil.makeObjectName(INTERFACE_NAME, "UserMgr");
61
62     public static final String USER_TYPE_NORMAL = "normal";
63     public static final String SCOPE_FILES = "files";
e9389d 64     public static final String MATCH_ALL = "";
c4768d 65
47baea 66     public static final String PASSWORD = "PASSWORD";
LD 67     public static final String NOTACTIVATED = "NOTACTIVATED";
68     public static final String LOCKED = "LOCKED";
69     public static final String UNKNOWN = "UNKNOWN";
70
c4768d 71     //
DL 72     // Instance data
73     //
74
75     private MainControl mc;
76     private DefaultControl control;
77     private MXBeanTracker<UserMgrMXBean> beanTracker;
78     private UserType uType;
79
80     private String scopeStr = SCOPE_FILES;
81     private String typeStr = USER_TYPE_NORMAL;
82     private String matchStr = MATCH_ALL;
83
47baea 84     // Assignable lists
LD 85     private List<String> scopeList = null;
86     private List<String> shellList = null;
87     private List<Group> groupList = null;
88     private List<String> profileList = null;
05f426 89     private List<String> authProfileList = null;
47baea 90     private List<String> authList = null;
LD 91     private List<String> roleList = null;
92     private List<String> supplgroups = null;
c4768d 93
47baea 94     private User defUser = null;
c4768d 95
DL 96     //
97     // Constructors
98     //
99
100     /**
101      * Constructs a {@code UserMgrPanelDescriptor}.
102      *
103      * @param        id
104      *            a unique identifier for this Panel, taken from the panel
105      *            registration
106      *
107      * @param        context
108      *            a handle to interact with the Visual Panels client
109      */
110     public UserMgrPanelDescriptor(String id, ClientContext context)
47baea 111     throws TrackerException, ActionFailedException {
c4768d 112
DL 113         super(id, context);
114
115     beanTracker = new MXBeanTracker<UserMgrMXBean>(
116         OBJECT_NAME, UserMgrMXBean.class, Stability.PRIVATE, context);
117
118     setComparator(SimpleHasId.COMPARATOR);
119
120     // Initialize list of users
121     initUsers(SCOPE_FILES, USER_TYPE_NORMAL, MATCH_ALL);
122
123         control = new PanelFrameControl<UserMgrPanelDescriptor>(this);
124         mc = new MainControl(this);
125     control.addChildren(mc);
47baea 126     context.addConnectionListener(this);
c4768d 127     }
DL 128
129     //
130     // PanelDescriptor methods
131     //
132
133     /**
134      * Returns the top-most Control for this PanelDescriptor.
135      */
136     @Override
137     public Control getControl() {
138         return control;
139     }
140
141     //
142     // HasIcons methods
143     //
144
145     /**
146      * Returns a list of icons.
147      */
148     @Override
149     public List<? extends Icon> getIcons() {
150     return UserManagedObject.userIcons;
151     }
152
153     //
154     // ManagedObject methods
155     //
156
157     /**
158      * Stops monitoring the connection to the remote host.
159      */
160     @Override
161     public void dispose() {
162     beanTracker.dispose();
163     super.dispose();
164     }
165
166     /**
167      * Returns the name of this Managed Object.
168      */
169     @Override
170     public String getName() {
171     return Finder.getString("panel.usermgr.name");
172     }
173
174     //
175     // AbstractManagedObject methods
176     //
177
178     @Override
179     public void addChildren(UserManagedObject... toAdd) {
180     super.addChildren(toAdd);
181
182     ChangeableAggregator aggregator = getChangeableAggregator();
183     for (UserManagedObject umo : toAdd) {
184         aggregator.addChangeables(umo.getChangeableAggregator());
185     }
186     }
187
188     //
189     // UserMgrPanelDescriptor methods
190     //
191
192     public void removeChildren(UserManagedObject toRemove) {
193     super.removeChildren(toRemove);
194
195     ChangeableAggregator aggregator = getChangeableAggregator();
196     aggregator.removeChangeable(toRemove.getChangeableAggregator());
197     }
198
199     public void removeAllChildren() {
200     super.clearChildren();
201     ChangeableAggregator aggregator = getChangeableAggregator();
202     aggregator.reset();
203     }
204
205     public UserMgrMXBean getUserMgrBean() {
206         return beanTracker.getBean();
207     }
208
209     public UserManagedObject getUserManagedObject(String id) {
210
211     SimpleHasId tmpHasId = new SimpleHasId();
212     synchronized (children) {
213         tmpHasId.setId(id);
214         int index = Collections.binarySearch(children, tmpHasId,
215         SimpleHasId.COMPARATOR);
216
217         if (index >= 0) {
218         return children.get(index);
219         }
220
221         return null;
222     }
223     }
224
225     public void deleteUserManagedObject(UserManagedObject toRemove) {
226     removeChildren(toRemove);
227     }
228
229     public void addUserManagedObject(UserManagedObject toAdd) {
230     addChildren(toAdd);
231     }
232
47baea 233     public void saveDeletedUser(UserManagedObject umo)
LD 234         throws ActionAbortedException,
c4768d 235     ActionFailedException, ActionUnauthorizedException {
DL 236
47baea 237     try {
LD 238         getUserMgrBean().deleteUser(umo.getName());
239         deleteUserManagedObject(umo);
240     } catch (SecurityException se) {
241         throw new ActionUnauthorizedException(se);
242     } catch (ObjectException e) {
243         UserMgrError ume = e.getPayload(UserMgrError.class);
244         String msg = Finder.getString("usermgr.error.invalidData");
245         String err = Finder.getString(
246         "usermgr.error.delete", umo.getUsername());
247         getLog().log(Level.SEVERE, err + msg, e);
248         throw new ActionFailedException(err + msg);
249     // Any other remaining exceptions
250     } catch (Exception e) {
251         String msg = Finder.getString("usermgr.error.system");
252         String err = Finder.getString(
253         "usermgr.error.delete", umo.getUsername());
254         getLog().log(Level.SEVERE, err + msg, e);
255         throw new ActionFailedException(err + msg);
c4768d 256     }
DL 257     }
258
47baea 259     public void saveAddedUser(UserManagedObject umo)
LD 260         throws ActionAbortedException,
c4768d 261     ActionFailedException, ActionUnauthorizedException {
DL 262
47baea 263     try {
LD 264         char[] password = umo.getPassword();
265         User user = getUserMgrBean().addUser(umo.getNewUser(), password);
266         addUserManagedObject(umo);
267         Arrays.fill(password, (char)0);
268         umo.updateUser(user);
c4768d 269
47baea 270     } catch (SecurityException se) {
LD 271         throw new ActionUnauthorizedException(se);
272     } catch (ObjectException e) {
c4768d 273         UserMgrError ume = e.getPayload(UserMgrError.class);
DL 274         String msg;
275         UserMgrErrorType error = (ume != null) ?
276             ume.getErrorCode() : UserMgrErrorType.INVALIDDATA;
277         switch (error) {
278             case USEREXISTS:
279             msg = Finder.getString("usermgr.error.userExists");
280             break;
281             case PASSERROR:
282             msg = Finder.getString("usermgr.error.passError");
283             break;
284             default: // Invalid Data
285             msg = Finder.getString("usermgr.error.invalidData");
286             break;
287         }
288         String err = Finder.getString("usermgr.error.add",
289             umo.getUsername());
290         getLog().log(Level.SEVERE, err + msg, e);
291             deleteUserManagedObject(umo);
292         throw new ActionFailedException(err + msg);
47baea 293     // Any other remaining exceptions
LD 294     } catch (Exception e) {
c4768d 295         String msg = Finder.getString("usermgr.error.system");
DL 296         String err = Finder.getString(
297             "usermgr.error.add", umo.getUsername());
298         getLog().log(Level.SEVERE, err + msg, e);
299         throw new ActionFailedException(err + msg);
300     }
301     }
302
303     public void saveModifiedUsers() throws ActionAbortedException,
304     ActionFailedException, ActionUnauthorizedException {
305
306     List<UserManagedObject> kids = getChildren();
307     for (UserManagedObject umo : kids) {
308         if (umo.getChangeableAggregator().isChanged()) {
309         User user = umo.getModifiedUser();
310         UserChangeFields changes = umo.getModifiedChanges();
311         try {
312             if (user != null) {
313             char[] password = null;
314             if (umo.getPassProperty().isChanged())
315                 password = umo.getPassword();
05f426 316             // System.out.println(umo.toString());
c4768d 317             getUserMgrBean().modifyUser(user, password, changes);
DL 318             if (password != null)
319                 Arrays.fill(password, (char)0);
320             }
321         } catch (SecurityException se) {
322             throw new ActionUnauthorizedException(se);
323         } catch (ObjectException e) {
324             UserMgrError ume = e.getPayload(UserMgrError.class);
325             String msg;
326             UserMgrErrorType error = (ume != null) ?
327             ume.getErrorCode() : UserMgrErrorType.INVALIDDATA;
328             switch (error) {
329             case PASSERROR:
330                 msg = Finder.getString("usermgr.error.passError");
331                 break;
332             default:
333                 msg = Finder.getString("usermgr.error.invalidData");
334                 break;
335             }
336             String err = Finder.getString(
337             "usermgr.error.modify", umo.getUsername());
338             getLog().log(Level.SEVERE, err + msg, e);
339             throw new ActionFailedException(err + msg);
340         // Any other remaining exceptions
341         } catch (Exception e) {
342             String msg = Finder.getString("usermgr.error.system");
343             String err = Finder.getString(
344             "usermgr.error.modify", umo.getUsername());
345             getLog().log(Level.SEVERE, err + msg, e);
346             throw new ActionFailedException(err + msg);
347         }
348         umo.getChangeableAggregator().save();
349         }
350     }
351     }
352
353     public List<Group> getGroups() {
47baea 354     return groupList;
c4768d 355     }
DL 356
357     public List<String> getSupplGroups() {
47baea 358     return supplgroups;
c4768d 359     }
DL 360
361     public List<String> getShells() {
47baea 362         return shellList;
c4768d 363     }
DL 364
365     public List<String> getScopes() {
47baea 366         return scopeList;
c4768d 367     }
DL 368
369     public List<String> getProfiles() {
47baea 370         return profileList;
c4768d 371     }
DL 372
373     public List<String> getAuths() {
47baea 374         return authList;
c4768d 375     }
DL 376
377     public List<String> getRoles() {
47baea 378         return roleList;
c4768d 379     }
DL 380
381     public UserImpl getDefaultUser() {
47baea 382     UserImpl defaultUser = new UserImpl(
c4768d 383         "", 0L, defUser.getGroupID(),
DL 384         "", "", defUser.getDefaultShell(),
385         0, 0, 0, 0,
386         "", "", "", "", "", "",
387         "", "", "", "", "", "",
05f426 388         null, null, null, null, null, null, null);
c4768d 389
47baea 390         return defaultUser;
c4768d 391     }
DL 392
393     public void initUsers(String scopeStr,
47baea 394             String typeStr, String matchStr)
LD 395         throws ActionFailedException {
c4768d 396     int count = 0;
DL 397     String statusStr;
398     String listTitle;
399
400
401
402     statusStr = Finder.getString("usermgr.status.scope") +
403         " " + scopeStr;
404     if (typeStr.equals(USER_TYPE_NORMAL)) {
405         uType = UserType.NORMAL;
406         listTitle = Finder.getString("usermgr.list.title.user");
407     } else {
408         uType = UserType.ROLE;
409         listTitle = Finder.getString("usermgr.list.title.role");
410     }
411
412
413     removeAllChildren();
414     try {
47baea 415         UserMgrMXBean bean = getUserMgrBean();
LD 416
417         // Set scope only if the scope changed
418         if (scopeStr.equals(this.scopeStr) == false) {
419         ScopeType sType;
420         if (scopeStr.equals(SCOPE_FILES)) {
421             sType = ScopeType.FILES;
422         } else {
423             sType = ScopeType.LDAP;
424         }
425         bean.selectScope(sType);
426         this.scopeStr = scopeStr;
427         }
428
429         setFilter(uType, matchStr);
430         this.typeStr = typeStr;
431         this.matchStr = matchStr;
432
433         List<User> users = getUsers();
434
c4768d 435         boolean uTypeSet = false;
47baea 436
c4768d 437         for (User user : users) {
DL 438         String username = user.getUsername();
439         if (uTypeSet == false) {
440             uType = bean.getUserType(username);
441             uTypeSet = true;
442         }
443
444         UserManagedObject umo = new UserManagedObject(this,
445             user, uType, null, false);
446         addChildren(umo);
447         }
448
47baea 449         // Get Assignable Lists
LD 450         scopeList = bean.getscopes();
451         groupList = bean.getgroups();
452         shellList = bean.getshells();
453         authList = bean.getauths();
454         profileList = bean.getprofiles();
455         roleList = bean.getroles();
456         supplgroups = bean.getsupplGroups();
457         defUser = bean.getdefaultUser();
458
459     } catch (Exception e) {
460         String msg = Finder.getString("usermgr.error.system");
461         throw new ActionFailedException(msg);
c4768d 462     } finally {
DL 463         setStatusText(statusStr);
464         if (mc != null) {
465         mc.setListTitle(listTitle);
466         }
467         }
47baea 468     }
LD 469
470     /*
471      * Solaris provides 3 levels of password change:
472      *  1. Can change any password
473      *  2. Can only set initial password
474      *  3. Cannot change any password
475      */
476     public boolean canChangePassword(UserManagedObject umo) {
477     String statusStr =  defUser.getAccountStatus();
478
479     if (statusStr.equals(PASSWORD)) {
480         return true;
481     } else if (statusStr.equals(LOCKED)) {
482             return false;
483     } else if (statusStr.equals(NOTACTIVATED) && (umo == null ||
484         umo.getAccountStatus().equals(UNKNOWN) ||
485         umo.getAccountStatus().equals(NOTACTIVATED))) {
486         return true;
487     }
488
489         return false;
c4768d 490     }
DL 491
492     public boolean isTypeRole() {
493         return (uType == UserType.ROLE ? true : false);
494     }
495
496     public String getTypeString() {
497         return (uType == UserType.ROLE ? "role" : "user");
498     }
499
500     public String getScope() {
501         return (scopeStr);
502     }
503
504     public String getType() {
505         return (typeStr);
506     }
507
508
509     public String getMatch() {
510         return (matchStr);
511     }
512
47baea 513     /*
LD 514      * Connection Listener interfaces
515      */
516
517     /*
518      * If a role assumption or user change occurs because
519      * of permission/auth failure, initialize the users list
520      */
521     public void connectionChanged(ConnectionEvent ce) {
522     try {
523         initUsers(scopeStr, typeStr, matchStr);
524         } catch (Exception e) {
525             getLog().log(Level.SEVERE, "Error setting filter.", e);
526     }
527     }
528
529     public void connectionFailed(ConnectionEvent ce) {
530     setStatusText(Finder.getString("usermgr.error.connfailed"));
531     }
532
c4768d 533     //
e9389d 534     // Get the user for the given user name
LD 535     //
536     public User getUser(String name) {
537
538     User user = null;
539
540     try {
541         user = getUserMgrBean().getUser(name);
542         } catch (ObjectException e) {
543             getLog().log(Level.SEVERE, "Error getting user " +
544         name + " : ", e);
545     }
546
547     return user;
548     }
549
550     //
c4768d 551     // Private methods
DL 552     //
553
554     private List<User> getUsers() {
47baea 555
c4768d 556     List<User> users = null;
DL 557     try {
558         users = getUserMgrBean().getusers();
559         } catch (ObjectException e) {
560             getLog().log(Level.SEVERE, "Error getting user list.", e);
561     }
562     return users;
563     }
564
565     private void setFilter(UserType utype, String search) {
566     try {
567         getUserMgrBean().setFilter(utype, search);
568         } catch (Exception e) {
569             getLog().log(Level.SEVERE, "Error setting filter.", e);
570     }
571     }
572 }