#!/usr/bin/perl # Feature changes: # 1. Rewritten in Perl, using String::MkPasswd # 2. Simplified accounts structure, all accounts now # feature an expiry time, and may not traverse into # other accounts' directories # 3. Changed username format to all lowercase letters, # numbers and underscore # 4. The user purging script actually works! # 5. Better passwords via String::MkPasswd # 6. You can now extend user accounts use strict; use String::MkPasswd qq{mkpasswd}; my $username = lc( shift ); my $expiration = shift; if( $< != 0 ) { print qq{You must run this script as root or sudo!\n}; exit; } if( $username eq q{} ) { print qq{You must enter a username!\n}; exit; } elsif ( defined( getpwnam( $username ) ) ) { print qq{This username already exists!\n}; exit; } elsif ( $username =~ m/\W/ ) { print qq{You may not use special characters in the username!\n}; exit; } mkdir qq{/FTP/$username}; mkdir qq{/FTP/$username/$username}; my $password = mkpasswd(); if ( $password =~ m/\"|\'/ ) { $password = mkpasswd(); } system( qq{useradd -s /bin/false -M $username} ); system( qq{usermod -g customers $username} ); system( qq{usermod -d /$username $username} ); system( qq{chown -R $username:customers /FTP/$username/$username} ); print qq{Created SFTP user $username\n}; system( qq{echo "$username:$password" | chpasswd} ); print qq{Assigned password $password to $username\n}; open( my $expFile , qq{>>/etc/ssh/sftp-users.dat} ); my $currentTime = time; my $newTime; if( defined( $expiration ) ) { $newTime = ( $expiration * 86400 ) + $currentTime; print qq{Set expiration to $expiration days.\n}; } else { $newTime = $currentTime + 604800; print qq{Set expiration to 7 days (default).\n}; } print $expFile qq{$username,$newTime\n}; close( $expFile );