~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch/ChannelAgentForwarding.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
10
  2. Redistributions in binary form must reproduce the above copyright 
 
11
     notice, this list of conditions and the following disclaimer in 
 
12
     the documentation and/or other materials provided with the distribution.
 
13
 
 
14
  3. The names of the authors may not be used to endorse or promote products
 
15
     derived from this software without specific prior written permission.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
 
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using System;
 
33
using System.Collections;
 
34
using System.IO;
 
35
using NSch;
 
36
using Sharpen;
 
37
 
 
38
namespace NSch
 
39
{
 
40
        internal class ChannelAgentForwarding : Channel
 
41
        {
 
42
                private const int LOCAL_WINDOW_SIZE_MAX = unchecked((int)(0x20000));
 
43
 
 
44
                private const int LOCAL_MAXIMUM_PACKET_SIZE = unchecked((int)(0x4000));
 
45
 
 
46
                private readonly int SSH2_AGENTC_REQUEST_IDENTITIES = 11;
 
47
 
 
48
                private readonly int SSH2_AGENT_IDENTITIES_ANSWER = 12;
 
49
 
 
50
                private readonly int SSH2_AGENTC_SIGN_REQUEST = 13;
 
51
 
 
52
                private readonly int SSH2_AGENT_SIGN_RESPONSE = 14;
 
53
 
 
54
                private readonly int SSH2_AGENTC_ADD_IDENTITY = 17;
 
55
 
 
56
                private readonly int SSH2_AGENTC_REMOVE_IDENTITY = 18;
 
57
 
 
58
                private readonly int SSH2_AGENTC_REMOVE_ALL_IDENTITIES = 19;
 
59
 
 
60
                private readonly int SSH2_AGENT_FAILURE = 30;
 
61
 
 
62
                internal bool init = true;
 
63
 
 
64
                private Buffer rbuf = null;
 
65
 
 
66
                private Buffer wbuf = null;
 
67
 
 
68
                private Packet packet = null;
 
69
 
 
70
                private Buffer mbuf = null;
 
71
 
 
72
                public ChannelAgentForwarding() : base()
 
73
                {
 
74
                        SetLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX);
 
75
                        SetLocalWindowSize(LOCAL_WINDOW_SIZE_MAX);
 
76
                        SetLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE);
 
77
                        type = Util.Str2byte("auth-agent@openssh.com");
 
78
                        rbuf = new Buffer();
 
79
                        rbuf.Reset();
 
80
                        //wbuf=new Buffer(rmpsize);
 
81
                        //packet=new Packet(wbuf);
 
82
                        mbuf = new Buffer();
 
83
                        connected = true;
 
84
                }
 
85
 
 
86
                public override void Run()
 
87
                {
 
88
                        try
 
89
                        {
 
90
                                SendOpenConfirmation();
 
91
                        }
 
92
                        catch (Exception)
 
93
                        {
 
94
                                close = true;
 
95
                                Disconnect();
 
96
                        }
 
97
                }
 
98
 
 
99
                /// <exception cref="System.IO.IOException"></exception>
 
100
                internal override void Write(byte[] foo, int s, int l)
 
101
                {
 
102
                        if (packet == null)
 
103
                        {
 
104
                                wbuf = new Buffer(rmpsize);
 
105
                                packet = new Packet(wbuf);
 
106
                        }
 
107
                        rbuf.Shift();
 
108
                        if (rbuf.buffer.Length < rbuf.index + l)
 
109
                        {
 
110
                                byte[] newbuf = new byte[rbuf.s + l];
 
111
                                System.Array.Copy(rbuf.buffer, 0, newbuf, 0, rbuf.buffer.Length);
 
112
                                rbuf.buffer = newbuf;
 
113
                        }
 
114
                        rbuf.PutByte(foo, s, l);
 
115
                        int mlen = rbuf.GetInt();
 
116
                        if (mlen > rbuf.GetLength())
 
117
                        {
 
118
                                rbuf.s -= 4;
 
119
                                return;
 
120
                        }
 
121
                        int typ = rbuf.GetByte();
 
122
                        Session _session = null;
 
123
                        try
 
124
                        {
 
125
                                _session = GetSession();
 
126
                        }
 
127
                        catch (JSchException e)
 
128
                        {
 
129
                                throw new IOException(e.ToString());
 
130
                        }
 
131
                        ArrayList identities = _session.jsch.identities;
 
132
                        UserInfo userinfo = _session.GetUserInfo();
 
133
                        if (typ == SSH2_AGENTC_REQUEST_IDENTITIES)
 
134
                        {
 
135
                                mbuf.Reset();
 
136
                                mbuf.PutByte(unchecked((byte)SSH2_AGENT_IDENTITIES_ANSWER));
 
137
                                lock (identities)
 
138
                                {
 
139
                                        int count = 0;
 
140
                                        for (int i = 0; i < identities.Count; i++)
 
141
                                        {
 
142
                                                Identity identity = (Identity)(identities[i]);
 
143
                                                if (identity.GetPublicKeyBlob() != null)
 
144
                                                {
 
145
                                                        count++;
 
146
                                                }
 
147
                                        }
 
148
                                        mbuf.PutInt(count);
 
149
                                        for (int i_1 = 0; i_1 < identities.Count; i_1++)
 
150
                                        {
 
151
                                                Identity identity = (Identity)(identities[i_1]);
 
152
                                                byte[] pubkeyblob = identity.GetPublicKeyBlob();
 
153
                                                if (pubkeyblob == null)
 
154
                                                {
 
155
                                                        continue;
 
156
                                                }
 
157
                                                mbuf.PutString(pubkeyblob);
 
158
                                                mbuf.PutString(Util.empty);
 
159
                                        }
 
160
                                }
 
161
                                byte[] bar = new byte[mbuf.GetLength()];
 
162
                                mbuf.GetByte(bar);
 
163
                                Send(bar);
 
164
                        }
 
165
                        else
 
166
                        {
 
167
                                if (typ == SSH2_AGENTC_SIGN_REQUEST)
 
168
                                {
 
169
                                        byte[] blob = rbuf.GetString();
 
170
                                        byte[] data = rbuf.GetString();
 
171
                                        int flags = rbuf.GetInt();
 
172
                                        //      if((flags & 1)!=0){ //SSH_AGENT_OLD_SIGNATURE // old OpenSSH 2.0, 2.1
 
173
                                        //        datafellows = SSH_BUG_SIGBLOB;
 
174
                                        //      }
 
175
                                        Identity identity = null;
 
176
                                        lock (identities)
 
177
                                        {
 
178
                                                for (int i = 0; i < identities.Count; i++)
 
179
                                                {
 
180
                                                        Identity _identity = (Identity)(identities[i]);
 
181
                                                        if (_identity.GetPublicKeyBlob() == null)
 
182
                                                        {
 
183
                                                                continue;
 
184
                                                        }
 
185
                                                        if (!Util.Array_equals(blob, _identity.GetPublicKeyBlob()))
 
186
                                                        {
 
187
                                                                continue;
 
188
                                                        }
 
189
                                                        if (_identity.IsEncrypted())
 
190
                                                        {
 
191
                                                                if (userinfo == null)
 
192
                                                                {
 
193
                                                                        continue;
 
194
                                                                }
 
195
                                                                while (_identity.IsEncrypted())
 
196
                                                                {
 
197
                                                                        if (!userinfo.PromptPassphrase("Passphrase for " + _identity.GetName()))
 
198
                                                                        {
 
199
                                                                                break;
 
200
                                                                        }
 
201
                                                                        string _passphrase = userinfo.GetPassphrase();
 
202
                                                                        if (_passphrase == null)
 
203
                                                                        {
 
204
                                                                                break;
 
205
                                                                        }
 
206
                                                                        byte[] passphrase = Util.Str2byte(_passphrase);
 
207
                                                                        try
 
208
                                                                        {
 
209
                                                                                if (_identity.SetPassphrase(passphrase))
 
210
                                                                                {
 
211
                                                                                        break;
 
212
                                                                                }
 
213
                                                                        }
 
214
                                                                        catch (JSchException)
 
215
                                                                        {
 
216
                                                                                break;
 
217
                                                                        }
 
218
                                                                }
 
219
                                                        }
 
220
                                                        if (!_identity.IsEncrypted())
 
221
                                                        {
 
222
                                                                identity = _identity;
 
223
                                                                break;
 
224
                                                        }
 
225
                                                }
 
226
                                        }
 
227
                                        byte[] signature = null;
 
228
                                        if (identity != null)
 
229
                                        {
 
230
                                                signature = identity.GetSignature(data);
 
231
                                        }
 
232
                                        mbuf.Reset();
 
233
                                        if (signature == null)
 
234
                                        {
 
235
                                                mbuf.PutByte(unchecked((byte)SSH2_AGENT_FAILURE));
 
236
                                        }
 
237
                                        else
 
238
                                        {
 
239
                                                mbuf.PutByte(unchecked((byte)SSH2_AGENT_SIGN_RESPONSE));
 
240
                                                mbuf.PutString(signature);
 
241
                                        }
 
242
                                        byte[] bar = new byte[mbuf.GetLength()];
 
243
                                        mbuf.GetByte(bar);
 
244
                                        Send(bar);
 
245
                                }
 
246
                        }
 
247
                }
 
248
 
 
249
                private void Send(byte[] message)
 
250
                {
 
251
                        packet.Reset();
 
252
                        wbuf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA));
 
253
                        wbuf.PutInt(recipient);
 
254
                        wbuf.PutInt(4 + message.Length);
 
255
                        wbuf.PutString(message);
 
256
                        try
 
257
                        {
 
258
                                GetSession().Write(packet, this, 4 + message.Length);
 
259
                        }
 
260
                        catch (Exception)
 
261
                        {
 
262
                        }
 
263
                }
 
264
        }
 
265
}