123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- #include <config.h>
- #include <ctype.h>
- #include <sys/types.h>
- #include <pwd.h>
- #include <grp.h>
- #include <stdlib.h>
- #include <string.h>
- #include "lib/global.h"
- #include "lib/unixcompat.h"
- #include "lib/util.h"
- #include "lib/widget.h"
- #include "lib/strutil.h"
- #include "vfs.h"
- #include "utilvfs.h"
- #ifndef TUNMLEN
- #define TUNMLEN 256
- #endif
- #ifndef TGNMLEN
- #define TGNMLEN 256
- #endif
- #define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
- #define GUID_DEFAULT_CONST -993
- char *
- vfs_get_local_username (void)
- {
- struct passwd *p_i;
- p_i = getpwuid (geteuid ());
- return (p_i && p_i->pw_name) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous");
- }
- int
- vfs_finduid (const char *uname)
- {
- static int saveuid = GUID_DEFAULT_CONST;
- static char saveuname[TUNMLEN] = "\0";
- size_t uname_len;
- uname_len = strlen (uname);
- if (uname[0] != saveuname[0]
- || strncmp (uname, saveuname, MIN (uname_len, TUNMLEN - 1)) != 0)
- {
- struct passwd *pw;
- g_strlcpy (saveuname, uname, TUNMLEN);
- pw = getpwnam (uname);
- if (pw)
- {
- saveuid = pw->pw_uid;
- }
- else
- {
- static int my_uid = GUID_DEFAULT_CONST;
- if (my_uid < 0)
- my_uid = getuid ();
- saveuid = my_uid;
- }
- }
- return saveuid;
- }
- int
- vfs_findgid (const char *gname)
- {
- static int savegid = GUID_DEFAULT_CONST;
- static char savegname[TGNMLEN] = "\0";
- size_t gname_len;
- gname_len = strlen (gname);
- if (gname[0] != savegname[0]
- || strncmp (gname, savegname, MIN (gname_len, TGNMLEN - 1)) != 0)
- {
- struct group *gr;
- g_strlcpy (savegname, gname, TGNMLEN);
- gr = getgrnam (gname);
- if (gr)
- {
- savegid = gr->gr_gid;
- }
- else
- {
- static int my_gid = GUID_DEFAULT_CONST;
- if (my_gid < 0)
- my_gid = getgid ();
- savegid = my_gid;
- }
- }
- return savegid;
- }
- int
- vfs_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *param_basename)
- {
- const char *p;
- GString *suffix;
- int shift;
- int fd;
-
- p = strrchr (param_basename, PATH_SEP);
- if (p == NULL)
- p = param_basename;
- else
- p++;
-
- shift = strlen (p) - (MC_MAXPATHLEN - 16);
- if (shift > 0)
- p += shift;
- suffix = g_string_sized_new (32);
-
- for (; *p != '\0' && *p != '#'; p++)
- if (strchr (".-_@", *p) != NULL || g_ascii_isalnum (*p))
- g_string_append_c (suffix, *p);
- fd = mc_mkstemps (pname_vpath, prefix, suffix->str);
- g_string_free (suffix, TRUE);
- return fd;
- }
- vfs_path_element_t *
- vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags)
- {
- vfs_path_element_t *path_element;
- char *pcopy;
- size_t pcopy_len;
- const char *pend;
- char *colon, *at, *rest;
- path_element = g_new0 (vfs_path_element_t, 1);
- path_element->port = default_port;
- pcopy_len = strlen (path);
- pcopy = g_strndup (path, pcopy_len);
- pend = pcopy + pcopy_len;
- if ((flags & URL_NOSLASH) == 0)
- {
- char *dir = pcopy;
-
- while (!IS_PATH_SEP (*dir) && *dir != '\0')
- dir++;
- if (*dir == '\0')
- path_element->path = g_strdup (PATH_SEP_STR);
- else
- {
- path_element->path = g_strndup (dir, pcopy_len - (size_t) (dir - pcopy));
- *dir = '\0';
- }
- }
-
- at = strrchr (pcopy, '@');
-
- if (at == NULL)
- rest = pcopy;
- else
- {
- char *inner_colon;
- *at = '\0';
- inner_colon = strchr (pcopy, ':');
- if (inner_colon != NULL)
- {
- *inner_colon = '\0';
- inner_colon++;
- path_element->password = g_strdup (inner_colon);
- }
- if (*pcopy != '\0')
- path_element->user = g_strdup (pcopy);
- if (pend == at + 1)
- rest = at;
- else
- rest = at + 1;
- }
- if ((flags & URL_USE_ANONYMOUS) == 0)
- {
- g_free (path_element->user);
- path_element->user = vfs_get_local_username ();
- }
-
- if (*rest != '[')
- colon = strchr (rest, ':');
- else
- {
- colon = strchr (++rest, ']');
- if (colon != NULL)
- {
- colon[0] = '\0';
- colon[1] = '\0';
- colon++;
- }
- else
- {
- vfs_path_element_free (path_element);
- g_free (pcopy);
- return NULL;
- }
- }
- if (colon != NULL)
- {
- *colon = '\0';
-
- if (sscanf (colon + 1, "%d", &path_element->port) == 1)
- {
- if (path_element->port <= 0 || path_element->port >= 65536)
- path_element->port = default_port;
- }
- else
- while (*(++colon) != '\0')
- {
- switch (*colon)
- {
- case 'C':
- path_element->port = 1;
- break;
- case 'r':
- path_element->port = 2;
- break;
- default:
- break;
- }
- }
- }
- path_element->host = g_strdup (rest);
- g_free (pcopy);
- #ifdef HAVE_CHARSET
- path_element->dir.converter = INVALID_CONV;
- #endif
- return path_element;
- }
- void __attribute__ ((noreturn)) vfs_die (const char *m)
- {
- message (D_ERROR, _("Internal error:"), "%s", m);
- exit (EXIT_FAILURE);
- }
- char *
- vfs_get_password (const char *msg)
- {
- return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD,
- INPUT_COMPLETE_NONE);
- }
|