Hi All,

         I'm implementing the cocoa application using objective-c. Now my
requirement is to restart my launchd daemon service.

         I've tried using authorization services to perform the previlaged
unload of daemon , but didn't succeed in this.

         Please help me to solve this.Thanks in Advance.


Note : Attaching the sample code for your reference.


*returnCode = [blAuth
unloadLaunchctlProcess:@"/Library/LaunchDaemons/com.mycompany.cocoadaemon.plist"];
*

*
//============================================================================
// - (void)unloadLaunchctlProcess:(NSString *)commandFromPS
//============================================================================
// unloads the launchctl process specified in (NSString *)commandFromPS
using launchctl
// The more specific (ie., closer to matching the actual listing in ps)
// commandFromPS is the better your accuracy will be, esp. when multiple
// versions of the process exist.
//
- (BOOL)unloadLaunchctlProcess:(NSString *)commandFromPS {*

* if( ![self isAuthenticated:[NSArray arrayWithObject:commandFromPS]] ) {
  [self authenticate:[NSArray arrayWithObject:commandFromPS]];
 }

 return [self executeCommand:@"/bin/launchctl" withArgs:[NSArray
arrayWithObjects:@"unload",@"-w",commandFromPS,nil]];
} *

//============================================================================
// - (BOOL)isAuthenticated:(NSArray *)forCommands
//============================================================================
// Find outs if the user has the appropriate authorization rights for the
// commands listed in (NSArray *)forCommands.
// This should be called each time you need to know whether the user
// is authorized, since the AuthorizationRef can be invalidated elsewhere,
or
// may expire after a short period of time.
//
- (BOOL)isAuthenticated:(NSArray *)forCommands {
 AuthorizationRights rights;
 AuthorizationRights *authorizedRights;
 AuthorizationFlags flags;

 int numItems = [forCommands count];
 AuthorizationItem *items = malloc( sizeof(AuthorizationItem) * numItems );
 char paths[20][128]; // only handles upto 20 commands with paths upto 128
characters in length

 OSStatus err = 0;
 BOOL authorized = NO;
 int i = 0;

 if(authorizationRef==NULL) {
  rights.count=0;
  rights.items = NULL;

  flags = kAuthorizationFlagDefaults;

  err = AuthorizationCreate(&rights, kAuthorizationEmptyEnvironment, flags,
&authorizationRef);
 }

 if( numItems < 1 ) {
  return authorized;
 }

 while( i < numItems && i < 20 ) {
   [[forCommands objectAtIndex:i] getCString:paths[i]];

  items[i].name = kAuthorizationRightExecute;
  items[i].value = paths[i];
  items[i].valueLength = [[forCommands objectAtIndex:i] cStringLength];
  items[i].flags = 0;

  i++;
 }

    rights.count = numItems;
    rights.items = items;

    flags = kAuthorizationFlagExtendRights;

    err = AuthorizationCopyRights(authorizationRef, &rights,
kAuthorizationEmptyEnvironment, flags, &authorizedRights);

    authorized = (errAuthorizationSuccess==err);

 if(authorized)
  AuthorizationFreeItemSet(authorizedRights);

 free(items);

    return authorized;
}

//============================================================================
// - (BOOL)fetchPassword:(NSArray *)forCommands
//============================================================================
// Adds rights for commands specified in (NSArray *)forCommands.
// Commands should be passed as a NSString comtaining the path to the
executable.
// Returns YES if rights were gained
//
- (BOOL)fetchPassword:(NSArray *)forCommands {
 NSLog(@"fetchPassword for (%@)",forCommands);
 AuthorizationRights rights;
 AuthorizationRights *authorizedRights;
 AuthorizationFlags flags;

 int numItems = [forCommands count];
 AuthorizationItem *items = malloc( sizeof(AuthorizationItem) * numItems );
 char paths[20][128];

 OSStatus err = 0;
 BOOL authorized = NO;
 int i = 0;

 if( numItems < 1 )
  return authorized;

 while( i < numItems && i < 20 ) {
  [[forCommands objectAtIndex:i] getCString:paths[i]];

  items[i].name = kAuthorizationRightExecute;
  items[i].value = paths[i];
  items[i].valueLength = [[forCommands objectAtIndex:i] cStringLength];
  items[i].flags = 0;

  i++;
 }

 rights.count = numItems;
 rights.items = items;

 flags = kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagExtendRights;

 err = AuthorizationCopyRights(authorizationRef, &rights,
kAuthorizationEmptyEnvironment, flags, &authorizedRights);

 authorized = (errAuthorizationSuccess == err);

 if(authorized) {
  AuthorizationFreeItemSet(authorizedRights);
 }

 free(items);

 return authorized;
}

//============================================================================
// - (BOOL)authenticate:(NSArray *)forCommands
//============================================================================
// Authenticates the commands in the array (NSArray *)forCommands by calling

// fetchPassword.
//
- (BOOL)authenticate:(NSArray *)forCommands {
 if( ![self isAuthenticated:forCommands] ) {
        [self fetchPassword:forCommands];
 }

 return [self isAuthenticated:forCommands];
}

//============================================================================
// -(void)executeCommand:(NSString *)pathToCommand withArgs:(NSArray
*)arguments
//============================================================================
// Executes command in (NSString *)pathToCommand with the arguments listed
in
// (NSArray *)arguments as root.
// pathToCommand should be a string contain the path to the command
// (eg., /usr/bin/more), arguments should be an array of strings each
containing
// a single argument.
//
-(BOOL)executeCommand:(NSString *)pathToCommand withArgs:(NSArray
*)arguments {
 char* args[30]; // can only handle 30 arguments to a given command
 OSStatus err = 0;
 int i = 0;
 FILE *      channel;

 channel = NULL;

 if(![self authenticate:[NSArray arrayWithObject:pathToCommand]])
  return NO;

 if( arguments == nil || [arguments count] < 1  ) {
  err = AuthorizationExecuteWithPrivileges(authorizationRef, [pathToCommand
cString], 0, NULL, &channel);
 }
 else {
  NSLog(@" [pathToCommand cString] is (%s)",[pathToCommand cString]);
  while( i < [arguments count] && i < 19) {
   args[i] = (char*)[[arguments objectAtIndex:i] cString];
   NSLog(@" args[%d] is (%s)",i,args[i]);
   i++;
  }
  args[i] = NULL;


  err = AuthorizationExecuteWithPrivileges(authorizationRef,
            [pathToCommand cString],
            0, args, &channel);
 }

 if (err ==0) {
  do {
   char thisLine[1024];
   Boolean     success;
            success = (fgets(thisLine, sizeof(thisLine), channel) != NULL);
   char *cmd= (char *)[pathToCommand cString];
   if (success ) {
    NSLog(@": command [%s] output %s", cmd, thisLine);
   } else {
    NSLog(@" command [%s] has ended. EOF indicator [%d] -- thisLine is (%s)
\n", cmd, feof(channel),thisLine);
    break;
   }
  } while (true);
  fclose(channel);
 }

    if(err!=0) {
  NSBeep();
  NSLog(@"Error %d in AuthorizationExecuteWithPrivileges command for (%@)
with arguments (%@)",err,pathToCommand,arguments);
  return NO;
 }
 else {
  return YES;
 }
}


-JanakiRam.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to