xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/nssov/host.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1*549b59edSchristos /*	$NetBSD: host.c,v 1.3 2021/08/14 16:14:52 christos Exp $	*/
24e6df137Slukem 
3bb30016cSlukem /* host.c - host lookup routines */
4d11b170bStron /* $OpenLDAP$ */
54e6df137Slukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
64e6df137Slukem  *
7*549b59edSchristos  * Copyright 2008-2021 The OpenLDAP Foundation.
84e6df137Slukem  * Portions Copyright 2008 by Howard Chu, Symas Corp.
9bb30016cSlukem  * All rights reserved.
10bb30016cSlukem  *
11bb30016cSlukem  * Redistribution and use in source and binary forms, with or without
12bb30016cSlukem  * modification, are permitted only as authorized by the OpenLDAP
13bb30016cSlukem  * Public License.
14bb30016cSlukem  *
15bb30016cSlukem  * A copy of this license is available in the file LICENSE in the
16bb30016cSlukem  * top-level directory of the distribution or, alternatively, at
17bb30016cSlukem  * <http://www.OpenLDAP.org/license.html>.
18bb30016cSlukem  */
194e6df137Slukem /* ACKNOWLEDGEMENTS:
20bb30016cSlukem  * This code references portions of the nss-ldapd package
21bb30016cSlukem  * written by Arthur de Jong. The nss-ldapd code was forked
22bb30016cSlukem  * from the nss-ldap library written by Luke Howard.
23bb30016cSlukem  */
24bb30016cSlukem 
25bb30016cSlukem #include "nssov.h"
26bb30016cSlukem 
27bb30016cSlukem /* ( nisSchema.2.6 NAME 'ipHost' SUP top AUXILIARY
28bb30016cSlukem  *	 DESC 'Abstraction of a host, an IP device. The distinguished
29bb30016cSlukem  *				 value of the cn attribute denotes the host's canonical
30bb30016cSlukem  *				 name. Device SHOULD be used as a structural class'
31bb30016cSlukem  *	 MUST ( cn $ ipHostNumber )
32bb30016cSlukem  *	 MAY ( l $ description $ manager ) )
33bb30016cSlukem  */
34bb30016cSlukem 
35bb30016cSlukem /* the basic search filter for searches */
36bb30016cSlukem static struct berval host_filter = BER_BVC("(objectClass=ipHost)");
37bb30016cSlukem 
38bb30016cSlukem /* the attributes to request with searches */
39bb30016cSlukem static struct berval host_keys[] = {
40bb30016cSlukem 	BER_BVC("cn"),
41bb30016cSlukem 	BER_BVC("ipHostNumber"),
42bb30016cSlukem 	BER_BVNULL
43bb30016cSlukem };
44bb30016cSlukem 
45bb30016cSlukem NSSOV_INIT(host)
46bb30016cSlukem 
47bb30016cSlukem NSSOV_CBPRIV(host,
48bb30016cSlukem 	char buf[1024];
49bb30016cSlukem 	struct berval name;
50bb30016cSlukem 	struct berval addr;);
51bb30016cSlukem 
52bb30016cSlukem /* write a single host entry to the stream */
write_host(nssov_host_cbp * cbp,Entry * entry)53bb30016cSlukem static int write_host(nssov_host_cbp *cbp,Entry *entry)
54bb30016cSlukem {
55376af7d7Schristos 	int32_t tmpint32;
56bb30016cSlukem 	int numaddr,i,numname,dupname;
57bb30016cSlukem 	struct berval name,*names,*addrs;
58bb30016cSlukem 	Attribute *a;
59bb30016cSlukem 
60bb30016cSlukem 	/* get the most canonical name */
61bb30016cSlukem 	nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
62bb30016cSlukem 	/* get the other names for the host */
63bb30016cSlukem 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
64bb30016cSlukem 	if ( !a || !a->a_vals )
65bb30016cSlukem 	{
664e6df137Slukem 		Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
67*549b59edSchristos 			entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val );
68bb30016cSlukem 		return 0;
69bb30016cSlukem 	}
70bb30016cSlukem 	names = a->a_vals;
71bb30016cSlukem 	numname = a->a_numvals;
72bb30016cSlukem 	/* if the name is not yet found, get the first entry from names */
73bb30016cSlukem 	if (BER_BVISNULL(&name)) {
74bb30016cSlukem 		name=names[0];
75bb30016cSlukem 		dupname = 0;
76bb30016cSlukem 	} else {
77bb30016cSlukem 		dupname = -1;
78bb30016cSlukem 		for (i=0; i<numname; i++) {
794e6df137Slukem 			if ( bvmatch(&name, &a->a_nvals[i])) {
80bb30016cSlukem 				dupname = i;
81bb30016cSlukem 				break;
82bb30016cSlukem 			}
83bb30016cSlukem 		}
84bb30016cSlukem 	}
85bb30016cSlukem 	/* get the addresses */
86bb30016cSlukem 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
87bb30016cSlukem 	if ( !a || !a->a_vals )
88bb30016cSlukem 	{
894e6df137Slukem 		Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
90*549b59edSchristos 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val );
91bb30016cSlukem 		return 0;
92bb30016cSlukem 	}
93bb30016cSlukem 	addrs = a->a_vals;
94bb30016cSlukem 	numaddr = a->a_numvals;
95bb30016cSlukem 	/* write the entry */
96ef2f90d3Sadam 	WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
97bb30016cSlukem 	WRITE_BERVAL(cbp->fp,&name);
98bb30016cSlukem 	if ( dupname >= 0 ) {
99bb30016cSlukem 		WRITE_INT32(cbp->fp,numname-1);
100bb30016cSlukem 	} else {
101bb30016cSlukem 		WRITE_INT32(cbp->fp,numname);
102bb30016cSlukem 	}
103bb30016cSlukem 	for (i=0;i<numname;i++) {
104bb30016cSlukem 		if (i == dupname) continue;
105bb30016cSlukem 		WRITE_BERVAL(cbp->fp,&names[i]);
106bb30016cSlukem 	}
107bb30016cSlukem 	WRITE_INT32(cbp->fp,numaddr);
108bb30016cSlukem 	for (i=0;i<numaddr;i++)
109bb30016cSlukem 	{
110bb30016cSlukem 		WRITE_ADDRESS(cbp->fp,&addrs[i]);
111bb30016cSlukem 	}
112bb30016cSlukem 	return 0;
113bb30016cSlukem }
114bb30016cSlukem 
115bb30016cSlukem NSSOV_CB(host)
116bb30016cSlukem 
117bb30016cSlukem NSSOV_HANDLE(
118bb30016cSlukem 	host,byname,
119bb30016cSlukem 	char fbuf[1024];
120bb30016cSlukem 	struct berval filter = {sizeof(fbuf)};
121bb30016cSlukem 	filter.bv_val = fbuf;
122bb30016cSlukem 	BER_BVZERO(&cbp.addr);
123ef2f90d3Sadam 	READ_STRING(fp,cbp.buf);
124bb30016cSlukem 	cbp.name.bv_len = tmpint32;
125bb30016cSlukem 	cbp.name.bv_val = cbp.buf;,
126*549b59edSchristos 	Debug(LDAP_DEBUG_TRACE,"nssov_host_byname(%s)\n",cbp.name.bv_val);,
127bb30016cSlukem 	NSLCD_ACTION_HOST_BYNAME,
128bb30016cSlukem 	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
129bb30016cSlukem )
130bb30016cSlukem 
131bb30016cSlukem NSSOV_HANDLE(
132bb30016cSlukem 	host,byaddr,
133bb30016cSlukem 	int af;
134bb30016cSlukem 	char addr[64];
135bb30016cSlukem 	int len=sizeof(addr);
136bb30016cSlukem 	char fbuf[1024];
137bb30016cSlukem 	struct berval filter = {sizeof(fbuf)};
138bb30016cSlukem 	filter.bv_val = fbuf;
139bb30016cSlukem 	BER_BVZERO(&cbp.name);
140bb30016cSlukem 	READ_ADDRESS(fp,addr,len,af);
141bb30016cSlukem 	/* translate the address to a string */
142bb30016cSlukem 	if (inet_ntop(af,addr,cbp.buf,sizeof(cbp.buf))==NULL)
143bb30016cSlukem 	{
144*549b59edSchristos 		Debug(LDAP_DEBUG_ANY,"nssov: unable to convert address to string\n");
145bb30016cSlukem 		return -1;
146bb30016cSlukem 	}
147bb30016cSlukem 	cbp.addr.bv_val = cbp.buf;
148bb30016cSlukem 	cbp.addr.bv_len = strlen(cbp.buf);,
149*549b59edSchristos 	Debug(LDAP_DEBUG_TRACE,"nssov_host_byaddr(%s)\n",cbp.addr.bv_val);,
150bb30016cSlukem 	NSLCD_ACTION_HOST_BYADDR,
151bb30016cSlukem 	nssov_filter_byid(cbp.mi,1,&cbp.addr,&filter)
152bb30016cSlukem )
153bb30016cSlukem 
154bb30016cSlukem NSSOV_HANDLE(
155bb30016cSlukem 	host,all,
156bb30016cSlukem 	struct berval filter;
157bb30016cSlukem 	/* no parameters to read */
158bb30016cSlukem 	BER_BVZERO(&cbp.name);
159bb30016cSlukem 	BER_BVZERO(&cbp.addr);,
160*549b59edSchristos 	Debug(LDAP_DEBUG_TRACE,"nssov_host_all()\n");,
161bb30016cSlukem 	NSLCD_ACTION_HOST_ALL,
162bb30016cSlukem 	(filter=cbp.mi->mi_filter,0)
163bb30016cSlukem )
164