This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: cygwin perl useradd command

On Fri, 9 May 2008, Jaspreet Singh wrote:

> ok Hugh Sasse
> as you have saw what i have done. i understand everything you
> wrote as i am a programer like about using functions and grep.
> But, the problem is that i am a windows programer and do
> programing in Visual Basic, I have already compleated this
> useradd, usermod , userdel, etc Commands in VB.
> VB programs cant be distributed as it need licence from Microsoft.
> So, i was tring to do open source kinda thing.

Yes, I agree with your goals.  In VB you are probably way ahead of me.
> Believe me i dont know anything about Perl. I was just tring to

It has been part of Perl's philosophy that beginners are welcome,
and beginner's code is not to be frowned upon.  My comments were
intended to be constructive, and help you progress in perl.  I wrote
them off-list so there was no possibility of you feeling criticised
in public, and I'm content that you replied to the list as I
specifically said.

> write a script for these commands and still tring as i have ported
> webmin and virtualmin to cygwin with all of the required modules
> so as i am tring if you can do what you just wrote to my sample perl script it will be really a great.
> The big problem is user and group with same name with virtualization in passwd and group file. How to calculate that
> The easiest way is create real user with "_usr" and real group with "_grp" at the end of the username and groupname in windows, then input in passwd and group file on cygwin without "_usr" and "_grp", with real UID and GID.

I can't put in the time to do this for you but you can extract
things from this, written for Solaris and no longer needed.

#!/usr/local/bin/perl -w

# Program to add users according to their name.

$group_to_add_to = 113; # The group to which we are
                        # adding users
$user_stem = "rx93";    # The start of the username

%gcos_by_name = ();
%name_by_gcos = ();

$final_uid = 0;
$final_username = "";

# First we need to get the information about users
# we already have.

sub init_passwords

   # You could do this with
   #   open(PASSWD, "</etc/passwd") || die "Can't open passwd for reading";
   #   while (<PASSWD>) { 
   #     ($name,$passwd,$uid,$gid, $gcos,$dir,$shell) = split(/:/); 
   #     #...
   #   }
   #   close(PASSWD);
   # or something.

   while (($name,$passwd,$uid,$gid,
           $quota,$comment,$gcos,$dir,$shell) = getpwent) 
      $gcos_by_name{$name} = $gcos;
      $name_by_gcos{$gcos} = $name;
      $final_uid = $uid if $group == $group_to_add_to;
      $final_username = $name if $group == $group_to_add_to;
}; # end init_passwords

# Then we need to continuously prompt for names,
sub get_users_name()
   my($first_name, $first_initial);
   my($last_name, $last_initial);
   my($username, $gcos);

   print "Enter new user's Title:";
   $title = <STDIN>;
   print "Enter First Name:";
   $first_name = <STDIN>;

   print "Enter (middle) Initials:";
   $middle_initials = <STDIN>;

   print "Enter Last Name:";
   $last_name = <STDIN>;

   # derive a new username for the user.
   $first_name =~ /(\w)\w+/;
   $first_initial = $1;

   $last_name =~ /(\w)\w+/;
   $last_initial = $1;

   $username = lc "$user_stem$first_initial$last_initial";

   # Check the username doesn't clash
   if (defined($gcos_by_name{$username}))
      # If if does generate a unique name.
      $new_username = $username . "1";
      while (defined($gcos_by_name{$new_username}))
      $username = $new_username;

   # The next thing to do is to construct the gcos
   # field for this user, 
   $gcos = "$first_name $last_name";

   # and make sure this does not
   # clash with one already there (John Smith...)
   if (defined($name_by_gcos{$gcos}))
      $gcos = "$first_name $middle_initials $last_name";

   if (defined($name_by_gcos{$gcos}))
      print STDERR "Warning: $gcos is already used by $name_by_gcos{$gcos}\n";
      $name_by_gcos{$gcos} = $username;
   $gcos_by_name{$username} = $gcos;

   print "$gcos will have username $username\n";

}; #end get_users_name

sub setup_account
   my($username, $gcos) = @_;
   # create a directory for the user

   my($uid) = getuid($username);
   my($gid) = getgid($username);

   chown $uid, $gid, "/home/$username";

   # install the appropriate files for them,

   foreach $file (".profile", ".cshrc", ".login")
      system("cp /etc/skel/local$file /home/$username/$file");
      # and make sure they own the files.
      chown $uid, $gid, "/home/$username/$file";
}; # end setup_account;

sub update_passwd
   my($username, $gcos) = @_;

   my($uid) = $final_uid++;

   $passwd_entry = "$username:x:$uid:$group_to_add_to:$gcos:/home/$username:/bin/csh\n";

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

   # Shadow entry  stuff deleted.


$finished = 0;
   ($name,$gcos) = &get_users_name;
   print "Another? ";
   while(($ans = <STDIN>) =~ /^[yYnN]/)
      print "Uh?  Yes or No. Another? ";
   $finished = ($ans =~ /^y/i);


Unsubscribe info:
Problem reports:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]