author | Matt Mackall <mpm@selenic.com> |
Thu, 15 Jul 2010 15:06:45 -0500 | |
changeset 11596 | 47ca289a3a08 |
parent 11594 | 67863f9d805f |
child 11618 | 83070a9cd526 |
permissions | -rw-r--r-- |
2399 | 1 |
# sshserver.py - ssh protocol server support for mercurial |
2396 | 2 |
# |
4635
63b9d2deed48
Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4258
diff
changeset
|
3 |
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
2859 | 4 |
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
2396 | 5 |
# |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8109
diff
changeset
|
6 |
# This software may be used and distributed according to the terms of the |
10263 | 7 |
# GNU General Public License version 2 or any later version. |
2396 | 8 |
|
3891 | 9 |
from i18n import _ |
11596
47ca289a3a08
ssh: drop some old imports
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
10 |
import util, hook, wireproto |
47ca289a3a08
ssh: drop some old imports
Matt Mackall <mpm@selenic.com>
parents:
11594
diff
changeset
|
11 |
import os, sys |
2396 | 12 |
|
13 |
class sshserver(object): |
|
14 |
def __init__(self, ui, repo): |
|
15 |
self.ui = ui |
|
16 |
self.repo = repo |
|
17 |
self.lock = None |
|
18 |
self.fin = sys.stdin |
|
19 |
self.fout = sys.stdout |
|
20 |
||
5833
323b9c55b328
hook: redirect stdout to stderr for ssh and http servers
Matt Mackall <mpm@selenic.com>
parents:
4635
diff
changeset
|
21 |
hook.redirect(True) |
2396 | 22 |
sys.stdout = sys.stderr |
23 |
||
24 |
# Prevent insertion/deletion of CRs |
|
25 |
util.set_binary(self.fin) |
|
26 |
util.set_binary(self.fout) |
|
27 |
||
11579 | 28 |
def getargs(self, args): |
29 |
data = {} |
|
30 |
keys = args.split() |
|
31 |
count = len(keys) |
|
32 |
for n in xrange(len(keys)): |
|
33 |
argline = self.fin.readline()[:-1] |
|
34 |
arg, l = argline.split() |
|
35 |
val = self.fin.read(int(l)) |
|
36 |
if arg not in keys: |
|
37 |
raise util.Abort("unexpected parameter %r" % arg) |
|
38 |
if arg == '*': |
|
39 |
star = {} |
|
40 |
for n in xrange(int(l)): |
|
41 |
arg, l = argline.split() |
|
42 |
val = self.fin.read(int(l)) |
|
43 |
star[arg] = val |
|
44 |
data['*'] = star |
|
45 |
else: |
|
46 |
data[arg] = val |
|
47 |
return [data[k] for k in keys] |
|
48 |
||
49 |
def getarg(self, name): |
|
50 |
return self.getargs(name)[0] |
|
2396 | 51 |
|
52 |
def respond(self, v): |
|
53 |
self.fout.write("%d\n" % len(v)) |
|
54 |
self.fout.write(v) |
|
55 |
self.fout.flush() |
|
56 |
||
11584
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
57 |
def sendchangegroup(self, changegroup): |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
58 |
while True: |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
59 |
d = changegroup.read(4096) |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
60 |
if not d: |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
61 |
break |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
62 |
self.fout.write(d) |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
63 |
|
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
64 |
self.fout.flush() |
1af96b090116
protocol: unify changegroup commands
Matt Mackall <mpm@selenic.com>
parents:
11581
diff
changeset
|
65 |
|
11585
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
66 |
def sendstream(self, source): |
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
67 |
for chunk in source: |
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
68 |
self.fout.write(chunk) |
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
69 |
self.fout.flush() |
5d907fbb9703
protocol: unify stream_out command
Matt Mackall <mpm@selenic.com>
parents:
11584
diff
changeset
|
70 |
|
11593
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
71 |
def getfile(self, fpout): |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
72 |
self.respond('') |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
73 |
count = int(self.fin.readline()) |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
74 |
while count: |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
75 |
fpout.write(self.fin.read(count)) |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
76 |
count = int(self.fin.readline()) |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
77 |
|
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
78 |
def redirect(self): |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
79 |
pass |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
80 |
|
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
81 |
def respondpush(self, ret): |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
82 |
self.respond('') |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
83 |
self.respond(str(ret)) |
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
84 |
|
2396 | 85 |
def serve_forever(self): |
8109
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
86 |
try: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
87 |
while self.serve_one(): |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
88 |
pass |
8109
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
89 |
finally: |
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
90 |
if self.lock is not None: |
496ae1ea4698
switch lock releasing in the core from gc to explicit
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
7875
diff
changeset
|
91 |
self.lock.release() |
2396 | 92 |
sys.exit(0) |
93 |
||
94 |
def serve_one(self): |
|
95 |
cmd = self.fin.readline()[:-1] |
|
11581
4530b3307fb9
protocol: introduce wireproto.py
Matt Mackall <mpm@selenic.com>
parents:
11580
diff
changeset
|
96 |
if cmd and not wireproto.dispatch(self.repo, self, cmd): |
2396 | 97 |
impl = getattr(self, 'do_' + cmd, None) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
98 |
if impl: |
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
99 |
r = impl() |
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
100 |
if r is not None: |
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
101 |
self.respond(r) |
2397
e9d402506514
merge change to ssh protocol.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2396
diff
changeset
|
102 |
else: self.respond("") |
2396 | 103 |
return cmd != '' |
104 |
||
105 |
def do_lock(self): |
|
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
106 |
'''DEPRECATED - allowing remote client to lock repo is not safe''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
107 |
|
2396 | 108 |
self.lock = self.repo.lock() |
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
109 |
return "" |
2396 | 110 |
|
111 |
def do_unlock(self): |
|
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
112 |
'''DEPRECATED''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
113 |
|
2396 | 114 |
if self.lock: |
115 |
self.lock.release() |
|
116 |
self.lock = None |
|
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
117 |
return "" |
2396 | 118 |
|
119 |
def do_addchangegroup(self): |
|
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
120 |
'''DEPRECATED''' |
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
121 |
|
2396 | 122 |
if not self.lock: |
123 |
self.respond("not locked") |
|
124 |
return |
|
125 |
||
126 |
self.respond("") |
|
11593
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
127 |
r = self.repo.addchangegroup(self.fin, 'serve', self._client(), |
11442
ee1ed6afac21
addchangegroup: pass in lock to release it before changegroup hook is called
Matt Mackall <mpm@selenic.com>
parents:
11369
diff
changeset
|
128 |
lock=self.lock) |
11580
69248b5add46
protocol: move most ssh responses to returns
Matt Mackall <mpm@selenic.com>
parents:
11579
diff
changeset
|
129 |
return str(r) |
2439
e8c4f3d3df8c
extend network protocol to stop clients from locking servers
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2419
diff
changeset
|
130 |
|
11593
d054cc5c7737
protocol: unify unbundle on the server side
Matt Mackall <mpm@selenic.com>
parents:
11585
diff
changeset
|
131 |
def _client(self): |
2673
109a22f5434a
hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2622
diff
changeset
|
132 |
client = os.environ.get('SSH_CLIENT', '').split(' ', 1)[0] |
109a22f5434a
hooks: add url to changegroup, incoming, prechangegroup, pretxnchangegroup hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2622
diff
changeset
|
133 |
return 'remote:ssh:' + client |