xref: /onnv-gate/usr/src/cmd/agents/snmp/agent/reg_subtree.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 2004 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 <unistd.h>
30*0Sstevel@tonic-gate #include <sys/types.h>
31*0Sstevel@tonic-gate #include <sys/times.h>
32*0Sstevel@tonic-gate #include <netinet/in.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <sys/socket.h>
35*0Sstevel@tonic-gate #include <errno.h>
36*0Sstevel@tonic-gate #include <syslog.h>
37*0Sstevel@tonic-gate #include <string.h>
38*0Sstevel@tonic-gate #include <arpa/inet.h>
39*0Sstevel@tonic-gate #include <netdb.h>
40*0Sstevel@tonic-gate #include <nlist.h>
41*0Sstevel@tonic-gate #include <limits.h>
42*0Sstevel@tonic-gate #include <stdlib.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include "asn1.h"
45*0Sstevel@tonic-gate #include "snmp_msg.h"
46*0Sstevel@tonic-gate #include "impl.h"
47*0Sstevel@tonic-gate #include "error.h"
48*0Sstevel@tonic-gate #include "trace.h"
49*0Sstevel@tonic-gate #include "snmp.h"
50*0Sstevel@tonic-gate #include "pdu.h"
51*0Sstevel@tonic-gate #include "request.h"
52*0Sstevel@tonic-gate #include "pagent.h"
53*0Sstevel@tonic-gate #include "subtree.h"
54*0Sstevel@tonic-gate #include "table.h"
55*0Sstevel@tonic-gate #include "trap.h"
56*0Sstevel@tonic-gate #include "node.h"
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #define	READ_COMM	"public"
59*0Sstevel@tonic-gate #define	WRITE_COMM	"private"
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #define REG_AGENT_ID		1
62*0Sstevel@tonic-gate #define REG_AGENT_STATUS	2
63*0Sstevel@tonic-gate #define REG_AGENT_TIME_OUT	3
64*0Sstevel@tonic-gate #define REG_AGENT_PORT_NUMBER	4
65*0Sstevel@tonic-gate #define REG_AGENT_PERSONAL_FILE	5
66*0Sstevel@tonic-gate #define REG_AGENT_CONFIG_FILE	6
67*0Sstevel@tonic-gate #define REG_AGENT_EXECUTABLE	7
68*0Sstevel@tonic-gate #define REG_AGENT_VERSION_NUM	8
69*0Sstevel@tonic-gate #define REG_AGENT_PROCESS_ID	9
70*0Sstevel@tonic-gate #define REG_AGENT_NAME		10
71*0Sstevel@tonic-gate #define REG_AGENT_SYSTEM_UP_TIME 11
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #define REG_TREE_INDEX		1
74*0Sstevel@tonic-gate #define REG_TREE_AGENT_ID	2
75*0Sstevel@tonic-gate #define REG_TREE_OID		3
76*0Sstevel@tonic-gate #define REG_TREE_STATUS		4
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate #define REG_TBL_INDEX		1
79*0Sstevel@tonic-gate #define REG_TBL_AGENT_ID	2
80*0Sstevel@tonic-gate #define REG_TBL_OID		3
81*0Sstevel@tonic-gate #define REG_TBL_SCOL	 	4
82*0Sstevel@tonic-gate #define REG_TBL_ECOL		5
83*0Sstevel@tonic-gate #define REG_TBL_SROW		6
84*0Sstevel@tonic-gate #define REG_TBL_EROW		7
85*0Sstevel@tonic-gate #define REG_TBL_STATUS		8
86*0Sstevel@tonic-gate #define FAIL 0
87*0Sstevel@tonic-gate #define SUCCESS 1
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #define	SNMP_COMM_MAX		32
90*0Sstevel@tonic-gate #define	AGENT_CONFIG_FILE	"/etc/snmp/conf/snmpd.conf"
91*0Sstevel@tonic-gate #define	MAX_CONFIG_FILE		128
92*0Sstevel@tonic-gate char	snmp_fullmib_read_community[SNMP_COMM_MAX] = {NULL};
93*0Sstevel@tonic-gate char	agent_config_file[MAX_CONFIG_FILE] = AGENT_CONFIG_FILE;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate int set_conf_word();
96*0Sstevel@tonic-gate int agent_read_config();
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate struct cmd {
99*0Sstevel@tonic-gate 	char *name;
100*0Sstevel@tonic-gate 	int (*rtn)();
101*0Sstevel@tonic-gate 	char *arg1;
102*0Sstevel@tonic-gate 	int arg2;
103*0Sstevel@tonic-gate } cmds[] = {
104*0Sstevel@tonic-gate 	{ "read-community", set_conf_word,
105*0Sstevel@tonic-gate 	snmp_fullmib_read_community, SNMP_COMM_MAX },
106*0Sstevel@tonic-gate 	{ 0, 0, 0, 0}
107*0Sstevel@tonic-gate };
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /* global variable */
110*0Sstevel@tonic-gate struct CallbackItem *callItem = NULL;
111*0Sstevel@tonic-gate int numCallItem=0;
112*0Sstevel@tonic-gate int *trapTableMap=NULL;
113*0Sstevel@tonic-gate struct TrapHndlCxt *trapBucket=NULL;
114*0Sstevel@tonic-gate struct TrapEnterpriseInfo *trapEnterpriseInfo=NULL;
115*0Sstevel@tonic-gate /* For arbitrary length enterprise OID in traps - bug 4133978 */
116*0Sstevel@tonic-gate struct TrapAnyEnterpriseInfo *trapAnyEnterpriseInfo=NULL;
117*0Sstevel@tonic-gate int numTrapElem=0;
118*0Sstevel@tonic-gate extern int dont_read_config_file;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate static Subid reg_subagent_subids[] = { 1,3,6,1,4,1,42,2,15,8,1,0,0 };
121*0Sstevel@tonic-gate static Oid reg_subagent_oid = {reg_subagent_subids, 13 };
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static Subid agent_tbl_index_subids[] = { 1,3,6,1,4,1,42,2,15,9,0 };
124*0Sstevel@tonic-gate static Oid agent_tbl_index_oid = { agent_tbl_index_subids, 11 };
125*0Sstevel@tonic-gate static Subid reg_tree_ra_subids[] = { 1,3,6,1,4,1,42,2,15,12,1,3,0,0};
126*0Sstevel@tonic-gate static Oid reg_tree_ra_oid = { reg_tree_ra_subids, 14};
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static Subid ra_trap_port_subids[] = { 1,3,6,1,4,1,42,2,15,4,0};
129*0Sstevel@tonic-gate static Oid ra_trap_port_oid = { ra_trap_port_subids, 11};
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate static Subid ra_check_point_subids[] = { 1,3,6,1,4,1,42,2,15,5,0};
132*0Sstevel@tonic-gate static Oid ra_check_point_oid = { ra_check_point_subids, 11};
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /* last three numbers are columar obj, agentid, table_id */
135*0Sstevel@tonic-gate static Subid reg_shared_table_subids[] = { 1,3,6,1,4,1,42,2,15,10,1,0,0,0};
136*0Sstevel@tonic-gate static Oid reg_shared_table_oid = { reg_shared_table_subids, 14};
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate static int _SSASendTrap_generic(char *, int);
139*0Sstevel@tonic-gate static int _SSASendTrap_generic2(char *, int, IndexType *);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * agent table id can be: from command line, global var. sap_agent_tbl_id,
143*0Sstevel@tonic-gate  * or from getpid(). The agent_id can be less than one.
144*0Sstevel@tonic-gate  */
145*0Sstevel@tonic-gate int
sap_glb_agent_table_id()146*0Sstevel@tonic-gate sap_glb_agent_table_id()
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	return((int)getpid());
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate SNMP_variable *
sap_append_integer_variable(SNMP_variable * list,Oid * oid,int num)152*0Sstevel@tonic-gate sap_append_integer_variable(SNMP_variable *list, Oid *oid, int num)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate   SNMP_value value;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate   value.v_integer = num;
157*0Sstevel@tonic-gate   list = snmp_typed_variable_append(list,oid,INTEGER,&value,error_label);
158*0Sstevel@tonic-gate   if(list == NULL){
159*0Sstevel@tonic-gate 	error("sap_append_integer_variable failed: oid: %s, value: %d\n",
160*0Sstevel@tonic-gate 		SSAOidString(oid),num);
161*0Sstevel@tonic-gate   }
162*0Sstevel@tonic-gate   return(list);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate SNMP_variable *
sap_append_variable(SNMP_variable * list,Oid * oid,int num,int type)166*0Sstevel@tonic-gate sap_append_variable(SNMP_variable *list, Oid *oid, int num, int type)
167*0Sstevel@tonic-gate {
168*0Sstevel@tonic-gate   SNMP_value value;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate   value.v_integer = num;
171*0Sstevel@tonic-gate   list = snmp_typed_variable_append(list,oid,type,&value,error_label);
172*0Sstevel@tonic-gate   if(list == NULL){
173*0Sstevel@tonic-gate 	error("sap_append_variable failed: oid: %s, value: %d, type: %d\n",
174*0Sstevel@tonic-gate 		SSAOidString(oid),num,type);
175*0Sstevel@tonic-gate   }
176*0Sstevel@tonic-gate   return(list);
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate 
sap_append_string_variable(SNMP_variable * list,Oid * oid,char * str)179*0Sstevel@tonic-gate SNMP_variable *sap_append_string_variable(SNMP_variable *list, Oid *oid, char* str)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate   SNMP_value value;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate   if(str == NULL) return NULL;
184*0Sstevel@tonic-gate   value.v_string.chars = (u_char *)str;
185*0Sstevel@tonic-gate   /* LINTED */
186*0Sstevel@tonic-gate   value.v_string.len = (int)strlen(str);
187*0Sstevel@tonic-gate   list = snmp_typed_variable_append(list,oid,STRING,&value,error_label);
188*0Sstevel@tonic-gate   if(list == NULL){
189*0Sstevel@tonic-gate 	error("sap_append_string_variable failed: oid: %s, value: %s\n"
190*0Sstevel@tonic-gate 		, SSAOidString(oid),str);
191*0Sstevel@tonic-gate   }
192*0Sstevel@tonic-gate   return(list);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
sap_append_oid_variable(SNMP_variable * list,Oid * oid,Oid * name)195*0Sstevel@tonic-gate SNMP_variable *sap_append_oid_variable(SNMP_variable *list, Oid *oid, Oid* name)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate   SNMP_value value;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate   if(oid == NULL || name == NULL) return NULL;
200*0Sstevel@tonic-gate   value.v_oid.subids = name->subids;
201*0Sstevel@tonic-gate   value.v_oid.len = name->len;
202*0Sstevel@tonic-gate   list = snmp_typed_variable_append(list,oid,OBJID,&value,error_label);
203*0Sstevel@tonic-gate   if(list == NULL){
204*0Sstevel@tonic-gate 	error("sap_append_oid_varaible(%s,%s) failed\n",
205*0Sstevel@tonic-gate 		SSAOidString(oid),SSAOidString(name));
206*0Sstevel@tonic-gate   }
207*0Sstevel@tonic-gate   return(list);
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 
reg_subagent_form_variables(Agent * agent,SNMP_variable * list)211*0Sstevel@tonic-gate SNMP_variable *reg_subagent_form_variables(Agent *agent,SNMP_variable *list)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate   struct tms buffer;
214*0Sstevel@tonic-gate   clock_t system_up_time;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate   if(agent == NULL){ return NULL; }
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate   /* set up the index */
219*0Sstevel@tonic-gate   reg_subagent_oid.subids[reg_subagent_oid.len-1] = agent->agent_id;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate   reg_subagent_oid.subids[reg_subagent_oid.len-2] = REG_AGENT_PROCESS_ID;
222*0Sstevel@tonic-gate   list = sap_append_integer_variable(list,&reg_subagent_oid,agent->process_id);
223*0Sstevel@tonic-gate   if(list == NULL) return list;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate   if(agent->agent_status!=0){
226*0Sstevel@tonic-gate     reg_subagent_oid.subids[reg_subagent_oid.len-2] = REG_AGENT_STATUS;
227*0Sstevel@tonic-gate     list = sap_append_integer_variable(list,&reg_subagent_oid,agent->agent_status);
228*0Sstevel@tonic-gate     if(list == NULL) return list;
229*0Sstevel@tonic-gate     reg_subagent_oid.subids[reg_subagent_oid.len-2] =
230*0Sstevel@tonic-gate 		REG_AGENT_SYSTEM_UP_TIME;
231*0Sstevel@tonic-gate     system_up_time = times(&buffer);
232*0Sstevel@tonic-gate     list = sap_append_variable(list, &reg_subagent_oid,
233*0Sstevel@tonic-gate 		/* LINTED */
234*0Sstevel@tonic-gate 		(uint32_t)system_up_time,
235*0Sstevel@tonic-gate 		TIMETICKS);
236*0Sstevel@tonic-gate     if(list == NULL) return list;
237*0Sstevel@tonic-gate   }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate   reg_subagent_oid.subids[reg_subagent_oid.len-2] = REG_AGENT_TIME_OUT;
240*0Sstevel@tonic-gate   list = sap_append_integer_variable(list,&reg_subagent_oid,agent->timeout);
241*0Sstevel@tonic-gate   if(list == NULL) return list;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate   reg_subagent_oid.subids[reg_subagent_oid.len-2] = REG_AGENT_PORT_NUMBER;
244*0Sstevel@tonic-gate   list = sap_append_integer_variable(list,&reg_subagent_oid,agent->address.sin_port);
245*0Sstevel@tonic-gate   if(list == NULL) return list;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate   if(agent->personal_file != NULL){
248*0Sstevel@tonic-gate   	reg_subagent_oid.subids[reg_subagent_oid.len-2] =
249*0Sstevel@tonic-gate 				REG_AGENT_PERSONAL_FILE;
250*0Sstevel@tonic-gate   	list = sap_append_string_variable(list,&reg_subagent_oid,agent->personal_file);
251*0Sstevel@tonic-gate   	if(list == NULL) return list;
252*0Sstevel@tonic-gate   }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate   if(agent->config_file != NULL) {
255*0Sstevel@tonic-gate   	reg_subagent_oid.subids[reg_subagent_oid.len-2] =
256*0Sstevel@tonic-gate 				REG_AGENT_CONFIG_FILE;
257*0Sstevel@tonic-gate   	list = sap_append_string_variable(list,&reg_subagent_oid,agent->config_file);
258*0Sstevel@tonic-gate   	if(list == NULL) return list;
259*0Sstevel@tonic-gate   }
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate   if(agent->executable != NULL) {
262*0Sstevel@tonic-gate   	reg_subagent_oid.subids[reg_subagent_oid.len-2] =
263*0Sstevel@tonic-gate 				REG_AGENT_EXECUTABLE;
264*0Sstevel@tonic-gate   	list = sap_append_string_variable(list,&reg_subagent_oid,agent->executable);
265*0Sstevel@tonic-gate   	if(list == NULL) return list;
266*0Sstevel@tonic-gate   }
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate   if(agent->version_string != NULL) {
269*0Sstevel@tonic-gate   	reg_subagent_oid.subids[reg_subagent_oid.len-2] =
270*0Sstevel@tonic-gate 				REG_AGENT_VERSION_NUM;
271*0Sstevel@tonic-gate   	list = sap_append_string_variable(list,&reg_subagent_oid,agent->version_string);
272*0Sstevel@tonic-gate   	if(list == NULL) return list;
273*0Sstevel@tonic-gate   }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate   if(agent->name != NULL) {
276*0Sstevel@tonic-gate   	reg_subagent_oid.subids[reg_subagent_oid.len-2] =
277*0Sstevel@tonic-gate 				REG_AGENT_NAME;
278*0Sstevel@tonic-gate   	list = sap_append_string_variable(list,&reg_subagent_oid,agent->name);
279*0Sstevel@tonic-gate   	if(list == NULL) return list;
280*0Sstevel@tonic-gate   }
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate   return(list);
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate  * temporary using the process id as the agent id
289*0Sstevel@tonic-gate  * set the following variables,
290*0Sstevel@tonic-gate  *	xtimeout
291*0Sstevel@tonic-gate  *	xportnumber
292*0Sstevel@tonic-gate  *	xpersonal file
293*0Sstevel@tonic-gate  *	xconfig file
294*0Sstevel@tonic-gate  *	executable
295*0Sstevel@tonic-gate  *	version number
296*0Sstevel@tonic-gate  *	protocol
297*0Sstevel@tonic-gate  *	xprocess id
298*0Sstevel@tonic-gate  *	xagentname
299*0Sstevel@tonic-gate  */
send_request_to_agent(SNMP_variable * list,int type,char * community,int port,struct timeval * timeout,IPAddress * agent_addr)300*0Sstevel@tonic-gate static SNMP_pdu *send_request_to_agent(SNMP_variable *list, int type,
301*0Sstevel@tonic-gate 		char* community, int port, struct timeval *timeout,
302*0Sstevel@tonic-gate 		IPAddress *agent_addr)
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate 	SNMP_pdu *request, *response=NULL;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 	error_label[0] = '\0';
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	request = request_create( (community==NULL?"public":community), type, error_label);
309*0Sstevel@tonic-gate 	if(request == NULL)
310*0Sstevel@tonic-gate 	{
311*0Sstevel@tonic-gate 		return NULL;
312*0Sstevel@tonic-gate 	}
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	if(list == NULL){
315*0Sstevel@tonic-gate 		snmp_pdu_free(request);
316*0Sstevel@tonic-gate 		error("SSARegSubagent failed\n");
317*0Sstevel@tonic-gate 		return NULL;
318*0Sstevel@tonic-gate 	}
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	request->first_variable = list;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	/* later change the timeout to user-provided number */
323*0Sstevel@tonic-gate 	response =
324*0Sstevel@tonic-gate 	     request_send_to_port_time_out_blocking(agent_addr, port,timeout, request, error_label);
325*0Sstevel@tonic-gate 	snmp_pdu_free(request);
326*0Sstevel@tonic-gate 	return response;
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate SNMP_pdu *
send_request_to_relay_agent(SNMP_variable * list,int type)330*0Sstevel@tonic-gate send_request_to_relay_agent(SNMP_variable *list, int type)
331*0Sstevel@tonic-gate {
332*0Sstevel@tonic-gate 	char *community = READ_COMM;
333*0Sstevel@tonic-gate 	struct timeval timeout;
334*0Sstevel@tonic-gate 	static int my_ip_address_initialized = False;
335*0Sstevel@tonic-gate 	static IPAddress my_ip_address;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	if (my_ip_address_initialized == False) {
338*0Sstevel@tonic-gate 		if (get_my_ip_address(&my_ip_address, error_label))
339*0Sstevel@tonic-gate 			return (NULL);
340*0Sstevel@tonic-gate 		my_ip_address_initialized = True;
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 	timeout.tv_sec = 100;
343*0Sstevel@tonic-gate 	timeout.tv_usec = 0;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	if (type == SET_REQ_MSG)
346*0Sstevel@tonic-gate 		community = WRITE_COMM;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	return (send_request_to_agent(list, type, community, SNMP_PORT,
349*0Sstevel@tonic-gate 		&timeout, &my_ip_address));
350*0Sstevel@tonic-gate }
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 
create_variable(Oid * name)353*0Sstevel@tonic-gate static SNMP_variable *create_variable(Oid *name)
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate 	SNMP_variable *new;
356*0Sstevel@tonic-gate 	if((new = snmp_variable_new(error_label)) == NULL)
357*0Sstevel@tonic-gate 		return NULL;
358*0Sstevel@tonic-gate 	if(SSAOidCpy(&(new->name),name,error_label))
359*0Sstevel@tonic-gate 	{
360*0Sstevel@tonic-gate 		snmp_variable_free(new);
361*0Sstevel@tonic-gate 		return NULL;
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 	new->type = NULLOBJ;
364*0Sstevel@tonic-gate  	return new;
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate int
sap_avail_index(Oid * name,int type)368*0Sstevel@tonic-gate sap_avail_index(Oid *name, int type)
369*0Sstevel@tonic-gate {
370*0Sstevel@tonic-gate   	SNMP_pdu *response;
371*0Sstevel@tonic-gate 	SNMP_variable *new, *variable;
372*0Sstevel@tonic-gate 	int idx = 0;
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 	if ((new = create_variable(name)) == NULL)
375*0Sstevel@tonic-gate 		return (idx);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	response = send_request_to_relay_agent(new,type);
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	if (response == NULL)
380*0Sstevel@tonic-gate 		return (idx);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	if (response->error_status) {
383*0Sstevel@tonic-gate 		(void)sprintf(error_label, "%s",
384*0Sstevel@tonic-gate 			error_status_string(response->error_status));
385*0Sstevel@tonic-gate 		snmp_pdu_free(response);
386*0Sstevel@tonic-gate 		return (idx);
387*0Sstevel@tonic-gate 	}
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	/* need checking the response */
390*0Sstevel@tonic-gate 	variable = response->first_variable;
391*0Sstevel@tonic-gate 	if (SSAOidCmp(&(variable->name), name)
392*0Sstevel@tonic-gate 		|| (variable->type != INTEGER)
393*0Sstevel@tonic-gate 		|| (variable->val.integer == NULL)
394*0Sstevel@tonic-gate 		|| (variable->val_len != sizeof (int)) ) {
395*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_BAD_RESPONSE);
396*0Sstevel@tonic-gate 		snmp_pdu_free(response);
397*0Sstevel@tonic-gate 		return (idx);
398*0Sstevel@tonic-gate 	}
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	idx = *(variable->val.integer);
401*0Sstevel@tonic-gate 	snmp_pdu_free(response);
402*0Sstevel@tonic-gate 	return (idx);
403*0Sstevel@tonic-gate }
404*0Sstevel@tonic-gate 
405*0Sstevel@tonic-gate /*
406*0Sstevel@tonic-gate  * send a request to an agent, return TRUE, if agent response
407*0Sstevel@tonic-gate  * otherwise, FALSE
408*0Sstevel@tonic-gate  */
probe_agent(Oid * name,int type,char * community,int port,struct timeval * timeout,IPAddress * agent_addr)409*0Sstevel@tonic-gate static int probe_agent(Oid *name, int type, char *community, int port,
410*0Sstevel@tonic-gate 		struct timeval *timeout,IPAddress *agent_addr)
411*0Sstevel@tonic-gate {
412*0Sstevel@tonic-gate   	SNMP_pdu *response;
413*0Sstevel@tonic-gate 	SNMP_variable *new, *variable;
414*0Sstevel@tonic-gate   	static int my_ip_address_initialized = False;
415*0Sstevel@tonic-gate   	static IPAddress my_ip_address;
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate   	if(my_ip_address_initialized == False)
418*0Sstevel@tonic-gate   	{
419*0Sstevel@tonic-gate 		if(get_my_ip_address(&my_ip_address, error_label))
420*0Sstevel@tonic-gate 		{
421*0Sstevel@tonic-gate 			return NULL;
422*0Sstevel@tonic-gate 		}
423*0Sstevel@tonic-gate 		my_ip_address_initialized = True;
424*0Sstevel@tonic-gate   	}
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	if( (new = create_variable(name)) == NULL )
427*0Sstevel@tonic-gate 		return -1;
428*0Sstevel@tonic-gate 	response = send_request_to_agent(new,type,community,port,timeout,
429*0Sstevel@tonic-gate 				agent_addr!=NULL?agent_addr:&my_ip_address);
430*0Sstevel@tonic-gate 	if(response == NULL)
431*0Sstevel@tonic-gate 	{
432*0Sstevel@tonic-gate 		return FALSE;
433*0Sstevel@tonic-gate 	}
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate   /* need checking the response */
437*0Sstevel@tonic-gate 	variable = response->first_variable;
438*0Sstevel@tonic-gate 	if(SSAOidCmp(&(variable->name), name) )
439*0Sstevel@tonic-gate 	{
440*0Sstevel@tonic-gate 		snmp_pdu_free(response);
441*0Sstevel@tonic-gate 		return FALSE;
442*0Sstevel@tonic-gate 	}
443*0Sstevel@tonic-gate 	snmp_pdu_free(response);
444*0Sstevel@tonic-gate 	return(TRUE);
445*0Sstevel@tonic-gate }
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate /*
448*0Sstevel@tonic-gate  * agent_addr == NULL => use the local host address
449*0Sstevel@tonic-gate  * community == NULL => public
450*0Sstevel@tonic-gate  */
451*0Sstevel@tonic-gate int
SSAAgentIsAlive(IPAddress * agent_addr,int port,char * community,struct timeval * timeout)452*0Sstevel@tonic-gate SSAAgentIsAlive(IPAddress *agent_addr, int port, char *community,
453*0Sstevel@tonic-gate 	struct timeval *timeout)
454*0Sstevel@tonic-gate {
455*0Sstevel@tonic-gate 	static Subid system_service_subids[] = {1, 3, 6, 1, 2, 1, 1, 7, 0};
456*0Sstevel@tonic-gate 	static Oid system_service_oid = { system_service_subids, 9};
457*0Sstevel@tonic-gate 		if (agent_read_config() == -1) {
458*0Sstevel@tonic-gate 			perror("Can not read configuration file ");
459*0Sstevel@tonic-gate 			return (-1);
460*0Sstevel@tonic-gate 		}
461*0Sstevel@tonic-gate 		community = snmp_fullmib_read_community;
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate return (probe_agent(&system_service_oid, GET_REQ_MSG,
464*0Sstevel@tonic-gate 			community != NULL ? community: "public",
465*0Sstevel@tonic-gate 			port, timeout, agent_addr));
466*0Sstevel@tonic-gate }
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate int
get_available_index_from_relay_agent()469*0Sstevel@tonic-gate get_available_index_from_relay_agent()
470*0Sstevel@tonic-gate {
471*0Sstevel@tonic-gate   return(sap_avail_index(&agent_tbl_index_oid,GET_REQ_MSG));
472*0Sstevel@tonic-gate }
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate int
bump_index_of_relay_agent(int num)475*0Sstevel@tonic-gate bump_index_of_relay_agent(int num)
476*0Sstevel@tonic-gate {
477*0Sstevel@tonic-gate   SNMP_variable *variable=NULL;
478*0Sstevel@tonic-gate   SNMP_pdu *response;
479*0Sstevel@tonic-gate   int idx = 0;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate   variable = sap_append_integer_variable(variable,&agent_tbl_index_oid,num++);
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate   if(variable == NULL)
484*0Sstevel@tonic-gate 	return  idx;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate   response = send_request_to_relay_agent(variable,SET_REQ_MSG);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate   if(response == NULL)
489*0Sstevel@tonic-gate 	return idx;
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate   if(response->error_status) {
492*0Sstevel@tonic-gate 	(void)sprintf(error_label, "%s",
493*0Sstevel@tonic-gate 		error_status_string(response->error_status));
494*0Sstevel@tonic-gate 	snmp_pdu_free(response);
495*0Sstevel@tonic-gate 	return idx;
496*0Sstevel@tonic-gate   }
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate   /* need checking the response */
499*0Sstevel@tonic-gate 	variable = response->first_variable;
500*0Sstevel@tonic-gate 	if(SSAOidCmp(&(variable->name), &agent_tbl_index_oid)
501*0Sstevel@tonic-gate 		|| (variable->type != INTEGER)
502*0Sstevel@tonic-gate 		|| (variable->val.integer == NULL)
503*0Sstevel@tonic-gate 		|| (variable->val_len != sizeof(int)) )
504*0Sstevel@tonic-gate 	{
505*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_BAD_RESPONSE);
506*0Sstevel@tonic-gate 		snmp_pdu_free(response);
507*0Sstevel@tonic-gate 		return idx;
508*0Sstevel@tonic-gate 	}
509*0Sstevel@tonic-gate   idx = *(variable->val.integer);
510*0Sstevel@tonic-gate   snmp_pdu_free(response);
511*0Sstevel@tonic-gate   return(idx);
512*0Sstevel@tonic-gate }
513*0Sstevel@tonic-gate 
check_dup_agent_name(char * agent_name)514*0Sstevel@tonic-gate int check_dup_agent_name(char *agent_name)
515*0Sstevel@tonic-gate {
516*0Sstevel@tonic-gate   SNMP_variable *variable=NULL;
517*0Sstevel@tonic-gate   SNMP_pdu *response;
518*0Sstevel@tonic-gate   int res=TRUE;
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate   variable = sap_append_string_variable(variable,&ra_check_point_oid,agent_name);
521*0Sstevel@tonic-gate   if(variable == NULL) return  res;
522*0Sstevel@tonic-gate   response = send_request_to_relay_agent(variable,SET_REQ_MSG);
523*0Sstevel@tonic-gate   if(response == NULL)
524*0Sstevel@tonic-gate   {
525*0Sstevel@tonic-gate 	return res;
526*0Sstevel@tonic-gate   }
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate   if(response->error_status)
529*0Sstevel@tonic-gate   {
530*0Sstevel@tonic-gate 	(void)sprintf(error_label, "%s",
531*0Sstevel@tonic-gate 		error_status_string(response->error_status));
532*0Sstevel@tonic-gate 	snmp_pdu_free(response);
533*0Sstevel@tonic-gate 	return res;
534*0Sstevel@tonic-gate   }
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate   /* need checking the response */
537*0Sstevel@tonic-gate 	variable = response->first_variable;
538*0Sstevel@tonic-gate 	if(SSAOidCmp(&(variable->name), &ra_check_point_oid)
539*0Sstevel@tonic-gate 		|| (variable->type != STRING)
540*0Sstevel@tonic-gate 		|| (variable->val.string == NULL)
541*0Sstevel@tonic-gate 		|| (variable->val_len == 0) )
542*0Sstevel@tonic-gate 	{
543*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_BAD_RESPONSE);
544*0Sstevel@tonic-gate 		snmp_pdu_free(response);
545*0Sstevel@tonic-gate 		return res;
546*0Sstevel@tonic-gate 	}
547*0Sstevel@tonic-gate   res = FALSE;
548*0Sstevel@tonic-gate   snmp_pdu_free(response);
549*0Sstevel@tonic-gate   /*snmp_variable_free(variable);this mem. is freed above*/
550*0Sstevel@tonic-gate   return(res);
551*0Sstevel@tonic-gate }
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate /*
555*0Sstevel@tonic-gate  * mechanism for getting agent id
556*0Sstevel@tonic-gate  * first, set relayCheckPoint to agent name
557*0Sstevel@tonic-gate  * second, if first successful(no agent name dup.), get the available id
558*0Sstevel@tonic-gate  */
559*0Sstevel@tonic-gate int
SSASubagentOpen(int num_of_retry,char * agent_name)560*0Sstevel@tonic-gate SSASubagentOpen(int num_of_retry, char* agent_name)
561*0Sstevel@tonic-gate {
562*0Sstevel@tonic-gate 	int i, index;
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	if (trace_level > 0)
565*0Sstevel@tonic-gate 		trace("SSASubagent called with %s\n", agent_name);
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	if (check_dup_agent_name(agent_name) == TRUE)
568*0Sstevel@tonic-gate 		return INVALID_HANDLER;
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	for (i = 0; i < num_of_retry; i++) {
571*0Sstevel@tonic-gate 		if ((index = get_available_index_from_relay_agent()) == 0)
572*0Sstevel@tonic-gate 			return INVALID_HANDLER;
573*0Sstevel@tonic-gate 		index++;
574*0Sstevel@tonic-gate 		if ((bump_index_of_relay_agent(index)) == index )
575*0Sstevel@tonic-gate 			return index - 1;
576*0Sstevel@tonic-gate 		else
577*0Sstevel@tonic-gate 			if (i > num_of_retry)
578*0Sstevel@tonic-gate 				return INVALID_HANDLER;
579*0Sstevel@tonic-gate 	}
580*0Sstevel@tonic-gate 	return INVALID_HANDLER;
581*0Sstevel@tonic-gate }
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate int
SSARegSubagent(Agent * agent)584*0Sstevel@tonic-gate SSARegSubagent(Agent* agent)
585*0Sstevel@tonic-gate {
586*0Sstevel@tonic-gate   SNMP_variable *list=NULL;
587*0Sstevel@tonic-gate   SNMP_pdu *response;
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate   /* form variable list */
590*0Sstevel@tonic-gate  	list = reg_subagent_form_variables(agent,list);
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	if(list == NULL){
593*0Sstevel@tonic-gate 		error("SSARegSubagent failed\n");
594*0Sstevel@tonic-gate 		return 0;
595*0Sstevel@tonic-gate   	}
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	response = send_request_to_relay_agent(list,SET_REQ_MSG);
598*0Sstevel@tonic-gate 	if(response == NULL)
599*0Sstevel@tonic-gate 		return 0;
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	if(response->error_status)
602*0Sstevel@tonic-gate 	{
603*0Sstevel@tonic-gate 		(void)sprintf(error_label, "%s",
604*0Sstevel@tonic-gate 			error_status_string(response->error_status));
605*0Sstevel@tonic-gate 		snmp_pdu_free(response);
606*0Sstevel@tonic-gate 		return 0;
607*0Sstevel@tonic-gate 	}
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 	snmp_pdu_free(response);
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	return 1;
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate /*
616*0Sstevel@tonic-gate  * register the subtree, index is AgentID.RegTreeIndex
617*0Sstevel@tonic-gate  * if the registration is not accepted by the relay agent,
618*0Sstevel@tonic-gate  * the error_status is non-zero
619*0Sstevel@tonic-gate  */
SSARegSubtree(SSA_Subtree * subtree)620*0Sstevel@tonic-gate int SSARegSubtree(SSA_Subtree *subtree)
621*0Sstevel@tonic-gate {
622*0Sstevel@tonic-gate   Oid *tree_oid = &(subtree->name);
623*0Sstevel@tonic-gate   int agent_id = subtree->regTreeAgentID;
624*0Sstevel@tonic-gate   SNMP_variable *list=NULL;
625*0Sstevel@tonic-gate   SNMP_pdu *response;
626*0Sstevel@tonic-gate   int idx = subtree->regTreeIndex;
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	/* form variable list */
629*0Sstevel@tonic-gate 	reg_tree_ra_oid.subids[reg_tree_ra_oid.len-1] = idx;
630*0Sstevel@tonic-gate 	reg_tree_ra_oid.subids[reg_tree_ra_oid.len-2] = agent_id;
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate         if (subtree->regTreeStatus != SSA_OPER_STATUS_NOT_IN_SERVICE) {
633*0Sstevel@tonic-gate 	reg_tree_ra_oid.subids[reg_tree_ra_oid.len-3] = REG_TREE_OID;
634*0Sstevel@tonic-gate   	list = sap_append_oid_variable(list,&reg_tree_ra_oid,tree_oid);
635*0Sstevel@tonic-gate 	if(list == NULL) return 0;
636*0Sstevel@tonic-gate         }
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	reg_tree_ra_oid.subids[reg_tree_ra_oid.len-3] = REG_TREE_STATUS;
639*0Sstevel@tonic-gate 	list = sap_append_integer_variable(list,&reg_tree_ra_oid,subtree->regTreeStatus);
640*0Sstevel@tonic-gate 	if(list == NULL) return 0;
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	response = send_request_to_relay_agent(list,SET_REQ_MSG);
643*0Sstevel@tonic-gate 	if(response == NULL)
644*0Sstevel@tonic-gate 	{
645*0Sstevel@tonic-gate 		return 0;
646*0Sstevel@tonic-gate 	}
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate 	if(response->error_status)
649*0Sstevel@tonic-gate 	{
650*0Sstevel@tonic-gate 		(void)sprintf(error_label, "%s",
651*0Sstevel@tonic-gate 			error_status_string(response->error_status));
652*0Sstevel@tonic-gate 		snmp_pdu_free(response);
653*0Sstevel@tonic-gate 		return 0;
654*0Sstevel@tonic-gate 	}
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate /* needs checking
657*0Sstevel@tonic-gate 	variable = response->first_variable;
658*0Sstevel@tonic-gate 	if(variable->next_variable
659*0Sstevel@tonic-gate 		|| SSAOidCmp(&(variable->name), &reg_tree_ra_oid)
660*0Sstevel@tonic-gate 		|| (variable->type != OBJID)
661*0Sstevel@tonic-gate 		|| (variable->val_len/sizeof(Subid) != tree_oid->len) )
662*0Sstevel@tonic-gate 	{
663*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_BAD_RESPONSE);
664*0Sstevel@tonic-gate 		snmp_pdu_free(response);
665*0Sstevel@tonic-gate 		return 0;
666*0Sstevel@tonic-gate 	}
667*0Sstevel@tonic-gate */
668*0Sstevel@tonic-gate 	snmp_pdu_free(response);
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate 	return idx;
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate }
673*0Sstevel@tonic-gate 
SSARegSubtable(SSA_Table * table)674*0Sstevel@tonic-gate int SSARegSubtable(SSA_Table *table)
675*0Sstevel@tonic-gate {
676*0Sstevel@tonic-gate   Oid *table_oid = &(table->regTblOID);
677*0Sstevel@tonic-gate   int agent_id = table->regTblAgentID;
678*0Sstevel@tonic-gate   SNMP_variable *list=NULL;
679*0Sstevel@tonic-gate   SNMP_pdu *response;
680*0Sstevel@tonic-gate   int idx = table->regTblIndex;
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate 	/* form variable list */
683*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-1] = idx;
684*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-2] = agent_id;
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-3] = REG_TBL_OID;
687*0Sstevel@tonic-gate   	list = sap_append_oid_variable(list,&reg_shared_table_oid,table_oid);
688*0Sstevel@tonic-gate 	if(list == NULL) return 0;
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-3] = REG_TBL_SCOL;
691*0Sstevel@tonic-gate   	list = sap_append_integer_variable(list,&reg_shared_table_oid,table->regTblStartColumn);
692*0Sstevel@tonic-gate 	if(list == NULL) return 0;
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-3] = REG_TBL_ECOL;
695*0Sstevel@tonic-gate   	list = sap_append_integer_variable(list,&reg_shared_table_oid,table->regTblEndColumn);
696*0Sstevel@tonic-gate 	if(list == NULL) return 0;
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-3] = REG_TBL_SROW;
699*0Sstevel@tonic-gate   	list = sap_append_integer_variable(list,&reg_shared_table_oid,table->regTblStartRow);
700*0Sstevel@tonic-gate 	if(list == NULL) return 0;
701*0Sstevel@tonic-gate 
702*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-3] = REG_TBL_EROW;
703*0Sstevel@tonic-gate   	list = sap_append_integer_variable(list,&reg_shared_table_oid,table->regTblEndRow);
704*0Sstevel@tonic-gate 	if(list == NULL) return 0;
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	reg_shared_table_oid.subids[reg_shared_table_oid.len-3] = REG_TBL_STATUS;
708*0Sstevel@tonic-gate   	list = sap_append_integer_variable(list,&reg_shared_table_oid,table->regTblStatus);
709*0Sstevel@tonic-gate 	if(list == NULL) return 0;
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate 	response = send_request_to_relay_agent(list,SET_REQ_MSG);
712*0Sstevel@tonic-gate 	if(response == NULL)
713*0Sstevel@tonic-gate 	{
714*0Sstevel@tonic-gate 		return 0;
715*0Sstevel@tonic-gate 	}
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	if(response->error_status)
718*0Sstevel@tonic-gate 	{
719*0Sstevel@tonic-gate 		(void)sprintf(error_label, "%s",
720*0Sstevel@tonic-gate 			error_status_string(response->error_status));
721*0Sstevel@tonic-gate 		snmp_pdu_free(response);
722*0Sstevel@tonic-gate 		return 0;
723*0Sstevel@tonic-gate 	}
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate /* needs checking
726*0Sstevel@tonic-gate 	variable = response->first_variable;
727*0Sstevel@tonic-gate 	if(variable->next_variable
728*0Sstevel@tonic-gate 		|| SSAOidCmp(&(variable->name), &reg_shared_table_oid)
729*0Sstevel@tonic-gate 		|| (variable->type != OBJID)
730*0Sstevel@tonic-gate 		|| (variable->val_len/sizeof(Subid) != table_oid->len) )
731*0Sstevel@tonic-gate 	{
732*0Sstevel@tonic-gate 		(void)sprintf(error_label, ERR_MSG_BAD_RESPONSE);
733*0Sstevel@tonic-gate 		snmp_pdu_free(response);
734*0Sstevel@tonic-gate 		return 0;
735*0Sstevel@tonic-gate 	}
736*0Sstevel@tonic-gate */
737*0Sstevel@tonic-gate 	snmp_pdu_free(response);
738*0Sstevel@tonic-gate 
739*0Sstevel@tonic-gate 	return idx;
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate }
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 
SSAGetTrapPort()744*0Sstevel@tonic-gate int SSAGetTrapPort() {
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 	if (dont_read_config_file == TRUE)
747*0Sstevel@tonic-gate 		return (SNMP_TRAP_PORT);
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 	/* not agent_tbl_index_oid, but trap_port_number */
750*0Sstevel@tonic-gate return (sap_avail_index(&ra_trap_port_oid, GET_REQ_MSG));
751*0Sstevel@tonic-gate }
752*0Sstevel@tonic-gate 
search_trap_num(char * name)753*0Sstevel@tonic-gate static int search_trap_num(char* name)
754*0Sstevel@tonic-gate {
755*0Sstevel@tonic-gate  int idx;
756*0Sstevel@tonic-gate  if(!name) return -1;
757*0Sstevel@tonic-gate  for(idx=0;idx<numTrapElem;idx++)
758*0Sstevel@tonic-gate  	if(!strcmp(name,trapBucket[idx].name)) return idx;
759*0Sstevel@tonic-gate  return -1;
760*0Sstevel@tonic-gate }
761*0Sstevel@tonic-gate 
_SSASendTrap(char * name)762*0Sstevel@tonic-gate int _SSASendTrap(char *name)
763*0Sstevel@tonic-gate {
764*0Sstevel@tonic-gate 	int mode=1;
765*0Sstevel@tonic-gate 	return _SSASendTrap_generic(name, mode);
766*0Sstevel@tonic-gate }
767*0Sstevel@tonic-gate 
_SSASendTrap2(char * name)768*0Sstevel@tonic-gate int _SSASendTrap2(char *name)
769*0Sstevel@tonic-gate {
770*0Sstevel@tonic-gate 	int mode=2;
771*0Sstevel@tonic-gate 	return _SSASendTrap_generic(name, mode);
772*0Sstevel@tonic-gate }
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate /* For arbitrary length enterprise OID in traps - bug 4133978 */
_SSASendTrap3(char * name)775*0Sstevel@tonic-gate int _SSASendTrap3(char *name)
776*0Sstevel@tonic-gate {
777*0Sstevel@tonic-gate         int mode=3;
778*0Sstevel@tonic-gate         return _SSASendTrap_generic(name, mode);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate /* Specific index of trap var bindings */
_SSASendTrap4(char * name,IndexType * pindex_obj)782*0Sstevel@tonic-gate int _SSASendTrap4(char *name, IndexType *pindex_obj)
783*0Sstevel@tonic-gate {
784*0Sstevel@tonic-gate 	int mode=3;  /* shouldn't change */
785*0Sstevel@tonic-gate 	return _SSASendTrap_generic2(name, mode, pindex_obj);
786*0Sstevel@tonic-gate }
787*0Sstevel@tonic-gate 
_SSASendTrap_generic(char * name,int mode)788*0Sstevel@tonic-gate static int _SSASendTrap_generic(char *name, int mode)
789*0Sstevel@tonic-gate {
790*0Sstevel@tonic-gate 	IPAddress dest_ip_address, my_ip_address;
791*0Sstevel@tonic-gate 	int generic;
792*0Sstevel@tonic-gate 	int specific;
793*0Sstevel@tonic-gate 	uint32_t time_stamp;
794*0Sstevel@tonic-gate 	int trap_port = SSAGetTrapPort();
795*0Sstevel@tonic-gate 	SNMP_variable *variables=NULL;
796*0Sstevel@tonic-gate 	Integer val_integer;
797*0Sstevel@tonic-gate 	String val_str;
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 	Oid val_oid, ent_oid;
800*0Sstevel@tonic-gate 	Subid ent_subids[MAX_OID_LEN];
801*0Sstevel@tonic-gate 	int num;
802*0Sstevel@tonic-gate 	Object *ptr;
803*0Sstevel@tonic-gate 	int trapNum;
804*0Sstevel@tonic-gate 	int i;
805*0Sstevel@tonic-gate 	Subid tempArray[MAX_OID_LEN],*oldArray;
806*0Sstevel@tonic-gate 	int oidLen, j;
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	if(!trapBucket) return -1;
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	if((trapNum=search_trap_num(name))==-1) return -1;
811*0Sstevel@tonic-gate 
812*0Sstevel@tonic-gate 	if(get_my_ip_address(&dest_ip_address,error_label)== -1) return -1;
813*0Sstevel@tonic-gate 	if(get_my_ip_address(&my_ip_address,error_label)== -1) return -1;
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	generic = trapBucket[trapNum].generic;
816*0Sstevel@tonic-gate 	specific = trapBucket[trapNum].specific;
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate 	num = trapTableMap[trapNum];
819*0Sstevel@tonic-gate 	ptr=callItem[num].ptr;
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 	if ( mode == 2 ) { /*_SSASendTrap2*/
823*0Sstevel@tonic-gate 		ent_oid.subids = ent_subids;
824*0Sstevel@tonic-gate 		ent_oid.len = sun_oid.len;
825*0Sstevel@tonic-gate 		for (i=0;i<7;i++)
826*0Sstevel@tonic-gate 			ent_subids[i]=trapEnterpriseInfo[trapNum].subids[i];
827*0Sstevel@tonic-gate 	}
828*0Sstevel@tonic-gate 
829*0Sstevel@tonic-gate 	/* Handling arbitrary length OIDs. Retaining _SSASendTrap2 to maintain
830*0Sstevel@tonic-gate 	   backward compatibility with subagents created using old mibcodegen
831*0Sstevel@tonic-gate 	   Bug 4133978 */
832*0Sstevel@tonic-gate 	if ( mode == 3 ) { /*_SSASendTrap3*/
833*0Sstevel@tonic-gate 		Subid k;
834*0Sstevel@tonic-gate 		ent_oid.subids = ent_subids;
835*0Sstevel@tonic-gate 		for (i=0;(k=trapAnyEnterpriseInfo[trapNum].subids[i]) !=-1;i++)
836*0Sstevel@tonic-gate 			ent_subids[i]=k;
837*0Sstevel@tonic-gate 		ent_oid.len = i;
838*0Sstevel@tonic-gate 	}
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 	while(num != -1 && ptr){
841*0Sstevel@tonic-gate 
842*0Sstevel@tonic-gate 		/* Appending a zero at the end: Bug ID 4103570*/
843*0Sstevel@tonic-gate 		oldArray=ptr->name.subids;
844*0Sstevel@tonic-gate 		oidLen = ptr->name.len;
845*0Sstevel@tonic-gate 		for(j=0;j<oidLen;j++)
846*0Sstevel@tonic-gate 			tempArray[j]=ptr->name.subids[j];
847*0Sstevel@tonic-gate 		tempArray[oidLen]=0;
848*0Sstevel@tonic-gate 		ptr->name.len=oidLen+1;
849*0Sstevel@tonic-gate 		ptr->name.subids=&(tempArray[0]);
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	     	if((ptr->asn1_type == INTEGER) | (ptr->asn1_type == GAUGE) | (ptr->asn1_type == COUNTER) | (ptr->asn1_type == TIMETICKS)){
852*0Sstevel@tonic-gate 			ptr->get(&val_integer);
853*0Sstevel@tonic-gate 			variables = ssa_append_integer_variable(variables,&(ptr->name),val_integer,error_label, ptr->asn1_type);
854*0Sstevel@tonic-gate 		}else if(ptr->asn1_type == STRING){
855*0Sstevel@tonic-gate 			ptr->get(&val_str);
856*0Sstevel@tonic-gate 			variables = ssa_append_string_variable(variables,&(ptr->name),val_str,error_label);
857*0Sstevel@tonic-gate 			/* Memory leak fix */
858*0Sstevel@tonic-gate 			free((void *)(val_str.chars));
859*0Sstevel@tonic-gate 
860*0Sstevel@tonic-gate 		}else if(ptr->asn1_type == OBJID){
861*0Sstevel@tonic-gate 			ptr->get(&val_oid);
862*0Sstevel@tonic-gate 			variables = ssa_append_oid_variable(variables,&(ptr->name),val_oid,error_label);
863*0Sstevel@tonic-gate 			/* Memory leak fix */
864*0Sstevel@tonic-gate 			free((void *)(val_oid.subids));
865*0Sstevel@tonic-gate 		}
866*0Sstevel@tonic-gate 		ptr->name.len=oidLen;
867*0Sstevel@tonic-gate 		ptr->name.subids=oldArray;
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 		num = callItem[num].next;
870*0Sstevel@tonic-gate                 if(num <0 ) ptr=NULL;
871*0Sstevel@tonic-gate                    else ptr = callItem[num].ptr;
872*0Sstevel@tonic-gate 	}
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate 	time_stamp = -1U;
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	if (trap_send_with_more_para(&dest_ip_address,
877*0Sstevel@tonic-gate 		my_ip_address, NULL, 1, &ent_oid,
878*0Sstevel@tonic-gate 		generic, specific, trap_port,
879*0Sstevel@tonic-gate 		time_stamp, variables, error_label))
880*0Sstevel@tonic-gate 			(void)printf("trap_send fails!\n");
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	/* Memory leak fix */
883*0Sstevel@tonic-gate 	snmp_variable_list_free(variables);
884*0Sstevel@tonic-gate 	return 0;
885*0Sstevel@tonic-gate }
886*0Sstevel@tonic-gate 
_SSASendTrap_generic2(char * name,int mode,IndexType * pindex_obj)887*0Sstevel@tonic-gate static int _SSASendTrap_generic2(char *name, int mode, IndexType *pindex_obj)
888*0Sstevel@tonic-gate {
889*0Sstevel@tonic-gate 	IPAddress dest_ip_address, my_ip_address;
890*0Sstevel@tonic-gate 	int generic;
891*0Sstevel@tonic-gate 	int specific;
892*0Sstevel@tonic-gate 	uint32_t time_stamp;
893*0Sstevel@tonic-gate 	int trap_port = SSAGetTrapPort();
894*0Sstevel@tonic-gate 	SNMP_variable *variables=NULL;
895*0Sstevel@tonic-gate 	Integer val_integer;
896*0Sstevel@tonic-gate 	String val_str={NULL,0};
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	Oid val_oid, ent_oid;
899*0Sstevel@tonic-gate 	Subid ent_subids[MAX_OID_LEN];
900*0Sstevel@tonic-gate 	int num;
901*0Sstevel@tonic-gate 	Object *ptr;
902*0Sstevel@tonic-gate 	int trapNum;
903*0Sstevel@tonic-gate 	int i;
904*0Sstevel@tonic-gate         int n;
905*0Sstevel@tonic-gate 	Subid tempArray[MAX_OID_LEN],*oldArray;
906*0Sstevel@tonic-gate 	int oidLen, j;
907*0Sstevel@tonic-gate         int length;
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	if(!trapBucket) return -1;
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate 	if((trapNum=search_trap_num(name))==-1) return -1;
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate 	if(get_my_ip_address(&dest_ip_address,error_label)== -1) return -1;
914*0Sstevel@tonic-gate 	if(get_my_ip_address(&my_ip_address,error_label)== -1) return -1;
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 	generic = trapBucket[trapNum].generic;
917*0Sstevel@tonic-gate 	specific = trapBucket[trapNum].specific;
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 	num = trapTableMap[trapNum];
920*0Sstevel@tonic-gate 	ptr=callItem[num].ptr;
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate 	if ( mode == 2 ) { /*_SSASendTrap2*/
924*0Sstevel@tonic-gate 		ent_oid.subids = ent_subids;
925*0Sstevel@tonic-gate 		ent_oid.len = sun_oid.len;
926*0Sstevel@tonic-gate 		for (i=0;i<7;i++)
927*0Sstevel@tonic-gate 			ent_subids[i]=trapEnterpriseInfo[trapNum].subids[i];
928*0Sstevel@tonic-gate 	}
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	/* Handling arbitrary length OIDs. Retaining _SSASendTrap2 to maintain
931*0Sstevel@tonic-gate 	   backward compatibility with subagents created using old mibcodegen
932*0Sstevel@tonic-gate 	   Bug 4133978 */
933*0Sstevel@tonic-gate 	if ( mode == 3 ) { /*_SSASendTrap3*/
934*0Sstevel@tonic-gate 		Subid k;
935*0Sstevel@tonic-gate 		ent_oid.subids = ent_subids;
936*0Sstevel@tonic-gate 		for (i=0;(k=trapAnyEnterpriseInfo[trapNum].subids[i]) !=-1;i++)
937*0Sstevel@tonic-gate 			ent_subids[i]=k;
938*0Sstevel@tonic-gate 		ent_oid.len = i;
939*0Sstevel@tonic-gate 	}
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate         n=0;  /* index to an array of an indices. Point to the first index */
942*0Sstevel@tonic-gate 	while(num != -1 && ptr){
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 		/* Appending a zero at the end: Bug ID 4103570*/
945*0Sstevel@tonic-gate 		oldArray=ptr->name.subids;
946*0Sstevel@tonic-gate 		oidLen = ptr->name.len;
947*0Sstevel@tonic-gate 		for(j=0;j<oidLen;j++)
948*0Sstevel@tonic-gate 			tempArray[j]=ptr->name.subids[j];
949*0Sstevel@tonic-gate 		tempArray[oidLen]=0;
950*0Sstevel@tonic-gate 		ptr->name.len=oidLen+1;
951*0Sstevel@tonic-gate                 length = (ptr->name.len -1); /* for non-scalars don't add trailing yet */
952*0Sstevel@tonic-gate 		ptr->name.subids=&(tempArray[0]);
953*0Sstevel@tonic-gate 	        if (ptr->type == 1) {              /* scalar value */
954*0Sstevel@tonic-gate 			if((ptr->asn1_type == INTEGER) | (ptr->asn1_type == GAUGE) | (ptr->asn1_type == COUNTER) | (ptr->asn1_type == TIMETICKS)){
955*0Sstevel@tonic-gate 				ptr->get(&val_integer);
956*0Sstevel@tonic-gate 				variables = ssa_append_integer_variable(variables,&(ptr->name),val_integer,error_label,ptr->asn1_type);
957*0Sstevel@tonic-gate                                 n++;  /* Yes, even scalars need a dummy index */
958*0Sstevel@tonic-gate 			}else if(ptr->asn1_type == STRING){
959*0Sstevel@tonic-gate 				ptr->get(&val_str);
960*0Sstevel@tonic-gate 				variables = ssa_append_string_variable(variables,&(ptr->name),val_str,error_label);
961*0Sstevel@tonic-gate 				/* Memory leak fix */
962*0Sstevel@tonic-gate 				free((void *)(val_str.chars));
963*0Sstevel@tonic-gate                                 n++;
964*0Sstevel@tonic-gate 			}else if(ptr->asn1_type == OBJID){
965*0Sstevel@tonic-gate 				ptr->get(&val_oid);
966*0Sstevel@tonic-gate 				variables = ssa_append_oid_variable(variables,&(ptr->name),val_oid,error_label);
967*0Sstevel@tonic-gate 				/* Memory leak fix */
968*0Sstevel@tonic-gate 				free((void *)(val_oid.subids));
969*0Sstevel@tonic-gate                                 n++;
970*0Sstevel@tonic-gate 			}
971*0Sstevel@tonic-gate                	} else                /* tabular value = 2 */
972*0Sstevel@tonic-gate                       {  if ((ptr->asn1_type == INTEGER) | (ptr->asn1_type == GAUGE) | (ptr->asn1_type == COUNTER) | (ptr->asn1_type == TIMETICKS)){
973*0Sstevel@tonic-gate                                ptr->get(EXACT_ENTRY,&val_integer,&pindex_obj[n]);
974*0Sstevel@tonic-gate                                for (i=0; i<= pindex_obj[n].len; i++)
975*0Sstevel@tonic-gate                                     ptr->name.subids[length+i] = pindex_obj[n].value[i];
976*0Sstevel@tonic-gate                                length = length + pindex_obj[n].len;
977*0Sstevel@tonic-gate                                ptr->name.len = length;
978*0Sstevel@tonic-gate                                ptr->name.subids[length] = 0;  /* append the trailing Zero */
979*0Sstevel@tonic-gate                                variables = ssa_append_integer_variable(variables,&(ptr->name),val_integer,error_label,ptr->asn1_type);
980*0Sstevel@tonic-gate                                n++; /* done with this index, increment to index for next trap variable */
981*0Sstevel@tonic-gate                        	 }else if(ptr->asn1_type == STRING){
982*0Sstevel@tonic-gate                                ptr->get(EXACT_ENTRY,&val_str,&pindex_obj[n]);
983*0Sstevel@tonic-gate                                for (i=0; i<= pindex_obj[n].len; i++)
984*0Sstevel@tonic-gate                                     ptr->name.subids[length+i] = pindex_obj[n].value[i];
985*0Sstevel@tonic-gate                                length = length + pindex_obj[n].len;
986*0Sstevel@tonic-gate                                ptr->name.len = length;
987*0Sstevel@tonic-gate                                ptr->name.subids[length] = 0;  /* append the trailing Zero */
988*0Sstevel@tonic-gate                                variables = ssa_append_string_variable(variables,&(ptr->name),val_str,error_label);
989*0Sstevel@tonic-gate 	                       /* Memory leak fix */
990*0Sstevel@tonic-gate                                free((void *)(val_str.chars));
991*0Sstevel@tonic-gate                                n++;   /* index for next trap variable */
992*0Sstevel@tonic-gate                          }else if(ptr->asn1_type == OBJID){
993*0Sstevel@tonic-gate                                ptr->get(EXACT_ENTRY,&val_oid,&pindex_obj[n]);
994*0Sstevel@tonic-gate                                for (i=0; i<= pindex_obj[n].len; i++)
995*0Sstevel@tonic-gate                                     ptr->name.subids[length+i] = pindex_obj[n].value[i];
996*0Sstevel@tonic-gate                                length = length + pindex_obj[n].len;
997*0Sstevel@tonic-gate                                ptr->name.len = length;
998*0Sstevel@tonic-gate                                ptr->name.subids[length] = 0;  /* append the trailing Zero */
999*0Sstevel@tonic-gate                                variables = ssa_append_oid_variable(variables,&(ptr->name),val_oid,error_label);
1000*0Sstevel@tonic-gate                                 /* Memory leak fix */
1001*0Sstevel@tonic-gate                                 free((void *)(val_oid.subids));
1002*0Sstevel@tonic-gate                                 n++;  /* index for next trap variable */
1003*0Sstevel@tonic-gate                         }
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate                }  /* else */
1006*0Sstevel@tonic-gate 
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 		ptr->name.len=oidLen;
1009*0Sstevel@tonic-gate 		ptr->name.subids=oldArray;
1010*0Sstevel@tonic-gate 
1011*0Sstevel@tonic-gate 		num = callItem[num].next;
1012*0Sstevel@tonic-gate                 if(num <0 ) ptr=NULL;
1013*0Sstevel@tonic-gate                    else ptr = callItem[num].ptr;
1014*0Sstevel@tonic-gate 	}
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate 	time_stamp = -1U;
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	if (trap_send_with_more_para(&dest_ip_address,
1019*0Sstevel@tonic-gate 		my_ip_address, NULL, 1, &ent_oid,
1020*0Sstevel@tonic-gate 		generic, specific, trap_port,
1021*0Sstevel@tonic-gate 		time_stamp, variables, error_label))
1022*0Sstevel@tonic-gate 			(void)printf("trap_send fails!\n");
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate 	/* Memory leak fix */
1025*0Sstevel@tonic-gate 	snmp_variable_list_free(variables);
1026*0Sstevel@tonic-gate 	return 0;
1027*0Sstevel@tonic-gate }
1028*0Sstevel@tonic-gate /*
1029*0Sstevel@tonic-gate  * This function is similar to one used in mibiisa
1030*0Sstevel@tonic-gate  * Here, we check the community strings from the snmpd.conf configuration file.
1031*0Sstevel@tonic-gate  */
1032*0Sstevel@tonic-gate int
agent_read_config()1033*0Sstevel@tonic-gate agent_read_config()
1034*0Sstevel@tonic-gate {
1035*0Sstevel@tonic-gate 	FILE	*hostf;
1036*0Sstevel@tonic-gate 	char	linebuff[256];
1037*0Sstevel@tonic-gate 	struct cmd *cmd;
1038*0Sstevel@tonic-gate 
1039*0Sstevel@tonic-gate 	if ((hostf = fopen(agent_config_file, "r")) == (FILE *)NULL) {
1040*0Sstevel@tonic-gate 		perror("Can not open agent configuration file");
1041*0Sstevel@tonic-gate 		return (-1);
1042*0Sstevel@tonic-gate 	}
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate 	for (;;) {
1045*0Sstevel@tonic-gate 		char *ccp;
1046*0Sstevel@tonic-gate 		int cmd_len;
1047*0Sstevel@tonic-gate 
1048*0Sstevel@tonic-gate 	(void) fgets(linebuff, sizeof (linebuff), hostf);
1049*0Sstevel@tonic-gate 	if (feof(hostf) || ferror(hostf))
1050*0Sstevel@tonic-gate 		break;
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate 		/* Weed out any comment text */
1053*0Sstevel@tonic-gate 		if ((ccp = strchr(linebuff, '#')) != (char *)NULL)
1054*0Sstevel@tonic-gate 			*ccp = '\0';
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 	/* Zap the newline, if any */
1057*0Sstevel@tonic-gate 	if ((ccp = strchr(linebuff, '\n')) != (char *)NULL)
1058*0Sstevel@tonic-gate 		*ccp = '\0';
1059*0Sstevel@tonic-gate 	if ((linebuff[0] == '\0') || (linebuff[0] == '\n')) continue;
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	/* Parse off the command name */
1062*0Sstevel@tonic-gate 	cmd_len = strcspn(linebuff, " \t");
1063*0Sstevel@tonic-gate 	ccp = linebuff + cmd_len + strspn(linebuff + cmd_len, " \t");
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 	/* Look up the command */
1066*0Sstevel@tonic-gate 	for (cmd = cmds; cmd->name; cmd++) {
1067*0Sstevel@tonic-gate 		if ((strlen(cmd->name) == cmd_len) &&
1068*0Sstevel@tonic-gate 		(strncmp(linebuff, cmd->name, cmd_len) == 0)) {
1069*0Sstevel@tonic-gate 			(*cmd->rtn)(ccp, cmd);
1070*0Sstevel@tonic-gate 			goto next;
1071*0Sstevel@tonic-gate 		}
1072*0Sstevel@tonic-gate 	}
1073*0Sstevel@tonic-gate 
1074*0Sstevel@tonic-gate 	next:
1075*0Sstevel@tonic-gate 	;
1076*0Sstevel@tonic-gate 	}
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate fclose(hostf);
1079*0Sstevel@tonic-gate return (0);
1080*0Sstevel@tonic-gate }
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate int
set_conf_word(string,cmd)1083*0Sstevel@tonic-gate set_conf_word(string, cmd)
1084*0Sstevel@tonic-gate char *string;
1085*0Sstevel@tonic-gate struct cmd *cmd;
1086*0Sstevel@tonic-gate {
1087*0Sstevel@tonic-gate 	char *ccp;
1088*0Sstevel@tonic-gate 	/* Take only the first word */
1089*0Sstevel@tonic-gate 	if ((ccp = strchr(string, ' ')) != (char *)NULL)
1090*0Sstevel@tonic-gate 	    *ccp = '\0';
1091*0Sstevel@tonic-gate 	if ((ccp = strchr(string, '\t')) != (char *)NULL)
1092*0Sstevel@tonic-gate 	    *ccp = '\0';
1093*0Sstevel@tonic-gate 	strncpy(cmd->arg1, string, cmd->arg2);
1094*0Sstevel@tonic-gate return (1);
1095*0Sstevel@tonic-gate }
1096