#!/usr/bin/perl # # Skrypt do zarządzania kontami e-mailowymi w bazie danych MySQL. # Do działania wymaga odpowiednio przygotowanej tabeli w bazie danych. # Przykładowa baza danych: http://pasternok.org/sources/postfix.sql.bz2 # # Copyleft (c) 2008 Damian Pasternok. All rights reversed. use strict; use DBI; use Term::ReadKey; my $wersja = '0.1'; my $domena = 'domena.pl'; my $maildir = '/var/mail/virtual'; my $transport = 'virtual'; my $quota = 512000000; my $maildirmake = '/usr/lib/courier-imap/bin/maildirmake'; my $baza = 'postfix'; my $db_username = 'postfix'; my $db_password = 'tajnehaslo'; my $dsn = 'DBI:mysql:'.$baza.':localhost'; my $dbh = DBI->connect($dsn, $db_username, $db_password); sub dodaj_konto(); sub usun_konto(); sub wyswietl_konta(); sub zmien_haslo(); main: system("clear"); print "\n======================================\n". "= Postfix MySQL accounts manager =\n". "= version $wersja =\n". "= copyleft (c) 2008 Damian Pasternok =\n". "======================================\n\n"; print "Menu:\n". "\t1 - dodaj konto e-mail\n". "\t2 - usuń konto e-mail\n". "\t3 - wyświetl listę kont\n". "\t4 - zmień hasło\n\n". "\t5 - zakończ program\n\n". "Twój wybór: "; wybor: my $wybor = ; if($wybor == 1) { dodaj_konto(); } elsif($wybor == 2) { usun_konto(); } elsif($wybor == 3) { wyswietl_konta(); } elsif($wybor == 4) { zmien_haslo(); } elsif($wybor == 5) { goto zakoncz; } else { print "Twój wybór: "; goto wybor; } sub dodaj_konto() { print "Podaj adres e-mail, który chcesz dodać: "; email: my $email = ; chomp $email; my $sth = $dbh->prepare(qq{ select username from mailbox where username = '$email' }); $sth->execute(); my $username = $sth->fetchrow_array(); chomp $username; $sth->finish(); if($email eq $username) { print "W bazie istnieje już taki e-mail (lub zostawiłeś puste pole)!\n". "Podaj adres e-mail, który chcesz dodać: "; goto email; } else { name: print "Podaj nową nazwę użytkownika: "; my $name = ; chomp $name; if($name eq "") { print "Pole nie może być puste!\n"; goto name; } password: print "Podaj hasło dla nowego konta: "; ReadMode('noecho'); my $password = ReadLine(0); chomp $password; if($password eq "") { print "\nPole nie może być puste!\n"; goto password; } print "\nPotwierdź hasło: "; my $password2 = ReadLine(0); chomp $password2; if($password eq $password2) { my $data = `date '+%F +%T'`; $sth = $dbh->prepare(qq{ insert into mailbox values ('$email', '$password', '$name', '$email/','$transport' , '$domena', '$data', '$data', '1', '$quota', '0') }); $sth->execute(); system("$maildirmake $maildir/$email"); system("chown -R postfix: $maildir/$email"); print "\n\nKonto $email zostało dodane!\n\n"; $sth->finish(); } else { print "\nPodane hasła różnią się od siebie!\n"; goto password; } ReadMode('restore'); } } sub usun_konto() { print "Podaj adres e-mail, który chcesz usunąć: "; email: my $email = ; chomp $email; my $sth = $dbh->prepare(qq{ select username from mailbox where username = '$email' }); $sth->execute(); my $username = $sth->fetchrow_array(); $sth->finish(); if($email eq $username and $email ne "") { wybor: print "Czy na pewno chcesz usunąć konto $email? (T/N) "; my $wybor = ; chomp $wybor; if($wybor eq 't' || $wybor eq 'T') { my $sth = $dbh->prepare(qq{ delete from mailbox where username = '$email' }); $sth->execute(); system("rm -rf $maildir/$email"); print "\nKonto $email zostało usunięte!\n\n"; $sth->finish(); } elsif($wybor eq 'n' || $wybor eq 'N') { print "\n"; } else { goto wybor; } } else { print "W bazie nie istnieje podany e-mail lub zostawiłeś puste pole!\n". "Podaj adres e-mail, który chcesz usunąć: "; goto email; } } sub wyswietl_konta() { print "\nLista kont w domenie $domena:\nE-mail\t\t\t\tNazwa użytkownika\n"; my $sth = $dbh->prepare(qq{ select username, name from mailbox }); $sth->execute(); while(my $ref = $sth->fetchrow_hashref()) { print "$ref->{'username'}\t\t\t$ref->{'name'}\n"; } $sth->finish(); print "\n"; } sub zmien_haslo() { print "\nPodaj adres e-mail, dla którego chcesz zmienić hasło: "; email: my $email = ; chomp $email; my $sth = $dbh->prepare(qq{ select username from mailbox where username = '$email' }); $sth->execute(); my $username = $sth->fetchrow_array(); $sth->finish(); if($email eq $username && $email ne "") { password: print "Podaj nowe hasło dla konta $email: "; ReadMode('noecho'); my $password = ReadLine(0); chomp $password; if($password eq "") { print "\nPole nie może być puste!\n"; goto password; } print "\nPotwierdź hasło: "; my $password2 = ReadLine(0); chomp $password2; if($password eq $password2) { my $data = `date '+%F +%T'`; $sth = $dbh->prepare(qq{ update mailbox set PASSWORD = '$password', change_date = '$data' where username = '$email' }); $sth->execute(); print "\n\nHasło zostało zmienione!\n\n"; $sth->finish(); } else { print "\nPodane hasła różnią się od siebie!\n"; goto password; } ReadMode('restore'); } else { print "W bazie nie istnieje podany e-mail (lub zostawiłeś puste pole)!\n". "Podaj adres e-mail, dla którego chcesz zmienić hasło: "; goto email; } } printf "Naciśnij dowolny klawisz...\n"; ReadMode('cbreak'); ReadKey(0); ReadMode('normal'); goto main; zakoncz: $dbh->disconnect();