Fix another giant batch of resource leaks. (Mostly memory, but there's
[u/mdw/putty] / unix / uxsftp.c
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>
8 #include <stdlib.h>
9 #include <fcntl.h>
10 #include <dirent.h>
11 #include <unistd.h>
12 #include <utime.h>
13 #include <errno.h>
14 #include <assert.h>
15 #include <glob.h>
16 #ifndef HAVE_NO_SYS_SELECT_H
17 #include <sys/select.h>
18 #endif
19
20 #include "putty.h"
21 #include "ssh.h"
22 #include "psftp.h"
23 #include "int64.h"
24
25 /*
26 * In PSFTP our selects are synchronous, so these functions are
27 * empty stubs.
28 */
29 int uxsel_input_add(int fd, int rwx) { return 0; }
30 void uxsel_input_remove(int id) { }
31
32 char *x_get_default(const char *key)
33 {
34 return NULL; /* this is a stub */
35 }
36
37 void platform_get_x11_auth(struct X11Display *display, Conf *conf)
38 {
39 /* Do nothing, therefore no auth. */
40 }
41 const int platform_uses_x11_unix_by_default = TRUE;
42
43 /*
44 * Default settings that are specific to PSFTP.
45 */
46 char *platform_default_s(const char *name)
47 {
48 return NULL;
49 }
50
51 int platform_default_i(const char *name, int def)
52 {
53 return def;
54 }
55
56 FontSpec *platform_default_fontspec(const char *name)
57 {
58 return fontspec_new("");
59 }
60
61 Filename *platform_default_filename(const char *name)
62 {
63 if (!strcmp(name, "LogFileName"))
64 return filename_from_str("putty.log");
65 else
66 return filename_from_str("");
67 }
68
69 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
70
71 int 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
80 /*
81 * Set local current directory. Returns NULL on success, or else an
82 * error message which must be freed after printing.
83 */
84 char *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 */
96 char *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
119 struct RFile {
120 int fd;
121 };
122
123 RFile *open_existing_file(char *name, uint64 *size,
124 unsigned long *mtime, unsigned long *atime,
125 long *perms)
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
137 if (size || mtime || atime || perms) {
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
144 if (size)
145 *size = uint64_make((statbuf.st_size >> 16) >> 16,
146 statbuf.st_size);
147
148 if (mtime)
149 *mtime = statbuf.st_mtime;
150
151 if (atime)
152 *atime = statbuf.st_atime;
153
154 if (perms)
155 *perms = statbuf.st_mode;
156 }
157
158 return ret;
159 }
160
161 int read_from_file(RFile *f, void *buffer, int length)
162 {
163 return read(f->fd, buffer, length);
164 }
165
166 void close_rfile(RFile *f)
167 {
168 close(f->fd);
169 sfree(f);
170 }
171
172 struct WFile {
173 int fd;
174 char *name;
175 };
176
177 WFile *open_new_file(char *name, long perms)
178 {
179 int fd;
180 WFile *ret;
181
182 fd = open(name, O_CREAT | O_TRUNC | O_WRONLY,
183 (mode_t)(perms ? perms : 0666));
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
194
195 WFile *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;
206 ret->name = dupstr(name);
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
215 *size = uint64_make((statbuf.st_size >> 16) >> 16,
216 statbuf.st_size);
217 }
218
219 return ret;
220 }
221
222 int 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
245 void 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 */
256 void close_wfile(WFile *f)
257 {
258 close(f->fd);
259 sfree(f->name);
260 sfree(f);
261 }
262
263 /* Seek offset bytes through file, from whence, where whence is
264 FROM_START, FROM_CURRENT, or FROM_END */
265 int seek_file(WFile *f, uint64 offset, int whence)
266 {
267 off_t fileofft;
268 int lseek_whence;
269
270 fileofft = (((off_t) offset.hi << 16) << 16) + offset.lo;
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
289 uint64 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
296 ret = uint64_make((fileofft >> 16) >> 16, fileofft);
297
298 return ret;
299 }
300
301 int 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
320 struct DirHandle {
321 DIR *dir;
322 };
323
324 DirHandle *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
338 char *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
353 void close_directory(DirHandle *dir)
354 {
355 closedir(dir->dir);
356 sfree(dir);
357 }
358
359 int test_wildcard(char *name, int cmdline)
360 {
361 struct stat statbuf;
362
363 if (stat(name, &statbuf) == 0) {
364 return WCTYPE_FILENAME;
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 }
384 }
385
386 /*
387 * Actually return matching file names for a local wildcard.
388 */
389 struct WildcardMatcher {
390 glob_t globbed;
391 int i;
392 };
393 WildcardMatcher *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 }
405 char *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 }
411 void finish_wildcard_matching(WildcardMatcher *dir) {
412 globfree(&dir->globbed);
413 sfree(dir);
414 }
415
416 int 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
427 int create_directory(char *name)
428 {
429 return mkdir(name, 0777) == 0;
430 }
431
432 char *dir_file_cat(char *dir, char *file)
433 {
434 return dupcat(dir, "/", file, NULL);
435 }
436
437 /*
438 * Do a select() between all currently active network fds and
439 * optionally stdin.
440 */
441 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
442 {
443 fd_set rset, wset, xset;
444 int i, fdcount, fdsize, *fdlist;
445 int fd, fdstate, rwx, ret, maxfd;
446 unsigned long now = GETTICKCOUNT();
447
448 fdlist = NULL;
449 fdcount = fdsize = 0;
450
451 do {
452
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++;
457
458 if (i < 1 && !no_fds_ok)
459 return -1; /* doom */
460
461 /* Expand the fdlist buffer if necessary. */
462 if (i > fdsize) {
463 fdsize = i + 16;
464 fdlist = sresize(fdlist, fdsize, int);
465 }
466
467 FD_ZERO(&rset);
468 FD_ZERO(&wset);
469 FD_ZERO(&xset);
470 maxfd = 0;
471
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 {
492 unsigned long next, then;
493 long ticks;
494 struct timeval tv, *ptv;
495
496 if (run_timers(now, &next)) {
497 then = now;
498 now = GETTICKCOUNT();
499 if (now - then > next - then)
500 ticks = 0;
501 else
502 ticks = next - now;
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;
512 else
513 now = GETTICKCOUNT();
514 } while (ret < 0 && errno != EINTR);
515 } while (ret == 0);
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
539 return FD_ISSET(0, &rset) ? 1 : 0;
540 }
541
542 /*
543 * Wait for some network data and process it.
544 */
545 int ssh_sftp_loop_iteration(void)
546 {
547 return ssh_sftp_do_select(FALSE, FALSE);
548 }
549
550 /*
551 * Read a PSFTP command line from stdin.
552 */
553 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
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) {
565 ret = ssh_sftp_do_select(TRUE, no_fds_ok);
566 if (ret < 0) {
567 printf("connection died\n");
568 sfree(buf);
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");
579 sfree(buf);
580 return NULL;
581 }
582 if (ret == 0) {
583 /* eof on stdin; no error, but no answer either */
584 sfree(buf);
585 return NULL;
586 }
587
588 if (buf[buflen++] == '\n') {
589 /* we have a full line */
590 return buf;
591 }
592 }
593 }
594 }
595
596 void frontend_net_error_pending(void) {}
597
598 /*
599 * Main program: do platform-specific initialisation and then call
600 * psftp_main().
601 */
602 int main(int argc, char *argv[])
603 {
604 uxsel_init();
605 return psftp_main(argc, argv);
606 }