remove unused variable
[dropbear.git] / dropbearconvert.c
blob9e16fe72f63ec45404d4c7f70e8b65c04137478f
1 /*
2 * Dropbear - a SSH2 server
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 /* This program converts to/from Dropbear and OpenSSH private-key formats */
26 #include "includes.h"
27 #include "signkey.h"
28 #include "buffer.h"
29 #include "dbutil.h"
30 #include "keyimport.h"
33 static int do_convert(int intype, const char* infile, int outtype,
34 const char* outfile);
36 static void printhelp(char * progname);
38 static void printhelp(char * progname) {
40 fprintf(stderr, "Usage: %s <inputtype> <outputtype> <inputfile> <outputfile>\n\n"
41 "CAUTION: This program is for convenience only, and is not secure if used on\n"
42 "untrusted input files, ie it could allow arbitrary code execution.\n"
43 "All parameters must be specified in order.\n"
44 "\n"
45 "The input and output types are one of:\n"
46 "openssh\n"
47 "dropbear\n"
48 "\n"
49 "Example:\n"
50 "dropbearconvert openssh dropbear /etc/ssh/ssh_host_rsa_key /etc/dropbear_rsa_host_key\n",
51 progname);
54 #if defined(DBMULTI_dropbearconvert) || !defined(DROPBEAR_MULTI)
55 #if defined(DBMULTI_dropbearconvert) && defined(DROPBEAR_MULTI)
56 int dropbearconvert_main(int argc, char ** argv) {
57 #else
58 int main(int argc, char ** argv) {
59 #endif
61 int intype, outtype;
62 const char* infile;
63 const char* outfile;
65 #ifdef DEBUG_TRACE
66 /* It's hard for it to get in the way _too_ much */
67 debug_trace = 1;
68 #endif
70 /* get the commandline options */
71 if (argc != 5) {
72 fprintf(stderr, "All arguments must be specified\n");
73 goto usage;
76 /* input type */
77 if (argv[1][0] == 'd') {
78 intype = KEYFILE_DROPBEAR;
79 } else if (argv[1][0] == 'o') {
80 intype = KEYFILE_OPENSSH;
81 } else {
82 fprintf(stderr, "Invalid input key type\n");
83 goto usage;
86 /* output type */
87 if (argv[2][0] == 'd') {
88 outtype = KEYFILE_DROPBEAR;
89 } else if (argv[2][0] == 'o') {
90 outtype = KEYFILE_OPENSSH;
91 } else {
92 fprintf(stderr, "Invalid output key type\n");
93 goto usage;
96 /* we don't want output readable by others */
97 umask(077);
99 infile = argv[3];
100 outfile = argv[4];
102 return do_convert(intype, infile, outtype, outfile);
104 usage:
105 printhelp(argv[0]);
106 return 1;
108 #endif
110 static int do_convert(int intype, const char* infile, int outtype,
111 const char* outfile) {
113 sign_key * key = NULL;
114 char * keytype = NULL;
115 int ret = 1;
117 key = import_read(infile, NULL, intype);
118 if (!key) {
119 fprintf(stderr, "Error reading key from '%s'\n",
120 infile);
121 goto out;
124 #ifdef DROPBEAR_RSA
125 if (key->rsakey != NULL) {
126 keytype = "RSA";
128 #endif
129 #ifdef DROPBEAR_DSS
130 if (key->dsskey != NULL) {
131 keytype = "DSS";
133 #endif
135 fprintf(stderr, "Key is a %s key\n", keytype);
137 if (import_write(outfile, key, NULL, outtype) != 1) {
138 fprintf(stderr, "Error writing key to '%s'\n", outfile);
139 } else {
140 fprintf(stderr, "Wrote key to '%s'\n", outfile);
141 ret = 0;
144 out:
145 if (key) {
146 sign_key_free(key);
148 return ret;