xref: /onnv-gate/usr/src/cmd/agents/snmp/agent/access.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <sys/socket.h>
34*0Sstevel@tonic-gate #include <netinet/in.h>
35*0Sstevel@tonic-gate #include <arpa/inet.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include "impl.h"
38*0Sstevel@tonic-gate #include "error.h"
39*0Sstevel@tonic-gate #include "trace.h"
40*0Sstevel@tonic-gate #include "asn1.h"
41*0Sstevel@tonic-gate #include "snmp.h"
42*0Sstevel@tonic-gate #include "trap.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include "agent_msg.h"
45*0Sstevel@tonic-gate #include "access.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define WILD_CARD_HOST_NAME	"*"
48*0Sstevel@tonic-gate #define WILD_CARD_ADDRESS 0
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define MAX_BUF_SIZE 256
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /***** STATIC VARIABLES *****/
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static Manager *first_manager = NULL;
55*0Sstevel@tonic-gate static Community *first_community = NULL;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static EFilter *first_efilter = NULL;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static NameOidPair *first_name_oid_pair = NULL;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 
init_manager_set()62*0Sstevel@tonic-gate void init_manager_set ()
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	first_community = NULL;	/* TODO: check this out */
65*0Sstevel@tonic-gate 	first_manager = NULL;
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
set_first_manager(Manager * mgr)68*0Sstevel@tonic-gate void set_first_manager (Manager *mgr)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	first_manager = mgr;
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
get_curr_manager_set()73*0Sstevel@tonic-gate Manager * get_curr_manager_set ()
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	return first_manager;
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /***********************************************************/
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  *	returns	0 if OK
82*0Sstevel@tonic-gate  *		1 if error
83*0Sstevel@tonic-gate  *		-1 if fatal error
84*0Sstevel@tonic-gate  */
85*0Sstevel@tonic-gate 
manager_add(char * name,char * error_label)86*0Sstevel@tonic-gate Manager* manager_add(char *name, char *error_label)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	IPAddress ip_address;
89*0Sstevel@tonic-gate 	Manager *new;
90*0Sstevel@tonic-gate 	Manager *m;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	error_label[0] = '\0';
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	if(name == NULL)
97*0Sstevel@tonic-gate 	{
98*0Sstevel@tonic-gate 		(void)sprintf(error_label, "BUG: manager_add(): name is NULL");
99*0Sstevel@tonic-gate 		return NULL;
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	/* skip the ip adx for wild-card host */
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	if(strcmp(name,WILD_CARD_HOST_NAME)){
106*0Sstevel@tonic-gate 		/* try to find the IP address from the name */
107*0Sstevel@tonic-gate 		if(name_to_ip_address(name, &ip_address, error_label))
108*0Sstevel@tonic-gate 		{
109*0Sstevel@tonic-gate 			return NULL;
110*0Sstevel@tonic-gate 		}
111*0Sstevel@tonic-gate 	}
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	/* checking for dup is on the wild-card host name */
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/* check if this manager does not already exist */
118*0Sstevel@tonic-gate 	if(strcmp(name,WILD_CARD_HOST_NAME)){
119*0Sstevel@tonic-gate 		for(m = first_manager; m; m = m->next_manager)
120*0Sstevel@tonic-gate 		{
121*0Sstevel@tonic-gate 			if(ip_address.s_addr == m->ip_address.s_addr)
122*0Sstevel@tonic-gate 			{
123*0Sstevel@tonic-gate 				(void)sprintf(error_label, ERR_MSG_MANAGER_DUP, name);
124*0Sstevel@tonic-gate 				return m;
125*0Sstevel@tonic-gate 			}
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 	}else{
128*0Sstevel@tonic-gate 		for(m = first_manager; m; m = m->next_manager)
129*0Sstevel@tonic-gate 		{
130*0Sstevel@tonic-gate 			if(!strcmp(m->name,name))
131*0Sstevel@tonic-gate 			{
132*0Sstevel@tonic-gate 				return m;
133*0Sstevel@tonic-gate 			}
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/* allocate, initialize and link the new manager */
139*0Sstevel@tonic-gate 	new = (Manager *) calloc(1,sizeof(Manager));
140*0Sstevel@tonic-gate 	if(new == NULL)
141*0Sstevel@tonic-gate 	{
142*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_ALLOC);
143*0Sstevel@tonic-gate 		return NULL;
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 	new->next_manager = NULL;
146*0Sstevel@tonic-gate 	new->name = NULL;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	new->name = strdup(name);
149*0Sstevel@tonic-gate 	if(new->name == NULL)
150*0Sstevel@tonic-gate 	{
151*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_ALLOC);
152*0Sstevel@tonic-gate 		free(new);
153*0Sstevel@tonic-gate 		return NULL;
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/* ip adx for wild-card host should be zero */
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if(strcmp(name,WILD_CARD_HOST_NAME)){
159*0Sstevel@tonic-gate 		new->ip_address.s_addr = ip_address.s_addr;
160*0Sstevel@tonic-gate 	}else{
161*0Sstevel@tonic-gate 		new->ip_address.s_addr = WILD_CARD_ADDRESS;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	new->next_manager = first_manager;
166*0Sstevel@tonic-gate 	first_manager = new;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	return new;
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /***********************************************************/
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate  * returns a pointer to the manager if the request succeeds,
176*0Sstevel@tonic-gate  * otherwise returns NULL
177*0Sstevel@tonic-gate  */
178*0Sstevel@tonic-gate 
is_valid_manager(Address * address,Manager ** mngr)179*0Sstevel@tonic-gate Manager *is_valid_manager(Address *address, Manager **mngr)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate         Manager *m;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate         *mngr = NULL;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate         if(address == NULL)
186*0Sstevel@tonic-gate         {
187*0Sstevel@tonic-gate                 error("BUG: is_valid_manager(): address is NULL");
188*0Sstevel@tonic-gate                 return NULL;
189*0Sstevel@tonic-gate         }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate         if(first_manager == NULL)
192*0Sstevel@tonic-gate         {
193*0Sstevel@tonic-gate                 return NULL;
194*0Sstevel@tonic-gate         }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate         for(m = first_manager; m; m = m->next_manager)
197*0Sstevel@tonic-gate         {
198*0Sstevel@tonic-gate                 if(address->sin_addr.s_addr == m->ip_address.s_addr)
199*0Sstevel@tonic-gate                 {
200*0Sstevel@tonic-gate                         *mngr = m;
201*0Sstevel@tonic-gate                         return m;
202*0Sstevel@tonic-gate                 }
203*0Sstevel@tonic-gate         }
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate         /* check for wild-card host */
206*0Sstevel@tonic-gate         for(m = first_manager; m; m = m->next_manager)
207*0Sstevel@tonic-gate         {
208*0Sstevel@tonic-gate                 if(!strcmp(m->name,WILD_CARD_HOST_NAME)){
209*0Sstevel@tonic-gate                         *mngr = m;
210*0Sstevel@tonic-gate                         return m;
211*0Sstevel@tonic-gate                 }
212*0Sstevel@tonic-gate         }
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate         return m;
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /***********************************************************/
218*0Sstevel@tonic-gate 
delete_manager_list()219*0Sstevel@tonic-gate void delete_manager_list()
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	Manager *next;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	while(first_manager)
225*0Sstevel@tonic-gate 	{
226*0Sstevel@tonic-gate 		next = first_manager->next_manager;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		if(first_manager->name)
229*0Sstevel@tonic-gate 		{
230*0Sstevel@tonic-gate 			free(first_manager->name);
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 		free(first_manager);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		first_manager = next;
236*0Sstevel@tonic-gate 	}
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	first_manager = NULL;
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
manager_list_free(Manager * mngr)241*0Sstevel@tonic-gate void manager_list_free(Manager *mngr)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	Manager *next;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	while(mngr)
247*0Sstevel@tonic-gate 	{
248*0Sstevel@tonic-gate 		next = mngr->next_manager;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 		if(mngr->name)
251*0Sstevel@tonic-gate 		{
252*0Sstevel@tonic-gate 			free(mngr->name);
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 		free(mngr);
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 		mngr = next;
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	mngr = NULL;
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate 
sub_member_free(SubMember * mem)263*0Sstevel@tonic-gate void sub_member_free(SubMember *mem)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate   Manager *mngr;
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate   if(mem==NULL) return;
268*0Sstevel@tonic-gate   mem->count--;
269*0Sstevel@tonic-gate   if(mem->count<0){
270*0Sstevel@tonic-gate   	mngr = mem->first_manager;
271*0Sstevel@tonic-gate   	manager_list_free(mngr);
272*0Sstevel@tonic-gate   	if(mem->community_string != NULL) free(mem->community_string);
273*0Sstevel@tonic-gate   	free(mem);
274*0Sstevel@tonic-gate   }
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate 
sub_group_list_free(SubGroup * group)277*0Sstevel@tonic-gate void sub_group_list_free(SubGroup *group)
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate 	SubGroup *next;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	while(group)
283*0Sstevel@tonic-gate 	{
284*0Sstevel@tonic-gate 		next = group->next_sub_group;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 		if(group->first_sub_member != NULL)
287*0Sstevel@tonic-gate 		{
288*0Sstevel@tonic-gate 			sub_member_free(group->first_sub_member);
289*0Sstevel@tonic-gate 		}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 		free(group);
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 		group = next;
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate 
trap_slot_list_free(TrapSlot * slot)298*0Sstevel@tonic-gate void trap_slot_list_free(TrapSlot *slot)
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate 	TrapSlot *next;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	while(slot)
304*0Sstevel@tonic-gate 	{
305*0Sstevel@tonic-gate 		next = slot->next_trap_slot;
306*0Sstevel@tonic-gate 		if(slot->first_sub_group != NULL)
307*0Sstevel@tonic-gate 		{
308*0Sstevel@tonic-gate 			sub_group_list_free(slot->first_sub_group);
309*0Sstevel@tonic-gate 		}
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 		free(slot);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 		slot = next;
314*0Sstevel@tonic-gate 	}
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 
delete_efilter_list()319*0Sstevel@tonic-gate void delete_efilter_list()
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate 	EFilter *next;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	while(first_efilter)
325*0Sstevel@tonic-gate 	{
326*0Sstevel@tonic-gate 		next = first_efilter->next_efilter;
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 		if(first_efilter->name)
329*0Sstevel@tonic-gate 		{
330*0Sstevel@tonic-gate 			free(first_efilter->name);
331*0Sstevel@tonic-gate 		}
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 		free(first_efilter);
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 		first_efilter = next;
336*0Sstevel@tonic-gate 	}
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 	first_efilter = NULL;
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate /***********************************************************/
342*0Sstevel@tonic-gate 
trace_managers()343*0Sstevel@tonic-gate void trace_managers()
344*0Sstevel@tonic-gate {
345*0Sstevel@tonic-gate 	Manager *m;
346*0Sstevel@tonic-gate 	AccessServer *as;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	trace("MANAGERS:\n");
349*0Sstevel@tonic-gate 	trace("---------\n");
350*0Sstevel@tonic-gate 	for(m = first_manager; m; m = m->next_manager)
351*0Sstevel@tonic-gate 	{
352*0Sstevel@tonic-gate 		trace("%-30s %-20s\n",
353*0Sstevel@tonic-gate 			m->name,
354*0Sstevel@tonic-gate 			!strcmp(m->name,WILD_CARD_HOST_NAME)?
355*0Sstevel@tonic-gate 			"0":inet_ntoa(m->ip_address)   );
356*0Sstevel@tonic-gate 		for(as=m->first_acc_server;as;as=as->next_acc_server)
357*0Sstevel@tonic-gate 			trace_access_server(as);
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 	trace("\n");
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate /***********************************************************/
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate /*
367*0Sstevel@tonic-gate  *	returns	0 if OK
368*0Sstevel@tonic-gate  *		1 if error
369*0Sstevel@tonic-gate  *		-1 if fatal error
370*0Sstevel@tonic-gate  */
371*0Sstevel@tonic-gate 
community_add(char * name,int type,char * error_label)372*0Sstevel@tonic-gate int community_add(char *name, int type, char *error_label)
373*0Sstevel@tonic-gate {
374*0Sstevel@tonic-gate 	int ret;
375*0Sstevel@tonic-gate 	Community *new;
376*0Sstevel@tonic-gate 	Community *c;
377*0Sstevel@tonic-gate 	Community *last = NULL;
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	error_label[0] = '\0';
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	if(name == NULL)
383*0Sstevel@tonic-gate 	{
384*0Sstevel@tonic-gate 		(void)sprintf(error_label, "BUG: community_add(): name is NULL");
385*0Sstevel@tonic-gate 		return -1;
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	if(name[0] == '\0')
389*0Sstevel@tonic-gate 	{
390*0Sstevel@tonic-gate 		(void)sprintf(error_label, "BUG: community_add(): name is empty");
391*0Sstevel@tonic-gate 		return -1;
392*0Sstevel@tonic-gate 	}
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate 	if( (type != READ_ONLY) && (type != READ_WRITE) )
395*0Sstevel@tonic-gate 	{
396*0Sstevel@tonic-gate 		(void)sprintf(error_label, "BUG: community_add(): bad type (%d)", type);
397*0Sstevel@tonic-gate 		return -1;
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	for(c = first_community; c; c = c->next_community)
401*0Sstevel@tonic-gate 	{
402*0Sstevel@tonic-gate 		ret = strcmp(name, c->name);
403*0Sstevel@tonic-gate 		if(ret > 0)
404*0Sstevel@tonic-gate 		{
405*0Sstevel@tonic-gate 			break;
406*0Sstevel@tonic-gate 		}
407*0Sstevel@tonic-gate 		else
408*0Sstevel@tonic-gate 		if(ret == 0)
409*0Sstevel@tonic-gate 		{
410*0Sstevel@tonic-gate 			(void)sprintf(error_label, ERR_MSG_COMMUNITY_DUP, name);
411*0Sstevel@tonic-gate 			return 1;
412*0Sstevel@tonic-gate 		}
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 		last = c;
415*0Sstevel@tonic-gate 	}
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 	new = (Community *) calloc(1,sizeof(Community));
418*0Sstevel@tonic-gate 	if(new == NULL)
419*0Sstevel@tonic-gate 	{
420*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_ALLOC);
421*0Sstevel@tonic-gate 		return -1;
422*0Sstevel@tonic-gate 	}
423*0Sstevel@tonic-gate 	new->next_community = NULL;
424*0Sstevel@tonic-gate 	new->name = NULL;
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	new->name = strdup(name);
427*0Sstevel@tonic-gate 	if(new->name == NULL)
428*0Sstevel@tonic-gate 	{
429*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_ALLOC);
430*0Sstevel@tonic-gate 		free(new);
431*0Sstevel@tonic-gate 		return -1;
432*0Sstevel@tonic-gate 	}
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	new->type = type;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	if(last)
437*0Sstevel@tonic-gate 	{
438*0Sstevel@tonic-gate 		last->next_community = new;
439*0Sstevel@tonic-gate 	}
440*0Sstevel@tonic-gate 	else
441*0Sstevel@tonic-gate 	{
442*0Sstevel@tonic-gate 		first_community = new;
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate 	new->next_community = c;
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 	return 0;
448*0Sstevel@tonic-gate }
449*0Sstevel@tonic-gate 
get_access_type(Manager * mngr,char * name)450*0Sstevel@tonic-gate int get_access_type(Manager *mngr,char *name)
451*0Sstevel@tonic-gate {
452*0Sstevel@tonic-gate   AccessServer *as;
453*0Sstevel@tonic-gate   AccessPolicy *ap;
454*0Sstevel@tonic-gate   Community *comm;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate   if(name==NULL || mngr==NULL) return NULL;
457*0Sstevel@tonic-gate   for(as=mngr->first_acc_server;as;as=as->next_acc_server)
458*0Sstevel@tonic-gate   {
459*0Sstevel@tonic-gate     if((ap=as->first_acc_policy)!=NULL)
460*0Sstevel@tonic-gate 	for(comm=ap->first_community;comm;comm=comm->next_community)
461*0Sstevel@tonic-gate 		if(comm->name!=NULL && !strcmp(name,comm->name))
462*0Sstevel@tonic-gate 			return ap->access_type;
463*0Sstevel@tonic-gate   }
464*0Sstevel@tonic-gate   return -1;
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate /***********************************************************/
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate /* returns True or False        */
471*0Sstevel@tonic-gate 
is_valid_community(char * name,int type,Manager * mngr)472*0Sstevel@tonic-gate int is_valid_community(char *name, int type, Manager *mngr)
473*0Sstevel@tonic-gate {
474*0Sstevel@tonic-gate 	int access_type;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if(name == NULL)
478*0Sstevel@tonic-gate 	{
479*0Sstevel@tonic-gate 		error("BUG: is_valid_community(): name is NULL");
480*0Sstevel@tonic-gate 		return False;
481*0Sstevel@tonic-gate 	}
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 	if( (type != GETNEXT_REQ_MSG )
484*0Sstevel@tonic-gate 		&& (type != GET_REQ_MSG)
485*0Sstevel@tonic-gate 		&& (type != SET_REQ_MSG) )
486*0Sstevel@tonic-gate 	{
487*0Sstevel@tonic-gate 		error("BUG: is_valid_community(): bad type(0x%x)", type);
488*0Sstevel@tonic-gate 		return False;
489*0Sstevel@tonic-gate 	}
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate   	if(mngr==NULL)  return True; /* accept reqs from any hosts */
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	if(mngr->first_acc_server!=NULL){
494*0Sstevel@tonic-gate 		if( (access_type = get_access_type(mngr,name)) == -1)
495*0Sstevel@tonic-gate 			return False;
496*0Sstevel@tonic-gate 	}
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	if(type != SET_REQ_MSG)
499*0Sstevel@tonic-gate 	{
500*0Sstevel@tonic-gate 		return True;
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 	else
503*0Sstevel@tonic-gate 	{
504*0Sstevel@tonic-gate 		if(access_type == READ_WRITE)
505*0Sstevel@tonic-gate 		{
506*0Sstevel@tonic-gate 			return True;
507*0Sstevel@tonic-gate 		}
508*0Sstevel@tonic-gate 		else
509*0Sstevel@tonic-gate 		{
510*0Sstevel@tonic-gate 			return False;
511*0Sstevel@tonic-gate 		}
512*0Sstevel@tonic-gate 	}
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate }
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate /***********************************************************/
518*0Sstevel@tonic-gate 
delete_community_list()519*0Sstevel@tonic-gate void delete_community_list()
520*0Sstevel@tonic-gate {
521*0Sstevel@tonic-gate 	Community *next;
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate 	while(first_community)
525*0Sstevel@tonic-gate 	{
526*0Sstevel@tonic-gate 		next = first_community->next_community;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 		if(first_community->name)
529*0Sstevel@tonic-gate 		{
530*0Sstevel@tonic-gate 			free(first_community->name);
531*0Sstevel@tonic-gate 		}
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 		free(first_community);
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 		first_community = next;
536*0Sstevel@tonic-gate 	}
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	first_community = NULL;
539*0Sstevel@tonic-gate }
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate /***********************************************************/
543*0Sstevel@tonic-gate 
trace_access_server(AccessServer * as)544*0Sstevel@tonic-gate void trace_access_server(AccessServer *as)
545*0Sstevel@tonic-gate {
546*0Sstevel@tonic-gate   AccessPolicy *ap;
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate   if(as==NULL) return;
549*0Sstevel@tonic-gate   if( (ap=as->first_acc_policy)!=NULL )
550*0Sstevel@tonic-gate 	trace_access_policy(ap);
551*0Sstevel@tonic-gate }
552*0Sstevel@tonic-gate 
trace_access_policy(AccessPolicy * ap)553*0Sstevel@tonic-gate void trace_access_policy(AccessPolicy *ap)
554*0Sstevel@tonic-gate {
555*0Sstevel@tonic-gate   Community *c;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate   if(ap==NULL) return;
558*0Sstevel@tonic-gate   trace("\tCOMMUNITIES(");
559*0Sstevel@tonic-gate   switch(ap->access_type)
560*0Sstevel@tonic-gate   {
561*0Sstevel@tonic-gate 	case READ_ONLY:
562*0Sstevel@tonic-gate 		trace("%s", "READ_ONLY");
563*0Sstevel@tonic-gate 		break;
564*0Sstevel@tonic-gate 	case READ_WRITE:
565*0Sstevel@tonic-gate 		trace("%s", "READ_WRITE");
566*0Sstevel@tonic-gate 		break;
567*0Sstevel@tonic-gate   }
568*0Sstevel@tonic-gate   trace("): ");
569*0Sstevel@tonic-gate   for(c=ap->first_community;c;c=c->next_community)
570*0Sstevel@tonic-gate 	trace_communities(c);
571*0Sstevel@tonic-gate   trace("\n");
572*0Sstevel@tonic-gate }
573*0Sstevel@tonic-gate 
trace_communities(Community * c)574*0Sstevel@tonic-gate void trace_communities(Community *c)
575*0Sstevel@tonic-gate {
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	trace(" %s", c->name);
579*0Sstevel@tonic-gate }
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate /***********************************************************/
community_list_free(Community * comm)583*0Sstevel@tonic-gate void community_list_free(Community *comm)
584*0Sstevel@tonic-gate {
585*0Sstevel@tonic-gate 	Community *next;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	while(comm)
589*0Sstevel@tonic-gate 	{
590*0Sstevel@tonic-gate 		next = comm->next_community;
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 		if(comm->name)
593*0Sstevel@tonic-gate 		{
594*0Sstevel@tonic-gate 			free(comm->name);
595*0Sstevel@tonic-gate 		}
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 		free(comm);
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 		comm = next;
600*0Sstevel@tonic-gate 	}
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 	comm = NULL;
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate 
access_policy_list_delete(AccessPolicy * ap)605*0Sstevel@tonic-gate void access_policy_list_delete(AccessPolicy *ap)
606*0Sstevel@tonic-gate {
607*0Sstevel@tonic-gate   if(ap==NULL) return;
608*0Sstevel@tonic-gate   ap->count--;
609*0Sstevel@tonic-gate   if(ap->count<=0){
610*0Sstevel@tonic-gate   	free(ap);
611*0Sstevel@tonic-gate   }
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate 
access_policy_list_free(AccessPolicy * ap)614*0Sstevel@tonic-gate void access_policy_list_free(AccessPolicy *ap)
615*0Sstevel@tonic-gate {
616*0Sstevel@tonic-gate   if(ap==NULL) return;
617*0Sstevel@tonic-gate   ap->count--;
618*0Sstevel@tonic-gate   if(ap->count<=0){
619*0Sstevel@tonic-gate   	community_list_free(ap->first_community);
620*0Sstevel@tonic-gate   	free(ap);
621*0Sstevel@tonic-gate   }
622*0Sstevel@tonic-gate }
623*0Sstevel@tonic-gate 
access_server_delete(AccessServer * as)624*0Sstevel@tonic-gate void access_server_delete(AccessServer *as)
625*0Sstevel@tonic-gate {
626*0Sstevel@tonic-gate   if(as==NULL) return;
627*0Sstevel@tonic-gate   access_policy_list_delete(as->first_acc_policy);
628*0Sstevel@tonic-gate   free(as);
629*0Sstevel@tonic-gate }
630*0Sstevel@tonic-gate 
access_server_free(AccessServer * as)631*0Sstevel@tonic-gate void access_server_free(AccessServer *as)
632*0Sstevel@tonic-gate {
633*0Sstevel@tonic-gate   if(as==NULL) return;
634*0Sstevel@tonic-gate   access_policy_list_free(as->first_acc_policy);
635*0Sstevel@tonic-gate   free(as);
636*0Sstevel@tonic-gate }
637*0Sstevel@tonic-gate 
agent_manager_list_free(Manager * mgr)638*0Sstevel@tonic-gate void agent_manager_list_free(Manager *mgr)
639*0Sstevel@tonic-gate {
640*0Sstevel@tonic-gate 	Manager *nextmgr;
641*0Sstevel@tonic-gate 	AccessServer *as, *last=NULL;
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	if (mgr == NULL)
644*0Sstevel@tonic-gate 		return;
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 	while(mgr)
647*0Sstevel@tonic-gate 	{
648*0Sstevel@tonic-gate 		nextmgr = mgr->next_manager;
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 		as = mgr->first_acc_server;
651*0Sstevel@tonic-gate 		while (as) {
652*0Sstevel@tonic-gate 			last = as->next_acc_server;
653*0Sstevel@tonic-gate 			access_server_delete(as);
654*0Sstevel@tonic-gate 			as = last;
655*0Sstevel@tonic-gate 		}
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 		if(mgr->name)
658*0Sstevel@tonic-gate 			free(mgr->name);
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 		free(mgr);
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 		mgr = nextmgr;
663*0Sstevel@tonic-gate 	}
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 	mgr = NULL;
666*0Sstevel@tonic-gate }
667*0Sstevel@tonic-gate 
access_server_add_tail(Manager * mngr,AccessServer * acc_server)668*0Sstevel@tonic-gate void access_server_add_tail(Manager* mngr, AccessServer *acc_server)
669*0Sstevel@tonic-gate {
670*0Sstevel@tonic-gate   AccessServer *as, *last=NULL;
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate   if(mngr==NULL || acc_server==NULL) return;
673*0Sstevel@tonic-gate   for(as=mngr->first_acc_server;as;as=as->next_acc_server)
674*0Sstevel@tonic-gate 	last = as;
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate   if(last==NULL){
677*0Sstevel@tonic-gate 	mngr->first_acc_server = acc_server;
678*0Sstevel@tonic-gate   }else{
679*0Sstevel@tonic-gate 	last->next_acc_server = acc_server;
680*0Sstevel@tonic-gate   }
681*0Sstevel@tonic-gate   acc_server->next_acc_server = NULL;
682*0Sstevel@tonic-gate   acc_server->attached = TRUE;
683*0Sstevel@tonic-gate }
684*0Sstevel@tonic-gate 
community_attach(AccessPolicy * ap,Community * comm)685*0Sstevel@tonic-gate void community_attach(AccessPolicy *ap, Community *comm)
686*0Sstevel@tonic-gate {
687*0Sstevel@tonic-gate   if(ap==NULL || comm==NULL) return;
688*0Sstevel@tonic-gate   if(ap->first_community==NULL)
689*0Sstevel@tonic-gate 	ap->first_community = comm;
690*0Sstevel@tonic-gate   else{
691*0Sstevel@tonic-gate 	comm->next_community = ap->first_community;
692*0Sstevel@tonic-gate 	ap->first_community = comm;
693*0Sstevel@tonic-gate   }
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate 
efilter_add(char * name,char * error_label)697*0Sstevel@tonic-gate EFilter* efilter_add(char *name, char *error_label)
698*0Sstevel@tonic-gate {
699*0Sstevel@tonic-gate 	EFilter *new;
700*0Sstevel@tonic-gate 	EFilter *m;
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 	error_label[0] = '\0';
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 	if(name == NULL)
707*0Sstevel@tonic-gate 	{
708*0Sstevel@tonic-gate 		(void)sprintf(error_label, "BUG: efilter_add(): name is NULL");
709*0Sstevel@tonic-gate 		return NULL;
710*0Sstevel@tonic-gate 	}
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	for(m = first_efilter; m; m = m->next_efilter)
714*0Sstevel@tonic-gate 	{
715*0Sstevel@tonic-gate 		if(!strcmp(m->name,name))
716*0Sstevel@tonic-gate 		{
717*0Sstevel@tonic-gate 			return m;
718*0Sstevel@tonic-gate 		}
719*0Sstevel@tonic-gate 	}
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 	/* allocate, initialize and link the new efilter */
723*0Sstevel@tonic-gate 	new = (EFilter *) calloc(1,sizeof(EFilter));
724*0Sstevel@tonic-gate 	if(new == NULL)
725*0Sstevel@tonic-gate 	{
726*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_ALLOC);
727*0Sstevel@tonic-gate 		return NULL;
728*0Sstevel@tonic-gate 	}
729*0Sstevel@tonic-gate 	new->next_efilter = NULL;
730*0Sstevel@tonic-gate 	new->name = NULL;
731*0Sstevel@tonic-gate 
732*0Sstevel@tonic-gate 	new->name = strdup(name);
733*0Sstevel@tonic-gate 	if(new->name == NULL)
734*0Sstevel@tonic-gate 	{
735*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_ALLOC);
736*0Sstevel@tonic-gate 		free(new);
737*0Sstevel@tonic-gate 		return NULL;
738*0Sstevel@tonic-gate 	}
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	new->enterprise = enterprise_name_to_oid(new->name);
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	new->next_efilter = first_efilter;
743*0Sstevel@tonic-gate 	first_efilter = new;
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 	return new;
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate 
trap_slot_add(int num,EFilter * efilter,char * error_label)748*0Sstevel@tonic-gate TrapSlot* trap_slot_add(int num,EFilter *efilter,char *error_label)
749*0Sstevel@tonic-gate {
750*0Sstevel@tonic-gate 	TrapSlot *new;
751*0Sstevel@tonic-gate 	TrapSlot *m;
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	if(efilter==NULL) return NULL;
755*0Sstevel@tonic-gate 	if(num < 0)
756*0Sstevel@tonic-gate 	{
757*0Sstevel@tonic-gate 		(void)sprintf(error_label, "BUG: trap_slot_add(): name is NULL");
758*0Sstevel@tonic-gate 		return NULL;
759*0Sstevel@tonic-gate 	}
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	for(m = efilter->first_trap_slot; m; m = m->next_trap_slot)
763*0Sstevel@tonic-gate 	{
764*0Sstevel@tonic-gate 		if(m->num == num)
765*0Sstevel@tonic-gate 		{
766*0Sstevel@tonic-gate 			return m;
767*0Sstevel@tonic-gate 		}
768*0Sstevel@tonic-gate 	}
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	/* allocate, initialize and link the new efilter */
772*0Sstevel@tonic-gate 	new = (TrapSlot *) calloc(1,sizeof(TrapSlot));
773*0Sstevel@tonic-gate 	if(new == NULL)
774*0Sstevel@tonic-gate 	{
775*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_ALLOC);
776*0Sstevel@tonic-gate 		return NULL;
777*0Sstevel@tonic-gate 	}
778*0Sstevel@tonic-gate 	new->num = num;
779*0Sstevel@tonic-gate 	new->next_trap_slot = efilter->first_trap_slot;
780*0Sstevel@tonic-gate 	efilter->first_trap_slot = new;
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 	return new;
783*0Sstevel@tonic-gate }
784*0Sstevel@tonic-gate 
sub_group_add_tail(TrapSlot * slot,SubGroup * group)785*0Sstevel@tonic-gate void sub_group_add_tail(TrapSlot *slot, SubGroup *group)
786*0Sstevel@tonic-gate {
787*0Sstevel@tonic-gate   SubGroup *sg, *last =NULL;
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate   if(slot==NULL || group==NULL) return;
790*0Sstevel@tonic-gate   for(sg=slot->first_sub_group;sg;sg=sg->next_sub_group)
791*0Sstevel@tonic-gate 	last = sg;
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate   if(last==NULL){
794*0Sstevel@tonic-gate 	slot->first_sub_group = group;
795*0Sstevel@tonic-gate   }else{
796*0Sstevel@tonic-gate 	last->next_sub_group = group;
797*0Sstevel@tonic-gate   }
798*0Sstevel@tonic-gate   group->next_sub_group = NULL;
799*0Sstevel@tonic-gate }
800*0Sstevel@tonic-gate 
mem_filter_join(int low,int high,SubMember * mem,EFilter * filter)801*0Sstevel@tonic-gate void mem_filter_join(int low, int high,SubMember *mem,EFilter *filter)
802*0Sstevel@tonic-gate {
803*0Sstevel@tonic-gate   /* find the trap slot in the filter */
804*0Sstevel@tonic-gate   /* create subgroup, attach submember to subgroup */
805*0Sstevel@tonic-gate   /* insert subgroup into the trap slot */
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate   int idx;
808*0Sstevel@tonic-gate   TrapSlot *slot;
809*0Sstevel@tonic-gate   SubGroup *group;
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate   if(low<0 || high<0 || filter==NULL || mem==NULL) return;
812*0Sstevel@tonic-gate   for(idx=low;idx<=high;idx++){
813*0Sstevel@tonic-gate 	slot = trap_slot_add(idx,filter,error_label);
814*0Sstevel@tonic-gate 	if(slot==NULL) continue;
815*0Sstevel@tonic-gate 	group = calloc(1,sizeof(SubGroup));
816*0Sstevel@tonic-gate 	if(group==NULL){
817*0Sstevel@tonic-gate 		error("malloc() failed");
818*0Sstevel@tonic-gate 	}
819*0Sstevel@tonic-gate         /* The efilter list may contain duplicate entries because
820*0Sstevel@tonic-gate            the agent ACL file may be read several times. This seems
821*0Sstevel@tonic-gate            to be necessary to mantain other functionality in the ACL
822*0Sstevel@tonic-gate            such as specifying managers. The following hack makes sure
823*0Sstevel@tonic-gate            the trap is sent to each host only by not allowing duplicate
824*0Sstevel@tonic-gate            members in an efilter.
825*0Sstevel@tonic-gate         */
826*0Sstevel@tonic-gate         if (slot->first_sub_group == NULL) {	/* always add initial first_sub_group */
827*0Sstevel@tonic-gate 		sub_group_add_tail(slot,group);
828*0Sstevel@tonic-gate 		group->first_sub_member = mem;
829*0Sstevel@tonic-gate 		mem->count++;
830*0Sstevel@tonic-gate         }else {                                 /* at least one sub_group exists  */
831*0Sstevel@tonic-gate                 if (strcmp(slot->first_sub_group->first_sub_member->first_manager->name,
832*0Sstevel@tonic-gate                            mem->first_manager->name)) {  /* check for duplicate member */
833*0Sstevel@tonic-gate                                   sub_group_add_tail(slot,group);
834*0Sstevel@tonic-gate                                   group->first_sub_member = mem;
835*0Sstevel@tonic-gate                                   mem->count++;
836*0Sstevel@tonic-gate                 } else    /* don't add duplicate  */
837*0Sstevel@tonic-gate                       free(group);
838*0Sstevel@tonic-gate         }
839*0Sstevel@tonic-gate   }
840*0Sstevel@tonic-gate }
841*0Sstevel@tonic-gate 
trace_hosts(Manager * mngr)842*0Sstevel@tonic-gate static void trace_hosts(Manager *mngr)
843*0Sstevel@tonic-gate {
844*0Sstevel@tonic-gate    Manager *m;
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate    for(m=mngr;m;m=m->next_manager){
847*0Sstevel@tonic-gate 	trace("\t\t%s %s\n",
848*0Sstevel@tonic-gate 			m->name,
849*0Sstevel@tonic-gate 			inet_ntoa(m->ip_address));
850*0Sstevel@tonic-gate    }
851*0Sstevel@tonic-gate }
852*0Sstevel@tonic-gate 
trace_sub_member(SubMember * mem)853*0Sstevel@tonic-gate static void trace_sub_member(SubMember *mem)
854*0Sstevel@tonic-gate {
855*0Sstevel@tonic-gate   if(mem==NULL) return;
856*0Sstevel@tonic-gate   if(mem->community_string != NULL)
857*0Sstevel@tonic-gate   	trace("\tcommunity-string: %s\n",mem->community_string);
858*0Sstevel@tonic-gate   trace_hosts(mem->first_manager);
859*0Sstevel@tonic-gate }
860*0Sstevel@tonic-gate 
trace_sub_group(SubGroup * group)861*0Sstevel@tonic-gate static void trace_sub_group(SubGroup *group)
862*0Sstevel@tonic-gate {
863*0Sstevel@tonic-gate   if(group==NULL) return;
864*0Sstevel@tonic-gate   trace_sub_member(group->first_sub_member);
865*0Sstevel@tonic-gate }
866*0Sstevel@tonic-gate 
trace_trap_slot(TrapSlot * slot)867*0Sstevel@tonic-gate static void trace_trap_slot(TrapSlot *slot)
868*0Sstevel@tonic-gate {
869*0Sstevel@tonic-gate   SubGroup *group;
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate   if(slot==NULL) return;
872*0Sstevel@tonic-gate   trace("\ttrap-num=%d",slot->num);
873*0Sstevel@tonic-gate   for(group=slot->first_sub_group;group;group=group->next_sub_group)
874*0Sstevel@tonic-gate   	trace_sub_group(group);
875*0Sstevel@tonic-gate }
876*0Sstevel@tonic-gate 
trace_filter()877*0Sstevel@tonic-gate void trace_filter()
878*0Sstevel@tonic-gate {
879*0Sstevel@tonic-gate   EFilter *filter;
880*0Sstevel@tonic-gate   TrapSlot *slot;
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate   trace("#EFILTER:\n");
883*0Sstevel@tonic-gate   for(filter=first_efilter;filter;filter=filter->next_efilter)
884*0Sstevel@tonic-gate   {
885*0Sstevel@tonic-gate 	trace("enterprise=\"%s\"\n",filter->name);
886*0Sstevel@tonic-gate 	for(slot=filter->first_trap_slot;slot;slot=slot->next_trap_slot)
887*0Sstevel@tonic-gate 		trace_trap_slot(slot);
888*0Sstevel@tonic-gate   }
889*0Sstevel@tonic-gate   trace("\n");
890*0Sstevel@tonic-gate }
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate /**** Enterprise related functions *****/
893*0Sstevel@tonic-gate 
trace_name_oid_pair()894*0Sstevel@tonic-gate void trace_name_oid_pair()
895*0Sstevel@tonic-gate {
896*0Sstevel@tonic-gate   NameOidPair *np;
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate   trace("NAME_OID_PAIR:\n");
899*0Sstevel@tonic-gate   for(np=first_name_oid_pair;np;np=np->next)
900*0Sstevel@tonic-gate         trace("name: %s oid: %s\n",np->name,SSAOidString(np->oid));
901*0Sstevel@tonic-gate   trace("\n");
902*0Sstevel@tonic-gate }
903*0Sstevel@tonic-gate 
enterprise_name_to_oid(char * name)904*0Sstevel@tonic-gate Oid *enterprise_name_to_oid(char *name)
905*0Sstevel@tonic-gate {
906*0Sstevel@tonic-gate   NameOidPair *np;
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate   if(name == NULL) return NULL;
909*0Sstevel@tonic-gate   for(np=first_name_oid_pair;np;np=np->next){
910*0Sstevel@tonic-gate         if(np->name!=NULL && !strcmp(name,np->name))
911*0Sstevel@tonic-gate                 return np->oid;
912*0Sstevel@tonic-gate   }
913*0Sstevel@tonic-gate   return NULL;
914*0Sstevel@tonic-gate }
915*0Sstevel@tonic-gate 
set_name_and_oid_pair(char * inbuf)916*0Sstevel@tonic-gate static NameOidPair* set_name_and_oid_pair(char *inbuf)
917*0Sstevel@tonic-gate {
918*0Sstevel@tonic-gate         char *str;
919*0Sstevel@tonic-gate         char *name_str, *oid_str;
920*0Sstevel@tonic-gate         Oid  *oid = NULL;
921*0Sstevel@tonic-gate 	NameOidPair *np;
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate         if ((inbuf== NULL) || (inbuf[0]== '#')) return NULL;
924*0Sstevel@tonic-gate 
925*0Sstevel@tonic-gate         /* first "  for name */
926*0Sstevel@tonic-gate         if ((str = strchr(inbuf, '"')) == NULL) return NULL;
927*0Sstevel@tonic-gate         str++;
928*0Sstevel@tonic-gate         name_str = str;
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate                 /* second " for name */
931*0Sstevel@tonic-gate         if ((str = strchr(str, '"')) == NULL) return NULL;
932*0Sstevel@tonic-gate         *str = '\0';
933*0Sstevel@tonic-gate 
934*0Sstevel@tonic-gate         str++;
935*0Sstevel@tonic-gate         /* first " for oid_str*/
936*0Sstevel@tonic-gate         if ((str = strchr(str, '"')) == NULL)  return NULL;
937*0Sstevel@tonic-gate         str++;
938*0Sstevel@tonic-gate         oid_str = str;
939*0Sstevel@tonic-gate 
940*0Sstevel@tonic-gate         /* second " for oid_str*/
941*0Sstevel@tonic-gate         if ((str = strchr(str, '"')) == NULL) return NULL;
942*0Sstevel@tonic-gate         *str = '\0';
943*0Sstevel@tonic-gate         oid = SSAOidStrToOid(oid_str,error_label);
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate 	np = calloc(1,sizeof(NameOidPair));
946*0Sstevel@tonic-gate 	if(np==NULL){
947*0Sstevel@tonic-gate 	  error("calloc failed");
948*0Sstevel@tonic-gate 	  return NULL;
949*0Sstevel@tonic-gate 	}
950*0Sstevel@tonic-gate 	np->oid = oid;
951*0Sstevel@tonic-gate 	np->name = strdup(name_str);
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	if (np->name == NULL) {
954*0Sstevel@tonic-gate 		free(np);
955*0Sstevel@tonic-gate 		return(NULL);
956*0Sstevel@tonic-gate 	}
957*0Sstevel@tonic-gate 
958*0Sstevel@tonic-gate 	return np;
959*0Sstevel@tonic-gate }
960*0Sstevel@tonic-gate 
insert_name_oid_pair(char * name_str,char * oid_str)961*0Sstevel@tonic-gate static void insert_name_oid_pair(char *name_str,char* oid_str)
962*0Sstevel@tonic-gate {
963*0Sstevel@tonic-gate   char inbuf[MAX_BUF_SIZE];
964*0Sstevel@tonic-gate   NameOidPair *np;
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate    (void)sprintf(inbuf,"\"%s\"   \"%s\"\n",name_str,oid_str);
967*0Sstevel@tonic-gate    if( (np=set_name_and_oid_pair(inbuf)) != NULL){
968*0Sstevel@tonic-gate 		np->next = first_name_oid_pair;
969*0Sstevel@tonic-gate 		first_name_oid_pair = np;
970*0Sstevel@tonic-gate    }
971*0Sstevel@tonic-gate }
972*0Sstevel@tonic-gate 
load_enterprise_oid(char * filename)973*0Sstevel@tonic-gate void load_enterprise_oid(char* filename)
974*0Sstevel@tonic-gate {
975*0Sstevel@tonic-gate   FILE *fd;
976*0Sstevel@tonic-gate   char inbuf[MAX_BUF_SIZE];
977*0Sstevel@tonic-gate   NameOidPair *np;
978*0Sstevel@tonic-gate 
979*0Sstevel@tonic-gate   if(filename==NULL) return;
980*0Sstevel@tonic-gate   fd = fopen(filename,"r");
981*0Sstevel@tonic-gate   if(fd==NULL){
982*0Sstevel@tonic-gate 	error("can open the file %s",filename);
983*0Sstevel@tonic-gate 	return;
984*0Sstevel@tonic-gate   }
985*0Sstevel@tonic-gate   while(fgets(inbuf,MAX_BUF_SIZE,fd)){
986*0Sstevel@tonic-gate 	if( (np=set_name_and_oid_pair(inbuf)) != NULL){
987*0Sstevel@tonic-gate 	/* insert np */
988*0Sstevel@tonic-gate 		np->next = first_name_oid_pair;
989*0Sstevel@tonic-gate 		first_name_oid_pair = np;
990*0Sstevel@tonic-gate 	}
991*0Sstevel@tonic-gate   }
992*0Sstevel@tonic-gate   /* insert a couple of extra name-oid pairs:
993*0Sstevel@tonic-gate 	sun, snmp
994*0Sstevel@tonic-gate    */
995*0Sstevel@tonic-gate 	insert_name_oid_pair("snmp", "1.3.6.1.2.1.11");
996*0Sstevel@tonic-gate 	insert_name_oid_pair("sun", "1.3.6.1.4.1.42.2.1.1");
997*0Sstevel@tonic-gate 
998*0Sstevel@tonic-gate   (void)fclose(fd);
999*0Sstevel@tonic-gate }
1000*0Sstevel@tonic-gate 
find_efilter(Oid * oid)1001*0Sstevel@tonic-gate static EFilter* find_efilter(Oid* oid)
1002*0Sstevel@tonic-gate {
1003*0Sstevel@tonic-gate   EFilter *filter;
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate   for(filter=first_efilter;filter;filter=filter->next_efilter)
1006*0Sstevel@tonic-gate   {
1007*0Sstevel@tonic-gate 	if(SSAOidCmp(filter->enterprise,oid)==0) return filter;
1008*0Sstevel@tonic-gate   }
1009*0Sstevel@tonic-gate   return NULL;
1010*0Sstevel@tonic-gate }
1011*0Sstevel@tonic-gate 
find_trap_slot(int num,EFilter * filter)1012*0Sstevel@tonic-gate static TrapSlot* find_trap_slot(int num,EFilter *filter)
1013*0Sstevel@tonic-gate {
1014*0Sstevel@tonic-gate   TrapSlot *slot;
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate   for(slot=filter->first_trap_slot;slot;slot=slot->next_trap_slot)
1017*0Sstevel@tonic-gate 	if(slot->num==num) return slot;
1018*0Sstevel@tonic-gate   return NULL;
1019*0Sstevel@tonic-gate }
1020*0Sstevel@tonic-gate 
trap_filter_action(Oid * oid,int generic,int specific,uint32_t time_stamp,SNMP_variable * variables)1021*0Sstevel@tonic-gate void trap_filter_action(Oid *oid,int generic,int specific,
1022*0Sstevel@tonic-gate         uint32_t time_stamp,SNMP_variable *variables)
1023*0Sstevel@tonic-gate {
1024*0Sstevel@tonic-gate   EFilter *filter;
1025*0Sstevel@tonic-gate   TrapSlot *slot;
1026*0Sstevel@tonic-gate   SubGroup *group;
1027*0Sstevel@tonic-gate   Manager *manager;
1028*0Sstevel@tonic-gate   static Subid snmp_subids[] = {1,3,6,1,2,1,11};
1029*0Sstevel@tonic-gate   static Oid snmp_oid = {snmp_subids, 7};
1030*0Sstevel@tonic-gate   int trap_num;
1031*0Sstevel@tonic-gate   IPAddress my_ip_address;
1032*0Sstevel@tonic-gate 
1033*0Sstevel@tonic-gate   (void)memset(&my_ip_address, 0, sizeof(IPAddress));
1034*0Sstevel@tonic-gate 
1035*0Sstevel@tonic-gate   if(oid==NULL) return;
1036*0Sstevel@tonic-gate   if( (filter=find_efilter(oid))==NULL ) return;
1037*0Sstevel@tonic-gate   if(SSAOidCmp(oid,&snmp_oid)==0)
1038*0Sstevel@tonic-gate 	trap_num = generic;
1039*0Sstevel@tonic-gate   else
1040*0Sstevel@tonic-gate 	trap_num = specific;
1041*0Sstevel@tonic-gate   if( (slot=find_trap_slot(trap_num,filter))==NULL ) return;
1042*0Sstevel@tonic-gate   for(group=slot->first_sub_group;group;group=group->next_sub_group){
1043*0Sstevel@tonic-gate 	if(group->first_sub_member!=NULL){
1044*0Sstevel@tonic-gate 	  for(manager=group->first_sub_member->first_manager;manager;
1045*0Sstevel@tonic-gate 		manager=manager->next_manager){
1046*0Sstevel@tonic-gate 		trap_send_raw(&(manager->ip_address),my_ip_address,
1047*0Sstevel@tonic-gate 			group->first_sub_member->community_string,0,
1048*0Sstevel@tonic-gate 			oid,generic,specific, SNMP_TRAP_PORT,time_stamp,
1049*0Sstevel@tonic-gate 			variables,error_label);
1050*0Sstevel@tonic-gate 	  }
1051*0Sstevel@tonic-gate 	}
1052*0Sstevel@tonic-gate   }
1053*0Sstevel@tonic-gate }
1054*0Sstevel@tonic-gate 
1055