Run Bash script as root using Objective-C / Cocoa

Given that there is a file called copystuff in the Resources folder in the xCode project, and this file reads:

#!/bin/sh
cp -R /Users/someuser/Documents /Users/admin/Desktop

And if this bit of code below is associated with a button in IB ... it will copy the directory / Users / someuser / Documents to / Users / admin when the button is clicked in the Cocoa application ... It works when the application is launched in the account admin (using OS X 10.5.x here) ...

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle]
pathForResource:@"copystuff" ofType:@"sh"], nil]];
[task launch];

My question is, is there a way for NSTask to execute a script running as root, and this code is called from an account without an administrator? Or asked differently. Is it possible to code Objective-C to run scripts from say / usr / bin as root from an account without an administrator?

+3
2

script, . NSFileManager . , root, . , , , .

+7

, , ?

setuid , . , , set-user-ID-on-execute set-group-ID-on-execute . , ...

man chmod .

:

$ echo "Hello, world!" > file
$ sudo chown root file
$ sudo chmod 600 file
$ cat file
cat: file: Permission denied

:

#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  setuid(0);
  system("cat file");
  return 0;
}

, :

$ cc -Wall -o app main.c
$ chown root app
$ chmod 4755 app
$ ./app
Hello, world!
+4

Source: https://habr.com/ru/post/1732385/


All Articles