int _builtin_cd(char **cmd, t_shenv *shenv) { int ret; char *cwd; static char *oldpwd = NULL; cwd = my_getcwd(); if (cmd[1] == NULL) ret = chdir_home(); else if (!strcmp(cmd[1], "-")) { if ((ret = my_chdir(oldpwd)) == EXIT_SUCCESS) { printf("%s\n", (oldpwd == NULL) ? my_getcwd() : oldpwd); xfree(1, &oldpwd); oldpwd = cwd; update_env(oldpwd, shenv); return (EXIT_SUCCESS); } return (EXIT_FAILURE); } else ret = my_chdir(cmd[1]); update_env(cwd, shenv); if (ret == EXIT_SUCCESS && xfree(1, &oldpwd)) oldpwd = cwd; return ((ret) ? (EXIT_FAILURE) : (EXIT_SUCCESS)); }
/* * Main entry point: */ int MAIN(int argc, char **argv) { // First print GPL information: printf("%s %s [%s] Copyright (C) 2017 basil\n", PROGRAM_NAME_LONG, PROGRAM_VERSION, PLATFORM); puts("License GPLv3+: GNU GPL version 3 or later " "<http://gnu.org/licenses/gpl.html>."); puts("This is free software: you are free to change and redistribute it."); puts("There is NO WARRANTY, to the extent permitted by law."); putchar('\n'); // Process options: options_init(argc, argv); // Initialise various components (order is important!). log_init(); trace("changing to home directory %s", PROGRAM_DIR); chdir_home(); trace("installing files (if required)"); install_files(); trace("initialising user configuration"); config_init(); trace("initialising tunnel management"); tunnel_init(); // Initialise the sockets library (if required on this platform). trace("initialising sockets"); init_sockets(); // Get number of threads. int num_threads = (options_get()->seen_num_threads? options_get()->val_num_threads: NUM_THREADS_DEFAULT); if (num_threads < 1 || num_threads > NUM_THREADS_MAX) { error("unable to spawn %d threads; expected a number within the " "range 1..%u", num_threads, NUM_THREADS_MAX); } // Create configuration server thread. trace("launching configuration server thread"); if (thread_lock_init(&config_lock)) { error("unable to initialise global configuration lock"); } thread_t config_thread; if (!options_get()->seen_no_ui && thread_create(&config_thread, configuration_thread, NULL) != 0) { error("unable to create configuration server thread"); } // Open the packet capture/injection device driver. trace("initialising packet capture"); if (!options_get()->seen_no_capture) { init_capture(); } // Open the tunnels. trace("initialising tunnels"); tunnel_file_read(); tunnel_open(); // Go to sleep if we are not capturing packets. while (options_get()->seen_no_capture) { sleeptime(UINT64_MAX); } // Start worker threads. for (int i = 1; i < num_threads; i++) { thread_t work_thread; if (thread_create(&work_thread, worker_thread, NULL) != 0) { error("unable to create worker thread"); } } worker_thread((void *)0); return EXIT_SUCCESS; }
/** * Do everything before the fork and do everything the exec fork * * @param argc The number of command line arguments * @param argv The command line arguments */ void do_login(int argc, char** argv) { char* username = NULL; char* hostname = NULL; char preserve_env = 0; int ret; #ifdef USE_TTY_GROUP struct group* group; #endif #if AUTH > 0 /* Disable echoing */ passphrase_disable_echo1(STDIN_FILENO /* Will be the terminal. */); /* This should be done as early and quickly as possible so as little as possible of the passphrase gets leaked to the output if the user begins entering the passphrase directly after the username. */ #endif /* Set process group ID */ setpgrp(); /* Parse command line arguments */ { char double_dashed = 0; char hostname_on_next = 0; int i; for (i = 1; i < argc; i++) { char *arg = *(argv + i); char c; if (*arg == 0) ; else if ((*arg == '-') && (double_dashed == 0)) while ((c = *(++arg))) if (c == 'H') ; else if (c == 'V') _exit(2); else if (c == 'p') preserve_env = 1; else if (c == 'h') { if (*(arg + 1)) hostname = arg + 1; else hostname_on_next = 1; break; } else if (c == 'f') { if (*(arg + 1)) username = arg + 1; skip_auth = 1; break; } else if (c == '-') { double_dashed = 1; break; } else printf("%s: unrecognised options: -%c\n", *argv, c); else if (hostname_on_next) { hostname = arg; hostname_on_next = 0; } else username = arg; } } /* Change that a username has been specified */ if (username == NULL) { printf("%s: no username specified\n", *argv); sleep(ERROR_SLEEP); _exit(2); } /* Verify that the user may login */ ret = fork_exec_wait_hook(HOOK_VERIFY, argc, argv); if ((ret >= 0) && WIFEXITED(ret) && (WEXITSTATUS(ret) == 1)) { sleep(ERROR_SLEEP); _exit(2); } if (skip_auth) { /* Reset terminal settings */ passphrase_reenable_echo1(STDIN_FILENO); /* Only root may bypass authentication */ if (getuid()) { printf("%s: only root by use the -f option\n", *argv); sleep(ERROR_SLEEP); _exit(2); } } /* Print ant we want a passphrase, if -f has not been used */ else { printf("Passphrase: "); fflush(stdout); } /* Done early to make to program look like it is even faster than it is */ /* Make sure nopony is spying */ #ifdef USE_TTY_GROUP if ((group = getgrnam(TTY_GROUP))) tty_group = group->gr_gid; endgrent(); #endif secure_tty(tty_group); #if AUTH > 0 if (skip_auth == 0) { /* Redisable echoing */ passphrase_disable_echo1(STDIN_FILENO); } #endif /* Set up clean quiting and time out */ signal(SIGALRM, timeout_quit); signal(SIGQUIT, user_quit); signal(SIGINT, user_quit); siginterrupt(SIGALRM, 1); siginterrupt(SIGQUIT, 1); siginterrupt(SIGINT, 1); #if AUTH > 0 alarm(TIMEOUT_SECONDS); #endif /* Get user information */ if ((entry = getpwnam(username)) == NULL) { if (errno) perror("getpwnam"); else printf("User does not exist\n"); sleep(ERROR_SLEEP); _exit(1); } endpwent(); username = entry->pw_name; /* Verify passphrase or other token, if -f has not been used */ ret = 2; #if AUTH == 0 (void) hostname; #else initialise_login(hostname, username, read_passphrase); if (skip_auth == 0) ret = authenticate_login(); /* Passphrase entered, turn off timeout */ alarm(0); #endif if (ret == 2) printf("(auto-authenticated)\n"); if (ret == 0) { preexit(); fork_exec_wait_hook(HOOK_DENIED, argc, argv); xsleep(FAILURE_SLEEP); _exit(1); } preexit(); /* Verify account, such as that it is enabled */ verify_account(); /* Run login hook */ fork_exec_wait_hook(HOOK_LOGIN, argc, argv); /* Partial login */ chown_tty(entry->pw_uid, tty_group, 0); chdir_home(entry); ensure_shell(entry); set_environ(entry, preserve_env); open_login_session(); /* Stop signal handling */ signal(SIGALRM, SIG_DFL); signal(SIGQUIT, SIG_DFL); signal(SIGTSTP, SIG_IGN); child_pid = fork(); /* vfork cannot be used as the child changes the user, the parent would not be able to chown the TTY */ if (child_pid == -1) { perror("fork"); close_login_session(); sleep(ERROR_SLEEP); _exit(1); } else if (child_pid) return; /* Do not go beyond this in the parent */ /* In case the shell does not do this */ setsid(); /* Set controlling terminal */ if (ioctl(STDIN_FILENO, TIOCSCTTY, 1)) perror("TIOCSCTTY"); signal(SIGINT, SIG_DFL); /* Partial login */ ret = entry->pw_uid ? initgroups(username, entry->pw_gid) /* supplemental groups for user, can require network */ : setgroups(0, NULL); /* supplemental groups for root, does not require netork */ if (ret == -1) { perror(entry->pw_uid ? "initgroups" : "setgroups"); sleep(ERROR_SLEEP); _exit(1); } set_user(entry); exec_shell(entry); }