Mercurial > dropbear
comparison cli-runopts.c @ 64:efb5e0b335cf
TCP forwarding works.
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 12 Aug 2004 13:48:42 +0000 |
parents | 20563735e8b5 |
children | 38c3146aa23d |
comparison
equal
deleted
inserted
replaced
63:dcc43965928f | 64:efb5e0b335cf |
---|---|
26 #include "runopts.h" | 26 #include "runopts.h" |
27 #include "signkey.h" | 27 #include "signkey.h" |
28 #include "buffer.h" | 28 #include "buffer.h" |
29 #include "dbutil.h" | 29 #include "dbutil.h" |
30 #include "algo.h" | 30 #include "algo.h" |
31 #include "tcpfwd.h" | |
31 | 32 |
32 cli_runopts cli_opts; /* GLOBAL */ | 33 cli_runopts cli_opts; /* GLOBAL */ |
33 | 34 |
34 static void printhelp(); | 35 static void printhelp(); |
35 static void parsehostname(char* userhostarg); | 36 static void parsehostname(char* userhostarg); |
36 #ifdef DROPBEAR_PUBKEY_AUTH | 37 #ifdef DROPBEAR_PUBKEY_AUTH |
37 static void loadidentityfile(const char* filename); | 38 static void loadidentityfile(const char* filename); |
39 #endif | |
40 #ifdef ENABLE_CLI_ANYTCPFWD | |
41 static void addforward(char* str, struct TCPFwdList** fwdlist); | |
38 #endif | 42 #endif |
39 | 43 |
40 static void printhelp() { | 44 static void printhelp() { |
41 | 45 |
42 fprintf(stderr, "Dropbear client v%s\n" | 46 fprintf(stderr, "Dropbear client v%s\n" |
46 "-t Allocate a pty\n" | 50 "-t Allocate a pty\n" |
47 "-T Don't allocate a pty\n" | 51 "-T Don't allocate a pty\n" |
48 #ifdef DROPBEAR_PUBKEY_AUTH | 52 #ifdef DROPBEAR_PUBKEY_AUTH |
49 "-i <identityfile> (multiple allowed)\n" | 53 "-i <identityfile> (multiple allowed)\n" |
50 #endif | 54 #endif |
51 #ifndef DISABLE_REMOTETCPFWD | 55 #ifdef ENABLE_CLI_LOCALTCPFWD |
52 "-L <listenport:remotehsot:reportport> Local port forwarding\n" | 56 "-L <listenport:remotehsot:reportport> Local port forwarding\n" |
53 #endif | 57 #endif |
54 #ifndef DISABLE_TCPFWD_DIRECT | 58 #ifdef ENABLE_CLI_REMOTETCPFWD |
55 "-R <listenport:remotehost:remoteport> Remote port forwarding\n" | 59 "-R <listenport:remotehost:remoteport> Remote port forwarding\n" |
56 #endif | 60 #endif |
57 ,DROPBEAR_VERSION, cli_opts.progname); | 61 ,DROPBEAR_VERSION, cli_opts.progname); |
58 } | 62 } |
59 | 63 |
63 char ** next = 0; | 67 char ** next = 0; |
64 unsigned int cmdlen; | 68 unsigned int cmdlen; |
65 #ifdef DROPBEAR_PUBKEY_AUTH | 69 #ifdef DROPBEAR_PUBKEY_AUTH |
66 int nextiskey = 0; /* A flag if the next argument is a keyfile */ | 70 int nextiskey = 0; /* A flag if the next argument is a keyfile */ |
67 #endif | 71 #endif |
68 #ifdef DROPBEAR_CLI_LOCALTCP | 72 #ifdef ENABLE_CLI_LOCALTCPFWD |
69 int nextislocal = 0; | 73 int nextislocal = 0; |
70 #endif | 74 #endif |
71 #ifdef DROPBEAR_CLI_REMOTETCP | 75 #ifdef ENABLE_CLI_REMOTETCPFWD |
72 int nextisremote = 0; | 76 int nextisremote = 0; |
73 #endif | 77 #endif |
74 | |
75 | |
76 | 78 |
77 /* see printhelp() for options */ | 79 /* see printhelp() for options */ |
78 cli_opts.progname = argv[0]; | 80 cli_opts.progname = argv[0]; |
79 cli_opts.remotehost = NULL; | 81 cli_opts.remotehost = NULL; |
80 cli_opts.remoteport = NULL; | 82 cli_opts.remoteport = NULL; |
82 cli_opts.cmd = NULL; | 84 cli_opts.cmd = NULL; |
83 cli_opts.wantpty = 9; /* 9 means "it hasn't been touched", gets set later */ | 85 cli_opts.wantpty = 9; /* 9 means "it hasn't been touched", gets set later */ |
84 #ifdef DROPBEAR_PUBKEY_AUTH | 86 #ifdef DROPBEAR_PUBKEY_AUTH |
85 cli_opts.pubkeys = NULL; | 87 cli_opts.pubkeys = NULL; |
86 #endif | 88 #endif |
87 #ifdef DROPBEAR_CLI_LOCALTCP | 89 #ifdef ENABLE_CLI_LOCALTCPFWD |
88 cli_opts.localports = NULL; | 90 cli_opts.localfwds = NULL; |
89 #endif | 91 #endif |
90 #ifdef DROPBEAR_CLI_REMOTETCP | 92 #ifdef ENABLE_CLI_REMOTETCPFWD |
91 cli_opts.remoteports = NULL; | 93 cli_opts.remotefwds = NULL; |
92 #endif | 94 #endif |
93 opts.nolocaltcp = 0; | 95 opts.nolocaltcp = 0; |
94 opts.noremotetcp = 0; | 96 opts.noremotetcp = 0; |
95 /* not yet | 97 /* not yet |
96 opts.ipv4 = 1; | 98 opts.ipv4 = 1; |
102 #ifdef DROPBEAR_PUBKEY_AUTH | 104 #ifdef DROPBEAR_PUBKEY_AUTH |
103 if (nextiskey) { | 105 if (nextiskey) { |
104 /* Load a hostkey since the previous argument was "-i" */ | 106 /* Load a hostkey since the previous argument was "-i" */ |
105 loadidentityfile(argv[i]); | 107 loadidentityfile(argv[i]); |
106 nextiskey = 0; | 108 nextiskey = 0; |
109 continue; | |
110 } | |
111 #endif | |
112 #ifdef ENABLE_CLI_REMOTETCPFWD | |
113 if (nextisremote) { | |
114 TRACE(("nextisremote true")); | |
115 addforward(argv[i], &cli_opts.remotefwds); | |
116 nextisremote = 0; | |
117 continue; | |
118 } | |
119 #endif | |
120 #ifdef ENABLE_CLI_LOCALTCPFWD | |
121 if (nextislocal) { | |
122 TRACE(("nextislocal true")); | |
123 addforward(argv[i], &cli_opts.localfwds); | |
124 nextislocal = 0; | |
107 continue; | 125 continue; |
108 } | 126 } |
109 #endif | 127 #endif |
110 if (next) { | 128 if (next) { |
111 /* The previous flag set a value to assign */ | 129 /* The previous flag set a value to assign */ |
133 cli_opts.wantpty = 1; | 151 cli_opts.wantpty = 1; |
134 break; | 152 break; |
135 case 'T': /* don't want a pty */ | 153 case 'T': /* don't want a pty */ |
136 cli_opts.wantpty = 0; | 154 cli_opts.wantpty = 0; |
137 break; | 155 break; |
156 #ifdef ENABLE_CLI_LOCALTCPFWD | |
157 case 'L': | |
158 nextislocal = 1; | |
159 break; | |
160 #endif | |
161 #ifdef ENABLE_CLI_REMOTETCPFWD | |
162 case 'R': | |
163 nextisremote = 1; | |
164 break; | |
165 #endif | |
138 default: | 166 default: |
139 fprintf(stderr, "Unknown argument '%s'\n", argv[i]); | 167 fprintf(stderr, "Unknown argument '%s'\n", argv[i]); |
140 printhelp(); | 168 printhelp(); |
141 exit(EXIT_FAILURE); | 169 exit(EXIT_FAILURE); |
142 break; | 170 break; |
143 } /* Switch */ | 171 } /* Switch */ |
144 | 172 |
145 continue; /* next argument */ | 173 continue; /* next argument */ |
146 | 174 |
147 } else { | 175 } else { |
148 TRACE(("non-flag arg")); | 176 TRACE(("non-flag arg: '%s'", argv[i])); |
149 | 177 |
150 /* Either the hostname or commands */ | 178 /* Either the hostname or commands */ |
151 | 179 |
152 if (cli_opts.remotehost == NULL) { | 180 if (cli_opts.remotehost == NULL) { |
153 | 181 |
224 #endif | 252 #endif |
225 | 253 |
226 | 254 |
227 /* Parses a [user@]hostname argument. userhostarg is the argv[i] corresponding | 255 /* Parses a [user@]hostname argument. userhostarg is the argv[i] corresponding |
228 * - note that it will be modified */ | 256 * - note that it will be modified */ |
229 static void parsehostname(char* userhostarg) { | 257 static void parsehostname(char* orighostarg) { |
230 | 258 |
231 uid_t uid; | 259 uid_t uid; |
232 struct passwd *pw = NULL; | 260 struct passwd *pw = NULL; |
261 char *userhostarg = NULL; | |
262 | |
263 /* We probably don't want to be editing argvs */ | |
264 userhostarg = m_strdup(orighostarg); | |
233 | 265 |
234 cli_opts.remotehost = strchr(userhostarg, '@'); | 266 cli_opts.remotehost = strchr(userhostarg, '@'); |
235 if (cli_opts.remotehost == NULL) { | 267 if (cli_opts.remotehost == NULL) { |
236 /* no username portion, the cli-auth.c code can figure the | 268 /* no username portion, the cli-auth.c code can figure the |
237 * local user's name */ | 269 * local user's name */ |
255 | 287 |
256 if (cli_opts.remotehost[0] == '\0') { | 288 if (cli_opts.remotehost[0] == '\0') { |
257 dropbear_exit("Bad hostname"); | 289 dropbear_exit("Bad hostname"); |
258 } | 290 } |
259 } | 291 } |
292 | |
293 #ifdef ENABLE_CLI_ANYTCPFWD | |
294 /* Turn a "listenport:remoteaddr:remoteport" string into into a forwarding | |
295 * set, and add it to the forwarding list */ | |
296 static void addforward(char* origstr, struct TCPFwdList** fwdlist) { | |
297 | |
298 char * listenport = NULL; | |
299 char * connectport = NULL; | |
300 char * connectaddr = NULL; | |
301 struct TCPFwdList* newfwd = NULL; | |
302 char * str = NULL; | |
303 | |
304 TRACE(("enter addforward")); | |
305 | |
306 /* We probably don't want to be editing argvs */ | |
307 str = m_strdup(origstr); | |
308 | |
309 listenport = str; | |
310 | |
311 connectaddr = strchr(str, ':'); | |
312 if (connectaddr == NULL) { | |
313 TRACE(("connectaddr == NULL")); | |
314 goto fail; | |
315 } | |
316 | |
317 connectaddr[0] = '\0'; | |
318 connectaddr++; | |
319 | |
320 connectport = strchr(connectaddr, ':'); | |
321 if (connectport == NULL) { | |
322 TRACE(("connectport == NULL")); | |
323 goto fail; | |
324 } | |
325 | |
326 connectport[0] = '\0'; | |
327 connectport++; | |
328 | |
329 newfwd = (struct TCPFwdList*)m_malloc(sizeof(struct TCPFwdList)); | |
330 | |
331 /* Now we check the ports - note that the port ints are unsigned, | |
332 * the check later only checks for >= MAX_PORT */ | |
333 newfwd->listenport = strtol(listenport, NULL, 10); | |
334 if (errno != 0) { | |
335 TRACE(("bad listenport strtol")); | |
336 goto fail; | |
337 } | |
338 | |
339 newfwd->connectport = strtol(connectport, NULL, 10); | |
340 if (errno != 0) { | |
341 TRACE(("bad connectport strtol")); | |
342 goto fail; | |
343 } | |
344 | |
345 newfwd->connectaddr = connectaddr; | |
346 | |
347 if (newfwd->listenport > 65535) { | |
348 TRACE(("listenport > 65535")); | |
349 goto badport; | |
350 } | |
351 | |
352 if (newfwd->connectport > 65535) { | |
353 TRACE(("connectport > 65535")); | |
354 goto badport; | |
355 } | |
356 | |
357 newfwd->next = *fwdlist; | |
358 *fwdlist = newfwd; | |
359 | |
360 TRACE(("leave addforward: done")); | |
361 return; | |
362 | |
363 fail: | |
364 dropbear_exit("Bad TCP forward '%s'", origstr); | |
365 | |
366 badport: | |
367 dropbear_exit("Bad TCP port in '%s'", origstr); | |
368 } | |
369 #endif |