/[svn]/vendor/pxys2-2.1.0/pxtarget/pxcheck.c
ViewVC logotype

Contents of /vendor/pxys2-2.1.0/pxtarget/pxcheck.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3253 - (show annotations)
Wed Apr 2 20:46:18 2014 UTC (6 years, 11 months ago) by michael
File MIME type: text/x-chdr
File size: 5142 byte(s)
- Imported pxys2-2.1.0

1 /* Copyright (C) 2006 Pascal Gloor
2 *
3 * This file is part of pxtarget (from pxys)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <inttypes.h>
26 #include <string.h>
27 #include <netdb.h>
28 #include <fcntl.h>
29 #include <sys/time.h>
30 #include <sys/wait.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <errno.h>
36 #include <time.h>
37 #include <errno.h>
38
39 #include "sha256.h"
40
41 #define RCSID "$Id: pxcheck.c,v 1.3 2006/09/10 22:24:06 spale Exp $"
42
43 #define HTTP_RESPONSE "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"
44 #define COMMON_RESPONSE "*** PXSCAN STRING: "
45
46 /* functions */
47 void chop(char *str);
48 uint16_t resolve_port(char *port);
49 uint32_t resolve_host(char *host);
50 char *build_sha256(char *secret);
51 void usage(char *prg);
52
53 /* print usage */
54 void usage(char *prg) {
55 fprintf(stderr,"PX Target\n");
56 fprintf(stderr,"usage: %s [-h] <-s string> <host:port> [host:port] ...\n",prg);
57 fprintf(stderr,"-h : This page.\n");
58 fprintf(stderr,"-s : Shared secret\n");
59 exit(EXIT_FAILURE);
60 }
61
62 /* main */
63 int main(int argc, char *argv[]) {
64 char *prg = *argv;
65 char *secret = NULL;
66
67 argc--;
68 argv++;
69
70 if ( !argc ) usage(prg);
71
72 /* command line arguments parsing */
73 while(argc) {
74 if ( strcmp(*argv,"-s") == 0 ) {
75 argc--;
76 argv++;
77 if ( !argc ) usage(prg);
78
79 secret = *argv;
80 }
81 else if ( strcmp(*argv,"-h") == 0 )
82 usage(prg);
83 else
84 break;
85
86 argc--;
87 argv++;
88 }
89
90 if ( !argc ) usage(prg);
91
92 if ( secret == NULL ) {
93 fprintf(stderr,"missing shared secret (-s)\n");
94 usage(prg);
95 }
96
97 while(argc) {
98 int s = -1;
99 ssize_t len;
100 struct sockaddr_in sockin;
101 char buf[1024];
102 char *strhost;
103 char *strport;
104 char *string;
105
106 if ( strlen(*argv) >= sizeof(buf) ) {
107 fprintf(stderr,"Invalid host:port '%s'\n",*argv);
108 goto next;
109 }
110
111 strcpy(buf,*argv);
112
113 if ( ( strhost = strtok(buf,":") ) == NULL ) {
114 fprintf(stderr,"Invalid host:port '%s'\n",*argv);
115 goto next;
116 }
117
118 if ( ( strport = strtok(NULL,"" ) ) == NULL ) {
119 fprintf(stderr,"Invalid host:port '%s'\n",*argv);
120 goto next;
121 }
122
123 sockin.sin_family = AF_INET;
124 sockin.sin_addr.s_addr = resolve_host(strhost);
125 sockin.sin_port = resolve_port(strport);
126
127 printf("host=%s port=%u ip=%s ",strhost,
128 ntohs(sockin.sin_port),inet_ntoa(sockin.sin_addr));
129
130 if ( ( s = socket(PF_INET, SOCK_STREAM, 0) ) == -1 ) {
131 printf("error=\"socket(%s)\"",strerror(errno));
132 goto next;
133 }
134
135 if ( ( connect(s, (struct sockaddr*)&sockin, sizeof(struct sockaddr_in)) ) == -1 ) {
136 printf("error=\"connect(%s)\"",strerror(errno));
137 goto next;
138 }
139
140 if ( ( send(s, "\r\n", 2, 0) ) != 2 ) {
141 printf("error=\"send(%s)\"",strerror(errno));
142 goto next;
143 }
144
145 if ( ( len = recv(s, buf, sizeof(buf)-1, 0) ) < 1 ) {
146 printf("error=\"recv(%s)\"",strerror(errno));
147 goto next;
148 }
149
150 buf[len] = '\0';
151 chop(buf);
152
153 if ( strlen(buf) < sizeof(COMMON_RESPONSE) - 1 + 64 ) {
154 printf("error=\"invalid_string(%s)\"",buf);
155 goto next;
156 }
157
158 if ( memcmp(buf, COMMON_RESPONSE, sizeof(COMMON_RESPONSE)-1) != 0 ) {
159 printf("error=\"invalid_string(%s)\"",buf);
160 goto next;
161 }
162
163 string = build_sha256(secret);
164
165 if ( memcmp(buf + sizeof(COMMON_RESPONSE) - 1, string, 64 ) != 0 ) {
166 printf("error=\"sha256_mismatch(%s)\"",buf);
167 goto next;
168 }
169
170 printf("error=none");
171
172 next:
173
174 printf("\n");
175
176 if ( s > 0 ) close(s);
177
178 argc--;
179 argv++;
180
181 }
182
183 return 0;
184 }
185
186 uint16_t resolve_port(char *port) {
187 return htons(strtoul(port, NULL, 0));
188 }
189
190 uint32_t resolve_host(char *host) {
191 struct hostent *he;
192
193 if ( ( he = gethostbyname(host) ) == NULL ) {
194 fprintf(stderr,"gethostbyname('%s'): %s\n",
195 host, hstrerror(h_errno));
196 return INADDR_NONE;
197 }
198
199 return *(uint32_t*)he->h_addr;
200 }
201
202 char *build_sha256(char *secret) {
203 static char buf[1024];
204 unsigned char hash[32];
205 sha256_context ctx;
206 time_t ts = time(NULL);
207 int i;
208
209 sprintf(buf,"%li",(long int)ts - ( ts % 3600 ));
210
211 sha256_starts(&ctx);
212 sha256_update(&ctx, (uint8_t*)secret, strlen(secret));
213 sha256_update(&ctx, (uint8_t*)buf, strlen(buf));
214 sha256_finish(&ctx, hash);
215
216 for(i=0; i<32; i++)
217 sprintf(buf + (i*2),"%02X",hash[i]);
218
219 strcpy(buf + 64,"\r\n");
220
221 return buf;
222 }
223
224 void chop(char *str) {
225 int len;
226
227 if ( str == NULL )
228 return;
229
230 len = strlen(str);
231
232 if ( str[len-2] == '\r' && str[len-1] == '\n' )
233 str[len-2] = '\0';
234 else if ( str[len-1] == '\n' )
235 str[len-1] = '\0';
236 }

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28