1 /* service.c - service lookup routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2011 The OpenLDAP Foundation.
6 * Portions Copyright 2008 by Howard Chu, Symas Corp.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
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.
25 /* ( nisSchema.2.3 NAME 'ipService' SUP top STRUCTURAL
26 * DESC 'Abstraction an Internet Protocol service.
27 * Maps an IP port and protocol (such as tcp or udp)
28 * to one or more names; the distinguished value of
29 * the cn attribute denotes the service's canonical
31 * MUST ( cn $ ipServicePort $ ipServiceProtocol )
32 * MAY ( description ) )
35 /* the basic search filter for searches */
36 static struct berval service_filter = BER_BVC("(objectClass=ipService)");
38 /* the attributes to request with searches */
39 static struct berval service_keys[] = {
41 BER_BVC("ipServicePort"),
42 BER_BVC("ipServiceProtocol"),
46 static int mkfilter_service_byname(nssov_mapinfo *mi,struct berval *name,
47 struct berval *protocol,struct berval *buf)
49 char buf2[1024],buf3[1024];
50 struct berval bv2 = {sizeof(buf2),buf2};
51 struct berval bv3 = {sizeof(buf3),buf3};
53 /* escape attributes */
54 if (nssov_escape(name,&bv2))
56 if (!BER_BVISNULL(protocol)) {
57 if (nssov_escape(protocol,&bv3))
59 if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[0].an_desc->ad_cname.bv_len +
60 bv3.bv_len + mi->mi_attrs[2].an_desc->ad_cname.bv_len + 9 > buf->bv_len )
62 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))",
64 mi->mi_attrs[0].an_desc->ad_cname.bv_val, bv2.bv_val,
65 mi->mi_attrs[2].an_desc->ad_cname.bv_val, bv3.bv_val );
67 if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[0].an_desc->ad_cname.bv_len + 6 >
70 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))",
71 mi->mi_filter.bv_val, mi->mi_attrs[0].an_desc->ad_cname.bv_val,
77 static int mkfilter_service_bynumber(nssov_mapinfo *mi,struct berval *numb,
78 struct berval *protocol,struct berval *buf)
81 struct berval bv2 = {sizeof(buf2),buf2};
83 /* escape attribute */
84 if (!BER_BVISNULL(protocol)) {
85 if (nssov_escape(protocol,&bv2))
87 if (numb->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[1].an_desc->ad_cname.bv_len +
88 bv2.bv_len + mi->mi_attrs[2].an_desc->ad_cname.bv_len + 9 > buf->bv_len )
90 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))",
92 mi->mi_attrs[1].an_desc->ad_cname.bv_val, numb->bv_val,
93 mi->mi_attrs[2].an_desc->ad_cname.bv_val, bv2.bv_val );
95 if (numb->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[1].an_desc->ad_cname.bv_len + 6 >
98 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))",
99 mi->mi_filter.bv_val, mi->mi_attrs[1].an_desc->ad_cname.bv_val,
107 NSSOV_CBPRIV(service,
111 struct berval prot;);
113 static int write_service(nssov_service_cbp *cbp,Entry *entry)
115 int32_t tmpint32,tmp2int32,tmp3int32;
116 struct berval name,*names,*ports,*protos;
117 struct berval tmparr[2];
121 int i,numname,dupname,numprot;
123 /* get the most canonical name */
124 nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
125 /* get the other names for the rpc */
126 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
127 if ( !a || !a->a_vals )
129 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value\n",
130 entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val, 0 );
134 numname = a->a_numvals;
135 /* if the name is not yet found, get the first entry from names */
136 if (BER_BVISNULL(&name)) {
141 for (i=0; i<numname; i++) {
142 if ( bvmatch(&name, &a->a_nvals[i])) {
148 /* get the service number */
149 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
150 if ( !a || !a->a_vals )
152 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value\n",
153 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
155 } else if ( a->a_numvals > 1 ) {
156 Debug(LDAP_DEBUG_ANY,"service entry %s contains multiple %s values\n",
157 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
159 port=(int)strtol(a->a_vals[0].bv_val,&tmp,0);
162 Debug(LDAP_DEBUG_ANY,"service entry %s contains non-numeric %s value\n",
163 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
167 if (BER_BVISNULL(&cbp->prot))
169 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[2].an_desc );
170 if ( !a || !a->a_vals )
172 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value\n",
173 entry->e_name.bv_val, cbp->mi->mi_attrs[2].an_desc->ad_cname.bv_val, 0 );
177 numprot = a->a_numvals;
183 BER_BVZERO(&protos[1]);
186 /* write the entries */
187 for (i=0;i<numprot;i++)
190 WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
191 WRITE_BERVAL(cbp->fp,&name);
192 if ( dupname >= 0 ) {
193 WRITE_INT32(cbp->fp,numname-1);
195 WRITE_INT32(cbp->fp,numname);
197 for (j=0;j<numname;j++) {
198 if (j == dupname) continue;
199 WRITE_BERVAL(cbp->fp,&names[j]);
201 WRITE_INT32(cbp->fp,port);
202 WRITE_BERVAL(cbp->fp,&protos[i]);
212 struct berval filter = {sizeof(fbuf)};
213 filter.bv_val = fbuf;
214 READ_STRING(fp,cbp.nbuf);
215 cbp.name.bv_len = tmpint32;
216 cbp.name.bv_val = cbp.nbuf;
217 READ_STRING(fp,cbp.pbuf);
218 cbp.prot.bv_len = tmpint32;
219 cbp.prot.bv_val = tmpint32 ? cbp.pbuf : NULL;,
220 Debug(LDAP_DEBUG_TRACE,"nssov_service_byname(%s,%s)\n",cbp.name.bv_val,cbp.prot.bv_val,0);,
221 NSLCD_ACTION_SERVICE_BYNAME,
222 mkfilter_service_byname(cbp.mi,&cbp.name,&cbp.prot,&filter)
229 struct berval filter = {sizeof(fbuf)};
230 filter.bv_val = fbuf;
231 READ_INT32(fp,number);
232 cbp.name.bv_val = cbp.nbuf;
233 cbp.name.bv_len = snprintf(cbp.nbuf,sizeof(cbp.nbuf),"%d",number);
234 READ_STRING(fp,cbp.pbuf);
235 cbp.prot.bv_len = tmpint32;
236 cbp.prot.bv_val = tmpint32 ? cbp.pbuf : NULL;,
237 Debug(LDAP_DEBUG_TRACE,"nssov_service_bynumber(%s,%s)\n",cbp.name.bv_val,cbp.prot.bv_val,0);,
238 NSLCD_ACTION_SERVICE_BYNUMBER,
239 mkfilter_service_bynumber(cbp.mi,&cbp.name,&cbp.prot,&filter)
244 struct berval filter;
245 /* no parameters to read */
246 BER_BVZERO(&cbp.prot);,
247 Debug(LDAP_DEBUG_TRACE,"nssov_service_all()\n",0,0,0);,
248 NSLCD_ACTION_SERVICE_ALL,
249 (filter=cbp.mi->mi_filter,0)