1 /* $NetBSD: service.c,v 1.1.1.4 2014/05/28 09:58:28 tron Exp $ */ 2 3 /* service.c - service lookup routines */ 4 /* $OpenLDAP$ */ 5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6 * 7 * Copyright 2008-2014 The OpenLDAP Foundation. 8 * Portions Copyright 2008 by Howard Chu, Symas Corp. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted only as authorized by the OpenLDAP 13 * Public License. 14 * 15 * A copy of this license is available in the file LICENSE in the 16 * top-level directory of the distribution or, alternatively, at 17 * <http://www.OpenLDAP.org/license.html>. 18 */ 19 /* ACKNOWLEDGEMENTS: 20 * This code references portions of the nss-ldapd package 21 * written by Arthur de Jong. The nss-ldapd code was forked 22 * from the nss-ldap library written by Luke Howard. 23 */ 24 25 #include "nssov.h" 26 27 /* ( nisSchema.2.3 NAME 'ipService' SUP top STRUCTURAL 28 * DESC 'Abstraction an Internet Protocol service. 29 * Maps an IP port and protocol (such as tcp or udp) 30 * to one or more names; the distinguished value of 31 * the cn attribute denotes the service's canonical 32 * name' 33 * MUST ( cn $ ipServicePort $ ipServiceProtocol ) 34 * MAY ( description ) ) 35 */ 36 37 /* the basic search filter for searches */ 38 static struct berval service_filter = BER_BVC("(objectClass=ipService)"); 39 40 /* the attributes to request with searches */ 41 static struct berval service_keys[] = { 42 BER_BVC("cn"), 43 BER_BVC("ipServicePort"), 44 BER_BVC("ipServiceProtocol"), 45 BER_BVNULL 46 }; 47 48 static int mkfilter_service_byname(nssov_mapinfo *mi,struct berval *name, 49 struct berval *protocol,struct berval *buf) 50 { 51 char buf2[1024],buf3[1024]; 52 struct berval bv2 = {sizeof(buf2),buf2}; 53 struct berval bv3 = {sizeof(buf3),buf3}; 54 55 /* escape attributes */ 56 if (nssov_escape(name,&bv2)) 57 return -1; 58 if (!BER_BVISNULL(protocol)) { 59 if (nssov_escape(protocol,&bv3)) 60 return -1; 61 if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[0].an_desc->ad_cname.bv_len + 62 bv3.bv_len + mi->mi_attrs[2].an_desc->ad_cname.bv_len + 9 > buf->bv_len ) 63 return -1; 64 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))", 65 mi->mi_filter.bv_val, 66 mi->mi_attrs[0].an_desc->ad_cname.bv_val, bv2.bv_val, 67 mi->mi_attrs[2].an_desc->ad_cname.bv_val, bv3.bv_val ); 68 } else { 69 if (bv2.bv_len + mi->mi_filter.bv_len + mi->mi_attrs[0].an_desc->ad_cname.bv_len + 6 > 70 buf->bv_len ) 71 return -1; 72 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))", 73 mi->mi_filter.bv_val, mi->mi_attrs[0].an_desc->ad_cname.bv_val, 74 bv2.bv_val ); 75 } 76 return 0; 77 } 78 79 static int mkfilter_service_bynumber(nssov_mapinfo *mi,struct berval *numb, 80 struct berval *protocol,struct berval *buf) 81 { 82 char buf2[1024]; 83 struct berval bv2 = {sizeof(buf2),buf2}; 84 85 /* escape attribute */ 86 if (!BER_BVISNULL(protocol)) { 87 if (nssov_escape(protocol,&bv2)) 88 return -1; 89 if (numb->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[1].an_desc->ad_cname.bv_len + 90 bv2.bv_len + mi->mi_attrs[2].an_desc->ad_cname.bv_len + 9 > buf->bv_len ) 91 return -1; 92 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s)(%s=%s))", 93 mi->mi_filter.bv_val, 94 mi->mi_attrs[1].an_desc->ad_cname.bv_val, numb->bv_val, 95 mi->mi_attrs[2].an_desc->ad_cname.bv_val, bv2.bv_val ); 96 } else { 97 if (numb->bv_len + mi->mi_filter.bv_len + mi->mi_attrs[1].an_desc->ad_cname.bv_len + 6 > 98 buf->bv_len ) 99 return -1; 100 buf->bv_len = snprintf(buf->bv_val, buf->bv_len, "(&%s(%s=%s))", 101 mi->mi_filter.bv_val, mi->mi_attrs[1].an_desc->ad_cname.bv_val, 102 numb->bv_val ); 103 } 104 return 0; 105 } 106 107 NSSOV_INIT(service) 108 109 NSSOV_CBPRIV(service, 110 char nbuf[256]; 111 char pbuf[256]; 112 struct berval name; 113 struct berval prot;); 114 115 static int write_service(nssov_service_cbp *cbp,Entry *entry) 116 { 117 int32_t tmpint32,tmp2int32,tmp3int32; 118 struct berval name,*names,*ports,*protos; 119 struct berval tmparr[2]; 120 Attribute *a; 121 char *tmp; 122 int port; 123 int i,numname,dupname,numprot; 124 125 /* get the most canonical name */ 126 nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name ); 127 /* get the other names for the rpc */ 128 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc ); 129 if ( !a || !a->a_vals ) 130 { 131 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value\n", 132 entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val, 0 ); 133 return 0; 134 } 135 names = a->a_vals; 136 numname = a->a_numvals; 137 /* if the name is not yet found, get the first entry from names */ 138 if (BER_BVISNULL(&name)) { 139 name=names[0]; 140 dupname = 0; 141 } else { 142 dupname = -1; 143 for (i=0; i<numname; i++) { 144 if ( bvmatch(&name, &a->a_nvals[i])) { 145 dupname = i; 146 break; 147 } 148 } 149 } 150 /* get the service number */ 151 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc ); 152 if ( !a || !a->a_vals ) 153 { 154 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value\n", 155 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 ); 156 return 0; 157 } else if ( a->a_numvals > 1 ) { 158 Debug(LDAP_DEBUG_ANY,"service entry %s contains multiple %s values\n", 159 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 ); 160 } 161 port=(int)strtol(a->a_vals[0].bv_val,&tmp,0); 162 if (*tmp) 163 { 164 Debug(LDAP_DEBUG_ANY,"service entry %s contains non-numeric %s value\n", 165 entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 ); 166 return 0; 167 } 168 /* get protocols */ 169 if (BER_BVISNULL(&cbp->prot)) 170 { 171 a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[2].an_desc ); 172 if ( !a || !a->a_vals ) 173 { 174 Debug(LDAP_DEBUG_ANY,"service entry %s does not contain %s value\n", 175 entry->e_name.bv_val, cbp->mi->mi_attrs[2].an_desc->ad_cname.bv_val, 0 ); 176 return 0; 177 } 178 protos = a->a_vals; 179 numprot = a->a_numvals; 180 } 181 else 182 { 183 protos=tmparr; 184 protos[0]=cbp->prot; 185 BER_BVZERO(&protos[1]); 186 numprot = 1; 187 } 188 /* write the entries */ 189 for (i=0;i<numprot;i++) 190 { 191 int j; 192 WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN); 193 WRITE_BERVAL(cbp->fp,&name); 194 if ( dupname >= 0 ) { 195 WRITE_INT32(cbp->fp,numname-1); 196 } else { 197 WRITE_INT32(cbp->fp,numname); 198 } 199 for (j=0;j<numname;j++) { 200 if (j == dupname) continue; 201 WRITE_BERVAL(cbp->fp,&names[j]); 202 } 203 WRITE_INT32(cbp->fp,port); 204 WRITE_BERVAL(cbp->fp,&protos[i]); 205 } 206 return 0; 207 } 208 209 NSSOV_CB(service) 210 211 NSSOV_HANDLE( 212 service,byname, 213 char fbuf[1024]; 214 struct berval filter = {sizeof(fbuf)}; 215 filter.bv_val = fbuf; 216 READ_STRING(fp,cbp.nbuf); 217 cbp.name.bv_len = tmpint32; 218 cbp.name.bv_val = cbp.nbuf; 219 READ_STRING(fp,cbp.pbuf); 220 cbp.prot.bv_len = tmpint32; 221 cbp.prot.bv_val = tmpint32 ? cbp.pbuf : NULL;, 222 Debug(LDAP_DEBUG_TRACE,"nssov_service_byname(%s,%s)\n",cbp.name.bv_val,cbp.prot.bv_val ? cbp.prot.bv_val : "",0);, 223 NSLCD_ACTION_SERVICE_BYNAME, 224 mkfilter_service_byname(cbp.mi,&cbp.name,&cbp.prot,&filter) 225 ) 226 227 NSSOV_HANDLE( 228 service,bynumber, 229 int number; 230 char fbuf[1024]; 231 struct berval filter = {sizeof(fbuf)}; 232 filter.bv_val = fbuf; 233 READ_INT32(fp,number); 234 cbp.name.bv_val = cbp.nbuf; 235 cbp.name.bv_len = snprintf(cbp.nbuf,sizeof(cbp.nbuf),"%d",number); 236 READ_STRING(fp,cbp.pbuf); 237 cbp.prot.bv_len = tmpint32; 238 cbp.prot.bv_val = tmpint32 ? cbp.pbuf : NULL;, 239 Debug(LDAP_DEBUG_TRACE,"nssov_service_bynumber(%s,%s)\n",cbp.name.bv_val,cbp.prot.bv_val,0);, 240 NSLCD_ACTION_SERVICE_BYNUMBER, 241 mkfilter_service_bynumber(cbp.mi,&cbp.name,&cbp.prot,&filter) 242 ) 243 244 NSSOV_HANDLE( 245 service,all, 246 struct berval filter; 247 /* no parameters to read */ 248 BER_BVZERO(&cbp.prot);, 249 Debug(LDAP_DEBUG_TRACE,"nssov_service_all()\n",0,0,0);, 250 NSLCD_ACTION_SERVICE_ALL, 251 (filter=cbp.mi->mi_filter,0) 252 ) 253