Replace passwd by chpasswd to set password because Debian passwd command

does not have the '--stdin' option.
This commit is contained in:
Christian P. MOMON 2012-09-17 15:39:29 +02:00
parent 94d55f65d2
commit 280280b494

View file

@ -8,7 +8,6 @@ import fr.devinsy.util.cmdexec.CmdExec;
import fr.devinsy.util.unix.acl.Acl; import fr.devinsy.util.unix.acl.Acl;
import fr.devinsy.util.unix.acl.AclManager; import fr.devinsy.util.unix.acl.AclManager;
/** /**
* *
*/ */
@ -17,140 +16,82 @@ public class Unix
static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Unix.class); static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Unix.class);
static public final String SUDO = "/usr/bin/sudo"; static public final String SUDO = "/usr/bin/sudo";
/** /**
* *
*/ */
static public boolean isLogin (String login) static public void appendToFile(final String text, final String path) throws Exception
{ {
boolean result; if ((text == null) || (text.length() == 0) || (path == null) || (path.length() == 0))
result = EtcPasswdFile.instance().contains(login);
//
return (result);
}
/**
*
*/
static public boolean isGroup (String groupName)
{ {
boolean result; throw new Exception("Parameter undefined: [" + text + "][" + path + "].");
result = EtcGroupFile.instance().contains(groupName);
//
return (result);
}
/**
*
*/
static public User searchLogin(String login)
{
User result;
result = EtcPasswdFile.instance ().get (login);
//
return (result);
}
/**
*
*/
static public Group searchGroup (String groupName)
{
Group result;
result = EtcGroupFile.instance ().get(groupName);
//
return (result);
}
/**
*
*/
static public Vector<String> searchLoginGroups (String login)
{
Vector<String> result;
result = EtcGroupFile.instance ().getLoginGroups (login);
//
return (result);
}
/**
* chfn [ -f full-name ] [ username ]
*/
static public void setRealName (String login, String newRealName) throws Exception
{
if ((login == null) || (login.length () == 0))
{
throw new Exception ("Login parameter undefined.");
}
else if (newRealName == null)
{
throw new Exception ("New real name parameter undefined.");
} }
else else
{ {
try try
{ {
logger.info ("Real name changing for user [" + login + "]."); CmdExec.run(SUDO, "bash", "-c", "echo \"" + text + "\" >> " + path);
CmdExec.run (SUDO, "chfn", "-f", newRealName, login);
EtcPasswdFile.instance().update();
logger.info ("Real name changed for user [" + login + "].");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Error detected on setting of the new real name for [" + login + "].", exception); throw new Exception("Error detected appending text to file [" + path + "].", exception);
} }
} }
} }
/** /**
* *
*/ */
static public void setPassword (String login, String newPassword) throws Exception static public void chmod(final String changes, final String path) throws Exception
{ {
if ((login == null) || (login.length () == 0)) if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
{ {
throw new Exception ("Login parameter undefined."); throw new Exception("Parameter undefined: [" + changes + "][" + path + "].");
} }
else if (newPassword == null) else if (!new File(path).exists())
{ {
throw new Exception ("New password parameter undefined."); throw new Exception("Path not found: [" + path + "].");
} }
else else
{ {
try try
{ {
logger.info ("Password setting for [" + login + "]."); CmdExec.run(SUDO, "chmod", changes, path);
CmdExec.run (SUDO, "bash", "-c", "echo \"" + newPassword + "\"| passwd " + login + " --stdin");
logger.info ("Password set for [" + login + "].");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Error detected on setting of the new password for [" + login + "].", exception); throw new Exception("Error running chmod command for [" + changes + "][" + path + "].", exception);
} }
} }
} }
/** /**
* *
*/ */
static public void createUserAccount (String login) throws Exception static public void clearAcl(final String id, final String filePathName) throws Exception
{
AclManager.clearId(id, filePathName);
}
/**
*
*/
static public void clearAclGroup(final String group, final String filePathName) throws Exception
{
AclManager.clearGroup(group, filePathName);
}
/**
*
*/
static public void clearAclUser(final String login, final String filePathName) throws Exception
{
AclManager.clearUser(login, filePathName);
}
/**
*
*/
static public void createUserAccount(final String login) throws Exception
{ {
if ((login == null) || (login.length() == 0)) if ((login == null) || (login.length() == 0))
{ {
@ -162,11 +103,10 @@ public class Unix
} }
} }
/** /**
* *
*/ */
static public void createUserAccount (String login, String name) throws Exception static public void createUserAccount(final String login, final String name) throws Exception
{ {
if ((login == null) || (login.length() == 0)) if ((login == null) || (login.length() == 0))
{ {
@ -182,11 +122,10 @@ public class Unix
} }
} }
/** /**
* *
*/ */
static public void createUserAccount (String login, String name, String home) throws Exception static public void createUserAccount(final String login, final String name, final String home) throws Exception
{ {
if ((login == null) || (login.length() == 0)) if ((login == null) || (login.length() == 0))
{ {
@ -216,11 +155,10 @@ public class Unix
} }
} }
/** /**
* *
*/ */
static public void createUserAccount (String login, String name, String home, String password) throws Exception static public void createUserAccount(final String login, final String name, final String home, final String password) throws Exception
{ {
if ((password == null) || (password.length() == 0)) if ((password == null) || (password.length() == 0))
{ {
@ -237,36 +175,10 @@ public class Unix
} }
} }
/** /**
* *
*/ */
static public void deleteUserAccount (String login) throws Exception static public void deleteGroup(final String group) throws Exception
{
if ((login == null) || (login.length() == 0))
{
throw new Exception ("Login parameter undefined.");
}
else
{
try
{
logger.info ("Deleting user account for [" + login + "].");
CmdExec.run (SUDO + " /usr/sbin/userdel " + login);
logger.info ("User account delted for [" + login + "].");
}
catch (Exception exception)
{
throw new Exception ("Error running userdel command for login [" + login + "].", exception);
}
}
}
/**
*
*/
static public void deleteGroup (String group) throws Exception
{ {
if ((group == null) || (group.length() == 0)) if ((group == null) || (group.length() == 0))
{ {
@ -287,11 +199,125 @@ public class Unix
} }
} }
/**
*
*/
static public void deleteUserAccount(final String login) throws Exception
{
if ((login == null) || (login.length() == 0))
{
throw new Exception("Login parameter undefined.");
}
else
{
try
{
logger.info("Deleting user account for [" + login + "].");
CmdExec.run(SUDO + " /usr/sbin/userdel " + login);
logger.info("User account delted for [" + login + "].");
}
catch (Exception exception)
{
throw new Exception("Error running userdel command for login [" + login + "].", exception);
}
}
}
/** /**
* *
*/ */
static public void modifyLogin (String sourceLogin, String targetLogin, String sourceHomeDirectory) throws Exception static public String getAclData(final String filePathName) throws Exception
{
String result;
result = AclManager.getAclData(filePathName);
//
return (result);
}
/**
*
*/
static public String[] getAclUsers(final String filePathName) throws Exception
{
String[] result;
Acl acl = AclManager.getAcl(filePathName);
result = acl.currentAcl().getUserIds();
//
return (result);
}
/**
*
*/
static public boolean isGroup(final String groupName)
{
boolean result;
result = EtcGroupFile.instance().contains(groupName);
//
return (result);
}
/**
*
*/
static public boolean isLogin(final String login)
{
boolean result;
result = EtcPasswdFile.instance().contains(login);
//
return (result);
}
/**
*
*/
static public void link(final String sourcePath, final String targetPath) throws Exception
{
logger.info("[" + sourcePath + "][" + targetPath + "]");
if ((sourcePath == null) || (sourcePath.length() == 0) || (targetPath == null) || (targetPath.length() == 0))
{
throw new Exception("Parameter undefined: [" + sourcePath + "][" + targetPath + "].");
}
else
{
File sourceFile = new File(sourcePath);
File targetFile = new File(targetPath);
if (!sourceFile.exists())
{
throw new Exception("Source does not exist: [" + sourcePath + "].");
}
else if ((targetFile.exists()) && (!targetFile.isDirectory()))
{
throw new Exception("Target already exists: [" + targetPath + "].");
}
else
{
try
{
CmdExec.run(SUDO, "ln", "-s", sourcePath, targetPath);
}
catch (Exception exception)
{
throw new Exception("Error detected linking [" + sourcePath + "][" + targetPath + "].", exception);
}
}
}
}
/**
*
*/
static public void modifyLogin(final String sourceLogin, final String targetLogin, final String sourceHomeDirectory) throws Exception
{ {
logger.info("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); logger.info("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]");
if ((sourceLogin == null) || (sourceLogin.length() == 0)) if ((sourceLogin == null) || (sourceLogin.length() == 0))
@ -353,11 +379,36 @@ public class Unix
logger.info("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]"); logger.info("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]");
} }
/**
*
*/
static public void recursiveChmod(final String changes, final String path) throws Exception
{
if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
{
throw new Exception("Parameter undefined: [" + changes + "][" + path + "].");
}
else if (!new File(path).exists())
{
throw new Exception("Path not found: [" + path + "].");
}
else
{
try
{
CmdExec.run(SUDO, "chmod", "-R", changes, path);
}
catch (Exception exception)
{
throw new Exception("Error running recursive chmod command for [" + changes + "][" + path + "].", exception);
}
}
}
/** /**
* *
*/ */
static public void renameGroup (String sourceGroup, String targetGroup) throws Exception static public void renameGroup(final String sourceGroup, final String targetGroup) throws Exception
{ {
logger.info("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]"); logger.info("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]");
if ((sourceGroup == null) || (sourceGroup.length() == 0)) if ((sourceGroup == null) || (sourceGroup.length() == 0))
@ -392,67 +443,49 @@ public class Unix
logger.info("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]"); logger.info("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]");
} }
/**
*
*/
static public Group searchGroup(final String groupName)
{
Group result;
result = EtcGroupFile.instance().get(groupName);
//
return (result);
}
/** /**
* *
*/ */
static public void recursiveChmod (String changes, String path) throws Exception static public User searchLogin(final String login)
{ {
if ((changes == null) || (changes.length() == 0) || User result;
(path == null) || (path.length() == 0))
{
throw new Exception ("Parameter undefined: [" + changes + "][" + path + "].");
}
else if (!new File(path).exists())
{
throw new Exception ("Path not found: [" + path + "].");
}
else
{
try
{
CmdExec.run (SUDO, "chmod", "-R", changes, path);
}
catch (Exception exception)
{
throw new Exception ("Error running recursive chmod command for [" + changes + "][" + path + "].", exception);
}
}
}
result = EtcPasswdFile.instance().get(login);
//
return (result);
}
/** /**
* *
*/ */
static public void chmod (String changes, String path) throws Exception static public Vector<String> searchLoginGroups(final String login)
{ {
if ((changes == null) || (changes.length() == 0) || Vector<String> result;
(path == null) || (path.length() == 0))
{
throw new Exception ("Parameter undefined: [" + changes + "][" + path + "].");
}
else if (!new File(path).exists())
{
throw new Exception ("Path not found: [" + path + "].");
}
else
{
try
{
CmdExec.run (SUDO, "chmod", changes, path);
}
catch (Exception exception)
{
throw new Exception ("Error running chmod command for [" + changes + "][" + path + "].", exception);
}
}
}
result = EtcGroupFile.instance().getLoginGroups(login);
//
return (result);
}
/** /**
* *
*/ */
static public void setfacl (String ... args) throws Exception static public void setfacl(final String... args) throws Exception
{ {
try try
{ {
@ -460,108 +493,73 @@ public class Unix
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + ".", exception); throw new Exception("Error running setfacl command for " + StringConcatenator.toStringWithBrackets(args) + ":" + exception.getMessage() + ".",
exception);
} }
} }
/** /**
* * As 'passwd' command has not the option '--stdin' in all systems (eg.
* Debian), this method uses the 'chpasswd' command.
*/ */
static public String getAclData (String filePathName) throws Exception static public void setPassword(final String login, final String newPassword) throws Exception
{ {
String result; if ((login == null) || (login.length() == 0))
{
result = AclManager.getAclData(filePathName); throw new Exception("Login parameter undefined.");
//
return(result);
} }
else if (newPassword == null)
/**
*
*/
static public String[] getAclUsers (String filePathName) throws Exception
{ {
String[] result; throw new Exception("New password parameter undefined.");
Acl acl = AclManager.getAcl(filePathName);
result = acl.currentAcl().getUserIds();
//
return(result);
}
/**
*
*/
static public void appendToFile (String text, String path) throws Exception
{
if ((text == null) || (text.length() == 0) ||
(path == null) || (path.length() == 0))
{
throw new Exception ("Parameter undefined: [" + text + "][" + path + "].");
} }
else else
{ {
try try
{ {
CmdExec.run (SUDO, "bash", "-c", "echo \"" + text + "\" >> " + path); logger.info("Password setting for [" + login + "].");
CmdExec.run(SUDO, "bash", "-c", "echo \"" + login + ":" + newPassword + "\"| chpasswd ");
logger.info("Password set for [" + login + "].");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Error detected appending text to file [" + path + "].", exception); throw new Exception("Error detected on setting of the new password for [" + login + "].", exception);
} }
} }
} }
/** /**
* * chfn [ -f full-name ] [ username ]
*/ */
static public void link (String sourcePath, String targetPath) throws Exception static public void setRealName(final String login, final String newRealName) throws Exception
{ {
logger.info("[" + sourcePath + "][" + targetPath + "]"); if ((login == null) || (login.length() == 0))
if ((sourcePath == null) || (sourcePath.length() == 0) ||
(targetPath == null) || (targetPath.length() == 0))
{ {
throw new Exception ("Parameter undefined: [" + sourcePath + "][" + targetPath + "]."); throw new Exception("Login parameter undefined.");
} }
else else if (newRealName == null)
{ {
File sourceFile = new File (sourcePath); throw new Exception("New real name parameter undefined.");
File targetFile = new File (targetPath);
if (!sourceFile.exists())
{
throw new Exception ("Source does not exist: [" + sourcePath + "].");
}
else if ((targetFile.exists()) && (!targetFile.isDirectory()))
{
throw new Exception ("Target already exists: [" + targetPath + "].");
} }
else else
{ {
try try
{ {
CmdExec.run (SUDO, "ln", "-s", sourcePath, targetPath); logger.info("Real name changing for user [" + login + "].");
CmdExec.run(SUDO, "chfn", "-f", newRealName, login);
EtcPasswdFile.instance().update();
logger.info("Real name changed for user [" + login + "].");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Error detected linking [" + sourcePath + "][" + targetPath + "].", exception); throw new Exception("Error detected on setting of the new real name for [" + login + "].", exception);
} }
} }
} }
}
/** /**
* *
*/ */
static public void unlink (String filePathName) throws Exception static public void unlink(final String filePathName) throws Exception
{ {
logger.info("[" + filePathName + "]"); logger.info("[" + filePathName + "]");
if (filePathName == null) if (filePathName == null)
@ -573,33 +571,6 @@ public class Unix
new File(filePathName).delete(); new File(filePathName).delete();
} }
} }
/**
*
*/
static public void clearAclUser (String login, String filePathName) throws Exception
{
AclManager.clearUser(login, filePathName);
}
/**
*
*/
static public void clearAclGroup (String group, String filePathName) throws Exception
{
AclManager.clearGroup(group, filePathName);
}
/**
*
*/
static public void clearAcl (String id, String filePathName) throws Exception
{
AclManager.clearId(id, filePathName);
}
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////