qtransportauth_qws.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QTRANSPORTAUTH_QWS_H
00043 #define QTRANSPORTAUTH_QWS_H
00044 
00045 #include <QtCore/qglobal.h>
00046 
00047 #if !defined(QT_NO_SXE) || defined(SXE_INSTALLER)
00048 
00049 #include <QtCore/qobject.h>
00050 #include <QtCore/qhash.h>
00051 #include <QtCore/qstring.h>
00052 #include <QtCore/qbuffer.h>
00053 #include <QtCore/qpointer.h>
00054 
00055 #include <sys/types.h>
00056 
00057 QT_BEGIN_HEADER
00058 
00059 QT_BEGIN_NAMESPACE
00060 
00061 QT_MODULE(Gui)
00062 
00063 class QAuthDevice;
00064 class QWSClient;
00065 class QIODevice;
00066 class QTransportAuthPrivate;
00067 class QMutex;
00068 
00069 class Q_GUI_EXPORT QTransportAuth : public QObject
00070 {
00071     Q_OBJECT
00072 public:
00073     static QTransportAuth *getInstance();
00074 
00075     enum Result {
00076         // Error codes
00077         Pending = 0x00,
00078         TooSmall = 0x01,
00079         CacheMiss = 0x02,
00080         NoMagic = 0x03,
00081         NoSuchKey = 0x04,
00082         FailMatch = 0x05,
00083         OutOfDate = 0x06,
00084         // reserved for expansion
00085         Success = 0x1e,
00086         ErrMask = 0x1f,
00087 
00088         // Verification codes
00089         Allow = 0x20,
00090         Deny = 0x40,
00091         Ask = 0x60,
00092         // reserved
00093         StatusMask = 0xe0
00094     };
00095 
00096     enum Properties {
00097         Trusted = 0x01,
00098         Connection = 0x02,
00099         UnixStreamSock = 0x04,
00100         SharedMemory = 0x08,
00101         MessageQueue = 0x10,
00102         UDP = 0x20,
00103         TCP = 0x40,
00104         UserDefined = 0x80,
00105         TransportType = 0xfc
00106     };
00107 
00108     struct Data
00109     {
00110         Data() { processId = -1; }
00111         Data( unsigned char p, int d )
00112             : properties( p )
00113             , descriptor( d )
00114             , processId( -1 )
00115         {
00116             if (( properties & TransportType ) == TCP ||
00117                 ( properties & TransportType ) == UnixStreamSock )
00118                 properties |= Connection;
00119         }
00120 
00121         unsigned char properties;
00122         unsigned char progId;
00123         unsigned char status;
00124         unsigned int descriptor;   // socket fd or shmget key
00125         pid_t processId;
00126 
00127         bool trusted() const;
00128         void setTrusted( bool );
00129         bool connection() const;
00130         void setConnection( bool );
00131     };
00132 
00133     static const char *errorString( const QTransportAuth::Data & );
00134 
00135     QTransportAuth::Data *connectTransport( unsigned char, int );
00136 
00137     QAuthDevice *authBuf( QTransportAuth::Data *, QIODevice * );
00138     QAuthDevice *recvBuf( QTransportAuth::Data *, QIODevice * );
00139     QIODevice *passThroughByClient( QWSClient * ) const;
00140 
00141     void setKeyFilePath( const QString & );
00142     QString keyFilePath() const;
00143     const unsigned char *getClientKey( unsigned char progId );
00144     void invalidateClientKeyCache();
00145     QMutex *getKeyFileMutex();
00146     void setLogFilePath( const QString & );
00147     QString logFilePath() const;
00148     void setPackageRegistry( QObject *registry );
00149     bool isDiscoveryMode() const;
00150     void setProcessKey( const char * );
00151     void setProcessKey( const char *, const char * );
00152     void registerPolicyReceiver( QObject * );
00153     void unregisterPolicyReceiver( QObject * );
00154 
00155     bool authToMessage( QTransportAuth::Data &d, char *hdr, const char *msg, int msgLen );
00156     bool authFromMessage( QTransportAuth::Data &d, const char *msg, int msgLen );
00157 
00158     bool authorizeRequest( QTransportAuth::Data &d, const QString &request );
00159 
00160 Q_SIGNALS:
00161     void policyCheck( QTransportAuth::Data &, const QString & );
00162     void authViolation( QTransportAuth::Data & );
00163 private Q_SLOTS:
00164     void bufferDestroyed( QObject * );
00165 
00166 private:
00167     // users should never construct their own
00168     QTransportAuth();
00169     ~QTransportAuth();
00170 
00171     friend class QAuthDevice;
00172     Q_DECLARE_PRIVATE(QTransportAuth)
00173 };
00174 
00175 class Q_GUI_EXPORT RequestAnalyzer
00176 {
00177 public:
00178     RequestAnalyzer();
00179     virtual ~RequestAnalyzer();
00180     QString operator()( QByteArray *data ) { return analyze( data ); }
00181     bool requireMoreData() const { return moreData; }
00182     qint64 bytesAnalyzed() const { return dataSize; }
00183 protected:
00184     virtual QString analyze( QByteArray * );
00185     bool moreData;
00186     qint64 dataSize;
00187 };
00188 
00198 class Q_GUI_EXPORT QAuthDevice : public QIODevice
00199 {
00200     Q_OBJECT
00201 public:
00202     enum AuthDirection {
00203         Receive,
00204         Send
00205     };
00206     QAuthDevice( QIODevice *, QTransportAuth::Data *, AuthDirection );
00207     ~QAuthDevice();
00208     void setTarget( QIODevice *t ) { m_target = t; }
00209     QIODevice *target() const { return m_target; }
00210     void setClient( QObject* );
00211     QObject *client() const;
00212     void setRequestAnalyzer( RequestAnalyzer * );
00213     bool isSequential() const;
00214     bool atEnd() const;
00215     qint64 bytesAvailable() const;
00216     qint64 bytesToWrite() const;
00217     bool seek( qint64 );
00218     QByteArray & buffer();
00219 
00220 protected:
00221     qint64 readData( char *, qint64 );
00222     qint64 writeData(const char *, qint64 );
00223 private Q_SLOTS:
00224     void recvReadyRead();
00225     void targetBytesWritten( qint64 );
00226 private:
00227     bool authorizeMessage();
00228 
00229     QTransportAuth::Data *d;
00230     AuthDirection way;
00231     QIODevice *m_target;
00232     QObject *m_client;
00233     QByteArray msgQueue;
00234     qint64 m_bytesAvailable;
00235     qint64 m_skipWritten;
00236 
00237     RequestAnalyzer *analyzer;
00238 };
00239 
00240 inline bool QAuthDevice::isSequential() const
00241 {
00242     return true;
00243 }
00244 
00245 inline bool QAuthDevice::seek( qint64 )
00246 {
00247     return false;
00248 }
00249 
00250 inline bool QAuthDevice::atEnd() const
00251 {
00252     return msgQueue.isEmpty();
00253 }
00254 
00255 inline qint64 QAuthDevice::bytesAvailable() const
00256 {
00257     if ( way == Receive )
00258         return m_bytesAvailable;
00259     else
00260         return ( m_target ? m_target->bytesAvailable() : 0 );
00261 }
00262 
00263 inline qint64 QAuthDevice::bytesToWrite() const
00264 {
00265     return msgQueue.size();
00266 }
00267 
00268 inline QByteArray &QAuthDevice::buffer()
00269 {
00270     return msgQueue;
00271 }
00272 
00273 
00274 
00275 
00276 QT_END_NAMESPACE
00277 
00278 QT_END_HEADER
00279 
00280 #endif // QT_NO_SXE
00281 #endif // QTRANSPORTAUTH_QWS_H