1/* service.c - service lookup routines */ 2/* $OpenLDAP$ */ 3/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 4 * 5 * Copyright 2008-2011 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 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 30 * name' 31 * MUST ( cn $ ipServicePort $ ipServiceProtocol ) 32 * MAY ( description ) ) 33 */ 34 35/* the basic search filter for searches */ 36static struct berval service_filter = BER_BVC("(objectClass=ipService)"); 37 38/* the attributes to request with searches */ 39static struct berval service_keys[] = { 40 BER_BVC("cn"), 41 BER_BVC("ipServicePort"), 42 BER_BVC("ipServiceProtocol"), 43 BER_BVNULL 44}; 45 46static int mkfilter_service_byname(nssov_mapinfo *mi,struct berval *name, 47 struct berval *protocol,struct berval *buf) 48{ 49 char buf2[1024],buf3[1024]; 50 struct berval bv2 = {sizeof(buf2),buf2}; 51 struct berval bv3 = {sizeof(buf3),buf3}; 52 53 /* escape attributes */ 54 if (nssov_escape(name,&bv2)) 55 return -1; 56 if (!BER_BVISNULL(protocol)) { 57 if (nssov_escape(protocol,&bv3)) 58 return -1; 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 ) 61 return -1; 62 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))", 63 mi->mi_filter.bv_val, 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 ); 66 } else { 67 if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[0].an_desc->ad_cname.bv_len + 6 > 68 buf->bv_len ) 69 return -1; 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, 72 bv2.bv_val ); 73 } 74 return 0; 75} 76 77static int mkfilter_service_bynumber(nssov_mapinfo *mi,struct berval *numb, 78 struct berval *protocol,struct berval *buf) 79{ 80 char buf2[1024]; 81 struct berval bv2 = {sizeof(buf2),buf2}; 82 83 /* escape attribute */ 84 if (!BER_BVISNULL(protocol)) { 85 if (nssov_escape(protocol,&bv2)) 86 return -1; 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 ) 89 return -1; 90 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))", 91 mi->mi_filter.bv_val, 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 ); 94 } else { 95 if (numb->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[1].an_desc->ad_cname.bv_len + 6 > 96 buf->bv_len ) 97 return -1; 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, 100 numb->bv_val ); 101 } 102 return 0; 103} 104 105NSSOV_INIT(service) 106 107NSSOV_CBPRIV(service, 108 char nbuf[256]; 109 char pbuf[256]; 110 struct berval name; 111 struct berval prot;); 112 113static int write_service(nssov_service_cbp *cbp,Entry *entry) 114{ 115 int32_t tmpint32,tmp2int32,tmp3int32; 116 struct berval name,*names,*ports,*protos; 117 struct berval tmparr[2]; 118 Attribute *a; 119 char *tmp; 120 int port; 121 int i,numname,dupname,numprot; 122 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 ) 128 { 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 ); 131 return 0; 132 } 133 names = a->a_vals; 134 numname = a->a_numvals; 135 /* if the name is not yet found, get the first entry from names */ 136 if (BER_BVISNULL(&name)) { 137 name=names[0]; 138 dupname = 0; 139 } else { 140 dupname = -1; 141 for (i=0; i<numname; i++) { 142 if ( bvmatch(&name, &a->a_nvals[i])) { 143 dupname = i; 144 break; 145 } 146 } 147 } 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 ) 151 { 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 ); 154 return 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 ); 158 } 159 port=(int)strtol(a->a_vals[0].bv_val,&tmp,0); 160 if (*tmp) 161 { 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 ); 164 return 0; 165 } 166 /* get protocols */ 167 if (BER_BVISNULL(&cbp->prot)) 168 { 169 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[2].an_desc ); 170 if ( !a || !a->a_vals ) 171 { 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 ); 174 return 0; 175 } 176 protos = a->a_vals; 177 numprot = a->a_numvals; 178 } 179 else 180 { 181 protos=tmparr; 182 protos[0]=cbp->prot; 183 BER_BVZERO(&protos[1]); 184 numprot = 1; 185 } 186 /* write the entries */ 187 for (i=0;i<numprot;i++) 188 { 189 int j; 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); 194 } else { 195 WRITE_INT32(cbp->fp,numname); 196 } 197 for (j=0;j<numname;j++) { 198 if (j == dupname) continue; 199 WRITE_BERVAL(cbp->fp,&names[j]); 200 } 201 WRITE_INT32(cbp->fp,port); 202 WRITE_BERVAL(cbp->fp,&protos[i]); 203 } 204 return 0; 205} 206 207NSSOV_CB(service) 208 209NSSOV_HANDLE( 210 service,byname, 211 char fbuf[1024]; 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) 223) 224 225NSSOV_HANDLE( 226 service,bynumber, 227 int number; 228 char fbuf[1024]; 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) 240) 241 242NSSOV_HANDLE( 243 service,all, 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) 250) 251