]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - cmdline.c
Support for PuTTY-style command-line arguments in Unix PuTTY. I
[PuTTY.git] / cmdline.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include "putty.h"
5
6 /*
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.
12  * 
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.
19  * 
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.)
24  */
25
26 #define NPRIORITIES 2
27
28 struct cmdline_saved_param {
29     char *p, *value;
30 };
31 struct cmdline_saved_param_set {
32     struct cmdline_saved_param *params;
33     int nsaved, savesize;
34 };
35
36 /*
37  * C guarantees this structure will be initialised to all zero at
38  * program start, which is exactly what we want.
39  */
40 static struct cmdline_saved_param_set saves[NPRIORITIES];
41
42 static void cmdline_save_param(char *p, char *value, int pri)
43 {
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);
48     }
49     saves[pri].params[saves[pri].nsaved].p = p;
50     saves[pri].params[saves[pri].nsaved].value = value;
51     saves[pri].nsaved++;
52 }
53
54 #define SAVEABLE(pri) do { \
55     if (need_save) { cmdline_save_param(p, value, pri); return ret; } \
56 } while (0)
57
58 char *cmdline_password = NULL;
59
60 static int cmdline_get_line(const char *prompt, char *str,
61                             int maxlen, int is_pw)
62 {
63     static int tried_once = 0;
64
65     assert(is_pw && cmdline_password);
66
67     if (tried_once) {
68         return 0;
69     } else {
70         strncpy(str, cmdline_password, maxlen);
71         str[maxlen - 1] = '\0';
72         tried_once = 1;
73         return 1;
74     }
75 }
76
77 /*
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).
86  */
87 int cmdline_tooltype = 0;
88
89 static int cmdline_check_unavailable(int flag, char *p)
90 {
91     if (cmdline_tooltype & flag) {
92         cmdline_error("option \"%s\" not available in this tool", p);
93         return 1;
94     }
95     return 0;
96 }
97
98 #define UNAVAILABLE_IN(flag) do { \
99     if (cmdline_check_unavailable(flag, p)) return ret; \
100 } while (0)
101
102 /*
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.
109  */
110
111 #define RETURN(x) do { \
112     if ((x) == 2 && !value) return -2; \
113     ret = x; \
114     if (need_save < 0) return x; \
115 } while (0)
116
117 int cmdline_process_param(char *p, char *value, int need_save, Config *cfg)
118 {
119     int ret = 0;
120
121     if (!strcmp(p, "-load")) {
122         RETURN(2);
123         /* This parameter must be processed immediately rather than being
124          * saved. */
125         do_defaults(value, cfg);
126         return 2;
127     }
128     if (!strcmp(p, "-ssh")) {
129         RETURN(1);
130         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
131         SAVEABLE(0);
132         default_protocol = cfg->protocol = PROT_SSH;
133         default_port = cfg->port = 22;
134         return 1;
135     }
136     if (!strcmp(p, "-telnet")) {
137         RETURN(1);
138         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
139         SAVEABLE(0);
140         default_protocol = cfg->protocol = PROT_TELNET;
141         default_port = cfg->port = 23;
142         return 1;
143     }
144     if (!strcmp(p, "-rlogin")) {
145         RETURN(1);
146         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
147         SAVEABLE(0);
148         default_protocol = cfg->protocol = PROT_RLOGIN;
149         default_port = cfg->port = 513;
150         return 1;
151     }
152     if (!strcmp(p, "-raw")) {
153         RETURN(1);
154         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
155         SAVEABLE(0);
156         default_protocol = cfg->protocol = PROT_RAW;
157     }
158     if (!strcmp(p, "-v")) {
159         RETURN(1);
160         flags |= FLAG_VERBOSE;
161     }
162     if (!strcmp(p, "-l")) {
163         RETURN(2);
164         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
165         SAVEABLE(0);
166         strncpy(cfg->username, value, sizeof(cfg->username));
167         cfg->username[sizeof(cfg->username) - 1] = '\0';
168     }
169     if ((!strcmp(p, "-L") || !strcmp(p, "-R"))) {
170         char *fwd, *ptr, *q, *qq;
171         int i=0;
172         RETURN(2);
173         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
174         SAVEABLE(0);
175         fwd = value;
176         ptr = cfg->portfwd;
177         /* if multiple forwards, find end of list */
178         if (ptr[0]=='R' || ptr[0]=='L') {
179             for (i = 0; i < sizeof(cfg->portfwd) - 2; i++)
180                 if (ptr[i]=='\000' && ptr[i+1]=='\000')
181                     break;
182             ptr = ptr + i + 1;  /* point to next forward slot */
183         }
184         ptr[0] = p[1];  /* insert a 'L' or 'R' at the start */
185         if (strlen(fwd) > sizeof(cfg->portfwd) - i - 2) {
186             cmdline_error("out of space for port forwardings");
187             return ret;
188         }
189         strncpy(ptr+1, fwd, sizeof(cfg->portfwd) - i);
190         /*
191          * We expect _at least_ two colons in this string. The
192          * possible formats are `sourceport:desthost:destport', or
193          * `sourceip:sourceport:desthost:destport' if you're
194          * specifying a particular loopback address. We need to
195          * replace the one between source and dest with a \t; this
196          * means we must find the second-to-last colon in the
197          * string.
198          */
199         q = qq = strchr(ptr, ':');
200         while (qq) {
201             char *qqq = strchr(qq+1, ':');
202             if (qqq)
203                 q = qq;
204             qq = qqq;
205         }
206         if (q) *q = '\t';              /* replace second-last colon with \t */
207         cfg->portfwd[sizeof(cfg->portfwd) - 1] = '\0';
208         cfg->portfwd[sizeof(cfg->portfwd) - 2] = '\0';
209         ptr[strlen(ptr)+1] = '\000';    /* append two '\000' */
210     }
211     if (!strcmp(p, "-m")) {
212         char *filename, *command;
213         int cmdlen, cmdsize;
214         FILE *fp;
215         int c, d;
216
217         RETURN(2);
218         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
219         SAVEABLE(0);
220
221         filename = value;
222
223         cmdlen = cmdsize = 0;
224         command = NULL;
225         fp = fopen(filename, "r");
226         if (!fp) {
227             cmdline_error("unable to open command "
228                           "file \"%s\"", filename);
229             return ret;
230         }
231         do {
232             c = fgetc(fp);
233             d = c;
234             if (c == EOF)
235                 d = 0;
236             if (cmdlen >= cmdsize) {
237                 cmdsize = cmdlen + 512;
238                 command = sresize(command, cmdsize, char);
239             }
240             command[cmdlen++] = d;
241         } while (c != EOF);
242         cfg->remote_cmd_ptr = command;
243         cfg->remote_cmd_ptr2 = NULL;
244         cfg->nopty = TRUE;      /* command => no terminal */
245     }
246     if (!strcmp(p, "-P")) {
247         RETURN(2);
248         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
249         SAVEABLE(1);                   /* lower priority than -ssh,-telnet */
250         cfg->port = atoi(value);
251     }
252     if (!strcmp(p, "-pw")) {
253         RETURN(2);
254         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
255         cmdline_password = value;
256         ssh_get_line = cmdline_get_line;
257         ssh_getline_pw_only = TRUE;
258     }
259
260     if (!strcmp(p, "-A")) {
261         RETURN(1);
262         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
263         SAVEABLE(0);
264         cfg->agentfwd = 1;
265     }
266     if (!strcmp(p, "-a")) {
267         RETURN(1);
268         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
269         SAVEABLE(0);
270         cfg->agentfwd = 0;
271     }
272
273     if (!strcmp(p, "-X")) {
274         RETURN(1);
275         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
276         SAVEABLE(0);
277         cfg->x11_forward = 1;
278     }
279     if (!strcmp(p, "-x")) {
280         RETURN(1);
281         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
282         SAVEABLE(0);
283         cfg->x11_forward = 0;
284     }
285
286     if (!strcmp(p, "-t")) {
287         RETURN(1);
288         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
289         SAVEABLE(0);
290         cfg->nopty = 0;
291     }
292     if (!strcmp(p, "-T")) {
293         RETURN(1);
294         UNAVAILABLE_IN(TOOLTYPE_FILETRANSFER | TOOLTYPE_NONNETWORK);
295         SAVEABLE(0);
296         cfg->nopty = 1;
297     }
298
299     if (!strcmp(p, "-C")) {
300         RETURN(1);
301         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
302         SAVEABLE(0);
303         cfg->compression = 1;
304     }
305
306     if (!strcmp(p, "-1")) {
307         RETURN(1);
308         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
309         SAVEABLE(0);
310         cfg->sshprot = 0;              /* ssh protocol 1 only */
311     }
312     if (!strcmp(p, "-2")) {
313         RETURN(1);
314         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
315         SAVEABLE(0);
316         cfg->sshprot = 3;              /* ssh protocol 2 only */
317     }
318
319     if (!strcmp(p, "-i")) {
320         RETURN(2);
321         UNAVAILABLE_IN(TOOLTYPE_NONNETWORK);
322         SAVEABLE(0);
323         cfg->keyfile = filename_from_str(value);
324     }
325
326     return ret;                        /* unrecognised */
327 }
328
329 void cmdline_run_saved(Config *cfg)
330 {
331     int pri, i;
332     for (pri = 0; pri < NPRIORITIES; pri++)
333         for (i = 0; i < saves[pri].nsaved; i++)
334             cmdline_process_param(saves[pri].params[i].p,
335                                   saves[pri].params[i].value, 0, cfg);
336 }