xref: /onnv-gate/usr/src/cmd/agents/snmp/trapsend/trapsend.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 /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <netinet/in.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <sys/socket.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <syslog.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <arpa/inet.h>
41*0Sstevel@tonic-gate #include <netdb.h>
42*0Sstevel@tonic-gate #include <nlist.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include "snmp_msg.h"
45*0Sstevel@tonic-gate #include "impl.h"
46*0Sstevel@tonic-gate #include "trace.h"
47*0Sstevel@tonic-gate #include "snmp.h"
48*0Sstevel@tonic-gate #include "pdu.h"
49*0Sstevel@tonic-gate #include "trap.h"
50*0Sstevel@tonic-gate #include "error.h"
51*0Sstevel@tonic-gate #include "oid.h"
52*0Sstevel@tonic-gate #include "usage.h"
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #include "sea_i18n.h"
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static int is_number (char *buf);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate main(argc, argv)
61*0Sstevel@tonic-gate int argc;
62*0Sstevel@tonic-gate char *argv[];
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate     extern char *optarg;
65*0Sstevel@tonic-gate 	extern int optind;
66*0Sstevel@tonic-gate 	int opt;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	char hostname[MAXHOSTNAMELEN];
69*0Sstevel@tonic-gate 	IPAddress ip_address;
70*0Sstevel@tonic-gate 	IPAddress my_ip_addr;
71*0Sstevel@tonic-gate 	Oid *enterprise;
72*0Sstevel@tonic-gate 	int generic, specific, level;
73*0Sstevel@tonic-gate 	SNMP_variable *variables;
74*0Sstevel@tonic-gate 	struct hostent *hp;
75*0Sstevel@tonic-gate 	int trap_port = -1;
76*0Sstevel@tonic-gate 	u_long time_stamp = (u_long)-1;
77*0Sstevel@tonic-gate 	int enterprise_flag= 0, a_flag = 0, i_flag = 0;
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	optind = 1;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	/* the default host name is local host */
82*0Sstevel@tonic-gate 	gethostname(hostname, sizeof(hostname));
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	/* default Oid for enterprise is sun */
85*0Sstevel@tonic-gate 	enterprise = &sun_oid;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	/* generic, specific */
88*0Sstevel@tonic-gate 	generic = 6;
89*0Sstevel@tonic-gate 	specific = 1;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate         {
93*0Sstevel@tonic-gate         char domain_path[MAXPATHLEN];
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate         setlocale(LC_ALL, "");
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate         sprintf(domain_path, SEA_LOCALE_PATH);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate         bindtextdomain(DOMAIN_MGET, domain_path);
100*0Sstevel@tonic-gate         bindtextdomain(DOMAIN_SGET,   domain_path);
101*0Sstevel@tonic-gate         bindtextdomain(DOMAIN_LIBGET,   domain_path);
102*0Sstevel@tonic-gate         bindtextdomain(DOMAIN_LGET, domain_path);
103*0Sstevel@tonic-gate         bindtextdomain(DOMAIN_FGET,  domain_path);  /* formatting string */
104*0Sstevel@tonic-gate         }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	/* get command-line options */
108*0Sstevel@tonic-gate     while ((opt = getopt(argc, argv, "h:c:e:E:g:s:i:t:a:T:p:")) != EOF) {
109*0Sstevel@tonic-gate 		switch (opt) {
110*0Sstevel@tonic-gate 			case 'T':
111*0Sstevel@tonic-gate 				level = atoi(optarg);
112*0Sstevel@tonic-gate 				if(trace_set(level, error_label)){
113*0Sstevel@tonic-gate 					fprintf(stderr, " %d is not a valid trace level!\n",
114*0Sstevel@tonic-gate 							level);
115*0Sstevel@tonic-gate 					usage();
116*0Sstevel@tonic-gate 				}
117*0Sstevel@tonic-gate 				break;
118*0Sstevel@tonic-gate 			case 'h':		/* host to send trap to */
119*0Sstevel@tonic-gate 				if (strlcpy(hostname, optarg, sizeof (hostname))
120*0Sstevel@tonic-gate 					> MAXHOSTNAMELEN) {
121*0Sstevel@tonic-gate 					fprintf(stderr, "%s: hostname too long!\
122*0Sstevel@tonic-gate 						\n", optarg);
123*0Sstevel@tonic-gate 					exit(1);
124*0Sstevel@tonic-gate 				}
125*0Sstevel@tonic-gate 			case 'c':
126*0Sstevel@tonic-gate 				trap_community = optarg;
127*0Sstevel@tonic-gate 				break;
128*0Sstevel@tonic-gate 			case 'e':
129*0Sstevel@tonic-gate 				if (enterprise_flag) {
130*0Sstevel@tonic-gate 					usage();
131*0Sstevel@tonic-gate 				}
132*0Sstevel@tonic-gate 				enterprise = SSAOidStrToOid(optarg,error_label);
133*0Sstevel@tonic-gate 				if (!enterprise){ /* error */
134*0Sstevel@tonic-gate 					fprintf(stderr,
135*0Sstevel@tonic-gate 							"%s: not a valid enterprise oid string!\n",
136*0Sstevel@tonic-gate 							optarg);
137*0Sstevel@tonic-gate 					usage();
138*0Sstevel@tonic-gate 				}
139*0Sstevel@tonic-gate 				enterprise_flag = 1;
140*0Sstevel@tonic-gate 				break;
141*0Sstevel@tonic-gate 			case 'E':
142*0Sstevel@tonic-gate 				if (enterprise_flag) {
143*0Sstevel@tonic-gate 					usage();
144*0Sstevel@tonic-gate 				}
145*0Sstevel@tonic-gate 				enterprise = get_oid(optarg);
146*0Sstevel@tonic-gate 				if (!enterprise) {
147*0Sstevel@tonic-gate 					usage();
148*0Sstevel@tonic-gate 				}
149*0Sstevel@tonic-gate 				enterprise_flag = 1;
150*0Sstevel@tonic-gate 				break;
151*0Sstevel@tonic-gate 			case 'g':         /* generic trap type */
152*0Sstevel@tonic-gate 				if (is_number(optarg))
153*0Sstevel@tonic-gate 					usage();
154*0Sstevel@tonic-gate 				generic = atoi(optarg);
155*0Sstevel@tonic-gate 				if ((generic > 6 ) || (generic < 0))
156*0Sstevel@tonic-gate 					usage();
157*0Sstevel@tonic-gate 				break;
158*0Sstevel@tonic-gate 			case 's':         /* specific trap type */
159*0Sstevel@tonic-gate 				if (is_number(optarg))
160*0Sstevel@tonic-gate 					usage();
161*0Sstevel@tonic-gate 				specific = atoi(optarg);
162*0Sstevel@tonic-gate 				break;
163*0Sstevel@tonic-gate 			case 'i':
164*0Sstevel@tonic-gate 				if (name_to_ip_address(optarg,
165*0Sstevel@tonic-gate 									   &my_ip_addr,
166*0Sstevel@tonic-gate 									   error_label)) {
167*0Sstevel@tonic-gate 					usage();
168*0Sstevel@tonic-gate 				}
169*0Sstevel@tonic-gate 				i_flag = 1;
170*0Sstevel@tonic-gate 				break;
171*0Sstevel@tonic-gate 			case 't': /* timestamp */
172*0Sstevel@tonic-gate 				time_stamp = atol(optarg);
173*0Sstevel@tonic-gate 				break;
174*0Sstevel@tonic-gate 			case 'p':
175*0Sstevel@tonic-gate 				if (is_number(optarg))
176*0Sstevel@tonic-gate 					usage();
177*0Sstevel@tonic-gate 				trap_port = atoi(optarg);
178*0Sstevel@tonic-gate 				break;
179*0Sstevel@tonic-gate 			case 'a':         /* attribute information */
180*0Sstevel@tonic-gate 				if ((variables = get_variable(optarg))== NULL){
181*0Sstevel@tonic-gate 					fprintf(stderr,
182*0Sstevel@tonic-gate 							"%s: not a valid variable!\n", optarg);
183*0Sstevel@tonic-gate 					usage();
184*0Sstevel@tonic-gate 				}
185*0Sstevel@tonic-gate 				a_flag = 1;
186*0Sstevel@tonic-gate 				break;
187*0Sstevel@tonic-gate 			case '?':		/* usage help */
188*0Sstevel@tonic-gate 				usage();
189*0Sstevel@tonic-gate 				break;
190*0Sstevel@tonic-gate 			default:
191*0Sstevel@tonic-gate 				usage();
192*0Sstevel@tonic-gate 				break;
193*0Sstevel@tonic-gate 		}  /* switch */
194*0Sstevel@tonic-gate 	}/* while */
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	if ((optind != argc) || (!a_flag))
197*0Sstevel@tonic-gate 		usage();
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if ((ip_address.s_addr = inet_addr(hostname)) == -1 ) {
200*0Sstevel@tonic-gate 		if ((hp = gethostbyname(hostname)) == NULL) {
201*0Sstevel@tonic-gate 			fprintf(stderr, "\n%s is not a valid hostname!\n\n", hostname);
202*0Sstevel@tonic-gate 			usage();
203*0Sstevel@tonic-gate 		}
204*0Sstevel@tonic-gate 		memcpy(&(ip_address.s_addr), hp->h_addr, hp->h_length);
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	/* some trace message */
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	if (trap_send_with_more_para(&ip_address,
212*0Sstevel@tonic-gate 								 my_ip_addr, trap_community, i_flag,
213*0Sstevel@tonic-gate 								 enterprise,
214*0Sstevel@tonic-gate 								 generic,
215*0Sstevel@tonic-gate 								 specific,
216*0Sstevel@tonic-gate 								 trap_port,
217*0Sstevel@tonic-gate 								 time_stamp,
218*0Sstevel@tonic-gate 								 variables,
219*0Sstevel@tonic-gate 								 error_label)) {
220*0Sstevel@tonic-gate 		fprintf(stderr, "trap_send not success!\n\n");
221*0Sstevel@tonic-gate 		return (-1);
222*0Sstevel@tonic-gate 	} else {
223*0Sstevel@tonic-gate 		return (0);
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate /*	if (trap_send(&ip_address,
227*0Sstevel@tonic-gate 				  enterprise,
228*0Sstevel@tonic-gate 				  generic,
229*0Sstevel@tonic-gate 				  specific,
230*0Sstevel@tonic-gate 				  variables,
231*0Sstevel@tonic-gate 				  error_label)) {
232*0Sstevel@tonic-gate 		fprintf(stderr, "trap_send not success!\n\n");
233*0Sstevel@tonic-gate 	}	 */
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate static int is_number (buf)
239*0Sstevel@tonic-gate char *buf;
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	int len, i;
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	if (buf == NULL)
244*0Sstevel@tonic-gate 		return (-1);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	len = strlen(buf);
247*0Sstevel@tonic-gate 	for (i= 0; i < len; i++)
248*0Sstevel@tonic-gate 		if (!isdigit(buf[i])) {
249*0Sstevel@tonic-gate 		fprintf(stderr, "\n%s is not a valid generic or specific trap type number!\n\n", buf);
250*0Sstevel@tonic-gate 		return(-1);
251*0Sstevel@tonic-gate 		}
252*0Sstevel@tonic-gate 	return (0);
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255