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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Gary Mills
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include "ldap_parse.h"
#include "nis_parse_ldap_conf.h"
#include "nis_parse_ldap_util.h"
#include "ldap_util.h"
/* Forward declarations */
int getfullmapname(char **, const char *);
int checkfullmapname(const char *, const char *, __nis_table_mapping_t **,
__nis_table_mapping_t **);
int append_domainContext(__nis_table_mapping_t **, char *, char *);
static int merge_table_mapping(__nis_table_mapping_t *in,
__nis_table_mapping_t *out);
__nis_table_mapping_t *new_merged_mapping(const char *,
__nis_table_mapping_t *intbl);
static int append_mapping_rule(__nis_mapping_rule_t *src_rule,
__nis_table_mapping_t *tbl, int flag);
static int copy_object_dn(__nis_object_dn_t *in,
__nis_object_dn_t *newdn);
/*
* FUNCTION: initialize_table_mapping
*
* Initialize the __nis_table_mapping_t structure.
*
* INPUT: __nis_table_mapping_t
*
*/
void
initialize_table_mapping(
__nis_table_mapping_t *mapping)
{
if (mapping != NULL) {
mapping->dbId = NULL;
mapping->index.numIndexes = 0;
mapping->index.name = NULL;
mapping->index.value = NULL;
mapping->numColumns = 0;
mapping->column = NULL;
mapping->initTtlLo = (time_t)NO_VALUE_SET;
mapping->initTtlHi = (time_t)NO_VALUE_SET;
mapping->ttl = (time_t)NO_VALUE_SET;
mapping->usedns_flag = 0;
mapping->securemap_flag = 0;
mapping->commentChar = DEFAULT_COMMENT_CHAR;
mapping->numSplits = 0;
mapping->objectDN = NULL;
mapping->separatorStr = DEFAULT_SEP_STRING;
mapping->numRulesFromLDAP = 0;
mapping->numRulesToLDAP = 0;
mapping->ruleFromLDAP = NULL;
mapping->ruleToLDAP = NULL;
mapping->e = NULL;
mapping->objName = NULL;
mapping->objPath = NULL;
mapping->obj = NULL;
mapping->isMaster = 0;
mapping->seq_num = NO_VALUE_SET;
}
}
/*
* FUNCTION: initialize_yp_parse_structs
*
* Initialize the __yp_domain_context_t structure.
*
* INPUT: __yp_domain_context_t
*
*/
void
initialize_yp_parse_structs(
__yp_domain_context_t *ypDomains)
{
ypDomains->numDomains = 0;
ypDomains->domainLabels = NULL;
ypDomains->domains = NULL;
ypDomains->numYppasswdd = 0;
ypDomains->yppasswddDomainLabels = NULL;
}
/*
* FUNCTION: merge_table_mapping
*
* Merges information from one table_mapping struct
* into another
*
* INPUT: Source and Destination table_mapping structs.
* RETURN: 0 on success and > 0 on error.
*/
static int
merge_table_mapping(
__nis_table_mapping_t *in,
__nis_table_mapping_t *out)
{
int i;
int orig_num_rules;
int append;
if (in == NULL)
return (1);
if (in->dbId == NULL)
return (1);
/*
* If 'in' is generic (non-expanded) and 'out' is domain-specific,
* then rules from 'in' should not be appended to those in 'out'.
*/
if (!strchr(in->dbId, COMMA_CHAR) && strchr(out->dbId, COMMA_CHAR))
append = 0;
else
append = 1;
if (!out->index.numIndexes && in->index.numIndexes > 0) {
if (!dup_index(&in->index, &out->index))
return (1);
}
/* add_column() increments numColumns, so we don't */
if (!out->numColumns && in->numColumns > 0) {
for (i = 0; i < in->numColumns; i++) {
if (!add_column(out, in->column[i]))
return (1);
}
}
if (out->commentChar == DEFAULT_COMMENT_CHAR &&
in->commentChar != DEFAULT_COMMENT_CHAR)
out->commentChar = in->commentChar;
if (out->usedns_flag == 0)
out->usedns_flag = in->usedns_flag;
if (out->securemap_flag == 0)
out->securemap_flag = in->securemap_flag;
if ((strcmp(out->separatorStr, DEFAULT_SEP_STRING) == 0) &&
(strcmp(in->separatorStr, DEFAULT_SEP_STRING) != 0)) {
out->separatorStr = s_strdup(in->separatorStr);
if (!out->separatorStr)
return (2);
}
if (!out->numSplits && !out->e && in->e) {
out->numSplits = in->numSplits;
out->e = (__nis_mapping_element_t *)
s_calloc(1, (in->numSplits+1) *
sizeof (__nis_mapping_element_t));
if (!out->e)
return (2);
for (i = 0; i <= in->numSplits; i++) {
if (!dup_mapping_element(&in->e[i], &out->e[i])) {
for (; i > 0; i--) {
free_mapping_element(&out->e[i - 1]);
}
out->e = NULL;
return (1);
}
}
}
if (out->initTtlLo == (time_t)NO_VALUE_SET &&
in->initTtlLo != (time_t)NO_VALUE_SET)
out->initTtlLo = in->initTtlLo;
if (out->initTtlHi == (time_t)NO_VALUE_SET &&
in->initTtlHi != (time_t)NO_VALUE_SET)
out->initTtlHi = in->initTtlHi;
if (out->ttl == (time_t)NO_VALUE_SET &&
in->ttl != (time_t)NO_VALUE_SET)
out->ttl = in->ttl;
if (!out->numRulesFromLDAP && in->numRulesFromLDAP) {
out->ruleFromLDAP = dup_mapping_rules(in->ruleFromLDAP,
in->numRulesFromLDAP);
if (!out->ruleFromLDAP)
return (1);
out->numRulesFromLDAP = in->numRulesFromLDAP;
} else if (append && out->numRulesFromLDAP && in->numRulesFromLDAP) {
orig_num_rules = out->numRulesFromLDAP;
for (i = 0; i < in->numRulesFromLDAP; i++) {
if (append_mapping_rule(in->ruleFromLDAP[i], out, 0)) {
for (i = out->numRulesFromLDAP;
i > orig_num_rules; i--) {
free_mapping_rule(out->ruleFromLDAP[i]);
out->ruleFromLDAP[i] = NULL;
}
return (1);
}
}
}
if (!out->numRulesToLDAP && in->numRulesToLDAP) {
out->ruleToLDAP = dup_mapping_rules(in->ruleToLDAP,
in->numRulesToLDAP);
if (!out->ruleToLDAP)
return (1);
out->numRulesToLDAP = in->numRulesToLDAP;
} else if (append && out->numRulesToLDAP && in->numRulesToLDAP) {
orig_num_rules = out->numRulesToLDAP;
for (i = 0; i < in->numRulesToLDAP; i++) {
if (append_mapping_rule(in->ruleToLDAP[i], out, 1)) {
for (i = out->numRulesToLDAP;
i > orig_num_rules; i--) {
free_mapping_rule(out->ruleToLDAP[i]);
out->ruleToLDAP[i] = NULL;
}
return (1);
}
}
}
if (!out->objectDN && in->objectDN) {
out->objectDN = (__nis_object_dn_t *)
s_calloc(1, sizeof (__nis_object_dn_t));
if (!out->objectDN)
return (2);
if (copy_object_dn(in->objectDN, out->objectDN)) {
free_object_dn(out->objectDN);
out->objectDN = NULL;
return (1);
}
}
if (!out->objName && in->objName) {
if (!strchr(in->objName, SPACE_CHAR)) {
/* objName has no space- a single map dbIdMapping */
out->objName = s_strndup(in->objName,
strlen(in->objName));
if (!out->objName)
return (2);
}
}
if (!out->objName && out->dbId) {
out->objName = s_strndup(out->dbId, strlen(out->dbId));
if (!out->objName)
return (2);
}
if (out->seq_num == NO_VALUE_SET && in->seq_num >= 0)
out->seq_num = in->seq_num;
return (p_error == no_parse_error ? 0 : 1);
}
/*
* FUNCTION: copy_object_dn
*
* Copies a __nis_object_dn_t structure.
*
* RETURN: 0 on success, > 0 on failure.
*
* NOTE: The caller MUST free newdn using
* free_object_dn() if return value != 0 (error condition)
*/
static int
copy_object_dn(__nis_object_dn_t *in, __nis_object_dn_t *newdn)
{
if (in == NULL) {
p_error = parse_no_object_dn;
return (1);
}
while (in != NULL) {
if (in->read.base == NULL) {
newdn->read.base = NULL;
} else {
newdn->read.base = s_strndup(
in->read.base, strlen(in->read.base));
if (newdn->read.base == NULL)
return (2);
}
newdn->read.scope = in->read.scope;
if (in->read.attrs) {
newdn->read.attrs = s_strndup(
in->read.attrs, strlen(in->read.attrs));
if (newdn->read.attrs == NULL) {
return (2);
}
} else {
newdn->read.attrs = NULL;
}
newdn->read.element = in->read.element;
if (in->write.base != NULL) {
newdn->write.base = s_strndup(
in->write.base, strlen(in->write.base));
if (newdn->write.base == NULL)
return (2);
} else {
newdn->write.base = NULL;
}
newdn->write.scope = in->write.scope;
if (in->write.attrs != NULL) {
newdn->write.attrs = s_strndup(
in->write.attrs, strlen(in->write.attrs));
if (newdn->write.attrs == NULL) {
return (2);
}
} else {
newdn->write.attrs = NULL;
}
newdn->write.element = in->write.element;
if (in->dbIdName) {
newdn->dbIdName = s_strndup(in->dbIdName,
strlen(in->dbIdName));
if (newdn->dbIdName == NULL)
return (2);
}
if (in->delDisp)
newdn->delDisp = in->delDisp;
if (in->dbId && in->numDbIds > 0) {
newdn->dbId = dup_mapping_rules(in->dbId,
in->numDbIds);
if (!newdn->dbId)
return (1);
newdn->numDbIds = in->numDbIds;
}
if (in->next != NULL) {
newdn->next = (__nis_object_dn_t *)s_calloc(1,
sizeof (__nis_object_dn_t));
if (newdn->next == NULL)
return (1);
newdn = newdn->next;
in = in->next;
} else {
return (0);
}
} /* End of while on in */
return (0);
}
/*
* FUNCTION: free_yp_domain_context
*
* Frees __yp_domain_context_t
*
* INPUT: __yp_domain_context_t
*/
void
free_yp_domain_context(__yp_domain_context_t *domains)
{
int i;
if (domains != NULL) {
for (i = 0; i < domains->numDomains; i++) {
if (domains->domains[i] != NULL) {
free(domains->domains[i]);
domains->domains[i] = NULL;
}
if (domains->domainLabels[i] != NULL) {
free(domains->domainLabels[i]);
domains->domainLabels[i] = NULL;
}
}
domains->domains = NULL;
domains->domainLabels = NULL;
for (i = 0; i < domains->numYppasswdd; i++) {
if (domains->yppasswddDomainLabels[i] != NULL) {
free(domains->yppasswddDomainLabels[i]);
domains->yppasswddDomainLabels[i] =
NULL;
}
}
domains->yppasswddDomainLabels = NULL;
domains->numDomains = 0;
domains = NULL;
}
}
/*
* FUNCTION: second_parser_pass
*
* Prepares the linked list of table_mappings for processing
* by finish_parse(), adding, merging and deleting structures
* as necessary. Also adds dummy objectDN info. for splitField's.
*
* RETURN VALUE: 0 on success, > 0 on failure.
*/
int
second_parser_pass(__nis_table_mapping_t **table_mapping)
{
__nis_table_mapping_t *t, *t2;
__nis_table_mapping_t *t_new = NULL, *tg;
__nis_table_mapping_t *prev = NULL;
char *objs, *dom;
char *objName = NULL;
char *lasts;
char *tobj, *alias, *dupalias, *tmp;
char *myself = "second_parser_pass";
int i = 0, len;
prev = NULL;
for (t = *table_mapping; t != NULL; ) {
/*
* Temporarily using this field to flag deletion.
* 0 : don't delete
* 1 : delete
* The mapping structure will be deleted in final_parser_pass
*/
t->isMaster = 0;
if (!t->dbId) {
p_error = parse_bad_map_error;
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s: no dbId field", myself);
return (1);
}
tg = NULL;
dom = strchr(t->dbId, COMMA_CHAR);
if (t->objName != NULL) {
objName = strdup(t->objName);
if (objName == NULL) {
p_error = parse_no_mem_error;
logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for objName",
myself);
return (1);
}
objs = (char *)strtok_r(objName, " ", &lasts);
/* Get the generic mapping */
if (dom != NULL) {
tg = find_table_mapping(t->dbId, dom - t->dbId,
*table_mapping);
}
} else {
objs = NULL;
if (dom == NULL) {
t->objName = s_strndup(t->dbId,
strlen(t->dbId));
if (!t->objName) {
logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for "
"t->objName", myself);
objs = NULL;
return (2);
}
} else {
/* Force relationship for domain specific */
/* Get the generic mapping */
tg = find_table_mapping(t->dbId, dom - t->dbId,
*table_mapping);
if (tg == NULL || tg->objName == NULL) {
/* If not found, use dbId for objName */
t->objName = s_strndup(t->dbId,
strlen(t->dbId));
if (t->objName == NULL) {
logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for t->objName",
myself);
return (2);
}
} else {
dom++;
tobj = s_strndup(tg->objName,
strlen(tg->objName));
if (tobj == NULL) {
logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for t->objName",
myself);
return (2);
}
alias = (char *)strtok_r(tobj, " ",
&lasts);
/* Loop 'breaks' on errors */
while (alias) {
tmp = NULL;
dupalias = s_strndup(alias,
strlen(alias));
if (!dupalias)
break;
if (getfullmapname(&dupalias,
dom)) {
i = 1;
break;
}
if (t->objName == NULL)
t->objName = dupalias;
else {
len = strlen(t->objName)
+ strlen(dupalias) +
2;
tmp = s_calloc(1, len);
if (tmp == NULL)
break;
snprintf(tmp, len,
"%s %s",
t->objName,
dupalias);
free(dupalias);
dupalias = NULL;
free(t->objName);
t->objName = tmp;
}
alias = (char *)strtok_r(NULL,
" ", &lasts);
}
if (tobj)
free(tobj);
if (alias ||
(objName = s_strdup(t->objName))
== NULL) {
if (i)
logmsg(MSG_NOTIMECHECK,
LOG_ERR,
"%s: getfullmapname failed for %s for domain \"%s\"",
myself, dupalias,
dom);
else {
p_error =
parse_no_mem_error;
logmsg(MSG_NOMEM,
LOG_ERR,
"%s: Cannot allocate memory",
myself);
}
if (dupalias)
free(dupalias);
if (t->objName)
free(t->objName);
return (2);
}
objs = (char *)strtok_r(objName, " ",
&lasts);
}
}
}
if (tg != NULL) {
if (merge_table_mapping(tg, t)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information from the %s to the %s mapping structure",
tg->dbId, t->dbId);
objs = NULL;
if (objName)
free(objName);
return (1);
}
}
/*
* If objName is "map1 map2" then do the second pass.
* If it is just "map1" however skip the expansion.
* Also skip it if t->objName is null.
*/
if (objs && strncasecmp(objs, t->objName,
strlen(t->objName))) {
t2 = find_table_mapping(objs, strlen(objs),
*table_mapping);
if (t2) {
if (merge_table_mapping(t, t2)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information from the %s to the %s mapping structure",
t->dbId, t2->dbId);
objs = NULL;
if (objName)
free(objName);
return (1);
}
t->isMaster = 1;
} else {
t_new = new_merged_mapping(objs, t);
if (t_new) {
t->isMaster = 1;
if (prev != NULL)
prev->next = t_new;
else
*table_mapping = t_new;
prev = t_new;
prev->next = t;
} else {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error creating a new mapping structure %s",
objs);
objs = NULL;
if (objName)
free(objName);
return (1);
}
}
while ((objs = (char *)strtok_r(NULL, " ", &lasts))
!= NULL) {
t2 = find_table_mapping(objs, strlen(objs),
*table_mapping);
if (t2) {
if (merge_table_mapping(t, t2)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information from the %s to the %s mapping structure",
t->dbId, t2->dbId);
objs = NULL;
if (objName)
free(objName);
return (1);
}
t->isMaster = 1;
} else {
/*
* create a new t_map with dbId = objs
* and copy t->* into new t_map
*/
t_new = new_merged_mapping(objs, t);
if (t_new) {
t->isMaster = 1;
if (prev != NULL)
prev->next = t_new;
else
*table_mapping = t_new;
prev = t_new;
prev->next = t;
} else {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error creating a new mapping structure %s",
objs);
objs = NULL;
if (objName)
free(objName);
return (1);
}
}
}
} /* if objs!= NULL */
prev = t;
t = t->next;
if (objName) {
free(objName);
objName = NULL;
objs = NULL;
}
} /* for t = table_mapping loop */
return (0);
}
__nis_table_mapping_t *
new_merged_mapping(const char *match,
__nis_table_mapping_t *intbl)
{
__nis_table_mapping_t *outtable = NULL;
outtable = (__nis_table_mapping_t *)
s_calloc(1, sizeof (__nis_table_mapping_t));
if (outtable == NULL)
return (NULL);
initialize_table_mapping(outtable);
outtable->dbId = s_strndup(match, strlen(match));
if (outtable->dbId == NULL) {
free_table_mapping(outtable);
outtable = NULL;
return (NULL);
}
if (merge_table_mapping(intbl, outtable)) {
free_table_mapping(outtable);
outtable = NULL;
}
return (outtable);
}
/*
* FUNCTION: final_parser_pass
*
* completes the final expansion of t_map structures linked list.
* all structures will have a non-null objPath as well as a objName
* in the form of "mapname . domainname ." or "splitfieldname .
* domainname .".
*
* RETURN VALUE: 0 on success, -1 on failure, -2 on fatal error.
*/
int
final_parser_pass(
__nis_table_mapping_t **table_mapping,
__yp_domain_context_t *ypDomains)
{
__nis_table_mapping_t *t;
__nis_table_mapping_t *t1, *returned_map;
__nis_table_mapping_t *prev = NULL;
int i;
char *myself = "final_parser_pass";
int nm;
bool_t r;
int del_tbl_flag = 0;
if (ypDomains) {
if (!ypDomains->numDomains) {
p_error = parse_internal_error;
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s:No domains specified.", myself);
return (-1);
}
} else {
p_error = parse_internal_error;
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s:No domain structure supplied.", myself);
return (-1);
}
prev = NULL;
for (t = *table_mapping; t != NULL; ) {
/* Delete if marked for deletion by second_parser_pass */
if (t->isMaster == 1) {
if (prev != NULL)
prev->next = t->next;
else
*table_mapping = t->next;
t1 = t;
t = t->next;
free_table_mapping(t1);
continue;
}
if (!t->objName && t->dbId) {
t->objName = s_strndup(t->dbId, strlen(t->dbId));
if (!t->objName) {
logmsg(MSG_NOMEM, LOG_ERR,
"%s:Could not allocate.", myself);
return (-1);
}
}
i = ypDomains->numDomains;
while (i > 0) {
if (i == 1) {
/* modify existing table_mapping's */
nm = checkfullmapname(t->dbId,
ypDomains->domainLabels[0],
table_mapping, &returned_map);
if (nm == 1) {
/* delete this mapping structure */
logmsg(MSG_NOTIMECHECK,
LOG_WARNING,
"Mapping structure %s,%s "
"already exists.",
t->dbId,
ypDomains->domainLabels[0]);
if (merge_table_mapping(t,
returned_map)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information "
"from the %s to the %s "
"mapping structure.",
t->dbId,
returned_map->dbId);
return (-1);
}
if (del_tbl_flag == 0)
del_tbl_flag = 1;
} else if (nm == -1) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error searching for %s,%s structure",
t->dbId,
ypDomains->domainLabels[0]);
return (-1);
} else if (nm == 0 || nm == 2) {
if ((append_domainContext(&t,
ypDomains->domainLabels[0],
ypDomains->domains[0])) != 0) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error appending domainContext %s",
ypDomains->domainLabels[0]);
return (-1);
}
del_tbl_flag = 0;
}
} else { /* if (i > 1) */
/* need to create new table_mapping's */
nm = checkfullmapname(t->dbId,
ypDomains->domainLabels[i - 1],
table_mapping, &returned_map);
if (nm == -1) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error searching for %s,%s structure",
t->dbId,
ypDomains->domainLabels[i - 1]);
return (-1);
} else if (nm == 0) {
t1 = new_merged_mapping(t->dbId, t);
/* we clone ourselves */
if (t1) {
if ((append_domainContext(&t1,
ypDomains->domainLabels[i - 1],
ypDomains->domains[i - 1])) != 0) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error appending domainContext %s",
ypDomains->domainLabels[i - 1]);
free(t1);
return (-1);
}
if (prev != NULL) {
t1->next = prev->next;
prev->next = t1;
prev = prev->next;
} else {
t1->next =
*table_mapping;
*table_mapping = t1;
prev = t1;
}
} else { /* if !t1 */
p_error = parse_internal_error;
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s:Could not create new table -"
" check all instances of %s for errors",
myself, t->dbId);
return (-1);
}
} else if (nm == 1) {
logmsg(MSG_NOTIMECHECK, LOG_WARNING,
"Mapping structure %s,%s already exists.",
t->dbId,
ypDomains->domainLabels[i - 1]);
/*
* We should be deleting this, but can't
* really do it here, because we need to
* match with the domainLabels[0] case
* too. So we will just flag it for now.
*/
if (merge_table_mapping(t,
returned_map)) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error merging information from the %s to the %s mapping structure.",
t->dbId,
returned_map->dbId);
return (-1);
}
del_tbl_flag = 1;
} else if (nm == 2) {
if ((append_domainContext(&t,
ypDomains->domainLabels[i - 1],
ypDomains->domains[i - 1])) != 0) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error appending domainContext %s",
ypDomains->domainLabels[i - 1]);
return (-1);
}
} /* end of "if (nm == 0)" */
} /* end of else if (i > 1) */
/*
* 'merge_table_mapping' only copies unexpanded
* objectDN values into returned_map. Hence,
* read.base and write.base in returned_map
* needs to be expanded.
*/
if (nm == 1 && returned_map && returned_map->objectDN) {
r = make_fqdn(
returned_map->objectDN,
ypDomains->domains[i - 1]);
if (r == TRUE &&
returned_map->objectDN->write.base) {
r = make_full_dn(
&returned_map->objectDN->write.base,
ypDomains->domains[i - 1]);
}
if (r == FALSE) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error appending domainContext "
"%s to %s",
ypDomains->domainLabels[i - 1],
returned_map->dbId);
return (-2);
}
}
i--;
} /* end of while i > 0 loop */
if (del_tbl_flag == 1) {
if (prev != NULL) {
prev->next = t->next;
free_table_mapping(t);
t = prev->next;
} else {
*table_mapping = t->next;
free_table_mapping(t);
t = *table_mapping;
}
del_tbl_flag = 0;
} else {
prev = t;
t = t->next;
}
} /* end of table mapping loop */
for (t = *table_mapping; t != NULL; t = t->next) {
if (!t->dbId) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s:Fatal error: structure with no dbId found.",
myself);
return (-2);
}
append_dot(&t->dbId);
if (!t->objectDN) {
p_error = parse_internal_error;
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s:No objectDN for %s.", myself, t->dbId);
return (-1);
}
}
return (0);
}
/*
* FUNCTION: append_mapping_rule
*
* Appends mapping rules to a table_mapping structure
* with previously existing rules. flag controls whether
* the functions works on the rules From or To LDAP.
*
* RETURN VALUE: 0 on success, >= 1 on failure.
*/
static int
append_mapping_rule(__nis_mapping_rule_t *src_rule,
__nis_table_mapping_t *dst, int flag)
{
__nis_mapping_rule_t **rules = NULL;
if (flag == 0) {
if (dst->ruleFromLDAP == NULL) {
p_error = parse_internal_error;
return (1);
}
rules = (__nis_mapping_rule_t **)
s_realloc(dst->ruleFromLDAP,
(dst->numRulesFromLDAP + 1) *
sizeof (__nis_mapping_rule_t *));
if (rules == NULL)
return (2);
dst->ruleFromLDAP = rules;
rules[dst->numRulesFromLDAP] = dup_mapping_rule(src_rule);
if (rules[dst->numRulesFromLDAP] == NULL) {
p_error = parse_no_mem_error;
return (2);
}
dst->numRulesFromLDAP++;
} else if (flag == 1) {
if (dst->ruleToLDAP == NULL) {
p_error = parse_internal_error;
return (1);
}
rules = (__nis_mapping_rule_t **)
s_realloc(dst->ruleToLDAP,
(dst->numRulesToLDAP + 1) *
sizeof (__nis_mapping_rule_t *));
if (rules == NULL)
return (2);
dst->ruleToLDAP = rules;
rules[dst->numRulesToLDAP] = dup_mapping_rule(src_rule);
if (rules[dst->numRulesToLDAP] == NULL) {
p_error = parse_no_mem_error;
return (2);
}
dst->numRulesToLDAP++;
} else
return (1);
return (0);
}
/*
* FUNCTION: check_domain_specific_order
*
* Makes sure that an attribute with explicitly specified
* nisLDAPdomainContext is found before its non-domain
* specific counterpart.
*
* RETURN VALUE: 0 normal exit
* 1 if domain specific attribute found
* after non-domain specific one.
* -1 some error condition
*/
int
check_domain_specific_order(const char *sd,
config_key attrib_num,
__nis_table_mapping_t *table_mapping,
__yp_domain_context_t *ypDomains)
{
__nis_table_mapping_t *t;
char *myself = "check_domain_specific_order";
char *type;
char *dbId = 0;
int i, len;
int match = 0;
if (ypDomains) {
if (!ypDomains->numDomains) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s:No domains specified.", myself);
return (-1);
}
} else {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"%s:No domain structure supplied.", myself);
return (-1);
}
for (i = 0; i < ypDomains->numDomains; i++) {
for (t = table_mapping; t != NULL; t = t->next) {
len = strlen(sd);
if ((strcasecmp(t->dbId, sd) == 0) && (len ==
strlen(t->dbId)))
/* prevent from matching against itself */
continue;
dbId = s_strndup(t->dbId, strlen(t->dbId));
if (dbId == NULL) {
logmsg(MSG_NOMEM, LOG_ERR,
"%s:Memory allocation error.", myself);
return (-1);
}
if (getfullmapname(&dbId,
ypDomains->domainLabels[i])) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Error getting fully qualified name for %s",
dbId);
free(dbId);
return (-1);
}
if ((strcasecmp(dbId, sd) == 0) && (len ==
strlen(dbId))) {
match = 0;
switch (attrib_num) {
case key_yp_map_flags:
if (t->usedns_flag != 0 ||
t->securemap_flag != 0)
match = 1;
type = YP_MAP_FLAGS;
break;
case key_yp_comment_char:
if (t->commentChar !=
DEFAULT_COMMENT_CHAR)
match = 1;
type = YP_COMMENT_CHAR;
break;
case key_yp_repeated_field_separators:
if (strcmp(t->separatorStr,
DEFAULT_SEP_STRING) != 0)
match = 1;
type =
YP_REPEATED_FIELD_SEPARATORS;
break;
case key_yp_name_fields:
if (t->e && t->numColumns)
match = 1;
type = YP_NAME_FIELDS;
break;
case key_yp_split_field:
if (t->e && t->numColumns)
match = 1;
type = YP_SPLIT_FIELD;
break;
case key_yp_db_id_map:
if (t->objName)
match = 1;
type = YP_DB_ID_MAP;
break;
case key_yp_entry_ttl:
if (t->initTtlLo !=
(time_t)NO_VALUE_SET)
match = 1;
type = YP_ENTRY_TTL;
break;
case key_yp_ldap_object_dn:
if (t->objectDN)
match = 1;
type = YP_LDAP_OBJECT_DN;
break;
case key_nis_to_ldap_map:
if (t->ruleToLDAP)
match = 1;
type = NIS_TO_LDAP_MAP;
break;
case key_ldap_to_nis_map:
if (t->ruleFromLDAP)
match = 1;
type = LDAP_TO_NIS_MAP;
break;
default:
type = "unknown";
match = 0;
break;
} /* end of switch */
if (match) {
logmsg(MSG_NOTIMECHECK, LOG_ERR,
"Relative attribute '%s' of type '%s' found before fully qualified one '%s'",
t->dbId, type, sd);
free(dbId);
dbId = NULL;
return (1);
}
} /* end of strncasecmp */
free(dbId);
dbId = NULL;
} /* end of t loop */
} /* end of i loop */
if (dbId)
free(dbId);
dbId = NULL;
return (0);
}
int
getfullmapname(char **mapname, const char *domainname)
{
char *maps = *mapname;
int maplen = strlen(maps);
int domainlen = strlen(domainname);
if (!maplen || !domainlen ||
maps[maplen - 1] == PERIOD_CHAR)
return (1);
else if (strchr(maps, COMMA_CHAR)) {
/* map already has a domain part, do nothing */
return (0);
} else {
append_comma(&maps);
maplen = strlen(maps);
maps = realloc(maps, (maplen + domainlen + 1));
if (maps != NULL) {
if (strlcat(maps, domainname, (maplen + domainlen + 1))
>= (maplen + domainlen + 1))
return (1);
*mapname = maps;
return (0);
} else
return (1);
}
}
/*
* FUNCTION: checkfullmapname
*
* Tries to find out if by appending the table mapping structures
* with each of the provided nisLDAPdomainContexts, an already
* existing fqdn table mapping structure results. That would be the
* case when a full qualified domain specific attribute was present.
*
* Note that per NISLDAPmapping(5) such an attribute MUST be listed
* in the mapping file BEFORE its non-fqdn counterpart.
*
* RETURNS: 0 normal exit, 1 if an existing structure found, -1 for all
* errors, 2 if already fqdn. If returning 1 the existing structure is
* in found_map.
*/
int
checkfullmapname(const char *mapname, const char *domainname,
__nis_table_mapping_t **table_mapping,
__nis_table_mapping_t **found_map)
{
char *map;
*found_map = NULL;
/* This function does not alter mapname */
if (!mapname || !domainname || *table_mapping == NULL)
return (-1);
if (strchr(mapname, COMMA_CHAR))
return (2);
if ((map = s_strndup(mapname, strlen(mapname))) == 0)
return (-1);
if (getfullmapname(&map, domainname)) {
free(map);
return (-1);
}
*found_map = find_table_mapping(map, strlen(map), *table_mapping);
if (*found_map) {
free(map);
return (1);
}
free(map);
return (0);
}
/*
* FUNCTION: append_domainContext
*
* Higher level function to append the domains to the appropriate
* fields in a table mapping structure. Calls either getfullmapname()
* or make_full_dn() to do the actual append.
*
* RETURNS: 0 on success, -1 on any error.
*/
int
append_domainContext(__nis_table_mapping_t **table_map,
char *DomainLabel, char *Domain)
{
__nis_table_mapping_t *tmp_map = *table_map;
char *lasts;
char *tmp_dbId = NULL;
char *id = NULL;
int domain_specific = 0;
char *myself = "append_domainContext";
if (!DomainLabel || !Domain || !tmp_map)
return (-1);
if (tmp_map->dbId == NULL || tmp_map->objName == NULL) {
p_error = parse_bad_map_error;
return (-1);
}
tmp_dbId = s_strndup(tmp_map->dbId, strlen(tmp_map->dbId));
if (!tmp_dbId)
return (-1);
if (strchr(tmp_map->dbId, COMMA_CHAR)) {
domain_specific = 1;
id = (char *)strtok_r(tmp_dbId, COMMA_STRING, &lasts);
if (id)
id = (char *)strtok_r(NULL, COMMA_STRING, &lasts);
else {
free(tmp_dbId);
return (-1);
}
if (!id) {
free(tmp_dbId);
return (-1);
}
if (strcasecmp(id, DomainLabel)) {
free(tmp_dbId);
return (0);
}
} else {
if (getfullmapname(&tmp_map->dbId, DomainLabel)) {
free(tmp_dbId);
return (-1);
}
append_dot(&tmp_map->dbId);
}
if (tmp_dbId)
free(tmp_dbId);
tmp_dbId = NULL;
if (getfullmapname(&tmp_map->objName, DomainLabel))
return (-1);
append_dot(&tmp_map->objName);
/*
* If domain specific mapping doesn't have objectDN,
* then don't touch. Most probably, pass for the generic mapping
* will handle this by coping over it's own objectDN
*/
if (domain_specific && tmp_map->objectDN == NULL)
return (0);
if (tmp_map->objectDN == NULL) {
/* Allocate memory to objectDN */
tmp_map->objectDN = (__nis_object_dn_t *)
s_calloc(1, sizeof (__nis_object_dn_t));
if (tmp_map->objectDN == NULL) {
logmsg(MSG_NOMEM, LOG_ERR,
"%s: Cannot allocate memory for objectDN",
myself);
return (2);
}
tmp_map->objectDN->read.base = NULL;
tmp_map->objectDN->write.base = NULL;
tmp_map->objectDN->read.attrs = NULL;
tmp_map->objectDN->write.attrs = NULL;
tmp_map->objectDN->read.scope = LDAP_SCOPE_ONELEVEL;
tmp_map->objectDN->write.scope = LDAP_SCOPE_UNKNOWN;
}
if (!make_fqdn(tmp_map->objectDN, Domain))
return (-1);
if (tmp_map->objectDN->write.base) {
if (!make_full_dn(&tmp_map->objectDN->write.base, Domain))
return (-1);
}
return (0);
}
|