forked from PADL/nss_ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap-grp.c
1429 lines (1234 loc) · 34.9 KB
/
ldap-grp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 1997-2006 Luke Howard.
This file is part of the nss_ldap library.
Contributed by Luke Howard, <[email protected]>, 1997.
The nss_ldap library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The nss_ldap library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the nss_ldap library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
static char rcsId[] =
"$Id$";
#include "config.h"
#ifdef HAVE_PORT_BEFORE_H
#include <port_before.h>
#endif
#if defined(HAVE_THREAD_H) && !defined(_AIX)
#include <thread.h>
#elif defined(HAVE_PTHREAD_H)
#include <pthread.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <grp.h>
#ifdef HAVE_LBER_H
#include <lber.h>
#endif
#ifdef HAVE_LDAP_H
#include <ldap.h>
#endif
#ifndef HAVE_SNPRINTF
#include "snprintf.h"
#endif
#include "ldap-nss.h"
#include "ldap-grp.h"
#include "util.h"
#ifdef HAVE_PORT_AFTER_H
#include <port_after.h>
#endif
#ifdef HAVE_NSS_H
static ent_context_t *gr_context = NULL;
#endif
#ifdef HAVE_USERSEC_H
typedef struct ldap_initgroups_args
{
char *grplist;
size_t listlen;
int depth;
struct name_list *known_groups;
int backlink;
}
ldap_initgroups_args_t;
#else
# ifdef HAVE_NSSWITCH_H
typedef struct ldap_initgroups_args
{
struct nss_groupsbymem *gbm;
int depth;
struct name_list *known_groups;
int backlink;
}
ldap_initgroups_args_t;
# else
typedef struct ldap_initgroups_args
{
gid_t group;
long int *start;
long int *size;
gid_t **groups;
long int limit;
int depth;
struct name_list *known_groups;
int backlink;
}
ldap_initgroups_args_t;
# endif
#endif /* HAVE_USERSEC_H */
static NSS_STATUS
ng_chase (const char *dn, ldap_initgroups_args_t * lia);
static NSS_STATUS
ng_chase_backlink (const char ** membersOf, ldap_initgroups_args_t * lia);
/*
* Range retrieval logic was reimplemented from example in
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ldap/ldap/searching_using_range_retrieval.asp
*/
static NSS_STATUS
do_parse_range (const char *attributeType,
const char *attributeDescription, int *start, int *end)
{
NSS_STATUS stat = NSS_NOTFOUND;
char *attribute;
size_t attributeTypeLength;
size_t attributeDescriptionLength;
char *p;
#ifdef HAVE_STRTOK_R
char *st = NULL;
#endif
debug ("==> do_parse_range");
*start = 0;
*end = -1;
if (strcasecmp (attributeType, attributeDescription) == 0)
{
debug ("==> do_parse_range: returns NSS_SUCCESS");
return NSS_SUCCESS;
}
attributeDescriptionLength = strlen (attributeDescription);
attributeTypeLength = strlen (attributeType);
if (attributeDescriptionLength < attributeTypeLength)
{
/* could not be a subtype */
debug ("<== do_parse_range: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
/* XXX need to copy as strtok() is destructive */
attribute = strdup (attributeDescription);
if (attribute == NULL)
{
debug ("<== do_parse_range: returns NSS_TRYAGAIN");
return NSS_TRYAGAIN;
}
#ifndef HAVE_STRTOK_R
for (p = strtok (attribute, ";"); p != NULL; p = strtok (NULL, ";"))
#else
for (p = strtok_r (attribute, ";", &st);
p != NULL; p = strtok_r (NULL, ";", &st))
#endif /* !HAVE_STRTOK_R */
{
char *q;
if (p == attribute)
{
if (strcasecmp (p, attributeType) != 0)
{
free (attribute);
debug ("<== do_parse_range: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
}
else if (strncasecmp (p, "range=", sizeof ("range=") - 1) == 0)
{
p += sizeof ("range=") - 1;
q = strchr (p, '-');
if (q == NULL)
{
free (attribute);
debug ("<== do_parse_range: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
*q++ = '\0';
*start = strtoul (p, (char **) NULL, 10);
if (strcmp (q, "*") == 0)
*end = -1;
else
*end = strtoul (q, (char **) NULL, 10);
stat = NSS_SUCCESS;
break;
}
}
free (attribute);
debug ("<== do_parse_range: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
static NSS_STATUS
do_get_range_values (LDAPMessage * e,
const char *attributeType,
int *start, int *end, char ***pGroupMembers)
{
NSS_STATUS stat = NSS_NOTFOUND;
BerElement *ber = NULL;
char *attribute;
debug ("==> do_get_range_values");
*pGroupMembers = NULL;
for (attribute = _nss_ldap_first_attribute (e, &ber);
attribute != NULL; attribute = _nss_ldap_next_attribute (e, ber))
{
stat = do_parse_range (attributeType, attribute, start, end);
if (stat == NSS_SUCCESS)
{
*pGroupMembers = _nss_ldap_get_values (e, attribute);
if (*pGroupMembers == NULL)
{
stat = NSS_NOTFOUND;
}
else if ((*pGroupMembers)[0] == NULL)
{
debug (":== do_get_range_values: call ldap_value_free");
ldap_value_free (*pGroupMembers);
*pGroupMembers = NULL;
stat = NSS_NOTFOUND;
}
}
#ifdef HAVE_LDAP_MEMFREE
debug (":== do_get_range_values: call ldap_memfree");
ldap_memfree (attribute);
#endif
if (stat == NSS_SUCCESS)
break;
}
if (ber != NULL)
{
debug (":== do_get_range_values: call ber_free");
ber_free (ber, 0);
}
debug ("<== do_get_range_values: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
/*
* Format an attribute with description as:
* attribute;range=START-END
*/
static NSS_STATUS
do_construct_range_attribute (const char *attribute,
int start,
int end,
char **buffer,
size_t * buflen,
const char **pAttributeWithRange)
{
size_t len;
char startbuf[32], endbuf[32];
debug ("==> do_construct_range_attribute");
snprintf (startbuf, sizeof (startbuf), "%u", start);
if (end != -1)
snprintf (endbuf, sizeof (endbuf), "%u", end);
else
snprintf (endbuf, sizeof (endbuf), "*");
len = strlen (attribute) + sizeof (";range=") - 1;
len += strlen (startbuf) + 1 /* - */ + strlen (endbuf);
len++; /* \0 */
if (*buflen < len)
{
debug ("<== do_construct_range_attribute: returns NSS_TRYAGAIN");
return NSS_TRYAGAIN;
}
*pAttributeWithRange = *buffer;
snprintf (*buffer, len, "%s;range=%s-%s", attribute, startbuf, endbuf);
*buffer += len;
*buflen -= len;
debug ("<== do_construct_range_attribute: returns NSS_SUCCESS");
return NSS_SUCCESS;
}
/*
* Expand group members, including nested groups
*/
static NSS_STATUS
do_parse_group_members (LDAPMessage * e,
char ***pGroupMembers,
size_t * pGroupMembersCount,
size_t * pGroupMembersNext,
size_t * pGroupMembersBufferSize,
int *pGroupMembersBufferIsMalloced,
char **buffer, size_t * buflen,
int *depth,
struct name_list **pKnownGroups,
int itemsLeft) /* traversed groups */
{
NSS_STATUS stat = NSS_SUCCESS;
char **dnValues = NULL;
char **uidValues = NULL;
char **groupMembers;
size_t groupMembersCount, i;
char **valiter;
/* support for range retrieval */
const char *uniquemember_attr;
const char *uniquemember_attrs[2];
LDAPMessage *res = NULL;
int start, end = 0;
char *groupdn = NULL;
debug ("==> do_parse_group_members");
uniquemember_attr = ATM (LM_GROUP, uniqueMember);
uniquemember_attrs[0] = uniquemember_attr;
uniquemember_attrs[1] = NULL;
if (*depth > LDAP_NSS_MAXGR_DEPTH)
{
debug ("<== do_parse_group_members: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
i = *pGroupMembersNext; /* index of next member */
groupMembers = *pGroupMembers;
groupdn = _nss_ldap_get_dn (e);
if (groupdn == NULL)
{
stat = NSS_NOTFOUND;
goto out;
}
if (_nss_ldap_namelist_find (*pKnownGroups, groupdn))
{
stat = NSS_NOTFOUND;
goto out;
}
/* store group DN for nested group loop detection */
stat = _nss_ldap_namelist_push (pKnownGroups, groupdn);
if (stat != NSS_SUCCESS)
{
goto out;
}
do
{
if (e == NULL)
{
stat = NSS_NOTFOUND;
goto out;
}
groupMembersCount = 0; /* number of members in this group */
(void) do_get_range_values (e, uniquemember_attrs[0], &start, &end, &dnValues);
if (dnValues != NULL)
{
debug (":== do_parse_group_members: call ldap_count_values on dnValues");
groupMembersCount += ldap_count_values (dnValues);
}
uidValues = _nss_ldap_get_values (e, ATM (LM_GROUP, memberUid));
if (uidValues != NULL)
{
debug (":== do_parse_group_members: call ldap_count_values on uidValues");
groupMembersCount += ldap_count_values (uidValues);
}
/*
* Check whether we need to increase the group membership buffer.
* As an optimization the buffer is preferentially allocated off
* the stack
*/
if ((*pGroupMembersCount + groupMembersCount) * sizeof (char *) >=
*pGroupMembersBufferSize)
{
*pGroupMembersBufferSize =
(*pGroupMembersCount + groupMembersCount + 1) * sizeof (char *);
*pGroupMembersBufferSize +=
(LDAP_NSS_NGROUPS * sizeof (char *)) - 1;
*pGroupMembersBufferSize -=
(*pGroupMembersBufferSize %
(LDAP_NSS_NGROUPS * sizeof (char *)));
if (*pGroupMembersBufferIsMalloced == 0)
{
groupMembers = *pGroupMembers;
*pGroupMembers = NULL; /* force malloc() */
}
*pGroupMembers =
(char **) realloc (*pGroupMembers, *pGroupMembersBufferSize);
if (*pGroupMembers == NULL)
{
*pGroupMembersBufferIsMalloced = 0; /* don't try to free */
stat = NSS_TRYAGAIN;
goto out;
}
if (*pGroupMembersBufferIsMalloced == 0)
{
memcpy (*pGroupMembers, groupMembers, i * sizeof (char *));
groupMembers = NULL; /* defensive programming */
*pGroupMembersBufferIsMalloced = 1;
}
}
groupMembers = *pGroupMembers;
*pGroupMembersCount += groupMembersCount;
/* Parse distinguished name members */
if (dnValues != NULL)
{
for (valiter = dnValues; *valiter != NULL; valiter++)
{
LDAPMessage *res;
NSS_STATUS parseStat;
int isNestedGroup = 0;
char *uid;
uid = strrchr (*valiter, '#');
if (uid != NULL)
{
*uid = '\0';
}
parseStat = _nss_ldap_dn2uid (*valiter, &groupMembers[i],
buffer, buflen, &isNestedGroup,
&res);
if (parseStat == NSS_SUCCESS)
{
if (isNestedGroup == 0)
{
/* just a normal user which we have flattened */
i++;
itemsLeft--;
continue;
}
(*depth)++;
parseStat =
do_parse_group_members (_nss_ldap_first_entry (res),
&groupMembers, pGroupMembersCount, &i,
pGroupMembersBufferSize,
pGroupMembersBufferIsMalloced,
buffer, buflen, depth,
pKnownGroups, itemsLeft);
(*depth)--;
if (parseStat == NSS_TRYAGAIN)
{
stat = NSS_TRYAGAIN;
goto out;
}
debug (":== do_parse_group_members: call ldap_msgfree");
ldap_msgfree (res);
}
else if (parseStat == NSS_TRYAGAIN)
{
stat = NSS_TRYAGAIN;
goto out;
}
}
}
/* Parse RFC 2307 (flat) members */
if (uidValues != NULL)
{
for (valiter = uidValues; *valiter != NULL; valiter++)
{
size_t len = strlen (*valiter) + 1;
if (*buflen < len)
{
stat = NSS_TRYAGAIN;
goto out;
}
groupMembers[i] = *buffer;
*buffer += len;
*buflen -= len;
memcpy (groupMembers[i++], *valiter, len);
}
}
/* Get next range for Active Directory compat */
if (end != -1)
{
stat = do_construct_range_attribute (uniquemember_attr,
end + 1,
-1,
buffer,
buflen,
&uniquemember_attrs[0]);
if (stat == NSS_SUCCESS)
{
if (dnValues != NULL)
{
debug (":== do_parse_group_members: call ldap_value_free on dnValues");
ldap_value_free (dnValues);
dnValues = NULL;
}
if (uidValues != NULL)
{
debug (":== do_parse_group_members: call ldap_value_free on uidValues");
ldap_value_free (uidValues);
uidValues = NULL;
}
if (res != NULL)
{
debug (":== do_parse_group_members: call ldap_msgfree");
ldap_msgfree (res);
res = NULL;
}
stat = _nss_ldap_read (groupdn, uniquemember_attrs, &res);
if (stat != NSS_SUCCESS)
goto out;
e = _nss_ldap_first_entry (res);
}
}
}
while (end != -1);
out:
if (dnValues != NULL)
{
debug (":== do_parse_group_members: call ldap_value_free on dnValues");
ldap_value_free (dnValues);
}
if (uidValues != NULL)
{
debug (":== do_parse_group_members: call ldap_value_free on uidValues");
ldap_value_free (uidValues);
}
if (res != NULL)
{
debug (":== do_parse_group_members: call ldap_msgfree");
ldap_msgfree (res);
}
if (groupdn != NULL)
{
#ifdef HAVE_LDAP_MEMFREE
debug (":== do_parse_group_members: call ldap_memfree");
ldap_memfree (groupdn);
#else
free (groupdn);
#endif
}
*pGroupMembers = groupMembers;
*pGroupMembersNext = i;
debug ("<== do_parse_group_members: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
/*
* "Fix" group membership list into caller provided buffer,
* and NULL terminate.
*/
static NSS_STATUS
do_fix_group_members_buffer (char **mallocedGroupMembers,
size_t groupMembersCount,
char ***pGroupMembers,
char **buffer, size_t * buflen)
{
size_t len;
debug ("==> do_fix_group_members_buffer");
len = (groupMembersCount + 1) * sizeof (char *);
if (bytesleft (*buffer, *buflen, char *) < len)
{
debug ("<== do_fix_group_members_buffer: returns NSS_TRYAGAIN");
return NSS_TRYAGAIN;
}
align (*buffer, *buflen, char *);
*pGroupMembers = (char **) *buffer;
*buffer += len;
*buflen -= len;
memcpy (*pGroupMembers, mallocedGroupMembers,
groupMembersCount * sizeof (char *));
(*pGroupMembers)[groupMembersCount] = NULL;
debug ("<== do_fix_group_members_buffer: returns NSS_SUCCESS");
return NSS_SUCCESS;
}
static NSS_STATUS
_nss_ldap_parse_gr (LDAPMessage * e,
ldap_state_t * pvt,
void *result, char *buffer, size_t buflen)
{
struct group *gr = (struct group *) result;
char *gid;
NSS_STATUS stat;
char **groupMembers;
size_t groupMembersCount, groupMembersAttrCount;
size_t groupMembersBufferSize;
char *groupMembersBuffer[LDAP_NSS_NGROUPS];
int groupMembersBufferIsMalloced;
int depth;
struct name_list *knownGroups = NULL;
debug ("==> _nss_ldap_parse_gr");
stat =
_nss_ldap_assign_attrval (e, ATM (LM_GROUP, gidNumber), &gid, &buffer,
&buflen);
if (stat != NSS_SUCCESS)
{
debug ("<== _nss_ldap_parse_gr: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
gr->gr_gid =
(*gid == '\0') ? (unsigned) GID_NOBODY : (gid_t) strtoul (gid,
(char **) NULL,
10);
stat =
_nss_ldap_assign_attrval (e, ATM (LM_GROUP, cn), &gr->gr_name, &buffer,
&buflen);
if (stat != NSS_SUCCESS)
{
debug ("<== _nss_ldap_parse_gr: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
stat =
_nss_ldap_assign_userpassword (e, ATM (LM_GROUP, userPassword),
&gr->gr_passwd, &buffer, &buflen);
if (stat != NSS_SUCCESS)
{
debug ("<== _nss_ldap_parse_gr: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
if (_nss_ldap_test_config_flag (NSS_LDAP_FLAGS_RFC2307BIS))
{
groupMembers = groupMembersBuffer;
groupMembersAttrCount = 0;
groupMembersCount = 0;
groupMembersBufferSize = sizeof (groupMembersBuffer);
groupMembersBufferIsMalloced = 0;
depth = 0;
stat = do_parse_group_members (e, &groupMembers,
&groupMembersAttrCount,
&groupMembersCount,
&groupMembersBufferSize,
&groupMembersBufferIsMalloced, &buffer,
&buflen, &depth, &knownGroups, 0);
if (stat != NSS_SUCCESS)
{
if (groupMembersBufferIsMalloced)
free (groupMembers);
_nss_ldap_namelist_destroy (&knownGroups);
debug ("<== _nss_ldap_parse_gr: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
stat = do_fix_group_members_buffer (groupMembers, groupMembersCount,
&gr->gr_mem, &buffer, &buflen);
if (groupMembersBufferIsMalloced)
free (groupMembers);
_nss_ldap_namelist_destroy (&knownGroups);
}
else
{
stat =
_nss_ldap_assign_attrvals (e, ATM (LM_GROUP, memberUid), NULL,
&gr->gr_mem, &buffer, &buflen, NULL);
}
debug ("<== _nss_ldap_parse_gr: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
/*
* Add a group ID to a group list, and optionally the group IDs
* of any groups to which this group belongs (RFC2307bis nested
* group expansion is done by do_parse_initgroups_nested()).
*/
static NSS_STATUS
do_parse_initgroups (LDAPMessage * e,
ldap_state_t * pvt, void *result,
char *buffer, size_t buflen)
{
char **values;
ssize_t i;
gid_t gid;
ldap_initgroups_args_t *lia = (ldap_initgroups_args_t *) result;
debug ("==> do_parse_initgroups");
values = _nss_ldap_get_values (e, ATM (LM_GROUP, gidNumber));
if (values == NULL)
{
/* invalid group; skip it */
debug ("<== do_parse_initgroups: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
if (values[0] == NULL)
{
/* invalid group; skip it */
debug ("<== do_parse_initgroups: calls ldap_value_free and returns NSS_NOTFOUND");
ldap_value_free (values);
return NSS_NOTFOUND;
}
#ifdef HAVE_USERSEC_H
i = strlen (values[0]);
lia->grplist = realloc (lia->grplist, lia->listlen + i + 2);
if (lia->grplist == NULL)
{
debug ("<== do_parse_initgroups: calls ldap_value_free and returns NSS_TRYAGAIN");
ldap_value_free (values);
return NSS_TRYAGAIN;
}
memcpy (lia->grplist + lia->listlen, values[0], i);
lia->grplist[lia->listlen + i] = ',';
lia->listlen += i + 1;
debug ("<== do_parse_initgroups: calls ldap_value_free");
ldap_value_free (values);
#else
gid = strtoul (values[0], (char **) NULL, 10);
debug ("<== do_parse_initgroups: calls ldap_value_free");
ldap_value_free (values);
if (gid == LONG_MAX && errno == ERANGE)
{
/* invalid group, skip it */
debug ("<== do_parse_initgroups: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
# ifdef HAVE_NSSWITCH_H
/* weed out duplicates; is this really our resposibility? */
for (i = 0; i < lia->gbm->numgids; i++)
{
if (lia->gbm->gid_array[i] == (gid_t) gid)
{
debug ("<== do_parse_initgroups: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
}
if (lia->gbm->numgids == lia->gbm->maxgids)
{
/* can't fit any more */
/*
* should probably return NSS_TRYAGAIN but IIRC
* will send Solaris into an infinite loop XXX
*/
debug ("<== do_parse_initgroups: returns NSS_SUCCESS");
return NSS_SUCCESS;
}
lia->gbm->gid_array[lia->gbm->numgids++] = (gid_t) gid;
# else
if (gid == lia->group)
{
/* primary group, so skip it */
debug ("<== do_parse_initgroups: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
if (lia->limit > 0)
{
if (*(lia->start) >= lia->limit)
{
/* can't fit any more */
debug ("<== do_parse_initgroups: returns NSS_TRYAGAIN");
return NSS_TRYAGAIN;
}
}
if (*(lia->size) == 0)
{
*(lia->groups) = (gid_t *) realloc(*(lia->groups),
LDAP_NSS_NGROUPS * sizeof (gid_t));
if (*(lia->groups) == NULL)
{
debug ("<== do_parse_initgroups: returns NSS_TRYAGAIN");
return NSS_TRYAGAIN;
}
*(lia->size) = LDAP_NSS_NGROUPS;
}
if (*(lia->start) == *(lia->size))
{
/* Need a bigger buffer */
*(lia->groups) = (gid_t *) realloc (*(lia->groups),
2 * *(lia->size) * sizeof (gid_t));
if (*(lia->groups) == NULL)
{
debug ("<== do_parse_initgroups: returns NSS_TRYAGAIN");
return NSS_TRYAGAIN;
}
*(lia->size) *= 2;
}
else
{
assert(*(lia->start) < *(lia->size));
}
/* weed out duplicates; is this really our responsibility? */
for (i = 0; i < *(lia->start); i++)
{
if ((*(lia->groups))[i] == gid)
{
debug ("<== do_parse_initgroups: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
}
/* add to group list */
(*(lia->groups))[*(lia->start)] = gid;
(*(lia->start)) += 1;
# endif /* HAVE_NSSWITCH_H */
#endif /* HAVE_USERSEC_H */
debug ("<== do_parse_initgroups: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
static NSS_STATUS
do_parse_initgroups_nested (LDAPMessage * e,
ldap_state_t * pvt, void *result,
char *buffer, size_t buflen)
{
NSS_STATUS stat;
ldap_initgroups_args_t *lia = (ldap_initgroups_args_t *) result;
char **values;
char *groupdn;
debug ("==> do_parse_initgroups_nested");
stat = do_parse_initgroups (e, pvt, result, buffer, buflen);
if (stat != NSS_NOTFOUND)
{
debug ("<== do_parse_initgroups_nested: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
if (!_nss_ldap_test_config_flag (NSS_LDAP_FLAGS_RFC2307BIS))
{
debug ("<== do_parse_initgroups_nested: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
if (lia->backlink != 0)
{
/*
* Now add the GIDs of any groups of which this group is
* a member.
*/
values = _nss_ldap_get_values (e, ATM (LM_GROUP, memberOf));
if (values != NULL)
{
NSS_STATUS stat;
lia->depth++;
stat = ng_chase_backlink ((const char **)values, lia);
lia->depth--;
debug ("<== do_parse_initgroups_nested: calls ldap_value_free");
ldap_value_free (values);
debug ("<== do_parse_initgroups_nested: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
}
else
{
/*
* Now add the GIDs of any groups which refer to this group
*/
groupdn = _nss_ldap_get_dn (e);
if (groupdn != NULL)
{
NSS_STATUS stat;
lia->depth++;
stat = ng_chase (groupdn, lia);
lia->depth--;
#ifdef HAVE_LDAP_MEMFREE
debug ("<== do_parse_initgroups_nested: calls ldap_memfree");
ldap_memfree (groupdn);
#else
free (groupdn);
#endif
}
}
debug ("<== do_parse_initgroups_nested: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
static NSS_STATUS
ng_chase (const char *dn, ldap_initgroups_args_t * lia)
{
ldap_args_t a;
NSS_STATUS stat;
ent_context_t *ctx = NULL;
const char *gidnumber_attrs[2];
int erange;
debug ("==> ng_chase");
if (lia->depth > LDAP_NSS_MAXGR_DEPTH)
{
debug ("<== ns_chase: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
if (_nss_ldap_namelist_find (lia->known_groups, dn))
{
debug ("<== ns_chase: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
gidnumber_attrs[0] = ATM (LM_GROUP, gidNumber);
gidnumber_attrs[1] = NULL;
LA_INIT (a);
LA_STRING (a) = dn;
LA_TYPE (a) = LA_TYPE_STRING;
if (_nss_ldap_ent_context_init_internal_locked (&ctx) == NULL)
{
debug ("<== ns_chase: returns NSS_UNAVAIL");
return NSS_UNAVAIL;
}
stat = _nss_ldap_getent_ex (&a, &ctx, lia, NULL, 0,
&erange, _nss_ldap_filt_getgroupsbydn,
LM_GROUP, gidnumber_attrs,
do_parse_initgroups_nested);
if (stat == NSS_SUCCESS)
{
stat = _nss_ldap_namelist_push (&lia->known_groups, dn);
}
_nss_ldap_ent_context_release (&ctx);
debug ("<== ns_chase: returns %s(%d)", __nss_ldap_status2string(stat), stat);
return stat;
}
static NSS_STATUS
ng_chase_backlink (const char ** membersOf, ldap_initgroups_args_t * lia)
{
ldap_args_t a;
NSS_STATUS stat;
ent_context_t *ctx = NULL;
const char *gidnumber_attrs[3];
const char **memberP;
const char **filteredMembersOf; /* remove already traversed groups */
size_t memberCount, i;
int erange;
debug ("==> ng_chase_backlink");
if (lia->depth > LDAP_NSS_MAXGR_DEPTH)
{
debug ("<== ng_chase_backlink: returns NSS_NOTFOUND");
return NSS_NOTFOUND;
}
for (memberCount = 0; membersOf[memberCount] != NULL; memberCount++)