how to find a users *true* home directory with a shell script?

how to find a users *true* home directory with a shell script?

Post by Alan Russel » Thu, 17 May 2001 00:47:52



In Aix it's

 cat /etc/passwd | grep $UserID  | cut -d":" -f6


Quote:> Hello, I am trying to write a ksh script to find a users *true* home
> directory with a shell script (i.e. no perl or C on these servers).  I
> found a post on google of a shell script/C program combo called rpath
> but google didn't have the entire script (it was pretty long) and
> besides I'd prefer a solution that uses just unix utilities and the
> ksh features (93 or 88).

>     Example:  adam's home directory per the passwd file is /home/adam
> but /home is actually a link to /fs0/home so adam's true home is
> /fs0/home/adam. How to find this out so I can put it into a script?
> Any unix utils?  Currently I am using "df /home/adam" to find the true
> filesystem it is on but it is the directories under that (home in this
> case) that I would have a problem "finding" since df will only report
> the parent filesystem.  And then what if /fs0/home links somewhere
> else?   So is there some way to stat or view up the true directory
> tree from the known destination ("adam" home in the fs0 filesystem)?

>  Why?  The app I am rolling out requires that group/other not have the
> write bit on any of the directories of the users home directory
> (security reasons) so I need to run a "chmod g-w xxx" where xxx is
> each directory in turn (i.e. chmod g-w /home   then chmod g-w
> /fs0/home and finally chmod g-w /fs0/home/adam).  I can't do a
> recursive chmod since other stuff may be in each of those directories.
> If you have some other solution than my attempt to find the "one true
> home directory" then please suggest.  Assumption about where home
> should or should not be cannot be made as I will be rolling this out
> to 20 different servers and so far they have a mix of links, true
> homes, etc.

> thanks for any ideas

> adam

 
 
 

how to find a users *true* home directory with a shell script?

Post by Barry Margoli » Thu, 17 May 2001 01:32:20




>In Aix it's

> cat /etc/passwd | grep $UserID  | cut -d":" -f6

What about this part of his question:



>>     Example:  adam's home directory per the passwd file is /home/adam
>> but /home is actually a link to /fs0/home so adam's true home is

       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:>> /fs0/home/adam. How to find this out so I can put it into a script?

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

how to find a users *true* home directory with a shell script?

Post by t.. » Thu, 17 May 2001 01:56:54



> I am trying to write a ksh script to find a users *true* home
> directory with a shell script (i.e. no perl or C on these servers).
> I'd prefer a solution that uses just unix utilities and the
> ksh features (93 or 88).

>     Example:  adam's home directory per the passwd file is /home/adam
> but /home is actually a link to /fs0/home so adam's true home is
> /fs0/home/adam. How to find this out so I can put it into a script?
>  Why?  The app I am rolling out requires that group/other not have the
> write bit on any of the directories of the users home directory
> (security reasons) so I need to run a "chmod g-w xxx" where xxx is
> each directory in turn (i.e. chmod g-w /home   then chmod g-w
> /fs0/home and finally chmod g-w /fs0/home/adam).

With ksh you can use 'cd -P' to climb the physical directory tree;
e.g., "cd -P ." will change the current directory to "true" or
physical path, without symlinks. So you could do it like this:

eval "cd ~$user"
while [[ $PWD != / ]] ;do
  chmod og-w .
  cd -P ..
done

The first chmod is done on the home directory, possibly symlinked
but that's fine as chmod will change the real directory anyway.
Thereafter it walks the physical path towards / and changes
each directory in turn.

Note that you need to do that for every step in the symbolic
form of the home directory path (otherwise symlink on a symlinked
directory may foil your efforts). Something like this might work:

eval "cd ~$user"
while [[ $PWD != / ]] ;do
  (
    while [[ $PWD != / ]] ;do
      chmod og-w .
      cd -P ..
    done
  )
  cd -L ..
done

It will probably chmod some directories several times,
depending on how many symbolic links there are along the path,
but that shouldn't hurt.

--
Tapani Tarvainen

 
 
 

how to find a users *true* home directory with a shell script?

Post by Alan Russel » Thu, 17 May 2001 02:00:47


Try:
cd ~$UserID
df -k . | awk {'print $7'} | tail -1




> >In Aix it's

> > cat /etc/passwd | grep $UserID  | cut -d":" -f6

> What about this part of his question:



> >>     Example:  adam's home directory per the passwd file is /home/adam
> >> but /home is actually a link to /fs0/home so adam's true home is
>        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >> /fs0/home/adam. How to find this out so I can put it into a script?

> --

> Genuity, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the

group.
 
 
 

how to find a users *true* home directory with a shell script?

Post by Barry Margoli » Thu, 17 May 2001 02:14:28




>Try:
>cd ~$UserID
>df -k . | awk {'print $7'} | tail -1

That will print /fs0/home, not /fs0/home/adam.

I think you need to reread the original posting, since he mentioned using
df and asked about this very problem.

Here's my suggestion:

UsersHomedir=`cd ~$UserID;pwd`

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

how to find a users *true* home directory with a shell script?

Post by Benjamin.Altma » Thu, 17 May 2001 02:49:18


That's not portable, on HPUX the last line of df -k doesn't have a 7th field
and the 7th field of the first line contains the word "allocated".

> Try:
> cd ~$UserID
> df -k . | awk {'print $7'} | tail -1





> > >In Aix it's

> > > cat /etc/passwd | grep $UserID  | cut -d":" -f6

> > What about this part of his question:



> > >>     Example:  adam's home directory per the passwd file is /home/adam
> > >> but /home is actually a link to /fs0/home so adam's true home is
> >        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > >> /fs0/home/adam. How to find this out so I can put it into a script?

> > --

> > Genuity, Burlington, MA
> > *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
> newsgroups.
> > Please DON'T copy followups to me -- I'll assume it wasn't posted to the
> group.

 
 
 

how to find a users *true* home directory with a shell script?

Post by Alan Russel » Thu, 17 May 2001 03:28:49


On HPUX replace df -k with bdf -k .

> That's not portable, on HPUX the last line of df -k doesn't have a 7th
field
> and the 7th field of the first line contains the word "allocated".


> > Try:
> > cd ~$UserID
> > df -k . | awk {'print $7'} | tail -1





> > > >In Aix it's

> > > > cat /etc/passwd | grep $UserID  | cut -d":" -f6

> > > What about this part of his question:



> > > >>     Example:  adam's home directory per the passwd file is
/home/adam
> > > >> but /home is actually a link to /fs0/home so adam's true home is
> > >        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > > >> /fs0/home/adam. How to find this out so I can put it into a script?

> > > --

> > > Genuity, Burlington, MA
> > > *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
> > newsgroups.
> > > Please DON'T copy followups to me -- I'll assume it wasn't posted to
the
> > group.

 
 
 

how to find a users *true* home directory with a shell script?

Post by Benjamin.Altma » Thu, 17 May 2001 04:32:46


$ bdf -k
bdf: illegal option -- k
usage: bdf [ -b ] [ -i ] [ -l ] [-t type | file... ]

> On HPUX replace df -k with bdf -k .


> > That's not portable, on HPUX the last line of df -k doesn't have a 7th
> field
> > and the 7th field of the first line contains the word "allocated".


> > > Try:
> > > cd ~$UserID
> > > df -k . | awk {'print $7'} | tail -1





> > > > >In Aix it's

> > > > > cat /etc/passwd | grep $UserID  | cut -d":" -f6

> > > > What about this part of his question:



> > > > >>     Example:  adam's home directory per the passwd file is
> /home/adam
> > > > >> but /home is actually a link to /fs0/home so adam's true home is
> > > >        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > > > >> /fs0/home/adam. How to find this out so I can put it into a script?

> > > > --

> > > > Genuity, Burlington, MA
> > > > *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
> > > newsgroups.
> > > > Please DON'T copy followups to me -- I'll assume it wasn't posted to
> the
> > > group.

 
 
 

how to find a users *true* home directory with a shell script?

Post by brian hile » Thu, 17 May 2001 06:27:36



> Hello, I am trying to write a ksh script to find a users *true* home
> directory with a shell script (i.e. no perl or C on these servers).  I
> ...

Here is my ksh[88] function to do what you need. Use:

resolvepath -l $HOME # <= actually the values representing the users home dirs

=Brian

#! /bin/echo error: only source
#*TAG:42576 4:Jan 9 1973:0755:resolvepath:

# Copyright: (c) 2001
# Description: resolve and canonicize full pathname of arguments
# Name: resolvepath
# Requires:

# See-also: File-PathConvert-0.4.tar (perl)
# Usage: resolvepath [-hlp] path...
# Version: 1.08

#01
function resolvepath # [-hlp] path...
{       set -o noglob
        # Ksh Bug: OPTIND cannot be declared integer!
        typeset -i rc
        typeset IFS OPTARG arg dir fn headers= symlink= oarg opt usepath=
        while getopts :HhLlPp opt
        do      case $opt in
                (h)     headers=ON ;;
                (+h|H)  headers=OFF ;;
                (l)     symlink=ON ;;
                (+l|L)  symlink= ;;
                (p)     usepath=ON ;;
                (+p|P)  usepath= ;;
                ([:?])  print -ru2 "usage: $0 [-hlp] path...
-h      - prepend the output with argument header
-l      - show logical path with symlinks resolved [physical path]
-p      - apply path lookup, if applicable"
                        return 2 ;;
                esac
        done
        shift OPTIND-1
        if [[ $headers = OFF ]]
        then    headers=
        else    (($#>1)) && headers=ON
        fi
        for arg
        do      oarg=$arg
                if [[ $usepath = ON && ! -d $arg ]]
                then    # Ksh Bug: "whence" cannot handle args with spaces
                        arg=$(whence -p "$arg") ||
                        {       print -ru2 'resolvepath: whence: error:' \
                                "\"$oarg\" not found"
                                rc=rc+1 continue
                        }
                fi
                [[ -a $arg ]] ||
                {       print -ru2 "resolvepath: error: \"$arg\" not found"
                        rc=rc+1 continue
                }
                [[ $arg != */* ]] && arg="./$arg"
                if [[ -d $arg ]]
                then    dir=$arg fn=
                else    dir=${arg%/*} fn=${arg##*/}
                fi
${DIAG:+print -ru2 "[ $0: dirpart=\"$dir\", filepart=\"$fn\" ]"}
                # Ksh Bug: "cd -P dir" works, but "cd -P -- dir" does not!
                [[ $dir = -* ]] && dir="./$dir"       # work-around for above bug
                \cd ${symlink:+-P} "$dir" || rc=rc+1 continue
                print -r -- "${headers:+$oarg:     }${PWD%/}/$fn"     # <= TAB
                \cd - >&-
        done
        return $rc

Quote:}

#02 EMBEDDED MAN-PAGE FOR "src2man"
: '
#++
NAME
        resolvepath - resolve and canonicize the full pathname of argument

SYNOPSIS
        resolvepath [-hlp] path...

OPTIONS
        -h      - Prepend the output with argument header.
        -l      - Show logical path with symlinks resolved. [physical path]
        -p      - Apply path lookup, if applicable.

DESCRIPTION
        ...

        If and only if the path is a directory, the output is guaranteed
        to be terminated with a slash ("/").

RETURN CODE
        Returns 0 if successful, 2 for options parsing errors, otherwise
        the number of arguments in error.

EXAMPLE
        $ resolvepath .
        /home/brian/side/lib/

        $ resolvepath cat
        resolvepath: error: "cat" not found
        $ resolvepath -p cat
        /bin/cat
        $ resolvepath -lp cat
        /usr/bin/cat

        $ resolvepath /
        /
        $ resolvepath //
        /
        $ resolvepath /..
        /
        $ resolvepath /.//..///.////../////.//////..///////.////////..
        /

ENVIRONMENT
        PATH

SEE ALSO
        predictshell(3S), stat(3S)

AUTHOR

CAVEATS
        The algorithm that is used requires the directory component of
        the resolved argument to be executable; i.e. you must have
        permission to chdir to it.

BUGS
        Filenames with embedded spaces will be failed to be recognized;
        this is a bug in the ksh builtin "whence", not resolvepath(3S).

#--
'

 
 
 

how to find a users *true* home directory with a shell script?

Post by Michael Heimin » Thu, 17 May 2001 07:26:35



> Hello, I am trying to write a ksh script to find a users *true* home
> directory with a shell script (i.e. no perl or C on these servers).  I
> found a post on google of a shell script/C program combo called rpath
> but google didn't have the entire script (it was pretty long) and
> besides I'd prefer a solution that uses just unix utilities and the
> ksh features (93 or 88).

>     Example:  adam's home directory per the passwd file is /home/adam
> but /home is actually a link to /fs0/home so adam's true home is
> /fs0/home/adam. How to find this out so I can put it into a script?
> Any unix utils?  Currently I am using "df /home/adam" to find the true
> filesystem it is on but it is the directories under that (home in this
> case) that I would have a problem "finding" since df will only report
> the parent filesystem.  And then what if /fs0/home links somewhere
> else?   So is there some way to stat or view up the true directory
> tree from the known destination ("adam" home in the fs0 filesystem)?

>  Why?  The app I am rolling out requires that group/other not have the
> write bit on any of the directories of the users home directory
> (security reasons) so I need to run a "chmod g-w xxx" where xxx is
> each directory in turn (i.e. chmod g-w /home   then chmod g-w
> /fs0/home and finally chmod g-w /fs0/home/adam).  I can't do a
> recursive chmod since other stuff may be in each of those directories.
> If you have some other solution than my attempt to find the "one true
> home directory" then please suggest.  Assumption about where home
> should or should not be cannot be made as I will be rolling this out
> to 20 different servers and so far they have a mix of links, true
> homes, etc.

This line could get you started, it does link checking and uses gawk,
but it should work with other awk.

awk -F: '{system ("if [ -L "$6" ] ; then echo ""user: "$1";fi")}'
/etc/passwd

Michael Heiming

 
 
 

how to find a users *true* home directory with a shell script?

Post by Keith Thompso » Thu, 17 May 2001 09:20:58



> In Aix it's

>  cat /etc/passwd | grep $UserID  | cut -d":" -f6

1. This is a classic "useless use of cat".  Replace
      cat /etc/passwd | grep $UserID
   with
      grep $UserID /etc/passwd

2. If the AIX system uses NIS, the user's name may not be in the
   /etc/passwd.  (I'm assuming AIX supports NIS; I've seen it on both
   Solaris and Linux).

3. If $UserID is "foo", this will find entries for users "foo",
   "fool", "xfoo", etc.  It will also find all entries that contain
   the string "foo" *anywhere*; consider a user "ash" on a system
   where some users use bash as their default shell.
   Replace
      grep $UserID
   with
      grep "^$UserID:"
   For csh and derivatives, use
      grep "^${UserID}:"

4. In any case, it doesn't answer the poster's original question,
   which required resolving any symlinks.

I believe that this will work (someone else already posted it):

cd ~$UserID ; pwd

(assuming sufficient privileges.)

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.

 
 
 

how to find a users *true* home directory with a shell script?

Post by Cyrille Lefevr » Thu, 17 May 2001 10:43:43


[snip]

Quote:> I believe that this will work (someone else already posted it):

> cd ~$UserID ; pwd

/bin/pwd would be better to retreive the physical path and not the
logical path.

Cyrille.
--


Supprimer "%no-spam" et ".invalid" pour me repondre. | about who it chooses
Remove "%no-spam" and ".invalid" to answer me back.  | to be friends with.

 
 
 

how to find a users *true* home directory with a shell script?

Post by Julian T. J. Midgl » Thu, 17 May 2001 18:22:43






>>Try:
>>cd ~$UserID
>>df -k . | awk {'print $7'} | tail -1

>That will print /fs0/home, not /fs0/home/adam.

>I think you need to reread the original posting, since he mentioned using
>df and asked about this very problem.

>Here's my suggestion:

>UsersHomedir=`cd ~$UserID;pwd`

Unfortunately, depending on your implementation of pwd, this may
return the symlinked path.  (It certainly does for me on Linux, HP-UX,
AIX, IRIX, and Solaris).  It seems unlikely that it will actually work
on any UNIX...

Julian

--
Julian T. J. Midgley                    http://www.xenoclast.org
Cambridge, England.                       PGP Key ID: 0xBCC7863F

 
 
 

how to find a users *true* home directory with a shell script?

Post by Julian T. J. Midgl » Thu, 17 May 2001 18:24:52








>>>Try:
>>>cd ~$UserID
>>>df -k . | awk {'print $7'} | tail -1

>>That will print /fs0/home, not /fs0/home/adam.

>>I think you need to reread the original posting, since he mentioned using
>>df and asked about this very problem.

>>Here's my suggestion:

>>UsersHomedir=`cd ~$UserID;pwd`

>Unfortunately, depending on your implementation of pwd, this may
>return the symlinked path.  (It certainly does for me on Linux, HP-UX,
>AIX, IRIX, and Solaris).  It seems unlikely that it will actually work
>on any UNIX...

Investigating further - this works fine if the pwd concerned is
/bin/pwd and not /sbin/pwd.

Julian

--
Julian T. J. Midgley                    http://www.xenoclast.org
Cambridge, England.                       PGP Key ID: 0xBCC7863F

 
 
 

how to find a users *true* home directory with a shell script?

Post by Keith Thompso » Fri, 18 May 2001 11:16:26




> [snip]
> > I believe that this will work (someone else already posted it):

> > cd ~$UserID ; pwd

> /bin/pwd would be better to retreive the physical path and not the
> logical path.

Quite right.  I hadn't realized that pwd is a builtin command in ksh.

It's also builtin in bash andsh.  Experiment shows that the sh version
works like /bin/pwd, i.e., it resolves symlinks; this may vary from
one system to another, and sh typically doesn't support ~user anyway.

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.