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