Changeset 84

Show
Ignore:
Timestamp:
12/27/05 17:19:06 (3 years ago)
Author:
mrowe
Message:

Make use of the getters/setters throughout SSHAgent.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Libs/SSHAgent.m

    r83 r84  
    169169        int i; 
    170170 
    171         /* If the PID is > 0, the agent should (in theory) be running. */ 
    172         if([self PID] > 0) 
     171        if ([self isRunning]) 
    173172        { 
    174173                NSLog(@"Agent is already started"); 
     
    176175        } 
    177176 
    178         /* Clear the agentSocketPath object. */ 
    179         if([self agentSocketPath])  
    180         { 
    181                 [agentSocketPathLock lock]; 
    182                 [agentSocketPath release]; 
    183                 agentSocketPath = nil; 
    184                 [agentSocketPathLock unlock]; 
    185         } 
    186  
    187  
    188         if([self socketPath] == nil) 
     177        [self setAgentSocketPath:nil]; 
     178 
     179        if (![self socketPath]) 
    189180        { 
    190181                NSLog(@"DEBUG: start: socketPath not set"); 
     
    215206                        continue; 
    216207 
     208                NSString *key = [columns objectAtIndex:1]; 
    217209                /* If 2nd column matches "SSH_AUTH_SOCK", then 3rd column is the socket path. */ 
    218                 if ([@"SSH_AUTH_SOCK" isEqualToString:[columns objectAtIndex:1]]) 
    219                 { 
    220                         [agentSocketPathLock lock]; 
    221                         agentSocketPath = [[NSString stringWithString:[columns objectAtIndex:2]] retain]; 
    222                         [agentSocketPathLock unlock]; 
    223                 } 
     210                if ([key isEqualToString:@"SSH_AUTH_SOCK"]) 
     211                        [self setAgentSocketPath:[columns objectAtIndex:2]]; 
    224212 
    225213                /* If 2nd column matches "SSH_AGENT_PID", then 3rd column is the PID. */ 
    226                 if ([@"SSH_AGENT_PID" isEqualToString:[columns objectAtIndex:1]]) 
    227                 { 
    228                         [thePIDLock lock]; 
    229                         thePID = [[columns objectAtIndex:2] intValue]; 
    230                         [thePIDLock unlock]; 
    231                 } 
    232         } 
    233  
    234         /* If the PID is > 0 but the socket path isn't filled, stop the agent and return -1. */ 
    235         if(([self PID] > 0) && (([self agentSocketPath] == nil) || ([agentSocketPath length] < 1))) 
     214                else if ([key isEqualToString:@"SSH_AGENT_PID"]) 
     215                        [self setPID:[[columns objectAtIndex:2] intValue]]; 
     216        } 
     217 
     218        /* If the agent is not running, or the socket path is empty then stop the agent and fail */ 
     219        if (![self isRunning] || ![[self agentSocketPath] length]) 
    236220        { 
    237221                NSLog(@"SSHAgent start: ssh-agent didn't give the output we expected"); 
     
    240224        } 
    241225 
    242         /* If there's no PID just return -1. */ 
    243         if([self PID] < 1) 
    244         { 
    245                 NSLog(@"SSHAgent start: ssh-agent didn't give the output we expected"); 
    246                 return NO; 
    247         } 
    248  
    249226        /* Handle connections in a seperate thread. */ 
    250227        [NSThread detachNewThreadSelector:@selector(handleAgentConnections) toTarget:self withObject:nil]; 
     
    261238- (BOOL)stop 
    262239{ 
    263         /* We can't stop something if we haven't got the pid. */ 
    264         if([self PID] > 0) 
    265         { 
    266                 /* We don't need to check if this fails. We clean up the variables either way. */ 
    267                 kill([self PID], SIGTERM); 
    268  
    269                 [agentSocketPathLock lock]; 
    270                 [agentSocketPath release]; 
    271                 agentSocketPath = nil; 
    272                 [agentSocketPathLock unlock]; 
    273  
    274                 [self closeSockets]; 
    275  
    276                 [thePIDLock lock]; 
    277                 thePID = 0; 
    278                 [thePIDLock unlock]; 
    279  
    280                 [keysOnAgentLock lock]; 
    281                 [keysOnAgent release]; 
    282                 keysOnAgent = nil; 
    283                 [keysOnAgentLock unlock]; 
    284  
    285                 [[NSNotificationCenter defaultCenter]  postNotificationName:@"AgentStopped" object:nil]; 
    286         } 
     240        /* We can't stop something if we it's not running */ 
     241        if (![self isRunning]) 
     242                return YES; 
     243 
     244        /* We don't need to check if this fails. We clean up the variables either way. */ 
     245        kill([self PID], SIGTERM); 
     246 
     247        [self setAgentSocketPath:nil]; 
     248        [self closeSockets]; 
     249        [self setPID:0]; 
     250        [self setKeysOnAgent:nil]; 
     251 
     252        [[NSNotificationCenter defaultCenter]  postNotificationName:@"AgentStopped" object:nil]; 
    287253 
    288254        return YES; 
     
    504470                                                                write(fds[i+1], buf, r); 
    505471 
    506                                                                 [keysOnAgentLock lock]; 
    507                                                                 keysOnAgent = [[[SSHAgent currentAgent] currentKeysOnAgent] retain]; 
    508                                                                 [keysOnAgentLock unlock];                
     472                                                                [self setKeysOnAgent:[[SSHAgent currentAgent] currentKeysOnAgent]]; 
    509473                                                                [[NSNotificationCenter defaultCenter]  postNotificationName:@"KeysOnAgentUnknown" object:nil]; 
    510474                                                        } 
     
    515479                                                                write(fds[i+1], buf, r); 
    516480 
    517                                                                 [keysOnAgentLock lock]; 
    518                                                                 keysOnAgent = [[[SSHAgent currentAgent] currentKeysOnAgent] retain]; 
    519                                                                 [keysOnAgentLock unlock];                
    520  
     481                                                                [self setKeysOnAgent:[[SSHAgent currentAgent] currentKeysOnAgent]]; 
    521482                                                                [[NSNotificationCenter defaultCenter]  postNotificationName:@"KeysOnAgentUnknown" object:nil]; 
    522483                                                        } 
     
    708669        NSArray *columns, *key, *lines; 
    709670 
    710         /* If the PID is < 1, we assume the agent isn't running. */ 
    711         if([self PID] < 1) 
    712         { 
     671        if (![self isRunning]) 
    713672                return nil; 
    714         } 
    715673 
    716674        /* Initialize a ssh-add SSHTool, set the arguments to -l for a list of keys. */ 
     
    775733- (void)keysOnAgentStatusChange:(NSNotification *)notification 
    776734{ 
    777         if([[notification name] isEqualToString:@"AgentEmptied"]) 
    778         { 
    779                 [keysOnAgentLock lock]; 
    780                 if(keysOnAgent) 
    781                 { 
    782                         [keysOnAgent release]; 
    783                 } 
    784  
    785                 keysOnAgent = nil; 
    786                  
    787                 [keysOnAgentLock unlock]; 
    788         } 
    789  
    790         else if([[notification name] isEqualToString:@"AgentFilled"]) 
    791         { 
    792                 [keysOnAgentLock lock]; 
    793                 keysOnAgent = [[[SSHAgent currentAgent] currentKeysOnAgent] retain]; 
    794                 [keysOnAgentLock unlock]; 
    795         } 
    796  
     735        if ([[notification name] isEqualToString:@"AgentEmptied"]) 
     736                [self setKeysOnAgent:nil]; 
     737 
     738        else if ([[notification name] isEqualToString:@"AgentFilled"]) 
     739                [self setKeysOnAgent:[[SSHAgent currentAgent] currentKeysOnAgent]]; 
    797740} 
    798741