8 * Some command-line parameters need to be saved up until after
9 * we've loaded the saved session which will form the basis of our
10 * eventual running configuration. For this we use the macro
11 * SAVEABLE, which notices if the `need_save' parameter is set and
12 * saves the parameter and value on a list.
14 * We also assign priorities to saved parameters, just to slightly
15 * ameliorate silly ordering problems. For example, if you specify
16 * a saved session to load, it will be loaded _before_ all your
17 * local modifications such as -L are evaluated; and if you specify
18 * a protocol and a port, the protocol is set up first so that the
19 * port can override its choice of port number.
24 struct cmdline_saved_param {
27 struct cmdline_saved_param_set {
28 struct cmdline_saved_param *params;
33 * C guarantees this structure will be initialised to all zero at
34 * program start, which is exactly what we want.
36 static struct cmdline_saved_param_set saves[NPRIORITIES];
38 static void cmdline_save_param(char *p, char *value, int pri)
40 if (saves[pri].nsaved >= saves[pri].savesize) {
41 saves[pri].savesize = saves[pri].nsaved + 32;
43 srealloc(saves[pri].params,
44 saves[pri].savesize*sizeof(*saves[pri].params));
46 saves[pri].params[saves[pri].nsaved].p = p;
47 saves[pri].params[saves[pri].nsaved].value = value;
51 #define SAVEABLE(pri) do { \
52 if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
55 char *cmdline_password = NULL;
57 static int cmdline_get_line(const char *prompt, char *str,
58 int maxlen, int is_pw)
60 static int tried_once = 0;
62 assert(is_pw && cmdline_password);
67 strncpy(str, cmdline_password, maxlen);
68 str[maxlen - 1] = '\0';
75 * Here we have a flags word which describes the capabilities of
76 * the particular tool on whose behalf we're running. We will
77 * refuse certain command-line options if a particular tool
78 * inherently can't do anything sensible. For example, the file
79 * transfer tools (psftp, pscp) can't do a great deal with protocol
80 * selections (ever tried running scp over telnet?) or with port
81 * forwarding (even if it wasn't a hideously bad idea, they don't
82 * have the select() infrastructure to make them work).
84 int cmdline_tooltype = 0;
86 static int cmdline_check_unavailable(int flag, char *p)
88 if (cmdline_tooltype & flag) {
89 cmdline_error("option \"%s\" not available in this tool", p);
95 #define UNAVAILABLE_IN(flag) do { \
96 if (cmdline_check_unavailable(flag, p)) return ret; \
100 * Process a standard command-line parameter. `p' is the parameter
101 * in question; `value' is the subsequent element of argv, which
102 * may or may not be required as an operand to the parameter.
103 * Return value is 2 if both arguments were used; 1 if only p was
104 * used; 0 if the parameter wasn't one we recognised; -2 if it
105 * should have been 2 but value was NULL.
108 #define RETURN(x) do { \
109 if ((x) == 2 && !value) return -2; \
113 int cmdline_process_param(char *p, char *value, int need_save)
117 if (!strcmp(p, "-load")) {
119 SAVEABLE(0); /* very high priority */
120 do_defaults(value, &cfg);
123 if (!strcmp(p, "-ssh")) {
125 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
127 default_protocol = cfg.protocol = PROT_SSH;
128 default_port = cfg.port = 22;
131 if (!strcmp(p, "-telnet")) {
133 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
135 default_protocol = cfg.protocol = PROT_TELNET;
136 default_port = cfg.port = 23;
139 if (!strcmp(p, "-rlogin")) {
141 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
143 default_protocol = cfg.protocol = PROT_RLOGIN;
144 default_port = cfg.port = 513;
147 if (!strcmp(p, "-raw")) {
149 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
151 default_protocol = cfg.protocol = PROT_RAW;
153 if (!strcmp(p, "-v")) {
155 flags |= FLAG_VERBOSE;
157 if (!strcmp(p, "-l")) {
160 strncpy(cfg.username, value, sizeof(cfg.username));
161 cfg.username[sizeof(cfg.username) - 1] = '\0';
163 if ((!strcmp(p, "-L") || !strcmp(p, "-R"))) {
167 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
171 /* if multiple forwards, find end of list */
172 if (ptr[0]=='R' || ptr[0]=='L') {
173 for (i = 0; i < sizeof(cfg.portfwd) - 2; i++)
174 if (ptr[i]=='\000' && ptr[i+1]=='\000')
176 ptr = ptr + i + 1; /* point to next forward slot */
178 ptr[0] = p[1]; /* insert a 'L' or 'R' at the start */
179 if (strlen(fwd) > sizeof(cfg.portfwd) - i - 2) {
180 cmdline_error("out of space for port forwardings");
183 strncpy(ptr+1, fwd, sizeof(cfg.portfwd) - i);
184 q = strchr(ptr, ':');
185 if (q) *q = '\t'; /* replace first : with \t */
186 cfg.portfwd[sizeof(cfg.portfwd) - 1] = '\0';
187 cfg.portfwd[sizeof(cfg.portfwd) - 2] = '\0';
188 ptr[strlen(ptr)+1] = '\000'; /* append two '\000' */
190 if (!strcmp(p, "-m")) {
191 char *filename, *command;
197 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
202 cmdlen = cmdsize = 0;
204 fp = fopen(filename, "r");
206 cmdline_error("unable to open command "
207 "file \"%s\"", filename);
215 if (cmdlen >= cmdsize) {
216 cmdsize = cmdlen + 512;
217 command = srealloc(command, cmdsize);
219 command[cmdlen++] = d;
221 cfg.remote_cmd_ptr = command;
222 cfg.remote_cmd_ptr2 = NULL;
223 cfg.nopty = TRUE; /* command => no terminal */
225 if (!strcmp(p, "-P")) {
227 SAVEABLE(2); /* lower priority than -ssh,-telnet */
228 cfg.port = atoi(value);
230 if (!strcmp(p, "-pw")) {
232 cmdline_password = value;
233 ssh_get_line = cmdline_get_line;
234 ssh_getline_pw_only = TRUE;
237 if (!strcmp(p, "-A")) {
239 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
243 if (!strcmp(p, "-a")) {
245 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
250 if (!strcmp(p, "-X")) {
252 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
256 if (!strcmp(p, "-x")) {
258 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
263 if (!strcmp(p, "-t")) {
265 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
269 if (!strcmp(p, "-T")) {
271 UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER);
276 if (!strcmp(p, "-C")) {
282 if (!strcmp(p, "-1")) {
285 cfg.sshprot = 0; /* ssh protocol 1 only */
287 if (!strcmp(p, "-2")) {
290 cfg.sshprot = 3; /* ssh protocol 2 only */
293 if (!strcmp(p, "-i")) {
296 strncpy(cfg.keyfile, value, sizeof(cfg.keyfile));
297 cfg.keyfile[sizeof(cfg.keyfile)-1] = '\0';
300 return ret; /* unrecognised */
303 void cmdline_run_saved(void)
306 for (pri = 0; pri < NPRIORITIES; pri++)
307 for (i = 0; i < saves[pri].nsaved; i++)
308 cmdline_process_param(saves[pri].params[i].p,
309 saves[pri].params[i].value, 0);