OSX Keychain / best-correct way to write items to Keychain ?

Mac OSX specific forum
adellavia
New User
New User
Posts: 4
Joined: Thu May 29, 2014 6:00 pm

OSX Keychain / best-correct way to write items to Keychain ?

Post by adellavia »

Dear all, I have a question about the correct way to write a network password in the OSX Keychain.

I have see that's possible to use the Terminal/Shell program "security" and with this program add a new item in the Keychain, but the question is:

- Is this the best-correct-efficent way to do this job with PureBasic ?

For now I have write a partial code using RunProgram and the security command, but I don't have a Cocoamessage "alternative way" to do the same thing.

I have used this code with an old Applescript Studio program, to permit to smb printers network connections (SMB://printserver.domain.com/PRINTERNAME) to read authentication data in the Keychain and send automatically the username and password to print/AD server every time a network user needs to print something.

Thank you for any information you can give me.

Code: Select all

domain$="DOMAIN"
username$="User"
password$="Password"
printserver$="PSERV"


Result = RunProgram("security", "add-internet-password -a" + Chr(32) + domain$ + ";" + username$ + Chr(32) + "-s" + Chr(32) + printserver$ + Chr(32) + "-r" + Chr(32) + Chr(34) + "smb" + Chr(32) + Chr(34) + Chr(32) + "-w" + Chr(32) + password$ + Chr(32) + "-D" + Chr(32) + Chr(34) + "Network Password" + Chr(34) + Chr(32) + "-T /System/Library/CoreServices/NetAuthAgent.app","", #PB_Program_Open | #PB_Program_Read | #PB_Program_Error)

Output$ = ""
  If Result
    While ProgramRunning(Result)
      If AvailableProgramOutput(Result)
        Output$ + ReadProgramString(Result) + Chr(13)
      EndIf
    Wend
    Output$ + Chr(13) + Chr(13)
    Output$ + "Exitcode: " + Str(ProgramExitCode(Result))
    Output$ + Chr(13) + Chr(13)
    Output$ + "Extended error message: " + ReadProgramError(Result)
    
    CloseProgram(Result) ; Close the connection to the program
  EndIf
  
  MessageRequester("Output", Output$)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: OSX Keychain / best-correct way to write items to Keycha

Post by wilbert »

I'm not familiar with working with the keychain but maybe you can use the keychain services with ImportC
https://developer.apple.com/library/mac ... rence.html
Windows (x64)
Raspberry Pi OS (Arm64)
adellavia
New User
New User
Posts: 4
Joined: Thu May 29, 2014 6:00 pm

Re: OSX Keychain / best-correct way to write items to Keycha

Post by adellavia »

Hi wilbert, thanks for the reply. I think that currently I don't have enough experience to translate the Apple Reference code and create a PureBasic procedure to do the same thing.

About this need, can I ask if maybe there is a guide on this forum where it is shown an example of how to interpret the Apple Reference manual and create a similar procedure (also regarding other Cocoa features and not Keychain) ?

Thank you :)

wilbert wrote:I'm not familiar with working with the keychain but maybe you can use the keychain services with ImportC
https://developer.apple.com/library/mac ... rence.html
Post Reply