co.de.mon.key

Weblog

Own mailserver - Debian 7 Wheezy VPS - Part 3 - database

| Comments

Let’s map the virtual-* postfix configuration into mysql database

Create database

1
mysqladmin -p create mailserver

Enter mysql root password

1
mysql -p mailserver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
GRANT SELECT ON mailserver.*
TO 'mailuser'@'127.0.0.1'
IDENTIFIED BY 'passw0rd';

CREATE TABLE `virtual_domains` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `virtual_users` (
  `id` int(11) NOT NULL auto_increment,
  `domain_id` int(11) NOT NULL,
  `password` varchar(32) NOT NULL,
  `email` varchar(100) NOT NULL,
  `path` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `virtual_aliases` (
  `id` int(11) NOT NULL auto_increment,
  `domain_id` int(11) NOT NULL,
  `source` varchar(100) NOT NULL,
  `destination` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
INSERT INTO `mailserver`.`virtual_domains` (
  `id` ,
  `name`
)
VALUES (
  '1', 'test.codemonkey.pl'
);

INSERT INTO `mailserver`.`virtual_users` (
  `id` ,
  `domain_id` ,
  `password` ,
  `email` ,
  `path`
)
VALUES (
  '1', '1', MD5( 'paSs' ) , 'malpka@test.codemonkey.pl', 'test.codemonkey.pl/malpka'
);

INSERT INTO `mailserver`.`virtual_aliases` (
  `id`,
  `domain_id`,
  `source`,
  `destination`
)
VALUES (
  '1', '1', 'malpka2@test.codemonkey.pl', 'malpka@test.codemonkey.pl'
);

Reconfigure postfix

Enable mysql configuration

1
apt-get install postfix-mysql

Configure mysql for virtual_mailbox_domains

1
2
3
4
5
6
7
8
9
10
cat << EOF > /etc/postfix/virtual_mailbox_domains_mysql
user = mailuser
password = passw0rd
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'
EOF

postconf -e virtual_mailbox_domains=mysql:/etc/postfix/virtual_mailbox_domains_mysql
postmap /etc/postfix/virtual_mailbox_domains_mysql

Configure mysql for virtual_mailbox_maps

1
2
3
4
5
6
7
8
9
10
cat << EOF > /etc/postfix/virtual_mailbox_maps_mysql
user = mailuser
password = passw0rd
hosts = 127.0.0.1
dbname = mailserver
query = SELECT path FROM virtual_users WHERE email='%s'
EOF

postconf -e virtual_mailbox_maps=mysql:/etc/postfix/virtual_mailbox_maps_mysql
postmap /etc/postfix/virtual_mailbox_maps_mysql

Default MBox file for the virtual mail is a concatenation of virtual_mailbox_base and the result of above query, so for our example it will be /var/vmail/test.codemonkey.pl/malpka.

Configure mysql for virtual_alias_maps

1
2
3
4
5
6
7
8
9
10
cat << EOF > /etc/postfix/virtual_alias_maps_mysql
user = mailuser
password = passw0rd
hosts = 127.0.0.1
dbname = mailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'
EOF

postconf -e virtual_alias_maps=mysql:/etc/postfix/virtual_alias_maps_mysql
postmap /etc/postfix/virtual_alias_maps_mysql

Final configuration

1
2
3
4
chgrp postfix /etc/postfix/virtual*mysql
chmod u=rw,g=r,o= /etc/postfix/virtual*mysql

postfix reload

Own mailserver - Debian 7 Wheezy VPS - Part 2 - virtual domains

| Comments

Add a domain to postfix configuration – temporary solution src

1
postfix -e mydestination="example.org, example.com, example.net"

Now local users receive mails from all domans, i.e. user malpka will receive mails from malpka@example.org, malpka@example.com and malpka@example.net. Mails are saved in /var/mail/malpka

Separate mail from UNIX-accounts

Add vmail directory and vmail user

1
2
3
4
5
mkdir -p /var/vmail
groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /var/vmail -m
chown -R vmail:vmail /var/vmail
chmod u+w /var/vmail

Reconfigure postfix to use virtual

Let’s add a mail subdomain

1
example.org   IN MX  1   example.com

Postfix reconfiguration

1
2
3
4
5
6
7
postconf -e virtual_mailbox_domains=example.org
postconf -e virtual_mailbox_base=/var/vmail
postconf -e virtual_mailbox_maps=hash:/etc/postfix/virtual_mailbox_maps
postconf -e virtual_minimum_uid=100
postconf -e virtual_uid_maps=static:5000
postconf -e virtual_gid_maps=static:5000
postconf -e virtual_alias_maps=hash:/etc/postfix/virtual_alias_maps
  • virtual_mailbox_domains – comma separated list of domains that are handled by postfix’ virtual domains mechanism. The rest (from mydestination) is handled as a local mail.
  • virtual_mailbox_base – base folder for virtual mailboxes. This is the prefix that is prepended to the items from virtual_mailbox_maps
  • virtual_mailbox_maps – location of mailboxes for accounts
  • virtual_minimum_uid – safety reasons
  • virtual_uid_maps, virtual_gid_maps – user/group for writing

Domain listed in virtual_mailbox_domains must not be present in mydestination

Mailbox maps

1
2
echo "malpka@example.org example.org/malpka" >> virtual_mailbox_maps
echo "@example.org example.org/catchall" >> virtual_mailbox_maps

If you would like to use Maildir format instead of MBox, just add slash (example.org/malpka/)

Aliases

1
echo "malpka2@example.org malpka" >> virtual_alias_maps

Hash configuration files

1
2
postmap virtual_alias_maps
postmap virtual_mailbox_maps

Create mailbox file and setup permission again.

1
2
3
4
5
touch /var/vmail/example.org/malpka
touch /var/vmail/example.org/catchall

chown -R vmail:vmail /var/vmail
chmod u+w /var/vmail

Reload postfix configuration

1
postfix reload

Test

Receive mail.

1
mutt -f /var/vmail/example.org/malpka

Remarks

  • Despite every email address has it’s own place (different MBox/Maildir) you can only access it from the local system (root or vmail user). Fix in Part 4 – dovecot.
  • the mails that you send from a local accounts will still be from $myorigin domain
  • MBox vs Maildir war
  • postfix virtual_* configuration can be moved to a database, when needed (more users)

Own mailserver - Debian 7 Wheezy VPS - Part 1 - basic mailserver

| Comments

Check / set up machine’s hostname

1
2
malpka@codemonkey:~$ hostname
codemonkey.pl

Set up DNS record

1
2
@  IN MX  1  codemonkey.pl
@  IN TXT "v=spf1 a mx ~all"

Check SMTP (25) port for outside availability

Install postfix

1
apt-get install postfix

Voila! ;)

Check if we have access to mail command

1
apt-get install mailutils

and send the mail (end with Ctrl+D)

Mail received, post reply

After posting the reply

Configure mail aliases

In /etc/aliases file we can add redirections between local users

1
2
3
codemonkey:~# tail -n 1 /etc/aliases
root: malpka
codemonkey:~# newaliases

Remarks

  • be aware of STMP relay security! SMTPD_ACCESS_README.html
  • works for any local system user
  • uses /var/mail/{username} as a default mailbox location and MBox as a format
  • mail servers like gmail will reject e-mails from our mailserver until the RevDNS and SPF record are properly configured.

HOWTO test local network speed

| Comments

I have a problem to determine where is my LAN’s bottleneck. Copying files between two computers should be faster. Let’s check it out.

To mesure the speed we will prepare a client-server connectionL

  1. Preparation

Install netcat on machines that will take part in the tests. On a OpenWRT router we can install it through a package manager.

opkg update opkg install bash netcat

  1. Server

nc -l -p 1122 > /dev/null

  1. Client

dd if=/dev/zero bs=10000 count=1 | nc 192.168.1.3 1122

  1. Results
1
2
3
4
malpka@micromalpki:~$ dd if=/dev/zero bs=100M count=1 | nc 192.168.1.3 1122
1+0 przeczytanych recordów
1+0 zapisanych recordów
skopiowane 104857600 bajtów (105 MB), 13,6733 s, 7,7 MB/s

Git force merge strategy for specific file

| Comments

In order to set a default strategy for a file(s) in git merge you need to:

Enable the ours merge driver

  • for current repository
1
git config merge.ours.driver true
  • globally
1
git config --global merge.ours.driver true

Tell git to use the merge strategy for a file

1
echo "solutionfolder/file.xml merge=ours" >> .git/info/attributes

As a result the next pull with conflicts will automagically use ours version.

Just remember that the file will not longer be marked as merge conflict.

inspiration

Git na apache2 przez davfs

| Comments

Szybkie repozytorium GIT przez HTTP w Debianie

Tworzymy podkatalog w katalogu VHosta

1
2
cd /var/www/domain.com
mkdir myrepo.git

Inicjacja repozytorium

1
2
cd myrepo.git
git --bare init

Ustawienie praw dostępu

1
chown -R www-data:www-data .

Dostosowanie gita pod http

1
git update-server-info

Włączenie davfs

1
a2enmod dav_fs

Dodanie konfiguracji davfs w /etc/apache2/sites-enabled/domain.com, najprostsza wersja bez autoryzacji:

1
2
3
4
<Location /myrepo.git>
    DAV on
    AuthType None
</Location>

lub trochę bardziej bezpieczna:

1
htpasswd /etc/apache2/passwords/passwd_git username
1
2
3
4
5
6
7
<Location /myrepo.git>
      DAV on
      AuthType Basic
      AuthName "Git"
      AuthUserFile /etc/apache2/passwords/passwd_git
      Require valid-user
</Location>

Restart apache

1
apache2 -k restart

Pierwszy test przez przeglądarkę: http://domain.com/myrepo.git

Rabat 10 zł na zamówienie w Plackowej Izbie

| Comments

Do końca maja ważny jest rabat 10 zł na zamówienie powyżej 25 zł w Plackowej Izbie na Zagórskiej w Kielcach w portalu pyszne.pl. Kod rabatowy przysługuje jedynie nowym klientom.

Jeśli powyższy byłby już wykorzystany to kolejne to:

  • ECMGLCNJ
  • HHLGNMHN

Oczywiście są też kody na inne punkty.

Sprawdzam zasięg ;)

Windows 8 (i nie tylko) za darmo

| Comments

Oprócz świetnych warunków licencyjnych Dreamspark dla studentów, Microsoft oferuje także możliwość przetestowania niektórych produktów. W prosty sposób można mieć legalny system operacyjny z pewnego źródła, na stosunkowo długi czas – od 90 nawet do 240 dni, z dostępem do aktualizacji.

Z zamieszczonych poniżej linków można pobrać obrazy iso. Posiadają one zintegrowany klucz, nie jest on wymagany podczas instalacji.

Proste wyliczanki w SQL Server - CTE

| Comments

Tylko pamiętajcie o średniku przed with! ;)

Odliczanie od 1 do 10.

1
2
3
4
5
6
7
;with cte as
(
  select 1 as x
  union all
  select x+1 from cte where x < 10
)
select * from cte

Wynik

1
2
3
4
5
6
7
8
9
10
11
12
x
-----------
1
2
3
4
5
6
7
8
9
10

Równie dobrze możemy przechodzić po datach: