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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/* Copyright (c) 2002 Sam Trenholme
*
* TERMS
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* This software is provided 'as is' with no guarantees of correctness or
* fitness for purpose.
*/
#include "../libs/JsStr.h"
#include "../MaraDns.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#ifdef __FreeBSD__
#include <sys/time.h>
#endif
#include <sys/types.h>
#ifndef DARWIN
#include <sys/resource.h>
#endif
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
/* A define */
#define BUFSIZE 65000
/* Test zone server which does the following:
* Bind on port 53
* Wait for someone to connect
* When someone connects, they will send a two-byte big-endian number
followed by that number fo bytes
* After this packet is sent, this program will open up a file
called "test_zonedata.txt". It will convert each line, which is
an octal-escaped JS-string sequence, in to a binary js-string object.
Each line will be sent as the same two-octet length followed by the
raw binary data in question.
* After it is done, it will close the connection on port 53.
*/
/* Bind to TCP port 53 on IP address 127.0.0.99
Input: pointer to socket to bind on
Output: JS_ERROR on error, JS_SUCCESS on success
*/
int tcpbind(int *sock) {
int len_inet; /* Length */
struct sockaddr_in dns_tcp;
char *ip = "127.0.0.99";
/* Sanity checks */
if(sock == 0)
return JS_ERROR;
/* Create a raw TCP socket */
if((*sock = socket(PF_INET,SOCK_STREAM,0)) == -1) {
return JS_ERROR;
}
/* Choose an IP and port to bind to */
memset(&dns_tcp,0,sizeof(dns_tcp));
dns_tcp.sin_family = AF_INET;
dns_tcp.sin_port = htons(53);
if((dns_tcp.sin_addr.s_addr = inet_addr(ip)) == INADDR_NONE)
return JS_ERROR;
len_inet = sizeof(dns_tcp);
/* Bind to the socket. Note that we usually have to be root to do this */
if(bind(*sock,(struct sockaddr *)&dns_tcp,len_inet) == -1)
return JS_ERROR;
/* Set up an active listen on the socket */
if(listen(*sock,250) == -1)
return JS_ERROR;
/* We are now on TCP port 53. Leave */
return JS_SUCCESS;
}
/* Start a TCP connection on socket sock.
Input: pointer to socket
Output: Integer value of TCP connection on success, JS_ERROR on error
(or permission denied)
*/
int gettcp(int *sock) {
int ret, counter;
struct sockaddr_in adr_clnt;
int len_inet;
uint32 ip;
len_inet = sizeof(adr_clnt);
ret = accept(*sock, (struct sockaddr *)&adr_clnt,&len_inet);
if(ret == -1)
return JS_ERROR;
return ret;
}
/* Given the output of show_esc_stdout, create a binary js_string
object.
Input: js_string object to convert escape sequences in to binary
sequences
Output: Pointer to newly created js_string object which contains
the binary sequence; 0 if any problems happened
*/
js_string *decode_esc_sequence(js_string *esc) {
js_string *ret;
int iplace, oplace, counter, inescape, octal_value;
unsigned char octet;
iplace = oplace = 0;
if(js_has_sanity(esc) != JS_SUCCESS) {
return 0;
}
/* Find out how big to make the output string */
for(counter = 0 ; counter < esc->unit_count ; counter++) {
octet = *(esc->string + counter);
if(octet == '\\') {
counter++;
if(counter >= esc->unit_count) {
return 0;
}
octet = *(esc->string + counter);
/* Anything besides a number is a single character escaped */
if(octet < '0' || octet > '7') {
oplace++;
}
else {
for(inescape = 0; inescape < 3 ; inescape++) {
octet = *(esc->string + counter);
/* We only accept three-digit octal sequences */
if(octet < '0' || octet > '7') {
return 0;
}
if(inescape < 2)
counter++;
if(counter >= esc->unit_count) {
return 0;
}
}
oplace++;
}
}
/* Normal non-escape character */
else if(octet >= 32) {
oplace++;
}
}
/* oplace now has the number of octets the outputted string should
have; if there was anything unusual in the escape sequences,
we will not have gotten to here */
if((ret = js_create(oplace + 2,1)) == 0) {
return 0;
}
ret->unit_count = oplace;
/* Now, copy over the escaped string to the unescaped return string */
oplace = 0;
for(counter = 0 ; counter < esc->unit_count ; counter++) {
octet = *(esc->string + counter);
if(octet == '\\') {
counter++;
if(counter >= esc->unit_count) {
js_destroy(ret);
return 0;
}
octet = *(esc->string + counter);
/* Anything besides a number is a single character escaped */
if(octet < '0' || octet > '7') {
*(ret->string + oplace) = *(esc->string + counter);
oplace++;
}
else {
octal_value = 0;
for(inescape = 0; inescape < 3 ; inescape++) {
octal_value *= 8;
octet = *(esc->string + counter);
/* We only accept three-digit octal sequences */
if(octet < '0' || octet > '7') {
js_destroy(ret);
return 0;
}
octal_value += octet - '0';
if(inescape < 2)
counter++;
if(counter >= esc->unit_count) {
js_destroy(ret);
return 0;
}
}
*(ret->string + oplace) = octal_value;
oplace++;
}
}
/* Normal non-escape character */
else if(octet >= 32) {
*(ret->string + oplace) = *(esc->string + counter);
oplace++;
}
}
return ret;
}
/* The core of the test zoneserver */
int main() {
int sock;
/* Bind to port 53 */
if(tcpbind(&sock) == JS_ERROR) {
printf("Binding problem.\n");
exit(1);
}
/* Drop root privileges */
if(setuid(99) != 0) {
printf("Could not drop root privileges\n");
exit(1);
}
/* Old Linux kernel bug */
if(setuid(0) == 0) {
printf("We're still root!\n");
exit(1);
}
/* Listen for data on the TCP socket */
for(;;) {
int connect;
pid_t pid;
connect = gettcp(&sock);
if(connect == JS_ERROR)
continue;
printf("Someone has connected...\n");
/* Fork off to handle the connection */
pid = fork();
if(!pid) { /* Child */
js_string *line, *gotit;
FILE *in;
unsigned char getline[BUFSIZE];
unsigned char get[2];
int length;
/* Get the two byte length header */
if(recv(connect,get,2,MSG_WAITALL) != 2) {
close(connect); /* Close connection on error */
exit(0); /* End child */
}
/* Determine how long the query will be */
length = (get[0] & 0xff) << 8 | (get[1] & 0xff);
if(length > BUFSIZE - 100) {
close(connect); /* Close connection on error */
exit(0); /* End child */
}
if(recv(connect,getline,length,MSG_WAITALL) != length) {
close(connect); /* Close connection on error */
exit(0); /* End child */
}
/* Now, send them the reply */
if((in = fopen("test_zonedata.txt","rb")) == NULL) {
printf("No test_zonedata.txt file\n");
close(connect); /* Close connection on error */
exit(0);
}
gotit = js_create(BUFSIZE,1);
if(gotit == 0) {
close(connect); /* Close connection on error */
exit(0);
}
/* Get a line */
while(fgets(getline,BUFSIZE - 100,in) != NULL) {
/* And process it */
if(js_qstr2js(gotit,getline) == JS_ERROR) {
close(connect); /* Close connection on error */
exit(0);
}
line = decode_esc_sequence(gotit);
/* Determine the length of the response to send */
get[0] = (line->unit_count & 0xff00) >> 8;
get[1] = line->unit_count & 0xff;
if(write(connect,get,2) == -1) {
close(connect); /* Close connection on error */
exit(0);
}
if(write(connect,line->string,line->unit_count) == -1) {
close(connect); /* Close connection on error */
exit(0);
}
js_destroy(line);
}
close(connect); /* Close connection */
exit(0);
}
/* Hackish way to clean up child processes without needlessly slowing
the program */
while(waitpid(0,NULL,WNOHANG) > 0);
}
}
|