summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/sslDirect/sslStubs.c
blob: 28d71f143b4a96018329d268db7c4076992d2925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*********************************************************
 * Copyright (C) 2013-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * sslStubs.c --
 *
 *      Stubs for AsyncSokcet SSL functions without actually using SSL.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>

#include "str.h"

#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#endif

#include "log.h"
#include "err.h"
#include "msg.h"
#include "sslDirect.h"
#include "vm_assert.h"

#define LOGLEVEL_MODULE SSLStubs
#include "loglevel_user.h"


struct SSLSockStruct {
   int fd;
   Bool closeFdOnShutdown;
#ifdef __APPLE__
   Bool loggedKernelReadBug;
#endif
};


/*
 *----------------------------------------------------------------------
 *
 * SSL_Write()
 *
 *    Functional equivalent of the write() syscall.
 *
 * Results:
 *    Returns the number of bytes written, or -1 on error.
 *
 * Side effects:
 *
 *----------------------------------------------------------------------
 */

ssize_t
SSL_Write(SSLSock sslSock,   // IN
          const char *buf,   // IN
          size_t num)        // IN
{
   return send(sslSock->fd, buf, num, 0);
}


/*
 *----------------------------------------------------------------------
 *
 * SSL_Read --
 *
 *    Functional equivalent of the read() syscall.
 *
 * Results:
 *    Returns the number of bytes read, or -1 on error.  The
 *    data read will be placed in buf.
 *
 * Side effects:
 *
 *
 *----------------------------------------------------------------------
 */

ssize_t
SSL_Read(SSLSock sslSock,   // IN
         char *buf,         // OUT
         size_t num)        // IN
{
   int ret;
   ASSERT(sslSock);

#ifdef _WIN32
   ret = recv(sslSock->fd, buf, num, 0);
#else
   ret = read(sslSock->fd, buf, num);
#endif

#ifdef __APPLE__
   /*
    * Detect bug 161237 (Apple bug 5202831), which should no longer be
    * happening due to a workaround in our code.
    *
    * There is a bug on Mac OS 10.4 and 10.5 where passing an fd
    * over a socket can result in that fd being in an inconsistent state.
    * We can detect when this happens when read(2) returns zero
    * even if the other end of the socket is not disconnected.
    * We verify this by calling write(sslSock->fd, "", 0) and
    * see if it is okay. (If the socket was really closed, it would
    * return -1 with errno==EPIPE.)
    */
   if (ret == 0) {
      ssize_t writeRet;
#ifdef VMX86_DEBUG
      struct stat statBuffer;

      /*
       * Make sure we're using a socket.
       */
      ASSERT((fstat(sslSock->fd, &statBuffer) == 0) &&
             ((statBuffer.st_mode & S_IFSOCK) == S_IFSOCK));

#endif
      writeRet = write(sslSock->fd, "", 0);
      if (writeRet == 0) {
         /*
          * The socket is still good. read(2) should not have returned zero.
          */
         if (! sslSock->loggedKernelReadBug) {
            Log("Error: Encountered Apple bug #5202831.  Disconnecting.\n");
            sslSock->loggedKernelReadBug = TRUE;
         }
      }
   }
#endif

   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * SSL_GetFd()
 *
 *    Returns an socket's file descriptor or handle.
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

int
SSL_GetFd(SSLSock sslSock) // IN
{
   ASSERT(sslSock);

   return sslSock->fd;
}


/*
 *----------------------------------------------------------------------
 *
 * SSL_Pending()
 *
 *	  Always returns 0 for non-SSL socket.
 *
 * Results:
 *	  0
 *
 * Side effects:
 *    None.
 *
 *----------------------------------------------------------------------
 */

int
SSL_Pending(SSLSock sslSock) // IN
{
   return 0;
}


/*
 *----------------------------------------------------------------------
 *
 * SSL_New()
 *
 * Results:
 *    Returns a freshly allocated SSLSock structure.
 *
 * Side effects:
 *    None
 *
 *----------------------------------------------------------------------
 */

SSLSock
SSL_New(int fd,                       // IN
        Bool closeFdOnShutdown)       // IN
{
   SSLSock sslSock;

   sslSock = calloc(1, sizeof *sslSock);
   ASSERT_MEM_ALLOC(sslSock);
   sslSock->fd = fd;
   sslSock->closeFdOnShutdown = closeFdOnShutdown;

   return sslSock;
}


/*
 *----------------------------------------------------------------------
 *
 * SSL_Shutdown()
 *
 *    Functional equivalent of the close() syscall.  Does
 *    not close the actual fd used for the connection.
 *
 *
 * Results:
 *    0 on success, -1 on failure.
 *
 * Side effects:
 *    closes the connection, freeing up the memory associated
 *    with the passed in socket object
 *
 *----------------------------------------------------------------------
 */

int
SSL_Shutdown(SSLSock sslSock)     // IN
{
   int ret = 0;
   ASSERT(sslSock);

   LOG(10, ("Starting shutdown for %d\n", sslSock->fd));
   if (sslSock->closeFdOnShutdown) {
      LOG(10, ("Trying to close %d\n", sslSock->fd));

#ifdef _WIN32
      ret = closesocket(sslSock->fd);
#else
      ret = close(sslSock->fd);
#endif

   }

   free(sslSock);
   LOG(10, ("shutdown done\n"));

   return ret;
}


/*
 *----------------------------------------------------------------------
 *
 * SSL_SetCloseOnShutdownFlag()
 *
 *    Sets closeFdOnShutdown flag.
 *
 * Results:
 *    None.  Always succeeds.  Do not call close/closesocket on
 *    the fd after this, call SSL_Shutdown() instead.
 *
 * Side effects:
 *
 *----------------------------------------------------------------------
 */

void
SSL_SetCloseOnShutdownFlag(SSLSock sslSock)    // IN
{
   ASSERT(sslSock);
   sslSock->closeFdOnShutdown = TRUE;
}


/*
 *----------------------------------------------------------------------
 *
 * SSL_RecvDataAndFd --
 *
 *    recvmsg wrapper which can receive only file descriptors, not other
 *    control data.
 *
 * Results:
 *    Returns the number of bytes received, or -1 on error.  The
 *    data read will be placed in buf.  *fd is either -1 if no fd was
 *    received, or descriptor...
 *
 * Side effects:
 *
 *
 *----------------------------------------------------------------------
 */

ssize_t
SSL_RecvDataAndFd(SSLSock sslSock,   // IN/OUT
                  char *buf,         // OUT
                  size_t num,        // IN
                  int *fd)           // OUT
{
   int ret;
   ASSERT(sslSock);
   ASSERT(fd);

   *fd = -1;

   /*
    * No fd passing over socket or Windows. Windows needs different code.
    */
#ifdef _WIN32
   return SSL_Read(sslSock, buf, num);
#else
   {
      struct iovec iov;
      struct msghdr msg = { 0 };
      uint8 cmsgBuf[CMSG_SPACE(sizeof(int))];

      iov.iov_base = buf;
      iov.iov_len = num;
      msg.msg_name = NULL;
      msg.msg_namelen = 0;
      msg.msg_iov = &iov;
      msg.msg_iovlen = 1;
      msg.msg_control = cmsgBuf;
      msg.msg_controllen = sizeof cmsgBuf;
      ret = recvmsg(sslSock->fd, &msg, 0);
      if (ret >= 0 && msg.msg_controllen != 0) {
         struct cmsghdr *cmsg;

         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
            if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
               int receivedFd = *(int *)CMSG_DATA(cmsg);

               ASSERT(*fd == -1);
               *fd = receivedFd;
            }
         }
      }
   }
#endif

   return ret;
}


/*
 *-----------------------------------------------------------------------------
 *
 * SSL_TryCompleteAccept --
 *
 *    Stub only, should not be called when SSL is not used.
 * Results:
 *    0
 *
 * Side effects:
 *    None.
 *
 *-----------------------------------------------------------------------------
 */

int
SSL_TryCompleteAccept(SSLSock ssl) // IN
{
   ASSERT(0);
   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * SSL_WantRead --
 *
 *    Stub only, should not be called when SSL is not used.
 * Results:
 *    0
 *
 * Side effects:
 *    None.
 *
 *-----------------------------------------------------------------------------
 */

int
SSL_WantRead(const SSLSock ssl)
{
   ASSERT(0);
   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * SSL_SetupAcceptWithContext --
 *
 *    Stub only, should not be called when SSL is not used.
 * Results:
 *    FALSE
 *
 * Side effects:
 *    None.
 *
 *-----------------------------------------------------------------------------
 */

Bool
SSL_SetupAcceptWithContext(SSLSock sSock, // IN: SSL socket
                           void *ctx)     // IN: OpenSSL context (SSL_CTX *)
{
   ASSERT(0);
   return FALSE;
}