12 #include <config/autoconf.h>
15 /* This little program is a wrapper for the ssh key-gen program that produces
16 * ssh keys as required. The basic outline is simple, keys will be produced
17 * at boot time for sshd only if they don't already exist. For ssh the path
18 * is slightly more complex, keys will be created every boot unless sshd is
19 * also installed in which case its behaviour will override.
21 * In addition, the flash file system will be synced if sshd is enabled.
22 * This means that sshd will only use a single set of keys which is good
23 * because ssh causes pain if the daemon it is connecting to changes its
28 /* Where we end up installing our key files */
29 #define BASE_DIR "/etc/config/"
31 /* List of file names to mangle.
32 * The key type is included at the end after a \0 which happens to terminate
33 * the string for us :-)
35 static const char *files[] = {
36 "ssh_host_rsa_key\0rsa",
37 #ifndef CONFIG_USER_SSH_ONLY_RSA_V2_KEYGEN
38 "ssh_host_dsa_key\0dsa",
40 #if defined(INCLUDE_SSH)
45 #endif /* CONFIG_USER_SSH_ONLY_RSA_V2_KEYGEN */
50 #if defined(INCLUDE_SSHD) || defined(INCLUDE_SSH)
51 /* Check if the key files are alreayd there or not */
52 static inline int check_files(void) {
56 for (i=0; files[i] != NULL; i++) {
57 strcpy(fname, BASE_DIR);
58 strcpy(fname+sizeof(BASE_DIR)-1, files[i]);
59 if (-1 == stat(fname, &st))
61 strcat(fname, ".pub");
62 if (-1 == stat(fname, &st))
70 /* Remove all key files. The key generator fails if they're already there */
71 static inline void remove_files(void) {
74 for (i=0; files[i] != NULL; i++) {
75 strcpy(fname, BASE_DIR);
76 strcpy(fname+sizeof(BASE_DIR)-1, files[i]);
78 strcat(fname+sizeof(BASE_DIR)-1, ".pub");
84 /* Exec the key generation program with the specified args */
85 static void exec(char *const av[]) {
86 extern char **environ;
96 execve("/bin/ssh-keygen", av, environ);
102 } else if (pid != -1) {
103 waitpid(pid, &status, 0);
108 /* Scan through and generate the appropriate keys */
109 static inline void gen_files(void) {
114 /* set up command args... */
116 av[ac++] = "ssh-keygen";
119 strcpy(fname, BASE_DIR);
126 tc = ac++; /* Placeholder for type */
129 /* Loop through the files creating keys */
130 for (i=0; files[i] != NULL; i++) {
131 strcpy(fname+sizeof(BASE_DIR)-1, files[i]);
132 av[tc] = 1+strchr(files[i], '\0');
138 #if defined(INCLUDE_SSHD) || defined(INCLUDE_SSH)
139 /* Write back our config file system */
143 system("exec flatfsd -s");
148 /* The main driver routine */
149 int main(int argc, char *argv[]) {
151 #if defined(INCLUDE_SSHD) || defined(INCLUDE_SSH)
157 #if defined(INCLUDE_SSHD) || defined(INCLUDE_SSH)