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 | */ |
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 | |
4a693cfc |
37 | void platform_get_x11_auth(struct X11Display *display, Conf *conf) |
876eefd4 |
38 | { |
39 | /* Do nothing, therefore no auth. */ |
40 | } |
8def70c3 |
41 | const int platform_uses_x11_unix_by_default = TRUE; |
876eefd4 |
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 | |
ae62eaeb |
56 | FontSpec *platform_default_fontspec(const char *name) |
876eefd4 |
57 | { |
ae62eaeb |
58 | return fontspec_new(""); |
876eefd4 |
59 | } |
60 | |
962468d4 |
61 | Filename *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 |
69 | char *get_ttymode(void *frontend, const char *mode) { return NULL; } |
70 | |
edd0cb8a |
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 | |
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 | */ |
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 | |
0ac1920c |
123 | RFile *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 | |
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 | |
ee07dce4 |
177 | WFile *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 | |
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; |
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 |
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 | |
0ac1920c |
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 | |
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 | |
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 | |
7bb18feb |
296 | ret = uint64_make((fileofft >> 16) >> 16, fileofft); |
0ac1920c |
297 | |
298 | return ret; |
299 | } |
300 | |
876eefd4 |
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 | { |
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 | */ |
389 | struct WildcardMatcher { |
9c77ddf6 |
390 | glob_t globbed; |
391 | int i; |
876eefd4 |
392 | }; |
9c77ddf6 |
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 | } |
876eefd4 |
415 | |
e9d14678 |
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 | |
876eefd4 |
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 | /* |
39934deb |
438 | * Do a select() between all currently active network fds and |
439 | * optionally stdin. |
876eefd4 |
440 | */ |
65857773 |
441 | static 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; |
39934deb |
446 | 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 { |
492 | long next, ticks; |
493 | struct timeval tv, *ptv; |
494 | |
495 | if (run_timers(now, &next)) { |
496 | ticks = next - GETTICKCOUNT(); |
497 | if (ticks <= 0) |
498 | ticks = 1; /* just in case */ |
499 | tv.tv_sec = ticks / 1000; |
500 | tv.tv_usec = ticks % 1000 * 1000; |
501 | ptv = &tv; |
502 | } else { |
503 | ptv = NULL; |
504 | } |
505 | ret = select(maxfd, &rset, &wset, &xset, ptv); |
506 | if (ret == 0) |
507 | now = next; |
2ac3322e |
508 | else { |
509 | long newnow = GETTICKCOUNT(); |
510 | /* |
511 | * Check to see whether the system clock has |
512 | * changed massively during the select. |
513 | */ |
514 | if (newnow - now < 0 || newnow - now > next - now) { |
515 | /* |
516 | * If so, look at the elapsed time in the |
517 | * select and use it to compute a new |
518 | * tickcount_offset. |
519 | */ |
520 | long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000; |
521 | /* So we'd like GETTICKCOUNT to have returned othernow, |
522 | * but instead it return newnow. Hence ... */ |
523 | tickcount_offset += othernow - newnow; |
524 | now = othernow; |
525 | } else { |
526 | now = newnow; |
527 | } |
528 | } |
39934deb |
529 | } while (ret < 0 && errno != EINTR); |
530 | } while (ret == 0); |
876eefd4 |
531 | |
532 | if (ret < 0) { |
533 | perror("select"); |
534 | exit(1); |
535 | } |
536 | |
537 | for (i = 0; i < fdcount; i++) { |
538 | fd = fdlist[i]; |
539 | /* |
540 | * We must process exceptional notifications before |
541 | * ordinary readability ones, or we may go straight |
542 | * past the urgent marker. |
543 | */ |
544 | if (FD_ISSET(fd, &xset)) |
545 | select_result(fd, 4); |
546 | if (FD_ISSET(fd, &rset)) |
547 | select_result(fd, 1); |
548 | if (FD_ISSET(fd, &wset)) |
549 | select_result(fd, 2); |
550 | } |
551 | |
552 | sfree(fdlist); |
553 | |
39934deb |
554 | return FD_ISSET(0, &rset) ? 1 : 0; |
555 | } |
556 | |
557 | /* |
558 | * Wait for some network data and process it. |
559 | */ |
560 | int ssh_sftp_loop_iteration(void) |
561 | { |
65857773 |
562 | return ssh_sftp_do_select(FALSE, FALSE); |
39934deb |
563 | } |
564 | |
565 | /* |
566 | * Read a PSFTP command line from stdin. |
567 | */ |
65857773 |
568 | char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok) |
39934deb |
569 | { |
570 | char *buf; |
571 | int buflen, bufsize, ret; |
572 | |
573 | fputs(prompt, stdout); |
574 | fflush(stdout); |
575 | |
576 | buf = NULL; |
577 | buflen = bufsize = 0; |
578 | |
579 | while (1) { |
65857773 |
580 | ret = ssh_sftp_do_select(TRUE, no_fds_ok); |
39934deb |
581 | if (ret < 0) { |
582 | printf("connection died\n"); |
583 | return NULL; /* woop woop */ |
584 | } |
585 | if (ret > 0) { |
586 | if (buflen >= bufsize) { |
587 | bufsize = buflen + 512; |
588 | buf = sresize(buf, bufsize, char); |
589 | } |
590 | ret = read(0, buf+buflen, 1); |
591 | if (ret < 0) { |
592 | perror("read"); |
593 | return NULL; |
594 | } |
595 | if (ret == 0) { |
596 | /* eof on stdin; no error, but no answer either */ |
597 | return NULL; |
598 | } |
599 | |
600 | if (buf[buflen++] == '\n') { |
601 | /* we have a full line */ |
602 | return buf; |
603 | } |
604 | } |
605 | } |
876eefd4 |
606 | } |
607 | |
43a1c4a4 |
608 | void frontend_net_error_pending(void) {} |
609 | |
876eefd4 |
610 | /* |
611 | * Main program: do platform-specific initialisation and then call |
612 | * psftp_main(). |
613 | */ |
614 | int main(int argc, char *argv[]) |
615 | { |
616 | uxsel_init(); |
617 | return psftp_main(argc, argv); |
618 | } |