xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/nssov/ether.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* ether.c - ethernet address lookup routines */
2 /* $OpenLDAP: pkg/ldap/contrib/slapd-modules/nssov/ether.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 struct ether_addr {
24   uint8_t ether_addr_octet[6];
25 };
26 
27 /* ( nisSchema.2.11 NAME 'ieee802Device' SUP top AUXILIARY
28  *   DESC 'A device with a MAC address; device SHOULD be
29  *         used as a structural class'
30  *   MAY macAddress )
31  */
32 
33 /* the basic search filter for searches */
34 static struct berval ether_filter = BER_BVC("(objectClass=ieee802Device)");
35 
36 /* the attributes to request with searches */
37 static struct berval ether_keys[] = {
38 	BER_BVC("cn"),
39 	BER_BVC("macAddress"),
40 	BER_BVNULL
41 };
42 
43 NSSOV_INIT(ether)
44 
45 NSSOV_CBPRIV(ether,
46 	char buf[256];
47 	struct berval name;
48 	struct berval addr;);
49 
50 #define WRITE_ETHER(fp,addr) \
51   {int ao[6]; \
52   sscanf(addr.bv_val,"%02x:%02x:%02x:%02x:%02x:%02x", \
53   	&ao[0], &ao[1], &ao[2], &ao[3], &ao[4], &ao[5] );\
54 	tmpaddr.ether_addr_octet[0] = ao[0]; \
55 	tmpaddr.ether_addr_octet[1] = ao[1]; \
56 	tmpaddr.ether_addr_octet[2] = ao[2]; \
57 	tmpaddr.ether_addr_octet[3] = ao[3]; \
58 	tmpaddr.ether_addr_octet[4] = ao[4]; \
59 	tmpaddr.ether_addr_octet[5] = ao[5]; } \
60   WRITE_TYPE(fp,tmpaddr,uint8_t[6]);
61 
62 static int write_ether(nssov_ether_cbp *cbp,Entry *entry)
63 {
64 	int32_t tmpint32;
65 	struct ether_addr tmpaddr;
66 	struct berval tmparr[2], empty;
67 	struct berval *names,*ethers;
68 	Attribute *a;
69 	int i,j;
70 
71 	/* get the name of the ether entry */
72 	if (BER_BVISNULL(&cbp->name))
73 	{
74 		a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[0].an_desc);
75 		if ( !a )
76 		{
77 			Debug(LDAP_DEBUG_ANY,"ether entry %s does not contain %s value",
78 							entry->e_name.bv_val,cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val,0 );
79 			return 0;
80 		}
81 		names = a->a_vals;
82 	}
83 	else
84 	{
85 		names=tmparr;
86 		names[0]=cbp->name;
87 		BER_BVZERO(&names[1]);
88 	}
89 	/* get the addresses */
90 	if (BER_BVISNULL(&cbp->addr))
91 	{
92 		a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[1].an_desc);
93 		if ( !a )
94 		{
95 			Debug(LDAP_DEBUG_ANY,"ether entry %s does not contain %s value",
96 							entry->e_name.bv_val,cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val,0 );
97 			return 0;
98 		}
99 		ethers = a->a_vals;
100 		/* TODO: move parsing of addresses up here */
101 	}
102 	else
103 	{
104 		ethers=tmparr;
105 		ethers[0]=cbp->addr;
106 		BER_BVZERO(&ethers[1]);
107 	}
108 	/* write entries for all names and addresses */
109 	for (i=0;!BER_BVISNULL(&names[i]);i++)
110 		for (j=0;!BER_BVISNULL(&ethers[j]);j++)
111 		{
112 			WRITE_INT32(cbp->fp,NSLCD_RESULT_SUCCESS);
113 			WRITE_BERVAL(cbp->fp,&names[i]);
114 			WRITE_ETHER(cbp->fp,ethers[j]);
115 		}
116 	return 0;
117 }
118 
119 NSSOV_CB(ether)
120 
121 NSSOV_HANDLE(
122 	ether,byname,
123 	char fbuf[1024];
124 	struct berval filter = {sizeof(fbuf)};
125 	filter.bv_val = fbuf;
126 	BER_BVZERO(&cbp.addr);
127 	READ_STRING_BUF2(fp,cbp.buf,sizeof(cbp.buf));
128 	cbp.name.bv_len = tmpint32;
129 	cbp.name.bv_val = cbp.buf;,
130 	Debug(LDAP_DEBUG_TRACE,"nssov_ether_byname(%s)",cbp.name.bv_val,0,0);,
131 	NSLCD_ACTION_ETHER_BYNAME,
132 	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
133 )
134 
135 NSSOV_HANDLE(
136 	ether,byether,
137 	struct ether_addr addr;
138 	char fbuf[1024];
139 	struct berval filter = {sizeof(fbuf)};
140 	filter.bv_val = fbuf;
141 	BER_BVZERO(&cbp.name);
142 	READ_TYPE(fp,addr,uint8_t[6]);
143 	cbp.addr.bv_len = snprintf(cbp.buf,sizeof(cbp.buf), "%x:%x:%x:%x:%x:%x",
144 		addr.ether_addr_octet[0],
145 		addr.ether_addr_octet[1],
146 		addr.ether_addr_octet[2],
147 		addr.ether_addr_octet[3],
148 		addr.ether_addr_octet[4],
149 		addr.ether_addr_octet[5]);
150 	cbp.addr.bv_val = cbp.buf;,
151 	Debug(LDAP_DEBUG_TRACE,"nssov_ether_byether(%s)",cbp.addr.bv_val,0,0);,
152 	NSLCD_ACTION_ETHER_BYETHER,
153 	nssov_filter_byid(cbp.mi,1,&cbp.addr,&filter)
154 )
155 
156 NSSOV_HANDLE(
157 	ether,all,
158 	struct berval filter;
159 	/* no parameters to read */
160 	BER_BVZERO(&cbp.name);
161 	BER_BVZERO(&cbp.addr);,
162 	Debug(LDAP_DEBUG_TRACE,"nssov_ether_all()",0,0,0);,
163 	NSLCD_ACTION_ETHER_ALL,
164 	(filter=cbp.mi->mi_filter,0)
165 )
166