1/* network.c - network address lookup routines */
2/* $OpenLDAP$ */
3/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2008-2011 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#include <ac/socket.h>
26
27/* ( nisSchema.2.7 NAME 'ipNetwork' SUP top STRUCTURAL
28 *   DESC 'Abstraction of a network. The distinguished value of
29 *   MUST ( cn $ ipNetworkNumber )
30 *   MAY ( ipNetmaskNumber $ l $ description $ manager ) )
31 */
32
33/* the basic search filter for searches */
34static struct berval network_filter = BER_BVC("(objectClass=ipNetwork)");
35
36/* the attributes used in searches */
37static struct berval network_keys[] = {
38	BER_BVC("cn"),
39	BER_BVC("ipNetworkNumber"),
40	BER_BVNULL
41};
42
43NSSOV_INIT(network)
44
45NSSOV_CBPRIV(network,
46	char buf[1024];
47	struct berval name;
48	struct berval addr;);
49
50/* write a single network entry to the stream */
51static int write_network(nssov_network_cbp *cbp,Entry *entry)
52{
53	int32_t tmpint32,tmp2int32,tmp3int32;
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 network */
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,"network 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,0);
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,"network 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, 0 );
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
113NSSOV_CB(network)
114
115NSSOV_HANDLE(
116	network,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_network_byname(%s)\n",cbp.name.bv_val,0,0);,
125	NSLCD_ACTION_NETWORK_BYNAME,
126	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
127)
128
129NSSOV_HANDLE(
130	network,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",0,0,0);
143		return -1;
144	}
145	cbp.addr.bv_val = cbp.buf;
146	cbp.addr.bv_len = strlen(cbp.buf);,
147	Debug(LDAP_DEBUG_TRACE,"nslcd_network_byaddr(%s)\n",cbp.addr.bv_val,0,0);,
148	NSLCD_ACTION_NETWORK_BYADDR,
149	nssov_filter_byid(cbp.mi,1,&cbp.addr,&filter)
150)
151
152NSSOV_HANDLE(
153	network,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_network_all()\n",0,0,0);,
159	NSLCD_ACTION_NETWORK_ALL,
160	(filter=cbp.mi->mi_filter,0)
161)
162