summaryrefslogtreecommitdiffstats
path: root/web/attachments/41928-check_ssh.diff
blob: fce5ad2336a67a4c4bd38a2d31cf83415afab4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Index: plugins/check_ssh.c
===================================================================
RCS file: /cvsroot/nagiosplug/nagiosplug/plugins/check_ssh.c,v
retrieving revision 1.4
diff -u -r1.4 check_ssh.c
--- plugins/check_ssh.c	13 Jan 2003 12:15:16 -0000	1.4
+++ plugins/check_ssh.c	9 Feb 2003 19:32:30 -0000
@@ -34,7 +34,6 @@
 void print_help (void);
 void print_usage (void);
 
-char *ssh_resolve (char *hostname);
 int ssh_connect (char *haddr, short hport);
 
 int
@@ -49,7 +48,7 @@
 	alarm (socket_timeout);
 
 	/* ssh_connect exits if error is found */
-	ssh_connect (ssh_resolve (server_name), port);
+	ssh_connect (server_name, port);
 
 	alarm (0);
 
@@ -62,6 +61,7 @@
 process_arguments (int argc, char **argv)
 {
 	int c;
+	char *tmp = NULL;
 
 #ifdef HAVE_GETOPT_H
 	int option_index = 0;
@@ -109,6 +109,8 @@
 			socket_timeout = atoi (optarg);
 			break;
 		case 'H':									/* host */
+			if (is_host (optarg) == FALSE)
+				usage ("Invalid hostname/address\n");
 			server_name = optarg;
 			break;
 		case 'p':									/* port */
@@ -125,9 +127,12 @@
 
 	c = optind;
 	if (server_name == NULL && argv[c]) {
-		server_name = argv[c++];
+		if (is_host (argv[c])) {
+			server_name = argv[c++];
+		}
 	}
-	else if (port == -1 && argv[c]) {
+
+	if (port == -1 && argv[c]) {
 		if (is_intpos (argv[c])) {
 			port = atoi (argv[c++]);
 		}
@@ -153,26 +158,6 @@
 
 /************************************************************************
 *
-* Resolve hostname into IP address
-*
-*-----------------------------------------------------------------------*/
-
-char *
-ssh_resolve (char *hostname)
-{
-	struct hostent *host;
-
-	host = gethostbyname (hostname);
-	if (!host) {
-		herror (hostname);
-		exit (STATE_CRITICAL);
-	}
-	return (host->h_addr);
-}
-
-
-/************************************************************************
-*
 * Try to connect to SSH server at specified server and port
 *
 *-----------------------------------------------------------------------*/
@@ -180,39 +165,24 @@
 int
 ssh_connect (char *haddr, short hport)
 {
-	int s;
-	struct sockaddr_in addr;
-	int addrlen;
-	int len;
+	int sock;
+	int result;
 	char *output = NULL;
 	char *buffer = NULL;
 	char *ssh_proto = NULL;
 	char *ssh_server = NULL;
 	char revision[20];
 
-	sscanf ("$Revision: 1.4 $", "$Revision: %[0123456789.]", revision);
+	sscanf ("$Revision: 1.4 $", "$Revision: %[0-9.]", revision);
 
-	addrlen = sizeof (addr);
-	memset (&addr, 0, addrlen);
-	addr.sin_port = htons (hport);
-	addr.sin_family = AF_INET;
-	bcopy (haddr, (void *) &addr.sin_addr.s_addr, 4);
-
-	s = socket (AF_INET, SOCK_STREAM, 0);
-	if (!s) {
-		printf ("socket(): %s for %s:%d\n", strerror (errno), server_name, hport);
-		exit (STATE_CRITICAL);
-	}
+	result = my_tcp_connect (haddr, hport, &sock);
 
-	if (connect (s, (struct sockaddr *) &addr, addrlen)) {
-		printf ("connect(): %s for %s:%d\n", strerror (errno), server_name,
-						hport);
-		exit (STATE_CRITICAL);
-	}
+	if (result != STATE_OK)
+		return result;
 
 	output = (char *) malloc (BUFF_SZ + 1);
 	memset (output, 0, BUFF_SZ + 1);
-	recv (s, output, BUFF_SZ, 0);
+	recv (sock, output, BUFF_SZ, 0);
 	if (strncmp (output, "SSH", 3)) {
 		printf ("Server answer: %s", output);
 		exit (STATE_CRITICAL);
@@ -228,7 +198,7 @@
 			("SSH ok - %s (protocol %s)\n",
 			 ssh_server, ssh_proto);
 		asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision);
-		send (s, buffer, strlen (buffer), MSG_DONTWAIT);
+		send (sock, buffer, strlen (buffer), MSG_DONTWAIT);
 		if (verbose)
 			printf ("%s\n", buffer);
 		exit (STATE_OK);