Subject: Re: cygwin perl useradd command
From: Jaspreet Singh <[email-masked]>
To: Hugh Sasse <[email-masked]>
Cc: [email-masked]
Date: Sat, 10 May 2008 20:00:01 +0530 (IST)
List-Id: <cygwin.cygwin.com>

alright now i completed. this may be not optimized code but whatever this is what i came up with

========================================================================

#!/usr/bin/perl

$userprefix  = "cygusr_";
$groupprefix = "cyggrp_";

# Function Output Help/Usage
sub printhelp
{
		print "Perl useradd command for cygwin.\n";
		print "\n";
		print "usage :\n";
		print " -u	:	Username\n";
		print " -p	:	Password\n";
		print " -g	:	Initial Group\n";
		print " -c	:	Comments\n";
		print " -d	:	Home Directory\n";
		print " -s	:	Shell\n";
		print " -m	:	Copy /etc/skel/* to <home_directory>\n";
		print " -h	:	Show usage\n";

};

   # Resolve passed arguments
   $usr        = "";   # Username
   $pass       = "";   # Password
   $pUID       = "";   # UID
   $pGID       = "";   # GID (Initial Group)
   $comm       = "";   # Comments
   $home       = "";   # Home Directory
   $shell      = "";   # User Shell
   $createhome = "";
   while ($ARGV[$i])
       	{
		if ($ARGV[$i] eq "-h")
                    {
                       die &printhelp;
                    }
                elsif ($ARGV[$i] eq "-u")
                    {
                       $usr = $ARGV[$i + 1];
                    }
                elsif ($ARGV[$i] eq "-p")
                    {
                       $pass = $ARGV[$i + 1];
                    }
                elsif ($ARGV[$i] eq "-g")
                    {
                       $pGID = $ARGV[$i + 1];
                    }
                elsif ($ARGV[$i] eq "-c")
                    {
                       $comm = $ARGV[$i + 1];
                    }
                elsif ($ARGV[$i] eq "-d")
                    {
                       $home = $ARGV[$i + 1];
                    }
                elsif ($ARGV[$i] eq "-s")
                    {
                       $shell = $ARGV[$i + 1];
                    }
                elsif ($ARGV[$i] eq "-m")
                    {
                       $createhome = $ARGV[$i];
                    }
		$i++;
	}

   if ($usr eq "")
      {
         die &printhelp;
      }

   if ($pGID eq "")
      {
         $pGID = "default";
      }
   else
      {
       # If GID is specified in the arguments then we find out the
       # GID Numeric

       # for cygwin verification we open : /etc/group

       open(GROUP, "</etc/group") || die "Can't open group for reading.\n";
       while (<GROUP>)
             {
               ($gname, $sid, $ggid, $list) = split(/:/);
                if (($ggid eq $pGID) || ($gname eq $pGID))
	           {
                     $temp = $ggid;
	           }
	     }
       close(GROUP);
       if ($temp ne "")
          {
           $pGID = $ggid;
          }
       else
          {
           $pGID = "default";
          }
      }

   if ($comm eq "")
      {
         $comm = "CYGWIN User";
      }

   if ($home eq "")
      {
         $home = "/home/".$usr;
      }

   if ($shell eq "")
      {
         $shell = "/bin/bash";
      }

   # Temp Userstring
   $userstring = $usr.":".$pass.":".$pUID.":".$pGID.":".$comm.":".$home.":".$shell;
   # Username for windows
   $finalusr = $userprefix.$usr;

   #-------------------------------------------------------------------------#
   # There shouldn't be any user with $usr and $finalusr on widows or cygwin #
   #-------------------------------------------------------------------------#

   # Now we have to verify that there isn't any user with $usr username
   # on windows and in /etc/passwd file of cygwin for real user verificatoion.

   # for windows verification we use : /usr/bin/mkpasswd -l -u $usr

   $verifyusr = `/usr/bin/mkpasswd -l -u $usr 2>&1`;
   chop ($verifyusr);
   if ($verifyusr ne "mkpasswd (272): [2221] The user name could not be found.\r")
      {
       die "User with same user name already exists. see windows accounts.\n";
      }

   # for cygwin verification we open : /etc/passwd

   open(PASSWD, "</etc/passwd") || die "Can't open passwd for reading.\n";
   while (<PASSWD>)
      {
        ($name,$passwd,$uid,$gid, $gcos,$dir,$zshell) = split(/:/);
        if ($name eq $usr)
           {
            close(PASSWD);
       	    die "User with same user name already exists. see /etc/passwd..\n";
           }
      }
   close(PASSWD);

   # Now we have to verify that there isn't any user with $finalusr username
   # on windows and in /etc/passwd file of cygwin for real user verificatoion.

   # for windows verification we use : /usr/bin/mkpasswd -l -u $finalusr

   $verifyusr = `/usr/bin/mkpasswd -l -u $finalusr 2>&1`;
   chop ($verifyusr);
   if ($verifyusr ne "mkpasswd (272): [2221] The user name could not be found.\r")
      {
       die "User with same user name and prefix already exists. see windows accounts.\n";
      }

   # for cygwin verification we open : /etc/passwd

   open(PASSWD, "</etc/passwd") || die "Can't open passwd for reading.\n";
   while (<PASSWD>)
      {
        ($name,$passwd,$uid,$gid, $gcos,$dir,$zshell) = split(/:/);
        if ($name eq $finalusr)
           {
            close(PASSWD);
       	    die "User with same user name and prefix already exists. see /etc/passwd.\n";
           }
      }
   close(PASSWD);

   #-------------------------------------------------------#
   # There shouldn't be any group with $finalusr on widows #
   #-------------------------------------------------------#

   # Now we have to verify that there isn't any group with $finalusr username
   # on windows for real group verificatoion.

   $verifygroup = `/usr/bin/mkgroup -l -g $finalusr 2>&1`;
   $verifygroup = substr ($verifygroup,0,13);
   if ($verifygroup ne "mkgroup (255)")
      {
       die "Group with same username already exists. see windows groups.\n";
      }

   #------------------------------------------#
   # There must be NET USER command on widows #
   #------------------------------------------#

   # Now we create and execute NET USER /ADD $finalusr command.

   $CMD = "NET USER /ADD ".$finalusr;
   if ($pass ne "")
      {
       $CMD = $CMD." ".$pass;
      }
   if ($comm ne "")
      {
       $CMD = $CMD." /COMMENT:\"".$comm."\" /FULLNAME:\"".$comm."\"";
      }
   $CMD = `$CMD 2>&1`;
   #die $CMD."\n";

   # Now verify user with cygwin /usr/bin/mkpasswd -l -u $finalusr

   $CMDMK = `mkpasswd -l -u $finalusr 2>&1`;
   chomp;
   ($login, $npasswd, $nuid, $ngid, $ngcos, $nhome, $nshell) = split(/:/,$CMDMK);
   if ($login ne $finalusr)
   	{
   		die "NET USER /ADD command failed.\n";
   	}

   # Now we delete user from Users group on windows with
   # NET LOCALGROUP /DELETE Users $finalusr as windows assign it as
   # default group to all new users.

   $DELUSRGRP = `NET LOCALGROUP /DELETE Users $finalusr 2>&1`;

   # Now we get all user values

   $usr   = $usr;      # Virtual Username
   $pass  = $npasswd;  # Password entry used by cygwin
   $pUID  = $nuid;     # UID
   if ($pGID eq "default")
      {
       $pGID = $ngid;  # cygwin default GID if not found in argument
      }
   else
      {
       $pGID  = $pGID; # GID found in argument
      }
   $comm  = $ngcos;    # Comments entry used by cygwin
   $home  = $home;     # Home Directory found in arguments or defalut /home/<username>
   $shell = $shell;    # User Shell found in arguments or defalut /bin/bash

   #-----------------------------------------------------------------------------------#
   # This will overwrites GID in passed arrgument and cygwin default GID for new users #
   #-----------------------------------------------------------------------------------#

   # Now we find if group with same virtual username exists in
   # cygwin /etc/group. If found then the GID of user is changed
   # to be GID of group.

   # for cygwin verification we open : /etc/group

   open(GROUP, "</etc/group") || die "Can't open group for reading.\n";
   while (<GROUP>)
      {
        ($gname, $sid, $ggid, $list) = split(/:/);
        if ($gname eq $usr)
           {
             $temp = $ggid;
           }
      }
   close(GROUP);

   if ($temp ne "")
      {
       $pGID = $temp
      }
   else
      {
       die "Specified Group not found. see cygwin /etc/group.\n";
      }

   # Now we create virtual user userstring for cygwin /etc/passwd

   $userstring = $usr.":".$pass.":".$pUID.":".$pGID.":".$comm.":".$home.":".$shell;

   # Now we add to cygwin /etc/passwd file

   open(PASSWD, ">>/etc/passwd") or die "Unable to open /etc/passwd for appending";
   print PASSWD $userstring;
   close(PASSWD);

   if ($createhome eq "-m")
      {
       mkdir("$home",0755);
       #system("cp /etc/skel/.bash_profile $home/.bash_profile");
       #system("cp /etc/skel/.bashrc $home/.bashrc");
       #system("cp /etc/skel/.inputrc $home/.inputrc");
       #system("cp /etc/skel/.zshrc $home/.zshrc");
       #system("cp /etc/skel/.xinitrc $home/.xinitrc");
       #system("cp /etc/skel/.xserverrc $home/.xserverrc");
       #system("cp /etc/skel/.bash_logout $home/.bash_logout");
       #mkdir("$home/.mail",0777);
       #system("cp /etc/skel/.mail/mailbox $home/.mail/mailbox");
       #chown $pUID, $pGID, "$home/.mail";
       chown $pUID, $pGID, "$home";
      }

   print "New user added to system.\n";

==========================================================================

if any optimization anybody can do or any kinda error or mistake, you are welcome

      Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/

--
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/