CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
I need help with perl script to add users to nis server

 



webpdx
Novice

May 23, 2009, 2:01 AM

Post #1 of 16 (1862 views)
I need help with perl script to add users to nis server Can't Post

I am having some problems with a perl script that I use to add user accounts to a NIS server running CentOS Linux.

The script runs fine but when it creates the users. it keeps the same uid and gid for every user. For example, if I add 3 users with my script. they all have a uid and gid of 16. I need some help getting my script to start with a min uid and gid of 500 and increment the uid and gid by +1 whenever I add the user. I've created a link at pastebin.com that contains the code to avoid pasting it directly in this forum. To view the code, click the link below. I really need to get this working as soon as possible. I am stumped after many sleepless nights of trying to get it to work.

http://jaysen.pastebin.com/m45f3584a

Thank You

Jaysen


FishMonger
Veteran / Moderator

May 23, 2009, 6:44 AM

Post #2 of 16 (1857 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post

Why are you manually creating the home dir and modifying the passwd, shadow, and group files? It would be better to use the standard linux utilities i.e., useradd, and groupadd.

Why are you not using the warnings and strict pragmas?

Each of the die statements on the open calls should include the reason it failed which is in $!

If you want to make sure the uid is above 500, then simply initialize $u (in the FindUID sub) to 500.


webpdx
Novice

May 23, 2009, 2:23 PM

Post #3 of 16 (1853 views)
Re: [FishMonger] I need help with perl script to add users to nis server [In reply to] Can't Post

The standard unix utilities will only work if I am using the standard passwd and group files in /etc but I am not. My files are stored in /home/admin/yp. At least that is my understanding.

Regarding the warnings and strict pragmas, I didn't write this script. I got it from work and I use it to add customers to my work servers everyday with no problems. I had to make some small changes to remove things that do not pertain to my own setup.

Can you give me an example of what I should change in the FindUID sub to make it use uids and gids above 500? I know very little perl and thought this was going to be a small change that I needed to make. I guess I thought wrong.


FishMonger
Veteran / Moderator

May 23, 2009, 3:18 PM

Post #4 of 16 (1850 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post


Code
sub FindUID { 
my($u,$ret) = 501,501;

if ($ENV{'FORCE_UID'}) {
return ($ENV{'FORCE_UID'});
}
open(PW, $pwfile) or die "Can't open $pwfile";
while (<PW>) {
$u = (split(/:/))[2];
if ($u > $ret) {
$ret = $u;
}
}
while (scalar(getpwuid($ret)) || scalar(getgrgid($ret))) {
$ret++;
}
close(PW);
return ($ret);
}



webpdx
Novice

May 23, 2009, 3:53 PM

Post #5 of 16 (1847 views)
Re: [FishMonger] I need help with perl script to add users to nis server [In reply to] Can't Post

I just made that change to the code and tried it. Still a no go. it still created the user "jaysen" with a uid and gid of 16.

drwxr-xr-x 5 root root 4096 May 23 15:46 .
drwxr-xr-x 22 root root 4096 May 23 15:21 ..
drwx------ 4 admin admin 4096 May 22 19:32 admin
drwx--s--x 3 16 16 4096 May 23 15:46 jaysen
drwx------ 2 root root 16384 May 22 07:59 lost+found


FishMonger
Veteran / Moderator

May 23, 2009, 4:03 PM

Post #6 of 16 (1844 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post

What is the value of the $ENV{'FORCE_UID'} environment variable?


FishMonger
Veteran / Moderator

May 23, 2009, 4:13 PM

Post #7 of 16 (1842 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post

The MakeHome sub is changing the ownership of the public_html dir, but not the home dir.

Is 16 the uid of the user account that runs the perl script?

Have you looked at the passwd file to see what uid it has for the user and is it the same as the owner of the home dir?


webpdx
Novice

May 23, 2009, 4:40 PM

Post #8 of 16 (1839 views)
Re: [FishMonger] I need help with perl script to add users to nis server [In reply to] Can't Post

I run the script manually as the root user. The script also is owned by root for now. Prob not a good idea but since I am testing in a local environment not connected to the net. I won't worry too much about it.

Yes, the /home/admin/yp/passwd file also has the same uid of 16. Same for the group file

[root@localhost yp]# pwd

/home/admin/yp

[root@localhost yp]# cat passwd

jaysen:x:16::Jaysen Johnson:/home/jaysen:/bin/bash

[root@localhost yp]# cat group

jaysen:x:16:jaysen

[root@localhost yp]#


webpdx
Novice

May 23, 2009, 4:45 PM

Post #9 of 16 (1836 views)
Re: [FishMonger] I need help with perl script to add users to nis server [In reply to] Can't Post


In Reply To
What is the value of the $ENV{'FORCE_UID'} environment variable?


I am not sure what this is or if I even need it. Since this originally is not my script. I just don't know. If it's not needed then I can take it out. I wish I understood this stuff more.

I certainly appreciate the help.


FishMonger
Veteran / Moderator

May 23, 2009, 4:48 PM

Post #10 of 16 (1835 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post


In Reply To
I run the script manually as the root user. The script also is owned by root for now. Prob not a good idea but since I am testing in a local environment not connected to the net. I won't worry too much about it.

Yes, the /home/admin/yp/passwd file also has the same uid of 16. Same for the group file

[root@localhost yp]# pwd

/home/admin/yp

[root@localhost yp]# cat passwd

jaysen:x:16::Jaysen Johnson:/home/jaysen:/bin/bash

[root@localhost yp]# cat group

jaysen:x:16:jaysen

[root@localhost yp]#

You'll need to confirm this, but it appears that the 16 is coming from $ENV{'FORCE_UID'}

Also, your passwd file doesn't have a value for the group id and there is nothing in your code that assigns a value to $gid.
My assumption was that $gid was being assigned in global.pl but apparently not.


FishMonger
Veteran / Moderator

May 23, 2009, 4:52 PM

Post #11 of 16 (1834 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post

$ENV{'FORCE_UID'} is an environment variable.

If you don't have that environment var, then that if block could be removed.


FishMonger
Veteran / Moderator

May 23, 2009, 4:58 PM

Post #12 of 16 (1832 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post

We need to make a slight adjustment to the change I suggested in the FindUID sub.

change:
my($u,$ret) = 501,501;

to this:
my($u,$ret) = (501,501);


webpdx
Novice

May 23, 2009, 5:14 PM

Post #13 of 16 (1829 views)
Re: [FishMonger] I need help with perl script to add users to nis server [In reply to] Can't Post

I will make the change to the FindUID sub. Here is the code for the global.pl. If it helps

if (!defined($skip_localhost_check) && `hostname` ne "localhost.localdomain\n") {
die "Must be run on localhost!"
}

#$logarea = '/home/admin/users';
$ypdir = '/home/admin/yp';
$pwfile = "$ypdir/passwd";
$shfile = "$ypdir/shadow";
$grfile = "$ypdir/group";
$ahfile = "$ypdir/auto.home";
$alfile = "$ypdir/aliases";
$vifile = "$ypdir/virtualtable";
#$welcomemsg = '/home/support/termserv.txt';

$whoami = $ENV{'SUDO_USER'} || (`who am i` =~ /!(\S+)/)[0] || $ENV{'USER'};


**EDIT**

After making the change to the FindUID, I created two new test users, bob and john. It now appears to be assigning the correct uid and gid and it does increment them by 1 when more users are added. The group also shows the username instead of the uid but the user is still coming up as the uid. I think we are on the right track. :-)

drwxr-xr-x 7 root root 4096 May 23 17:17 .
drwxr-xr-x 22 root root 4096 May 23 15:21 ..
drwx------ 4 admin admin 4096 May 22 19:32 admin
drwx--s--x 3 501 bob 4096 May 23 17:15 bob
drwx--s--x 3 16 16 4096 May 23 15:46 jaysen
drwx--s--x 3 502 john 4096 May 23 17:17 john
drwx------ 2 root root 16384 May 22 07:59 lost+found


(This post was edited by webpdx on May 23, 2009, 5:22 PM)


FishMonger
Veteran / Moderator

May 23, 2009, 5:48 PM

Post #14 of 16 (1823 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post

At this point it appears to be a problem with the NIS config because it doesn't look like is pulling the user info from the $pwfile file. I don't have any experience in setting up NIS.


FishMonger
Veteran / Moderator

May 23, 2009, 5:51 PM

Post #15 of 16 (1822 views)
Re: [webpdx] I need help with perl script to add users to nis server [In reply to] Can't Post


In Reply To
Here is the code for the global.pl. If it helps

if (!defined($skip_localhost_check) && `hostname` ne "localhost.localdomain\n") {
die "Must be run on localhost!"
}

#$logarea = '/home/admin/users';
$ypdir = '/home/admin/yp';
$pwfile = "$ypdir/passwd";
$shfile = "$ypdir/shadow";
$grfile = "$ypdir/group";
$ahfile = "$ypdir/auto.home";
$alfile = "$ypdir/aliases";
$vifile = "$ypdir/virtualtable";
#$welcomemsg = '/home/support/termserv.txt';

$whoami = $ENV{'SUDO_USER'} || (`who am i` =~ /!(\S+)/)[0] || $ENV{'USER'};


If that's all that's being done in that script, I move that to the main script and drop the global.pl script.


webpdx
Novice

May 23, 2009, 6:53 PM

Post #16 of 16 (1820 views)
Re: [FishMonger] I need help with perl script to add users to nis server [In reply to] Can't Post


In Reply To
At this point it appears to be a problem with the NIS config because it doesn't look like is pulling the user info from the $pwfile file. I don't have any experience in setting up NIS.


I think I got it fixed. I was looking at the script and added a line to sub GetInfo subroutine. If you look at the end of the subroutine, there is a line that says $uid = FindUID()

So I added a line that says $gid = FindUID() right below it. After doing that, I attempted to create another user and now it shows up like this for the user jaysen

drwxr-xr-x 5 root root 4096 May 23 18:33 .
drwxr-xr-x 22 root root 4096 May 23 15:21 ..
drwx------ 4 admin admin 4096 May 22 19:32 admin
drwx--s--x 3 jaysen jaysen 4096 May 23 18:33 jaysen
drwx------ 2 root root 16384 May 22 07:59 lost+found

When I do an id jaysen, it now shows

[root@localhost home]# id jaysen
uid=501(jaysen) gid=501(jaysen) groups=501(jaysen)
[root@localhost home]#

The /home/admin/yp/passwd file shows this

[root@localhost yp]# cat passwd
jaysen:x:501:501:Jaysen Johnson:/home/jaysen:/bin/bash
[root@localhost yp]#

And the /home/admin/yp/group shows this

[root@localhost yp]# cat group
jaysen:x:501:jaysen
[root@localhost yp]#

I am hoping this will work without causing any other problems. Any thoughts or suggestions?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives