7 * Some command-line parameters need to be saved up until after
8 * we've loaded the saved session which will form the basis of our
9 * eventual running configuration. For this we use the macro
10 * SAVEABLE, which notices if the `need_save' parameter is set and
11 * saves the parameter and value on a list.
13 * We also assign priorities to saved parameters, just to slightly
14 * ameliorate silly ordering problems. For example, if you specify
15 * a saved session to load, it will be loaded _before_ all your
16 * local modifications such as -L are evaluated; and if you specify
17 * a protocol and a port, the protocol is set up first so that the
18 * port can override its choice of port number.
20 * (In fact -load is not saved at all, since in at least Plink the
21 * processing of further command-line options depends on whether or
22 * not the loaded session contained a hostname. So it must be
23 * executed immediately.)
28 struct cmdline_saved_param {
31 struct cmdline_saved_param_set {
32 struct cmdline_saved_param *params;
37 * C guarantees this structure will be initialised to all zero at
38 * program start, which is exactly what we want.
40 static struct cmdline_saved_param_set saves[NPRIORITIES];
42 static void cmdline_save_param(char *p, char *value, int pri)
44 if (saves[pri].nsaved >= saves[pri].savesize) {
45 saves[pri].savesize = saves[pri].nsaved + 32;
46 saves[pri].params = sresize(saves[pri].params, saves[pri].savesize,
47 struct cmdline_saved_param);
49 saves[pri].params[saves[pri].nsaved].p = p;
50 saves[pri].params[saves[pri].nsaved].value = value;
54 #define SAVEABLE(pri) do { \
55 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
58 char *cmdline_password = NULL;
60 static int cmdline_get_line(const char *prompt, char *str,
61 int maxlen, int is_pw)
63 static int tried_once = 0;
65 assert(is_pw && cmdline_password);
70 strncpy(str, cmdline_password, maxlen);
71 str[maxlen - 1] = '\0';
78 * Here we have a flags word which describes the capabilities of
79 * the particular tool on whose behalf we're running. We will
80 * refuse certain command-line options if a particular tool
81 * inherently can't do anything sensible. For example, the file
82 * transfer tools (psftp, pscp) can't do a great deal with protocol
83 * selections (ever tried running scp over telnet?) or with port
84 * forwarding (even if it wasn't a hideously bad idea, they don't
85 * have the select() infrastructure to make them work).
87 int cmdline_tooltype = 0;
89 static int cmdline_check_unavailable(int flag, char *p)
91 if (cmdline_tooltype & flag) {
92 cmdline_error("option \"%s\" not available in this tool", p);
98 #define UNAVAILABLE_IN(flag) do { \
99 if (cmdline_check_unavailable(flag, p)) return ret; \
103 * Process a standard command-line parameter. `p' is the parameter
104 * in question; `value' is the subsequent element of argv, which
105 * may or may not be required as an operand to the parameter.
106 * Return value is 2 if both arguments were used; 1 if only p was
107 * used; 0 if the parameter wasn't one we recognised; -2 if it
108 * should have been 2 but value was NULL.
111 #define RETURN(x) do { \
112 if ((x) == 2 && !value) return -2; \
114 if (need_save < 0) return x; \
117 int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
121 if (!strcmp(p, "-load")) {
123 /* This parameter must be processed immediately rather than being
125 do_defaults(value, cfg);
128 if (!strcmp(p, "-ssh")) {
130 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
132 default_protocol = cfg->protocol = PROT_SSH;
133 default_port = cfg->port = 22;
136 if (!strcmp(p, "-telnet")) {
138 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
140 default_protocol = cfg->protocol = PROT_TELNET;
141 default_port = cfg->port = 23;
144 if (!strcmp(p, "-rlogin")) {
146 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
148 default_protocol = cfg->protocol = PROT_RLOGIN;
149 default_port = cfg->port = 513;
152 if (!strcmp(p, "-raw")) {
154 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
156 default_protocol = cfg->protocol = PROT_RAW;
158 if (!strcmp(p, "-v")) {
160 flags |= FLAG_VERBOSE;
162 if (!strcmp(p, "-l")) {
164 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
166 strncpy(cfg->username, value, sizeof(cfg->username));
167 cfg->username[sizeof(cfg->username) - 1] = '\0';
169 if ((!strcmp(p, "-L") || !strcmp(p, "-R") || !strcmp(p, "-D"))) {
170 char *fwd, *ptr, *q, *qq;
173 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
175 dynamic = !strcmp(p, "-D");
178 /* if multiple forwards, find end of list */
179 if (ptr[0]=='R' || ptr[0]=='L' || ptr[0] == 'D') {
180 for (i = 0; i < sizeof(cfg->portfwd) - 2; i++)
181 if (ptr[i]=='\000' && ptr[i+1]=='\000')
183 ptr = ptr + i + 1; /* point to next forward slot */
185 ptr[0] = p[1]; /* insert a 'L', 'R' or 'D' at the start */
186 if (strlen(fwd) > sizeof(cfg->portfwd) - i - 2) {
187 cmdline_error("out of space for port forwardings");
190 strncpy(ptr+1, fwd, sizeof(cfg->portfwd) - i);
193 * We expect _at least_ two colons in this string. The
194 * possible formats are `sourceport:desthost:destport',
195 * or `sourceip:sourceport:desthost:destport' if you're
196 * specifying a particular loopback address. We need to
197 * replace the one between source and dest with a \t;
198 * this means we must find the second-to-last colon in
201 q = qq = strchr(ptr, ':');
203 char *qqq = strchr(qq+1, ':');
208 if (q) *q = '\t'; /* replace second-last colon with \t */
210 cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
211 cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
212 ptr[strlen(ptr)+1] = '\000'; /* append two '\000' */
214 if (!strcmp(p, "-m")) {
215 char *filename, *command;
221 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
226 cmdlen = cmdsize = 0;
228 fp = fopen(filename, "r");
230 cmdline_error("unable to open command "
231 "file \"%s\"", filename);
239 if (cmdlen >= cmdsize) {
240 cmdsize = cmdlen + 512;
241 command = sresize(command, cmdsize, char);
243 command[cmdlen++] = d;
245 cfg->remote_cmd_ptr = command;
246 cfg->remote_cmd_ptr2 = NULL;
247 cfg->nopty = TRUE; /* command => no terminal */
249 if (!strcmp(p, "-P")) {
251 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
252 SAVEABLE(1); /* lower priority than -ssh,-telnet */
253 cfg->port = atoi(value);
255 if (!strcmp(p, "-pw")) {
257 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
258 cmdline_password = value;
259 ssh_get_line = cmdline_get_line;
260 ssh_getline_pw_only = TRUE;
263 if (!strcmp(p, "-A")) {
265 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
269 if (!strcmp(p, "-a")) {
271 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
276 if (!strcmp(p, "-X")) {
278 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
280 cfg->x11_forward = 1;
282 if (!strcmp(p, "-x")) {
284 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
286 cfg->x11_forward = 0;
289 if (!strcmp(p, "-t")) {
291 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
295 if (!strcmp(p, "-T")) {
297 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
302 if (!strcmp(p, "-C")) {
304 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
306 cfg->compression = 1;
309 if (!strcmp(p, "-1")) {
311 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
313 cfg->sshprot = 0; /* ssh protocol 1 only */
315 if (!strcmp(p, "-2")) {
317 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
319 cfg->sshprot = 3; /* ssh protocol 2 only */
322 if (!strcmp(p, "-i")) {
324 UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
326 cfg->keyfile = filename_from_str(value);
329 return ret; /* unrecognised */
332 void cmdline_run_saved(Config *cfg)
335 for (pri = 0; pri < NPRIORITIES; pri++)
336 for (i = 0; i < saves[pri].nsaved; i++)
337 cmdline_process_param(saves[pri].params[i].p,
338 saves[pri].params[i].value, 0, cfg);