📄 unixuserdatabase.java
字号:
try {
rolesList.add(new UNIXRole(line));
}
catch(IllegalArgumentException iae) {
}
}
} finally {
Util.closeStream(fin);
}
Collections.sort(rolesList);
roles = new UNIXRole[rolesList.size()];
rolesList.toArray(roles);
}
} else {
throw new IOException("Could not locate " + GROUP_FILE.getAbsolutePath());
}
}
private void checkPasswdFile() throws Exception {
Date current = null;
if (PASSWD_FILE.exists()) {
if (checkShadowFile()) {
lastPasswdFileChange = null;
}
if (checkUserEmailMapFile()) {
lastPasswdFileChange = null;
}
current = new Date(PASSWD_FILE.lastModified());
if (lastPasswdFileChange == null || !lastPasswdFileChange.equals(current)) {
lastPasswdFileChange = current;
String line = null;
FileInputStream fin = new FileInputStream(PASSWD_FILE);
List userList = new ArrayList();
try {
BufferedReader r = new BufferedReader(new InputStreamReader(fin));
while ((line = r.readLine()) != null) {
String[] elements = line.split(":");
String username = elements[0];
if(elements.length > 5) {
String password = elements[1];
int uid = Integer.parseInt(elements[2]);
int gid = Integer.parseInt(elements[3]);
String fullname = elements[4];
String home = elements[5];
String shell = "";
if (elements.length > 6) {
shell = elements[6];
}
List userRolesList = new ArrayList();
Role primaryRole = getRoleByGID(gid);
if (primaryRole == null) {
log.warn("No primary group for user " + username);
} else {
userRolesList.add(primaryRole);
}
for (int i = 0; i < roles.length; i++) {
if (roles[i].containsMember(username)
&& !(primaryRole != null && roles[i].getPrincipalName().equals(
primaryRole.getPrincipalName()))) {
userRolesList.add(roles[i]);
}
}
Role[] userRoles = new Role[userRolesList.size()];
userRolesList.toArray(userRoles);
char[] pw = null;
if (password.equals("x")) {
pw = (char[]) shadowPasswords.get(username);
} else {
pw = password.toCharArray();
}
UNIXUser user = new UNIXUser(username,userEmailMap == null ? "" : userEmailMap.getProperty(username, ""), pw, uid, gid, fullname, home, shell, userRoles);
loadAttributes(user);
userList.add(user);
}
}
} finally {
Util.closeStream(fin);
}
Collections.sort(userList);
users = new UNIXUser[userList.size()];
userList.toArray(users);
}
} else {
throw new IOException("Could not locate " + PASSWD_FILE.getAbsolutePath());
}
}
private synchronized boolean checkShadowFile() throws Exception {
Date current = null;
if (SHADOW_FILE.exists()) {
current = new Date(SHADOW_FILE.lastModified());
if (lastShadowFileChange == null || !lastShadowFileChange.equals(current)) {
lastShadowFileChange = current;
String line = null;
FileInputStream fin = new FileInputStream(SHADOW_FILE);
shadowPasswords = new HashMap();
try {
BufferedReader r = new BufferedReader(new InputStreamReader(fin));
while ((line = r.readLine()) != null) {
String[] elements = line.split(":");
String username = elements[0];
if (elements.length > 1 && !username.equals("+")) {
char[] password = elements[1].toCharArray();
shadowPasswords.put(username, password);
}
}
} finally {
Util.closeStream(fin);
}
return true;
}
} else {
throw new IOException("Could not locate " + PASSWD_FILE.getAbsolutePath());
}
return false;
}
private synchronized boolean checkUserEmailMapFile() throws Exception {
if (!USER_EMAIL_MAP_FILE.exists()) {
if (userEmailMap != null) {
userEmailMap = null;
userEmailMapLastModified = -1;
return true;
}
} else if (userEmailMap == null) {
userEmailMap = new Properties();
}
if (userEmailMap != null
&& (userEmailMapLastModified == -1 || userEmailMapLastModified != USER_EMAIL_MAP_FILE.lastModified())) {
FileInputStream fin = null;
try {
fin = new FileInputStream(USER_EMAIL_MAP_FILE);
userEmailMap.load(fin);
} catch (IOException ioe) {
log.error("Failed to load user email map.");
} finally {
Util.closeStream(fin);
}
userEmailMapLastModified = USER_EMAIL_MAP_FILE.lastModified();
return true;
}
return false;
}
/**
* @param gid
* @return
*/
private Role getRoleByGID(int gid) throws Exception {
checkGroupFile();
for (int i = 0; i < roles.length; i++) {
if (roles[i].getGid() == gid) {
return roles[i];
}
}
return null;
}
public void cleanup() throws Exception {
}
public int getInstallationPropertyCategory() {
return -1;
}
public boolean isOpen() {
return open;
}
public User[] getUsersInRole(Role role) throws Exception {
return CoreUtil.getUsersInRole(role, this);
}
public void changePassword(String username, String password, boolean forcePasswordChangeAtLogon) throws UserDatabaseException,
InvalidLoginCredentialsException {
if (!supportsPasswordChange()) {
throw new InvalidLoginCredentialsException("Database doesn't support password change.");
}
if (forcePasswordChangeAtLogon) {
log.warn("Password change function of UNIX user database does not support forcePassswordChangeAtLogon.");
}
Process p = null;
try {
p = Runtime.getRuntime().exec(
"true".equals(System.getProperty("sslexplorer.useDevConfig", "false")) ? "sudo /usr/sbin/chpasswd"
: "/usr/sbin/chpasswd");
new StreamReaderThread(p.getInputStream());
new StreamReaderThread(p.getErrorStream());
OutputStream out = p.getOutputStream();
PrintWriter pw = new PrintWriter(out);
pw.println(username + ":" + password);
pw.flush();
out.close();
try {
p.waitFor();
} catch (InterruptedException ie) {
}
int ret = p.exitValue();
if (ret != 0) {
throw new UserDatabaseException("Failed to change password. chpasswd returned exit code " + ret + ".");
}
} catch (IOException e) {
throw new UserDatabaseException("Failed to change password.", e);
} finally {
if (p != null) {
Util.closeStream(p.getOutputStream());
Util.closeStream(p.getInputStream());
Util.closeStream(p.getErrorStream());
}
}
}
class StreamReaderThread extends Thread {
InputStream in;
StreamReaderThread(InputStream in) {
this.in = in;
}
public void run() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
if (log.isInfoEnabled())
log.info("Output from chpasswd: '" + line + "'");
}
} catch (IOException ioe) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -