Ejemplo n.º 1
0
/**
 * main - Begin here
 *
 * Start from here.
 *
 * Return:  0  Success, the program worked
 *	    1  Error, something went wrong
 */
int main(int argc, char **argv)
{
	unsigned long mnt_flags = 0;
	int result = 0;
	ntfs_volume *vol;

	ntfs_log_set_handler(ntfs_log_handler_outerr);

	if (!parse_options(argc, argv))
		return 1;

	utils_set_locale();

	if (!opts.label)
		opts.noaction++;

	vol = utils_mount_volume(opts.device,
			(opts.noaction ? MS_RDONLY : 0) |
			(opts.force ? MS_RECOVER : 0));
	if (!vol)
		return 1;

	if (opts.label)
		result = change_label(vol, mnt_flags, opts.label, opts.force);
	else
		result = print_label(vol, mnt_flags);

	ntfs_umount(vol, FALSE);
	return result;
}
Ejemplo n.º 2
0
void ntfsInit (void)
{
    static bool isInit = false;

    // Initialise ntfs-3g (if not already done so)
    if (!isInit) {
        isInit = true;

        // Set the log handler
        #ifdef NTFS_ENABLE_LOG
        ntfs_log_set_handler(ntfs_log_handler_stderr);
        #else
        ntfs_log_set_handler(ntfs_log_handler_null);
        #endif
    }

    return;
}
Ejemplo n.º 3
0
/**
 * main - Begin here
 *
 * Start from here.
 *
 * Return:  0  Success, the program worked
 *	    1  Error, something went wrong
 */
int main(int argc, char **argv)
{
	unsigned long mnt_flags = 0;
	int result = 0;
	ntfs_volume *vol;

	ntfs_log_set_handler(ntfs_log_handler_outerr);

	result = parse_options(argc, argv);
	if (result >= 0)
		return (result);

	result = 0;
	utils_set_locale();

	if ((opts.label || opts.new_serial)
	    && !opts.noaction
	    && !opts.force
	    && !ntfs_check_if_mounted(opts.device, &mnt_flags)
	    && (mnt_flags & NTFS_MF_MOUNTED)) {
		ntfs_log_error("Cannot make changes to a mounted device\n");
		result = 1;
		goto abort;
	}

	if (!opts.label && !opts.new_serial)
		opts.noaction++;

	vol = utils_mount_volume(opts.device,
			(opts.noaction ? NTFS_MNT_RDONLY : 0) |
			(opts.force ? NTFS_MNT_RECOVER : 0));
	if (!vol)
		return 1;

	if (opts.new_serial) {
		result = set_new_serial(vol);
		if (result)
			goto unmount;
	} else {
		if (opts.verbose)
			result = print_serial(vol);
	}
	if (opts.label)
		result = change_label(vol, opts.label);
	else
		result = print_label(vol, mnt_flags);

unmount :
	ntfs_umount(vol, FALSE);
abort :
		/* "result" may be a negative reply of a library function */
	return (result ? 1 : 0);
}
Ejemplo n.º 4
0
int
fstyp_mod_init(int fd, off64_t offset, fstyp_mod_handle_t *handle)
{
    fstyp_ntfs_t *h;
    int* d_private;
    
    if (offset != 0) {
        return (FSTYP_ERR_OFFSET);
    }
    
    d_private = malloc(sizeof(int));
    if(d_private == NULL) {
        return (FSTYP_ERR_NOMEM);
    }

    if ((h = calloc(1, sizeof (fstyp_ntfs_t))) == NULL) {
        free(d_private);
        return (FSTYP_ERR_NOMEM);
    }
    
    h->dev = NULL;
    h->vol = NULL;
	h->attr = NULL;
    h->fd = fd;

    *d_private = fd;
    
    /* Ignore all logs! */
    ntfs_log_set_handler(ntfs_log_handler_null);

    /* XXX: Hack our open routine cause we already have fd */
    if(!io_ops_initialized) {
        memcpy(&ntfs_device_fstyp_io_ops,
               &ntfs_device_default_io_ops,
               sizeof(struct ntfs_device_operations));
        
        ntfs_device_fstyp_io_ops.open  = ntfs_device_fstyp_io_open;
        ntfs_device_fstyp_io_ops.close = ntfs_device_fstyp_io_close;
        
        io_ops_initialized = B_TRUE;
    }
    
    h->dev = ntfs_device_alloc("ntfs", 0, &ntfs_device_fstyp_io_ops, d_private);
    if(h->dev == NULL) {
        free(d_private);
        free(h);
        return (FSTYP_ERR_NOMEM);
    }

    *handle = (fstyp_mod_handle_t)h;
    return (0);
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
	int err;

	ntfs_log_set_handler(ntfs_log_handler_stderr);

	if (parse_options(argc, argv)) {
		usage();
		exit(NTFS_VOLUME_SYNTAX_ERROR);
	}

	err = ntfs_open(opts.device);

	free(opts.device);
	exit(err);
}
Ejemplo n.º 6
0
/**
 * main - Begin here
 *
 * Start from here.
 *
 * Return:  0  Success, the program worked
 *	    1  Error, something went wrong
 */
int main(int argc, char *argv[])
{
	ntfs_volume *vol;
	ntfs_inode *inode;
	ATTR_TYPES attr;
	int result = 1;

	ntfs_log_set_handler(ntfs_log_handler_stderr);

	if (!parse_options(argc, argv))
		return 1;

	utils_set_locale();

	vol = utils_mount_volume(opts.device, MS_RDONLY |
			(opts.force ? MS_RECOVER : 0));
	if (!vol) {
		ntfs_log_perror("ERROR: couldn't mount volume");
		return 1;
	}

	if (opts.inode != -1)
		inode = ntfs_inode_open(vol, opts.inode);
	else
		inode = ntfs_pathname_to_inode(vol, NULL, opts.file);

	if (!inode) {
		ntfs_log_perror("ERROR: Couldn't open inode");
		return 1;
	}

	attr = AT_DATA;
	if (opts.attr != cpu_to_le32(-1))
		attr = opts.attr;

	result = cat(vol, inode, attr, opts.attr_name, opts.attr_name_len);

	ntfs_inode_close(inode);
	ntfs_umount(vol, FALSE);

	return result;
}
Ejemplo n.º 7
0
/**
 * main - Begin here
 *
 * Start from here.
 *
 * Return:  0  Success, the program worked
 *	    1  Error, something went wrong
 */
int main(int argc, char *argv[])
{
    u8 *pfx_buf;
    char *password;
    ntfs_rsa_private_key rsa_key;
    ntfs_volume *vol;
    ntfs_inode *inode;
    ntfs_fek *fek;
    unsigned pfx_size;
    int res;
    NTFS_DF_TYPES df_type;
    char thumbprint[NTFS_SHA1_THUMBPRINT_SIZE];

#ifdef DEBUG
    ntfs_log_set_handler(ntfs_log_handler_stderr);
#endif

    if (!parse_options(argc, argv))
        return 1;
    utils_set_locale();

    /* Initialize crypto in ntfs. */
    if (ntfs_crypto_init()) {
        ntfs_log_error("Failed to initialize crypto.  Aborting.\n");
        return 1;
    }
    /* Load the PKCS#12 (.pfx) file containing the user's private key. */
    if (ntfs_pkcs12_load_pfxfile(opts.keyfile, &pfx_buf, &pfx_size)) {
        ntfs_log_error("Failed to load key file.  Aborting.\n");
        ntfs_crypto_deinit();
        return 1;
    }
    /* Ask the user for their password. */
    password = getpass("Enter the password with which the private key was "
                       "encrypted: ");
    if (!password) {
        ntfs_log_perror("Failed to obtain user password");
        free(pfx_buf);
        ntfs_crypto_deinit();
        return 1;
    }
    /* Obtain the user's private RSA key from the key file. */
    rsa_key = ntfs_pkcs12_extract_rsa_key(pfx_buf, pfx_size, password,
                                          thumbprint, sizeof(thumbprint), &df_type);
    /* Destroy the password. */
    memset(password, 0, strlen(password));
    /* No longer need the pfx file contents. */
    free(pfx_buf);
    if (!rsa_key) {
        ntfs_log_error("Failed to extract the private RSA key.\n");
        ntfs_crypto_deinit();
        return 1;
    }
    /* Mount the ntfs volume. */
    vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY |
                             (opts.force ? NTFS_MNT_RECOVER : 0));
    if (!vol) {
        ntfs_log_error("Failed to mount ntfs volume.  Aborting.\n");
        ntfs_rsa_private_key_release(rsa_key);
        ntfs_crypto_deinit();
        return 1;
    }
    /* Open the encrypted ntfs file. */
    if (opts.inode != -1)
        inode = ntfs_inode_open(vol, opts.inode);
    else
        inode = ntfs_pathname_to_inode(vol, NULL, opts.file);
    if (!inode) {
        ntfs_log_error("Failed to open encrypted file.  Aborting.\n");
        ntfs_umount(vol, FALSE);
        ntfs_rsa_private_key_release(rsa_key);
        ntfs_crypto_deinit();
        return 1;
    }
    /* Obtain the file encryption key of the encrypted file. */
    fek = ntfs_inode_fek_get(inode, rsa_key, thumbprint,
                             sizeof(thumbprint), df_type);
    ntfs_rsa_private_key_release(rsa_key);
    if (fek) {
        res = ntfs_cat_decrypt(inode, fek);
        ntfs_fek_release(fek);
    } else {
        ntfs_log_error("Failed to obtain file encryption key.  "
                       "Aborting.\n");
        res = 1;
    }
    ntfs_inode_close(inode);
    ntfs_umount(vol, FALSE);
    ntfs_crypto_deinit();
    return res;
}
Ejemplo n.º 8
0
dir_partition_t dir_partition_ntfs_init(disk_t *disk_car, const partition_t *partition, dir_data_t *dir_data, const int verbose, const int expert)
{
#if defined(HAVE_LIBNTFS) || defined(HAVE_LIBNTFS3G)
  struct ntfs_device *dev;
  my_data_t *my_data=NULL;
  ntfs_volume *vol=NULL;
#ifdef NTFS_LOG_LEVEL_VERBOSE
  ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE);
  ntfs_log_set_handler(ntfs_log_handler_stderr);
#endif

  dev = ntfs_device_alloc("/", 0, &ntfs_device_testdisk_io_ops, NULL);
  if (dev)
  {
    my_data=(my_data_t *)MALLOC(sizeof(*my_data));
    my_data->partition=partition;
    my_data->disk_car=disk_car;
    my_data->offset=0;
    dev->d_private=my_data;
    /* Call ntfs_device_mount() to do the actual mount. */
#ifdef MS_RDONLY
    vol = ntfs_device_mount(dev, MS_RDONLY);
#else
    vol = ntfs_device_mount(dev, NTFS_MNT_RDONLY);
#endif
#ifdef HAVE_NTFS_VOLUME_STARTUP
    if(!vol) {
#ifdef MS_RDONLY
      vol = ntfs_volume_startup(dev, MS_RDONLY);
#else
      vol = ntfs_volume_startup(dev, NTFS_MNT_RDONLY);
#endif
      if(vol)
      {
	log_warning("NTFS filesystem needs to be repaired.\n");
      }
    }
#endif
  }
  if (!vol) {
    free(my_data);
    ntfs_device_free(dev);
    return DIR_PART_EIO;
  }
  if (vol->flags & VOLUME_IS_DIRTY) {
    log_warning("NTFS Volume is dirty.\n");
  }
  {
    struct ntfs_dir_struct *ls=(struct ntfs_dir_struct *)MALLOC(sizeof(*ls));
    ls->dir_list=NULL;
    ls->vol=vol;
    ls->my_data=my_data;
    ls->dir_data=dir_data;
#ifdef HAVE_ICONV
    if ((ls->cd = iconv_open("UTF-8", "UTF-16LE")) == (iconv_t)(-1))
    {
      log_error("ntfs_ucstoutf8: iconv_open failed\n");
    }
#endif
    strncpy(dir_data->current_directory,"/",sizeof(dir_data->current_directory));
    dir_data->current_inode=FILE_root;
    dir_data->param=FLAG_LIST_ADS;
    if(expert!=0)
      dir_data->param|=FLAG_LIST_SYSTEM;
    dir_data->verbose=verbose;
    dir_data->capabilities=CAPA_LIST_ADS;
    dir_data->get_dir=&ntfs_dir;
    dir_data->copy_file=&ntfs_copy;
    dir_data->close=&dir_partition_ntfs_close;
    dir_data->local_dir=NULL;
    dir_data->private_dir_data=ls;
  }
  return DIR_PART_OK;
#else
  return DIR_PART_ENOSYS;
#endif
}
Ejemplo n.º 9
0
/**
 * main
 */
int main(int argc, char **argv)
{
	unsigned long mnt_flags, ul;
	int err;
	ntfs_inode *ni;
	ntfs_volume *vol;
#ifdef HAVE_WINDOWS_H
	char *unix_name;
#endif

	vol = (ntfs_volume*)NULL;
	ntfs_log_set_handler(ntfs_log_handler_outerr);

	/* Initialize opts to zero / required values. */
	memset(&opts, 0, sizeof(opts));

	/* Parse command line options. */
	parse_options(argc, argv);

	utils_set_locale();

	/* Make sure the file system is not mounted. */
	if (ntfs_check_if_mounted(dev_name, &mnt_flags))
		ntfs_log_perror("Failed to determine whether %s is mounted",
				dev_name);
	else if (mnt_flags & NTFS_MF_MOUNTED) {
		ntfs_log_error("%s is mounted.\n", dev_name);
		if (!opts.force)
			err_exit((ntfs_volume*)NULL, "Refusing to run!\n");
		fprintf(stderr, "ntfsfallocate forced anyway. Hope /etc/mtab "
				"is incorrect.\n");
	}

	/* Mount the device. */
	if (opts.no_action) {
		ntfs_log_quiet("Running in READ-ONLY mode!\n");
		ul = NTFS_MNT_RDONLY;
	} else
		if (opts.force)
			ul = NTFS_MNT_RECOVER;
		else
			ul = 0;
	vol = ntfs_mount(dev_name, ul);
	if (!vol)
		err_exit(vol, "Failed to mount %s: %s\n", dev_name,
			strerror(errno));

	if ((vol->flags & VOLUME_IS_DIRTY) && !opts.force)
		err_exit(vol, "Volume is dirty, please run chkdsk.\n");

	if (ntfs_volume_get_free_space(vol))
		err_exit(vol, "Failed to get free clusters %s: %s\n",
					dev_name, strerror(errno));

	/* Open the specified inode. */
#ifdef HAVE_WINDOWS_H
	unix_name = (char*)malloc(strlen(file_name) + 1);
	if (unix_name) {
		int i;
		for (i=0; file_name[i]; i++)
			if (file_name[i] == '\\')
				unix_name[i] = '/';
			else
				unix_name[i] = file_name[i];
		unix_name[i] = 0;
		ni = ntfs_pathname_to_inode(vol, NULL, unix_name);
		free(unix_name);
	} else
		ni = (ntfs_inode*)NULL;
#else
	ni = ntfs_pathname_to_inode(vol, NULL, file_name);
#endif
	if (!ni)
		err_exit(vol, "Failed to open file \"%s\": %s\n", file_name,
				strerror(errno));
	if (!opts.no_action)
		err = ntfs_fallocate(ni, opt_alloc_offs, opt_alloc_len);

	/* Close the inode. */
	if (ntfs_inode_close(ni)) {
		err = -1;
		err_exit(vol, "Failed to close inode \"%s\" : %s\n", file_name,
				strerror(errno));
	}

	/* Unmount the volume. */
	err = ntfs_umount(vol, 0);
	vol = (ntfs_volume*)NULL;
	if (err)
		ntfs_log_perror("Warning: Failed to umount %s", dev_name);

	/* Free the attribute name if it exists. */
	if (attr_name_len)
		ntfs_ucsfree(attr_name);

	ntfs_log_quiet("ntfsfallocate completed successfully. Have a nice day.\n");
	return 0;
}