From 4c9ac19e878750cf2c2832759d9902796079d9f0 Mon Sep 17 00:00:00 2001 From: Adam Vandenberg Date: Sat, 8 Jun 2013 18:17:28 -0700 Subject: [PATCH] Consolidate sudo checks. Closes Homebrew/homebrew#20318. --- Library/Homebrew/cmd/install.rb | 4 ---- Library/Homebrew/cmd/link.rb | 4 ---- Library/Homebrew/cmd/pin.rb | 3 --- Library/Homebrew/cmd/unpin.rb | 3 --- Library/Homebrew/cmd/upgrade.rb | 6 ------ Library/Homebrew/exceptions.rb | 8 -------- Library/Homebrew/global.rb | 6 ++++++ Library/brew.rb | 32 ++++++++++++++++++++------------ 8 files changed, 26 insertions(+), 40 deletions(-) diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index c0aa1a2e9a..cbeb433eb5 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -18,10 +18,6 @@ module Homebrew extend self end end unless ARGV.force? - if Process.uid.zero? and not File.stat(HOMEBREW_BREW_FILE).uid.zero? - raise "Cowardly refusing to `sudo brew install'\n#{SUDO_BAD_ERRMSG}" - end - install_formulae ARGV.formulae end diff --git a/Library/Homebrew/cmd/link.rb b/Library/Homebrew/cmd/link.rb index 0184febaf2..67ce43478a 100644 --- a/Library/Homebrew/cmd/link.rb +++ b/Library/Homebrew/cmd/link.rb @@ -5,10 +5,6 @@ module Homebrew extend self def link raise KegUnspecifiedError if ARGV.named.empty? - if Process.uid.zero? and not File.stat(HOMEBREW_BREW_FILE).uid.zero? - raise "Cowardly refusing to `sudo brew link'\n#{SUDO_BAD_ERRMSG}" - end - mode = OpenStruct.new mode.overwrite = true if ARGV.include? '--overwrite' diff --git a/Library/Homebrew/cmd/pin.rb b/Library/Homebrew/cmd/pin.rb index 2c0d5d33d7..b8c1d1e330 100644 --- a/Library/Homebrew/cmd/pin.rb +++ b/Library/Homebrew/cmd/pin.rb @@ -2,9 +2,6 @@ require 'formula' module Homebrew extend self def pin - if Process.uid.zero? and not File.stat(HOMEBREW_BREW_FILE).uid.zero? - abort "Cowardly refusing to `sudo pin'" - end raise FormulaUnspecifiedError if ARGV.named.empty? ARGV.formulae.each do |f| diff --git a/Library/Homebrew/cmd/unpin.rb b/Library/Homebrew/cmd/unpin.rb index 265f7a941f..4da280eefe 100644 --- a/Library/Homebrew/cmd/unpin.rb +++ b/Library/Homebrew/cmd/unpin.rb @@ -2,9 +2,6 @@ require 'formula' module Homebrew extend self def unpin - if Process.uid.zero? and not File.stat(HOMEBREW_BREW_FILE).uid.zero? - abort "Cowardly refusing to `sudo unpin'" - end raise FormulaUnspecifiedError if ARGV.named.empty? ARGV.formulae.each do |f| diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index fe41a15072..28882360ba 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -8,12 +8,6 @@ end module Homebrew extend self def upgrade - if Process.uid.zero? and not File.stat(HOMEBREW_BREW_FILE).uid.zero? - # note we only abort if Homebrew is *not* installed as sudo and the user - # calls brew as root. The fix is to chown brew to root. - abort "Cowardly refusing to `sudo brew upgrade'" - end - Homebrew.perform_preinstall_checks if ARGV.named.empty? diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index c91d917d0a..9c812ce349 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -213,11 +213,3 @@ class ChecksumMismatchError < RuntimeError super + advice.to_s end end - -module Homebrew extend self - SUDO_BAD_ERRMSG = <<-EOS.undent - You can use brew with sudo, but only if the brew executable is owned by root. - However, this is both not recommended and completely unsupported so do so at - your own risk. - EOS -end diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index cb6e36e073..f6e942bff5 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -99,3 +99,9 @@ HOMEBREW_PULL_OR_COMMIT_URL_REGEX = 'https:\/\/github.com\/(\w+)\/homebrew(-\w+) require 'compat' unless ARGV.include? "--no-compat" or ENV['HOMEBREW_NO_COMPAT'] ORIGINAL_PATHS = ENV['PATH'].split(':').map{ |p| Pathname.new(p).expand_path rescue nil }.compact.freeze + +SUDO_BAD_ERRMSG = <<-EOS.undent + You can use brew with sudo, but only if the brew executable is owned by root. + However, this is both not recommended and completely unsupported so do so at + your own risk. +EOS diff --git a/Library/brew.rb b/Library/brew.rb index 92de2cbee5..eb46e88a3a 100755 --- a/Library/brew.rb +++ b/Library/brew.rb @@ -58,17 +58,17 @@ end begin trap("INT", std_trap) # restore default CTRL-C handler - aliases = {'ls' => :list, - 'homepage' => :home, - '-S' => :search, - 'up' => :update, - 'ln' => :link, - 'instal' => :install, # gem does the same - 'rm' => :uninstall, - 'remove' => :uninstall, - 'configure' => :diy, - 'abv' => :info, - 'dr' => :doctor, + aliases = {'ls' => 'list', + 'homepage' => 'home', + '-S' => 'search', + 'up' => 'update', + 'ln' => 'link', + 'instal' => 'install', # gem does the same + 'rm' => 'uninstall', + 'remove' => 'uninstall', + 'configure' => 'diy', + 'abv' => 'info', + 'dr' => 'doctor', '--repo' => '--repository', 'environment' => '--env' # same as gem } @@ -76,11 +76,19 @@ begin cmd = ARGV.shift cmd = aliases[cmd] if aliases[cmd] - if cmd == '-c1' + if cmd == '-c1' # Shortcut for one line of configuration cmd = '--config' ARGV.unshift('-1') end + sudo_check = Set.new %w[ install link pin unpin upgrade ] + + if sudo_check.include? cmd + if Process.uid.zero? and not File.stat(HOMEBREW_BREW_FILE).uid.zero? + raise "Cowardly refusing to `sudo brew #{cmd}`\n#{SUDO_BAD_ERRMSG}" + end + end + # Add example external commands to PATH before checking. ENV['PATH'] += ":#{HOMEBREW_REPOSITORY}/Library/Contributions/cmd" if require? HOMEBREW_REPOSITORY/"Library/Homebrew/cmd"/cmd