⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssh-keygen.c

📁 OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。它用安全、加密的网络连接工具代替了 telnet、ftp、 rlogin、rsh 和 rcp 工具。OpenSSH 支持
💻 C
📖 第 1 页 / 共 3 页
字号:
		key_free(private);		exit(1);	}	printf("Key now has comment '%s'\n", comment);	if (identity_comment) {		strlcpy(new_comment, identity_comment, sizeof(new_comment));	} else {		printf("Enter new comment: ");		fflush(stdout);		if (!fgets(new_comment, sizeof(new_comment), stdin)) {			memset(passphrase, 0, strlen(passphrase));			key_free(private);			exit(1);		}		if (strchr(new_comment, '\n'))			*strchr(new_comment, '\n') = 0;	}	/* Save the file using the new passphrase. */	if (!key_save_private(private, identity_file, passphrase, new_comment)) {		printf("Saving the key failed: %s.\n", identity_file);		memset(passphrase, 0, strlen(passphrase));		xfree(passphrase);		key_free(private);		xfree(comment);		exit(1);	}	memset(passphrase, 0, strlen(passphrase));	xfree(passphrase);	public = key_from_private(private);	key_free(private);	strlcat(identity_file, ".pub", sizeof(identity_file));	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);	if (fd == -1) {		printf("Could not save your public key in %s\n", identity_file);		exit(1);	}	f = fdopen(fd, "w");	if (f == NULL) {		printf("fdopen %s failed", identity_file);		exit(1);	}	if (!key_write(public, f))		fprintf(stderr, "write key failed");	key_free(public);	fprintf(f, " %s\n", new_comment);	fclose(f);	xfree(comment);	printf("The comment in your key file has been changed.\n");	exit(0);}static voidusage(void){	fprintf(stderr, "Usage: %s [options]\n", __progname);	fprintf(stderr, "Options:\n");	fprintf(stderr, "  -b bits     Number of bits in the key to create.\n");	fprintf(stderr, "  -c          Change comment in private and public key files.\n");	fprintf(stderr, "  -e          Convert OpenSSH to IETF SECSH key file.\n");	fprintf(stderr, "  -f filename Filename of the key file.\n");	fprintf(stderr, "  -g          Use generic DNS resource record format.\n");	fprintf(stderr, "  -i          Convert IETF SECSH to OpenSSH key file.\n");	fprintf(stderr, "  -l          Show fingerprint of key file.\n");	fprintf(stderr, "  -p          Change passphrase of private key file.\n");	fprintf(stderr, "  -q          Quiet.\n");	fprintf(stderr, "  -y          Read private key file and print public key.\n");	fprintf(stderr, "  -t type     Specify type of key to create.\n");	fprintf(stderr, "  -B          Show bubblebabble digest of key file.\n");	fprintf(stderr, "  -H          Hash names in known_hosts file\n");	fprintf(stderr, "  -F hostname Find hostname in known hosts file\n");	fprintf(stderr, "  -C comment  Provide new comment.\n");	fprintf(stderr, "  -N phrase   Provide new passphrase.\n");	fprintf(stderr, "  -P phrase   Provide old passphrase.\n");	fprintf(stderr, "  -r hostname Print DNS resource record.\n");#ifdef SMARTCARD	fprintf(stderr, "  -D reader   Download public key from smartcard.\n");	fprintf(stderr, "  -U reader   Upload private key to smartcard.\n");#endif /* SMARTCARD */	fprintf(stderr, "  -G file     Generate candidates for DH-GEX moduli\n");	fprintf(stderr, "  -T file     Screen candidates for DH-GEX moduli\n");	exit(1);}/* * Main program for key management. */intmain(int ac, char **av){	char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;	char out_file[MAXPATHLEN], *reader_id = NULL;	char *rr_hostname = NULL;	Key *private, *public;	struct passwd *pw;	struct stat st;	int opt, type, fd, download = 0, memory = 0;	int generator_wanted = 0, trials = 100;	int do_gen_candidates = 0, do_screen_candidates = 0;	int log_level = SYSLOG_LEVEL_INFO;	BIGNUM *start = NULL;	FILE *f;	extern int optind;	extern char *optarg;	__progname = ssh_get_progname(av[0]);	SSLeay_add_all_algorithms();	log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);	init_rng();	seed_rng();	/* we need this for the home * directory.  */	pw = getpwuid(getuid());	if (!pw) {		printf("You don't exist, go away!\n");		exit(1);	}	if (gethostname(hostname, sizeof(hostname)) < 0) {		perror("gethostname");		exit(1);	}	while ((opt = getopt(ac, av,	    "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {		switch (opt) {		case 'b':			bits = atoi(optarg);			if (bits < 512 || bits > 32768) {				printf("Bits has bad value.\n");				exit(1);			}			break;		case 'F':			find_host = 1;			rr_hostname = optarg;			break;		case 'H':			hash_hosts = 1;			break;		case 'R':			delete_host = 1;			rr_hostname = optarg;			break;		case 'l':			print_fingerprint = 1;			break;		case 'B':			print_bubblebabble = 1;			break;		case 'p':			change_passphrase = 1;			break;		case 'c':			change_comment = 1;			break;		case 'f':			strlcpy(identity_file, optarg, sizeof(identity_file));			have_identity = 1;			break;		case 'g':			print_generic = 1;			break;		case 'P':			identity_passphrase = optarg;			break;		case 'N':			identity_new_passphrase = optarg;			break;		case 'C':			identity_comment = optarg;			break;		case 'q':			quiet = 1;			break;		case 'e':		case 'x':			/* export key */			convert_to_ssh2 = 1;			break;		case 'i':		case 'X':			/* import key */			convert_from_ssh2 = 1;			break;		case 'y':			print_public = 1;			break;		case 'd':			key_type_name = "dsa";			break;		case 't':			key_type_name = optarg;			break;		case 'D':			download = 1;		case 'U':			reader_id = optarg;			break;		case 'v':			if (log_level == SYSLOG_LEVEL_INFO)				log_level = SYSLOG_LEVEL_DEBUG1;			else {				if (log_level >= SYSLOG_LEVEL_DEBUG1 &&				    log_level < SYSLOG_LEVEL_DEBUG3)					log_level++;			}			break;		case 'r':			rr_hostname = optarg;			break;		case 'W':			generator_wanted = atoi(optarg);			if (generator_wanted < 1)				fatal("Desired generator has bad value.");			break;		case 'a':			trials = atoi(optarg);			break;		case 'M':			memory = atoi(optarg);			break;		case 'G':			do_gen_candidates = 1;			strlcpy(out_file, optarg, sizeof(out_file));			break;		case 'T':			do_screen_candidates = 1;			strlcpy(out_file, optarg, sizeof(out_file));			break;		case 'S':			/* XXX - also compare length against bits */			if (BN_hex2bn(&start, optarg) == 0)				fatal("Invalid start point.");			break;		case '?':		default:			usage();		}	}	/* reinit */	log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);	if (optind < ac) {		printf("Too many arguments.\n");		usage();	}	if (change_passphrase && change_comment) {		printf("Can only have one of -p and -c.\n");		usage();	}	if (delete_host || hash_hosts || find_host)		do_known_hosts(pw, rr_hostname);	if (print_fingerprint || print_bubblebabble)		do_fingerprint(pw);	if (change_passphrase)		do_change_passphrase(pw);	if (change_comment)		do_change_comment(pw);	if (convert_to_ssh2)		do_convert_to_ssh2(pw);	if (convert_from_ssh2)		do_convert_from_ssh2(pw);	if (print_public)		do_print_public(pw);	if (rr_hostname != NULL) {		do_print_resource_record(pw, rr_hostname);	}	if (reader_id != NULL) {#ifdef SMARTCARD		if (download)			do_download(pw, reader_id);		else			do_upload(pw, reader_id);#else /* SMARTCARD */		fatal("no support for smartcards.");#endif /* SMARTCARD */	}	if (do_gen_candidates) {		FILE *out = fopen(out_file, "w");		if (out == NULL) {			error("Couldn't open modulus candidate file \"%s\": %s",			    out_file, strerror(errno));			return (1);		}		if (gen_candidates(out, memory, bits, start) != 0)			fatal("modulus candidate generation failed\n");		return (0);	}	if (do_screen_candidates) {		FILE *in;		FILE *out = fopen(out_file, "w");		if (have_identity && strcmp(identity_file, "-") != 0) {			if ((in = fopen(identity_file, "r")) == NULL) {				fatal("Couldn't open modulus candidate "				    "file \"%s\": %s", identity_file,				    strerror(errno));			}		} else			in = stdin;		if (out == NULL) {			fatal("Couldn't open moduli file \"%s\": %s",			    out_file, strerror(errno));		}		if (prime_test(in, out, trials, generator_wanted) != 0)			fatal("modulus screening failed\n");		return (0);	}	arc4random_stir();	if (key_type_name == NULL) {		printf("You must specify a key type (-t).\n");		usage();	}	type = key_type_from_name(key_type_name);	if (type == KEY_UNSPEC) {		fprintf(stderr, "unknown key type %s\n", key_type_name);		exit(1);	}	if (!quiet)		printf("Generating public/private %s key pair.\n", key_type_name);	private = key_generate(type, bits);	if (private == NULL) {		fprintf(stderr, "key_generate failed");		exit(1);	}	public  = key_from_private(private);	if (!have_identity)		ask_filename(pw, "Enter file in which to save the key");	/* Create ~/.ssh directory if it doesn\'t already exist. */	snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);	if (strstr(identity_file, dotsshdir) != NULL &&	    stat(dotsshdir, &st) < 0) {		if (mkdir(dotsshdir, 0700) < 0)			error("Could not create directory '%s'.", dotsshdir);		else if (!quiet)			printf("Created directory '%s'.\n", dotsshdir);	}	/* If the file already exists, ask the user to confirm. */	if (stat(identity_file, &st) >= 0) {		char yesno[3];		printf("%s already exists.\n", identity_file);		printf("Overwrite (y/n)? ");		fflush(stdout);		if (fgets(yesno, sizeof(yesno), stdin) == NULL)			exit(1);		if (yesno[0] != 'y' && yesno[0] != 'Y')			exit(1);	}	/* Ask for a passphrase (twice). */	if (identity_passphrase)		passphrase1 = xstrdup(identity_passphrase);	else if (identity_new_passphrase)		passphrase1 = xstrdup(identity_new_passphrase);	else {passphrase_again:		passphrase1 =			read_passphrase("Enter passphrase (empty for no "			    "passphrase): ", RP_ALLOW_STDIN);		passphrase2 = read_passphrase("Enter same passphrase again: ",		    RP_ALLOW_STDIN);		if (strcmp(passphrase1, passphrase2) != 0) {			/*			 * The passphrases do not match.  Clear them and			 * retry.			 */			memset(passphrase1, 0, strlen(passphrase1));			memset(passphrase2, 0, strlen(passphrase2));			xfree(passphrase1);			xfree(passphrase2);			printf("Passphrases do not match.  Try again.\n");			goto passphrase_again;		}		/* Clear the other copy of the passphrase. */		memset(passphrase2, 0, strlen(passphrase2));		xfree(passphrase2);	}	if (identity_comment) {		strlcpy(comment, identity_comment, sizeof(comment));	} else {		/* Create default commend field for the passphrase. */		snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);	}	/* Save the key with the given passphrase and comment. */	if (!key_save_private(private, identity_file, passphrase1, comment)) {		printf("Saving the key failed: %s.\n", identity_file);		memset(passphrase1, 0, strlen(passphrase1));		xfree(passphrase1);		exit(1);	}	/* Clear the passphrase. */	memset(passphrase1, 0, strlen(passphrase1));	xfree(passphrase1);	/* Clear the private key and the random number generator. */	key_free(private);	arc4random_stir();	if (!quiet)		printf("Your identification has been saved in %s.\n", identity_file);	strlcat(identity_file, ".pub", sizeof(identity_file));	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);	if (fd == -1) {		printf("Could not save your public key in %s\n", identity_file);		exit(1);	}	f = fdopen(fd, "w");	if (f == NULL) {		printf("fdopen %s failed", identity_file);		exit(1);	}	if (!key_write(public, f))		fprintf(stderr, "write key failed");	fprintf(f, " %s\n", comment);	fclose(f);	if (!quiet) {		char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);		printf("Your public key has been saved in %s.\n",		    identity_file);		printf("The key fingerprint is:\n");		printf("%s %s\n", fp, comment);		xfree(fp);	}	key_free(public);	exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -