Minor polish, again, read security(7)
[ikiwiki.git] / docs / handbook / Security / index.mdwn
1 # Security 
2
3 [[!toc levels=3]]
4
5 ## Synopsis 
6
7 This chapter will provide a basic introduction to system security concepts, some general good rules of thumb, and some advanced topics under DragonFly. A lot of the topics covered here can be applied to system and Internet security in general as well. The Internet is no longer a ***friendly*** place in which everyone wants to be your kind neighbor. Securing your system is imperative to protect your data, intellectual property, time, and much more from the hands of hackers and the like.
8
9 DragonFly provides an array of utilities and mechanisms to ensure the integrity and security of your system and network.
10
11 After reading this chapter, you will know:
12
13 * About the various crypt mechanisms available in DragonFly, such as DES and MD5.
14
15 * How to set up one-time password authentication.
16
17 * How to create firewalls using IPFW.
18
19 * How to configure IPsec and create a VPN between DragonFly/Windows® machines.
20
21 * How to configure and use  **OpenSSH** , DragonFly's SSH implementation.
22
23 Before reading this chapter, you should:
24
25 * Understand basic DragonFly and Internet concepts.
26 * Read [security(7)](https://leaf.dragonflybsd.org/cgi/web-man?command=security&section=7).
27
28 ## Securing DragonFly 
29
30  **Command vs. Protocol:**  Throughout this document, we will use  **bold**  text to refer to a command or application. This is used for instances such as ssh, since it is a protocol as well as command.
31
32 ## DES, MD5, and Crypt 
33
34 Every user on a UNIX® system has a password associated with their account. It seems obvious that these passwords need to be known only to the user and the actual operating system. In order to keep these passwords secret, they are encrypted with what is known as a ***one-way hash***, that is, they can only be easily encrypted but not decrypted. In other words, what we told you a moment ago was obvious is not even true: the operating system itself does not ***really*** know the password. It only knows the ***encrypted*** form of the password. The only way to get the ***plain-text*** password is by a brute force search of the space of possible passwords.
35
36 Unfortunately the only secure way to encrypt passwords when UNIX came into being was based on DES, the Data Encryption Standard. This was not such a problem for users resident in the US, but since the source code for DES could not be exported outside the US, DragonFly had to find a way to both comply with US law and retain compatibility with all the other UNIX variants that still used DES.
37
38 The solution was to divide up the encryption libraries so that US users could install the DES libraries and use DES but international users still had an encryption method that could be exported abroad. This is how DragonFly came to use MD5 as its default encryption method. MD5 is believed to be more secure than DES, so installing DES is offered primarily for compatibility reasons.
39
40 ### Recognizing Your Crypt Mechanism 
41
42 `libcrypt.a` provides a configurable password authentication hash library. Currently the library supports DES, MD5, Blowfish, SHA256, and SHA512 hash functions. By default DragonFly uses SHA256 to encrypt passwords.
43
44 It is pretty easy to identify which encryption method DragonFly is set up to use. Examining the encrypted passwords in the `/etc/master.passwd` file is one way. Passwords encrypted with the MD5 hash are longer than those encrypted with the DES hash and also begin with the characters `$1$`. Passwords starting with `$2a$` are encrypted with the Blowfish hash function. DES password strings do not have any particular identifying characteristics, but they are shorter than MD5 passwords, and are coded in a 64-character alphabet which does not include the `$` character, so a relatively short string which does not begin with a dollar sign is very likely a DES password.
45
46 The password format used for new passwords is controlled by the `passwd_format` login capability in `/etc/login.conf`, which takes values of `des`, `md5` or `blf`. See the [login.conf(5)](http://leaf.dragonflybsd.org/cgi/web-man?command#login.conf&section5) manual page for more information about login capabilities.
47
48 ## One-time Passwords 
49
50 S/Key is a one-time password scheme based on a one-way hash function. DragonFly uses the MD4 hash for compatibility but other systems have used MD5 and DES-MAC. S/Key ia part of the FreeBSD base system, and is also used on a growing number of other operating systems. S/Key is a registered trademark of Bell Communications Research, Inc.
51
52 There are three different sorts of passwords which we will discuss below. The first is your usual UNIX® style password; we will call this a ***UNIX password***. The second sort is the one-time password which is generated by the S/Key `key` program or the OPIE [opiekey(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#opiekey&section1) program and accepted by the `keyinit` or [opiepasswd(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=opiepasswd&section=1) programs and the login prompt; we will call this a ***one-time password***. The final sort of password is the secret password which you give to the `key`/`opiekey` programs (and sometimes the `keyinit`/`opiepasswd` programs) which it uses to generate one-time passwords; we will call it a ***secret password*** or just unqualified ***password***.
53
54 The secret password does not have anything to do with your UNIX password; they can be the same but this is not recommended. S/Key and OPIE secret passwords are not limited to eight characters like old UNIX passwords[(1)](#FTN.AEN8429), they can be as long as you like. Passwords of six or seven word long phrases are fairly common. For the most part, the S/Key or OPIE system operates completely independently of the UNIX password system.
55
56 Besides the password, there are two other pieces of data that are important to S/Key and OPIE. One is what is known as the ***seed*** or ***key***, consisting of two letters and five digits. The other is what is called the ***iteration count***, a number between 1 and 100. S/Key creates the one-time password by concatenating the seed and the secret password, then applying the MD4/MD5 hash as many times as specified by the iteration count and turning the result into six short English words. These six English words are your one-time password. The authentication system (primarily PAM) keeps track of the last one-time password used, and the user is authenticated if the hash of the user-provided password is equal to the previous password. Because a one-way hash is used it is impossible to generate future one-time passwords if a successfully used password is captured; the iteration count is decremented after each successful login to keep the user and the login program in sync. When the iteration count gets down to 1, S/Key and OPIE must be reinitialized.
57
58 There are three programs involved in each system which we will discuss below. The `key` and `opiekey` programs accept an iteration count, a seed, and a secret password, and generate a one-time password or a consecutive list of one-time passwords. The `keyinit` and `opiepasswd` programs are used to initialize S/Key and OPIE respectively, and to change passwords, iteration counts, or seeds; they take either a secret passphrase, or an iteration count, seed, and one-time password. The `keyinfo` and `opieinfo` programs examine the relevant credentials files (`/etc/skeykeys` or `/etc/opiekeys`) and print out the invoking user's current iteration count and seed.
59
60 There are four different sorts of operations we will cover. The first is using `keyinit` or `opiepasswd` over a secure connection to set up one-time-passwords for the first time, or to change your password or seed. The second operation is using `keyinit` or `opiepasswd` over an insecure connection, in conjunction with `key` or `opiekey` over a secure connection, to do the same. The third is using `key`/`opiekey` to log in over an insecure connection. The fourth is using `key` or `opiekey` to generate a number of keys which can be written down or printed out to carry with you when going to some location without secure connections to anywhere.
61
62 ### Secure Connection Initialization 
63
64 To initialize S/Key for the first time, change your password, or change your seed while logged in over a secure connection (e.g., on the console of a machine or via  **ssh** ), use the `keyinit` command without any parameters while logged in as yourself:
65
66     
67
68     % keyinit
69
70     Adding unfurl:
71
72     Reminder - Only use this method if you are directly connected.
73
74     If you are using telnet or rlogin exit with no password and use keyinit -s.
75
76     Enter secret password:
77
78     Again secret password:
79
80     
81
82     ID unfurl s/key is 99 to17757
83
84     DEFY CLUB PRO NASH LACE SOFT
85
86 For OPIE, `opiepasswd` is used instead:
87
88     
89
90     % opiepasswd -c
91
92     [grimreaper] ~ $ opiepasswd -f -c
93
94     Adding unfurl:
95
96     Only use this method from the console; NEVER from remote. If you are using
97
98     telnet, xterm, or a dial-in, type ^C now or exit with no password.
99
100     Then run opiepasswd without the -c parameter.
101
102     Using MD5 to compute responses.
103
104     Enter new secret pass phrase:
105
106     Again new secret pass phrase:
107
108     ID unfurl OTP key is 499 to4268
109
110     MOS MALL GOAT ARM AVID COED
111
112 At the Enter new secret pass phrase: or Enter secret password: prompts, you should enter a password or phrase. Remember, this is not the password that you will use to login with, this is used to generate your one-time login keys. The ***ID*** line gives the parameters of your particular instance: your login name, the iteration count, and seed. When logging in the system will remember these parameters and present them back to you so you do not have to remember them. The last line gives the particular one-time password which corresponds to those parameters and your secret password; if you were to re-login immediately, this one-time password is the one you would use.
113
114 ### Insecure Connection Initialization 
115
116 To initialize or change your secret password over an insecure connection, you will need to already have a secure connection to some place where you can run `key` or `opiekey`; this might be in the form of a desk accessory on a Macintosh®, or a shell prompt on a machine you trust. You will also need to make up an iteration count (100 is probably a good value), and you may make up your own seed or use a randomly-generated one. Over on the insecure connection (to the machine you are initializing), use the `keyinit -s` command:
117
118     
119
120     % keyinit -s
121
122     Updating unfurl:
123
124     Old key: to17758
125
126     Reminder you need the 6 English words from the key command.
127
128     Enter sequence count from 1 to 9999: 100
129
130     Enter new key [default to17759]:
131
132     s/key 100 to 17759
133
134     s/key access password:
135
136     s/key access password:CURE MIKE BANE HIM RACY GORE
137
138 For OPIE, you need to use `opiepasswd`:
139
140     
141
142     % opiepasswd
143
144     
145
146     Updating unfurl:
147
148     You need the response from an OTP generator.
149
150     Old secret pass phrase:
151
152             otp-md5 498 to4268 ext
153
154             Response: GAME GAG WELT OUT DOWN CHAT
155
156     New secret pass phrase:
157
158             otp-md5 499 to4269
159
160             Response: LINE PAP MILK NELL BUOY TROY
161
162     
163
164     ID mark OTP key is 499 gr4269
165
166     LINE PAP MILK NELL BUOY TROY
167
168 To accept the default seed (which the `keyinit` program confusingly calls a `key`), press  **Return** . Then before entering an access password, move over to your secure connection or S/Key desk accessory, and give it the same parameters:
169
170     
171
172     % key 100 to17759
173
174     Reminder - Do not use this program while logged in via telnet or rlogin.
175
176     Enter secret password: <secret password>
177
178     CURE MIKE BANE HIM RACY GORE
179
180 Or for OPIE:
181
182     
183
184     % opiekey 498 to4268
185
186     Using the MD5 algorithm to compute response.
187
188     Reminder: Don't use opiekey from telnet or dial-in sessions.
189
190     Enter secret pass phrase:
191
192     GAME GAG WELT OUT DOWN CHAT
193
194 Now switch back over to the insecure connection, and copy the one-time password generated over to the relevant program.
195
196 ### Generating a Single One-time Password 
197
198 Once you have initialized S/Key, when you login you will be presented with a prompt like this:
199
200     
201
202     % telnet example.com
203
204     Trying 10.0.0.1...
205
206     Connected to example.com
207
208     Escape character is '^]'.
209
210     
211
212     DragonFly/i386 (example.com) (ttypa)
213
214     
215
216     login: <username>
217
218     s/key 97 fw13894
219
220     Password:
221
222 Or for OPIE:
223
224     
225
226     % telnet example.com
227
228     Trying 10.0.0.1...
229
230     Connected to example.com
231
232     Escape character is '^]'.
233
234     
235
236     DragonFly/i386 (example.com) (ttypa)
237
238     
239
240     login: <username>
241
242     otp-md5 498 gr4269 ext
243
244     Password:
245
246 As a side note, the S/Key and OPIE prompts have a useful feature (not shown here): if you press  **Return**  at the password prompt, the prompter will turn echo on, so you can see what you are typing. This can be extremely useful if you are attempting to type in a password by hand, such as from a printout.
247
248 At this point you need to generate your one-time password to answer this login prompt. This must be done on a trusted system that you can run `key` or `opiekey` on. (There are versions of these for DOS, Windows® and Mac OS® as well.) They need both the iteration count and the seed as command line options. You can cut-and-paste these right from the login prompt on the machine that you are logging in to.
249
250 On the trusted system:
251
252     
253
254     % key 97 fw13894
255
256     Reminder - Do not use this program while logged in via telnet or rlogin.
257
258     Enter secret password:
259
260     WELD LIP ACTS ENDS ME HAAG
261
262 For OPIE:
263
264     
265
266     % opiekey 498 to4268
267
268     Using the MD5 algorithm to compute response.
269
270     Reminder: Don't use opiekey from telnet or dial-in sessions.
271
272     Enter secret pass phrase:
273
274     GAME GAG WELT OUT DOWN CHAT
275
276 Now that you have your one-time password you can continue logging in:
277
278     
279
280     login: <username>
281
282     s/key 97 fw13894
283
284     Password: <return to enable echo>
285
286     s/key 97 fw13894
287
288     Password [echo on]: WELD LIP ACTS ENDS ME HAAG
289
290     Last login: Tue Mar 21 11:56:41 from 10.0.0.2 ...
291
292 ### Generating Multiple One-time Passwords 
293
294 Sometimes you have to go places where you do not have access to a trusted machine or secure connection. In this case, it is possible to use the `key` and `opiekey` commands to generate a number of one-time passwords beforehand to be printed out and taken with you. For example:
295
296     
297
298     % key -n 5 30 zz99999
299
300     Reminder - Do not use this program while logged in via telnet or rlogin.
301
302     Enter secret password: <secret password>
303
304     26: SODA RUDE LEA LIND BUDD SILT
305
306     27: JILT SPY DUTY GLOW COWL ROT
307
308     28: THEM OW COLA RUNT BONG SCOT
309
310     29: COT MASH BARR BRIM NAN FLAG
311
312     30: CAN KNEE CAST NAME FOLK BILK
313
314 Or for OPIE:
315
316     
317
318     % opiekey -n 5 30 zz99999
319
320     Using the MD5 algorithm to compute response.
321
322     Reminder: Don't use opiekey from telnet or dial-in sessions.
323
324     Enter secret pass phrase: <secret password>
325
326     26: JOAN BORE FOSS DES NAY QUIT
327
328     27: LATE BIAS SLAY FOLK MUCH TRIG
329
330     28: SALT TIN ANTI LOON NEAL USE
331
332     29: RIO ODIN GO BYE FURY TIC
333
334     30: GREW JIVE SAN GIRD BOIL PHI
335
336 The `-n 5` requests five keys in sequence, the `30` specifies what the last iteration number should be. Note that these are printed out in ***reverse*** order of eventual use. If you are really paranoid, you might want to write the results down by hand; otherwise you can cut-and-paste into `lpr`. Note that each line shows both the iteration count and the one-time password; you may still find it handy to scratch off passwords as you use them.
337
338 ### Restricting Use of UNIX® Passwords 
339
340 S/Key can place restrictions on the use of UNIX passwords based on the host name, user name, terminal port, or IP address of a login session. These restrictions can be found in the configuration file `/etc/skey.access`. The [skey.access(5)](http://leaf.dragonflybsd.org/cgi/web-man?command#skey.access&section5) manual page has more information on the complete format of the file and also details some security cautions to be aware of before depending on this file for security.
341
342 If there is no `/etc/skey.access` file (this is the default), then all users will be allowed to use UNIX passwords. If the file exists, however, then all users will be required to use S/Key unless explicitly permitted to do otherwise by configuration statements in the `skey.access` file. In all cases, UNIX passwords are permitted on the console.
343
344 Here is a sample `skey.access` configuration file which illustrates the three most common sorts of configuration statements:
345
346     
347
348     permit internet 192.168.0.0 255.255.0.0
349
350     permit user fnord
351
352     permit port ttyd0
353
354 The first line (`permit internet`) allows users whose IP source address (which is vulnerable to spoofing) matches the specified value and mask, to use UNIX passwords. This should not be considered a security mechanism, but rather, a means to remind authorized users that they are using an insecure network and need to use S/Key for authentication.
355
356 The second line (`permit user`) allows the specified username, in this case `fnord`, to use UNIX passwords at any time. Generally speaking, this should only be used for people who are either unable to use the `key` program, like those with dumb terminals, or those who are uneducable.
357
358 The third line (`permit port`) allows all users logging in on the specified terminal line to use UNIX passwords; this would be used for dial-ups.
359
360 Here is a sample `opieaccess` file:
361
362     
363
364     permit 192.168.0.0 255.255.0.0
365
366 This line allows users whose IP source address (which is vulnerable to spoofing) matches the specified value and mask, to use UNIX passwords at any time.
367
368 If no rules in `opieaccess` are matched, the default is to deny non-OPIE logins.
369
370 #### Notes 
371
372 [[!table  data="""
373 |<tablestyle="width:100%"> [one-time-passwords.html#AEN8429 (1)] | Under DragonFly the standard login password may be up to 128 characters in length. |
374
375 """]]
376
377 CategoryHandbook
378
379 CategoryHandbook-security
380
381 ## Firewalls 
382
383 Firewalls are an area of increasing interest for people who are connected to the Internet, and are even finding applications on private networks to provide enhanced security. This section will hopefully explain what firewalls are, how to use them, and how to use the facilities provided in the DragonFly kernel to implement them.
384
385  **Note:** People often think that having a firewall between your internal network and the ***Big Bad Internet*** will solve all your security problems. It may help, but a poorly set up firewall system is more of a security risk than not having one at all. A firewall can add another layer of security to your systems, but it cannot stop a really determined cracker from penetrating your internal network. If you let internal security lapse because you believe your firewall to be impenetrable, you have just made the crackers job that much easier.
386
387 ### What Is a Firewall? 
388
389 There are currently two distinct types of firewalls in common use on the Internet today. The first type is more properly called a ***packet filtering router***. This type of firewall utilizes a multi-homed machine and a set of rules to determine whether to forward or block individual packets. A multi-homed machine is simply a device with multiple network interfaces. The second type, known as a ***proxy server***, relies on daemons to provide authentication and to forward packets, possibly on a multi-homed machine which has kernel packet forwarding disabled.
390
391 Sometimes sites combine the two types of firewalls, so that only a certain machine (known as a ***bastion host***) is allowed to send packets through a packet filtering router onto an internal network. Proxy services are run on the bastion host, which are generally more secure than normal authentication mechanisms.
392
393 DragonFly comes with a kernel packet filter (known as IPFW), which is what the rest of this section will concentrate on. Proxy servers can be built on DragonFly from third party software, but there is such a variety of proxy servers available that it would be impossible to cover them in this section.
394
395 #### Packet Filtering Routers 
396
397 A router is a machine which forwards packets between two or more networks. A packet filtering router is programmed to compare each packet to a list of rules before deciding if it should be forwarded or not. Most modern IP routing software includes packet filtering functionality that defaults to forwarding all packets. To enable the filters, you need to define a set of rules.
398
399 To decide whether a packet should be passed on, the firewall looks through its set of rules for a rule which matches the contents of the packet's headers. Once a match is found, the rule action is obeyed. The rule action could be to drop the packet, to forward the packet, or even to send an ICMP message back to the originator. Only the first match counts, as the rules are searched in order. Hence, the list of rules can be referred to as a ***rule chain***.
400
401 The packet-matching criteria varies depending on the software used, but typically you can specify rules which depend on the source IP address of the packet, the destination IP address, the source port number, the destination port number (for protocols which support ports), or even the packet type (UDP, TCP, ICMP, etc).
402
403 #### Proxy Servers 
404
405 Proxy servers are machines which have had the normal system daemons ( **telnetd** ,  **ftpd** , etc) replaced with special servers. These servers are called ***proxy servers***, as they normally only allow onward connections to be made. This enables you to run (for example) a proxy  **telnet**  server on your firewall host, and people can  **telnet**  in to your firewall from the outside, go through some authentication mechanism, and then gain access to the internal network (alternatively, proxy servers can be used for signals coming from the internal network and heading out).
406
407 Proxy servers are normally more secure than normal servers, and often have a wider variety of authentication mechanisms available, including ***one-shot*** password systems so that even if someone manages to discover what password you used, they will not be able to use it to gain access to your systems as the password expires immediately after the first use. As they do not actually give users access to the host machine, it becomes a lot more difficult for someone to install backdoors around your security system.
408
409 Proxy servers often have ways of restricting access further, so that only certain hosts can gain access to the servers. Most will also allow the administrator to specify which users can talk to which destination machines. Again, what facilities are available depends largely on what proxy software you choose.
410
411 ### Firewall options in DragonFlyBSD
412
413 DragonFlyBSD inherited the IPFW firewall (versions 1 and 2) when it forked from FreeBSD. Pretty soon after though, we imported the new pf packet filter that the OpenBSD developers created from scratch. It is a cleaner code base and is now the recommended solution for firewalling DragonFly. Keep in mind that the PF version in DragonFly is not in sync with OpenBSD's PF code. We have not yet incorporated the improvements made in PF over the last few years, but we have some improvements of our own. IPFW is still and will remain supported for the foreseeable future; it has some features not yet available in PF.
414
415 A copy of the OpenBSD PF user's guide corresponding to the version of PF in DragonFly can be found in [[PFUsersGuide]].
416
417 #### What Does IPFW Allow Me to Do? 
418
419 IPFW, the software supplied with DragonFly, is a packet filtering and accounting system which resides in the kernel, and has a user-land control utility, [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8). Together, they allow you to define and query the rules used by the kernel in its routing decisions.
420
421 There are two related parts to IPFW. The firewall section performs packet filtering. There is also an IP accounting section which tracks usage of the router, based on rules similar to those used in the firewall section. This allows the administrator to monitor how much traffic the router is getting from a certain machine, or how much WWW traffic it is forwarding, for example.
422
423 As a result of the way that IPFW is designed, you can use IPFW on non-router machines to perform packet filtering on incoming and outgoing connections. This is a special case of the more general use of IPFW, and the same commands and techniques should be used in this situation.
424
425 #### Enabling IPFW on DragonFly 
426
427 As the main part of the IPFW system lives in the kernel, you will need to add one or more options to your kernel configuration file, depending on what facilities you want, and recompile your kernel. See "Reconfiguring your Kernel" ([kernelconfig.html Chapter 9]) for more details on how to recompile your kernel.
428
429  **Warning:** IPFW defaults to a policy of `deny ip from any to any`. If you do not add other rules during startup to allow access, ***you will lock yourself out*** of the server upon rebooting into a firewall-enabled kernel. We suggest that you set `firewall_type=open` in your `/etc/rc.conf` file when first enabling this feature, then refining the firewall rules in `/etc/rc.firewall` after you have tested that the new kernel feature works properly. To be on the safe side, you may wish to consider performing the initial firewall configuration from the local console rather than via  **ssh** . Another option is to build a kernel using both the `IPFIREWALL` and `IPFIREWALL_DEFAULT_TO_ACCEPT` options. This will change the default rule of IPFW to `allow ip from any to any` and avoid the possibility of a lockout.
430
431 There are currently four kernel configuration options relevant to IPFW:
432
433 `options IPFIREWALL`:: Compiles into the kernel the code for packet filtering.`options IPFIREWALL_VERBOSE`:: Enables code to allow logging of packets through [syslogd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#syslogd&section8). Without this option, even if you specify that packets should be logged in the filter rules, nothing will happen.`options IPFIREWALL_VERBOSE_LIMIT=10`:: Limits the number of packets logged through [syslogd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=syslogd&section=8) on a per entry basis. You may wish to use this option in hostile environments in which you want to log firewall activity, but do not want to be open to a denial of service attack via syslog flooding.
434
435 When a chain entry reaches the packet limit specified, logging is turned off for that particular entry. To resume logging, you will need to reset the associated counter using the [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) utility:
436
437     
438
439     # ipfw zero 4500
440
441 Where 4500 is the chain entry you wish to continue logging.`options IPFIREWALL_DEFAULT_TO_ACCEPT`:: This changes the default rule action from ***deny*** to ***allow***. This avoids the possibility of locking yourself out if you happen to boot a kernel with `IPFIREWALL` support but have not configured your firewall yet. It is also very useful if you often use [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) as a filter for specific problems as they arise. Use with care though, as this opens up the firewall and changes the way it works.
442
443 #### Configuring IPFW 
444
445 The configuration of the IPFW software is done through the [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) utility. The syntax for this command looks quite complicated, but it is relatively simple once you understand its structure.
446
447 There are currently four different command categories used by the utility: addition/deletion, listing, flushing, and clearing. Addition/deletion is used to build the rules that control how packets are accepted, rejected, and logged. Listing is used to examine the contents of your rule set (otherwise known as the chain) and packet counters (accounting). Flushing is used to remove all entries from the chain. Clearing is used to zero out one or more accounting entries.
448
449 ##### Altering the IPFW Rules 
450
451 The syntax for this form of the command is:
452
453 `ipfw` [-N] command [index] action [log] protocol addresses [options]
454
455 There is one valid flag when using this form of the command:
456
457 -N:: Resolve addresses and service names in output.
458
459 The ***command*** given can be shortened to the shortest unique form. The valid ***commands*** are:
460
461 add:: Add an entry to the firewall/accounting rule listdelete:: Delete an entry from the firewall/accounting rule list
462
463 Previous versions of IPFW used separate firewall and accounting entries. The present version provides packet accounting with each firewall entry.
464
465 If an ***index*** value is supplied, it is used to place the entry at a specific point in the chain. Otherwise, the entry is placed at the end of the chain at an index 100 greater than the last chain entry (this does not include the default policy, rule 65535, deny).
466
467 The `log` option causes matching rules to be output to the system console if the kernel was compiled with `IPFIREWALL_VERBOSE`.
468
469 Valid ***actions*** are:
470
471 reject:: Drop the packet, and send an ICMP host or port unreachable (as appropriate) packet to the source.allow:: Pass the packet on as normal. (aliases: `pass`, `permit`, and `accept`)deny:: Drop the packet. The source is not notified via an ICMP message (thus it appears that the packet never arrived at the destination).count:: Update packet counters but do not allow/deny the packet based on this rule. The search continues with the next chain entry.
472
473 Each ***action*** will be recognized by the shortest unambiguous prefix.
474
475 The ***protocols*** which can be specified are:
476
477 all:: Matches any IP packeticmp:: Matches ICMP packetstcp:: Matches TCP packetsudp:: Matches UDP packets
478
479 The ***address*** specification is:
480
481 from `***address/mask***` [`***port***`] to `***address/mask***` [`***port***`] [via `***interface***`]
482
483 You can only specify `***port***` in conjunction with ***protocols*** which support ports (UDP and TCP).
484
485 The `via` is optional and may specify the IP address or domain name of a local IP interface, or an interface name (e.g. `ed0`) to match only packets coming through this interface. Interface unit numbers can be specified with an optional wildcard. For example, `ppp*` would match all kernel PPP interfaces.
486
487 The syntax used to specify an `***address/mask***` is:
488
489     
490
491     `***address***`
492
493  or
494
495     
496
497     `***address***`/`***mask-bits***`
498
499  or
500
501     
502
503     `***address***`:`***mask-pattern***`
504
505 A valid hostname may be specified in place of the IP address. ' **mask-bits** ' is a decimal number representing how many bits in the address mask should be set. e.g. specifying `192.216.222.1/24` will create a mask which will allow any address in a class C subnet (in this case, `192.216.222`) to be matched. ' **mask-pattern** ' is an IP address which will be logically AND'ed with the address given. The keyword `any` may be used to specify ***any IP address***.
506
507 The port numbers to be blocked are specified as:
508
509 `***port***` [,`***port***` [,`***port***` [...]]]
510
511  to specify either a single port or a list of ports, or
512
513 `***port***`-`***port***`
514
515  to specify a range of ports. You may also combine a single range with a list, but the range must always be specified first.
516
517 The ***options*** available are:
518
519 frag:: Matches if the packet is not the first fragment of the datagram.in:: Matches if the packet is on the way in.out:: Matches if the packet is on the way out.ipoptions `***spec***`:: Matches if the IP header contains the comma separated list of options specified in `***spec***`. The supported IP options are: `ssrr` (strict source route), `lsrr` (loose source route), `rr` (record packet route), and `ts` (time stamp). The absence of a particular option may be specified with a leading `!`.established:: Matches if the packet is part of an already established TCP connection (i.e. it has the RST or ACK bits set). You can optimize the performance of the firewall by placing ***established*** rules early in the chain.setup:: Matches if the packet is an attempt to establish a TCP connection (the SYN bit is set but the ACK bit is not).tcpflags `***flags***`:: Matches if the TCP header contains the comma separated list of `***flags***`. The supported flags are `fin`, `syn`, `rst`, `psh`, `ack`, and `urg`. The absence of a particular flag may be indicated by a leading `!`.icmptypes `***types***`:: Matches if the ICMP type is present in the list `***types***`. The list may be specified as any combination of ranges and/or individual types separated by commas. Commonly used ICMP types are: `0` echo reply (ping reply), `3` destination unreachable, `5` redirect, `8` echo request (ping request), and `11` time exceeded (used to indicate TTL expiration as with [traceroute(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#traceroute&section8)).
520
521 ##### Listing the IPFW Rules 
522
523 The syntax for this form of the command is:
524
525 `ipfw` [-a] [-c] [-d] [-e] [-t] [-N] [-S] list
526
527 There are seven valid flags when using this form of the command:
528
529 -a:: While listing, show counter values. This option is the only way to see accounting counters.-c:: List rules in compact form.-d:: Show dynamic rules in addition to static rules.-e:: If `-d` was specified, also show expired dynamic rules.-t:: Display the last match times for each chain entry. The time listing is incompatible with the input syntax used by the [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) utility.-N:: Attempt to resolve given addresses and service names.-S:: Show the set each rule belongs to. If this flag is not specified, disabled rules will not be listed.
530
531 ##### Flushing the IPFW Rules 
532
533 The syntax for flushing the chain is:
534
535 `ipfw` flush
536
537 This causes all entries in the firewall chain to be removed except the fixed default policy enforced by the kernel (index 65535). Use caution when flushing rules; the default deny policy will leave your system cut off from the network until allow entries are added to the chain.
538
539 ##### Clearing the IPFW Packet Counters 
540
541 The syntax for clearing one or more packet counters is:
542
543 `ipfw` zero [`***index***`]
544
545 When used without an `***index***` argument, all packet counters are cleared. If an `***index***` is supplied, the clearing operation only affects a specific chain entry.
546
547 #### Example Commands for  **ipfw**  
548
549 This command will deny all packets from the host `evil.crackers.org` to the telnet port of the host `nice.people.org`:
550
551     
552
553     # ipfw add deny tcp from evil.crackers.org to nice.people.org 23
554
555 The next example denies and logs any TCP traffic from the entire `crackers.org` network (a class C) to the `nice.people.org` machine (any port).
556
557     
558
559     # ipfw add deny log tcp from evil.crackers.org/24 to nice.people.org
560
561 If you do not want people sending X sessions to your internal network (a subnet of a class C), the following command will do the necessary filtering:
562
563     
564
565     # ipfw add deny tcp from any to my.org/28 6000 setup
566
567 To see the accounting records:
568
569     
570
571     # ipfw -a list
572
573  or in the short form
574
575     
576
577     # ipfw -a l
578
579 You can also see the last time a chain entry was matched with:
580
581     
582
583     # ipfw -at l
584
585 #### Building a Packet Filtering Firewall 
586
587  **Note:** The following suggestions are just that: suggestions. The requirements of each firewall are different and we cannot tell you how to build a firewall to meet your particular requirements.
588
589 When initially setting up your firewall, unless you have a test bench setup where you can configure your firewall host in a controlled environment, it is strongly recommend you use the logging version of the commands and enable logging in the kernel. This will allow you to quickly identify problem areas and cure them without too much disruption. Even after the initial setup phase is complete, I recommend using the logging for `deny' as it allows tracing of possible attacks and also modification of the firewall rules if your requirements alter.
590
591  **Note:** If you use the logging versions of the `accept` command, be aware that it can generate ***large*** amounts of log data. One log entry will be generated for every packet that passes through the firewall, so large FTP/http transfers, etc, will really slow the system down. It also increases the latencies on those packets as it requires more work to be done by the kernel before the packet can be passed on.  **syslogd**  will also start using up a lot more processor time as it logs all the extra data to disk, and it could quite easily fill the partition `/var/log` is located on.
592
593 You should enable your firewall from `/etc/rc.conf.local` or `/etc/rc.conf`. The associated manual page explains which knobs to fiddle and lists some preset firewall configurations. If you do not use a preset configuration, `ipfw list` will output the current ruleset into a file that you can pass to `rc.conf`. If you do not use `/etc/rc.conf.local` or `/etc/rc.conf` to enable your firewall, it is important to make sure your firewall is enabled before any IP interfaces are configured.
594
595 The next problem is what your firewall should actually ***do***! This is largely dependent on what access to your network you want to allow from the outside, and how much access to the outside world you want to allow from the inside. Some general rules are:
596
597 * Block all incoming access to ports below 1024 for TCP. This is where most of the security sensitive services are, like finger, SMTP (mail) and telnet.
598
599 * Block ***all*** incoming UDP traffic. There are very few useful services that travel over UDP, and what useful traffic there is, is normally a security threat (e.g. Suns RPC and NFS protocols). This has its disadvantages also, since UDP is a connectionless protocol, denying incoming UDP traffic also blocks the replies to outgoing UDP traffic. This can cause a problem for people (on the inside) using external archie (prospero) servers. If you want to allow access to archie, you will have to allow packets coming from ports 191 and 1525 to any internal UDP port through the firewall.  **ntp**  is another service you may consider allowing through, which comes from port 123.
600
601 * Block traffic to port 6000 from the outside. Port 6000 is the port used for access to X11 servers, and can be a security threat (especially if people are in the habit of doing `xhost +` on their workstations). X11 can actually use a range of ports starting at 6000, the upper limit being how many X displays you can run on the machine. The upper limit as defined by RFC 1700 (Assigned Numbers) is 6063.
602
603 * Check what ports any internal servers use (e.g. SQL servers, etc). It is probably a good idea to block those as well, as they normally fall outside the 1-1024 range specified above.
604
605 Another checklist for firewall configuration is available from CERT at http://www.cert.org/tech_tips/packet_filtering.html
606
607 As stated above, these are only ***guidelines***. You will have to decide what filter rules you want to use on your firewall yourself. We cannot accept ANY responsibility if someone breaks into your network, even if you follow the advice given above.
608
609 #### IPFW Overhead and Optimization 
610
611 Many people want to know how much overhead IPFW adds to a system. The answer to this depends mostly on your rule set and processor speed. For most applications dealing with Ethernet and small rule sets, the answer is ***negligible***. For those of you that need actual measurements to satisfy your curiosity, read on.
612
613 The following measurements were made using FreeBSD 2.2.5-STABLE on a 486-66. (While IPFW has changed slightly in later releases of DragonFly, it still performs with similar speed.) IPFW was modified to measure the time spent within the `ip_fw_chk` routine, displaying the results to the console every 1000 packets.
614
615 Two rule sets, each with 1000 rules, were tested. The first set was designed to demonstrate a worst case scenario by repeating the rule:
616
617     
618
619     # ipfw add deny tcp from any to any 55555
620
621 This demonstrates a worst case scenario by causing most of IPFW's packet check routine to be executed before finally deciding that the packet does not match the rule (by virtue of the port number). Following the 999th iteration of this rule was an `allow ip from any to any`.
622
623 The second set of rules were designed to abort the rule check quickly:
624
625     
626
627     # ipfw add deny ip from 1.2.3.4 to 1.2.3.4
628
629 The non-matching source IP address for the above rule causes these rules to be skipped very quickly. As before, the 1000th rule was an `allow ip from any to any`.
630
631 The per-packet processing overhead in the former case was approximately 2.703 ms/packet, or roughly 2.7 microseconds per rule. Thus the theoretical packet processing limit with these rules is around 370 packets per second. Assuming 10 Mbps Ethernet and a ~1500 byte packet size, we would only be able to achieve 55.5% bandwidth utilization.
632
633 For the latter case each packet was processed in approximately 1.172 ms, or roughly 1.2 microseconds per rule. The theoretical packet processing limit here would be about 853 packets per second, which could consume 10 Mbps Ethernet bandwidth.
634
635 The excessive number of rules tested and the nature of those rules do not provide a real-world scenario -- they were used only to generate the timing information presented here. Here are a few things to keep in mind when building an efficient rule set:
636
637 * Place an `established` rule early on to handle the majority of TCP traffic. Do not put any `allow tcp` statements before this rule.
638
639 * Place heavily triggered rules earlier in the rule set than those rarely used (***without changing the permissiveness of the firewall***, of course). You can see which rules are used most often by examining the packet counting statistics with `ipfw -a l`.
640
641 ## OpenSSL 
642
643 [OpenSSL](http://www.openssl.org/) provides a general-purpose cryptography library, as well as the Secure Sockets Layer v2/v3 (SSLv2/SSLv3) and Transport Layer Security v1 (TLSv1) network security protocols.
644
645 However, one of the algorithms (specifically IDEA) included in OpenSSL is protected by patents in the USA and elsewhere, and is not available for unrestricted use. IDEA is included in the OpenSSL sources in DragonFly, but it is not built by default. If you wish to use it, and you comply with the license terms, enable the `MAKE_IDEA` switch in `/etc/make.conf` and rebuild your sources using `make world`.
646
647 Today, the RSA algorithm is free for use in USA and other countries. In the past it was protected by a patent.
648
649 ## VPN over IPsec 
650
651 Creating a VPN between two networks, separated by the Internet, using DragonFly gateways.
652
653 ### Understanding IPsec 
654
655 This section will guide you through the process of setting up IPsec, and to use it in an environment which consists of DragonFly and  **Microsoft® Windows® 2000/XP**  machines, to make them communicate securely. In order to set up IPsec, it is necessary that you are familiar with the concepts of building a custom kernel (see [kernelconfig.html Chapter 9]).
656
657 ***IPsec*** is a protocol which sits on top of the Internet Protocol (IP) layer. It allows two or more hosts to communicate in a secure manner (hence the name). The DragonFly IPsec ***network stack*** is based on the [KAME](http://www.kame.net/) implementation, which has support for both protocol families, IPv4 and IPv6.
658
659 IPsec consists of two sub-protocols:
660
661 * ***Encapsulated Security Payload (ESP)***, protects the IP packet data from third party interference, by encrypting the contents using symmetric cryptography algorithms (like Blowfish, 3DES).
662
663 * ***Authentication Header (AH)***, protects the IP packet header from third party interference and spoofing, by computing a cryptographic checksum and hashing the IP packet header fields with a secure hashing function. This is then followed by an additional header that contains the hash, to allow the information in the packet to be authenticated.
664
665 ESP and AH can either be used together or separately, depending on the environment.
666
667 IPsec can either be used to directly encrypt the traffic between two hosts (known as ***Transport Mode***); or to build ***virtual tunnels*** between two subnets, which could be used for secure communication between two corporate networks (known as ***Tunnel Mode***). The latter is more commonly known as a ***Virtual Private Network (VPN)***. The [ipsec(4)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipsec&section4) manual page should be consulted for detailed information on the IPsec subsystem in DragonFly.
668
669 To add IPsec support to your kernel, add the following options to your kernel configuration file:
670
671     
672
673     options   IPSEC        #IP security
674
675     options   IPSEC_ESP    #IP security (crypto; define w/ IPSEC)
676
677     
678
679 If IPsec debugging support is desired, the following kernel option should also be added:
680
681     
682
683     options   IPSEC_DEBUG  #debug for IP security
684
685     
686
687 ### The Problem 
688
689 There's no standard for what constitutes a VPN. VPNs can be implemented using a number of different technologies, each of which have their own strengths and weaknesses. This article presents a number of scenarios, and strategies for implementing a VPN for each scenario.
690
691 ### Scenario #1: Two networks, connected to the Internet, to behave as one 
692
693 This is the scenario that caused me to first investigating VPNs. The premise is as follows:
694
695 * You have at least two sites
696
697 * Both sites are using IP internally
698
699 * Both sites are connected to the Internet, through a gateway that is running DragonFly.
700
701 * The gateway on each network has at least one public IP address.
702
703 * The internal addresses of the two networks can be public or private IP addresses, it doesn't matter. You can be running NAT on the gateway machine if necessary.
704
705 * The internal IP addresses of the two networks ***do not collide***. While I expect it is theoretically possible to use a combination of VPN technology and NAT to get this to work, I expect it to be a configuration nightmare.
706
707 If you find that you are trying to connect two networks, both of which, internally, use the same private IP address range (e.g., both of them use `192.168.1.x`), then one of the networks will have to be renumbered.
708
709 The network topology might look something like this:
710
711 security/ipsec-network.png
712
713 Notice the two public IP addresses. I'll use the letters to refer to them in the rest of this article. Anywhere you see those letters in this article, replace them with your own public IP addresses. Note also that internally, the two gateway machines have .1 IP addresses, and that the two networks have different private IP addresses (`192.168.1.x` and `192.168.2.x` respectively). All the machines on the private networks have been configured to use the `.1` machine as their default gateway.
714
715 The intention is that, from a network point of view, each network should view the machines on the other network as though they were directly attached the same router -- albeit a slightly slow router with an occasional tendency to drop packets.
716
717 This means that (for example), machine `192.168.1.20` should be able to run
718
719     
720
721     ping 192.168.2.34
722
723 and have it work, transparently. Windows machines should be able to see the machines on the other network, browse file shares, and so on, in exactly the same way that they can browse machines on the local network.
724
725 And the whole thing has to be secure. This means that traffic between the two networks has to be encrypted.
726
727 Creating a VPN between these two networks is a multi-step process. The stages are as follows:
728
729   1. Create a ***virtual*** network link between the two networks, across the Internet. Test it, using tools like [ping(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ping&section8), to make sure it works.
730
731   1. Apply security policies to ensure that traffic between the two networks is transparently encrypted and decrypted as necessary. Test this, using tools like [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#tcpdump&section1), to ensure that traffic is encrypted.
732
733   1. Configure additional software on the DragonFly gateways, to allow Windows machines to see one another across the VPN.
734
735 #### Step 1: Creating and testing a ***virtual*** network link 
736
737 Suppose that you were logged in to the gateway machine on network #1 (with public IP address `A.B.C.D`, private IP address `192.168.1.1`), and you ran `ping 192.168.2.1`, which is the private address of the machine with IP address `W.X.Y.Z`. What needs to happen in order for this to work?
738
739   1. The gateway machine needs to know how to reach `192.168.2.1`. In other words, it needs to have a route to `192.168.2.1`.
740
741   1. Private IP addresses, such as those in the `192.168.x` range are not supposed to appear on the Internet at large. Instead, each packet you send to `192.168.2.1` will need to be wrapped up inside another packet. This packet will need to appear to be from `A.B.C.D`, and it will have to be sent to `W.X.Y.Z`. This process is called ***encapsulation***.
742
743   1. Once this packet arrives at `W.X.Y.Z` it will need to ***unencapsulated***, and delivered to `192.168.2.1`.
744
745 You can think of this as requiring a ***tunnel*** between the two networks. The two ***tunnel mouths*** are the IP addresses `A.B.C.D` and `W.X.Y.Z`, and the tunnel must be told the addresses of the private IP addresses that will be allowed to pass through it. The tunnel is used to transfer traffic with private IP addresses across the public Internet.
746
747 This tunnel is created by using the generic interface, or `gif` devices on DragonFly. As you can imagine, the `gif` interface on each gateway host must be configured with four IP addresses; two for the public IP addresses, and two for the private IP addresses.
748
749 Support for the gif device must be compiled in to the DragonFly kernel on both machines. You can do this by adding the line:
750
751     
752
753     pseudo-device gif
754
755 to the kernel configuration files on both machines, and then compile, install, and reboot as normal.
756
757 Configuring the tunnel is a two step process. First the tunnel must be told what the outside (or public) IP addresses are, using [gifconfig(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#gifconfig&section8). Then the private IP addresses must be configured using [ifconfig(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=ifconfig&section=8).
758
759 On the gateway machine on network #1 you would run the following two commands to configure the tunnel.
760
761     
762
763     gifconfig gif0 A.B.C.D W.X.Y.Z
764
765     ifconfig gif0 inet 192.168.1.1 192.168.2.1 netmask 0xffffffff
766
767     
768
769 On the other gateway machine you run the same commands, but with the order of the IP addresses reversed.
770
771     
772
773     gifconfig gif0 W.X.Y.Z A.B.C.D
774
775     ifconfig gif0 inet 192.168.2.1 192.168.1.1 netmask 0xffffffff
776
777     
778
779 You can then run:
780
781     
782
783     gifconfig gif0
784
785 to see the configuration. For example, on the network #1 gateway, you would see this:
786
787     
788
789     # gifconfig gif0
790
791     gif0: flags=8011&lt;UP,POINTTOPOINT,MULTICAST&gt; mtu 1280
792
793     inet 192.168.1.1 --&gt; 192.168.2.1 netmask 0xffffffff
794
795     physical address inet A.B.C.D --&gt; W.X.Y.Z
796
797     
798
799 As you can see, a tunnel has been created between the physical addresses `A.B.C.D` and `W.X.Y.Z`, and the traffic allowed through the tunnel is that between `192.168.1.1` and `192.168.2.1`.
800
801 This will also have added an entry to the routing table on both machines, which you can examine with the command `netstat -rn`. This output is from the gateway host on network #1.
802
803     
804
805     # netstat -rn
806
807     Routing tables
808
809     
810
811     Internet:
812
813     Destination      Gateway       Flags    Refs    Use    Netif  Expire
814
815     ...
816
817     192.168.2.1      192.168.1.1   UH        0        0    gif0
818
819     ...
820
821     
822
823 As the ***Flags*** value indicates, this is a host route, which means that each gateway knows how to reach the other gateway, but they do not know how to reach the rest of their respective networks. That problem will be fixed shortly.
824
825 It is likely that you are running a firewall on both machines. This will need to be circumvented for your VPN traffic. You might want to allow all traffic between both networks, or you might want to include firewall rules that protect both ends of the VPN from one another.
826
827 It greatly simplifies testing if you configure the firewall to allow all traffic through the VPN. You can always tighten things up later. If you are using [ipfw(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ipfw&section8) on the gateway machines then a command like
828
829     
830
831     ipfw add 1 allow ip from any to any via gif0
832
833 will allow all traffic between the two end points of the VPN, without affecting your other firewall rules. Obviously you will need to run this command on both gateway hosts.
834
835 This is sufficient to allow each gateway machine to ping the other. On `192.168.1.1`, you should be able to run
836
837     
838
839     ping 192.168.2.1
840
841 and get a response, and you should be able to do the same thing on the other gateway machine.
842
843 However, you will not be able to reach internal machines on either network yet. This is because of the routing -- although the gateway machines know how to reach one another, they do not know how to reach the network behind each one.
844
845 To solve this problem you must add a static route on each gateway machine. The command to do this on the first gateway would be:
846
847     
848
849     route add 192.168.2.0 192.168.2.1 netmask 0xffffff00
850
851     
852
853 This says ***In order to reach the hosts on the network `192.168.2.0`, send the packets to the host `192.168.2.1`***. You will need to run a similar command on the other gateway, but with the `192.168.1.x` addresses instead.
854
855 IP traffic from hosts on one network will now be able to reach hosts on the other network.
856
857 That has now created two thirds of a VPN between the two networks, in as much as it is ***virtual*** and it is a ***network***. It is not private yet. You can test this using [ping(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ping&section8) and [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=tcpdump&section=1). Log in to the gateway host and run
858
859     
860
861     tcpdump dst host 192.168.2.1
862
863 In another log in session on the same host run
864
865     
866
867     ping 192.168.2.1
868
869 You will see output that looks something like this:
870
871     
872
873     16:10:24.018080 192.168.1.1 &gt; 192.168.2.1: icmp: echo request
874
875     16:10:24.018109 192.168.1.1 &gt; 192.168.2.1: icmp: echo reply
876
877     16:10:25.018814 192.168.1.1 &gt; 192.168.2.1: icmp: echo request
878
879     16:10:25.018847 192.168.1.1 &gt; 192.168.2.1: icmp: echo reply
880
881     16:10:26.028896 192.168.1.1 &gt; 192.168.2.1: icmp: echo request
882
883     16:10:26.029112 192.168.1.1 &gt; 192.168.2.1: icmp: echo reply
884
885     
886
887 As you can see, the ICMP messages are going back and forth unencrypted. If you had used the `-s` parameter to [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#tcpdump&section1) to grab more bytes of data from the packets you would see more information.
888
889 Obviously this is unacceptable. The next section will discuss securing the link between the two networks so that it all traffic is automatically encrypted.
890
891  **Summary:** 
892
893 * Configure both kernels with ***pseudo-device gif***.
894
895 * Edit `/etc/rc.conf` on gateway host #1 and add the following lines (replacing IP addresses as necessary).
896
897       
898
899       gifconfig_gif0="A.B.C.D W.X.Y.Z"
900
901       ifconfig_gif0="inet 192.168.1.1 192.168.2.1 netmask 0xffffffff"
902
903       static_routes="vpn"
904
905       route_vpn="192.168.2.0 192.168.2.1 netmask 0xffffff00"
906
907   
908
909 * Edit your firewall script (`/etc/rc.firewall`, or similar) on both hosts, and add
910
911       
912
913       ipfw add 1 allow ip from any to any via gif0
914
915   
916
917 * Make similar changes to `/etc/rc.conf` on gateway host #2, reversing the order of IP addresses.
918
919 #### Step 2: Securing the link 
920
921 To secure the link we will be using IPsec. IPsec provides a mechanism for two hosts to agree on an encryption key, and to then use this key in order to encrypt data between the two hosts.
922
923 The are two areas of configuration to be considered here.
924
925   1. There must be a mechanism for two hosts to agree on the encryption mechanism to use. Once two hosts have agreed on this mechanism there is said to be a ***security association*** between them.
926
927   1. There must be a mechanism for specifying which traffic should be encrypted. Obviously, you don't want to encrypt all your outgoing traffic -- you only want to encrypt the traffic that is part of the VPN. The rules that you put in place to determine what traffic will be encrypted are called ***security policies***.
928
929 Security associations and security policies are both maintained by the kernel, and can be modified by userland programs. However, before you can do this you must configure the kernel to support IPsec and the Encapsulated Security Payload (ESP) protocol. This is done by configuring a kernel with:
930
931     
932
933     options IPSEC
934
935     options IPSEC_ESP
936
937     
938
939 and recompiling, reinstalling, and rebooting. As before you will need to do this to the kernels on both of the gateway hosts.
940
941 You have two choices when it comes to setting up security associations. You can configure them by hand between two hosts, which entails choosing the encryption algorithm, encryption keys, and so forth, or you can use daemons that implement the Internet Key Exchange protocol (IKE) to do this for you.
942
943 I recommend the latter. Apart from anything else, it is easier to set up.
944
945 Editing and displaying security policies is carried out using [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8). By analogy, `setkey` is to the kernel's security policy tables as [route(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=route&section=8) is to the kernel's routing tables. `setkey` can also display the current security associations, and to continue the analogy further, is akin to `netstat -r` in that respect.
946
947 There are a number of choices for daemons to manage security associations with DragonFly. This article will describe how to use one of these, racoon. racoon is in the FreeBSD ports collection, in the security/ category, and is installed in the usual way.
948
949 racoon must be run on both gateway hosts. On each host it is configured with the IP address of the other end of the VPN, and a secret key (which you choose, and must be the same on both gateways).
950
951 The two daemons then contact one another, confirm that they are who they say they are (by using the secret key that you configured). The daemons then generate a new secret key, and use this to encrypt the traffic over the VPN. They periodically change this secret, so that even if an attacker were to crack one of the keys (which is as theoretically close to unfeasible as it gets) it won't do them much good -- by the time they've cracked the key the two daemons have chosen another one.
952
953 racoon's configuration is stored in `${PREFIX}/etc/racoon`. You should find a configuration file there, which should not need to be changed too much. The other component of racoon's configuration, which you will need to change, is the ***pre-shared key***.
954
955 The default racoon configuration expects to find this in the file `${PREFIX}/etc/racoon/psk.txt`. It is important to note that the pre-shared key is ***not*** the key that will be used to encrypt your traffic across the VPN link, it is simply a token that allows the key management daemons to trust one another.
956
957 `psk.txt` contains a line for each remote site you are dealing with. In this example, where there are two sites, each `psk.txt` file will contain one line (because each end of the VPN is only dealing with one other end).
958
959 On gateway host #1 this line should look like this:
960
961     
962
963     W.X.Y.Z            secret
964
965 That is, the ***public*** IP address of the remote end, whitespace, and a text string that provides the secret. Obviously, you shouldn't use ***secret*** as your key -- the normal rules for choosing a password apply.
966
967 On gateway host #2 the line would look like this
968
969     
970
971     A.B.C.D            secret
972
973 That is, the public IP address of the remote end, and the same secret key. `psk.txt` must be mode `0600` (i.e., only read/write to `root`) before racoon will run.
974
975 You must run racoon on both gateway machines. You will also need to add some firewall rules to allow the IKE traffic, which is carried over UDP to the ISAKMP (Internet Security Association Key Management Protocol) port. Again, this should be fairly early in your firewall ruleset.
976
977     
978
979     ipfw add 1 allow udp from A.B.C.D to W.X.Y.Z isakmp
980
981     ipfw add 1 allow udp from W.X.Y.Z to A.B.C.D isakmp
982
983     
984
985 Once racoon is running you can try pinging one gateway host from the other. The connection is still not encrypted, but racoon will then set up the security associations between the two hosts -- this might take a moment, and you may see this as a short delay before the ping commands start responding.
986
987 Once the security association has been set up you can view it using [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8). Run
988
989     
990
991     setkey -D
992
993 on either host to view the security association information.
994
995 That's one half of the problem. They other half is setting your security policies.
996
997 To create a sensible security policy, let's review what's been set up so far. This discussions hold for both ends of the link.
998
999 Each IP packet that you send out has a header that contains data about the packet. The header includes the IP addresses of both the source and destination. As we already know, private IP addresses, such as the `192.168.x.y` range are not supposed to appear on the public Internet. Instead, they must first be encapsulated inside another packet. This packet must have the public source and destination IP addresses substituted for the private addresses.
1000
1001 So if your outgoing packet started looking like this:
1002
1003 security/ipsec-out-pkt.png
1004
1005 Then it will be encapsulated inside another packet, looking something like this:
1006
1007 security/ipsec-encap-pkt.png
1008
1009 This encapsulation is carried out by the `gif` device. As you can see, the packet now has real IP addresses on the outside, and our original packet has been wrapped up as data inside the packet that will be put out on the Internet.
1010
1011 Obviously, we want all traffic between the VPNs to be encrypted. You might try putting this in to words, as:
1012
1013 ***If a packet leaves from `A.B.C.D`, and it is destined for `W.X.Y.Z`, then encrypt it, using the necessary security associations.***
1014
1015 ***If a packet arrives from `W.X.Y.Z`, and it is destined for `A.B.C.D`, then decrypt it, using the necessary security associations.***
1016
1017 That's close, but not quite right. If you did this, all traffic to and from `W.X.Y.Z`, even traffic that was not part of the VPN, would be encrypted. That's not quite what you want. The correct policy is as follows
1018
1019 ***If a packet leaves from `A.B.C.D`, and that packet is encapsulating another packet, and it is destined for `W.X.Y.Z`, then encrypt it, using the necessary security associations.***
1020
1021 ***If a packet arrives from `W.X.Y.Z`, and that packet is encapsulating another packet, and it is destined for `A.B.C.D`, then encrypt it, using the necessary security associations.***
1022
1023 A subtle change, but a necessary one.
1024
1025 Security policies are also set using [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8). [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command=setkey&section=8) features a configuration language for defining the policy. You can either enter configuration instructions via stdin, or you can use the `-f` option to specify a filename that contains configuration instructions.
1026
1027 The configuration on gateway host #1 (which has the public IP address `A.B.C.D`) to force all outbound traffic to `W.X.Y.Z` to be encrypted is:
1028
1029     
1030
1031     spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P out ipsec esp/tunnel/A.B.C.D-W.X.Y.Z/require;
1032
1033     
1034
1035 Put these commands in a file (e.g., `/etc/ipsec.conf`) and then run
1036
1037     
1038
1039     # setkey -f /etc/ipsec.conf
1040
1041 `spdadd` tells [setkey(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#setkey&section8) that we want to add a rule to the secure policy database. The rest of this line specifies which packets will match this policy. `A.B.C.D/32` and `W.X.Y.Z/32` are the IP addresses and netmasks that identify the network or hosts that this policy will apply to. In this case, we want it to apply to traffic between these two hosts. `ipencap` tells the kernel that this policy should only apply to packets that encapsulate other packets. `-P out` says that this policy applies to outgoing packets, and `ipsec` says that the packet will be secured.
1042
1043 The second line specifies how this packet will be encrypted. `esp` is the protocol that will be used, while `tunnel` indicates that the packet will be further encapsulated in an IPsec packet. The repeated use of `A.B.C.D` and `W.X.Y.Z` is used to select the security association to use, and the final `require` mandates that packets must be encrypted if they match this rule.
1044
1045 This rule only matches outgoing packets. You will need a similar rule to match incoming packets.
1046
1047     
1048
1049     spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P in ipsec esp/tunnel/W.X.Y.Z-A.B.C.D/require;
1050
1051 Note the `in` instead of `out` in this case, and the necessary reversal of the IP addresses.
1052
1053 The other gateway host (which has the public IP address `W.X.Y.Z`) will need similar rules.
1054
1055     
1056
1057     spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P out ipsec esp/tunnel/W.X.Y.Z-A.B.C.D/require;
1058
1059            spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P in ipsec esp/tunnel/A.B.C.D-W.X.Y.Z/require;
1060
1061 Finally, you need to add firewall rules to allow ESP and IPENCAP packets back and forth. These rules will need to be added to both hosts.
1062
1063     
1064
1065     ipfw add 1 allow esp from A.B.C.D to W.X.Y.Z
1066
1067     ipfw add 1 allow esp from W.X.Y.Z to A.B.C.D
1068
1069     ipfw add 1 allow ipencap from A.B.C.D to W.X.Y.Z
1070
1071     ipfw add 1 allow ipencap from W.X.Y.Z to A.B.C.D
1072
1073     
1074
1075 Because the rules are symmetric you can use the same rules on each gateway host.
1076
1077 Outgoing packets will now look something like this:
1078
1079 security/ipsec-crypt-pkt.png
1080
1081 When they are received by the far end of the VPN they will first be decrypted (using the security associations that have been negotiated by racoon). Then they will enter the `gif` interface, which will unwrap the second layer, until you are left with the innermost packet, which can then travel in to the inner network.
1082
1083 You can check the security using the same [ping(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#ping&section8) test from earlier. First, log in to the `A.B.C.D` gateway machine, and run:
1084
1085     
1086
1087     tcpdump dst host 192.168.2.1
1088
1089 In another log in session on the same host run
1090
1091     
1092
1093     ping 192.168.2.1
1094
1095 This time you should see output like the following:
1096
1097     
1098
1099     XXX tcpdump output
1100
1101 Now, as you can see, [tcpdump(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#tcpdump&section1) shows the ESP packets. If you try to examine them with the `-s` option you will see (apparently) gibberish, because of the encryption.
1102
1103 Congratulations. You have just set up a VPN between two remote sites.
1104
1105  **Summary** 
1106
1107 * Configure both kernels with:
1108
1109       
1110
1111       options IPSEC
1112
1113       options IPSEC_ESP
1114
1115   
1116
1117 * Install [`security/racoon`](http://pkgsrc.se/security/racoon). Edit `${PREFIX}/etc/racoon/psk.txt` on both gateway hosts, adding an entry for the remote host's IP address and a secret key that they both know. Make sure this file is mode 0600.
1118
1119 * Add the following lines to `/etc/rc.conf` on each host:
1120
1121       
1122
1123       ipsec_enable="YES"
1124
1125       ipsec_file="/etc/ipsec.conf"
1126
1127   
1128
1129 * Create an `/etc/ipsec.conf` on each host that contains the necessary spdadd lines. On gateway host #1 this would be:
1130
1131       
1132
1133       spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P out ipsec
1134
1135         esp/tunnel/A.B.C.D-W.X.Y.Z/require;
1136
1137       spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P in ipsec
1138
1139         esp/tunnel/W.X.Y.Z-A.B.C.D/require;
1140
1141   
1142
1143   On gateway host #2 this would be:
1144
1145       
1146
1147       spdadd W.X.Y.Z/32 A.B.C.D/32 ipencap -P out ipsec
1148
1149         esp/tunnel/W.X.Y.Z-A.B.C.D/require;
1150
1151       spdadd A.B.C.D/32 W.X.Y.Z/32 ipencap -P in ipsec
1152
1153         esp/tunnel/A.B.C.D-W.X.Y.Z/require;
1154
1155   
1156
1157 * Add firewall rules to allow IKE, ESP, and IPENCAP traffic to both hosts:
1158
1159       
1160
1161       ipfw add 1 allow udp from A.B.C.D to W.X.Y.Z isakmp
1162
1163       ipfw add 1 allow udp from W.X.Y.Z to A.B.C.D isakmp
1164
1165       ipfw add 1 allow esp from A.B.C.D to W.X.Y.Z
1166
1167       ipfw add 1 allow esp from W.X.Y.Z to A.B.C.D
1168
1169       ipfw add 1 allow ipencap from A.B.C.D to W.X.Y.Z
1170
1171       ipfw add 1 allow ipencap from W.X.Y.Z to A.B.C.D
1172
1173   
1174
1175 The previous two steps should suffice to get the VPN up and running. Machines on each network will be able to refer to one another using IP addresses, and all traffic across the link will be automatically and securely encrypted.
1176
1177 ----
1178
1179 ## OpenSSH 
1180
1181  **OpenSSH**  is a set of network connectivity tools used to access remote machines securely. It can be used as a direct replacement for `rlogin`, `rsh`, `rcp`, and `telnet`. Additionally, any other TCP/IP connections can be tunneled/forwarded securely through SSH.  **OpenSSH**  encrypts all traffic to effectively eliminate eavesdropping, connection hijacking, and other network-level attacks.
1182
1183  **OpenSSH**  is maintained by the OpenBSD project, and is based upon SSH v1.2.12 with all the recent bug fixes and updates. It is compatible with both SSH protocols 1 and 2.
1184
1185 ### Advantages of Using OpenSSH 
1186
1187 Normally, when using [telnet(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#telnet&section1) or [rlogin(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=rlogin&section=1), data is sent over the network in an clear, un-encrypted form. Network sniffers anywhere in between the client and server can steal your user/password information or data transferred in your session.  **OpenSSH**  offers a variety of authentication and encryption methods to prevent this from happening.
1188
1189 ### Enabling sshd 
1190
1191 Be sure to make the following addition to your `rc.conf` file:
1192
1193     
1194
1195     sshd_enable="YES"
1196
1197 This will load [sshd(8)](http://leaf.dragonflybsd.org/cgi/web-man?command#sshd&section8&manpath=OpenBSD+3.3), the daemon program for  **OpenSSH** , the next time your system initializes. Alternatively, you can simply run directly the  **sshd**  daemon by typing `rcstart sshd` on the command line.
1198
1199 ### SSH Client 
1200
1201 The [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh&section1&manpath=OpenBSD+3.3) utility works similarly to [rlogin(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=rlogin&section=1).
1202
1203     
1204
1205     # ssh user@example.com
1206
1207     Host key not found from the list of known hosts.
1208
1209     Are you sure you want to continue connecting (yes/no)? yes
1210
1211     Host 'example.com' added to the list of known hosts.
1212
1213     user@example.com's password: *******
1214
1215 The login will continue just as it would have if a session was created using `rlogin` or `telnet`. SSH utilizes a key fingerprint system for verifying the authenticity of the server when the client connects. The user is prompted to enter `yes` only when connecting for the first time. Future attempts to login are all verified against the saved fingerprint key. The SSH client will alert you if the saved fingerprint differs from the received fingerprint on future login attempts. The fingerprints are saved in `~/.ssh/known_hosts`, or `~/.ssh/known_hosts2` for SSH v2 fingerprints.
1216
1217 By default,  **OpenSSH**  servers are configured to accept both SSH v1 and SSH v2 connections. The client, however, can choose between the two. Version 2 is known to be more robust and secure than its predecessor.
1218
1219 The [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh&section1&manpath=OpenBSD+3.3) command can be forced to use either protocol by passing it the `-1` or `-2` argument for v1 and v2, respectively.
1220
1221 ### Secure Copy 
1222
1223 The [scp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#scp&section1&manpath=OpenBSD+3.3) command works similarly to [rcp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=rcp&section=1); it copies a file to or from a remote machine, except in a secure fashion.
1224
1225     
1226
1227     #  scp user@example.com:/COPYRIGHT COPYRIGHT
1228
1229     user@example.com's password: *******
1230
1231     COPYRIGHT            100% |*****************************|  4735
1232
1233     00:00
1234
1235     #
1236
1237 Since the fingerprint was already saved for this host in the previous example, it is verified when using [scp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#scp&section1&manpath=OpenBSD+3.3) here.
1238
1239 The arguments passed to [scp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#scp&section1&manpath=OpenBSD+3.3) are similar to [cp(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=cp&section=1), with the file or files in the first argument, and the destination in the second. Since the file is fetched over the network, through SSH, one or more of the file arguments takes on the form `user@host:<path_to_remote_file>`. The `user@` part is optional. If omitted, it will default to the same username as you are currently logged in as, unless configured otherwise.
1240
1241 ### Configuration 
1242
1243 The system-wide configuration files for both the  **OpenSSH**  daemon and client reside within the `/etc/ssh` directory.
1244
1245 `ssh_config` configures the client settings, while `sshd_config` configures the daemon.
1246
1247 Additionally, the `sshd_program` (`/usr/sbin/sshd` by default), and `sshd_flags` `rc.conf` options can provide more levels of configuration.
1248
1249 Each user can have a personal configuration file in `~/.ssh/config`. The file can configure various client options, and can include host-specific options. With the following configuration file, a user could type `ssh shell` which would be equivalent to `ssh -X user@shell.example.com`.
1250
1251     
1252
1253     Host shell
1254
1255      Hostname shell.example.com
1256
1257      Username user
1258
1259      Protocol 2
1260
1261      ForwardX11 yes
1262
1263 ### ssh-keygen 
1264
1265 Instead of using passwords, [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) can be used to generate RSA keys to authenticate a user:
1266
1267     
1268
1269     % ssh-keygen -t rsa1
1270
1271     Initializing random number generator...
1272
1273     Generating p:  .++ (distance 66)
1274
1275     Generating q:  ..............................++ (distance 498)
1276
1277     Computing the keys...
1278
1279     Key generation complete.
1280
1281     Enter file in which to save the key (/home/user/.ssh/identity):
1282
1283     Enter passphrase:
1284
1285     Enter the same passphrase again:
1286
1287     Your identification has been saved in /home/user/.ssh/identity.
1288
1289     ...
1290
1291 [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) will create a public and private key pair for use in authentication. The private key is stored in `~/.ssh/identity`, whereas the public key is stored in `~/.ssh/identity.pub`. The public key must be placed in `~/.ssh/authorized_keys` of the remote machine in order for the setup to work.
1292
1293 This will allow connection to the remote machine based upon RSA authentication instead of passwords.
1294
1295  **Note:** The `-t rsa1` option will create RSA keys for use by SSH protocol version 1. If you want to use RSA keys with the SSH protocol version 2, you have to use the command `ssh-keygen -t rsa`.
1296
1297 If a passphrase is used in [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3), the user will be prompted for a password each time in order to use the private key.
1298
1299 A SSH protocol version 2 DSA key can be created for the same purpose by using the `ssh-keygen -t dsa` command. This will create a public/private DSA key for use in SSH protocol version 2 sessions only. The public key is stored in `~/.ssh/id_dsa.pub`, while the private key is in `~/.ssh/id_dsa`.
1300
1301 DSA public keys are also placed in `~/.ssh/authorized_keys` on the remote machine.
1302
1303 [ssh-agent(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-agent&section1&manpath=OpenBSD+3.3) and [ssh-add(1)](http://leaf.dragonflybsd.org/cgi/web-man?command=ssh-add&section=1&manpath=OpenBSD+3.3) are utilities used in managing multiple passworded private keys.
1304
1305  **Warning:** The various options and files can be different according to the  **OpenSSH**  version you have on your system, to avoid problems you should consult the [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) manual page.
1306
1307 ### SSH Tunneling 
1308
1309  **OpenSSH**  has the ability to create a tunnel to encapsulate another protocol in an encrypted session.
1310
1311 The following command tells [ssh(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh&section1&manpath=OpenBSD+3.3) to create a tunnel for  **telnet** :
1312
1313     
1314
1315     % ssh -2 -N -f -L 5023:localhost:23 user@foo.example.com
1316
1317     %
1318
1319 The `ssh` command is used with the following options:
1320
1321 `-2`
1322
1323  :: Forces `ssh` to use version 2 of the protocol. (Do not use if you are working with older SSH servers)
1324
1325 `-N`
1326
1327  :: Indicates no command, or tunnel only. If omitted, `ssh` would initiate a normal session.
1328
1329 `-f`
1330
1331  :: Forces `ssh` to run in the background.
1332
1333 `-L`
1334
1335  :: Indicates a local tunnel in `***localport:remotehost:remoteport***` fashion.
1336
1337 `user@foo.example.com`
1338
1339  :: The remote SSH server.
1340
1341 An SSH tunnel works by creating a listen socket on `localhost` on the specified port. It then forwards any connection received on the local host/port via the SSH connection to the specified remote host and port.
1342
1343 In the example, port `***5023***` on `localhost` is being forwarded to port `***23***` on `localhost` of the remote machine. Since `***23***` is  **telnet** , this would create a secure  **telnet**  session through an SSH tunnel.
1344
1345 This can be used to wrap any number of insecure TCP protocols such as SMTP, POP3, FTP, etc.
1346
1347  **Example 10-1. Using SSH to Create a Secure Tunnel for SMTP** 
1348
1349     
1350
1351     % ssh -2 -N -f -L 5025:localhost:25 user@mailserver.example.com
1352
1353     user@mailserver.example.com's password: *****
1354
1355     % telnet localhost 5025
1356
1357     Trying 127.0.0.1...
1358
1359     Connected to localhost.
1360
1361     Escape character is '^]'.
1362
1363     220 mailserver.example.com ESMTP
1364
1365 This can be used in conjunction with an [ssh-keygen(1)](http://leaf.dragonflybsd.org/cgi/web-man?command#ssh-keygen&section1&manpath=OpenBSD+3.3) and additional user accounts to create a more seamless/hassle-free SSH tunneling environment. Keys can be used in place of typing a password, and the tunnels can be run as a separate user.
1366
1367 #### Practical SSH Tunneling Examples 
1368
1369 ##### Secure Access of a POP3 Server 
1370
1371 At work, there is an SSH server that accepts connections from the outside. On the same office network resides a mail server running a POP3 server. The network, or network path between your home and office may or may not be completely trustable. Because of this, you need to check your e-mail in a secure manner. The solution is to create an SSH connection to your office's SSH server, and tunnel through to the mail server.
1372
1373     
1374
1375     % ssh -2 -N -f -L 2110:mail.example.com:110 user@ssh-server.example.com
1376
1377     user@ssh-server.example.com's password: ******
1378
1379 When the tunnel is up and running, you can point your mail client to send POP3 requests to `localhost` port 2110. A connection here will be forwarded securely across the tunnel to `mail.example.com`.
1380
1381 ##### Bypassing a Draconian Firewall 
1382
1383 Some network administrators impose extremely draconian firewall rules, filtering not only incoming connections, but outgoing connections. You may be only given access to contact remote machines on ports 22 and 80 for SSH and web surfing.
1384
1385 You may wish to access another (perhaps non-work related) service, such as an Ogg Vorbis server to stream music. If this Ogg Vorbis server is streaming on some other port than 22 or 80, you will not be able to access it.
1386
1387 The solution is to create an SSH connection to a machine outside of your network's firewall, and use it to tunnel to the Ogg Vorbis server.
1388
1389     
1390
1391     % ssh -2 -N -f -L 8888:music.example.com:8000 user@unfirewalled-system.example.org
1392
1393     user@unfirewalled-system.example.org's password: *******
1394
1395 Your streaming client can now be pointed to `localhost` port 8888, which will be forwarded over to `music.example.com` port 8000, successfully evading the firewall.