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 (the "License").
7 * You may not use this file except in compliance 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/*
24 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25 * Use is subject to license terms.
26 */
27
28#pragma ident	"%Z%%M%	%I%	%E% SMI"
29
30/*
31 * HISTORY
32 * 5-13-96	Jerry Yeung		parse security config. file
33 * 6-25-96      Jerry Yeung             parse trap info.
34 * 6-27-96 	Jerry Yeung		optional port stmt
35 * 6-28-96	Jerry Yeung		add setuid support
36 * 7-03-96	Jerry Yeung		add watchdog, maxAgentTimeOut
37 *					    pollInterval
38 * 7-13-96	Jerry Yeung		remove resource_name
39 * 7-17-96	Jerry Yeung		change reg file suffix
40 * 7-17-96	Jerry Yeung		change personal to registration_file
41 */
42%}
43
44%start configuration
45
46%token NUMBER
47%token MACROS
48%token EQUAL
49%token OPENBRACKET
50%token CLOSEBRACKET
51%token IDENTIFIER
52%token MIB2
53%token SUN
54%token ENTERPRISE
55%token DOT
56%token AGENTS
57%token NAME
58%token SUBTREES
59%token TABLES
60%token TABLE
61%token COLUMNS
62%token INDEXS
63%token TIMEOUT
64%token PORT
65%token QUOTEDSTRING
66%token COMMA
67%token MINUS
68%token OPENSQUAREBRACKET
69%token CLOSESQUAREBRACKET
70%token WATCHDOGTIME
71%token MAXAGENTTIMEOUT
72%token POLLINTERVAL
73%token ENVIRONMENT
74
75/* support SNMP security(5-13-96) */
76%token COMMUNITIES
77%token READONLY
78%token READWRITE
79%token MANAGERS
80%token TRAPCOMMUNITY
81%token TRAPDESTINATORS
82%token ACL
83%token ACCESS
84%token TRAPNUM
85%token HOSTS
86%token TRAP
87
88/* support resource */
89%token RESOURCE
90%token REGISTRATION_FILE
91%token SECURITY
92%token POLICY
93%token TYPE
94%token COMMAND
95%token DIRECTORY
96%token USER
97
98%{
99#include <unistd.h>
100#include <stdio.h>
101#include <sys/types.h>
102#include <sys/stat.h>
103#include <sys/mman.h>
104#include <netinet/in.h>
105#include <fcntl.h>
106#include <dirent.h>
107#include <string.h>
108
109#include "impl.h"
110#include "error.h"
111#include "trace.h"
112#include "pdu.h"
113
114#include "snmprelay_msg.h"
115#include "agent.h"
116#include "subtree.h"
117#include "session.h"
118#include "dispatcher.h"
119
120/** SNMP security (5-13-96) */
121#include "trap.h"
122#include "access.h"
123
124#include "res.h"
125#include "sh_table.h"
126
127
128/***** DEFINE *****/
129
130/*
131 #define DEBUG_YACC(string) printf("\t\tYACC: %s: %s at line %d\n", string, yytext, yylineno);
132*/
133
134#define DEBUG_YACC(string)
135
136/*
137#define SNMPRELAY_SUFFIX	".snmprelay"
138*/
139#define SNMPRELAY_SUFFIX	".reg"
140#define SNMPRESOURCE_SUFFIX	".rsrc"
141#define SNMPRELAY_REG_FILE	"snmpdx.reg"
142#define SNMPACL_SUFFIX		".acl"
143
144
145/***** TYPEDEF *****/
146
147typedef struct _Macro {
148	struct _Macro *next_macro;
149	char *label;
150	Oid name;
151} Macro;
152
153
154/***** GLOBAL VARIABLES *****/
155
156char config_file_4_res[300] = "";
157
158
159/***** STATIC VARIABLES AND FUNCTIONS *****/
160/*(6-18) reconfig */
161#define RES_PARSING_STATE_FROM_SCRATCH 0
162#define RES_PARSING_STATE_RE_READ 1
163static time_t last_res_modify_time=0;
164static int res_parsing_state =0; /* 0:init */
165
166/* access control(6-20-96) */
167static AccessServer *static_access_server=NULL;
168static AccessPolicy *static_access_policy=NULL;
169static Community *static_community=NULL;
170
171/* trap filter (6-25-96) */
172static SubMember *static_sub_member=NULL;
173static Manager *static_host=NULL;
174static EFilter *static_filter=NULL;
175static int static_trap_low=-1;
176static int static_trap_high=-1;
177
178/* snmp security(5-13-96) */
179static int community_type = 0;
180static char *current_filename = NULL;
181
182/* lexinput points to the current focus point in the config file */
183static char *lexinput;
184
185/* first_macro is the begining of the list	*/
186/* of the user defined macros			*/
187
188static Macro *first_macro = NULL;
189
190
191/* the boolean parsing_oid is used to		*/
192/* know if we are parsing an			*/
193/* object identifier or not.			*/
194
195static int parsing_oid = False;
196
197
198/* here are the values of the predifined macros	*/
199/* that can be used in the configuration file	*/
200
201static Subid subids_mib2[] = { 1, 3, 6, 1, 2, 1 };
202static int mib2_len = 6;
203static Subid subids_enterprise[] = { 1, 3, 6, 1, 4, 1 };
204static int enterprise_len = 6;
205static Subid subids_sun[] = { 1, 3, 6, 1, 4, 1, 42 };
206static int sun_len = 7;
207
208/* the 2 static variables static_subids and	*/
209/* static_len are used when parsing an	*/
210/* object identifier. Then the boolean		*/
211/* parsing_oid should be true. When a new sub-	*/
212/* identifier (or a list of sub-identifiers)	*/
213/* is found, we use the function		*/
214/* subids_cat to append it to the static_subids.*/
215
216static Subid *static_subids = NULL;
217static int static_len = 0;
218static int subids_cat(Subid *subids, int len);
219
220
221/* macro_add() is used to append a macro to	*/
222/* the macro list. macro_find is used to find	*/
223/* a macro in the macro list			*/
224
225static Macro *macro_add(char *label, Subid *subids, int len);
226static Macro *macro_find(char *label);
227static void macro_free(Macro *mp);
228static void macro_list_delete();
229static void trace_macros();
230
231
232/* static_label is used when parsing a macro	*/
233static char *static_label = NULL;
234
235/* static_agent is used when parsing an agent	*/
236static Agent *static_agent = NULL;
237
238/* resource support */
239static SapResource *static_res = NULL;
240
241/* static_table is used when parsing n table	*/
242static Table *static_table = NULL;
243Table *tmp_table;
244
245/* static_inf_value and static_max_value are	*/
246/* used when parsing a range			*/
247static int static_inf_value = -1;
248static int static_sup_value = -1;
249
250char *save_string = NULL;
251char *com_string = NULL;
252char *host_string = NULL;
253int found_dot=FALSE;
254int found_comma=FALSE;
255
256
257%}
258
259%%
260
261
262configuration :	agents | macros agents | snmp_security | environment resources
263		{
264			DEBUG_YACC("configuration")
265		}
266
267/******************* SNMP security (5-13-96) *********/
268snmp_security : acls trap_block /*trapcommunity trapdestinators*/
269                {
270                        DEBUG_YACC("security configuration")
271                }
272
273/***************/
274/* accesscontrol */
275/***************/
276
277acls :	/*empty*/ |t_acls t_equal t_openbracket acls_list t_closebracket
278	{
279		DEBUG_YACC("acls_list1")
280	}
281	| t_acls t_equal t_openbracket error t_closebracket
282	{
283	          DEBUG_YACC("acls_listError")
284		  error("BUG: acl stmt parsing error at line %d",yylineno);
285                  if(static_access_policy != NULL){
286                        access_policy_list_free(static_access_policy);
287                        static_access_policy = NULL;
288		  }
289	}
290
291acls_list : /*empty*/ | acls_list acl_item
292	{
293		DEBUG_YACC("acls_list2")
294	}
295
296acl_item : t_openbracket
297	{
298		static_access_policy = calloc(1,sizeof(AccessPolicy));
299		if(static_access_policy == NULL)
300		{
301			error("malloc() failed");
302			YYERROR;
303		}
304	} communities_stmt acl_access
305	{
306		if(static_access_policy!=NULL)
307			static_access_policy->access_type = community_type;
308	} hosts t_closebracket
309	{
310		/* create AccessServer */
311		/* put the AccessPolicy into AccessServer */
312		/* put AccessServer into corresponding manager */
313
314                {
315                        /* Hack to send last community string which is a dot string */
316
317                        if (com_string != NULL)
318                        {                  /* add community into AccessPolicy */
319                           static_community = calloc(1,sizeof(Community));
320                           if(static_community == NULL)
321                           {
322                              error("malloc() failed");
323                              YYERROR;
324                           }
325                           static_community->name = strdup(com_string);
326                           community_attach(static_access_policy,static_community);
327                           static_community = NULL;
328                           free(com_string);
329			   com_string=NULL;
330                           found_comma = FALSE;
331                        }
332                        /* Hack to send last manager host string which is a dot string */
333
334                        if (host_string != NULL)
335                        {
336                            Manager *res;
337
338                           res = manager_add(host_string, error_label);
339                           if(res==NULL){
340                               error("error in %s at line %d: %s",
341                                     current_filename? current_filename:
342"???",
343                                     yylineno, error_label);
344                           }
345                           static_access_server = calloc(1,sizeof(AccessServer));
346                           if(static_access_server == NULL)
347                           {
348                              error("malloc() failed");
349                              if(static_access_policy)
350                              access_policy_list_free(static_access_policy);
351                              YYERROR;
352                           }
353                              if(static_access_policy!=NULL)
354                              	static_access_policy->count++;
355                              static_access_server->first_acc_policy = static_access_policy;
356                              access_server_add_tail(res,static_access_server);
357                              static_access_server = NULL;
358			      free(host_string);
359		              host_string=NULL;
360                   	      found_comma = FALSE;
361                        }
362                }
363                static_access_server = NULL;
364                static_access_policy = NULL;
365                static_community = NULL;
366                community_type = 0;
367	}
368
369communities_stmt :    t_communities t_equal communities_set
370	{
371		DEBUG_YACC("communities_stmt");
372	}
373
374communities_set : communities_set  t_comma community_elem | community_elem
375	{
376                DEBUG_YACC("communities_set");
377	}
378
379community_elem : community_elem  t_dot community_item | t_dot community_elem | community_item
380        {
381               DEBUG_YACC("community_elem")
382        }
383
384community_item : ct_identifier
385 	{
386		DEBUG_YACC("community_item")
387		if(static_access_policy==NULL){
388			error("acl statement error");
389			YYERROR;
390		}
391
392                if (found_comma && (com_string != NULL))
393                {
394		     static_community = calloc(1,sizeof(Community));
395                     if(static_community == NULL)
396                     {
397                        error("malloc() failed");
398                        YYERROR;
399                     }
400
401	             static_community->name = strdup(com_string);
402                     community_attach(static_access_policy,static_community);
403                     static_community = NULL;
404	             free(com_string);
405	             com_string=NULL;
406                     found_comma=FALSE;
407                }
408                if (com_string == NULL && found_dot == FALSE)
409	        {
410                    /* com_string= strdup(save_string);*/
411                    /* first part of community string */
412                    com_string=malloc(50);
413                    if(com_string == NULL){
414                        error("malloc() failed");
415                        YYERROR;
416                    }
417                    strcpy(com_string,save_string);
418                    free(save_string);
419                }
420
421
422		if (found_dot )
423		{
424			if (com_string == NULL)
425				com_string = malloc(50);
426			strcat(com_string,".");
427					/* allow a dot in community string */
428			strcat(com_string,save_string);
429					/* add part after the dot    */
430			free(save_string);
431			found_dot=FALSE;
432		}
433	}
434
435acl_access :	t_access t_equal acl_access_type
436	{
437		DEBUG_YACC("acl_access")
438        }
439
440acl_access_type : t_readonly | t_readwrite
441		{
442			DEBUG_YACC("acl_access_type")
443		}
444
445hosts : t_managers t_equal hosts_list
446	{
447		DEBUG_YACC("hosts")
448	}
449
450hosts_list : hosts_list t_comma host_elem | host_elem
451	{
452		DEBUG_YACC("hosts_list");
453	}
454
455host_elem : host_elem t_dot host_item | host_item
456        {
457                DEBUG_YACC("host_elem");
458	}
459
460host_item :  ct_identifier
461	{
462		/* add the host item to manager list */
463		/* it should return the pointer if exists */
464                Manager *res;
465
466                DEBUG_YACC("manager_item")
467
468		if (found_comma && (host_string != NULL))
469		{
470                	res = manager_add(host_string, error_label);
471                	if(res==NULL)
472			{
473                                error("error in %s at line %d: %s",
474                                        current_filename? current_filename:
475"???",
476                                        yylineno, error_label);
477                	}
478                	static_access_server = calloc(1,sizeof(AccessServer));
479                	if(static_access_server == NULL)
480                	{
481                        	error("malloc() failed");
482                        	if(static_access_policy)
483                          	access_policy_list_free(static_access_policy);
484                        	YYERROR;
485                	}
486                	if(static_access_policy!=NULL)
487                        	static_access_policy->count++;
488                	static_access_server->first_acc_policy = static_access_policy;
489			access_server_add_tail(res,static_access_server);
490			static_access_server = NULL;
491                        free(host_string);
492			host_string=NULL;
493			found_comma = FALSE;
494		}
495		if (host_string == NULL)
496                {
497		    /* host_string= strdup(save_string);*/
498                    /* first part of host string */
499                    host_string=malloc(50);
500                    if(host_string == NULL){
501                        error("malloc() failed");
502                        YYERROR;
503	            }
504                    strcpy(host_string,save_string);
505                    free(save_string);
506                }
507
508
509                if (found_dot )
510                {
511                     strcat(host_string,".");           /* allow a dot in hoststring */
512                     strcat(host_string,save_string);   /* add part after the dot    */
513                     free(save_string);
514                     found_dot=FALSE;
515                }
516
517	}
518
519/***************/
520/* communities */
521/***************/
522
523/*
524
525communities :	t_communities t_equal t_openbracket communities_list t_closebracket
526		{
527			DEBUG_YACC("communities")
528		}
529		| t_communities t_equal t_openbracket error t_closebracket
530		{
531		  error("BUG: community stmt parsing error at line %d",yylineno);
532		  if(community_name != NULL) free(community_name);
533		}
534
535
536communities_list :  | communities_list community_item
537		{
538			DEBUG_YACC("communities_list")
539		}
540
541
542community_item : ct_identifier
543		{
544			DEBUG_YACC("community_item 1")
545
546			if(community_name)
547			{
548				error("BUG: community_name is not NULL in community_item");
549			}
550
551			community_name = strdup(yytext);
552			if(community_name == NULL)
553			{
554				error(ERR_MSG_ALLOC);
555				YYERROR;
556			}
557		}
558		communitytype
559		{
560			int res;
561
562			DEBUG_YACC("community_item 2")
563
564			if(community_name == NULL)
565			{
566				error("BUG: community_name is NULL in community_item");
567			}
568
569			res = community_add(community_name, community_type, error_label);
570			switch(res)
571			{
572				case 0:
573					break;
574
575				case 1:
576					error("error in %s at line %d: %s",
577						current_filename? current_filename: "???",
578						yylineno, error_label);
579					break;
580
581				default:
582					error("fatal error in %s at line %d: %s",
583						current_filename? current_filename: "???",
584						yylineno, error_label);
585					YYERROR;
586			}
587
588			free(community_name);
589			community_name = NULL;
590		}
591
592communitytype : t_readonly | t_readwrite
593		{
594			DEBUG_YACC("community_type")
595		}
596*/
597
598/************/
599/* managers */
600/************/
601
602/*
603managers :      t_managers t_equal t_openbracket managers_list t_closebracket
604                {
605                        DEBUG_YACC("agents")
606                }
607	 	| t_managers t_equal t_openbracket error t_closebracket
608		{
609		  error("BUG: managers stmt parsing error at line %d",yylineno);
610		}
611
612managers_list :  | managers_list list_separator manager_item
613                {
614                        DEBUG_YACC("managers_list")
615                }
616
617manager_item :  ct_identifier
618                {
619                        Manager *res;
620
621                        DEBUG_YACC("manager_item")
622
623                        res = manager_add(yytext, error_label);
624                        if(res==NULL){
625                                        error("error in %s at line %d: %s",
626                                                current_filename? current_filename:
627"???",
628                                                yylineno, error_label);
629                        }
630                }
631*/
632
633/*** trap hanlding (6-25-96) */
634trap_block :    t_trap t_equal t_openbracket trap_list t_closebracket
635                {
636                        DEBUG_YACC("trap_block")
637			found_comma = FALSE;
638                }
639                | t_trap t_equal t_openbracket error t_closebracket
640                {
641                        /* clean up */
642                        if(static_sub_member != NULL){
643                          sub_member_free(static_sub_member);
644                          static_sub_member=NULL;
645                        }
646                }
647
648trap_list : /*empty*/ | trap_list trap_item
649        {
650                DEBUG_YACC("trap_list")
651        }
652
653trap_item : t_openbracket
654        {
655                /* create submember */
656                static_sub_member = calloc(1,sizeof(SubMember));
657                if(static_sub_member == NULL)
658                {
659                        error("malloc() failed");
660                        YYERROR;
661                }
662        } trap_community_string trap_interest_hosts
663        {
664                /* attach submember to subgroup */
665        } enterprise_list t_closebracket
666        {
667                static_sub_member = NULL;
668        }
669
670trap_community_string : t_trapcommunity t_equal ct_identifier
671        {
672                /* set the community field in submember */
673                if(static_sub_member != NULL)
674                {
675                        static_sub_member->community_string = strdup(yytext);
676                        if(static_sub_member == NULL)
677                        {
678                                error(ERR_MSG_ALLOC);
679                                YYERROR;
680                        }
681                }else{
682                        error("BUG: missing trap community name");
683                }
684        }
685
686trap_interest_hosts : t_hosts t_equal trap_interest_hosts_list
687        {
688                DEBUG_YACC("trap_interest_hosts")
689        }
690
691trap_interest_hosts_list : trap_interest_hosts_list t_comma
692                 trap_interest_host_item | trap_interest_host_item
693        {
694                DEBUG_YACC("trap_interest_hosts_list")
695        }
696
697trap_interest_host_item : ct_identifier
698        {
699                DEBUG_YACC("trap_interest_host_item")
700                /* attach host to the submember */
701                if(static_sub_member==NULL){
702                        error("trap statement error");
703                        YYERROR;
704                }else{
705                  static_host = calloc(1,sizeof(Manager));
706                  if(static_host == NULL)
707                  {
708                        error("malloc() failed");
709                        YYERROR;
710                  }
711                  static_host->name = strdup(yytext);
712                  if(name_to_ip_address(static_host->name,
713                        &static_host->ip_address,error_label)){
714                        error("unknown host %s",static_host->name);
715                        free(static_host);
716                        static_host=NULL;
717                        YYERROR;
718                  }
719                  static_host->next_manager = static_sub_member->first_manager;
720                  static_sub_member->first_manager=static_host;
721                  static_host=NULL;
722                }
723        }
724
725enterprise_list : /* empty */ | enterprise_list enterprise_item
726        {
727                DEBUG_YACC("enterprise_list")
728        }
729
730enterprise_item : t_openbracket enterprise_stmt trap_number_stmt
731                  t_closebracket
732                {
733                        DEBUG_YACC("enterprise_item")
734                }
735
736enterprise_stmt : ENTERPRISE t_equal t_quotedstring
737                {
738                        /* currently, it supports single enterprise */
739
740                        DEBUG_YACC("enterprise_stmt")
741                        /* add or find the enterprise */
742                        static_filter = efilter_add(quoted_string,error_label);
743                        if(static_filter==NULL){
744                          error("error in %s at line %d: %s",
745                                current_filename?current_filename:"???",
746                                yylineno,error_label);
747                        }
748                }
749
750trap_number_stmt : t_trap_num t_equal trap_number_list
751                {
752                        DEBUG_YACC("trap_number_stmt")
753                }
754
755trap_number_list : trap_number_item
756                {
757                        DEBUG_YACC("trap_number_list")
758                }
759                | trap_number_list t_comma trap_number_item
760                {
761                        DEBUG_YACC("trap_number_list")
762                }
763
764trap_number_item : trap_range
765	{
766			DEBUG_YACC("trap_number_item")
767                        /* for each trap, find/add to the
768                           enterprise, and add_tailthe subgroup
769                           to each trap */
770
771                        if(static_filter!=NULL){
772                                /* expand the trap */
773                                mem_filter_join(static_trap_low,
774                                        static_trap_high,static_sub_member,
775                                        static_filter);
776                        }else{
777                                error("error in enterprise statement");
778                                YYERROR;
779                        }
780
781	}
782
783trap_range :    NUMBER
784                {
785                        /* starting trap num */
786                        static_trap_low = token_value;
787                }
788                t_minus  NUMBER
789                {
790                        /* ending trap num */
791                        static_trap_high = token_value;
792                }
793                | NUMBER
794                {
795                        /* start & end num the same */
796                        DEBUG_YACC("trap_range")
797                        static_trap_low=static_trap_high=token_value;
798                }
799
800/*
801trapcommunity : t_trapcommunity t_equal ct_identifier
802                {
803                        DEBUG_YACC("trap_community")
804
805                        if(trap_community)
806                        {
807                                error("BUG: trap_community not NULL in trap_community");
808                        }
809
810                        trap_community = strdup(yytext);
811                        if(trap_community == NULL)
812                        {
813                                error(ERR_MSG_ALLOC);
814                                YYERROR;
815                        }
816                }
817*/
818
819/*******************/
820/* trapdestinators */
821/*******************/
822
823/*
824trapdestinators : t_trapdestinators t_equal t_openbracket trapdestinators_list t_closebracket
825                {
826                        DEBUG_YACC("trapdestinators")
827                }
828		| t_trapdestinators t_equal t_openbracket error t_closebracket
829		{
830		  error("BUG: trapdestinators stmt parsing error at line %d",yylineno);
831		}
832
833
834trapdestinators_list :  | trapdestinators_list list_separator trapdestinator_item
835                {
836                        DEBUG_YACC("trapdestinators_list")
837                }
838
839
840trapdestinator_item : ct_identifier
841                {
842                        int res;
843
844                        DEBUG_YACC("trapdestinator_item")
845
846                        res = trap_destinator_add(yytext, error_label);
847                        switch(res)
848                        {
849                                case 0:
850                                        break;
851
852                                case 1:
853                                        error("error in %s at line %d: %s",
854                                                current_filename? current_filename:
855"???",
856                                                yylineno, error_label);
857                                        break;
858
859                                default:
860                                        error("fatal error in %s at line %d: %s",
861                                                current_filename? current_filename:
862"???",
863                                                yylineno, error_label);
864                                        YYERROR;
865                        }
866                }
867
868*/
869
870/******************* SNMP security (5-13-96) *********/
871
872
873
874
875/**********/
876/* macros */
877/**********/
878
879macros :	t_macros t_equal t_openbracket macros_list t_closebracket
880		{
881			DEBUG_YACC("macros")
882		}
883		| t_macros t_equal t_openbracket error t_closebracket
884		{ error("BUG at line %d: macro-parsing error", yylineno);
885			parsing_oid = False;
886			if(static_label != NULL) free(static_label);
887			static_label = NULL;
888			if(static_subids != NULL) free(static_subids);
889			static_subids = NULL;
890			static_len = 0;
891		}
892
893
894macros_list :	/* empty */ | macros_list macro_item
895		{
896			DEBUG_YACC("macros_list")
897		}
898
899
900macro_item :	label t_equal
901		{
902			if(parsing_oid != False)
903			{
904				error("BUG at line %d: parsing_oid not False in macro_item", yylineno);
905			}
906			parsing_oid = True;
907
908			if(static_subids != NULL)
909			{
910				error("BUG at line %d: static_subids not NULL in macro_item", yylineno);
911			}
912			if(static_len != 0)
913			{
914				error("BUG at line %d: static_len not 0 in macro_item", yylineno);
915			}
916		}
917		subids_list
918		{
919			DEBUG_YACC("macro_item")
920
921			if(macro_add(static_label, static_subids, static_len) == NULL)
922			{
923				error("error at line %d", yylineno);
924				YYERROR;
925			}
926
927			parsing_oid = False;
928			free(static_label);
929			static_label = NULL;
930			free(static_subids);
931			static_subids = NULL;
932			static_len = 0;
933		}
934
935
936label :		t_identifier
937		{
938			DEBUG_YACC("label")
939
940			if(static_label != NULL)
941			{
942				error("BUG at line %d: static_label not NULL in label", yylineno);
943			}
944			static_label = strdup(yytext);
945			if(static_label == NULL)
946			{
947				error("malloc() failed");
948				YYERROR;
949			}
950		}
951
952/************/
953/* environment */
954/************/
955
956environment:	/*empty*/ | t_environment t_equal t_openbracket environment_list
957		t_closebracket
958		{
959			DEBUG_YACC("environment")
960		}
961
962environment_list: /* empty */ | environment_list environment_item
963		{
964			DEBUG_YACC("environment_list")
965		}
966
967environment_item: 	poll_interval | max_agent_time_out
968		{
969			DEBUG_YACC("environment_item")
970		}
971
972poll_interval:	t_poll_interval t_equal NUMBER
973		{
974			DEBUG_YACC("poll_interval")
975			relay_agent_poll_interval = token_value;
976		}
977
978max_agent_time_out:	t_max_agent_time_out t_equal NUMBER
979		{
980			DEBUG_YACC("max_agent_time_out")
981			relay_agent_max_agent_time_out = token_value;
982		}
983
984
985/***********/
986/* resouces */
987/************/
988
989resources:	/*empty*/ | t_resource t_equal t_openbracket resources_list t_closebracket
990		{
991			DEBUG_YACC("resources")
992		}
993
994resources_list:	/*empty*/ | resources_list resource_item
995			{
996				DEBUG_YACC("resources_list")
997			}
998resource_item:	t_openbracket
999		{
1000		  if(static_res != NULL)
1001			error("BUG at line%d: static_res not NULL",yylineno);
1002		  static_res = malloc(sizeof(SapResource));
1003		  if(static_res == NULL)
1004		  {
1005			error("malloc() failed");
1006			YYERROR;
1007		  }
1008		  memset(static_res,0,sizeof(SapResource));
1009		}
1010		fileslist policy res_type user start_cmd t_closebracket
1011		{
1012			DEBUG_YACC("agent_item")
1013		   if(res_parsing_state == RES_PARSING_STATE_RE_READ){
1014			if(reconfig_first_res == NULL)
1015			{
1016				static_res->next_res = NULL;
1017			}
1018			else
1019			{
1020				static_res->next_res = reconfig_first_res;
1021			}
1022			reconfig_first_res = static_res;
1023		   }else{
1024			if(first_res == NULL)
1025			{
1026				static_res->next_res = NULL;
1027			}
1028			else
1029			{
1030				static_res->next_res = first_res;
1031			}
1032			first_res = static_res;
1033		  }
1034		  static_res = NULL;
1035		}
1036		| t_openbracket error t_closebracket
1037		{
1038			error("BUG at line %d: resource stmt error",yylineno);
1039		   if(static_res != NULL){
1040			if(static_res->dir_file != NULL)
1041				free(static_res->dir_file);
1042			if(static_res->personal_file != NULL)
1043				free(static_res->personal_file);
1044			if(static_res->sec_file != NULL)
1045				free(static_res->sec_file);
1046			if(static_res->policy != NULL)
1047				free(static_res->policy);
1048			if(static_res->type != NULL)
1049				free(static_res->type);
1050			if(static_res->start_cmd != NULL)
1051				free(static_res->start_cmd);
1052			free(static_res);
1053		   }
1054			static_res = NULL;
1055		}
1056
1057
1058fileslist:	file_item | fileslist file_item
1059		{
1060			DEBUG_YACC("fileslist");
1061		}
1062file_item:	personal_file | sec_file | directory_file
1063		{
1064			DEBUG_YACC("file_item");
1065		}
1066
1067personal_file:	t_registration_file t_equal t_quotedstring
1068		{
1069		  DEBUG_YACC("personal_file")
1070		  if(static_res->personal_file != NULL)
1071			error("BUG at line %d: static_res->personal_file not NULL ",yylineno);
1072		  static_res->personal_file = strdup(quoted_string);
1073		  if(static_res->personal_file == NULL){
1074			error("malloc() failed");
1075			YYERROR;
1076		  }
1077		}
1078sec_file:	t_sec_fname t_equal t_quotedstring
1079		{
1080		  DEBUG_YACC("sec_file")
1081		  if(static_res->sec_file != NULL)
1082			error("BUG at line %d: static_res->sec_file not NULL ",yylineno);
1083		  static_res->sec_file = strdup(quoted_string);
1084		  if(static_res->sec_file == NULL){
1085			error("malloc() failed");
1086			YYERROR;
1087		  }
1088		}
1089
1090directory_file:	t_dir_fname t_equal t_quotedstring
1091		{
1092		  DEBUG_YACC("directory_file")
1093		  if(static_res->dir_file != NULL)
1094			error("BUG at line %d: static_res->dir_file not NULL ",yylineno);
1095		  static_res->dir_file = strdup(quoted_string);
1096		  if(static_res->dir_file == NULL){
1097			error("malloc() failed");
1098			YYERROR;
1099		  }
1100		}
1101
1102policy: 	/*empty*/ | t_policy t_equal t_quotedstring
1103		{
1104		  DEBUG_YACC("policy")
1105		  if(static_res->policy != NULL)
1106			error("BUG at line %d: static_res->policy not NULL ",yylineno);
1107		  static_res->policy = strdup(quoted_string);
1108		  if(static_res->policy == NULL){
1109			error("malloc() failed");
1110			YYERROR;
1111		  }
1112		}
1113
1114
1115user: 	/*empty*/ | t_user t_equal t_quotedstring
1116		{
1117		  DEBUG_YACC("user")
1118		  if(static_res->user != NULL)
1119			error("BUG at line %d: static_res->user not NULL ",yylineno);
1120		  static_res->user = strdup(quoted_string);
1121		  if(static_res->user == NULL){
1122			error("malloc() failed");
1123			YYERROR;
1124		  }
1125		}
1126
1127res_type: 	/*empty*/ | t_res_type t_equal t_quotedstring
1128		{
1129		  DEBUG_YACC("res_type")
1130		  if(static_res->type != NULL)
1131			error("BUG at line %d: static_res->type not NULL ",yylineno);
1132		  static_res->type = strdup(quoted_string);
1133		  if(static_res->type == NULL){
1134			error("malloc() failed");
1135			YYERROR;
1136		  }
1137		}
1138
1139
1140
1141start_cmd:	t_command t_equal t_quotedstring
1142		{
1143		  DEBUG_YACC("start_cmd")
1144		  if(static_res->start_cmd != NULL)
1145			error("BUG at line %d: static_res->start_cmd not NULL ",yylineno);
1146		  static_res->start_cmd = strdup(quoted_string);
1147		  if(static_res->start_cmd == NULL){
1148			error("malloc() failed");
1149			YYERROR;
1150		  }
1151		}
1152
1153t_resource:	RESOURCE
1154		{
1155			DEBUG_YACC("t_resource");
1156		}
1157
1158t_registration_file:	REGISTRATION_FILE
1159		{
1160			DEBUG_YACC("t_registration_file");
1161		}
1162
1163t_sec_fname:	SECURITY
1164		{
1165			DEBUG_YACC("t_sec_file");
1166		}
1167
1168t_dir_fname:	DIRECTORY
1169		{
1170			DEBUG_YACC("t_dir_fname");
1171		}
1172
1173t_policy:	POLICY
1174		{
1175			DEBUG_YACC("t_policy");
1176		}
1177
1178t_res_type:	TYPE
1179		{
1180			DEBUG_YACC("t_res_type");
1181		}
1182
1183t_user:		USER
1184		{
1185			DEBUG_YACC("t_user");
1186		}
1187
1188t_command:	COMMAND
1189		{
1190			DEBUG_YACC("t_command");
1191		}
1192
1193/**********/
1194/* agents */
1195/**********/
1196
1197agents :	t_agents t_equal t_openbracket agents_list t_closebracket
1198		{
1199			DEBUG_YACC("agents")
1200		}
1201
1202
1203agents_list :	/*empty */ | agents_list agent_item
1204		{
1205			DEBUG_YACC("agents_list")
1206		}
1207
1208
1209agent_item :		t_openbracket
1210		{
1211			if(static_agent != NULL)
1212			{
1213				error("BUG at line %d: static_agent not NULL in agent", yylineno);
1214			}
1215			static_agent = malloc(sizeof(Agent));
1216			if(static_agent == NULL)
1217			{
1218				error("malloc() failed");
1219				YYERROR;
1220			}
1221			memset(static_agent, 0, sizeof(Agent));
1222			static_agent->agentID = sap_agent_id++;
1223			/* Bug fix 4145620 - The subagents listen on the loopback driver */
1224			static_agent->address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1225			static_agent->agentStatus = SSA_OPER_STATUS_INIT;
1226		}
1227		name subtrees_tables timeout optional_watch_dog_time optional_port t_closebracket
1228		{
1229			DEBUG_YACC("agent_item");
1230
1231			if(first_agent == NULL)
1232			{
1233				static_agent->next_agent = NULL;
1234			}
1235			else
1236			{
1237				static_agent->next_agent = first_agent;
1238			}
1239			first_agent = static_agent;
1240			static_agent = NULL;
1241		}
1242		| t_openbracket error t_closebracket
1243		{
1244			error("BUG at line %d: agent statement error",yylineno);
1245			if(static_agent != NULL){
1246				delete_all_subtree_from_agent(static_agent);
1247				delete_all_tables_for_agent(static_agent);
1248				if(static_agent->agentName.chars != NULL){
1249				  free(static_agent->agentName.chars);
1250				  static_agent->agentName.chars = NULL;
1251				}
1252				static_agent->agentName.len = 0;
1253				if(static_agent->name != NULL){
1254					free(static_agent->name);
1255					static_agent->name = NULL;
1256				}
1257				free(static_agent);
1258				static_agent = NULL;
1259			}
1260			/* clean up */
1261		}
1262
1263
1264name :		t_name t_equal t_quotedstring
1265		{
1266			DEBUG_YACC("name")
1267
1268			if(static_agent->name != NULL)
1269			{
1270				error("BUG at line %d: static_agent->name not NULL in name", yylineno);
1271			}
1272			static_agent->name = strdup(quoted_string);
1273			(static_agent->agentName).chars =
1274				(u_char*)strdup(static_agent->name);
1275			(static_agent->agentName).len = strlen(static_agent->name);
1276
1277			if(static_agent->name == NULL)
1278			{
1279				error("malloc() failed");
1280				YYERROR;
1281			}
1282		}
1283
1284
1285subtrees_tables :  subtrees tables | subtrees | tables
1286		{
1287			DEBUG_YACC("subtrees_tables")
1288		}
1289
1290
1291subtrees :	t_subtrees t_equal t_openbracket
1292		{
1293			if(parsing_oid != False)
1294			{
1295				error("BUG at line %d: parsing_oid is not False in subtrees", yylineno);
1296			}
1297			parsing_oid = True;
1298
1299			if(static_subids != NULL)
1300			{
1301				error("BUG at line %d: static_subids not NULL in subtrees", yylineno);
1302			}
1303			if(static_len != 0)
1304			{
1305				error("BUG at line %d: static_len not 0 in subtrees", yylineno);
1306			}
1307		}
1308		subtrees_list t_closebracket
1309		{
1310			DEBUG_YACC("subtrees")
1311
1312			if(parsing_oid != True)
1313			{
1314				error("BUG at line %d: parsing_oid is not True in subtrees", yylineno);
1315			}
1316			parsing_oid = False;
1317		}
1318
1319
1320subtrees_list : /* empty */ | subtrees_list_comma_separated
1321		{
1322			DEBUG_YACC("subtrees_list")
1323		}
1324
1325
1326subtrees_list_comma_separated : subtree_item | subtrees_list_comma_separated t_comma subtree_item
1327		{
1328			DEBUG_YACC("subtrees_list_comma_separated")
1329		}
1330
1331
1332subtree_item : subids_list
1333		{
1334			Subtree *sp;
1335
1336
1337			DEBUG_YACC("subtree_item")
1338
1339			if(parsing_oid != True)
1340			{
1341				error("BUG at line %d: parsing_oid is not True in subtree_item", yylineno);
1342			}
1343
1344			if(subtree_add(static_agent, static_subids, static_len,NULL) == -1)
1345			{
1346				error("error at line %d", yylineno);
1347				YYERROR;
1348			}
1349
1350			/* add the mirror table(mibpatch) */
1351			/* assume that the subtree is the first agent
1352			   subtree */
1353			sp = static_agent->first_agent_subtree;
1354			create_mirror_table_from_subtree(sp);
1355
1356			free(static_subids);
1357			static_subids = NULL;
1358			static_len = 0;
1359			found_comma = FALSE;
1360		}
1361
1362
1363tables :	t_tables t_equal t_openbracket tables_list t_closebracket
1364		{
1365			DEBUG_YACC("tables")
1366		}
1367
1368
1369tables_list :	/* empty */ | tables_list table_item
1370		{
1371			DEBUG_YACC("tables_list")
1372		}
1373
1374
1375table_item :	t_openbracket
1376		{
1377			if(static_agent == NULL)
1378			{
1379				error("BUG at line %d: static_agent is NULL in table_item", yylineno);
1380			}
1381
1382			if(static_table)
1383			{
1384				error("BUG at line %d: static_table not NULL in table_item", yylineno);
1385			}
1386
1387			static_table = calloc(1,sizeof(Table));
1388			if(static_table == NULL)
1389			{
1390				error("malloc() failed");
1391				YYERROR;
1392			}
1393			static_table->regTblStatus = SSA_OPER_STATUS_ACTIVE;
1394			static_table->next_table = NULL;
1395			static_table->agent = static_agent;
1396			static_table->regTblAgentID = static_agent->agentID;
1397			static_table->regTblIndex = ++static_agent->agentTblIndex;
1398			static_table->name.subids = NULL;
1399			static_table->name.len = 0;
1400			static_table->first_column_subid = 0;
1401			static_table->last_column_subid = 0;
1402/*
1403			static_table->indexs.subids = NULL;
1404			static_table->indexs.len = 0;
1405*/
1406		}
1407		table columns indexs t_closebracket
1408		{
1409			DEBUG_YACC("table_item")
1410
1411			if(static_table == NULL)
1412			{
1413				error("BUG at line %d: static_table is NULL in table_item", yylineno);
1414			}else{
1415			  /* check for the validation of the table,
1416		  	   * if insertion is ok, then put it into the
1417			   * table lists
1418			   */
1419
1420			  if(single_table_to_subtrees(TABLE_TO_OID_TRY,
1421			    static_table,error_label) == -1){
1422				/* may need more elaboration in error */
1423				error("Table %d insertion failed",
1424				(static_table->name.subids)?
1425				SSAOidString(&(static_table->name)):"");
1426				table_free(static_table);
1427			  }
1428			  if(single_table_to_subtrees(TABLE_TO_OID_GO,
1429			    static_table,error_label) != -1){
1430
1431				if(first_table==NULL){
1432				  first_table =static_table;
1433				}else{
1434				  for(tmp_table=first_table;tmp_table;
1435				      tmp_table=tmp_table->next_table)
1436					last_table = tmp_table;
1437				  last_table->next_table = static_table;
1438				}
1439			  }
1440			}
1441			static_table = NULL;
1442		}
1443
1444
1445table :		t_table t_equal
1446		{
1447			if(parsing_oid != False)
1448			{
1449				error("BUG at line %d: parsing_oid is not False in tables", yylineno);
1450			}
1451
1452			parsing_oid = True;
1453		}
1454		subids_list
1455		{
1456			DEBUG_YACC("table")
1457
1458			if(parsing_oid != True)
1459			{
1460				error("BUG at line %d: parsing_oid is not True in tables", yylineno);
1461			}
1462			parsing_oid = False;
1463
1464			if(static_table == NULL)
1465			{
1466				error_exit("BUG at line %d: static_table is NULL in table", yylineno);
1467			}
1468
1469			static_table->name.subids = static_subids;
1470			static_subids = NULL;
1471			static_table->name.len = static_len;
1472			static_len = 0;
1473		}
1474
1475
1476columns :	t_columns t_equal range
1477		{
1478			DEBUG_YACC("columns")
1479
1480			if(static_table == NULL)
1481			{
1482				error_exit("BUG at line %d: static_table is NULL in columns", yylineno);
1483			}
1484
1485			static_table->first_column_subid = static_inf_value;
1486			static_inf_value = -1;
1487			static_table->last_column_subid = static_sup_value;
1488			static_sup_value = -1;
1489		}
1490
1491
1492/*
1493indexs :	t_indexs t_equal
1494		{
1495			if(parsing_oid != False)
1496			{
1497				error("BUG at line %d: parsing_oid is not False in indexs", yylineno);
1498			}
1499
1500			parsing_oid = True;
1501		}
1502		subids_list
1503		{
1504			DEBUG_YACC("indexs")
1505
1506			if(parsing_oid != True)
1507			{
1508				error("BUG at line %d: parsing_oid is not True in indexs", yylineno);
1509			}
1510			parsing_oid = False;
1511
1512			if(static_table == NULL)
1513			{
1514				error_exit("BUG at line %d: static_table is NULL in indexs", yylineno);
1515			}
1516
1517			static_table->indexs.subids = static_subids;
1518			static_subids = NULL;
1519			static_table->indexs.len = static_len;
1520			static_len = 0;
1521		}
1522*/
1523
1524indexs :	t_indexs t_equal range
1525		{
1526			DEBUG_YACC("indexs")
1527
1528			if(static_inf_value == -1)
1529			{
1530				error("BUG at line %d: static_inf_value is -1", yylineno);
1531			}
1532			if(static_sup_value == -1)
1533			{
1534				error("BUG at line %d: static_sup_value is -1", yylineno);
1535			}
1536			static_table->first_index_subid = static_inf_value;
1537			static_table->last_index_subid = static_sup_value;
1538			static_inf_value = -1;
1539			static_sup_value = -1;
1540		}
1541
1542
1543range :		t_opensquarebracket t_number
1544		{
1545			if(static_inf_value != -1)
1546			{
1547				error("BUG at line %d: static_inf_value (%d) is not -1 in range",
1548					yylineno,
1549					static_inf_value);
1550			}
1551
1552			static_inf_value = token_value;
1553		}
1554		t_minus t_number
1555		{
1556			if(static_sup_value != -1)
1557			{
1558				error("BUG at line %d: static_sup_value (%d) is not -1 in range",
1559					yylineno,
1560					static_inf_value);
1561			}
1562
1563			static_sup_value = token_value;
1564		}
1565		t_closesquarebracket
1566		{
1567			DEBUG_YACC("range")
1568		}
1569		| t_number
1570		{
1571			if(static_inf_value != -1)
1572			{
1573				error("BUG at line %d: static_inf_value (%d) is not -1 in range",
1574					yylineno,
1575					static_inf_value);
1576			}
1577			if(static_sup_value != -1)
1578			{
1579				error("BUG at line %d: static_sup_value (%d) is not -1 in range",
1580					yylineno,
1581					static_sup_value);
1582			}
1583
1584			static_inf_value = token_value;
1585			static_sup_value = token_value;
1586		}
1587
1588
1589timeout :	t_timeout t_equal t_number
1590		{
1591			DEBUG_YACC("subtree")
1592
1593			static_agent->agentTimeOut =static_agent->timeout = token_value;
1594		}
1595
1596optional_watch_dog_time : /*empty*/ |	t_watch_dog_time t_equal NUMBER
1597		{
1598			DEBUG_YACC("optional_watch_dog_time")
1599			static_agent->agentWatchDogTime = token_value;
1600		}
1601
1602
1603optional_port : /*empty*/ | port
1604	{
1605		DEBUG_YACC("optional_port")
1606	}
1607
1608port :		t_port t_equal t_number
1609		{
1610			DEBUG_YACC("port")
1611
1612			if(token_value > 0xFFFF)
1613			{
1614				error("error at line %d: the port number (%d) should not be greater than %d", yylineno, token_value, 0xFFFF);
1615				YYERROR;
1616			}
1617
1618			static_agent->address.sin_port = (short) token_value;
1619			static_agent->agentPortNumber =
1620				static_agent->address.sin_port;
1621
1622			if(agent_find(&(static_agent->address)))
1623			{
1624				error("error at line %d: the port number %d is already used by another agent", yylineno, token_value);
1625				YYERROR;
1626			}
1627		}
1628
1629
1630
1631/***************/
1632/* subids_list */
1633/***************/
1634
1635subids_list :	subid | subids_list t_dot subid
1636		{
1637			DEBUG_YACC("subids_list")
1638                        found_dot = FALSE;
1639		}
1640
1641
1642subid :		t_mib2 | t_sun | t_enterprise | t_identifier | t_number
1643		{
1644			DEBUG_YACC("subid")
1645		}
1646
1647
1648
1649/*******************/
1650/* terminal tokens */
1651/*******************/
1652
1653/**************** SNMP security (5-13-96) ***/
1654ct_identifier : IDENTIFIER
1655		{
1656			DEBUG_YACC("ct_indentifier")
1657/*
1658                        if(save_string == NULL)
1659                        {
1660                           error("malloc() failed");
1661                           YYERROR;
1662                        }
1663*/
1664                        save_string = strdup(yytext);
1665
1666
1667		}
1668
1669t_communities : COMMUNITIES
1670                {
1671                        DEBUG_YACC("t_communities")
1672                }
1673
1674t_hosts : HOSTS
1675        {
1676                DEBUG_YACC("t_hosts")
1677        }
1678
1679t_acls	: ACL
1680	{
1681		DEBUG_YACC("t_acls")
1682	}
1683
1684t_access : ACCESS
1685        {
1686                DEBUG_YACC("t_access")
1687        }
1688
1689
1690t_readonly :    READONLY
1691                {
1692                        DEBUG_YACC("t_readonly")
1693
1694                        community_type = READ_ONLY;
1695                }
1696
1697t_readwrite :   READWRITE
1698                {
1699                        DEBUG_YACC("t_readwrite")
1700
1701                        community_type = READ_WRITE;
1702                }
1703
1704t_managers :    MANAGERS
1705                {
1706                        DEBUG_YACC("t_managers")
1707                }
1708
1709t_trap :        TRAP
1710                {
1711                        DEBUG_YACC("t_trap")
1712                }
1713
1714t_trap_num:     TRAPNUM
1715                {
1716                        DEBUG_YACC("t_trap_num")
1717                }
1718
1719t_trapcommunity : TRAPCOMMUNITY
1720                {
1721                        DEBUG_YACC("t_trapcommunity")
1722                }
1723
1724
1725/*
1726t_trapdestinators : TRAPDESTINATORS
1727                {
1728                        DEBUG_YACC("t_trapdestinators")
1729                }
1730
1731list_separator :  | t_comma
1732                {
1733                        DEBUG_YACC("list_separator")
1734                }
1735*/
1736
1737
1738/**************** SNMP security (5-13-96) ***/
1739
1740
1741t_number :	NUMBER
1742		{
1743			DEBUG_YACC("t_number")
1744
1745			if(parsing_oid == True)
1746			{
1747				if(subids_cat((Subid *) &token_value, 1) == -1)
1748				{
1749					YYERROR;
1750				}
1751			}
1752		}
1753
1754
1755t_macros :	MACROS
1756		{
1757			DEBUG_YACC("t_macros")
1758		}
1759
1760
1761t_equal :	EQUAL
1762		{
1763			DEBUG_YACC("t_equal")
1764		}
1765
1766
1767t_minus :	MINUS
1768		{
1769			DEBUG_YACC("t_minus")
1770		}
1771
1772
1773t_openbracket :	OPENBRACKET
1774		{
1775			DEBUG_YACC("t_openbracket")
1776		}
1777
1778
1779t_closebracket : CLOSEBRACKET
1780		{
1781			DEBUG_YACC("t_closebracket")
1782		}
1783
1784
1785t_opensquarebracket : OPENSQUAREBRACKET
1786		{
1787			DEBUG_YACC("t_opensquarebracket")
1788		}
1789
1790
1791t_closesquarebracket : CLOSESQUAREBRACKET
1792		{
1793			DEBUG_YACC("t_closesquarebracket")
1794		}
1795
1796
1797t_identifier :	IDENTIFIER
1798		{
1799			DEBUG_YACC("t_identifier")
1800
1801			if(parsing_oid == True)
1802			{
1803				Macro *mp;
1804
1805
1806				mp = macro_find(yytext);
1807				if(mp == NULL)
1808				{
1809					error("error at line %d: %s is not a macro", yylineno, yytext);
1810					YYERROR;
1811				}
1812
1813				if(subids_cat(mp->name.subids, mp->name.len) == -1)
1814				{
1815					YYERROR;
1816				}
1817			}
1818		}
1819
1820
1821t_mib2 :	MIB2
1822		{
1823			DEBUG_YACC("t_mib2")
1824
1825			if(parsing_oid == False)
1826			{
1827				error("BUG at line %d: parsing_oid not True in t_mib2", yylineno);
1828			}
1829			if(subids_cat(subids_mib2, mib2_len) == -1)
1830			{
1831				YYERROR;
1832			}
1833		}
1834
1835
1836t_sun :		SUN
1837		{
1838			DEBUG_YACC("t_sun")
1839
1840			if(parsing_oid == False)
1841			{
1842				error("BUG at line %d: parsing_oid not True in t_sun", yylineno);
1843			}
1844			if(subids_cat(subids_sun, sun_len) == -1)
1845			{
1846				YYERROR;
1847			}
1848		}
1849
1850
1851t_enterprise :	ENTERPRISE
1852		{
1853			DEBUG_YACC("t_enterprise")
1854
1855			if(parsing_oid == False)
1856			{
1857				error("BUG at line %d: parsing_oid not True in t_enterprise", yylineno);
1858			}
1859			if(subids_cat(subids_enterprise, enterprise_len) == -1)
1860			{
1861				YYERROR;
1862			}
1863		}
1864
1865t_dot :		DOT
1866		{
1867			DEBUG_YACC("t_dot")
1868                        found_dot=TRUE;
1869		}
1870
1871
1872t_agents :	AGENTS
1873		{
1874			DEBUG_YACC("t_agents")
1875		}
1876
1877
1878t_name :	NAME
1879		{
1880			DEBUG_YACC("t_name")
1881		}
1882
1883
1884t_subtrees :	SUBTREES
1885		{
1886			DEBUG_YACC("t_subtrees")
1887		}
1888
1889
1890t_tables :	TABLES
1891		{
1892			DEBUG_YACC("t_tables")
1893		}
1894
1895
1896t_table :	TABLE
1897		{
1898			DEBUG_YACC("t_table")
1899		}
1900
1901
1902t_columns :	COLUMNS
1903		{
1904			DEBUG_YACC("t_columns")
1905		}
1906
1907
1908t_indexs :	INDEXS
1909		{
1910			DEBUG_YACC("t_indexs")
1911		}
1912
1913
1914t_timeout :	TIMEOUT
1915		{
1916			DEBUG_YACC("t_timeout")
1917		}
1918
1919t_environment:	ENVIRONMENT
1920		{
1921			DEBUG_YACC("t_environment")
1922		}
1923
1924t_watch_dog_time :	WATCHDOGTIME
1925		{
1926			DEBUG_YACC("t_watch_dog_time")
1927		}
1928
1929t_poll_interval: 	POLLINTERVAL
1930		{
1931			DEBUG_YACC("t_poll_interval")
1932		}
1933
1934t_max_agent_time_out:	MAXAGENTTIMEOUT
1935		{
1936			DEBUG_YACC("t_max_agent_time_out")
1937		}
1938
1939
1940t_port :	PORT
1941		{
1942			DEBUG_YACC("t_port")
1943		}
1944
1945
1946t_quotedstring : QUOTEDSTRING
1947		{
1948			DEBUG_YACC("t_quotedstring\n")
1949		}
1950
1951
1952t_comma :	COMMA
1953		{
1954			DEBUG_YACC("t_comma")
1955                        found_comma=TRUE;
1956		}
1957%%
1958
1959#include "config.lex.c"
1960
1961/****************************************************************/
1962
1963static int subids_cat(Subid *subids, int len)
1964{
1965	Subid *new_subids;
1966	int new_len;
1967
1968
1969	new_len = static_len + len;
1970	new_subids = (Subid *) malloc(new_len * sizeof(Subid));
1971	if(new_subids == NULL)
1972	{
1973		error("malloc() failed");
1974		if(static_subids)
1975		{
1976			free(static_subids);
1977		}
1978		static_subids = NULL;
1979		static_len = 0;
1980		return -1;
1981	}
1982	memcpy(new_subids, static_subids, static_len * sizeof(Subid));
1983	memcpy(&(new_subids[static_len]), subids, len * sizeof(Subid));
1984
1985
1986	if(static_subids)
1987	{
1988		free(static_subids);
1989	}
1990	static_subids = new_subids;
1991	static_len = new_len;
1992
1993	return 0;
1994}
1995
1996
1997/****************************************************************/
1998
1999static Macro *macro_add(char *label, Subid *subids, int len)
2000{
2001	Macro *new;
2002
2003
2004	if(macro_find(label) != NULL)
2005	{
2006		error("%s is already a macro", label);
2007		return NULL;
2008	}
2009
2010	new = (Macro *) malloc(sizeof(Macro));
2011	if(new == NULL)
2012	{
2013		error("malloc() failed");
2014		return NULL;
2015	}
2016	new->label = NULL;
2017	new->name.subids = NULL;
2018
2019	new->label = strdup(label);
2020	if(new->label == NULL)
2021	{
2022		error("malloc() failed");
2023		macro_free(new);
2024		return NULL;
2025	}
2026	new->name.subids = (Subid *) malloc(len * sizeof(Subid));
2027	if(new->name.subids == NULL)
2028	{
2029		error("malloc() failed");
2030		macro_free(new);
2031		return NULL;
2032	}
2033	memcpy(new->name.subids, subids, len * sizeof(Subid));
2034	new->name.len = len;
2035	new->next_macro = first_macro;
2036	first_macro = new;
2037
2038	return new;
2039}
2040
2041
2042/****************************************************************/
2043
2044static Macro *macro_find(char *label)
2045{
2046	Macro *mp;
2047
2048
2049	for(mp = first_macro; mp; mp = mp->next_macro)
2050	{
2051		if(strcmp(mp->label, label) == 0)
2052		{
2053			return mp;
2054		}
2055	}
2056
2057	return NULL;
2058}
2059
2060
2061/****************************************************************/
2062
2063static void macro_free(Macro *mp)
2064{
2065	if(mp == NULL)
2066	{
2067		return;
2068	}
2069
2070	if(mp->label)
2071	{
2072		free(mp->label);
2073	}
2074
2075	if(mp->name.subids)
2076	{
2077		free(mp->name.subids);
2078	}
2079
2080	free(mp);
2081
2082	return;
2083}
2084
2085
2086/****************************************************************/
2087
2088static void macro_list_delete()
2089{
2090	Macro *mp = first_macro;
2091	Macro *next;
2092
2093
2094	while(mp)
2095	{
2096		next = mp->next_macro;
2097
2098		macro_free(mp);
2099
2100		mp = next;
2101	}
2102
2103	first_macro = NULL;
2104
2105	return;
2106}
2107
2108
2109/****************************************************************/
2110
2111static void trace_macros()
2112{
2113	Macro *mp;
2114
2115
2116	trace("MACROS:\n");
2117	for(mp = first_macro; mp; mp = mp->next_macro)
2118	{
2119		trace("\t%-30s %-30s\n",
2120			mp->label,
2121			SSAOidString(&(mp->name)));
2122	}
2123	trace("\n");
2124}
2125
2126
2127/****************************************************************/
2128
2129int yyerror(char *s)
2130{
2131	error("%s at line %d: %s", s, yylineno, yytext);
2132	return (0);
2133}
2134
2135
2136/****************************************************************/
2137
2138/*
2139 * filename is the file to be read
2140 * file_time is the modified time of the file, this argument can be NULL
2141 */
2142int parsing_file(char* filename,time_t *file_time)
2143{
2144  struct stat statb;
2145  int fd;
2146  char *fileaddr;
2147  int error_free = TRUE;
2148
2149  yylineno = 1;
2150  if((fd = open(filename, O_RDONLY)) < 0)
2151  {
2152	error(ERR_MSG_OPEN, filename, errno_string());
2153	error_free = FALSE;
2154	return (error_free);
2155  }
2156
2157  /*
2158   * get the size of the file
2159   */
2160  if(fstat(fd, &statb) < 0 )
2161  {
2162	error(ERR_MSG_FSTAT, filename, errno_string());
2163	error_free = FALSE;
2164	return(error_free);
2165   }
2166
2167  if(S_ISREG(statb.st_mode)==0)
2168  {
2169	error(" parsing file error: %s is not a file\n",filename);
2170	error_free = FALSE;
2171	return(error_free);
2172   }
2173
2174   /* file time stamp */
2175   if(file_time) *file_time = statb.st_mtime;
2176
2177   /*
2178    * and map it into my address space
2179    */
2180   if(statb.st_size)
2181   {
2182	if((fileaddr = (char *) mmap(0, statb.st_size, PROT_READ|PROT_WRITE,
2183				MAP_PRIVATE, fd, 0)) <= (char *) 0)
2184	{
2185		error(ERR_MSG_MMAP, filename, errno_string());
2186		error_free = FALSE;
2187		return(error_free);
2188	}
2189
2190	/*
2191	 * set current lex focus on the file
2192	 */
2193
2194	lexinput = fileaddr;
2195
2196	/*
2197	 * and parse the file
2198	 */
2199
2200	if(yyparse() == 1)
2201	{
2202		error("parsing %s failed", filename);
2203		error_free = FALSE;
2204	}
2205
2206	/*
2207	 * Parsing is finished
2208	 *
2209	 * unmap the file and close it
2210	 */
2211
2212	if(munmap(fileaddr, statb.st_size) == -1)
2213	{
2214		error(ERR_MSG_MUNMAP, errno_string());
2215	}
2216  } else {
2217	/* empty file, ignore it */
2218	error("empty configuration file %s", filename);
2219	error_free = FALSE;
2220  }
2221
2222  if(close(fd) == -1)
2223  {
2224	error(ERR_MSG_CLOSE, errno_string());
2225  }
2226
2227  return(error_free);
2228}
2229
2230
2231int personal_file_reading(char* dirname, char* filename, time_t *file_time)
2232{
2233
2234  static char file[100];
2235  int error_free;
2236
2237	file[0] = '\0';
2238	if(dirname != NULL)
2239		sprintf(file, "%s/%s", dirname, filename);
2240	else
2241		sprintf(file, "%s", filename);
2242
2243        error_free = parsing_file(file,file_time);
2244	macro_list_delete();
2245	return(error_free);
2246}
2247
2248/* If we have a serious problem, this function will	*/
2249/* terminate (<==> exit) the program			*/
2250/* now it won't call error_exit, call error only and
2251   return FALSE */
2252
2253int config_init(char *dirname)
2254{
2255	DIR *dirp;
2256	struct dirent *direntp;
2257	int error_free = TRUE;
2258	struct stat statbuf;
2259
2260
2261	dirp = opendir(dirname);
2262	if(dirp == NULL)
2263	{
2264		error(ERR_MSG_OPENDIR,
2265			dirname, errno_string());
2266		error_free = FALSE;
2267		return error_free;
2268	}
2269
2270	if(stat(dirname,&statbuf)>=0 &&
2271	   S_ISDIR(statbuf.st_mode)==0)
2272	{
2273		error(ERR_MSG_OPENDIR,
2274			dirname, errno_string());
2275		error_free = FALSE;
2276		return error_free;
2277	}
2278
2279	while((direntp = readdir(dirp)) != NULL)
2280	{
2281
2282
2283		if(strcmp(direntp->d_name,SNMPRELAY_REG_FILE))
2284			continue;
2285
2286		sprintf(config_file_4_res, "%s/%s", dirname, direntp->d_name);
2287
2288		if(parsing_file(config_file_4_res,NULL) == FALSE){
2289		  error_free = FALSE;
2290		  continue;
2291		}
2292
2293		macro_list_delete();
2294
2295		config_file_4_res[0] = '\0';
2296	}
2297
2298	if(closedir(dirp) == -1)
2299	{
2300		error(ERR_MSG_CLOSEDIR, dirname, errno_string());
2301	}
2302
2303/*
2304	if(tables_to_subtrees(error_label))
2305	{
2306		error("tables_to_subtrees() failed: %s", error_label);
2307		error_free = FALSE;
2308	}
2309	table_list_delete();
2310*/
2311
2312
2313	if(first_agent == NULL)
2314	{
2315		error("No SNMP agent configured");
2316		error_free = FALSE;
2317	}
2318
2319	if(trace_level > 0)
2320	{
2321		trace_subtrees();
2322		trace_agents();
2323	}
2324	return(error_free);
2325}
2326
2327
2328
2329/****************************************************************/
2330/*
2331 * if pass is  TRY, see can we add all the subtrees of the table
2332 * if pass is GO, we really go ahead and add the subtrees
2333 */
2334int single_table_to_subtrees(int pass,Table *tp, char* error_label)
2335{
2336  /* update the columnar object for the table */
2337	Subtree *sp;
2338	Subid one = 1;
2339	Subid column;
2340	Subid index;
2341	TblTag *tbl_tag=NULL;
2342
2343
2344	error_label[0] = '\0';
2345
2346	if(static_subids)
2347	{
2348		error("BUG: tables_to_subtrees(): static_subids not NULL");
2349		free(static_subids);
2350		static_subids = NULL;
2351	}
2352	if(static_len)
2353	{
2354		error("BUG: tables_to_subtrees(): static_len not 0");
2355		static_len = 0;
2356	}
2357
2358
2359
2360		for(index = tp->first_index_subid; index <= tp->last_index_subid; index++)
2361		{
2362			for(column = tp->first_column_subid; column <= tp->last_column_subid; column++)
2363			{
2364				if(pass == TABLE_TO_OID_GO){
2365				  tbl_tag = (TblTag*)malloc(sizeof(TblTag));
2366				  if(tbl_tag != NULL){
2367				    tbl_tag->entry_index = tp->first_index_subid;
2368				    tbl_tag->type = TBL_TAG_TYPE_LEAF;
2369				    tbl_tag->table = tp;
2370				  }
2371				}
2372				if(subids_cat(tp->name.subids, tp->name.len))
2373				{
2374					return -1;
2375				}
2376				if(subids_cat(&one, 1) == -1)
2377				{
2378					return -1;
2379				}
2380				if(subids_cat(&column, 1) == -1)
2381				{
2382					return -1;
2383				}
2384				if(subids_cat(&index, 1) == -1)
2385				{
2386					return -1;
2387				}
2388
2389				if(pass == TABLE_TO_OID_GO &&subtree_add(tp->agent, static_subids, static_len,tbl_tag) == -1)
2390				{
2391					sprintf(error_label, "subtree_add() failed for table %s for the agent %s",
2392						SSAOidString(&(tp->name)),
2393						tp->agent->name);
2394					if(static_subids)
2395					{
2396						free(static_subids);
2397					}
2398					if(tbl_tag)
2399						free(tbl_tag);
2400					tbl_tag = NULL;
2401					static_subids = NULL;
2402					static_len = 0;
2403					return -1;
2404				}
2405				if(pass == TABLE_TO_OID_TRY &&
2406				   (sp=subtree_find(static_subids,static_len))
2407				   != NULL){
2408					return -1;
2409				}
2410
2411				if(static_subids)
2412				{
2413					free(static_subids);
2414				}
2415				static_subids = NULL;
2416				static_len = 0;
2417				tbl_tag = NULL;
2418			}
2419		}
2420
2421
2422	return 0;
2423}
2424
2425/* This function translates the tables in subtrees	*/
2426
2427
2428
2429/*********** SNMP security (5-13-96) ******/
2430/* If we have a serious problem, this function will	*/
2431/* terminate (<==> exit) the program			*/
2432
2433int sec_config_init(char *filename)
2434{
2435	int error_free = TRUE;
2436	Manager *manager;
2437
2438
2439	delete_manager_list();
2440	delete_community_list();
2441	if(trap_community)
2442	{
2443		free(trap_community);
2444		trap_community = NULL;
2445	}
2446	delete_trap_destinator_list();
2447
2448 	error_free = parsing_file(filename,NULL);
2449
2450	if(trace_level > 0)
2451	{
2452		trace("\n");
2453		trace_managers();
2454		trace_filter();
2455		trace_trap_destinators();
2456	}
2457
2458	return(error_free);
2459}
2460
2461int res_file_init(char *dirname,SapResource *sp)
2462{
2463	DIR *dirp;
2464	struct dirent *direntp;
2465	int error_free = TRUE;
2466  	time_t file_time=0;
2467
2468
2469	dirp = opendir(dirname);
2470	if(dirp == NULL)
2471	{
2472		error(ERR_MSG_OPENDIR,
2473			dirname, errno_string());
2474		error_free = FALSE;
2475		return error_free;
2476	}
2477
2478	while((direntp = readdir(dirp)) != NULL)
2479	{
2480
2481		int pos;
2482
2483		pos = strlen(direntp->d_name) - strlen(SNMPRESOURCE_SUFFIX);
2484		if( (pos<0) ||
2485		    strcmp(&(direntp->d_name[pos]),SNMPRESOURCE_SUFFIX))
2486			continue;
2487
2488		sprintf(config_file_4_res, "%s/%s", dirname, direntp->d_name);
2489
2490		if(parsing_file(config_file_4_res,&file_time) == FALSE){
2491		  error_free = FALSE;
2492		  continue;
2493		}
2494		if(sp!=NULL)
2495			sp->rsrc_file_time = file_time;
2496
2497		config_file_4_res[0] = '\0';
2498	}
2499
2500	if(closedir(dirp) == -1)
2501	{
2502		error(ERR_MSG_CLOSEDIR, dirname, errno_string());
2503	}
2504
2505  return(error_free);
2506}
2507/*********** SNMP security (5-13-96) ******/
2508
2509void res_config_init(char *dirname)
2510{
2511	int error_free;
2512   	SapResource *rp;
2513
2514	/* delete_resource_list() */
2515
2516	/* if recovery , delete all processes in the pid file */
2517	read_pid_file(pid_file);
2518
2519	if(recovery_on == FALSE) kill_all_pid_rec_list();
2520
2521	/* MRF */
2522	/* parsing the resource files in the default directory */
2523	/* last_res_modify_time should be the largest one */
2524	error_free = res_file_init(dirname,first_res);
2525
2526   	for(rp=first_res; rp ; rp=rp->next_res){
2527		resource_handling(rp);
2528	}
2529
2530	read_acl();
2531
2532	if(recovery_on == TRUE) kill_part_pid_rec_list();
2533
2534	recovery_on = FALSE; /* recovery is done */
2535	delete_pid_rec_list();
2536
2537	/* should update pid file */
2538
2539}
2540
2541int resource_update(char *dirname)
2542{
2543  time_t file_time;
2544  int error_free = TRUE;
2545
2546	/* MRF: find out the largest latest time stamp of resource files */
2547
2548  /* mark the resouce element to be kept */
2549  mark_all_resources_not_visit();
2550  res_parsing_state = RES_PARSING_STATE_RE_READ;
2551  reconfig_first_res = NULL;
2552
2553  delete_pid_rec_list();
2554  read_pid_file(pid_file);
2555
2556	/* MRF: reading all resource file again */
2557  /* parsing the resource file */
2558  error_free = res_file_init(dirname,reconfig_first_res);
2559
2560  if(error_free==FALSE){
2561	error("parsing error in reading the resouce file %s:%s",
2562		dirname,error_label);
2563	return FALSE;
2564  }
2565
2566  merging_resource_list();
2567
2568  read_acl();
2569
2570  write_pid_file(pid_file);
2571
2572  res_parsing_state = RES_PARSING_STATE_FROM_SCRATCH;
2573
2574  return TRUE;
2575}
2576
2577int read_acl()
2578{
2579	SapResource * sp, * nextsp;
2580	Agent *agent;
2581	int error_free=TRUE;
2582
2583	sp = first_res;
2584	while (sp) {
2585		if (sp->agent && sp->agent->first_manager) {
2586			agent_manager_list_free (sp->agent->first_manager);
2587			sp->agent->first_manager = NULL;
2588		}
2589		sp = sp->next_res;
2590	}
2591	init_manager_set ();
2592	sp = first_res;
2593	while (sp) {
2594		if (sp->sec_file) {
2595 			error_free = parsing_file(sp->sec_file, NULL);
2596
2597			if(trace_level > 0)
2598			{
2599				trace("\n");
2600				trace_agents();
2601				trace_managers();
2602				trace_filter();
2603				trace_trap_destinators();
2604			}
2605
2606			if (error_free) {
2607				if (sp->agent)
2608					sp->agent->first_manager = get_curr_manager_set();
2609				init_manager_set ();
2610			}
2611
2612		}
2613		sp = sp->next_res;
2614	}
2615	return TRUE;
2616}
2617int read_agent_acl(SapResource * sp)
2618{
2619	Agent *agent;
2620	int error_free=TRUE;
2621
2622	if (sp) {
2623		if (sp->agent && sp->agent->first_manager) {
2624			agent_manager_list_free (sp->agent->first_manager);
2625			sp->agent->first_manager = NULL;
2626		}
2627	}
2628	init_manager_set ();
2629	if (sp) {
2630		if (sp->sec_file) {
2631 			error_free = parsing_file(sp->sec_file, NULL);
2632
2633			if(trace_level > 0)
2634			{
2635				trace("\n");
2636				trace_agents();
2637				trace_managers();
2638				trace_filter();
2639				trace_trap_destinators();
2640			}
2641
2642			if (error_free) {
2643				if (sp->agent)
2644					sp->agent->first_manager = get_curr_manager_set();
2645				init_manager_set ();
2646			}
2647
2648		}
2649	}
2650	return TRUE;
2651}
2652
2653int read_agent_files (Agent * ap)
2654{
2655	SapResource *sp, *firstguy, *next;
2656	time_t file_time;
2657
2658	if (ap == NULL) {
2659		return 1;
2660	}
2661	if (trace_level > 0)
2662		trace ("read_agent_files() agent -%s- ipaddr %s timeout %d id %d status %d pid %d mgr %X\n",
2663			ap->name ? ap->name : "NO NAME",
2664			address_string (&(ap->address)),
2665			ap->timeout, ap->agentID, ap->agentStatus,
2666			ap->agentProcessID, ap->first_manager);
2667
2668	if (ap->name == NULL || *(ap->name) == 0) {
2669		return 1;
2670	}
2671
2672	/* find an existing SapResource - if found, update the acl
2673	** for this agent
2674	*/
2675	sp = resource_find_by_name (ap->name);
2676	if (sp) {
2677		read_agent_acl (sp);
2678		sp->agent = ap;
2679		return 0;
2680	}
2681
2682	firstguy = first_res;
2683	first_res = NULL;
2684	if (static_res) {
2685		resource_free (static_res);
2686	}
2687	static_res = NULL;
2688	/* see if the agent has a personal resource file - if found,
2689	** create a new SapResource with this
2690	** agentPersonalFile is the .reg file
2691	** agentConfigFile is the .rsrc file
2692	*/
2693	res_parsing_state = RES_PARSING_STATE_FROM_SCRATCH;
2694	if (ap->agentConfigFile.len) {
2695		if (parsing_file((char *)(ap->agentConfigFile.chars),&file_time) == TRUE) {
2696			first_res->next_res = firstguy;
2697			first_res->agent = ap;
2698			static_res = NULL;
2699			return 0;
2700		}
2701	}
2702
2703	/* else, parse all the resource files in the default directory
2704	** to find one whose agent name matches this one, and use it
2705	*/
2706	res_parsing_state = RES_PARSING_STATE_FROM_SCRATCH;
2707	res_file_init (config_dir, NULL);
2708	sp = first_res;
2709	while (sp) {
2710		if (sp->res_name && !strcmp (sp->res_name, ap->name))
2711			break;
2712		else if (sp->personal_file && ap->agentPersonalFile.chars && !strcmp (sp->personal_file, (char *) (ap->agentPersonalFile.chars)))
2713			break;
2714		else
2715			sp = sp->next_res;
2716	}
2717	if (sp) {
2718		resource_detach (sp);
2719		sp->next_res = firstguy;
2720		firstguy = sp;
2721		firstguy->agent = ap;
2722		if (firstguy->res_name == NULL)
2723			firstguy->res_name = strdup (ap->name);
2724	}
2725
2726	sp = first_res;
2727	while (sp) {
2728		next = sp->next_res;
2729		resource_free (sp);
2730		sp = next;
2731	}
2732	first_res = firstguy;
2733	read_agent_acl (first_res);
2734	return 0;
2735}
2736
2737