On Wednesday 17 December 2003 06:31, Stephen McConnell wrote:
> De Lima, Claude wrote:

> >org.apache.avalon.util.env.EnvAccessException: Failed to access null
> >environment variable - User cdelima does not seem to exist in /etc/passwd

> That's possible but it only really works in the command line scenarios -
> in things like the test cases and other enbedded scenarios we really
> need to resolve env variables independenly of system properties.  The
> best solution is to fix the problem at the Env layer.
>
> Alex .. any thoughts?

I am not Alex, but I have a thought...

Since the only reason why the password file is read, is to get the user's 
preferred shell, I think that it is a bit of an overkill, just to read an 
environment variable.
I don't think that any Unix/Linux would be operational at all, if it doesn't 
have the default shell "sh" installed, since most scripts makes such 
assumption, and Env.java could safely do the same.

So, 
    private static String getUnixUserShell()
        throws EnvAccessException
    {
        if ( null != s_shell )
        {
            return s_shell ;
        }
        
        File l_etcpasswd = new File( "/etc/passwd" ) ;
        if ( l_etcpasswd.exists() && l_etcpasswd.canRead() )
        {
            BufferedReader l_in = null ;
    
            try 
            {
                String l_entry = null ;
                l_in = new BufferedReader( new FileReader( l_etcpasswd ) ) ;
        
                while ( null != ( l_entry = l_in.readLine() ) )
                {
                    // Skip entries other than the one for this username
                    if ( ! l_entry.startsWith( USERNAME ) ) 
                    {
                        continue ;
                    }
        
                    // Get the shell part of the passwd entry
                    int l_index = l_entry.lastIndexOf( ':' ) ;

                    if ( l_index == -1 )
                    {
                        throw new EnvAccessException( 
                            "/etc/passwd contains malformed user entry for " 
                            + USERNAME ) ;
                    }
        
                    s_shell = l_entry.substring( l_index + 1 ) ;
                    return s_shell ;
                }
            } 
            catch ( IOException e )
            {
                throw new EnvAccessException( e ) ;
            }
    
            throw new EnvAccessException( "User " + USERNAME 
                + " does not seem to exist in /etc/passwd" ) ;
        }
    
        throw new EnvAccessException( "Don't know what to do with"
            + " a UNIX system without a readable /etc/passwd file" ) ;
    }

can safely be changed to;

    private static String getUnixUserShell()
    {
        return "/bin/sh";
    }

!!!!!!!!! and will always work.

Niclas


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to