]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxsftp.c
794cc9cbda7e0099c0794ec79a2083a1c16ed05b
[PuTTY.git] / 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 uxsel_id *uxsel_input_add(int fd, int rwx) { return NULL; }
30 void uxsel_input_remove(uxsel_id *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, const 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(const 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(const 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(const 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(const 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(const 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(const 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(const 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 char *stripslashes(const char *str, int local)
417 {
418     char *p;
419
420     /*
421      * On Unix, we do the same thing regardless of the 'local'
422      * parameter.
423      */
424     p = strrchr(str, '/');
425     if (p) str = p+1;
426
427     return (char *)str;
428 }
429
430 int vet_filename(const char *name)
431 {
432     if (strchr(name, '/'))
433         return FALSE;
434
435     if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
436         return FALSE;
437
438     return TRUE;
439 }
440
441 int create_directory(const char *name)
442 {
443     return mkdir(name, 0777) == 0;
444 }
445
446 char *dir_file_cat(const char *dir, const char *file)
447 {
448     return dupcat(dir, "/", file, NULL);
449 }
450
451 /*
452  * Do a select() between all currently active network fds and
453  * optionally stdin.
454  */
455 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
456 {
457     fd_set rset, wset, xset;
458     int i, fdcount, fdsize, *fdlist;
459     int fd, fdstate, rwx, ret, maxfd;
460     unsigned long now = GETTICKCOUNT();
461     unsigned long next;
462
463     fdlist = NULL;
464     fdcount = fdsize = 0;
465
466     do {
467
468         /* Count the currently active fds. */
469         i = 0;
470         for (fd = first_fd(&fdstate, &rwx); fd >= 0;
471              fd = next_fd(&fdstate, &rwx)) i++;
472
473         if (i < 1 && !no_fds_ok)
474             return -1;                 /* doom */
475
476         /* Expand the fdlist buffer if necessary. */
477         if (i > fdsize) {
478             fdsize = i + 16;
479             fdlist = sresize(fdlist, fdsize, int);
480         }
481
482         FD_ZERO(&rset);
483         FD_ZERO(&wset);
484         FD_ZERO(&xset);
485         maxfd = 0;
486
487         /*
488          * Add all currently open fds to the select sets, and store
489          * them in fdlist as well.
490          */
491         fdcount = 0;
492         for (fd = first_fd(&fdstate, &rwx); fd >= 0;
493              fd = next_fd(&fdstate, &rwx)) {
494             fdlist[fdcount++] = fd;
495             if (rwx & 1)
496                 FD_SET_MAX(fd, maxfd, rset);
497             if (rwx & 2)
498                 FD_SET_MAX(fd, maxfd, wset);
499             if (rwx & 4)
500                 FD_SET_MAX(fd, maxfd, xset);
501         }
502
503         if (include_stdin)
504             FD_SET_MAX(0, maxfd, rset);
505
506         if (toplevel_callback_pending()) {
507             struct timeval tv;
508             tv.tv_sec = 0;
509             tv.tv_usec = 0;
510             ret = select(maxfd, &rset, &wset, &xset, &tv);
511             if (ret == 0)
512                 run_toplevel_callbacks();
513         } else if (run_timers(now, &next)) {
514             do {
515                 unsigned long then;
516                 long ticks;
517                 struct timeval tv;
518
519                 then = now;
520                 now = GETTICKCOUNT();
521                 if (now - then > next - then)
522                     ticks = 0;
523                 else
524                     ticks = next - now;
525                 tv.tv_sec = ticks / 1000;
526                 tv.tv_usec = ticks % 1000 * 1000;
527                 ret = select(maxfd, &rset, &wset, &xset, &tv);
528                 if (ret == 0)
529                     now = next;
530                 else
531                     now = GETTICKCOUNT();
532             } while (ret < 0 && errno == EINTR);
533         } else {
534             do {
535                 ret = select(maxfd, &rset, &wset, &xset, NULL);
536             } while (ret < 0 && errno == EINTR);
537         }
538     } while (ret == 0);
539
540     if (ret < 0) {
541         perror("select");
542         exit(1);
543     }
544
545     for (i = 0; i < fdcount; i++) {
546         fd = fdlist[i];
547         /*
548          * We must process exceptional notifications before
549          * ordinary readability ones, or we may go straight
550          * past the urgent marker.
551          */
552         if (FD_ISSET(fd, &xset))
553             select_result(fd, 4);
554         if (FD_ISSET(fd, &rset))
555             select_result(fd, 1);
556         if (FD_ISSET(fd, &wset))
557             select_result(fd, 2);
558     }
559
560     sfree(fdlist);
561
562     run_toplevel_callbacks();
563
564     return FD_ISSET(0, &rset) ? 1 : 0;
565 }
566
567 /*
568  * Wait for some network data and process it.
569  */
570 int ssh_sftp_loop_iteration(void)
571 {
572     return ssh_sftp_do_select(FALSE, FALSE);
573 }
574
575 /*
576  * Read a PSFTP command line from stdin.
577  */
578 char *ssh_sftp_get_cmdline(const char *prompt, int no_fds_ok)
579 {
580     char *buf;
581     int buflen, bufsize, ret;
582
583     fputs(prompt, stdout);
584     fflush(stdout);
585
586     buf = NULL;
587     buflen = bufsize = 0;
588
589     while (1) {
590         ret = ssh_sftp_do_select(TRUE, no_fds_ok);
591         if (ret < 0) {
592             printf("connection died\n");
593             sfree(buf);
594             return NULL;               /* woop woop */
595         }
596         if (ret > 0) {
597             if (buflen >= bufsize) {
598                 bufsize = buflen + 512;
599                 buf = sresize(buf, bufsize, char);
600             }
601             ret = read(0, buf+buflen, 1);
602             if (ret < 0) {
603                 perror("read");
604                 sfree(buf);
605                 return NULL;
606             }
607             if (ret == 0) {
608                 /* eof on stdin; no error, but no answer either */
609                 sfree(buf);
610                 return NULL;
611             }
612
613             if (buf[buflen++] == '\n') {
614                 /* we have a full line */
615                 return buf;
616             }
617         }
618     }
619 }
620
621 void frontend_net_error_pending(void) {}
622
623 void platform_psftp_pre_conn_setup(void) {}
624
625 /*
626  * Main program: do platform-specific initialisation and then call
627  * psftp_main().
628  */
629 int main(int argc, char *argv[])
630 {
631     uxsel_init();
632     return psftp_main(argc, argv);
633 }