static int ntfs_drive_letter(ntfs_volume *vol, ntfschar letter) { char defines[NTFS_MAX_NAME_LEN + 5]; char *drive; int ret; int sz; int olderrno; ntfs_inode *ni; ret = -1; drive = (char*)NULL; sz = ntfs_ucstombs(&letter, 1, &drive, 0); if (sz > 0) { strcpy(defines,mappingdir); if ((*drive >= 'a') && (*drive <= 'z')) *drive += 'A' - 'a'; strcat(defines,drive); strcat(defines,":"); olderrno = errno; ni = ntfs_pathname_to_inode(vol, NULL, defines); if (ni && !ntfs_inode_close(ni)) ret = 1; else if (errno == ENOENT) { ret = 0; /* avoid errno pollution */ errno = olderrno; } } if (drive) free(drive); return (ret); }
struct XATTRMAPPING *ntfs_xattr_build_mapping(ntfs_volume *vol, const char *xattrmap_path) { struct XATTRMAPPING *firstmapping; struct XATTRMAPPING *mapping; BOOL user_efs; BOOL notfound; ntfs_inode *ni; int fd; firstmapping = (struct XATTRMAPPING*)NULL; notfound = FALSE; if (!xattrmap_path) xattrmap_path = XATTRMAPPINGFILE; if (xattrmap_path[0] == '/') { fd = open(xattrmap_path,O_RDONLY); if (fd > 0) { firstmapping = ntfs_read_xattr_mapping(basicread, (void*)&fd); close(fd); } else notfound = TRUE; } else { ni = ntfs_pathname_to_inode(vol, NULL, xattrmap_path); if (ni) { firstmapping = ntfs_read_xattr_mapping(localread, ni); ntfs_inode_close(ni); } else notfound = TRUE; } if (notfound && strcmp(xattrmap_path, XATTRMAPPINGFILE)) { ntfs_log_early_error("Could not open \"%s\"\n",xattrmap_path); } if (vol->efs_raw) { user_efs = TRUE; for (mapping=firstmapping; mapping; mapping=mapping->next) if (mapping->xattr == XATTR_NTFS_EFSINFO) user_efs = FALSE; } else user_efs = FALSE; if (user_efs) { mapping = (struct XATTRMAPPING*)ntfs_malloc( sizeof(struct XATTRMAPPING) + strlen(nf_ns_alt_xattr_efsinfo)); if (mapping) { mapping->next = firstmapping; mapping->xattr = XATTR_NTFS_EFSINFO; strcpy(mapping->name,nf_ns_alt_xattr_efsinfo); firstmapping = mapping; } } return (firstmapping); }
/** * 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; }
void ntfsrec_command_cd(struct ntfsrec_command_processor *state, char *arguments) { char cwd_buffer[MAX_PATH_LENGTH]; ntfs_inode *inode; if (*arguments == '\0') { puts("Usage: cd <directory>"); return; } ntfsrec_check_trailing_slash(arguments); if (ntfsrec_calculate_path(cwd_buffer, MAX_PATH_LENGTH, state->cwd, arguments) == NR_FALSE) { printf("Error: invalid path arugment %s\n", arguments); return; } inode = ntfs_pathname_to_inode(state->reader->mount.volume, NULL, cwd_buffer); if (inode == NULL) { printf("Error: can't find path %s\n", cwd_buffer); return; } if (inode->mrec->flags & MFT_RECORD_IS_DIRECTORY) { strncpy(state->cwd, cwd_buffer, MAX_PATH_LENGTH); } else { printf("Error: %s isn't a directory.\n", cwd_buffer); ntfs_inode_close(inode); return; } if (state->cwd_inode != NULL) ntfs_inode_close(state->cwd_inode); state->cwd_inode = inode; }
/** * PRIVATE: Callback for directory walking */ int ntfs_readdir_filler (DIR_ITER *dirState, const ntfschar *name, const int name_len, const int name_type, const s64 pos, const MFT_REF mref, const unsigned dt_type) { ntfs_dir_state *dir = STATE(dirState); ntfs_dir_entry *entry = NULL; char *entry_name = NULL; // Sanity check if (!dir || !dir->vd) { errno = EINVAL; return -1; } // Ignore DOS file names if (name_type == FILE_NAME_DOS) { return 0; } // Preliminary check that this entry can be enumerated (as described by the volume descriptor) if (MREF(mref) == FILE_root || MREF(mref) >= FILE_first_user || dir->vd->showSystemFiles) { // Convert the entry name to our current local if (ntfsUnicodeToLocal(name, name_len, &entry_name, 0) < 0) { ntfs_free(entry); return -1; } // If this is not the parent or self directory reference if ((strcmp(entry_name, ".") != 0) && (strcmp(entry_name, "..") != 0)) { // Open the entry ntfs_inode *ni = ntfs_pathname_to_inode(dir->vd->vol, dir->ni, entry_name); if (!ni) { ntfs_free(entry); return -1; } // Double check that this entry can be emuerated (as described by the volume descriptor) if (((ni->flags & FILE_ATTR_HIDDEN) && !dir->vd->showHiddenFiles) || ((ni->flags & FILE_ATTR_SYSTEM) && !dir->vd->showSystemFiles)) { ntfs_inode_close(ni); return 0; } // Close the entry ntfs_inode_close(ni); } // Allocate a new directory entry entry = ntfs_alloc(sizeof(ntfs_dir_entry)); if (!entry) return -1; // Setup the entry entry->name = entry_name; entry->next = NULL; // Link the entry to the directory if (!dir->first) { dir->first = entry; } else { ntfs_dir_entry *last = dir->first; while (last->next) last = last->next; last->next = entry; } } return 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; }
/** * 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; }
ntfs_inode *ntfsParseEntry (ntfs_vd *vd, const char *path, int reparseLevel) { ntfs_inode *ni = NULL; char *target = NULL; int attr_size; // Sanity check if (!vd) { errno = ENODEV; return NULL; } // Get the actual path of the entry path = ntfsRealPath(path); if (!path) { errno = EINVAL; return NULL; } else if (path[0] == '\0') { path = "."; } // Find the entry, taking into account our current directory (if any) if (path[0] != PATH_SEP) ni = ntfs_pathname_to_inode(vd->vol, vd->cwd_ni, path++); else ni = ntfs_pathname_to_inode(vd->vol, NULL, path); // If the entry was found and it has reparse data then parse its true path; // this resolves the true location of symbolic links and directory junctions if (ni && (ni->flags & FILE_ATTR_REPARSE_POINT)) { if (ntfs_possible_symlink(ni)) { // Sanity check, give up if we are parsing to deep if (reparseLevel > NTFS_MAX_SYMLINK_DEPTH) { ntfsCloseEntry(vd, ni); errno = ELOOP; return NULL; } // Get the target path of this entry target = ntfs_make_symlink(ni, path, &attr_size); if (!target) { ntfsCloseEntry(vd, ni); return NULL; } // Close the entry (we are no longer interested in it) ntfsCloseEntry(vd, ni); // Parse the entries target ni = ntfsParseEntry(vd, target, reparseLevel++); // Clean up // use free because the value was not allocated with ntfs_alloc free(target); } } return ni; }