~ubuntu-branches/ubuntu/trusty/italc/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/git_allocate-rfbProtocolExtension-statically.patch/ica/src/ItalcVncServer.cpp

  • Committer: Package Import Robot
  • Author(s): Ryan Tandy
  • Date: 2015-02-25 12:22:56 UTC
  • Revision ID: package-import@ubuntu.com-20150225122256-q0p2y3jz7lbk567n
Tags: 1:2.0.1-4ubuntu0.14.04.1
debian/patches/git_allocate-rfbProtocolExtension-statically.patch: 
Import upstream patch to fix ica crashing when a client connects.
(LP: #1297345)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ItalcVncServer.cpp - implementation of ItalcVncServer, a VNC-server-
 
3
 *                      abstraction for platform independent VNC-server-usage
 
4
 *
 
5
 * Copyright (c) 2006-2010 Tobias Doerffel <tobydox/at/users/dot/sf/dot/net>
 
6
 *
 
7
 * This file is part of iTALC - http://italc.sourceforge.net
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public
 
20
 * License along with this program (see COPYING); if not, write to the
 
21
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
22
 * Boston, MA 02111-1307, USA.
 
23
 *
 
24
 */
 
25
 
 
26
#include <italcconfig.h>
 
27
 
 
28
#include "rfb/rfb.h"
 
29
#include "rfb/dh.h"
 
30
 
 
31
#ifdef ITALC_BUILD_WIN32
 
32
 
 
33
#include <windows.h>
 
34
#include <pthread.h>
 
35
 
 
36
#else
 
37
 
 
38
extern "C"
 
39
{
 
40
#include "d3des.h"
 
41
}
 
42
 
 
43
#endif
 
44
 
 
45
#include <QtCore/QCoreApplication>
 
46
#include <QtCore/QDir>
 
47
#include <QtCore/QProcess>
 
48
 
 
49
#include "ItalcVncServer.h"
 
50
#include "ItalcConfiguration.h"
 
51
#include "ItalcCore.h"
 
52
#include "ItalcCoreServer.h"
 
53
#include "ItalcRfbExt.h"
 
54
#include "Logger.h"
 
55
#include "LogonAuthentication.h"
 
56
 
 
57
 
 
58
extern "C" int x11vnc_main( int argc, char * * argv );
 
59
 
 
60
 
 
61
 
 
62
qint64 libvncServerDispatcher( char * _buf, const qint64 _len,
 
63
                                const SocketOpCodes _op_code, void * _user )
 
64
{
 
65
        rfbClientPtr cl = (rfbClientPtr) _user;
 
66
        switch( _op_code )
 
67
        {
 
68
                case SocketRead:
 
69
                        return rfbReadExact( cl, _buf, _len ) == 1 ? _len : 0;
 
70
                case SocketWrite:
 
71
                        return rfbWriteExact( cl, _buf, _len ) == 1 ? _len : 0;
 
72
                case SocketGetPeerAddress:
 
73
                        strncpy( _buf, cl->host, _len );
 
74
                        break;
 
75
        }
 
76
        return 0;
 
77
 
 
78
}
 
79
 
 
80
 
 
81
 
 
82
 
 
83
static rfbBool italcCoreNewClient( struct _rfbClientRec *, void * * )
 
84
{
 
85
        return true;
 
86
}
 
87
 
 
88
 
 
89
 
 
90
 
 
91
static rfbBool lvs_italcHandleMessage( struct _rfbClientRec *client,
 
92
                                        void *data,
 
93
                                        const rfbClientToServerMsg *message )
 
94
{
 
95
        if( message->type == rfbItalcCoreRequest )
 
96
        {
 
97
                return ItalcCoreServer::instance()->
 
98
                                handleItalcClientMessage( libvncServerDispatcher, client );
 
99
        }
 
100
        return false;
 
101
}
 
102
 
 
103
 
 
104
extern "C" void rfbClientSendString(rfbClientPtr cl, char *reason);
 
105
 
 
106
 
 
107
static void lvs_italcSecurityHandler( struct _rfbClientRec *cl )
 
108
{
 
109
        bool authOK = ItalcCoreServer::instance()->
 
110
                                                                authSecTypeItalc( libvncServerDispatcher, cl );
 
111
 
 
112
        uint32_t result = authOK ? rfbVncAuthOK : rfbVncAuthFailed;
 
113
        result = Swap32IfLE( result );
 
114
        rfbWriteExact( cl, (char *) &result, 4 );
 
115
 
 
116
        if( authOK )
 
117
        {
 
118
                cl->state = rfbClientRec::RFB_INITIALISATION;
 
119
        }
 
120
        else
 
121
        {
 
122
                rfbClientSendString( cl, (char *) "Signature verification failed!" );
 
123
    }
 
124
}
 
125
 
 
126
 
 
127
 
 
128
 
 
129
#ifndef ITALC_BUILD_WIN32
 
130
 
 
131
static void vncDecryptBytes(unsigned char *where, const int length, const unsigned char *key)
 
132
{
 
133
        int i, j;
 
134
        rfbDesKey((unsigned char*) key, DE1);
 
135
        for (i = length - 8; i > 0; i -= 8) {
 
136
                rfbDes(where + i, where + i);
 
137
                for (j = 0; j < 8; j++)
 
138
                        where[i + j] ^= where[i + j - 8];
 
139
        }
 
140
        /* i = 0 */
 
141
        rfbDes(where, where);
 
142
        for (i = 0; i < 8; i++)
 
143
                where[i] ^= key[i];
 
144
}
 
145
 
 
146
 
 
147
 
 
148
static bool authMsLogon( struct _rfbClientRec *cl )
 
149
{
 
150
        DiffieHellman dh;
 
151
        char gen[8], mod[8], pub[8], resp[8];
 
152
        char user[256], passwd[64];
 
153
        unsigned char key[8];
 
154
 
 
155
        dh.createKeys();
 
156
        int64ToBytes( dh.getValue(DH_GEN), gen );
 
157
        int64ToBytes( dh.getValue(DH_MOD), mod );
 
158
        int64ToBytes( dh.createInterKey(), pub );
 
159
        if( !rfbWriteExact( cl, gen, sizeof(gen) ) ) return false;
 
160
        if( !rfbWriteExact( cl, mod, sizeof(mod) ) ) return false;
 
161
        if( !rfbWriteExact( cl, pub, sizeof(pub) ) ) return false;
 
162
        if( !rfbReadExact( cl, resp, sizeof(resp) ) ) return false;
 
163
        if( !rfbReadExact( cl, user, sizeof(user) ) ) return false;
 
164
        if( !rfbReadExact( cl, passwd, sizeof(passwd) ) ) return false;
 
165
 
 
166
        int64ToBytes( dh.createEncryptionKey( bytesToInt64( resp ) ), (char*) key );
 
167
        vncDecryptBytes( (unsigned char*) user, sizeof(user), key ); user[255] = '\0';
 
168
        vncDecryptBytes( (unsigned char*) passwd, sizeof(passwd), key ); passwd[63] = '\0';
 
169
 
 
170
        AuthenticationCredentials credentials;
 
171
        credentials.setLogonUsername( user );
 
172
        credentials.setLogonPassword( passwd );
 
173
 
 
174
        return LogonAuthentication::authenticateUser( credentials );
 
175
}
 
176
 
 
177
 
 
178
 
 
179
static void lvs_msLogonIISecurityHandler( struct _rfbClientRec *cl )
 
180
{
 
181
        bool authOK = authMsLogon( cl );
 
182
 
 
183
        uint32_t result = authOK ? rfbVncAuthOK : rfbVncAuthFailed;
 
184
        result = Swap32IfLE( result );
 
185
        rfbWriteExact( cl, (char *) &result, 4 );
 
186
 
 
187
        if( authOK )
 
188
        {
 
189
                cl->state = rfbClientRec::RFB_INITIALISATION;
 
190
        }
 
191
        else
 
192
        {
 
193
                rfbClientSendString( cl, (char *) "MS Logon II authentication failed!" );
 
194
    }
 
195
}
 
196
 
 
197
#endif
 
198
 
 
199
 
 
200
#ifdef ITALC_BUILD_WIN32
 
201
 
 
202
extern bool Myinit( HINSTANCE hInstance );
 
203
extern int WinVNCAppMain();
 
204
 
 
205
#endif
 
206
 
 
207
 
 
208
 
 
209
ItalcVncServer::ItalcVncServer() :
 
210
        QThread(),
 
211
        m_port( ItalcCore::serverPort )
 
212
{
 
213
}
 
214
 
 
215
 
 
216
 
 
217
ItalcVncServer::~ItalcVncServer()
 
218
{
 
219
}
 
220
 
 
221
 
 
222
 
 
223
static void runX11vnc( QStringList cmdline, int port, bool plainVnc )
 
224
{
 
225
        cmdline
 
226
                << "-nosel"     // do not exchange clipboard-contents
 
227
                << "-nosetclipboard"    // do not exchange clipboard-contents
 
228
                << "-rfbport" << QString::number( port )
 
229
                                // set port where the VNC server should listen
 
230
                ;
 
231
 
 
232
#ifdef ITALC_BUILD_LINUX
 
233
        if( !plainVnc && ItalcCore::config->isHttpServerEnabled() )
 
234
        {
 
235
                QDir d( QCoreApplication::applicationDirPath() );
 
236
                if( d.cdUp() && d.cd( "share" ) && d.cd( "italc" ) &&
 
237
                                d.cd( "JavaViewer" ) )
 
238
                {
 
239
                        cmdline << "-httpport"
 
240
                                        << QString::number( ItalcCore::config->httpServerPort() )
 
241
                                        << "-httpdir" << d.absolutePath();
 
242
                        LogStream() << "Using JavaViewer files at" << d.absolutePath();
 
243
                }
 
244
                else
 
245
                {
 
246
                        qWarning( "Could not find JavaViewer files. "
 
247
                                                "Check your iTALC installation!" );
 
248
                }
 
249
        }
 
250
 
 
251
        // workaround for x11vnc when running in an NX session
 
252
        foreach( const QString &s, QProcess::systemEnvironment() )
 
253
        {
 
254
                if( s.startsWith( "NXSESSIONID=" ) || s.startsWith( "X2GO_SESSION=" ) )
 
255
                {
 
256
                        cmdline << "-noxdamage";
 
257
                }
 
258
        }
 
259
#endif
 
260
 
 
261
        // build new C-style command line array based on cmdline-QStringList
 
262
        char **argv = new char *[cmdline.size()+1];
 
263
        argv[0] = qstrdup( QCoreApplication::arguments()[0].toUtf8().constData() );
 
264
        int argc = 1;
 
265
 
 
266
        for( QStringList::iterator it = cmdline.begin();
 
267
                                it != cmdline.end(); ++it, ++argc )
 
268
        {
 
269
                argv[argc] = new char[it->length() + 1];
 
270
                strcpy( argv[argc], it->toUtf8().constData() );
 
271
        }
 
272
 
 
273
        if( plainVnc == false )
 
274
        {
 
275
                // register iTALC protocol extension
 
276
                rfbProtocolExtension pe;
 
277
                pe.newClient = italcCoreNewClient;
 
278
                pe.init = NULL;
 
279
                pe.enablePseudoEncoding = NULL;
 
280
                pe.pseudoEncodings = NULL;
 
281
                pe.handleMessage = lvs_italcHandleMessage;
 
282
                pe.close = NULL;
 
283
                pe.usage = NULL;
 
284
                pe.processArgument = NULL;
 
285
                pe.next = NULL;
 
286
                rfbRegisterProtocolExtension( &pe );
 
287
        }
 
288
 
 
289
        // register handler for iTALC's security-type
 
290
        rfbSecurityHandler shi = { rfbSecTypeItalc, lvs_italcSecurityHandler, NULL };
 
291
        rfbRegisterSecurityHandler( &shi );
 
292
 
 
293
#ifndef ITALC_BUILD_WIN32
 
294
        // register handler for MS Logon II security type
 
295
        rfbSecurityHandler shmsl = { rfbUltraVNC_MsLogonIIAuth, lvs_msLogonIISecurityHandler, NULL };
 
296
        rfbRegisterSecurityHandler( &shmsl );
 
297
 
 
298
        rfbSecurityHandler shmsl_legacy = { rfbMSLogon, lvs_msLogonIISecurityHandler, NULL };
 
299
        rfbRegisterSecurityHandler( &shmsl_legacy );
 
300
#endif
 
301
 
 
302
        // run x11vnc-server
 
303
        x11vnc_main( argc, argv );
 
304
 
 
305
}
 
306
 
 
307
 
 
308
 
 
309
void ItalcVncServer::runVncReflector( int srcPort, int dstPort )
 
310
{
 
311
#ifdef ITALC_BUILD_WIN32
 
312
        pthread_win32_process_attach_np();
 
313
        pthread_win32_thread_attach_np();
 
314
#endif
 
315
 
 
316
        QStringList args;
 
317
        args << "-viewonly"
 
318
                << "-reflect"
 
319
                << QString( "localhost:%1" ).arg( srcPort );
 
320
        if( ItalcCore::config->isDemoServerMultithreaded() )
 
321
        {
 
322
                args << "-threads";
 
323
        }
 
324
 
 
325
        while( 1 )
 
326
        {
 
327
                runX11vnc( args, dstPort, true );
 
328
        }
 
329
 
 
330
#ifdef ITALC_BUILD_WIN32
 
331
        pthread_win32_thread_detach_np();
 
332
        pthread_win32_process_detach_np();
 
333
#endif
 
334
}
 
335
 
 
336
 
 
337
 
 
338
void ItalcVncServer::run()
 
339
{
 
340
#ifdef ITALC_BUILD_LINUX
 
341
        QStringList cmdline;
 
342
 
 
343
        QStringList acceptedArguments;
 
344
        acceptedArguments
 
345
                        << "-noshm"
 
346
                        << "-solid"
 
347
                        << "-xrandr"
 
348
                        << "-onetile";
 
349
 
 
350
        foreach( const QString & arg, QCoreApplication::arguments() )
 
351
        {
 
352
                if( acceptedArguments.contains( arg ) )
 
353
                {
 
354
                        cmdline.append( arg );
 
355
                }
 
356
        }
 
357
 
 
358
        runX11vnc( cmdline, m_port, false );
 
359
 
 
360
#elif ITALC_BUILD_WIN32
 
361
 
 
362
        // run winvnc-server
 
363
        Myinit( GetModuleHandle( NULL ) );
 
364
        WinVNCAppMain();
 
365
 
 
366
#endif
 
367
 
 
368
}
 
369
 
 
370