qtransportauth_qws.h
上传用户:detong
上传日期:2022-06-22
资源大小:20675k
文件大小:7k
源码类别:

系统编程

开发平台:

Unix_Linux

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  4. ** Contact: Qt Software Information (qt-info@nokia.com)
  5. **
  6. ** This file is part of the QtGui module of the Qt Toolkit.
  7. **
  8. ** Commercial Usage
  9. ** Licensees holding valid Qt Commercial licenses may use this file in
  10. ** accordance with the Qt Commercial License Agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and Nokia.
  13. **
  14. **
  15. ** GNU General Public License Usage
  16. ** Alternatively, this file may be used under the terms of the GNU
  17. ** General Public License versions 2.0 or 3.0 as published by the Free
  18. ** Software Foundation and appearing in the file LICENSE.GPL included in
  19. ** the packaging of this file.  Please review the following information
  20. ** to ensure GNU General Public Licensing requirements will be met:
  21. ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
  22. ** http://www.gnu.org/copyleft/gpl.html.
  23. **
  24. ** Qt for Windows(R) Licensees
  25. ** As a special exception, Nokia, as the sole copyright holder for Qt
  26. ** Designer, grants users of the Qt/Eclipse Integration plug-in the
  27. ** right for the Qt/Eclipse Integration to link to functionality
  28. ** provided by Qt Designer and its related libraries.
  29. **
  30. ** If you are unsure which license is appropriate for your use, please
  31. ** contact the sales department at qt-sales@nokia.com.
  32. **
  33. ****************************************************************************/
  34. #ifndef QTRANSPORTAUTH_QWS_H
  35. #define QTRANSPORTAUTH_QWS_H
  36. #include <QtCore/qglobal.h>
  37. #if !defined(QT_NO_SXE) || defined(SXE_INSTALLER)
  38. #include <QtCore/qobject.h>
  39. #include <QtCore/qhash.h>
  40. #include <QtCore/qstring.h>
  41. #include <QtCore/qbuffer.h>
  42. #include <QtCore/qpointer.h>
  43. #include <sys/types.h>
  44. QT_BEGIN_HEADER
  45. QT_BEGIN_NAMESPACE
  46. QT_MODULE(Gui)
  47. class QAuthDevice;
  48. class QWSClient;
  49. class QIODevice;
  50. class QTransportAuthPrivate;
  51. class QMutex;
  52. class Q_GUI_EXPORT QTransportAuth : public QObject
  53. {
  54.     Q_OBJECT
  55. public:
  56.     static QTransportAuth *getInstance();
  57.     enum Result {
  58.         // Error codes
  59.         Pending = 0x00,
  60.         TooSmall = 0x01,
  61.         CacheMiss = 0x02,
  62.         NoMagic = 0x03,
  63.         NoSuchKey = 0x04,
  64.         FailMatch = 0x05,
  65.         OutOfDate = 0x06,
  66.         // reserved for expansion
  67.         Success = 0x1e,
  68.         ErrMask = 0x1f,
  69.         // Verification codes
  70.         Allow = 0x20,
  71.         Deny = 0x40,
  72.         Ask = 0x60,
  73.         // reserved
  74.         StatusMask = 0xe0
  75.     };
  76.     enum Properties {
  77.         Trusted = 0x01,
  78.         Connection = 0x02,
  79.         UnixStreamSock = 0x04,
  80.         SharedMemory = 0x08,
  81.         MessageQueue = 0x10,
  82.         UDP = 0x20,
  83.         TCP = 0x40,
  84.         UserDefined = 0x80,
  85.         TransportType = 0xfc
  86.     };
  87.     struct Data
  88.     {
  89.         Data() { processId = -1; }
  90.         Data( unsigned char p, int d )
  91.             : properties( p )
  92.             , descriptor( d )
  93.             , processId( -1 )
  94.         {
  95.             if (( properties & TransportType ) == TCP ||
  96.                 ( properties & TransportType ) == UnixStreamSock )
  97.                 properties |= Connection;
  98.         }
  99.         unsigned char properties;
  100.         unsigned char progId;
  101.         unsigned char status;
  102.         unsigned int descriptor;   // socket fd or shmget key
  103.         pid_t processId;
  104.         bool trusted() const;
  105.         void setTrusted( bool );
  106.         bool connection() const;
  107.         void setConnection( bool );
  108.     };
  109.     static const char *errorString( const QTransportAuth::Data & );
  110.     QTransportAuth::Data *connectTransport( unsigned char, int );
  111.     QAuthDevice *authBuf( QTransportAuth::Data *, QIODevice * );
  112.     QAuthDevice *recvBuf( QTransportAuth::Data *, QIODevice * );
  113.     QIODevice *passThroughByClient( QWSClient * ) const;
  114.     void setKeyFilePath( const QString & );
  115.     QString keyFilePath() const;
  116.     const unsigned char *getClientKey( unsigned char progId );
  117.     void invalidateClientKeyCache();
  118.     QMutex *getKeyFileMutex();
  119.     void setLogFilePath( const QString & );
  120.     QString logFilePath() const;
  121.     void setPackageRegistry( QObject *registry );
  122.     bool isDiscoveryMode() const;
  123.     void setProcessKey( const char * );
  124.     void setProcessKey( const char *, const char * );
  125.     void registerPolicyReceiver( QObject * );
  126.     void unregisterPolicyReceiver( QObject * );
  127.     bool authToMessage( QTransportAuth::Data &d, char *hdr, const char *msg, int msgLen );
  128.     bool authFromMessage( QTransportAuth::Data &d, const char *msg, int msgLen );
  129.     bool authorizeRequest( QTransportAuth::Data &d, const QString &request );
  130. Q_SIGNALS:
  131.     void policyCheck( QTransportAuth::Data &, const QString & );
  132.     void authViolation( QTransportAuth::Data & );
  133. private Q_SLOTS:
  134.     void bufferDestroyed( QObject * );
  135. private:
  136.     // users should never construct their own
  137.     QTransportAuth();
  138.     ~QTransportAuth();
  139.     friend class QAuthDevice;
  140.     Q_DECLARE_PRIVATE(QTransportAuth)
  141. };
  142. class Q_GUI_EXPORT RequestAnalyzer
  143. {
  144. public:
  145.     RequestAnalyzer();
  146.     virtual ~RequestAnalyzer();
  147.     QString operator()( QByteArray *data ) { return analyze( data ); }
  148.     bool requireMoreData() const { return moreData; }
  149.     qint64 bytesAnalyzed() const { return dataSize; }
  150. protected:
  151.     virtual QString analyze( QByteArray * );
  152.     bool moreData;
  153.     qint64 dataSize;
  154. };
  155. /*!
  156.   internal
  157.   class QAuthDevice
  158.   brief Pass-through QIODevice sub-class for authentication.
  159.    Use this class to forward on or receive forwarded data over a real
  160.    device for authentication.
  161. */
  162. class Q_GUI_EXPORT QAuthDevice : public QIODevice
  163. {
  164.     Q_OBJECT
  165. public:
  166.     enum AuthDirection {
  167.         Receive,
  168.         Send
  169.     };
  170.     QAuthDevice( QIODevice *, QTransportAuth::Data *, AuthDirection );
  171.     ~QAuthDevice();
  172.     void setTarget( QIODevice *t ) { m_target = t; }
  173.     QIODevice *target() const { return m_target; }
  174.     void setClient( QObject* );
  175.     QObject *client() const;
  176.     void setRequestAnalyzer( RequestAnalyzer * );
  177.     bool isSequential() const;
  178.     bool atEnd() const;
  179.     qint64 bytesAvailable() const;
  180.     qint64 bytesToWrite() const;
  181.     bool seek( qint64 );
  182.     QByteArray & buffer();
  183. protected:
  184.     qint64 readData( char *, qint64 );
  185.     qint64 writeData(const char *, qint64 );
  186. private Q_SLOTS:
  187.     void recvReadyRead();
  188.     void targetBytesWritten( qint64 );
  189. private:
  190.     bool authorizeMessage();
  191.     QTransportAuth::Data *d;
  192.     AuthDirection way;
  193.     QIODevice *m_target;
  194.     QObject *m_client;
  195.     QByteArray msgQueue;
  196.     qint64 m_bytesAvailable;
  197.     qint64 m_skipWritten;
  198.     RequestAnalyzer *analyzer;
  199. };
  200. inline bool QAuthDevice::isSequential() const
  201. {
  202.     return true;
  203. }
  204. inline bool QAuthDevice::seek( qint64 )
  205. {
  206.     return false;
  207. }
  208. inline bool QAuthDevice::atEnd() const
  209. {
  210.     return msgQueue.isEmpty();
  211. }
  212. inline qint64 QAuthDevice::bytesAvailable() const
  213. {
  214.     if ( way == Receive )
  215.         return m_bytesAvailable;
  216.     else
  217.         return ( m_target ? m_target->bytesAvailable() : 0 );
  218. }
  219. inline qint64 QAuthDevice::bytesToWrite() const
  220. {
  221.     return msgQueue.size();
  222. }
  223. inline QByteArray &QAuthDevice::buffer()
  224. {
  225.     return msgQueue;
  226. }
  227. QT_END_NAMESPACE
  228. QT_END_HEADER
  229. #endif // QT_NO_SXE
  230. #endif // QTRANSPORTAUTH_QWS_H