mercurial-serverA set of tools for managing authorization and access control forssh-based Mercurial repositoriesPaul Crowley, paul@lshift.net, 2008-2009This software may be used and distributed according to the termsof the GNU General Public License, incorporated herein by reference.WHAT IT GIVES YOUThese tools make it easier to provide a centralized repository hostwith read/write access to many repositories for many developers.All of the repositories controlled by these tools are owned by a single user(the "hg" user in what follows), but many remote users can act on them, anddifferent users can have different permissions. We don't use file permissions toachieve that - instead, developers log in as the "hg" user when they connect tothe repository host using ssh, using ssh URLs of the form"ssh://hg@repository-host/repository-name". A restricted shell prevents themfrom using this access for unauthorized purposes. Developers are authenticatedonly using SSH keys; no other form of authentication is supported. To give a user access to the repository, place their key in anappropriately-named subdirectory of "/etc/mercurial-server/keys" and run"/etc/mercurial-server/refresh-auth". You can then control what access they haveto what repositories by editing the control file"/etc/mercurial-server/access.conf", which can match the names of these keysagainst a glob pattern. For convenient remote control of access, you can instead (if you have theprivileges) make changes to a special repository called "hgadmin", whichcontains its own "access.conf" file and "keys" directory. Changes pushed to thisrepository take effect immediately. The two "access.conf" files areconcatenated, and the keys directories merged.QUICK STARTYou and all developers using this system will need an SSH public key, and willalmost certainly want to be running ssh-agent (or its equivalent, eg Pageantunder Windows). If you're not familiar with ssh-agent, you should learn aboutthat before using this.In what follows, certain operations (eg installing mercurial-server itself) haveto be done on the repository server (which we call "repository-host"), but anyoperation that involves checking in or out of Mercurial can be done wherever ismost convenient to you; the most usual arrangment would be that you'd do thesethings at the machine you sit at, and on which you run ssh-agent, which is whatauthenticates you when you talk to the repository server.Ensure there is no user called "hg" on the repository host, and run "./install".This installs the mercurial-server files and control files, and creates and setsup the "hg" user.Place your SSH public key in the directory "/etc/mercurial-server/keys/root". Isuggest creating yourself a directory and naming the key after your hostname (iethe file is called something like"/etc/mercurial-server/keys/root/yourname/yourhostname") so that you can easilymanage users who have a different key on each host they use. Then run"/etc/mercurial-server/refresh-auth".The repository is now ready to use, and you are now the sole user able to changeand create repositories on this repository host. CREATING REPOSITORIESTo create a new repository, you clone a local repository onto the remote server.So if you want a new empty repository called "myproject", you can do (asyourself): hg init myproject hg clone myproject ssh://hg@repository-host/myprojectADDING OTHER USERSBecause your key is in the "keys/root" subdirectory, you have the equivalent of"root privileges" over mercurial-server (not the whole computer, justmercurial-server). You can add other root users by putting their keys next toyours, or you can make less privileged users by putting their keys in the"keys/users" subdirectory - these users will be able to read and write to anyrepository (except one - see below) but will not be able to create newrepositories. As always, when you change "/etc/mercurial-server/keys" you needto re-run "/etc/mercurial-server/refresh-auth".LOGGINGEvery push and pull is logged with the key used: see the file .hg/serve-log ineach repository.USING HGADMINIt can be inconvenient to log on to the repository server, become root, copykeys around, and run "refresh-auth" every time you want to change userprivileges. This is where mercurial-server shines :-) Suppose you have anotheruser's SSH public key in the file "/tmp/theirkey" (on the machine you sit at,not necessarily the repository server) and you want to give them user-levelaccess to the repository server. Run these commands: hg clone ssh://hg@repository-server/hgadmin cd hgadmin mkdir keys/user/thatuser cp /tmp/theirkey keys/user/thatuser/theirhostname hg add hg commit -m "Added key for thatuser" hg pushIn other words, hgadmin is a version controlled version of"/etc/mercurial-server/keys", and changes to it take effect immediately. Only"keys/root" users can act on "hgadmin" - those with keys in "keys/users" arelocked out. Multiple admins can use Mercurial's version control to cooperate oncontrolling access to the repository server in a natural way. You can also add"root" users by putting their key in the "keys/root" directory in just the sameway - these users will now be able to control hgadmin and create newrepositories just as you can.ACCESS.CONFOut of the box, there are just two kinds of users: the ones with keys in"keys/root" and those in "keys/users". However, you can change this by editing"access.conf". There are two "access.conf" files, one in "/etc/mercurial-server"and one in "hgadmin"; the two are simply concatenated before being read.Each line of access.conf has the following syntax:<rule> <condition> <condition> ...Rule is one ofinit - allow any operation, including the creation of new repositorieswrite - allow reads and writes to this file in this repositoryread - allow the repo to be read but reject matching writesdeny - deny all requestsA condition is a globpattern matched against a relative path, one of:user=<globpattern> - user's keyrepo=<globpattern> - repo (as the user supplies it)file=<globpattern> - file in the repobranch=<globpattern> - name of the branchThe first rule in the file which has all its conditions satisfied is used todetermine whether an action is allowed.Paths cannot contain any special characters except "/"; glob patterns cannotcontain any special characters except "/" and "*". "*" matches zero or morecharacters not including "/" while "**" matches zero or more charactersincluding "/".Blank lines and lines that start with "#" are ignored.FILE CONDITIONSmercurial-server supports file and branch conditions, which restrict anoperation depending on what files it modifies and what branch the work is on.However, the way these conditions work is subtle and can be counterintuitive -if you want to keep things simple, stick to user and repo conditions, and thenthings are likely to work the way you would expect.The rules file is used to make four decisions:- Whether to allow a repository to be created- Whether to allow access to a repository- Whether to allow a changeset on a particular branch at all- Whether to allow a changeset to change a particular fileWhen the first two of these decisions are being made, nothing is known aboutwhat files might be changed, and so all file conditions automatically succeedfor the purpose of such decisions. This means that doing tricky things with fileconditions can have counterintuitive consequences:- You cannot limit read access to a subset of a repository with a "read" ruleand a file condition: any user who has access to a repository can read all of itand its full history. Such a rule can only have the effect of masking a later"write" rule, as in this example: read repo=specialrepo file=dontwritethis write repo=specialrepoallows all users to read specialrepo, and to write to all files *except* thatany changeset which writes to "dontwritethis" will be rejected.- For similar reasons, don't give "init" rules file conditions.- Don't try to deny write access to a particular file on a particular branch - adeveloper can write to the file on another branch and then merge it in. Eitherdeny all writes to the branch from that user, or allow them to write to all thefiles they can write to on any branch. In other words, something like this willhave the intended effect: write user=docs/* branch=docs file=docs/*But something like this will not have the intended effect; it will effectivelyallow these users to write to any file on any branch, by writing it to "docs"first: write user=docs/* branch=docs write user=docs/* file=docs/* read user=docs/*HOW IT WORKSWhen a developer attempts to connect to a repository via ssh, the SSH daemonsearches for a match for that user's key in ~hg/.ssh/authorized_keys. If thedeveloper is authorised to connect to the repository they will have an entry inthis file. The entry includes a "command" prefix which specifies that therestricted shell should be used; this shell is passed an argument identifyingthe developer. The shell parses the command the developer is trying to execute,and consults a rules file to see if that developer is allowed to perform thataction on that repository. The bulk of the work of the restricted shell is doneby the Python program "hg-ssh", but the shell script "hg-ssh-wrapper" sets upsome configuration so that you can change it to suit your local installation.The file ~hg/.ssh/authorized_keys is generated by "refresh-auth", which recursesthrough two directories of files containing SSH keys and generates an entry inauthorized_keys for each one, using the name of the key file as the identifierfor the developer. These keys will live in the "keys" subdirectory"/etc/mercurial-server" and the "keys" subdirectory of a repository called"hgadmin". A hook in this repository re-runs "refresh-auth" on the most recentversion after every push.Finally, hook in an extension is run for each changeset that is remotelycommitted, which uses the rules file to determine whether to allow thechangeset.LOCKED OUT?Once you're working with "hgadmin", it can be convenient to remove all the keysin "/etc/mercurial-server/keys" and all the entries in"/etc/mercurial-server/access.conf" and use hgadmin to control everything. Ifyou find yourself locked out, you can get back in again by restoring some of theentries you removed from these files - remember,"/etc/mercurial-server/access.conf" takes precedence over the "access.conf" in"hgadmin".THANKSThanks for reading this far. If you use mercurial-server, please tell me aboutit.Paul Crowley, 2009