xref: /onnv-gate/usr/src/cmd/agents/snmp/trapsend/trapsend.c (revision 1706:0e053bac3f94)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1706Swesolows  * Common Development and Distribution License (the "License").
6*1706Swesolows  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1706Swesolows 
220Sstevel@tonic-gate /*
23*1706Swesolows  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <syslog.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <arpa/inet.h>
410Sstevel@tonic-gate #include <netdb.h>
420Sstevel@tonic-gate #include <nlist.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include "snmp_msg.h"
450Sstevel@tonic-gate #include "impl.h"
460Sstevel@tonic-gate #include "trace.h"
470Sstevel@tonic-gate #include "snmp.h"
480Sstevel@tonic-gate #include "pdu.h"
490Sstevel@tonic-gate #include "trap.h"
500Sstevel@tonic-gate #include "error.h"
510Sstevel@tonic-gate #include "oid.h"
520Sstevel@tonic-gate #include "usage.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include "sea_i18n.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static int is_number (char *buf);
590Sstevel@tonic-gate 
60*1706Swesolows int
main(int argc,char * argv[])61*1706Swesolows main(int argc, char *argv[])
620Sstevel@tonic-gate {
630Sstevel@tonic-gate     extern char *optarg;
640Sstevel@tonic-gate 	extern int optind;
650Sstevel@tonic-gate 	int opt;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	char hostname[MAXHOSTNAMELEN];
680Sstevel@tonic-gate 	IPAddress ip_address;
690Sstevel@tonic-gate 	IPAddress my_ip_addr;
700Sstevel@tonic-gate 	Oid *enterprise;
710Sstevel@tonic-gate 	int generic, specific, level;
720Sstevel@tonic-gate 	SNMP_variable *variables;
730Sstevel@tonic-gate 	struct hostent *hp;
740Sstevel@tonic-gate 	int trap_port = -1;
750Sstevel@tonic-gate 	u_long time_stamp = (u_long)-1;
760Sstevel@tonic-gate 	int enterprise_flag= 0, a_flag = 0, i_flag = 0;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	optind = 1;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	/* the default host name is local host */
810Sstevel@tonic-gate 	gethostname(hostname, sizeof(hostname));
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	/* default Oid for enterprise is sun */
840Sstevel@tonic-gate 	enterprise = &sun_oid;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/* generic, specific */
870Sstevel@tonic-gate 	generic = 6;
880Sstevel@tonic-gate 	specific = 1;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate         {
920Sstevel@tonic-gate         char domain_path[MAXPATHLEN];
930Sstevel@tonic-gate 
940Sstevel@tonic-gate         setlocale(LC_ALL, "");
950Sstevel@tonic-gate 
960Sstevel@tonic-gate         sprintf(domain_path, SEA_LOCALE_PATH);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate         bindtextdomain(DOMAIN_MGET, domain_path);
990Sstevel@tonic-gate         bindtextdomain(DOMAIN_SGET,   domain_path);
1000Sstevel@tonic-gate         bindtextdomain(DOMAIN_LIBGET,   domain_path);
1010Sstevel@tonic-gate         bindtextdomain(DOMAIN_LGET, domain_path);
1020Sstevel@tonic-gate         bindtextdomain(DOMAIN_FGET,  domain_path);  /* formatting string */
1030Sstevel@tonic-gate         }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/* get command-line options */
1070Sstevel@tonic-gate     while ((opt = getopt(argc, argv, "h:c:e:E:g:s:i:t:a:T:p:")) != EOF) {
1080Sstevel@tonic-gate 		switch (opt) {
1090Sstevel@tonic-gate 			case 'T':
1100Sstevel@tonic-gate 				level = atoi(optarg);
1110Sstevel@tonic-gate 				if(trace_set(level, error_label)){
1120Sstevel@tonic-gate 					fprintf(stderr, " %d is not a valid trace level!\n",
1130Sstevel@tonic-gate 							level);
1140Sstevel@tonic-gate 					usage();
1150Sstevel@tonic-gate 				}
1160Sstevel@tonic-gate 				break;
1170Sstevel@tonic-gate 			case 'h':		/* host to send trap to */
1180Sstevel@tonic-gate 				if (strlcpy(hostname, optarg, sizeof (hostname))
1190Sstevel@tonic-gate 					> MAXHOSTNAMELEN) {
1200Sstevel@tonic-gate 					fprintf(stderr, "%s: hostname too long!\
1210Sstevel@tonic-gate 						\n", optarg);
1220Sstevel@tonic-gate 					exit(1);
1230Sstevel@tonic-gate 				}
1240Sstevel@tonic-gate 			case 'c':
1250Sstevel@tonic-gate 				trap_community = optarg;
1260Sstevel@tonic-gate 				break;
1270Sstevel@tonic-gate 			case 'e':
1280Sstevel@tonic-gate 				if (enterprise_flag) {
1290Sstevel@tonic-gate 					usage();
1300Sstevel@tonic-gate 				}
1310Sstevel@tonic-gate 				enterprise = SSAOidStrToOid(optarg,error_label);
1320Sstevel@tonic-gate 				if (!enterprise){ /* error */
1330Sstevel@tonic-gate 					fprintf(stderr,
1340Sstevel@tonic-gate 							"%s: not a valid enterprise oid string!\n",
1350Sstevel@tonic-gate 							optarg);
1360Sstevel@tonic-gate 					usage();
1370Sstevel@tonic-gate 				}
1380Sstevel@tonic-gate 				enterprise_flag = 1;
1390Sstevel@tonic-gate 				break;
1400Sstevel@tonic-gate 			case 'E':
1410Sstevel@tonic-gate 				if (enterprise_flag) {
1420Sstevel@tonic-gate 					usage();
1430Sstevel@tonic-gate 				}
1440Sstevel@tonic-gate 				enterprise = get_oid(optarg);
1450Sstevel@tonic-gate 				if (!enterprise) {
1460Sstevel@tonic-gate 					usage();
1470Sstevel@tonic-gate 				}
1480Sstevel@tonic-gate 				enterprise_flag = 1;
1490Sstevel@tonic-gate 				break;
1500Sstevel@tonic-gate 			case 'g':         /* generic trap type */
1510Sstevel@tonic-gate 				if (is_number(optarg))
1520Sstevel@tonic-gate 					usage();
1530Sstevel@tonic-gate 				generic = atoi(optarg);
1540Sstevel@tonic-gate 				if ((generic > 6 ) || (generic < 0))
1550Sstevel@tonic-gate 					usage();
1560Sstevel@tonic-gate 				break;
1570Sstevel@tonic-gate 			case 's':         /* specific trap type */
1580Sstevel@tonic-gate 				if (is_number(optarg))
1590Sstevel@tonic-gate 					usage();
1600Sstevel@tonic-gate 				specific = atoi(optarg);
1610Sstevel@tonic-gate 				break;
1620Sstevel@tonic-gate 			case 'i':
1630Sstevel@tonic-gate 				if (name_to_ip_address(optarg,
1640Sstevel@tonic-gate 									   &my_ip_addr,
1650Sstevel@tonic-gate 									   error_label)) {
1660Sstevel@tonic-gate 					usage();
1670Sstevel@tonic-gate 				}
1680Sstevel@tonic-gate 				i_flag = 1;
1690Sstevel@tonic-gate 				break;
1700Sstevel@tonic-gate 			case 't': /* timestamp */
1710Sstevel@tonic-gate 				time_stamp = atol(optarg);
1720Sstevel@tonic-gate 				break;
1730Sstevel@tonic-gate 			case 'p':
1740Sstevel@tonic-gate 				if (is_number(optarg))
1750Sstevel@tonic-gate 					usage();
1760Sstevel@tonic-gate 				trap_port = atoi(optarg);
1770Sstevel@tonic-gate 				break;
1780Sstevel@tonic-gate 			case 'a':         /* attribute information */
1790Sstevel@tonic-gate 				if ((variables = get_variable(optarg))== NULL){
1800Sstevel@tonic-gate 					fprintf(stderr,
1810Sstevel@tonic-gate 							"%s: not a valid variable!\n", optarg);
1820Sstevel@tonic-gate 					usage();
1830Sstevel@tonic-gate 				}
1840Sstevel@tonic-gate 				a_flag = 1;
1850Sstevel@tonic-gate 				break;
1860Sstevel@tonic-gate 			case '?':		/* usage help */
1870Sstevel@tonic-gate 				usage();
1880Sstevel@tonic-gate 				break;
1890Sstevel@tonic-gate 			default:
1900Sstevel@tonic-gate 				usage();
1910Sstevel@tonic-gate 				break;
1920Sstevel@tonic-gate 		}  /* switch */
1930Sstevel@tonic-gate 	}/* while */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if ((optind != argc) || (!a_flag))
1960Sstevel@tonic-gate 		usage();
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	if ((ip_address.s_addr = inet_addr(hostname)) == -1 ) {
1990Sstevel@tonic-gate 		if ((hp = gethostbyname(hostname)) == NULL) {
2000Sstevel@tonic-gate 			fprintf(stderr, "\n%s is not a valid hostname!\n\n", hostname);
2010Sstevel@tonic-gate 			usage();
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 		memcpy(&(ip_address.s_addr), hp->h_addr, hp->h_length);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/* some trace message */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (trap_send_with_more_para(&ip_address,
2110Sstevel@tonic-gate 								 my_ip_addr, trap_community, i_flag,
2120Sstevel@tonic-gate 								 enterprise,
2130Sstevel@tonic-gate 								 generic,
2140Sstevel@tonic-gate 								 specific,
2150Sstevel@tonic-gate 								 trap_port,
2160Sstevel@tonic-gate 								 time_stamp,
2170Sstevel@tonic-gate 								 variables,
2180Sstevel@tonic-gate 								 error_label)) {
2190Sstevel@tonic-gate 		fprintf(stderr, "trap_send not success!\n\n");
2200Sstevel@tonic-gate 		return (-1);
2210Sstevel@tonic-gate 	} else {
2220Sstevel@tonic-gate 		return (0);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*	if (trap_send(&ip_address,
2260Sstevel@tonic-gate 				  enterprise,
2270Sstevel@tonic-gate 				  generic,
2280Sstevel@tonic-gate 				  specific,
2290Sstevel@tonic-gate 				  variables,
2300Sstevel@tonic-gate 				  error_label)) {
2310Sstevel@tonic-gate 		fprintf(stderr, "trap_send not success!\n\n");
2320Sstevel@tonic-gate 	}	 */
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 
is_number(buf)2370Sstevel@tonic-gate static int is_number (buf)
2380Sstevel@tonic-gate char *buf;
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	int len, i;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (buf == NULL)
2430Sstevel@tonic-gate 		return (-1);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	len = strlen(buf);
2460Sstevel@tonic-gate 	for (i= 0; i < len; i++)
2470Sstevel@tonic-gate 		if (!isdigit(buf[i])) {
2480Sstevel@tonic-gate 		fprintf(stderr, "\n%s is not a valid generic or specific trap type number!\n\n", buf);
2490Sstevel@tonic-gate 		return(-1);
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 	return (0);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
254