As far as I can tell, it works fine in the creation of the groups, the user account and the folders in /FTP/$username. However, if you can spot anything to improve on, please do point it out! Anyway, back to the issue... The account doesn't seem to be expiring (i.e. automatically deleting itself and the user folder in /FTP/$username/ and removing itself from any system groups) and when I run the backup command "sudo sftp-delete-user $username" (code below) it seems to successfully delete the user account, as it doesn't show up in "/etc/passwd" but the user folders in /FTP/$username/ all still remain. As per the "how to post a question effectively" page, laziness is a virtue for many people in the vast umbrella of I.T. and programming - if it's not already obvious, I'm a system administrator/network engineer for a cyber security company and this being a manual process creates additional overhead that I simply don't have the time or the will to do manually, so any help would be greatly appreciated!#!/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 );
And the code for the command "sudo sftp-purge-users" also doesn't seem to work. This script is almost identical to the previous "sftp-delete-user" with the only difference being that line 18 is removed.#!/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 File::Copy; use File::Path; my $username = lc( shift ); if( $< != 0 ) { print qq{You must run this script as root or sudo!\n}; exit; } open( my $oldExpFile, qq{</etc/ssh/sftp-users.dat} ) || die qq{Couldn't open expirations file!\n}; open( my $newExpFile, qq{>/etc/ssh/sftp-users.dat.tmp} ) || die qq{Couldn't create new expirations file!\n}; while( my $line = <$oldExpFile> ) { ( my $userInLine ) = $line =~ /^(.+?),.+?$/; if( $username eq $userInLine ) { system( qq{deluser $username} ); system( qq{delgroup $username} ); rmtree( qq{/FTP/$username} ); print qq{Deleted $username!\n}; } else { print $newExpFile $line; } } close( $oldExpFile ); close( $newExpFile ); move( qq{/etc/ssh/sftp-users.dat.tmp}, qq{/etc/ssh/sftp-users.dat} );
In reply to Script for SFTP users not deleting accounts automatically by C18ANT
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |