/
ds_module.c
1478 lines (1132 loc) · 35.3 KB
/
ds_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008-2011 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This ds contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this ds except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* ds.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#ifdef DS_AVAILABLE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <pthread.h>
#include <ils.h>
#include <pwd.h>
#include <grp.h>
#include <fstab.h>
#include <netdb.h>
#include <notify.h>
#include <notify_keys.h>
#include <si_data.h>
#include <si_module.h>
#include <netdb_async.h>
#include <net/if.h>
#include <xpc/xpc.h>
#include <xpc/private.h>
#include <opendirectory/odipc.h>
#include <servers/bootstrap.h>
#include <bootstrap_priv.h>
#include <opendirectory/DSlibinfoMIG_types.h>
#ifdef DEBUG
#include <asl.h>
#endif
#ifdef __i386__
/* <rdar://problem/10675978> */
__attribute__((weak_import))
void xpc_dictionary_get_audit_token(xpc_object_t xdict, audit_token_t *token);
__attribute__((weak_import))
xpc_pipe_t xpc_pipe_create(const char *name, uint64_t flags);
__attribute__((weak_import))
void xpc_pipe_invalidate(xpc_pipe_t pipe);
__attribute__((weak_import))
int xpc_pipe_routine(xpc_pipe_t pipe, xpc_object_t message, xpc_object_t *reply);
#endif
#define IPV6_ADDR_LEN 16
#define IPV4_ADDR_LEN 4
typedef si_item_t *(*od_extract_t)(si_mod_t *si, xpc_object_t reply, const void *extra, uint64_t valid_global, uint64_t valid_cat);
/* notify SPI */
uint32_t notify_peek(int token, uint32_t *val);
typedef struct
{
int notify_token_global;
int notify_token_user;
int notify_token_group;
int notify_token_service;
} ds_si_private_t;
extern uint32_t gL1CacheEnabled;
extern int _si_opendirectory_disabled;
static pthread_key_t _ds_serv_cache_key = 0;
static xpc_pipe_t __od_pipe; /* use accessor only */
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
mach_port_t _ds_port;
static void
_od_fork_child(void)
{
// re-enable opendirectory interaction since we forked
_si_opendirectory_disabled = 0;
if (__od_pipe != NULL) {
xpc_pipe_invalidate(__od_pipe);
/* disable release due to 10649340, it will cause a minor leak for each fork without exec */
// xpc_release(__od_pipe);
__od_pipe = NULL;
}
_ds_port = MACH_PORT_NULL;
pthread_mutex_unlock(&mutex);
}
static void
_od_fork_prepare(void)
{
pthread_mutex_lock(&mutex);
}
static void
_od_fork_parent(void)
{
pthread_mutex_unlock(&mutex);
}
static void
_ds_serv_cache_free(void *x)
{
if (x != NULL) si_item_release(x);
}
void
_si_disable_opendirectory(void)
{
_si_opendirectory_disabled = 1;
_ds_port = MACH_PORT_NULL;
}
XPC_RETURNS_RETAINED
static xpc_pipe_t
_od_xpc_pipe(bool resetPipe)
{
static dispatch_once_t once;
xpc_pipe_t result = NULL;
#ifdef __i386__
if (xpc_pipe_create == NULL) {
_si_disable_opendirectory();
return NULL;
}
#endif
dispatch_once(&once, ^(void) {
char *rc_xbs;
/* if this is a build environment we ignore opendirectoryd */
rc_xbs = getenv("RC_XBS");
if ((issetugid() == 0) && (rc_xbs != NULL) && (strcmp(rc_xbs, "YES") == 0)) {
_si_opendirectory_disabled = 1;
return;
}
pthread_atfork(_od_fork_prepare, _od_fork_parent, _od_fork_child);
});
if (_si_opendirectory_disabled == 1) {
return NULL;
}
pthread_mutex_lock(&mutex);
if (resetPipe) {
xpc_release(__od_pipe);
__od_pipe = NULL;
}
if (__od_pipe == NULL) {
if (!issetugid() && getenv("OD_DEBUG_MODE") != NULL) {
__od_pipe = xpc_pipe_create(kODMachLibinfoPortNameDebug, 0);
} else {
__od_pipe = xpc_pipe_create(kODMachLibinfoPortName, XPC_PIPE_FLAG_PRIVILEGED);
}
}
if (__od_pipe != NULL) result = xpc_retain(__od_pipe);
pthread_mutex_unlock(&mutex);
return result;
}
static bool
_od_running(void)
{
xpc_pipe_t pipe;
pipe = _od_xpc_pipe(false);
if (pipe != NULL) {
xpc_release(pipe);
}
if (_si_opendirectory_disabled) {
return 0;
}
return (pipe != NULL);
}
static void
_ds_child(void)
{
_ds_port = MACH_PORT_NULL;
}
int
_ds_running(void)
{
kern_return_t status;
char *od_debug_mode = NULL;
if (_ds_port != MACH_PORT_NULL) return 1;
if (_si_opendirectory_disabled) return 0;
pthread_atfork(NULL, NULL, _ds_child);
if (!issetugid()) {
od_debug_mode = getenv("OD_DEBUG_MODE");
}
if (od_debug_mode) {
status = bootstrap_look_up(bootstrap_port, kDSStdMachDSLookupPortName "_debug", &_ds_port);
} else {
status = bootstrap_look_up2(bootstrap_port, kDSStdMachDSLookupPortName, &_ds_port, 0, BOOTSTRAP_PRIVILEGED_SERVER);
}
if ((status != BOOTSTRAP_SUCCESS) && (status != BOOTSTRAP_UNKNOWN_SERVICE)) _ds_port = MACH_PORT_NULL;
return (_ds_port != MACH_PORT_NULL);
}
static bool
_valid_token(xpc_object_t reply)
{
audit_token_t token;
/*
* This should really call audit_token_to_au32,
* but that's in libbsm, not in a Libsystem library.
*/
xpc_dictionary_get_audit_token(reply, &token);
return ((uid_t) token.val[1] == 0);
}
static void
_ds_get_validation(si_mod_t *si, uint64_t *a, uint64_t *b, int cat)
{
ds_si_private_t *pp;
uint32_t peek;
int status;
if (si == NULL) return;
pp = (ds_si_private_t *)si->private;
if (pp == NULL) return;
if (a != NULL)
{
*a = 0;
status = notify_peek(pp->notify_token_global, &peek);
if (status == NOTIFY_STATUS_OK) *a = ntohl(peek);
}
if (b != NULL)
{
*b = 0;
peek = 0;
status = NOTIFY_STATUS_FAILED;
if (cat == CATEGORY_USER) status = notify_peek(pp->notify_token_user, &peek);
else if (cat == CATEGORY_GROUP) status = notify_peek(pp->notify_token_group, &peek);
else if (cat == CATEGORY_GROUPLIST) status = notify_peek(pp->notify_token_group, &peek);
else if (cat == CATEGORY_SERVICE) status = notify_peek(pp->notify_token_service, &peek);
if (status == NOTIFY_STATUS_OK) *b = ntohl(peek);
}
}
XPC_RETURNS_RETAINED
__private_extern__ xpc_object_t
_od_rpc_call(const char *procname, xpc_object_t payload, xpc_pipe_t (*get_pipe)(bool))
{
xpc_object_t result = NULL;
xpc_object_t reply;
xpc_pipe_t od_pipe;
int retries, rc;
od_pipe = get_pipe(false);
if (od_pipe == NULL) return NULL;
if (payload == NULL) {
payload = xpc_dictionary_create(NULL, NULL, 0);
}
// we nest it for backward compatibility so we can do independent submissions
xpc_dictionary_set_string(payload, OD_RPC_NAME, procname);
xpc_dictionary_set_int64(payload, OD_RPC_VERSION, 2);
for (retries = 0; od_pipe != NULL && retries < 2; retries++) {
rc = xpc_pipe_routine(od_pipe, payload, &reply);
switch (rc) {
case EPIPE:
xpc_release(od_pipe);
od_pipe = get_pipe(true);
break;
case EAGAIN:
/* just loop and try to send again */
break;
case 0:
if (_valid_token(reply) == true) {
result = reply;
}
/* fall through since we got a valid response */
default:
/* release and NULL the pipe it'll break the loop */
xpc_release(od_pipe);
od_pipe = NULL;
break;
}
}
if (od_pipe != NULL) {
xpc_release(od_pipe);
}
return result;
}
static si_list_t *
_ds_list(si_mod_t *si, int cat, const char *procname, const void *extra, od_extract_t extract)
{
__block si_list_t *list;
uint64_t va, vb;
xpc_object_t reply, result;
if (procname == NULL) return NULL;
_ds_get_validation(si, &va, &vb, cat);
list = NULL;
reply = _od_rpc_call(procname, NULL, _od_xpc_pipe);
if (reply != NULL) {
result = xpc_dictionary_get_value(reply, OD_RPC_RESULT);
if (result != NULL && xpc_get_type(result) == XPC_TYPE_ARRAY) {
xpc_array_apply(result, ^bool(size_t index, xpc_object_t value) {
si_item_t *item = extract(si, value, extra, va, vb);
list = si_list_add(list, item);
si_item_release(item);
return true;
});
}
xpc_release(reply);
}
return list;
}
static si_item_t *
_ds_item(si_mod_t *si, int cat, const char *procname, const void *extra, od_extract_t extract, xpc_object_t payload)
{
xpc_object_t result;
uint64_t va, vb;
si_item_t *item = NULL;
if (procname == NULL) return NULL;
result = _od_rpc_call(procname, payload, _od_xpc_pipe);
if (result != NULL) {
_ds_get_validation(si, &va, &vb, cat);
if (xpc_dictionary_get_int64(result, OD_RPC_ERROR) == 0) {
item = extract(si, result, extra, va, vb);
}
xpc_release(result);
}
return item;
}
static int
_ds_is_valid(si_mod_t *si, si_item_t *item)
{
si_mod_t *src;
ds_si_private_t *pp;
int status;
uint32_t oldval, newval;
if (si == NULL) return 0;
if (item == NULL) return 0;
if (si->name == NULL) return 0;
if (item->src == NULL) return 0;
pp = (ds_si_private_t *)si->private;
if (pp == NULL) return 0;
src = (si_mod_t *)item->src;
if (src->name == NULL) return 0;
if (string_not_equal(si->name, src->name)) return 0;
/* check global invalidation */
oldval = item->validation_a;
newval = -1;
status = notify_peek(pp->notify_token_global, &newval);
if (status != NOTIFY_STATUS_OK) return 0;
newval = ntohl(newval);
if (oldval != newval) return 0;
oldval = item->validation_b;
newval = -1;
if (item->type == CATEGORY_USER) status = notify_peek(pp->notify_token_user, &newval);
else if (item->type == CATEGORY_GROUP) status = notify_peek(pp->notify_token_group, &newval);
else if (item->type == CATEGORY_SERVICE) status = notify_peek(pp->notify_token_service, &newval);
else return 0;
if (status != NOTIFY_STATUS_OK) return 0;
newval = ntohl(newval);
if (oldval != newval) return 0;
return 1;
}
static void
_free_addr_list(char **l)
{
int i;
if (l == NULL) return;
for (i = 0; l[i] != NULL; i++) free(l[i]);
free(l);
}
/* map ipv4 addresses and append to v6 list */
static int
_map_v4(char ***v6, uint32_t n6, char **v4, uint32_t n4)
{
struct in6_addr a6;
uint32_t i;
a6.__u6_addr.__u6_addr32[0] = 0x00000000;
a6.__u6_addr.__u6_addr32[1] = 0x00000000;
a6.__u6_addr.__u6_addr32[2] = htonl(0x0000ffff);
if (*v6 == NULL)
{
*v6 = (char **)calloc(n4 + 1, sizeof(char *));
}
else
{
*v6 = (char **)reallocf(*v6, (n6 + n4 + 1) * sizeof(char *));
}
if (*v6 == NULL) return -1;
for (i = 0; i < n4; i++)
{
(*v6)[n6] = (char *)calloc(1, IPV6_ADDR_LEN);
if ((*v6)[n6] == NULL) return -1;
memcpy(&(a6.__u6_addr.__u6_addr32[3]), v4[i], IPV4_ADDR_LEN);
memcpy((*v6)[n6], &(a6.__u6_addr.__u6_addr32[0]), IPV6_ADDR_LEN);
n6++;
}
return 0;
}
static xpc_object_t
_xpc_query_key_string(const char *key, const char *value)
{
xpc_object_t payload;
if (value == NULL) return NULL;
payload = xpc_dictionary_create(NULL, NULL, 0);
if (payload == NULL) return NULL;
xpc_dictionary_set_string(payload, key, value);
return payload;
}
static xpc_object_t
_xpc_query_key_id(const char *key, id_t idValue)
{
xpc_object_t payload;
payload = xpc_dictionary_create(NULL, NULL, 0);
if (payload == NULL) return NULL;
xpc_dictionary_set_int64(payload, key, idValue);
return payload;
}
static xpc_object_t
_xpc_query_key_uuid(const char *key, uuid_t uu)
{
xpc_object_t payload;
payload = xpc_dictionary_create(NULL, NULL, 0);
if (payload == NULL) return NULL;
xpc_dictionary_set_uuid(payload, key, uu);
return payload;
}
static xpc_object_t
_xpc_query_key_int(const char *key, int64_t intValue)
{
xpc_object_t payload;
payload = xpc_dictionary_create(NULL, NULL, 0);
if (payload == NULL) return NULL;
xpc_dictionary_set_int64(payload, key, intValue);
return payload;
}
#pragma mark -
static char **
_extract_array(xpc_object_t reply, const char *key, unsigned int *len)
{
xpc_object_t xpc_array;
char **result;
xpc_array = xpc_dictionary_get_value(reply, key);
if (xpc_array == NULL || xpc_get_type(xpc_array) != XPC_TYPE_ARRAY) {
return calloc(1, sizeof(*result));
}
result = calloc(xpc_array_get_count(xpc_array) + 1, sizeof(*result));
if (result == NULL) {
return NULL;
}
if (len != NULL) {
/* include trailing NULL */
(*len) = xpc_array_get_count(xpc_array) + 1;
}
xpc_array_apply(xpc_array, ^_Bool(size_t idx, xpc_object_t value) {
result[idx] = (char *) xpc_string_get_string_ptr(value);
return true;
});
return result;
}
static const char *
_extract_string(xpc_object_t reply, const char *key)
{
xpc_object_t value = xpc_dictionary_get_value(reply, key);
xpc_type_t type;
const char *result = NULL;
if (value == NULL) {
return "";
}
type = xpc_get_type(value);
if (type == XPC_TYPE_STRING) {
result = xpc_string_get_string_ptr(value);
} else if (type == XPC_TYPE_ARRAY && xpc_array_get_count(value) != 0) {
result = xpc_array_get_string(value, 0);
}
if (result == NULL) {
result = "";
}
return result;
}
static uint32_t
_extract_uint32(xpc_object_t reply, const char *key)
{
xpc_object_t value = xpc_dictionary_get_value(reply, key);
xpc_type_t type;
uint32_t result;
if (value == NULL) {
return 0;
}
type = xpc_get_type(value);
if (type == XPC_TYPE_ARRAY && xpc_array_get_count(value) != 0) {
value = xpc_array_get_value(value, 0);
type = xpc_get_type(value);
}
if (type == XPC_TYPE_STRING) {
result = (int) strtol(xpc_string_get_string_ptr(value), NULL, 10);
} else if (type == XPC_TYPE_INT64) {
result = (uint32_t) xpc_int64_get_value(value);
} else if (type == XPC_TYPE_BOOL) {
result = xpc_bool_get_value(value);
} else {
result = 0;
}
return result;
}
static si_item_t *
_extract_user(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
struct passwd tmp;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.pw_name = (char *) _extract_string(reply, "pw_name");
tmp.pw_passwd = (char *) _extract_string(reply, "pw_passwd");
tmp.pw_uid = (uid_t) _extract_uint32(reply, "pw_uid");
tmp.pw_gid = (gid_t) _extract_uint32(reply, "pw_gid");
tmp.pw_change = (time_t) _extract_uint32(reply, "pw_change");
tmp.pw_expire = (time_t) _extract_uint32(reply, "pw_expire");
tmp.pw_class = (char *) _extract_string(reply, "pw_class");
tmp.pw_gecos = (char *) _extract_string(reply, "pw_gecos");
tmp.pw_dir = (char *) _extract_string(reply, "pw_dir");
tmp.pw_shell = (char *) _extract_string(reply, "pw_shell");
return (si_item_t *)LI_ils_create("L4488ss44LssssL", (unsigned long)si, CATEGORY_USER, 1, valid_global, valid_cat, tmp.pw_name, tmp.pw_passwd, tmp.pw_uid, tmp.pw_gid, tmp.pw_change, tmp.pw_class, tmp.pw_gecos, tmp.pw_dir, tmp.pw_shell, tmp.pw_expire);
}
static si_item_t *
_extract_group(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
si_item_t *item;
struct group tmp;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.gr_name = (char *) _extract_string(reply, "gr_name");
tmp.gr_passwd = (char *) _extract_string(reply, "gr_passwd");
tmp.gr_gid = (gid_t) _extract_uint32(reply, "gr_gid");
tmp.gr_mem = _extract_array(reply, "gr_mem", NULL);
item = (si_item_t *) LI_ils_create("L4488ss4*", (unsigned long)si, CATEGORY_GROUP, 1, valid_global, valid_cat, tmp.gr_name, tmp.gr_passwd, tmp.gr_gid, tmp.gr_mem);
if (tmp.gr_mem != NULL) {
free(tmp.gr_mem); /* have to free because it's allocated */
}
return item;
}
static si_item_t *
_extract_netgroup(si_mod_t *si, xpc_object_t reply, const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
const char *host, *user, *domain;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
host = _extract_string(reply, "host");
user = _extract_string(reply, "user");
domain = _extract_string(reply, "domain");
return (si_item_t *)LI_ils_create("L4488sss", (unsigned long)si, CATEGORY_NETGROUP, 1, valid_global, valid_cat, host, user, domain);
}
static si_item_t *
_extract_alias(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
struct aliasent tmp;
si_item_t *item;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.alias_name = (char *) _extract_string(reply, "alias_name");
tmp.alias_local = _extract_uint32(reply, "alias_local");
tmp.alias_members = _extract_array(reply, "alias_members", &tmp.alias_members_len);
item = (si_item_t *)LI_ils_create("L4488s4*4", (unsigned long)si, CATEGORY_ALIAS, 1, valid_global, valid_cat, tmp.alias_name, tmp.alias_members_len, tmp.alias_members, tmp.alias_local);
if (tmp.alias_members != NULL) {
free(tmp.alias_members);
}
return item;
}
static si_item_t *
_extract_network(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
struct netent tmp;
si_item_t *item;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.n_name = (char *) _extract_string(reply, "n_name");
tmp.n_aliases = _extract_array(reply, "n_aliases", NULL);
tmp.n_net = _extract_uint32(reply, "n_net");
tmp.n_addrtype = AF_INET; /* opendirectoryd doesn't return this value, only AF_INET is supported */
item = (si_item_t *)LI_ils_create("L4488s*44", (unsigned long)si, CATEGORY_NETWORK, 1, valid_global, valid_cat, tmp.n_name, tmp.n_aliases, tmp.n_addrtype, tmp.n_net);
if (tmp.n_aliases != NULL) {
free(tmp.n_aliases);
}
return item;
}
static si_item_t *
_extract_service(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
struct servent tmp;
si_item_t *item;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.s_name = (char *) _extract_string(reply, "s_name");
tmp.s_aliases = _extract_array(reply, "s_aliases", NULL);
tmp.s_port = (unsigned int) htons(_extract_uint32(reply, "s_port"));
tmp.s_proto = (char *) _extract_string(reply, "s_proto");
item = (si_item_t *)LI_ils_create("L4488s*4s", (unsigned long)si, CATEGORY_SERVICE, 1, valid_global, valid_cat, tmp.s_name, tmp.s_aliases, tmp.s_port, tmp.s_proto);
if (tmp.s_aliases != NULL) {
free(tmp.s_aliases);
}
return item;
}
static si_item_t *
_extract_protocol(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
struct protoent tmp;
si_item_t *item;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.p_name = (char *) _extract_string(reply, "p_name");
tmp.p_proto = (int) _extract_uint32(reply, "p_proto");
tmp.p_aliases = _extract_array(reply, "p_aliases", NULL);
item = (si_item_t *)LI_ils_create("L4488s*4", (unsigned long)si, CATEGORY_PROTOCOL, 1, valid_global, valid_cat, tmp.p_name, tmp.p_aliases, tmp.p_proto);
if (tmp.p_aliases != NULL) {
free(tmp.p_aliases);
}
return item;
}
static si_item_t *
_extract_rpc(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
struct rpcent tmp;
si_item_t *item;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.r_name = (char *) _extract_string(reply, "r_name");
tmp.r_number = (int) _extract_uint32(reply, "r_number");
tmp.r_aliases = _extract_array(reply, "r_aliases", NULL);
item = (si_item_t *)LI_ils_create("L4488s*4", (unsigned long)si, CATEGORY_RPC, 1, valid_global, valid_cat, tmp.r_name, tmp.r_aliases, tmp.r_number);
if (tmp.r_aliases != NULL) {
free(tmp.r_aliases);
}
return item;
}
static si_item_t *
_extract_fstab(si_mod_t *si, xpc_object_t reply, __unused const void *ignored, uint64_t valid_global, uint64_t valid_cat)
{
struct fstab tmp;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
tmp.fs_file = (char *) _extract_string(reply, "fs_file");
if (tmp.fs_file == NULL) return NULL;
tmp.fs_spec = (char *) _extract_string(reply, "fs_spec");
tmp.fs_freq = _extract_uint32(reply, "fs_freq");
tmp.fs_passno = _extract_uint32(reply, "fs_passno");
tmp.fs_mntops = (char *) _extract_string(reply, "fs_mntops");
tmp.fs_type = (char *) _extract_string(reply, "fs_type");
tmp.fs_vfstype = (char *) _extract_string(reply, "fs_vfstype");
return (si_item_t *)LI_ils_create("L4488sssss44", (unsigned long)si, CATEGORY_FS, 1, valid_global, valid_cat, tmp.fs_spec, tmp.fs_file, tmp.fs_vfstype, tmp.fs_mntops, tmp.fs_type, tmp.fs_freq, tmp.fs_passno);
}
static si_item_t *
_extract_mac_mac(si_mod_t *si, xpc_object_t reply, const void *extra, uint64_t valid_global, uint64_t valid_cat)
{
const char *value;
char *cmac;
si_item_t *out;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
if (extra == NULL) return NULL;
value = _extract_string(reply, "mac");
if (value == NULL || value[0] == '\0') return NULL;
cmac = si_standardize_mac_address(value);
if (cmac == NULL) return NULL;
out = (si_item_t *)LI_ils_create("L4488ss", (unsigned long)si, CATEGORY_MAC, 1, valid_global, valid_cat, extra, cmac);
free(cmac);
return out;
}
static si_item_t *
_extract_mac_name(si_mod_t *si, xpc_object_t reply, const void *extra, uint64_t valid_global, uint64_t valid_cat)
{
const char *name;
si_item_t *out;
if (si == NULL) return NULL;
if (reply == NULL) return NULL;
if (extra == NULL) return NULL;
name = _extract_string(reply, "name");
out = (si_item_t *)LI_ils_create("L4488ss", (unsigned long)si, CATEGORY_MAC, 1, valid_global, valid_cat, name, extra);
return out;
}
#pragma mark -
static si_item_t *
ds_user_byname(si_mod_t *si, const char *name)
{
xpc_object_t payload;
si_item_t *item;
if (!_od_running()) return NULL;
payload = _xpc_query_key_string("name", name);
if (payload == NULL) return NULL;
item = _ds_item(si, CATEGORY_USER, "getpwnam", NULL, _extract_user, payload);
xpc_release(payload);
return item;
}
static si_item_t *
ds_user_byuid(si_mod_t *si, uid_t uid)
{
xpc_object_t payload;
si_item_t *item;
if (!_od_running()) return NULL;
payload = _xpc_query_key_id("uid", uid);
if (payload == NULL) return NULL;
item = _ds_item(si, CATEGORY_USER, "getpwuid", NULL, _extract_user, payload);
xpc_release(payload);
return item;
}
static si_item_t *
ds_user_byuuid(si_mod_t *si, uuid_t uuid)
{
xpc_object_t payload;
si_item_t *item;
if (!_od_running()) return NULL;
payload = _xpc_query_key_uuid("uuid", uuid);
if (payload == NULL) return NULL;
item = _ds_item(si, CATEGORY_USER, "getpwuuid", NULL, _extract_user, payload);
xpc_release(payload);
return item;
}
static si_list_t *
ds_user_all(si_mod_t *si)
{
return _ds_list(si, CATEGORY_USER, "getpwent", NULL, _extract_user);
}
static si_item_t *
ds_group_byname(si_mod_t *si, const char *name)
{
xpc_object_t payload;
si_item_t *item;
if (!_od_running()) return NULL;
payload = _xpc_query_key_string("name", name);
if (payload == NULL) return NULL;
item = _ds_item(si, CATEGORY_GROUP, "getgrnam", NULL, _extract_group, payload);
xpc_release(payload);
return item;
}
static si_item_t *
ds_group_bygid(si_mod_t *si, gid_t gid)
{
xpc_object_t payload;
si_item_t *item;
if (!_od_running()) return NULL;
payload = _xpc_query_key_id("gid", gid);
if (payload == NULL) return NULL;
item = _ds_item(si, CATEGORY_GROUP, "getgrgid", NULL, _extract_group, payload);
xpc_release(payload);
return item;
}
static si_item_t *
ds_group_byuuid(si_mod_t *si, uuid_t uuid)
{
xpc_object_t payload;
si_item_t *item;
if (!_od_running()) return NULL;
payload = _xpc_query_key_uuid("uuid", uuid);
if (payload == NULL) return NULL;
item = _ds_item(si, CATEGORY_GROUP, "getgruuid", NULL, _extract_group, payload);
xpc_release(payload);
return item;
}
static si_list_t *
ds_group_all(si_mod_t *si)
{
if (!_od_running()) return NULL;
return _ds_list(si, CATEGORY_GROUP, "getgrent", NULL, _extract_group);
}
static si_item_t *
ds_grouplist(si_mod_t *si, const char *name, uint32_t ngroups)
{
xpc_object_t payload, reply;
si_item_t *item = NULL;
if (!_od_running()) return NULL;
if (name == NULL) return NULL;
payload = xpc_dictionary_create(NULL, NULL, 0);
if (payload == NULL) return NULL;
xpc_dictionary_set_string(payload, "name", name);
xpc_dictionary_set_int64(payload, "ngroups", ngroups);
reply = _od_rpc_call("getgrouplist", payload, _od_xpc_pipe);
if (reply != NULL) {
size_t gidptrsz;
const gid_t *gidptr = xpc_dictionary_get_data(reply, "groups", &gidptrsz);
int32_t count;
uint64_t va, vb;
_ds_get_validation(si, &va, &vb, CATEGORY_GROUPLIST);
/* see what we were sent */
count = _extract_uint32(reply, "count");
if (count != 0) {
item = (si_item_t *)LI_ils_create("L4488s4@", (unsigned long)si, CATEGORY_GROUPLIST, 1, va, vb, name, count,
gidptrsz, gidptr);
}
xpc_release(reply);