📄 cli.c
字号:
" The outer volume is opened with keyfiles ./key1 and ./key2 and the\n"" hidden volume with ./key_hidden. Passwords for both volumes are empty.\n""\n""Report bugs at <http://www.truecrypt.org/bugs>.\n" );}static BOOL DumpMountList (int devNo){ int i; if (!CheckKernelModuleVersion (FALSE)) return FALSE; if (!GetMountList ()) return FALSE; for (i = 0; MountList[i].DeviceNumber != -1; i++) { MountListEntry *e = &MountList[i]; if (devNo != -1 && e->DeviceNumber != devNo) continue; if (Verbose == 0) { printf (TC_MAP_DEV "%d %s\n", e->DeviceNumber, e->VolumePath); } else { char eaName[128]; EAGetName (eaName, e->EA); printf (TC_MAP_DEV "%d:\n" " Volume: %s\n" " Type: %s\n" " Size: %lld bytes\n" " Encryption algorithm: %s\n" " Mode of operation: %s\n" " Read-only: %s\n" " Hidden volume protected: %s\n\n", e->DeviceNumber, e->VolumePath, e->Hidden ? "Hidden" : "Normal", e->VolumeSize, eaName, EAGetModeName (e->EA, e->Mode, TRUE), (e->Flags & FLAG_READ_ONLY) ? "Yes" : "No", (e->Flags & FLAG_PROTECTION_ACTIVATED) ? "Yes - damage prevented" : ( (e->Flags & FLAG_HIDDEN_VOLUME_PROTECTION) ? "Yes" : "No" ) ); } } return TRUE;}static BOOL EnumMountPoints (char *device, char *mountPoint){ static FILE *m = NULL; if (device == NULL) { fclose (m); m = NULL; return TRUE; } if (m == NULL) { m = fopen ("/proc/mounts", "r"); if (m == NULL) { perror ("fopen /proc/mounts"); return FALSE; } } if (fscanf (m, "%" MAX_PATH_STR "s %" MAX_PATH_STR "s %*s %*s %*s %*s", device, mountPoint) != 2) { fclose (m); m = NULL; return FALSE; } return TRUE;}static BOOL DismountFileSystem (char *device){ char mountedDevice[MAX_PATH], mountPoint[MAX_PATH]; BOOL result = TRUE; while (EnumMountPoints (mountedDevice, mountPoint)) { if (strcmp (mountedDevice, device) == 0) { if (!Execute (FALSE, "umount", mountPoint, NULL)) result = FALSE; else if (Verbose >= 1) printf ("Dismounted %s\n", mountPoint); } } return result;}// devNo: -1 = Dismount all volumesstatic BOOL DismountVolume (int devNo){ char mapDevice[MAX_PATH]; int nMountedVolumes = 0; int i; BOOL found = FALSE; BOOL status = TRUE; if (!CheckKernelModuleVersion (FALSE)) return FALSE; if (!GetMountList ()) return FALSE; if (devNo == -1 && MountList[0].DeviceNumber == -1) { error ("No volumes mounted\n"); return FALSE; } // Flush write buffers before dismount if there are // mounted volumes with hidden volume protection for (i = 0; MountList[i].DeviceNumber != -1; i++) { if (MountList[i].Flags & FLAG_HIDDEN_VOLUME_PROTECTION) { sync (); MountListValid = FALSE; GetMountList (); break; } } for (i = 0; MountList[i].DeviceNumber != -1; i++) { MountListEntry *e = &MountList[i]; nMountedVolumes++; if (devNo == -1 || e->DeviceNumber == devNo) { BOOL dismounted = FALSE; found = TRUE; if (e->Flags & FLAG_PROTECTION_ACTIVATED) printf ("WARNING: Write to the hidden volume %s has been prevented!\n", e->VolumePath); sprintf (mapDevice, TC_MAP_DEV "%d", e->DeviceNumber); if (DismountFileSystem (mapDevice)) { char name[32]; sprintf (name, "truecrypt%d", e->DeviceNumber); dismounted = Execute (FALSE, "dmsetup", "remove", name, NULL); if (dismounted && IsFile (e->VolumePath)) { if (!DeleteLoopDevice (e->DeviceMinor)) status = FALSE; RestoreFileTime (e->VolumePath, UpdateTime ? time (NULL) : (time_t) e->ModTime, UpdateTime ? time (NULL) : (time_t) e->AcTime); } } if (!dismounted) { error ("Cannot dismount %s\n", mapDevice); status = FALSE; } else { nMountedVolumes--; if (Verbose >= 1) printf ("Unmapped %s\n", mapDevice); } if (devNo != -1) break; } } if (!found) { error (TC_MAP_DEV "%d not mounted\n", devNo); return FALSE; } if (nMountedVolumes == 0) { // Ignore errors as volumes may be mounted asynchronously UnloadKernelModule (TRUE); } return status;}// Convert a string to device number// text: device number or name or mount pointBOOL ToDeviceNumber (char *text, int *deviceNumber){ char mountedDevice[MAX_PATH], mountPoint[MAX_PATH]; int i; if (sscanf (text, "%d", deviceNumber) == 1) return TRUE; if (sscanf (text, TC_MAP_DEV "%d", deviceNumber) == 1) return TRUE; while (EnumMountPoints (mountedDevice, mountPoint)) { if (strcmp (mountPoint, text) == 0 && sscanf (mountedDevice, TC_MAP_DEV "%d", deviceNumber) == 1) { EnumMountPoints (NULL, NULL); return TRUE; } } if (!GetMountList ()) return FALSE; for (i = 0; MountList[i].DeviceNumber != -1; i++) { MountListEntry *e = &MountList[i]; if (e->DeviceNumber == -1) break; if (strcmp (text, e->VolumePath) == 0) { *deviceNumber = e->DeviceNumber; return TRUE; } } error ("%s not mounted\n", text); return FALSE;}BOOL CheckAdminPrivileges (){ char path[MAX_PATH]; char *env; if (getuid () != 0 && geteuid () != 0) { error ("Administrator (root) privileges required\n"); return FALSE; } if (getuid () != 0) { // Impersonate root to support executing of commands like mount setuid (0); env = getenv ("PATH"); snprintf (path, sizeof (path), "%s%s/sbin:/usr/sbin:/bin", env ? env : "", env ? ":" : ""); setenv ("PATH", path, 1); } return TRUE;}BOOL LockMemory (){ // Lock process memory if (mlockall (MCL_FUTURE) != 0) { perror ("Cannot prevent memory swapping: mlockall"); return FALSE; } return TRUE;}int main (int argc, char **argv){ char *volumePath = NULL; char *mountPoint = NULL; char volumePathBuf[MAX_PATH]; int i, o; int optIndex = 0; FILE *f; struct option longOptions[] = { {"device-number", required_argument, 0, 0}, {"dismount", optional_argument, 0, 'd'}, {"display-password", 0, 0, 0}, {"keyfile", required_argument, 0, 'k'}, {"keyfile-protected", required_argument, 0, 'K'}, {"filesystem", required_argument, 0, 0}, {"list", 0, 0, 'l'}, {"help", 0, 0, 'h'}, {"mount-options", required_argument, 0, 0}, {"password", required_argument, 0, 'l'}, {"password-tries", required_argument, 0, 0}, {"protect-hidden", 0, 0, 'P'}, {"read-only", 0, 0, 'r'}, {"test", 0, 0, 0}, {"update-time", 0, 0, 0}, {"verbose", 0, 0, 'v'}, {"version", 0, 0, 'V'}, {0, 0, 0, 0} }; // Make sure pipes will not use file descriptors <= STDERR_FILENO f = fdopen (STDIN_FILENO, "r"); if (f == NULL) open ("/dev/null", 0); f = fdopen (STDOUT_FILENO, "w"); if (f == NULL) open ("/dev/null", 0); f = fdopen (STDERR_FILENO, "w"); if (f == NULL) open ("/dev/null", 0); signal (SIGHUP, OnSignal); signal (SIGINT, OnSignal); signal (SIGQUIT, OnSignal); signal (SIGABRT, OnSignal); signal (SIGPIPE, OnSignal); signal (SIGTERM, OnSignal); atexit (OnExit); while ((o = getopt_long (argc, argv, "dhk:K:lp:PrvV", longOptions, &optIndex)) != -1) { switch (o) { case 'd': // Dismount { int devNo; if (optind < argc) { if (!ToDeviceNumber (argv[optind++], &devNo)) return 1; if (optind < argc) goto usage; } else devNo = -1; if (!CheckAdminPrivileges ()) return 1; return DismountVolume (devNo) ? 0 : 1; } case 'l': // List { int devNo; if (optind < argc) { if (!ToDeviceNumber (argv[optind++], &devNo)) return 1; if (optind < argc) goto usage; } else devNo = -1; if (!CheckAdminPrivileges ()) return 1; return DumpMountList (devNo) ? 0 : 1; } case 'k': case 'K': // Keyfile { KeyFile *kf = malloc (sizeof (KeyFile)); if (!kf) { perror ("malloc"); return 1; } strncpy (kf->FileName, optarg, sizeof (kf->FileName)); if (o == 'k') FirstKeyFile = KeyFileAdd (FirstKeyFile, kf); else FirstProtVolKeyFile = KeyFileAdd (FirstProtVolKeyFile, kf); } break; case 'p': // Password if (!CmdPasswordValid) { strncpy (CmdPassword.Text, optarg, sizeof (CmdPassword.Text)); CmdPassword.Length = strlen (CmdPassword.Text); CmdPasswordValid = TRUE; } else if (!CmdPassword2Valid) { strncpy (CmdPassword2.Text, optarg, sizeof (CmdPassword2.Text)); CmdPassword2.Length = strlen (CmdPassword2.Text); CmdPassword2Valid = TRUE; } break; case 'P': // Hidden volume protection ProtectHidden = TRUE; break; case 'r': ReadOnly = TRUE; break; case 'v': // Verbose Verbose++; break; case 'V': DumpVersion (stdout); return 0; case 'h': // Help DumpHelp (); return 0; case 0: if (strcmp ("display-password", longOptions[optIndex].name) == 0) { DisplayPassword = TRUE; break; } if (strcmp ("device-number", longOptions[optIndex].name) == 0) { if (sscanf (optarg, "%d", &UseDeviceNumber) == 1 && UseDeviceNumber >= 0) break; else goto usage; } if (strcmp ("filesystem", longOptions[optIndex].name) == 0) { Filesystem = optarg; break; } if (strcmp ("mount-options", longOptions[optIndex].name) == 0) { MountOpts = optarg; break; } if (strcmp ("password-tries", longOptions[optIndex].name) == 0) { if (sscanf (optarg, "%d", &PasswordEntryTries) == 1) break; else goto usage; } if (strcmp ("test", longOptions[optIndex].name) == 0) { if (AutoTestAlgorithms ()) { printf ("Self-tests of all algorithms passed.\n"); return 0; } printf ("Self-tests of algorithms FAILED!\n"); return 1; } if (strcmp ("update-time", longOptions[optIndex].name) == 0) { UpdateTime = TRUE; break; } goto usage; default: goto usage; } } if (optind >= argc) goto usage; if (optind < argc) volumePath = argv[optind++]; if (optind < argc) mountPoint = argv[optind++]; if (optind < argc) goto usage; if (!CheckAdminPrivileges ()) return 1; LockMemory (); // Relative path => absolute if (volumePath[0] != '/') { char s[MAX_PATH]; getcwd (s, sizeof (s)); snprintf (volumePathBuf, sizeof (volumePathBuf), "%s/%s", s, volumePath); volumePath = volumePathBuf; } return MountVolume (volumePath, mountPoint) == FALSE;usage: DumpUsage (stderr); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -