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