~n-muench/ubuntu/precise/open-vm-tools/open-vm-tools-precise.sid-merge1

« back to all changes in this revision

Viewing changes to lib/SLPv2Parser/SLPv2MsgParser.c

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*********************************************************
2
 
 * Copyright (C) 2005 VMware, Inc. All rights reserved.
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify it
5
 
 * under the terms of the GNU Lesser General Public License as published
6
 
 * by the Free Software Foundation version 2.1 and no later version.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
11
 
 * License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program; if not, write to the Free Software Foundation, Inc.,
15
 
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
16
 
 *
17
 
 *********************************************************/
18
 
#include "vmware.h"
19
 
 
20
 
#ifdef WIN32
21
 
#include <winsock.h>
22
 
#else
23
 
#include <sys/types.h>
24
 
#include <netinet/in.h>
25
 
#endif
26
 
#include <string.h>
27
 
#include <stdlib.h>
28
 
 
29
 
#include "str.h"
30
 
#include "util.h"
31
 
#include "SLPv2.h"
32
 
#include "SLPv2Private.h"
33
 
 
34
 
/*
35
 
 *-----------------------------------------------------------------------------
36
 
 *
37
 
 * SLPv2MsgParserStringValid -
38
 
 *
39
 
 *      Returns TRUE if the string at 'offset' bytes into 'packet'
40
 
 *      actually fits inside the packet, which is 'len' bytes.
41
 
 *
42
 
 * Results:
43
 
 *      Returns TRUE upon success, FALSE upon memory error.
44
 
 *
45
 
 * Side effects:
46
 
 *      None
47
 
 *
48
 
 *-----------------------------------------------------------------------------
49
 
 */
50
 
 
51
 
Bool
52
 
SLPv2MsgParserStringValid(char *packet,  // IN
53
 
                          int len,       // IN
54
 
                          int offset)    // IN
55
 
{
56
 
   uint16 stringLength = Portable_ntohs(*((uint16 *) (packet + len)));
57
 
   return (offset + stringLength > len) ? FALSE : TRUE;
58
 
}
59
 
 
60
 
/*
61
 
 *-----------------------------------------------------------------------------
62
 
 *
63
 
 * SLPv2MsgParserGetString -
64
 
 *
65
 
 *      Returns a C string, given the (16 bit length) Pascal string at
66
 
 *      packet+offset.
67
 
 *
68
 
 * Results:
69
 
 *      Returns a C string.
70
 
 *
71
 
 * Side effects:
72
 
 *      None
73
 
 *
74
 
 *-----------------------------------------------------------------------------
75
 
 */
76
 
 
77
 
char *
78
 
SLPv2MsgParserGetString(char *packet,     // IN
79
 
                        int packetLength, // IN
80
 
                        int offset,       // IN
81
 
                        Bool *ok)         // OUT
82
 
{
83
 
   uint16 stringLength = Portable_ntohs(*((uint16 *) (packet + offset)));
84
 
   char *string = NULL;
85
 
   Bool myOk = TRUE;
86
 
 
87
 
   /* make sure the string actually fits in the packet */
88
 
   if (offset + stringLength > packetLength) {
89
 
      if (NULL != ok) {
90
 
         myOk = FALSE;
91
 
      }
92
 
      goto bye;
93
 
   }
94
 
 
95
 
   string = Util_SafeMalloc(stringLength + 1);
96
 
 
97
 
   /*
98
 
    * We use memcpy() because Str_Strcpy()  doesn't handle non-NULL
99
 
    * terminated strings.
100
 
    */
101
 
   memcpy(string, packet + offset + 2, stringLength);
102
 
   string[stringLength] = '\0';
103
 
 
104
 
bye:
105
 
   if (NULL != ok) {
106
 
      if (*ok == FALSE && myOk == TRUE) {
107
 
         myOk = FALSE;
108
 
      }
109
 
      *ok = myOk;
110
 
   }
111
 
   return string;
112
 
}
113
 
 
114
 
/*
115
 
 *-----------------------------------------------------------------------------
116
 
 *
117
 
 * SLPv2MsgParser_Init -
118
 
 *
119
 
 *      Initializes a SLPv2_Parse structure.
120
 
 *
121
 
 * Results:
122
 
 *      Returns a pointer to a SLPv2_Parse structure or NULL upon error.
123
 
 *
124
 
 * Side effects:
125
 
 *      None
126
 
 *
127
 
 *-----------------------------------------------------------------------------
128
 
 */
129
 
 
130
 
struct SLPv2_Parse *
131
 
SLPv2MsgParser_Init()
132
 
{
133
 
   struct SLPv2_Parse *parse;
134
 
 
135
 
   parse = (struct SLPv2_Parse *) Util_SafeMalloc(sizeof (struct SLPv2_Parse));
136
 
   parse->header = NULL;
137
 
   parse->languageTag = NULL;
138
 
 
139
 
   parse->serviceRequest.prList = NULL;
140
 
   parse->serviceRequest.serviceType = NULL;
141
 
   parse->serviceRequest.scope = NULL;
142
 
   parse->serviceRequest.predicate = NULL;
143
 
   parse->serviceRequest.spi = NULL;
144
 
 
145
 
   parse->serviceReply.error = 0;
146
 
   parse->serviceReply.urlCount = 0;
147
 
   parse->serviceReply.url = NULL;
148
 
 
149
 
   parse->attributeRequest.prList   = NULL;
150
 
   parse->attributeRequest.url      = NULL;
151
 
   parse->attributeRequest.scope    = NULL;
152
 
   parse->attributeRequest.tagList  = NULL;
153
 
   parse->attributeRequest.spi      = NULL;
154
 
 
155
 
   parse->attributeReply.error         = 0;
156
 
   parse->attributeReply.attributeList = NULL;
157
 
 
158
 
   return parse;
159
 
}
160
 
 
161
 
/*
162
 
 *-----------------------------------------------------------------------------
163
 
 *
164
 
 * SLPv2MsgParserGetHeader -
165
 
 *
166
 
 *      Populates the SLPv2_Parse structure with SLPv2 header data.
167
 
 *
168
 
 * Results:
169
 
 *      Returns TRUE upon success, FALSE upon memory error.
170
 
 *
171
 
 * Side effects:
172
 
 *      None
173
 
 *
174
 
 *-----------------------------------------------------------------------------
175
 
 */
176
 
 
177
 
Bool
178
 
SLPv2MsgParserGetHeader(char *packet,               // IN
179
 
                        int len,                    // IN
180
 
                        struct SLPv2_Parse *parse)  // IN
181
 
{
182
 
   uint16 languageTagOffset = sizeof(struct SLPv2_Header);
183
 
   Bool parseOk = TRUE;
184
 
   uint32 lengthTemp;
185
 
   uint8 *lengthArrayTemp = (uint8 *) &lengthTemp;
186
 
   
187
 
   parse->header = (struct SLPv2_Header *) packet;
188
 
 
189
 
   if (len < sizeof(struct SLPv2_Header)) {
190
 
      return FALSE;
191
 
   }
192
 
 
193
 
   if (SLPV2_VERSION != parse->header->version) {
194
 
      return FALSE;
195
 
   }
196
 
 
197
 
   parse->languageTagLength = Portable_ntohs(*((uint16 *) (packet + languageTagOffset)));
198
 
 
199
 
   parse->languageTag = SLPv2MsgParserGetString(packet,
200
 
                                                len,
201
 
                                                languageTagOffset,
202
 
                                                &parseOk);
203
 
   if (! parseOk) {
204
 
      return FALSE;
205
 
   }
206
 
 
207
 
   
208
 
   lengthArrayTemp[0] = parse->header->length[0];
209
 
   lengthArrayTemp[1] = parse->header->length[1];
210
 
   lengthArrayTemp[2] = parse->header->length[2];
211
 
   lengthTemp = Portable_ntohl(lengthTemp);
212
 
   parse->header->length[0] = lengthArrayTemp[0];
213
 
   parse->header->length[1] = lengthArrayTemp[1];
214
 
   parse->header->length[2] = lengthArrayTemp[2];
215
 
   
216
 
   parse->header->xid = Portable_ntohs(parse->header->xid);
217
 
 
218
 
   return TRUE;
219
 
}
220
 
 
221
 
/*
222
 
 *-----------------------------------------------------------------------------
223
 
 *
224
 
 * SLPv2MsgParserParseServiceRequest -
225
 
 *
226
 
 *      Populates the SLPv2_Parse structure with SLPv2 Service Request data.
227
 
 *
228
 
 * Results:
229
 
 *      Returns TRUE upon success, FALSE upon memory error.
230
 
 *
231
 
 * Side effects:
232
 
 *      None
233
 
 *
234
 
 *-----------------------------------------------------------------------------
235
 
 */
236
 
 
237
 
Bool
238
 
SLPv2MsgParserParseServiceRequest(char *packet,               // IN
239
 
                                int len,                    // IN
240
 
                                struct SLPv2_Parse *parse)  // IN
241
 
{
242
 
   Bool parseOkay = TRUE;
243
 
 
244
 
   /* previous responder list */
245
 
   uint16 prOffset = sizeof(struct SLPv2_Header) + parse->languageTagLength + 2;
246
 
   uint16 prLength = Portable_ntohs(*((uint16 *) (packet + prOffset)));
247
 
 
248
 
   /* service type */
249
 
   uint16 stOffset = prOffset + prLength + 2;
250
 
   uint16 stLength = Portable_ntohs(*((uint16 *) (packet + stOffset)));
251
 
 
252
 
   /* scope list */
253
 
   uint16 slOffset = stOffset + stLength + 2;
254
 
   uint16 slLength = Portable_ntohs(*((uint16 *) (packet + slOffset)));
255
 
 
256
 
   /* predicate */
257
 
   uint16 predicateOffset = slOffset + slLength + 2;
258
 
   uint16 predicateLength = Portable_ntohs(*((uint16 *) (packet + predicateOffset)));
259
 
 
260
 
   /* security parameter index */
261
 
   uint16 spiOffset = predicateOffset + predicateLength + 2;
262
 
 
263
 
   parse->serviceRequest.prList = SLPv2MsgParserGetString(packet, len,
264
 
                                                          prOffset, &parseOkay);
265
 
   parse->serviceRequest.serviceType = SLPv2MsgParserGetString(packet, len,
266
 
                                                          stOffset, &parseOkay);
267
 
   parse->serviceRequest.scope = SLPv2MsgParserGetString(packet, len,
268
 
                                                          slOffset, &parseOkay);
269
 
   parse->serviceRequest.predicate = SLPv2MsgParserGetString(packet, len,
270
 
                                                   predicateOffset, &parseOkay);
271
 
   parse->serviceRequest.spi = SLPv2MsgParserGetString(packet, len,
272
 
                                                         spiOffset, &parseOkay);
273
 
   if (! parseOkay) {
274
 
      return FALSE;
275
 
   }
276
 
 
277
 
   return TRUE;
278
 
}
279
 
 
280
 
                                  
281
 
/*
282
 
 *-----------------------------------------------------------------------------
283
 
 *
284
 
 * SLPv2MsgParserParseServiceReply -
285
 
 *
286
 
 *      Populates the SLPv2_Parse structure with SLPv2 Service Reply data.
287
 
 *
288
 
 * Results:
289
 
 *      Returns TRUE upon success, FALSE upon memory error.
290
 
 *
291
 
 * Side effects:
292
 
 *      None
293
 
 *
294
 
 *-----------------------------------------------------------------------------
295
 
 */
296
 
 
297
 
Bool
298
 
SLPv2MsgParserParseServiceReply(char *packet,               // IN
299
 
                              int len,                    // IN
300
 
                              struct SLPv2_Parse *parse)  // IN
301
 
{
302
 
   uint16 errorOffset;
303
 
   uint16 urlCountOffset;
304
 
   uint16 urlOffset;
305
 
   Bool   parseOk = TRUE;
306
 
   int    i;
307
 
 
308
 
   /* error code */
309
 
   errorOffset = sizeof(struct SLPv2_Header) + parse->languageTagLength + 2;
310
 
   parse->serviceReply.error = Portable_ntohs(*((uint16 *) (packet + errorOffset)));
311
 
 
312
 
   urlCountOffset = errorOffset + 2;
313
 
   parse->serviceReply.urlCount = Portable_ntohs(*((uint16 *) (packet
314
 
                                                           + urlCountOffset)));
315
 
   parse->serviceReply.url = Util_SafeMalloc(sizeof(char *)
316
 
                                         * parse->serviceReply.urlCount);
317
 
   urlOffset = urlCountOffset + 2;
318
 
 
319
 
   /*
320
 
    * Zero out the URL array so if we fail in the middle of
321
 
    * populating it, * SLPv2MsgParser_Destroy will free a bunch
322
 
    * of NULLs instead of dangling pointers.
323
 
    */
324
 
   for (i = 0; i < parse->serviceReply.urlCount; i++) {
325
 
      parse->serviceReply.url[i] = NULL;
326
 
   }
327
 
 
328
 
   for (i = 0; i < parse->serviceReply.urlCount; i++) {
329
 
      uint16 urlLength = Portable_ntohs(*((uint16 *) (packet + urlOffset)));
330
 
 
331
 
      parse->serviceReply.url[i] = SLPv2MsgParserGetString(packet, len,
332
 
                                                         urlOffset, &parseOk);
333
 
      if (! parseOk) {
334
 
         return FALSE;
335
 
      }
336
 
      urlOffset = urlOffset + urlLength + 2;
337
 
   }
338
 
 
339
 
   return TRUE;
340
 
}
341
 
 
342
 
 
343
 
/*
344
 
 *-----------------------------------------------------------------------------
345
 
 *
346
 
 * SLPv2MsgParserParseAttributeRequest -
347
 
 *
348
 
 *      Populates the SLPv2_Parse structure with SLPv2 Attribute Request data.
349
 
 *
350
 
 * Results:
351
 
 *      Returns TRUE upon success, FALSE upon memory error.
352
 
 *
353
 
 * Side effects:
354
 
 *      None
355
 
 *
356
 
 *-----------------------------------------------------------------------------
357
 
 */
358
 
 
359
 
Bool
360
 
SLPv2MsgParserParseAttributeRequest(char *packet,               // IN
361
 
                                  int len,                    // IN
362
 
                                  struct SLPv2_Parse *parse)  // IN
363
 
{
364
 
   Bool parseOkay = TRUE;
365
 
 
366
 
   /* previous responder list */
367
 
   uint16 prOffset = sizeof(struct SLPv2_Header) + parse->languageTagLength + 2;
368
 
   uint16 prLength = Portable_ntohs(*((uint16 *) (packet + prOffset)));
369
 
 
370
 
   /* url */
371
 
   uint16 urlOffset = prOffset + prLength + 2;
372
 
   uint16 urlLength = Portable_ntohs(*((uint16 *) (packet + urlOffset)));
373
 
 
374
 
   /* scope list */
375
 
   uint16 slOffset = urlOffset + urlLength + 2;
376
 
   uint16 slLength = Portable_ntohs(*((uint16 *) (packet + slOffset)));
377
 
 
378
 
   /* tag list */
379
 
   uint16 tagOffset = slOffset + slLength + 2;
380
 
   uint16 tagLength = Portable_ntohs(*((uint16 *) (packet + tagOffset)));
381
 
 
382
 
   /* security parameter index */
383
 
   uint16 spiOffset = tagOffset + tagLength + 2;
384
 
 
385
 
 
386
 
   parse->attributeRequest.prList  = SLPv2MsgParserGetString(packet, len,
387
 
                                                          prOffset, &parseOkay);
388
 
   parse->attributeRequest.url     = SLPv2MsgParserGetString(packet, len,
389
 
                                                         urlOffset, &parseOkay);
390
 
   parse->attributeRequest.scope   = SLPv2MsgParserGetString(packet, len,
391
 
                                                          slOffset, &parseOkay);
392
 
   parse->attributeRequest.tagList = SLPv2MsgParserGetString(packet, len,
393
 
                                                         tagOffset, &parseOkay);
394
 
   parse->attributeRequest.spi     = SLPv2MsgParserGetString(packet, len,
395
 
                                                         spiOffset, &parseOkay);
396
 
 
397
 
   if (! parseOkay) {
398
 
      return FALSE;
399
 
   }
400
 
 
401
 
   return TRUE;
402
 
}
403
 
 
404
 
 
405
 
/*
406
 
 *-----------------------------------------------------------------------------
407
 
 *
408
 
 * SLPv2MsgParserParseAttributeReply -
409
 
 *
410
 
 *      Populates the SLPv2_Parse structure with SLPv2 Attribute Reply data.
411
 
 *
412
 
 * Results:
413
 
 *      Returns TRUE upon success, FALSE upon memory error.
414
 
 *
415
 
 * Side effects:
416
 
 *      None
417
 
 *
418
 
 *-----------------------------------------------------------------------------
419
 
 */
420
 
 
421
 
Bool
422
 
SLPv2MsgParserParseAttributeReply(char *packet,               // IN
423
 
                                int len,                    // IN
424
 
                                struct SLPv2_Parse *parse)  // IN
425
 
{
426
 
   uint16 errorOffset;
427
 
   uint16 attributeOffset;
428
 
   Bool parseOkay = TRUE;
429
 
   /* error code */
430
 
   errorOffset = sizeof(struct SLPv2_Header) + parse->languageTagLength + 2;
431
 
   parse->attributeReply.error = Portable_ntohs(*((uint16 *) (packet + errorOffset)));
432
 
 
433
 
   /* attribute list */
434
 
   attributeOffset = errorOffset + 2;
435
 
   parse->attributeReply.attributeList = SLPv2MsgParserGetString(packet, len,
436
 
                                                   attributeOffset, &parseOkay);
437
 
 
438
 
   if (! parseOkay) {
439
 
      return FALSE;
440
 
   }
441
 
 
442
 
   return TRUE;
443
 
}
444
 
 
445
 
/*
446
 
 *-----------------------------------------------------------------------------
447
 
 *
448
 
 * SLPv2MsgParser_Parse -
449
 
 *
450
 
 *      Returns TRUE if the packet parses as a SLPv2 message.
451
 
 *
452
 
 * Results:
453
 
 *      Returns TRUE upon success, FALSE upon memory error.
454
 
 *
455
 
 * Side effects:
456
 
 *      None
457
 
 *
458
 
 *-----------------------------------------------------------------------------
459
 
 */
460
 
 
461
 
Bool
462
 
SLPv2MsgParser_Parse(struct SLPv2_Parse *parse,     // IN
463
 
                     char *packet,                  // IN
464
 
                     int len)                       // IN
465
 
{
466
 
   Bool parseOk = TRUE;
467
 
   ASSERT(NULL != parse);
468
 
 
469
 
   parseOk = SLPv2MsgParserGetHeader(packet, len, parse);
470
 
 
471
 
   if (parseOk) {
472
 
      switch (parse->header->functionId) {
473
 
      case SLPV2_SERVICEREQUEST:
474
 
         parseOk = SLPv2MsgParserParseServiceRequest(packet, len, parse);
475
 
         break;
476
 
      case SLPV2_SERVICEREPLY:
477
 
         parseOk = SLPv2MsgParserParseServiceReply(packet, len, parse);
478
 
         break;
479
 
      case SLPV2_ATTRIBUTEREQUEST:
480
 
         parseOk = SLPv2MsgParserParseAttributeRequest(packet, len, parse);
481
 
         break;
482
 
      case SLPV2_ATTRIBUTEREPLY:
483
 
         parseOk = SLPv2MsgParserParseAttributeReply(packet, len, parse);
484
 
         break;
485
 
      default:
486
 
         parseOk = FALSE;
487
 
      } // switch
488
 
   }  // if parseOk
489
 
 
490
 
   return parseOk;
491
 
}
492
 
 
493
 
 
494
 
/*
495
 
 *-----------------------------------------------------------------------------
496
 
 *
497
 
 * SLPv2MsgParser_Destroy -
498
 
 *
499
 
 *      Returns TRUE if the
500
 
 *
501
 
 * Results:
502
 
 *      Disposes of a SLPv2_Parse structure.
503
 
 *
504
 
 * Side effects:
505
 
 *      None
506
 
 *
507
 
 *-----------------------------------------------------------------------------
508
 
 */
509
 
 
510
 
void
511
 
SLPv2MsgParser_Destroy(struct SLPv2_Parse *parse) // IN
512
 
{
513
 
   int i;
514
 
 
515
 
   ASSERT(NULL != parse);
516
 
 
517
 
   /*
518
 
    * header.  We don't free(parse->header) because that's up to the caller
519
 
    * to manage, since the caller allocated the buffer that contains the
520
 
    * packet.
521
 
    */ 
522
 
 
523
 
   parse->header = NULL;
524
 
   free(parse->languageTag);
525
 
   parse->languageTag = NULL;
526
 
 
527
 
   /*
528
 
    * service request strings.
529
 
    */
530
 
   free(parse->serviceRequest.prList);
531
 
   free(parse->serviceRequest.serviceType);
532
 
   free(parse->serviceRequest.scope);
533
 
   free(parse->serviceRequest.predicate);
534
 
   free(parse->serviceRequest.spi);
535
 
   parse->serviceRequest.prList = NULL;
536
 
   parse->serviceRequest.serviceType = NULL;
537
 
   parse->serviceRequest.scope = NULL;
538
 
   parse->serviceRequest.predicate = NULL;
539
 
   parse->serviceRequest.spi = NULL;
540
 
   
541
 
   /*
542
 
    * service response.
543
 
    */
544
 
   for (i=0; i < parse->serviceReply.urlCount; i++) {
545
 
      free(parse->serviceReply.url[i]);
546
 
      parse->serviceReply.url[i] = NULL;
547
 
   }
548
 
   free(parse->serviceReply.url);
549
 
 
550
 
   /*
551
 
    * attribute request.
552
 
    */
553
 
   free(parse->attributeRequest.prList);
554
 
   free(parse->attributeRequest.url);
555
 
   free(parse->attributeRequest.scope);
556
 
   free(parse->attributeRequest.tagList);
557
 
   free(parse->attributeRequest.spi);
558
 
   parse->attributeRequest.prList = NULL;
559
 
   parse->attributeRequest.url = NULL;
560
 
   parse->attributeRequest.scope = NULL;
561
 
   parse->attributeRequest.tagList = NULL;
562
 
   parse->attributeRequest.spi = NULL;
563
 
 
564
 
   /*
565
 
    * attribute reply.
566
 
    */
567
 
   free(parse->attributeReply.attributeList);
568
 
   parse->attributeReply.attributeList = NULL;
569
 
 
570
 
   free(parse);
571
 
} // SLPv2MsgParser_Destroy
572