Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI for Vexim2 #267

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

## CLI for Vexim2

These files assume you have passwordless Mysql access to localhost i.e. proper settings in /root/.my.cnf

They also assume DB name is set :

```
echo DB=vexim2 >> /etc/vexim2.conf
```


### Installation

```
cp -v bin/vexim2_* /usr/local/bin/
chmod +x /usr/local/bin/vexim2_*
```

### Usage

All commands are safe to run without arguements. If they need some, they will ask for.

33 changes: 33 additions & 0 deletions bin/vexim2_changepass
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 USER@DOMAIN PASSWORD"
exit 1
}


HOME=/root/

LOCALPART_D=$1
PASSWORD=$2

[ -n "$LOCALPART_D" ] && [ -n "$PASSWORD" ] || exitmessage "empty option"




HASH=$( dovecot pw -s SHA512-CRYPT -p $PASSWORD | sed 's@{SHA512-CRYPT}@@' )

mysql "$DB" -se "select username from users where username='$LOCALPART_D' " | grep . || exitmessage "user doesnt exist"

mysql "$DB" -se "update users set crypt='$HASH'
where username='$LOCALPART_D' " && echo CHANGED || exitmessage "error SQL query"


# echo "password for $LOCALPART@$D has been changed" | mail -s "Email Pass Reset" [email protected]

25 changes: 25 additions & 0 deletions bin/vexim2_del_domain
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 DOMAIN"
exit 1
}


#set -x
HOME=/root/

D=$1

[ -n "$D" ] || exitmessage "empty option"



mysql "$DB" -se "delete from domains where domain='$D'"
mysql "$DB" -se "delete from users where username like '%@$D' ";

22 changes: 22 additions & 0 deletions bin/vexim2_del_user
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 USER@DOMAIN"
exit 1
}


HOME=/root/

LOCALPART_D=$1

[ -n "$LOCALPART_D" ] || exitmessage "empty option"

mysql "$DB" -se "delete from users where username = '$LOCALPART_D' " && echo DELETED


12 changes: 12 additions & 0 deletions bin/vexim2_list_domainaliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



HOME=/root/


mysql "$DB" -se " select concat (alias,' -> ',domain) from domainalias JOIN domains ON domainalias.domain_id=domains.domain_id ; "| cat
#| grep -v siteadmin


12 changes: 12 additions & 0 deletions bin/vexim2_list_users
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



HOME=/root/


mysql "$DB" -se "select username,crypt from users "
#| grep -v siteadmin


43 changes: 43 additions & 0 deletions bin/vexim2_new_alias
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 USER@DOMAIN DESTINATION"
exit 1
}


HOME=/root/
#set -x

EMAIL=$1
LOCALPART=`echo $EMAIL | cut -d@ -f1`
D=` echo $EMAIL | cut -d@ -f2`
DESTINATION=$2

[ -n "$LOCALPART" ] && [ -n "$D" ] && [ -n "$DESTINATION" ] || exitmessage "empty option"




mysql "$DB" -se "select domain from domains where domain='$D'" | grep . || exitmessage "domain $DOMAIN doesnt exist"

mysql "$DB" -se "insert into users (domain_id, localpart, username, crypt, uid, gid, smtp, pop, type, enabled, realname ) values
(
(select domain_id from domains where domain='$D'),
'$LOCALPART',
'$LOCALPART@$D',
'',
'`id -u vexim`',
'`id -g vexim`',
'$DESTINATION',
'/var/vmail/$D/$LOCALPART',
'local',
1,
'$LOCALPART'
)" && echo ADDED || exitmessage "error SQL query"

30 changes: 30 additions & 0 deletions bin/vexim2_new_alias_domain
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 ALIASDOMAIN DSTDOMAIN"
echo " where mail recieved for u@ALIASDOMAIN stored to u@DSTDOMAIN"
exit 1
}


HOME=/root/
#set -x

ALIASDOMAIN=$1
DSTDOMAIN=$2

[ -n "$ALIASDOMAIN" ] && [ -n "$DSTDOMAIN" ] || exitmessage "empty option"


DSTDOMAIN_ID=`mysql "$DB" -se "select domain_id from domains where domain='$DSTDOMAIN' " `

[ -n "$DSTDOMAIN_ID" ] || exitmessage "DSTDOMAIN doesnt exist"

mysql "$DB" -se "insert into domainalias (domain_id, alias) values
( $DSTDOMAIN_ID, '$ALIASDOMAIN')" && echo ADDED || exitmessage "error SQL query"

48 changes: 48 additions & 0 deletions bin/vexim2_new_catchall
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash
. /etc/vexim2.conf



function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 DOMAIN DESTINATION"
exit 1
}


HOME=/root/
#set -x

D=$1
DESTINATION=$2

[ -n "$D" ] && [ -n "$DESTINATION" ] || exitmessage "empty option"



mysql "$DB" -se "select domain from domains where domain='$D'" | grep -q . || exitmessage "domain doesnt exist"
mysql "$DB" -se "select username from users where username='*@$D' " | grep -q . && exitmessage "catchall already exist"

mysql "$DB" -se "insert into users (domain_id, localpart, username, crypt, uid, gid, smtp, pop, type, enabled, realname ) values
(
(select domain_id from domains where domain='$D'),
'*',
'*@$D',
'',
'`id -u mail`',
'`id -g mail`',
'$DESTINATION',
'$DESTINATION',
'catch',
1,
'CatchAll'
)" && echo ADDED || exitmessage "error SQL query"



#echo "Hello, new mailbox!" | mail -s "the 1st letter" "$LOCALPART@$D"




42 changes: 42 additions & 0 deletions bin/vexim2_new_domain
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
. /etc/vexim2.conf


function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 DOMAIN [postmasterPASSWORD]"
exit 1
}


#set -x
HOME=/root/

D=`echo $1 | perl -ne 's@[^\.\-\w\d]@@g; print lc'`
PASSWORD=$2

[ -n "$D" ] || exitmessage "empty option"




mysql "$DB" -se "select domain from domains where domain='$D'" | grep -q . && exitmessage "domain exists"

mysql "$DB" -se "insert into domains (domain, maildir, type, uid, gid, avscan, spamassassin ) values ('$D','/var/vmail/$D','local',
'`id -u vexim`',
'`id -g vexim`',
1,
1
);";



vexim2_new_user postmaster@$D $PASSWORD

mysql "$DB" -se "update users set admin=1 where username='postmaster@$D' ; "





74 changes: 74 additions & 0 deletions bin/vexim2_new_user
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
. /etc/vexim2.conf


function generatePassword
{

perl -e '
my $password;
my $_rand;
$password_length = 10;

my @chars = split(" ", "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9");
srand;

for (my $i=0; $i <= $password_length ;$i++) {
$password .= $chars[int rand scalar @chars];
}
print $password;
'

}


function exitmessage {
echo "ERROR: $1, exiting"
echo "usage:"
echo "$0 USER@DOMAIN [PASSWORD]"
exit 1
}


HOME=/root/
#set -x

EMAIL=$1
LOCALPART=`echo $EMAIL | cut -d@ -f1`
D=` echo $EMAIL | cut -d@ -f2`

PASSWORD=$2
[ -z "$PASSWORD" ] && PASSWORD=`generatePassword`

[ -n "$LOCALPART" ] && [ -n "$D" ] && [ -n "$PASSWORD" ] || exitmessage "empty option"

HASH=$( dovecot pw -s SHA512-CRYPT -p $PASSWORD | sed 's@{SHA512-CRYPT}@@' )

mysql "$DB" -se "select domain from domains where domain='$D'" | grep -q . || exitmessage "domain doesnt exist"

mysql "$DB" -se "insert into users (domain_id, localpart, username, crypt, uid, gid, smtp, pop, type, enabled, realname, on_avscan, on_spamassassin )
values
(
(select domain_id from domains where domain='$D'),
'$LOCALPART',
'$LOCALPART@$D',
'$HASH',
'`id -u vexim`',
'`id -g vexim`',
'/var/vmail/$D/$LOCALPART/Maildir',
'/var/vmail/$D/$LOCALPART',
'local',
1,
'$LOCALPART',
(select avscan from domains where domain='$D'),
(select spamassassin from domains where domain='$D')
)" && echo ADDED || exitmessage "error SQL query"



echo "Hello, new mailbox!" | mail -s "the 1st letter" "$LOCALPART@$D"


echo "$LOCALPART@$D $PASSWORD"