1 #include <membership.h>
3 #include "open_directory.h"
8 CFErrorRef error
= NULL
;
11 node
= ODNodeCreateWithNodeType(NULL
, kODSessionDefault
, kODTypeAuthenticationSearchNode
, &error
);
22 CopyRecordWithUUID(ODNodeRef node
, CFStringRef type
, uuid_t uuid
)
24 CFErrorRef error
= NULL
;
27 ODQueryRef query
= NULL
;
28 CFTypeRef vals
[] = { CFSTR(kDSAttributesStandardAll
) };
29 CFArrayRef attributes
= CFArrayCreate(NULL
, vals
, 1, &kCFTypeArrayCallBacks
);
30 CFArrayRef results
= NULL
;
31 ODRecordRef record
= NULL
;
33 uuid_unparse(uuid
, uuidstr
);
34 uuidref
= CFStringCreateWithCString(NULL
, uuidstr
, kCFStringEncodingUTF8
);
37 query
= ODQueryCreateWithNode(NULL
, node
, type
, CFSTR(kDS1AttrGeneratedUID
), kODMatchEqualTo
, uuidref
, attributes
, 100, &error
);
40 results
= ODQueryCopyResults(query
, false, &error
);
43 if (CFArrayGetCount(results
) == 1) {
44 record
= (ODRecordRef
)CFArrayGetValueAtIndex(results
, 0);
61 CopyGroupRecordWithGID(ODNodeRef node
, gid_t gid
)
65 mbr_gid_to_uuid(gid
, uuid
);
67 return CopyRecordWithUUID(node
, CFSTR(kDSStdRecordTypeGroups
), uuid
);
71 CopyUserRecordWithUID(ODNodeRef node
, uid_t uid
)
75 mbr_uid_to_uuid(uid
, uuid
);
77 return CopyRecordWithUUID(node
, CFSTR(kDSStdRecordTypeUsers
), uuid
);
81 CopyUserRecordWithUsername(ODNodeRef node
, char *name
)
84 CFTypeRef vals
[] = { CFSTR(kDSAttributesStandardAll
) };
85 CFArrayRef attributes
= CFArrayCreate(NULL
, vals
, 1, &kCFTypeArrayCallBacks
);
88 nameref
= CFStringCreateWithCString(NULL
, name
, kCFStringEncodingUTF8
);
93 return ODNodeCopyRecord(node
, CFSTR(kDSStdRecordTypeUsers
), nameref
, attributes
, &error
);
97 CopyAttrFromRecord(ODRecordRef record
, CFStringRef attribute
)
99 CFErrorRef error
= NULL
;
100 CFArrayRef values
= ODRecordCopyValues(record
, attribute
, &error
);
101 CFStringRef result
= NULL
;
104 if (CFArrayGetCount(values
) == 1) {
105 result
= CFArrayGetValueAtIndex(values
, 0);
115 GetIntAttrFromRecord(ODRecordRef record
, CFStringRef attribute
, int *output
)
118 CFStringRef str
= CopyAttrFromRecord(record
, attribute
);
121 *output
= CFStringGetIntValue(str
);
130 GetUIDFromRecord(ODRecordRef record
)
134 GetIntAttrFromRecord(record
, CFSTR(kDS1AttrUniqueID
), &uid
);
140 GetGIDFromRecord(ODRecordRef record
)
144 GetIntAttrFromRecord(record
, CFSTR(kDS1AttrPrimaryGroupID
), &gid
);
150 CopyGroupRecordsForUser(ODNodeRef node
, ODRecordRef user
, CFIndex limit
)
152 CFMutableArrayRef groups
;
154 ODRecordRef primary_group
;
155 CFErrorRef error
= NULL
;
161 query
= ODQueryCreateWithNode(NULL
, node
, CFSTR(kDSStdRecordTypeGroups
),
162 CFSTR(kDSNAttrMember
), kODMatchContains
, ODRecordGetRecordName(user
), NULL
, limit
, &error
);
163 results
= ODQueryCopyResults(query
, false, &error
);
166 groups
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
168 primary_gid
= GetGIDFromRecord(user
);
169 primary_group
= CopyGroupRecordWithGID(node
, primary_gid
);
170 CFArrayAppendValue(groups
, primary_group
);
171 CFRelease(primary_group
);
173 for (i
= 0; i
< CFArrayGetCount(results
); i
++) {
174 gr
= (ODRecordRef
)CFArrayGetValueAtIndex(results
, i
);
175 if (GetGIDFromRecord(gr
) != primary_gid
) {
176 CFArrayAppendValue(groups
, gr
);
186 cvfprintf(FILE *file
, const char *format
, va_list args
)
190 CFStringRef formatStr
= CFStringCreateWithCStringNoCopy(NULL
, format
, kCFStringEncodingUTF8
, kCFAllocatorNull
);
192 CFStringRef str
= CFStringCreateWithFormatAndArguments(NULL
, NULL
, formatStr
, args
);
194 size_t size
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(str
), kCFStringEncodingUTF8
) + 1;
196 if (cstr
&& CFStringGetCString(str
, cstr
, size
, kCFStringEncodingUTF8
)) {
197 result
= fprintf(file
, "%s", cstr
);
202 CFRelease(formatStr
);
208 cfprintf(FILE *file
, const char *format
, ...)
213 va_start(args
, format
);
214 result
= cvfprintf(file
, format
, args
);
220 cprintf(const char *format
, ...)
225 va_start(args
, format
);
226 result
= cvfprintf(stdout
, format
, args
);