/[rdesktop]/sourceforge.net/trunk/rdesktop/rdesktop.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/rdesktop.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38 - (show annotations)
Thu Apr 4 12:04:33 2002 UTC (22 years ago) by matthewc
File MIME type: text/plain
File size: 7136 byte(s)
Committing my keymap work - unlike the version in the unified patches,
this one uses external keymap files (which are my preference).

1 /*
2 rdesktop: A Remote Desktop Protocol client.
3 Entrypoint and utility functions
4 Copyright (C) Matthew Chapman 1999-2001
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <stdlib.h> /* malloc realloc free */
22 #include <stdarg.h> /* va_list va_start va_end */
23 #include <unistd.h> /* read close getuid getgid getpid getppid gethostname */
24 #include <fcntl.h> /* open */
25 #include <pwd.h> /* getpwuid */
26 #include <sys/stat.h> /* stat */
27 #include <sys/time.h> /* gettimeofday */
28 #include <sys/times.h> /* times */
29 #include "rdesktop.h"
30
31 char username[16];
32 char hostname[16];
33 char keymapname[16];
34 int keylayout;
35 int width;
36 int height;
37 BOOL bitmap_compression = True;
38 BOOL sendmotion = True;
39 BOOL orders = True;
40 BOOL licence = True;
41 BOOL encryption = True;
42 BOOL desktop_save = True;
43 BOOL fullscreen = False;
44
45 /* Display usage information */
46 static void
47 usage(char *program)
48 {
49 printf("Usage: %s [options] server\n", program);
50 printf(" -u: user name\n");
51 printf(" -d: domain\n");
52 printf(" -s: shell\n");
53 printf(" -c: working directory\n");
54 printf(" -p: password (autologon)\n");
55 printf(" -n: client hostname\n");
56 printf(" -k: keyboard layout\n");
57 printf(" -g: desktop geometry (WxH)\n");
58 printf(" -f: full-screen mode\n");
59 printf(" -b: force bitmap updates\n");
60 printf(" -e: disable encryption (French TS)\n");
61 printf(" -m: do not send motion events\n");
62 printf(" -l: do not request licence\n\n");
63 }
64
65 /* Client program */
66 int
67 main(int argc, char *argv[])
68 {
69 char fullhostname[64];
70 char domain[16];
71 char password[16];
72 char shell[32];
73 char directory[32];
74 char title[32];
75 struct passwd *pw;
76 char *server, *p;
77 uint32 flags;
78 int c;
79
80 printf("rdesktop: A Remote Desktop Protocol client.\n");
81 printf("Version " VERSION ". Copyright (C) 1999-2001 Matt Chapman.\n");
82 printf("See http://www.rdesktop.org/ for more information.\n\n");
83
84 flags = RDP_LOGON_NORMAL;
85 domain[0] = password[0] = shell[0] = directory[0] = 0;
86 strcpy(keymapname, "us");
87
88 while ((c = getopt(argc, argv, "u:d:s:c:p:n:k:g:fbemlh?")) != -1)
89 {
90 switch (c)
91 {
92 case 'u':
93 STRNCPY(username, optarg, sizeof(username));
94 break;
95
96 case 'd':
97 STRNCPY(domain, optarg, sizeof(domain));
98 break;
99
100 case 's':
101 STRNCPY(shell, optarg, sizeof(shell));
102 break;
103
104 case 'c':
105 STRNCPY(directory, optarg, sizeof(directory));
106 break;
107
108 case 'p':
109 STRNCPY(password, optarg, sizeof(password));
110 flags |= RDP_LOGON_AUTO;
111 break;
112
113 case 'n':
114 STRNCPY(hostname, optarg, sizeof(hostname));
115 break;
116
117 case 'k':
118 STRNCPY(keymapname, optarg, sizeof(keymapname));
119 break;
120
121 case 'g':
122 width = strtol(optarg, &p, 10);
123 if (*p == 'x')
124 height = strtol(p+1, NULL, 10);
125
126 if ((width == 0) || (height == 0))
127 {
128 error("invalid geometry\n");
129 return 1;
130 }
131 break;
132
133 case 'f':
134 fullscreen = True;
135 break;
136
137 case 'b':
138 orders = False;
139 break;
140
141 case 'e':
142 encryption = False;
143 break;
144
145 case 'm':
146 sendmotion = False;
147 break;
148
149 case 'l':
150 licence = False;
151 break;
152
153 case 'h':
154 case '?':
155 default:
156 usage(argv[0]);
157 return 1;
158 }
159 }
160
161 if (argc - optind < 1)
162 {
163 usage(argv[0]);
164 return 1;
165 }
166
167 server = argv[optind];
168
169 if (username[0] == 0)
170 {
171 pw = getpwuid(getuid());
172 if ((pw == NULL) || (pw->pw_name == NULL))
173 {
174 error("could not determine username, use -u\n");
175 return 1;
176 }
177
178 STRNCPY(username, pw->pw_name, sizeof(username));
179 }
180
181 if (hostname[0] == 0)
182 {
183 if (gethostname(fullhostname, sizeof(fullhostname)) == -1)
184 {
185 error("could not determine local hostname, use -n\n");
186 return 1;
187 }
188
189 p = strchr(fullhostname, '.');
190 if (p != NULL)
191 *p = 0;
192
193 STRNCPY(hostname, fullhostname, sizeof(hostname));
194 }
195
196 if (!strcmp(password, "-"))
197 {
198 p = getpass("Password: ");
199 if (p == NULL)
200 {
201 error("failed to read password\n");
202 return 0;
203 }
204 STRNCPY(password, p, sizeof(password));
205 }
206
207 if ((width == 0) || (height == 0))
208 {
209 width = 800;
210 height = 600;
211 }
212
213 strcpy(title, "rdesktop - ");
214 strncat(title, server, sizeof(title) - sizeof("rdesktop - "));
215
216 if (ui_create_window(title))
217 {
218 if (!rdp_connect(server, flags, domain, password, shell,
219 directory))
220 return 1;
221
222 printf("Connection successful.\n");
223 rdp_main_loop();
224 printf("Disconnecting...\n");
225 ui_destroy_window();
226 rdp_disconnect();
227 }
228
229 return 0;
230 }
231
232 /* Generate a 32-byte random for the secure transport code. */
233 void
234 generate_random(uint8 *random)
235 {
236 struct stat st;
237 struct tms tmsbuf;
238 uint32 *r = (uint32 *) random;
239 int fd;
240
241 /* If we have a kernel random device, use it. */
242 if (((fd = open("/dev/urandom", O_RDONLY)) != -1)
243 || ((fd = open("/dev/random", O_RDONLY)) != -1))
244 {
245 read(fd, random, 32);
246 close(fd);
247 return;
248 }
249
250 /* Otherwise use whatever entropy we can gather - ideas welcome. */
251 r[0] = (getpid()) | (getppid() << 16);
252 r[1] = (getuid()) | (getgid() << 16);
253 r[2] = times(&tmsbuf); /* system uptime (clocks) */
254 gettimeofday((struct timeval *) &r[3], NULL); /* sec and usec */
255 stat("/tmp", &st);
256 r[5] = st.st_atime;
257 r[6] = st.st_mtime;
258 r[7] = st.st_ctime;
259 }
260
261 /* malloc; exit if out of memory */
262 void *
263 xmalloc(int size)
264 {
265 void *mem = malloc(size);
266 if (mem == NULL)
267 {
268 error("xmalloc %d\n", size);
269 exit(1);
270 }
271 return mem;
272 }
273
274 /* realloc; exit if out of memory */
275 void *
276 xrealloc(void *oldmem, int size)
277 {
278 void *mem = realloc(oldmem, size);
279 if (mem == NULL)
280 {
281 error("xrealloc %d\n", size);
282 exit(1);
283 }
284 return mem;
285 }
286
287 /* free */
288 void
289 xfree(void *mem)
290 {
291 free(mem);
292 }
293
294 /* report an error */
295 void
296 error(char *format, ...)
297 {
298 va_list ap;
299
300 fprintf(stderr, "ERROR: ");
301
302 va_start(ap, format);
303 vfprintf(stderr, format, ap);
304 va_end(ap);
305 }
306
307 /* report an unimplemented protocol feature */
308 void
309 unimpl(char *format, ...)
310 {
311 va_list ap;
312
313 fprintf(stderr, "NOT IMPLEMENTED: ");
314
315 va_start(ap, format);
316 vfprintf(stderr, format, ap);
317 va_end(ap);
318 }
319
320 /* produce a hex dump */
321 void
322 hexdump(unsigned char *p, unsigned int len)
323 {
324 unsigned char *line = p;
325 unsigned int thisline, offset = 0;
326 int i;
327
328 while (offset < len)
329 {
330 printf("%04x ", offset);
331 thisline = len - offset;
332 if (thisline > 16)
333 thisline = 16;
334
335 for (i = 0; i < thisline; i++)
336 printf("%02x ", line[i]);
337
338 for (; i < 16; i++)
339 printf(" ");
340
341 for (i = 0; i < thisline; i++)
342 printf("%c",
343 (line[i] >= 0x20
344 && line[i] < 0x7f) ? line[i] : '.');
345
346 printf("\n");
347 offset += thisline;
348 line += thisline;
349 }
350 }

  ViewVC Help
Powered by ViewVC 1.1.26