mas-cli / mas

:package: Mac App Store command line interface
MIT License
10.82k stars 273 forks source link

Support running mas as root #89

Open scarrychris opened 7 years ago

scarrychris commented 7 years ago

When running mas as root through ARD the output isn't sent back or returns a false statement. It just says this completed successfully, or it tells me everything is up to date which I know is false. I am trying to use this to automate all my Mac App Store updates on my fleet but cannot because none of them are local admins. Makes this tool pretty useless for me the way it is right now. Is this possible to run as root, if not can it be?

norman-chan commented 6 years ago

Can't run as root at the moment, but you can use launchctl asuser to run as current logged in user instead.

!/bin/bash

currentUser=$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");') uid=$(id -u $currentUser)

/bin/launchctl asuser $uid sudo -iu "$currentUser" /[path to mas]/mas [command] ex: /bin/launchctl asuser $uid sudo -iu "$currentUser" /tmp/mas account

cdalvaro commented 5 years ago

Some reporting about this issue...

I have been running some commands logged as root (su -) but executing them as another user which is logged in into the Store.

This is the summary:

Testing Account

root# su - UserWithMAS -c '/usr/local/bin/mas account'

✅ The output is the correct Apple ID account for user UserWithMAS

Testing Search

root# su - UserWithMAS -c '/usr/local/bin/mas search Xcode'
   497799835  Xcode                                              (10.1)
  1163893338  App School for Xcode and  iOS 10 Development Free  (1.0)
  1183412116  Swiftify for Xcode                                 (4.5.1)
  1179007212  Code School for Xcode Free -Learn How to Make Apps (1.1.3)
  1083165894  Course for Xcode 7 Lite                            (1.0)
  1168397789  Alignment for Xcode                                (1.1.2)
   734258109  Watchdog for Xcode                                 (1.9)
  1246672247  BlockComment for Xcode                             (1.0.0)

✅ The apps are listed properly.

Testing Upgrade

root# su - UserWithMAS -c '/usr/local/bin/mas upgrade 497799835'
Warning: Nothing found to upgrade
root# echo $?
0

✅ The app is up-to-date and the return code is 0

Testing Installing

root# su - UserWithMAS -c '/usr/local/bin/mas install Magnet'
Invalid value for 'Argument<Array<Int>>(defaultValue: nil, usage: "app ID(s) to install", usageParameter: nil)': Magnet
root# echo $?
1

mas cannot install the app because I have passed intentionally the name of the app rather the id and the return code is 1

# Magnet id: 441258766 (This app is in my purchased apps list)
root# su - UserWithMAS -c '/usr/local/bin/mas install 441258766'

❌ The process hangs without saying anything until manual abort

I hope this information will help for the debugging

phatblat commented 5 years ago

Thanks for the details @cdalvaro.

Is the "UserWithMAS" signed into the Mac App Store? Are they logged into macOS through a GUI session?

cdalvaro commented 5 years ago

The user 'UserWithMAS' is logged into the Mac App Store through a GUI session.

Running mas account as UserWithMAS or su - UserWithMAS -c '/usr/local/bin/mas account as root both calls return the correct Apple ID.

phatblat commented 5 years ago

OK, good to know. I know that sometimes mas commands can trigger GUI dialogs and that can hang the process. If you log out UserWithMAS or reboot and don't log in when them, does the install still hang when run through su, or does it give an error?

cdalvaro commented 5 years ago

If I sign out from the Mac App Store (though the GUI) and then check the account status with the terminal, these are the results:

UserWithMAS$ mas account
Not signed in
Error: Not signed in

UserWithMAS$ mas install 441258766
Error: Not signed in
root# su - UserWithMAS -c '/usr/local/bin/mas account'
Not signed in
Error: Not signed in

root# su - UserWithMAS -c '/usr/local/bin/mas install 441258766'
Error: Not signed in

Then, if I sign in into the Store and execute the same commands, the result is the following:

UserWithMAS$ mas account
<The correct Apple ID>

UserWithMAS$ mas install 441258766
==> Downloading Magnet
==> Installed Magnet
root# su - UserWithMAS -c '/usr/local/bin/mas account'
<The correct Apple ID>

# Before uninstalling the previous installation
root# su - UserWithMAS -c '/usr/local/bin/mas install 441258766'
Warning: Magnet is already installed

# After having uninstalled the previous installation
root# su - UserWithMAS -c '/usr/local/bin/mas install 441258766'
<The process hangs without any output>
phatblat commented 5 years ago

Great. Thanks for checking.

rgoldberg commented 4 days ago

For many commands dealing with installed apps, running as root probably won't ever work.

  1. We lookup products from the CKSoftwareMap.shared().allProducts() array from the Apple private CommerceKit framework, which seems to only list apps installed for the Apple ID associated with the current macOS user. root shouldn't be associated with any Apple ID. Maybe we could sudo -u "${SUDO_USER}" …, but that still might not populate CKSoftwareMap.shared().allProducts(), because that might only be populated for full GUI logins. We could possibly stop using this part of CommerceKit, but that would require workarounds.

  2. I doubt (but don't know for sure) that you can install or update apps when running as root.

  3. You might not be able to signin to the App Store when running as root.

  4. Even if you can do 2 and/or 3, you might run into issues down the line.