* direntry.c: Use g_strlcpy instead strncpy for fix buffer overrun
[midnight-commander.git] / vfs / utilvfs.c
blob2acc3c1e45d67f7cba3a77aac658765385fbe7c2
1 /* Utilities for VFS modules.
3 Copyright (C) 1988, 1992 Free Software Foundation
4 Copyright (C) 1995, 1996 Miguel de Icaza
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License
8 as published by the Free Software Foundation; either version 2 of
9 the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <config.h>
21 #include <ctype.h>
23 #include "utilvfs.h"
24 #include "vfs.h"
26 /* Extract the hostname and username from the path */
27 /* path is in the form: [user@]hostname:port/remote-dir, e.g.:
29 * ftp://sunsite.unc.edu/pub/linux
30 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
31 * ftp://tsx-11.mit.edu:8192/
32 * ftp://joe@foo.edu:11321/private
33 * ftp://joe:password@foo.se
35 * Returns g_malloc()ed host, user and pass they are present.
36 * If the user is empty, e.g. ftp://@roxanne/private, and URL_ALLOW_ANON
37 * is not set, then the current login name is supplied.
39 * Return value is a g_malloc()ed string with the pathname relative to the
40 * host.
43 #ifdef USE_NETCODE
44 char *
45 vfs_split_url (const char *path, char **host, char **user, int *port,
46 char **pass, int default_port, int flags)
48 struct passwd *passwd_info;
49 char *dir, *colon, *inner_colon, *at, *rest;
50 char *retval;
51 char *pcopy = g_strdup (path);
52 char *pend = pcopy + strlen (pcopy);
54 if (pass)
55 *pass = NULL;
56 *port = default_port;
57 *user = NULL;
58 retval = NULL;
60 dir = pcopy;
61 if (!(flags & URL_NOSLASH)) {
62 /* locate path component */
63 while (*dir != PATH_SEP && *dir)
64 dir++;
65 if (*dir) {
66 retval = g_strdup (dir);
67 *dir = 0;
68 } else
69 retval = g_strdup (PATH_SEP_STR);
72 /* search for any possible user */
73 at = strchr (pcopy, '@');
75 /* We have a username */
76 if (at) {
77 *at = 0;
78 inner_colon = strchr (pcopy, ':');
79 if (inner_colon) {
80 *inner_colon = 0;
81 inner_colon++;
82 if (pass)
83 *pass = g_strdup (inner_colon);
85 if (*pcopy != 0)
86 *user = g_strdup (pcopy);
88 if (pend == at + 1)
89 rest = at;
90 else
91 rest = at + 1;
92 } else
93 rest = pcopy;
95 if (!*user && !(flags & URL_ALLOW_ANON)) {
96 passwd_info = getpwuid (geteuid ());
97 if (passwd_info && passwd_info->pw_name)
98 *user = g_strdup (passwd_info->pw_name);
99 else {
100 /* This is very unlikely to happen */
101 *user = g_strdup ("anonymous");
103 endpwent ();
106 /* Check if the host comes with a port spec, if so, chop it */
107 colon = strchr (rest, ':');
108 if (colon) {
109 *colon = 0;
110 if (sscanf (colon + 1, "%d", port) == 1) {
111 if (*port <= 0 || *port >= 65536)
112 *port = default_port;
113 } else {
114 while (*(++colon)) {
115 switch (*colon) {
116 case 'C':
117 *port = 1;
118 break;
119 case 'r':
120 *port = 2;
121 break;
126 if (host)
127 *host = g_strdup (rest);
129 g_free (pcopy);
130 return retval;
132 #endif /* USE_NETCODE */
135 * Look up a user or group name from a uid/gid, maintaining a cache.
136 * FIXME, for now it's a one-entry cache.
137 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
138 * This file should be modified for non-unix systems to do something
139 * reasonable.
142 #ifndef TUNMLEN
143 #define TUNMLEN 256
144 #endif
145 #ifndef TGNMLEN
146 #define TGNMLEN 256
147 #endif
149 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
150 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
153 vfs_finduid (const char *uname)
155 static int saveuid = -993;
156 static char saveuname[TUNMLEN];
157 static int my_uid = -993;
159 struct passwd *pw;
161 if (uname[0] != saveuname[0] /* Quick test w/o proc call */
162 ||0 != strncmp (uname, saveuname, TUNMLEN)) {
163 g_strlcpy (saveuname, uname, TUNMLEN);
164 pw = getpwnam (uname);
165 if (pw) {
166 saveuid = pw->pw_uid;
167 } else {
168 saveuid = myuid;
171 return saveuid;
175 vfs_findgid (const char *gname)
177 static int savegid = -993;
178 static char savegname[TGNMLEN];
179 static int my_gid = -993;
181 struct group *gr;
183 if (gname[0] != savegname[0] /* Quick test w/o proc call */
184 ||0 != strncmp (gname, savegname, TUNMLEN)) {
185 g_strlcpy (savegname, gname, TUNMLEN);
186 gr = getgrnam (gname);
187 if (gr) {
188 savegid = gr->gr_gid;
189 } else {
190 savegid = mygid;
193 return savegid;
197 * Create a temporary file with a name resembling the original.
198 * This is needed e.g. for local copies requested by extfs.
199 * Some extfs scripts may look at the extension.
200 * We also protect stupid scripts agains dangerous names.
203 vfs_mkstemps (char **pname, const char *prefix, const char *basename)
205 const unsigned char *p;
206 char *suffix, *q;
207 int shift;
208 int fd;
210 /* Strip directories */
211 p = strrchr (basename, PATH_SEP);
212 if (!p)
213 p = basename;
214 else
215 p++;
217 /* Protection against very long names */
218 shift = strlen (p) - (MC_MAXPATHLEN - 16);
219 if (shift > 0)
220 p += shift;
222 suffix = g_malloc (MC_MAXPATHLEN);
224 /* Protection against unusual characters */
225 q = suffix;
226 while (*p && (*p != '#')) {
227 if (strchr (".-_@", *p) || isalnum (*p))
228 *q++ = *p;
229 p++;
231 *q = 0;
233 fd = mc_mkstemps (pname, prefix, suffix);
234 g_free (suffix);
235 return fd;
238 /* Parsing code is used by ftpfs, fish and extfs */
239 #define MAXCOLS 30
241 static char *columns[MAXCOLS]; /* Points to the string in column n */
242 static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */
245 vfs_split_text (char *p)
247 char *original = p;
248 int numcols;
250 memset (columns, 0, sizeof (columns));
252 for (numcols = 0; *p && numcols < MAXCOLS; numcols++) {
253 while (*p == ' ' || *p == '\r' || *p == '\n') {
254 *p = 0;
255 p++;
257 columns[numcols] = p;
258 column_ptr[numcols] = p - original;
259 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
260 p++;
262 return numcols;
265 static int
266 is_num (int idx)
268 char *column = columns[idx];
270 if (!column || column[0] < '0' || column[0] > '9')
271 return 0;
273 return 1;
276 /* Return 1 for MM-DD-YY and MM-DD-YYYY */
277 static int
278 is_dos_date (const char *str)
280 int len;
282 if (!str)
283 return 0;
285 len = strlen (str);
286 if (len != 8 && len != 10)
287 return 0;
289 if (str[2] != str[5])
290 return 0;
292 if (!strchr ("\\-/", (int) str[2]))
293 return 0;
295 return 1;
298 static int
299 is_week (const char *str, struct tm *tim)
301 static const char *week = "SunMonTueWedThuFriSat";
302 char *pos;
304 if (!str)
305 return 0;
307 if ((pos = strstr (week, str)) != NULL) {
308 if (tim != NULL)
309 tim->tm_wday = (pos - week) / 3;
310 return 1;
312 return 0;
315 static int
316 is_month (const char *str, struct tm *tim)
318 static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
319 char *pos;
321 if (!str)
322 return 0;
324 if ((pos = strstr (month, str)) != NULL) {
325 if (tim != NULL)
326 tim->tm_mon = (pos - month) / 3;
327 return 1;
329 return 0;
333 * Check for possible locale's abbreviated month name (Jan..Dec).
334 * Any 3 bytes long string without digit and control characters.
335 * isalpha() is locale specific, so it cannot be used if current
336 * locale is "C" and ftp server use Cyrillic.
337 * TODO: Punctuation characters also cannot be part of month name.
338 * NB: It is assumed there are no whitespaces in month.
340 static int
341 is_localized_month (const unsigned char *month)
343 int i = 0;
344 while ((i < 3) && *month && !isdigit (*month) && !iscntrl (*month)) {
345 i++;
346 month++;
348 return ((i == 3) && (*month == 0));
351 static int
352 is_time (const char *str, struct tm *tim)
354 char *p, *p2;
356 if (!str)
357 return 0;
359 if ((p = strchr (str, ':')) && (p2 = strrchr (str, ':'))) {
360 if (p != p2) {
361 if (sscanf
362 (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min,
363 &tim->tm_sec) != 3)
364 return 0;
365 } else {
366 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
367 return 0;
369 } else
370 return 0;
372 return 1;
375 static int
376 is_year (char *str, struct tm *tim)
378 long year;
380 if (!str)
381 return 0;
383 if (strchr (str, ':'))
384 return 0;
386 if (strlen (str) != 4)
387 return 0;
389 if (sscanf (str, "%ld", &year) != 1)
390 return 0;
392 if (year < 1900 || year > 3000)
393 return 0;
395 tim->tm_year = (int) (year - 1900);
397 return 1;
401 * FIXME: this is broken. Consider following entry:
402 * -rwx------ 1 root root 1 Aug 31 10:04 2904 1234
403 * where "2904 1234" is filename. Well, this code decodes it as year :-(.
407 vfs_parse_filetype (char c)
409 switch (c) {
410 case 'd':
411 return S_IFDIR;
412 case 'b':
413 return S_IFBLK;
414 case 'c':
415 return S_IFCHR;
416 case 'l':
417 return S_IFLNK;
418 case 's': /* Socket */
419 #ifdef S_IFSOCK
420 return S_IFSOCK;
421 #else
422 /* If not supported, we fall through to IFIFO */
423 return S_IFIFO;
424 #endif
425 case 'D': /* Solaris door */
426 #ifdef S_IFDOOR
427 return S_IFDOOR;
428 #else
429 return S_IFIFO;
430 #endif
431 case 'p':
432 return S_IFIFO;
433 case 'm':
434 case 'n': /* Don't know what these are :-) */
435 case '-':
436 case '?':
437 return S_IFREG;
438 default:
439 return -1;
444 vfs_parse_filemode (const char *p)
445 { /* converts rw-rw-rw- into 0666 */
446 int res = 0;
447 switch (*(p++)) {
448 case 'r':
449 res |= 0400;
450 break;
451 case '-':
452 break;
453 default:
454 return -1;
456 switch (*(p++)) {
457 case 'w':
458 res |= 0200;
459 break;
460 case '-':
461 break;
462 default:
463 return -1;
465 switch (*(p++)) {
466 case 'x':
467 res |= 0100;
468 break;
469 case 's':
470 res |= 0100 | S_ISUID;
471 break;
472 case 'S':
473 res |= S_ISUID;
474 break;
475 case '-':
476 break;
477 default:
478 return -1;
480 switch (*(p++)) {
481 case 'r':
482 res |= 0040;
483 break;
484 case '-':
485 break;
486 default:
487 return -1;
489 switch (*(p++)) {
490 case 'w':
491 res |= 0020;
492 break;
493 case '-':
494 break;
495 default:
496 return -1;
498 switch (*(p++)) {
499 case 'x':
500 res |= 0010;
501 break;
502 case 's':
503 res |= 0010 | S_ISGID;
504 break;
505 case 'l': /* Solaris produces these */
506 case 'S':
507 res |= S_ISGID;
508 break;
509 case '-':
510 break;
511 default:
512 return -1;
514 switch (*(p++)) {
515 case 'r':
516 res |= 0004;
517 break;
518 case '-':
519 break;
520 default:
521 return -1;
523 switch (*(p++)) {
524 case 'w':
525 res |= 0002;
526 break;
527 case '-':
528 break;
529 default:
530 return -1;
532 switch (*(p++)) {
533 case 'x':
534 res |= 0001;
535 break;
536 case 't':
537 res |= 0001 | S_ISVTX;
538 break;
539 case 'T':
540 res |= S_ISVTX;
541 break;
542 case '-':
543 break;
544 default:
545 return -1;
547 return res;
550 /* This function parses from idx in the columns[] array */
552 vfs_parse_filedate (int idx, time_t *t)
554 char *p;
555 struct tm tim;
556 int d[3];
557 int got_year = 0;
558 int l10n = 0; /* Locale's abbreviated month name */
559 time_t current_time;
560 struct tm *local_time;
562 /* Let's setup default time values */
563 current_time = time (NULL);
564 local_time = localtime (&current_time);
565 tim.tm_mday = local_time->tm_mday;
566 tim.tm_mon = local_time->tm_mon;
567 tim.tm_year = local_time->tm_year;
569 tim.tm_hour = 0;
570 tim.tm_min = 0;
571 tim.tm_sec = 0;
572 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
574 p = columns[idx++];
576 /* We eat weekday name in case of extfs */
577 if (is_week (p, &tim))
578 p = columns[idx++];
580 /* Month name */
581 if (is_month (p, &tim)) {
582 /* And we expect, it followed by day number */
583 if (is_num (idx))
584 tim.tm_mday = (int) atol (columns[idx++]);
585 else
586 return 0; /* No day */
588 } else {
589 /* We usually expect:
590 Mon DD hh:mm
591 Mon DD YYYY
592 But in case of extfs we allow these date formats:
593 Mon DD YYYY hh:mm
594 Mon DD hh:mm YYYY
595 Wek Mon DD hh:mm:ss YYYY
596 MM-DD-YY hh:mm
597 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
598 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
600 /* Special case with MM-DD-YY or MM-DD-YYYY */
601 if (is_dos_date (p)) {
602 p[2] = p[5] = '-';
604 if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) {
605 /* Months are zero based */
606 if (d[0] > 0)
607 d[0]--;
609 if (d[2] > 1900) {
610 d[2] -= 1900;
611 } else {
612 /* Y2K madness */
613 if (d[2] < 70)
614 d[2] += 100;
617 tim.tm_mon = d[0];
618 tim.tm_mday = d[1];
619 tim.tm_year = d[2];
620 got_year = 1;
621 } else
622 return 0; /* sscanf failed */
623 } else {
624 /* Locale's abbreviated month name followed by day number */
625 if (is_localized_month (p) && (is_num (idx++)))
626 l10n = 1;
627 else
628 return 0; /* unsupported format */
632 /* Here we expect to find time and/or year */
634 if (is_num (idx)) {
635 if (is_time (columns[idx], &tim)
636 || (got_year = is_year (columns[idx], &tim))) {
637 idx++;
639 /* This is a special case for ctime() or Mon DD YYYY hh:mm */
640 if (is_num (idx) && (columns[idx + 1][0])) {
641 if (got_year) {
642 if (is_time (columns[idx], &tim))
643 idx++; /* time also */
644 } else {
645 if ((got_year = is_year (columns[idx], &tim)))
646 idx++; /* year also */
649 } /* only time or date */
650 } else
651 return 0; /* Nor time or date */
654 * If the date is less than 6 months in the past, it is shown without year
655 * other dates in the past or future are shown with year but without time
656 * This does not check for years before 1900 ... I don't know, how
657 * to represent them at all
659 if (!got_year && local_time->tm_mon < 6
660 && local_time->tm_mon < tim.tm_mon
661 && tim.tm_mon - local_time->tm_mon >= 6)
663 tim.tm_year--;
665 if (l10n || (*t = mktime (&tim)) < 0)
666 *t = 0;
667 return idx;
671 vfs_parse_ls_lga (const char *p, struct stat *s, char **filename,
672 char **linkname)
674 int idx, idx2, num_cols;
675 int i;
676 char *p_copy = NULL;
677 char *t = NULL;
678 const char *line = p;
680 if (strncmp (p, "total", 5) == 0)
681 return 0;
683 if ((i = vfs_parse_filetype (*(p++))) == -1)
684 goto error;
686 s->st_mode = i;
687 if (*p == ' ') /* Notwell 4 */
688 p++;
689 if (*p == '[') {
690 if (strlen (p) <= 8 || p[8] != ']')
691 goto error;
692 /* Should parse here the Notwell permissions :) */
693 if (S_ISDIR (s->st_mode))
694 s->st_mode |=
695 (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP
696 | S_IXOTH);
697 else
698 s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
699 p += 9;
700 } else {
701 if ((i = vfs_parse_filemode (p)) == -1)
702 goto error;
703 s->st_mode |= i;
704 p += 9;
706 /* This is for an extra ACL attribute (HP-UX) */
707 if (*p == '+')
708 p++;
711 p_copy = g_strdup (p);
712 num_cols = vfs_split_text (p_copy);
714 s->st_nlink = atol (columns[0]);
715 if (s->st_nlink <= 0)
716 goto error;
718 if (!is_num (1))
719 s->st_uid = vfs_finduid (columns[1]);
720 else
721 s->st_uid = (uid_t) atol (columns[1]);
723 /* Mhm, the ls -lg did not produce a group field */
724 for (idx = 3; idx <= 5; idx++)
725 if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
726 || is_dos_date (columns[idx])
727 || is_localized_month (columns[idx]))
728 break;
730 if (idx == 6
731 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
732 goto error;
734 /* We don't have gid */
735 if (idx == 3
736 || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
737 idx2 = 2;
738 else {
739 /* We have gid field */
740 if (is_num (2))
741 s->st_gid = (gid_t) atol (columns[2]);
742 else
743 s->st_gid = vfs_findgid (columns[2]);
744 idx2 = 3;
747 /* This is device */
748 if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)) {
749 int maj, min;
751 /* Corner case: there is no whitespace(s) between maj & min */
752 if (!is_num (idx2) && idx2 == 2) {
753 if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &min, &min) != 2)
754 goto error;
755 } else {
756 if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
757 goto error;
759 if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
760 goto error;
762 #ifdef HAVE_STRUCT_STAT_ST_RDEV
763 s->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
764 #endif
765 s->st_size = 0;
767 } else {
768 /* Common file size */
769 if (!is_num (idx2))
770 goto error;
772 s->st_size = (size_t) atol (columns[idx2]);
773 #ifdef HAVE_STRUCT_STAT_ST_RDEV
774 s->st_rdev = 0;
775 #endif
778 idx = vfs_parse_filedate (idx, &s->st_mtime);
779 if (!idx)
780 goto error;
781 /* Use resulting time value */
782 s->st_atime = s->st_ctime = s->st_mtime;
783 /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
784 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
785 s->st_blksize = 512;
786 #endif
787 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
788 s->st_blocks = (s->st_size + 511) / 512;
789 #endif
791 for (i = idx + 1, idx2 = 0; i < num_cols; i++)
792 if (strcmp (columns[i], "->") == 0) {
793 idx2 = i;
794 break;
797 if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
798 &&idx2) {
800 if (filename) {
801 *filename =
802 g_strndup (p + column_ptr[idx],
803 column_ptr[idx2] - column_ptr[idx] - 1);
805 if (linkname) {
806 t = g_strdup (p + column_ptr[idx2 + 1]);
807 *linkname = t;
809 } else {
810 /* Extract the filename from the string copy, not from the columns
811 * this way we have a chance of entering hidden directories like ". ."
813 if (filename) {
815 * filename = g_strdup (columns [idx++]);
818 t = g_strdup (p + column_ptr[idx]);
819 *filename = t;
821 if (linkname)
822 *linkname = NULL;
825 if (t) {
826 int p = strlen (t);
827 if ((--p > 0) && (t[p] == '\r' || t[p] == '\n'))
828 t[p] = 0;
829 if ((--p > 0) && (t[p] == '\r' || t[p] == '\n'))
830 t[p] = 0;
833 g_free (p_copy);
834 return 1;
836 error:
838 static int errorcount = 0;
840 if (++errorcount < 5) {
841 message (1, _("Cannot parse:"), "%s",
842 (p_copy && *p_copy) ? p_copy : line);
843 } else if (errorcount == 5)
844 message (1, MSG_ERROR,
845 _("More parsing errors will be ignored."));
848 g_free (p_copy);
849 return 0;
852 void
853 vfs_die (const char *m)
855 message (1, _("Internal error:"), "%s", m);
856 exit (1);
859 char *
860 vfs_get_password (const char *msg)
862 return input_dialog (msg, _("Password:"), INPUT_PASSWORD);