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