2011-06-03 20 views
8

Nella mia applicazione Cocoa sto provando a utilizzare NSTask per eseguire alcuni comandi Git di base. Ogni volta che ho eseguito un comando che richiede autorizzazioni (chiavi SSH) per accedere a un remoto (ad esempio git push, git pull), non riesce con il seguente errore:NSTask e Git - Problemi di autorizzazioni

Permission denied (publickey). The remote end hung up unexpectedly

Esecuzione gli stessi comandi da terminale funziona bene, così ho' Pensando che questo potrebbe essere un problema con NSTask non impostare una variabile di ambiente che verrebbe utilizzata da qualche parte nel processo di accesso alle chiavi ssh. Ho provato a impostare manualmente le variabili d'ambiente e HOMEUSER come questo:

[task setEnvironment:[NSDictionary dictionaryWithObjectsAndKeys:NSHomeDirectory(), @"HOME", NSUserName(), @"USER", nil]]; 

Ma questo non ha alcun effetto. C'è qualche particolare variabile di ambiente che devo impostare in NSTask affinché funzioni correttamente?

MODIFICA: Grazie al suggerimento di Dustin, ho avuto un po 'di più nel capire questo. Ho usato il comando env per elencare le variabili di ambiente per la mia sessione corrente e ho trovato questo:

SSH_AUTH_SOCK=/tmp/launch-DMQopt/Listeners 

Per provare, ho copiato quel percorso e impostarlo come una variabile di ambiente di NSTask e corse di nuovo il codice, e questo tempo ha funzionato! Detto questo, sono certo che le modifiche a SSH_AUTH_SOCK per ogni sessione, quindi non posso semplicemente hardcode. Come faccio a generare/recuperare dinamicamente questa variabile?

+1

Provalo nell'altro senso, usa 'env -i' sulla riga di comando e vedi quanto devi aggiungere prima che funzioni. – Dustin

+0

Ottenuto un po 'di più grazie al tuo suggerimento, leggi il mio post modificato :) – indragie

risposta

4

Si potrebbe provare a seguire il tutorial "Wrapping rsync or SSH in an NSTask" (da Ira), che fa parlare SSH_AUTH_SOCK variabile:

Since writing this post I've realised that I omitted an important additional step in setting up the environment variables for the NSTask.
In order to make passwordless key-based authentication work it's necessary to grab the SSH_AUTH_SOCK variable from the user's environment and include this in the NSTask's environment.
So, when setting environment variables for example;

NSTask *task; 
NSDictionary *environmentDict = [[NSProcessInfo processInfo] environment]; 
// Environment variables needed for password based authentication 
NSMutableDictionary *env = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
         @"NONE", @"DISPLAY",       askPassPath, @"SSH_ASKPASS", 
         userName,@"AUTH_USERNAME", 
         hostName,@"AUTH_HOSTNAME", 
         nil]; 

// Environment variable needed for key based authentication 
[env setObject:[environmentDict objectForKey:@"SSH_AUTH_SOCK"] forKey:@"SSH_AUTH_SOCK"]; 

// Setting the task's environment 
[task setEnvironment:env]; 

Tuttavia, l'OP indragie commenti:

I had tried this earlier but since it was being invoked with XCode, the SSH_AUTH_SOCK env var. wasn't being passed to it.
Opening the app from Finder corrects this issue.



// Get the path of the Askpass program, which is 
// setup to be included as part of the main application bundle 
NSString *askPassPath = [NSBundle pathForResource:@"Askpass" 
       ofType:@"" 
       inDirectory:[[NSBundle mainBundle] bundlePath]]; 
+1

Avevo provato prima ma da quando è stato richiamato con Xcode, il var di SSH_AUTH_SOCK var. non è stato passato ad esso. L'apertura dell'app da Finder corregge questo problema. Grazie! – indragie

+0

@indragie: eccellente. Ho incluso il tuo commento nella risposta per maggiore visibilità. – VonC

+0

@VonC Puoi dirmi quale variabile è askPassPath? –

Problemi correlati