comparison svr-runopts.c @ 434:0aaaf68e97dc

Add -p [address:]port option for binding to addresses, patch from Max-Gerd Retzlaff
author Matt Johnston <matt@ucc.asn.au>
date Thu, 22 Feb 2007 14:52:46 +0000
parents be18c7dd486e
children 337c45621e81
comparison
equal deleted inserted replaced
291:55a99934db87 434:0aaaf68e97dc
30 #include "algo.h" 30 #include "algo.h"
31 31
32 svr_runopts svr_opts; /* GLOBAL */ 32 svr_runopts svr_opts; /* GLOBAL */
33 33
34 static void printhelp(const char * progname); 34 static void printhelp(const char * progname);
35 static void addportandaddress(char* spec);
35 36
36 static void printhelp(const char * progname) { 37 static void printhelp(const char * progname) {
37 38
38 fprintf(stderr, "Dropbear sshd v%s\n" 39 fprintf(stderr, "Dropbear sshd v%s\n"
39 "Usage: %s [options]\n" 40 "Usage: %s [options]\n"
68 #endif 69 #endif
69 #ifdef ENABLE_SVR_REMOTETCPFWD 70 #ifdef ENABLE_SVR_REMOTETCPFWD
70 "-k Disable remote port forwarding\n" 71 "-k Disable remote port forwarding\n"
71 "-a Allow connections to forwarded ports from any host\n" 72 "-a Allow connections to forwarded ports from any host\n"
72 #endif 73 #endif
73 "-p port Listen on specified tcp port, up to %d can be specified\n" 74 "-p [address:]port\n"
74 " (default %s if none specified)\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"
75 #ifdef INETD_MODE 78 #ifdef INETD_MODE
76 "-i Start for inetd\n" 79 "-i Start for inetd\n"
77 #endif 80 #endif
78 #ifdef DEBUG_TRACE 81 #ifdef DEBUG_TRACE
79 "-v verbose\n" 82 "-v verbose\n"
90 93
91 void svr_getopts(int argc, char ** argv) { 94 void svr_getopts(int argc, char ** argv) {
92 95
93 unsigned int i; 96 unsigned int i;
94 char ** next = 0; 97 char ** next = 0;
98 int nextisport = 0;
95 99
96 /* see printhelp() for options */ 100 /* see printhelp() for options */
97 svr_opts.rsakeyfile = NULL; 101 svr_opts.rsakeyfile = NULL;
98 svr_opts.dsskeyfile = NULL; 102 svr_opts.dsskeyfile = NULL;
99 svr_opts.bannerfile = NULL; 103 svr_opts.bannerfile = NULL;
124 #ifdef ENABLE_SVR_REMOTETCPFWD 128 #ifdef ENABLE_SVR_REMOTETCPFWD
125 opts.listen_fwd_all = 0; 129 opts.listen_fwd_all = 0;
126 #endif 130 #endif
127 131
128 for (i = 1; i < (unsigned int)argc; i++) { 132 for (i = 1; i < (unsigned int)argc; i++) {
133 if (nextisport) {
134 addportandaddress(argv[i]);
135 nextisport = 0;
136 continue;
137 }
138
129 if (next) { 139 if (next) {
130 *next = argv[i]; 140 *next = argv[i];
131 if (*next == NULL) { 141 if (*next == NULL) {
132 dropbear_exit("Invalid null argument"); 142 dropbear_exit("Invalid null argument");
133 } 143 }
175 case 'i': 185 case 'i':
176 svr_opts.inetdmode = 1; 186 svr_opts.inetdmode = 1;
177 break; 187 break;
178 #endif 188 #endif
179 case 'p': 189 case 'p':
180 if (svr_opts.portcount < DROPBEAR_MAX_PORTS) { 190 nextisport = 1;
181 svr_opts.ports[svr_opts.portcount] = NULL; 191 break;
182 next = &svr_opts.ports[svr_opts.portcount];
183 /* Note: if it doesn't actually get set, we'll
184 * decrement it after the loop */
185 svr_opts.portcount++;
186 }
187 break;
188 #ifdef DO_MOTD 192 #ifdef DO_MOTD
189 /* motd is displayed by default, -m turns it off */ 193 /* motd is displayed by default, -m turns it off */
190 case 'm': 194 case 'm':
191 svr_opts.domotd = 0; 195 svr_opts.domotd = 0;
192 break; 196 break;
221 } 225 }
222 226
223 /* Set up listening ports */ 227 /* Set up listening ports */
224 if (svr_opts.portcount == 0) { 228 if (svr_opts.portcount == 0) {
225 svr_opts.ports[0] = m_strdup(DROPBEAR_DEFPORT); 229 svr_opts.ports[0] = m_strdup(DROPBEAR_DEFPORT);
230 svr_opts.addresses[0] = m_strdup(DROPBEAR_DEFADDRESS);
226 svr_opts.portcount = 1; 231 svr_opts.portcount = 1;
227 } else { 232 }
228 /* we may have been given a -p option but no argument to go with 233
229 * it */
230 if (svr_opts.ports[svr_opts.portcount-1] == NULL) {
231 svr_opts.portcount--;
232 }
233 }
234
235 if (svr_opts.dsskeyfile == NULL) { 234 if (svr_opts.dsskeyfile == NULL) {
236 svr_opts.dsskeyfile = DSS_PRIV_FILENAME; 235 svr_opts.dsskeyfile = DSS_PRIV_FILENAME;
237 } 236 }
238 if (svr_opts.rsakeyfile == NULL) { 237 if (svr_opts.rsakeyfile == NULL) {
239 svr_opts.rsakeyfile = RSA_PRIV_FILENAME; 238 svr_opts.rsakeyfile = RSA_PRIV_FILENAME;
259 buf_setpos(svr_opts.banner, 0); 258 buf_setpos(svr_opts.banner, 0);
260 } 259 }
261 260
262 } 261 }
263 262
263 static void addportandaddress(char* spec) {
264
265 char *myspec = NULL;
266
267 if (svr_opts.portcount < DROPBEAR_MAX_PORTS) {
268
269 /* We don't free it, it becomes part of the runopt state */
270 myspec = m_strdup(spec);
271
272 /* search for ':', that separates address and port */
273 svr_opts.ports[svr_opts.portcount] = strchr(myspec, ':');
274
275 if (svr_opts.ports[svr_opts.portcount] == NULL) {
276 /* no ':' -> the whole string specifies just a port */
277 svr_opts.ports[svr_opts.portcount] = myspec;
278 } else {
279 /* Split the address/port */
280 svr_opts.ports[svr_opts.portcount][0] = '\0';
281 svr_opts.ports[svr_opts.portcount]++;
282 svr_opts.addresses[svr_opts.portcount] = myspec;
283 }
284
285 if (svr_opts.addresses[svr_opts.portcount] == NULL) {
286 /* no address given -> fill in the default address */
287 svr_opts.addresses[svr_opts.portcount] = m_strdup(DROPBEAR_DEFADDRESS);
288 }
289
290 if (svr_opts.ports[svr_opts.portcount][0] == '\0') {
291 /* empty port -> exit */
292 dropbear_exit("Bad port");
293 }
294
295 svr_opts.portcount++;
296 }
297 }
298
264 static void disablekey(int type, const char* filename) { 299 static void disablekey(int type, const char* filename) {
265 300
266 int i; 301 int i;
267 302
268 for (i = 0; sshhostkey[i].name != NULL; i++) { 303 for (i = 0; sshhostkey[i].name != NULL; i++) {