コード例 #1
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);
}
コード例 #2
0
ファイル: ssh-keyscan.c プロジェクト: cyrilmagsuci/freebsd
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);
}
コード例 #3
0
ファイル: ssh-vulnkey.c プロジェクト: AhmadTux/DragonFlyBSD
int
main(int argc, char **argv)
{
	int opt, all_users = 0;
	int ret = 1;
	extern int optind;

	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
	sanitise_stdfd();

	__progname = ssh_get_progname(argv[0]);

	SSLeay_add_all_algorithms();
	log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);

	/* We don't need the RNG ourselves, but symbol references here allow
	 * ld to link us properly.
	 */
	seed_rng();

	while ((opt = getopt(argc, argv, "ahq")) != -1) {
		switch (opt) {
		case 'a':
			all_users = 1;
			break;
		case 'q':
			quiet = 1;
			break;
		case 'h':
		default:
			usage();
		}
	}

	if (all_users) {
		struct passwd *pw;

		if (!do_host())
			ret = 0;

		while ((pw = getpwent()) != NULL) {
			if (pw->pw_dir) {
				if (!do_user(pw->pw_dir))
					ret = 0;
			}
		}
	} else if (optind == argc) {
		struct passwd *pw;

		if (!do_host())
			ret = 0;

		if ((pw = getpwuid(getuid())) == NULL)
			fprintf(stderr, "No user found with uid %u\n",
			    (u_int)getuid());
		else {
			if (!do_user(pw->pw_dir))
				ret = 0;
		}
	} else {
		while (optind < argc)
			if (!do_filename(argv[optind++], 0))
				ret = 0;
	}

	return ret;
}