~ubuntu-branches/ubuntu/trusty/maradns/trusty-proposed

« back to all changes in this revision

Viewing changes to deadwood-3.2.05/sqa/sqa_server_fail/microdns_server_fail.c

  • Committer: Package Import Robot
  • Author(s): Dariusz Dwornikowski
  • Date: 2014-02-16 19:36:04 UTC
  • mfrom: (1.2.11) (21.1.11 experimental)
  • Revision ID: package-import@ubuntu.com-20140216193604-xtmcopn9pilzszae
Tags: 2.0.09-1
* New maintainer (Closes: #739084)
* New upstream release to unstable
* Several security bugs (Closes: #739755)
   - security bugfix for CVE-2011-5055, CVE-2011-5056, CVE-2012-0024,
   CVE-2012-1570
   - security bugfix agains blind spoofing attack (no CVE number)
   - security bugfix for packet of death attack (no CVE number)
* Bump standards to 3.9.5
* Updated d/postinst to no longer modify conffiles (Closes: #710903)
* Init script fixed (Closes: #709826)
* --reinstall no longer kills the process (Closes: #701657)
* Updated old d/changelog entries, added information when the CVEs were
  fixed: 2.0.06-1, 2.0.04-1, 1.4.11-1, 1.2.12.06-1, 1.2.12.05-1, 1.0.28-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2009-2010 Sam Trenholme
 
2
 *
 
3
 * TERMS
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer.
 
11
 * 2. Redistributions in binary form must reproduce the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer in the
 
13
 *    documentation and/or other materials provided with the distribution.
 
14
 *
 
15
 * This software is provided 'as is' with no guarantees of correctness or
 
16
 * fitness for purpose.
 
17
 */
 
18
 
 
19
#include <sys/socket.h>
 
20
#include <netinet/in.h>
 
21
#include <arpa/inet.h>
 
22
#include <string.h>
 
23
#include <unistd.h>
 
24
#include <fcntl.h>
 
25
#include <stdlib.h>
 
26
#include <stdio.h>
 
27
#include <stdint.h>
 
28
 
 
29
/* We use a special SOCKET type for easier Windows porting */
 
30
#define SOCKET int
 
31
 
 
32
/* Based on command-line arguments, set the IP we will bind to and the
 
33
   IP we send over the pipe */
 
34
uint32_t get_ip(int argc, char **argv) {
 
35
 
 
36
        uint32_t ip;
 
37
 
 
38
        /* Set the BIND ip and the IP we give everyone */
 
39
        if(argc < 2 || argc > 3) {
 
40
                printf(
 
41
                "Usage: microdns 10.11.5.11 [{ip of microdns server}]\n"
 
42
                );
 
43
                exit(1);
 
44
                }
 
45
 
 
46
        /* Set the IP we give everyone */
 
47
        ip = inet_addr(argv[1]);
 
48
        ip = ntohl(ip);
 
49
 
 
50
        /* Set the IP we bind to (default is "0", which means "all IPs") */
 
51
        ip = 0;
 
52
        if(argc == 3) {
 
53
                ip = inet_addr(argv[2]);
 
54
        }
 
55
        /* Return the IP we bind to */
 
56
        return ip;
 
57
}
 
58
 
 
59
/* Get port: Get a port locally and return the socket the port is on */
 
60
SOCKET get_port(uint32_t ip, char **argv, struct sockaddr_in *dns_udp) {
 
61
        SOCKET sock;
 
62
        int len_inet;
 
63
 
 
64
        /* Bind to port 53 */
 
65
        sock = socket(AF_INET,SOCK_DGRAM,0);
 
66
        if(sock == -1) {
 
67
                perror("socket error");
 
68
                exit(0);
 
69
        }
 
70
        memset(dns_udp,0,sizeof(struct sockaddr_in));
 
71
        dns_udp->sin_family = AF_INET;
 
72
        dns_udp->sin_port = htons(53);
 
73
        dns_udp->sin_addr.s_addr = ip;
 
74
        if(dns_udp->sin_addr.s_addr == INADDR_NONE) {
 
75
                printf("Problem with bind IP %s\n",argv[2]);
 
76
                exit(0);
 
77
        }
 
78
        len_inet = sizeof(struct sockaddr_in);
 
79
        if(bind(sock,(struct sockaddr *)dns_udp,len_inet) == -1) {
 
80
                perror("bind error");
 
81
                exit(0);
 
82
        }
 
83
 
 
84
        /* Linux kernel bug */
 
85
        /* fcntl(sock, F_SETFL, O_NONBLOCK); */
 
86
 
 
87
        return sock;
 
88
}
 
89
 
 
90
int main(int argc, char **argv) {
 
91
        int a, len_inet;
 
92
        SOCKET sock;
 
93
        char in[512];
 
94
        socklen_t foo = sizeof(in);
 
95
        struct sockaddr_in dns_udp;
 
96
        uint32_t ip = 0; /* 0.0.0.0; default bind IP */
 
97
        int leni = sizeof(struct sockaddr);
 
98
 
 
99
        ip = get_ip(argc, argv);
 
100
        sock = get_port(ip,argv,&dns_udp);
 
101
 
 
102
        /* Now that we know the IP and are on port 53, process incoming
 
103
         * DNS requests */
 
104
        for(;;) {
 
105
                /* Get data from UDP port 53 */
 
106
                len_inet = recvfrom(sock,in,255,0,(struct sockaddr *)&dns_udp,
 
107
                        &foo);
 
108
                /* Roy Arends check: We only answer questions */
 
109
                if(len_inet < 3 || (in[2] & 0x80) != 0x00) {
 
110
                        continue;
 
111
                }
 
112
 
 
113
                /* Prepare the reply */
 
114
                if(len_inet > 12) {
 
115
                        /* Make this an answer */
 
116
                        in[2] |= 0x80;
 
117
                        /* Send a SERVER FAIL */
 
118
                        in[3] &= 0xf0; /* Clear RCODE */
 
119
                        in[3] |= 0x02; /* Server fail */
 
120
                }
 
121
 
 
122
                /* Send the reply */
 
123
                sendto(sock,in,len_inet + 16,0, (struct sockaddr *)&dns_udp,
 
124
                        leni);
 
125
        }
 
126
 
 
127
}
 
128