Post-release destabilisation! Completely remove the struct type
[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
4a693cfc 37void platform_get_x11_auth(struct X11Display *display, Conf *conf)
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;
bb8bd6e9 205 ret->name = dupstr(name);
0ac1920c 206
207 if (size) {
208 struct stat statbuf;
209 if (fstat(fd, &statbuf) < 0) {
210 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
211 memset(&statbuf, 0, sizeof(statbuf));
212 }
213
7bb18feb 214 *size = uint64_make((statbuf.st_size >> 16) >> 16,
215 statbuf.st_size);
0ac1920c 216 }
217
218 return ret;
219}
220
876eefd4 221int write_to_file(WFile *f, void *buffer, int length)
222{
223 char *p = (char *)buffer;
224 int so_far = 0;
225
226 /* Keep trying until we've really written as much as we can. */
227 while (length > 0) {
228 int ret = write(f->fd, p, length);
229
230 if (ret < 0)
231 return ret;
232
233 if (ret == 0)
234 break;
235
236 p += ret;
237 length -= ret;
238 so_far += ret;
239 }
240
241 return so_far;
242}
243
244void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
245{
246 struct utimbuf ut;
247
248 ut.actime = atime;
249 ut.modtime = mtime;
250
251 utime(f->name, &ut);
252}
253
254/* Closes and frees the WFile */
255void close_wfile(WFile *f)
256{
257 close(f->fd);
258 sfree(f->name);
259 sfree(f);
260}
261
0ac1920c 262/* Seek offset bytes through file, from whence, where whence is
263 FROM_START, FROM_CURRENT, or FROM_END */
264int seek_file(WFile *f, uint64 offset, int whence)
265{
266 off_t fileofft;
267 int lseek_whence;
268
7bb18feb 269 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
0ac1920c 270
271 switch (whence) {
272 case FROM_START:
273 lseek_whence = SEEK_SET;
274 break;
275 case FROM_CURRENT:
276 lseek_whence = SEEK_CUR;
277 break;
278 case FROM_END:
279 lseek_whence = SEEK_END;
280 break;
281 default:
282 return -1;
283 }
284
285 return lseek(f->fd, fileofft, lseek_whence) >= 0 ? 0 : -1;
286}
287
288uint64 get_file_posn(WFile *f)
289{
290 off_t fileofft;
291 uint64 ret;
292
293 fileofft = lseek(f->fd, (off_t) 0, SEEK_CUR);
294
7bb18feb 295 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
0ac1920c 296
297 return ret;
298}
299
876eefd4 300int file_type(char *name)
301{
302 struct stat statbuf;
303
304 if (stat(name, &statbuf) < 0) {
305 if (errno != ENOENT)
306 fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
307 return FILE_TYPE_NONEXISTENT;
308 }
309
310 if (S_ISREG(statbuf.st_mode))
311 return FILE_TYPE_FILE;
312
313 if (S_ISDIR(statbuf.st_mode))
314 return FILE_TYPE_DIRECTORY;
315
316 return FILE_TYPE_WEIRD;
317}
318
319struct DirHandle {
320 DIR *dir;
321};
322
323DirHandle *open_directory(char *name)
324{
325 DIR *dir;
326 DirHandle *ret;
327
328 dir = opendir(name);
329 if (!dir)
330 return NULL;
331
332 ret = snew(DirHandle);
333 ret->dir = dir;
334 return ret;
335}
336
337char *read_filename(DirHandle *dir)
338{
339 struct dirent *de;
340
341 do {
342 de = readdir(dir->dir);
343 if (de == NULL)
344 return NULL;
345 } while ((de->d_name[0] == '.' &&
346 (de->d_name[1] == '\0' ||
347 (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
348
349 return dupstr(de->d_name);
350}
351
352void close_directory(DirHandle *dir)
353{
354 closedir(dir->dir);
355 sfree(dir);
356}
357
358int test_wildcard(char *name, int cmdline)
359{
876eefd4 360 struct stat statbuf;
361
9c77ddf6 362 if (stat(name, &statbuf) == 0) {
876eefd4 363 return WCTYPE_FILENAME;
9c77ddf6 364 } else if (cmdline) {
365 /*
366 * On Unix, we never need to parse wildcards coming from
367 * the command line, because the shell will have expanded
368 * them into a filename list already.
369 */
370 return WCTYPE_NONEXISTENT;
371 } else {
372 glob_t globbed;
373 int ret = WCTYPE_NONEXISTENT;
374
375 if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
376 if (globbed.gl_pathc > 0)
377 ret = WCTYPE_WILDCARD;
378 globfree(&globbed);
379 }
380
381 return ret;
382 }
876eefd4 383}
384
385/*
9c77ddf6 386 * Actually return matching file names for a local wildcard.
876eefd4 387 */
388struct WildcardMatcher {
9c77ddf6 389 glob_t globbed;
390 int i;
876eefd4 391};
9c77ddf6 392WildcardMatcher *begin_wildcard_matching(char *name) {
393 WildcardMatcher *ret = snew(WildcardMatcher);
394
395 if (glob(name, 0, NULL, &ret->globbed) < 0) {
396 sfree(ret);
397 return NULL;
398 }
399
400 ret->i = 0;
401
402 return ret;
403}
404char *wildcard_get_filename(WildcardMatcher *dir) {
405 if (dir->i < dir->globbed.gl_pathc) {
406 return dupstr(dir->globbed.gl_pathv[dir->i++]);
407 } else
408 return NULL;
409}
410void finish_wildcard_matching(WildcardMatcher *dir) {
411 globfree(&dir->globbed);
412 sfree(dir);
413}
876eefd4 414
e9d14678 415int vet_filename(char *name)
416{
417 if (strchr(name, '/'))
418 return FALSE;
419
420 if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
421 return FALSE;
422
423 return TRUE;
424}
425
876eefd4 426int create_directory(char *name)
427{
428 return mkdir(name, 0777) == 0;
429}
430
431char *dir_file_cat(char *dir, char *file)
432{
433 return dupcat(dir, "/", file, NULL);
434}
435
436/*
39934deb 437 * Do a select() between all currently active network fds and
438 * optionally stdin.
876eefd4 439 */
65857773 440static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
876eefd4 441{
442 fd_set rset, wset, xset;
443 int i, fdcount, fdsize, *fdlist;
444 int fd, fdstate, rwx, ret, maxfd;
39934deb 445 long now = GETTICKCOUNT();
876eefd4 446
447 fdlist = NULL;
448 fdcount = fdsize = 0;
449
39934deb 450 do {
876eefd4 451
39934deb 452 /* Count the currently active fds. */
453 i = 0;
454 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
455 fd = next_fd(&fdstate, &rwx)) i++;
876eefd4 456
65857773 457 if (i < 1 && !no_fds_ok)
39934deb 458 return -1; /* doom */
876eefd4 459
39934deb 460 /* Expand the fdlist buffer if necessary. */
461 if (i > fdsize) {
462 fdsize = i + 16;
463 fdlist = sresize(fdlist, fdsize, int);
464 }
876eefd4 465
39934deb 466 FD_ZERO(&rset);
467 FD_ZERO(&wset);
468 FD_ZERO(&xset);
469 maxfd = 0;
876eefd4 470
39934deb 471 /*
472 * Add all currently open fds to the select sets, and store
473 * them in fdlist as well.
474 */
475 fdcount = 0;
476 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
477 fd = next_fd(&fdstate, &rwx)) {
478 fdlist[fdcount++] = fd;
479 if (rwx & 1)
480 FD_SET_MAX(fd, maxfd, rset);
481 if (rwx & 2)
482 FD_SET_MAX(fd, maxfd, wset);
483 if (rwx & 4)
484 FD_SET_MAX(fd, maxfd, xset);
485 }
486
487 if (include_stdin)
488 FD_SET_MAX(0, maxfd, rset);
489
490 do {
491 long next, ticks;
492 struct timeval tv, *ptv;
493
494 if (run_timers(now, &next)) {
495 ticks = next - GETTICKCOUNT();
496 if (ticks <= 0)
497 ticks = 1; /* just in case */
498 tv.tv_sec = ticks / 1000;
499 tv.tv_usec = ticks % 1000 * 1000;
500 ptv = &tv;
501 } else {
502 ptv = NULL;
503 }
504 ret = select(maxfd, &rset, &wset, &xset, ptv);
505 if (ret == 0)
506 now = next;
2ac3322e 507 else {
508 long newnow = GETTICKCOUNT();
509 /*
510 * Check to see whether the system clock has
511 * changed massively during the select.
512 */
513 if (newnow - now < 0 || newnow - now > next - now) {
514 /*
515 * If so, look at the elapsed time in the
516 * select and use it to compute a new
517 * tickcount_offset.
518 */
519 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
520 /* So we'd like GETTICKCOUNT to have returned othernow,
521 * but instead it return newnow. Hence ... */
522 tickcount_offset += othernow - newnow;
523 now = othernow;
524 } else {
525 now = newnow;
526 }
527 }
39934deb 528 } while (ret < 0 && errno != EINTR);
529 } while (ret == 0);
876eefd4 530
531 if (ret < 0) {
532 perror("select");
533 exit(1);
534 }
535
536 for (i = 0; i < fdcount; i++) {
537 fd = fdlist[i];
538 /*
539 * We must process exceptional notifications before
540 * ordinary readability ones, or we may go straight
541 * past the urgent marker.
542 */
543 if (FD_ISSET(fd, &xset))
544 select_result(fd, 4);
545 if (FD_ISSET(fd, &rset))
546 select_result(fd, 1);
547 if (FD_ISSET(fd, &wset))
548 select_result(fd, 2);
549 }
550
551 sfree(fdlist);
552
39934deb 553 return FD_ISSET(0, &rset) ? 1 : 0;
554}
555
556/*
557 * Wait for some network data and process it.
558 */
559int ssh_sftp_loop_iteration(void)
560{
65857773 561 return ssh_sftp_do_select(FALSE, FALSE);
39934deb 562}
563
564/*
565 * Read a PSFTP command line from stdin.
566 */
65857773 567char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
39934deb 568{
569 char *buf;
570 int buflen, bufsize, ret;
571
572 fputs(prompt, stdout);
573 fflush(stdout);
574
575 buf = NULL;
576 buflen = bufsize = 0;
577
578 while (1) {
65857773 579 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
39934deb 580 if (ret < 0) {
581 printf("connection died\n");
582 return NULL; /* woop woop */
583 }
584 if (ret > 0) {
585 if (buflen >= bufsize) {
586 bufsize = buflen + 512;
587 buf = sresize(buf, bufsize, char);
588 }
589 ret = read(0, buf+buflen, 1);
590 if (ret < 0) {
591 perror("read");
592 return NULL;
593 }
594 if (ret == 0) {
595 /* eof on stdin; no error, but no answer either */
596 return NULL;
597 }
598
599 if (buf[buflen++] == '\n') {
600 /* we have a full line */
601 return buf;
602 }
603 }
604 }
876eefd4 605}
606
607/*
608 * Main program: do platform-specific initialisation and then call
609 * psftp_main().
610 */
611int main(int argc, char *argv[])
612{
613 uxsel_init();
614 return psftp_main(argc, argv);
615}