1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-vfs-method.c - Handling of access methods in the GNOME
Virtual File System.
Copyright (C) 1999 Free Software Foundation
The Gnome Library 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.
The Gnome Library 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 GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Ettore Perazzoli <ettore@gnu.org> */
#include <config.h>
#include "gnome-vfs-method.h"
#include "gnome-vfs-configuration.h"
#include "gnome-vfs-private.h"
#ifdef G_OS_WIN32
#include "gnome-vfs-private-utils.h"
#endif
#include <gmodule.h>
#include <libgnomevfs/gnome-vfs-module.h>
#include <libgnomevfs/gnome-vfs-transform.h>
#include <libgnomevfs/gnome-vfs-method.h>
#ifdef USE_DAEMON
#include "gnome-vfs-daemon-method.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define GNOME_VFS_MODULE_INIT "vfs_module_init"
#define GNOME_VFS_MODULE_TRANSFORM "vfs_module_transform"
#define GNOME_VFS_MODULE_SHUTDOWN "vfs_module_shutdown"
struct _ModuleElement {
char *name;
const char *args;
GnomeVFSMethod *method;
GnomeVFSTransform *transform;
GnomeVFSMethodShutdownFunc shutdown_function;
gboolean run_in_daemon;
};
typedef struct _ModuleElement ModuleElement;
static gboolean method_already_initialized = FALSE;
static gboolean gnome_vfs_is_daemon = FALSE;
static GType daemon_volume_monitor_type = 0;
static void (*daemon_force_probe_callback) (GnomeVFSVolumeMonitor *volume_monitor) = NULL;
static GHashTable *module_hash = NULL;
G_LOCK_DEFINE_STATIC (gnome_vfs_method_init);
static GStaticRecMutex module_hash_lock = G_STATIC_REC_MUTEX_INIT;
static GList *module_path_list = NULL;
/* Pass some integration stuff here, so we can make the library not depend
on the daemon-side code */
void
gnome_vfs_set_is_daemon (GType volume_monitor_type,
GnomeVFSDaemonForceProbeCallback force_probe_callback)
{
gnome_vfs_is_daemon = TRUE;
daemon_volume_monitor_type = volume_monitor_type;
daemon_force_probe_callback = force_probe_callback;
}
gboolean
gnome_vfs_get_is_daemon (void)
{
return gnome_vfs_is_daemon;
}
GType
gnome_vfs_get_daemon_volume_monitor_type (void)
{
return daemon_volume_monitor_type;
}
GnomeVFSDaemonForceProbeCallback
_gnome_vfs_get_daemon_force_probe_callback (void)
{
return daemon_force_probe_callback;
}
static void
module_element_free (gpointer elementp)
{
ModuleElement *element = (ModuleElement *) elementp;
if (element->shutdown_function != NULL) {
(* element->shutdown_function) (element->method);
}
/* NB: this is the key */
g_free (element->name);
g_free (element);
}
static gboolean
init_hash_table (void)
{
module_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, module_element_free);
return TRUE;
}
static gboolean
install_path_list (const gchar *user_path_list)
{
const gchar *p, *oldp;
/* Notice that this assumes the list has already been locked. */
oldp = user_path_list;
while (1) {
gchar *elem;
p = strchr (oldp, G_SEARCHPATH_SEPARATOR);
if (p == NULL) {
if (*oldp != '\0') {
elem = g_strdup (oldp);
module_path_list = g_list_append
(module_path_list, elem);
}
break;
} else if (p != oldp) {
elem = g_strndup (oldp, p - oldp);
module_path_list = g_list_append (module_path_list,
elem);
} else {
elem = NULL;
}
oldp = p + 1;
}
return TRUE;
}
static gboolean
init_path_list (void)
{
const gchar *user_path_list;
if (module_path_list != NULL)
return TRUE;
/* User-supplied path. */
user_path_list = getenv ("GNOME_VFS_MODULE_PATH");
if (user_path_list != NULL) {
if (! install_path_list (user_path_list))
return FALSE;
}
/* Default path. It comes last so that users can override it. */
module_path_list = g_list_append (module_path_list,
g_build_filename (GNOME_VFS_LIBDIR,
GNOME_VFS_MODULE_SUBDIR,
NULL));
return TRUE;
}
/**
* gnome_vfs_method_init:
*
* Initializes the gnome-vfs methods. If already initialized then will simply return
* %TRUE.
*
* Return value: Returns %TRUE.
*/
gboolean
gnome_vfs_method_init (void)
{
G_LOCK (gnome_vfs_method_init);
if (method_already_initialized)
goto gnome_vfs_method_init_out;
if (! init_hash_table ())
goto gnome_vfs_method_init_out;
if (! init_path_list ())
goto gnome_vfs_method_init_out;
method_already_initialized = TRUE;
gnome_vfs_method_init_out:
G_UNLOCK (gnome_vfs_method_init);
return method_already_initialized;
}
static void
load_module (const gchar *module_name, const char *method_name, const char *args,
GnomeVFSMethod **method, GnomeVFSTransform **transform,
GnomeVFSMethodShutdownFunc *shutdown_function)
{
GModule *module;
GnomeVFSMethod *temp_method = NULL;
GnomeVFSTransform *temp_transform = NULL;
GnomeVFSMethodInitFunc init_function = NULL;
GnomeVFSTransformInitFunc transform_function = NULL;
*method = NULL;
*transform = NULL;
*shutdown_function = NULL;
module = g_module_open (module_name, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
if (module == NULL) {
g_warning ("Cannot load module `%s' (%s)", module_name, g_module_error ());
return;
}
g_module_symbol (module, GNOME_VFS_MODULE_INIT,
(gpointer *) &init_function);
g_module_symbol (module, GNOME_VFS_MODULE_TRANSFORM,
(gpointer *) &transform_function);
g_module_symbol (module, GNOME_VFS_MODULE_SHUTDOWN,
(gpointer *) shutdown_function);
if ((init_function == NULL || *shutdown_function == NULL) &&
(transform_function == NULL)) {
g_warning ("module '%s' does not have init, transform and shutfown functions; may be an out-of-date module", module_name);
return;
}
if (init_function)
temp_method = (* init_function) (method_name, args);
if (temp_method == NULL && init_function) {
g_warning ("module '%s' returned a NULL handle", module_name);
return;
}
if (temp_method != NULL) {
/* Some basic checks */
if (temp_method->method_table_size == 0) {
g_warning ("module '%s' has 0 table size", module_name);
return;
} else if (temp_method->method_table_size > (0x100 * sizeof (GnomeVFSMethod))) {
g_warning ("module '%s' has unreasonable table size, perhaps it is using the old GnomeVFSMethod struct?", module_name);
return;
} else if (!VFS_METHOD_HAS_FUNC(temp_method, open)) {
g_warning ("module '%s' has no open fn", module_name);
return;
#if 0
} else if (!VFS_METHOD_HAS_FUNC(temp_method, create)) {
g_warning ("module '%s' has no create fn", module_name);
return;
#endif
} else if (!VFS_METHOD_HAS_FUNC(temp_method, is_local)) {
g_warning ("module '%s' has no is-local fn", module_name);
return;
#if 0
} else if (!VFS_METHOD_HAS_FUNC(temp_method, get_file_info)) {
g_warning ("module '%s' has no get-file-info fn", module_name);
return;
#endif
}
/* More advanced assumptions. */
if (VFS_METHOD_HAS_FUNC(temp_method, tell) && !VFS_METHOD_HAS_FUNC(temp_method, seek)) {
g_warning ("module '%s' has tell and no seek", module_name);
return;
}
if (VFS_METHOD_HAS_FUNC(temp_method, seek) && !VFS_METHOD_HAS_FUNC(temp_method, tell)) {
g_warning ("module '%s' has seek and no tell", module_name);
return;
}
}
if (transform_function)
temp_transform = (* transform_function) (method_name, args);
if (temp_transform) {
if (temp_transform->transform == NULL) {
g_warning ("module '%s' has no transform method", module_name);
return;
}
}
*method = temp_method;
*transform = temp_transform;
}
static void
load_module_in_path_list (const gchar *base_name, const char *method_name, const char *args,
GnomeVFSMethod **method, GnomeVFSTransform **transform,
GnomeVFSMethodShutdownFunc *shutdown_function)
{
GList *p;
*method = NULL;
*transform = NULL;
for (p = module_path_list; p != NULL; p = p->next) {
const gchar *path;
gchar *name;
path = p->data;
name = g_module_build_path (path, base_name);
load_module (name, method_name, args, method, transform, shutdown_function);
g_free (name);
if (*method != NULL || *transform != NULL)
return;
}
}
static ModuleElement *
gnome_vfs_add_module_to_hash_table (const gchar *name)
{
GnomeVFSMethod *method = NULL;
GnomeVFSTransform *transform = NULL;
GnomeVFSMethodShutdownFunc shutdown_function = NULL;
ModuleElement *module_element;
const char *module_name;
#if defined (HAVE_SETEUID) || defined (HAVE_SETRESUID)
uid_t saved_uid;
#endif
#if defined (HAVE_SETEGID) || defined (HAVE_SETRESGID)
gid_t saved_gid;
#endif
const char *args;
gboolean run_in_daemon;
g_static_rec_mutex_lock (&module_hash_lock);
module_element = g_hash_table_lookup (module_hash, name);
if (module_element != NULL)
goto add_module_out;
module_name = _gnome_vfs_configuration_get_module_path (name, &args, &run_in_daemon);
if (module_name == NULL)
goto add_module_out;
if (gnome_vfs_is_daemon || !run_in_daemon) {
/* Set the effective UID/GID to the user UID/GID to prevent attacks to
setuid/setgid executables. */
#if defined (HAVE_SETEUID) || defined (HAVE_SETRESUID)
saved_uid = geteuid ();
#endif
#if defined (HAVE_SETEGID) || defined (HAVE_SETRESGID)
saved_gid = getegid ();
#endif
#if defined(HAVE_SETEUID)
seteuid (getuid ());
#elif defined(HAVE_SETRESUID)
setresuid (-1, getuid (), -1);
#endif
#if defined(HAVE_SETEGID)
setegid (getgid ());
#elif defined(HAVE_SETRESGID)
setresgid (-1, getgid (), -1);
#endif
if (g_path_is_absolute (module_name))
load_module (module_name, name, args, &method, &transform, &shutdown_function);
else
load_module_in_path_list (module_name, name, args, &method, &transform, &shutdown_function);
#if defined(HAVE_SETEUID)
seteuid (saved_uid);
#elif defined(HAVE_SETRESUID)
setresuid (-1, saved_uid, -1);
#endif
#if defined(HAVE_SETEGID)
setegid (saved_gid);
#elif defined(HAVE_SETRESGID)
setresgid (-1, saved_gid, -1);
#endif
} else {
#ifdef USE_DAEMON
method = _gnome_vfs_daemon_method_get ();
#endif
}
if (method == NULL && transform == NULL)
goto add_module_out;
module_element = g_new (ModuleElement, 1);
module_element->name = g_strdup (name);
module_element->method = method;
module_element->transform = transform;
module_element->shutdown_function = shutdown_function;
module_element->run_in_daemon = run_in_daemon;
g_hash_table_insert (module_hash, module_element->name, module_element);
add_module_out:
g_static_rec_mutex_unlock (&module_hash_lock);
return module_element;
}
/**
* gnome_vfs_method_get:
* @name: name of the protocol.
*
* Returns the method handle for the given protocol @name. @name could be any protocol
* which gnome-vfs implements. Like ftp, http, smb etc..
*/
GnomeVFSMethod *
gnome_vfs_method_get (const gchar *name)
{
ModuleElement *module_element;
g_return_val_if_fail (name != NULL, NULL);
module_element = gnome_vfs_add_module_to_hash_table (name);
return module_element ? module_element->method : NULL;
}
/**
* gnome_vfs_transform_get:
* @name: name of the method to get the transform of.
*
* Get the transform for the method @name.
*
* Return value: a #GnomeVFSTransform handle for @name.
*/
GnomeVFSTransform *
gnome_vfs_transform_get (const gchar *name)
{
ModuleElement *module_element;
g_return_val_if_fail (name != NULL, NULL);
module_element = gnome_vfs_add_module_to_hash_table (name);
return module_element ? module_element->transform : NULL;
}
void
_gnome_vfs_method_shutdown (void)
{
G_LOCK (gnome_vfs_method_init);
if (module_hash != NULL) {
g_hash_table_destroy (module_hash);
module_hash = NULL;
}
method_already_initialized = FALSE;
G_UNLOCK (gnome_vfs_method_init);
}
|