From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12042 invoked by alias); 9 May 2008 17:20:28 -0000 Received: (qmail 12030 invoked by uid 22791); 9 May 2008 17:20:25 -0000 X-Spam-Check-By: sourceware.org Received: from mail2.dmu.ac.uk (HELO mail2.dmu.ac.uk) (146.227.160.29) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 09 May 2008 17:20:00 +0000 Received: from localhost (localhost.test.com [127.0.0.1]) by mail2.dmu.ac.uk (Postfix) with ESMTP id 2530A7DCB93; Fri, 9 May 2008 18:16:58 +0100 (IST) Received: from europa.cse.dmu.ac.uk (europa.cse.dmu.ac.uk [146.227.57.47]) by mail2.dmu.ac.uk (Postfix) with ESMTP id B00537DC94C; Fri, 9 May 2008 18:15:54 +0100 (IST) Received: from brains (brains.eng.cse.dmu.ac.uk [146.227.22.1]) by europa.cse.dmu.ac.uk (8.13.6/8.13.6) with ESMTP id m49HIq3W002566; Fri, 9 May 2008 18:18:52 +0100 (BST) Received: from brains.eng.cse.dmu.ac.uk ([146.227.22.1] helo=brains) by brains with esmtp (Exim 4.44) id 1JuWFA-00038Z-Et; Fri, 09 May 2008 18:18:52 +0100 Date: Fri, 09 May 2008 17:58:00 -0000 From: Hugh Sasse To: Jaspreet Singh cc: cygwin@cygwin.com Subject: Re: cygwin perl useradd command In-Reply-To: <787560.80682.qm@web94913.mail.in2.yahoo.com> Message-ID: References: <787560.80682.qm@web94913.mail.in2.yahoo.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-IsSubscribed: yes Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com X-SW-Source: 2008-05/txt/msg00151.txt.bz2 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 { my($name,$passwd,$uid,$gid, $quota,$comment,$gcos,$dir,$shell); # You could do this with # open(PASSWD, ") { # ($name,$passwd,$uid,$gid, $gcos,$dir,$shell) = split(/:/); # #... # } # close(PASSWD); # or something. setpwent; 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; } endpwent; }; # end init_passwords # Then we need to continuously prompt for names, sub get_users_name() { my($title); my($first_name, $first_initial); my($last_name, $last_initial); my($middle_initials); my($username, $gcos); print "Enter new user's Title:"; $title = ; print "Enter First Name:"; $first_name = ; print "Enter (middle) Initials:"; $middle_initials = ; print "Enter Last Name:"; $last_name = ; # 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})) { $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"; } else { $name_by_gcos{$gcos} = $username; } $gcos_by_name{$username} = $gcos; print "$gcos will have username $username\n"; return($username,$gcos); }; #end get_users_name sub setup_account { my($username, $gcos) = @_; # create a directory for the user my($uid) = getuid($username); my($gid) = getgid($username); mkdir("/home/$username",0755); 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; close(PASSWD); # Shadow entry stuff deleted. } init_passwords $finished = 0; while(!$finished) { ($name,$gcos) = &get_users_name; &update_passwd($name,$gcos); &setup_account($name,$gcos); print "Another? "; while(($ans = ) =~ /^[yYnN]/) { print "Uh? Yes or No. Another? "; }; $finished = ($ans =~ /^y/i); }; __END__ HTH Hugh -- 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/