2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Utilities for VFS modules.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2020-01-18 19:54:37 +03:00
|
|
|
Copyright (C) 1988-2020
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2011-10-15 14:56:47 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
Copyright (C) 1995, 1996 Miguel de Icaza
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
1998-02-27 07:54:42 +03:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-10-15 14:56:47 +04:00
|
|
|
GNU General Public License for more details.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
1998-09-27 23:27:58 +04:00
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief Source: Utilities for VFS modules
|
|
|
|
* \author Miguel de Icaza
|
|
|
|
* \date 1995, 1996
|
|
|
|
*/
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <config.h>
|
2009-01-30 22:10:40 +03:00
|
|
|
|
2003-10-29 03:50:36 +03:00
|
|
|
#include <ctype.h>
|
2010-07-10 14:01:19 +04:00
|
|
|
#include <sys/types.h>
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <stdlib.h>
|
2010-07-10 14:01:19 +04:00
|
|
|
#include <string.h>
|
2009-02-06 02:30:45 +03:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2010-01-21 13:47:01 +03:00
|
|
|
#include "lib/unixcompat.h"
|
2010-11-11 16:58:29 +03:00
|
|
|
#include "lib/util.h" /* mc_mkstemps() */
|
2010-11-12 11:03:57 +03:00
|
|
|
#include "lib/widget.h" /* message() */
|
2011-10-27 15:22:14 +04:00
|
|
|
#include "lib/strutil.h" /* INVALID_CONV */
|
2009-05-08 14:01:05 +04:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
#include "vfs.h"
|
2009-05-08 14:01:05 +04:00
|
|
|
#include "utilvfs.h"
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
#ifndef TUNMLEN
|
|
|
|
#define TUNMLEN 256
|
|
|
|
#endif
|
|
|
|
#ifndef TGNMLEN
|
|
|
|
#define TGNMLEN 256
|
|
|
|
#endif
|
|
|
|
|
2011-02-10 18:02:54 +03:00
|
|
|
#define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
|
|
|
|
|
2013-10-15 10:34:04 +04:00
|
|
|
/*
|
|
|
|
* FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
|
|
|
|
*/
|
|
|
|
#define GUID_DEFAULT_CONST -993
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-12-25 18:03:23 +03:00
|
|
|
/** Get current username
|
|
|
|
*
|
|
|
|
* @return g_malloc()ed string with the name of the currently logged in
|
|
|
|
* user ("anonymous" if uid is not registered in the system)
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2009-12-25 18:03:23 +03:00
|
|
|
char *
|
2010-11-08 13:21:45 +03:00
|
|
|
vfs_get_local_username (void)
|
2009-12-25 18:03:23 +03:00
|
|
|
{
|
2010-11-08 13:21:45 +03:00
|
|
|
struct passwd *p_i;
|
2009-12-25 18:03:23 +03:00
|
|
|
|
|
|
|
p_i = getpwuid (geteuid ());
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
return (p_i && p_i->pw_name) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous"); /* Unknown UID, strange */
|
2009-12-25 18:03:23 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
2003-10-16 20:50:09 +04:00
|
|
|
* Look up a user or group name from a uid/gid, maintaining a cache.
|
|
|
|
* FIXME, for now it's a one-entry cache.
|
|
|
|
* This file should be modified for non-unix systems to do something
|
|
|
|
* reasonable.
|
|
|
|
*/
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-10-16 20:50:09 +04:00
|
|
|
|
|
|
|
int
|
2004-08-17 13:48:15 +04:00
|
|
|
vfs_finduid (const char *uname)
|
2003-10-16 20:50:09 +04:00
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
static int saveuid = GUID_DEFAULT_CONST;
|
|
|
|
static char saveuname[TUNMLEN] = "\0";
|
2003-10-16 20:50:09 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (uname[0] != saveuname[0] /* Quick test w/o proc call */
|
|
|
|
|| 0 != strncmp (uname, saveuname, TUNMLEN))
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
struct passwd *pw;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
g_strlcpy (saveuname, uname, TUNMLEN);
|
|
|
|
pw = getpwnam (uname);
|
|
|
|
if (pw)
|
|
|
|
{
|
|
|
|
saveuid = pw->pw_uid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
static int my_uid = GUID_DEFAULT_CONST;
|
|
|
|
|
|
|
|
if (my_uid < 0)
|
|
|
|
my_uid = getuid ();
|
|
|
|
|
|
|
|
saveuid = my_uid;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
2003-10-16 20:50:09 +04:00
|
|
|
}
|
|
|
|
return saveuid;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2003-10-16 20:50:09 +04:00
|
|
|
int
|
2004-08-17 13:48:15 +04:00
|
|
|
vfs_findgid (const char *gname)
|
2003-10-16 20:50:09 +04:00
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
static int savegid = GUID_DEFAULT_CONST;
|
|
|
|
static char savegname[TGNMLEN] = "\0";
|
2003-10-16 20:50:09 +04:00
|
|
|
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (gname[0] != savegname[0] /* Quick test w/o proc call */
|
|
|
|
|| 0 != strncmp (gname, savegname, TUNMLEN))
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
struct group *gr;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
g_strlcpy (savegname, gname, TUNMLEN);
|
|
|
|
gr = getgrnam (gname);
|
|
|
|
if (gr)
|
|
|
|
{
|
|
|
|
savegid = gr->gr_gid;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
static int my_gid = GUID_DEFAULT_CONST;
|
|
|
|
|
|
|
|
if (my_gid < 0)
|
|
|
|
my_gid = getgid ();
|
|
|
|
|
|
|
|
savegid = my_gid;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
2003-10-16 20:50:09 +04:00
|
|
|
}
|
|
|
|
return savegid;
|
|
|
|
}
|
2003-10-29 03:50:36 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
2003-10-29 03:50:36 +03:00
|
|
|
* Create a temporary file with a name resembling the original.
|
|
|
|
* This is needed e.g. for local copies requested by extfs.
|
|
|
|
* Some extfs scripts may look at the extension.
|
|
|
|
* We also protect stupid scripts agains dangerous names.
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2003-10-29 03:50:36 +03:00
|
|
|
int
|
2012-01-04 16:34:05 +04:00
|
|
|
vfs_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *param_basename)
|
2003-10-29 03:50:36 +03:00
|
|
|
{
|
2005-05-11 03:21:42 +04:00
|
|
|
const char *p;
|
2017-12-02 11:27:19 +03:00
|
|
|
GString *suffix;
|
2003-10-29 03:50:36 +03:00
|
|
|
int shift;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Strip directories */
|
2009-10-30 04:12:04 +03:00
|
|
|
p = strrchr (param_basename, PATH_SEP);
|
2017-12-02 11:27:19 +03:00
|
|
|
if (p == NULL)
|
2010-11-08 13:21:45 +03:00
|
|
|
p = param_basename;
|
2003-10-29 03:50:36 +03:00
|
|
|
else
|
2010-11-08 13:21:45 +03:00
|
|
|
p++;
|
2003-10-29 03:50:36 +03:00
|
|
|
|
|
|
|
/* Protection against very long names */
|
|
|
|
shift = strlen (p) - (MC_MAXPATHLEN - 16);
|
|
|
|
if (shift > 0)
|
2010-11-08 13:21:45 +03:00
|
|
|
p += shift;
|
2003-10-29 03:50:36 +03:00
|
|
|
|
2017-12-02 11:27:19 +03:00
|
|
|
suffix = g_string_sized_new (32);
|
2003-10-29 03:50:36 +03:00
|
|
|
|
|
|
|
/* Protection against unusual characters */
|
2017-12-02 11:27:19 +03:00
|
|
|
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);
|
2003-10-29 03:50:36 +03:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
2003-11-05 09:08:16 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-07-10 14:01:19 +04:00
|
|
|
/** Extract the hostname and username from the path
|
|
|
|
*
|
|
|
|
* Format of the path is [user@]hostname:port/remote-dir, e.g.:
|
|
|
|
*
|
|
|
|
* ftp://sunsite.unc.edu/pub/linux
|
|
|
|
* ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
|
|
|
|
* ftp://tsx-11.mit.edu:8192/
|
|
|
|
* ftp://joe@foo.edu:11321/private
|
|
|
|
* ftp://joe:password@foo.se
|
|
|
|
*
|
|
|
|
* @param path is an input string to be parsed
|
|
|
|
* @param default_port is an input default port
|
2011-03-29 11:20:34 +04:00
|
|
|
* @param flags are parsing modifier flags (@see vfs_url_flags_t)
|
2010-07-10 14:01:19 +04:00
|
|
|
*
|
2011-03-29 11:20:34 +04:00
|
|
|
* @return g_malloc()ed url info.
|
2010-07-10 14:01:19 +04:00
|
|
|
* If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
|
|
|
|
* is not set, then the current login name is supplied.
|
2011-03-29 11:20:34 +04:00
|
|
|
* Return value is a g_malloc()ed structure with the pathname relative to the
|
2010-07-10 14:01:19 +04:00
|
|
|
* host.
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-06-07 14:18:08 +04:00
|
|
|
vfs_path_element_t *
|
2011-03-29 11:20:34 +04:00
|
|
|
vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags)
|
2010-07-10 14:01:19 +04:00
|
|
|
{
|
2011-06-07 14:18:08 +04:00
|
|
|
vfs_path_element_t *path_element;
|
2011-03-29 11:20:34 +04:00
|
|
|
|
|
|
|
char *pcopy;
|
2012-04-03 16:48:55 +04:00
|
|
|
size_t pcopy_len;
|
2011-03-29 11:20:34 +04:00
|
|
|
const char *pend;
|
2018-01-13 00:54:34 +03:00
|
|
|
char *colon, *at, *rest;
|
2010-07-10 14:01:19 +04:00
|
|
|
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element = g_new0 (vfs_path_element_t, 1);
|
|
|
|
path_element->port = default_port;
|
2010-07-10 14:01:19 +04:00
|
|
|
|
2012-04-03 16:48:55 +04:00
|
|
|
pcopy_len = strlen (path);
|
|
|
|
pcopy = g_strndup (path, pcopy_len);
|
|
|
|
pend = pcopy + pcopy_len;
|
2011-03-29 11:20:34 +04:00
|
|
|
|
|
|
|
if ((flags & URL_NOSLASH) == 0)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2018-01-13 00:54:34 +03:00
|
|
|
char *dir = pcopy;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* locate path component */
|
2015-01-07 11:34:53 +03:00
|
|
|
while (!IS_PATH_SEP (*dir) && *dir != '\0')
|
2010-11-08 13:21:45 +03:00
|
|
|
dir++;
|
2011-03-29 11:20:34 +04:00
|
|
|
if (*dir == '\0')
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element->path = g_strdup (PATH_SEP_STR);
|
2011-03-29 11:20:34 +04:00
|
|
|
else
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2012-04-03 16:48:55 +04:00
|
|
|
path_element->path = g_strndup (dir, pcopy_len - (size_t) (dir - pcopy));
|
2011-03-29 11:20:34 +04:00
|
|
|
*dir = '\0';
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
2010-07-10 14:01:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* search for any possible user */
|
|
|
|
at = strrchr (pcopy, '@');
|
|
|
|
|
|
|
|
/* We have a username */
|
2011-03-29 11:20:34 +04:00
|
|
|
if (at == NULL)
|
|
|
|
rest = pcopy;
|
|
|
|
else
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
char *inner_colon;
|
|
|
|
|
2011-03-29 11:20:34 +04:00
|
|
|
*at = '\0';
|
2010-11-08 13:21:45 +03:00
|
|
|
inner_colon = strchr (pcopy, ':');
|
2011-03-29 11:20:34 +04:00
|
|
|
if (inner_colon != NULL)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-03-29 11:20:34 +04:00
|
|
|
*inner_colon = '\0';
|
2010-11-08 13:21:45 +03:00
|
|
|
inner_colon++;
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element->password = g_strdup (inner_colon);
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
2011-03-29 11:20:34 +04:00
|
|
|
|
|
|
|
if (*pcopy != '\0')
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element->user = g_strdup (pcopy);
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
if (pend == at + 1)
|
|
|
|
rest = at;
|
|
|
|
else
|
|
|
|
rest = at + 1;
|
|
|
|
}
|
2010-07-10 14:01:19 +04:00
|
|
|
|
2011-03-29 11:20:34 +04:00
|
|
|
if ((flags & URL_USE_ANONYMOUS) == 0)
|
2018-01-13 00:54:34 +03:00
|
|
|
{
|
|
|
|
g_free (path_element->user);
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element->user = vfs_get_local_username ();
|
2018-01-13 00:54:34 +03:00
|
|
|
}
|
2010-07-10 14:01:19 +04:00
|
|
|
/* Check if the host comes with a port spec, if so, chop it */
|
2011-03-29 11:20:34 +04:00
|
|
|
if (*rest != '[')
|
|
|
|
colon = strchr (rest, ':');
|
|
|
|
else
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
|
|
|
colon = strchr (++rest, ']');
|
2011-03-29 11:20:34 +04:00
|
|
|
if (colon != NULL)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
|
|
|
colon[0] = '\0';
|
|
|
|
colon[1] = '\0';
|
|
|
|
colon++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-07 14:18:08 +04:00
|
|
|
vfs_path_element_free (path_element);
|
2018-01-13 00:54:34 +03:00
|
|
|
g_free (pcopy);
|
2010-11-08 13:21:45 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-29 11:20:34 +04:00
|
|
|
if (colon != NULL)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-03-29 11:20:34 +04:00
|
|
|
*colon = '\0';
|
2013-10-10 16:21:26 +04:00
|
|
|
/* cppcheck-suppress invalidscanf */
|
2011-06-07 14:18:08 +04:00
|
|
|
if (sscanf (colon + 1, "%d", &path_element->port) == 1)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-06-07 14:18:08 +04:00
|
|
|
if (path_element->port <= 0 || path_element->port >= 65536)
|
|
|
|
path_element->port = default_port;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
else
|
2011-03-29 11:20:34 +04:00
|
|
|
while (*(++colon) != '\0')
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
|
|
|
switch (*colon)
|
|
|
|
{
|
|
|
|
case 'C':
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element->port = 1;
|
2010-11-08 13:21:45 +03:00
|
|
|
break;
|
|
|
|
case 'r':
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element->port = 2;
|
2010-11-08 13:21:45 +03:00
|
|
|
break;
|
2015-04-19 13:57:38 +03:00
|
|
|
default:
|
|
|
|
break;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
}
|
2010-07-10 14:01:19 +04:00
|
|
|
}
|
|
|
|
|
2011-06-07 14:18:08 +04:00
|
|
|
path_element->host = g_strdup (rest);
|
2018-01-13 00:54:34 +03:00
|
|
|
g_free (pcopy);
|
2012-04-30 14:58:25 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2011-10-27 15:22:14 +04:00
|
|
|
path_element->dir.converter = INVALID_CONV;
|
2012-04-30 14:58:25 +04:00
|
|
|
#endif
|
2011-03-29 11:20:34 +04:00
|
|
|
|
2011-06-07 14:18:08 +04:00
|
|
|
return path_element;
|
2010-07-10 14:01:19 +04:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-05 09:08:16 +03:00
|
|
|
|
2016-09-17 22:46:22 +03:00
|
|
|
void __attribute__ ((noreturn)) vfs_die (const char *m)
|
2003-11-05 09:08:16 +03:00
|
|
|
{
|
2009-01-10 16:13:56 +03:00
|
|
|
message (D_ERROR, _("Internal error:"), "%s", m);
|
2010-03-31 13:28:06 +04:00
|
|
|
exit (EXIT_FAILURE);
|
2003-11-05 09:08:16 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2003-11-05 09:08:16 +03:00
|
|
|
char *
|
2004-08-16 19:45:05 +04:00
|
|
|
vfs_get_password (const char *msg)
|
2003-11-05 09:08:16 +03:00
|
|
|
{
|
2013-01-25 16:41:41 +04:00
|
|
|
return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD,
|
2013-01-25 18:29:15 +04:00
|
|
|
INPUT_COMPLETE_NONE);
|
2003-11-05 09:08:16 +03:00
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|