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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. const fs = require('fs')
  2. var cli = require('./cliverse')
  3. var nodeShellExec = cli.nodeShellExec;
  4. var __isElevated = null;
  5. var shell_verse = {
  6. // getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case.
  7. getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
  8. , runTask : ( taskToRun ) => {
  9. if (__isElevated) return shell_verse.elevatedRunner( taskToRun )
  10. else return shell_verse.runNonElevated( taskToRun )
  11. }
  12. , elevatedRunner( taskToRun ){
  13. try {
  14. var __runasresult = null;
  15. return taskToRun().then((r)=>{
  16. // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
  17. fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' })
  18. fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
  19. return __runasresult = r;
  20. })
  21. .catch((e) => {
  22. fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
  23. fs.writeFileSync('run.done', 'failure')
  24. console.error(e)
  25. })
  26. .finally(() => {
  27. if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done')
  28. })
  29. }
  30. catch (e) {
  31. console.error('Error Invalid command : ' + e)
  32. fs.writeFileSync('run.done', 'error')
  33. }
  34. finally {
  35. }
  36. }
  37. , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
  38. , runElevated : ( taskToRun ) => {
  39. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  40. // taskToRun by default is the launched command and args. Specially in windows out of proc.
  41. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  42. if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
  43. if (__isElevated) {
  44. return shell_verse.elevatedRunner(taskToRun)
  45. }
  46. else {
  47. console.log('Requesting Elevated Privileges');
  48. // requesteElevation is acutally request elevation and run. Both In Proc and Out of Proc.
  49. // Linux doesnt require elevation for most commands...
  50. return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  51. }
  52. }
  53. , runElevatedBatch( batchToRun ){
  54. // 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.
  55. // Irrespective of the batch we just call runElevated once.
  56. return [runElevated(batchToRun[0])]
  57. }
  58. , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
  59. , runNonElevated : ( taskToRun ) => {
  60. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  61. if(__isElevated) {
  62. return Promise.resolve( new Error('Cannot Run Task in Elevated Mode.') )
  63. }
  64. else {
  65. // taskToRun by default is the launched command and args.
  66. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  67. return taskToRun().then(r=>{
  68. taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
  69. return r;
  70. }).catch((e) => {
  71. e.info = taskToRun.info;
  72. taskToRun.statuslog.statuslog(e);
  73. if(taskToRun.errHandler) throw taskToRun.errHandler(e)
  74. // console.error(e)
  75. throw e;
  76. }).finally(()=>{})
  77. }
  78. }
  79. , isElevated : ()=>{
  80. return acquireElevationState().then( ()=>{
  81. shell_verse.isElevated = () => {
  82. return Promise.resolve(__isElevated)
  83. }
  84. return shell_verse.isElevated()
  85. })
  86. }
  87. // , isElevationOutOfProc : ()=>{ return true }
  88. , acquireElevationState : () => {
  89. return nodeShellExec("fsutil", ["dirty", "query", "C:"], {
  90. inherit: true
  91. // , shell: true
  92. , stdio: 'ignore'
  93. , env: process.env
  94. , title: `check privileged execution mode using "fsutil dirty query C:"`
  95. }).then((exitcode) => {
  96. console.log('Elevated')
  97. __isElevated = true;
  98. }).catch(() => {
  99. __isElevated = false;
  100. console.log('Not Elevated');
  101. }).finally(()=>{
  102. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  103. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  104. return __isElevated;
  105. })
  106. }
  107. , getTaskCheckExists : cli.createTask('getTaskCheckExists', 'where')
  108. , getbash : ()=>{ return "C:\\Program Files\\Git\\bin\\sh.exe" }
  109. , createJuntionOrLink : (dirOrFile, target, opts) =>{
  110. return nodeShellExec('mklink', ['/J', dirOrFile, target], opts).catch((e) => { console.error(e) })
  111. }
  112. , removeJuncionOrLink : ( junctionOrLink )=>{
  113. return nodeShellExec('rmdir', [junctionOrLink], { inherit: true, shell: true, env: process.env })
  114. }
  115. , requestElevation(elevatedRunner, taskToRun) {
  116. // PB : TODO -- Multiple parallel request elevations should be queued into a promise.
  117. var processedArgs = taskToRun.processedArgs, selectedinstance = taskToRun.selectedinstance , statuslog = taskToRun.statuslog
  118. // Wait for the runas to complete before we read it.
  119. try {
  120. fs.unlinkSync('run.done')
  121. fs.unlinkSync('run.log')
  122. }
  123. catch (e) { } //Ignore
  124. // Find node path to send to hta.
  125. return nodeShellExec('where', ['node']).then(r => {
  126. var namedArgs = [];
  127. console.log('result : ' + JSON.stringify(r))
  128. Object.keys(processedArgs).forEach((v) => { v != '_' ? namedArgs.push('--' + v + '=' + processedArgs[v]) : null; })
  129. // PB : TODO -- Convert all the cli args back to string.
  130. var args = [`${selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/windowselevate.hta`].concat(processedArgs._)
  131. namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null;
  132. args.push('--runas=self');
  133. // args.push('--nodepath=' + r.messages[r.messages.length - 1])
  134. // if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV)
  135. // if (processedArgs.debug) args.push('--debug=true') // Enable to debug elevated..
  136. // console.dir(processedArgs._)
  137. // console.dir(namedArgs.join(' '))
  138. console.dir(args)
  139. // throw 'test'
  140. return nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
  141. , {
  142. inherit: true
  143. , shell: true
  144. , env: taskToRun.ENV
  145. , runas: 'self'
  146. , title: `runas`
  147. }
  148. ).then(() => {
  149. // runas returned.
  150. try {
  151. // PB : TODO -- Log is comma prefixed. Needs to be proper JSON.
  152. var runaslog = JSON.parse('[ { "success" : true, "result" : "started"}' + fs.readFileSync('run.log', { flags: 'a+' }) + ']');
  153. runaslog.forEach((logEntry) => {
  154. statuslog.statuslog(logEntry.success ? null : logEntry, logEntry)
  155. logEntry.success ? (console.log(['success :' + logEntry.result]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' ')))
  156. })
  157. }
  158. catch (e) {
  159. // We must have a runas log
  160. statuslog.statuslog(e)
  161. console.error('Run log error probably was not created by runas : ' + e)
  162. }
  163. })
  164. .catch(err => console.error('Elevation failed : ' + err));
  165. })
  166. }
  167. }
  168. module.exports = shell_verse