delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/05/10/10:30:40

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
X-Yahoo-Newman-Property: ymail-3
X-Yahoo-Newman-Id: 603225 DOT 45926 DOT bm AT omp105 DOT mail DOT in2 DOT yahoo DOT com
X-YMail-OSG: JShv.8oVM1kVoCBlMdIIAJSvEhB.o88VaB7BuLu8dTenm8IYv.ntwr_yZ8VHL.yNGrtR7ENZCutbYwowHbc9wDnt8KDevSYDWIrhV4Ng6J1lX6SzpK16dfhXR52ZmswnmaHDNQ--
X-Mailer: YahooMailWebService/0.7.185
Date: Sat, 10 May 2008 20:00:01 +0530 (IST)
From: Jaspreet Singh <jaspreet_online2000 AT yahoo DOT com>
Reply-To: jaspreet_online2000 AT yahoo DOT com
Subject: Re: cygwin perl useradd command
To: Hugh Sasse <hgs AT dmu DOT ac DOT uk>
Cc: cygwin AT cygwin DOT com
In-Reply-To: <Pine.GSO.4.64.0805091314490.5627@brains.eng.cse.dmu.ac.uk>
MIME-Version: 1.0
Message-ID: <524613.65281.qm@web94913.mail.in2.yahoo.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id m4AEUd2p004708

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/


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019