2 * FreeRDP: A Remote Desktop Protocol client.
5 * Copyright 2009-2011 Jay Sorg
6 * Copyright 2011 Vic Lee
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <freerdp/settings.h>
26 #include <freerdp/utils/print.h>
27 #include <freerdp/utils/memory.h>
28 #include <freerdp/utils/args.h>
31 * Parse command-line arguments and update rdpSettings members accordingly.
32 * @param settings pointer to rdpSettings struct to be updated.
33 * @param argc number of arguments available.
34 * @param argv string array of the arguments.
35 * @param plugin_callback function to be called when a plugin needs to be loaded.
36 * @param plugin_user_data pointer to be passed to the plugin_callback function.
37 * @param ui_callback function to be called when a UI-specific argument is being processed.
38 * @param ui_user_data pointer to be passed to the ui_callback function.
39 * @return number of arguments that were parsed, or FREERDP_ARGS_PARSE_RESULT on failure or --version/--help
41 int freerdp_parse_args(rdpSettings* settings, int argc, char** argv,
42 ProcessPluginArgs plugin_callback, void* plugin_user_data,
43 ProcessUIArgs ui_callback, void* ui_user_data)
49 int num_extensions = 0;
50 RDP_PLUGIN_DATA* plugin_data;
54 if ((strcmp("-h", argv[index]) == 0 ) || (strcmp("--help", argv[index]) == 0 ))
57 "FreeRDP - A Free Remote Desktop Protocol Client\n"
58 "See http://www.freerdp.com for more information\n"
60 "Usage: %s [options] server:port\n"
61 " -0: connect to console session\n"
62 " -a: set color depth in bit, default is 16\n"
63 " -c: initial working directory\n"
64 " -D: hide window decorations\n"
67 " -f: fullscreen mode\n"
68 " -g: set geometry, using format WxH or X%% or 'workarea', default is 1024x768\n"
69 " -h: print this help\n"
70 " -k: set keyboard layout ID\n"
71 " -K: do not interfere with window manager bindings\n"
72 " -m: don't send mouse motion events\n"
74 " -o: console audio\n"
76 " -s: set startup-shell\n"
77 " -t: alternative port number, default is 3389\n"
79 " -x: performance flags (m[odem], b[roadband] or l[an])\n"
80 " -X: embed into another window with a given XID.\n"
81 " -z: enable compression\n"
82 " --app: RemoteApp connection. This implies -g workarea\n"
83 " --ext: load an extension\n"
84 " --no-auth: disable authentication\n"
85 " --no-fastpath: disable fast-path\n"
86 " --gdi: graphics rendering (hw, sw)\n"
87 " --no-osb: disable offscreen bitmaps\n"
88 " --no-bmp-cache: disable bitmap cache\n"
89 " --plugin: load a virtual channel plugin\n"
90 " --rfx: enable RemoteFX\n"
91 " --rfx-mode: RemoteFX operational flags (v[ideo], i[mage]), default is video\n"
92 " --nsc: enable NSCodec (experimental)\n"
93 " --disable-wallpaper: disables wallpaper\n"
94 " --composition: enable desktop composition\n"
95 " --disable-full-window-drag: disables full window drag\n"
96 " --disable-menu-animations: disables menu animations\n"
97 " --disable-theming: disables theming\n"
98 " --no-rdp: disable Standard RDP encryption\n"
99 " --no-tls: disable TLS encryption\n"
100 " --no-nla: disable network level authentication\n"
101 " --ntlm: force NTLM authentication protocol version (1 or 2)\n"
102 " --ignore-certificate: ignore verification of logon certificate\n"
103 " --sec: force protocol security (rdp, tls or nla)\n"
104 " --secure-checksum: use salted checksums with Standard RDP encryption\n"
105 " --version: print version information\n"
107 return FREERDP_ARGS_PARSE_HELP; //TODO: What is the correct return
109 else if (strcmp("-a", argv[index]) == 0)
114 printf("missing color depth\n");
115 return FREERDP_ARGS_PARSE_FAILURE;
117 settings->color_depth = atoi(argv[index]);
119 else if (strcmp("-u", argv[index]) == 0)
124 printf("missing username\n");
125 return FREERDP_ARGS_PARSE_FAILURE;
127 settings->username = xstrdup(argv[index]);
129 else if (strcmp("-p", argv[index]) == 0)
134 printf("missing password\n");
135 return FREERDP_ARGS_PARSE_FAILURE;
137 settings->password = xstrdup(argv[index]);
138 settings->autologon = 1;
141 * Overwrite original password which could be revealed by a simple "ps aux" command.
142 * This approach won't hide the password length, but it is better than nothing.
145 memset(argv[index], '*', strlen(argv[index]));
147 else if (strcmp("-d", argv[index]) == 0)
152 printf("missing domain\n");
153 return FREERDP_ARGS_PARSE_FAILURE;
155 settings->domain = xstrdup(argv[index]);
157 else if (strcmp("-s", argv[index]) == 0)
162 printf("missing shell\n");
163 return FREERDP_ARGS_PARSE_FAILURE;
165 settings->shell = xstrdup(argv[index]);
167 else if (strcmp("-c", argv[index]) == 0)
172 printf("missing directory\n");
173 return FREERDP_ARGS_PARSE_FAILURE;
175 settings->directory = xstrdup(argv[index]);
177 else if (strcmp("-g", argv[index]) == 0)
182 printf("missing dimensions\n");
183 return FREERDP_ARGS_PARSE_FAILURE;
186 if (strncmp("workarea", argv[index], 1) == 0)
188 settings->workarea = true;
192 settings->width = (uint16) strtol(argv[index], &p, 10);
196 settings->height = (uint16) strtol(p + 1, &p, 10);
200 settings->percent_screen = settings->width;
201 if (settings->percent_screen <= 0 || settings->percent_screen > 100)
203 printf("invalid geometry percentage\n");
204 return FREERDP_ARGS_PARSE_FAILURE;
209 if (ui_callback != NULL)
210 ui_callback(settings, "-g", p, ui_user_data);
214 else if (strcmp("-f", argv[index]) == 0)
216 settings->fullscreen = true;
218 else if (strcmp("-D", argv[index]) == 0)
220 settings->decorations = false;
222 else if (strcmp("-T", argv[index]) == 0)
227 printf("missing window title\n");
228 return FREERDP_ARGS_PARSE_FAILURE;
231 settings->window_title = xstrdup(argv[index]);
233 else if (strcmp("-t", argv[index]) == 0)
238 printf("missing port number\n");
239 return FREERDP_ARGS_PARSE_FAILURE;
241 settings->port = atoi(argv[index]);
243 else if (strcmp("-k", argv[index]) == 0)
248 printf("missing keyboard layout id\n");
249 return FREERDP_ARGS_PARSE_FAILURE;
251 sscanf(argv[index], "%X", &(settings->kbd_layout));
253 else if (strcmp("-K", argv[index]) == 0)
255 settings->grab_keyboard = false;
257 else if (strcmp("-n", argv[index]) == 0)
262 printf("missing client hostname\n");
263 return FREERDP_ARGS_PARSE_FAILURE;
265 strncpy(settings->client_hostname, argv[index], sizeof(settings->client_hostname) - 1);
266 settings->client_hostname[sizeof(settings->client_hostname) - 1] = 0;
268 else if (strcmp("-o", argv[index]) == 0)
270 settings->console_audio = true;
272 else if (strcmp("-0", argv[index]) == 0)
274 settings->console_session = true;
276 else if (strcmp("-z", argv[index]) == 0)
278 settings->compression = true;
280 else if (strcmp("--ntlm", argv[index]) == 0)
284 settings->ntlm_version = atoi(argv[index]);
286 if (settings->ntlm_version != 2)
287 settings->ntlm_version = 1;
289 else if (strcmp("--no-glyph-cache", argv[index]) == 0)
291 settings->glyph_cache = false;
293 else if (strcmp("--no-osb", argv[index]) == 0)
295 settings->offscreen_bitmap_cache = false;
297 else if (strcmp("--no-bmp-cache", argv[index]) == 0)
299 settings->bitmap_cache = false;
301 else if (strcmp("--no-auth", argv[index]) == 0)
303 settings->authentication = false;
305 else if (strcmp("--ignore-certificate", argv[index]) == 0)
307 settings->ignore_certificate = true;
309 else if (strcmp("--certificate-name", argv[index]) == 0)
314 printf("missing certificate name\n");
315 return FREERDP_ARGS_PARSE_FAILURE;
318 settings->certificate_name = xstrdup(argv[index]);
320 else if (strcmp("--no-fastpath", argv[index]) == 0)
322 settings->fastpath_input = false;
323 settings->fastpath_output = false;
325 else if (strcmp("--gdi", argv[index]) == 0)
330 printf("missing GDI backend\n");
331 return FREERDP_ARGS_PARSE_FAILURE;
333 if (strncmp("sw", argv[index], 1) == 0) /* software */
335 settings->sw_gdi = true;
337 else if (strncmp("hw", argv[index], 1) == 0) /* hardware */
339 settings->sw_gdi = false;
343 printf("unknown GDI backend\n");
344 return FREERDP_ARGS_PARSE_FAILURE;
347 else if (strcmp("--rfx", argv[index]) == 0)
349 settings->rfx_codec = true;
350 settings->fastpath_output = true;
351 settings->color_depth = 32;
352 settings->frame_acknowledge = false;
353 settings->performance_flags = PERF_FLAG_NONE;
354 settings->large_pointer = true;
356 else if (strcmp("--rfx-mode", argv[index]) == 0)
361 printf("missing RemoteFX mode flag\n");
362 return FREERDP_ARGS_PARSE_FAILURE;
364 if (argv[index][0] == 'v') /* video */
366 settings->rfx_codec_mode = 0x00;
368 else if (argv[index][0] == 'i') /* image */
370 settings->rfx_codec_mode = 0x02;
374 printf("unknown RemoteFX mode flag\n");
375 return FREERDP_ARGS_PARSE_FAILURE;
378 else if (strcmp("--nsc", argv[index]) == 0)
380 settings->ns_codec = true;
382 else if (strcmp("--dump-rfx", argv[index]) == 0)
387 printf("missing file name\n");
388 return FREERDP_ARGS_PARSE_FAILURE;
390 settings->dump_rfx_file = xstrdup(argv[index]);
391 settings->dump_rfx = true;
393 else if (strcmp("--play-rfx", argv[index]) == 0)
398 printf("missing file name\n");
399 return FREERDP_ARGS_PARSE_FAILURE;
401 settings->play_rfx_file = xstrdup(argv[index]);
402 settings->play_rfx = true;
404 else if (strcmp("--fonts", argv[index]) == 0)
406 settings->smooth_fonts = true;
408 else if (strcmp("--disable-wallpaper", argv[index]) == 0)
410 settings->disable_wallpaper = true;
412 else if (strcmp("--disable-full-window-drag", argv[index]) == 0)
414 settings->disable_full_window_drag = true;
416 else if (strcmp("--disable-menu-animations", argv[index]) == 0)
418 settings->disable_menu_animations = true;
420 else if (strcmp("--disable-theming", argv[index]) == 0)
422 settings->disable_theming = true;
424 else if (strcmp("--composition", argv[index]) == 0)
426 settings->desktop_composition = true;
428 else if (strcmp("--no-motion", argv[index]) == 0)
430 settings->mouse_motion = false;
432 else if (strcmp("--app", argv[index]) == 0)
434 settings->remote_app = true;
435 settings->rail_langbar_supported = true;
436 settings->workarea = true;
437 settings->performance_flags = PERF_DISABLE_WALLPAPER | PERF_DISABLE_FULLWINDOWDRAG;
439 else if (strcmp("-x", argv[index]) == 0)
444 printf("missing performance flag\n");
445 return FREERDP_ARGS_PARSE_FAILURE;
447 if (argv[index][0] == 'm') /* modem */
449 settings->performance_flags = PERF_DISABLE_WALLPAPER |
450 PERF_DISABLE_FULLWINDOWDRAG | PERF_DISABLE_MENUANIMATIONS |
451 PERF_DISABLE_THEMING;
453 settings->connection_type = CONNECTION_TYPE_MODEM;
455 else if (argv[index][0] == 'b') /* broadband */
457 settings->performance_flags = PERF_DISABLE_WALLPAPER;
458 settings->connection_type = CONNECTION_TYPE_BROADBAND_HIGH;
460 else if (argv[index][0] == 'l') /* lan */
462 settings->performance_flags = PERF_FLAG_NONE;
463 settings->connection_type = CONNECTION_TYPE_LAN;
467 settings->performance_flags = strtol(argv[index], 0, 16);
470 else if (strcmp("-X", argv[index]) == 0)
476 printf("missing parent window XID\n");
477 return FREERDP_ARGS_PARSE_FAILURE;
480 settings->parent_window_xid = strtoul(argv[index], NULL, 16);
482 if (settings->parent_window_xid == 0)
484 printf("invalid parent window XID\n");
485 return FREERDP_ARGS_PARSE_FAILURE;
488 else if (strcmp("--no-rdp", argv[index]) == 0)
490 settings->rdp_security = false;
492 else if (strcmp("--no-tls", argv[index]) == 0)
494 settings->tls_security = false;
496 else if (strcmp("--no-nla", argv[index]) == 0)
498 settings->nla_security = false;
500 else if (strcmp("--sec", argv[index]) == 0)
505 printf("missing protocol security\n");
506 return FREERDP_ARGS_PARSE_FAILURE;
508 if (strncmp("rdp", argv[index], 1) == 0) /* Standard RDP */
510 settings->rdp_security = true;
511 settings->tls_security = false;
512 settings->nla_security = false;
513 settings->encryption = true;
514 settings->encryption_method = ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS;
515 settings->encryption_level = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
517 else if (strncmp("tls", argv[index], 1) == 0) /* TLS */
519 settings->rdp_security = false;
520 settings->tls_security = true;
521 settings->nla_security = false;
523 else if (strncmp("nla", argv[index], 1) == 0) /* NLA */
525 settings->rdp_security = false;
526 settings->tls_security = false;
527 settings->nla_security = true;
531 printf("unknown protocol security\n");
532 return FREERDP_ARGS_PARSE_FAILURE;
535 else if (strcmp("--plugin", argv[index]) == 0)
541 printf("missing plugin name\n");
542 return FREERDP_ARGS_PARSE_FAILURE;
545 if (index < argc - 1 && strcmp("--data", argv[index + 1]) == 0)
549 while (index < argc && strcmp("--", argv[index]) != 0)
551 if (plugin_data == NULL)
552 plugin_data = (RDP_PLUGIN_DATA*) xmalloc(sizeof(RDP_PLUGIN_DATA) * (i + 2));
554 plugin_data = (RDP_PLUGIN_DATA*) xrealloc(plugin_data, sizeof(RDP_PLUGIN_DATA) * (i + 2));
556 plugin_data[i].size = sizeof(RDP_PLUGIN_DATA);
557 plugin_data[i].data[0] = NULL;
558 plugin_data[i].data[1] = NULL;
559 plugin_data[i].data[2] = NULL;
560 plugin_data[i].data[3] = NULL;
561 plugin_data[i + 1].size = 0;
563 for (j = 0, p = argv[index]; j < 4 && p != NULL; j++)
567 plugin_data[i].data[j] = p + 1;
568 p = strchr(p + 1, '\'');
573 plugin_data[i].data[j] = p;
584 if (plugin_callback != NULL)
586 if (!plugin_callback(settings, argv[t], plugin_data, plugin_user_data))
587 return FREERDP_ARGS_PARSE_FAILURE;
590 else if (strcmp("--ext", argv[index]) == 0)
595 printf("missing extension name\n");
596 return FREERDP_ARGS_PARSE_FAILURE;
598 if (num_extensions >= sizeof(settings->extensions) / sizeof(struct rdp_ext_set))
600 printf("maximum extensions reached\n");
601 return FREERDP_ARGS_PARSE_FAILURE;
603 snprintf(settings->extensions[num_extensions].name,
604 sizeof(settings->extensions[num_extensions].name),
606 settings->extensions[num_extensions].data = NULL;
607 if (index < argc - 1 && strcmp("--data", argv[index + 1]) == 0)
610 settings->extensions[num_extensions].data = argv[index];
612 while (index < argc && strcmp("--", argv[index]) != 0)
620 else if (strcmp("--secure-checksum", argv[index]) == 0)
622 settings->secure_checksum = true;
624 else if (strcmp("--version", argv[index]) == 0)
626 printf("This is FreeRDP version %s\n", FREERDP_VERSION_FULL);
627 return FREERDP_ARGS_PARSE_VERSION;
629 else if (argv[index][0] != '-')
631 if (argv[index][0] == '[' && (p = strchr(argv[index], ']'))
632 && (p[1] == 0 || (p[1] == ':' && !strchr(p + 2, ':'))))
634 /* Either "[...]" or "[...]:..." with at most one : after the brackets */
635 settings->hostname = xstrdup(argv[index] + 1);
636 if ((p = strchr((char*)settings->hostname, ']')))
640 settings->port = atoi(p + 2);
645 /* Port number is cut off and used if exactly one : in the string */
646 settings->hostname = xstrdup(argv[index]);
647 if ((p = strchr((char*)settings->hostname, ':')) && !strchr(p + 1, ':'))
650 settings->port = atoi(p + 1);
653 /* server is the last argument for the current session. arguments
654 followed will be parsed for the next session. */
657 if (settings->smooth_fonts)
658 settings->performance_flags |= PERF_ENABLE_FONT_SMOOTHING;
660 if (settings->desktop_composition)
661 settings->performance_flags |= PERF_ENABLE_DESKTOP_COMPOSITION;
663 if (settings->disable_wallpaper)
664 settings->performance_flags |= PERF_DISABLE_WALLPAPER;
666 if (settings->disable_full_window_drag)
667 settings->performance_flags |= PERF_DISABLE_FULLWINDOWDRAG;
669 if (settings->disable_menu_animations)
670 settings->performance_flags |= PERF_DISABLE_MENUANIMATIONS;
672 if (settings->disable_theming)
673 settings->performance_flags |= PERF_DISABLE_THEMING;
679 if (ui_callback != NULL)
681 t = ui_callback(settings, argv[index], (index + 1 < argc && argv[index + 1][0] != '-' ?
682 argv[index + 1] : NULL), ui_user_data);
685 printf("invalid option: %s\n", argv[index]);
686 return FREERDP_ARGS_PARSE_FAILURE;
693 printf("missing server name\n");
694 return FREERDP_ARGS_PARSE_FAILURE;