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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
|
/*
SSSD
PAC Responder - utility finctions
Copyright (C) Sumit Bose <sbose@redhat.com> 2012
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <util/data_blob.h>
#include <gen_ndr/security.h>
#include "util/util.h"
#include "responder/pac/pacsrv.h"
static errno_t get_rid(struct dom_sid *sid, uint32_t *rid)
{
if (sid == NULL || sid->num_auths < 1 || rid == NULL) {
return EINVAL;
}
*rid = sid->sub_auths[sid->num_auths - 1];
return EOK;
}
/**
* Find the Posix ID to a SID from the local IPA domain
*/
errno_t local_sid_to_id(struct local_mapping_ranges *map, struct dom_sid *sid,
uint32_t *id)
{
int ret;
uint32_t rid;
if (map == NULL || sid == NULL || id == NULL) {
return EINVAL;
}
ret = get_rid(sid, &rid);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("get_rid failed.\n"));
return ret;
}
if (rid >= map->primary_rids.min && rid <= map->primary_rids.max) {
*id = map->local_ids.min + (rid - map->primary_rids.min);
} else if (rid >= map->secondary_rids.min &&
rid <= map->secondary_rids.max) {
*id = map->local_ids.min + (rid - map->secondary_rids.min);
} else {
return ENOENT;
}
if (*id < map->local_ids.min || *id > map->local_ids.max) {
return ERANGE;
}
return EOK;
}
struct sss_domain_info *find_domain_by_id(struct sss_domain_info *domains,
const char *id_str)
{
struct sss_domain_info *dom;
struct sss_domain_info *ret_dom = NULL;
if (id_str == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("Missing domain id.\n"));
return NULL;
}
for (dom = domains; dom; dom = get_next_domain(dom, true)) {
if (dom->domain_id == NULL) {
continue;
}
if (strcasecmp(dom->domain_id, id_str) == 0) {
ret_dom = dom;
break;
}
}
if (!ret_dom) {
DEBUG(SSSDBG_OP_FAILURE, ("No domain with domain ID [%s] found.\n",
id_str));
}
return ret_dom;
}
/**
* Add a new remote domain and the corresponding ID range to the context of
* the libsss_idmap. Without this it is not possible to find the Posix UID for
* a user fo the remote domain.
*/
errno_t add_idmap_domain(struct sss_idmap_ctx *idmap_ctx,
struct sysdb_ctx *sysdb,
const char *domain_name,
const char *dom_sid_str)
{
struct sss_idmap_range range = {0, 0};
enum idmap_error_code err;
TALLOC_CTX *tmp_ctx = NULL;
size_t range_count;
struct range_info **range_list;
size_t c;
int ret;
if (domain_name == NULL || dom_sid_str == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("Missing domain name or SID.\n"));
return EINVAL;
}
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_new failed.\n"));
return ENOMEM;
}
ret = sysdb_get_ranges(tmp_ctx, sysdb, &range_count, &range_list);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("sysdb_get_ranges failed.\n"));
goto done;
}
for (c = 0; c < range_count; c++) {
if (range_list[c]->trusted_dom_sid != NULL &&
strcmp(range_list[c]->trusted_dom_sid, dom_sid_str) == 0) {
range.min = range_list[c]->base_id;
range.max = range_list[c]->base_id +
range_list[c]->id_range_size - 1;
/* TODO: add support for multiple ranges. */
break;
}
}
if (range.min == 0 && range.max == 0) {
DEBUG(SSSDBG_OP_FAILURE, ("Failed to find mapping range for domain "
"[%s][%s].\n", domain_name, dom_sid_str));
ret = ENOENT;
goto done;
}
err = sss_idmap_add_domain(idmap_ctx, domain_name, dom_sid_str, &range);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_idmap_add_domain failed.\n"));
return EFAULT;
}
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
/**
* Find the corresponding UID for a user from a remote domain based on the
* domain SID of the remote domain and the RID of the user.
*/
errno_t domsid_rid_to_uid(struct pac_ctx *pac_ctx,
struct sysdb_ctx *sysdb,
const char *domain_name,
struct dom_sid2 *domsid, uint32_t rid,
uid_t *uid)
{
enum idmap_error_code err;
char *sid_str = NULL;
char *dom_sid_str = NULL;
uint32_t id;
int ret;
err = sss_idmap_smb_sid_to_sid(pac_ctx->idmap_ctx, domsid,
&dom_sid_str);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_idmap_smb_sid_to_sid failed.\n"));
ret = EFAULT;
goto done;
}
sid_str = talloc_asprintf(NULL, "%s-%lu", dom_sid_str, (unsigned long) rid);
if (sid_str == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("dom_sid_and_rid_string failed.\n"));
return ENOMEM;
}
err = sss_idmap_sid_to_unix(pac_ctx->idmap_ctx, sid_str, &id);
if (err == IDMAP_NO_DOMAIN) {
ret = add_idmap_domain(pac_ctx->idmap_ctx, sysdb, domain_name,
dom_sid_str);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("add_idmap_domain failed.\n"));
goto done;
}
err = sss_idmap_sid_to_unix(pac_ctx->idmap_ctx, sid_str, &id);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_FATAL_FAILURE, ("sss_idmap_sid_to_unix failed "
"even in the second attempt.\n"));
ret = ENOENT;
goto done;
}
} else if (err != IDMAP_SUCCESS && err != IDMAP_NO_DOMAIN) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_idmap_sid_to_unix failed.\n"));
ret = EFAULT;
goto done;
}
*uid = (uid_t) id;
ret = EOK;
done:
talloc_free(dom_sid_str);
talloc_free(sid_str);
return ret;
}
/**
* Return information about the local domain from the main PAC responder
* context or try to read it from cache and store it in the context.
*/
errno_t get_parent_domain_data(struct pac_ctx *pac_ctx,
struct sss_domain_info *dom,
struct dom_sid **_sid,
struct local_mapping_ranges **_range_map)
{
struct sysdb_ctx *sysdb;
int ret;
struct ldb_dn *basedn;
const char *attrs[] = {SYSDB_SUBDOMAIN_ID,
NULL};
size_t msgs_count;
const char *sid_str;
struct ldb_message **msgs;
TALLOC_CTX *tmp_ctx = NULL;
struct dom_sid *sid = NULL;
enum idmap_error_code err;
size_t range_count;
struct range_info **range_list;
struct local_mapping_ranges *r_map = NULL;
size_t c;
if (pac_ctx->my_dom_sid == NULL || pac_ctx->range_map == NULL) {
sysdb = dom->sysdb;
if (sysdb == NULL) {
DEBUG(SSSDBG_FATAL_FAILURE, ("Missing sysdb context.\n"));
ret = EINVAL;
goto done;
}
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_new failed.\n"));
ret = ENOMEM;
goto done;
}
/* The data of the parent domain should be read here. */
basedn = sysdb_domain_dn(sysdb, tmp_ctx,
IS_SUBDOMAIN(dom) ? dom->parent : dom);
if (basedn == NULL) {
ret = ENOMEM;
goto done;
}
if (pac_ctx->my_dom_sid == NULL) {
ret = sysdb_search_entry(tmp_ctx, sysdb, basedn, LDB_SCOPE_BASE, NULL,
attrs, &msgs_count, &msgs);
if (ret != LDB_SUCCESS) {
ret = EIO;
goto done;
}
if (msgs_count != 1) {
DEBUG(SSSDBG_OP_FAILURE, ("Base search returned [%d] results, "
"expected 1.\n", msgs_count));
ret = EINVAL;
goto done;
}
sid_str = ldb_msg_find_attr_as_string(msgs[0], SYSDB_SUBDOMAIN_ID, NULL);
if (sid_str == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("SID of my domain is not available.\n"));
ret = EINVAL;
goto done;
}
err = sss_idmap_sid_to_smb_sid(pac_ctx->idmap_ctx, sid_str, &sid);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_idmap_sid_to_smb_sid failed.\n"));
ret = EFAULT;
goto done;
}
pac_ctx->my_dom_sid = talloc_memdup(pac_ctx, sid,
sizeof(struct dom_sid));
if (pac_ctx->my_dom_sid == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_memdup failed.\n"));
ret = ENOMEM;
goto done;
}
}
if (pac_ctx->range_map == NULL) {
ret = sysdb_get_ranges(tmp_ctx, sysdb, &range_count, &range_list);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("sysdb_get_ranges failed.\n"));
goto done;
}
for (c = 0; c < range_count; c++) {
if (range_list[c]->trusted_dom_sid == NULL &&
range_list[c]->secondary_base_rid != 0) {
r_map = talloc_zero(pac_ctx,
struct local_mapping_ranges);
if (r_map == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_zero failed.\n"));
ret = ENOMEM;
goto done;
}
r_map->local_ids.min = range_list[c]->base_id;
r_map->local_ids.max = range_list[c]->base_id +
range_list[c]->id_range_size - 1;
r_map->primary_rids.min = range_list[c]->base_rid;
r_map->primary_rids.max = range_list[c]->base_rid +
range_list[c]->id_range_size - 1;
r_map->secondary_rids.min = range_list[c]->secondary_base_rid;
r_map->secondary_rids.max = range_list[c]->secondary_base_rid +
range_list[c]->id_range_size - 1;
/* TODO: add support for multiple ranges. */
break;
}
}
if (r_map == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("Failed to find local id map.\n"));
ret = ENOENT;
goto done;
}
pac_ctx->range_map = r_map;
}
}
*_sid = pac_ctx->my_dom_sid;
*_range_map = pac_ctx->range_map;
ret = EOK;
done:
talloc_free(sid);
talloc_free(tmp_ctx);
return ret;
}
/**
* Check if a given SID belongs to a domain identified by the domain SID.
*/
bool dom_sid_in_domain(const struct dom_sid *domain_sid,
const struct dom_sid *sid)
{
size_t c;
if (!domain_sid || !sid) {
return false;
}
if (domain_sid->sid_rev_num != sid->sid_rev_num) {
return false;
}
for (c = 0; c < 6; c++) {
if (domain_sid->id_auth[c] != sid->id_auth[c]) {
return false;
}
}
if (domain_sid->num_auths > sid->num_auths) {
return false;
}
for (c = 0; c < domain_sid->num_auths-1; c++) {
if (domain_sid->sub_auths[c] != sid->sub_auths[c]) {
return false;
}
}
return true;
}
static errno_t get_dom_grps_from_hash(TALLOC_CTX *mem_ctx,
hash_table_t *gid_table,
struct sss_domain_info *grp_dom,
struct pac_dom_grps *dom_grps)
{
int ret;
size_t gid_count;
size_t g;
struct hash_iter_context_t *iter;
hash_entry_t *entry;
gid_t *gids = NULL;
if (grp_dom == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("Missing domain for groups.\n"));
return EINVAL;
}
gid_count = hash_count(gid_table);
if (gid_count == 0) {
DEBUG(SSSDBG_TRACE_FUNC, ("No groups found.\n"));
ret = EOK;
goto done;
}
gids = talloc_zero_array(mem_ctx, gid_t, gid_count);
if (gids == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_array failed.\n"));
ret = ENOMEM;
goto done;
}
iter = new_hash_iter_context(gid_table);
if (iter == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("new_hash_iter_context failed.\n"));
ret = EIO;
goto done;
}
g = 0;
while ((entry = iter->next(iter)) != NULL) {
gids[g] = entry->key.ul;
g++;
}
if (gid_count != g) {
DEBUG(SSSDBG_OP_FAILURE, ("Number of hash entries and groups do not "
"match.\n"));
ret = EINVAL;
goto done;
}
ret = EOK;
done:
if (ret != EOK) {
talloc_free(gids);
} else {
dom_grps->grp_dom = grp_dom;
dom_grps->gid_count = gid_count;
dom_grps->gids = gids;
}
return ret;
}
/**
* Find all Posix GIDs from a PAC by searching for group SIDs from the local
* domain and convert them to GIDs.
*/
errno_t get_gids_from_pac(TALLOC_CTX *mem_ctx,
struct pac_ctx *pac_ctx,
struct local_mapping_ranges *range_map,
struct dom_sid *domain_sid,
struct PAC_LOGON_INFO *logon_info,
size_t *_gid_count, struct pac_dom_grps **_gids)
{
int ret;
size_t gid_count = 0;
size_t s;
struct netr_SamInfo3 *info3;
struct pac_dom_grps *gids = NULL;
struct sss_domain_info *grp_dom;
char *sid_str = NULL;
enum idmap_error_code err;
struct dom_sid *grp_sid = NULL;
uint32_t id;
hash_table_t *gid_table;
hash_key_t key;
hash_value_t value;
TALLOC_CTX *tmp_ctx = NULL;
if (pac_ctx == NULL || range_map == NULL || domain_sid == NULL ||
logon_info == NULL || _gid_count == NULL || _gids == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("Missing parameter.\n"));
return EINVAL;
}
info3 = &logon_info->info3;
if (info3->sidcount == 0 && info3->base.groups.count == 0) {
DEBUG(SSSDBG_TRACE_ALL, ("No extra groups found.\n"));
ret = EOK;
goto done;
}
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_new failed.\n"));
ret = ENOMEM;
goto done;
}
/* Currently three group containers are allocated, one for the IPA domain, one
* for the trusted AD domain and an empty one to indicate the end of the
* list. */
gids = talloc_zero_array(tmp_ctx, struct pac_dom_grps, 3);
if (gids == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_zero_array failed.\n"));
ret = ENOMEM;
goto done;
}
ret = sss_hash_create(tmp_ctx, info3->sidcount, &gid_table);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_hash_create failed.\n"));
goto done;
}
key.type = HASH_KEY_ULONG;
value.type = HASH_VALUE_PTR;
err = sss_idmap_smb_sid_to_sid(pac_ctx->idmap_ctx, domain_sid,
&sid_str);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_idmap_smb_sid_to_sid failed.\n"));
ret = EFAULT;
goto done;
}
grp_dom = find_domain_by_id(pac_ctx->rctx->domains, sid_str);
if (grp_dom == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("find_domain_by_id failed.\n"));
ret = EINVAL;
goto done;
}
for(s = 0; s < info3->sidcount; s++) {
if (dom_sid_in_domain(domain_sid, info3->sids[s].sid)) {
ret = local_sid_to_id(range_map, info3->sids[s].sid,
&id);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("get_rid failed.\n"));
goto done;
}
key.ul = id;
value.ptr = grp_dom;
ret = hash_enter(gid_table, &key, &value);
if (ret != HASH_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("hash_enter failed [%d][%s].\n",
ret, hash_error_string(ret)));
ret = EIO;
goto done;
}
DEBUG(SSSDBG_TRACE_ALL, ("Found extra group with gid [%d].\n", id));
}
}
ret = get_dom_grps_from_hash(gids, gid_table, grp_dom, &gids[0]);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("get_dom_grps_from_hash failed.\n"));
goto done;
}
gid_count += gids[0].gid_count;
talloc_free(gid_table);
ret = sss_hash_create(tmp_ctx, info3->base.groups.count, &gid_table);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_hash_create failed.\n"));
goto done;
}
talloc_zfree(sid_str);
err = sss_idmap_smb_sid_to_sid(pac_ctx->idmap_ctx, info3->base.domain_sid,
&sid_str);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_idmap_smb_sid_to_sid failed.\n"));
ret = EFAULT;
goto done;
}
grp_dom = find_domain_by_id(pac_ctx->rctx->domains, sid_str);
if (grp_dom == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("find_domain_by_id failed.\n"));
ret = EINVAL;
goto done;
}
err = sss_idmap_sid_to_smb_sid(pac_ctx->idmap_ctx, sid_str, &grp_sid);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_idmap_sid_to_smb_sid failed.\n"));
ret = EFAULT;
goto done;
}
grp_sid->num_auths++;
for (s = 0; s < info3->base.groups.count; s++) {
grp_sid->sub_auths[grp_sid->num_auths - 1] =
info3->base.groups.rids[s].rid;
err = sss_idmap_smb_sid_to_unix(pac_ctx->idmap_ctx, grp_sid,
&id);
if (err != IDMAP_SUCCESS) {
DEBUG(SSSDBG_FATAL_FAILURE, ("sss_idmap_smb_sid_to_unix failed for"
"[%s] [%d].\n", sid_str,
info3->base.groups.rids[s].rid));
ret = ENOENT;
goto done;
}
key.ul = id;
value.ptr = grp_dom;
ret = hash_enter(gid_table, &key, &value);
if (ret != HASH_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("hash_enter failed [%d][%s].\n",
ret, hash_error_string(ret)));
ret = EIO;
goto done;
}
DEBUG(SSSDBG_TRACE_ALL, ("Found extra group with gid [%d].\n", id));
}
ret = get_dom_grps_from_hash(gids, gid_table, grp_dom, &gids[1]);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("get_dom_grps_from_hash failed.\n"));
goto done;
}
gid_count += gids[1].gid_count;
ret = EOK;
done:
talloc_free(sid_str);
talloc_free(grp_sid);
if (ret == EOK) {
*_gid_count = gid_count;
*_gids = talloc_steal(mem_ctx, gids);
}
talloc_free(tmp_ctx);
return ret;
}
/**
* Extract the PAC logon data from an NDR blob.
*/
errno_t get_data_from_pac(TALLOC_CTX *mem_ctx,
uint8_t *pac_blob, size_t pac_len,
struct PAC_LOGON_INFO **_logon_info)
{
DATA_BLOB blob;
struct ndr_pull *ndr_pull;
struct PAC_DATA *pac_data;
enum ndr_err_code ndr_err;
size_t c;
int ret;
blob.data = pac_blob;
blob.length = pac_len;
ndr_pull = ndr_pull_init_blob(&blob, mem_ctx);
if (ndr_pull == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("ndr_pull_init_blob failed.\n"));
return ENOMEM;
}
ndr_pull->flags |= LIBNDR_FLAG_REF_ALLOC; /* FIXME: is this really needed ? */
pac_data = talloc_zero(mem_ctx, struct PAC_DATA);
if (pac_data == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_zero failed.\n"));
return ENOMEM;
}
ndr_err = ndr_pull_PAC_DATA(ndr_pull, NDR_SCALARS|NDR_BUFFERS, pac_data);
if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
DEBUG(SSSDBG_OP_FAILURE, ("ndr_pull_PAC_DATA failed [%d]\n", ndr_err));
return EBADMSG;
}
for(c = 0; c < pac_data->num_buffers; c++) {
if (pac_data->buffers[c].type == PAC_TYPE_LOGON_INFO) {
*_logon_info = pac_data->buffers[c].info->logon_info.info;
return EOK;
}
}
ret = EINVAL;
talloc_free(pac_data);
return ret;
}
/**
* Fill up the passwd struct with data from the PAC logon info
*/
errno_t get_pwd_from_pac(TALLOC_CTX *mem_ctx,
struct pac_ctx *pac_ctx,
struct sss_domain_info *dom,
struct PAC_LOGON_INFO *logon_info,
struct passwd **_pwd,
struct sysdb_attrs **_attrs)
{
struct passwd *pwd = NULL;
struct sysdb_attrs *attrs = NULL;
struct netr_SamBaseInfo *base_info;
int ret;
char *lname;
char *uc_realm;
char *upn;
pwd = talloc_zero(mem_ctx, struct passwd);
if (pwd == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_zero failed.\n"));
return ENOMEM;
}
base_info = &logon_info->info3.base;
if (base_info->account_name.size == 0) {
DEBUG(SSSDBG_OP_FAILURE, ("Missing account name in PAC.\n"));
ret = EINVAL;
goto done;
}
if (base_info->rid == 0) {
DEBUG(SSSDBG_OP_FAILURE, ("Missing user RID in PAC.\n"));
ret = EINVAL;
goto done;
}
/* To be compatible with winbind based lookups we have to use lower
* case names only, effectively making the domain case-insenvitive. */
lname = sss_tc_utf8_str_tolower(pwd, base_info->account_name.string);
if (lname == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_tc_utf8_str_tolower failed.\n"));
ret = ENOMEM;
goto done;
}
pwd->pw_name = talloc_asprintf(pwd, dom->names->fq_fmt,
lname, dom->name);
if (!pwd->pw_name) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_sprintf failed.\n"));
ret = ENOMEM;
goto done;
}
ret = domsid_rid_to_uid(pac_ctx, dom->sysdb, dom->name,
base_info->domain_sid,
base_info->rid, &pwd->pw_uid);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("domsid_rid_to_uid failed.\n"));
goto done;
}
pwd->pw_gid = 0; /* We use MPGs for sub-domains */
if (base_info->full_name.size != 0) {
pwd->pw_gecos = talloc_strdup(pwd, base_info->full_name.string);
if (pwd->pw_gecos == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_strdup failed.\n"));
ret = ENOMEM;
goto done;
}
} else {
DEBUG(SSSDBG_OP_FAILURE, ("Missing full name in PAC, "
"gecos field will by empty.\n "));
}
if (dom->subdomain_homedir) {
pwd->pw_dir = expand_homedir_template(pwd, dom->subdomain_homedir,
lname, pwd->pw_uid, NULL,
dom->name, dom->flat_name);
if (pwd->pw_dir == NULL) {
ret = ENOMEM;
goto done;
}
}
pwd->pw_shell = NULL; /* Using default */
attrs = sysdb_new_attrs(mem_ctx);
if (attrs == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("sysdb_new_attrs failed.\n"));
ret = ENOMEM;
goto done;
}
uc_realm = get_uppercase_realm(mem_ctx, dom->name);
if (uc_realm == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("get_uppercase_realm failed.\n"));
ret = ENOMEM;
goto done;
}
upn = talloc_asprintf(mem_ctx, "%s@%s", lname, uc_realm);
talloc_free(uc_realm);
if (upn == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_asprintf failed.\n"));
ret = ENOMEM;
goto done;
}
ret = sysdb_attrs_add_string(attrs, SYSDB_UPN, upn);
talloc_free(upn);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_add_string failed.\n"));
goto done;
}
ret = sysdb_attrs_add_string(attrs, SYSDB_NAME_ALIAS, pwd->pw_name);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("sysdb_attrs_add_string failed.\n"));
goto done;
}
*_pwd = pwd;
*_attrs = attrs;
ret = EOK;
done:
if (ret != EOK) {
talloc_free(pwd);
}
return ret;
}
errno_t diff_gid_lists(TALLOC_CTX *mem_ctx,
size_t cur_grp_num,
struct grp_info *cur_grp_list,
size_t new_gid_num,
struct pac_dom_grps *new_gid_list,
size_t *_add_gid_num,
struct pac_dom_grps **_add_gid_list,
size_t *_del_grp_num,
struct grp_info ***_del_grp_list)
{
int ret;
size_t c;
size_t g;
hash_table_t *table;
hash_key_t key;
hash_value_t value;
size_t add_gid_num = 0;
struct pac_dom_grps *add_gid_list = NULL;
size_t del_grp_num = 0;
struct grp_info **del_grp_list = NULL;
TALLOC_CTX *tmp_ctx = NULL;
unsigned long value_count;
hash_value_t *values;
size_t new_dom_num = 0;
if ((cur_grp_num != 0 && cur_grp_list == NULL) ||
(new_gid_num != 0 && new_gid_list == NULL)) {
DEBUG(SSSDBG_OP_FAILURE, ("Missing group array.\n"));
return EINVAL;
}
if (cur_grp_num == 0 && new_gid_num == 0) {
ret = EOK;
goto done;
}
tmp_ctx = talloc_new(NULL);
if (tmp_ctx == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_new failed.\n"));
ret = ENOMEM;
goto done;
}
if (new_gid_num != 0) {
for (new_dom_num = 0; new_gid_list[new_dom_num].grp_dom != NULL;
new_dom_num++);
}
if (cur_grp_num == 0 && new_gid_num != 0) {
add_gid_num = new_gid_num;
add_gid_list = talloc_zero_array(tmp_ctx, struct pac_dom_grps,
new_dom_num + 1);
if (add_gid_list == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_array failed.\n"));
ret = ENOMEM;
goto done;
}
for (c = 0; c < new_dom_num; c++) {
add_gid_list[c].grp_dom = new_gid_list[c].grp_dom;
add_gid_list[c].gid_count = new_gid_list[c].gid_count;
if (new_gid_list[c].gid_count != 0) {
add_gid_list[c].gids = talloc_zero_array(add_gid_list, gid_t,
new_gid_list[c].gid_count);
if (add_gid_list[c].gids == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_zero_array failed.\n"));
ret = ENOMEM;
goto done;
}
for (g = 0; g < new_gid_list[c].gid_count; g++) {
add_gid_list[c].gids[g] = new_gid_list[c].gids[g];
}
}
}
ret = EOK;
goto done;
}
if (cur_grp_num != 0 && new_gid_num == 0) {
del_grp_num = cur_grp_num;
del_grp_list = talloc_array(tmp_ctx, struct grp_info *, del_grp_num);
if (del_grp_list == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_array failed.\n"));
ret = ENOMEM;
goto done;
}
for (c = 0; c < del_grp_num; c++) {
del_grp_list[c] = &cur_grp_list[c];
}
ret = EOK;
goto done;
}
/* Add all current GIDs to a hash and then compare with the new ones in a
* single loop */
ret = sss_hash_create(tmp_ctx, cur_grp_num, &table);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("sss_hash_create failed.\n"));
goto done;
}
key.type = HASH_KEY_ULONG;
value.type = HASH_VALUE_PTR;
for (c = 0; c < cur_grp_num; c++) {
key.ul = (unsigned long) cur_grp_list[c].gid;
value.ptr = &cur_grp_list[c];
ret = hash_enter(table, &key, &value);
if (ret != HASH_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("hash_enter failed.\n"));
ret = EIO;
goto done;
}
}
add_gid_list = talloc_zero_array(tmp_ctx, struct pac_dom_grps,
new_dom_num + 1);
if (add_gid_list == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_array failed.\n"));
ret = ENOMEM;
goto done;
}
for (c = 0; c < new_dom_num; c++) {
add_gid_list[c].grp_dom = new_gid_list[c].grp_dom;
add_gid_list[c].gid_count = 0;
for (g = 0; g < new_gid_list[c].gid_count; g++) {
key.ul = (unsigned long) new_gid_list[c].gids[g];
ret = hash_delete(table, &key);
if (ret == HASH_ERROR_KEY_NOT_FOUND) {
/* gid not found, must be added */
add_gid_list[c].gid_count++;
add_gid_list[c].gids = talloc_realloc(add_gid_list,
add_gid_list,
gid_t,
add_gid_list[c].gid_count);
if (add_gid_list[c].gids == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_realloc failed.\n"));
ret = ENOMEM;
goto done;
}
add_gid_list[c].gids[add_gid_list[c].gid_count - 1] =
new_gid_list[c].gids[g];
} else if (ret != HASH_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("hash_delete failed.\n"));
ret = EIO;
goto done;
}
}
add_gid_num += add_gid_list[c].gid_count;
}
/* the remaining entries in the hash are not in the new list anymore and
* must be deleted */
ret = hash_values(table, &value_count, &values);
if (ret != HASH_SUCCESS) {
DEBUG(SSSDBG_OP_FAILURE, ("hash_keys failed.\n"));
ret = EIO;
goto done;
}
del_grp_num = value_count;
del_grp_list = talloc_array(tmp_ctx, struct grp_info *, del_grp_num);
if (del_grp_list == NULL) {
DEBUG(SSSDBG_OP_FAILURE, ("talloc_array failed.\n"));
ret = ENOMEM;
goto done;
}
for (c = 0; c < del_grp_num; c++) {
del_grp_list[c] = (struct grp_info *) values[c].ptr;
}
ret = EOK;
done:
if (ret == EOK) {
*_add_gid_num = add_gid_num;
*_add_gid_list = talloc_steal(mem_ctx, add_gid_list);
*_del_grp_num = del_grp_num;
*_del_grp_list = talloc_steal(mem_ctx, del_grp_list);
}
talloc_free(tmp_ctx);
return ret;
}
static bool compare_string_with_attr(const char *val, struct ldb_message *msg,
const char *attr)
{
const char *str;
str = ldb_msg_find_attr_as_string(msg, attr, NULL);
if ((str == NULL && val == NULL) ||
(str != NULL && val != NULL && strcmp(str, val) == 0)) {
return true;
}
return false;
}
bool new_and_cached_user_differs(struct passwd *pwd, struct ldb_message *msg)
{
if (pwd == NULL || msg == NULL) {
return true;
}
if (!compare_string_with_attr(pwd->pw_name, msg, SYSDB_NAME)) {
DEBUG(SSSDBG_TRACE_FUNC, ("Names differ."));
return true;
}
if (!compare_string_with_attr(pwd->pw_gecos, msg, SYSDB_GECOS)) {
DEBUG(SSSDBG_TRACE_FUNC, ("Gecos fields differ."));
return true;
}
if (!compare_string_with_attr(pwd->pw_dir, msg, SYSDB_HOMEDIR)) {
DEBUG(SSSDBG_TRACE_FUNC, ("Home directories differ."));
return true;
}
if (!compare_string_with_attr(pwd->pw_shell, msg, SYSDB_SHELL)) {
DEBUG(SSSDBG_TRACE_FUNC, ("Shells differ."));
return true;
}
return false;
}
|