2 Utilities for VFS modules.
4 Copyright (C) 1988, 1992, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 The Free Software Foundation, Inc.
8 Copyright (C) 1995, 1996 Miguel de Icaza
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: Utilities for VFS modules
29 * \author Miguel de Icaza
36 #include <sys/types.h>
43 #include "lib/global.h"
44 #include "lib/unixcompat.h"
45 #include "lib/util.h" /* mc_mkstemps() */
46 #include "lib/widget.h" /* message() */
47 #include "lib/strutil.h" /* INVALID_CONV */
52 /*** global variables ****************************************************************************/
54 /*** file scope macro definitions ****************************************************************/
63 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
64 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
66 #define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
76 /* --------------------------------------------------------------------------------------------- */
77 /*** public functions ****************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
79 /** Get current username
81 * @return g_malloc()ed string with the name of the currently logged in
82 * user ("anonymous" if uid is not registered in the system)
86 vfs_get_local_username (void)
90 p_i
= getpwuid (geteuid ());
92 return (p_i
&& p_i
->pw_name
) ? g_strdup (p_i
->pw_name
) : g_strdup ("anonymous"); /* Unknown UID, strange */
95 /* --------------------------------------------------------------------------------------------- */
97 * Look up a user or group name from a uid/gid, maintaining a cache.
98 * FIXME, for now it's a one-entry cache.
99 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
100 * This file should be modified for non-unix systems to do something
104 /* --------------------------------------------------------------------------------------------- */
107 vfs_finduid (const char *uname
)
109 static int saveuid
= -993;
110 static char saveuname
[TUNMLEN
];
111 static int my_uid
= -993;
115 if (uname
[0] != saveuname
[0] /* Quick test w/o proc call */
116 || 0 != strncmp (uname
, saveuname
, TUNMLEN
))
118 g_strlcpy (saveuname
, uname
, TUNMLEN
);
119 pw
= getpwnam (uname
);
122 saveuid
= pw
->pw_uid
;
132 /* --------------------------------------------------------------------------------------------- */
135 vfs_findgid (const char *gname
)
137 static int savegid
= -993;
138 static char savegname
[TGNMLEN
];
139 static int my_gid
= -993;
143 if (gname
[0] != savegname
[0] /* Quick test w/o proc call */
144 || 0 != strncmp (gname
, savegname
, TUNMLEN
))
146 g_strlcpy (savegname
, gname
, TUNMLEN
);
147 gr
= getgrnam (gname
);
150 savegid
= gr
->gr_gid
;
160 /* --------------------------------------------------------------------------------------------- */
162 * Create a temporary file with a name resembling the original.
163 * This is needed e.g. for local copies requested by extfs.
164 * Some extfs scripts may look at the extension.
165 * We also protect stupid scripts agains dangerous names.
169 vfs_mkstemps (vfs_path_t
** pname_vpath
, const char *prefix
, const char *param_basename
)
176 /* Strip directories */
177 p
= strrchr (param_basename
, PATH_SEP
);
183 /* Protection against very long names */
184 shift
= strlen (p
) - (MC_MAXPATHLEN
- 16);
188 suffix
= g_malloc (MC_MAXPATHLEN
);
190 /* Protection against unusual characters */
192 while (*p
&& (*p
!= '#'))
194 if (strchr (".-_@", *p
) || isalnum ((unsigned char) *p
))
200 fd
= mc_mkstemps (pname_vpath
, prefix
, suffix
);
205 /* --------------------------------------------------------------------------------------------- */
206 /** Extract the hostname and username from the path
208 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
210 * ftp://sunsite.unc.edu/pub/linux
211 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
212 * ftp://tsx-11.mit.edu:8192/
213 * ftp://joe@foo.edu:11321/private
214 * ftp://joe:password@foo.se
216 * @param path is an input string to be parsed
217 * @param default_port is an input default port
218 * @param flags are parsing modifier flags (@see vfs_url_flags_t)
220 * @return g_malloc()ed url info.
221 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
222 * is not set, then the current login name is supplied.
223 * Return value is a g_malloc()ed structure with the pathname relative to the
228 vfs_url_split (const char *path
, int default_port
, vfs_url_flags_t flags
)
230 vfs_path_element_t
*path_element
;
234 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
236 path_element
= g_new0 (vfs_path_element_t
, 1);
237 path_element
->port
= default_port
;
239 pcopy
= g_strdup (path
);
240 pend
= pcopy
+ strlen (pcopy
);
243 if ((flags
& URL_NOSLASH
) == 0)
245 /* locate path component */
246 while (*dir
!= PATH_SEP
&& *dir
!= '\0')
249 path_element
->path
= g_strdup (PATH_SEP_STR
);
252 path_element
->path
= g_strdup (dir
);
257 /* search for any possible user */
258 at
= strrchr (pcopy
, '@');
260 /* We have a username */
266 inner_colon
= strchr (pcopy
, ':');
267 if (inner_colon
!= NULL
)
271 path_element
->password
= g_strdup (inner_colon
);
275 path_element
->user
= g_strdup (pcopy
);
283 if ((flags
& URL_USE_ANONYMOUS
) == 0)
284 path_element
->user
= vfs_get_local_username ();
286 /* Check if the host comes with a port spec, if so, chop it */
288 colon
= strchr (rest
, ':');
291 colon
= strchr (++rest
, ']');
300 vfs_path_element_free (path_element
);
308 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
310 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
311 path_element
->port
= default_port
;
314 while (*(++colon
) != '\0')
319 path_element
->port
= 1;
322 path_element
->port
= 2;
328 path_element
->host
= g_strdup (rest
);
329 path_element
->dir
.converter
= INVALID_CONV
;
334 /* --------------------------------------------------------------------------------------------- */
337 vfs_die (const char *m
)
339 message (D_ERROR
, _("Internal error:"), "%s", m
);
343 /* --------------------------------------------------------------------------------------------- */
346 vfs_get_password (const char *msg
)
348 return input_dialog (msg
, _("Password:"), MC_HISTORY_VFS_PASSWORD
, INPUT_PASSWORD
);
351 /* --------------------------------------------------------------------------------------------- */