Changes to update Tomato RAF.
[tomato.git] / release / src / router / dropbear / svr-runopts.c
blobc6e3508202b7cd725e4eb75de167cbc629630d59
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 #include "includes.h"
26 #include "runopts.h"
27 #include "signkey.h"
28 #include "buffer.h"
29 #include "dbutil.h"
30 #include "algo.h"
32 svr_runopts svr_opts; /* GLOBAL */
34 static void printhelp(const char * progname);
35 static void addportandaddress(char* spec);
37 static void printhelp(const char * progname) {
39 fprintf(stderr, "Dropbear sshd v%s\n"
40 "Usage: %s [options]\n"
41 "Options are:\n"
42 "-b bannerfile Display the contents of bannerfile"
43 " before user login\n"
44 " (default: none)\n"
45 #ifdef DROPBEAR_DSS
46 "-d dsskeyfile Use dsskeyfile for the DSS host key\n"
47 " (default: %s)\n"
48 #endif
49 #ifdef DROPBEAR_RSA
50 "-r rsakeyfile Use rsakeyfile for the RSA host key\n"
51 " (default: %s)\n"
52 #endif
53 "-F Don't fork into background\n"
54 #ifdef DISABLE_SYSLOG
55 "(Syslog support not compiled in, using stderr)\n"
56 #else
57 "-E Log to stderr rather than syslog\n"
58 #endif
59 #ifdef DO_MOTD
60 "-m Don't display the motd on login\n"
61 #endif
62 "-w Disallow root logins\n"
63 #if defined(ENABLE_SVR_PASSWORD_AUTH) || defined(ENABLE_SVR_PAM_AUTH)
64 "-s Disable password logins\n"
65 "-g Disable password logins for root\n"
66 #endif
67 #ifdef ENABLE_SVR_LOCALTCPFWD
68 "-j Disable local port forwarding\n"
69 #endif
70 #ifdef ENABLE_SVR_REMOTETCPFWD
71 "-k Disable remote port forwarding\n"
72 "-a Allow connections to forwarded ports from any host\n"
73 #endif
74 "-p [address:]port\n"
75 " Listen on specified tcp port (and optionally address),\n"
76 " up to %d can be specified\n"
77 " (default port is %s if none specified)\n"
78 "-P PidFile Create pid file PidFile\n"
79 " (default %s)\n"
80 #ifdef INETD_MODE
81 "-i Start for inetd\n"
82 #endif
83 "-W <receive_window_buffer> (default %d, larger may be faster, max 1MB)\n"
84 "-K <keepalive> (0 is never, default %d, in seconds)\n"
85 "-I <idle_timeout> (0 is never, default %d, in seconds)\n"
86 #ifdef DEBUG_TRACE
87 "-v verbose (compiled with DEBUG_TRACE)\n"
88 #endif
89 ,DROPBEAR_VERSION, progname,
90 #ifdef DROPBEAR_DSS
91 DSS_PRIV_FILENAME,
92 #endif
93 #ifdef DROPBEAR_RSA
94 RSA_PRIV_FILENAME,
95 #endif
96 DROPBEAR_MAX_PORTS, DROPBEAR_DEFPORT, DROPBEAR_PIDFILE,
97 DEFAULT_RECV_WINDOW, DEFAULT_KEEPALIVE, DEFAULT_IDLE_TIMEOUT);
100 void svr_getopts(int argc, char ** argv) {
102 unsigned int i;
103 char ** next = 0;
104 int nextisport = 0;
105 char* recv_window_arg = NULL;
106 char* keepalive_arg = NULL;
107 char* idle_timeout_arg = NULL;
109 /* see printhelp() for options */
110 svr_opts.rsakeyfile = NULL;
111 svr_opts.dsskeyfile = NULL;
112 svr_opts.bannerfile = NULL;
113 svr_opts.banner = NULL;
114 svr_opts.forkbg = 1;
115 svr_opts.norootlogin = 0;
116 svr_opts.noauthpass = 0;
117 svr_opts.norootpass = 0;
118 svr_opts.inetdmode = 0;
119 svr_opts.portcount = 0;
120 svr_opts.hostkey = NULL;
121 svr_opts.pidfile = DROPBEAR_PIDFILE;
122 #ifdef ENABLE_SVR_LOCALTCPFWD
123 svr_opts.nolocaltcp = 0;
124 #endif
125 #ifdef ENABLE_SVR_REMOTETCPFWD
126 svr_opts.noremotetcp = 0;
127 #endif
128 #ifndef DISABLE_ZLIB
129 opts.enable_compress = 1;
130 #endif
131 /* not yet
132 opts.ipv4 = 1;
133 opts.ipv6 = 1;
135 #ifdef DO_MOTD
136 svr_opts.domotd = 1;
137 #endif
138 #ifndef DISABLE_SYSLOG
139 svr_opts.usingsyslog = 1;
140 #endif
141 opts.recv_window = DEFAULT_RECV_WINDOW;
142 opts.keepalive_secs = DEFAULT_KEEPALIVE;
143 opts.idle_timeout_secs = DEFAULT_IDLE_TIMEOUT;
145 #ifdef ENABLE_SVR_REMOTETCPFWD
146 opts.listen_fwd_all = 0;
147 #endif
149 for (i = 1; i < (unsigned int)argc; i++) {
150 if (nextisport) {
151 addportandaddress(argv[i]);
152 nextisport = 0;
153 continue;
156 if (next) {
157 *next = argv[i];
158 if (*next == NULL) {
159 dropbear_exit("Invalid null argument");
161 next = 0x00;
162 continue;
165 if (argv[i][0] == '-') {
166 switch (argv[i][1]) {
167 case 'b':
168 next = &svr_opts.bannerfile;
169 break;
170 #ifdef DROPBEAR_DSS
171 case 'd':
172 next = &svr_opts.dsskeyfile;
173 break;
174 #endif
175 #ifdef DROPBEAR_RSA
176 case 'r':
177 next = &svr_opts.rsakeyfile;
178 break;
179 #endif
180 case 'F':
181 svr_opts.forkbg = 0;
182 break;
183 #ifndef DISABLE_SYSLOG
184 case 'E':
185 svr_opts.usingsyslog = 0;
186 break;
187 #endif
188 #ifdef ENABLE_SVR_LOCALTCPFWD
189 case 'j':
190 svr_opts.nolocaltcp = 1;
191 break;
192 #endif
193 #ifdef ENABLE_SVR_REMOTETCPFWD
194 case 'k':
195 svr_opts.noremotetcp = 1;
196 break;
197 case 'a':
198 opts.listen_fwd_all = 1;
199 break;
200 #endif
201 #ifdef INETD_MODE
202 case 'i':
203 svr_opts.inetdmode = 1;
204 break;
205 #endif
206 case 'p':
207 nextisport = 1;
208 break;
209 case 'P':
210 next = &svr_opts.pidfile;
211 break;
212 #ifdef DO_MOTD
213 /* motd is displayed by default, -m turns it off */
214 case 'm':
215 svr_opts.domotd = 0;
216 break;
217 #endif
218 case 'w':
219 svr_opts.norootlogin = 1;
220 break;
221 case 'W':
222 next = &recv_window_arg;
223 break;
224 case 'K':
225 next = &keepalive_arg;
226 break;
227 case 'I':
228 next = &idle_timeout_arg;
229 break;
230 #if defined(ENABLE_SVR_PASSWORD_AUTH) || defined(ENABLE_SVR_PAM_AUTH)
231 case 's':
232 svr_opts.noauthpass = 1;
233 break;
234 case 'g':
235 svr_opts.norootpass = 1;
236 break;
237 #endif
238 case 'h':
239 printhelp(argv[0]);
240 exit(EXIT_FAILURE);
241 break;
242 case 'u':
243 /* backwards compatibility with old urandom option */
244 break;
245 #ifdef DEBUG_TRACE
246 case 'v':
247 debug_trace = 1;
248 break;
249 #endif
250 default:
251 fprintf(stderr, "Unknown argument %s\n", argv[i]);
252 printhelp(argv[0]);
253 exit(EXIT_FAILURE);
254 break;
259 /* Set up listening ports */
260 if (svr_opts.portcount == 0) {
261 svr_opts.ports[0] = m_strdup(DROPBEAR_DEFPORT);
262 svr_opts.addresses[0] = m_strdup(DROPBEAR_DEFADDRESS);
263 svr_opts.portcount = 1;
266 if (svr_opts.dsskeyfile == NULL) {
267 svr_opts.dsskeyfile = DSS_PRIV_FILENAME;
269 if (svr_opts.rsakeyfile == NULL) {
270 svr_opts.rsakeyfile = RSA_PRIV_FILENAME;
273 if (svr_opts.bannerfile) {
274 struct stat buf;
275 if (stat(svr_opts.bannerfile, &buf) != 0) {
276 dropbear_exit("Error opening banner file '%s'",
277 svr_opts.bannerfile);
280 if (buf.st_size > MAX_BANNER_SIZE) {
281 dropbear_exit("Banner file too large, max is %d bytes",
282 MAX_BANNER_SIZE);
285 svr_opts.banner = buf_new(buf.st_size);
286 if (buf_readfile(svr_opts.banner, svr_opts.bannerfile)!=DROPBEAR_SUCCESS) {
287 dropbear_exit("Error reading banner file '%s'",
288 svr_opts.bannerfile);
290 buf_setpos(svr_opts.banner, 0);
294 if (recv_window_arg) {
295 opts.recv_window = atol(recv_window_arg);
296 if (opts.recv_window == 0 || opts.recv_window > MAX_RECV_WINDOW) {
297 dropbear_exit("Bad recv window '%s'", recv_window_arg);
301 if (keepalive_arg) {
302 unsigned int val;
303 if (m_str_to_uint(keepalive_arg, &val) == DROPBEAR_FAILURE) {
304 dropbear_exit("Bad keepalive '%s'", keepalive_arg);
306 opts.keepalive_secs = val;
309 if (idle_timeout_arg) {
310 unsigned int val;
311 if (m_str_to_uint(idle_timeout_arg, &val) == DROPBEAR_FAILURE) {
312 dropbear_exit("Bad idle_timeout '%s'", idle_timeout_arg);
314 opts.idle_timeout_secs = val;
318 static void addportandaddress(char* spec) {
320 char *myspec = NULL;
322 if (svr_opts.portcount < DROPBEAR_MAX_PORTS) {
324 /* We don't free it, it becomes part of the runopt state */
325 myspec = m_strdup(spec);
327 /* search for ':', that separates address and port */
328 svr_opts.ports[svr_opts.portcount] = strchr(myspec, ':');
330 if (svr_opts.ports[svr_opts.portcount] == NULL) {
331 /* no ':' -> the whole string specifies just a port */
332 svr_opts.ports[svr_opts.portcount] = myspec;
333 } else {
334 /* Split the address/port */
335 svr_opts.ports[svr_opts.portcount][0] = '\0';
336 svr_opts.ports[svr_opts.portcount]++;
337 svr_opts.addresses[svr_opts.portcount] = myspec;
340 if (svr_opts.addresses[svr_opts.portcount] == NULL) {
341 /* no address given -> fill in the default address */
342 svr_opts.addresses[svr_opts.portcount] = m_strdup(DROPBEAR_DEFADDRESS);
345 if (svr_opts.ports[svr_opts.portcount][0] == '\0') {
346 /* empty port -> exit */
347 dropbear_exit("Bad port");
350 svr_opts.portcount++;
354 static void disablekey(int type, const char* filename) {
356 int i;
358 for (i = 0; sshhostkey[i].name != NULL; i++) {
359 if (sshhostkey[i].val == type) {
360 sshhostkey[i].usable = 0;
361 break;
364 dropbear_log(LOG_WARNING, "Failed reading '%s', disabling %s", filename,
365 type == DROPBEAR_SIGNKEY_DSS ? "DSS" : "RSA");
368 /* Must be called after syslog/etc is working */
369 void loadhostkeys() {
371 int ret;
372 int type;
374 TRACE(("enter loadhostkeys"))
376 svr_opts.hostkey = new_sign_key();
378 #ifdef DROPBEAR_RSA
379 type = DROPBEAR_SIGNKEY_RSA;
380 ret = readhostkey(svr_opts.rsakeyfile, svr_opts.hostkey, &type);
381 if (ret == DROPBEAR_FAILURE) {
382 disablekey(DROPBEAR_SIGNKEY_RSA, svr_opts.rsakeyfile);
384 #endif
385 #ifdef DROPBEAR_DSS
386 type = DROPBEAR_SIGNKEY_DSS;
387 ret = readhostkey(svr_opts.dsskeyfile, svr_opts.hostkey, &type);
388 if (ret == DROPBEAR_FAILURE) {
389 disablekey(DROPBEAR_SIGNKEY_DSS, svr_opts.dsskeyfile);
391 #endif
393 if ( 1
394 #ifdef DROPBEAR_DSS
395 && svr_opts.hostkey->dsskey == NULL
396 #endif
397 #ifdef DROPBEAR_RSA
398 && svr_opts.hostkey->rsakey == NULL
399 #endif
401 dropbear_exit("No hostkeys available");
404 TRACE(("leave loadhostkeys"))