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

📄 ssh-keygen.c

📁 OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。它用安全、加密的网络连接工具代替了 telnet、ftp、 rlogin、rsh 和 rcp 工具。OpenSSH 支持
💻 C
📖 第 1 页 / 共 3 页
字号:
	}	xfree(keys);	exit(0);}#endif /* SMARTCARD */static voiddo_fingerprint(struct passwd *pw){	FILE *f;	Key *public;	char *comment = NULL, *cp, *ep, line[16*1024], *fp;	int i, skip = 0, num = 1, invalid = 1;	enum fp_rep rep;	enum fp_type fptype;	struct stat st;	fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;	rep =    print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;	if (!have_identity)		ask_filename(pw, "Enter file in which the key is");	if (stat(identity_file, &st) < 0) {		perror(identity_file);		exit(1);	}	public = key_load_public(identity_file, &comment);	if (public != NULL) {		fp = key_fingerprint(public, fptype, rep);		printf("%u %s %s\n", key_size(public), fp, comment);		key_free(public);		xfree(comment);		xfree(fp);		exit(0);	}	if (comment)		xfree(comment);	f = fopen(identity_file, "r");	if (f != NULL) {		while (fgets(line, sizeof(line), f)) {			i = strlen(line) - 1;			if (line[i] != '\n') {				error("line %d too long: %.40s...", num, line);				skip = 1;				continue;			}			num++;			if (skip) {				skip = 0;				continue;			}			line[i] = '\0';			/* Skip leading whitespace, empty and comment lines. */			for (cp = line; *cp == ' ' || *cp == '\t'; cp++)				;			if (!*cp || *cp == '\n' || *cp == '#')				continue ;			i = strtol(cp, &ep, 10);			if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {				int quoted = 0;				comment = cp;				for (; *cp && (quoted || (*cp != ' ' &&				    *cp != '\t')); cp++) {					if (*cp == '\\' && cp[1] == '"')						cp++;	/* Skip both */					else if (*cp == '"')						quoted = !quoted;				}				if (!*cp)					continue;				*cp++ = '\0';			}			ep = cp;			public = key_new(KEY_RSA1);			if (key_read(public, &cp) != 1) {				cp = ep;				key_free(public);				public = key_new(KEY_UNSPEC);				if (key_read(public, &cp) != 1) {					key_free(public);					continue;				}			}			comment = *cp ? cp : comment;			fp = key_fingerprint(public, fptype, rep);			printf("%u %s %s\n", key_size(public), fp,			    comment ? comment : "no comment");			xfree(fp);			key_free(public);			invalid = 0;		}		fclose(f);	}	if (invalid) {		printf("%s is not a public key file.\n", identity_file);		exit(1);	}	exit(0);}static voidprint_host(FILE *f, char *name, Key *public, int hash){	if (hash && (name = host_hash(name, NULL, 0)) == NULL)		fatal("hash_host failed");	fprintf(f, "%s ", name);	if (!key_write(public, f))		fatal("key_write failed");	fprintf(f, "\n");}static voiddo_known_hosts(struct passwd *pw, const char *name){	FILE *in, *out = stdout;	Key *public;	char *cp, *cp2, *kp, *kp2;	char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];	int c, i, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;	if (!have_identity) {		cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);		if (strlcpy(identity_file, cp, sizeof(identity_file)) >=		    sizeof(identity_file))			fatal("Specified known hosts path too long");		xfree(cp);		have_identity = 1;	}	if ((in = fopen(identity_file, "r")) == NULL)		fatal("fopen: %s", strerror(errno));	/*	 * Find hosts goes to stdout, hash and deletions happen in-place	 * A corner case is ssh-keygen -HF foo, which should go to stdout	 */	if (!find_host && (hash_hosts || delete_host)) {		if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||		    strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||		    strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||		    strlcat(old, ".old", sizeof(old)) >= sizeof(old))			fatal("known_hosts path too long");		umask(077);		if ((c = mkstemp(tmp)) == -1)			fatal("mkstemp: %s", strerror(errno));		if ((out = fdopen(c, "w")) == NULL) {			c = errno;			unlink(tmp);			fatal("fdopen: %s", strerror(c));		}		inplace = 1;	}	while (fgets(line, sizeof(line), in)) {		num++;		i = strlen(line) - 1;		if (line[i] != '\n') {			error("line %d too long: %.40s...", num, line);			skip = 1;			invalid = 1;			continue;		}		if (skip) {			skip = 0;			continue;		}		line[i] = '\0';		/* Skip leading whitespace, empty and comment lines. */		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)			;		if (!*cp || *cp == '\n' || *cp == '#') {			if (inplace)				fprintf(out, "%s\n", cp);			continue;		}		/* Find the end of the host name portion. */		for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)			;		if (*kp == '\0' || *(kp + 1) == '\0') {			error("line %d missing key: %.40s...",			    num, line);			invalid = 1;			continue;		}		*kp++ = '\0';		kp2 = kp;		public = key_new(KEY_RSA1);		if (key_read(public, &kp) != 1) {			kp = kp2;			key_free(public);			public = key_new(KEY_UNSPEC);			if (key_read(public, &kp) != 1) {				error("line %d invalid key: %.40s...",				    num, line);				key_free(public);				invalid = 1;				continue;			}		}		if (*cp == HASH_DELIM) {			if (find_host || delete_host) {				cp2 = host_hash(name, cp, strlen(cp));				if (cp2 == NULL) {					error("line %d: invalid hashed "					    "name: %.64s...", num, line);					invalid = 1;					continue;				}				c = (strcmp(cp2, cp) == 0);				if (find_host && c) {					printf("# Host %s found: "					    "line %d type %s\n", name,					    num, key_type(public));					print_host(out, cp, public, 0);				}				if (delete_host && !c)					print_host(out, cp, public, 0);			} else if (hash_hosts)				print_host(out, cp, public, 0);		} else {			if (find_host || delete_host) {				c = (match_hostname(name, cp,				    strlen(cp)) == 1);				if (find_host && c) {					printf("# Host %s found: "					    "line %d type %s\n", name,					    num, key_type(public));					print_host(out, cp, public, hash_hosts);				}				if (delete_host && !c)					print_host(out, cp, public, 0);			} else if (hash_hosts) {				for(cp2 = strsep(&cp, ",");				    cp2 != NULL && *cp2 != '\0';				    cp2 = strsep(&cp, ",")) {					if (strcspn(cp2, "*?!") != strlen(cp2))						fprintf(stderr, "Warning: "						    "ignoring host name with "						    "metacharacters: %.64s\n",						    cp2);					else						print_host(out, cp2, public, 1);				}				has_unhashed = 1;			}		}		key_free(public);	}	fclose(in);	if (invalid) {		fprintf(stderr, "%s is not a valid known_host file.\n",		    identity_file);		if (inplace) {			fprintf(stderr, "Not replacing existing known_hosts "			    "file beacuse of errors");			fclose(out);			unlink(tmp);		}		exit(1);	}	if (inplace) {		fclose(out);		/* Backup existing file */		if (unlink(old) == -1 && errno != ENOENT)			fatal("unlink %.100s: %s", old, strerror(errno));		if (link(identity_file, old) == -1)			fatal("link %.100s to %.100s: %s", identity_file, old,			    strerror(errno));		/* Move new one into place */		if (rename(tmp, identity_file) == -1) {			error("rename\"%s\" to \"%s\": %s", tmp, identity_file,			    strerror(errno));			unlink(tmp);			unlink(old);			exit(1);		}		fprintf(stderr, "%s updated.\n", identity_file);		fprintf(stderr, "Original contents retained as %s\n", old);		if (has_unhashed) {			fprintf(stderr, "WARNING: %s contains unhashed "			    "entries\n", old);			fprintf(stderr, "Delete this file to ensure privacy "			     "of hostnames\n");		}	}	exit(0);}/* * Perform changing a passphrase.  The argument is the passwd structure * for the current user. */static voiddo_change_passphrase(struct passwd *pw){	char *comment;	char *old_passphrase, *passphrase1, *passphrase2;	struct stat st;	Key *private;	if (!have_identity)		ask_filename(pw, "Enter file in which the key is");	if (stat(identity_file, &st) < 0) {		perror(identity_file);		exit(1);	}	/* Try to load the file with empty passphrase. */	private = key_load_private(identity_file, "", &comment);	if (private == NULL) {		if (identity_passphrase)			old_passphrase = xstrdup(identity_passphrase);		else			old_passphrase =			    read_passphrase("Enter old passphrase: ",			    RP_ALLOW_STDIN);		private = key_load_private(identity_file, old_passphrase,		    &comment);		memset(old_passphrase, 0, strlen(old_passphrase));		xfree(old_passphrase);		if (private == NULL) {			printf("Bad passphrase.\n");			exit(1);		}	}	printf("Key has comment '%s'\n", comment);	/* Ask the new passphrase (twice). */	if (identity_new_passphrase) {		passphrase1 = xstrdup(identity_new_passphrase);		passphrase2 = NULL;	} else {		passphrase1 =			read_passphrase("Enter new passphrase (empty for no "			    "passphrase): ", RP_ALLOW_STDIN);		passphrase2 = read_passphrase("Enter same passphrase again: ",		    RP_ALLOW_STDIN);		/* Verify that they are the same. */		if (strcmp(passphrase1, passphrase2) != 0) {			memset(passphrase1, 0, strlen(passphrase1));			memset(passphrase2, 0, strlen(passphrase2));			xfree(passphrase1);			xfree(passphrase2);			printf("Pass phrases do not match.  Try again.\n");			exit(1);		}		/* Destroy the other copy. */		memset(passphrase2, 0, strlen(passphrase2));		xfree(passphrase2);	}	/* Save the file using the new passphrase. */	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);		key_free(private);		xfree(comment);		exit(1);	}	/* Destroy the passphrase and the copy of the key in memory. */	memset(passphrase1, 0, strlen(passphrase1));	xfree(passphrase1);	key_free(private);		 /* Destroys contents */	xfree(comment);	printf("Your identification has been saved with the new passphrase.\n");	exit(0);}/* * Print the SSHFP RR. */static voiddo_print_resource_record(struct passwd *pw, char *hname){	Key *public;	char *comment = NULL;	struct stat st;	if (!have_identity)		ask_filename(pw, "Enter file in which the key is");	if (stat(identity_file, &st) < 0) {		perror(identity_file);		exit(1);	}	public = key_load_public(identity_file, &comment);	if (public != NULL) {		export_dns_rr(hname, public, stdout, print_generic);		key_free(public);		xfree(comment);		exit(0);	}	if (comment)		xfree(comment);	printf("failed to read v2 public key from %s.\n", identity_file);	exit(1);}/* * Change the comment of a private key file. */static voiddo_change_comment(struct passwd *pw){	char new_comment[1024], *comment, *passphrase;	Key *private;	Key *public;	struct stat st;	FILE *f;	int fd;	if (!have_identity)		ask_filename(pw, "Enter file in which the key is");	if (stat(identity_file, &st) < 0) {		perror(identity_file);		exit(1);	}	private = key_load_private(identity_file, "", &comment);	if (private == NULL) {		if (identity_passphrase)			passphrase = xstrdup(identity_passphrase);		else if (identity_new_passphrase)			passphrase = xstrdup(identity_new_passphrase);		else			passphrase = read_passphrase("Enter passphrase: ",			    RP_ALLOW_STDIN);		/* Try to load using the passphrase. */		private = key_load_private(identity_file, passphrase, &comment);		if (private == NULL) {			memset(passphrase, 0, strlen(passphrase));			xfree(passphrase);			printf("Bad passphrase.\n");			exit(1);		}	} else {		passphrase = xstrdup("");	}	if (private->type != KEY_RSA1) {		fprintf(stderr, "Comments are only supported for RSA1 keys.\n");

⌨️ 快捷键说明

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