119
+ − 1
<?xml version="1.0" encoding="utf-8"?>
120
+ − 2
<article xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en"
+ − 3
xmlns:xlink="http://www.w3.org/1999/xlink">
119
+ − 4
<info>
+ − 5
<title>Sharing Mercurial repositories with mercurial-server</title>
+ − 6
<author><firstname>Paul</firstname><surname>Crowley</surname></author>
120
+ − 7
<copyright><year>2009</year><holder>Paul Crowley, LShift Ltd</holder></copyright>
119
+ − 8
</info>
+ − 9
<section>
+ − 10
<title>About mercurial-server</title>
+ − 11
<para>
120
+ − 12
Home page: <link xlink:href="http://www.lshift.net/mercurial-server.html"/>
+ − 13
</para>
+ − 14
<para>
119
+ − 15
mercurial-server is software for Debian and Ubuntu systems which gives your
120
+ − 16
developers remote read/write access to <link
+ − 17
xlink:href="http://hg-scm.org/">Mercurial</link> repositories using SSH public
119
+ − 18
key authentication; it provides convenient and fine-grained key management
+ − 19
and access control.
+ − 20
</para>
+ − 21
<para>
+ − 22
mercurial-server is the easiest and most secure way for several developers
+ − 23
to have read/write access to a central repository, but that's not the only
+ − 24
way for several people to work on the same project using Mercurial; you
120
+ − 25
should be familiar with the <link
+ − 26
xlink:href="http://mercurial.selenic.com/wiki/MultipleCommitters">other ways of
+ − 27
handling multiple commiters</link> before deciding to use this.
119
+ − 28
</para>
+ − 29
<para>
+ − 30
Though mercurial-server is currently targeted at Debian-based systems such
+ − 31
as Ubuntu, other users have reported success getting it running on other
+ − 32
Unix-based systems such as Red Hat. Running it on a non-Unix system such as
+ − 33
Windows is not supported. You will need root privileges to install it.
+ − 34
</para>
+ − 35
<section>
+ − 36
<title>Legalese</title>
+ − 37
<para>
+ − 38
This program is free software; you can redistribute it and/or modify it
+ − 39
under the terms of the GNU General Public License as published by the Free
+ − 40
Software Foundation; either version 2 of the License, or (at your option)
+ − 41
any later version.
+ − 42
</para>
+ − 43
<para>
+ − 44
This program is distributed in the hope that it will be useful, but
+ − 45
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ − 46
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ − 47
more details.
+ − 48
</para>
+ − 49
<para>
+ − 50
You should have received a copy of the GNU General Public License along
+ − 51
with this program; if not, write to the Free Software Foundation, Inc., 51
+ − 52
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ − 53
</para>
+ − 54
</section>
+ − 55
</section>
+ − 56
<section>
+ − 57
<title>Step by step</title>
+ − 58
<para>
120
+ − 59
mercurial-server authenticates users not using passwords but using <link
+ − 60
xlink:href="http://sial.org/howto/openssh/publickey-auth/">SSH public
+ − 61
keys</link>; everyone who wants access to a mercurial-server repository
+ − 62
will need such a key, so you'll need to familiarize yourself with them
+ − 63
before proceeding. In combination with <command>ssh-agent</command> (or
+ − 64
equivalents such as the Windows program <link
+ − 65
xlink:href="http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter9.html#pageant">Pageant</link>),
+ − 66
this means that users will not need to type in a password to access the
+ − 67
repository.
119
+ − 68
</para>
120
+ − 69
<section>
+ − 70
<title>Creating a repository host</title>
119
+ − 71
<para>
120
+ − 72
In what follows, we assume that you usually sit at a machine called
+ − 73
<systemitem class="systemname">my-workstation</systemitem> and you wish to
+ − 74
install mercurial-server on <systemitem
+ − 75
class="systemname">repository-host</systemitem>. First, you'll need to
+ − 76
create an SSH public key if you haven't already. You should consult your
+ − 77
system documentation on how to do this, but it should look something like
+ − 78
this.
119
+ − 79
</para>
+ − 80
<screen>
+ − 81
<computeroutput>user@my-workstation:~$ </computeroutput><userinput>ssh-keygen</userinput>
+ − 82
<computeroutput>Generating public/private rsa key pair.
+ − 83
Enter passphrase (empty for no passphrase):
+ − 84
Enter same passphrase again:
+ − 85
Your identification has been saved in /home/user/.ssh/id_rsa.
+ − 86
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
+ − 87
The key fingerprint is:
+ − 88
8b:aa:0a:98:fe:e7:84:48:a3:fe:5f:31:4b:16:e6:0b user@my-workstation
+ − 89
user@my-workstation:~$ </computeroutput><userinput>ssh-add</userinput>
+ − 90
<computeroutput>Enter passphrase for /home/user/.ssh/id_rsa:
+ − 91
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)
+ − 92
user@my-workstation:~$ </computeroutput></screen>
+ − 93
<para>Now copy the files you're going to need over to your target system, and install mercurial-server</para>
+ − 94
<screen>
+ − 95
<computeroutput>user@my-workstation:~$ </computeroutput><userinput>ssh-copy-id repository-host</userinput>
+ − 96
<computeroutput>user@repository-host's password:
+ − 97
Now try logging into the machine, with "ssh 'repository-host'", and check in:
+ − 98
+ − 99
.ssh/authorized_keys
+ − 100
+ − 101
to make sure we haven't added extra keys that you weren't expecting.
+ − 102
user@my-workstation:~$ </computeroutput><userinput>scp mercurial-server_0.6.1_amd64.deb repository-host:</userinput>
+ − 103
<computeroutput>mercurial-server_0.6.1_amd64.deb 100%
+ − 104
user@my-workstation:~$ </computeroutput><userinput>ssh -A repository-host</userinput>
+ − 105
<computeroutput>user@repository-host:~$ </computeroutput><userinput>sudo dpkg -i ../mercurial-server_0.6.1_amd64.deb</userinput>
+ − 106
<computeroutput>[sudo] password for user:
+ − 107
Selecting previously deselected package mercurial-server.
+ − 108
(Reading database ... 144805 files and directories currently installed.)
+ − 109
Unpacking mercurial-server (from .../mercurial-server_0.6.1_amd64.deb) ...
+ − 110
Setting up mercurial-server (0.6.1) ...
+ − 111
user@repository-host:~$ </computeroutput></screen>
+ − 112
<para>
120
+ − 113
mercurial-server is now installed on the repository host. Next, we need to give you permission to access its repositories.
119
+ − 114
</para>
+ − 115
<screen>
+ − 116
<computeroutput>user@repository-host:~$ </computeroutput><userinput>ssh-add -L > my-key</userinput>
+ − 117
<computeroutput>user@repository-host:~$ </computeroutput><userinput>sudo mkdir -p /etc/mercurial-server/keys/root/user</userinput>
+ − 118
<computeroutput>user@repository-host:~$ </computeroutput><userinput>sudo cp my-key /etc/mercurial-server/keys/root/user/my-workstation</userinput>
+ − 119
<computeroutput>user@repository-host:~$ </computeroutput><userinput>sudo -u hg /usr/share/mercurial-server/refresh-auth</userinput>
+ − 120
<computeroutput>user@repository-host:~$ </computeroutput><userinput>exit</userinput>
+ − 121
<computeroutput>Connection to shell closed.
+ − 122
user@my-workstation:~$ </computeroutput></screen>
+ − 123
<para>
120
+ − 124
You can now create repositories on the remote machine and have complete
+ − 125
read-write access to all of them; you need never log on to <systemitem
+ − 126
class="systemname">repository-host</systemitem> again.
119
+ − 127
</para>
120
+ − 128
</section>
+ − 129
<section>
+ − 130
<title>Creating repositories</title>
119
+ − 131
<screen>
+ − 132
<computeroutput>user@my-workstation:~$ </computeroutput><userinput>cd my-mercurial-project</userinput>
+ − 133
<computeroutput>user@my-workstation:~/my-mercurial-project$ </computeroutput><userinput>hg clone . ssh://hg@repository-host/repository/name</userinput>
+ − 134
<computeroutput>searching for changes
+ − 135
remote: adding changesets
+ − 136
remote: adding manifests
+ − 137
remote: adding file changes
+ − 138
remote: added 119 changesets with 284 changes to 61 files
+ − 139
user@my-workstation:~/my-mercurial-project$ </computeroutput><userinput>hg pull ssh://hg@repository-host/repository/name</userinput>
+ − 140
<computeroutput>pulling from ssh://hg@repository-host/repository/name
+ − 141
searching for changes
+ − 142
no changes found
+ − 143
user@my-workstation:~/my-mercurial-project$ </computeroutput></screen>
120
+ − 144
</section>
+ − 145
<section>
+ − 146
<title>Adding other users</title>
119
+ − 147
<para>
120
+ − 148
As things stand, no-one but you has any access to any repositories you
+ − 149
create on this system. In order to give anyone else access, you'll need a
+ − 150
copy of their SSH public key; we'll assume you have that key in
+ − 151
<filename>~/other-users-key.pub</filename>. You could give them access by
+ − 152
logging into <systemitem class="systemname">repository-host</systemitem>,
+ − 153
putting the key in the right place under <filename
+ − 154
class='directory'>/etc/mercurial-server/keys</filename>, and re-running
+ − 155
<userinput>sudo -u hg /usr/share/mercurial-server/refresh-auth</userinput>.
+ − 156
However, there's a more convenient way.
119
+ − 157
</para>
+ − 158
<screen>
+ − 159
<computeroutput>user@my-workstation:~/my-mercurial-project$ </computeroutput><userinput>cd ..</userinput>
+ − 160
<computeroutput>user@my-workstation:~$ </computeroutput><userinput>hg clone ssh://hg@repository-host/hgadmin</userinput>
+ − 161
<computeroutput>destination directory: hgadmin
+ − 162
no changes found
+ − 163
updating working directory
+ − 164
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+ − 165
user@my-workstation:~$ </computeroutput><userinput>cd hgadmin</userinput>
+ − 166
<computeroutput>user@my-workstation:~/hgadmin$ </computeroutput><userinput>mkdir -p keys/users/other-user</userinput>
+ − 167
<computeroutput>user@my-workstation:~/hgadmin$ </computeroutput><userinput>cp ~/other-users-key.pub keys/users/other-user/their-workstation</userinput>
+ − 168
<computeroutput>user@my-workstation:~/hgadmin$ </computeroutput><userinput>hg add</userinput>
+ − 169
<computeroutput>adding keys/users/other-user/their-workstation
+ − 170
user@my-workstation:~/hgadmin$ </computeroutput><userinput>hg commit -m "Add other user"</userinput>
+ − 171
<computeroutput>user@my-workstation:~/hgadmin$ </computeroutput><userinput>hg push</userinput>
+ − 172
<computeroutput>pushing to ssh://hg@repository-host/hgadmin
+ − 173
searching for changes
+ − 174
remote: adding changesets
+ − 175
remote: adding manifests
+ − 176
remote: adding file changes
+ − 177
remote: added 1 changesets with 1 changes to 1 files
+ − 178
user@my-workstation:~/hgadmin$ </computeroutput></screen>
+ − 179
<para>
120
+ − 180
The new user can now read and write to your
+ − 181
<literal>ssh://hg@repository-host/repository/name</literal> repository.
+ − 182
Most other changes to access control can be made simply by making and
+ − 183
pushing changes to <literal>hgadmin</literal>, and you can use Mercurial to
+ − 184
cooperate with other root users in the normal way.
+ − 185
</para>
+ − 186
</section>
+ − 187
<section>
+ − 188
<title>Basic access control</title>
+ − 189
<para>
+ − 190
Out of the box, mercurial-server supports two kinds of users: "root" users and normal users. If you followed the steps above, you are a "root" user because your key is under <filename class='directory'>keys/root</filename>, while the other user you gave access to is a normal user since their key is under <filename class='directory'>keys/users</filename>. Keys that are not in either of these directories will by default have no access to anything.
+ − 191
</para>
+ − 192
<para>
+ − 193
Root users can edit <literal>hgadmin</literal>, create new repositories and read and write to existing ones. Normal users cannot access <literal>hgadmin</literal> or create new repositories, but they can read and write to any other repository. This is only the default configuration; for more advanced configuration read <xref linkend="accesscontrol"/>.
+ − 194
</para>
+ − 195
</section>
+ − 196
</section>
+ − 197
<section>
+ − 198
<title>How mercurial-server works</title>
+ − 199
<para>
+ − 200
All of the repositories controlled by mercurial-server are owned by a
+ − 201
single user, the <literal>hg</literal> user, which is why all URLs for
+ − 202
mercurial-server repositories start with <literal>ssh://hg@...</literal>.
+ − 203
Each SSH key that has access to the repository has an entry in
+ − 204
<filename>~hg/.ssh/authorized_keys</filename>; this is how the SSH daemon
+ − 205
knows to give that key access. When the user connects over SSH, their
+ − 206
commands are run in a specially crafted restricted shell; this shell knows
+ − 207
which key was used to connect, determines what the user is trying to do,
+ − 208
and checks the access rules to decide whether to allow it.
+ − 209
</para>
+ − 210
<para>
+ − 211
This restricted shell also ensures that certain Mercurial extensions are
+ − 212
loaded when the user acts on a repository; these extensions check the
+ − 213
access control rules for any changeset that the user tries to commit, and
+ − 214
log all pushes and pulls into a per-repository access log.
+ − 215
</para>
+ − 216
<para>
+ − 217
<command>refresh-auth</command> recurses through the <filename
+ − 218
class='directory'>/etc/mercurial-server/keys</filename> and the <filename
+ − 219
class='directory'>keys</filename> directory in the
+ − 220
<literal>hgadmin</literal> repository, creating an entry in
+ − 221
<filename>~hg/.ssh/authorized_keys</filename> for each one. This is redone
+ − 222
automatically whenever a change is pushed to <literal>hgadmin</literal>.
+ − 223
</para>
+ − 224
</section>
+ − 225
<section id="accesscontrol">
+ − 226
<title>Access control</title>
+ − 227
<para>
+ − 228
mercurial-server offers much more fine-grained access control than this division into two classes of users. Let's suppose you wish to give Pat access to the <literal>widget</literal> repository, but no other. We first copy Pat's SSH public key into the <filename
+ − 229
class='directory'>keys/widget/pat</filename> directory in <literal>hgadmin</literal>. Now mercurial-server knows about Pat's key, but will give Pat no access to anything because the key is not under either <filename
+ − 230
class='directory'>keys/root</filename> or <filename
+ − 231
class='directory'>keys/users</filename>. To grant this key access, we must give mercurial-server a new access rule, so we create a file in <literal>hgadmin</literal> called <filename>access.conf</filename>, with the following contents:</para>
+ − 232
<programlisting>
+ − 233
write repo=widget user=widget/**
+ − 234
</programlisting>
+ − 235
<para>
+ − 236
Pat will have read and write access as soon as we add, commit, and push these files.
+ − 237
</para>
+ − 238
<para>
+ − 239
Each line of access.conf has the following syntax:
+ − 240
</para>
+ − 241
<programlisting>
+ − 242
<replaceable>rule</replaceable> <replaceable>condition</replaceable> <replaceable>condition...</replaceable>
+ − 243
</programlisting>
+ − 244
<para>
+ − 245
Blank lines and lines that start with <literal>#</literal> are ignored. Rule is one of
+ − 246
</para>
+ − 247
<itemizedlist>
+ − 248
<listitem>
+ − 249
<literal>init</literal>: allow reads, writes, and the creation of new repositories
+ − 250
</listitem>
+ − 251
<listitem>
+ − 252
<literal>write</literal>: allow reads and writes
+ − 253
</listitem>
+ − 254
<listitem>
+ − 255
<literal>read</literal>: allow only read operations
+ − 256
</listitem>
+ − 257
<listitem>
+ − 258
<literal>deny</literal>: deny all requests
+ − 259
</listitem>
+ − 260
</itemizedlist>
+ − 261
<para>
+ − 262
When considering a request, mercurial-server steps through all the rules in <filename>/etc/mercurial-server/access.conf</filename> and then all the rules in <filename>access.conf</filename> in <literal>hgadmin</literal> looking for a rule which matches on every condition. If it does not find such a rule, it denies the request; otherwise it checks whether the rule grants sufficient privilege to allow it.
+ − 263
</para>
+ − 264
<para>
+ − 265
By default, <filename>/etc/mercurial-server/access.conf</filename> has the following rules:
+ − 266
</para>
+ − 267
<programlisting>
+ − 268
init user=root/**
+ − 269
deny repo=hgadmin
+ − 270
write user=users/**
+ − 271
</programlisting>
+ − 272
<para>
+ − 273
These rules ensure that root users can do any operation on any repository, that no other users can access the <literal>hgadmin</literal> repository, and that those with keys in <filename class='directory'>keys/users</filename> can read or write to any repository but not create repositories.
+ − 274
</para>
+ − 275
<para>
+ − 276
A condition is a globpattern matched against a relative path. The two most
+ − 277
important conditions are
+ − 278
</para>
+ − 279
<itemizedlist>
+ − 280
<listitem>
+ − 281
<code><literal>user=</literal><replaceable>globpattern</replaceable></code>: path to the user's key
+ − 282
</listitem>
+ − 283
<listitem>
+ − 284
<code><literal>repo=</literal><replaceable>globpattern</replaceable></code>: path to the repository
+ − 285
</listitem>
+ − 286
</itemizedlist>
+ − 287
<para>
+ − 288
"*" only matches one directory level, where "**" matches as many as you
+ − 289
want. More precisely, "*" matches zero or more characters not including "/"
+ − 290
while "**" matches zero or more characters including "/".
+ − 291
</para>
+ − 292
<section>
+ − 293
<title>File conditions</title>
+ − 294
<para>
+ − 295
Here be dragons...
+ − 296
</para>
+ − 297
</section>
+ − 298
</section>
+ − 299
<section>
+ − 300
<title>Security</title>
+ − 301
<para>
+ − 302
mercurial-server relies entirely on sshd to grant access to remote users.
+ − 303
As a result, it runs no daemons, installs no setuid programs, and no part
+ − 304
of it runs as root except the install process: all programs run as the user
+ − 305
hg. And any attack on mercurial-server can only be started if the Bad Guys
+ − 306
already have a public key in <filename>~hg/.ssh/authorized_keys</filename>,
+ − 307
otherwise sshd will bar the way.
+ − 308
</para>
+ − 309
<para>
+ − 310
No matter what command the user tries to run on the remote system via SSH,
+ − 311
mercurial-server is run. It parses the command line the user asked for, and
+ − 312
interprets and runs the corresponding hg operation itself if access is
+ − 313
allowed, so users can only read and add to history within repositories;
+ − 314
they cannot run any other hg command. In addition, every push and pull is
+ − 315
logged with a datestamp, changeset ID and the key that performed the
+ − 316
operation.
+ − 317
</para>
+ − 318
<para>
+ − 319
However, while the first paragraph holds no matter what bugs
+ − 320
mercurial-server contains, the second depends on the relevant code being
+ − 321
correct; though the entire codebase is short, mercurial-server is a fairly
+ − 322
new program and may harbour bugs. Backups are essential!
+ − 323
</para>
+ − 324
</section>
+ − 325
<section>
+ − 326
<title>Thanks</title>
+ − 327
<para>
+ − 328
Thanks for reading this far. If you use mercurial-server, please tell me about
+ − 329
it.
+ − 330
</para>
+ − 331
<para>
+ − 332
Paul Crowley, <email>paul@lshift.net</email>, 2009
119
+ − 333
</para>
+ − 334
</section>
+ − 335
</article>
+ − 336