Debian Codegraph @

hgext/factotum.py

raw
1 # factotum.py - Plan 9 factotum integration for Mercurial
2 #
3 # Copyright (C) 2012 Steven Stallion <[email protected]>
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2 of the License, or (at your
8 # option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 '''http authentication with factotum
20
21 This extension allows the factotum(4) facility on Plan 9 from Bell Labs
22 platforms to provide authentication information for HTTP access. Configuration
23 entries specified in the auth section as well as authentication information
24 provided in the repository URL are fully supported. If no prefix is specified,
25 a value of "*" will be assumed.
26
27 By default, keys are specified as::
28
29 proto=pass service=hg prefix=<prefix> user=<username> !password=<password>
30
31 If the factotum extension is unable to read the required key, one will be
32 requested interactively.
33
34 A configuration section is available to customize runtime behavior. By
35 default, these entries are::
36
37 [factotum]
38 executable = /bin/auth/factotum
39 mountpoint = /mnt/factotum
40 service = hg
41
42 The executable entry defines the full path to the factotum binary. The
43 mountpoint entry defines the path to the factotum file service. Lastly, the
44 service entry controls the service name used when reading keys.
45
46 '''
47
48 from __future__ import annotations
49
50 import os
51 from mercurial.i18n import _
52 from mercurial.utils import procutil
53 from mercurial import (
54 error,
55 httpconnection,
56 registrar,
57 url,
58 util,
59 )
60
61 urlreq = util.urlreq
62 passwordmgr = url.passwordmgr
63
64 ERRMAX = 128
65
66 _executable = _mountpoint = _service = None
67
68 configtable = {}
69 configitem = registrar.configitem(configtable)
70
71 configitem(
72 b'factotum',
73 b'executable',
74 default=b'/bin/auth/factotum',
75 )
76 configitem(
77 b'factotum',
78 b'mountpoint',
79 default=b'/mnt/factotum',
80 )
81 configitem(
82 b'factotum',
83 b'service',
84 default=b'hg',
85 )
86
87
88 def auth_getkey(self, params):
89 if not self.ui.interactive():
90 raise error.Abort(_(b'factotum not interactive'))
91 if b'user=' not in params:
92 params = b'%s user?' % params
93 params = b'%s !password?' % params
94 os.system(procutil.tonativestr(b"%s -g '%s'" % (_executable, params)))
95
96
97 def auth_getuserpasswd(self, getkey, params):
98 params = b'proto=pass %s' % params
99 while True:
100 fd = os.open(b'%s/rpc' % _mountpoint, os.O_RDWR)
101 try:
102 os.write(fd, b'start %s' % params)
103 l = os.read(fd, ERRMAX).split()
104 if l[0] == b'ok':
105 os.write(fd, b'read')
106 status, user, passwd = os.read(fd, ERRMAX).split(None, 2)
107 if status == b'ok':
108 if passwd.startswith(b"'"):
109 if passwd.endswith(b"'"):
110 passwd = passwd[1:-1].replace(b"''", b"'")
111 else:
112 raise error.Abort(_(b'malformed password string'))
113 return (user, passwd)
114 except OSError:
115 raise error.Abort(_(b'factotum not responding'))
116 finally:
117 os.close(fd)
118 getkey(self, params)
119
120
121 def monkeypatch_method(cls):
122 def decorator(func):
123 setattr(cls, func.__name__, func)
124 return func
125
126 return decorator
127
128
129 @monkeypatch_method(passwordmgr)
130 def find_user_password(self, realm, authuri):
131 user, passwd = self.passwddb.find_user_password(realm, authuri)
132 if user and passwd:
133 self._writedebug(user, passwd)
134 return (user, passwd)
135
136 prefix = b''
137 res = httpconnection.readauthforuri(self.ui, authuri, user)
138 if res:
139 _, auth = res
140 prefix = auth.get(b'prefix')
141 user, passwd = auth.get(b'username'), auth.get(b'password')
142 if not user or not passwd:
143 if not prefix:
144 prefix = realm.split(b' ')[0].lower()
145 params = b'service=%s prefix=%s' % (_service, prefix)
146 if user:
147 params = b'%s user=%s' % (params, user)
148 user, passwd = auth_getuserpasswd(self, auth_getkey, params)
149
150 self.add_password(realm, authuri, user, passwd)
151 self._writedebug(user, passwd)
152 return (user, passwd)
153
154
155 def uisetup(ui):
156 global _executable
157 _executable = ui.config(b'factotum', b'executable')
158 global _mountpoint
159 _mountpoint = ui.config(b'factotum', b'mountpoint')
160 global _service
161 _service = ui.config(b'factotum', b'service')