xref: /onnv-gate/usr/src/cmd/agents/snmp/trapsend/oid.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * Copyright 1996 Sun Microsystems, Inc.  All Rights Reserved.
23*0Sstevel@tonic-gate  * Use is subject to license terms.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <stdio.h>
29*0Sstevel@tonic-gate #include <string.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <unistd.h>
32*0Sstevel@tonic-gate #include <sys/types.h>
33*0Sstevel@tonic-gate #include <netinet/in.h>
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <sys/socket.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <syslog.h>
38*0Sstevel@tonic-gate #include <string.h>
39*0Sstevel@tonic-gate #include <arpa/inet.h>
40*0Sstevel@tonic-gate #include <netdb.h>
41*0Sstevel@tonic-gate #include <nlist.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #include "snmp_msg.h"
44*0Sstevel@tonic-gate #include "impl.h"
45*0Sstevel@tonic-gate #include "trace.h"
46*0Sstevel@tonic-gate #include "snmp.h"
47*0Sstevel@tonic-gate #include "pdu.h"
48*0Sstevel@tonic-gate #include "trap.h"
49*0Sstevel@tonic-gate #include "error.h"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #define BUFSIZE 256
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /* get_oid_from_file
55*0Sstevel@tonic-gate    it finds the first oid which enterprise string match the input.
56*0Sstevel@tonic-gate    */
57*0Sstevel@tonic-gate 
get_oid_with_name(char * inbuf,char * enterprise_str)58*0Sstevel@tonic-gate static Oid *get_oid_with_name(char *inbuf, char *enterprise_str)
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	char *str;
61*0Sstevel@tonic-gate 	char *name_ptr;
62*0Sstevel@tonic-gate 	Oid  *oid = NULL;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if ((inbuf== NULL) || (inbuf[0]== '#')) return (NULL);
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	/* first "  for name */
67*0Sstevel@tonic-gate 	if ((str = strchr(inbuf, '"')) == NULL) return (NULL);
68*0Sstevel@tonic-gate 	*str++;
69*0Sstevel@tonic-gate 	name_ptr = str;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 		/* second " for name */
72*0Sstevel@tonic-gate 	if ((str = strchr(str, '"')) == NULL) return (NULL);
73*0Sstevel@tonic-gate 	*str = '\0';
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (!strncasecmp(name_ptr, enterprise_str, strlen(enterprise_str))) {
76*0Sstevel@tonic-gate 		*str++;
77*0Sstevel@tonic-gate 		/* first " for oid_str*/
78*0Sstevel@tonic-gate 		if ((str = strchr(str, '"')) == NULL)  return (NULL);
79*0Sstevel@tonic-gate 		*str++;
80*0Sstevel@tonic-gate 		name_ptr = str;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 		/* second " for oid_str*/
83*0Sstevel@tonic-gate 		if ((str = strchr(str, '"')) == NULL) return (NULL);
84*0Sstevel@tonic-gate 		*str = '\0';
85*0Sstevel@tonic-gate 		oid = SSAOidStrToOid(name_ptr,error_label);
86*0Sstevel@tonic-gate 	}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	return(oid);
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
get_oid(char * enterprise_str)92*0Sstevel@tonic-gate Oid *get_oid(char *enterprise_str)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	char *snm_home;
95*0Sstevel@tonic-gate 	Oid *oid = NULL;
96*0Sstevel@tonic-gate 	FILE *fd;
97*0Sstevel@tonic-gate 	char inbuf[BUFSIZE];
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	if ((snm_home = getenv("SNMHOME")) == NULL ) {
100*0Sstevel@tonic-gate 		sprintf(inbuf,
101*0Sstevel@tonic-gate 				"/etc/snmp/conf/enterprises.oid");
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 	else {
104*0Sstevel@tonic-gate 		sprintf(inbuf,
105*0Sstevel@tonic-gate 				"%s/agents/enterprise.oid", snm_home);
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate     fd = fopen(inbuf, "r");
109*0Sstevel@tonic-gate     if (fd == NULL) {
110*0Sstevel@tonic-gate 		fprintf(stderr, "Cannot open %s\n", inbuf);
111*0Sstevel@tonic-gate 		return (NULL);
112*0Sstevel@tonic-gate     }
113*0Sstevel@tonic-gate 	else {
114*0Sstevel@tonic-gate 		if(trace_level > 0)	{
115*0Sstevel@tonic-gate 			trace("Parsing %s\n", inbuf);
116*0Sstevel@tonic-gate 		}
117*0Sstevel@tonic-gate     }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	while (fgets(inbuf, BUFSIZE, fd)) {
120*0Sstevel@tonic-gate 		oid = get_oid_with_name(inbuf, enterprise_str);
121*0Sstevel@tonic-gate 		if (oid != NULL) {
122*0Sstevel@tonic-gate 			fclose (fd);
123*0Sstevel@tonic-gate 			return(oid);
124*0Sstevel@tonic-gate 		}
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate     fclose(fd);
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	return (oid);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate /* error return NULL,  success variable */
get_variable(char * buf)133*0Sstevel@tonic-gate SNMP_variable *get_variable(char *buf)
134*0Sstevel@tonic-gate {
135*0Sstevel@tonic-gate 	char name[BUFSIZE];
136*0Sstevel@tonic-gate 	char type_str[BUFSIZE];
137*0Sstevel@tonic-gate 	u_char type;
138*0Sstevel@tonic-gate 	char value[BUFSIZE];
139*0Sstevel@tonic-gate 	SNMP_value snmp_value;
140*0Sstevel@tonic-gate 	SNMP_variable *variable = NULL;
141*0Sstevel@tonic-gate 	Oid *name_oid, *value_oid;
142*0Sstevel@tonic-gate 	int count;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	char *s;
145*0Sstevel@tonic-gate     int i;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate     /* get the attribute name and the type */
148*0Sstevel@tonic-gate     if (sscanf(buf, "%s %s", name, type_str) != 2)
149*0Sstevel@tonic-gate       return(NULL);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate     /* get the value */
152*0Sstevel@tonic-gate     /* everything after the '(' is the value field */
153*0Sstevel@tonic-gate     if (!(s = (char *)strchr(buf, '(')))
154*0Sstevel@tonic-gate 		return(NULL);
155*0Sstevel@tonic-gate     s++;
156*0Sstevel@tonic-gate 	count = 1;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate     for (; *s && *s == ' '; s++); /* skip leading blanks */
159*0Sstevel@tonic-gate     for (i = 0; *s && count && i< BUFSIZE ; s++, i++) {
160*0Sstevel@tonic-gate 		if (*s == ')')
161*0Sstevel@tonic-gate 			count --;
162*0Sstevel@tonic-gate 		if (*s == '(')
163*0Sstevel@tonic-gate 			count ++;
164*0Sstevel@tonic-gate 		if (count)
165*0Sstevel@tonic-gate 			value[i] = *s;
166*0Sstevel@tonic-gate 		else
167*0Sstevel@tonic-gate 			value[i] = '\0';
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	if (i>= BUFSIZE) {
171*0Sstevel@tonic-gate 		fprintf(stderr, "object value is too long!\n");
172*0Sstevel@tonic-gate 		usage();
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate     value[i] = '\0';
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate     if (strcmp(type_str, "STRING") == 0) {
177*0Sstevel@tonic-gate 		type = STRING;
178*0Sstevel@tonic-gate 		snmp_value.v_string.chars = (u_char *) value;
179*0Sstevel@tonic-gate 		snmp_value.v_string.len = strlen(value);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate     else if (strcmp(type_str, "IPADDRESS") == 0) {
182*0Sstevel@tonic-gate 		type = IPADDRESS;
183*0Sstevel@tonic-gate 		snmp_value.v_string.chars = (u_char *) value;
184*0Sstevel@tonic-gate 		snmp_value.v_string.len = strlen(value);
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate     else if (strcmp(type_str, "OPAQUE") == 0) {
187*0Sstevel@tonic-gate 		type = OPAQUE;
188*0Sstevel@tonic-gate 		snmp_value.v_string.chars = (u_char *) value;
189*0Sstevel@tonic-gate 		snmp_value.v_string.len = strlen(value);
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate     else if (strcmp(type_str, "INTEGER") == 0) {
192*0Sstevel@tonic-gate 		type = INTEGER;
193*0Sstevel@tonic-gate 		snmp_value.v_integer = atoi(value);
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate     else if (strcmp(type_str, "COUNTER") == 0) {
196*0Sstevel@tonic-gate 		type = COUNTER;
197*0Sstevel@tonic-gate 		snmp_value.v_integer = atoi(value);
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate     else if (strcmp(type_str, "GAUGE") == 0) {
200*0Sstevel@tonic-gate 		type = GAUGE;
201*0Sstevel@tonic-gate 		snmp_value.v_integer = atoi(value);
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate     else if (strcmp(type_str, "TIMETICKS") == 0) {
204*0Sstevel@tonic-gate 		type = TIMETICKS;
205*0Sstevel@tonic-gate 		snmp_value.v_integer = atoi(value);
206*0Sstevel@tonic-gate 	}
207*0Sstevel@tonic-gate     else if (strcmp(type_str, "OBJECTID") == 0) {
208*0Sstevel@tonic-gate 		type = OBJID;
209*0Sstevel@tonic-gate 		if ((value_oid = SSAOidStrToOid(value,error_label)) == NULL) return (NULL) ;
210*0Sstevel@tonic-gate 		snmp_value.v_oid.subids = value_oid->subids;
211*0Sstevel@tonic-gate 		snmp_value.v_oid.len = value_oid->len;
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 	else return (NULL);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	if ((name_oid = SSAOidStrToOid(name,error_label)) == NULL) return (NULL);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	variable = snmp_typed_variable_new(name_oid,
219*0Sstevel@tonic-gate 									   type, &snmp_value, error_label);
220*0Sstevel@tonic-gate 	SSAOidFree(name_oid);
221*0Sstevel@tonic-gate     return(variable);
222*0Sstevel@tonic-gate }  /* get_variable */
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 
225