123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- const { any } = require('bbhverse');
- const fs = require('fs')
-
- var cli = require('./cliverse')
- var nodeShellExec = cli.nodeShellExec;
-
- function elevatedRunIPCWriteMessage( target, m ) {
- fs.writeFileSync(target, ', ' + JSON.stringify( m ), { 'flag': 'a+' })
- }
-
-
- var __isElevated = null;
- var shell_verse = {
-
- getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
- , runTask : ( taskToRun ) => {
- if (__isElevated) return shell_verse.elevatedRunner( taskToRun )
- else return shell_verse.runNonElevated( taskToRun )
- }
-
- , elevatedRunner( taskToRun, inBatch ){
-
- try {
- var runlogjson = `${selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/run.log`
- var __runasresult = null;
- return taskToRun().then((r)=>{
-
- elevatedRunIPCWriteMessage( runlogjson, { info : taskToRun.info, success: true } )
- if(!inBatch) fs.writeFileSync('run.done', 'success')
- return __runasresult = r;
- })
- .catch((e) => {
- elevatedRunIPCWriteMessage( runlogjson, e )
- if(!inBatch)fs.writeFileSync('run.done', 'failure')
- console.error(e)
- })
- .finally(() => {
-
- })
- }
- catch (e) {
- console.error('Error Invalid command : ' + e)
- if(!inBatch) fs.writeFileSync('run.done', 'error')
- }
- finally {
- }
- }
- , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
- , getElevatedTaskInBatch : function( taskToRun ){ return ()=>{ return shell_verse.runElevatedInBatch(taskToRun) }}
- , runElevatedInBatch : ( taskToRun ) => {
- if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true)
- else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
- }
- , runElevated : ( taskToRun ) => {
-
-
-
-
-
- if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
-
-
- if (__isElevated) {
- return shell_verse.elevatedRunner(taskToRun)
- }
- else {
- console.log('Requesting Elevated Privileges');
-
-
- return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
- }
- }
- , runElevatedBatch( batchToRun ){
-
-
- if (__isElevated) {
- return any(batchToRun).then((r)=>{
-
-
- fs.writeFileSync('run.done', 'success')
- return __runasresult = r;
- })
- .catch((e) => {
-
- fs.writeFileSync('run.done', 'failure')
- console.error(e)
- })
-
-
-
- }
- else {
- return this.runElevated(batchToRun[0])
- }
- }
-
- , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
- , runNonElevated : ( taskToRun ) => {
-
- if(__isElevated) {
- return Promise.resolve( 'Skipping regular task in elevated shell.' )
- }
- else {
-
-
-
- return taskToRun().then(r=>{
- taskToRun.statuslog.statuslog(null, taskToRun.info )
- return r;
- }).catch((e) => {
- e.info = taskToRun.info;
- if(taskToRun.errHandler) throw taskToRun.errHandler(e)
- taskToRun.statuslog.statuslog(e);
-
- throw e;
- }).finally(()=>{})
- }
- }
-
- , isElevated : ()=>{
- return acquireElevationState().then( ()=>{
- shell_verse.isElevated = () => {
- return Promise.resolve(__isElevated)
- }
- return shell_verse.isElevated()
- })
- }
-
-
-
- , acquireElevationState : () => {
- return nodeShellExec("fsutil", ["dirty", "query", "C:"], {
- inherit: true
-
- , stdio: 'ignore'
- , env: process.env
- , title: `check privileged execution mode using "fsutil dirty query C:"`
- }).then((exitcode) => {
- console.log('Elevated')
- __isElevated = true;
- }).catch(() => {
- __isElevated = false;
- console.log('Not Elevated');
- }).finally(()=>{
- shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
- shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
- return __isElevated;
- })
- }
-
- , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'where')
-
- , getbash : ()=>{ return "C:\\Program Files\\Git\\bin\\sh.exe" }
-
- , createJuntionOrLink : (dirOrFile, target, opts) =>{
- return nodeShellExec('mklink', ['/J', dirOrFile, target], opts).catch((e) => { console.error(e) })
- }
- , removeJuncionOrLink : ( junctionOrLink )=>{
- return nodeShellExec('rmdir', [junctionOrLink], { inherit: true, shell: true, env: process.env })
- }
-
- , requestElevation(elevatedRunner, taskToRun) {
-
-
- var processedArgs = taskToRun.processedArgs, selectedinstance = taskToRun.selectedinstance , statuslog = taskToRun.statuslog
-
- try {
- fs.unlinkSync('run.done')
- }
- catch (e) { }
-
-
- return nodeShellExec('where', ['node']).then(r => {
- var namedArgs = [];
- console.log('result : ' + JSON.stringify(r))
- Object.keys(processedArgs).forEach((v) => { v != '_' ? namedArgs.push('--' + v + '=' + processedArgs[v]) : null; })
-
- var args = [`${selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/windowselevate.hta`].concat(processedArgs._)
- namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null;
- args.push('--runas=self');
- var elevatedruntimestamp = (new Date()).getTime()
- args.push(`--runtimestamp=${elevatedruntimestamp}`);
-
-
-
-
-
-
- console.dir(args)
-
-
- return nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
- , {
- inherit: true
- , shell: true
- , env: taskToRun.ENV
- , runas: 'self'
- , title: `runas`
- }
- ).then(() => {
-
- try {
-
- var runlogjson = `${selectedinstance.root}/.elxr/run-${elevatedruntimestamp}/run.log`
- var runaslog = JSON.parse('[' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']');
- try { fs.unlinkSync(runlogjson) } catch(e){ }
-
-
- runaslog.forEach((logEntry) => {
- statuslog.statuslog(logEntry.success ? null : logEntry, logEntry)
- logEntry.success ? (console.log(['success :' + logEntry.result]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' ')))
- })
- }
- catch (e) {
-
- statuslog.statuslog(e)
- console.error('Run log error probably was not created by runas : ' + e)
- }
- })
- .catch(err => console.error('Elevation failed : ' + err));
- })
- }
- , iswin(){ return true}
- , islin(){ return false}
- }
-
- module.exports = shell_verse
|