1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
|
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
Entrypoint and utility functions
Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
Copyright 2002-2011 Peter Astrand <astrand@cendio.se> for Cendio AB
Copyright 2010-2018 Henrik Andersson <hean01@cendio.se> for Cendio AB
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h> /* va_list va_start va_end */
#include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
#include <fcntl.h> /* open */
#include <pwd.h> /* getpwuid */
#include <termios.h> /* tcgetattr tcsetattr */
#include <sys/stat.h> /* stat */
#include <sys/time.h> /* gettimeofday */
#include <sys/times.h> /* times */
#include <ctype.h> /* toupper */
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include "rdesktop.h"
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
#ifdef EGD_SOCKET
#include <sys/types.h>
#include <sys/socket.h> /* socket connect */
#include <sys/un.h> /* sockaddr_un */
#endif
#include "ssl.h"
/* Reconnect timeout based on approximated cookie life-time */
#define RECONNECT_TIMEOUT (3600+600)
#define RDESKTOP_LICENSE_STORE "/.local/share/rdesktop/licenses"
uint8 g_static_rdesktop_salt_16[16] = {
0xb8, 0x82, 0x29, 0x31, 0xc5, 0x39, 0xd9, 0x44,
0x54, 0x15, 0x5e, 0x14, 0x71, 0x38, 0xd5, 0x4d
};
char g_title[64] = "";
char *g_username;
char g_password[64] = "";
char g_hostname[16] = "";
char g_keymapname[PATH_MAX] = "";
unsigned int g_keylayout = 0x409; /* Defaults to US keyboard layout */
int g_keyboard_type = 0x4; /* Defaults to US keyboard layout */
int g_keyboard_subtype = 0x0; /* Defaults to US keyboard layout */
int g_keyboard_functionkeys = 0xc; /* Defaults to US keyboard layout */
int g_dpi = 0; /* device DPI: default not set */
/* Following variables holds the requested width and height for a
rdesktop window, this is sent upon connect and tells the server
what size of session we want to have. Set to decent defaults. */
uint32 g_requested_session_width = 1024;
uint32 g_requested_session_height = 768;
window_size_type_t g_window_size_type = Fixed;
int g_xpos = 0;
int g_ypos = 0;
int g_pos = 0; /* 0 position unspecified,
1 specified,
2 xpos neg,
4 ypos neg */
extern int g_tcp_port_rdp;
int g_server_depth = -1;
int g_win_button_size = 0; /* If zero, disable single app mode */
RD_BOOL g_network_error = False;
RD_BOOL g_sendmotion = True;
RD_BOOL g_bitmap_cache = True;
RD_BOOL g_bitmap_cache_persist_enable = False;
RD_BOOL g_bitmap_cache_precache = True;
RD_BOOL g_use_ctrl = True;
RD_BOOL g_encryption = True;
RD_BOOL g_encryption_initial = True;
RD_BOOL g_packet_encryption = True;
RD_BOOL g_desktop_save = True; /* desktop save order */
RD_BOOL g_polygon_ellipse_orders = True; /* polygon / ellipse orders */
RD_BOOL g_fullscreen = False;
RD_BOOL g_grab_keyboard = True;
RD_BOOL g_local_cursor = False;
RD_BOOL g_hide_decorations = False;
RDP_VERSION g_rdp_version = RDP_V5; /* Default to version 5 */
RD_BOOL g_rdpclip = True;
RD_BOOL g_console_session = False;
RD_BOOL g_numlock_sync = False;
RD_BOOL g_lspci_enabled = False;
RD_BOOL g_owncolmap = False;
RD_BOOL g_ownbackstore = True; /* We can't rely on external BackingStore */
RD_BOOL g_seamless_rdp = False;
RD_BOOL g_use_password_as_pin = False;
char g_seamless_shell[512];
char g_seamless_spawn_cmd[512];
RD_BOOL g_seamless_persistent_mode = True;
RD_BOOL g_user_quit = False;
uint32 g_embed_wnd;
uint32 g_rdp5_performanceflags = (PERF_DISABLE_FULLWINDOWDRAG |
PERF_DISABLE_MENUANIMATIONS |
PERF_ENABLE_FONT_SMOOTHING);
/* Session Directory redirection */
RD_BOOL g_redirect = False;
char *g_redirect_server;
uint32 g_redirect_server_len;
char *g_redirect_domain;
uint32 g_redirect_domain_len;
char *g_redirect_username;
uint32 g_redirect_username_len;
uint8 *g_redirect_lb_info;
uint32 g_redirect_lb_info_len;
uint8 *g_redirect_cookie;
uint32 g_redirect_cookie_len;
uint32 g_redirect_flags = 0;
uint32 g_redirect_session_id = 0;
uint32 g_reconnect_logonid = 0;
char g_reconnect_random[16];
time_t g_reconnect_random_ts;
RD_BOOL g_has_reconnect_random = False;
RD_BOOL g_reconnect_loop = False;
uint8 g_client_random[SEC_RANDOM_SIZE];
RD_BOOL g_pending_resize = False;
RD_BOOL g_pending_resize_defer = True;
struct timeval g_pending_resize_defer_timer = {0};
#ifdef WITH_RDPSND
RD_BOOL g_rdpsnd = False;
#endif
char g_codepage[16] = "";
char *g_sc_csp_name = NULL; /* Smartcard CSP name */
char *g_sc_reader_name = NULL;
char *g_sc_card_name = NULL;
char *g_sc_container_name = NULL;
extern RDPDR_DEVICE g_rdpdr_device[];
extern uint32 g_num_devices;
extern char *g_rdpdr_clientname;
/* Display usage information */
static void
usage(char *program)
{
fprintf(stderr, "rdesktop: A Remote Desktop Protocol client.\n");
fprintf(stderr,
"Version " PACKAGE_VERSION ". Copyright (C) 1999-2016 Matthew Chapman et al.\n");
fprintf(stderr, "See http://www.rdesktop.org/ for more information.\n\n");
fprintf(stderr, "Usage: %s [options] server[:port]\n", program);
fprintf(stderr, " -u: user name\n");
fprintf(stderr, " -d: domain\n");
fprintf(stderr, " -s: shell / seamless application to start remotely\n");
fprintf(stderr, " -c: working directory\n");
fprintf(stderr, " -p: password (- to prompt)\n");
fprintf(stderr, " -n: client hostname\n");
fprintf(stderr, " -k: keyboard layout on server (en-us, de, sv, etc.)\n");
fprintf(stderr, " -g: desktop geometry (WxH[@DPI][+X[+Y]])\n");
#ifdef WITH_SCARD
fprintf(stderr, " -i: enables smartcard authentication, password is used as pin\n");
#endif
fprintf(stderr, " -f: full-screen mode\n");
fprintf(stderr, " -b: force bitmap updates\n");
fprintf(stderr, " -L: local codepage\n");
fprintf(stderr, " -A: path to SeamlessRDP shell, this enables SeamlessRDP mode\n");
fprintf(stderr, " -B: use BackingStore of X-server (if available)\n");
fprintf(stderr, " -e: disable encryption (French TS)\n");
fprintf(stderr, " -E: disable encryption from client to server\n");
fprintf(stderr, " -m: do not send motion events\n");
fprintf(stderr, " -M: use local mouse cursor\n");
fprintf(stderr, " -C: use private colour map\n");
fprintf(stderr, " -D: hide window manager decorations\n");
fprintf(stderr, " -K: keep window manager key bindings\n");
fprintf(stderr, " -S: caption button size (single application mode)\n");
fprintf(stderr, " -T: window title\n");
fprintf(stderr, " -t: disable use of remote ctrl\n");
fprintf(stderr, " -N: enable numlock synchronization\n");
fprintf(stderr, " -X: embed into another window with a given id.\n");
fprintf(stderr, " -a: connection colour depth\n");
fprintf(stderr, " -z: enable rdp compression\n");
fprintf(stderr, " -x: RDP5 experience (m[odem 28.8], b[roadband], l[an] or hex nr.)\n");
fprintf(stderr, " -P: use persistent bitmap caching\n");
fprintf(stderr, " -r: enable specified device redirection (this flag can be repeated)\n");
fprintf(stderr,
" '-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1\n");
fprintf(stderr, " or COM1=/dev/ttyS0,COM2=/dev/ttyS1\n");
fprintf(stderr,
" '-r disk:floppy=/mnt/floppy': enable redirection of /mnt/floppy to 'floppy' share\n");
fprintf(stderr, " or 'floppy=/mnt/floppy,cdrom=/mnt/cdrom'\n");
fprintf(stderr, " '-r clientname=<client name>': Set the client name displayed\n");
fprintf(stderr, " for redirected disks\n");
fprintf(stderr,
" '-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1\n");
fprintf(stderr, " or LPT1=/dev/lp0,LPT2=/dev/lp1\n");
fprintf(stderr, " '-r printer:mydeskjet': enable printer redirection\n");
fprintf(stderr,
" or mydeskjet=\"HP LaserJet IIIP\" to enter server driver as well\n");
#ifdef WITH_RDPSND
fprintf(stderr,
" '-r sound:[local[:driver[:device]]|off|remote]': enable sound redirection\n");
fprintf(stderr, " remote would leave sound on server\n");
fprintf(stderr, " available drivers for 'local':\n");
rdpsnd_show_help();
#endif
fprintf(stderr,
" '-r clipboard:[off|PRIMARYCLIPBOARD|CLIPBOARD]': enable clipboard\n");
fprintf(stderr, " redirection.\n");
fprintf(stderr,
" 'PRIMARYCLIPBOARD' looks at both PRIMARY and CLIPBOARD\n");
fprintf(stderr, " when sending data to server.\n");
fprintf(stderr, " 'CLIPBOARD' looks at only CLIPBOARD.\n");
#ifdef WITH_SCARD
fprintf(stderr, " '-r scard[:\"Scard Name\"=\"Alias Name[;Vendor Name]\"[,...]]\n");
fprintf(stderr, " example: -r scard:\"eToken PRO 00 00\"=\"AKS ifdh 0\"\n");
fprintf(stderr,
" \"eToken PRO 00 00\" -> Device in GNU/Linux and UNIX environment\n");
fprintf(stderr,
" \"AKS ifdh 0\" -> Device shown in Windows environment \n");
fprintf(stderr, " example: -r scard:\"eToken PRO 00 00\"=\"AKS ifdh 0;AKS\"\n");
fprintf(stderr,
" \"eToken PRO 00 00\" -> Device in GNU/Linux and UNIX environment\n");
fprintf(stderr,
" \"AKS ifdh 0\" -> Device shown in Microsoft Windows environment \n");
fprintf(stderr,
" \"AKS\" -> Device vendor name \n");
#endif
fprintf(stderr, " -0: attach to console\n");
fprintf(stderr, " -4: use RDP version 4\n");
fprintf(stderr, " -5: use RDP version 5 (default)\n");
#ifdef WITH_SCARD
fprintf(stderr, " -o: name=value: Adds an additional option to rdesktop.\n");
fprintf(stderr,
" sc-csp-name Specifies the Crypto Service Provider name which\n");
fprintf(stderr,
" is used to authenticate the user by smartcard\n");
fprintf(stderr,
" sc-container-name Specifies the container name, this is usually the username\n");
fprintf(stderr, " sc-reader-name Smartcard reader name to use\n");
fprintf(stderr,
" sc-card-name Specifies the card name of the smartcard to use\n");
#endif
fprintf(stderr, " -v: enable verbose logging\n");
fprintf(stderr, "\n");
}
static int
handle_disconnect_reason(RD_BOOL deactivated, uint16 reason)
{
char *text;
int retval;
switch (reason)
{
case ERRINFO_NO_INFO:
text = "No information available";
if (deactivated)
retval = EX_OK;
else
retval = EXRD_UNKNOWN;
break;
case ERRINFO_RPC_INITIATED_DISCONNECT:
text = "Administrator initiated disconnect";
retval = EXRD_DISCONNECT_BY_ADMIN;
break;
case ERRINFO_RPC_INITIATED_LOGOFF:
text = "Administrator initiated logout";
retval = EXRD_LOGOFF_BY_ADMIN;
break;
case ERRINFO_IDLE_TIMEOUT:
text = "Server idle session time limit reached";
retval = EXRD_IDLE_TIMEOUT;
break;
case ERRINFO_LOGON_TIMEOUT:
text = "Server active session time limit reached";
retval = EXRD_LOGON_TIMEOUT;
break;
case ERRINFO_DISCONNECTED_BY_OTHERCONNECTION:
text = "The session was replaced";
retval = EXRD_REPLACED;
break;
case ERRINFO_OUT_OF_MEMORY:
text = "The server is out of memory";
retval = EXRD_OUT_OF_MEM;
break;
case ERRINFO_SERVER_DENIED_CONNECTION:
text = "The server denied the connection";
retval = EXRD_DENIED;
break;
case ERRINFO_SERVER_DENIED_CONNECTION_FIPS:
text = "The server denied the connection for security reasons";
retval = EXRD_DENIED_FIPS;
break;
case ERRINFO_SERVER_INSUFFICIENT_PRIVILEGES:
text = "The user cannot connect to the server due to insufficient access privileges.";
retval = EXRD_INSUFFICIENT_PRIVILEGES;
break;
case ERRINFO_SERVER_FRESH_CREDENTIALS_REQUIRED:
text = "The server does not accept saved user credentials and requires that the user enter their credentials for each connection.";
retval = EXRD_FRESH_CREDENTIALS_REQUIRED;
break;
case ERRINFO_RPC_INITIATED_DISCONNECT_BYUSER:
text = "Disconnect initiated by user";
retval = EXRD_DISCONNECT_BY_USER;
break;
case ERRINFO_LOGOFF_BYUSER:
text = "Logout initiated by user";
retval = EXRD_LOGOFF_BY_USER;
break;
case ERRINFO_LICENSE_INTERNAL:
text = "Internal licensing error";
retval = EXRD_LIC_INTERNAL;
break;
case ERRINFO_LICENSE_NO_LICENSE_SERVER:
text = "No license server available";
retval = EXRD_LIC_NOSERVER;
break;
case ERRINFO_LICENSE_NO_LICENSE:
text = "No valid license available";
retval = EXRD_LIC_NOLICENSE;
break;
case ERRINFO_LICENSE_BAD_CLIENT_MSG:
text = "Invalid licensing message from client";
retval = EXRD_LIC_MSG;
break;
case ERRINFO_LICENSE_HWID_DOESNT_MATCH_LICENSE:
text = "The client license has been modified and does no longer match the hardware ID";
retval = EXRD_LIC_HWID;
break;
case ERRINFO_LICENSE_BAD_CLIENT_LICENSE:
text = "The client license is in an invalid format";
retval = EXRD_LIC_CLIENT;
break;
case ERRINFO_LICENSE_CANT_FINISH_PROTOCOL:
text = "Network error during licensing protocol";
retval = EXRD_LIC_NET;
break;
case ERRINFO_LICENSE_CLIENT_ENDED_PROTOCOL:
text = "Licensing protocol was not completed";
retval = EXRD_LIC_PROTO;
break;
case ERRINFO_LICENSE_BAD_CLIENT_ENCRYPTION:
text = "Incorrect client license encryption";
retval = EXRD_LIC_ENC;
break;
case ERRINFO_LICENSE_CANT_UPGRADE_LICENSE:
text = "Can't upgrade or renew license";
retval = EXRD_LIC_UPGRADE;
break;
case ERRINFO_LICENSE_NO_REMOTE_CONNECTIONS:
text = "The server is not licensed to accept remote connections";
retval = EXRD_LIC_NOREMOTE;
break;
case ERRINFO_CB_DESTINATION_NOT_FOUND:
text = "The target endpoint chosen by the broker could not be found";
retval = EXRD_CB_DEST_NOT_FOUND;
break;
case ERRINFO_CB_LOADING_DESTINATION:
text = "The target endpoint is disconnecting from the broker";
retval = EXRD_CB_DEST_LOADING;
break;
case ERRINFO_CB_REDIRECTING_TO_DESTINATION:
text = "Error occurred while being redirected by broker";
retval = EXRD_CB_REDIR_DEST;
break;
case ERRINFO_CB_SESSION_ONLINE_VM_WAKE:
text = "Error while the endpoint VM was being awakened by the broker";
retval = EXRD_CB_VM_WAKE;
break;
case ERRINFO_CB_SESSION_ONLINE_VM_BOOT:
text = "Error while the endpoint VM was being started by the broker";
retval = EXRD_CB_VM_BOOT;
break;
case ERRINFO_CB_SESSION_ONLINE_VM_NO_DNS:
text = "The IP address of the endpoint VM could not be determined by the broker";
retval = EXRD_CB_VM_NODNS;
break;
case ERRINFO_CB_DESTINATION_POOL_NOT_FREE:
text = "No available endpoints in the connection broker pool";
retval = EXRD_CB_DEST_POOL_NOT_FREE;
break;
case ERRINFO_CB_CONNECTION_CANCELLED:
text = "Connection processing cancelled by the broker";
retval = EXRD_CB_CONNECTION_CANCELLED;
break;
case ERRINFO_CB_CONNECTION_ERROR_INVALID_SETTINGS:
text = "The connection settings could not be validated by the broker";
retval = EXRD_CB_INVALID_SETTINGS;
break;
case ERRINFO_CB_SESSION_ONLINE_VM_BOOT_TIMEOUT:
text = "Timeout while the endpoint VM was being started by the broker";
retval = EXRD_CB_VM_BOOT_TIMEOUT;
break;
case ERRINFO_CB_SESSION_ONLINE_VM_SESSMON_FAILED:
text = "Session monitoring error while the endpoint VM was being started by the broker";
retval = EXRD_CB_VM_BOOT_SESSMON_FAILED;
break;
case ERRINFO_REMOTEAPPSNOTENABLED:
text = "The server can only host Remote Applications";
retval = EXRD_RDP_REMOTEAPPSNOTENABLED;
break;
case ERRINFO_UPDATESESSIONKEYFAILED:
text = "Update of session keys failed";
retval = EXRD_RDP_UPDATESESSIONKEYFAILED;
break;
case ERRINFO_DECRYPTFAILED:
text = "Decryption or session key creation failed";
retval = EXRD_RDP_DECRYPTFAILED;
break;
case ERRINFO_ENCRYPTFAILED:
text = "Encryption failed";
retval = EXRD_RDP_ENCRYPTFAILED;
break;
default:
text = "Unknown reason";
retval = EXRD_UNKNOWN;
}
if (reason > 0x1000 && reason < 0x7fff && retval == EXRD_UNKNOWN) {
fprintf(stderr, "Internal protocol error: %x", reason);
} else if (reason != ERRINFO_NO_INFO) {
fprintf(stderr, "disconnect: %s.\n", text);
}
return retval;
}
static void
rdesktop_reset_state(void)
{
g_pending_resize_defer = True;
rdp_reset_state();
#ifdef WITH_SCARD
scard_reset_state();
#endif
#ifdef WITH_RDPSND
rdpsnd_reset_state();
#endif
}
static RD_BOOL
read_password(char *password, int size)
{
struct termios tios;
RD_BOOL ret = False;
int istty = 0;
const char *prompt;
char *p;
if (g_use_password_as_pin)
{
prompt = "Smart card PIN: ";
}
else
{
prompt = "Password: ";
}
if (tcgetattr(STDIN_FILENO, &tios) == 0)
{
fputs(prompt, stderr);
tios.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tios);
istty = 1;
}
if (fgets(password, size, stdin) != NULL)
{
ret = True;
/* strip final newline */
p = strchr(password, '\n');
if (p != NULL)
*p = 0;
}
if (istty)
{
tios.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tios);
fprintf(stderr, "\n");
}
return ret;
}
static void
parse_server_and_port(char *server)
{
char *p;
#ifdef IPv6
int addr_colons;
#endif
#ifdef IPv6
p = server;
addr_colons = 0;
while (*p)
if (*p++ == ':')
addr_colons++;
if (addr_colons >= 2)
{
/* numeric IPv6 style address format - [1:2:3::4]:port */
p = strchr(server, ']');
if (*server == '[' && p != NULL)
{
if (*(p + 1) == ':' && *(p + 2) != '\0')
g_tcp_port_rdp = strtol(p + 2, NULL, 10);
/* remove the port number and brackets from the address */
*p = '\0';
strncpy(server, server + 1, strlen(server));
}
}
else
{
/* DNS name or IPv4 style address format - server.example.com:port or 1.2.3.4:port */
p = strchr(server, ':');
if (p != NULL)
{
g_tcp_port_rdp = strtol(p + 1, NULL, 10);
*p = 0;
}
}
#else /* no IPv6 support */
p = strchr(server, ':');
if (p != NULL)
{
g_tcp_port_rdp = strtol(p + 1, NULL, 10);
*p = 0;
}
#endif /* IPv6 */
}
// [WxH|P%|W%xH%][@DPI][+X[+Y]]|workarea
int parse_geometry_string(const char *optarg)
{
sint32 value;
const char *ps;
char *pe;
/* special keywords */
if (strcmp(optarg, "workarea") == 0)
{
g_window_size_type = Workarea;
return 0;
}
/* parse first integer */
ps = optarg;
value = strtol(ps, &pe, 10);
if (ps == pe || value <= 0)
{
logger(Core, Error, "invalid geometry, expected positive integer for width");
return -1;
}
g_requested_session_width = value;
ps = pe;
/* expect % or x */
if (*ps != '%' && *ps != 'x')
{
logger(Core, Error, "invalid geometry, expected '%%' or 'x' after width");
return -1;
}
if (*ps == '%')
{
g_window_size_type = PercentageOfScreen;
ps++;
pe++;
}
if (*ps == 'x')
{
ps++;
value = strtol(ps, &pe, 10);
if (ps == pe || value <= 0)
{
logger(Core, Error, "invalid geometry, expected positive integer for height");
return -1;
}
g_requested_session_height = value;
ps = pe;
if (*ps == '%' && g_window_size_type == Fixed)
{
logger(Core, Error, "invalid geometry, unexpected '%%' after height");
return -1;
}
if (g_window_size_type == PercentageOfScreen)
{
if (*ps != '%')
{
logger(Core, Error, "invalid geometry, expected '%%' after height");
return -1;
}
ps++;
pe++;
}
}
else
{
if (g_window_size_type == PercentageOfScreen)
{
/* percentage of screen used for both width and height */
g_requested_session_height = g_requested_session_width;
}
else
{
logger(Core, Error, "invalid geometry, missing height (WxH)");
return -1;
}
}
/* parse optional dpi */
if (*ps == '@')
{
ps++;
pe++;
value = strtol(ps, &pe, 10);
if (ps == pe || value <= 0)
{
logger(Core, Error, "invalid geometry, expected positive integer for DPI");
return -1;
}
g_dpi = value;
ps = pe;
}
/* parse optional window position */
if (*ps == '+' || *ps == '-')
{
/* parse x position */
value = strtol(ps, &pe, 10);
if (ps == pe)
{
logger(Core, Error, "invalid geometry, expected an integer for X position");
return -1;
}
g_pos |= (value < 0) ? 2 : 1;
g_xpos = value;
ps = pe;
}
if (*ps == '+' || *ps == '-')
{
/* parse y position */
value = strtol(ps, &pe, 10);
if (ps == pe)
{
logger(Core, Error, "invalid geometry, expected an integer for Y position");
return -1;
}
g_pos |= (value < 0) ? 4 : 1;
g_ypos = value;
ps = pe;
}
if (*pe != '\0')
{
logger(Core, Error, "invalid geometry, unexpected characters at end of string");
return -1;
}
return 0;
}
static void
setup_user_requested_session_size()
{
switch(g_window_size_type)
{
case Fullscreen:
ui_get_screen_size(&g_requested_session_width, &g_requested_session_height);
break;
case Workarea:
ui_get_workarea_size(&g_requested_session_width, &g_requested_session_height);
break;
case Fixed:
break;
case PercentageOfScreen:
ui_get_screen_size_from_percentage(g_requested_session_width,
g_requested_session_height,
&g_requested_session_width,
&g_requested_session_height);
break;
}
}
/* Client program */
int
main(int argc, char *argv[])
{
char server[256];
char fullhostname[64];
char domain[256];
char shell[256];
char directory[256];
RD_BOOL deactivated;
struct passwd *pw;
uint32 flags, ext_disc_reason = 0;
char *p;
int c;
char *locale = NULL;
int username_option = 0;
RD_BOOL geometry_option = False;
#ifdef WITH_RDPSND
char *rdpsnd_optarg = NULL;
#endif
/* setup debug logging from environment */
logger_set_subjects(getenv("RDESKTOP_DEBUG"));
#ifdef HAVE_LOCALE_H
/* Set locale according to environment */
locale = setlocale(LC_ALL, "");
if (locale)
{
locale = xstrdup(locale);
}
#endif
/* Ignore SIGPIPE, since we are using popen() */
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGPIPE, &act, NULL);
/* setup default flags for TS_INFO_PACKET */
flags = RDP_INFO_MOUSE | RDP_INFO_DISABLECTRLALTDEL
| RDP_INFO_UNICODE | RDP_INFO_MAXIMIZESHELL | RDP_INFO_ENABLEWINDOWSKEY;
g_seamless_spawn_cmd[0] = domain[0] = g_password[0] = shell[0] = directory[0] = 0;
g_embed_wnd = 0;
g_num_devices = 0;
while ((c = getopt(argc, argv,
"A:u:L:d:s:c:p:n:k:g:o:fbBeEitmMzCDKS:T:NX:a:x:Pr:045vh?")) != -1)
{
switch (c)
{
case 'A':
g_seamless_rdp = True;
STRNCPY(g_seamless_shell, optarg, sizeof(g_seamless_shell));
break;
case 'u':
g_username = (char *) xmalloc(strlen(optarg) + 1);
STRNCPY(g_username, optarg, strlen(optarg) + 1);
username_option = 1;
break;
case 'L':
STRNCPY(g_codepage, optarg, sizeof(g_codepage));
break;
case 'd':
STRNCPY(domain, optarg, sizeof(domain));
break;
case 's':
STRNCPY(shell, optarg, sizeof(shell));
g_seamless_persistent_mode = False;
break;
case 'c':
STRNCPY(directory, optarg, sizeof(directory));
break;
case 'p':
if (!((optarg[0] == '-') && (optarg[1] == 0)))
{
STRNCPY(g_password, optarg, sizeof(g_password));
flags |= RDP_INFO_AUTOLOGON;
/* try to overwrite argument so it won't appear in `ps` */
p = optarg;
while (*p)
*(p++) = 'X';
}
break;
#ifdef WITH_SCARD
case 'i':
flags |= RDP_INFO_PASSWORD_IS_SC_PIN;
g_use_password_as_pin = True;
break;
#endif
case 't':
g_use_ctrl = False;
break;
case 'n':
STRNCPY(g_hostname, optarg, sizeof(g_hostname));
break;
case 'k':
STRNCPY(g_keymapname, optarg, sizeof(g_keymapname));
break;
case 'g':
geometry_option = True;
g_fullscreen = False;
if (parse_geometry_string(optarg) != 0)
{
return EX_USAGE;
}
break;
case 'f':
g_window_size_type = Fullscreen;
g_fullscreen = True;
break;
case 'b':
g_bitmap_cache = False;
break;
case 'B':
g_ownbackstore = False;
break;
case 'e':
g_encryption_initial = g_encryption = False;
break;
case 'E':
g_packet_encryption = False;
break;
case 'm':
g_sendmotion = False;
break;
case 'M':
g_local_cursor = True;
break;
case 'C':
g_owncolmap = True;
break;
case 'D':
g_hide_decorations = True;
break;
case 'K':
g_grab_keyboard = False;
break;
case 'S':
if (!strcmp(optarg, "standard"))
{
g_win_button_size = 18;
break;
}
g_win_button_size = strtol(optarg, &p, 10);
if (*p)
{
logger(Core, Error, "invalid button size");
return EX_USAGE;
}
break;
case 'T':
STRNCPY(g_title, optarg, sizeof(g_title));
break;
case 'N':
g_numlock_sync = True;
break;
case 'X':
g_embed_wnd = strtol(optarg, NULL, 0);
break;
case 'a':
g_server_depth = strtol(optarg, NULL, 10);
if (g_server_depth != 8 &&
g_server_depth != 16 &&
g_server_depth != 15 && g_server_depth != 24
&& g_server_depth != 32)
{
logger(Core, Error,
"Invalid server colour depth specified");
return EX_USAGE;
}
break;
case 'z':
logger(Core, Debug, "rdp compression enabled");
flags |= (RDP_INFO_COMPRESSION | RDP_INFO_COMPRESSION2);
break;
case 'x':
if (str_startswith(optarg, "m")) /* modem */
{
g_rdp5_performanceflags = (PERF_DISABLE_CURSOR_SHADOW |
PERF_DISABLE_WALLPAPER |
PERF_DISABLE_FULLWINDOWDRAG |
PERF_DISABLE_MENUANIMATIONS |
PERF_DISABLE_THEMING);
}
else if (str_startswith(optarg, "b")) /* broadband */
{
g_rdp5_performanceflags = (PERF_DISABLE_WALLPAPER |
PERF_ENABLE_FONT_SMOOTHING);
}
else if (str_startswith(optarg, "l")) /* LAN */
{
g_rdp5_performanceflags = PERF_ENABLE_FONT_SMOOTHING;
}
else
{
g_rdp5_performanceflags = strtol(optarg, NULL, 16);
}
break;
case 'P':
g_bitmap_cache_persist_enable = True;
break;
case 'r':
if (str_startswith(optarg, "sound"))
{
optarg += 5;
if (*optarg == ':')
{
optarg++;
while ((p = next_arg(optarg, ',')))
{
if (str_startswith(optarg, "remote"))
flags |= RDP_INFO_REMOTE_CONSOLE_AUDIO;
if (str_startswith(optarg, "local"))
#ifdef WITH_RDPSND
{
rdpsnd_optarg =
next_arg(optarg, ':');
g_rdpsnd = True;
}
#else
logger(Core, Warning,
"Not compiled with sound support");
#endif
if (str_startswith(optarg, "off"))
#ifdef WITH_RDPSND
g_rdpsnd = False;
#else
logger(Core, Warning,
"Not compiled with sound support");
#endif
optarg = p;
}
}
else
{
#ifdef WITH_RDPSND
g_rdpsnd = True;
#else
logger(Core, Warning,
"Not compiled with sound support");
#endif
}
}
else if (str_startswith(optarg, "disk"))
{
/* -r disk:h:=/mnt/floppy */
disk_enum_devices(&g_num_devices, optarg + 4);
}
else if (str_startswith(optarg, "comport"))
{
serial_enum_devices(&g_num_devices, optarg + 7);
}
else if (str_startswith(optarg, "lspci"))
{
g_lspci_enabled = True;
}
else if (str_startswith(optarg, "lptport"))
{
parallel_enum_devices(&g_num_devices, optarg + 7);
}
else if (str_startswith(optarg, "printer"))
{
printer_enum_devices(&g_num_devices, optarg + 7);
}
else if (str_startswith(optarg, "clientname"))
{
g_rdpdr_clientname = xmalloc(strlen(optarg + 11) + 1);
strcpy(g_rdpdr_clientname, optarg + 11);
}
else if (str_startswith(optarg, "clipboard"))
{
optarg += 9;
if (*optarg == ':')
{
optarg++;
if (str_startswith(optarg, "off"))
g_rdpclip = False;
else
cliprdr_set_mode(optarg);
}
else
g_rdpclip = True;
}
else if (strncmp("scard", optarg, 5) == 0)
{
#ifdef WITH_SCARD
scard_enum_devices(&g_num_devices, optarg + 5);
#else
logger(Core, Warning,
"Not compiled with smartcard support\n");
#endif
}
else
{
logger(Core, Warning,
"Unknown -r argument '%s'. Possible arguments are: comport, disk, lptport, printer, sound, clipboard, scard",
optarg);
}
break;
case '0':
g_console_session = True;
break;
case '4':
g_rdp_version = RDP_V4;
break;
case '5':
g_rdp_version = RDP_V5;
break;
#if WITH_SCARD
case 'o':
{
char *p = strchr(optarg, '=');
if (p == NULL)
{
logger(Core, Warning,
"Skipping specified option '%s', lacks name=value format",
optarg);
continue;
}
if (strncmp(optarg, "sc-csp-name", strlen("sc-scp-name")) ==
0)
g_sc_csp_name = strdup(p + 1);
else if (strncmp
(optarg, "sc-reader-name",
strlen("sc-reader-name")) == 0)
g_sc_reader_name = strdup(p + 1);
else if (strncmp
(optarg, "sc-card-name",
strlen("sc-card-name")) == 0)
g_sc_card_name = strdup(p + 1);
else if (strncmp
(optarg, "sc-container-name",
strlen("sc-container-name")) == 0)
g_sc_container_name = strdup(p + 1);
}
break;
#endif
case 'v':
logger_set_verbose(1);
break;
case 'h':
case '?':
default:
usage(argv[0]);
return EX_USAGE;
}
}
if (argc - optind != 1)
{
usage(argv[0]);
return EX_USAGE;
}
if (g_local_cursor)
{
/* there is no point wasting bandwidth on cursor shadows
* that we're just going to throw out anyway */
g_rdp5_performanceflags |= PERF_DISABLE_CURSOR_SHADOW;
}
STRNCPY(server, argv[optind], sizeof(server));
parse_server_and_port(server);
if (g_seamless_rdp)
{
if (shell[0])
STRNCPY(g_seamless_spawn_cmd, shell, sizeof(g_seamless_spawn_cmd));
STRNCPY(shell, g_seamless_shell, sizeof(shell));
if (g_win_button_size)
{
logger(Core, Error, "You cannot use -S and -A at the same time");
return EX_USAGE;
}
g_rdp5_performanceflags &= ~PERF_DISABLE_FULLWINDOWDRAG;
if (geometry_option)
{
logger(Core, Error, "You cannot use -g and -A at the same time");
return EX_USAGE;
}
if (g_fullscreen)
{
logger(Core, Error, "You cannot use -f and -A at the same time");
return EX_USAGE;
}
if (g_hide_decorations)
{
logger(Core, Error, "You cannot use -D and -A at the same time");
return EX_USAGE;
}
if (g_embed_wnd)
{
logger(Core, Error, "You cannot use -X and -A at the same time");
return EX_USAGE;
}
if (g_rdp_version < RDP_V5)
{
logger(Core, Error, "You cannot use -4 and -A at the same time");
return EX_USAGE;
}
g_window_size_type = Fullscreen;
g_grab_keyboard = False;
}
if (!username_option)
{
pw = getpwuid(getuid());
if ((pw == NULL) || (pw->pw_name == NULL))
{
logger(Core, Error,
"could not determine username, use -u <username> to set one");
return EX_OSERR;
}
/* +1 for trailing \0 */
int pwlen = strlen(pw->pw_name) + 1;
g_username = (char *) xmalloc(pwlen);
STRNCPY(g_username, pw->pw_name, pwlen);
}
if (g_codepage[0] == 0)
{
if (setlocale(LC_CTYPE, ""))
{
STRNCPY(g_codepage, nl_langinfo(CODESET), sizeof(g_codepage));
}
else
{
STRNCPY(g_codepage, DEFAULT_CODEPAGE, sizeof(g_codepage));
}
}
if (g_hostname[0] == 0)
{
if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
{
logger(Core, Error, "could not determine local hostname, use -n\n");
return EX_OSERR;
}
p = strchr(fullhostname, '.');
if (p != NULL)
*p = 0;
STRNCPY(g_hostname, fullhostname, sizeof(g_hostname));
}
if (g_keymapname[0] == 0)
{
if (locale && xkeymap_from_locale(locale))
{
logger(Core, Notice, "Autoselecting keyboard map '%s' from locale",
g_keymapname);
}
else
{
STRNCPY(g_keymapname, "en-us", sizeof(g_keymapname));
}
}
if (locale)
xfree(locale);
/* If no password provided at this point, prompt for password / pin */
if (!g_password[0])
{
if (read_password(g_password, sizeof(g_password)))
{
flags |= RDP_INFO_AUTOLOGON;
}
else
{
logger(Core, Error, "Failed to read password or pin from stdin");
return EX_OSERR;
}
}
if (g_title[0] == 0)
{
strcpy(g_title, "rdesktop - ");
strncat(g_title, server, sizeof(g_title) - sizeof("rdesktop - "));
}
/* Only startup ctrl functionality is seamless are used for now. */
if (g_use_ctrl && g_seamless_rdp)
{
if (ctrl_init(server, domain, g_username) < 0)
{
logger(Core, Error, "Failed to initialize ctrl mode");
exit(1);
}
if (ctrl_is_slave())
{
logger(Core, Notice,
"rdesktop in slave mode sending command to master process");
if (g_seamless_spawn_cmd[0])
return ctrl_send_command("seamless.spawn", g_seamless_spawn_cmd);
logger(Core, Notice, "No command specified to be spawned in seamless mode");
return EX_USAGE;
}
}
if (!ui_init())
return EX_OSERR;
#ifdef WITH_RDPSND
if (!rdpsnd_init(rdpsnd_optarg))
logger(Core, Warning, "Initializing sound-support failed");
#endif
if (g_lspci_enabled)
lspci_init();
rdpdr_init();
dvc_init();
rdpedisp_init();
setup_user_requested_session_size();
g_reconnect_loop = False;
while (1)
{
rdesktop_reset_state();
if (g_redirect)
{
STRNCPY(domain, g_redirect_domain, sizeof(domain));
xfree(g_username);
g_username = (char *) xmalloc(strlen(g_redirect_username) + 1);
STRNCPY(g_username, g_redirect_username, strlen(g_redirect_username) + 1);
STRNCPY(server, g_redirect_server, sizeof(server));
flags |= RDP_INFO_AUTOLOGON;
logger(Core, Notice, "Redirected to %s@%s session %d.",
g_redirect_username, g_redirect_server, g_redirect_session_id);
/* A redirect on SSL from a 2003 WTS will result in a 'connection reset by peer'
and therefor we just clear this error before we connect to redirected server.
*/
g_network_error = False;
}
utils_apply_session_size_limitations(&g_requested_session_width, &g_requested_session_height);
if (!rdp_connect
(server, flags, domain, g_password, shell, directory, g_reconnect_loop))
{
g_network_error = False;
if (g_reconnect_loop == False)
return EX_PROTOCOL;
/* check if auto reconnect cookie has timed out */
if (time(NULL) - g_reconnect_random_ts > RECONNECT_TIMEOUT)
{
logger(Core, Notice,
"Tried to reconnect for %d minutes, giving up.",
RECONNECT_TIMEOUT / 60);
return EX_PROTOCOL;
}
sleep(4);
continue;
}
if (g_redirect)
{
rdp_disconnect();
continue;
}
/* By setting encryption to False here, we have an encrypted login
packet but unencrypted transfer of other packets */
if (!g_packet_encryption)
g_encryption_initial = g_encryption = False;
logger(Core, Verbose, "Connection successful");
rd_create_ui();
tcp_run_ui(True);
deactivated = False;
g_reconnect_loop = False;
ext_disc_reason = ERRINFO_UNSET;
rdp_main_loop(&deactivated, &ext_disc_reason);
tcp_run_ui(False);
logger(Core, Verbose, "Disconnecting...");
rdp_disconnect();
/* Version <= Windows 2008 server have a different behaviour for
user initiated disconnected. Lets translate this specific
behaviour into the same as for later versions for proper
handling.
*/
if (deactivated == True && ext_disc_reason == ERRINFO_NO_INFO)
{
deactivated = 0;
ext_disc_reason = ERRINFO_LOGOFF_BYUSER;
}
else if (ext_disc_reason == 0)
{
/* We do not know how to handle error info value of 0 */
ext_disc_reason = ERRINFO_UNSET;
}
/* Handler disconnect */
if (g_user_quit || deactivated == True || ext_disc_reason != ERRINFO_UNSET)
{
/* We should exit the rdesktop instance */
break;
}
else
{
/* We should handle a reconnect for any reason */
if (g_redirect)
{
logger(Core, Verbose, "Redirect reconnect loop triggered.");
}
else if (g_network_error)
{
if (g_reconnect_random_ts == 0)
{
/* If there is no auto reconnect cookie available
for reconnect, do not enter reconnect loop. Windows
2016 server does not send any for unknown reasons.
*/
logger(Core, Notice,
"Disconnected due to network error, exiting...");
break;
}
/* handle network error and start autoreconnect */
logger(Core, Notice,
"Disconnected due to network error, retrying to reconnect for %d minutes.",
RECONNECT_TIMEOUT / 60);
g_network_error = False;
g_reconnect_loop = True;
}
else if (g_pending_resize)
{
/* Enter a reconnect loop if we have a pending resize request */
logger(Core, Verbose, "Resize reconnect loop triggered, new size %dx%d",
g_requested_session_width, g_requested_session_height);
g_pending_resize = False;
g_reconnect_loop = True;
ui_seamless_end();
ui_destroy_window();
}
else
{
logger(Core, Debug, "Unhandled reconnect reason, exiting...");
break;
}
}
}
ui_seamless_end();
ui_destroy_window();
cache_save_state();
ui_deinit();
if (g_user_quit)
return EXRD_WINDOW_CLOSED;
return handle_disconnect_reason(deactivated, ext_disc_reason);
if (g_redirect_username)
xfree(g_redirect_username);
xfree(g_username);
}
#ifdef EGD_SOCKET
/* Read 32 random bytes from PRNGD or EGD socket (based on OpenSSL RAND_egd) */
static RD_BOOL
generate_random_egd(uint8 * buf)
{
struct sockaddr_un addr;
RD_BOOL ret = False;
int fd;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
return False;
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, EGD_SOCKET, sizeof(EGD_SOCKET));
if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1)
goto err;
/* PRNGD and EGD use a simple communications protocol */
buf[0] = 1; /* Non-blocking (similar to /dev/urandom) */
buf[1] = 32; /* Number of requested random bytes */
if (write(fd, buf, 2) != 2)
goto err;
if ((read(fd, buf, 1) != 1) || (buf[0] == 0)) /* Available? */
goto err;
if (read(fd, buf, 32) != 32)
goto err;
ret = True;
err:
close(fd);
return ret;
}
#endif
/* Generate a 32-byte random for the secure transport code. */
void
generate_random(uint8 * random)
{
struct stat st;
struct tms tmsbuf;
RDSSL_MD5 md5;
uint32 *r;
int fd, n;
/* If we have a kernel random device, try that first */
if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
|| ((fd = open("/dev/random", O_RDONLY)) != -1))
{
n = read(fd, random, 32);
close(fd);
if (n == 32)
return;
}
#ifdef EGD_SOCKET
/* As a second preference use an EGD */
if (generate_random_egd(random))
return;
#endif
/* Otherwise use whatever entropy we can gather - ideas welcome. */
r = (uint32 *) random;
r[0] = (getpid()) | (getppid() << 16);
r[1] = (getuid()) | (getgid() << 16);
r[2] = times(&tmsbuf); /* system uptime (clocks) */
gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
stat("/tmp", &st);
r[5] = st.st_atime;
r[6] = st.st_mtime;
r[7] = st.st_ctime;
/* Hash both halves with MD5 to obscure possible patterns */
rdssl_md5_init(&md5);
rdssl_md5_update(&md5, random, 16);
rdssl_md5_final(&md5, random);
rdssl_md5_update(&md5, random + 16, 16);
rdssl_md5_final(&md5, random + 16);
}
/* malloc; exit if out of memory */
void *
xmalloc(int size)
{
void *mem = malloc(size);
if (mem == NULL)
{
logger(Core, Error, "xmalloc, failed to allocate %d bytes", size);
exit(EX_UNAVAILABLE);
}
return mem;
}
/* Exit on NULL pointer. Use to verify result from XGetImage etc */
void
exit_if_null(void *ptr)
{
if (ptr == NULL)
{
logger(Core, Error, "unexpected null pointer. Out of memory?");
exit(EX_UNAVAILABLE);
}
}
/* strdup */
char *
xstrdup(const char *s)
{
char *mem = strdup(s);
if (mem == NULL)
{
logger(Core, Error, "xstrdup(), strdup() failed: %s", strerror(errno));
exit(EX_UNAVAILABLE);
}
return mem;
}
/* realloc; exit if out of memory */
void *
xrealloc(void *oldmem, size_t size)
{
void *mem;
if (size == 0)
size = 1;
mem = realloc(oldmem, size);
if (mem == NULL)
{
logger(Core, Error, "xrealloc, failed to reallocate %ld bytes", size);
exit(EX_UNAVAILABLE);
}
return mem;
}
/* free */
void
xfree(void *mem)
{
free(mem);
}
/* produce a hex dump */
void
hexdump(unsigned char *p, unsigned int len)
{
unsigned char *line = p;
unsigned offset = 0;
int i, thisline;
while (offset < len)
{
printf("%04x ", offset);
thisline = len - offset;
if (thisline > 16)
thisline = 16;
for (i = 0; i < thisline; i++)
printf("%02x ", line[i]);
for (; i < 16; i++)
printf(" ");
for (i = 0; i < thisline; i++)
printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
printf("\n");
offset += thisline;
line += thisline;
}
}
/*
input: src is the string we look in for needle.
Needle may be escaped by a backslash, in
that case we ignore that particular needle.
return value: returns next src pointer, for
successive executions, like in a while loop
if retval is 0, then there are no more args.
pitfalls:
src is modified. 0x00 chars are inserted to
terminate strings.
return val, points on the next val chr after ins
0x00
example usage:
while( (pos = next_arg( optarg, ',')) ){
printf("%s\n",optarg);
optarg=pos;
}
*/
char *
next_arg(char *src, char needle)
{
char *nextval;
char *p;
char *mvp = 0;
/* EOS */
if (*src == (char) 0x00)
return 0;
p = src;
/* skip escaped needles */
while ((nextval = strchr(p, needle)))
{
mvp = nextval - 1;
/* found backslashed needle */
if (*mvp == '\\' && (mvp > src))
{
/* move string one to the left */
while (*(mvp + 1) != (char) 0x00)
{
*mvp = *(mvp + 1);
mvp++;
}
*mvp = (char) 0x00;
p = nextval;
}
else
{
p = nextval + 1;
break;
}
}
/* more args available */
if (nextval)
{
*nextval = (char) 0x00;
return ++nextval;
}
/* no more args after this, jump to EOS */
nextval = src + strlen(src);
return nextval;
}
void
toupper_str(char *p)
{
while (*p)
{
if ((*p >= 'a') && (*p <= 'z'))
*p = toupper((int) *p);
p++;
}
}
RD_BOOL
str_startswith(const char *s, const char *prefix)
{
return (strncmp(s, prefix, strlen(prefix)) == 0);
}
/* Split input into lines, and call linehandler for each
line. Incomplete lines are saved in the rest variable, which should
initially point to NULL. When linehandler returns False, stop and
return False. Otherwise, return True. */
RD_BOOL
str_handle_lines(const char *input, char **rest, str_handle_lines_t linehandler, void *data)
{
char *buf, *p;
char *oldrest;
size_t inputlen;
size_t buflen;
size_t restlen = 0;
RD_BOOL ret = True;
/* Copy data to buffer */
inputlen = strlen(input);
if (*rest)
restlen = strlen(*rest);
buflen = restlen + inputlen + 1;
buf = (char *) xmalloc(buflen);
buf[0] = '\0';
if (*rest)
STRNCPY(buf, *rest, buflen);
strncat(buf, input, inputlen);
p = buf;
while (1)
{
char *newline = strchr(p, '\n');
if (newline)
{
*newline = '\0';
if (!linehandler(p, data))
{
p = newline + 1;
ret = False;
break;
}
p = newline + 1;
}
else
{
break;
}
}
/* Save in rest */
oldrest = *rest;
restlen = buf + buflen - p;
*rest = (char *) xmalloc(restlen);
STRNCPY((*rest), p, restlen);
xfree(oldrest);
xfree(buf);
return ret;
}
/* Execute the program specified by argv. For each line in
stdout/stderr output, call linehandler. Returns false on failure. */
RD_BOOL
subprocess(char *const argv[], str_handle_lines_t linehandler, void *data)
{
pid_t child;
int fd[2];
int n = 1;
char output[256];
char *rest = NULL;
if (pipe(fd) < 0)
{
logger(Core, Error, "subprocess(), pipe() failed: %s", strerror(errno));
return False;
}
if ((child = fork()) < 0)
{
logger(Core, Error, "subprocess(), fork() failed: %s", strerror(errno));
return False;
}
/* Child */
if (child == 0)
{
/* Close read end */
close(fd[0]);
/* Redirect stdout and stderr to pipe */
dup2(fd[1], 1);
dup2(fd[1], 2);
/* Execute */
execvp(argv[0], argv);
logger(Core, Error, "subprocess(), execvp() failed: %s", strerror(errno));
_exit(128);
}
/* Parent. Close write end. */
close(fd[1]);
while (n > 0)
{
n = read(fd[0], output, 255);
output[n] = '\0';
str_handle_lines(output, &rest, linehandler, data);
}
xfree(rest);
return True;
}
/* not all clibs got ltoa */
#define LTOA_BUFSIZE (sizeof(long) * 8 + 1)
char *
l_to_a(long N, int base)
{
static char ret[LTOA_BUFSIZE];
char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf);
register int divrem;
if (base < 36 || 2 > base)
base = 10;
if (N < 0)
{
*head++ = '-';
N = -N;
}
tail = buf + sizeof(buf);
*--tail = 0;
do
{
divrem = N % base;
*--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10;
N /= base;
}
while (N);
strcpy(head, tail);
return ret;
}
int
load_licence(unsigned char **data)
{
uint8 ho[20], hi[16];
char *home, path[PATH_MAX], hash[41];
struct stat st;
int fd, length;
home = getenv("HOME");
if (home == NULL)
return -1;
memset(hi, 0, sizeof(hi));
snprintf((char *) hi, 16, "%s", g_hostname);
sec_hash_sha1_16(ho, hi, g_static_rdesktop_salt_16);
sec_hash_to_string(hash, sizeof(hash), ho, sizeof(ho));
snprintf(path, PATH_MAX, "%s" RDESKTOP_LICENSE_STORE "/%s.cal", home, hash);
path[sizeof(path) - 1] = '\0';
fd = open(path, O_RDONLY);
if (fd == -1)
{
/* fallback to try reading old license file */
snprintf(path, PATH_MAX, "%s/.rdesktop/license.%s", home, g_hostname);
path[sizeof(path) - 1] = '\0';
if ((fd = open(path, O_RDONLY)) == -1)
return -1;
}
if (fstat(fd, &st))
{
close(fd);
return -1;
}
*data = (uint8 *) xmalloc(st.st_size);
length = read(fd, *data, st.st_size);
close(fd);
return length;
}
void
save_licence(unsigned char *data, int length)
{
uint8 ho[20], hi[16];
char *home, path[PATH_MAX], tmppath[PATH_MAX], hash[41];
int fd;
home = getenv("HOME");
if (home == NULL)
return;
snprintf(path, PATH_MAX, "%s" RDESKTOP_LICENSE_STORE, home);
path[sizeof(path) - 1] = '\0';
if (utils_mkdir_p(path, 0700) == -1)
{
logger(Core, Error, "save_license(), utils_mkdir_p() failed: %s", strerror(errno));
return;
}
memset(hi, 0, sizeof(hi));
snprintf((char *) hi, 16, "%s", g_hostname);
sec_hash_sha1_16(ho, hi, g_static_rdesktop_salt_16);
sec_hash_to_string(hash, sizeof(hash), ho, sizeof(ho));
/* write licence to {sha1}.cal.new, then atomically
rename to {sha1}.cal */
snprintf(path, PATH_MAX, "%s" RDESKTOP_LICENSE_STORE "/%s.cal", home, hash);
path[sizeof(path) - 1] = '\0';
snprintf(tmppath, PATH_MAX, "%s.new", path);
path[sizeof(path) - 1] = '\0';
fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd == -1)
{
logger(Core, Error, "save_license(), open() failed: %s", strerror(errno));
return;
}
if (write(fd, data, length) != length)
{
logger(Core, Error, "save_license(), write() failed: %s", strerror(errno));
unlink(tmppath);
}
else if (rename(tmppath, path) == -1)
{
logger(Core, Error, "save_license(), rename() failed: %s", strerror(errno));
unlink(tmppath);
}
close(fd);
}
/* create rdesktop ui */
void
rd_create_ui()
{
if (!ui_have_window())
{
/* create a window if we don't have one initialized */
if (!ui_create_window(g_requested_session_width, g_requested_session_height))
exit(EX_OSERR);
}
else
{
/* reset clipping if we already have a window */
ui_reset_clip();
}
}
/* Create the bitmap cache directory */
RD_BOOL
rd_pstcache_mkdir(void)
{
char *home;
char bmpcache_dir[256];
home = getenv("HOME");
if (home == NULL)
return False;
sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop");
if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
{
logger(Core, Error, "rd_pstcache_mkdir(), mkdir() failed: %s", strerror(errno));
return False;
}
sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache");
if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST)
{
logger(Core, Error, "rd_pstcache_mkdir(), mkdir() failed: %s", strerror(errno));
return False;
}
return True;
}
/* open a file in the .rdesktop directory */
int
rd_open_file(char *filename)
{
char *home;
char fn[256];
int fd;
home = getenv("HOME");
if (home == NULL)
return -1;
sprintf(fn, "%s/.rdesktop/%s", home, filename);
fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1)
logger(Core, Error, "rd_open_file(), open() failed: %s", strerror(errno));
return fd;
}
/* close file */
void
rd_close_file(int fd)
{
close(fd);
}
/* read from file*/
int
rd_read_file(int fd, void *ptr, int len)
{
return read(fd, ptr, len);
}
/* write to file */
int
rd_write_file(int fd, void *ptr, int len)
{
return write(fd, ptr, len);
}
/* move file pointer */
int
rd_lseek_file(int fd, int offset)
{
return lseek(fd, offset, SEEK_SET);
}
/* do a write lock on a file */
RD_BOOL
rd_lock_file(int fd, int start, int len)
{
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = start;
lock.l_len = len;
if (fcntl(fd, F_SETLK, &lock) == -1)
return False;
return True;
}
|