555
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win_verse.js 10KB

2 vuotta sitten
2 vuotta sitten
2 vuotta sitten
2 vuotta sitten
2 vuotta sitten
2 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. const { any } = require('bbhverse');
  2. const fs = require('fs')
  3. var path = require('path');
  4. var cli = require('./cliverse')
  5. var nodeShellExec = cli.nodeShellExec;
  6. function elevatedRunIPCWriteMessage( target, m ) {
  7. fs.writeFileSync(target, ', ' + JSON.stringify( m ), { 'flag': 'a+' })
  8. }
  9. var __isElevated = null;
  10. var shell_verse = {
  11. // getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case.
  12. getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
  13. , runTask : ( taskToRun ) => {
  14. if (__isElevated) return shell_verse.elevatedRunner( taskToRun )
  15. else return shell_verse.runNonElevated( taskToRun )
  16. }
  17. , elevatedRunner( taskToRun, inBatch ){
  18. // PB : TODO -- Should be called only when we are in an elevated shell that was already requested from an unelevated shell with a batch of tasks.
  19. try {
  20. var runlogjson = `${selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/run.log`
  21. var __runasresult = null;
  22. return taskToRun().then((r)=>{
  23. // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
  24. elevatedRunIPCWriteMessage( runlogjson, { info : taskToRun.info, success: true } )
  25. if(!inBatch) fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
  26. return __runasresult = r;
  27. })
  28. .catch((e) => {
  29. elevatedRunIPCWriteMessage( runlogjson, e)
  30. if(!inBatch)fs.writeFileSync('run.done', 'failure')
  31. console.error(e)
  32. })
  33. .finally(() => {
  34. // if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done')
  35. })
  36. }
  37. catch (e) {
  38. console.error('Error Invalid command : ' + e)
  39. if(!inBatch) fs.writeFileSync('run.done', 'error')
  40. }
  41. finally {
  42. }
  43. }
  44. , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
  45. , getElevatedTaskInBatch : function( taskToRun ){ return ()=>{ return shell_verse.runElevatedInBatch(taskToRun) }}
  46. , runElevatedInBatch : ( taskToRun ) => {
  47. if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true)
  48. else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  49. }
  50. , runElevated : ( taskToRun ) => {
  51. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  52. // taskToRun by default is the launched command and args. Specially in windows out of proc.
  53. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  54. if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
  55. if (__isElevated) {
  56. return shell_verse.elevatedRunner(taskToRun)
  57. }
  58. else {
  59. console.log('Requesting Elevated Privileges');
  60. // requesteElevation is acutally request elevation and run. Both In Proc and Out of Proc.
  61. // Linux doesnt require elevation for most commands...
  62. return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  63. }
  64. }
  65. , runElevatedBatch( batchToRun ){
  66. // In windows we don't need to run each task. We hand over to another shell which in elevated state rebuilds the whole batch and runs.
  67. // Irrespective of the batch we just call runElevated once.
  68. if (__isElevated) {
  69. return any(batchToRun).then((r)=>{
  70. // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
  71. // fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' })
  72. fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
  73. return __runasresult = r;
  74. })
  75. .catch((e) => {
  76. // fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
  77. fs.writeFileSync('run.done', 'failure')
  78. console.error(e)
  79. })
  80. // .finally(() => {
  81. // if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done')
  82. // });
  83. }
  84. else {
  85. return this.runElevated(batchToRun[0])
  86. }
  87. }
  88. , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
  89. , runNonElevated : ( taskToRun ) => {
  90. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  91. if(__isElevated) {
  92. return Promise.resolve( 'Skipping regular task in elevated shell.' ) // Regular tasks unless marked as common tasks should not run in elevated shell...
  93. }
  94. else {
  95. // taskToRun by default is the launched command and args.
  96. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  97. return taskToRun().then(r=>{
  98. taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
  99. return r;
  100. }).catch((e) => {
  101. e.info = taskToRun.info;
  102. if(taskToRun.errHandler) throw taskToRun.errHandler(e)
  103. taskToRun.statuslog.statuslog(e); //
  104. // console.error(e)
  105. throw e;
  106. }).finally(()=>{})
  107. }
  108. }
  109. , isElevated : ()=>{
  110. return acquireElevationState().then( ()=>{
  111. shell_verse.isElevated = () => {
  112. return Promise.resolve(__isElevated)
  113. }
  114. return shell_verse.isElevated()
  115. })
  116. }
  117. // , isElevationOutOfProc : ()=>{ return true }
  118. , acquireElevationState : () => {
  119. return nodeShellExec("fsutil", ["dirty", "query", "C:"], {
  120. inherit: true
  121. // , shell: true
  122. , stdio: 'ignore'
  123. , env: process.env
  124. , title: `check privileged execution mode using "fsutil dirty query C:"`
  125. }).then((exitcode) => {
  126. console.log('Elevated')
  127. __isElevated = true;
  128. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  129. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  130. return __isElevated
  131. }).catch((e) => {
  132. __isElevated = false;
  133. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  134. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  135. console.log('Not Elevated');
  136. return __isElevated
  137. })
  138. // .finally(()=>{
  139. // shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  140. // shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  141. // // return __isElevated; // Value returned from finally is not supported by node.
  142. // })
  143. }
  144. , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'where')
  145. , getbash : ()=>{ return "C:\\Program Files\\Git\\bin\\sh.exe" }
  146. , createJuntionOrLink : (dirOrFile, target, opts) =>{
  147. return nodeShellExec('mklink', ['/J', dirOrFile, target], opts).catch((e) => { console.error(e) })
  148. }
  149. , removeJuncionOrLink : ( junctionOrLink )=>{
  150. return nodeShellExec('rmdir', [junctionOrLink], { inherit: true, shell: true, env: process.env })
  151. }
  152. , requestElevation(elevatedRunner, taskToRun) {
  153. // PB : TODO -- Multiple parallel request elevations should be queued into a batch and serialized as a single promise.
  154. var processedArgs = taskToRun.processedArgs, selectedinstance = taskToRun.selectedinstance , statuslog = taskToRun.statuslog
  155. // Wait for the runas to complete before we read it.
  156. try {
  157. fs.unlinkSync('run.done') // Need a unique file for aech elevated run.
  158. }
  159. catch (e) { } //Ignore
  160. // Find node path to send to hta.
  161. return nodeShellExec('where', ['node']).then(r => {
  162. var namedArgs = [];
  163. console.log('result : ' + JSON.stringify(r))
  164. Object.keys(processedArgs).forEach((v) => { v != '_' ? namedArgs.push('--' + v + '=' + processedArgs[v]) : null; })
  165. // PB : TODO -- Convert all the cli args back to string.
  166. var args = [ path.normalize(`${selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/windowselevate.hta`) ].concat(processedArgs._)
  167. namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null;
  168. args.push('--runas=self');
  169. var elevatedruntimestamp = (new Date()).getTime()
  170. args.push(`--runtimestamp=${elevatedruntimestamp}`);
  171. // args.push('--nodepath=' + r.messages[r.messages.length - 1])
  172. // if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV)
  173. // if (processedArgs.debug) args.push('--debug=true') // Enable to debug elevated..
  174. // console.dir(processedArgs._)
  175. // console.dir(namedArgs.join(' '))
  176. console.dir(args)
  177. // throw 'test'
  178. return nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
  179. , {
  180. inherit: true
  181. , shell: true
  182. , env: taskToRun.ENV
  183. , runas: 'self'
  184. , title: `runas`
  185. }
  186. ).then(() => {
  187. // runas returned.
  188. try {
  189. // PB : TODO -- Log is comma prefixed. Needs to be proper JSON.
  190. var runlogjson = `${selectedinstance.root}/.elxr/run-${elevatedruntimestamp}/run.log`
  191. var runaslog = JSON.parse('[' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']');
  192. try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run.
  193. // console.log( "runaslog : " + runaslog.length )
  194. // Assemble elevated run results into the main run log
  195. runaslog.forEach((logEntry) => {
  196. statuslog.statuslog(logEntry.success ? null : logEntry, logEntry)
  197. logEntry.success ? (console.log(['success :' + (logEntry.result || logEntry.success)]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' ')))
  198. })
  199. }
  200. catch (e) {
  201. // We must have a runas log
  202. statuslog.statuslog(e)
  203. console.error('Run log error probably was not created by runas : ' + e)
  204. }
  205. })
  206. .catch(err => console.error('Elevation failed : ' + err));
  207. })
  208. }
  209. , iswin(){ return true}
  210. , islin(){ return false}
  211. }
  212. module.exports = shell_verse