1 /* $OpenBSD: envtest.c,v 1.1 2010/08/23 22:34:37 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <unistd.h> 25 26 extern char **environ; 27 28 static int 29 count_instances(const char *name) 30 { 31 int count = 0; 32 size_t namelen; 33 char **ep; 34 35 namelen = strlen(name); 36 for (ep = environ; *ep != NULL; ep++) { 37 if (strncmp(name, *ep, namelen) == 0 && (*ep)[namelen] == '=') 38 count++; 39 } 40 41 return count; 42 } 43 44 static void 45 fake_env(void) 46 { 47 static char *fakenv[7]; 48 49 fakenv[0] = "HOME=/root"; 50 fakenv[1] = "USER=root"; 51 fakenv[2] = "LOGNAME=root"; 52 fakenv[3] = "SHELL=/bin/sh"; 53 fakenv[4] = "USER=root"; 54 fakenv[5] = NULL; 55 56 environ = fakenv; 57 } 58 59 int 60 main(int argc, char *argv[]) 61 { 62 char *buf; 63 int n, failures = 0; 64 size_t len, bufsize; 65 66 /* Enable malloc security options. */ 67 setenv("MALLOC_OPTIONS", "S", 0); 68 69 fake_env(); 70 n = count_instances("USER"); 71 if (n != 2) { 72 fprintf(stderr, "initial: %d instances of USER, expected %d\n", 73 n, 2); 74 failures++; 75 } 76 77 if (unsetenv("USER") != 0) { 78 fprintf(stderr, "unsetenv: failed to remove USER\n"); 79 failures++; 80 } 81 n = count_instances("USER"); 82 if (n != 0) { 83 fprintf(stderr, "unsetenv: %d instances of USER, expected %d\n", 84 n, 0); 85 failures++; 86 } 87 88 fake_env(); 89 if (setenv("USER", "nobody", 0) != 0) { 90 fprintf(stderr, "setenv: failed to set USER\n"); 91 failures++; 92 } 93 n = count_instances("USER"); 94 if (n != 2) { 95 fprintf(stderr, "setenv: %d instances of USER, expected %d\n", 96 n, 2); 97 failures++; 98 } 99 100 fake_env(); 101 if (setenv("USER", "nobody", 1) != 0) { 102 fprintf(stderr, "setenv: failed to set USER\n"); 103 failures++; 104 } 105 n = count_instances("USER"); 106 if (n != 1) { 107 fprintf(stderr, "setenv: %d instances of USER, expected %d\n", 108 n, 1); 109 failures++; 110 } 111 112 fake_env(); 113 if (putenv("USER=nobody") != 0) { 114 fprintf(stderr, "putenv: failed to set USER\n"); 115 failures++; 116 } 117 n = count_instances("USER"); 118 if (n != 1) { 119 fprintf(stderr, "putenv: %d instances of USER, expected %d\n", 120 n, 1); 121 failures++; 122 } 123 124 return failures; 125 } 126