int main(int argc, char **argv) { int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; int opt, fopt_count = 0, j; char *tname, *cp, line[NI_MAXHOST]; FILE *fp; u_long linenum; extern int optind; extern char *optarg; __progname = ssh_get_progname(argv[0]); seed_rng(); TAILQ_INIT(&tq); /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ sanitise_stdfd(); if (argc <= 1) usage(); while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) { switch (opt) { case 'H': hash_hosts = 1; break; case 'p': ssh_port = a2port(optarg); if (ssh_port <= 0) { fprintf(stderr, "Bad port '%s'\n", optarg); exit(1); } break; case 'T': timeout = convtime(optarg); if (timeout == -1 || timeout == 0) { fprintf(stderr, "Bad timeout '%s'\n", optarg); usage(); } break; case 'v': if (!debug_flag) { debug_flag = 1; log_level = SYSLOG_LEVEL_DEBUG1; } else if (log_level < SYSLOG_LEVEL_DEBUG3) log_level++; else fatal("Too high debugging level."); break; case 'f': if (strcmp(optarg, "-") == 0) optarg = NULL; argv[fopt_count++] = optarg; break; case 't': get_keytypes = 0; tname = strtok(optarg, ","); while (tname) { int type = key_type_from_name(tname); switch (type) { case KEY_RSA1: get_keytypes |= KT_RSA1; break; case KEY_DSA: get_keytypes |= KT_DSA; break; case KEY_ECDSA: get_keytypes |= KT_ECDSA; break; case KEY_RSA: get_keytypes |= KT_RSA; break; case KEY_ED25519: get_keytypes |= KT_ED25519; break; case KEY_UNSPEC: fatal("unknown key type %s", tname); } tname = strtok(NULL, ","); } break; case '4': IPv4or6 = AF_INET; break; case '6': IPv4or6 = AF_INET6; break; case '?': default: usage(); } } if (optind == argc && !fopt_count) usage(); log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); maxfd = fdlim_get(1); if (maxfd < 0) fatal("%s: fdlim_get: bad value", __progname); if (maxfd > MAXMAXFD) maxfd = MAXMAXFD; if (MAXCON <= 0) fatal("%s: not enough file descriptors", __progname); if (maxfd > fdlim_get(0)) fdlim_set(maxfd); fdcon = xcalloc(maxfd, sizeof(con)); read_wait_nfdset = howmany(maxfd, NFDBITS); read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); for (j = 0; j < fopt_count; j++) { if (argv[j] == NULL) fp = stdin; else if ((fp = fopen(argv[j], "r")) == NULL) fatal("%s: %s: %s", __progname, argv[j], strerror(errno)); linenum = 0; while (read_keyfile_line(fp, argv[j] == NULL ? "(stdin)" : argv[j], line, sizeof(line), &linenum) != -1) { /* Chomp off trailing whitespace and comments */ if ((cp = strchr(line, '#')) == NULL) cp = line + strlen(line) - 1; while (cp >= line) { if (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '#') *cp-- = '\0'; else break; } /* Skip empty lines */ if (*line == '\0') continue; do_host(line); } if (ferror(fp)) fatal("%s: %s: %s", __progname, argv[j], strerror(errno)); fclose(fp); } while (optind < argc) do_host(argv[optind++]); while (ncon > 0) conloop(); return (0); }
int main(int argc, char **argv) { int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO; int opt, fopt_count = 0; char *tname; extern int optind; extern char *optarg; TAILQ_INIT(&tq); /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ sanitise_stdfd(); if (argc <= 1) usage(); while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) { switch (opt) { case 'H': hash_hosts = 1; break; case 'p': ssh_port = a2port(optarg); if (ssh_port <= 0) { fprintf(stderr, "Bad port '%s'\n", optarg); exit(1); } break; case 'T': timeout = convtime(optarg); if (timeout == -1 || timeout == 0) { fprintf(stderr, "Bad timeout '%s'\n", optarg); usage(); } break; case 'v': if (!debug_flag) { debug_flag = 1; log_level = SYSLOG_LEVEL_DEBUG1; } else if (log_level < SYSLOG_LEVEL_DEBUG3) log_level++; else fatal("Too high debugging level."); break; case 'f': if (strcmp(optarg, "-") == 0) optarg = NULL; argv[fopt_count++] = optarg; break; case 't': get_keytypes = 0; tname = strtok(optarg, ","); while (tname) { int type = key_type_from_name(tname); switch (type) { case KEY_RSA1: get_keytypes |= KT_RSA1; break; case KEY_DSA: get_keytypes |= KT_DSA; break; case KEY_RSA: get_keytypes |= KT_RSA; break; case KEY_UNSPEC: fatal("unknown key type %s", tname); } tname = strtok(NULL, ","); } break; case '4': IPv4or6 = AF_INET; break; case '6': IPv4or6 = AF_INET6; break; case '?': default: usage(); } } if (optind == argc && !fopt_count) usage(); log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1); maxfd = fdlim_get(1); if (maxfd < 0) fatal("%s: fdlim_get: bad value", __progname); if (maxfd > MAXMAXFD) maxfd = MAXMAXFD; if (MAXCON <= 0) fatal("%s: not enough file descriptors", __progname); if (maxfd > fdlim_get(0)) fdlim_set(maxfd); fdcon = xcalloc(maxfd, sizeof(con)); read_wait_nfdset = howmany(maxfd, NFDBITS); read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask)); if (fopt_count) { Linebuf *lb; char *line; int j; for (j = 0; j < fopt_count; j++) { lb = Linebuf_alloc(argv[j], error); if (!lb) continue; while ((line = Linebuf_getline(lb)) != NULL) do_host(line); Linebuf_free(lb); } } while (optind < argc) do_host(argv[optind++]); while (ncon > 0) conloop(); return (0); }
void async_init::start () { static bool initialized; if (initialized) panic ("async_init called twice\n"); initialized = true; /* Ignore SIGPIPE, since we may get a lot of these */ struct sigaction sa; bzero (&sa, sizeof (sa)); sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, NULL); if (!execsafe ()) { int fdlim_hard = fdlim_get (1); if (char *p = getenv ("FDLIM_HARD")) { int n = atoi (p); if (n > 0 && n < fdlim_hard) { fdlim_hard = n; fdlim_set (fdlim_hard, -1); } } } if (!getenv ("FDLIM_HARD") || !execsafe ()) { str var = strbuf ("FDLIM_HARD=%d", fdlim_get (1)); xputenv (const_cast<char*>(var.cstr())); var = strbuf ("FDLIM_SOFT=%d", fdlim_get (0)); xputenv (const_cast<char*>(var.cstr())); } sfs_core::selector_t::init (); sfs_core::selector = New sfs_core::std_selector_t (); lazylist = New list<lazycb_t, &lazycb_t::link>; #ifdef WRAP_DEBUG if (char *p = getenv ("CALLBACK_TRACE")) { if (strchr (p, 'f')) callback_trace |= CBTR_FD; if (strchr (p, 't')) callback_trace |= CBTR_TIME; if (strchr (p, 's')) callback_trace |= CBTR_SIG; if (strchr (p, 'c')) callback_trace |= CBTR_CHLD; if (strchr (p, 'l')) callback_trace |= CBTR_LAZY; if (strchr (p, 'a')) callback_trace |= -1; if (strchr (p, 'T')) callback_time = true; } #endif /* WRAP_DEBUG */ if (char *p = getenv ("SFS_OPTIONS")) { for (const char *cp = p; *cp; cp++) { switch (*cp) { case 'b': sfs_core::set_busywait (true); break; case 'e': if (sfs_core::set_select_policy (sfs_core::SELECT_EPOLL) < 0) warn ("failed to switch select policy to EPOLL\n"); break; case 'k': if (sfs_core::set_select_policy (sfs_core::SELECT_KQUEUE) < 0) warn ("failed to switch select policy to KQUEUE\n"); break; case 'z': sfs_core::set_zombie_collect (true); break; default: warn ("unknown SFS_OPTION: '%c'\n", *cp); break; } } } }
int main (int argc, char **argv ) { pid_t pid; uid_t UID; gid_t GID; pid_t pidwait; int waitstat; int maxfd; int s; /* Sanity check */ if (argc > MAX_ARGS) { error_sys("arg buffer too small"); exit(-1); } if (geteuid() != 0) { error_sys("must be called by root"); exit(-1); } /* fork child that will become prelude-manager */ if ((pid = fork()) < 0) error_sys("fork error"); else { if (pid == 0) { /* We're the child */ char *args[MAX_ARGS]; unsigned int i; /* Become session leader */ setsid(); /* Change working directory to root directory. The current working directory could be a mounted filesystem; if the daemon stays on a mounted filesystem it could prevent the filesystem from being umounted. */ chdir("/"); /* Clear out file creation mask */ umask(0); /* Close unneeded file descriptors */ maxfd = (int) sysconf(_SC_OPEN_MAX); if (maxfd == -1) maxfd = getdtablesize(); for (s = 3; s < maxfd; s++) (void) close(s); /* Increase limit on number of open file descriptors if necessary */ maxfd = fdlim_get(1); if (maxfd < 0) error_sys("fdlim_get: bad value"); if (maxfd > MAXMAXFD) maxfd = MAXMAXFD; if (maxfd > fdlim_get(0)) fdlim_set(maxfd); /* Build calling argv */ args[0] = PRELUDE_MANAGER_PATH; for (i=1;i<argc;i++) { args[i] = argv[i]; } args[i++] = NULL; /* Finally transform self into prelude-manager */ if (execvp(PRELUDE_MANAGER_PATH, args) < 0) error_sys("execve error"); else ; /* avoid if-then ambiguity */ } else { /* We're the parent Terminate */ exit(0); } } }