Re: [PATCH v2 1/2] common/rc: add _require_user_exists() to check if a user exists

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]



On Sun, Sep 26, 2021 at 06:13:54PM +0800, Zorro Lang wrote:
> On Fri, Sep 24, 2021 at 10:19:10AM +0100, Luis Henriques wrote:
> > Function _require_user() does check if a user exists *and* if it is able
> > to execute commands.  Add a new function to simply check if a user exists.
> > 
> > Signed-off-by: Luis Henriques <lhenriques@xxxxxxx>
> > ---
> >  common/rc | 27 ++++++++++++++++++---------
> >  1 file changed, 18 insertions(+), 9 deletions(-)
> > 
> > diff --git a/common/rc b/common/rc
> > index 154bc2dd7e94..de9ba56eefcf 100644
> > --- a/common/rc
> > +++ b/common/rc
> > @@ -2289,18 +2289,27 @@ _cat_group()
> >  	cat /etc/group
> >  }
> >  
> > -# check for a user on the machine, fsgqa as default
> > +# check if a user exists in the system
> > +#
> > +_require_user_exists()
> > +{
> > +	local user=$1
> > +	_cat_passwd | grep -q $user
> > +	[ "$?" == "0" ] || _notrun "$user user not defined."
> 
> As _require_user() does "su $qa_user" after the "grep", so it really make sure
> there's an "user". But if the _require_user_exists() only trys to grep /etc/passed
> to make sure there's an "$user", I'd like to make the "grep" condition be more exact.
> For example, if there's an user "myuser100" in the /etc/passwd, then _require_user_exists
> "myuser" or "user100" or "user1" or "user10" all return 0.
> 
> So how about:
>   _cat_passwd | grep -qw $user
> 
> Or more exact:
>   _cat_passwd | cut -d: -f1 | grep -qw $user
> 
> Or other better command line:)

Oh, the "-w" doesn't work with an user with "_", likes myuser_123. So
how about:

  _cat_passwd | grep -q ^$user:

Thanks,
Zorro

> 
> Thanks,
> Zorro
> 
> > +}
> > +
> > +# check if a user exists and is able to execute commands.
> > +# Uses 'fsgqa' user as default.
> >  #
> >  _require_user()
> >  {
> > -    qa_user=fsgqa
> > -    if [ -n "$1" ];then
> > -        qa_user=$1
> > -    fi
> > -    _cat_passwd | grep -q $qa_user
> > -    [ "$?" == "0" ] || _notrun "$qa_user user not defined."
> > -    echo /bin/true | su $qa_user
> > -    [ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
> > +	qa_user=fsgqa
> > +	if [ -n "$1" ];then
> > +		qa_user=$1
> > +	fi
> > +	_require_user_exists $qa_user
> > +	echo /bin/true | su $qa_user
> > +	[ "$?" == "0" ] || _notrun "$qa_user cannot execute commands."
> >  }
> >  
> >  # check for a chown support
> > 




[Index of Archives]     [Linux Filesystems Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux