Set user as login name
authorCédric Krier <ced@b2ck.com>
Sun, 14 Dec 2014 20:30:25 +0100
changeset 374 7a1d6b228af6
parent 373 a286d6c6b19c
child 378 a788cfad4cfa
Set user as login name using a configuration index of the key path.
doc/manual.docbook
src/hg-ssh
src/mercurialserver/config.py
--- a/doc/manual.docbook	Wed Mar 05 12:16:46 2014 +0100
+++ b/doc/manual.docbook	Sun Dec 14 20:30:25 2014 +0100
@@ -591,6 +591,15 @@
 </itemizedlist>
 </listitem>
 <listitem>
+<para><literal>[users]</literal></para>
+<itemizedlist>
+<listitem>
+<para><literal>index</literal>: the index of the key path that contains the
+user login.  If <literal>-1</literal>, the full path is used.</para>
+</listitem>
+</itemizedlist>
+</listitem>
+<listitem>
 <para><literal>[env]</literal>: all entries in here are added to the Unix environment
 when users connect.  The keys are converted to all-upper-case. </para>
 <itemizedlist>
--- a/src/hg-ssh	Wed Mar 05 12:16:46 2014 +0100
+++ b/src/hg-ssh	Sun Dec 14 20:30:25 2014 +0100
@@ -38,13 +38,26 @@
     os.environ[k.upper()] = v
 
 if len(sys.argv) == 3 and sys.argv[1] == "--base64":
-    ruleset.rules.set(user = base64.b64decode(sys.argv[2]))
+    user = base64.b64decode(sys.argv[2])
+    ruleset.rules.set(user = user)
 elif len(sys.argv) == 2:
-    ruleset.rules.set(user = sys.argv[1])
+    user = sys.argv[1]
+    ruleset.rules.set(user = user)
 else:
     fail("hg-ssh wrongly called, is authorized_keys corrupt? (%s)"
         % sys.argv)
 
+paths = []
+path = user
+while path:
+    path, tail = os.path.split(path)
+    paths.append(tail)
+paths.reverse()
+i = config.getUserPathIndex()
+if i >= 0 and i < len(paths):
+    user = paths[i]
+os.environ['LOGNAME'] = user
+
 os.chdir(config.getReposPath())
 
 for f in config.getAccessPaths():
--- a/src/mercurialserver/config.py	Wed Mar 05 12:16:46 2014 +0100
+++ b/src/mercurialserver/config.py	Sun Dec 14 20:30:25 2014 +0100
@@ -51,6 +51,9 @@
     return _getdefault("exceptions", "allowdots", [],
         lambda s: s.split(":"))
 
+def getUserPathIndex():
+    return _getdefault("users", "index", -1, int)
+
 # Work out where we are, don't use config.
 def initExe():
     global _exePath