UAC.nsh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*** UAC Plug-in ***
  2. Interactive User (MediumIL) Admin user (HighIL)
  3. ***[Setup.exe]************* ***[Setup.exe]**************
  4. * * * *
  5. * +++[.OnInit]+++++++++++ * * +++[.OnInit]++++++++++++ *
  6. * + UAC_RunElevated >---+-+----> * + + *
  7. * + NSIS.Quit + * * + + *
  8. * +++++++++++++++++++++++ * * ++++++++++++++++++++++++ *
  9. * * * *
  10. * * * *
  11. * +++[Section]+++++++++++ * * +++[Section]++++++++++++ *
  12. * + + * /--+-+-<UAC_AsUser_ExecShell+ *
  13. * +++++++++++++++++++++++ * | * ++++++++++++++++++++++++ *
  14. * * | * *
  15. * Win32.ShellExecute <---+--/ * *
  16. * * * *
  17. *************************** ****************************
  18. */
  19. !ifndef UAC_HDR__INC
  20. !verbose push
  21. !verbose 3
  22. !ifndef UAC_VERBOSE
  23. !define UAC_VERBOSE 3
  24. !endif
  25. !verbose ${UAC_VERBOSE}
  26. !define UAC_HDR__INC 0x00020400 ;MMmmbbrr
  27. !include LogicLib.nsh
  28. /* UAC_RunElevated
  29. **
  30. ** Starts the elevation operation.
  31. **
  32. ** Return values:
  33. **
  34. ** $0: Win32 error code (0 on success, 1223 if user aborted elevation dialog, anything else should be treated as a fatal error)
  35. ** $1: If $0==0:
  36. ** 0 UAC is not supported by the OS
  37. ** 1 Started a elevated child process, the current process should act like a wrapper (Call Quit without any further processing)
  38. ** 2 The process is already running @ HighIL (Member of admin group)
  39. ** 3 You should call RunElevated again (This can happen if a user without admin priv. is used in the runas dialog)
  40. ** $2: If $0==0 && $1==1: ExitCode of the elevated fork process (The NSIS errlvl is also set)
  41. ** $3: If $0==0: 1 if the user is a member of the admin group or 0 otherwise
  42. **/
  43. !macro UAC_RunElevated
  44. UAC::_ 0
  45. !macroend
  46. !macro UAC_PageElevation_RunElevated
  47. UAC::_ 0
  48. !macroend
  49. /*!macro UAC_OnInitElevation_RunElevated
  50. UAC::_ 0
  51. !macroend
  52. !macro UAC_OnInitElevation_OnGuiInit
  53. !macroend*/
  54. /* UAC_GetIntegrityLevel <NSISVar:Output | "s">
  55. **
  56. ** Get integrity level of current process
  57. **
  58. **/
  59. !macro UAC_GetIntegrityLevel outvar
  60. UAC::_ 6
  61. !if "${outvar}" != "s"
  62. Pop ${outvar}
  63. !endif
  64. !macroend
  65. /* UAC_IsAdmin
  66. **
  67. ** Is the current process running with administrator privileges? Result in $0
  68. **
  69. ** ${If} ${UAC_IsAdmin} ...
  70. **
  71. **/
  72. !macro UAC_IsAdmin
  73. UAC::_ 2
  74. !macroend
  75. !define UAC_IsAdmin `"" UAC_IsAdmin ""`
  76. !macro _UAC_IsAdmin _a _b _t _f
  77. !insertmacro _UAC_MakeLL_Cmp _!= 0 2s
  78. !macroend
  79. /* UAC_IsInnerInstance
  80. **
  81. ** Does the current process have a NSIS/UAC parent process that is part of the elevation operation?
  82. **
  83. ** ${If} ${UAC_IsInnerInstance} ...
  84. **
  85. **/
  86. !macro UAC_IsInnerInstance
  87. UAC::_ 3
  88. !macroend
  89. !define UAC_IsInnerInstance `"" UAC_IsInnerInstance ""`
  90. !macro _UAC_IsInnerInstance _a _b _t _f
  91. !insertmacro _UAC_MakeLL_Cmp _!= 0 3s
  92. !macroend
  93. /* UAC_PageElevation_OnInit, UAC_PageElevation_OnGuiInit,
  94. **
  95. ** Helper macros for elevation on a custom elevation page, see the DualMode example for more information.
  96. **
  97. **/
  98. !macro UAC_Notify_OnGuiInit
  99. UAC::_ 4
  100. !macroend
  101. !macro UAC_PageElevation_OnGuiInit
  102. !insertmacro UAC_Notify_OnGuiInit
  103. !macroend
  104. !macro UAC_PageElevation_OnInit
  105. UAC::_ 5
  106. ${IfThen} ${Errors} ${|} Quit ${|}
  107. !macroend
  108. /* UAC_AsUser_Call <Function|Label> <NSISAddressName> <UAC_* flags>
  109. **
  110. ** Calls a function or label in the user process instance.
  111. ** All the UAC_AsUser_* macros use this helper macro.
  112. **
  113. **/
  114. !define UAC_SYNCREGISTERS 0x1
  115. ;define UAC_SYNCSTACK 0x2
  116. !define UAC_SYNCOUTDIR 0x4
  117. !define UAC_SYNCINSTDIR 0x8
  118. ;define UAC_CLEARERRFLAG 0x10
  119. !macro UAC_AsUser_Call type name flags
  120. push $0
  121. Get${type}Address $0 ${name}
  122. !verbose push
  123. !verbose ${UAC_VERBOSE}
  124. !insertmacro _UAC_ParseDefineFlagsToInt _UAC_AsUser_Call__flags ${flags}
  125. !verbose pop
  126. StrCpy $0 "1$0:${_UAC_AsUser_Call__flags}"
  127. !undef _UAC_AsUser_Call__flags
  128. Exch $0
  129. UAC::_
  130. !macroend
  131. /*
  132. ** UAC_AsUser_GetSection <Flags|InstTypes|Size|Text> <SectionIndex> <NSISVar:Output>
  133. */
  134. !macro UAC_AsUser_GetSection secprop secidx outvar
  135. !insertmacro _UAC_AsUser_GenOp ${outvar} SectionGet${secprop} ${secidx} ""
  136. !macroend
  137. /*
  138. ** UAC_AsUser_GetGlobalVar <NSISVar:SourceAndOutput>
  139. ** UAC_AsUser_GetGlobal <NSISVar:Output> <NSISVar:Source>
  140. */
  141. !macro UAC_AsUser_GetGlobalVar var
  142. !insertmacro _UAC_AsUser_GenOp ${var} StrCpy "" ${var}
  143. !macroend
  144. !macro UAC_AsUser_GetGlobal outvar srcvar
  145. !insertmacro _UAC_AsUser_GenOp ${outvar} StrCpy "" ${srcvar}
  146. !macroend
  147. /*
  148. ** UAC_AsUser_ExecShell <Verb> <ApplicationOrFile> <Parameters> <Working Directory> <SW_*>
  149. **
  150. ** Call ExecShell in the user process instance.
  151. **
  152. */
  153. !macro UAC_AsUser_ExecShell verb command params workdir show
  154. !insertmacro _UAC_IncL
  155. goto _UAC_L_E_${__UAC_L}
  156. _UAC_L_F_${__UAC_L}:
  157. ExecShell "${verb}" "${command}" '${params}' ${show}
  158. return
  159. _UAC_L_E_${__UAC_L}:
  160. !if "${workdir}" != ""
  161. push $outdir
  162. SetOutPath "${workdir}"
  163. !endif
  164. !insertmacro UAC_AsUser_Call Label _UAC_L_F_${__UAC_L} ${UAC_SYNCREGISTERS}|${UAC_SYNCOUTDIR}|${UAC_SYNCINSTDIR} #|${UAC_CLEARERRFLAG}
  165. !if "${workdir}" != ""
  166. pop $outdir
  167. SetOutPath $outdir
  168. !endif
  169. !macroend
  170. !macro _UAC_MakeLL_Cmp cmpop cmp pluginparams
  171. !insertmacro _LOGICLIB_TEMP
  172. UAC::_ ${pluginparams}
  173. pop $_LOGICLIB_TEMP
  174. !insertmacro ${cmpop} $_LOGICLIB_TEMP ${cmp} `${_t}` `${_f}`
  175. !macroend
  176. !macro _UAC_definemath def val1 op val2
  177. !define /math _UAC_definemath "${val1}" ${op} ${val2}
  178. !ifdef ${def}
  179. !undef ${def}
  180. !endif
  181. !define ${def} "${_UAC_definemath}"
  182. !undef _UAC_definemath
  183. !macroend
  184. !macro _UAC_ParseDefineFlags_orin parse outflags
  185. !searchparse /noerrors ${${parse}} "" _UAC_ParseDefineFlags_orin_f1 "|" _UAC_ParseDefineFlags_orin_f2
  186. !define _UAC_ParseDefineFlags_orin_this ${_UAC_ParseDefineFlags_orin_f1}
  187. !undef ${parse}
  188. !define ${parse} ${_UAC_ParseDefineFlags_orin_f2}
  189. !define _UAC_ParseDefineFlags_orin_saveout ${${outflags}}
  190. !undef ${outflags}
  191. !define /math ${outflags} "${_UAC_ParseDefineFlags_orin_saveout}" | "${_UAC_ParseDefineFlags_orin_this}"
  192. !undef _UAC_ParseDefineFlags_orin_saveout
  193. !undef _UAC_ParseDefineFlags_orin_this
  194. !ifdef _UAC_ParseDefineFlags_orin_f1
  195. !undef _UAC_ParseDefineFlags_orin_f1
  196. !endif
  197. !ifdef _UAC_ParseDefineFlags_orin_f2
  198. !undef _UAC_ParseDefineFlags_orin_f2
  199. !endif
  200. !macroend
  201. !macro _UAC_ParseDefineFlags_Begin _outdef _in
  202. !define _UAC_PDF${_outdef}_parse "${_in}"
  203. !define _UAC_PDF${_outdef}_flags ""
  204. !define _UAC_PDF${_outdef}_r 0
  205. !insertmacro _UAC_ParseDefineFlags_orin _UAC_PDF${_outdef}_parse _UAC_PDF${_outdef}_flags ;0x1
  206. !insertmacro _UAC_ParseDefineFlags_orin _UAC_PDF${_outdef}_parse _UAC_PDF${_outdef}_flags ;0x2
  207. !insertmacro _UAC_ParseDefineFlags_orin _UAC_PDF${_outdef}_parse _UAC_PDF${_outdef}_flags ;0x4
  208. !insertmacro _UAC_ParseDefineFlags_orin _UAC_PDF${_outdef}_parse _UAC_PDF${_outdef}_flags ;0x8
  209. !insertmacro _UAC_ParseDefineFlags_orin _UAC_PDF${_outdef}_parse _UAC_PDF${_outdef}_flags ;0x10
  210. !macroend
  211. !macro _UAC_ParseDefineFlags_End _outdef
  212. !define ${_outdef} ${_UAC_PDF${_outdef}_r}
  213. !undef _UAC_PDF${_outdef}_r
  214. !undef _UAC_PDF${_outdef}_flags
  215. !undef _UAC_PDF${_outdef}_parse
  216. !macroend
  217. !macro _UAC_ParseDefineFlags_IncludeFlag _outdef flag
  218. !if ${_UAC_PDF${_outdef}_flags} & ${flag}
  219. !insertmacro _UAC_definemath _UAC_PDF${_outdef}_r ${_UAC_PDF${_outdef}_r} | ${flag}
  220. !endif
  221. !macroend
  222. !macro _UAC_ParseDefineFlagsToInt _outdef _in
  223. !insertmacro _UAC_ParseDefineFlags_Begin _UAC_ParseDefineFlagsToInt_tmp "${_in}"
  224. !define ${_outdef} ${_UAC_PDF_UAC_ParseDefineFlagsToInt_tmp_flags}
  225. !insertmacro _UAC_ParseDefineFlags_End _UAC_ParseDefineFlagsToInt_tmp
  226. !undef _UAC_ParseDefineFlagsToInt_tmp
  227. !macroend
  228. !macro _UAC_IncL
  229. !insertmacro _UAC_definemath __UAC_L "${__UAC_L}" + 1
  230. !macroend
  231. !macro _UAC_AsUser_GenOp outvar op opparam1 opparam2
  232. !define _UAC_AUGOGR_ID _UAC_AUGOGR_OP${outvar}${op}${opparam1}${opparam2}
  233. !ifndef ${_UAC_AUGOGR_ID} ;Has this exact action been done before?
  234. !if ${outvar} == $0
  235. !define ${_UAC_AUGOGR_ID} $1
  236. !else
  237. !define ${_UAC_AUGOGR_ID} $0
  238. !endif
  239. !if "${opparam1}" == ""
  240. !define _UAC_AUGOGR_OPP1 ${${_UAC_AUGOGR_ID}}
  241. !define _UAC_AUGOGR_OPP2 ${opparam2}
  242. !else
  243. !define _UAC_AUGOGR_OPP1 ${opparam1}
  244. !define _UAC_AUGOGR_OPP2 ${${_UAC_AUGOGR_ID}}
  245. !endif
  246. goto ${_UAC_AUGOGR_ID}_C
  247. ${_UAC_AUGOGR_ID}_F:
  248. ${op} ${_UAC_AUGOGR_OPP1} ${_UAC_AUGOGR_OPP2}
  249. return
  250. ${_UAC_AUGOGR_ID}_C:
  251. !undef _UAC_AUGOGR_OPP1
  252. !undef _UAC_AUGOGR_OPP2
  253. !endif
  254. push ${${_UAC_AUGOGR_ID}}
  255. !insertmacro UAC_AsUser_Call Label ${_UAC_AUGOGR_ID}_F ${UAC_SYNCREGISTERS}
  256. StrCpy ${outvar} ${${_UAC_AUGOGR_ID}}
  257. pop ${${_UAC_AUGOGR_ID}}
  258. !undef _UAC_AUGOGR_ID
  259. !macroend
  260. !verbose pop
  261. !endif /* UAC_HDR__INC */