int switch_user(struct russ_sconn *sconn) { uid_t uid; gid_t gid; uid = sconn->creds.uid; gid = sconn->creds.gid; #if 0 if (uid == 0) { russ_sconn_fatal(sconn, "error: cannot run for root (uid of 0)", -1); exit(0); } #endif /* set up env */ if ((chdir("/") < 0) || (clearenv() < 0)) { russ_sconn_fatal(sconn, "error: cannot set environment", RUSS_EXIT_FAILURE); exit(0); } /* switch user */ if (russ_switch_userinitgroups(uid, gid) < 0) { russ_sconn_fatal(sconn, RUSS_MSG_NOSWITCHUSER, RUSS_EXIT_FAILURE); exit(0); } return 0; }
static void setup_session(struct weston_launch *wl) { char **env; char *term; int i; if (wl->tty != STDIN_FILENO) { if (setsid() < 0) error(1, errno, "setsid failed"); if (ioctl(wl->tty, TIOCSCTTY, 0) < 0) error(1, errno, "TIOCSCTTY failed - tty is in use"); } term = getenv("TERM"); clearenv(); if (term) setenv("TERM", term, 1); setenv("USER", wl->pw->pw_name, 1); setenv("LOGNAME", wl->pw->pw_name, 1); setenv("HOME", wl->pw->pw_dir, 1); setenv("SHELL", wl->pw->pw_shell, 1); env = pam_getenvlist(wl->ph); if (env) { for (i = 0; env[i]; ++i) { if (putenv(env[i]) < 0) error(0, 0, "putenv %s failed", env[i]); } free(env); } }
int main(void) { pid_t pid; int status, wpid; char *argv[] = { "/bin/ls", "-laF", NULL, }; clearenv(); if ((pid = vfork()) == 0) { printf("Hi. I'm the child process...\n"); execvp(argv[0], argv); _exit(0); } printf("Hello. I'm the parent process.\n"); while (1) { wpid = wait(&status); if (wpid > 0 && wpid != pid) { continue; } if (wpid == pid) break; } printf("Child process exited.\nGoodbye.\n"); return EXIT_SUCCESS; }
/* The environment file must exist, may be empty, and is expected to be of the format: * key=value\0key=value\0... */ static void load_env(const char *env) { struct stat st; char *map, *k, *v; typeof(st.st_size) i; map_file(env, PROT_READ|PROT_WRITE, MAP_PRIVATE, &st, (void **)&map); pexit_if(clearenv() != 0, "Unable to clear environment"); if(!st.st_size) return; map[st.st_size - 1] = '\0'; /* ensure the mapping is null-terminated */ for(i = 0; i < st.st_size;) { k = &map[i]; i += strlen(k) + 1; exit_if((v = strchr(k, '=')) == NULL, "Malformed environment entry: \"%s\"", k); /* a private writable map is used permitting s/=/\0/ */ *v = '\0'; v++; pexit_if(setenv(k, v, 1) == -1, "Unable to set env variable: \"%s\"=\"%s\"", k, v); } }
int main(int argc, char **argv) { clearenv(); init(); printf("Initial: "); print(); check_perms(); printf("* TEMPORARY DROP \n"); drop_temporarily(); check_perms(); printf("* RESTORE \n"); restore(); check_perms(); printf("* PERNAMENT DROP \n"); drop_pernamently(); check_perms(); printf("* RESTORE (we should fail now) \n"); restore(); check_perms(); return 0; }
int lxc_attach_set_environment(enum lxc_attach_env_policy_t policy, char** extra_env, char** extra_keep) { /* TODO: implement extra_env, extra_keep * Rationale: * - extra_env is an array of strings of the form * "VAR=VALUE", which are to be set (after clearing or not, * depending on the value of the policy variable) * - extra_keep is an array of strings of the form * "VAR", which are extra environment variables to be kept * around after clearing (if that is done, otherwise, the * remain anyway) */ (void) extra_env; (void) extra_keep; if (policy == LXC_ATTACH_CLEAR_ENV) { if (clearenv()) { SYSERROR("failed to clear environment"); /* don't error out though */ } } if (putenv("container=lxc")) { SYSERROR("failed to set environment variable"); return -1; } return 0; }
/* * clearenv(3) * */ ssize_t alcove_sys_clearenv(alcove_state_t *ap, const char *arg, size_t len, char *reply, size_t rlen) { #ifdef __linux__ int rv = 0; UNUSED(ap); UNUSED(arg); UNUSED(len); rv = clearenv(); return (rv < 0) ? alcove_mk_errno(reply, rlen, errno) : alcove_mk_atom(reply, rlen, "ok"); #else UNUSED(ap); UNUSED(arg); UNUSED(len); environ = NULL; return alcove_mk_atom(reply, rlen, "ok"); #endif }
/* Stolen from su for GNU. Copyright (C) 1992-2004 Free Software Foundation, Inc. License: GPL v2 or later. */ static void modify_environment(const struct passwd *pw) { char *term; char *display; char *path; term = getenv("TERM"); display = getenv("DISPLAY"); path = getenv("PATH"); #if HAVE_CLEARENV clearenv(); #else extern char **environ; environ = n_malloc(2 * sizeof (char *)); environ[0] = 0; #endif if (term) do_putenv("TERM", term); if (display) do_putenv("DISPLAY", display); if (path) do_putenv("PATH", path); do_putenv("HOME", pw->pw_dir); do_putenv("SHELL", pw->pw_shell); do_putenv("USER", pw->pw_name); do_putenv("LOGNAME", pw->pw_name); }
void env_main(void) { char **ev; char **command = NULL; char *del = "="; if (toys.optflags) clearenv(); for (ev = toys.optargs; *ev != NULL; ev++) { char *env, *val = NULL; env = strtok(*ev, del); if (env) val = strtok(NULL, del); if (val) setenv(env, val, 1); else { command = ev; break; } } if (!command) { char **ep; if (environ) for (ep = environ; *ep; ep++) xputs(*ep); } else xexec_optargs(command - toys.optargs); }
int main(int argc, char **argv) { int c; progname = basename(*argv); if (argc < 2) usage(); clearenv(); putenv("HOME=/"); putenv("SHELL=/bin/sh"); putenv("UID=0"); putenv("USER=root"); putenv("PATH=/bin"); opterr = 0; optind = 1; while ((c = getopt(argc, argv, "n:")) != -1) { switch (c) { case 'n': putenv(optarg); break; default: usage(); break; } } execvp(*(argv+optind), argv+optind); return 127; }
int main(int argc, char *argv[]) { int j; char **ep; clearenv(); /* Erase entire environment */ /* Add any definitions specified on command line to environment */ for (j = 1; j < argc; j++) if (putenv(argv[j]) != 0) errExit("putenv: %s", argv[j]); /* Add a definition for GREET if one does not already exist */ if (setenv("GREET", "Hello world", 0) == -1) errExit("setenv"); /* Remove any existing definition of BYE */ unsetenv("BYE"); /* Display current environment */ for (ep = environ; *ep != NULL; ep++) puts(*ep); exit(EXIT_SUCCESS); }
int fpm_env_init_child(struct fpm_worker_pool_s *wp) /* {{{ */ { struct key_value_s *kv; char *title; spprintf(&title, 0, "pool %s", wp->config->name); fpm_env_setproctitle(title); efree(title); if (wp->config->clear_env) { clearenv(); } for (kv = wp->config->env; kv; kv = kv->next) { setenv(kv->key, kv->value, 1); } if (wp->user) { setenv("USER", wp->user, 1); } if (wp->home) { setenv("HOME", wp->home, 1); } return 0; }
void UserTable::RunAsUser(char* const* argv) const { struct passwd* pwd = getpwnam(m_user.c_str()); if ( pwd == NULL // check query result || setgid(pwd->pw_gid) != 0 // check GID || initgroups(m_user.c_str(),pwd->pw_gid) != 0 // check supplementary groups || setuid(pwd->pw_uid) != 0) // check UID { goto failed; } if (pwd->pw_uid != 0) { if (clearenv() != 0) goto failed; if ( setenv("LOGNAME", pwd->pw_name, 1) != 0 || setenv("USER", pwd->pw_name, 1) != 0 || setenv("USERNAME", pwd->pw_name, 1) != 0 || setenv("HOME", pwd->pw_dir, 1) != 0 || setenv("SHELL", pwd->pw_shell, 1) != 0 || setenv("PATH", DEFAULT_PATH, 1) != 0) { goto failed; } } execvp(argv[0], argv); // this may return only on failure failed: syslog(LOG_ERR, "cannot exec process: %s", strerror(errno)); _exit(1); }
/* * load environment variable */ bool_t loadenv(char * file) { struct xml * root, * env; root = xml_parse_file(file); if(!root || !root->name) return FALSE; if(strcmp(root->name, "environment") != 0) { xml_free(root); return FALSE; } clearenv(); for(env = xml_child(root, "env"); env; env = env->next) { if(env->txt) putenv(env->txt); } xml_free(root); return TRUE; }
int main (int argc, char *argv[]) { int i; char **var; /* Si intende aggiungere una variabile d'ambiente come argomento */ if (argc < 4) { fprintf(stderr, "Uso: %s name=value\n", argv[0]); exit(EXIT_FAILURE); } /* Cancella l'intero ambiente */ clearenv(); /* La variabile d'ambiente acquisita come argomento viene aggiunta alla environment list; ora vuota */ for (i = 1; i < argc; i++) if (putenv (argv[i]) != 0) { fprintf (stderr, "Err.(%s) - put env\n", strerror(errno)); exit(EXIT_FAILURE); } /* Si aggiunge una ulteriore variabile d'ambiente con setenv() */ if (setenv ("E-MAIL", "*****@*****.**", 0) < 0) { fprintf (stderr, "Err.(%s) - set env\n", strerror(errno)); exit(EXIT_FAILURE); } /* Poiche' l'intero ambiente e' stato cancellato da clearenv(), l'ouput sara' molto breve; l'environment list infatti comprendera' solo le due variabili d'ambiente aggiunte nell'ambito del processo in esecuzione */ for (var = environ; *var != NULL; var++) printf("%s\n", *var); exit (EXIT_SUCCESS); }
int main(int argc, char** argv) { set_env(); environ = NULL; test_env(); set_env(); environ[0] = NULL; test_env(); static char* emptyEnv[1] = {NULL}; set_env(); environ = emptyEnv; test_env(); set_env(); environ = (char**)calloc(1, sizeof(*environ)); test_env(); // clearenv() is not part of the POSIX specs #if 1 set_env(); clearenv(); test_env(); #endif return 0; }
TEST(UNISTD_TEST, clearenv) { extern char** environ; // Guarantee that environ is not initially empty... ASSERT_EQ(0, setenv("test-variable", "a", 1)); // Stash a copy. std::vector<char*> old_environ; for (size_t i = 0; environ[i] != NULL; ++i) { old_environ.push_back(strdup(environ[i])); } ASSERT_EQ(0, clearenv()); EXPECT_TRUE(environ == NULL || environ[0] == NULL); EXPECT_EQ(NULL, getenv("test-variable")); EXPECT_EQ(0, setenv("test-variable", "post-clear", 1)); EXPECT_STREQ("post-clear", getenv("test-variable")); // Put the old environment back. for (size_t i = 0; i < old_environ.size(); ++i) { EXPECT_EQ(0, putenv(old_environ[i])); } // Check it wasn't overwritten. EXPECT_STREQ("a", getenv("test-variable")); EXPECT_EQ(0, unsetenv("test-variable")); }
int xlsh_session_exec(pam_handle_t* handle, const char* session, const char* arg0) { struct passwd* pwinfo; const char* pwname; char terminal[256]; pid_t proc_shell; int proc_wait = 0; const char* _arg0 = arg0; if(!arg0) _arg0 = session; pam_get_item(handle, PAM_USER, (const void**)&pwname); pwinfo = getpwnam(pwname); if(!pwinfo) return XLSH_ERROR; if((proc_shell = fork()) == 0) { chdir(pwinfo->pw_dir); if(initgroups(pwname, pwinfo->pw_gid) == -1) exit(EXIT_FAILURE); if(setgid(pwinfo->pw_gid) == -1) exit(EXIT_FAILURE); if(setuid(pwinfo->pw_uid) == -1) exit(EXIT_FAILURE); if(getenv("TERM")) strncpy(terminal, getenv("TERM"), 256); else *terminal = 0; clearenv(); setenv("USER", pwinfo->pw_name, 1); setenv("LOGNAME", pwinfo->pw_name, 1); setenv("HOME", pwinfo->pw_dir, 1); setenv("PATH", xlsh_config[XLSH_ID_PATH].value, 1); if(xlsh_X) { setenv("SHELL", pwinfo->pw_shell, 1); setenv("DISPLAY", xlsh_config[XLSH_ID_DISPLAY].value, 1); if(libxlsh_proc_exec(XLSH_XRDB, 0) > 0) wait(&proc_wait); } else setenv("SHELL", session, 1); if(*terminal) setenv("TERM", terminal, 1); execlp(session, _arg0, (char*)0); exit(EXIT_FAILURE); } else if(proc_shell == -1) return XLSH_ERROR; return XLSH_EOK; }
END_TEST START_TEST(test_clearenv) { int ret; ret = clearenv(); fail_if(ret != -1); fail_if(errno != ENOSYS); }
bool Connection::receiveActions() { Logger::logDebug("Connection: enter: %s", __FUNCTION__); while (1) { uint32_t action = 0; // Get the action. recvMsg(&action); switch (action) { case INVOKER_MSG_EXEC: receiveExec(); break; case INVOKER_MSG_ARGS: receiveArgs(); break; case INVOKER_MSG_ENV: // Clean-up all the env variables clearenv(); receiveEnv(); break; case INVOKER_MSG_PRIO: receivePriority(); break; case INVOKER_MSG_DELAY: receiveDelay(); break; case INVOKER_MSG_IO: receiveIO(); break; case INVOKER_MSG_IDS: receiveIDs(); break; case INVOKER_MSG_END: sendMsg(INVOKER_MSG_ACK); if (m_sendPid) sendPid(getpid()); return true; default: Logger::logError("Connection: received invalid action (%08x)\n", action); return false; } } }
int main(int argc, char **argv) { GError *error = NULL; GOptionContext *context = NULL; uid_t orig_uid = -1; gid_t orig_gid = -1; EscalateHelper *helper = NULL; int exit_code = 2; clearenv(); umask(0077); context = g_option_context_new("- helper for pam_escalate.so"); if (!g_option_context_parse(context, &argc, &argv, &error)) { goto done; } if (argc > 1) { g_set_error(&error, ESCALATE_HELPER_ERROR, ESCALATE_HELPER_ERROR_EXTRA_ARGS, "Non-flag arguments are not accepted"); goto done; } orig_uid = getuid(); orig_gid = getgid(); if (orig_uid != geteuid()) { if (setuid(geteuid())) { g_error("setuid() failed: %s", g_strerror(errno)); } } if (orig_gid != getegid()) { if (setgid(getegid())) { g_error("setgid() failed: %s", g_strerror(errno)); } } helper = EscalateHelperNew(STDIN_FILENO, STDOUT_FILENO, orig_uid, orig_gid); if (!EscalateHelperHandleStart(helper, &error)) { goto done; } if (EscalateHelperDoAction(helper, &error)) { exit_code = 0; } else { exit_code = 1; } done: if (error) { g_printerr("Caught error: %s\n", error->message); g_error_free(error); } EscalateHelperFree(helper); return exit_code; }
static void free_mem (void) { /* Remove all traces. */ clearenv (); /* Now remove the search tree. */ __tdestroy (known_values, free); known_values = NULL; }
// fire-and-forget void send_request_intent(int uid, int pid) { char *cmd; if (asprintf(&cmd, "/system/bin/am start --ei pid %i --ei uid %i thejh.almighty/.AskPermissionActivity > /dev/null", pid, uid) < 0) err("alloc failed"); clearenv(); setenv("LD_LIBRARY_PATH", "/vendor/lib:/system/lib", 1); setenv("PATH", "/sbin:/system/sbin:/system/bin:/system/xbin", 1); setenv("BOOTCLASSPATH", "/system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar", 1); system(cmd); }
static void UCS_F_DTOR ucm_clear_env() { unsigned i; clearenv(); for (i = 0; i < ucm_malloc_hook_state.num_env_strs; ++i) { ucm_free(ucm_malloc_hook_state.env_strs[i], NULL); } ucm_free(ucm_malloc_hook_state.env_strs, NULL); }
void environment_restore(void) { Eina_List *l; char *e; if (!environment) return; clearenv(); EINA_LIST_FOREACH(environment, l, e) putenv(e); }
void __freeenvp( void ) { clearenv(); if( _RWD_environ ) { lib_free( _RWD_environ ); _RWD_environ = NULL; } if( _free_ep ) { lib_free( _free_ep ); _free_ep = NULL; } }
int main(int argc, char *argv[]) { int opt, es; struct option longopts[] = { {"ignore-environment", 0, NULL, 'i'}, {"unset", 0, NULL, 'u'}, {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'V'}, {0,0,0,0} }; while ((opt = getopt_long(argc, argv, "iu:Vh", longopts, NULL)) != -1) { switch(opt) { case 'i': #if defined (__linux__) es = clearenv(); if (es != 0) { g_error("Could not clear environment"); } #else while (*environ) { unsetenv(*environ); environ++; } #endif break; case 'u': es = unsetenv(optarg); if (es != 0) { char message[50]; sprintf(message, "Cound not unset %s", optarg); g_error(message); } break; case 'V': printf("%s (%s) version %s\n", APPNAME, APPSUITE, APPVERSION); printf("%s compiled on %s at %s\n", basename(__FILE__), __DATE__, __TIME__); exit(EXIT_SUCCESS); case 'h': show_help(); exit(EXIT_SUCCESS); default : show_help(); exit(EXIT_FAILURE); } } if (argc == optind) { /* Just dump all env variables */ if (print_all_env() != 0) { perror("print_all_env"); exit(EXIT_FAILURE); } } return EXIT_SUCCESS; }
int main(int _argc, char **_argv) { /* clear shell variables and re-assign a minimum set */ clearenv(); setenv("PATH", ":/bin:/usr/bin", 1); setenv("PROMPT", "$ ", 1); setenv("SHELL", _argv[0], 1); signal(SIGINT, SIG_IGN); /* ignore ^C */ while(1) { printf("%s", getenv("PROMPT")); if(!read_command()) break; split_command(); if(!argc) continue; expand_args(); /* process builtin commands */ if(!strcmp(argv[0],"exit")) { break; } else if(!strcmp(argv[0],"set")) { if(argc!=3) { fprintf(stderr, "set: two arguments required\n"); continue; } setenv(argv[1], argv[2], 1); } else if(!strcmp(argv[0], "cd")) { if(argc!=2) { fprintf(stderr, "cd: one argument required\n"); continue; } if(chdir(argv[1])==-1) { perror("cd"); } } else if(!strcmp(argv[0], "pwd")) { if(argc!=1) { fprintf(stderr, "pwd: no arguments allowed\n"); continue; } printf("%s\n", getcwd(command, BUF_LEN)); } else { /* run external command */ run_program(); } free_args(); } printf("\n"); return 0; }
int env_main(int argc UNUSED_PARAM, char **argv) { char **ep; unsigned opt; llist_t *unset_env = NULL; opt_complementary = "u::"; #if ENABLE_FEATURE_ENV_LONG_OPTIONS applet_long_options = env_longopts; #endif opt = getopt32(argv, "+iu:", &unset_env); argv += optind; if (*argv && LONE_DASH(argv[0])) { opt |= 1; ++argv; } if (opt & 1) { clearenv(); } while (unset_env) { char *var = llist_pop(&unset_env); /* This does not handle -uVAR=VAL * (coreutils _sets_ the variable in that case): */ /*unsetenv(var);*/ /* This does, but uses somewhan undocumented feature that * putenv("name_without_equal_sign") unsets the variable: */ putenv(var); } while (*argv && (strchr(*argv, '=') != NULL)) { if (putenv(*argv) < 0) { bb_perror_msg_and_die("putenv"); } ++argv; } if (*argv) { BB_EXECVP(*argv, argv); /* SUSv3-mandated exit codes. */ xfunc_error_retval = (errno == ENOENT) ? 127 : 126; bb_simple_perror_msg_and_die(*argv); } for (ep = environ; *ep; ep++) { puts(*ep); } fflush_stdout_and_exit(EXIT_SUCCESS); }
int main(void) { pid_t parent_pid; GInputStream *stdin_unix_stream; /* Nuke the environment to get a well-known and sanitized * environment to avoid attacks via e.g. the DBUS_SYSTEM_BUS_ADDRESS * environment variable and similar. */ if (clearenv () != 0) { FATAL_ERROR("Error clearing environment: %s\n", g_strerror (errno)); return 1; } #if !GLIB_CHECK_VERSION(2,36,0) g_type_init(); #endif loop = g_main_loop_new(NULL, FALSE); authority = polkit_authority_get_sync(NULL, NULL); parent_pid = getppid (); if (parent_pid == 1) { FATAL_ERROR("Parent process was reaped by init(1)\n"); return 1; } /* Do what pkexec does */ subject = polkit_unix_process_new_for_owner(parent_pid, 0, getuid ()); stdin_unix_stream = g_unix_input_stream_new(STDIN_FILENO, 0); stdin_stream = g_data_input_stream_new(stdin_unix_stream); g_data_input_stream_set_newline_type(stdin_stream, G_DATA_STREAM_NEWLINE_TYPE_LF); g_clear_object(&stdin_unix_stream); g_data_input_stream_read_line_async(stdin_stream, G_PRIORITY_DEFAULT, NULL, stdin_read_complete, NULL); g_main_loop_run(loop); if (polkit_cancellable) g_clear_object(&polkit_cancellable); g_object_unref(stdin_stream); g_object_unref(authority); g_object_unref(subject); g_main_loop_unref(loop); return exit_status; }