| 1 |
/* |
| 2 |
* Copyright (C) 2017 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vratislav Podzimek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#include <glib.h> |
| 24 |
|
| 25 |
#include <libmount/libmount.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <sys/wait.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include <blockdev/utils.h> |
| 33 |
|
| 34 |
#include "fs.h" |
| 35 |
#include "mount.h" |
| 36 |
|
| 37 |
#define MOUNT_ERR_BUF_SIZE 1024 |
| 38 |
|
| 39 |
#ifdef __clang__ |
| 40 |
#define ZERO_INIT {} |
| 41 |
#else |
| 42 |
#define ZERO_INIT {0} |
| 43 |
#endif |
| 44 |
|
| 45 |
typedef struct MountArgs { |
| 46 |
const gchar *mountpoint; |
| 47 |
const gchar *device; |
| 48 |
const gchar *fstype; |
| 49 |
const gchar *options; |
| 50 |
const gchar *spec; |
| 51 |
gboolean lazy; |
| 52 |
gboolean force; |
| 53 |
} MountArgs; |
| 54 |
|
| 55 |
typedef gboolean (*MountFunc) (MountArgs *args, GError **error); |
| 56 |
|
| 57 |
static gboolean do_mount (MountArgs *args, GError **error); |
| 58 |
|
| 59 |
#ifndef LIBMOUNT_NEW_ERR_API |
| 60 |
static gboolean get_unmount_error_old (struct libmnt_context *cxt, int rc, const gchar *spec, GError **error) { |
| 61 |
int syscall_errno = 0; |
| 62 |
int helper_status = 0; |
| 63 |
|
| 64 |
if (mnt_context_syscall_called (cxt) == 1) { |
| 65 |
syscall_errno = mnt_context_get_syscall_errno (cxt); |
| 66 |
switch (syscall_errno) { |
| 67 |
case 0: |
| 68 |
return TRUE; |
| 69 |
case EBUSY: |
| 70 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 71 |
"Target busy."); |
| 72 |
break; |
| 73 |
case EINVAL: |
| 74 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 75 |
"Not a mount point."); |
| 76 |
break; |
| 77 |
case EPERM: |
| 78 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_AUTH, |
| 79 |
"Operation not permitted."); |
| 80 |
break; |
| 81 |
default: |
| 82 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 83 |
"Unmount syscall failed: %d.", syscall_errno); |
| 84 |
break; |
| 85 |
} |
| 86 |
} else if (mnt_context_helper_executed (cxt) == 1) { |
| 87 |
helper_status = mnt_context_get_helper_status (cxt); |
| 88 |
if (helper_status != 0) { |
| 89 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 90 |
"Unmount helper program failed: %d.", helper_status); |
| 91 |
return FALSE; |
| 92 |
} else |
| 93 |
return TRUE; |
| 94 |
} else { |
| 95 |
if (rc == 0) |
| 96 |
return TRUE; |
| 97 |
else if (rc == -EPERM) { |
| 98 |
if (mnt_context_tab_applied (cxt)) |
| 99 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_AUTH, |
| 100 |
"Operation not permitted."); |
| 101 |
else |
| 102 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 103 |
"Not mounted."); |
| 104 |
} else { |
| 105 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 106 |
"Failed to unmount %s.", spec); |
| 107 |
} |
| 108 |
} |
| 109 |
return FALSE; |
| 110 |
} |
| 111 |
#else |
| 112 |
static gboolean get_unmount_error_new (struct libmnt_context *cxt, int rc, const gchar *spec, GError **error) { |
| 113 |
int ret = 0; |
| 114 |
int syscall_errno = 0; |
| 115 |
char buf[MOUNT_ERR_BUF_SIZE] = {0}; |
| 116 |
gboolean permission = FALSE; |
| 117 |
|
| 118 |
ret = mnt_context_get_excode (cxt, rc, buf, MOUNT_ERR_BUF_SIZE - 1); |
| 119 |
if (ret != 0) { |
| 120 |
/* check whether the call failed because of lack of permission */ |
| 121 |
if (mnt_context_syscall_called (cxt)) { |
| 122 |
syscall_errno = mnt_context_get_syscall_errno (cxt); |
| 123 |
permission = syscall_errno == EPERM; |
| 124 |
} else |
| 125 |
permission = ret == MNT_EX_USAGE && mnt_context_tab_applied (cxt); |
| 126 |
|
| 127 |
if (permission) |
| 128 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_AUTH, |
| 129 |
"Operation not permitted."); |
| 130 |
else { |
| 131 |
if (*buf == '\0') |
| 132 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 133 |
"Unknown error when unmounting %s", spec); |
| 134 |
else |
| 135 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 136 |
"%s", buf); |
| 137 |
} |
| 138 |
|
| 139 |
return FALSE; |
| 140 |
} |
| 141 |
|
| 142 |
return TRUE; |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
static gboolean do_unmount (MountArgs *args, GError **error) { |
| 147 |
struct libmnt_context *cxt = NULL; |
| 148 |
int ret = 0; |
| 149 |
gboolean success = FALSE; |
| 150 |
|
| 151 |
cxt = mnt_new_context (); |
| 152 |
|
| 153 |
if (mnt_context_set_target (cxt, args->spec) != 0) { |
| 154 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 155 |
"Failed to set '%s' as target for umount", args->spec); |
| 156 |
mnt_free_context (cxt); |
| 157 |
return FALSE; |
| 158 |
} |
| 159 |
|
| 160 |
if (args->lazy) { |
| 161 |
if (mnt_context_enable_lazy (cxt, TRUE) != 0) { |
| 162 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 163 |
"Failed to set lazy unmount for '%s'", args->spec); |
| 164 |
mnt_free_context (cxt); |
| 165 |
return FALSE; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (args->force) { |
| 170 |
if (mnt_context_enable_force (cxt, TRUE) != 0) { |
| 171 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 172 |
"Failed to set force unmount for '%s'", args->spec); |
| 173 |
mnt_free_context (cxt); |
| 174 |
return FALSE; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Unmounting %s", args->spec); |
| 179 |
|
| 180 |
ret = mnt_context_umount (cxt); |
| 181 |
#ifdef LIBMOUNT_NEW_ERR_API |
| 182 |
success = get_unmount_error_new (cxt, ret, args->spec, error); |
| 183 |
#else |
| 184 |
success = get_unmount_error_old (cxt, ret, args->spec, error); |
| 185 |
#endif |
| 186 |
|
| 187 |
mnt_free_context (cxt); |
| 188 |
return success; |
| 189 |
} |
| 190 |
|
| 191 |
#ifndef LIBMOUNT_NEW_ERR_API |
| 192 |
static gboolean get_mount_error_old (struct libmnt_context *cxt, int rc, MountArgs *args, GError **error) { |
| 193 |
int syscall_errno = 0; |
| 194 |
int helper_status = 0; |
| 195 |
unsigned long mflags = 0; |
| 196 |
|
| 197 |
if (mnt_context_get_mflags (cxt, &mflags) != 0) { |
| 198 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 199 |
"Failed to get options from string '%s'.", args->options); |
| 200 |
return FALSE; |
| 201 |
} |
| 202 |
|
| 203 |
if (mnt_context_syscall_called (cxt) == 1) { |
| 204 |
syscall_errno = mnt_context_get_syscall_errno (cxt); |
| 205 |
switch (syscall_errno) { |
| 206 |
case 0: |
| 207 |
return TRUE; |
| 208 |
case EBUSY: |
| 209 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 210 |
"Source is already mounted or target is busy."); |
| 211 |
break; |
| 212 |
case EINVAL: |
| 213 |
if (mflags & MS_REMOUNT) |
| 214 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 215 |
"Remount attempted, but %s is not mounted at %s.", args->device, args->mountpoint); |
| 216 |
else if (mflags & MS_MOVE) |
| 217 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 218 |
"Move attempted, but %s is not a mount point.", args->device); |
| 219 |
else |
| 220 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 221 |
"Wrong fs type, %s has an invalid superblock or missing helper program.", args->device); |
| 222 |
break; |
| 223 |
case EPERM: |
| 224 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_AUTH, |
| 225 |
"Operation not permitted."); |
| 226 |
break; |
| 227 |
case ENOTBLK: |
| 228 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 229 |
"%s is not a block device.", args->device); |
| 230 |
break; |
| 231 |
case ENOTDIR: |
| 232 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 233 |
"%s is not a directory.", args->mountpoint); |
| 234 |
break; |
| 235 |
case ENODEV: |
| 236 |
if (args->fstype == NULL || strlen (args->fstype) == 0) |
| 237 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 238 |
"Filesystem type not specified"); |
| 239 |
else |
| 240 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_UNKNOWN_FS, |
| 241 |
"Filesystem type %s not configured in kernel.", args->fstype); |
| 242 |
break; |
| 243 |
case EROFS: |
| 244 |
case EACCES: |
| 245 |
if (mflags & MS_RDONLY) { |
| 246 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 247 |
"Cannot mount %s read-only.", args->device); |
| 248 |
break; |
| 249 |
} else if (args->options && (mnt_optstr_get_option (args->options, "rw", NULL, NULL) == 0)) { |
| 250 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 251 |
"%s is write-protected but `rw' option given.", args->device); |
| 252 |
break; |
| 253 |
} else if (mflags & MS_BIND) { |
| 254 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 255 |
"Mount %s on %s failed.", args->device, args->mountpoint); |
| 256 |
break; |
| 257 |
} |
| 258 |
/* new versions of libmount do this automatically */ |
| 259 |
else { |
| 260 |
MountArgs ro_args = ZERO_INIT; |
| 261 |
gboolean success = FALSE; |
| 262 |
|
| 263 |
ro_args.device = args->device; |
| 264 |
ro_args.mountpoint = args->mountpoint; |
| 265 |
ro_args.fstype = args->fstype; |
| 266 |
if (!args->options) |
| 267 |
ro_args.options = g_strdup ("ro"); |
| 268 |
else |
| 269 |
ro_args.options = g_strdup_printf ("%s,ro", args->options); |
| 270 |
|
| 271 |
success = do_mount (&ro_args, error); |
| 272 |
|
| 273 |
g_free ((gchar*) ro_args.options); |
| 274 |
|
| 275 |
return success; |
| 276 |
} |
| 277 |
default: |
| 278 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 279 |
"Mount syscall failed: %d.", syscall_errno); |
| 280 |
break; |
| 281 |
} |
| 282 |
} else if (mnt_context_helper_executed (cxt) == 1) { |
| 283 |
helper_status = mnt_context_get_helper_status (cxt); |
| 284 |
if (helper_status != 0) { |
| 285 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 286 |
"Mount helper program failed: %d.", helper_status); |
| 287 |
return FALSE; |
| 288 |
} else |
| 289 |
return TRUE; |
| 290 |
} else { |
| 291 |
switch (rc) { |
| 292 |
case 0: |
| 293 |
return TRUE; |
| 294 |
case -EPERM: |
| 295 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_AUTH, |
| 296 |
"Only root can mount %s.", args->device); |
| 297 |
break; |
| 298 |
case -EBUSY: |
| 299 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 300 |
"%s is already mounted.", args->device); |
| 301 |
break; |
| 302 |
/* source or target explicitly defined and not found in fstab */ |
| 303 |
case -MNT_ERR_NOFSTAB: |
| 304 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 305 |
"Can't find %s in %s.", args->device ? args->device : args->mountpoint, mnt_get_fstab_path ()); |
| 306 |
break; |
| 307 |
case -MNT_ERR_MOUNTOPT: |
| 308 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 309 |
"Failed to parse mount options"); |
| 310 |
break; |
| 311 |
case -MNT_ERR_NOSOURCE: |
| 312 |
if (args->device) |
| 313 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 314 |
"Can't find %s.", args->device); |
| 315 |
else |
| 316 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 317 |
"Mount source not defined."); |
| 318 |
break; |
| 319 |
case -MNT_ERR_LOOPDEV: |
| 320 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 321 |
"Failed to setup loop device"); |
| 322 |
break; |
| 323 |
case -MNT_ERR_NOFSTYPE: |
| 324 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 325 |
"Filesystem type not specified"); |
| 326 |
break; |
| 327 |
default: |
| 328 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 329 |
"Failed to mount %s.", args->device ? args->device : args->mountpoint); |
| 330 |
break; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
return FALSE; |
| 335 |
} |
| 336 |
|
| 337 |
#else |
| 338 |
static gboolean get_mount_error_new (struct libmnt_context *cxt, int rc, MountArgs *args, GError **error) { |
| 339 |
int ret = 0; |
| 340 |
int syscall_errno = 0; |
| 341 |
char buf[MOUNT_ERR_BUF_SIZE] = {0}; |
| 342 |
gboolean permission = FALSE; |
| 343 |
|
| 344 |
ret = mnt_context_get_excode (cxt, rc, buf, MOUNT_ERR_BUF_SIZE - 1); |
| 345 |
if (ret != 0) { |
| 346 |
/* check whether the call failed because of lack of permission */ |
| 347 |
if (mnt_context_syscall_called (cxt) == 1) { |
| 348 |
syscall_errno = mnt_context_get_syscall_errno (cxt); |
| 349 |
permission = syscall_errno == EPERM; |
| 350 |
} else |
| 351 |
permission = ret == MNT_EX_USAGE && mnt_context_tab_applied (cxt); |
| 352 |
|
| 353 |
if (permission) |
| 354 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_AUTH, |
| 355 |
"Operation not permitted."); |
| 356 |
else if (syscall_errno == ENODEV) |
| 357 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_UNKNOWN_FS, |
| 358 |
"Filesystem type %s not configured in kernel.", args->fstype); |
| 359 |
else { |
| 360 |
if (*buf == '\0') |
| 361 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 362 |
"Unknown error when mounting %s", args->device ? args->device : args->mountpoint); |
| 363 |
else |
| 364 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 365 |
"%s", buf); |
| 366 |
} |
| 367 |
|
| 368 |
return FALSE; |
| 369 |
} |
| 370 |
|
| 371 |
return TRUE; |
| 372 |
} |
| 373 |
#endif |
| 374 |
|
| 375 |
static gboolean do_mount (MountArgs *args, GError **error) { |
| 376 |
struct libmnt_context *cxt = NULL; |
| 377 |
int ret = 0; |
| 378 |
gboolean success = FALSE; |
| 379 |
|
| 380 |
cxt = mnt_new_context (); |
| 381 |
|
| 382 |
if (!args->mountpoint && !args->device) { |
| 383 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 384 |
"You must specify at least one of: mount point, device."); |
| 385 |
mnt_free_context (cxt); |
| 386 |
return FALSE; |
| 387 |
} |
| 388 |
|
| 389 |
if (args->mountpoint) { |
| 390 |
if (mnt_context_set_target (cxt, args->mountpoint) != 0) { |
| 391 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 392 |
"Failed to set '%s' as target for mount", args->mountpoint); |
| 393 |
mnt_free_context (cxt); |
| 394 |
return FALSE; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
if (args->device) { |
| 399 |
if (mnt_context_set_source (cxt, args->device) != 0) { |
| 400 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 401 |
"Failed to set '%s' as source for mount", args->device); |
| 402 |
mnt_free_context (cxt); |
| 403 |
return FALSE; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if (args->fstype) { |
| 408 |
if (mnt_context_set_fstype (cxt, args->fstype) != 0) { |
| 409 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 410 |
"Failed to set '%s' as fstype for mount", args->fstype); |
| 411 |
mnt_free_context (cxt); |
| 412 |
return FALSE; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
if (args->options) { |
| 417 |
if (mnt_context_set_options (cxt, args->options) != 0) { |
| 418 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 419 |
"Failed to set '%s' as options for mount", args->options); |
| 420 |
mnt_free_context (cxt); |
| 421 |
return FALSE; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
#ifdef LIBMOUNT_NEW_ERR_API |
| 426 |
/* we don't want libmount to try RDONLY mounts if we were explicitly given the "rw" option */ |
| 427 |
if (args->options && (mnt_optstr_get_option (args->options, "rw", NULL, NULL) == 0)) |
| 428 |
mnt_context_enable_rwonly_mount (cxt, TRUE); |
| 429 |
#endif |
| 430 |
|
| 431 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Mounting %s (fstype %s) to %s with options: %s", |
| 432 |
args->device ? args->device : "unspecified device", |
| 433 |
args->fstype ? args->fstype : "auto", |
| 434 |
args->mountpoint ? args->mountpoint : "unspecified mountpoint", |
| 435 |
args->options ? args->options : "none"); |
| 436 |
|
| 437 |
ret = mnt_context_mount (cxt); |
| 438 |
|
| 439 |
/* we need to always do some libmount magic to check if the mount really |
| 440 |
succeeded -- `mnt_context_mount` can return zero when helper program |
| 441 |
(mount.type) fails |
| 442 |
*/ |
| 443 |
#ifdef LIBMOUNT_NEW_ERR_API |
| 444 |
success = get_mount_error_new (cxt, ret, args, error); |
| 445 |
#else |
| 446 |
success = get_mount_error_old (cxt, ret, args, error); |
| 447 |
#endif |
| 448 |
|
| 449 |
mnt_free_context (cxt); |
| 450 |
return success; |
| 451 |
} |
| 452 |
|
| 453 |
static gboolean set_ruid (uid_t uid, GError **error) { |
| 454 |
if (setresuid (uid, -1, -1) != 0) { |
| 455 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 456 |
"Error setting ruid: %m"); |
| 457 |
return FALSE; |
| 458 |
} |
| 459 |
|
| 460 |
return TRUE; |
| 461 |
} |
| 462 |
|
| 463 |
static gboolean set_rgid (gid_t gid, GError **error) { |
| 464 |
if (setresgid (gid, -1, -1) != 0) { |
| 465 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 466 |
"Error setting rgid: %m"); |
| 467 |
return FALSE; |
| 468 |
} |
| 469 |
|
| 470 |
return TRUE; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
/** |
| 475 |
* run_as_user: |
| 476 |
* |
| 477 |
* Runs given @func in a child process with real user and group ID specified by |
| 478 |
* @run_as_uid and @run_as_gid. The child process is ended after @func is finished. |
| 479 |
* This is used to run mount and unmount functions in a similar way how the `mount` |
| 480 |
* command, which is a suid binary, works and is used to set the libmount context |
| 481 |
* to restricted. |
| 482 |
*/ |
| 483 |
static gboolean run_as_user (MountFunc func, MountArgs *args, uid_t run_as_uid, gid_t run_as_gid, GError ** error) { |
| 484 |
uid_t current_uid = -1; |
| 485 |
gid_t current_gid = -1; |
| 486 |
pid_t pid = -1; |
| 487 |
pid_t wpid = -1; |
| 488 |
int pipefd[2]; |
| 489 |
int status = 0; |
| 490 |
GIOChannel *channel = NULL; |
| 491 |
GError *local_error = NULL; |
| 492 |
gchar *error_msg = NULL; |
| 493 |
gsize msglen = 0; |
| 494 |
|
| 495 |
current_uid = getuid (); |
| 496 |
current_gid = getgid (); |
| 497 |
|
| 498 |
if (geteuid () != 0) { |
| 499 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 500 |
"Not running as root, cannot change the UID/GID."); |
| 501 |
return FALSE; |
| 502 |
} |
| 503 |
|
| 504 |
if (pipe(pipefd) == -1) { |
| 505 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 506 |
"Error creating pipe."); |
| 507 |
return FALSE; |
| 508 |
} |
| 509 |
|
| 510 |
pid = fork (); |
| 511 |
|
| 512 |
if (pid == -1) { |
| 513 |
close (pipefd[0]); |
| 514 |
close (pipefd[1]); |
| 515 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 516 |
"Error forking."); |
| 517 |
return FALSE; |
| 518 |
} else if (pid == 0) { |
| 519 |
close (pipefd[0]); |
| 520 |
|
| 521 |
if (run_as_gid != current_gid) { |
| 522 |
if (!set_rgid (run_as_gid, error)) { |
| 523 |
if (write(pipefd[1], (*error)->message, strlen((*error)->message)) < 0) |
| 524 |
_exit (BD_FS_ERROR_PIPE); |
| 525 |
else |
| 526 |
_exit ((*error)->code); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
if (run_as_uid != current_uid) { |
| 531 |
if (!set_ruid (run_as_uid, error)) { |
| 532 |
if (write(pipefd[1], (*error)->message, strlen((*error)->message)) < 0) |
| 533 |
_exit (BD_FS_ERROR_PIPE); |
| 534 |
else |
| 535 |
_exit ((*error)->code); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
if (!func (args, error)) { |
| 540 |
if (write(pipefd[1], (*error)->message, strlen((*error)->message)) < 0) |
| 541 |
_exit (BD_FS_ERROR_PIPE); |
| 542 |
else |
| 543 |
_exit ((*error)->code); |
| 544 |
} |
| 545 |
|
| 546 |
_exit (EXIT_SUCCESS); |
| 547 |
|
| 548 |
} else { |
| 549 |
close (pipefd[1]); |
| 550 |
|
| 551 |
do { |
| 552 |
wpid = waitpid (pid, &status, WUNTRACED | WCONTINUED); |
| 553 |
if (wpid == -1) { |
| 554 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 555 |
"Error while waiting for process."); |
| 556 |
close (pipefd[0]); |
| 557 |
return FALSE; |
| 558 |
} |
| 559 |
|
| 560 |
if (WIFEXITED (status)) { |
| 561 |
if (WEXITSTATUS (status) != EXIT_SUCCESS) { |
| 562 |
if (WEXITSTATUS (status) == BD_FS_ERROR_PIPE) { |
| 563 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 564 |
"Error while reading error."); |
| 565 |
close (pipefd[0]); |
| 566 |
return FALSE; |
| 567 |
} |
| 568 |
|
| 569 |
channel = g_io_channel_unix_new (pipefd[0]); |
| 570 |
if (g_io_channel_read_to_end (channel, &error_msg, &msglen, &local_error) != G_IO_STATUS_NORMAL) { |
| 571 |
if (local_error) { |
| 572 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 573 |
"Error while reading error: %s (%d)", |
| 574 |
local_error->message, local_error->code); |
| 575 |
g_clear_error (&local_error); |
| 576 |
} else |
| 577 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 578 |
"Unknown error while reading error."); |
| 579 |
g_io_channel_unref (channel); |
| 580 |
close (pipefd[0]); |
| 581 |
g_free (error_msg); |
| 582 |
return FALSE; |
| 583 |
} |
| 584 |
|
| 585 |
if (g_io_channel_shutdown (channel, TRUE, &local_error) == G_IO_STATUS_ERROR) { |
| 586 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 587 |
"Error shutting down GIO channel: %s (%d)", |
| 588 |
local_error->message, local_error->code); |
| 589 |
g_clear_error (&local_error); |
| 590 |
g_io_channel_unref (channel); |
| 591 |
close (pipefd[0]); |
| 592 |
g_free (error_msg); |
| 593 |
return FALSE; |
| 594 |
} |
| 595 |
|
| 596 |
if (WEXITSTATUS (status) > BD_FS_ERROR_AUTH) |
| 597 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 598 |
error_msg); |
| 599 |
else |
| 600 |
g_set_error_literal (error, BD_FS_ERROR, WEXITSTATUS (status), |
| 601 |
error_msg); |
| 602 |
|
| 603 |
g_io_channel_unref (channel); |
| 604 |
close (pipefd[0]); |
| 605 |
g_free (error_msg); |
| 606 |
return FALSE; |
| 607 |
} else { |
| 608 |
close (pipefd[0]); |
| 609 |
return TRUE; |
| 610 |
} |
| 611 |
} else if (WIFSIGNALED (status)) { |
| 612 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 613 |
"Killed by signal %d.", WTERMSIG(status)); |
| 614 |
close (pipefd[0]); |
| 615 |
return FALSE; |
| 616 |
} |
| 617 |
|
| 618 |
} while (!WIFEXITED (status) && !WIFSIGNALED (status)); |
| 619 |
close (pipefd[0]); |
| 620 |
} |
| 621 |
|
| 622 |
return FALSE; |
| 623 |
} |
| 624 |
|
| 625 |
/** |
| 626 |
* bd_fs_unmount: |
| 627 |
* @spec: mount point or device to unmount |
| 628 |
* @lazy: enable/disable lazy unmount |
| 629 |
* @force: enable/disable force unmount |
| 630 |
* @extra: (nullable) (array zero-terminated=1): extra options for the unmount; |
| 631 |
* currently only 'run_as_uid' |
| 632 |
* and 'run_as_gid' are supported; |
| 633 |
* value must be a valid non zero |
| 634 |
* uid (gid), if you specify one of |
| 635 |
* these, the function will run in |
| 636 |
* a child process with real user |
| 637 |
* and/or group ID set to these values. |
| 638 |
* @error: (out) (optional): place to store error (if any) |
| 639 |
* |
| 640 |
* Returns: whether @spec was successfully unmounted or not |
| 641 |
* |
| 642 |
* Tech category: %BD_FS_TECH_MOUNT (no mode, ignored) |
| 643 |
*/ |
| 644 |
gboolean bd_fs_unmount (const gchar *spec, gboolean lazy, gboolean force, const BDExtraArg **extra, GError **error) { |
| 645 |
uid_t run_as_uid = -1; |
| 646 |
gid_t run_as_gid = -1; |
| 647 |
uid_t current_uid = -1; |
| 648 |
gid_t current_gid = -1; |
| 649 |
const BDExtraArg **extra_p = NULL; |
| 650 |
gchar *endptr = NULL; |
| 651 |
MountArgs args = ZERO_INIT; |
| 652 |
GError *l_error = NULL; |
| 653 |
gboolean ret = FALSE; |
| 654 |
|
| 655 |
args.spec = spec; |
| 656 |
args.lazy = lazy; |
| 657 |
args.force = force; |
| 658 |
|
| 659 |
current_uid = getuid (); |
| 660 |
run_as_uid = current_uid; |
| 661 |
|
| 662 |
current_gid = getgid (); |
| 663 |
run_as_gid = current_gid; |
| 664 |
|
| 665 |
if (extra) { |
| 666 |
for (extra_p=extra; *extra_p; extra_p++) { |
| 667 |
if ((*extra_p)->opt && (g_strcmp0 ((*extra_p)->opt, "run_as_uid") == 0)) { |
| 668 |
run_as_uid = g_ascii_strtoull ((*extra_p)->val, &endptr, 0); |
| 669 |
|
| 670 |
/* g_ascii_strtoull returns 0 in case of error */ |
| 671 |
if (run_as_uid == 0 && endptr == (*extra_p)->val) { |
| 672 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 673 |
"Invalid specification of UID: '%s'", (*extra_p)->val); |
| 674 |
return FALSE; |
| 675 |
} |
| 676 |
} else if ((*extra_p)->opt && (g_strcmp0 ((*extra_p)->opt, "run_as_gid") == 0)) { |
| 677 |
run_as_gid = g_ascii_strtoull ((*extra_p)->val, &endptr, 0); |
| 678 |
|
| 679 |
/* g_ascii_strtoull returns 0 in case of error */ |
| 680 |
if (run_as_gid == 0 && endptr == (*extra_p)->val) { |
| 681 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 682 |
"Invalid specification of GID: '%s'", (*extra_p)->val); |
| 683 |
return FALSE; |
| 684 |
} |
| 685 |
} else { |
| 686 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 687 |
"Unsupported argument for unmount: '%s'", (*extra_p)->opt); |
| 688 |
return FALSE; |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
if (run_as_uid != current_uid || run_as_gid != current_gid) { |
| 694 |
ret = run_as_user ((MountFunc) do_unmount, &args, run_as_uid, run_as_gid, &l_error); |
| 695 |
if (!ret) |
| 696 |
g_propagate_error (error, l_error); |
| 697 |
return ret; |
| 698 |
} else |
| 699 |
return do_unmount (&args, error); |
| 700 |
} |
| 701 |
|
| 702 |
/** |
| 703 |
* bd_fs_mount: |
| 704 |
* @device: (nullable): device to mount, if not specified @mountpoint entry |
| 705 |
* from fstab will be used |
| 706 |
* @mountpoint: (nullable): mountpoint for @device, if not specified @device |
| 707 |
* entry from fstab will be used |
| 708 |
* @fstype: (nullable): filesystem type |
| 709 |
* @options: (nullable): comma delimited options for mount |
| 710 |
* @extra: (nullable) (array zero-terminated=1): extra options for the mount; |
| 711 |
* currently only 'run_as_uid' |
| 712 |
* and 'run_as_gid' are supported; |
| 713 |
* value must be a valid non zero |
| 714 |
* uid (gid), if you specify one of |
| 715 |
* these, the function will run in |
| 716 |
* a child process with real user |
| 717 |
* and/or group ID set to these values. |
| 718 |
* @error: (out) (optional): place to store error (if any) |
| 719 |
* |
| 720 |
* Returns: whether @device (or @mountpoint) was successfully mounted or not |
| 721 |
* |
| 722 |
* Tech category: %BD_FS_TECH_MOUNT (no mode, ignored) |
| 723 |
*/ |
| 724 |
gboolean bd_fs_mount (const gchar *device, const gchar *mountpoint, const gchar *fstype, const gchar *options, const BDExtraArg **extra, GError **error) { |
| 725 |
uid_t run_as_uid = -1; |
| 726 |
gid_t run_as_gid = -1; |
| 727 |
uid_t current_uid = -1; |
| 728 |
gid_t current_gid = -1; |
| 729 |
const BDExtraArg **extra_p = NULL; |
| 730 |
gchar *endptr = NULL; |
| 731 |
MountArgs args = ZERO_INIT; |
| 732 |
GError *l_error = NULL; |
| 733 |
gboolean ret = FALSE; |
| 734 |
|
| 735 |
args.device = device; |
| 736 |
args.mountpoint = mountpoint; |
| 737 |
args.fstype = fstype; |
| 738 |
args.options = options; |
| 739 |
|
| 740 |
current_uid = getuid (); |
| 741 |
run_as_uid = current_uid; |
| 742 |
|
| 743 |
current_gid = getgid (); |
| 744 |
run_as_gid = current_gid; |
| 745 |
|
| 746 |
if (extra) { |
| 747 |
for (extra_p=extra; *extra_p; extra_p++) { |
| 748 |
if ((*extra_p)->opt && (g_strcmp0 ((*extra_p)->opt, "run_as_uid") == 0)) { |
| 749 |
run_as_uid = g_ascii_strtoull ((*extra_p)->val, &endptr, 0); |
| 750 |
|
| 751 |
/* g_ascii_strtoull returns 0 in case of error */ |
| 752 |
if (run_as_uid == 0 && endptr == (*extra_p)->val) { |
| 753 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 754 |
"Invalid specification of UID: '%s'", (*extra_p)->val); |
| 755 |
return FALSE; |
| 756 |
} |
| 757 |
} else if ((*extra_p)->opt && (g_strcmp0 ((*extra_p)->opt, "run_as_gid") == 0)) { |
| 758 |
run_as_gid = g_ascii_strtoull ((*extra_p)->val, &endptr, 0); |
| 759 |
|
| 760 |
/* g_ascii_strtoull returns 0 in case of error */ |
| 761 |
if (run_as_gid == 0 && endptr == (*extra_p)->val) { |
| 762 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 763 |
"Invalid specification of GID: '%s'", (*extra_p)->val); |
| 764 |
return FALSE; |
| 765 |
} |
| 766 |
} else { |
| 767 |
g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 768 |
"Unsupported argument for mount: '%s'", (*extra_p)->opt); |
| 769 |
return FALSE; |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
if (run_as_uid != current_uid || run_as_gid != current_gid) { |
| 775 |
ret = run_as_user ((MountFunc) do_mount, &args, run_as_uid, run_as_gid, &l_error); |
| 776 |
if (!ret) |
| 777 |
g_propagate_error (error, l_error); |
| 778 |
return ret; |
| 779 |
} else |
| 780 |
return do_mount (&args, error); |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* bd_fs_get_mountpoint: |
| 785 |
* @device: device to find mountpoint for |
| 786 |
* @error: (out) (optional): place to store error (if any) |
| 787 |
* |
| 788 |
* Get mountpoint for @device. If @device is mounted multiple times only |
| 789 |
* one mountpoint will be returned. |
| 790 |
* |
| 791 |
* Returns: (transfer full): mountpoint for @device, %NULL in case device is |
| 792 |
* not mounted or in case of an error (@error is set |
| 793 |
* in this case) |
| 794 |
* |
| 795 |
* Tech category: %BD_FS_TECH_MOUNT (no mode, ignored) |
| 796 |
*/ |
| 797 |
gchar* bd_fs_get_mountpoint (const gchar *device, GError **error) { |
| 798 |
struct libmnt_table *table = NULL; |
| 799 |
struct libmnt_fs *fs = NULL; |
| 800 |
struct libmnt_cache *cache = NULL; |
| 801 |
gint ret = 0; |
| 802 |
gchar *mountpoint = NULL; |
| 803 |
const gchar *target = NULL; |
| 804 |
|
| 805 |
table = mnt_new_table (); |
| 806 |
cache = mnt_new_cache (); |
| 807 |
|
| 808 |
ret = mnt_table_set_cache (table, cache); |
| 809 |
if (ret != 0) { |
| 810 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 811 |
"Failed to set cache for mount info table."); |
| 812 |
mnt_free_table (table); |
| 813 |
mnt_free_cache (cache); |
| 814 |
return NULL; |
| 815 |
} |
| 816 |
|
| 817 |
ret = mnt_table_parse_mtab (table, NULL); |
| 818 |
if (ret != 0) { |
| 819 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 820 |
"Failed to parse mount info."); |
| 821 |
mnt_free_table (table); |
| 822 |
mnt_free_cache (cache); |
| 823 |
return NULL; |
| 824 |
} |
| 825 |
|
| 826 |
fs = mnt_table_find_source (table, device, MNT_ITER_FORWARD); |
| 827 |
if (!fs) { |
| 828 |
mnt_free_table (table); |
| 829 |
mnt_free_cache (cache); |
| 830 |
return NULL; |
| 831 |
} |
| 832 |
|
| 833 |
target = mnt_fs_get_target (fs); |
| 834 |
if (!target) { |
| 835 |
mnt_free_table (table); |
| 836 |
mnt_free_cache (cache); |
| 837 |
return NULL; |
| 838 |
} |
| 839 |
|
| 840 |
mountpoint = g_strdup (target); |
| 841 |
mnt_free_table (table); |
| 842 |
mnt_free_cache (cache); |
| 843 |
return mountpoint; |
| 844 |
} |
| 845 |
|
| 846 |
/** |
| 847 |
* bd_fs_is_mountpoint: |
| 848 |
* @path: path (folder) to check |
| 849 |
* @error: (out) (optional): place to store error (if any) |
| 850 |
* |
| 851 |
* Returns: whether @path is a mountpoint or not |
| 852 |
* |
| 853 |
* Tech category: %BD_FS_TECH_MOUNT (no mode, ignored) |
| 854 |
*/ |
| 855 |
gboolean bd_fs_is_mountpoint (const gchar *path, GError **error) { |
| 856 |
struct libmnt_table *table = NULL; |
| 857 |
struct libmnt_fs *fs = NULL; |
| 858 |
struct libmnt_cache *cache = NULL; |
| 859 |
const gchar *target = NULL; |
| 860 |
gint ret = 0; |
| 861 |
|
| 862 |
table = mnt_new_table (); |
| 863 |
cache = mnt_new_cache (); |
| 864 |
|
| 865 |
ret = mnt_table_set_cache (table, cache); |
| 866 |
if (ret != 0) { |
| 867 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 868 |
"Failed to set cache for mount info table."); |
| 869 |
mnt_free_table (table); |
| 870 |
mnt_free_cache (cache); |
| 871 |
return FALSE; |
| 872 |
} |
| 873 |
|
| 874 |
ret = mnt_table_parse_mtab (table, NULL); |
| 875 |
if (ret != 0) { |
| 876 |
g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_FAIL, |
| 877 |
"Failed to parse mount info."); |
| 878 |
mnt_free_table (table); |
| 879 |
mnt_free_cache (cache); |
| 880 |
return FALSE; |
| 881 |
} |
| 882 |
|
| 883 |
fs = mnt_table_find_target (table, path, MNT_ITER_BACKWARD); |
| 884 |
if (!fs) { |
| 885 |
mnt_free_table (table); |
| 886 |
mnt_free_cache (cache); |
| 887 |
return FALSE; |
| 888 |
} |
| 889 |
|
| 890 |
target = mnt_fs_get_target (fs); |
| 891 |
if (!target) { |
| 892 |
mnt_free_table (table); |
| 893 |
mnt_free_cache (cache); |
| 894 |
return FALSE; |
| 895 |
} |
| 896 |
|
| 897 |
mnt_free_table (table); |
| 898 |
mnt_free_cache (cache); |
| 899 |
return TRUE; |
| 900 |
} |