EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
win-volumes.js
Go to the documentation of this file.
1/*
2Copyright 2022 Intel Corporation
3@author Bryan Roe
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19function trimObject(j)
20{
21 var i;
22 for(i in j)
23 {
24 if (j[i] == null || (typeof(j[i])=='string' && j[i]=='') || i.startsWith('__')) { delete j[i];}
25 }
26 if (j['SerialNumber'] < 0) { var tmp = Buffer.alloc(4); tmp.writeInt32LE(j['SerialNumber']); j['SerialNumber'] = tmp.readUInt32LE(); }
27 return (j);
28}
29
30
31function getVolumes()
32{
33 var v = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_Volume');
34 var i;
35
36 var ret = {};
37
38 for (i in v)
39 {
40 ret[v[i].DeviceID] = trimObject(v[i]);
41 }
42 try {
43 v = require('win-wmi').query('ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption', 'SELECT * FROM Win32_EncryptableVolume');
44 for (i in v)
45 {
46 var tmp = trimObject(v[i]);
47 for (var k in tmp)
48 {
49 ret[tmp.DeviceID][k] = tmp[k];
50 }
51 }
52 } catch (ex) { }
53 return (ret);
54}
55
56function windows_volumes()
57{
58 var promise = require('promise');
59 var p1 = new promise(function (res, rej) { this._res = res; this._rej = rej; });
60 var ret = {};
61 var values = require('win-wmi').query('ROOT\\CIMV2', 'SELECT * FROM Win32_LogicalDisk', ['DeviceID', 'VolumeName', 'FileSystem', 'Size', 'FreeSpace', 'DriveType']);
62 if(values[0]){
63 for (var i = 0; i < values.length; ++i) {
64 var drive = values[i]['DeviceID'].slice(0,-1);
65 ret[drive] = {
66 name: (values[i]['VolumeName'] ? values[i]['VolumeName'] : ""),
67 type: (values[i]['FileSystem'] ? values[i]['FileSystem'] : "Unknown"),
68 size: (values[i]['Size'] ? values[i]['Size'] : 0),
69 sizeremaining: (values[i]['FreeSpace'] ? values[i]['FreeSpace'] : 0),
70 removable: (values[i]['DriveType'] == 2),
71 cdrom: (values[i]['DriveType'] == 5)
72 };
73 }
74 }
75 try {
76 values = require('win-wmi').query('ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption', 'SELECT * FROM Win32_EncryptableVolume', ['DriveLetter','ConversionStatus','ProtectionStatus']);
77 if(values[0]){
78 for (var i = 0; i < values.length; ++i) {
79 var drive = values[i]['DriveLetter'].slice(0,-1);
80 var statuses = {
81 0: 'FullyDecrypted',
82 1: 'FullyEncrypted',
83 2: 'EncryptionInProgress',
84 3: 'DecryptionInProgress',
85 4: 'EncryptionPaused',
86 5: 'DecryptionPaused'
87 };
88 ret[drive].volumeStatus = statuses.hasOwnProperty(values[i].ConversionStatus) ? statuses[values[i].ConversionStatus] : 'FullyDecrypted';
89 ret[drive].protectionStatus = (values[i].ProtectionStatus == 0 ? 'Off' : (values[i].ProtectionStatus == 1 ? 'On' : 'Unknown'));
90 try {
91 var foundIDMarkedLine = false, foundMarkedLine = false, identifier = '', password = '';
92 var keychild = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['/c', 'manage-bde -protectors -get ' + drive + ': -Type recoverypassword'], {});
93 keychild.stdout.str = ''; keychild.stdout.on('data', function (c) { this.str += c.toString(); });
94 keychild.waitExit();
95 var lines = keychild.stdout.str.trim().split('\r\n');
96 for (var x = 0; x < lines.length; x++) { // Loop each line
97 var abc = lines[x].trim();
98 var englishidpass = (abc !== '' && abc.includes('Numerical Password:')); // English ID
99 var germanidpass = (abc !== '' && abc.includes('Numerisches Kennwort:')); // German ID
100 var frenchidpass = (abc !== '' && abc.includes('Mot de passe num')); // French ID
101 var englishpass = (abc !== '' && abc.includes('Password:') && !abc.includes('Numerical Password:')); // English Password
102 var germanpass = (abc !== '' && abc.includes('Kennwort:') && !abc.includes('Numerisches Kennwort:')); // German Password
103 var frenchpass = (abc !== '' && abc.includes('Mot de passe :') && !abc.includes('Mot de passe num')); // French Password
104 if (englishidpass || germanidpass || frenchidpass|| englishpass || germanpass || frenchpass) {
105 var nextline = lines[x + 1].trim();
106 if (x + 1 < lines.length && (nextline !== '' && (nextline.startsWith('ID:') || nextline.startsWith('ID :')) )) {
107 identifier = nextline.replace('ID:','').replace('ID :', '').trim();
108 foundIDMarkedLine = true;
109 }else if (x + 1 < lines.length && nextline !== '') {
110 password = nextline;
111 foundMarkedLine = true;
112 }
113 }
114 }
115 ret[drive].identifier = (foundIDMarkedLine ? identifier : ''); // Set Bitlocker Identifier
116 ret[drive].recoveryPassword = (foundMarkedLine ? password : ''); // Set Bitlocker Password
117 } catch(ex) { } // just carry on as we cant get bitlocker key
118 }
119 }
120 p1._res(ret);
121 } catch (ex) { p1._res(ret); } // just return volumes as cant get encryption/bitlocker
122 return (p1);
123}
124
125module.exports = {
126 getVolumes: function () { try { return (getVolumes()); } catch (x) { return ({}); } },
127 volumes_promise: windows_volumes
128};