#!/usr/bin/perl #==========================================# # Author: Marius Feraru aka AltBlue # # E-Mail: altblue@tuiasi.ro # # URL : http://www.cs.tuiasi.ro/altblue/ # # http://bofh.mednet.ro/altblue/ # #==========================================# $passwd_file = 'passwd'; ## get users' info open(PASS,$passwd_file); $file = join('',); close(PASS); @users = split(/user = /,$file); foreach(@users) { next if $_ eq ''; ## parsing fields... $_ =~ s/ {\n/\n/g; $_ =~ s/\n +/\n/g; @attr = split(/\n/,$_); ## passwd ($key,$value) = split(/ = /,$attr[1]); ($crypt,$passwd) = split(/ /,$value); ## user type ($key,$value)= split(/ = /,$attr[2]); ## build hash: user = cryptmode÷password÷user_type $PASS{$attr[0]} = "$crypt÷$passwd÷$value"; } $action = $ARGV[0]; $who = $ARGV[1]; if($action eq 'l') { ## lock user &ab_lock_user; } elsif($action eq 'u') { ## unlock user &ab_unlock_user; } elsif($action eq 'a') { ## add user :) &ab_add_user; } elsif($action eq 'd') { ## delete user &ab_del_user; } else { ## list users :) &ab_print_users; } sub ab_print_users { ### print users foreach(sort keys(%PASS)){ @attr = split(/÷/,$PASS{$_}); print "username: '$_'\n\tpassword type:\t$attr[0]\n\tpassword:\t$attr[1]\n\tuser type:\t$attr[2]\n"; } } sub ab_update_users { ### update user password file open(Pas,">$passwd_file") || die "ERROR - cannot open '$passwd': $!\n"; foreach(sort keys(%PASS)){ @attr = split(/÷/,$PASS{$_}); print Pas "user = $_ {\n login = $attr[0] $attr[1]\n member = $attr[2]\n}\n"; } close(Pas); } sub ab_check_user { if($who ne '' && defined $PASS{$who}) { return 1; } else { return 0; } } sub ab_lock_user { unless(&ab_check_user) { print "ERROR: user '$who' does not exist!\n"; exit 1; } $PASS{$who} =~ s/^([^÷]*)÷*([^÷]*)÷*([^÷]*)/$1÷\*$2÷$3/; # &ab_print_users; &ab_update_users; } sub ab_unlock_user { unless(&ab_check_user) { print "ERROR: user '$who' does not exist!\n"; exit 1; } $PASS{$who} =~ s/^([^÷]*)÷\*+/$1÷/; &ab_update_users; } sub ab_del_user { unless(&ab_check_user) { print "ERROR: user '$who' does not exist!\n"; exit 1; } delete $PASS{$who}; &ab_update_users; } sub ab_add_user { if(&ab_check_user) { print "ERROR: user '$who' already exists!\n"; exit 1; } # ask for password... $newpass # ask for user type and crypt method $newcrypt $newtype $PASS{$who} = "$newcrypt÷$newpass÷$newtype"; &ab_update_users; }