From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 159 invoked by alias); 31 Aug 2016 21:21:31 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 39144 Received: (qmail 2428 invoked from network); 31 Aug 2016 21:21:31 -0000 X-Qmail-Scanner-Diagnostics: from nm40-vm9.bullet.mail.ir2.yahoo.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(212.82.97.177):SA:0(0.0/5.0):. Processed in 0.593204 secs); 31 Aug 2016 21:21:31 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=FREEMAIL_FROM,SPF_PASS, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: okiddle@yahoo.co.uk X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.mail.yahoo.com designates 212.82.97.177 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.co.uk; s=s2048; t=1472678120; bh=VCOI2PZOSRlCwk7GT02lzOdj5Yz81/HYfM2WkSqvI0E=; h=In-reply-to:From:References:To:Subject:Date:From:Subject; b=gYbaiXNihWM6XlrAvSrI+0p/dLAXqrhMeLDCsW9vC8O6EfoOZYIzAJIJd+kQxojkfTfapitXYihcCYmB58/gELSsa8STCxz11uK6PH9ZSa7+0LBYXjYE3T3lMw5ql09yQeMwEC8mG2HcdNhJXpUlh4OU3w5AOBtxh2lsrpKgxYL98y4S8pEZdifjaLA/gCB/uHmlamloZPJrt5q9LsYDRnY6cEhs9g1Stubrv3NbejkcOfquwQ+NSnQ+7KjSkjhATTv2LDDusA5cQ5f+MZu5yZdLz+agmV1OCOhOaOSMl/FVBV2Z18EzS/+TGPgvEzNud0/rydxyqA62D/sPt6VIMg== X-Yahoo-Newman-Id: 758050.7516.bm@smtp111.mail.ir2.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: bQjdNigVM1nj_QL.fGruxs96j9zZxfpB8yzwgiITogY.lwF YPsRXvdJ8bk3t1ftjdS0R.fuCWisUGOT3cEq8FgvzVATqwdX3ktTKxE1dHry 5WgoejPGAduxlkWJYZGs67B96Hl9lv9_WBOXHy7.nSW1.8IJswpzBL0YxsuK X.LeX5F59GonPnfbliEcWqlJDLU52qhcTYOSQMJOQz.CCJeqVbIz.kDK0b8i 8Rh86HP4LA.oxPRAi06zEK5ZLbL4t0bD.2MuSij2GJFb7ookmEDlvWdB9HpQ r7sofRSSDVFzb72eL.ewbq38RpCT2j3kVjrWAZJsHc4R5P_rtKyV3taq17SG fqn_vUc.qVKu9GSU7rZoBtNNZF7ZN9LlSXOUe2nkgHMryK1zNGGYSrIiIXJH oz80KIrQSsf01GifMaXN9DBSJMIy.TgomCuFBWJB3g8fTkyMNAM3_k6H1J4o wOmNuqBrDo4P1KFlmD_w0Kr5RjUjHSeMObybGWSi5QPejHvs6._Jtocj9ISC jxTG72xXIS.yJ0oUslzGZCccubfO8Nx03bfUnCMH10oQ- X-Yahoo-SMTP: opAkk_CswBAce_kJ3nIPlH80cJI- In-reply-to: <20160722071927.GG2521@tarsus.local2> From: Oliver Kiddle References: <8eb6dce0-50d7-5ab2-503a-194c1de2e45d@redhat.com> <20160713045957.GA3893@tarsus.local2> <9968da53-c1fd-fa2a-f30c-c74f884d2478@redhat.com> <20160720065832.GA28939@tarsus.local2> <699166a0-b0f0-452c-2561-b7e3cc952062@redhat.com> <25001.1469117569@hydra.kiddle.eu> <20160722071927.GG2521@tarsus.local2> To: zsh-workers@zsh.org Subject: Re: [PATCH] _virsh (Was: Re: zsh virsh completion) MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <12553.1472678119.1@hydra.kiddle.eu> Content-Transfer-Encoding: 8bit Date: Wed, 31 Aug 2016 23:15:20 +0200 Message-ID: <12554.1472678120@hydra.kiddle.eu> On 22 Jul, Daniel Shahaf wrote: > In principle, I fully agree with you, with two differences: > > First, in addition to 'zstyle -t … gain-root' and > '(( $+funcstack[(r)_sudo] ))' as the conditions for invoking sudo, > I think a third alternative is to use _call_program and let the user set > the 'command' style to '-sudo'. It'd be best to put such logic in _call_program and _sudo so that it doesn't get duplicated in lots of places. > Secondly, you don't touch on what we would do when the 'gain-root' style > is unset. Given Marko's later email that virt-admin is not usable by > non-root users, perhaps we should do this: I'd suggest that it doesn't do anything special. Just run virsh list. Testing EUID might be correct but you can't actually be sure it won't work, perhaps due to groups or some RBAC mechanism. If it doesn't you'd end up with a message like: No matches for: `domains' In some cases, _wanted -x should perhaps be used. Putting the logic in _call_program also has an impact on this question because while virt-admin might be unusable to non-root users, some other commands might be partially usable. sudo may just allow it to get more matches. How about something like the following? _sudo sets the _comp_priv_prefix variable to provide a prefix to match those for the current command-line. If _call_program is called with -p, it will default to using this prefix unless overridden, either by a gain-privs style or the command style with a - prefix. This doesn't include an (( EUID )) check because it might be applicable where permissions other than root are gained. It'd perhaps be useful if the -p option to _call_program also caused it to add something to $curcontext when looking up the command style but I'm not sure where that could be done as we already have the tag and argument fields filled. Any ideas? Besides -u/--user, are other sudo options needed? Oliver diff --git a/Completion/Base/Core/_main_complete b/Completion/Base/Core/_main_complete index 9c4cfac..c292ce7 100644 --- a/Completion/Base/Core/_main_complete +++ b/Completion/Base/Core/_main_complete @@ -38,6 +38,7 @@ local func funcs ret=1 tmp _compskip format nm call match min max i num\ _saved_colors="$ZLS_COLORS" \ _saved_colors_set=${+ZLS_COLORS} \ _ambiguous_color='' +local -a _comp_priv_prefix # _precommand sets this to indicate we are following a precommand modifier local -a precommands diff --git a/Completion/Base/Utility/_call_program b/Completion/Base/Utility/_call_program index 010e094..f61180a 100644 --- a/Completion/Base/Utility/_call_program +++ b/Completion/Base/Utility/_call_program @@ -1,6 +1,13 @@ #autoload +X local tmp err_fd=-1 +local -a prefix + +if [[ "$1" = -p ]]; then + shift + zstyle -T ":completion:${curcontext}:${1}" gain-privs && + prefix=( $_comp_priv_prefix ) +fi if (( ${debug_fd:--1} > 2 )) || [[ ! -t 2 ]] then exec {err_fd}>&2 # debug_fd is saved stderr, 2 is trace or redirect @@ -13,10 +20,10 @@ if zstyle -s ":completion:${curcontext}:${1}" command tmp; then if [[ "$tmp" = -* ]]; then eval "$tmp[2,-1]" "$argv[2,-1]" else - eval "$tmp" + eval $prefix "$tmp" fi else - eval "$argv[2,-1]" + eval $prefix "$argv[2,-1]" fi 2>&$err_fd } always { diff --git a/Completion/Unix/Command/_libvirt b/Completion/Unix/Command/_libvirt index c855ac9..98c6a95 100644 --- a/Completion/Unix/Command/_libvirt +++ b/Completion/Unix/Command/_libvirt @@ -96,7 +96,7 @@ case $state in done [[ -z $cmd ]] && return 1 if [[ -z $_cache_virsh_cmdopts[$cmd] ]]; then - _cache_virsh_cmdopts[$cmd]=${(M)${${${${=${(f)"$(_call_program virsh virsh help $cmd 2>&1)"}}/\[}/\]}/\;}:#-[-0-9A-Za-z]*} + _cache_virsh_cmdopts[$cmd]=${(M)${${${${=${(f)"$(_call_program options virsh help $cmd 2>&1)"}}/\[}/\]}/\;}:#-[-0-9A-Za-z]*} fi _values -w options ${=_cache_virsh_cmdopts[$cmd]} && ret=0 ;; @@ -110,7 +110,7 @@ case $state in done [[ -z $cmd ]] && return 1 if [[ $words[-2] == --server ]]; then - _values servers ${=${(S)${${(f)$(sudo virt-admin srv-list)}##*--- }//[0-9]* }} && return 0 + _values servers ${=${(S)${${(f)$(_call_program -p servers virt-admin srv-list)}##*--- }//[0-9]* }} && return 0 fi if [[ $words[-2] == --client ]]; then local srv @@ -118,10 +118,10 @@ case $state in [[ $words[$i] == --server ]] && srv=$words[$i+1] && break done [[ -z $srv ]] && return 1 - _values servers ${=${${(f):-"$(sudo virt-admin srv-clients-list --server $srv)"}/ [a-z]*}//[^0-9]} && return 0 + _values servers ${=${${(f):-"$(_call_program -p servers virt-admin srv-clients-list --server $srv)"}/ [a-z]*}//[^0-9]} && return 0 fi if [[ -z $_cache_virt_admin_cmdopts[$cmd] ]]; then - _cache_virt_admin_cmdopts[$cmd]=${(M)${${${${=${(f)"$(_call_program virt-admin virt-admin help $cmd 2>&1)"}}/\[}/\]}/\;}:#-[-0-9A-Za-z]*} + _cache_virt_admin_cmdopts[$cmd]=${(M)${${${${=${(f)"$(_call_program options virt-admin help $cmd 2>&1)"}}/\[}/\]}/\;}:#-[-0-9A-Za-z]*} fi [[ -n $_cache_virt_admin_cmdopts[$cmd] ]] && \ _values -w options ${=_cache_virt_admin_cmdopts[$cmd]} && ret=0 diff --git a/Completion/Unix/Command/_sudo b/Completion/Unix/Command/_sudo index 63ac37f..1ceefa2 100644 --- a/Completion/Unix/Command/_sudo +++ b/Completion/Unix/Command/_sudo @@ -48,7 +48,7 @@ else '(-H --set-home -i --login -s --shell -e --edit)'{-H,--set-home}"[set HOME variable to target user's home dir]" \ '(-P --preserve-groups -i -login -s --shell -e --edit)'{-P,--preserve-groups}"[preserve group vector instead of setting to target's]" \ '(-)1:command: _command_names -e' - '*::arguments: _normal' + '*::arguments:{ _comp_priv_prefix=( $words[1] -n ${(kv)opt_args[(I)(-u|--user)]} ) ; _normal }' ) fi