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,34 +8,253 @@ 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;
/** /**
* *
*/ */
public class Unix 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))
{
throw new Exception("Parameter undefined: [" + text + "][" + path + "].");
}
else
{
try
{
CmdExec.run(SUDO, "bash", "-c", "echo \"" + text + "\" >> " + path);
}
catch (Exception exception)
{
throw new Exception("Error detected appending text to file [" + path + "].", exception);
}
}
}
result = EtcPasswdFile.instance().contains(login); /**
*
*/
static public void chmod(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", changes, path);
}
catch (Exception exception)
{
throw new Exception("Error running chmod command for [" + changes + "][" + path + "].", 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))
{
throw new Exception("Login parameter undefined.");
}
else
{
createUserAccount(login, login);
}
}
/**
*
*/
static public void createUserAccount(final String login, final String name) throws Exception
{
if ((login == null) || (login.length() == 0))
{
throw new Exception("Login parameter undefined.");
}
else if ((name == null) || (name.length() == 0))
{
throw new Exception("Name parameter undefined.");
}
else
{
createUserAccount(login, name, "/home/" + login);
}
}
/**
*
*/
static public void createUserAccount(final String login, final String name, final String home) throws Exception
{
if ((login == null) || (login.length() == 0))
{
throw new Exception("Login parameter undefined.");
}
else if ((name == null) || (name.length() == 0))
{
throw new Exception("Name parameter undefined.");
}
else if ((home == null) || (home.length() == 0))
{
throw new Exception("Home parameter undefined.");
}
else
{
try
{
logger.info("Creating user account for [" + login + "].");
CmdExec.run(SUDO, "/usr/sbin/useradd", "-c", name, "-d", home, login);
EtcPasswdFile.instance().update();
logger.info("User account created for [" + login + "].");
}
catch (Exception exception)
{
throw new Exception("Error detected creating user account [" + login + "].", 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))
{
throw new Exception("Password parameter undefined.");
}
else if (Unix.isLogin(login))
{
throw new Exception("Login [" + login + "] already in use");
}
else
{
createUserAccount(login, name, home);
setPassword(login, password);
}
}
/**
*
*/
static public void deleteGroup(final String group) throws Exception
{
if ((group == null) || (group.length() == 0))
{
throw new Exception("Group parameter undefined.");
}
else
{
try
{
logger.info("Deleting group for [" + group + "].");
CmdExec.run(SUDO + " groupdel " + group);
logger.info("Group deleted for [" + group + "].");
}
catch (Exception exception)
{
throw new Exception("Error running groupdel command for group [" + group + "].", exception);
}
}
}
/**
*
*/
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 String getAclData(final String filePathName) throws Exception
{
String result;
result = AclManager.getAclData(filePathName);
// //
return (result); 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 (String groupName) static public boolean isGroup(final String groupName)
{ {
boolean result; boolean result;
@ -45,274 +264,81 @@ public class Unix
return (result); return (result);
} }
/** /**
* *
*/ */
static public User searchLogin(String login) static public boolean isLogin(final String login)
{ {
User result; boolean result;
result = EtcPasswdFile.instance ().get (login); result = EtcPasswdFile.instance().contains(login);
// //
return (result); return (result);
} }
/** /**
* *
*/ */
static public Group searchGroup (String groupName) static public void link(final String sourcePath, final String targetPath) throws Exception
{ {
Group result; logger.info("[" + sourcePath + "][" + targetPath + "]");
if ((sourcePath == null) || (sourcePath.length() == 0) || (targetPath == null) || (targetPath.length() == 0))
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."); throw new Exception("Parameter undefined: [" + sourcePath + "][" + targetPath + "].");
}
else if (newRealName == null)
{
throw new Exception ("New real name parameter undefined.");
} }
else else
{ {
try File sourceFile = new File(sourcePath);
File targetFile = new File(targetPath);
if (!sourceFile.exists())
{ {
logger.info ("Real name changing for user [" + login + "]."); throw new Exception("Source does not exist: [" + sourcePath + "].");
CmdExec.run (SUDO, "chfn", "-f", newRealName, login);
EtcPasswdFile.instance().update();
logger.info ("Real name changed for user [" + login + "].");
} }
catch (Exception exception) else if ((targetFile.exists()) && (!targetFile.isDirectory()))
{ {
throw new Exception ("Error detected on setting of the new real name for [" + login + "].", exception); 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 setPassword (String login, String newPassword) throws Exception static public void modifyLogin(final String sourceLogin, final String targetLogin, final String sourceHomeDirectory) throws Exception
{ {
if ((login == null) || (login.length () == 0)) logger.info("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]");
if ((sourceLogin == null) || (sourceLogin.length() == 0))
{ {
throw new Exception ("Login parameter undefined."); throw new Exception("Original login parameters undefined");
} }
else if (newPassword == null) else if ((targetLogin == null) || (targetLogin.length() == 0))
{ {
throw new Exception ("New password parameter undefined."); throw new Exception("New login parameters undefined");
}
else
{
try
{
logger.info ("Password setting for [" + login + "].");
CmdExec.run (SUDO, "bash", "-c", "echo \"" + newPassword + "\"| passwd " + login + " --stdin");
logger.info ("Password set for [" + login + "].");
}
catch (Exception exception)
{
throw new Exception ("Error detected on setting of the new password for [" + login + "].", exception);
}
}
}
/**
*
*/
static public void createUserAccount (String login) throws Exception
{
if ((login == null) || (login.length () == 0))
{
throw new Exception ("Login parameter undefined.");
}
else
{
createUserAccount(login, login);
}
}
/**
*
*/
static public void createUserAccount (String login, String name) throws Exception
{
if ((login == null) || (login.length () == 0))
{
throw new Exception ("Login parameter undefined.");
}
else if ((name == null) || (name.length () == 0))
{
throw new Exception ("Name parameter undefined.");
}
else
{
createUserAccount(login, name, "/home/" + login);
}
}
/**
*
*/
static public void createUserAccount (String login, String name, String home) throws Exception
{
if ((login == null) || (login.length () == 0))
{
throw new Exception ("Login parameter undefined.");
}
else if ((name == null) || (name.length () == 0))
{
throw new Exception ("Name parameter undefined.");
}
else if ((home == null) || (home.length () == 0))
{
throw new Exception ("Home parameter undefined.");
}
else
{
try
{
logger.info ("Creating user account for [" + login + "].");
CmdExec.run (SUDO, "/usr/sbin/useradd", "-c", name, "-d", home, login);
EtcPasswdFile.instance().update();
logger.info ("User account created for [" + login + "].");
}
catch(Exception exception)
{
throw new Exception ("Error detected creating user account [" + login + "].", exception);
}
}
}
/**
*
*/
static public void createUserAccount (String login, String name, String home, String password) throws Exception
{
if ((password == null) || (password.length () == 0))
{
throw new Exception ("Password parameter undefined.");
}
else if (Unix.isLogin(login))
{
throw new Exception ("Login [" + login + "] already in use");
}
else
{
createUserAccount(login, name, home);
setPassword(login, password);
}
}
/**
*
*/
static public void deleteUserAccount (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 deleteGroup (String group) throws Exception
{
if ((group == null) || (group.length() == 0))
{
throw new Exception ("Group parameter undefined.");
}
else
{
try
{
logger.info ("Deleting group for [" + group + "].");
CmdExec.run (SUDO + " groupdel " + group);
logger.info ("Group deleted for [" + group + "].");
}
catch (Exception exception)
{
throw new Exception ("Error running groupdel command for group [" + group + "].", exception);
}
}
}
/**
*
*/
static public void modifyLogin (String sourceLogin, String targetLogin, String sourceHomeDirectory) throws Exception
{
logger.info ("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]");
if ((sourceLogin == null) || (sourceLogin.length () == 0))
{
throw new Exception ("Original login parameters undefined");
}
else if ((targetLogin == null) || (targetLogin.length () == 0))
{
throw new Exception ("New login parameters undefined");
} }
else if (!Unix.isLogin(sourceLogin)) else if (!Unix.isLogin(sourceLogin))
{ {
throw new Exception ("Original login unknow: [" + sourceLogin + "]."); throw new Exception("Original login unknow: [" + sourceLogin + "].");
} }
else if (Unix.isLogin(targetLogin)) else if (Unix.isLogin(targetLogin))
{ {
throw new Exception ("New login unknow: [" + targetLogin + "]."); throw new Exception("New login unknow: [" + targetLogin + "].");
} }
else if (sourceHomeDirectory == null) else if (sourceHomeDirectory == null)
{ {
throw new Exception ("sourceHomeDirectory parameter undefined, thus no home directory move."); throw new Exception("sourceHomeDirectory parameter undefined, thus no home directory move.");
} }
else if (!new File(sourceHomeDirectory).exists()) else if (!new File(sourceHomeDirectory).exists())
{ {
@ -329,130 +355,137 @@ public class Unix
{ {
try try
{ {
logger.info ("Login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); logger.info("Login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]");
CmdExec.run (SUDO + " usermod -l "+ targetLogin + " " + sourceLogin); CmdExec.run(SUDO + " usermod -l " + targetLogin + " " + sourceLogin);
logger.info ("Login modified: [" + sourceLogin + "] -> [" + targetLogin + "]"); logger.info("Login modified: [" + sourceLogin + "] -> [" + targetLogin + "]");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Login modification failed for [" + sourceLogin + "].", exception); throw new Exception("Login modification failed for [" + sourceLogin + "].", exception);
} }
try try
{ {
logger.info ("Renaming home directory: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); logger.info("Renaming home directory: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]");
CmdExec.run (SUDO + " mv " + sourceHomeDirectory + " " + targetHomeDirectory); CmdExec.run(SUDO + " mv " + sourceHomeDirectory + " " + targetHomeDirectory);
logger.info ("Home directory renamed: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); logger.info("Home directory renamed: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Home directory rename failed for [" + sourceHomeDirectory + "].", exception); throw new Exception("Home directory rename failed for [" + sourceHomeDirectory + "].", exception);
} }
} }
} }
logger.info ("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]"); logger.info("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]");
} }
/** /**
* *
*/ */
static public void renameGroup (String sourceGroup, String targetGroup) throws Exception static public void recursiveChmod(final String changes, final String path) throws Exception
{ {
logger.info ("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]"); if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0))
if ((sourceGroup == null) || (sourceGroup.length () == 0))
{ {
throw new Exception ("Original group name parameters undefined"); throw new Exception("Parameter undefined: [" + changes + "][" + path + "].");
} }
else if ((targetGroup == null) || (targetGroup.length () == 0)) else if (!new File(path).exists())
{ {
throw new Exception ("New group name parameters undefined"); 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(final String sourceGroup, final String targetGroup) throws Exception
{
logger.info("Starting group renaming: [" + sourceGroup + "] -> [" + targetGroup + "]");
if ((sourceGroup == null) || (sourceGroup.length() == 0))
{
throw new Exception("Original group name parameters undefined");
}
else if ((targetGroup == null) || (targetGroup.length() == 0))
{
throw new Exception("New group name parameters undefined");
} }
else if (!Unix.isGroup(sourceGroup)) else if (!Unix.isGroup(sourceGroup))
{ {
throw new Exception ("Original group unknow: [" + sourceGroup + "]."); throw new Exception("Original group unknow: [" + sourceGroup + "].");
} }
else if (Unix.isGroup(targetGroup)) else if (Unix.isGroup(targetGroup))
{ {
throw new Exception ("New group unknow: [" + targetGroup + "]."); throw new Exception("New group unknow: [" + targetGroup + "].");
} }
else else
{ {
try try
{ {
logger.info ("Login modifying: [" + sourceGroup + "] -> [" + targetGroup + "]"); logger.info("Login modifying: [" + sourceGroup + "] -> [" + targetGroup + "]");
CmdExec.run (SUDO + " groupmod -n "+ targetGroup + " " + sourceGroup); CmdExec.run(SUDO + " groupmod -n " + targetGroup + " " + sourceGroup);
logger.info ("Login modified: [" + sourceGroup + "] -> [" + targetGroup + "]"); logger.info("Login modified: [" + sourceGroup + "] -> [" + targetGroup + "]");
} }
catch (Exception exception) catch (Exception exception)
{ {
throw new Exception ("Group renaming failed for [" + sourceGroup + "].", exception); throw new Exception("Group renaming failed for [" + sourceGroup + "].", exception);
} }
} }
logger.info ("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]"); logger.info("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]");
} }
/** /**
* *
*/ */
static public void recursiveChmod (String changes, String path) throws Exception static public Group searchGroup(final String groupName)
{ {
if ((changes == null) || (changes.length() == 0) || Group 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 = EtcGroupFile.instance().get(groupName);
//
return (result);
}
/** /**
* *
*/ */
static public void chmod (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", changes, path);
}
catch (Exception exception)
{
throw new Exception ("Error running chmod command for [" + changes + "][" + path + "].", exception);
}
}
}
result = EtcPasswdFile.instance().get(login);
//
return (result);
}
/** /**
* *
*/ */
static public void setfacl (String ... args) throws Exception static public Vector<String> searchLoginGroups(final String login)
{
Vector<String> result;
result = EtcGroupFile.instance().getLoginGroups(login);
//
return (result);
}
/**
*
*/
static public void setfacl(final String... args) throws Exception
{ {
try try
{ {
@ -460,146 +493,84 @@ 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);
//
return(result);
}
/**
*
*/
static public String[] getAclUsers (String filePathName) throws Exception
{
String[] result;
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 + "]."); throw new Exception("Login parameter undefined.");
}
else if (newPassword == null)
{
throw new Exception("New password parameter undefined.");
} }
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 if (newRealName == null)
{
throw new Exception("New real name parameter undefined.");
} }
else else
{ {
File sourceFile = new File (sourcePath); try
File targetFile = new File (targetPath);
if (!sourceFile.exists())
{ {
throw new Exception ("Source does not exist: [" + sourcePath + "]."); 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 + "].");
} }
else if ((targetFile.exists()) && (!targetFile.isDirectory())) catch (Exception exception)
{ {
throw new Exception ("Target already exists: [" + targetPath + "]."); throw new Exception("Error detected on setting of the new real name for [" + login + "].", exception);
}
else
{
try
{
CmdExec.run (SUDO, "ln", "-s", sourcePath, targetPath);
}
catch (Exception exception)
{
throw new Exception ("Error detected linking [" + sourcePath + "][" + targetPath + "].", 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)
{ {
throw new Exception ("Parameter undefined: [" + filePathName + "]."); throw new Exception("Parameter undefined: [" + filePathName + "].");
} }
else else
{ {
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);
}
} }
// //////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////