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