xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/nssov/ether.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: ether.c,v 1.3 2021/08/14 16:14:52 christos Exp $	*/
2 
3 /* ether.c - ethernet address lookup routines */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2008-2021 The OpenLDAP Foundation.
8  * Copyright 2008 by Howard Chu, Symas Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* ACKNOWLEDGEMENTS:
20  * This code references portions of the nss-ldapd package
21  * written by Arthur de Jong. The nss-ldapd code was forked
22  * from the nss-ldap library written by Luke Howard.
23  */
24 
25 #include "nssov.h"
26 
27 struct ether_addr {
28   uint8_t ether_addr_octet[6];
29 };
30 
31 /* ( nisSchema.2.11 NAME 'ieee802Device' SUP top AUXILIARY
32  *   DESC 'A device with a MAC address; device SHOULD be
33  *         used as a structural class'
34  *   MAY macAddress )
35  */
36 
37 /* the basic search filter for searches */
38 static struct berval ether_filter = BER_BVC("(objectClass=ieee802Device)");
39 
40 /* the attributes to request with searches */
41 static struct berval ether_keys[] = {
42 	BER_BVC("cn"),
43 	BER_BVC("macAddress"),
44 	BER_BVNULL
45 };
46 
47 NSSOV_INIT(ether)
48 
49 NSSOV_CBPRIV(ether,
50 	char buf[256];
51 	struct berval name;
52 	struct berval addr;);
53 
54 #define WRITE_ETHER(fp,addr) \
55   {int ao[6]; \
56   sscanf(addr.bv_val,"%02x:%02x:%02x:%02x:%02x:%02x", \
57   	&ao[0], &ao[1], &ao[2], &ao[3], &ao[4], &ao[5] );\
58 	tmpaddr.ether_addr_octet[0] = ao[0]; \
59 	tmpaddr.ether_addr_octet[1] = ao[1]; \
60 	tmpaddr.ether_addr_octet[2] = ao[2]; \
61 	tmpaddr.ether_addr_octet[3] = ao[3]; \
62 	tmpaddr.ether_addr_octet[4] = ao[4]; \
63 	tmpaddr.ether_addr_octet[5] = ao[5]; } \
64   WRITE(fp,&tmpaddr,sizeof(uint8_t[6]));
65 
write_ether(nssov_ether_cbp * cbp,Entry * entry)66 static int write_ether(nssov_ether_cbp *cbp,Entry *entry)
67 {
68 	int32_t tmpint32;
69 	struct ether_addr tmpaddr;
70 	struct berval tmparr[2];
71 	struct berval *names,*ethers;
72 	Attribute *a;
73 	int i,j;
74 
75 	/* get the name of the ether entry */
76 	if (BER_BVISNULL(&cbp->name))
77 	{
78 		a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[0].an_desc);
79 		if ( !a )
80 		{
81 			Debug(LDAP_DEBUG_ANY,"ether entry %s does not contain %s value\n",
82 							entry->e_name.bv_val,cbp->mi->mi_attrs[0].an_desc->ad_cname.bv_val );
83 			return 0;
84 		}
85 		names = a->a_vals;
86 	}
87 	else
88 	{
89 		names=tmparr;
90 		names[0]=cbp->name;
91 		BER_BVZERO(&names[1]);
92 	}
93 	/* get the addresses */
94 	if (BER_BVISNULL(&cbp->addr))
95 	{
96 		a = attr_find(entry->e_attrs, cbp->mi->mi_attrs[1].an_desc);
97 		if ( !a )
98 		{
99 			Debug(LDAP_DEBUG_ANY,"ether entry %s does not contain %s value\n",
100 							entry->e_name.bv_val,cbp->mi->mi_attrs[1].an_desc->ad_cname.bv_val );
101 			return 0;
102 		}
103 		ethers = a->a_vals;
104 		/* TODO: move parsing of addresses up here */
105 	}
106 	else
107 	{
108 		ethers=tmparr;
109 		ethers[0]=cbp->addr;
110 		BER_BVZERO(&ethers[1]);
111 	}
112 	/* write entries for all names and addresses */
113 	for (i=0;!BER_BVISNULL(&names[i]);i++)
114 		for (j=0;!BER_BVISNULL(&ethers[j]);j++)
115 		{
116 			WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
117 			WRITE_BERVAL(cbp->fp,&names[i]);
118 			WRITE_ETHER(cbp->fp,ethers[j]);
119 		}
120 	return 0;
121 }
122 
123 NSSOV_CB(ether)
124 
125 NSSOV_HANDLE(
126 	ether,byname,
127 	char fbuf[1024];
128 	struct berval filter = {sizeof(fbuf)};
129 	filter.bv_val = fbuf;
130 	BER_BVZERO(&cbp.addr);
131 	READ_STRING(fp,cbp.buf);
132 	cbp.name.bv_len = tmpint32;
133 	cbp.name.bv_val = cbp.buf;,
134 	Debug(LDAP_DEBUG_TRACE,"nssov_ether_byname(%s)\n",cbp.name.bv_val);,
135 	NSLCD_ACTION_ETHER_BYNAME,
136 	nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
137 )
138 
139 NSSOV_HANDLE(
140 	ether,byether,
141 	struct ether_addr addr;
142 	char fbuf[1024];
143 	struct berval filter = {sizeof(fbuf)};
144 	filter.bv_val = fbuf;
145 	BER_BVZERO(&cbp.name);
146 	READ(fp,&addr,sizeof(uint8_t[6]));
147 	cbp.addr.bv_len = snprintf(cbp.buf,sizeof(cbp.buf), "%x:%x:%x:%x:%x:%x",
148 		addr.ether_addr_octet[0],
149 		addr.ether_addr_octet[1],
150 		addr.ether_addr_octet[2],
151 		addr.ether_addr_octet[3],
152 		addr.ether_addr_octet[4],
153 		addr.ether_addr_octet[5]);
154 	cbp.addr.bv_val = cbp.buf;,
155 	Debug(LDAP_DEBUG_TRACE,"nssov_ether_byether(%s)\n",cbp.addr.bv_val);,
156 	NSLCD_ACTION_ETHER_BYETHER,
157 	nssov_filter_byid(cbp.mi,1,&cbp.addr,&filter)
158 )
159 
160 NSSOV_HANDLE(
161 	ether,all,
162 	struct berval filter;
163 	/* no parameters to read */
164 	BER_BVZERO(&cbp.name);
165 	BER_BVZERO(&cbp.addr);,
166 	Debug(LDAP_DEBUG_TRACE,"nssov_ether_all()\n");,
167 	NSLCD_ACTION_ETHER_ALL,
168 	(filter=cbp.mi->mi_filter,0)
169 )
170