]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxsftp.c
Some systems (HP-UX) don't yet have <sys/select.h>, putting select() in
[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 "psftp.h"
22
23 /*
24  * In PSFTP our selects are synchronous, so these functions are
25  * empty stubs.
26  */
27 int uxsel_input_add(int fd, int rwx) { return 0; }
28 void uxsel_input_remove(int id) { }
29
30 char *x_get_default(const char *key)
31 {
32     return NULL;                       /* this is a stub */
33 }
34
35 void platform_get_x11_auth(char *display, int *protocol,
36                            unsigned char *data, int *datalen)
37 {
38     /* Do nothing, therefore no auth. */
39 }
40
41 /*
42  * Default settings that are specific to PSFTP.
43  */
44 char *platform_default_s(const char *name)
45 {
46     return NULL;
47 }
48
49 int platform_default_i(const char *name, int def)
50 {
51     return def;
52 }
53
54 FontSpec platform_default_fontspec(const char *name)
55 {
56     FontSpec ret;
57     *ret.name = '\0';
58     return ret;
59 }
60
61 Filename platform_default_filename(const char *name)
62 {
63     Filename ret;
64     if (!strcmp(name, "LogFileName"))
65         strcpy(ret.path, "putty.log");
66     else
67         *ret.path = '\0';
68     return ret;
69 }
70
71 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
72
73 /*
74  * Stubs for the GUI feedback mechanism in Windows PSCP.
75  */
76 void gui_update_stats(char *name, unsigned long size,
77                       int percentage, unsigned long elapsed,
78                       unsigned long done, unsigned long eta,
79                       unsigned long ratebs) {}
80 void gui_send_errcount(int list, int errs) {}
81 void gui_send_char(int is_stderr, int c) {}
82 void gui_enable(char *arg) {}
83
84
85 /*
86  * Set local current directory. Returns NULL on success, or else an
87  * error message which must be freed after printing.
88  */
89 char *psftp_lcd(char *dir)
90 {
91     if (chdir(dir) < 0)
92         return dupprintf("%s: chdir: %s", dir, strerror(errno));
93     else
94         return NULL;
95 }
96
97 /*
98  * Get local current directory. Returns a string which must be
99  * freed.
100  */
101 char *psftp_getcwd(void)
102 {
103     char *buffer, *ret;
104     int size = 256;
105
106     buffer = snewn(size, char);
107     while (1) {
108         ret = getcwd(buffer, size);
109         if (ret != NULL)
110             return ret;
111         if (errno != ERANGE) {
112             sfree(buffer);
113             return dupprintf("[cwd unavailable: %s]", strerror(errno));
114         }
115         /*
116          * Otherwise, ERANGE was returned, meaning the buffer
117          * wasn't big enough.
118          */
119         size = size * 3 / 2;
120         buffer = sresize(buffer, size, char);
121     }
122 }
123
124 struct RFile {
125     int fd;
126 };
127
128 RFile *open_existing_file(char *name, unsigned long *size,
129                           unsigned long *mtime, unsigned long *atime)
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) {
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 = statbuf.st_size;
150
151         if (mtime)
152             *mtime = statbuf.st_mtime;
153
154         if (atime)
155             *atime = statbuf.st_atime;
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)
178 {
179     int fd;
180     WFile *ret;
181
182     fd = open(name, O_CREAT | O_TRUNC | O_WRONLY, 0666);
183     if (fd < 0)
184         return NULL;
185
186     ret = snew(WFile);
187     ret->fd = fd;
188     ret->name = dupstr(name);
189
190     return ret;
191 }
192
193 int write_to_file(WFile *f, void *buffer, int length)
194 {
195     char *p = (char *)buffer;
196     int so_far = 0;
197
198     /* Keep trying until we've really written as much as we can. */
199     while (length > 0) {
200         int ret = write(f->fd, p, length);
201
202         if (ret < 0)
203             return ret;
204
205         if (ret == 0)
206             break;
207
208         p += ret;
209         length -= ret;
210         so_far += ret;
211     }
212
213     return so_far;
214 }
215
216 void set_file_times(WFile *f, unsigned long mtime, unsigned long atime)
217 {
218     struct utimbuf ut;
219
220     ut.actime = atime;
221     ut.modtime = mtime;
222
223     utime(f->name, &ut);
224 }
225
226 /* Closes and frees the WFile */
227 void close_wfile(WFile *f)
228 {
229     close(f->fd);
230     sfree(f->name);
231     sfree(f);
232 }
233
234 int file_type(char *name)
235 {
236     struct stat statbuf;
237
238     if (stat(name, &statbuf) < 0) {
239         if (errno != ENOENT)
240             fprintf(stderr, "%s: stat: %s\n", name, strerror(errno));
241         return FILE_TYPE_NONEXISTENT;
242     }
243
244     if (S_ISREG(statbuf.st_mode))
245         return FILE_TYPE_FILE;
246
247     if (S_ISDIR(statbuf.st_mode))
248         return FILE_TYPE_DIRECTORY;
249
250     return FILE_TYPE_WEIRD;
251 }
252
253 struct DirHandle {
254     DIR *dir;
255 };
256
257 DirHandle *open_directory(char *name)
258 {
259     DIR *dir;
260     DirHandle *ret;
261
262     dir = opendir(name);
263     if (!dir)
264         return NULL;
265
266     ret = snew(DirHandle);
267     ret->dir = dir;
268     return ret;
269 }
270
271 char *read_filename(DirHandle *dir)
272 {
273     struct dirent *de;
274
275     do {
276         de = readdir(dir->dir);
277         if (de == NULL)
278             return NULL;
279     } while ((de->d_name[0] == '.' &&
280               (de->d_name[1] == '\0' ||
281                (de->d_name[1] == '.' && de->d_name[2] == '\0'))));
282
283     return dupstr(de->d_name);
284 }
285
286 void close_directory(DirHandle *dir)
287 {
288     closedir(dir->dir);
289     sfree(dir);
290 }
291
292 int test_wildcard(char *name, int cmdline)
293 {
294     struct stat statbuf;
295
296     if (stat(name, &statbuf) == 0) {
297         return WCTYPE_FILENAME;
298     } else if (cmdline) {
299         /*
300          * On Unix, we never need to parse wildcards coming from
301          * the command line, because the shell will have expanded
302          * them into a filename list already.
303          */
304         return WCTYPE_NONEXISTENT;
305     } else {
306         glob_t globbed;
307         int ret = WCTYPE_NONEXISTENT;
308
309         if (glob(name, GLOB_ERR, NULL, &globbed) == 0) {
310             if (globbed.gl_pathc > 0)
311                 ret = WCTYPE_WILDCARD;
312             globfree(&globbed);
313         }
314
315         return ret;
316     }
317 }
318
319 /*
320  * Actually return matching file names for a local wildcard.
321  */
322 struct WildcardMatcher {
323     glob_t globbed;
324     int i;
325 };
326 WildcardMatcher *begin_wildcard_matching(char *name) {
327     WildcardMatcher *ret = snew(WildcardMatcher);
328
329     if (glob(name, 0, NULL, &ret->globbed) < 0) {
330         sfree(ret);
331         return NULL;
332     }
333
334     ret->i = 0;
335
336     return ret;
337 }
338 char *wildcard_get_filename(WildcardMatcher *dir) {
339     if (dir->i < dir->globbed.gl_pathc) {
340         return dupstr(dir->globbed.gl_pathv[dir->i++]);
341     } else
342         return NULL;
343 }
344 void finish_wildcard_matching(WildcardMatcher *dir) {
345     globfree(&dir->globbed);
346     sfree(dir);
347 }
348
349 int vet_filename(char *name)
350 {
351     if (strchr(name, '/'))
352         return FALSE;
353
354     if (name[0] == '.' && (!name[1] || (name[1] == '.' && !name[2])))
355         return FALSE;
356
357     return TRUE;
358 }
359
360 int create_directory(char *name)
361 {
362     return mkdir(name, 0777) == 0;
363 }
364
365 char *dir_file_cat(char *dir, char *file)
366 {
367     return dupcat(dir, "/", file, NULL);
368 }
369
370 /*
371  * Do a select() between all currently active network fds and
372  * optionally stdin.
373  */
374 static int ssh_sftp_do_select(int include_stdin, int no_fds_ok)
375 {
376     fd_set rset, wset, xset;
377     int i, fdcount, fdsize, *fdlist;
378     int fd, fdstate, rwx, ret, maxfd;
379     long now = GETTICKCOUNT();
380
381     fdlist = NULL;
382     fdcount = fdsize = 0;
383
384     do {
385
386         /* Count the currently active fds. */
387         i = 0;
388         for (fd = first_fd(&fdstate, &rwx); fd >= 0;
389              fd = next_fd(&fdstate, &rwx)) i++;
390
391         if (i < 1 && !no_fds_ok)
392             return -1;                 /* doom */
393
394         /* Expand the fdlist buffer if necessary. */
395         if (i > fdsize) {
396             fdsize = i + 16;
397             fdlist = sresize(fdlist, fdsize, int);
398         }
399
400         FD_ZERO(&rset);
401         FD_ZERO(&wset);
402         FD_ZERO(&xset);
403         maxfd = 0;
404
405         /*
406          * Add all currently open fds to the select sets, and store
407          * them in fdlist as well.
408          */
409         fdcount = 0;
410         for (fd = first_fd(&fdstate, &rwx); fd >= 0;
411              fd = next_fd(&fdstate, &rwx)) {
412             fdlist[fdcount++] = fd;
413             if (rwx & 1)
414                 FD_SET_MAX(fd, maxfd, rset);
415             if (rwx & 2)
416                 FD_SET_MAX(fd, maxfd, wset);
417             if (rwx & 4)
418                 FD_SET_MAX(fd, maxfd, xset);
419         }
420
421         if (include_stdin)
422             FD_SET_MAX(0, maxfd, rset);
423
424         do {
425             long next, ticks;
426             struct timeval tv, *ptv;
427
428             if (run_timers(now, &next)) {
429                 ticks = next - GETTICKCOUNT();
430                 if (ticks <= 0)
431                     ticks = 1;         /* just in case */
432                 tv.tv_sec = ticks / 1000;
433                 tv.tv_usec = ticks % 1000 * 1000;
434                 ptv = &tv;
435             } else {
436                 ptv = NULL;
437             }
438             ret = select(maxfd, &rset, &wset, &xset, ptv);
439             if (ret == 0)
440                 now = next;
441             else {
442                 long newnow = GETTICKCOUNT();
443                 /*
444                  * Check to see whether the system clock has
445                  * changed massively during the select.
446                  */
447                 if (newnow - now < 0 || newnow - now > next - now) {
448                     /*
449                      * If so, look at the elapsed time in the
450                      * select and use it to compute a new
451                      * tickcount_offset.
452                      */
453                     long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
454                     /* So we'd like GETTICKCOUNT to have returned othernow,
455                      * but instead it return newnow. Hence ... */
456                     tickcount_offset += othernow - newnow;
457                     now = othernow;
458                 } else {
459                     now = newnow;
460                 }
461             }
462         } while (ret < 0 && errno != EINTR);
463     } while (ret == 0);
464
465     if (ret < 0) {
466         perror("select");
467         exit(1);
468     }
469
470     for (i = 0; i < fdcount; i++) {
471         fd = fdlist[i];
472         /*
473          * We must process exceptional notifications before
474          * ordinary readability ones, or we may go straight
475          * past the urgent marker.
476          */
477         if (FD_ISSET(fd, &xset))
478             select_result(fd, 4);
479         if (FD_ISSET(fd, &rset))
480             select_result(fd, 1);
481         if (FD_ISSET(fd, &wset))
482             select_result(fd, 2);
483     }
484
485     sfree(fdlist);
486
487     return FD_ISSET(0, &rset) ? 1 : 0;
488 }
489
490 /*
491  * Wait for some network data and process it.
492  */
493 int ssh_sftp_loop_iteration(void)
494 {
495     return ssh_sftp_do_select(FALSE, FALSE);
496 }
497
498 /*
499  * Read a PSFTP command line from stdin.
500  */
501 char *ssh_sftp_get_cmdline(char *prompt, int no_fds_ok)
502 {
503     char *buf;
504     int buflen, bufsize, ret;
505
506     fputs(prompt, stdout);
507     fflush(stdout);
508
509     buf = NULL;
510     buflen = bufsize = 0;
511
512     while (1) {
513         ret = ssh_sftp_do_select(TRUE, no_fds_ok);
514         if (ret < 0) {
515             printf("connection died\n");
516             return NULL;               /* woop woop */
517         }
518         if (ret > 0) {
519             if (buflen >= bufsize) {
520                 bufsize = buflen + 512;
521                 buf = sresize(buf, bufsize, char);
522             }
523             ret = read(0, buf+buflen, 1);
524             if (ret < 0) {
525                 perror("read");
526                 return NULL;
527             }
528             if (ret == 0) {
529                 /* eof on stdin; no error, but no answer either */
530                 return NULL;
531             }
532
533             if (buf[buflen++] == '\n') {
534                 /* we have a full line */
535                 return buf;
536             }
537         }
538     }
539 }
540
541 /*
542  * Main program: do platform-specific initialisation and then call
543  * psftp_main().
544  */
545 int main(int argc, char *argv[])
546 {
547     uxsel_init();
548     return psftp_main(argc, argv);
549 }