1 /* service.c - service lookup routines */
4 * Copyright 2008 by Howard Chu, Symas Corp.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This code references portions of the nss-ldapd package
17 * written by Arthur de Jong. The nss-ldapd code was forked
18 * from the nss-ldap library written by Luke Howard.
23 /* ( nisSchema.2.3 NAME 'ipService' SUP top STRUCTURAL
24 * DESC 'Abstraction an Internet Protocol service.
25 * Maps an IP port and protocol (such as tcp or udp)
26 * to one or more names; the distinguished value of
27 * the cn attribute denotes the service's canonical
29 * MUST ( cn $ ipServicePort $ ipServiceProtocol )
30 * MAY ( description ) )
33 /* the basic search filter for searches */
34 static struct berval service_filter = BER_BVC("(objectClass=ipService)");
36 /* the attributes to request with searches */
37 static struct berval service_keys[] = {
39 BER_BVC("ipServicePort"),
40 BER_BVC("ipServiceProtocol"),
44 static int mkfilter_service_byname(nssov_mapinfo *mi,struct berval *name,
45 struct berval *protocol,struct berval *buf)
47 char buf2[1024],buf3[1024];
48 struct berval bv2 = {sizeof(buf2),buf2};
49 struct berval bv3 = {sizeof(buf3),buf3};
51 /* escape attributes */
52 if (nssov_escape(name,&bv2))
54 if (!BER_BVISNULL(protocol)) {
55 if (nssov_escape(protocol,&bv3))
57 if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[0].an_desc->ad_cname.bv_len +
58 bv3.bv_len + mi->mi_attrs[2].an_desc->ad_cname.bv_len + 9 > buf->bv_len )
60 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))",
62 mi->mi_attrs[0].an_desc->ad_cname.bv_val, bv2.bv_val,
63 mi->mi_attrs[2].an_desc->ad_cname.bv_val, bv3.bv_val );
65 if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[0].an_desc->ad_cname.bv_len + 6 >
68 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))",
69 mi->mi_filter.bv_val, mi->mi_attrs[0].an_desc->ad_cname.bv_val,
75 static int mkfilter_service_bynumber(nssov_mapinfo *mi,struct berval *numb,
76 struct berval *protocol,struct berval *buf)
79 struct berval bv2 = {sizeof(buf2),buf2};
81 /* escape attribute */
82 if (!BER_BVISNULL(protocol)) {
83 if (nssov_escape(protocol,&bv2))
85 if (numb->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[1].an_desc->ad_cname.bv_len +
86 bv2.bv_len + mi->mi_attrs[2].an_desc->ad_cname.bv_len + 9 > buf->bv_len )
88 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))",
90 mi->mi_attrs[1].an_desc->ad_cname.bv_val, numb->bv_val,
91 mi->mi_attrs[2].an_desc->ad_cname.bv_val, bv2.bv_val );
93 if (numb->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[1].an_desc->ad_cname.bv_len + 6 >
96 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))",
97 mi->mi_filter.bv_val, mi->mi_attrs[1].an_desc->ad_cname.bv_val,
105 NSSOV_CBPRIV(service,
109 struct berval prot;);
111 static int write_service(nssov_service_cbp *cbp,Entry *entry)
113 int32_t tmpint32,tmp2int32,tmp3int32;
114 struct berval name,*names,*ports,*protos;
115 struct berval tmparr[2];
119 int i,numname,dupname,numprot;
121 /* get the most canonical name */
122 nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
123 /* get the other names for the rpc */
124 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
125 if ( !a || !a->a_vals )
127 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value",
128 entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val, 0 );
132 numname = a->a_numvals;
133 /* if the name is not yet found, get the first entry from names */
134 if (BER_BVISNULL(&name)) {
139 for (i=0; i<numname; i++) {
140 if ( ber_bvmatch(&name, &a->a_nvals[i])) {
146 /* get the service number */
147 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
148 if ( !a || !a->a_vals )
150 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value",
151 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
153 } else if ( a->a_numvals > 1 ) {
154 Debug(LDAP_DEBUG_ANY,"service entry %s contains multiple %s values",
155 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
157 port=(int)strtol(a->a_vals[0].bv_val,&tmp,0);
160 Debug(LDAP_DEBUG_ANY,"service entry %s contains non-numeric %s value",
161 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
165 if (BER_BVISNULL(&cbp->prot))
167 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[2].an_desc );
168 if ( !a || !a->a_vals )
170 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value",
171 entry->e_name.bv_val, cbp->mi->mi_attrs[2].an_desc->ad_cname.bv_val, 0 );
175 numprot = a->a_numvals;
181 BER_BVZERO(&protos[1]);
184 /* write the entries */
185 for (i=0;i<numprot;i++)
188 WRITE_INT32(cbp->fp,NSLCD_RESULT_SUCCESS);
189 WRITE_BERVAL(cbp->fp,&name);
190 if ( dupname >= 0 ) {
191 WRITE_INT32(cbp->fp,numname-1);
193 WRITE_INT32(cbp->fp,numname);
195 for (j=0;j<numname;j++) {
196 if (j == dupname) continue;
197 WRITE_BERVAL(cbp->fp,&names[j]);
199 WRITE_INT32(cbp->fp,port);
200 WRITE_BERVAL(cbp->fp,&protos[i]);
210 struct berval filter = {sizeof(fbuf)};
211 filter.bv_val = fbuf;
212 READ_STRING_BUF2(fp,cbp.nbuf,sizeof(cbp.nbuf));
213 cbp.name.bv_len = tmpint32;
214 cbp.name.bv_val = cbp.nbuf;
215 READ_STRING_BUF2(fp,cbp.pbuf,sizeof(cbp.pbuf));
216 cbp.prot.bv_len = tmpint32;
217 cbp.prot.bv_val = tmpint32 ? cbp.pbuf : NULL;,
218 Debug(LDAP_DEBUG_TRACE,"nssov_service_byname(%s,%s)",cbp.name.bv_val,cbp.prot.bv_val,0);,
219 NSLCD_ACTION_SERVICE_BYNAME,
220 mkfilter_service_byname(cbp.mi,&cbp.name,&cbp.prot,&filter)
227 struct berval filter = {sizeof(fbuf)};
228 filter.bv_val = fbuf;
229 READ_INT32(fp,number);
230 cbp.name.bv_val = cbp.nbuf;
231 cbp.name.bv_len = snprintf(cbp.nbuf,sizeof(cbp.nbuf),"%d",number);
232 READ_STRING_BUF2(fp,cbp.pbuf,sizeof(cbp.pbuf));
233 cbp.prot.bv_len = tmpint32;
234 cbp.prot.bv_val = tmpint32 ? cbp.pbuf : NULL;,
235 Debug(LDAP_DEBUG_TRACE,"nssov_service_bynumber(%s,%s)",cbp.name.bv_val,cbp.prot.bv_val,0);,
236 NSLCD_ACTION_SERVICE_BYNUMBER,
237 mkfilter_service_bynumber(cbp.mi,&cbp.name,&cbp.prot,&filter)
242 struct berval filter;
243 /* no parameters to read */
244 BER_BVZERO(&cbp.prot);,
245 Debug(LDAP_DEBUG_TRACE,"nssov_service_all()",0,0,0);,
246 NSLCD_ACTION_SERVICE_ALL,
247 (filter=cbp.mi->mi_filter,0)