/* * get password */ static char *getpsw() { char *pwd = (char *)calloc(65, 1); /* no memory */ if (!pwd) return NULL; fprintf(stdout, "Please enter password: "******"%64s", pwd); setconsole(CONMODE_ECHO); //fprintf(stdout, "\npassword: %s\n", pwd); fprintf(stdout, "\n"); return pwd; }
Console::Console() { setconsole(); }
int main(int argc, char **argv) { char *gidmap = NULL, *inside = NULL, *outside = NULL, *uidmap = NULL; char *bind = NULL; int hostnet = 0, master, option, stdio = 0; pid_t child, parent; while ((option = getopt(argc, argv, "+:b:cg:i:no:u:")) > 0) switch (option) { case 'b': bind = optarg; break; case 'c': stdio++; break; case 'g': gidmap = optarg; break; case 'i': inside = optarg; break; case 'n': hostnet++; break; case 'o': outside = optarg; break; case 'u': uidmap = optarg; break; default: usage(argv[0]); } if (argc <= optind) usage(argv[0]); parent = getpid(); switch (child = fork()) { case -1: error(1, errno, "fork"); case 0: raise(SIGSTOP); // if (geteuid() != 0) // denysetgroups(parent); writemap(parent, GID, gidmap); writemap(parent, UID, uidmap); if (outside) { if (setgid(getgid()) < 0 || setuid(getuid()) < 0) error(1, 0, "Failed to drop privileges"); execlp(SHELL, SHELL, "-c", outside, NULL); error(1, errno, "exec %s", outside); } exit(EXIT_SUCCESS); } if (setgid(getgid()) < 0 || setuid(getuid()) < 0) error(1, 0, "Failed to drop privileges"); if (unshare(CLONE_NEWIPC | CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWUTS) < 0) error(1, 0, "Failed to unshare namespaces"); if (!hostnet && unshare(CLONE_NEWNET) < 0) error(1, 0, "Failed to unshare network namespace"); waitforstop(child); kill(child, SIGCONT); waitforexit(child); setgid(0); setgroups(0, NULL); setuid(0); master = stdio ? -1 : getconsole(); createroot(argv[optind], master, inside, bind); unshare(CLONE_NEWPID); switch (child = fork()) { case -1: error(1, errno, "fork"); case 0: mountproc(); if (!hostnet) mountsys(); enterroot(); if (master >= 0) { close(master); setconsole("/dev/console"); } clearenv(); putenv("container=contain"); if (argv[optind + 1]) execv(argv[optind + 1], argv + optind + 1); else execl(SHELL, SHELL, NULL); error(1, errno, "exec"); } return supervise(child, master); }
int main(int argc, char **argv) { int rc = 0; int devport = -1; /* */ memset(devnduid, 0, sizeof(devnduid)); memset(devsnid, 0, sizeof(devsnid)); /* options */ rc = parse_opt(argc, argv); if (rc) { return rc; } /* save current attributes */ saveconsole(); /* sockets */ rc = init_sockets(); if (rc) { return 1; } do { /* get the device list */ int devlistfd = connect_socket(serverip, serverport); if (devlistfd < 0) { fprintf(stderr, "failed to connect to server\n"); return 1; } /* dump the list if we're in device list mode */ if (listdev) { list_devices(devlistfd); return 0; } /* find our device (or the default one) and return the port to connect to */ devport = find_device(devlistfd, device); /* we do not need the device list port anymore */ close(devlistfd); if (devport < 0) { if (waitfordevice) { /* wait a bit and retry */ usleep(500000); continue; } else { fprintf(stderr, "unable to find device\n"); return 1; } } /* found device, abort the loop */ break; } while (1); //Port Forwarding Mode Begins if (portforwardmode) { initTcpRelay(serverip, devport); startTcpRelay(); return 0; } /* if we're in device list mode, dont bother extracting the command */ if (!listdev && !devcmd) { if (argc - optind < 1) { usage(argc, argv, 0); } /* construct the command from the rest of the arguments */ int i; command[0] = 0; for (i = optind; i < argc; i++) { if (i != optind) strcat(command, " "); strcat(command, argv[i]); } strcat(command, "\n"); #ifdef DEBUG_NOVACOM printf("command is %s\n", command); #endif } else if (devcmd) { int rc; /*host control cmd */ if( !strncmp(devcmd, "list", 4)) { snprintf(command, sizeof(command), "list host://"); devport = NOVACOM_CTRLPORT; } else if ( !strncmp(devcmd, "login", 5)) { /*device control cmd */ rc = prepare_cmd(devcmd, 5); if (!rc) { devport = NOVACOM_CTRLPORT; } else { return rc; } } else if (!strncmp(devcmd, "logout", 5)) { /*device control cmd */ /* command */ snprintf(command, sizeof(command), "logout dev://%s\n", devnduid); devport = NOVACOM_CTRLPORT; } else if (!strncmp(devcmd, "add", 3)) { /*device control cmd */ rc = prepare_cmd(devcmd, 3); if (!rc) { devport = NOVACOM_CTRLPORT; } else { return rc; } } else if (!strncmp(devcmd, "remove", 6)) { /*device control cmd */ rc = prepare_cmd(devcmd, 6); if (!rc) { devport = NOVACOM_CTRLPORT; } else { return rc; } } else { fprintf(stderr, "unsupported command(%s)\n", devcmd); return 1; } } /* connect to the device port */ int fd = connect_socket(serverip, devport); if (fd < 0) { fprintf(stderr, "failed to connect to server\n"); return 1; } /* put the tty into interactive mode */ if (term_mode) { setconsole(CONMODE_TERMSUPPORT); } /* signals */ if (dosignals || dosigwinch) { struct sigaction sa; int retVal = pipe(signalpipe); if (-1 != retVal) { fcntl(signalpipe[PIPE_READ], F_SETFL, fcntl(signalpipe[PIPE_READ], F_GETFL) | O_NONBLOCK); memset(&sa, 0, sizeof(sa)); sa.sa_handler = &signal_to_pipe; sa.sa_flags = SA_RESTART; // install signal handlers if (dosigwinch) sigaction(SIGWINCH, &sa, NULL); if (dosignals) { sigaction(SIGINT, &sa, NULL); sigaction(SIGHUP, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); } } else { fprintf(stderr, "failed to establish pipe \n"); close(fd); return 1; } } /* send the command */ if ( send(fd, command, strlen(command), 0) < 0) { fprintf(stderr, "novacom: unable to send command to server\n"); } /* parse it */ if (parse_response(fd) < 0) { close(fd); return 1; } else if(devcmd) { /* command executed, just exit */ close(fd); return 0; } rc = data_xfer(fd); if(rc != 0) { fprintf(stderr, "novacom: unexpected EOF from server\n"); } close(fd); return rc; }