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