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