001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2006 Marc Eppelmann
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.util.os.unix;
023:
024: import java.util.StringTokenizer;
025:
026: /**
027: * This represents a Unix User. If initialized via fromEtcPasswdLine(), the users
028: * Name, home, uid, gid, and shell can be asked.
029: *
030: * @author marc.eppelmann@reddot.de
031: */
032: public class UnixUser {
033: //~ Instance fields ********************************************************************
034:
035: /** internal itsName */
036: private String itsName;
037:
038: /** internal itsPasswdDigest */
039: private String itsPasswdDigest;
040:
041: /** internal itsId */
042: private String itsId;
043:
044: /** internal itsGid */
045: private String itsGid;
046:
047: /** internal itsDescription */
048: private String itsDescription;
049:
050: /** internal itsHome */
051: private String itsHome;
052:
053: /** internal itsShell */
054: private String itsShell;
055:
056: //~ Methods ****************************************************************************
057:
058: /**
059: * Gets the Users Login Name
060: *
061: * @return the users login Name
062: */
063: public String getName() {
064: return itsName;
065: }
066:
067: /**
068: * Gets the users passwd Digest or X if hidden in /etc/shadow
069: *
070: * @return the passwdDigest or x
071: */
072: public String getPasswdDigest() {
073: return itsPasswdDigest;
074: }
075:
076: /**
077: * Gets the Users UID
078: *
079: * @return The Uid
080: */
081: public String getId() {
082: return itsId;
083: }
084:
085: /**
086: * Gtes the Users Group ID
087: *
088: * @return the gid
089: */
090: public String getGid() {
091: return itsGid;
092: }
093:
094: /**
095: * Gets the Description aka Full Name
096: *
097: * @return the users descriptio or full name
098: */
099: public String getDescription() {
100: return itsDescription;
101: }
102:
103: /**
104: * Gets the Users Home Directory
105: *
106: * @return the users home dir
107: */
108: public String getHome() {
109: return itsHome;
110: }
111:
112: /**
113: * Gets the users default Login-Shell
114: *
115: * @return The login shell or /bin/false for system users
116: */
117: public String getShell() {
118: return itsShell;
119: }
120:
121: /**
122: * Parses a Line from /etc/passwd and stores each :token: in their field of the user.
123: * Sample Line from /etc/passwd "eppelmann.local:x:900:100:Marc Eppelmann:/mnt/local/home/eppelmann.local:/bin/bash"
124: * @param anEtcPasswdLine A Passwd Line of the User.
125: *
126: * @return The filled User
127: */
128: public UnixUser fromEtcPasswdLine(String anEtcPasswdLine) {
129: if (anEtcPasswdLine == null) {
130: return null;
131: }
132:
133: StringTokenizer usersToken = new StringTokenizer(
134: anEtcPasswdLine, ":");
135:
136: UnixUser u = new UnixUser();
137:
138: if (usersToken.hasMoreTokens()) {
139: u.itsName = usersToken.nextToken();
140: }
141:
142: if (usersToken.hasMoreTokens()) {
143: u.itsPasswdDigest = usersToken.nextToken();
144: }
145:
146: if (usersToken.hasMoreTokens()) {
147: u.itsId = usersToken.nextToken();
148: }
149:
150: if (usersToken.hasMoreTokens()) {
151: u.itsGid = usersToken.nextToken();
152: }
153:
154: if (usersToken.hasMoreTokens()) {
155: u.itsDescription = usersToken.nextToken();
156: }
157:
158: if (usersToken.hasMoreTokens()) {
159: u.itsHome = usersToken.nextToken();
160: }
161:
162: if (usersToken.hasMoreTokens()) {
163: u.itsShell = usersToken.nextToken();
164: }
165:
166: return u;
167: }
168:
169: /**
170: * Dumps the USer fields
171: *
172: * @return The User representation as String
173: */
174: public String toString() {
175: StringBuffer result = new StringBuffer();
176:
177: result.append("User: ");
178: result.append(itsName);
179:
180: result.append(" X: ");
181: result.append(itsPasswdDigest);
182:
183: result.append(" Id: ");
184: result.append(itsId);
185:
186: result.append(" Gid: ");
187: result.append(itsGid);
188:
189: result.append(" Desc.: ");
190: result.append(itsDescription);
191:
192: result.append(" Home: ");
193: result.append(itsHome);
194:
195: result.append(" Shell: ");
196: result.append(itsShell);
197:
198: return result.toString();
199: }
200:
201: /**
202: * Static Test Main
203: *
204: * @param args
205: */
206: public static void main(String[] args) {
207: System.out.println(new UnixUser().fromEtcPasswdLine(""));
208: System.out
209: .println(new UnixUser()
210: .fromEtcPasswdLine("eppelmann.local:x:500:100:Marc L Eppelmann:/mnt/local/home/eppelmann.local:/bin/bash"));
211: }
212: }
|