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
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
|
/*
Unix SMB/CIFS implementation.
ID Mapping
Copyright (C) Tim Potter 2000
Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
Copyright (C) Simo Sorce 2003-2007
Copyright (C) Jeremy Allison 2006
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 "includes.h"
#include "winbindd.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_IDMAP
static_decl_idmap;
struct idmap_backend {
const char *name;
struct idmap_methods *methods;
struct idmap_backend *prev, *next;
};
struct idmap_alloc_backend {
const char *name;
struct idmap_alloc_methods *methods;
struct idmap_alloc_backend *prev, *next;
};
struct idmap_alloc_context {
const char *params;
struct idmap_alloc_methods *methods;
bool initialized;
};
static TALLOC_CTX *idmap_ctx = NULL;
static struct idmap_backend *backends = NULL;
static struct idmap_domain **idmap_domains = NULL;
static int num_domains = 0;
static int pdb_dom_num = -1;
static int def_dom_num = -1;
static struct idmap_alloc_backend *alloc_backends = NULL;
static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
#define IDMAP_CHECK_RET(ret) do { \
if ( ! NT_STATUS_IS_OK(ret)) { \
DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
goto done; \
} } while(0)
#define IDMAP_REPORT_RET(ret) do { \
if ( ! NT_STATUS_IS_OK(ret)) { \
DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
} } while(0)
#define IDMAP_CHECK_ALLOC(mem) do { \
if (!mem) { \
DEBUG(0, ("Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; \
goto done; \
} } while(0)
static struct idmap_methods *get_methods(struct idmap_backend *be,
const char *name)
{
struct idmap_backend *b;
for (b = be; b; b = b->next) {
if (strequal(b->name, name)) {
return b->methods;
}
}
return NULL;
}
static struct idmap_alloc_methods *get_alloc_methods(
struct idmap_alloc_backend *be,
const char *name)
{
struct idmap_alloc_backend *b;
for (b = be; b; b = b->next) {
if (strequal(b->name, name)) {
return b->methods;
}
}
return NULL;
}
bool idmap_is_offline(void)
{
return ( lp_winbind_offline_logon() &&
get_global_winbindd_state_offline() );
}
/**********************************************************************
Allow a module to register itself as a method.
**********************************************************************/
NTSTATUS smb_register_idmap(int version, const char *name,
struct idmap_methods *methods)
{
struct idmap_methods *test;
struct idmap_backend *entry;
if (!idmap_ctx) {
return NT_STATUS_INTERNAL_DB_ERROR;
}
if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
DEBUG(0, ("Failed to register idmap module.\n"
"The module was compiled against "
"SMB_IDMAP_INTERFACE_VERSION %d,\n"
"current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
"Please recompile against the current version "
"of samba!\n",
version, SMB_IDMAP_INTERFACE_VERSION));
return NT_STATUS_OBJECT_TYPE_MISMATCH;
}
if (!name || !name[0] || !methods) {
DEBUG(0,("Called with NULL pointer or empty name!\n"));
return NT_STATUS_INVALID_PARAMETER;
}
test = get_methods(backends, name);
if (test) {
DEBUG(0,("Idmap module %s already registered!\n", name));
return NT_STATUS_OBJECT_NAME_COLLISION;
}
entry = talloc(idmap_ctx, struct idmap_backend);
if ( ! entry) {
DEBUG(0,("Out of memory!\n"));
return NT_STATUS_NO_MEMORY;
}
entry->name = talloc_strdup(idmap_ctx, name);
if ( ! entry->name) {
DEBUG(0,("Out of memory!\n"));
TALLOC_FREE(entry);
return NT_STATUS_NO_MEMORY;
}
entry->methods = methods;
DLIST_ADD(backends, entry);
DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
return NT_STATUS_OK;
}
/**********************************************************************
Allow a module to register itself as a method.
**********************************************************************/
NTSTATUS smb_register_idmap_alloc(int version, const char *name,
struct idmap_alloc_methods *methods)
{
struct idmap_alloc_methods *test;
struct idmap_alloc_backend *entry;
if (!idmap_ctx) {
return NT_STATUS_INTERNAL_DB_ERROR;
}
if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
DEBUG(0, ("Failed to register idmap alloc module.\n"
"The module was compiled against "
"SMB_IDMAP_INTERFACE_VERSION %d,\n"
"current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
"Please recompile against the current version "
"of samba!\n",
version, SMB_IDMAP_INTERFACE_VERSION));
return NT_STATUS_OBJECT_TYPE_MISMATCH;
}
if (!name || !name[0] || !methods) {
DEBUG(0,("Called with NULL pointer or empty name!\n"));
return NT_STATUS_INVALID_PARAMETER;
}
test = get_alloc_methods(alloc_backends, name);
if (test) {
DEBUG(0,("idmap_alloc module %s already registered!\n", name));
return NT_STATUS_OBJECT_NAME_COLLISION;
}
entry = talloc(idmap_ctx, struct idmap_alloc_backend);
if ( ! entry) {
DEBUG(0,("Out of memory!\n"));
return NT_STATUS_NO_MEMORY;
}
entry->name = talloc_strdup(idmap_ctx, name);
if ( ! entry->name) {
DEBUG(0,("Out of memory!\n"));
TALLOC_FREE(entry);
return NT_STATUS_NO_MEMORY;
}
entry->methods = methods;
DLIST_ADD(alloc_backends, entry);
DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
return NT_STATUS_OK;
}
static int close_domain_destructor(struct idmap_domain *dom)
{
NTSTATUS ret;
ret = dom->methods->close_fn(dom);
if (!NT_STATUS_IS_OK(ret)) {
DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
}
return 0;
}
/**************************************************************************
Shutdown.
**************************************************************************/
NTSTATUS idmap_close(void)
{
/* close the alloc backend first before freeing idmap_ctx */
if (idmap_alloc_ctx) {
idmap_alloc_ctx->methods->close_fn();
idmap_alloc_ctx->methods = NULL;
}
alloc_backends = NULL;
/* this talloc_free call will fire the talloc destructors
* that will free all active backends resources */
TALLOC_FREE(idmap_ctx);
idmap_domains = NULL;
backends = NULL;
return NT_STATUS_OK;
}
/****************************************************************************
****************************************************************************/
NTSTATUS idmap_init_cache(void)
{
/* Always initialize the cache. We'll have to delay initialization
of backends if we are offline */
if ( idmap_ctx ) {
return NT_STATUS_OK;
}
if ( (idmap_ctx = talloc_named_const(NULL, 0, "idmap_ctx")) == NULL ) {
return NT_STATUS_NO_MEMORY;
}
return NT_STATUS_OK;
}
/****************************************************************************
****************************************************************************/
static NTSTATUS idmap_init(void)
{
NTSTATUS ret;
static NTSTATUS idmap_init_status = NT_STATUS_UNSUCCESSFUL;
struct idmap_domain *dom;
char *compat_backend = NULL;
char *compat_params = NULL;
const char **dom_list = NULL;
const char *default_domain = NULL;
char *alloc_backend = NULL;
bool default_already_defined = False;
bool pri_dom_is_in_list = False;
int compat = 0;
int i;
ret = idmap_init_cache();
if (!NT_STATUS_IS_OK(ret))
return ret;
if (NT_STATUS_IS_OK(idmap_init_status)) {
return NT_STATUS_OK;
}
/* We can't reliably call intialization code here unless
we are online. But return NT_STATUS_OK so the upper
level code doesn't abort idmap lookups. */
if ( get_global_winbindd_state_offline() ) {
idmap_init_status = NT_STATUS_FILE_IS_OFFLINE;
return NT_STATUS_OK;
}
static_init_idmap;
dom_list = lp_idmap_domains();
if ( lp_idmap_backend() ) {
const char **compat_list = lp_idmap_backend();
char *p = NULL;
const char *q = NULL;
if ( dom_list ) {
DEBUG(0, ("WARNING: idmap backend and idmap domains are"
" mutually exclusive!\n"));
DEBUGADD(0,("idmap backend option will be IGNORED!\n"));
} else {
compat = 1;
compat_backend = talloc_strdup(idmap_ctx, *compat_list);
/* strip any leading idmap_ prefix of */
if (strncmp(*compat_list, "idmap_", 6) == 0 ) {
q = *compat_list += 6;
DEBUG(0, ("WARNING: idmap backend uses obsolete"
" and deprecated 'idmap_' prefix.\n"
"Please replace 'idmap_%s' by '%s' in"
" %s\n", q, q, get_dyn_CONFIGFILE()));
compat_backend = talloc_strdup(idmap_ctx, q);
} else {
compat_backend = talloc_strdup(idmap_ctx,
*compat_list);
}
if (compat_backend == NULL ) {
ret = NT_STATUS_NO_MEMORY;
goto done;
}
/* separate the backend and module arguments */
if ((p = strchr(compat_backend, ':')) != NULL) {
*p = '\0';
compat_params = p + 1;
}
}
} else if ( !dom_list ) {
/* Back compatible: without idmap domains and explicit
idmap backend. Taking default idmap backend: tdb */
compat = 1;
compat_backend = talloc_strdup( idmap_ctx, "tdb");
compat_params = compat_backend;
}
if ( ! dom_list) {
/* generate a list with our main domain */
const char ** dl;
dl = talloc_array(idmap_ctx, const char *, 2);
if (dl == NULL) {
ret = NT_STATUS_NO_MEMORY;
goto done;
}
dl[0] = talloc_strdup(dl, lp_workgroup());
if (dl[0] == NULL) {
ret = NT_STATUS_NO_MEMORY;
goto done;
}
/* terminate */
dl[1] = NULL;
dom_list = dl;
default_domain = dl[0];
}
/***************************
* initialize idmap domains
*/
DEBUG(1, ("Initializing idmap domains\n"));
for (i=0, num_domains=0; dom_list[i]; i++) {
const char *parm_backend;
char *config_option;
/* ignore BUILTIN and local MACHINE domains */
if (strequal(dom_list[i], "BUILTIN")
|| strequal(dom_list[i], get_global_sam_name()))
{
DEBUG(0,("idmap_init: Ignoring domain %s\n",
dom_list[i]));
continue;
}
if ((dom_list[i] != default_domain) &&
strequal(dom_list[i], lp_workgroup())) {
pri_dom_is_in_list = True;
}
/* init domain */
dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
IDMAP_CHECK_ALLOC(dom);
dom->name = talloc_strdup(dom, dom_list[i]);
IDMAP_CHECK_ALLOC(dom->name);
config_option = talloc_asprintf(dom, "idmap config %s",
dom->name);
IDMAP_CHECK_ALLOC(config_option);
/* default or specific ? */
dom->default_domain = lp_parm_bool(-1, config_option,
"default", False);
if (dom->default_domain ||
(default_domain && strequal(dom_list[i], default_domain))) {
/* make sure this is set even when we match
* default_domain */
dom->default_domain = True;
if (default_already_defined) {
DEBUG(1, ("ERROR: Multiple domains defined as"
" default!\n"));
ret = NT_STATUS_INVALID_PARAMETER;
goto done;
}
default_already_defined = True;
}
dom->readonly = lp_parm_bool(-1, config_option,
"readonly", False);
/* find associated backend (default: tdb) */
if (compat) {
parm_backend = talloc_strdup(idmap_ctx, compat_backend);
} else {
parm_backend = talloc_strdup(idmap_ctx,
lp_parm_const_string(
-1, config_option,
"backend", "tdb"));
}
IDMAP_CHECK_ALLOC(parm_backend);
/* get the backend methods for this domain */
dom->methods = get_methods(backends, parm_backend);
if ( ! dom->methods) {
ret = smb_probe_module("idmap", parm_backend);
if (NT_STATUS_IS_OK(ret)) {
dom->methods = get_methods(backends,
parm_backend);
}
}
if ( ! dom->methods) {
DEBUG(0, ("ERROR: Could not get methods for "
"backend %s\n", parm_backend));
ret = NT_STATUS_UNSUCCESSFUL;
goto done;
}
/* check the set_mapping function exists otherwise mark the
* module as readonly */
if ( ! dom->methods->set_mapping) {
DEBUG(5, ("Forcing to readonly, as this module can't"
" store arbitrary mappings.\n"));
dom->readonly = True;
}
/* now that we have methods,
* set the destructor for this domain */
talloc_set_destructor(dom, close_domain_destructor);
if (compat_params) {
dom->params = talloc_strdup(dom, compat_params);
IDMAP_CHECK_ALLOC(dom->params);
} else {
dom->params = NULL;
}
/* Finally instance a backend copy for this domain */
ret = dom->methods->init(dom);
if ( ! NT_STATUS_IS_OK(ret)) {
DEBUG(0, ("ERROR: Initialization failed for backend "
"%s (domain %s), deferred!\n",
parm_backend, dom->name));
}
idmap_domains = talloc_realloc(idmap_ctx, idmap_domains,
struct idmap_domain *, i+1);
if ( ! idmap_domains) {
DEBUG(0, ("Out of memory!\n"));
ret = NT_STATUS_NO_MEMORY;
goto done;
}
idmap_domains[num_domains] = dom;
/* save default domain position for future uses */
if (dom->default_domain) {
def_dom_num = num_domains;
}
/* Bump counter to next available slot */
num_domains++;
DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
dom->name, parm_backend,
dom->default_domain?"":"not ",
dom->readonly?"":"not "));
talloc_free(config_option);
}
/* on DCs we need to add idmap_tdb as the default backend if compat is
* defined (when the old implicit configuration is used)
* This is not done in the previous loop a on member server we exclude
* the local domain. But on a DC the local domain is the only domain
* available therefore we are left with no default domain */
if (((lp_server_role() == ROLE_DOMAIN_PDC) ||
(lp_server_role() == ROLE_DOMAIN_BDC)) &&
((num_domains == 0) && (compat == 1))) {
dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
IDMAP_CHECK_ALLOC(dom);
dom->name = talloc_strdup(dom, "__default__");
IDMAP_CHECK_ALLOC(dom->name);
dom->default_domain = True;
dom->readonly = False;
/* get the backend methods for this domain */
dom->methods = get_methods(backends, compat_backend);
if ( ! dom->methods) {
ret = smb_probe_module("idmap", compat_backend);
if (NT_STATUS_IS_OK(ret)) {
dom->methods = get_methods(backends,
compat_backend);
}
}
if ( ! dom->methods) {
DEBUG(0, ("ERROR: Could not get methods for "
"backend %s\n", compat_backend));
ret = NT_STATUS_UNSUCCESSFUL;
goto done;
}
/* now that we have methods,
* set the destructor for this domain */
talloc_set_destructor(dom, close_domain_destructor);
dom->params = talloc_strdup(dom, compat_params);
IDMAP_CHECK_ALLOC(dom->params);
/* Finally instance a backend copy for this domain */
ret = dom->methods->init(dom);
if ( ! NT_STATUS_IS_OK(ret)) {
DEBUG(0, ("ERROR: Initialization failed for backend "
"%s (domain %s), deferred!\n",
compat_backend, dom->name));
}
idmap_domains = talloc_realloc(idmap_ctx, idmap_domains,
struct idmap_domain *, 2);
if ( ! idmap_domains) {
DEBUG(0, ("Out of memory!\n"));
ret = NT_STATUS_NO_MEMORY;
goto done;
}
idmap_domains[num_domains] = dom;
def_dom_num = num_domains;
/* Bump counter to next available slot */
num_domains++;
DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
dom->name, compat_backend,
dom->default_domain?"":"not ",
dom->readonly?"":"not "));
}
/* automatically add idmap_nss backend if needed */
if ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
( ! pri_dom_is_in_list) &&
lp_winbind_trusted_domains_only()) {
dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
IDMAP_CHECK_ALLOC(dom);
dom->name = talloc_strdup(dom, lp_workgroup());
IDMAP_CHECK_ALLOC(dom->name);
dom->default_domain = False;
dom->readonly = True;
/* get the backend methods for passdb */
dom->methods = get_methods(backends, "nss");
/* (the nss module is always statically linked) */
if ( ! dom->methods) {
DEBUG(0, ("ERROR: No methods for idmap_nss ?!\n"));
ret = NT_STATUS_UNSUCCESSFUL;
goto done;
}
/* now that we have methods,
* set the destructor for this domain */
talloc_set_destructor(dom, close_domain_destructor);
if (compat_params) {
dom->params = talloc_strdup(dom, compat_params);
IDMAP_CHECK_ALLOC(dom->params);
} else {
dom->params = NULL;
}
/* Finally instance a backend copy for this domain */
ret = dom->methods->init(dom);
if ( ! NT_STATUS_IS_OK(ret)) {
DEBUG(0, ("ERROR: Init. failed for idmap_nss ?!\n"));
ret = NT_STATUS_UNSUCCESSFUL;
goto done;
}
idmap_domains = talloc_realloc(idmap_ctx,
idmap_domains,
struct idmap_domain *,
num_domains+1);
if ( ! idmap_domains) {
DEBUG(0, ("Out of memory!\n"));
ret = NT_STATUS_NO_MEMORY;
goto done;
}
idmap_domains[num_domains] = dom;
DEBUG(10, ("Domain %s - Backend nss - not default - readonly\n",
dom->name ));
num_domains++;
}
/**** automatically add idmap_passdb backend ****/
dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
IDMAP_CHECK_ALLOC(dom);
dom->name = talloc_strdup(dom, get_global_sam_name());
IDMAP_CHECK_ALLOC(dom->name);
dom->default_domain = False;
dom->readonly = True;
/* get the backend methods for passdb */
dom->methods = get_methods(backends, "passdb");
/* (the passdb module is always statically linked) */
if ( ! dom->methods) {
DEBUG(0, ("ERROR: No methods for idmap_passdb ?!\n"));
ret = NT_STATUS_UNSUCCESSFUL;
goto done;
}
/* now that we have methods, set the destructor for this domain */
talloc_set_destructor(dom, close_domain_destructor);
if (compat_params) {
dom->params = talloc_strdup(dom, compat_params);
IDMAP_CHECK_ALLOC(dom->params);
} else {
dom->params = NULL;
}
/* Finally instance a backend copy for this domain */
ret = dom->methods->init(dom);
if ( ! NT_STATUS_IS_OK(ret)) {
DEBUG(0, ("ERROR: Init. failed for idmap_passdb ?!\n"));
ret = NT_STATUS_UNSUCCESSFUL;
goto done;
}
idmap_domains = talloc_realloc(idmap_ctx,
idmap_domains,
struct idmap_domain *,
num_domains+1);
if ( ! idmap_domains) {
DEBUG(0, ("Out of memory!\n"));
ret = NT_STATUS_NO_MEMORY;
goto done;
}
idmap_domains[num_domains] = dom;
/* needed to handle special BUILTIN and wellknown SIDs cases */
pdb_dom_num = num_domains;
DEBUG(10, ("Domain %s - Backend passdb - not default - readonly\n",
dom->name));
num_domains++;
/**** finished adding idmap_passdb backend ****/
/* sort domains so that the default is the last one */
/* don't sort if no default domain defined */
if (def_dom_num != -1 && def_dom_num != num_domains-1) {
/* default is not last, move it */
struct idmap_domain *tmp;
if (pdb_dom_num > def_dom_num) {
pdb_dom_num --;
} else if (pdb_dom_num == def_dom_num) { /* ?? */
pdb_dom_num = num_domains - 1;
}
tmp = idmap_domains[def_dom_num];
for (i = def_dom_num; i < num_domains-1; i++) {
idmap_domains[i] = idmap_domains[i+1];
}
idmap_domains[i] = tmp;
def_dom_num = i;
}
/* Initialize alloc module */
DEBUG(3, ("Initializing idmap alloc module\n"));
alloc_backend = NULL;
if (compat) {
alloc_backend = talloc_strdup(idmap_ctx, compat_backend);
} else {
char *ab = lp_idmap_alloc_backend();
if (ab && (ab[0] != '\0')) {
alloc_backend = talloc_strdup(idmap_ctx,
lp_idmap_alloc_backend());
}
}
if ( alloc_backend ) {
idmap_alloc_ctx = TALLOC_ZERO_P(idmap_ctx,
struct idmap_alloc_context);
IDMAP_CHECK_ALLOC(idmap_alloc_ctx);
idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends,
alloc_backend);
if ( ! idmap_alloc_ctx->methods) {
ret = smb_probe_module("idmap", alloc_backend);
if (NT_STATUS_IS_OK(ret)) {
idmap_alloc_ctx->methods =
get_alloc_methods(alloc_backends,
alloc_backend);
}
}
if (idmap_alloc_ctx->methods) {
if (compat_params) {
idmap_alloc_ctx->params =
talloc_strdup(idmap_alloc_ctx,
compat_params);
IDMAP_CHECK_ALLOC(idmap_alloc_ctx->params);
} else {
idmap_alloc_ctx->params = NULL;
}
ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
if ( ! NT_STATUS_IS_OK(ret)) {
DEBUG(0, ("ERROR: Initialization failed for "
"alloc backend %s, deferred!\n",
alloc_backend));
} else {
idmap_alloc_ctx->initialized = True;
}
} else {
DEBUG(2, ("idmap_init: Unable to get methods for "
"alloc backend %s\n",
alloc_backend));
/* certain compat backends are just readonly */
if ( compat ) {
TALLOC_FREE(idmap_alloc_ctx);
ret = NT_STATUS_OK;
} else {
ret = NT_STATUS_UNSUCCESSFUL;
}
}
}
/* cleanup temporary strings */
TALLOC_FREE( compat_backend );
idmap_init_status = NT_STATUS_OK;
return ret;
done:
DEBUG(0, ("Aborting IDMAP Initialization ...\n"));
idmap_close();
return ret;
}
static NTSTATUS idmap_alloc_init(void)
{
NTSTATUS ret;
if (! NT_STATUS_IS_OK(ret = idmap_init())) {
return ret;
}
if ( ! idmap_alloc_ctx) {
return NT_STATUS_NOT_SUPPORTED;
}
if ( ! idmap_alloc_ctx->initialized) {
ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
if ( ! NT_STATUS_IS_OK(ret)) {
DEBUG(0, ("ERROR: Initialization failed for alloc "
"backend, deferred!\n"));
return ret;
} else {
idmap_alloc_ctx->initialized = True;
}
}
return NT_STATUS_OK;
}
/**************************************************************************
idmap allocator interface functions
**************************************************************************/
NTSTATUS idmap_allocate_uid(struct unixid *id)
{
NTSTATUS ret;
if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
return ret;
}
id->type = ID_TYPE_UID;
return idmap_alloc_ctx->methods->allocate_id(id);
}
NTSTATUS idmap_allocate_gid(struct unixid *id)
{
NTSTATUS ret;
if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
return ret;
}
id->type = ID_TYPE_GID;
return idmap_alloc_ctx->methods->allocate_id(id);
}
NTSTATUS idmap_set_uid_hwm(struct unixid *id)
{
NTSTATUS ret;
if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
return ret;
}
id->type = ID_TYPE_UID;
return idmap_alloc_ctx->methods->set_id_hwm(id);
}
NTSTATUS idmap_set_gid_hwm(struct unixid *id)
{
NTSTATUS ret;
if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
return ret;
}
id->type = ID_TYPE_GID;
return idmap_alloc_ctx->methods->set_id_hwm(id);
}
/******************************************************************************
Lookup an idmap_domain give a full user or group SID
******************************************************************************/
static struct idmap_domain* find_idmap_domain_from_sid( DOM_SID *account_sid )
{
DOM_SID domain_sid;
uint32 rid;
struct winbindd_domain *domain = NULL;
int i;
/* 1. Handle BUILTIN or Special SIDs and prevent them from
falling into the default domain space (if we have a
configured passdb backend. */
if ( (pdb_dom_num != -1) &&
(sid_check_is_in_builtin(account_sid) ||
sid_check_is_in_wellknown_domain(account_sid) ||
sid_check_is_in_unix_groups(account_sid) ||
sid_check_is_in_unix_users(account_sid)) )
{
return idmap_domains[pdb_dom_num];
}
/* 2. Lookup the winbindd_domain from the account_sid */
sid_copy( &domain_sid, account_sid );
sid_split_rid( &domain_sid, &rid );
domain = find_domain_from_sid_noinit( &domain_sid );
for (i = 0; domain && i < num_domains; i++) {
if ( strequal( idmap_domains[i]->name, domain->name ) ) {
return idmap_domains[i];
}
}
/* 3. Fall back to the default domain */
if ( def_dom_num != -1 ) {
return idmap_domains[def_dom_num];
}
return NULL;
}
/******************************************************************************
Lookup an index given an idmap_domain pointer
******************************************************************************/
static uint32 find_idmap_domain_index( struct idmap_domain *id_domain)
{
int i;
for (i = 0; i < num_domains; i++) {
if ( idmap_domains[i] == id_domain )
return i;
}
return -1;
}
/*********************************************************
Check if creating a mapping is permitted for the domain
*********************************************************/
static NTSTATUS idmap_can_map(const struct id_map *map,
struct idmap_domain **ret_dom)
{
struct idmap_domain *dom;
/* Check we do not create mappings for our own local domain,
* or BUILTIN or special SIDs */
if ((sid_compare_domain(map->sid, get_global_sam_sid()) == 0) ||
sid_check_is_in_builtin(map->sid) ||
sid_check_is_in_wellknown_domain(map->sid) ||
sid_check_is_in_unix_users(map->sid) ||
sid_check_is_in_unix_groups(map->sid) )
{
DEBUG(10, ("We are not supposed to create mappings for our own "
"domains (local, builtin, specials)\n"));
return NT_STATUS_UNSUCCESSFUL;
}
/* Special check for trusted domain only = Yes */
if (lp_winbind_trusted_domains_only()) {
struct winbindd_domain *wdom = find_our_domain();
if (wdom && (sid_compare_domain(map->sid, &wdom->sid) == 0)) {
DEBUG(10, ("We are not supposed to create mappings for "
"our primary domain when <trusted domain "
"only> is True\n"));
DEBUGADD(10, ("Leave [%s] unmapped\n",
sid_string_dbg(map->sid)));
return NT_STATUS_UNSUCCESSFUL;
}
}
if ( (dom = find_idmap_domain_from_sid( map->sid )) == NULL ) {
/* huh, couldn't find a suitable domain,
* let's just leave it unmapped */
DEBUG(10, ("Could not find idmap backend for SID %s\n",
sid_string_dbg(map->sid)));
return NT_STATUS_NO_SUCH_DOMAIN;
}
if (dom->readonly) {
/* ouch the domain is read only,
* let's just leave it unmapped */
DEBUG(10, ("idmap backend for SID %s is READONLY!\n",
sid_string_dbg(map->sid)));
return NT_STATUS_UNSUCCESSFUL;
}
*ret_dom = dom;
return NT_STATUS_OK;
}
static NTSTATUS idmap_new_mapping(TALLOC_CTX *ctx, struct id_map *map)
{
NTSTATUS ret;
struct idmap_domain *dom;
/* If we are offline we cannot lookup SIDs, deny mapping */
if (idmap_is_offline()) {
return NT_STATUS_FILE_IS_OFFLINE;
}
ret = idmap_can_map(map, &dom);
if ( ! NT_STATUS_IS_OK(ret)) {
return NT_STATUS_NONE_MAPPED;
}
/* check if this is a valid SID and then map it */
switch (map->xid.type) {
case ID_TYPE_UID:
ret = idmap_allocate_uid(&map->xid);
if ( ! NT_STATUS_IS_OK(ret)) {
/* can't allocate id, let's just leave it unmapped */
DEBUG(2, ("uid allocation failed! "
"Can't create mapping\n"));
return NT_STATUS_NONE_MAPPED;
}
break;
case ID_TYPE_GID:
ret = idmap_allocate_gid(&map->xid);
if ( ! NT_STATUS_IS_OK(ret)) {
/* can't allocate id, let's just leave it unmapped */
DEBUG(2, ("gid allocation failed! "
"Can't create mapping\n"));
return NT_STATUS_NONE_MAPPED;
}
break;
default:
/* invalid sid, let's just leave it unmapped */
DEBUG(3,("idmap_new_mapping: Refusing to create a "
"mapping for an unspecified ID type.\n"));
return NT_STATUS_NONE_MAPPED;
}
/* ok, got a new id, let's set a mapping */
map->status = ID_MAPPED;
DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
sid_string_dbg(map->sid),
(map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
(unsigned long)map->xid.id));
ret = dom->methods->set_mapping(dom, map);
if ( ! NT_STATUS_IS_OK(ret)) {
/* something wrong here :-( */
DEBUG(2, ("Failed to commit mapping\n!"));
/* TODO: would it make sense to have an "unalloc_id function?" */
return NT_STATUS_NONE_MAPPED;
}
return NT_STATUS_OK;
}
static NTSTATUS idmap_backends_set_mapping(const struct id_map *map)
{
struct idmap_domain *dom;
NTSTATUS ret;
DEBUG(10, ("Setting mapping %s <-> %s %lu\n",
sid_string_dbg(map->sid),
(map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
(unsigned long)map->xid.id));
ret = idmap_can_map(map, &dom);
if ( ! NT_STATUS_IS_OK(ret)) {
return ret;
}
DEBUG(10,("set_mapping for domain %s\n", dom->name ));
return dom->methods->set_mapping(dom, map);
}
static NTSTATUS idmap_backends_unixids_to_sids(struct id_map **ids)
{
struct idmap_domain *dom;
struct id_map **unmapped;
struct id_map **_ids;
TALLOC_CTX *ctx;
NTSTATUS ret;
int i, u, n;
if (!ids || !*ids) {
DEBUG(1, ("Invalid list of maps\n"));
return NT_STATUS_INVALID_PARAMETER;
}
ctx = talloc_named_const(NULL, 0, "idmap_backends_unixids_to_sids ctx");
if ( ! ctx) {
DEBUG(0, ("Out of memory!\n"));
return NT_STATUS_NO_MEMORY;
}
DEBUG(10, ("Query backends to map ids->sids\n"));
/* start from the default (the last one) and then if there are still
* unmapped entries cycle through the others */
_ids = ids;
unmapped = NULL;
for (n = num_domains-1; n >= 0; n--) { /* cycle backwards */
dom = idmap_domains[n];
DEBUG(10, ("Query sids from domain %s\n", dom->name));
ret = dom->methods->unixids_to_sids(dom, _ids);
IDMAP_REPORT_RET(ret);
unmapped = NULL;
for (i = 0, u = 0; _ids[i]; i++) {
if (_ids[i]->status != ID_MAPPED) {
unmapped = talloc_realloc(ctx, unmapped,
struct id_map *, u + 2);
IDMAP_CHECK_ALLOC(unmapped);
unmapped[u] = _ids[i];
u++;
}
}
if (unmapped) {
/* terminate the unmapped list */
unmapped[u] = NULL;
} else { /* no more entries, get out */
break;
}
_ids = unmapped;
}
if (unmapped) {
/* there are still unmapped ids,
* map them to the unix users/groups domains */
/* except for expired entries,
* these will be returned as valid (offline mode) */
for (i = 0; unmapped[i]; i++) {
if (unmapped[i]->status == ID_EXPIRED) continue;
switch (unmapped[i]->xid.type) {
case ID_TYPE_UID:
uid_to_unix_users_sid(
(uid_t)unmapped[i]->xid.id,
unmapped[i]->sid);
unmapped[i]->status = ID_MAPPED;
break;
case ID_TYPE_GID:
gid_to_unix_groups_sid(
(gid_t)unmapped[i]->xid.id,
unmapped[i]->sid);
unmapped[i]->status = ID_MAPPED;
break;
default: /* what?! */
unmapped[i]->status = ID_UNKNOWN;
break;
}
}
}
ret = NT_STATUS_OK;
done:
talloc_free(ctx);
return ret;
}
static NTSTATUS idmap_backends_sids_to_unixids(struct id_map **ids, int
num_ids)
{
struct id_map ***dom_ids;
struct idmap_domain *dom;
TALLOC_CTX *ctx;
NTSTATUS ret;
int i, *counters;
if ( (ctx = talloc_named_const(NULL, 0, "be_sids_to_ids")) == NULL ) {
DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
return NT_STATUS_NO_MEMORY;
}
DEBUG(10, ("Query backends to map sids->ids\n"));
/* split list per domain */
if (num_domains == 0) {
DEBUG(1, ("No domains available?\n"));
return NT_STATUS_UNSUCCESSFUL;
}
dom_ids = TALLOC_ZERO_ARRAY(ctx, struct id_map **, num_domains);
IDMAP_CHECK_ALLOC(dom_ids);
counters = TALLOC_ZERO_ARRAY(ctx, int, num_domains);
IDMAP_CHECK_ALLOC(counters);
/* partition the requests by domain */
for (i = 0; i < num_ids; i++) {
uint32 idx;
if ((dom = find_idmap_domain_from_sid(ids[i]->sid)) == NULL) {
/* no available idmap_domain. Move on */
continue;
}
DEBUG(10,("SID %s is being handled by %s\n",
sid_string_dbg(ids[i]->sid),
dom ? dom->name : "none" ));
idx = find_idmap_domain_index( dom );
SMB_ASSERT( idx != -1 );
dom_ids[idx] = talloc_realloc(ctx, dom_ids[idx],
struct id_map *,
counters[idx] + 2);
IDMAP_CHECK_ALLOC(dom_ids[idx]);
dom_ids[idx][counters[idx]] = ids[i];
counters[idx]++;
dom_ids[idx][counters[idx]] = NULL;
}
/* All the ids have been dispatched in the right queues.
Let's cycle through the filled ones */
for (i = 0; i < num_domains; i++) {
if (dom_ids[i]) {
dom = idmap_domains[i];
DEBUG(10, ("Query ids from domain %s\n", dom->name));
ret = dom->methods->sids_to_unixids(dom, dom_ids[i]);
IDMAP_REPORT_RET(ret);
}
}
/* ok all the backends have been contacted at this point */
/* let's see if we have any unmapped SID left and act accordingly */
for (i = 0; i < num_ids; i++) {
/* NOTE: this will NOT touch ID_EXPIRED entries that the backend
* was not able to confirm/deny (offline mode) */
if (ids[i]->status == ID_UNKNOWN ||
ids[i]->status == ID_UNMAPPED) {
/* ok this is an unmapped one, see if we can map it */
ret = idmap_new_mapping(ctx, ids[i]);
if (NT_STATUS_IS_OK(ret)) {
/* successfully mapped */
ids[i]->status = ID_MAPPED;
} else
if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
/* could not map it */
ids[i]->status = ID_UNMAPPED;
} else {
/* Something very bad happened down there
* OR we are offline */
ids[i]->status = ID_UNKNOWN;
}
}
}
ret = NT_STATUS_OK;
done:
talloc_free(ctx);
return ret;
}
/**************************************************************************
idmap interface functions
**************************************************************************/
NTSTATUS idmap_unixids_to_sids(struct id_map **ids, int n_ids)
{
TALLOC_CTX *ctx;
NTSTATUS ret;
struct id_map **bids;
int i, bi;
int bn = 0;
struct winbindd_domain *our_domain = find_our_domain();
if (! NT_STATUS_IS_OK(ret = idmap_init())) {
return ret;
}
if (!ids || !*ids) {
DEBUG(1, ("Invalid list of maps\n"));
return NT_STATUS_INVALID_PARAMETER;
}
ctx = talloc_named_const(NULL, 0, "idmap_unixids_to_sids ctx");
if ( ! ctx) {
DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
return NT_STATUS_NO_MEMORY;
}
/* no ids to be asked to the backends by default */
bids = NULL;
bi = 0;
for (i = 0; i < n_ids; i++) {
bool found, mapped, expired;
if ( ! ids[i]->sid) {
DEBUG(1, ("invalid null SID in id_map array"));
talloc_free(ctx);
return NT_STATUS_INVALID_PARAMETER;
}
ids[i]->status = ID_UNKNOWN;
found = idmap_cache_map_id(&ids[i]->xid, ids[i]->sid,
&mapped, &expired);
if (found) {
ids[i]->status = mapped ? ID_MAPPED : ID_UNMAPPED;
}
if (!found || (expired && IS_DOMAIN_ONLINE(our_domain))) {
/*
* Need to ask the backend
*/
ADD_TO_ARRAY(ctx, struct id_map *, ids[i], &bids, &bn);
if (bids == NULL) {
DEBUG(1, ("Out of memory!\n"));
talloc_free(ctx);
return NT_STATUS_NO_MEMORY;
}
}
}
/* let's see if there is any id mapping to be retrieved
* from the backends */
if (bi) {
/* Only do query if we are online */
if ( IS_DOMAIN_OFFLINE(our_domain) ) {
ret = NT_STATUS_FILE_IS_OFFLINE;
goto done;
}
ret = idmap_backends_unixids_to_sids(bids);
IDMAP_CHECK_RET(ret);
/* update the cache */
for (i = 0; i < bi; i++) {
if (bids[i]->status == ID_MAPPED) {
ret = idmap_cache_set(bids[i]);
} else if (bids[i]->status == ID_EXPIRED) {
/* the cache returned an expired entry and the
* backend was not able to clear the situation
* (offline). This handles a previous
* NT_STATUS_SYNCHRONIZATION_REQUIRED
* for disconnected mode, */
bids[i]->status = ID_MAPPED;
} else if (bids[i]->status == ID_UNKNOWN) {
/* something bad here. We were not able to
* handle this for some reason, mark it as
* unmapped and hope next time things will
* settle down. */
bids[i]->status = ID_UNMAPPED;
} else { /* unmapped */
ret = idmap_cache_set_negative_id(bids[i]);
}
IDMAP_CHECK_RET(ret);
}
}
ret = NT_STATUS_OK;
done:
talloc_free(ctx);
return ret;
}
NTSTATUS idmap_sids_to_unixids(struct id_map **ids, int n_ids)
{
TALLOC_CTX *ctx;
NTSTATUS ret;
struct id_map **bids;
int i;
int bn = 0;
struct winbindd_domain *our_domain = find_our_domain();
if (! NT_STATUS_IS_OK(ret = idmap_init())) {
return ret;
}
if (!ids || !*ids) {
DEBUG(1, ("Invalid list of maps\n"));
return NT_STATUS_INVALID_PARAMETER;
}
ctx = talloc_named_const(NULL, 0, "idmap_sids_to_unixids ctx");
if ( ! ctx) {
DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
return NT_STATUS_NO_MEMORY;
}
/* no ids to be asked to the backends by default */
bids = NULL;
for (i = 0; i < n_ids; i++) {
bool found, mapped, expired;
if ( ! ids[i]->sid) {
DEBUG(1, ("invalid null SID in id_map array\n"));
talloc_free(ctx);
return NT_STATUS_INVALID_PARAMETER;
}
ids[i]->status = ID_UNKNOWN;
found = idmap_cache_map_sid(ids[i]->sid, &ids[i]->xid,
&mapped, &expired);
if (found) {
ids[i]->status = mapped ? ID_MAPPED : ID_UNMAPPED;
}
if (!found || (expired && IS_DOMAIN_ONLINE(our_domain))) {
/*
* Need to ask the backends
*/
ADD_TO_ARRAY(ctx, struct id_map *, ids[i], &bids, &bn);
if (bids == NULL) {
DEBUG(1, ("Out of memory!\n"));
talloc_free(ctx);
return NT_STATUS_NO_MEMORY;
}
}
}
/* let's see if there is any id mapping to be retrieved
* from the backends */
if (bids) {
/* Only do query if we are online */
if ( IS_DOMAIN_OFFLINE(our_domain) ) {
ret = NT_STATUS_FILE_IS_OFFLINE;
goto done;
}
ret = idmap_backends_sids_to_unixids(bids, bn);
IDMAP_CHECK_RET(ret);
/* update the cache */
for (i = 0; i < bn; i++) {
if (bids[i]->status == ID_MAPPED) {
ret = idmap_cache_set(bids[i]);
} else if (bids[i]->status == ID_EXPIRED) {
/* the cache returned an expired entry and the
* backend was not able to clear the situation
* (offline). This handles a previous
* NT_STATUS_SYNCHRONIZATION_REQUIRED
* for disconnected mode, */
bids[i]->status = ID_MAPPED;
} else if (bids[i]->status == ID_UNKNOWN) {
/* something bad here. We were not able to
* handle this for some reason, mark it as
* unmapped and hope next time things will
* settle down. */
bids[i]->status = ID_UNMAPPED;
} else { /* unmapped */
ret = idmap_cache_set_negative_sid(bids[i]);
}
IDMAP_CHECK_RET(ret);
}
}
ret = NT_STATUS_OK;
done:
talloc_free(ctx);
return ret;
}
NTSTATUS idmap_set_mapping(const struct id_map *id)
{
TALLOC_CTX *ctx;
NTSTATUS ret;
if (! NT_STATUS_IS_OK(ret = idmap_init())) {
return ret;
}
/* sanity checks */
if ((id->sid == NULL) || (id->status != ID_MAPPED)) {
DEBUG(1, ("NULL SID or unmapped entry\n"));
return NT_STATUS_INVALID_PARAMETER;
}
/* TODO: check uid/gid range ? */
ctx = talloc_named_const(NULL, 0, "idmap_set_mapping ctx");
if ( ! ctx) {
DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
return NT_STATUS_NO_MEMORY;
}
/* set the new mapping */
ret = idmap_backends_set_mapping(id);
IDMAP_CHECK_RET(ret);
/* set the mapping in the cache */
ret = idmap_cache_set(id);
IDMAP_CHECK_RET(ret);
done:
talloc_free(ctx);
return ret;
}
char *idmap_fetch_secret(const char *backend, bool alloc,
const char *domain, const char *identity)
{
char *tmp, *ret;
int r;
if (alloc) {
r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
} else {
r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
}
if (r < 0)
return NULL;
strupper_m(tmp); /* make sure the key is case insensitive */
ret = secrets_fetch_generic(tmp, identity);
SAFE_FREE(tmp);
return ret;
}
|