]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/nssov/netgroup.c
88d210be0945eccdddb36aff9c18b73a6bee77d1
[openldap] / contrib / slapd-modules / nssov / netgroup.c
1 /* netgroup.c - netgroup lookup routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 
4  *
5  * Copyright 2008-2015 The OpenLDAP Foundation.
6  * Portions Copyright 2008 by Howard Chu, Symas Corp.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This code references portions of the nss-ldapd package
19  * written by Arthur de Jong. The nss-ldapd code was forked
20  * from the nss-ldap library written by Luke Howard.
21  */
22
23 #include "nssov.h"
24 #include <ac/ctype.h>
25
26 /* ( nisSchema.2.8 NAME 'nisNetgroup' SUP top STRUCTURAL
27  *   DESC 'Abstraction of a netgroup. May refer to other netgroups'
28  *   MUST cn
29  *   MAY ( nisNetgroupTriple $ memberNisNetgroup $ description ) )
30  */
31
32 /* the basic search filter for searches */
33 static struct berval netgroup_filter = BER_BVC("(objectClass=nisNetgroup)");
34
35 /* the attributes to request with searches */
36 static struct berval netgroup_keys[] = {
37         BER_BVC("cn"),
38         BER_BVC("nisNetgroupTriple"),
39         BER_BVC("memberNisNetgroup"),
40         BER_BVNULL
41 };
42
43 NSSOV_INIT(netgroup)
44
45 NSSOV_CBPRIV(netgroup,
46         char buf[256];
47         struct berval name;);
48
49 static int write_string_stripspace_len(TFILE *fp,const char *str,int len)
50 {
51         int32_t tmpint32;
52         int i,j;
53         DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"",str);
54         if (str==NULL)
55         {
56                 WRITE_INT32(fp,0);
57         }
58         else
59         {
60                 /* skip leading spaces */
61                 for (i=0;(str[i]!='\0')&&(isspace(str[i]));i++)
62                         /* nothing else to do */ ;
63                 /* skip trailing spaces */
64                 for (j=len;(j>i)&&(isspace(str[j-1]));j--)
65                         /* nothing else to do */ ;
66                 /* write length of string */
67                 WRITE_INT32(fp,j-i);
68                 /* write string itself */
69                 if (j>i)
70                 {
71                         WRITE(fp,str+i,j-i);
72                 }
73         }
74         /* we're done */
75         return 0;
76 }
77
78 #define WRITE_STRING_STRIPSPACE_LEN(fp,str,len) \
79         if (write_string_stripspace_len(fp,str,len)) \
80                 return -1;
81
82 #define WRITE_STRING_STRIPSPACE(fp,str) \
83         WRITE_STRING_STRIPSPACE_LEN(fp,str,strlen(str))
84
85 static int write_netgroup_triple(TFILE *fp,const char *triple)
86 {
87         int32_t tmpint32;
88         int i;
89         int hostb,hoste,userb,usere,domainb,domaine;
90         /* skip leading spaces */
91         for (i=0;(triple[i]!='\0')&&(isspace(triple[i]));i++)
92                 /* nothing else to do */ ;
93         /* we should have a bracket now */
94         if (triple[i]!='(')
95         {
96                 Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): entry does not begin with '(' (entry skipped)\n",0,0,0);
97                 return 0;
98         }
99         i++;
100         hostb=i;
101         /* find comma (end of host string) */
102         for (;(triple[i]!='\0')&&(triple[i]!=',');i++)
103                 /* nothing else to do */ ;
104         if (triple[i]!=',')
105         {
106                 Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): missing ',' (entry skipped)\n",0,0,0);
107                 return 0;
108         }
109         hoste=i;
110         i++;
111         userb=i;
112         /* find comma (end of user string) */
113         for (;(triple[i]!='\0')&&(triple[i]!=',');i++)
114                 /* nothing else to do */ ;
115         if (triple[i]!=',')
116         {
117                 Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): missing ',' (entry skipped)\n",0,0,0);
118                 return 0;
119         }
120         usere=i;
121         i++;
122         domainb=i;
123         /* find closing bracket (end of domain string) */
124         for (;(triple[i]!='\0')&&(triple[i]!=')');i++)
125                 /* nothing else to do */ ;
126         if (triple[i]!=')')
127         {
128                 Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): missing ')' (entry skipped)\n",0,0,0);
129                 return 0;
130         }
131         domaine=i;
132         i++;
133         /* skip trailing spaces */
134         for (;(triple[i]!='\0')&&(isspace(triple[i]));i++)
135                 /* nothing else to do */ ;
136         /* if anything is left in the string we have a problem */
137         if (triple[i]!='\0')
138         {
139                 Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): string contains trailing data (entry skipped)\n",0,0,0);
140                 return 0;
141         }
142         /* write strings */
143         WRITE_INT32(fp,NSLCD_RESULT_BEGIN);
144         WRITE_INT32(fp,NSLCD_NETGROUP_TYPE_TRIPLE);
145         WRITE_STRING_STRIPSPACE_LEN(fp,triple+hostb,hoste-hostb)
146         WRITE_STRING_STRIPSPACE_LEN(fp,triple+userb,usere-userb)
147         WRITE_STRING_STRIPSPACE_LEN(fp,triple+domainb,domaine-domainb)
148         /* we're done */
149         return 0;
150 }
151
152 static int write_netgroup(nssov_netgroup_cbp *cbp,Entry *entry)
153 {
154         int32_t tmpint32;
155         int i;
156         Attribute *a;
157
158         /* get the netgroup triples and member */
159         a = attr_find(entry->e_attrs,cbp->mi->mi_attrs[1].an_desc);
160         if ( a ) {
161         /* write the netgroup triples */
162                 for (i=0;i<a->a_numvals;i++)
163                 {
164                         if (write_netgroup_triple(cbp->fp, a->a_vals[i].bv_val))
165                                 return -1;
166                 }
167         }
168         a = attr_find(entry->e_attrs,cbp->mi->mi_attrs[2].an_desc);
169         if ( a ) {
170         /* write netgroup members */
171                 for (i=0;i<a->a_numvals;i++)
172                 {
173                         /* write the result code */
174                         WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
175                         /* write triple indicator */
176                         WRITE_INT32(cbp->fp,NSLCD_NETGROUP_TYPE_NETGROUP);
177                         /* write netgroup name */
178                         if (write_string_stripspace_len(cbp->fp,a->a_vals[i].bv_val,a->a_vals[i].bv_len))
179                                 return -1;
180                 }
181         }
182         /* we're done */
183         return 0;
184 }
185
186 NSSOV_CB(netgroup)
187
188 NSSOV_HANDLE(
189         netgroup,byname,
190         char fbuf[1024];
191         struct berval filter = {sizeof(fbuf)};
192         filter.bv_val = fbuf;
193         READ_STRING(fp,cbp.buf);,
194         cbp.name.bv_len = tmpint32;
195         cbp.name.bv_val = cbp.buf;
196         Debug(LDAP_DEBUG_TRACE,"nssov_netgroup_byname(%s)\n",cbp.name.bv_val,0,0);,
197         NSLCD_ACTION_NETGROUP_BYNAME,
198         nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
199 )