mas icon indicating copy to clipboard operation
mas copied to clipboard

Support running mas as root

Open scarrychris opened this issue 8 years ago • 8 comments

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?

scarrychris avatar May 16 '17 14:05 scarrychris

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

norman-chan avatar Mar 27 '18 22:03 norman-chan

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

cdalvaro avatar Dec 25 '18 12:12 cdalvaro

Thanks for the details @cdalvaro.

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

phatblat avatar Dec 27 '18 15:12 phatblat

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.

cdalvaro avatar Dec 27 '18 15:12 cdalvaro

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?

phatblat avatar Dec 27 '18 15:12 phatblat

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>

cdalvaro avatar Dec 27 '18 16:12 cdalvaro

Great. Thanks for checking.

phatblat avatar Dec 27 '18 16:12 phatblat