Revamp of the local X11 connection code. We now parse X display
[u/mdw/putty] / unix / uxsftp.c
CommitLineData
876eefd4 1/*
2 * uxsftp.c: the Unix-specific parts of PSFTP and PSCP.
3 */
4
5#include <sys/time.h>
6#include <sys/types.h>
7#include <sys/stat.h>
18609086 8#include <stdlib.h>
876eefd4 9#include <fcntl.h>
10#include <dirent.h>
11#include <unistd.h>
12#include <utime.h>
13#include <errno.h>
14#include <assert.h>
9c77ddf6 15#include <glob.h>
154c73d5 16#ifndef HAVE_NO_SYS_SELECT_H
17#include <sys/select.h>
18#endif
876eefd4 19
20#include "putty.h"
8def70c3 21#include "ssh.h"
876eefd4 22#include "psftp.h"
0ac1920c 23#include "int64.h"
876eefd4 24
25/*
26 * In PSFTP our selects are synchronous, so these functions are
27 * empty stubs.
28 */
29int uxsel_input_add(int fd, int rwx) { return 0; }
30void uxsel_input_remove(int id) { }
31
32char *x_get_default(const char *key)
33{
34 return NULL; /* this is a stub */
35}
36
8def70c3 37void platform_get_x11_auth(struct X11Display *display, const Config *cfg)
876eefd4 38{
39 /* Do nothing, therefore no auth. */
40}
8def70c3 41const int platform_uses_x11_unix_by_default = TRUE;
876eefd4 42
43/*
44 * Default settings that are specific to PSFTP.
45 */
46char *platform_default_s(const char *name)
47{
48 return NULL;
49}
50
51int platform_default_i(const char *name, int def)
52{
53 return def;
54}
55
56FontSpec platform_default_fontspec(const char *name)
57{
58 FontSpec ret;
59 *ret.name = '\0';
60 return ret;
61}
62
63Filename platform_default_filename(const char *name)
64{
65 Filename ret;
66 if (!strcmp(name, "LogFileName"))
67 strcpy(ret.path, "putty.log");
68 else
69 *ret.path = '\0';
70 return ret;
71}
72
c6ccd5c2 73char *get_ttymode(void *frontend, const char *mode) { return NULL; }
74
edd0cb8a 75int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
76{
77 int ret;
78 ret = cmdline_get_passwd_input(p, in, inlen);
79 if (ret == -1)
80 ret = console_get_userpass_input(p, in, inlen);
81 return ret;
82}
83
876eefd4 84/*
876eefd4 85 * Set local current directory. Returns NULL on success, or else an
86 * error message which must be freed after printing.
87 */
88char *psftp_lcd(char *dir)
89{
90 if (chdir(dir) < 0)
91 return dupprintf("%s: chdir: %s", dir, strerror(errno));
92 else
93 return NULL;
94}
95
96/*
97 * Get local current directory. Returns a string which must be
98 * freed.
99 */
100char *psftp_getcwd(void)
101{
102 char *buffer, *ret;
103 int size = 256;
104
105 buffer = snewn(size, char);
106 while (1) {
107 ret = getcwd(buffer, size);
108 if (ret != NULL)
109 return ret;
110 if (errno != ERANGE) {
111 sfree(buffer);
112 return dupprintf("[cwd unavailable: %s]", strerror(errno));
113 }
114 /*
115 * Otherwise, ERANGE was returned, meaning the buffer
116 * wasn't big enough.
117 */
118 size = size * 3 / 2;
119 buffer = sresize(buffer, size, char);
120 }
121}
122
123struct RFile {
124 int fd;
125};
126
0ac1920c 127RFile *open_existing_file(char *name, uint64 *size,
876eefd4 128 unsigned long *mtime, unsigned long *atime)
129{
130 int fd;
131 RFile *ret;
132
133 fd = open(name, O_RDONLY);
134 if (fd < 0)
135 return NULL;
136
137 ret = snew(RFile);
138 ret->fd = fd;
139
140 if (size || mtime || atime) {
141 struct stat statbuf;
142 if (fstat(fd, &statbuf) < 0) {
143 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
144 memset(&statbuf, 0, sizeof(statbuf));
145 }
146
7bb18feb 147 if (size)
148 *size = uint64_make((statbuf.st_size >> 16) >> 16,
149 statbuf.st_size);
0ac1920c 150
876eefd4 151 if (mtime)
152 *mtime = statbuf.st_mtime;
153
154 if (atime)
155 *atime = statbuf.st_atime;
156 }
157
158 return ret;
159}
160
161int read_from_file(RFile *f, void *buffer, int length)
162{
163 return read(f->fd, buffer, length);
164}
165
166void close_rfile(RFile *f)
167{
168 close(f->fd);
169 sfree(f);
170}
171
172struct WFile {
173 int fd;
174 char *name;
175};
176
177WFile *open_new_file(char *name)
178{
179 int fd;
180 WFile *ret;
181
182 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
183 if (fd < 0)
184 return NULL;
185
186 ret = snew(WFile);
187 ret->fd = fd;
188 ret->name = dupstr(name);
189
190 return ret;
191}
192
0ac1920c 193
194WFile *open_existing_wfile(char *name, uint64 *size)
195{
196 int fd;
197 WFile *ret;
198
199 fd = open(name, O_APPEND | O_WRONLY);
200 if (fd < 0)
201 return NULL;
202
203 ret = snew(WFile);
204 ret->fd = fd;
205
206 if (size) {
207 struct stat statbuf;
208 if (fstat(fd, &statbuf) < 0) {
209 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
210 memset(&statbuf, 0, sizeof(statbuf));
211 }
212
7bb18feb 213 *size = uint64_make((statbuf.st_size >> 16) >> 16,
214 statbuf.st_size);
0ac1920c 215 }
216
217 return ret;
218}
219
876eefd4 220int write_to_file(WFile *f, void *buffer, int length)
221{
222 char *p = (char *)buffer;
223 int so_far = 0;
224
225 /* Keep trying until we've really written as much as we can. */
226 while (length > 0) {
227 int ret = write(f->fd, p, length);
228
229 if (ret < 0)
230 return ret;
231
232 if (ret == 0)
233 break;
234
235 p += ret;
236 length -= ret;
237 so_far += ret;
238 }
239
240 return so_far;
241}
242
243void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
244{
245 struct utimbuf ut;
246
247 ut.actime = atime;
248 ut.modtime = mtime;
249
250 utime(f->name, &ut);
251}
252
253/* Closes and frees the WFile */
254void close_wfile(WFile *f)
255{
256 close(f->fd);
257 sfree(f->name);
258 sfree(f);
259}
260
0ac1920c 261/* Seek offset bytes through file, from whence, where whence is
262 FROM_START, FROM_CURRENT, or FROM_END */
263int seek_file(WFile *f, uint64 offset, int whence)
264{
265 off_t fileofft;
266 int lseek_whence;
267
7bb18feb 268 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
0ac1920c 269
270 switch (whence) {
271 case FROM_START:
272 lseek_whence = SEEK_SET;
273 break;
274 case FROM_CURRENT:
275 lseek_whence = SEEK_CUR;
276 break;
277 case FROM_END:
278 lseek_whence = SEEK_END;
279 break;
280 default:
281 return -1;
282 }
283
284 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
285}
286
287uint64 get_file_posn(WFile *f)
288{
289 off_t fileofft;
290 uint64 ret;
291
292 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
293
7bb18feb 294 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
0ac1920c 295
296 return ret;
297}
298
876eefd4 299int file_type(char *name)
300{
301 struct stat statbuf;
302
303 if (stat(name, &statbuf) < 0) {
304 if (errno != ENOENT)
305 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
306 return FILE_TYPE_NONEXISTENT;
307 }
308
309 if (S_ISREG(statbuf.st_mode))
310 return FILE_TYPE_FILE;
311
312 if (S_ISDIR(statbuf.st_mode))
313 return FILE_TYPE_DIRECTORY;
314
315 return FILE_TYPE_WEIRD;
316}
317
318struct DirHandle {
319 DIR *dir;
320};
321
322DirHandle *open_directory(char *name)
323{
324 DIR *dir;
325 DirHandle *ret;
326
327 dir = opendir(name);
328 if (!dir)
329 return NULL;
330
331 ret = snew(DirHandle);
332 ret->dir = dir;
333 return ret;
334}
335
336char *read_filename(DirHandle *dir)
337{
338 struct dirent *de;
339
340 do {
341 de = readdir(dir->dir);
342 if (de == NULL)
343 return NULL;
344 } while ((de->d_name[0] == '.' &&
345 (de->d_name[1] == '\0' ||
346 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
347
348 return dupstr(de->d_name);
349}
350
351void close_directory(DirHandle *dir)
352{
353 closedir(dir->dir);
354 sfree(dir);
355}
356
357int test_wildcard(char *name, int cmdline)
358{
876eefd4 359 struct stat statbuf;
360
9c77ddf6 361 if (stat(name, &statbuf) == 0) {
876eefd4 362 return WCTYPE_FILENAME;
9c77ddf6 363 } else if (cmdline) {
364 /*
365 * On Unix, we never need to parse wildcards coming from
366 * the command line, because the shell will have expanded
367 * them into a filename list already.
368 */
369 return WCTYPE_NONEXISTENT;
370 } else {
371 glob_t globbed;
372 int ret = WCTYPE_NONEXISTENT;
373
374 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
375 if (globbed.gl_pathc > 0)
376 ret = WCTYPE_WILDCARD;
377 globfree(&globbed);
378 }
379
380 return ret;
381 }
876eefd4 382}
383
384/*
9c77ddf6 385 * Actually return matching file names for a local wildcard.
876eefd4 386 */
387struct WildcardMatcher {
9c77ddf6 388 glob_t globbed;
389 int i;
876eefd4 390};
9c77ddf6 391WildcardMatcher *begin_wildcard_matching(char *name) {
392 WildcardMatcher *ret = snew(WildcardMatcher);
393
394 if (glob(name, 0, NULL, &ret->globbed) < 0) {
395 sfree(ret);
396 return NULL;
397 }
398
399 ret->i = 0;
400
401 return ret;
402}
403char *wildcard_get_filename(WildcardMatcher *dir) {
404 if (dir->i < dir->globbed.gl_pathc) {
405 return dupstr(dir->globbed.gl_pathv[dir->i++]);
406 } else
407 return NULL;
408}
409void finish_wildcard_matching(WildcardMatcher *dir) {
410 globfree(&dir->globbed);
411 sfree(dir);
412}
876eefd4 413
e9d14678 414int vet_filename(char *name)
415{
416 if (strchr(name, '/'))
417 return FALSE;
418
419 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
420 return FALSE;
421
422 return TRUE;
423}
424
876eefd4 425int create_directory(char *name)
426{
427 return mkdir(name, 0777) == 0;
428}
429
430char *dir_file_cat(char *dir, char *file)
431{
432 return dupcat(dir, "/", file, NULL);
433}
434
435/*
39934deb 436 * Do a select() between all currently active network fds and
437 * optionally stdin.
876eefd4 438 */
65857773 439static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
876eefd4 440{
441 fd_set rset, wset, xset;
442 int i, fdcount, fdsize, *fdlist;
443 int fd, fdstate, rwx, ret, maxfd;
39934deb 444 long now = GETTICKCOUNT();
876eefd4 445
446 fdlist = NULL;
447 fdcount = fdsize = 0;
448
39934deb 449 do {
876eefd4 450
39934deb 451 /* Count the currently active fds. */
452 i = 0;
453 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
454 fd = next_fd(&fdstate, &rwx)) i++;
876eefd4 455
65857773 456 if (i < 1 && !no_fds_ok)
39934deb 457 return -1; /* doom */
876eefd4 458
39934deb 459 /* Expand the fdlist buffer if necessary. */
460 if (i > fdsize) {
461 fdsize = i + 16;
462 fdlist = sresize(fdlist, fdsize, int);
463 }
876eefd4 464
39934deb 465 FD_ZERO(&rset);
466 FD_ZERO(&wset);
467 FD_ZERO(&xset);
468 maxfd = 0;
876eefd4 469
39934deb 470 /*
471 * Add all currently open fds to the select sets, and store
472 * them in fdlist as well.
473 */
474 fdcount = 0;
475 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
476 fd = next_fd(&fdstate, &rwx)) {
477 fdlist[fdcount++] = fd;
478 if (rwx & 1)
479 FD_SET_MAX(fd, maxfd, rset);
480 if (rwx & 2)
481 FD_SET_MAX(fd, maxfd, wset);
482 if (rwx & 4)
483 FD_SET_MAX(fd, maxfd, xset);
484 }
485
486 if (include_stdin)
487 FD_SET_MAX(0, maxfd, rset);
488
489 do {
490 long next, ticks;
491 struct timeval tv, *ptv;
492
493 if (run_timers(now, &next)) {
494 ticks = next - GETTICKCOUNT();
495 if (ticks <= 0)
496 ticks = 1; /* just in case */
497 tv.tv_sec = ticks / 1000;
498 tv.tv_usec = ticks % 1000 * 1000;
499 ptv = &tv;
500 } else {
501 ptv = NULL;
502 }
503 ret = select(maxfd, &rset, &wset, &xset, ptv);
504 if (ret == 0)
505 now = next;
2ac3322e 506 else {
507 long newnow = GETTICKCOUNT();
508 /*
509 * Check to see whether the system clock has
510 * changed massively during the select.
511 */
512 if (newnow - now < 0 || newnow - now > next - now) {
513 /*
514 * If so, look at the elapsed time in the
515 * select and use it to compute a new
516 * tickcount_offset.
517 */
518 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
519 /* So we'd like GETTICKCOUNT to have returned othernow,
520 * but instead it return newnow. Hence ... */
521 tickcount_offset += othernow - newnow;
522 now = othernow;
523 } else {
524 now = newnow;
525 }
526 }
39934deb 527 } while (ret < 0 && errno != EINTR);
528 } while (ret == 0);
876eefd4 529
530 if (ret < 0) {
531 perror("select");
532 exit(1);
533 }
534
535 for (i = 0; i < fdcount; i++) {
536 fd = fdlist[i];
537 /*
538 * We must process exceptional notifications before
539 * ordinary readability ones, or we may go straight
540 * past the urgent marker.
541 */
542 if (FD_ISSET(fd, &xset))
543 select_result(fd, 4);
544 if (FD_ISSET(fd, &rset))
545 select_result(fd, 1);
546 if (FD_ISSET(fd, &wset))
547 select_result(fd, 2);
548 }
549
550 sfree(fdlist);
551
39934deb 552 return FD_ISSET(0, &rset) ? 1 : 0;
553}
554
555/*
556 * Wait for some network data and process it.
557 */
558int ssh_sftp_loop_iteration(void)
559{
65857773 560 return ssh_sftp_do_select(FALSE, FALSE);
39934deb 561}
562
563/*
564 * Read a PSFTP command line from stdin.
565 */
65857773 566char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
39934deb 567{
568 char *buf;
569 int buflen, bufsize, ret;
570
571 fputs(prompt, stdout);
572 fflush(stdout);
573
574 buf = NULL;
575 buflen = bufsize = 0;
576
577 while (1) {
65857773 578 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
39934deb 579 if (ret < 0) {
580 printf("connection died\n");
581 return NULL; /* woop woop */
582 }
583 if (ret > 0) {
584 if (buflen >= bufsize) {
585 bufsize = buflen + 512;
586 buf = sresize(buf, bufsize, char);
587 }
588 ret = read(0, buf+buflen, 1);
589 if (ret < 0) {
590 perror("read");
591 return NULL;
592 }
593 if (ret == 0) {
594 /* eof on stdin; no error, but no answer either */
595 return NULL;
596 }
597
598 if (buf[buflen++] == '\n') {
599 /* we have a full line */
600 return buf;
601 }
602 }
603 }
876eefd4 604}
605
606/*
607 * Main program: do platform-specific initialisation and then call
608 * psftp_main().
609 */
610int main(int argc, char *argv[])
611{
612 uxsel_init();
613 return psftp_main(argc, argv);
614}