From nobody@FreeBSD.org  Wed Aug 25 19:59:43 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 3B4BB106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 25 Aug 2010 19:59:43 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 2AD8E8FC17
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 25 Aug 2010 19:59:43 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o7PJxgwX036258
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 25 Aug 2010 19:59:42 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o7PJxgCq036257;
	Wed, 25 Aug 2010 19:59:42 GMT
	(envelope-from nobody)
Message-Id: <201008251959.o7PJxgCq036257@www.freebsd.org>
Date: Wed, 25 Aug 2010 19:59:42 GMT
From: Jan Schaumann <jschauma@netmeister.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: usermod -u <non-numeric> should error
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         149972
>Category:       bin
>Synopsis:       [patch] pw(8): usermod -u <non-numeric> should error
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Aug 25 20:00:16 UTC 2010
>Closed-Date:    
>Last-Modified:  Sat Oct 22 23:20:08 UTC 2011
>Originator:     Jan Schaumann
>Release:        
>Organization:
>Environment:
>Description:
pw usermod -d /home/bob -u bob -m

yields

drwxr-xr-x  2 root  wheel  512 Aug 25 12:43 /home/bob

Ie, the home directory is owned by root.

This makes (some) sense once you realize that "-u" is supposed to take
a number:

-u uid        Specify the user/account numeric id.

So I'm guessing that in this case pw(8) determined that there is no UID
"bob", and hence created the directory as being owned by root.

Now to make the whole story short, I think that if the argument to "-u"
is supposed to be numeric, then pw(8) should error out if it is not
actually numeric.
>How-To-Repeat:
pw usermod -d /home/bob -u bob -m
>Fix:
some sort of atoi checking in src/usr.sbin/pw/pw_user.c

>Release-Note:
>Audit-Trail:

From: Ilya Bakulin <webmaster@kibab.com>
To: bug-followup@FreeBSD.org, jschauma@netmeister.org
Cc: gavin@FreeBSD.org
Subject: Re: bin/149972: pw(8): usermod -u <non-numeric> should error
Date: Sun, 10 Oct 2010 00:36:27 +0200

 --MP_/DYFS_+in8U_jcKIbTLh57db
 Content-Type: text/plain; charset=US-ASCII
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline
 
 Fixed. Now pw(1) will exit with error if supplied with non-numeric ID.
 Generated at:	EuroBSDCon-2010 hackers lounge by gpf@, kibab@, bcr@
 
 --MP_/DYFS_+in8U_jcKIbTLh57db
 Content-Type: text/x-patch
 Content-Transfer-Encoding: 7bit
 Content-Disposition: attachment; filename=pw_current.diff
 
 Index: pw_user.c
 ===================================================================
 --- pw_user.c	(revision 213663)
 +++ pw_user.c	(working copy)
 @@ -468,13 +468,22 @@
  			edited = 1;
  		}
  
 -		if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
 +		if ((arg = getarg(args, 'u')) != NULL) {
 +		    if(isdigit((unsigned char)*arg->val)) {
  			pwd->pw_uid = (uid_t) atol(arg->val);
  			edited = 1;
  			if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
  				errx(EX_DATAERR, "can't change uid of `root' account");
  			if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
  				warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
 +		    } else {
 +			/* Found something, but not a number */
 +			/*
 +			 * XXX Shouldn't we try to map the passed string to the username?
 +			 * man page however says that we're expecting numeric uid...
 +			 */
 +			 errx(EX_DATAERR, "Expected numeric user id as an argument to -u\n");
 +		    }
  		}
  
  		if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) {	/* Already checked this */
 
 --MP_/DYFS_+in8U_jcKIbTLh57db--

From: "Christopher J. Ruwe" <cjr@cruwe.de>
To: bug-followup@FreeBSD.org
Cc: jschauma@netmeister.org
Subject: Re: bin/149972: pw(8): usermod -u <non-numeric> should error
Date: Sat, 22 Oct 2011 18:22:46 +0200

 --MP_/kJVp8xL1s7v2IUd=yXg8OoU
 Content-Type: text/plain; charset=US-ASCII
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline
 
 The included diff implements the suggestion from the audit-trail. More
 specifically, when pw is invoced as in "pw usermod testuser2 -u
 testuser1", the routine changes testuser2's uid to testuser2's.
 
 This behaviour is in dissaccord with the man-page, which expects -u
 <uid>, it is, however, more convenient to the admin wishing to declare
 aliases.
 
 It is necessary to reach a decision as to if to implement the first fix (returning error) or the second (deriving uid from uname). The PR should be closed in the one or the other, but timely, fashion, though.
 
 Cheers,
 -- 
 Christopher J. Ruwe
 TZ GMT + 2
 
 
 --MP_/kJVp8xL1s7v2IUd=yXg8OoU
 Content-Type: application/octet-stream; name=pw_user.c-diff
 Content-Transfer-Encoding: base64
 Content-Disposition: attachment; filename=pw_user.c-diff
 
 NDc2LDQ4MmM0NzYsNTE5CjwgCQlpZiAoKGFyZyA9IGdldGFyZyhhcmdzLCAndScpKSAhPSBOVUxM
 ICYmIGlzZGlnaXQoKHVuc2lnbmVkIGNoYXIpKmFyZy0+dmFsKSkgewo8IAkJCXB3ZC0+cHdfdWlk
 ID0gKHVpZF90KSBhdG9sKGFyZy0+dmFsKTsKPCAJCQllZGl0ZWQgPSAxOwo8IAkJCWlmIChwd2Qt
 PnB3X3VpZCAhPSAwICYmIHN0cmNtcChwd2QtPnB3X25hbWUsICJyb290Iik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290IikgPT0gMCkKPiAJCSAg
 ICAgIHdhcm54KCJXQVJOSU5HOiBhY2NvdW50IGAlcycgd2lsbCBoYXZlIGEgdWlkIG9mIDAgKHN1
 cGVydXNlciBhY2Nlc3MhKSIsIHB3ZC0+cHdfbmFtZSk7Cj4gCj4gCQkgICAgaWYoIShwd2QgPSBH
 RVRQV05BTShhcmctPnZhbCkpKSAvKiAtdSA8dW5hbWU+Ki8KPiAJCSAgICAgIGVycngoRVhfREFU
 QUVSUiwgIlVzZXIgJXMgZG9lcyBub3QgZXhpc3QiLCBhcmctPnZhbCk7Cj4gCQkgICAgaW50IGFs
 aWFzX3VpZCA9IHB3ZC0+cHdfdWlkOyAKPiAKPiAJCSAgICBpZighKHB3ZCA9IEdFVFBXTkFNKGFf
 bmFtZS0+dmFsKSkpIC8qdXNlcm1vZCA8dW5hbWU+Ki8KPiAJCSAgICAgIGVycngoRVhfREFUQUVS
 UiwgIlVzZXIgJXMgZG9lcyBub3QgZXhpc3QiLCBhX25hbWUtPnZhbCk7Cj4gCj4gCQkgICAgcHdk
 LT5wd191aWQgPSAodWlkX3QpIGFsaWFzX3VpZDsKPiAJCSAgICB3YXJueCgiVXNlciAlcydzIHVp
 ZCBjaGFuZ2VkIHRvICVkIiwgcHdkLT5wd19uYW1lLCBwd2QtPnB3X3VpZCk7Cj4gCQkgICAgZWRp
 dGVkID0gMTsKPiAJCSAgfQo0ODNhNTIxCj4gCQkK
 
 --MP_/kJVp8xL1s7v2IUd=yXg8OoU--

From: "Christopher J. Ruwe" <cjr@cruwe.de>
To: bug-followup@FreeBSD.org <bug-followup@FreeBSD.org>
Cc: jschauma@netmeister.org <jschauma@netmeister.org>
Subject: Re: bin/149972: pw(8): usermod -u <non-numeric> should error
Date: Sun, 23 Oct 2011 01:15:32 +0200

 Sorry, above "fix" breaks when invoced with anything more than "pw
 usermpd user -u otheruser" and should not be considered a fix, it
 introduces more problems instead.
 
 -- 
 Christopher J. Ruwe
 TZ GMT + 2
>Unformatted:
