1
2/*
3Copyright 1996, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26*/
27
28#ifdef HAVE_CONFIG_H
29# include "xconfig.h"
30#endif
31
32#include <stdio.h>
33#include <X11/Xos.h>
34#include <X11/Xfuncs.h>
35#include <X11/Xmd.h>
36#include <X11/ICE/ICElib.h>
37#include <X11/Intrinsic.h>
38
39#define Status int
40#define Bool int
41#define		True 1
42#define		False 0
43
44extern const char *PM_VENDOR_STRING;
45extern const char *PM_VENDOR_RELEASE;
46
47extern int verbose;
48
49typedef struct {
50    IceConn iceConn;
51    int pmOpcode;
52    int proto_major_version;
53    int proto_minor_version;
54    char *vendor;
55    char *release;
56} PMconn;
57
58
59
60extern void ForwardRequest ( PMconn *requestor, char *serviceName, char *serverAddress, char *hostAddress, char *startOptions, int authLen, char *authName, char *authData );
61extern int HostBasedAuthProc ( char *hostname );
62extern int InitWatchProcs ( XtAppContext appContext );
63extern void InstallIOErrorHandler ( void );
64extern int main ( int argc, char **argv );
65extern void MyIoErrorHandler ( IceConn ice_conn );
66extern void NewConnectionXtProc ( XtPointer client_data, int *source, XtInputId *id );
67extern void PMReplyProcessMessages ( IceConn iceConn, IcePointer clientData, int opcode, unsigned long length, int swap );
68extern void PMSetupProcessMessages ( IceConn iceConn, IcePointer clientData, int opcode, unsigned long length, int swap, IceReplyWaitInfo *replyWait, int *replyReadyRet );
69extern void SendGetProxyAddrReply ( PMconn *requestor, int status, const char *addr, const char *error );
70extern void SetCloseOnExec ( int fd );
71extern void Usage ( void ) _X_NORETURN;
72extern void _XtIceWatchProc ( IceConn ice_conn, IcePointer client_data, int opening, IcePointer *watch_data );
73extern void _XtProcessIceMsgProc ( XtPointer client_data, int *source, XtInputId *id );
74
75
76
77/*
78 * Pad to a 64 bit boundary
79 */
80
81#define PAD64(_bytes) ((8 - ((unsigned int) (_bytes) % 8)) % 8)
82
83#define PADDED_BYTES64(_bytes) (_bytes + PAD64 (_bytes))
84
85
86/*
87 * Number of 8 byte units in _bytes.
88 */
89
90#define WORD64COUNT(_bytes) (((unsigned int) ((_bytes) + 7)) >> 3)
91
92
93/*
94 * Compute the number of bytes for a STRING representation
95 */
96
97#define STRING_BYTES(_str) (2 + (_str ? strlen (_str) : 0) + \
98		     PAD64 (2 + (_str ? strlen (_str) : 0)))
99
100
101
102#define SKIP_STRING(_pBuf, _swap) \
103{ \
104    CARD16 _len; \
105    EXTRACT_CARD16 (_pBuf, _swap, _len); \
106    _pBuf += _len; \
107    if (PAD64 (2 + _len)) \
108        _pBuf += PAD64 (2 + _len); \
109}
110
111/*
112 * STORE macros
113 */
114
115#define STORE_CARD16(_pBuf, _val) \
116{ \
117    *((CARD16 *) _pBuf) = _val; \
118    _pBuf += 2; \
119}
120
121#define STORE_STRING(_pBuf, _string) \
122{ \
123    int _len = _string ? strlen (_string) : 0; \
124    STORE_CARD16 (_pBuf, _len); \
125    if (_len) { \
126        memcpy (_pBuf, _string, _len); \
127        _pBuf += _len; \
128    } \
129    if (PAD64 (2 + _len)) \
130        _pBuf += PAD64 (2 + _len); \
131}
132
133
134/*
135 * EXTRACT macros
136 */
137
138#define EXTRACT_CARD16(_pBuf, _swap, _val) \
139{ \
140    _val = *((CARD16 *) _pBuf); \
141    _pBuf += 2; \
142    if (_swap) \
143        _val = lswaps (_val); \
144}
145
146#define EXTRACT_STRING(_pBuf, _swap, _string) \
147{ \
148    CARD16 _len; \
149    EXTRACT_CARD16 (_pBuf, _swap, _len); \
150    _string = malloc (_len + 1); \
151    memcpy (_string, _pBuf, _len); \
152    _string[_len] = '\0'; \
153    _pBuf += _len; \
154    if (PAD64 (2 + _len)) \
155        _pBuf += PAD64 (2 + _len); \
156}
157
158
159/*
160 * Byte swapping
161 */
162
163/* byte swap a long literal */
164#define lswapl(_val) ((((_val) & 0xff) << 24) |\
165		   (((_val) & 0xff00) << 8) |\
166		   (((_val) & 0xff0000) >> 8) |\
167		   (((_val) >> 24) & 0xff))
168
169/* byte swap a short literal */
170#define lswaps(_val) ((((_val) & 0xff) << 8) | (((_val) >> 8) & 0xff))
171
172
173#define CHECK_AT_LEAST_SIZE(_iceConn, _majorOp, _minorOp, _expected_len, _actual_len, _severity) \
174    if ((((_actual_len) - SIZEOF (iceMsg)) >> 3) > _expected_len) \
175    { \
176       _IceErrorBadLength (_iceConn, _majorOp, _minorOp, _severity); \
177       return; \
178    }
179
180
181#define CHECK_COMPLETE_SIZE(_iceConn, _majorOp, _minorOp, _expected_len, _actual_len, _pStart, _severity) \
182    if (((PADDED_BYTES64((_actual_len)) - SIZEOF (iceMsg)) >> 3) \
183        != _expected_len) \
184    { \
185       _IceErrorBadLength (_iceConn, _majorOp, _minorOp, _severity); \
186       IceDisposeCompleteMessage (iceConn, _pStart); \
187       return; \
188    }
189