write LDAP configuration using OpenDirectory framework in swift

i'm tying to create a custom LDAP configuration in a swift script, something similar to this without using python: https://github.com/Yohan460/Programmatic-OD-Python-Binding

my script fails at the custom call - not sure if the issue is my data types or that the functions used in the python example don't exactly translate. any help is appreciated, thanks in advance! p.s - i'm a total swift n00b

#!/usr/bin/swift
import Foundation
import OpenDirectory

let fileName = "ldap.plist"
let configData = try String(contentsOfFile: fileName)
let config = Data(configData.utf8)

let odSesh = ODSession.default()
let configNode = try! ODNode(session: odSesh,type: ODNodeType(kODNodeTypeConfigure))
let outNode = try! configNode.customCall(99991, send: config)

Replies

First up, I have to warn you that making custom calls to an OD node puts you on very sketchy ground compatibility-wise. Unless the call’s code and data format are explicitly documentated — which I don’t think is the case here — there’s no guarantee that this will continue to work in the long term.

With that in mind, I compared the Python and Swift snippets you posted and the missing elements seems to be the serialised authorisation reference at the end of the data. Specifically, your Swift code has no equivalent to this:

root_auth = b'\x00'*32
request = NSMutableData.dataWithBytes_length_(root_auth, 32)
request.appendData_(config_data)

The direct Swift equivalent would be this:

let nullAuth = Data(repeating: 0, count: 32)
config.append(nullAuth)

Note For this to work you’ll have to change config to be mutable (a var not a let).

Also, your code to read config is unnecessarily convoluted. Try this:

let fileURL = URL(fileURLWithPath: "ldap.plist")
var config = try Data(contentsOf: fileURL)

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

thanks, quinn! appreciate your expertise and quick response.

  • Have you solved this? I am trying use this Ldap_pythong_config.py ( https://support.google.com/a/answer/9089736?#macos&zippy=%2Cmacos ). No longer works in 12.3 (no python). A swift version would work? Maybe I adopt your script for this? Can you share you final script? Thank you.

Add a Comment