1

I am moving some files to /Applications directory by my application and using SMJobBless() for Elevating to admin rights privileges.

But it prompts for admin credentials for any type of account- admin or standard_user, can we put a check and ask for admin credentials only for standard users by using any objective-c API?

Edit -- If anyone has tried or gone through process of installing Dropbox, then Dropbox installer does the same(i.e it asks for admin privileges only for standard accounts for copying it's .app file to /Applications).

john fedric
  • 167
  • 1
  • 11
  • i think there are no public available apis to do that since apple allow usage only inside the app sandbox. so i assume you're a jailbreaker and then you can install and use all public available libs for unix development like gnu or bsd. without jailbreak you will not be able to get root access to the system. – thorb65 May 02 '15 at 13:36
  • @thorb65 Have you tried installing Dropbox, during installation process It copies it's .app file (after downloading) to /Applications directory and for admin account it's installer does not asks for any password. It only asks for privileges for standard accounts. I wonder how Dropbox is doing that? – john fedric May 02 '15 at 15:12
  • Has anyone tried this or any help for possible workaround? – john fedric May 03 '15 at 15:44

1 Answers1

1

Based on the answer of another question, I've made a file that seems to suit you (I've just translated it from C to Objective-C and removed the printf's; the original link is at the end of the answer).

NSPrivileges.h

#import <Foundation/Foundation.h>

@interface NSPrivileges : NSObject

+(BOOL)hasAdminPrivileges;

@end

NSPrivileges.m

#import "NSPrivileges.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <strings.h>
#include <pwd.h>
#include <grp.h>

@implementation NSPrivileges

+(BOOL)hasAdminPrivileges{
    uid_t current_user_id = getuid();
    struct passwd *pwentry = getpwuid(current_user_id);
    struct group *grentry = getgrgid(getgid());

    struct group *admin_group = getgrnam("admin");
    while(*admin_group->gr_mem != NULL) {
        if (strcmp(pwentry->pw_name, *admin_group->gr_mem) == 0) {
            return YES;
        }
        admin_group->gr_mem++;
    }
    return NO;
}

@end

Now, you just need to import NSPrivileges.h to the file which needs to know if the user is an admin or not and use the command [NSPrivileges hasAdminPrivileges] to find out (it will return a BOOL, so it will be easy to deal with). Hope that helps you.

Here is my reference: Objective C and OS user type

Community
  • 1
  • 1
VitorMM
  • 1,060
  • 8
  • 33