xref: /onnv-gate/usr/src/cmd/agents/snmp/agent/personal.y (revision 0:68f95e015346)
1 %{
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or http://www.opensolaris.org/os/licensing.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  *
23  * Copyright 1998 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 %}
30 
31 %start configuration
32 
33 %token NUMBER
34 %token MACROS
35 %token EQUAL
36 %token OPENBRACKET
37 %token CLOSEBRACKET
38 %token IDENTIFIER
39 %token MIB2
40 %token SUN
41 %token ENTERPRISE
42 %token DOT
43 %token AGENTS
44 %token NAME
45 %token SUBTREES
46 %token TABLES
47 %token TABLE
48 %token COLUMNS
49 %token INDEXS
50 %token TIMEOUT
51 %token PORT
52 %token QUOTEDSTRING
53 %token COMA
54 %token MINUS
55 %token OPENSQUAREBRACKET
56 %token CLOSESQUAREBRACKET
57 %token WATCHDOGTIME
58 
59 /* support SNMP security(5-13-96) */
60 %token COMMUNITIES
61 %token READONLY
62 %token READWRITE
63 %token MANAGERS
64 %token TRAPCOMMUNITY
65 %token TRAPDESTINATORS
66 %token ACL
67 %token ACCESS
68 %token TRAPNUM
69 %token HOSTS
70 %token TRAP
71 
72 %{
73 #include <unistd.h>
74 #include <stdio.h>
75 #include <sys/types.h>
76 #include <sys/stat.h>
77 #include <sys/mman.h>
78 #include <sys/socket.h>
79 #include <netinet/in.h>
80 #include <arpa/inet.h>
81 #include <fcntl.h>
82 #include <dirent.h>
83 #include <string.h>
84 
85 #include "impl.h"
86 #include "error.h"
87 #include "trace.h"
88 #include "pdu.h"
89 
90 #include "pagent.h"
91 #include "subtree.h"
92 
93 /** SNMP security (5-13-96) */
94 #include "trap.h"
95 #include "agent_msg.h"
96 #include "access.h"
97 #include "snmpd.h"
98 
99 
100 /***** DEFINE *****/
101 
102 /*
103 #define DEBUG_YACC(string) printf("\t\tYACC: %s: %s at line %d\n", string, yytext, yylineno);
104 */
105 #define DEBUG_YACC(string)
106 
107 #define SNMPRELAY_SUFFIX	".snmprelay"
108 
109 
110 /***** TYPEDEF *****/
111 
112 typedef struct _Macro {
113 	struct _Macro *next_macro;
114 	char *label;
115 	Oid name;
116 } Macro;
117 
118 #include "table.h"
119 static int table_index=0;
120 
121 
122 /*
123 typedef struct _Table {
124 	struct _Table *next_table;
125 	Agent *agent;
126 	Oid name;
127 	Subid regTblStartColumn;
128 	Subid regTblEndColumn;
129 	Subid regTblStartRow;
130 	Subid regTblEndRow;
131 } Table;
132 */
133 
134 
135 
136 /***** STATIC VARIABLES AND FUNCTIONS *****/
137 
138 /* access control(6-20-96) */
139 static AccessServer *static_access_server=NULL;
140 static AccessPolicy *static_access_policy=NULL;
141 static Community *static_community=NULL;
142 
143 /* trap filter (6-25-96) */
144 static SubMember *static_sub_member=NULL;
145 static Manager *static_host=NULL;
146 static EFilter *static_filter=NULL;
147 static int static_trap_low=-1;
148 static int static_trap_high=-1;
149 
150 /* snmp security(5-13-96) */
151 static int community_type = 0;
152 static char *current_filename = NULL;
153 
154 /* lexinput points to the current focus point in the config file */
155 static char *lexinput;
156 
157 /* first_macro is the begining of the list	*/
158 /* of the user defined macros			*/
159 
160 static Macro *first_macro = NULL;
161 
162 
163 /* first_table is the begining of the list	*/
164 /* of tables supported by the agents		*/
165 
166 static Table *first_table = NULL;
167 static Table *last_table = NULL;
168 
169 
170 /* the boolean parsing_oid is used to		*/
171 /* know if we are parsing an			*/
172 /* object identifier or not.			*/
173 
174 static int parsing_oid = False;
175 
176 
177 /* here are the values of the predifined macros	*/
178 /* that can be used in the configuration file	*/
179 
180 static Subid subids_mib2[] = { 1, 3, 6, 1, 2, 1 };
181 static int mib2_len = 6;
182 static Subid subids_enterprise[] = { 1, 3, 6, 1, 4, 1 };
183 static int enterprise_len = 6;
184 static Subid subids_sun[] = { 1, 3, 6, 1, 4, 1, 42 };
185 static int sun_len = 7;
186 
187 
188 /* the 2 static variables static_subids and	*/
189 /* static_len are used when parsing an	*/
190 /* object identifier. Then the boolean		*/
191 /* parsing_oid should be true. When a new sub-	*/
192 /* identifier (or a list of sub-identifiers)	*/
193 /* is found, we use the function		*/
194 /* subids_cat to append it to the static_subids.*/
195 
196 static Subid *static_subids = NULL;
197 static int static_len = 0;
198 static int subids_cat(Subid *subids, int len);
199 
200 
201 /* macro_add() is used to append a macro to	*/
202 /* the macro list. macro_find is used to find	*/
203 /* a macro in the macro list			*/
204 
205 static Macro *macro_add(char *label, Subid *subids, int len);
206 static Macro *macro_find(char *label);
207 static void macro_free(Macro *mp);
208 static void macro_list_delete();
209 
210 
211 static void table_free(Table *tp);
212 static void table_list_delete();
213 
214 extern int SSARegSubagent(Agent *);
215 extern int SSASubagentOpen(int, char *);
216 extern int SSARegSubtable(SSA_Table *);
217 
218 /* static_label is used when parsing a macro	*/
219 static char *static_label = NULL;
220 
221 /* static_agent is used when parsing an agent	*/
222 static Agent *static_agent = NULL;
223 
224 /* static_table is used when parsing n table	*/
225 static Table *static_table = NULL;
226 
227 /* static_inf_value and static_max_value are	*/
228 /* used when parsing a range			*/
229 static int static_inf_value = -1;
230 static int static_sup_value = -1;
231 
232 static int count;
233 %}
234 
235 %%
236 
237 
238 configuration :	agents | macros agents | snmp_security
239 		{
240 			DEBUG_YACC("configuration")
241 		}
242 
243 /******************* SNMP security (5-13-96) *********/
244 snmp_security : acls  trap_block  /*trapcommunity trapdestinators*/
245                 {
246                         DEBUG_YACC("security configuration")
247                 }
248 
249 /***************/
250 /* accesscontrol */
251 /***************/
252 
253 acls :	/*empty */ | t_acls t_equal t_openbracket acls_list t_closebracket
254 		{
255 			DEBUG_YACC("acls_list")
256 		}
257 		| t_acls t_equal t_openbracket error t_closebracket
258 		{
259 		  error("BUG: acl stmt parsing error at line %d",yylineno);
260 		  if(static_access_policy != NULL){
261 			access_policy_list_free(static_access_policy);
262 			static_access_policy = NULL;
263 		  }
264 		}
265 
266 acls_list : /*empty*/ | acls_list acl_item
267 	{
268 		DEBUG_YACC("acls_list")
269 	}
270 
271 acl_item : t_openbracket
272 	{
273 		static_access_policy = calloc(1,sizeof(AccessPolicy));
274 		if(static_access_policy == NULL)
275 		{
276 			error("calloc() failed");
277 			YYERROR;
278 		}
279 	} communities_stmt acl_access
280 	{
281 		if(static_access_policy!=NULL)
282 			static_access_policy->access_type = community_type;
283 	} hosts t_closebracket
284 	{
285 		/* create AccessServer */
286 		/* put the AccessPolicy into AccessServer */
287 		/* put AccessServer into corresponding manager */
288 		static_access_server = NULL;
289 		static_access_policy = NULL;
290 		static_community = NULL;
291 		community_type = 0;
292 	}
293 
294 communities_stmt :      t_communities t_equal communities_set
295         {
296                 DEBUG_YACC("communities_stmt");
297         }
298 
299 communities_set : communities_set t_coma community_elem | community_elem
300 	{
301 		DEBUG_YACC("communities_set");
302 	}
303 
304 community_elem : ct_identifier
305 	{
306 		if(static_access_policy==NULL){
307 			error("acl statement error");
308 			YYERROR;
309 		}
310 		/* add community into AccessPolicy */
311 		static_community = calloc(1, sizeof(Community));
312 		if(static_community == NULL)
313 		{
314 			error("calloc() failed");
315 			YYERROR;
316 		}
317 		static_community->name = strdup(yytext);
318 		community_attach(static_access_policy,static_community);
319 		static_community = NULL;
320 	}
321 
322 acl_access:	t_access t_equal acl_access_type
323 	{
324 		DEBUG_YACC("acl_access")
325 	}
326 
327 acl_access_type : t_readonly | t_readwrite
328 		{
329 			DEBUG_YACC("acl_access_type")
330 		}
331 
332 hosts : t_managers t_equal hosts_list
333         {
334                 DEBUG_YACC("hosts")
335         }
336 
337 hosts_list : hosts_list t_coma host_item | host_item
338 	{
339 		DEBUG_YACC("hosts_list");
340 	}
341 
342 host_item :  ct_identifier
343 	{
344 		/* add the host item to manager list */
345 		/* it should return the pointer if exists */
346                 Manager *res;
347 
348                 DEBUG_YACC("manager_item")
349 
350                 res = manager_add(yytext, error_label);
351                 if(res==NULL){
352                                 error("error in %s at line %d: %s",
353                                         current_filename? current_filename:
354 "???",
355                                         yylineno, error_label);
356                 }
357 		static_access_server = calloc(1,sizeof(AccessServer));
358 		if(static_access_server == NULL)
359 		{
360 			error("malloc() failed");
361 			if(static_access_policy)
362 			  access_policy_list_free(static_access_policy);
363 			YYERROR;
364 		}
365 		if(static_access_policy!=NULL)
366 			static_access_policy->count++;
367 		static_access_server->first_acc_policy = static_access_policy;
368 		access_server_add_tail(res,static_access_server);
369 		static_access_server = NULL;
370 	}
371 
372 
373 /************/
374 /* managers */
375 /************/
376 
377 /*
378 managers :      t_managers t_equal t_openbracket managers_list t_closebracket
379                 {
380                         DEBUG_YACC("agents")
381                 }
382 
383 
384 managers_list :  | managers_list list_separator manager_item
385                 {
386                         DEBUG_YACC("managers_list")
387                 }
388 
389 
390 manager_item :  ct_identifier
391                 {
392                         Manager *res;
393 
394                         DEBUG_YACC("manager_item")
395 
396                         res = manager_add(yytext, error_label);
397 			if(res==NULL){
398                                         error("error in %s at line %d: %s",
399                                                 current_filename? current_filename:
400 "???",
401                                                 yylineno, error_label);
402 			}
403                 }
404 */
405 
406 /*** trap hanlding (6-25-96) */
407 trap_block :	t_trap t_equal t_openbracket trap_list t_closebracket
408 		{
409 			DEBUG_YACC("trap_block")
410 		}
411 		| t_trap t_equal t_openbracket error t_closebracket
412 		{
413 			/* clean up */
414 			if(static_sub_member != NULL){
415 			  sub_member_free(static_sub_member);
416 			  static_sub_member=NULL;
417 			}
418 		}
419 
420 trap_list : /*empty*/ | trap_list trap_item
421 	{
422 		DEBUG_YACC("trap_list")
423 	}
424 
425 trap_item : t_openbracket
426 	{
427 		/* create submember */
428 		static_sub_member = calloc(1,sizeof(SubMember));
429 		if(static_sub_member == NULL)
430 		{
431 			error("malloc() failed");
432 			YYERROR;
433 		}
434 	} trap_community_string trap_interest_hosts
435 	{
436 		/* attach submember to subgroup */
437 	} enterprise_list t_closebracket
438 	{
439 		static_sub_member = NULL;
440 	}
441 
442 trap_community_string : t_trapcommunity t_equal ct_identifier
443 	{
444 		/* set the community field in submember */
445 		if(static_sub_member != NULL)
446 		{
447 			static_sub_member->community_string = strdup(yytext);
448 			if(static_sub_member == NULL)
449 			{
450 				error(ERR_MSG_ALLOC);
451 				YYERROR;
452 			}
453 		}else{
454 			error("BUG: missing trap community name");
455 		}
456 	}
457 
458 trap_interest_hosts : t_hosts t_equal trap_interest_hosts_list
459 	{
460 		DEBUG_YACC("trap_interest_hosts")
461 	}
462 
463 trap_interest_hosts_list : trap_interest_hosts_list t_coma
464 		 trap_interest_host_item | trap_interest_host_item
465 	{
466 		DEBUG_YACC("trap_interest_hosts_list")
467 	}
468 
469 trap_interest_host_item : ct_identifier
470 	{
471 		DEBUG_YACC("trap_interest_host_item")
472 		/* attach host to the submember */
473 		if(static_sub_member==NULL){
474 		 	error("trap statement error");
475 			YYERROR;
476 		}else{
477 		  static_host = calloc(1,sizeof(Manager));
478 		  if(static_host == NULL)
479 		  {
480 			error("malloc() failed");
481 			YYERROR;
482 		  }
483 		  static_host->name = strdup(yytext);
484 		  if(name_to_ip_address(static_host->name,
485 			&static_host->ip_address,error_label)){
486 			error("unknown host %s",static_host->name);
487 			free(static_host);
488 			static_host=NULL;
489 			YYERROR;
490 		  }
491 		  static_host->next_manager = static_sub_member->first_manager;
492 		  static_sub_member->first_manager=static_host;
493 		  static_host=NULL;
494 		}
495 	}
496 
497 enterprise_list : /* empty */ | enterprise_list enterprise_item
498 	{
499 		DEBUG_YACC("enterprise_list")
500 	}
501 
502 enterprise_item : t_openbracket enterprise_stmt trap_number_stmt
503 		  t_closebracket
504 		{
505 			DEBUG_YACC("enterprise_item")
506 		}
507 
508 enterprise_stmt : ENTERPRISE t_equal t_quotedstring
509 		{
510 			/* currently, it supports single enterprise */
511 
512 			DEBUG_YACC("enterprise_stmt")
513 			/* add or find the enterprise */
514 			static_filter = efilter_add(quoted_string,error_label);
515 			if(static_filter==NULL){
516 			  error("error in %s at line %d: %s",
517 				current_filename?current_filename:"???",
518 				yylineno,error_label);
519 			}
520 		}
521 
522 trap_number_stmt : t_trap_num t_equal trap_number_list
523 		{
524 			DEBUG_YACC("trap_number_stmt")
525 		}
526 
527 trap_number_list : trap_number_item
528 		{
529 			DEBUG_YACC("trap_number_list")
530 		}
531 		| trap_number_list t_coma trap_number_item
532 		{
533 			DEBUG_YACC("trap_number_list")
534 		}
535 
536 trap_number_item : trap_range
537 	{
538 			DEBUG_YACC("trap_number_item")
539 			/* for each trap, find/add to the
540 			   enterprise, and add_tailthe subgroup
541 			   to each trap */
542 
543 			if(static_filter!=NULL){
544 				/* expand the trap */
545 				mem_filter_join(static_trap_low,
546 					static_trap_high,static_sub_member,
547 					static_filter);
548 			}else{
549 				error("error in enterprise statement");
550 				YYERROR;
551 			}
552 	}
553 
554 trap_range :	 NUMBER
555 		{
556 			/* starting trap num */
557 		 	static_trap_low = token_value;
558 		}
559 		t_minus NUMBER
560 		{
561 			/* ending trap num */
562 			static_trap_high = token_value;
563 		}
564 		| NUMBER
565 		{
566 			/* start & end num the same */
567 			DEBUG_YACC("trap_range")
568 			static_trap_low=static_trap_high=token_value;
569 		}
570 
571 
572 /*
573 trapcommunity : t_trapcommunity t_equal ct_identifier
574                 {
575                         DEBUG_YACC("trap_community")
576 
577                         if(trap_community)
578                         {
579                                 error("BUG: trap_community not NULL in trap_community");
580                         }
581 
582                         trap_community = strdup(yytext);
583                         if(trap_community == NULL)
584                         {
585                                 error(ERR_MSG_ALLOC);
586                                 YYERROR;
587                         }
588                 }
589 */
590 
591 /*******************/
592 /* trapdestinators */
593 /*******************/
594 /*
595 
596 trapdestinators : t_trapdestinators t_equal t_openbracket trapdestinators_list t_closebracket
597                 {
598                         DEBUG_YACC("trapdestinators")
599                 }
600 
601 
602 trapdestinators_list :  | trapdestinators_list list_separator trapdestinator_item
603                 {
604                         DEBUG_YACC("trapdestinators_list")
605                 }
606 
607 
608 trapdestinator_item : ct_identifier
609                 {
610                         int res;
611 
612                         DEBUG_YACC("trapdestinator_item")
613 
614                         res = trap_destinator_add(yytext, error_label);
615                         switch(res)
616                         {
617                                 case 0:
618                                         break;
619 
620                                 case 1:
621                                         error("error in %s at line %d: %s",
622                                                 current_filename? current_filename:
623 "???",
624                                                 yylineno, error_label);
625                                         break;
626 
627                                 default:
628                                         error("fatal error in %s at line %d: %s",
629                                                 current_filename? current_filename:
630 "???",
631                                                 yylineno, error_label);
632                                         YYERROR;
633                         }
634                 }
635 
636 */
637 
638 /******************* SNMP security (5-13-96) *********/
639 
640 
641 
642 
643 /**********/
644 /* macros */
645 /**********/
646 
647 macros :	t_macros t_equal t_openbracket macros_list t_closebracket
648 		{
649 			DEBUG_YACC("macros")
650 		}
651 
652 
653 macros_list :	/* empty */ | macros_list macro_item
654 		{
655 			DEBUG_YACC("macros_list")
656 		}
657 
658 
659 macro_item :	label t_equal
660 		{
661 			if(parsing_oid != False)
662 			{
663 				error("BUG at line %d: parsing_oid not False in macro_item", yylineno);
664 			}
665 			parsing_oid = True;
666 
667 			if(static_subids != NULL)
668 			{
669 				error("BUG at line %d: static_subids not NULL in macro_item", yylineno);
670 			}
671 			if(static_len != 0)
672 			{
673 				error("BUG at line %d: static_len not 0 in macro_item", yylineno);
674 			}
675 		}
676 		subids_list
677 		{
678 			DEBUG_YACC("macro_item")
679 
680 			if(macro_add(static_label, static_subids, static_len) == NULL)
681 			{
682 				error("error at line %d", yylineno);
683 				YYERROR;
684 			}
685 
686 			parsing_oid = False;
687 			free(static_label);
688 			static_label = NULL;
689 			free(static_subids);
690 			static_subids = NULL;
691 			static_len = 0;
692 		}
693 
694 
695 label :		t_identifier
696 		{
697 			DEBUG_YACC("label")
698 
699 			if(static_label != NULL)
700 			{
701 				error("BUG at line %d: static_label not NULL in label", yylineno);
702 			}
703 			static_label = strdup(yytext);
704 			if(static_label == NULL)
705 			{
706 				error("malloc() failed");
707 				YYERROR;
708 			}
709 		}
710 
711 
712 
713 /**********/
714 /* agents */
715 /**********/
716 
717 agents :	t_agents t_equal t_openbracket agents_list t_closebracket
718 		{
719 			DEBUG_YACC("agents")
720 		}
721 
722 
723 agents_list :	agent_item | agents_list agent_item
724 		{
725 			DEBUG_YACC("agents_list")
726 		}
727 
728 
729 agent_item :		t_openbracket
730 		{
731 			if(static_agent != NULL)
732 			{
733 				error("BUG at line %d: static_agent not NULL in agent", yylineno);
734 			}
735 			static_agent = malloc(sizeof(Agent));
736 			if(static_agent == NULL)
737 			{
738 				error("malloc() failed");
739 				YYERROR;
740 			}
741 			(void)memset(static_agent, 0, sizeof(Agent));
742 			/* LINTED */
743 			static_agent->agent_id = (int32_t)getpid();
744 			static_agent->agent_status = SSA_OPER_STATUS_NOT_IN_SERVICE;
745 			static_agent->personal_file = strdup (current_filename);
746 		}
747 		name subtrees_tables timeout optional_watch_dog_time optional_port t_closebracket
748 		{
749 			DEBUG_YACC("agent_item");
750 
751 			/* add the agent id to agent, currently, agent_id is pid */
752 			if(first_agent == NULL)
753 			{
754 				static_agent->next_agent = NULL;
755 			}
756 			else
757 			{
758 				static_agent->next_agent = first_agent;
759 			}
760 			first_agent = static_agent;
761 
762 			/* if port is 0, assigned a non-reserved available port */
763 			if(static_agent->address.sin_port == 0 && agent_port_number != -1)
764 			  static_agent->address.sin_port = agent_port_number;
765 			else if(static_agent->address.sin_port==0)
766 			  static_agent->address.sin_port =
767 				get_a_non_reserved_port();
768 			if (agent_port_number == -1)
769 			  agent_port_number = static_agent->address.sin_port;
770 
771 			/* the registration is for confirmation and
772 	 		   fill in extra value */
773 			static_agent->agent_status = SSA_OPER_STATUS_ACTIVE;
774 			if(SSARegSubagent(static_agent) == 0)
775 			{
776 				error("subagent registration failed");
777 				YYERROR;
778 			}
779 			static_agent = NULL;
780 		}
781 
782 
783 name :		t_name t_equal t_quotedstring
784 		{
785 			DEBUG_YACC("name")
786 
787 			if(static_agent->name != NULL)
788 			{
789 				error("BUG at line %d: static_agent->name not NULL in name", yylineno);
790 			}
791 			static_agent->name = strdup(quoted_string);
792 			if(static_agent->name == NULL)
793 			{
794 				error("malloc() failed");
795 				YYERROR;
796 			}
797 /*
798  * Increased the num. of retries for SSASubagentOpen in order to insure success
799  * typically for boot time race condition between the master and subagent
800  * Initial sleep is introduced to increase the probability of success the very
801  * first time. This could be removed at a later time, after modifying the timeout
802  * parameters for the subagent
803  */
804                         (void)sleep(15);
805                         count=1;
806                         while(count) {
807 			if( (static_agent->agent_id =
808 			     SSASubagentOpen(max_agent_reg_retry,static_agent->name)) == INVALID_HANDLER )
809 			{
810                               if (count == 5) {
811 				error_exit("subagent registration failed");
812 				YYERROR;
813                               }
814 			}
815                         if (static_agent->agent_id ) break;
816                          count++;
817                         }
818 			if(SSARegSubagent(static_agent) == 0)
819 			{
820 				error("subagent registration failed");
821 				YYERROR;
822 			}
823 			/* LINTED */
824 			static_agent->process_id = (int32_t)getpid();
825 		}
826 
827 
828 subtrees_tables :  subtrees tables | subtrees | tables
829 		{
830 			DEBUG_YACC("subtrees_tables")
831 		}
832 
833 
834 subtrees :	t_subtrees t_equal t_openbracket
835 		{
836 			if(parsing_oid != False)
837 			{
838 				error("BUG at line %d: parsing_oid is not False in subtrees", yylineno);
839 			}
840 			parsing_oid = True;
841 
842 			if(static_subids != NULL)
843 			{
844 				error("BUG at line %d: static_subids not NULL in subtrees", yylineno);
845 			}
846 			if(static_len != 0)
847 			{
848 				error("BUG at line %d: static_len not 0 in subtrees", yylineno);
849 			}
850 		}
851 		subtrees_list t_closebracket
852 		{
853 			DEBUG_YACC("subtrees")
854 
855 			if(parsing_oid != True)
856 			{
857 				error("BUG at line %d: parsing_oid is not True in subtrees", yylineno);
858 			}
859 			parsing_oid = False;
860 		}
861 
862 
863 subtrees_list : /* empty */ | subtrees_list_coma_separated
864 		{
865 			DEBUG_YACC("subtrees_list")
866 		}
867 
868 
869 subtrees_list_coma_separated : subtree_item | subtrees_list_coma_separated t_coma subtree_item
870 		{
871 			DEBUG_YACC("subtrees_list_coma_separated")
872 		}
873 
874 
875 subtree_item : subids_list
876 		{
877 			DEBUG_YACC("subtree_item")
878 
879 			if(parsing_oid != True)
880 			{
881 				error("BUG at line %d: parsing_oid is not True in subtree_item", yylineno);
882 			}
883 
884 			if(subtree_add(static_agent, static_subids, static_len) == -1)
885 			{
886 				error("error at line %d", yylineno);
887 				YYERROR;
888 			}
889 
890 			free(static_subids);
891 			static_subids = NULL;
892 			static_len = 0;
893 		}
894 
895 
896 tables :	t_tables t_equal t_openbracket tables_list t_closebracket
897 		{
898 			DEBUG_YACC("tables")
899 		}
900 
901 
902 tables_list :	/* empty */ | tables_list table_item
903 		{
904 			DEBUG_YACC("tables_list")
905 		}
906 
907 
908 table_item :	t_openbracket
909 		{
910 			if(static_agent == NULL)
911 			{
912 				error("BUG at line %d: static_agent is NULL in table_item", yylineno);
913 			}
914 
915 			if(static_table)
916 			{
917 				error("BUG at line %d: static_table not NULL in table_item", yylineno);
918 			}
919 
920 			static_table = calloc(1,sizeof(Table));
921 			if(static_table == NULL)
922 			{
923 				error("malloc() failed");
924 				YYERROR;
925 			}
926 			static_table->regTblStatus =
927 			 SSA_OPER_STATUS_NOT_IN_SERVICE;
928 			static_table->next_table = NULL;
929 			static_table->agent = static_agent;
930 			if(static_agent!=NULL)
931 			  static_table->regTblAgentID =
932 			  static_agent->agent_id;
933 			static_table->regTblOID.subids = NULL;
934 			static_table->regTblOID.len = 0;
935 			static_table->regTblStartColumn = 0;
936 			static_table->regTblEndColumn = 0;
937 			static_table->regTblIndex = ++table_index;
938 /*
939 			static_table->indexs.subids = NULL;
940 			static_table->indexs.len = 0;
941 */
942 		}
943 		table columns indexs t_closebracket
944 		{
945 			DEBUG_YACC("table_item")
946 
947 			if(static_table == NULL)
948 			{
949 				error_exit("BUG at line %d: static_table is NULL in table_item", yylineno);
950 			}
951 
952 		 	/* register the table, if register fails, delete
953 			   the table */
954 			if(SSARegSubtable(static_table)==0){
955 				/* unregister the table */
956 
957 				error_exit("TABLE CONFIG");
958 			}
959 
960 
961 			if(last_table)
962 			{
963 				last_table->next_table = static_table;
964 			}
965 			else
966 			{
967 				first_table = static_table;
968 			}
969 			last_table = static_table;
970 			static_table = NULL;
971 		}
972 
973 
974 table :		t_table t_equal
975 		{
976 			if(parsing_oid != False)
977 			{
978 				error("BUG at line %d: parsing_oid is not False in tables", yylineno);
979 			}
980 
981 			parsing_oid = True;
982 		}
983 		subids_list
984 		{
985 			DEBUG_YACC("table")
986 
987 			if(parsing_oid != True)
988 			{
989 				error("BUG at line %d: parsing_oid is not True in tables", yylineno);
990 			}
991 			parsing_oid = False;
992 
993 			if(static_table == NULL)
994 			{
995 				error_exit("BUG at line %d: static_table is NULL in table", yylineno);
996 			}
997 
998 			static_table->regTblOID.subids = static_subids;
999 			static_subids = NULL;
1000 			static_table->regTblOID.len = static_len;
1001 			static_len = 0;
1002 		}
1003 
1004 
1005 columns :	t_columns t_equal range
1006 		{
1007 			DEBUG_YACC("columns")
1008 
1009 			if(static_table == NULL)
1010 			{
1011 				error_exit("BUG at line %d: static_table is NULL in columns", yylineno);
1012 			}
1013 
1014 			static_table->regTblStartColumn = static_inf_value;
1015 			static_inf_value = -1;
1016 			static_table->regTblEndColumn = static_sup_value;
1017 			static_sup_value = -1;
1018 		}
1019 
1020 
1021 /*
1022 indexs :	t_indexs t_equal
1023 		{
1024 			if(parsing_oid != False)
1025 			{
1026 				error("BUG at line %d: parsing_oid is not False in indexs", yylineno);
1027 			}
1028 
1029 			parsing_oid = True;
1030 		}
1031 		subids_list
1032 		{
1033 			DEBUG_YACC("indexs")
1034 
1035 			if(parsing_oid != True)
1036 			{
1037 				error("BUG at line %d: parsing_oid is not True in indexs", yylineno);
1038 			}
1039 			parsing_oid = False;
1040 
1041 			if(static_table == NULL)
1042 			{
1043 				error_exit("BUG at line %d: static_table is NULL in indexs", yylineno);
1044 			}
1045 
1046 			static_table->indexs.subids = static_subids;
1047 			static_subids = NULL;
1048 			static_table->indexs.len = static_len;
1049 			static_len = 0;
1050 		}
1051 */
1052 
1053 indexs :	t_indexs t_equal range
1054 		{
1055 			DEBUG_YACC("indexs")
1056 
1057 			if(static_inf_value == -1)
1058 			{
1059 				error("BUG at line %d: static_inf_value is -1", yylineno);
1060 			}
1061 			if(static_sup_value == -1)
1062 			{
1063 				error("BUG at line %d: static_sup_value is -1", yylineno);
1064 			}
1065 			static_table->regTblStartRow = static_inf_value;
1066 			static_table->regTblEndRow = static_sup_value;
1067 			static_inf_value = -1;
1068 			static_sup_value = -1;
1069 		}
1070 
1071 
1072 range :		t_opensquarebracket t_number
1073 		{
1074 			if(static_inf_value != -1)
1075 			{
1076 				error("BUG at line %d: static_inf_value (%d) is not -1 in range",
1077 					yylineno,
1078 					static_inf_value);
1079 			}
1080 
1081 			static_inf_value = token_value;
1082 		}
1083 		t_minus t_number
1084 		{
1085 			if(static_sup_value != -1)
1086 			{
1087 				error("BUG at line %d: static_sup_value (%d) is not -1 in range",
1088 					yylineno,
1089 					static_inf_value);
1090 			}
1091 
1092 			static_sup_value = token_value;
1093 		}
1094 		t_closesquarebracket
1095 		{
1096 			DEBUG_YACC("range")
1097 		}
1098 		| t_number
1099 		{
1100 			if(static_inf_value != -1)
1101 			{
1102 				error("BUG at line %d: static_inf_value (%d) is not -1 in range",
1103 					yylineno,
1104 					static_inf_value);
1105 			}
1106 			if(static_sup_value != -1)
1107 			{
1108 				error("BUG at line %d: static_sup_value (%d) is not -1 in range",
1109 					yylineno,
1110 					static_sup_value);
1111 			}
1112 
1113 			static_inf_value = token_value;
1114 			static_sup_value = token_value;
1115 		}
1116 
1117 
1118 timeout :	t_timeout t_equal t_number
1119 		{
1120 			DEBUG_YACC("subtree")
1121 
1122 			static_agent->timeout = token_value;
1123 		}
1124 
1125 optional_watch_dog_time : /*empty*/ |   t_watch_dog_time t_equal NUMBER
1126                 {
1127                         DEBUG_YACC("optional_watch_dog_time")
1128                         static_agent->watch_dog_time = token_value;
1129                 }
1130 
1131 optional_port:	/*empty*/ | port
1132 	{
1133                         DEBUG_YACC("optional_port")
1134 	}
1135 
1136 port :		t_port t_equal t_number
1137 		{
1138 			DEBUG_YACC("port")
1139 
1140 			if(token_value > 0xFFFF)
1141 			{
1142 				error("error at line %d: the port number (%d) should not be greater than %d", yylineno, token_value, 0xFFFF);
1143 				YYERROR;
1144 			}
1145 
1146 			/* LINTED */
1147 			static_agent->address.sin_port = (short) token_value;
1148 
1149 			if(agent_find(&(static_agent->address)))
1150 			{
1151 				error("error at line %d: the port number %d is already used by another agent", yylineno, token_value);
1152 				YYERROR;
1153 			}
1154 		}
1155 
1156 
1157 
1158 /***************/
1159 /* subids_list */
1160 /***************/
1161 
1162 subids_list :	subid | subids_list t_dot subid
1163 		{
1164 			DEBUG_YACC("subids_list")
1165 		}
1166 
1167 
1168 subid :		t_mib2 | t_sun | t_enterprise | t_identifier | t_number
1169 		{
1170 			DEBUG_YACC("subid")
1171 		}
1172 
1173 
1174 
1175 /*******************/
1176 /* terminal tokens */
1177 /*******************/
1178 
1179 /**************** SNMP security (5-13-96) ***/
1180 ct_identifier : IDENTIFIER
1181 		{
1182 			DEBUG_YACC("ct_indentifier")
1183 		}
1184 
1185 t_communities : COMMUNITIES
1186                 {
1187                         DEBUG_YACC("t_communities")
1188                 }
1189 
1190 t_hosts : HOSTS
1191 	{
1192 		DEBUG_YACC("t_hosts")
1193 	}
1194 
1195 t_acls  : ACL
1196         {
1197                 DEBUG_YACC("t_acls")
1198         }
1199 
1200 t_access : ACCESS
1201 	{
1202 		DEBUG_YACC("t_access")
1203 	}
1204 
1205 t_readonly :    READONLY
1206                 {
1207                         DEBUG_YACC("t_readonly")
1208 
1209                         community_type = READ_ONLY;
1210                 }
1211 
1212 t_readwrite :   READWRITE
1213                 {
1214                         DEBUG_YACC("t_readwrite")
1215 
1216                         community_type = READ_WRITE;
1217                 }
1218 
1219 t_managers :    MANAGERS
1220                 {
1221                         DEBUG_YACC("t_managers")
1222                 }
1223 
1224 
1225 t_trap :	TRAP
1226 		{
1227 			DEBUG_YACC("t_trap")
1228 		}
1229 
1230 t_trap_num:	TRAPNUM
1231 		{
1232 			DEBUG_YACC("t_trap_num")
1233 		}
1234 
1235 t_trapcommunity : TRAPCOMMUNITY
1236                 {
1237                         DEBUG_YACC("t_trapcommunity")
1238                 }
1239 
1240 
1241 /*
1242 t_trapdestinators : TRAPDESTINATORS
1243                 {
1244                         DEBUG_YACC("t_trapdestinators")
1245                 }
1246 
1247 list_separator : | t_coma
1248                 {
1249                         DEBUG_YACC("list_separator")
1250                 }
1251 */
1252 
1253 
1254 /**************** SNMP security (5-13-96) ***/
1255 
1256 
1257 t_number :	NUMBER
1258 		{
1259 			DEBUG_YACC("t_number")
1260 
1261 			if(parsing_oid == True)
1262 			{
1263 				if(subids_cat((Subid *) &token_value, 1) == -1)
1264 				{
1265 					YYERROR;
1266 				}
1267 			}
1268 		}
1269 
1270 
1271 t_macros :	MACROS
1272 		{
1273 			DEBUG_YACC("t_macros")
1274 		}
1275 
1276 
1277 t_equal :	EQUAL
1278 		{
1279 			DEBUG_YACC("t_equal")
1280 		}
1281 
1282 
1283 t_minus :	MINUS
1284 		{
1285 			DEBUG_YACC("t_minus")
1286 		}
1287 
1288 
1289 t_openbracket :	OPENBRACKET
1290 		{
1291 			DEBUG_YACC("t_openbracket")
1292 		}
1293 
1294 
1295 t_closebracket : CLOSEBRACKET
1296 		{
1297 			DEBUG_YACC("t_closebracket")
1298 		}
1299 
1300 
1301 t_opensquarebracket : OPENSQUAREBRACKET
1302 		{
1303 			DEBUG_YACC("t_opensquarebracket")
1304 		}
1305 
1306 
1307 t_closesquarebracket : CLOSESQUAREBRACKET
1308 		{
1309 			DEBUG_YACC("t_closesquarebracket")
1310 		}
1311 
1312 
1313 t_identifier :	IDENTIFIER
1314 		{
1315 			DEBUG_YACC("t_identifier")
1316 
1317 			if(parsing_oid == True)
1318 			{
1319 				Macro *mp;
1320 
1321 
1322 				mp = macro_find(yytext);
1323 				if(mp == NULL)
1324 				{
1325 					error("error at line %d: %s is not a macro", yylineno, yytext);
1326 					YYERROR;
1327 				}
1328 
1329 				if(subids_cat(mp->name.subids, mp->name.len) == -1)
1330 				{
1331 					YYERROR;
1332 				}
1333 			}
1334 		}
1335 
1336 
1337 t_mib2 :	MIB2
1338 		{
1339 			DEBUG_YACC("t_mib2")
1340 
1341 			if(parsing_oid == False)
1342 			{
1343 				error("BUG at line %d: parsing_oid not True in t_mib2", yylineno);
1344 			}
1345 			if(subids_cat(subids_mib2, mib2_len) == -1)
1346 			{
1347 				YYERROR;
1348 			}
1349 		}
1350 
1351 
1352 t_sun :		SUN
1353 		{
1354 			DEBUG_YACC("t_sun")
1355 
1356 			if(parsing_oid == False)
1357 			{
1358 				error("BUG at line %d: parsing_oid not True in t_sun", yylineno);
1359 			}
1360 			if(subids_cat(subids_sun, sun_len) == -1)
1361 			{
1362 				YYERROR;
1363 			}
1364 		}
1365 
1366 
1367 t_enterprise :	ENTERPRISE
1368 		{
1369 			DEBUG_YACC("t_enterprise")
1370 
1371 			if(parsing_oid == False)
1372 			{
1373 				error("BUG at line %d: parsing_oid not True in t_enterprise", yylineno);
1374 			}
1375 			if(subids_cat(subids_enterprise, enterprise_len) == -1)
1376 			{
1377 				YYERROR;
1378 			}
1379 		}
1380 
1381 t_dot :		DOT
1382 		{
1383 			DEBUG_YACC("t_dot")
1384 		}
1385 
1386 
1387 t_agents :	AGENTS
1388 		{
1389 			DEBUG_YACC("t_agents")
1390 		}
1391 
1392 
1393 t_name :	NAME
1394 		{
1395 			DEBUG_YACC("t_name")
1396 		}
1397 
1398 
1399 t_subtrees :	SUBTREES
1400 		{
1401 			DEBUG_YACC("t_subtrees")
1402 		}
1403 
1404 
1405 t_tables :	TABLES
1406 		{
1407 			DEBUG_YACC("t_tables")
1408 		}
1409 
1410 
1411 t_table :	TABLE
1412 		{
1413 			DEBUG_YACC("t_table")
1414 		}
1415 
1416 
1417 t_columns :	COLUMNS
1418 		{
1419 			DEBUG_YACC("t_columns")
1420 		}
1421 
1422 
1423 t_indexs :	INDEXS
1424 		{
1425 			DEBUG_YACC("t_indexs")
1426 		}
1427 
1428 
1429 t_timeout :	TIMEOUT
1430 		{
1431 			DEBUG_YACC("t_timeout")
1432 		}
1433 
1434 t_watch_dog_time :      WATCHDOGTIME
1435                 {
1436                         DEBUG_YACC("t_watch_dog_time")
1437                 }
1438 
1439 t_port :	PORT
1440 		{
1441 			DEBUG_YACC("t_port")
1442 		}
1443 
1444 
1445 t_quotedstring : QUOTEDSTRING
1446 		{
1447 			DEBUG_YACC("t_quotedstring\n")
1448 		}
1449 
1450 
1451 t_coma :	COMA
1452 		{
1453 			DEBUG_YACC("t_coma")
1454 		}
1455 %%
1456 
1457 #include "personal.lex.c"
1458 
1459 /****************************************************************/
1460 
1461 static int subids_cat(Subid *subids, int len)
1462 {
1463 	Subid *new_subids;
1464 	int new_len;
1465 
1466 
1467 	new_len = static_len + len;
1468 			/* LINTED */
1469 	new_subids = (Subid *) malloc(new_len * (int32_t)sizeof(Subid));
1470 	if(new_subids == NULL)
1471 	{
1472 		error("malloc() failed");
1473 		if(static_subids)
1474 		{
1475 			free(static_subids);
1476 		}
1477 		static_subids = NULL;
1478 		static_len = 0;
1479 		return -1;
1480 	}
1481 			/* LINTED */
1482 	(void)memcpy(new_subids, static_subids, static_len * (int32_t)sizeof(Subid));
1483 			/* LINTED */
1484 	(void)memcpy(&(new_subids[static_len]), subids, len * (int32_t)sizeof(Subid));
1485 
1486 
1487 	if(static_subids)
1488 	{
1489 		free(static_subids);
1490 	}
1491 	static_subids = new_subids;
1492 	static_len = new_len;
1493 
1494 	return 0;
1495 }
1496 
1497 
1498 /****************************************************************/
1499 
macro_add(char * label,Subid * subids,int len)1500 static Macro *macro_add(char *label, Subid *subids, int len)
1501 {
1502 	Macro *new;
1503 
1504 
1505 	if(macro_find(label) != NULL)
1506 	{
1507 		error("%s is already a macro", label);
1508 		return NULL;
1509 	}
1510 
1511 	new = (Macro *) malloc(sizeof(Macro));
1512 	if(new == NULL)
1513 	{
1514 		error("malloc() failed");
1515 		return NULL;
1516 	}
1517 	new->label = NULL;
1518 	new->name.subids = NULL;
1519 
1520 	new->label = strdup(label);
1521 	if(new->label == NULL)
1522 	{
1523 		error("malloc() failed");
1524 		macro_free(new);
1525 		return NULL;
1526 	}
1527 			/* LINTED */
1528 	new->name.subids = (Subid *) malloc(len * (int32_t)sizeof(Subid));
1529 	if(new->name.subids == NULL)
1530 	{
1531 		error("malloc() failed");
1532 		macro_free(new);
1533 		return NULL;
1534 	}
1535 			/* LINTED */
1536 	(void)memcpy(new->name.subids, subids, len * (int32_t)sizeof(Subid));
1537 	new->name.len = len;
1538 	new->next_macro = first_macro;
1539 	first_macro = new;
1540 
1541 	return new;
1542 }
1543 
1544 
1545 /****************************************************************/
1546 
macro_find(char * label)1547 static Macro *macro_find(char *label)
1548 {
1549 	Macro *mp;
1550 
1551 
1552 	for(mp = first_macro; mp; mp = mp->next_macro)
1553 	{
1554 		if(strcmp(mp->label, label) == 0)
1555 		{
1556 			return mp;
1557 		}
1558 	}
1559 
1560 	return NULL;
1561 }
1562 
1563 
1564 /****************************************************************/
1565 
macro_free(Macro * mp)1566 static void macro_free(Macro *mp)
1567 {
1568 	if(mp == NULL)
1569 	{
1570 		return;
1571 	}
1572 
1573 	if(mp->label)
1574 	{
1575 		free(mp->label);
1576 	}
1577 
1578 	if(mp->name.subids)
1579 	{
1580 		free(mp->name.subids);
1581 	}
1582 
1583 	free(mp);
1584 
1585 	return;
1586 }
1587 
1588 
1589 /****************************************************************/
1590 
macro_list_delete()1591 static void macro_list_delete()
1592 {
1593 	Macro *mp = first_macro;
1594 	Macro *next;
1595 
1596 
1597 	while(mp)
1598 	{
1599 		next = mp->next_macro;
1600 
1601 		macro_free(mp);
1602 
1603 		mp = next;
1604 	}
1605 
1606 	first_macro = NULL;
1607 
1608 	return;
1609 }
1610 
1611 
1612 
1613 /****************************************************************/
1614 
yyerror(char * s)1615 int yyerror(char *s)
1616 {
1617 	error("%s at line %d: %s", s, yylineno, yytext);
1618 
1619 	return (0);
1620 }
1621 
1622 
1623 /****************************************************************/
1624 
1625 /* If we have a serious problem, this function will	*/
1626 /* terminate (<==> exit) the program			*/
1627 
config_init(char * filename)1628 void config_init(char *filename)
1629 {
1630 		struct stat statb;
1631 		char *fileaddr;
1632 		int fd;
1633 
1634 
1635 		yylineno = 1;
1636 
1637 		if((fd = open(filename, O_RDONLY)) < 0)
1638 		{
1639 			error_exit(ERR_MSG_OPEN,
1640 				filename, errno_string());
1641 		}
1642 
1643 		/*
1644 		 * get the size of the file
1645 		 */
1646 		if(fstat(fd, &statb) < 0)
1647 		{
1648 			error_exit(ERR_MSG_FSTAT,
1649 				filename, errno_string());
1650 		}
1651 		if(!S_ISREG(statb.st_mode))
1652 		{
1653 			error_exit("filename: %s is not a file\n",filename);
1654 		}
1655 
1656 		/*
1657 		 * and map it into my address space
1658 		 */
1659 		if(statb.st_size != 0)
1660 		{
1661 			/* Purify IPR/IPW error - bug 4124843. yylook wants to
1662 			   read the last + 1 byte to decide EOF */
1663 			/* LINTED */
1664 			if((fileaddr = (char *) mmap(0, (int32_t)statb.st_size+1, PROT_READ|PROT_WRITE,
1665 				MAP_PRIVATE, fd, 0)) <= (char *) 0)
1666 			{
1667 				error_exit(ERR_MSG_MMAP,
1668 					filename, errno_string());
1669 			}
1670 
1671 			/*
1672 			 * set current lex focus on the file
1673 			 */
1674 
1675 			lexinput = fileaddr;
1676 
1677 			/*
1678 			 * and parse the file
1679 			 */
1680 			current_filename = filename;
1681 			if(yyparse() == 1)
1682 			{
1683 				error_exit("parsing %s failed", filename);
1684 			}
1685 			current_filename = NULL;
1686 
1687 			/*
1688 			 * Parsing is finished
1689 			 *
1690 			 * unmap the file and close it
1691 			 */
1692 
1693 			/* Purify IPR/IPW error - bug 4124843 */
1694 			/* LINTED */
1695 			if(munmap(fileaddr, (int32_t)statb.st_size+1) == -1)
1696 			{
1697 				error(ERR_MSG_MUNMAP, errno_string());
1698 			}
1699 		}
1700 		else
1701 		{
1702 			/* empty file, ignore it */
1703 
1704 			error_exit("empty configuration file %s", filename);
1705 		}
1706 
1707 		if(close(fd) == -1)
1708 		{
1709 			error(ERR_MSG_CLOSE, errno_string());
1710 		}
1711 
1712 		macro_list_delete();
1713 
1714 
1715 	table_list_delete();
1716 
1717 
1718 	if(first_agent == NULL)
1719 	{
1720 		error_exit("No SNMP agent configured");
1721 	}
1722 
1723 	if(trace_level > 0)
1724 	{
1725 		trace_subtrees();
1726 		trace_agents();
1727 	}
1728 }
1729 
1730 
1731 /****************************************************************/
1732 
table_list_delete()1733 static void table_list_delete()
1734 {
1735 	Table *next;
1736 
1737 	while(first_table)
1738 	{
1739 		next = first_table->next_table;
1740 		table_free(first_table);
1741 		first_table = next;
1742 	}
1743 
1744 	first_table = NULL;
1745 	last_table = NULL;
1746 }
1747 
1748 
1749 /****************************************************************/
1750 
table_free(Table * tp)1751 static void table_free(Table *tp)
1752 {
1753 	if(tp == NULL)
1754 	{
1755 		return;
1756 	}
1757 
1758 	if(tp->regTblOID.subids)
1759 	{
1760 		free(tp->regTblOID.subids);
1761 	}
1762 
1763 /*
1764 	if(tp->indexs.subids)
1765 	{
1766 		free(tp->indexs.subids);
1767 	}
1768 */
1769 
1770 	free(tp);
1771 }
1772 
1773 
1774 /****************************************************************/
1775 
1776 
1777 /*********** SNMP security (5-13-96) ******/
1778 /* If we have a serious problem, this function will	*/
1779 /* terminate (<==> exit) the program			*/
1780 
sec_config_init(char * filename)1781 void sec_config_init(char *filename)
1782 {
1783 	struct stat statb;
1784 	char *fileaddr;
1785 	int fd;
1786 
1787 
1788 	delete_manager_list();
1789 	delete_community_list();
1790 	if(trap_community)
1791 	{
1792 		free(trap_community);
1793 		trap_community = NULL;
1794 	}
1795 	delete_trap_destinator_list();
1796 
1797 
1798 	yylineno = 1;
1799 
1800 	if((fd = open(filename, O_RDONLY)) < 0)
1801 	{
1802 		error_exit(ERR_MSG_OPEN,
1803 			filename, errno_string());
1804 	}
1805 
1806 	/*
1807 	 * get the size of the file
1808 	 */
1809 	if(fstat(fd, &statb) < 0 )
1810 	{
1811 		error_exit(ERR_MSG_FSTAT,
1812 			filename, errno_string());
1813 	}
1814 	if(!S_ISREG(statb.st_mode))
1815 	{
1816 		error_exit("filename: %s is not a file\n",filename);
1817 	}
1818 
1819 	/*
1820 	 * and map it into my address space
1821 	 */
1822 	if(statb.st_size)
1823 	{
1824 		/* Purify IPR/IPW error - bug 4124843. yylook wants to
1825 		   read the last + 1 byte to decide EOF */
1826 			/* LINTED */
1827 		if((fileaddr = (char *) mmap(0, (int32_t)statb.st_size+1, PROT_READ|PROT_WRITE,
1828 			MAP_PRIVATE, fd, 0)) <= (char *) 0)
1829 		{
1830 			error_exit(ERR_MSG_MMAP,
1831 				filename, errno_string());
1832 		}
1833 
1834 		/*
1835 		 * set current lex focus on the file
1836 		 */
1837 
1838 		lexinput = fileaddr;
1839 
1840 		/*
1841 		 * and parse the file
1842 		 */
1843 
1844 		current_filename = filename;
1845 		if(yyparse() == 1)
1846 		{
1847 			error_exit("parsing %s failed", filename);
1848 		}
1849 		current_filename = NULL;
1850 
1851 		/*
1852 		 * Parsing is finished
1853 		 *
1854 		 * unmap the file and close it
1855 		 */
1856 
1857 		/* Purify IPR/IPW error - bug 4124843 */
1858 			/* LINTED */
1859 		if(munmap(fileaddr, (int32_t)statb.st_size+1) == -1)
1860 		{
1861 			error(ERR_MSG_MUNMAP,
1862 				errno_string());
1863 		}
1864 	}
1865 	else
1866 	{
1867 		/* empty file, ignore it */
1868 
1869 		error_exit("empty configuration file %s", filename);
1870 	}
1871 
1872 	if(close(fd) == -1)
1873 	{
1874 		error(ERR_MSG_CLOSE, errno_string());
1875 	}
1876 
1877 	if(trace_level > 0)
1878 	{
1879 		trace("\n");
1880 		trace_managers();
1881 		trace_filter();
1882 		trace_trap_destinators();
1883 	}
1884 }
1885 /*********** SNMP security (5-13-96) ******/
1886 
yywrap()1887 int yywrap()
1888 {
1889   return 1;
1890 }
1891 
get_a_non_reserved_port()1892 static int get_a_non_reserved_port()
1893 {
1894   struct sockaddr_in me;
1895   socklen_t len;
1896   int cnt=0;
1897   int sd;
1898 
1899   sd = socket(AF_INET,SOCK_DGRAM,0);
1900   if(sd<0) return 0;
1901   me.sin_family = AF_INET;
1902   me.sin_addr.s_addr = INADDR_ANY;
1903 
1904   for(;cnt<5;cnt++){
1905     me.sin_port = htons(0);
1906     if(bind(sd,(struct sockaddr*)&me,sizeof(me))!=0)continue;
1907     len = (socklen_t) sizeof(me);
1908     if(getsockname(sd,(struct sockaddr*)&me, &len)==-1) continue;
1909     (void)close(sd);
1910     return me.sin_port;
1911   }
1912   (void)close(sd);
1913   return 0;
1914 }
1915