LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2013, 09:44 PM   #1
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Rep: Reputation: Disabled
Creating user accounts from a csv file, force to change password and create alias


Hi, I'm new to Linux. My homework is to create user accounts from a csv file.I have a few questions, I hope someone can help me.I'm using CentOS Server
I'm using Perl to do the script. I run the script as root. I collect all the necessary information and add it to a variable, so the result is something like this:

useradd jbach -c "Jean-Sebastien Bach" -d /home/jbach -p "$1$Pe4mUap?$KpQdKCf501tsxXY5xDcLt/" -g 5000 -G 10

the user account is created, but when I check my file /etc/shadow
there is not password created, I'm using the crypt function to encrypt the password.

jbach:?/:15796:0:99999:7:::

How can I assign the password when I'm creating the account? I have to do in the script.

Also I have to force the user to change the password when logging in for the fist time. I found that I could use this command:
# chage -d 0 jbach, and it will change the number 15796 to 0, can I change this number with the command passwd??? if yes, which option?? I tried -n and -x
but it changes the other numbers and not the 15796.

Also, when creating an user, what is the difference between the option -m and -d. I'm using -d to create his/her home directory it this right?

Last question, I have to create alias for certain commands, like dir='ls -l', ,if I want those alias to be available on any session or terminal, where do I have to define them??? do I have to add them in the .bashrc file in /etc/skel by default, so when creating my users it will be add it automatically??? I'm not sure.
 
Old 03-31-2013, 10:16 PM   #2
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
I just added the alias in the .bashrc in the /etc/skel by default and it work, it created the new user with the alias. I think I got it right
 
Old 03-31-2013, 10:34 PM   #3
nicksu
Member
 
Registered: Dec 2012
Posts: 35

Rep: Reputation: Disabled
Quote:
Originally Posted by rojasm View Post
Hi, I'm new to Linux. My homework is to create user accounts from a csv file.I have a few questions, I hope someone can help me.I'm using CentOS Server
I'm using Perl to do the script. I run the script as root. I collect all the necessary information and add it to a variable, so the result is something like this:

useradd jbach -c "Jean-Sebastien Bach" -d /home/jbach -p "$1$Pe4mUap?$KpQdKCf501tsxXY5xDcLt/" -g 5000 -G 10

the user account is created, but when I check my file /etc/shadow
there is not password created, I'm using the crypt function to encrypt the password.

jbach:?/:15796:0:99999:7:::

How can I assign the password when I'm creating the account? I have to do in the script.

Also I have to force the user to change the password when logging in for the fist time. I found that I could use this command:
# chage -d 0 jbach, and it will change the number 15796 to 0, can I change this number with the command passwd??? if yes, which option?? I tried -n and -x
but it changes the other numbers and not the 15796.

Also, when creating an user, what is the difference between the option -m and -d. I'm using -d to create his/her home directory it this right?

Last question, I have to create alias for certain commands, like dir='ls -l', ,if I want those alias to be available on any session or terminal, where do I have to define them??? do I have to add them in the .bashrc file in /etc/skel by default, so when creating my users it will be add it automatically??? I'm not sure.
To set the password can use this
echo "password" | passwd --stdin user
 
Old 03-31-2013, 11:39 PM   #4
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks a lot, It worked, I change it to the encrypted password.

for the fact that I have to force the user to change the password when logging in for the fist time, is this command right?
chage -d 0 jbach

And just if u can explain what is the difference difference between the option -m and -d when using useradd?
 
Old 04-01-2013, 09:39 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Re useradd switches, they are clearly explained here http://linux.die.net/man/8/useradd if you read carefully.
 
Old 04-01-2013, 10:11 PM   #6
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks for the link, I read it
 
Old 04-01-2013, 10:32 PM   #7
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
I have another question, I need to create the account with an encrypted password, so I change this line
system ("echo \"Passw0rd" | passwd --stdin $name");

to this

/bin/echo $name:\$1\$ebjrjWoS\$SQjW1nqiGSc9cpy8i/j77/ | /usr/sbin/chpasswd -e"

so, this way I could pass the encrypted password, but when I tried directly in the terminal, it works, I got this as a result

jcote:$1$ebjrjWoS$SQjW1nqiGSc9cpy8i/j77/:15797:0:90:7:7::


but when I execute the perl script system("/bin/echo $name:\$1\$ebjrjWoS\$SQjW1nqiGSc9cpy8i/j77/ | /usr/sbin/chpasswd -e"); all I got is this:

jcote:/j77/:0:0:90:7:7::

I don't understand why it doesn't take the whole encrypted password.

Can someone help me to get it right.

Last edited by rojasm; 04-01-2013 at 10:32 PM. Reason: I didn't finisd my prhase
 
Old 04-01-2013, 11:11 PM   #8
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
I thought it could be that I'm mission the \, so I added but still it doesn't work

system("/bin/echo $name:\$1\$ebjrjWoS\$SQjW1nqiGSc9cpy8i\/j77\/ | /usr/sbin/chpasswd -e");
 
Old 04-01-2013, 11:46 PM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
It is failing because you are trying to go through multiple levels of escape handling.

It is way too difficult to get it right.

Look at the perl line:
Code:
system("/bin/echo $name:\$1\$ebjrjWoS\$SQjW1nqiGSc9cpy8i\/j77\/ | /usr/sbin/chpasswd -e");
The perl interpreter handles the \ escape lines.... So what gets passed to the shell (invoked by the "system" function) is:
Code:
system("/bin/echo actualname:$1$ebjrjWoS$SQjW1nqiGSc9cpy8i/j77 / | /usr/sbin/chpasswd -e");
Now the shell has to interpret the line:
Code:
/bin/echo actualname:$1$ebjrjWoS$SQjW1nqiGSc9cpy8i/j77/ | /usr/sbin/chpasswd -e
into:
Code:
/bin/echo actualname:/j77/ | /usr/sbin/chpasswd -e
So what happend? the shell parameter $1 is null (undefined), as is the shell variables $ebjrjWoS and $SQjW1nqiGSc9cpy8i. And that makes the password an invalid format, hence the ?/

How to get around this?

Don't use system - use a pipe and write exactly what you want to send to "chpasswd -e" through the pipe.

Using system is the same as "sh -c /bin/echo ...", so the parameters to "sh" are exposed. Even if you used the internal echo it is STILL exposed because it is part of the parameter to the "sh" command.

Using a pipe will only reveal the "chpasswd -e" command, and no useful parameters.
 
Old 04-02-2013, 03:55 AM   #10
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
Sorry, I don't know how to use or create a pipe. I'm a beginner :-(
I was reading on internet about pipes to understand more but I don't know how to create a pipe.
In my script I have a function that encrypt the password, and then it is stored in a variable.
I understood that a pipe could pass information from one script to another, or one command to another like
ls -al | grep csv, I understand that the grep function will be done in the result of the ls -al command,
but I don't get it how to use it in my case.
Could you give me an example of how to create a pipe?
Thanks in advance
 
Old 04-02-2013, 07:10 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Quote:
Originally Posted by rojasm View Post
Sorry, I don't know how to use or create a pipe. I'm a beginner :-(
I was reading on internet about pipes to understand more but I don't know how to create a pipe.
In my script I have a function that encrypt the password, and then it is stored in a variable.
I understood that a pipe could pass information from one script to another, or one command to another like
ls -al | grep csv, I understand that the grep function will be done in the result of the ls -al command,
but I don't get it how to use it in my case.
Could you give me an example of how to create a pipe?
Thanks in advance

Pipes are trivial in Perl. For the simple one:

Code:
 open(FILE,"|command");
Then just print to the FILE.
 
Old 04-02-2013, 08:34 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
For the user creations, you may want to use a dedicated tool http://linux.die.net/man/8/newusers
If you really want to do this in Perl; http://search.cpan.org/~sharyanto/Un...Passwd/File.pm
The advantage of these tools is they accept plaintext passwds eg from a file and create hashed ones, without exposing the plaintext to eg ps or top cmds.

If you want to use Perl, but create hashed passwds yourself, you'd need to check /etc/login.defs to find out what the system expects eg
Code:
# Use SHA512 to encrypt password.
ENCRYPT_METHOD SHA512
then grab the relevant module from CPAN (search.cpan.org) http://search.cpan.org/~drsteve/Cryp...pt/Password.pm
 
1 members found this post helpful.
Old 04-02-2013, 09:56 PM   #13
rojasm
LQ Newbie
 
Registered: Mar 2013
Location: Montreal
Distribution: CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thansk JPollar, I look a little bit more on internet and I found another example of how to pass the information through the pipe.

open(NAMEPIPE, "|command");
print NAMEPIPE "information to be passed";
close(NAMEPIPE);

Now my script it's working.

To chrism01, thanks for the links, but I really needed to use chpasswd, because part of my homework is to encrypt the password and pass it encrypted when creating the users.
 
Old 04-02-2013, 11:19 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
The last bit of my post covers that.
In any case, here's some good Perl links

http://perldoc.perl.org/
http://www.tizag.com/perlT/index.php
http://www.perlmonks.org/?node=Tutorials

... and do bookmark search.cpan.org, to save you re-inventing stuff.

Note that if you're using Perl, you should really do it all in Perl (where possible); Its a very powerful language, it's not supposed to be a substitute for eg bash, where you call lots of external programs like chpasswd etc.

Last edited by chrism01; 04-02-2013 at 11:23 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to force user to change password after expiration in Linux? ssy68 Linux - Newbie 4 06-30-2011 03:54 PM
[SOLVED] REDHAT missing functionality - force user to change password on login machielr Linux - Security 5 03-17-2011 03:36 PM
How to force a user to change their NIS password at logon? synthol6 AIX 1 08-17-2010 09:47 PM
Create user add file with default password and force user to change it? Morgandy Linux - Newbie 3 02-02-2010 06:06 PM
How to set password when creating user accounts correro Linux - Newbie 3 02-08-2007 03:32 AM


All times are GMT -5. The time now is 12:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration