Add all the source files from the old CVS tree,
[import/samba-docs-svnimport.git] / devel / encryption.xml
1 <chapter id="pwencrypt">
2
3
4 <chapterinfo>
5         <author>
6                 <firstname>Jeremy</firstname><surname>Allison</surname>
7                 <affiliation>
8                         <orgname>Samba Team</orgname>
9                         <address>
10                                 <email>samba@samba.org</email>
11                         </address>
12                 </affiliation>
13         </author>
14
15         <pubdate>19 Apr 1999</pubdate>
16 </chapterinfo>
17         
18 <title>LanMan and NT Password Encryption</title>
19
20 <sect1>
21         <title>Introduction</title>
22         
23         <para>With the development of LanManager and Windows NT 
24         compatible password encryption for Samba, it is now able 
25         to validate user connections in exactly the same way as 
26         a LanManager or Windows NT server.</para>
27
28         <para>This document describes how the SMB password encryption 
29         algorithm works and what issues there are in choosing whether 
30         you want to use it. You should read it carefully, especially 
31         the part about security and the "PROS and CONS" section.</para>
32         
33 </sect1>
34
35 <sect1>
36         <title>How does it work?</title>
37
38         <para>LanManager encryption is somewhat similar to UNIX 
39         password encryption. The server uses a file containing a 
40         hashed value of a user's password.  This is created by taking 
41         the user's plaintext password, capitalising it, and either 
42         truncating to 14 bytes or padding to 14 bytes with null bytes. 
43         This 14 byte value is used as two 56 bit DES keys to encrypt 
44         a 'magic' eight byte value, forming a 16 byte value which is 
45         stored by the server and client. Let this value be known as 
46         the "hashed password".</para>
47         
48         <para>Windows NT encryption is a higher quality mechanism, 
49         consisting of doing an MD4 hash on a Unicode version of the user's 
50         password. This also produces a 16 byte hash value that is 
51         non-reversible.</para>
52
53         <para>When a client (LanManager, Windows for WorkGroups, Windows 
54         95 or Windows NT) wishes to mount a Samba drive (or use a Samba 
55         resource), it first requests a connection and negotiates the 
56         protocol that the client and server will use. In the reply to this 
57         request the Samba server generates and appends an 8 byte, random 
58         value - this is stored in the Samba server after the reply is sent 
59         and is known as the "challenge".  The challenge is different for 
60         every client connection.</para>
61
62         <para>The client then uses the hashed password (16 byte values 
63         described above), appended with 5 null bytes, as three 56 bit 
64         DES keys, each of which is used to encrypt the challenge 8 byte 
65         value, forming a 24 byte value known as the "response".</para>
66
67         <para>In the SMB call SMBsessionsetupX (when user level security 
68         is selected) or the call SMBtconX (when share level security is 
69         selected), the 24 byte response is returned by the client to the 
70         Samba server.  For Windows NT protocol levels the above calculation 
71         is done on both hashes of the user's password and both responses are 
72         returned in the SMB call, giving two 24 byte values.</para>
73
74         <para>The Samba server then reproduces the above calculation, using 
75         its own stored value of the 16 byte hashed password (read from the 
76         <filename>smbpasswd</filename> file - described later) and the challenge 
77         value that it kept from the negotiate protocol reply. It then checks 
78         to see if the 24 byte value it calculates matches the 24 byte value 
79         returned to it from the client.</para>
80
81         <para>If these values match exactly, then the client knew the 
82         correct password (or the 16 byte hashed value - see security note 
83         below) and is thus allowed access. If not, then the client did not 
84         know the correct password and is denied access.</para>
85
86         <para>Note that the Samba server never knows or stores the cleartext 
87         of the user's password - just the 16 byte hashed values derived from 
88         it. Also note that the cleartext password or 16 byte hashed values 
89         are never transmitted over the network - thus increasing security.</para>
90 </sect1>
91
92 <sect1>
93         <title>The smbpasswd file</title>
94         <anchor id="SMBPASSWDFILEFORMAT"/>
95         <para>In order for Samba to participate in the above protocol 
96         it must be able to look up the 16 byte hashed values given a user name.
97         Unfortunately, as the UNIX password value is also a one way hash
98         function (ie. it is impossible to retrieve the cleartext of the user's
99         password given the UNIX hash of it), a separate password file
100         containing this 16 byte value must be kept. To minimise problems with
101         these two password files, getting out of sync, the UNIX <filename>
102         /etc/passwd</filename> and the <filename>smbpasswd</filename> file, 
103         a utility, <command>mksmbpasswd.sh</command>, is provided to generate
104         a smbpasswd file from a UNIX <filename>/etc/passwd</filename> file.
105         </para>
106
107
108         <para>To generate the smbpasswd file from your <filename>/etc/passwd
109         </filename> file use the following command:</para>
110         
111         <para><prompt>$ </prompt><userinput>cat /etc/passwd | mksmbpasswd.sh
112         &gt; /usr/local/samba/private/smbpasswd</userinput></para>
113         
114         <para>If you are running on a system that uses NIS, use</para>
115
116         <para><prompt>$ </prompt><userinput>ypcat passwd | mksmbpasswd.sh
117         &gt; /usr/local/samba/private/smbpasswd</userinput></para>
118         
119         <para>The <command>mksmbpasswd.sh</command> program is found in 
120         the Samba source directory. By default, the smbpasswd file is 
121         stored in :</para>
122
123         <para><filename>/usr/local/samba/private/smbpasswd</filename></para>
124
125         <para>The owner of the <filename>/usr/local/samba/private/</filename> 
126         directory should be set to root, and the permissions on it should 
127         be set to 0500 (<command>chmod 500 /usr/local/samba/private</command>).
128         </para>
129
130         <para>Likewise, the smbpasswd file inside the private directory should 
131         be owned by root and the permissions on is should be set to 0600
132         (<command>chmod 600 smbpasswd</command>).</para>
133
134
135         <para>The format of the smbpasswd file is (The line has been 
136         wrapped here. It should appear as one entry per line in 
137         your smbpasswd file.)</para>
138         
139         <para><programlisting>
140 username:uid:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:
141         [Account type]:LCT-&lt;last-change-time&gt;:Long name
142         </programlisting></para>
143         
144         <para>Although only the <replaceable>username</replaceable>, 
145         <replaceable>uid</replaceable>, <replaceable>
146         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</replaceable>,
147         [<replaceable>Account type</replaceable>] and <replaceable>
148         last-change-time</replaceable> sections are significant 
149         and are looked at in the Samba code.</para>
150
151         <para>It is <emphasis>VITALLY</emphasis> important that there by 32 
152         'X' characters between the two ':' characters in the XXX sections - 
153         the smbpasswd and Samba code will fail to validate any entries that 
154         do not have 32 characters  between ':' characters. The first XXX 
155         section is for the Lanman password hash, the second is for the 
156         Windows NT version.</para>
157
158         <para>When the password file is created all users have password entries
159         consisting of 32 'X' characters. By default this disallows any access
160         as this user. When a user has a password set, the 'X' characters change
161         to 32 ascii hexadecimal digits (0-9, A-F). These are an ascii
162         representation of the 16 byte hashed value of a user's password.</para>
163
164         <para>To set a user to have no password (not recommended), edit the file
165         using vi, and replace the first 11 characters with the ascii text
166         <constant>"NO PASSWORD"</constant> (minus the quotes).</para>
167
168         <para>For example, to clear the password for user bob, his smbpasswd file 
169         entry would look like :</para>
170
171         <para><programlisting>
172 bob:100:NO PASSWORDXXXXXXXXXXXXXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:
173         [U          ]:LCT-00000000:Bob's full name:/bobhome:/bobshell
174         </programlisting></para>
175         
176         <para>If you are allowing users to use the smbpasswd command to set 
177         their own passwords, you may want to give users NO PASSWORD initially 
178         so they do not have to enter a previous password when changing to their 
179         new password (not recommended). In order for you to allow this the
180         <command>smbpasswd</command> program must be able to connect to the 
181         <command>smbd</command> daemon as that user with no password. Enable this 
182         by adding the line :</para>
183
184         <para><command>null passwords = yes</command></para>
185         
186         <para>to the [global] section of the smb.conf file (this is why 
187         the above scenario is not recommended). Preferably, allocate your
188         users a default password to begin with, so you do not have
189         to enable this on your server.</para>
190
191         <para><emphasis>Note : </emphasis>This file should be protected very 
192         carefully. Anyone with access to this file can (with enough knowledge of 
193         the protocols) gain access to your SMB server. The file is thus more 
194         sensitive than a normal unix <filename>/etc/passwd</filename> file.</para>
195 </sect1>
196
197 </chapter>