Troubleshooting
Problem
The tool creates the home directories that are specified in the user profiles.
Resolving The Problem
All user profiles specify a home directory defined as part of the profile; however, there is nothing in the creation of a user profile that creates that directory. Often, applications or services (such as Kerberos) need this space as a repository. The default path is
https://public.dhe.ibm.com/services/us/igsc/cs2/ApiSamples/CreateHomeDirectories.zip
'/home/<usrprf>'; however, that is not always the case. The path might be something other than the default; therefore, a simple approach such as creating directories in the '/home/' directory with the same name as the user profile does not adequately resolve the issues that might arise when the home directories do not exist. This document provides a tool that makes use of the IBM i toolbox for Java to create the directories defined in the user profiles. The source code is listed at the end of this document. The source file and a compiled class are available on the public FTP server at the following URL:https://public.dhe.ibm.com/services/us/igsc/cs2/ApiSamples/CreateHomeDirectories.zip
| Caution: This program cannot be included in programs for resale. The source is provided as is. IBM offers a consulting agreement to assist with implementing this program, or you can contact your software service provider for assistance. |
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.ibm.as400.access.AS400;
import com.ibm.as400.access.AS400SecurityException;
import com.ibm.as400.access.CommandCall;
import com.ibm.as400.access.ErrorCompletingRequestException;
import com.ibm.as400.access.ObjectDoesNotExistException;
import com.ibm.as400.access.User;
/**
* The information contained in this document has not been submitted
* to any formal tests and is distributed on an 'As is' basis
* without any warranty either expressed or implied. The use of this
* information or the implementation of any of these techniques is a
* customer responsibility and depends on the customer's ability to
* evaluate and integrate them into the customer's operation
* environment. While each item may have been reviewed by IBM
* for accuracy in a specific situation, there is no guarantee that the
* same or similar results will be obtained elsewhere. Customers
* attempting to adapt these techniques to their environments do so
* at their own risk.
*/
public class CreateHomeDirectories {
/**
* This program creates home directories for all of the users on a system.
* It makes the user the owner of their own directory.
*
* @param args
*/
public static void main(String[] args) {
CreateHomeDirectories app = new CreateHomeDirectories();
try {
app.createUserProfileFile();
app.createHomeDirectories();
app.deleteUserProfileFile();
} catch (AS400SecurityException e) {
System.out.println("The command failed with a security exception.");
e.printStackTrace();
} catch (ErrorCompletingRequestException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void deleteUserProfileFile() throws AS400SecurityException,
ErrorCompletingRequestException,
IOException, InterruptedException,
PropertyVetoException, Exception
{
AS400 as400 = new AS400();
CommandCall command = new CommandCall(as400);
if (command.run("DLTF FILE(QGPL/ALLUSERS)"))
System.out.println("Removed QGPL/ALLUSERS file");
else {
System.out.println("Deletion of the QGPL/ALLUSERS file failed.");
throw new Exception();
}
as400.disconnectAllServices();
}
private void createHomeDirectories() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
User user = null;
String homeDir = null;
String commandString = null;
AS400 as400 = new AS400();
CommandCall command = new CommandCall(as400);
try {
Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
conn = DriverManager.getConnection("jdbc:db2://*LOCAL",null,null);
stmt = conn.createStatement();
rs = stmt.executeQuery("Select UPUPRF from QGPL.ALLUSERS");
while (rs.next()){
user = new User(as400,rs.getString(1));
homeDir = user.getHomeDirectory();
if ((new File(homeDir)).mkdirs()){
commandString = "CHGOWN OBJ('" + homeDir + "') NEWOWN(" + user.getUserProfileName() + ")";
command.run(commandString);
System.out.println("Successfully created home directory for user " + user.getUserProfileName() + "\t has home directory: " + homeDir);
}
else
System.out.println("The home directory for user " + user.getUserProfileName() + "\t already exists, it is: " + homeDir);
}
rs.close();
stmt.close();
conn.close();
as400.disconnectAllServices();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (AS400SecurityException e) {
e.printStackTrace();
} catch (ErrorCompletingRequestException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ObjectDoesNotExistException e) {
e.printStackTrace();
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
private void createUserProfileFile() throws AS400SecurityException, ErrorCompletingRequestException,
IOException, InterruptedException, PropertyVetoException, Exception
{
AS400 as400 = new AS400();
CommandCall command = new CommandCall(as400);
if (command.run("DSPUSRPRF USRPRF(*ALL) TYPE(*BASIC) OUTPUT(*OUTFILE) OUTFILE(QGPL/ALLUSERS) OUTMBR(*FIRST *REPLACE)"))
System.out.println("Created QGPL/ALLUSERS file");
else {
System.out.println("Creation of the QGPL/ALLUSERS file failed.");
throw new Exception();
}
as400.disconnectAllServices();
}
}
[{"Type":"MASTER","Line of Business":{"code":"LOB57","label":"Power"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Platform":[{"code":"PF012","label":"IBM i"}],"Version":"7.1.0"}]
Historical Number
546174739
Was this topic helpful?
Document Information
Modified date:
24 January 2024
UID
nas8N1012583