|
Lines 976-993
do_gen_all_hostkeys(struct passwd *pw)
Link Here
|
| 976 |
int first = 0; |
976 |
int first = 0; |
| 977 |
struct stat st; |
977 |
struct stat st; |
| 978 |
struct sshkey *private, *public; |
978 |
struct sshkey *private, *public; |
| 979 |
char comment[1024]; |
979 |
char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file; |
| 980 |
int i, type, fd, r; |
980 |
int i, type, fd, r; |
| 981 |
FILE *f; |
981 |
FILE *f; |
| 982 |
|
982 |
|
| 983 |
for (i = 0; key_types[i].key_type; i++) { |
983 |
for (i = 0; key_types[i].key_type; i++) { |
| 984 |
if (stat(key_types[i].path, &st) == 0) |
984 |
public = private = NULL; |
| 985 |
continue; |
985 |
xasprintf(&prv_tmp, "%s%s.XXXXXXXXXX", |
| 986 |
if (errno != ENOENT) { |
986 |
identity_file, key_types[i].path); |
|
|
987 |
xasprintf(&pub_tmp, "%s%s.pub.XXXXXXXXXX", |
| 988 |
identity_file, key_types[i].path); |
| 989 |
xasprintf(&prv_file, "%s%s", |
| 990 |
identity_file, key_types[i].path); |
| 991 |
xasprintf(&pub_file, "%s%s.pub", |
| 992 |
identity_file, key_types[i].path); |
| 993 |
|
| 994 |
if (stat(prv_file, &st) == 0) { |
| 995 |
if (st.st_size != 0) |
| 996 |
continue; |
| 997 |
} else if (errno != ENOENT) { |
| 987 |
error("Could not stat %s: %s", key_types[i].path, |
998 |
error("Could not stat %s: %s", key_types[i].path, |
| 988 |
strerror(errno)); |
999 |
strerror(errno)); |
| 989 |
first = 0; |
1000 |
goto failnext; |
| 990 |
continue; |
|
|
| 991 |
} |
1001 |
} |
| 992 |
|
1002 |
|
| 993 |
if (first == 0) { |
1003 |
if (first == 0) { |
|
Lines 997-1052
do_gen_all_hostkeys(struct passwd *pw)
Link Here
|
| 997 |
printf("%s ", key_types[i].key_type_display); |
1007 |
printf("%s ", key_types[i].key_type_display); |
| 998 |
fflush(stdout); |
1008 |
fflush(stdout); |
| 999 |
type = sshkey_type_from_name(key_types[i].key_type); |
1009 |
type = sshkey_type_from_name(key_types[i].key_type); |
| 1000 |
strlcpy(identity_file, key_types[i].path, sizeof(identity_file)); |
1010 |
if ((fd = mkstemp(prv_tmp)) == -1) { |
|
|
1011 |
error("Could not save your public key in %s: %s", |
| 1012 |
prv_tmp, strerror(errno)); |
| 1013 |
goto failnext; |
| 1014 |
} |
| 1015 |
close(fd); /* just using mkstemp() to generate/reserve a name */ |
| 1001 |
bits = 0; |
1016 |
bits = 0; |
| 1002 |
type_bits_valid(type, NULL, &bits); |
1017 |
type_bits_valid(type, NULL, &bits); |
| 1003 |
if ((r = sshkey_generate(type, bits, &private)) != 0) { |
1018 |
if ((r = sshkey_generate(type, bits, &private)) != 0) { |
| 1004 |
error("key_generate failed: %s", ssh_err(r)); |
1019 |
error("key_generate failed: %s", ssh_err(r)); |
| 1005 |
first = 0; |
1020 |
goto failnext; |
| 1006 |
continue; |
|
|
| 1007 |
} |
1021 |
} |
| 1008 |
if ((r = sshkey_from_private(private, &public)) != 0) |
1022 |
if ((r = sshkey_from_private(private, &public)) != 0) |
| 1009 |
fatal("sshkey_from_private failed: %s", ssh_err(r)); |
1023 |
fatal("sshkey_from_private failed: %s", ssh_err(r)); |
| 1010 |
snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, |
1024 |
snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, |
| 1011 |
hostname); |
1025 |
hostname); |
| 1012 |
if ((r = sshkey_save_private(private, identity_file, "", |
1026 |
if ((r = sshkey_save_private(private, prv_tmp, "", |
| 1013 |
comment, use_new_format, new_format_cipher, rounds)) != 0) { |
1027 |
comment, use_new_format, new_format_cipher, rounds)) != 0) { |
| 1014 |
error("Saving key \"%s\" failed: %s", |
1028 |
error("Saving key \"%s\" failed: %s", |
| 1015 |
identity_file, ssh_err(r)); |
1029 |
prv_tmp, ssh_err(r)); |
| 1016 |
sshkey_free(private); |
1030 |
goto failnext; |
| 1017 |
sshkey_free(public); |
|
|
| 1018 |
first = 0; |
| 1019 |
continue; |
| 1020 |
} |
1031 |
} |
| 1021 |
sshkey_free(private); |
1032 |
if ((fd = mkstemp(pub_tmp)) == -1) { |
| 1022 |
strlcat(identity_file, ".pub", sizeof(identity_file)); |
1033 |
error("Could not save your public key in %s: %s", |
| 1023 |
fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
1034 |
pub_tmp, strerror(errno)); |
| 1024 |
if (fd == -1) { |
1035 |
goto failnext; |
| 1025 |
error("Could not save your public key in %s", |
|
|
| 1026 |
identity_file); |
| 1027 |
sshkey_free(public); |
| 1028 |
first = 0; |
| 1029 |
continue; |
| 1030 |
} |
1036 |
} |
|
|
1037 |
(void)fchmod(fd, 0644); |
| 1031 |
f = fdopen(fd, "w"); |
1038 |
f = fdopen(fd, "w"); |
| 1032 |
if (f == NULL) { |
1039 |
if (f == NULL) { |
| 1033 |
error("fdopen %s failed", identity_file); |
1040 |
error("fdopen %s failed", pub_tmp); |
| 1034 |
close(fd); |
1041 |
close(fd); |
| 1035 |
sshkey_free(public); |
1042 |
goto failnext; |
| 1036 |
first = 0; |
|
|
| 1037 |
continue; |
| 1038 |
} |
1043 |
} |
| 1039 |
if ((r = sshkey_write(public, f)) != 0) { |
1044 |
if ((r = sshkey_write(public, f)) != 0) { |
| 1040 |
error("write key failed: %s", ssh_err(r)); |
1045 |
error("write key failed: %s", ssh_err(r)); |
| 1041 |
fclose(f); |
1046 |
fclose(f); |
| 1042 |
sshkey_free(public); |
1047 |
goto failnext; |
| 1043 |
first = 0; |
|
|
| 1044 |
continue; |
| 1045 |
} |
1048 |
} |
| 1046 |
fprintf(f, " %s\n", comment); |
1049 |
fprintf(f, " %s\n", comment); |
| 1047 |
fclose(f); |
1050 |
fclose(f); |
| 1048 |
sshkey_free(public); |
|
|
| 1049 |
|
1051 |
|
|
|
1052 |
/* Rename temporary files to their permanent locations. */ |
| 1053 |
if (rename(pub_tmp, pub_file) != 0) { |
| 1054 |
error("Unable to move %s into position: %s", |
| 1055 |
pub_file, strerror(errno)); |
| 1056 |
goto failnext; |
| 1057 |
} |
| 1058 |
if (rename(prv_tmp, prv_file) != 0) { |
| 1059 |
error("Unable to move %s into position: %s", |
| 1060 |
key_types[i].path, strerror(errno)); |
| 1061 |
failnext: |
| 1062 |
first = 0; |
| 1063 |
goto next; |
| 1064 |
} |
| 1065 |
next: |
| 1066 |
sshkey_free(private); |
| 1067 |
sshkey_free(public); |
| 1068 |
free(prv_tmp); |
| 1069 |
free(pub_tmp); |
| 1070 |
free(prv_file); |
| 1071 |
free(pub_file); |
| 1050 |
} |
1072 |
} |
| 1051 |
if (first != 0) |
1073 |
if (first != 0) |
| 1052 |
printf("\n"); |
1074 |
printf("\n"); |