|
Lines 992-1009
do_gen_all_hostkeys(struct passwd *pw)
Link Here
|
| 992 |
int first = 0; |
992 |
int first = 0; |
| 993 |
struct stat st; |
993 |
struct stat st; |
| 994 |
struct sshkey *private, *public; |
994 |
struct sshkey *private, *public; |
| 995 |
char comment[1024]; |
995 |
char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file; |
| 996 |
int i, type, fd, r; |
996 |
int i, type, fd, r; |
| 997 |
FILE *f; |
997 |
FILE *f; |
| 998 |
|
998 |
|
|
|
999 |
/* |
| 1000 |
* Ensure that prefix path identity_file ends in '/' if it has been |
| 1001 |
* specified for easier joining to its suffixes later. |
| 1002 |
*/ |
| 1003 |
if (*identity_file && identity_file[strlen(identity_file) - 1] != '/') { |
| 1004 |
if (strlcat(identity_file, "/", sizeof(identity_file)) >= |
| 1005 |
sizeof(identity_file)) |
| 1006 |
fatal("prefix path is too long"); |
| 1007 |
} |
| 1008 |
|
| 999 |
for (i = 0; key_types[i].key_type; i++) { |
1009 |
for (i = 0; key_types[i].key_type; i++) { |
| 1000 |
if (stat(key_types[i].path, &st) == 0) |
1010 |
public = private = NULL; |
| 1001 |
continue; |
1011 |
xasprintf(&prv_tmp, "%s%s.XXXXXXXXXX", |
| 1002 |
if (errno != ENOENT) { |
1012 |
identity_file, key_types[i].path); |
|
|
1013 |
xasprintf(&pub_tmp, "%s%s.pub.XXXXXXXXXX", |
| 1014 |
identity_file, key_types[i].path); |
| 1015 |
xasprintf(&prv_file, "%s%s", |
| 1016 |
identity_file, key_types[i].path); |
| 1017 |
xasprintf(&pub_file, "%s%s.pub", |
| 1018 |
identity_file, key_types[i].path); |
| 1019 |
|
| 1020 |
if (stat(prv_file, &st) == 0) { |
| 1021 |
if (st.st_size != 0) |
| 1022 |
continue; |
| 1023 |
} else if (errno != ENOENT) { |
| 1003 |
error("Could not stat %s: %s", key_types[i].path, |
1024 |
error("Could not stat %s: %s", key_types[i].path, |
| 1004 |
strerror(errno)); |
1025 |
strerror(errno)); |
| 1005 |
first = 0; |
1026 |
goto failnext; |
| 1006 |
continue; |
|
|
| 1007 |
} |
1027 |
} |
| 1008 |
|
1028 |
|
| 1009 |
if (first == 0) { |
1029 |
if (first == 0) { |
|
Lines 1013-1068
do_gen_all_hostkeys(struct passwd *pw)
Link Here
|
| 1013 |
printf("%s ", key_types[i].key_type_display); |
1033 |
printf("%s ", key_types[i].key_type_display); |
| 1014 |
fflush(stdout); |
1034 |
fflush(stdout); |
| 1015 |
type = sshkey_type_from_name(key_types[i].key_type); |
1035 |
type = sshkey_type_from_name(key_types[i].key_type); |
| 1016 |
strlcpy(identity_file, key_types[i].path, sizeof(identity_file)); |
1036 |
if ((fd = mkstemp(prv_tmp)) == -1) { |
|
|
1037 |
error("Could not save your public key in %s: %s", |
| 1038 |
prv_tmp, strerror(errno)); |
| 1039 |
goto failnext; |
| 1040 |
} |
| 1041 |
close(fd); /* just using mkstemp() to generate/reserve a name */ |
| 1017 |
bits = 0; |
1042 |
bits = 0; |
| 1018 |
type_bits_valid(type, NULL, &bits); |
1043 |
type_bits_valid(type, NULL, &bits); |
| 1019 |
if ((r = sshkey_generate(type, bits, &private)) != 0) { |
1044 |
if ((r = sshkey_generate(type, bits, &private)) != 0) { |
| 1020 |
error("key_generate failed: %s", ssh_err(r)); |
1045 |
error("key_generate failed: %s", ssh_err(r)); |
| 1021 |
first = 0; |
1046 |
goto failnext; |
| 1022 |
continue; |
|
|
| 1023 |
} |
1047 |
} |
| 1024 |
if ((r = sshkey_from_private(private, &public)) != 0) |
1048 |
if ((r = sshkey_from_private(private, &public)) != 0) |
| 1025 |
fatal("sshkey_from_private failed: %s", ssh_err(r)); |
1049 |
fatal("sshkey_from_private failed: %s", ssh_err(r)); |
| 1026 |
snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, |
1050 |
snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, |
| 1027 |
hostname); |
1051 |
hostname); |
| 1028 |
if ((r = sshkey_save_private(private, identity_file, "", |
1052 |
if ((r = sshkey_save_private(private, prv_tmp, "", |
| 1029 |
comment, use_new_format, new_format_cipher, rounds)) != 0) { |
1053 |
comment, use_new_format, new_format_cipher, rounds)) != 0) { |
| 1030 |
error("Saving key \"%s\" failed: %s", |
1054 |
error("Saving key \"%s\" failed: %s", |
| 1031 |
identity_file, ssh_err(r)); |
1055 |
prv_tmp, ssh_err(r)); |
| 1032 |
sshkey_free(private); |
1056 |
goto failnext; |
| 1033 |
sshkey_free(public); |
|
|
| 1034 |
first = 0; |
| 1035 |
continue; |
| 1036 |
} |
1057 |
} |
| 1037 |
sshkey_free(private); |
1058 |
if ((fd = mkstemp(pub_tmp)) == -1) { |
| 1038 |
strlcat(identity_file, ".pub", sizeof(identity_file)); |
1059 |
error("Could not save your public key in %s: %s", |
| 1039 |
fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
1060 |
pub_tmp, strerror(errno)); |
| 1040 |
if (fd == -1) { |
1061 |
goto failnext; |
| 1041 |
error("Could not save your public key in %s", |
|
|
| 1042 |
identity_file); |
| 1043 |
sshkey_free(public); |
| 1044 |
first = 0; |
| 1045 |
continue; |
| 1046 |
} |
1062 |
} |
|
|
1063 |
(void)fchmod(fd, 0644); |
| 1047 |
f = fdopen(fd, "w"); |
1064 |
f = fdopen(fd, "w"); |
| 1048 |
if (f == NULL) { |
1065 |
if (f == NULL) { |
| 1049 |
error("fdopen %s failed", identity_file); |
1066 |
error("fdopen %s failed", pub_tmp); |
| 1050 |
close(fd); |
1067 |
close(fd); |
| 1051 |
sshkey_free(public); |
1068 |
goto failnext; |
| 1052 |
first = 0; |
|
|
| 1053 |
continue; |
| 1054 |
} |
1069 |
} |
| 1055 |
if ((r = sshkey_write(public, f)) != 0) { |
1070 |
if ((r = sshkey_write(public, f)) != 0) { |
| 1056 |
error("write key failed: %s", ssh_err(r)); |
1071 |
error("write key failed: %s", ssh_err(r)); |
| 1057 |
fclose(f); |
1072 |
fclose(f); |
| 1058 |
sshkey_free(public); |
1073 |
goto failnext; |
| 1059 |
first = 0; |
|
|
| 1060 |
continue; |
| 1061 |
} |
1074 |
} |
| 1062 |
fprintf(f, " %s\n", comment); |
1075 |
fprintf(f, " %s\n", comment); |
| 1063 |
fclose(f); |
1076 |
fclose(f); |
| 1064 |
sshkey_free(public); |
|
|
| 1065 |
|
1077 |
|
|
|
1078 |
/* Rename temporary files to their permanent locations. */ |
| 1079 |
if (rename(pub_tmp, pub_file) != 0) { |
| 1080 |
error("Unable to move %s into position: %s", |
| 1081 |
pub_file, strerror(errno)); |
| 1082 |
goto failnext; |
| 1083 |
} |
| 1084 |
if (rename(prv_tmp, prv_file) != 0) { |
| 1085 |
error("Unable to move %s into position: %s", |
| 1086 |
key_types[i].path, strerror(errno)); |
| 1087 |
failnext: |
| 1088 |
first = 0; |
| 1089 |
goto next; |
| 1090 |
} |
| 1091 |
next: |
| 1092 |
sshkey_free(private); |
| 1093 |
sshkey_free(public); |
| 1094 |
free(prv_tmp); |
| 1095 |
free(pub_tmp); |
| 1096 |
free(prv_file); |
| 1097 |
free(pub_file); |
| 1066 |
} |
1098 |
} |
| 1067 |
if (first != 0) |
1099 |
if (first != 0) |
| 1068 |
printf("\n"); |
1100 |
printf("\n"); |