LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2009, 04:41 PM   #1
kernel-P4N1C
Member
 
Registered: Nov 2007
Location: Panama city, Republic of Panmaa
Posts: 167

Rep: Reputation: 18
random password script


Hello guys...

as the topic suggest; i wrote a bash script that generates a random password for a user... write it on a file and send an email with the file to the user...

it is supposed to be run on cron; and it works pretty good; but i've found a little problem.


while testing; i typed in some userid's that doesn't exist and the script still write the file with the random password and stuff...

how do i break the script if the wrong input is given ?


check my code below
Code:
#!/bin/bash

### This function change the password using the aray generated by randpass
function passchange { echo -e "$pwdxx\n$pwdxx" | (sudo passwd --stdin $USERID);
echo "Password saved on $filename file";}

#### This function generates a random aray of 8 characters... to change, edit "head -cX"
function randpass { pwdxx=`</dev/urandom tr -dc A-Za-z0-9 | head -c8`;}

### This function write the password to a text file
function outputfile { echo -e "ssh access for server $SERVERNAME\n\nuser: $USERID\npasswd: $pwdxx" > $filename;}

#echo "sending file to serverinfo"
#(scp $USER serverinf@sserver:/home/serverinf)


if [ $# -ne 1 ]
then
        echo ""
        echo "Usage -$0 USER"
        echo "  Where USER is the username who password will be change"
        exit 1
fi
##### ----- variables ----- ####
USERID=$1
SERVERNAME=` /usr/local/sbin/./servername`
filename=$SERVERNAME-$USERID

##### ----- Calling functions declared above ----- #####
randpass
outputfile
passchange

Last edited by kernel-P4N1C; 08-06-2009 at 04:43 PM.
 
Old 08-06-2009, 08:35 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you call passchange before outputfile, you can check if passwd change failed.
Alternately, if that proves to be too fiddly (what with those subshells, pipes etc), just start with a new fn checkuser, which looks for user in /etc/passwd.
I'd go with the latter myself.
 
Old 08-07-2009, 02:37 AM   #3
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
This will allow you to check for existing user names...

Code:
grep "^$1:" /etc/passwd > /dev/null
if [ $? = 0 ]; then
  USERID=$1
  SERVERNAME=$(hostname)
  filename=$SERVERNAME-$USERID

  randpass
  outputfile
  passchange
else
  echo "User $1 does not exist."
fi
Also, I dont like how you're writing the credentials to the file system. At minumum, you should do some strict access enforcement (chmod) on the file before writing the password to the file so normal users can have access to it. "shred -n3 -u" to remove the data would have limited use on a journaled filesystem.

Best case, you should pipe the information to gpg, encrypted to each user before it's emailed. But that would require a whole lot more prerequisits... like users having to know how to use pgp.
 
  


Reply



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
[SOLVED] uuencode for random password generation with /dev/urandom JZL240I-U SUSE / openSUSE 27 09-06-2007 01:00 AM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 06:48 PM
using a random file as password to dm-crypt qwijibow Linux - Security 6 11-08-2004 11:57 AM
random generated password LinuxRam Linux - Security 13 09-13-2004 05:25 AM
Help: Need random password generator linuxgamer Linux - Software 5 12-02-2003 01:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 PM.

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
Open Source Consulting | Domain Registration