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