"mkpasswd -c" from a cmd shell outputs wrong gid (cygwin-1.7)

Herb Maeder maeder-cygml@maeder.org
Sat Oct 11 14:29:00 GMT 2008

The "mkpasswd -c" command produces the wrong gid for the current user
under the following set of circumstances:

   * it is invoked from a cmd.exe shell
   * there are no running cygwin bash shells
   * output is redirected to cygwin's /etc/passwd file (with the permissions
     that setup.exe sets on it)

For example, the following should show the problem (from cmd.exe shell,
directly after completing an initial cygwin install with setup-1.7.exe):

   cd c:\cygwin-1.7\bin
   .\mkpasswd -l -c > ..\etc\passwd

For my case I see that it outputs "10544" as the gid, instead of the
expected "10513" (Domain Users).

The problem occurs regardless of the "-l" option (but using it in the test 
case will leave the passwd file a little more sane).

At first I was completely stumped as to how the file that output was
redirected to could possibly affect the output of the program itself.  
But after digging deeper, I think it boils down to this sequence

  OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &ptok)
  GetTokenInformation (ptok, TokenPrimaryGroup, &curr_pgrp,
                       sizeof curr_pgrp, &len)
  /* . . . */
  uid = *GetSidSubAuthority (curr_user.psid,
			     *GetSidSubAuthorityCount(curr_user.psid) - 1);
  gid = *GetSidSubAuthority (curr_pgrp.psid,
			     *GetSidSubAuthorityCount(curr_pgrp.psid) - 1);

The uid and gid are queried from the current process.  And as near as I
can figure, Administrator group priveleges (gid 544) are needed to write
to /etc/passwd.  So the gid gets set to 544, but it that is not really the
primary group id of the current user.  The net effect is that a
/etc/passwd file may be created with a primary gid is not in /etc/group.

I wonder if it might make more sense to get just the uid from the current
process token, but then lookup the primary group from the SID
corresponding to the uid.

Though it should be noted that only redirection in windows world will show
this problem.  Redirection within a bash shell it does not seem to have an
effect.  In fact, the problem goes away even from a cmd shell if there is
any bash shell open on the machine (that I can't explain).

In any case, a workaround to avoid this problem is to fire up a bash
command and redirect to /etc/passwd within bash (as opposed to redirecting
in the cmd shell world):

   cd c:\cygwin-1.7\bin
   .\bash -c "./mkpasswd -l -c > /etc/passwd"

Alternatively, redirecting to a non existant file, then moving it to
/etc/passwd should also work.


Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

More information about the Cygwin mailing list