1998-02-27 07:54:42 +03:00
|
|
|
/* Utilities for VFS modules.
|
|
|
|
|
2003-10-16 20:50:09 +04:00
|
|
|
Copyright (C) 1988, 1992 Free Software Foundation
|
1998-02-27 07:54:42 +03:00
|
|
|
Copyright (C) 1995, 1996 Miguel de Icaza
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
1998-09-27 23:27:58 +04:00
|
|
|
GNU Library General Public License for more details.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this program; if not, write to the Free Software
|
2000-08-23 02:50:00 +04:00
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
1998-09-27 23:27:58 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <config.h>
|
2003-10-29 03:50:36 +03:00
|
|
|
#include <ctype.h>
|
1999-01-21 01:01:11 +03:00
|
|
|
|
|
|
|
#include "utilvfs.h"
|
1998-11-21 22:36:01 +03:00
|
|
|
#include "vfs.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Extract the hostname and username from the path */
|
|
|
|
/* path is in the form: [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
|
|
|
|
*
|
2002-12-15 22:38:18 +03:00
|
|
|
* Returns g_malloc()ed host, user and pass they are present.
|
2002-07-12 03:08:30 +04:00
|
|
|
* If the user is empty, e.g. ftp://@roxanne/private, and URL_ALLOW_ANON
|
|
|
|
* is not set, then the current login name is supplied.
|
1998-02-27 07:54:42 +03:00
|
|
|
*
|
2002-12-15 22:38:18 +03:00
|
|
|
* Return value is a g_malloc()ed string with the pathname relative to the
|
2002-07-12 03:08:30 +04:00
|
|
|
* host.
|
|
|
|
*/
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-10-29 10:19:25 +03:00
|
|
|
#ifdef USE_NETCODE
|
2002-10-01 07:47:46 +04:00
|
|
|
char *
|
|
|
|
vfs_split_url (const char *path, char **host, char **user, int *port,
|
|
|
|
char **pass, int default_port, int flags)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
struct passwd *passwd_info;
|
|
|
|
char *dir, *colon, *inner_colon, *at, *rest;
|
|
|
|
char *retval;
|
1999-01-21 01:01:11 +03:00
|
|
|
char *pcopy = g_strdup (path);
|
2002-10-01 07:47:46 +04:00
|
|
|
char *pend = pcopy + strlen (pcopy);
|
|
|
|
|
2000-04-10 16:00:43 +04:00
|
|
|
if (pass)
|
|
|
|
*pass = NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
*port = default_port;
|
|
|
|
*user = NULL;
|
1998-11-22 19:17:42 +03:00
|
|
|
retval = NULL;
|
2002-10-01 07:47:46 +04:00
|
|
|
|
1998-11-21 22:36:01 +03:00
|
|
|
dir = pcopy;
|
1998-11-22 19:17:42 +03:00
|
|
|
if (!(flags & URL_NOSLASH)) {
|
1998-11-21 22:36:01 +03:00
|
|
|
/* locate path component */
|
2000-05-16 18:59:33 +04:00
|
|
|
while (*dir != PATH_SEP && *dir)
|
|
|
|
dir++;
|
2002-10-01 07:47:46 +04:00
|
|
|
if (*dir) {
|
1999-01-21 01:01:11 +03:00
|
|
|
retval = g_strdup (dir);
|
1998-11-21 22:36:01 +03:00
|
|
|
*dir = 0;
|
|
|
|
} else
|
1999-01-21 01:01:11 +03:00
|
|
|
retval = g_strdup (PATH_SEP_STR);
|
1998-11-21 22:36:01 +03:00
|
|
|
}
|
2002-10-01 07:47:46 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* search for any possible user */
|
2002-10-01 07:47:46 +04:00
|
|
|
at = strchr (pcopy, '@');
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* We have a username */
|
2002-10-01 07:47:46 +04:00
|
|
|
if (at) {
|
1998-02-27 07:54:42 +03:00
|
|
|
*at = 0;
|
|
|
|
inner_colon = strchr (pcopy, ':');
|
2002-07-12 03:08:30 +04:00
|
|
|
if (inner_colon) {
|
1998-02-27 07:54:42 +03:00
|
|
|
*inner_colon = 0;
|
|
|
|
inner_colon++;
|
2000-05-16 18:59:33 +04:00
|
|
|
if (pass)
|
1999-01-21 01:01:11 +03:00
|
|
|
*pass = g_strdup (inner_colon);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1999-03-30 10:09:56 +04:00
|
|
|
if (*pcopy != 0)
|
1999-01-21 01:01:11 +03:00
|
|
|
*user = g_strdup (pcopy);
|
2002-10-01 07:47:46 +04:00
|
|
|
|
|
|
|
if (pend == at + 1)
|
1998-02-27 07:54:42 +03:00
|
|
|
rest = at;
|
|
|
|
else
|
|
|
|
rest = at + 1;
|
|
|
|
} else
|
|
|
|
rest = pcopy;
|
|
|
|
|
2002-07-12 03:08:30 +04:00
|
|
|
if (!*user && !(flags & URL_ALLOW_ANON)) {
|
|
|
|
passwd_info = getpwuid (geteuid ());
|
|
|
|
if (passwd_info && passwd_info->pw_name)
|
|
|
|
*user = g_strdup (passwd_info->pw_name);
|
1998-02-27 07:54:42 +03:00
|
|
|
else {
|
2002-07-12 03:08:30 +04:00
|
|
|
/* This is very unlikely to happen */
|
|
|
|
*user = g_strdup ("anonymous");
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2002-07-12 03:08:30 +04:00
|
|
|
endpwent ();
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2002-07-12 03:08:30 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Check if the host comes with a port spec, if so, chop it */
|
|
|
|
colon = strchr (rest, ':');
|
2002-10-01 07:47:46 +04:00
|
|
|
if (colon) {
|
1998-02-27 07:54:42 +03:00
|
|
|
*colon = 0;
|
2002-10-01 07:47:46 +04:00
|
|
|
if (sscanf (colon + 1, "%d", port) == 1) {
|
1998-09-13 14:40:43 +04:00
|
|
|
if (*port <= 0 || *port >= 65536)
|
2002-10-01 07:47:46 +04:00
|
|
|
*port = default_port;
|
1998-09-13 14:40:43 +04:00
|
|
|
} else {
|
2002-10-01 07:47:46 +04:00
|
|
|
while (*(++colon)) {
|
|
|
|
switch (*colon) {
|
|
|
|
case 'C':
|
|
|
|
*port = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
*port = 2;
|
|
|
|
break;
|
1998-09-13 14:40:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2000-05-16 18:59:33 +04:00
|
|
|
if (host)
|
|
|
|
*host = g_strdup (rest);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
g_free (pcopy);
|
1998-02-27 07:54:42 +03:00
|
|
|
return retval;
|
|
|
|
}
|
2003-10-29 10:19:25 +03:00
|
|
|
#endif /* USE_NETCODE */
|
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.
|
|
|
|
* FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
|
|
|
|
* This file should be modified for non-unix systems to do something
|
|
|
|
* reasonable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TUNMLEN
|
|
|
|
#define TUNMLEN 256
|
|
|
|
#endif
|
|
|
|
#ifndef TGNMLEN
|
|
|
|
#define TGNMLEN 256
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
|
|
|
|
#define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
|
|
|
|
|
|
|
|
int
|
2003-10-28 03:57:02 +03:00
|
|
|
vfs_finduid (char *uname)
|
2003-10-16 20:50:09 +04:00
|
|
|
{
|
|
|
|
static int saveuid = -993;
|
|
|
|
static char saveuname[TUNMLEN];
|
|
|
|
static int my_uid = -993;
|
|
|
|
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
if (uname[0] != saveuname[0] /* Quick test w/o proc call */
|
|
|
|
||0 != strncmp (uname, saveuname, TUNMLEN)) {
|
|
|
|
strncpy (saveuname, uname, TUNMLEN);
|
|
|
|
pw = getpwnam (uname);
|
|
|
|
if (pw) {
|
|
|
|
saveuid = pw->pw_uid;
|
|
|
|
} else {
|
|
|
|
saveuid = myuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return saveuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-10-28 03:57:02 +03:00
|
|
|
vfs_findgid (char *gname)
|
2003-10-16 20:50:09 +04:00
|
|
|
{
|
|
|
|
static int savegid = -993;
|
|
|
|
static char savegname[TGNMLEN];
|
|
|
|
static int my_gid = -993;
|
|
|
|
|
|
|
|
struct group *gr;
|
|
|
|
|
|
|
|
if (gname[0] != savegname[0] /* Quick test w/o proc call */
|
|
|
|
||0 != strncmp (gname, savegname, TUNMLEN)) {
|
|
|
|
strncpy (savegname, gname, TUNMLEN);
|
|
|
|
gr = getgrnam (gname);
|
|
|
|
if (gr) {
|
|
|
|
savegid = gr->gr_gid;
|
|
|
|
} else {
|
|
|
|
savegid = mygid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return savegid;
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vfs_mkstemps (char **pname, const char *prefix, const char *basename)
|
|
|
|
{
|
|
|
|
const unsigned char *p;
|
|
|
|
char *suffix, *q;
|
|
|
|
int shift;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Strip directories */
|
|
|
|
p = strrchr (basename, PATH_SEP);
|
|
|
|
if (!p)
|
|
|
|
p = basename;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
|
|
|
|
/* Protection against very long names */
|
|
|
|
shift = strlen (p) - (MC_MAXPATHLEN - 16);
|
|
|
|
if (shift > 0)
|
|
|
|
p += shift;
|
|
|
|
|
|
|
|
suffix = g_malloc (MC_MAXPATHLEN);
|
|
|
|
|
|
|
|
/* Protection against unusual characters */
|
|
|
|
q = suffix;
|
|
|
|
while (*p && (*p != '#')) {
|
|
|
|
if (strchr (".-_@", *p) || isalnum (*p))
|
|
|
|
*q++ = *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
|
|
|
|
fd = mc_mkstemps (pname, prefix, suffix);
|
|
|
|
g_free (suffix);
|
|
|
|
return fd;
|
|
|
|
}
|