Back to index

openldap  2.4.31
host.c
Go to the documentation of this file.
00001 /* host.c - host lookup routines */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 
00004  *
00005  * Copyright 2008-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2008 by Howard Chu, Symas Corp.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This code references portions of the nss-ldapd package
00019  * written by Arthur de Jong. The nss-ldapd code was forked
00020  * from the nss-ldap library written by Luke Howard.
00021  */
00022 
00023 #include "nssov.h"
00024 
00025 /* ( nisSchema.2.6 NAME 'ipHost' SUP top AUXILIARY
00026  *      DESC 'Abstraction of a host, an IP device. The distinguished
00027  *                           value of the cn attribute denotes the host's canonical
00028  *                           name. Device SHOULD be used as a structural class'
00029  *      MUST ( cn $ ipHostNumber )
00030  *      MAY ( l $ description $ manager ) )
00031  */
00032 
00033 /* the basic search filter for searches */
00034 static struct berval host_filter = BER_BVC("(objectClass=ipHost)");
00035 
00036 /* the attributes to request with searches */
00037 static struct berval host_keys[] = {
00038        BER_BVC("cn"),
00039        BER_BVC("ipHostNumber"),
00040        BER_BVNULL
00041 };
00042 
00043 NSSOV_INIT(host)
00044 
00045 NSSOV_CBPRIV(host,
00046        char buf[1024];
00047        struct berval name;
00048        struct berval addr;);
00049 
00050 /* write a single host entry to the stream */
00051 static int write_host(nssov_host_cbp *cbp,Entry *entry)
00052 {
00053        int32_t tmpint32,tmp2int32,tmp3int32;
00054        int numaddr,i,numname,dupname;
00055        struct berval name,*names,*addrs;
00056        Attribute *a;
00057 
00058        /* get the most canonical name */
00059        nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
00060        /* get the other names for the host */
00061        a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
00062        if ( !a || !a->a_vals )
00063        {
00064               Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
00065                      entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val, 0 );
00066               return 0;
00067        }
00068        names = a->a_vals;
00069        numname = a->a_numvals;
00070        /* if the name is not yet found, get the first entry from names */
00071        if (BER_BVISNULL(&name)) {
00072               name=names[0];
00073               dupname = 0;
00074        } else {
00075               dupname = -1;
00076               for (i=0; i<numname; i++) {
00077                      if ( bvmatch(&name, &a->a_nvals[i])) {
00078                             dupname = i;
00079                             break;
00080                      }
00081               }
00082        }
00083        /* get the addresses */
00084        a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
00085        if ( !a || !a->a_vals )
00086        {
00087               Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
00088                      entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val, 0 );
00089               return 0;
00090        }
00091        addrs = a->a_vals;
00092        numaddr = a->a_numvals;
00093        /* write the entry */
00094        WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
00095        WRITE_BERVAL(cbp->fp,&name);
00096        if ( dupname >= 0 ) {
00097               WRITE_INT32(cbp->fp,numname-1);
00098        } else {
00099               WRITE_INT32(cbp->fp,numname);
00100        }
00101        for (i=0;i<numname;i++) {
00102               if (i == dupname) continue;
00103               WRITE_BERVAL(cbp->fp,&names[i]);
00104        }
00105        WRITE_INT32(cbp->fp,numaddr);
00106        for (i=0;i<numaddr;i++)
00107        {
00108               WRITE_ADDRESS(cbp->fp,&addrs[i]);
00109        }
00110        return 0;
00111 }
00112 
00113 NSSOV_CB(host)
00114 
00115 NSSOV_HANDLE(
00116        host,byname,
00117        char fbuf[1024];
00118        struct berval filter = {sizeof(fbuf)};
00119        filter.bv_val = fbuf;
00120        BER_BVZERO(&cbp.addr);
00121        READ_STRING(fp,cbp.buf);
00122        cbp.name.bv_len = tmpint32;
00123        cbp.name.bv_val = cbp.buf;,
00124        Debug(LDAP_DEBUG_TRACE,"nssov_host_byname(%s)\n",cbp.name.bv_val,0,0);,
00125        NSLCD_ACTION_HOST_BYNAME,
00126        nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
00127 )
00128 
00129 NSSOV_HANDLE(
00130        host,byaddr,
00131        int af;
00132        char addr[64];
00133        int len=sizeof(addr);
00134        char fbuf[1024];
00135        struct berval filter = {sizeof(fbuf)};
00136        filter.bv_val = fbuf;
00137        BER_BVZERO(&cbp.name);
00138        READ_ADDRESS(fp,addr,len,af);
00139        /* translate the address to a string */
00140        if (inet_ntop(af,addr,cbp.buf,sizeof(cbp.buf))==NULL)
00141        {
00142               Debug(LDAP_DEBUG_ANY,"nssov: unable to convert address to string\n",0,0,0);
00143               return -1;
00144        }
00145        cbp.addr.bv_val = cbp.buf;
00146        cbp.addr.bv_len = strlen(cbp.buf);,
00147        Debug(LDAP_DEBUG_TRACE,"nssov_host_byaddr(%s)\n",cbp.addr.bv_val,0,0);,
00148        NSLCD_ACTION_HOST_BYADDR,
00149        nssov_filter_byid(cbp.mi,1,&cbp.addr,&filter)
00150 )
00151 
00152 NSSOV_HANDLE(
00153        host,all,
00154        struct berval filter;
00155        /* no parameters to read */
00156        BER_BVZERO(&cbp.name);
00157        BER_BVZERO(&cbp.addr);,
00158        Debug(LDAP_DEBUG_TRACE,"nssov_host_all()\n",0,0,0);,
00159        NSLCD_ACTION_HOST_ALL,
00160        (filter=cbp.mi->mi_filter,0)
00161 )