<abstract>This document details how to create a virtual mailhosting system based upon postfix, mysql, courier-imap, and cyrus-sasl. </abstract>

17

<author title="Editor">

18

<mail link="seather@scygro.za.net">Scygro</mail>

19

</author>

20

<author title="Editor">

21

<mail link="swift@gentoo.org">Sven Vermeulen</mail>

22

</author>

23

24

<abstract>

25

This document details how to create a virtual mailhosting system based upon

26

postfix, mysql, courier-imap, and cyrus-sasl.

27

</abstract>

28

17

<version>1.0.7</version>

29

<version>1.7</version>

18

<date>December 10, 2003</date>

30

<date>2008-09-28</date>

19

<!--

20

31

21

Contents

22

23

I. Introduction

24

II. Postfix Basics

25

III. Courier-imap

26

IV. Cyrus-sasl

27

V. SSL Certificates for Postfix and Apache

28

VI. Adding SSL and SASL support to Postfix

29

VII. MySQL

30

VIII. Apache and phpMyAdmin

31

IX. The vmail user

32

X. Configuring MySQL Authentication and vhosts

33

XI. Squirrelmail

34

XII. Mailman

35

XIII. Content Filtering and Anti-Virus

36

XIV. Wrap Up

37

XV. Troubleshooting

38

39

--><chapter>

32

<chapter>

40

<title>Introduction</title>

33

<title>Introduction</title>

41

<body>

34

<section>

42

<p>For most gentoo users, a simple mail client and fetchmail will do. However, if you're hosting a domain with your system, you'll need a full blown MTA (Mail Transfer Agent). And if you're hosting multiple domains, then you'll definitely need something more robust to handle all of the email for your users. This system was designed to be an elegant solution to that problem.</p>

43

<p>A virtual mail system needs to be able to handle email for numerous domains with multiple users over a variety of interfaces. This presents some issues that must be dealt with. For instance, what if you have two users on different domains that want the same user name? If you are providing imap access and smtp-auth, how do combine the various authentication daemons into a single system? How do you provide security for the numerous components that comprise the system? How do you manage it all?</p>

44

<p>This howto will show you how to set up with a mail system capable of handling mail for as many domains as your hardware can handle, supports virtual mail users that don't require shell accounts, has domain specific user names, can authenticate web, imap, smtp, and pop3 clients against a single database, utilizes ssl for transport layer security, has a web interface, can handle mailing lists for any domain on the machine, and is controlled by a nice, central and easy mysql database. </p>

45

<p>There are quite a variety of ways to go about setting up a virtual mailhosting system. With so may options, another may be the best choice for your specific needs. Consider investigating <uri>http://www.qmail.org/</uri> and <uri>http://www.exim.org/</uri> to explore your options. </p>

<p>Make sure to turn on the following USE variables in <path>/etc/make.conf</path> before compiling the packages: <c>USE=&quot;mysql imap libwww maildir sasl ssl&quot;</c>. Otherwise you will most likely have to recompile things to get the support you need for all the protocols. Further, it's a good idea to turn off any other mail and network variables, like ipv6.</p>

50

<impo>This howto was written for postfix-2.0.x. If you are using postfix &lt; 2 some of the variables in this document will be different. It is recommended that you upgrade. Some other packages included in this howto are version sensitive as well. You are advised to read the documentation included with packages if you run into issues with this.</impo>

51

<impo>This document uses apache-1.3.x. Apache-2 has been marked stable in portage. However there are still a number of issues with php integration. Until php support in apache-2.0.x is marked stable, this guide will continue to use the 1.3.x version.</impo>

52

<impo>You need a domain name to run a public mail server, or at least an MX record for a domain. Ideally you would have control of at least two domains to take advantage of your new virtual domain functionality.</impo>

53

<impo>Make sure <path>/etc/hostname</path> is set to the right hostname for your mail server. Verify your hostname is set correctly with <c>hostname</c>. Also verify that there are no conflicting entries in <path>/etc/hosts</path>.</impo>

54

<note>It is recommended that you read this entire document and familiarize yourself with all the steps before attempting the install. If you run into problems with any of the steps, check the troubleshooting guide at the end of this document. Also, not all the referenced packages are necessary, this set up is very flexible. For instance, if you do not desire a web interface, feel free to skip the squirrelmail section.</note>

55

</body>

35

<body>

36

37

<p>

38

For most Gentoo users, a simple mail client and fetchmail will do. However, if

<comment>(Just add the "-v" after the smtpd in the following line)</comment>

79

smtp inet n - n - - smtpd -v

155

smtp inet n - n - - smtpd -v

156

</pre>

80

157

81

<codenote>Just add the <c>-v</c> after the smtpd in the above line</codenote>

158

<p>

159

Next, edit <path>/etc/mail/aliases</path> to add your local aliases. There

160

should at least be an alias for root like: <c>root: your@email.address</c>.

82

</pre>

161

</p>

83

<p>Next, edit <path>/etc/mail/aliases</path> to add your local aliases. There should at least be an alias for root like: <c>root: your@email.address</c>.</p>

162

84

<pre caption="Starting postfix for the first time">

163

<pre caption="Starting postfix for the first time">

85

# <i>/usr/bin/newaliases</i>

164

# <i>/usr/bin/newaliases</i>

86

<codenote>This will install the new aliases. You only need to do this </codenote>

165

<comment>(This will install the new aliases. You only need to do this

87

<codenote>when you update or install aliases.</codenote>

166

when you update or install aliases.)</comment>

88

167

89

# <i>/etc/init.d/postfix start</i>

168

# <i>/etc/init.d/postfix start</i>

90

</pre>

169

</pre>

91

<p>Now that postfix is running, fire up your favorite console mail client and send yourself an email. I use <c>mutt</c> for all my console mail. Verify that postfix is delivering mail to local users, once that's done, we're on to the next step.</p>

170

92

<note>I strongly recommend that you verify this basic postfix setup is functioning before you progress to the next step of the howto. </note>

171

<p>

172

Now that postfix is running, fire up your favorite console mail client and send

173

yourself an email. I use <c>mutt</c> for all my console mail. Verify that

174

postfix is delivering mail to local users, once that's done, we're on to the

175

next step.

176

</p>

177

178

<note>

179

I strongly recommend that you verify this basic postfix setup is functioning

180

before you progress to the next step of the howto.

181

</note>

182

93

</body>

183

</body>

184

</section>

94

</chapter>

185

</chapter>

186

95

<chapter>

187

<chapter>

96

<title>Courier-imap</title>

188

<title>Courier-imap</title>

189

<section>

97

<body>

190

<body>

98

<pre caption="Install courier-imap" ># <i>emerge courier-imap</i>

191

192

<pre caption="Install courier-imap and courier-authlib">

193

# <i>emerge courier-imap courier-authlib</i>

99

</pre>

194

</pre>

195

100

<pre caption="Courier-imap configuration">

196

<pre caption="Courier-imap configuration">

101

# <i>cd /etc/courier-imap</i>

197

# <i>cd /etc/courier-imap</i>

102

<codenote>If you want to use the ssl capabilities of courier-imap or pop3, </codenote>

198

<comment>(If you want to use the ssl capabilities of courier-imap or pop3,

103

<codenote>you'll need to create certs for this purpose.</codenote>

199

you'll need to create certs for this purpose.

104

<codenote>This step is recommended. If you do not want to use ssl, skip this step.</codenote>

200

This step is recommended. If you do not want to use ssl, skip this step.)</comment>

<p>Start up your favorite mail client and verify that all connections you've started work for receiving and sending mail. Now that the basics work, we're going to do a whole bunch of stuff at once to get the rest of the system running. Again, please verify that what we've installed already works before progressing.</p>

216

217

<p>

218

Start up your favorite mail client and verify that all connections you've

219

started work for receiving and sending mail. Of course, you won't be able to log

220

on to any of the services because authentication hasn't been configured yet, but

221

it is wise to check if the connections themselves work or not.

222

</p>

223

224

<p>

225

Now that the basics work, we're going to do a whole bunch of stuff at once to

226

get the rest of the system running. Again, please verify that what we've

227

installed already works before progressing.

228

</p>

229

120

</body>

230

</body>

231

</section>

121

</chapter>

232

</chapter>

233

122

<chapter>

234

<chapter>

123

<title>Cyrus-sasl</title>

235

<title>Cyrus-sasl</title>

236

<section>

124

<body>

237

<body>

125

<p>Next we're going to install cyrus-sasl. Sasl is going to play the role of actually passing your auth variables to pam, which will in turn pass that information to mysql for authentication of smtp users. For this howto, we'll not even try to verify that sasl is working until mysql is set up and contains a test user. Which is fine since we'll be authenticating against mysql in the end anyway.</p>

238

126

<note>Now for some reason, sasl will not play nicely with pam against the shadow file. I banged my head against this problem for, well, a long time. If anyone knows why sasl will not auth against the shadow file in its current gentoo incarnation, please <mail link="ken@kickasskungfu.com" >email me</mail> as I'd love to hear a solution to this.</note>

239

<p>

240

Next we're going to install cyrus-sasl. Sasl is going to play the role of

241

actually passing your auth variables to courier-auth, which will in turn pass

242

that information to mysql for authentication of smtp users. For this howto,

243

we'll not even try to verify that sasl is working until mysql is set up and

244

contains a test user. Which is fine since we'll be authenticating against

245

mysql in the end anyway.

246

</p>

247

127

<pre caption="Configuring and installing the cyrus-sasl ebuild">

248

<pre caption="Configuring and installing the cyrus-sasl ebuild">

128

# <i>USE='-ldap -mysql' emerge cyrus-sasl</i>

249

# <i>emerge cyrus-sasl</i>

129

<codenote>We don't have ldap and we're not using sasl's mysql capabilities </codenote>

130

<codenote>so we need to turn them off for this build.</codenote>

131

</pre>

250

</pre>

251

252

<p>

132

<p>Next, edit <path>/usr/lib/sasl2/smtpd.conf</path>.</p>

253

Next, edit <path>/etc/sasl2/smtpd.conf</path>.

254

</p>

255

133

<pre caption="Starting sasl">

256

<pre caption="Starting sasl">

134

# <i>nano -w /usr/lib/sasl2/smtpd.conf</i>

257

# <i>nano -w /etc/sasl2/smtpd.conf</i>

258

mech_list: PLAIN LOGIN

135

pwcheck_method: saslauthd

259

pwcheck_method: saslauthd

136

mech_list: LOGIN PLAIN

260

# <i>nano -w /etc/conf.d/saslauthd</i>

137

<codenote>It's important to turn off auth mehtods we are not using.</codenote>

261

SASLAUTHD_OPTS="${SASLAUTH_MECH} -a rimap -r"

138

<codenote>They cause problems for some mail clients.</codenote>

262

SASLAUTHD_OPTS="${SASLAUTHD_OPTS} -O localhost"

139

# <i>/etc/init.d/saslauthd start</i>

263

# <i>/etc/init.d/saslauthd start</i>

140

</pre>

264

</pre>

265

141

</body>

266

</body>

267

</section>

142

</chapter>

268

</chapter>

269

143

<chapter>

270

<chapter>

144

<title>SSL Certs for Postfix and Apache</title>

271

<title>SSL Certs for Postfix and Apache</title>

272

<section>

145

<body>

273

<body>

274

275

<p>

146

<p>Next we're going to make a set of ssl certificates for postfix and apache.</p>

276

Next we're going to make a set of ssl certificates for postfix and apache.

147

<pre>

277

</p>

278

279

<pre caption="Making ssl certicates">

148

# <i>cd /etc/ssl/</i>

280

# <i>cd /etc/ssl/</i>

149

# <i>nano -w openssl.cnf</i>

281

# <i>nano -w openssl.cnf</i>

150

282

151

<codenote>Change the following default values for your domain:</codenote>

283

<comment>Change the following default values for your domain:</comment>

152

countryName_default

284

countryName_default

153

stateOrProvinceName_default

285

stateOrProvinceName_default

154

localityName_default

286

localityName_default

155

0.organizationName_default

287

0.organizationName_default

156

commonName_default

288

commonName_default

157

emailAddress_default.

289

emailAddress_default.

158

290

159

<codenote>If the variables are not already present, just add them in a sensible place.</codenote>

291

<comment>(If the variables are not already present, just add them in a sensible place.)</comment>

160

161

292

162

# <i>cd misc</i>

293

# <i>cd misc</i>

163

# <i>nano -w CA.pl</i>

294

# <i>./CA.pl -newreq-nodes</i>

164

<codenote>We need to add -nodes to the # create a certificate and</codenote>

165

<codenote>#create a certificate request code in order to let our new ssl</codenote>

166

<codenote>certs be loaded without a password. Otherwise when you</codenote>

<codenote>smtpd_tls_auth_only is commented out to ease testing the system. </codenote>

358

<comment>(smtpd_tls_auth_only is commented out to ease testing the system.

228

<codenote>You can turn this on later if you desire.</codenote>

359

You can turn this on later if you desire.)</comment>

229

360

230

# <i>postfix reload</i>

361

# <i>postfix reload</i>

231

</pre>

362

</pre>

363

364

<p>

232

<p>Now we're going to verify that the config's we added were picked up by postfix.</p>

365

Now we're going to verify that the config's we added were picked up by postfix.

366

For this we are going to use <c>telnet</c> (provided by for instance

367

<c>net-misc/netkit-telnetd</c>) although you can also use <c>nc</c> (provided by

368

<c>net-analyzer/netcat</c>):

369

</p>

370

233

<pre caption="Verifying sasl and tls support">

371

<pre caption="Verifying sasl and tls support">

234

# <i>telnet localhost 25</i>

372

# <i>telnet localhost 25</i>

235

373

236

Trying 127.0.0.1...

374

Trying 127.0.0.1...

237

Connected to localhost.

375

Connected to localhost.

238

Escape character is '^]'.

376

Escape character is '^]'.

…

…

249

250-XVERP

387

250-XVERP

250

250 8BITMIME

388

250 8BITMIME

251

<i>^]</i>

389

<i>^]</i>

252

telnet> <i>quit</i>

390

telnet> <i>quit</i>

253

</pre>

391

</pre>

254

<p>Verify that the above AUTH and STARTTLS lines now appear in your postfix install. As I said before, as it stands now AUTH will not work. that's because sasl will try to auth against it's sasldb, instead of the shadow file for some unknown reason, which we have not set up. So we're going to just plow through and set up mysql to hold all of our auth and virtual domain information.

392

393

<p>

394

Verify that the above AUTH and STARTTLS lines now appear in your postfix

395

install. As I said before, as it stands now AUTH will not work. that's because

396

sasl will try to auth against it's sasldb, instead of the shadow file for some

397

unknown reason, which we have not set up. So we're going to just plow through

398

and set up mysql to hold all of our auth and virtual domain information.

255

</p>

399

</p>

400

256

</body>

401

</body>

402

</section>

403

</chapter>

404

257

</chapter>

405

<chapter>

406

<title>The vmail user</title>

407

<section>

408

<body>

409

410

<p>

411

Before we set up our virtual mailhosting environment, we create a functional

412

user under which the virtual mailboxes will be hosted. For clarity's sake we

413

will call this <e>vmail</e>:

414

</p>

415

416

<pre caption="Adding the vmail user">

417

# <i>adduser -d /home/vmail -s /bin/false -m vmail</i>

418

</pre>

419

420

<p>

421

So now you've set up the vmail account. You can create multiple accounts if you

422

want (to keep some structure in your set of virtual mail accounts). The user id,

423

group id and home dirs are referenced in the MySQL tables.

424

</p>

425

426

<p>

427

Next to the user account we also need to create the location where the mailboxes

<codenote>Verify that the new mailsql user can connect to the mysql server.</codenote>

474

<comment>(Verify that the new mailsql user can connect to the mysql server.)</comment>

282

475

283

# <i>mysql -u mailsql -p mailsql</i>

476

# <i>mysql -u mailsql -p mailsql</i>

284

</pre>

477

</pre>

478

479

<p>

285

<p>Your new database has default values and tables set up for two domains. The following tables are included:

480

Your new database has default values and tables set up for two domains. The

481

following tables are included:

482

</p>

483

286

<ul>

484

<ul>

287

<li>alias - local email alias and mailman alias information.</li>

485

<li>alias - local email alias and mailman alias information.</li>

288

<li>relocated - relocated user email address maps</li>

486

<li>relocated - relocated user email address maps</li>

487

<li>

289

<li>transport - default mail transport information for all domains you are hosting</li>

488

transport - default mail transport information for all domains you are

489

hosting

490

</li>

290

<li>users - all user account information</li>

491

<li>users - all user account information</li>

291

<li>virtual - virtual domain email alias maps</li>

492

<li>virtual - virtual domain email alias maps</li>

292

</ul>

493

</ul>

293

</p>

494

294

<pre caption="alias table sample">

495

<pre caption="alias table sample">

295

id alias destination

496

id alias destination

296

1 root foo@bar.com

497

1 root foo@bar.com

297

2 postmaster foo@bar.com

498

2 postmaster foo@bar.com

298

</pre>

499

</pre>

500

299

<pre caption="user table sample">

501

<pre caption="user table sample">

300

<codenote>Line wrapped for clarity</codenote>

502

<comment>(Line wrapped for clarity.)</comment>

301

id email clear name uid gid homedir \

503

id email clear name uid gid homedir \

302

maildir quota postfix

504

maildir quota postfix

303

10 foo@virt-bar.org $password realname virtid virtid /home/vmail \

505

10 foo@virt-domain.com $password realname virtid virtid /home/vmail \

304

/home/vmail/virt-bar.org/foo/.maildir/ y

506

/home/vmail/virt-domain.com/foo/.maildir/ y

305

13 foo@bar.com $password realname localid localid /home/foo \

507

13 foo@bar.com $password realname localid localid /home/foo \

306

/home/foo/.maildir/ y

508

/home/foo/.maildir/ y

509

</pre>

510

511

<p>

512

The values of the <c>virtid</c> uid and gid should be those of the <c>vmail</c>

513

user and group.

307

</pre>

514

</p>

515

308

<pre caption="transport table sample">

516

<pre caption="transport table sample">

309

id domain destination

517

id domain destination

310

1 bar.com local:

518

1 bar.com local:

311

2 virt-bar.org virtual:

519

2 virt-domain.com virtual:

312

</pre>

520

</pre>

521

313

<pre caption="virtual table sample">

522

<pre caption="virtual table sample">

314

id email destination

523

id email destination

315

3 root@virt-bar.org other@email.address

524

3 root@virt-domain.com other@email.address

316

</pre>

525

</pre>

526

317

</body>

527

</body>

528

</section>

318

</chapter>

529

</chapter>

530

319

<chapter>

531

<chapter>

320

<title>Apache and phpMyAdmin</title>

532

<title>Apache and phpMyAdmin</title>

533

<section>

321

<body>

534

<body>

535

536

<p>

322

<p>Next we'll set up apache and add an interface to interact with the database more easily. </p>

537

Next we'll set up apache and add an interface to interact with the database

538

more easily.

539

</p>

540

323

<pre caption="Setting up apache and phpmyadmin">

541

<pre caption="Setting up apache and phpmyadmin">

324

# <i>emerge apache mod_php phpmyadmin</i>

542

# <i>emerge apache phpmyadmin</i>

543

</pre>

544

545

<p>

546

There are plenty of guides out there about how to set up apache with php,

547

including guides provided by the <uri link="/proj/en/php/">Gentoo PHP

548

Project</uri>. There are also numerous posts on

549

<uri>http://forums.gentoo.org</uri> detailing how to solve problems with the

550

installation. So, that said, we're not going to cover it here. Set up the

551

apache and php installs, then continue with this howto. Now, a word for the

552

wise: .htaccess the directory that you put phpmyadmin in. If you do not do this,

553

search engine spiders will come along and index the page which in turn will mean

554

that anyone will be able to find your phpmyadmin page via google and in turn be

555

able to come change your database however they want which is <e>BAD!</e> There

556

are many howtos on this including:

557

<uri>http://www.csoft.net/docs/micro/htaccess.html.en</uri>.

325

</pre>

558

</p>

326

<p>There are plenty of guides out there about how to set up apache with php. Like this one: <uri>http://www.linuxguruz.org/z.php?id=31</uri>. There are also numerous posts on <uri>http://forums.gentoo.org</uri> detailing how to solve problems with the installation (search for 'apache php'). So, that said, I'm not going to cover it here. Set up the apache and php installs, then continue with this howto. Now, a word for the wise: .htaccess the directory that you put phpmyadmin in. If you do not do this, search engine spiders will come along and index the page which in turn will mean that anyone will be able to find your phpmyadmin page via google and in turn be able to come change your database however they want which is <e>BAD!</e> There are many howtos on this including: <uri>http://docs.csoft.net/micro/black-htaccess.html</uri>.</p>

559

327

<p>Now we're going to install the Apache certificates we made previously. The Apache-SSL directives that you need to use the resulting cert are:

560

<p>

561

Now we're going to install the Apache certificates we made previously. The

562

Apache-SSL directives that you need to use the resulting cert are:

563

</p>

564

328

<ul>

565

<ul>

329

<li>SSLCertificateFile /path/to/certs/new.cert.cert</li>

566

<li>SSLCertificateFile /path/to/certs/new.cert.cert</li>

330

<li>SSLCertificateKeyFile /path/to/certs/new.cert.key</li>

567

<li>SSLCertificateKeyFile /path/to/certs/new.cert.key</li>

331

</ul>

568

</ul>

332

</p>

569

333

<pre caption="Install Apache SSL certificates">

570

<pre caption="Install Apache SSL certificates">

334

# <i>cp /etc/ssl/misc/new.cert.cert /etc/apache/conf/ssl/</i>

571

# <i>cp /etc/ssl/misc/new.cert.cert /etc/apache2/ssl/</i>

335

# <i>cp /etc/ssl/misc/new.cert.key /etc/apache/conf/ssl/</i>

572

# <i>cp /etc/ssl/misc/new.cert.key /etc/apache2/ssl/</i>

336

# <i>nano -w /etc/apache/conf/vhosts/ssl.default-vhost.conf</i>

573

# <i>cd /etc/apache2/vhosts.d</i>

574

<comment>(Check if you have an ssl-vhost template already.

575

Copy that one instead of the default_vhost if that is the case)</comment>

576

# <i>cp 00_default_vhost.conf ssl-vhost.conf</i>

577

# <i>nano -w ssl-vhost.conf</i>

578

337

<codenote>Change the following parameters</codenote>

579

<comment>(Change the following parameters)</comment>

580

NameVirtualHost host.domain.name:443

338

581

582

&lt;VirtualHost host.domain.name:443&gt;

339

ServerName host.domain.name

583

ServerName host.domain.name

340

ServerAdmin your@email.address

584

ServerAdmin your@email.address

585

586

DocumentRoot "/var/www/localhost/htdocs/phpmyadmin";

587

&lt;Directory "/var/www/localhost/htdocs/phpmyadmin"&gt;

588

...

589

&lt;/Directory&gt;

590

341

SSLCertificateFile /etc/apache/conf/ssl/new.cert.cert

591

SSLCertificateFile /etc/apache2/ssl/new.cert.cert

342

SSLCertificateKeyFile /etc/apache/conf/ssl/new.cert.key

592

SSLCertificateKeyFile /etc/apache2/ssl/new.cert.key

593

SSLEngine on

594

...

595

&lt;/VirtualHost&gt;

343

596

597

# <i>nano -w /etc/conf.d/apache2</i>

598

<comment>(Add -D SSL -D PHP5 to the APACHE2_OPTS)</comment>

599

344

# <i>/etc/init.d/apache restart</i>

600

# <i>/etc/init.d/apache2 restart</i>

345

</pre>

601

</pre>

346

<note>If you have an existing apache install, you'll likely have to perform a full server reboot to install your new certificates. Check your logs to verify apache restarted successfully.</note>

<p>Now enter the phpmyadmin page and browse the tables. You'll want to add in your local aliases, edit your user table to add a test user, and change your transport table to add information about your domains. The default values supplied with the dumpfile should be a sufficient guide to what values need to go where. Make sure that if you put information in the database that it is accurate. For instance, make sure the local users home dir exists and that the correct uid/gid values are in place. The maildirs should be created automatically by postfix when the user receives their first email. So, in general, it's a good idea to send a &quot;Welcome&quot; mail to a new user after you setup their account to make sure the .maildir gets created. </p>

622

623

<p>

624

Now enter the phpmyadmin page and browse the tables. You'll want to add in your

supplied with the dumpfile should be a sufficient guide to what values need to

628

go where. Make sure that if you put information in the database that it is

629

accurate. For instance, make sure the local user's home dir exists and that the

630

correct uid/gid values are in place. The maildirs should be created

631

automatically by postfix when the user receives their first email. So, in

632

general, it's a good idea to send a "Welcome" mail to a new user after you

633

setup their account to make sure the .maildir gets created.

634

</p>

635

361

</body>

636

</body>

362

</chapter>

637

</section>

363

<chapter>

638

</chapter>

364

<title>The vmail user</title>

639

365

<body>

366

<p>At this point you may be wondering what user and directory to use for virtual mail users, and rightly so. Let's set that up.</p>

367

<pre caption="Adding the vmail user" >

368

# <i>adduser -d /home/vmail -s /bin/false vmail</i>

369

# <i>uid=`cat /etc/passwd | grep vmail | cut -f 3 -d :`</i>

370

# <i>groupadd -g $uid vmail</i>

371

# <i>mkdir /home/vmail</i>

372

# <i>chown vmail. /home/vmail</i>

373

</pre>

374

<p>So now when you're setting up vmail accounts, use the vmail uid, gid, and homedir. When you're setting up local accounts, use that users uid, gid, and homedir. We've been meaning to create a php admin page for this setup but haven't gotten around to it yet, as phpmyadmin generally works fine for us.</p>

375

</body>

376

</chapter>

377

<chapter>

640

<chapter>

378

<title>Configuring MySQL Authentication and vhosts</title>

641

<title>Configuring MySQL Authentication and vhosts</title>

642

<section>

379

<body>

643

<body>

380

<p>Next we'll reconfigure our authentication to use the mailsql database in courier-imap and postfix. In all of the following examples, replace $password with the password you set for the mailsql mysql user.</p>

644

381

<pre>

645

<p>

382

# <i>emerge pam_mysql</i>

646

Next we'll reconfigure our authentication to use the mailsql database in

383

# <i>nano -w /etc/pam.d/imap</i>

647

courier-imap and postfix. In all of the following examples, replace

384

<codenote>Comment out the existing auth lines and add the following as shown.</codenote>

648

<c>$password</c> with the password you set for the mailsql mysql user.

<comment>(The domains listed by the mydestination should not be listed in

525

virt-bar.com,

791

the virtual_mailbox_domains parameter)</comment>

526

$other-virtual-domain.com

792

virtual_mailbox_domains = virt-domain.com, $other-virtual-domain.com

527

793

528

virtual_minimum_uid = 1000

794

virtual_minimum_uid = 1000

795

<comment>(Substitute $vmail-gid with the GID of the vmail group)</comment>

529

virtual_gid_maps = static:$vmail-gid

796

virtual_gid_maps = static:$vmail-gid

530

virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-maps.cf

797

virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-maps.cf

531

virtual_alias_maps = mysql:/etc/postfix/mysql-virtual.cf

798

virtual_alias_maps = mysql:/etc/postfix/mysql-virtual.cf

799

<comment>(Substitute $vmail-uid with the UID of the vmail user)</comment>

532

virtual_uid_maps = static:$vmail-uid

800

virtual_uid_maps = static:$vmail-uid

533

virtual_mailbox_base = /

801

virtual_mailbox_base = /

534

#virtual_mailbox_limit =

802

#virtual_mailbox_limit =

535

</pre>

803

</pre>

536

<p>As of Postfix 2.0.x, there were a number of significant changes over the 1.1.x release. Notably the transport, virtual-gid, and virtual-uid tables are no longer necessary. The tables are still included if you wish to use them.</p>

804

805

<p>

806

For security reasons you should change the permissions of the various

807

<path>/etc/mail/mysql-*.cf</path>:

808

</p>

809

810

<pre caption="Changing file permission">

811

# <i>chmod 640 /etc/postfix/mysql-*.cf</i>

812

# <i>chgrp postfix /etc/postfix/mysql-*.cf</i>

813

</pre>

814

815

<p>

816

As of Postfix 2.0.x, there were a number of significant changes over the 1.1.x

817

release. Notably the transport, virtual-gid, and virtual-uid tables are no

818

longer necessary. The tables are still included if you wish to use them.

819

</p>

820

821

<note>

537

<note>It is recommended tha you read VIRTUAL_README included with the postfix doc's for more information.</note>

822

It is recommended that you read VIRTUAL_README included with the postfix docs

823

for more information.

824

</note>

825

826

<pre caption="Make postfix reload its tables">

538

<pre># <i>postfix reload</i>

827

# <i>postfix reload</i>

828

</pre>

829

830

<p>

831

Now, if all went well, you should have a functioning mailhost. Users should be

832

able to authenticate against the sql database, using their full email address,

833

for pop3, imap, and smtp. I would highly suggest that you verify that

834

everything is working at this point. If you run into problems (with as many

835

things as this setup has going on, it's likely that you will) check the

836

troubleshooting section of this howto.

539

</pre>

837

</p>

540

<p>Now, if all went well, you should have a functioning mailhost. Users should be able to authenticate against the sql database, using their full email address, for pop3, imap, and smtp. I would highly suggest that you verify that everything is working at this point. If you run into problems (with as many things as this setup has going on, it's likely that you will) check the troubleshooting section of this howto.</p>

838

541

</body>

839

</body>

840

</section>

542

</chapter>

841

</chapter>

842

543

<chapter>

843

<chapter>

544

<title>Squirrelmail</title>

844

<title>Squirrelmail</title>

845

<section>

545

<body>

846

<body>

546

<pre>

847

848

<pre caption="Install squirrelmail">

547

# <i>emerge squirrelmail</i>

849

# <i>emerge squirrelmail</i>

548

<codenote>I like to add a link to the htdocs space for a shorter url.</codenote>

850

<comment>(Install squirrelmail to localhost so that it's accessed by http://localhost/mail)

<codenote>Now you should be able to login to squirrelmail, again - with your full email address,</codenote>

857

Now you should be able to login to squirrelmail, again - with your full email address,

555

<codenote>and use your new webmail setup.</codenote>

858

and use your new webmail setup.)</comment>

556

</pre>

859

</pre>

860

557

</body>

861

</body>

862

</section>

558

</chapter>

863

</chapter>

864

559

<chapter>

865

<chapter>

560

<title>Mailman</title>

866

<title>Mailman</title>

867

<section>

561

<body>

868

<body>

562

<p>Last step: mailman. The new version of mailman has very nice virtual domain support, which is why I use it, not to mention it's really a great package. To get this package installed and working correctly for virtual domains is going to require a bit of hacking. I really recommend reading all of the mailman documentation, including README.POSTFIX.gz, to understand what's being done here.</p>

869

563

<p>One further note, current versions of mailman install to /usr/local/mailman. If you're like me and wish to change the default install location, it can be overridden in the ebuild filoe by changing the INSTALLDIR variable.</p>

870

<p>

564

<pre caption="/usr/portage/net-mail/mailman/mailman-$ver.ebuild">

871

Last step: mailman. The new version of mailman has very nice virtual domain

565

# <i>nano -w /usr/portage/net-mail/mailman/mailman-$ver.ebuild</i>

872

support, which is why I use it, not to mention it's really a great package. To

566

MAILGID="280"

873

get this package installed and working correctly for virtual domains is going

567

<codenote>Set MAILGID to the mailman group instead of nobody</codenote>

874

to require a bit of hacking. I really recommend reading all of the mailman

568

<codenote>This is needed for postfix integration</codenote>

875

documentation, including README.POSTFIX.gz, to understand what's being done

876

here.

569

</pre>

877

</p>

570

<pre>

878

879

<pre caption="Install mailman">

571

# <i>emerge mailman</i>

880

# <i>emerge mailman</i>

572

<codenote>This package is currently masked as well, so you'll need to unmask it or give </codenote>

573

<codenote>emerge an explicit path to the ebuild. Once it's installed, follow the directions</codenote>

574

<codenote>in the README.gentoo.gz *except* do not add your aliases to /etc/mail/aliases.</codenote>

575

<codenote>We will instead be linking the entire alias db into postfix.</codenote>

576

577

# <i>zless /usr/share/doc/mailman-$ver/README.gentoo.gz</i>

578

</pre>

881

</pre>

882

579

<pre caption="Setting defaults: Mailman/Defaults.py">

883

<pre caption="Setting defaults: Mailman/Defaults.py">

580

# <i> nano -w /var/mailman/Mailman/Defaults.py</i>

884

# <i> nano -w /usr/local/mailman/Mailman/Defaults.py</i>

581

<codenote>Change the values below to reflect your primary domain, virtuals will be set next.</codenote>

885

<comment>(Change the values below to reflect your primary domain, virtuals will be set next.)</comment>

You should now be able to setup mailing lists for any domain on your box. Last

963

note on this, make sure you run all mailman commands as the user mailman (<c>su

964

mailman</c>) or else the permissions will be wrong and you'll have to fix them.

965

Read the mailman doc's for more information on setting up and managing mailman

966

lists.

646

</pre>

967

</p>

647

<p> You should now be able to setup mailing lists for any domain on your box. Last note on this, make sure you run all mailman commands as the user mailman (<c>su mailman</c>) or else the permissions will be wrong and you'll have to fix them. Read the mailman doc's for more information on setting up and managing mailman lists.</p>

968

648

</body>

969

</body>

970

</section>

649

</chapter>

971

</chapter>

972

650

<chapter>

973

<chapter>

651

<title>Content Filtering and Anti-Virus</title>

974

<title>Content Filtering and Anti-Virus</title>

652

<body><p>Coming soon...it would be done already but I need some perl help and testing to make it so. If you'd like to volunteer for that, please email me.</p></body>

<p>Ok, you're all set, edit <path>/etc/postfix/master.cf</path> and turn off verbose mode for production use. You'll probably also want to add the services to your startup routine to make sure everything comes back up on a reboot. Make sure to add all the services you're using - apache, mysql, saslauthd, postfix, courier-imapd, courier-imapd-ssl, courier-pop3d, and courier-pop3d-ssl are all up to your decision on what access you want to provide. I generally have all the services enabled.</p>

991

992

<p>

993

Ok, you're all set, edit <path>/etc/postfix/master.cf</path> and turn off

994

verbose mode for production use. You'll probably also want to add the services

995

to your startup routine to make sure everything comes back up on a reboot. Make

996

sure to add all the services you're using - apache, mysql, saslauthd, postfix,

997

courier-imapd, courier-imapd-ssl, courier-pop3d, and courier-pop3d-ssl are all

998

up to your decision on what access you want to provide. I generally have all

999

the services enabled.

1000

</p>

1001

658

<pre caption="Wrap up">

1002

<pre caption="Wrap up">

659

# <i>postfix reload</i>

1003

# <i>postfix reload</i>

660

# <i>rc-update add $service default</i>

1004

# <i>rc-update add $service default</i>

661

</pre>

1005

</pre>

1006

662

<p>

1007

<p>

663

<e>Have fun!</e>

1008

<e>Have fun!</e>

664

</p>

1009

</p>

1010

665

</body>

1011

</body>

1012

</section>

666

</chapter>

1013

</chapter>

1014

667

<chapter>

1015

<chapter>

668

<title>Troubleshooting</title>

1016

<title>Troubleshooting</title>

669

<section>

1017

<section>

670

<title>Introduction</title>

1018

<title>Introduction</title>

671

<body>

1019

<body>

672

<p>Troubleshooting: This is a short troubleshooting guide for the set up we've detailed how to install here. It is not exhaustive, but meant as a place to get you started in figuring out problems. With a complicated setup such as this, it's imperative that you narrow down the problem to the particular component that is malfunctioning. In general I do that by following a few steps. Start from the base of the system and work your way up, ruling out components that work along the way until you discover which component is having the problem.</p>

1020

1021

<p>

1022

Troubleshooting: This is a short troubleshooting guide for the set up we've

1023

detailed how to install here. It is not exhaustive, but meant as a place to get

1024

you started in figuring out problems. With a complicated setup such as this,

1025

it's imperative that you narrow down the problem to the particular component

1026

that is malfunctioning. In general I do that by following a few steps. Start

1027

from the base of the system and work your way up, ruling out components that

1028

work along the way until you discover which component is having the problem.

1029

</p>

1030

673

</body>

1031

</body>

674

</section>

675

<section>

1032

</section>

1033

<section>

676

<title>Step 1: Check your config files.</title>

1034

<title>Step 1: Check your config files</title>

677

<body>

1035

<body>

678

<p>Typos are killers, especially when dealing with authentication systems. Scan your config's and mailsql database for typo's. You can debug all you want, but if you're not passing the right information back and forth to your mail system, it's not going to work. If you make a change to a config file for a service, make sure you restart that service so that the config change gets picked up.</p>

1036

679

<pre>

1037

<p>

1038

Typos are killers, especially when dealing with authentication systems. Scan

1039

your config's and mailsql database for typo's. You can debug all you want, but

1040

if you're not passing the right information back and forth to your mail system,

1041

it's not going to work. If you make a change to a config file for a service,

1042

make sure you restart that service so that the config change gets picked up.

1043

</p>

1044

1045

<pre caption="How to restart a service">

680

# <i>/etc/init.d/service restart</i>

1046

# <i>/etc/init.d/service restart</i>

681

</pre>

1047

</pre>

1048

682

</body>

1049

</body>

683

</section>

1050

</section>

684

<section>

1051

<section>

685

<title>Step 2: Are all the necessary services actually running?</title>

1052

<title>Step 2: Are all the necessary services actually running?</title>

686

<body>

1053

<body>

687

<p>If it's not running, start it up. It's awful hard to debug a service that isn't running. Sometimes a service will act like it's started but still not function. Sometimes, when a bad config is used, or a bad transmission comes into a mail component, the service will hang and keep the port from being used by another process. Sometimes you can detect this with netstat. Or, if you've been at it awhile, just take a break and reboot your box in the meantime. That will clear out any hung services. Then you can come back fresh and try it again.</p>

1054

688

<pre>

1055

<p>

1056

If it's not running, start it up. It's awful hard to debug a service that isn't

1057

running. Sometimes a service will act like it's started but still not function.

1058

Sometimes, when a bad config is used, or a bad transmission comes into a mail

1059

component, the service will hang and keep the port from being used by another

1060

process. Sometimes you can detect this with netstat. Or, if you've been at it

1061

awhile, just take a break and reboot your box in the meantime. That will clear

1062

out any hung services. Then you can come back fresh and try it again.

1063

</p>

1064

1065

<pre caption="Checking the status of a service">

689

# <i>/etc/init.d/$service status</i>

1066

# <i>/etc/init.d/$service status</i>

690

# <i>netstat -a | grep $service (or $port)</i>

1067

# <i>netstat -a | grep $service (or $port)</i>

691

</pre>

1068

</pre>

1069

692

</body>

1070

</body>

693

</section>

1071

</section>

694

<section>

1072

<section>

695

<title>Step 3: Are all the service using the current config's?</title>

1073

<title>Step 3: Are all the service using the current config's?</title>

696

<body>

1074

<body>

697

<p>If you've recently made a change to a config file, restart that service to make sure it's using the current version. Some of the components will dump their current config's to you, like postfix.</p>

1075

698

<pre>

1076

<p>

1077

If you've recently made a change to a config file, restart that service to make

1078

sure it's using the current version. Some of the components will dump their

<p>Repeat after me, logs are my friend. My next troubleshooting stop is always the logs. Sometimes it's helpful to try a failed operation again then check the logs so that the error message is right at the bottom (or top depending on your logger) instead of buried in there somewhere. See if there is any information in your log that can help you diagnose the problem, or at the very least, figure out which component is having the problem.</p>

1094

710

<pre>

1095

<p>

1096

Repeat after me, logs are my friend. My next troubleshooting stop is always the

1097

logs. Sometimes it's helpful to try a failed operation again then check the

1098

logs so that the error message is right at the bottom (or top depending on your

1099

logger) instead of buried in there somewhere. See if there is any information

1100

in your log that can help you diagnose the problem, or at the very least,

You may also find the debug_peer parameters in main.cf helpful. Setting these

1113

will increase log output over just verbose mode.

715

</pre>

1114

</p>

716

<p>You may also find the debug_peer parameters in main.cf helpful. Setting these will increase log output over just verbose mode.</p>

1115

717

<pre caption="adding debug_peer support">

1116

<pre caption="adding debug_peer support">

718

# <i>nano -w /etc/postfix/main.cf</i>

1117

# <i>nano -w /etc/postfix/main.cf</i>

719

debug_peer_level = 5

1118

debug_peer_level = 5

720

debug_peer_list = $host.domain.name

1119

debug_peer_list = $host.domain.name

721

<codenote>Uncomment one of the suggested debugger</codenote>

1120

<comment>(Uncomment one of the suggested debugger

722

<codenote>commands as well.</codenote>

1121

commands as well.)</comment>

723

</pre>

1122

</pre>

1123

724

</body>

1124

</body>

725

</section>

726

<section>

1125

</section>

1126

<section>

727

<title>Step 5: Talk to the service itself.</title>

1127

<title>Step 5: Talk to the service itself</title>

728

<body>

1128

<body>

729

<p>SMTP, IMAP, and POP3 all respond to telnet sessions. As we've seen earlier when we verified postfix's config. Sometimes it's helpful to open a telnet session to the service itself and see what's happening.</p>

1129

730

<pre>

1130

<p>

1131

SMTP, IMAP, and POP3 all respond to telnet sessions. As we've seen earlier when

1132

we verified postfix's config. Sometimes it's helpful to open a telnet session

1133

to the service itself and see what's happening.

1134

</p>

1135

1136

<pre caption="Connect to a service with telnet">

731

# <i>telnet localhost $port</i>

1137

# <i>telnet localhost $port</i>

732

<codenote>SMTP is 25, IMAP is 143, POP3 is 110. You should receive at least an OK string,</codenote>

1138

<comment>(SMTP is 25, IMAP is 143, POP3 is 110. You should receive at least an OK string,

733

<codenote>letting you know that the service is running and ready to respond to requests.</codenote>

1139

letting you know that the service is running and ready to respond to requests.)</comment>

734

1140

735

Trying 127.0.0.1...

1141

Trying 127.0.0.1...

736

Connected to localhost.

1142

Connected to localhost.

737

Escape character is '^]'.

1143

Escape character is '^]'.

738

* OK Courier-IMAP ready. Copyright 1998-2002 Double Precision, Inc.

1144

* OK Courier-IMAP ready. Copyright 1998-2002 Double Precision, Inc.

739

</pre>

1145

</pre>

1146

740

</body>

1147

</body>

741

</section>

742

<section>

1148

</section>

1149

<section>

743

<title>Step 6: Sometimes only the big guns will give you the information you need: strace.</title>

1150

<title>Step 6: Sometimes only the big guns will give you the information you need: strace</title>

744

<body>

1151

<body>

745

<p>You should have this installed anyway. This is an invaluable tool for debugging software. You can start commands from the command line with strace and watch all the system calls as they happen. It often dumps a huge amount of information, so you'll either need to watch it realtime as you retry a failed transaction with the mail system, or dump the output to a file for review.</p>

1152

746

<pre>

1153

<p>

1154

You should have this installed anyway. This is an invaluable tool for debugging

1155

software. You can start commands from the command line with strace and watch

1156

all the system calls as they happen. It often dumps a huge amount of

1157

information, so you'll either need to watch it realtime as you retry a failed

1158

transaction with the mail system, or dump the output to a file for review.

1159

</p>

1160

1161

<pre caption="Using strace">

747

# <i>emerge strace</i>

1162

# <i>emerge strace</i>

748

# <i>strace $command</i>

1163

# <i>strace $command</i>

749

# <i>strace -p `ps -C $service -o pid=`</i>

1164

# <i>strace -p `ps -C $service -o pid=`</i>

750

</pre>

1165

</pre>

1166

751

</body>

1167

</body>

752

</section>

1168

</section>

753

<section>

1169

<section>

754

<title>Step 7: Research</title>

1170

<title>Step 7: Research</title>

755

<body>

1171

<body>

756

<p>Once you have the information, if you can diagnose and fix the problem, great! If not, you'll probably need to go digging on the net for information that will help you fix it. Here's a list of sites you can check to see if your error has already been resolved. There's also a really good howto on setting up smtp-auth which contains some great debugging ideas.</p>

1172

1173

<p>

1174

Once you have the information, if you can diagnose and fix the problem, great!

1175

If not, you'll probably need to go digging on the net for information that will

1176

help you fix it. Here's a list of sites you can check to see if your error has

1177

already been resolved. There's also a really good howto on setting up smtp-auth