summaryrefslogtreecommitdiff
path: root/open-vm-tools/services/plugins/vix/vixToolsEnvVars.c
blob: 6f63b9d24afec45948de8007987d5a989daef5df (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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/*********************************************************
 * Copyright (C) 2010 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.
 *
 *********************************************************/

/*
 * vixToolsEnvVars.c --
 *
 *      Routines that encapsulate the complexity of dealing with
 *      environment variables when the process may be impersonating
 *      a user.
 */

#include <stdlib.h>
#ifdef __APPLE__
#include <crt_externs.h>
#endif

#include "util.h"
#include "unicode.h"
#include "dynbuf.h"
#include "str.h"
#include "posix.h"
#include "vixToolsInt.h"


#ifndef _WIN32
extern char **environ;
#endif

struct VixToolsEnvIterator {
#ifdef _WIN32
   enum {
      VIX_TOOLS_ENV_TYPE_ENV_BLOCK = 1,
      VIX_TOOLS_ENV_TYPE_ENVIRON,
   } envType;
   union {
      /* Used when envType is VIX_TOOLS_ENV_TYPE_ENV_BLOCK. */
      struct {
         wchar_t *envBlock;     // Keep the original around to free.
         wchar_t *currEnvVar;
      } eb;
      /* Used when envType is VIX_TOOLS_ENV_TYPE_ENVIRON. */
      wchar_t **environ;
   } data;
#else
   char **environ;
#endif
};


struct VixToolsUserEnvironment {
#ifdef _WIN32
   Bool impersonated;
   wchar_t *envBlock;      // Only used when impersonated == TRUE.
#else
   // The POSIX versions don't need any state currently.
#endif
};



/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsNewEnvIterator --
 *
 *      Create a new environment variable iterator for the user
 *      represented by 'userToken'.
 *      The resulting VixToolsEnvIterator must be freed using
 *      VixToolsDestroyEnvIterator.
 *
 * Results:
 *      VixError
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

VixError
VixToolsNewEnvIterator(void *userToken,                  // IN
#ifdef __FreeBSD__
                       char **envp,                      // IN
#endif
                       VixToolsEnvIterator **envItr)     // OUT
{
   VixError err = VIX_OK;
   VixToolsEnvIterator *it = Util_SafeMalloc(sizeof *it);

   if (NULL == envItr) {
      err = VIX_E_FAIL;
      goto abort;
   }

   *envItr = NULL;

#ifdef _WIN32
   if (PROCESS_CREATOR_USER_TOKEN != userToken) {
      /*
       * The process is impersonating a user, so retrieve the user's
       * environment block instead of using the process's environment.
       */
      it->envType = VIX_TOOLS_ENV_TYPE_ENV_BLOCK;
      err = VixToolsGetEnvBlock(userToken, &it->data.eb.envBlock);
      if (VIX_FAILED(err)) {
         goto abort;
      }
      it->data.eb.currEnvVar = it->data.eb.envBlock;
   } else {
      /*
       * The action is being performed as the user running the process
       * so the process's environment is fine.
       * TODO: Is this totally equivilent to the behavior when impersonated?
       * Would fetching the environment block include changes to the user's
       * or system's environment made after the process is running?
       */
      it->envType = VIX_TOOLS_ENV_TYPE_ENVIRON;
      it->data.environ = _wenviron;
   }
#elif defined(__APPLE__)
   it->environ = *_NSGetEnviron();
#elif defined(__FreeBSD__)
   it->environ = envp;
#else
   it->environ = environ;
#endif
   *envItr = it;
abort:
   if (VIX_FAILED(err)) {
      free(it);
   }

   return err;
}


/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsGetNextEnvVar --
 *
 *      Get the next envariable variable pair in the form NAME=VALUE.
 *
 * Results:
 *      A heap-allocated UTF-8 string, or NULL when the iterator has
 *      reached the end.
 *
 * Side effects:
 *      Advances the iterator.
 *
 *-----------------------------------------------------------------------------
 */

char *
VixToolsGetNextEnvVar(VixToolsEnvIterator *envItr)    // IN
{
   char *envVar;

   if (NULL == envItr) {
      return NULL;
   }

#ifdef _WIN32
   if (VIX_TOOLS_ENV_TYPE_ENV_BLOCK == envItr->envType) {
      if (L'\0' == envItr->data.eb.currEnvVar[0]) {
         envVar = NULL;
      } else {
         envVar = Unicode_AllocWithUTF16(envItr->data.eb.currEnvVar);
         while(*envItr->data.eb.currEnvVar++);
      }
   } else if (VIX_TOOLS_ENV_TYPE_ENVIRON == envItr->envType) {
      if (NULL == *envItr->data.environ) {
         envVar = NULL;
      } else {
         envVar = Unicode_AllocWithUTF16(*envItr->data.environ);
         envItr->data.environ++;
      }
   } else {
      /* Is someone using uninitialized memory? */
      NOT_IMPLEMENTED();
   }
#else
   if (NULL == *envItr->environ) {
      envVar = NULL;
   } else {
      envVar = Unicode_Alloc(*envItr->environ, STRING_ENCODING_DEFAULT);
      envItr->environ++;
   }
#endif
   return envVar;
}


/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsDestroyEnvIterator --
 *
 *      Free()s any memory associated with the VixToolsEnvIterator.
 *
 * Results:
 *      None
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

void
VixToolsDestroyEnvIterator(VixToolsEnvIterator *envItr)   // IN
{
   if (NULL != envItr) {
#ifdef _WIN32
      if (VIX_TOOLS_ENV_TYPE_ENV_BLOCK == envItr->envType) {
         if (NULL != envItr->data.eb.envBlock) {
            VixToolsDestroyEnvironmentBlock(envItr->data.eb.envBlock);
         }
      }
#endif
      free(envItr);
   }
}


/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsNewUserEnvironment --
 *
 *      Create a new UserEnvironment that can be used to query for
 *      environment variables.
 *
 * Results:
 *      VixError
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

VixError
VixToolsNewUserEnvironment(void *userToken,                   // IN
                           VixToolsUserEnvironment **env)     // OUT
{
   VixError err = VIX_OK;
   VixToolsUserEnvironment *myEnv = Util_SafeMalloc(sizeof *myEnv);

   if (NULL == env) {
      err = VIX_E_FAIL;
      goto abort;
   }

   *env = NULL;

#ifdef _WIN32
   if (PROCESS_CREATOR_USER_TOKEN != userToken) {
      myEnv->impersonated = TRUE;
      err = VixToolsGetEnvBlock(userToken, &myEnv->envBlock);
      if (VIX_FAILED(err)) {
         goto abort;
      }
   } else {
      myEnv->impersonated = FALSE;
      /* We will just read from the process's environment. */
   }
#endif

   *env = myEnv;

abort:
   if (VIX_FAILED(err)) {
      free(myEnv);
   }

   return err;
}


/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsGetEnvFromUserEnvironment --
 *
 *      Looks up the environment variable given by 'name' in the provided
 *      user environment.
 *
 * Results:
 *      A heap-allocated UTF-8 string, or NULL if the environment variable
 *      is not found.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

char *
VixToolsGetEnvFromUserEnvironment(const VixToolsUserEnvironment *env,  // IN
                                  const char *name)                    // IN
{
   char *envVar;

   if (NULL == env) {
      return NULL;
   }

#ifdef _WIN32
   if (env->impersonated) {
      envVar = VixToolsGetEnvVarFromEnvBlock(env->envBlock, name);
   } else {
      envVar = Util_SafeStrdup(Posix_Getenv(name));
   }
#else
   envVar = Util_SafeStrdup(Posix_Getenv(name));
#endif

   return envVar;
}


/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsDestroyUserEnvironment --
 *
 *      Releases any resources used by the VixToolsUserEnvironment.
 *      The VixToolsUserEnvironment must not be used after calling
 *      this function.
 *
 * Results:
 *      None
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

void
VixToolsDestroyUserEnvironment(VixToolsUserEnvironment *env)   // IN
{
   if (NULL != env) {
#ifdef _WIN32
      if (NULL != env->envBlock) {
         if (env->impersonated) {
            VixToolsDestroyEnvironmentBlock(env->envBlock);
         }
      }
#endif
      free(env);
   }
}


#ifdef _WIN32
/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsEnvironToEnvBlock --
 *
 *      Converts a NULL terminated array of UTF-8 environment variables in
 *      the form NAME=VALUE to an Win32 environment block, which is a single
 *      contiguous array containing UTF-16 environment variables in the same
 *      form, each separated by a UTF-16 null character, followed by two
 *      training null characters.
 *
 * Results:
 *      VixError
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

VixError
VixToolsEnvironToEnvBlock(char const * const *environ,    // IN: UTF-8
                          wchar_t **envBlock)             // OUT
{
   VixError err;
   DynBuf buf;
   Bool res;
   static const wchar_t nullTerm[] = { L'\0', L'\0' };

   DynBuf_Init(&buf);

   if ((NULL == environ) || (NULL == envBlock)) {
      err = VIX_E_FAIL;
      goto abort;
   }

   *envBlock = NULL;

   while (NULL != *environ) {
      wchar_t *envVar = Unicode_GetAllocUTF16(*environ);

      res = DynBuf_Append(&buf, envVar,
                          (wcslen(envVar) + 1) * sizeof(*envVar));
      free(envVar);
      if (!res) {
         err = VIX_E_OUT_OF_MEMORY;
         goto abort;
      }
      environ++;
   }

   /*
    * Append two null characters at the end. This adds an extra (third) null
    * if there was at least one environment variable (since there already
    * is one after the last string) but we need both if there were no
    * environment variables in the input array. I'll waste two bytes to
    * keep the code a little simpler.
    */
   res = DynBuf_Append(&buf, nullTerm, sizeof nullTerm);
   if (!res) {
      err = VIX_E_OUT_OF_MEMORY;
      goto abort;
   }

   *envBlock = DynBuf_Detach(&buf);
   err = VIX_OK;

abort:
   DynBuf_Destroy(&buf);

   return err;
}
#endif


/*
 *-----------------------------------------------------------------------------
 *
 * VixToolsValidateEnviron --
 *
 *      Ensures that the NULL terminated array of strings contains
 *      properly formated environment variables.
 *
 * Results:
 *      VixError
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

VixError
VixToolsValidateEnviron(char const * const *environ)   // IN
{
   if (NULL == environ) {
      return VIX_E_FAIL;
   }

   while (NULL != *environ) {
      /*
       * Each string should contain at least one '=', to delineate between
       * the name and the value.
       */
      if (NULL == Str_Strchr(*environ, '=')) {
         return VIX_E_INVALID_ARG;
      }
      environ++;
   }

   return VIX_OK;
}


#ifdef VMX86_DEVEL
#ifdef _WIN32
/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsEnvironToEnvBlockEmptyEnviron --
 *
 *      Tests VixToolsEnvironToEnvBlock() with an empty environment: an
 *      char ** pointing to a single NULL pointer.
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsEnvironToEnvBlockEmptyEnviron(void)
{
   const char *environ1[] = { NULL };
   wchar_t *envBlock;
   VixError err;

   err = VixToolsEnvironToEnvBlock(environ1, &envBlock);
   ASSERT(VIX_OK == err);

   ASSERT((L'\0' == envBlock[0]) && (L'\0' == envBlock[1]));
   free(envBlock);
}


/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsEnvironToEnvBlockTwoGood --
 *
 *      Tests VixToolsEnvironToEnvBlock() with an environment containing
 *      two valid entries.
 *
 * Results:
 *      Passes or ASSERTs
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsEnvironToEnvBlockTwoGood(void)
{
   const char *environ1[] = { "foo=bar", "env=block", NULL };
   wchar_t *envBlock, *currPos;
   VixError err;

   err = VixToolsEnvironToEnvBlock(environ1, &envBlock);
   ASSERT(VIX_OK == err);

   currPos = envBlock;
   ASSERT(wcscmp(currPos, L"foo=bar") == 0);
   currPos += wcslen(L"foo=bar") + 1;
   ASSERT(wcscmp(currPos, L"env=block") == 0);
   free(envBlock);
}


/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsEnvironToEnvBlock --
 *
 *      Runs unit tests for VixToolsEnvironToEnvBlock().
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsEnvironToEnvBlock(void)
{
   TestVixToolsEnvironToEnvBlockEmptyEnviron();
   TestVixToolsEnvironToEnvBlockTwoGood();
}
#endif // #ifdef _WIN32


/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsValidateEnvironEmptyEnviron --
 *
 *      Tests VixToolsEnvironToEnvBlock() with an empty environment.
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsValidateEnvironEmptyEnviron(void)
{
   const char *environ1[] = { NULL };
   VixError err;

   err = VixToolsValidateEnviron(environ1);
   ASSERT(VIX_OK == err);
}


/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsValidateEnvironTwoGoodVars --
 *
 *      Tests VixToolsEnvironToEnvBlock() with an environment containing
 *      two valid environment variables.
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsValidateEnvironTwoGoodVars(void)
{
   const char *environ1[] = { "foo=bar", "vix=api", NULL };
   VixError err;

   err = VixToolsValidateEnviron(environ1);
   ASSERT(VIX_OK == err);
}


/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsValidateEnvironOneBad --
 *
 *      Tests VixToolsEnvironToEnvBlock() with an environment containing
 *      one invalid environment variable.
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsValidateEnvironOneBad(void)
{
   const char *environ1[] = { "noequals", NULL };
   VixError err;

   err = VixToolsValidateEnviron(environ1);
   ASSERT(VIX_E_INVALID_ARG == err);
}


/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsValidateEnvironSecondBad --
 *
 *      Tests VixToolsEnvironToEnvBlock() with an environment containing
 *      one valid environment variable followed by one invalid environment
 *      variable.
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsValidateEnvironSecondBad(void)
{
   const char *environ1[] = { "foo=bar", "noequals", NULL };
   VixError err;

   err = VixToolsValidateEnviron(environ1);
   ASSERT(VIX_E_INVALID_ARG == err);
}


/*
 *-----------------------------------------------------------------------------
 *
 * TestVixToolsValidateEnviron --
 *
 *      Run unit tests for VixToolsValidateEnviron().
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

static void
TestVixToolsValidateEnviron(void)
{
   TestVixToolsValidateEnvironEmptyEnviron();
   TestVixToolsValidateEnvironTwoGoodVars();
   TestVixToolsValidateEnvironOneBad();
   TestVixToolsValidateEnvironSecondBad();
}


/*
 *-----------------------------------------------------------------------------
 *
 * TextVixToolsEnvVars --
 *
 *      Run unit tests for functions in this file.
 *
 * Results:
 *      Passes or ASSERTs.
 *
 * Side effects:
 *      None
 *
 *-----------------------------------------------------------------------------
 */

void
TestVixToolsEnvVars(void)
{
#ifdef _WIN32
   TestVixToolsEnvironToEnvBlock();
#endif
   TestVixToolsValidateEnviron();
}
#endif // #ifdef VMX86_DEVEL