1 /* host.c - host lookup routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2008-2021 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.6 NAME 'ipHost' SUP top AUXILIARY
26  *	 DESC 'Abstraction of a host, an IP device. The distinguished
27  *				 value of the cn attribute denotes the host's canonical
28  *				 name. Device SHOULD be used as a structural class'
29  *	 MUST ( cn $ ipHostNumber )
30  *	 MAY ( l $ description $ manager ) )
31  */
32 
33 /* the basic search filter for searches */
34 static struct berval host_filter = BER_BVC("(objectClass=ipHost)");
35 
36 /* the attributes to request with searches */
37 static struct berval host_keys[] = {
38 	BER_BVC("cn"),
39 	BER_BVC("ipHostNumber"),
40 	BER_BVNULL
41 };
42 
43 NSSOV_INIT(host)
44 
45 NSSOV_CBPRIV(host,
46 	char buf[1024];
47 	struct berval name;
48 	struct berval addr;);
49 
50 /* write a single host entry to the stream */
write_host(nssov_host_cbp * cbp,Entry * entry)51 static int write_host(nssov_host_cbp *cbp,Entry *entry)
52 {
53 	int32_t tmpint32;
54 	int numaddr,i,numname,dupname;
55 	struct berval name,*names,*addrs;
56 	Attribute *a;
57 
58 	/* get the most canonical name */
59 	nssov_find_rdnval( &entry->e_nname, cbp->mi->mi_attrs[0].an_desc, &name );
60 	/* get the other names for the host */
61 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[0].an_desc );
62 	if ( !a || !a->a_vals )
63 	{
64 		Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
65 			entry->e_name.bv_val, cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val );
66 		return 0;
67 	}
68 	names = a->a_vals;
69 	numname = a->a_numvals;
70 	/* if the name is not yet found, get the first entry from names */
71 	if (BER_BVISNULL(&name)) {
72 		name=names[0];
73 		dupname = 0;
74 	} else {
75 		dupname = -1;
76 		for (i=0; i<numname; i++) {
77 			if ( bvmatch(&name, &a->a_nvals[i])) {
78 				dupname = i;
79 				break;
80 			}
81 		}
82 	}
83 	/* get the addresses */
84 	a = attr_find( entry->e_attrs, cbp->mi->mi_attrs[1].an_desc );
85 	if ( !a || !a->a_vals )
86 	{
87 		Debug(LDAP_DEBUG_ANY,"host entry %s does not contain %s value\n",
88 			entry->e_name.bv_val, cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val );
89 		return 0;
90 	}
91 	addrs = a->a_vals;
92 	numaddr = a->a_numvals;
93 	/* write the entry */
94 	WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
95 	WRITE_BERVAL(cbp->fp,&name);
96 	if ( dupname >= 0 ) {
97 		WRITE_INT32(cbp->fp,numname-1);
98 	} else {
99 		WRITE_INT32(cbp->fp,numname);
100 	}
101 	for (i=0;i<numname;i++) {
102 		if (i == dupname) continue;
103 		WRITE_BERVAL(cbp->fp,&names[i]);
104 	}
105 	WRITE_INT32(cbp->fp,numaddr);
106 	for (i=0;i<numaddr;i++)
107 	{
108 		WRITE_ADDRESS(cbp->fp,&addrs[i]);
109 	}
110 	return 0;
111 }
112 
113 NSSOV_CB(host)
114 
115 NSSOV_HANDLE(
116 	host,byname,
117 	char fbuf[1024];
118 	struct berval filter = {sizeof(fbuf)};
119 	filter.bv_val = fbuf;
120 	BER_BVZERO(&cbp.addr);
121 	READ_STRING(fp,cbp.buf);
122 	cbp.name.bv_len = tmpint32;
123 	cbp.name.bv_val = cbp.buf;,
124 	Debug(LDAP_DEBUG_TRACE,"nssov_host_byname(%s)\n",cbp.name.bv_val);,
125 	NSLCD_ACTION_HOST_BYNAME,
126 	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
127 )
128 
129 NSSOV_HANDLE(
130 	host,byaddr,
131 	int af;
132 	char addr[64];
133 	int len=sizeof(addr);
134 	char fbuf[1024];
135 	struct berval filter = {sizeof(fbuf)};
136 	filter.bv_val = fbuf;
137 	BER_BVZERO(&cbp.name);
138 	READ_ADDRESS(fp,addr,len,af);
139 	/* translate the address to a string */
140 	if (inet_ntop(af,addr,cbp.buf,sizeof(cbp.buf))==NULL)
141 	{
142 		Debug(LDAP_DEBUG_ANY,"nssov: unable to convert address to string\n");
143 		return -1;
144 	}
145 	cbp.addr.bv_val = cbp.buf;
146 	cbp.addr.bv_len = strlen(cbp.buf);,
147 	Debug(LDAP_DEBUG_TRACE,"nssov_host_byaddr(%s)\n",cbp.addr.bv_val);,
148 	NSLCD_ACTION_HOST_BYADDR,
149 	nssov_filter_byid(cbp.mi,1,&cbp.addr,&filter)
150 )
151 
152 NSSOV_HANDLE(
153 	host,all,
154 	struct berval filter;
155 	/* no parameters to read */
156 	BER_BVZERO(&cbp.name);
157 	BER_BVZERO(&cbp.addr);,
158 	Debug(LDAP_DEBUG_TRACE,"nssov_host_all()\n");,
159 	NSLCD_ACTION_HOST_ALL,
160 	(filter=cbp.mi->mi_filter,0)
161 )
162