def find_airport_password(ssid):
    """Returns the password and a Keychain item reference for the requested Airport network"""
    item            = ctypes.c_void_p()
    password_length = ctypes.c_uint32(0)
    password_data   = ctypes.c_char_p(256)

    system_keychain = ctypes.c_void_p()
    keychain_ptr    = ctypes.pointer(system_keychain)

    rc = SECURITY.SecKeychainOpen("/Library/Keychains/System.keychain", keychain_ptr)
    if rc != 0:
        raise RuntimeError("Couldn't open system keychain: rc=%d" % rc)

    # n.b. The service name is often "AirPort Network" in the user's keychain
    #      but in the system keychain it appears to be a UUID. It might be
    #      necessary to check the description for network names which are not
    #      unique in the keychain.
    rc = SECURITY.SecKeychainFindGenericPassword (
        system_keychain,
        None,                               # Length of service name
        None,                               # Service name
        len(ssid),                          # Account name length
        ssid,                               # Account name
        ctypes.byref(password_length),      # Will be filled with pw length
        ctypes.pointer(password_data),      # Will be filled with pw data
        ctypes.pointer(item)
    )

    if rc == -25300:
        raise RuntimeError('No existing password for Airport network %s: rc=%d' % (ssid, rc))
    elif rc != 0:
        raise RuntimeError('Failed to find password for Airport network %s: rc=%d' % (ssid, rc))

    password = password_data.value[0:password_length.value]

    SECURITY.SecKeychainItemFreeContent(None, password_data)

    return (password, item)
dataset/ETHPy150Open MacSysadmin/pymacadmin/examples/ctypes/airport-update.py/find_airport_password/original