~ubuntu-branches/ubuntu/quantal/maradns/quantal

« back to all changes in this revision

Viewing changes to server/MaraDNS.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2012-01-12 23:35:38 UTC
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: package-import@ubuntu.com-20120112233538-5jkaqrh9nbqtf1ey
Tags: upstream-2.0.04+really1.4.09
ImportĀ upstreamĀ versionĀ 2.0.04+really1.4.09

Show diffs side-by-side

added added

removed removed

Lines of Context:
207
207
 
208
208
/* This displays ipv6 ips; thanks Remmy */
209
209
 
210
 
#ifdef IPV6
 
210
#ifdef AUTHONLY
211
211
/* Cygwin doesn't have ipv6 yet */
212
212
#ifndef __CYGWIN__
213
213
#ifndef MINGW32
481
481
                sendto(sock,reply->string,reply->unit_count,0,
482
482
                                (struct sockaddr *)ect->d,ect->addrlen);
483
483
                return JS_SUCCESS;
484
 
#ifdef IPV6
 
484
#ifdef AUTHONLY
485
485
/* Cygwin doesn't have ipv6 yet */
486
486
#ifndef __CYGWIN__
487
487
        } else if(ect->type == 6) {
874
874
    int len_inet = sizeof(struct sockaddr);
875
875
    int found = 0;
876
876
    int authoritative = 1;
 
877
    rr *where = 0;
877
878
    rr_list *answer = 0;
878
879
    mhash_e spot_data;
879
880
    int counter;
920
921
    header.nscount = 0;
921
922
    header.arcount = 0;
922
923
 
 
924
    if(called_from_recursive == 1) {
 
925
        goto use_old_udpany_code;
 
926
    }
 
927
 
923
928
    /* Start synthesizing the reply */
924
929
    /* Look for the list of all of the answers */
925
930
    spot_data = mhash_get(bighash,query);
1026
1031
    js_destroy(starwhitis);
1027
1032
    goto old_udpany_code_disabled;
1028
1033
 
 
1034
use_old_udpany_code:
 
1035
 
 
1036
    /* The old udpany code is still used by the recursive half of
 
1037
     * MaraDNS, so we have to keep it for the time being */
 
1038
 
 
1039
    /* Look for an A record with the same name as the query */
 
1040
    if(change_rtype(query,RR_A) == JS_ERROR)
 
1041
        goto giveerror;
 
1042
    spot_data = mhash_get(bighash,query);
 
1043
    /* If found, add the data to our records */
 
1044
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1045
        found = 1;
 
1046
        where = spot_data.value;
 
1047
        authoritative = where->authoritative;
 
1048
        if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1049
                   &(header.nscount),&(header.arcount),1,
 
1050
                   spot_data.point,0,0) == JS_ERROR)
 
1051
            goto giveerror;
 
1052
        }
 
1053
    /* Look for MX record with the same name as the query */
 
1054
    if(change_rtype(query,RR_MX) == JS_ERROR)
 
1055
        goto giveerror;
 
1056
    spot_data = mhash_get(bighash,query);
 
1057
    if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1058
        if(found == 1) {
 
1059
            if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1060
               &(header.nscount),&(header.arcount),0,
 
1061
               spot_data.point,0,0) == JS_ERROR)
 
1062
                goto giveerror;
 
1063
            }
 
1064
        else {
 
1065
            where = spot_data.value;
 
1066
            authoritative = where->authoritative;
 
1067
            if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1068
               &(header.nscount),&(header.arcount),1,
 
1069
               spot_data.point,0,0) == JS_ERROR)
 
1070
                goto giveerror;
 
1071
            }
 
1072
        found = 1;
 
1073
        }
 
1074
 
 
1075
    /* We optionally look for NS and SOA on an RR_ANY query */
 
1076
    if(rr_set == 15) {
 
1077
      /* Look for NS record with the same name as the query */
 
1078
      if(change_rtype(query,RR_NS) == JS_ERROR)
 
1079
        goto giveerror;
 
1080
      spot_data = mhash_get(bighash,query);
 
1081
      if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1082
        if(found == 1) {
 
1083
            if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1084
               &(header.nscount),&(header.arcount),0,
 
1085
               spot_data.point,0,0) == JS_ERROR)
 
1086
                goto giveerror;
 
1087
            }
 
1088
        else {
 
1089
            where = spot_data.value;
 
1090
            authoritative = where->authoritative;
 
1091
            if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1092
               &(header.nscount),&(header.arcount),1,
 
1093
               spot_data.point,0,0) == JS_ERROR)
 
1094
                goto giveerror;
 
1095
            }
 
1096
        found = 1;
 
1097
        }
 
1098
      /* Look for SOA record with the same name as the query */
 
1099
      if(change_rtype(query,RR_SOA) == JS_ERROR)
 
1100
        goto giveerror;
 
1101
      spot_data = mhash_get(bighash,query);
 
1102
      if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1103
        if(found == 1) {
 
1104
            if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1105
               &(header.nscount),&(header.arcount),0,
 
1106
               spot_data.point,0,0) == JS_ERROR)
 
1107
                goto giveerror;
 
1108
            }
 
1109
        else {
 
1110
            where = spot_data.value;
 
1111
            authoritative = where->authoritative;
 
1112
            if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1113
               &(header.nscount),&(header.arcount),1,
 
1114
               spot_data.point,0,0) == JS_ERROR)
 
1115
                goto giveerror;
 
1116
            }
 
1117
        found = 1;
 
1118
        }
 
1119
      }
 
1120
 
 
1121
    /* If not found, look for lower-case version of the same query */
 
1122
    if(found != 1) {
 
1123
        found = fold_case(query);
 
1124
        if(found == JS_ERROR)
 
1125
            goto giveerror;
 
1126
        if(found == 1) /* Case folded */ {
 
1127
            found = 0;
 
1128
            /* Look for lower case version of A record */
 
1129
            if(change_rtype(query,RR_A) == JS_ERROR)
 
1130
                goto giveerror;
 
1131
            spot_data = mhash_get(bighash,query);
 
1132
            /* If A record of lower-case found... */
 
1133
            if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1134
                found = 1;
 
1135
                where = spot_data.value;
 
1136
                authoritative = where->authoritative;
 
1137
                if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1138
                   &(header.nscount),&(header.arcount),1,
 
1139
                   spot_data.point,0,0) == JS_ERROR)
 
1140
                    goto giveerror;
 
1141
                }
 
1142
            if(change_rtype(query,RR_MX) == JS_ERROR)
 
1143
                goto giveerror;
 
1144
            spot_data = mhash_get(bighash,query);
 
1145
            /* If MX record of lower-case found... */
 
1146
            if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1147
                if(found == 1) {
 
1148
                    if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1149
                       &(header.nscount),&(header.arcount),0,
 
1150
                       spot_data.point,0,0) == JS_ERROR)
 
1151
                        goto giveerror;
 
1152
                    }
 
1153
                else {
 
1154
                    where = spot_data.value;
 
1155
                    authoritative = where->authoritative;
 
1156
                    if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1157
                       &(header.nscount),&(header.arcount),1,
 
1158
                       spot_data.point,0,0) == JS_ERROR)
 
1159
                        goto giveerror;
 
1160
                    }
 
1161
                }
 
1162
            /* Optionally look for SOA and NS records */
 
1163
            if(rr_set == 15) {
 
1164
              if(change_rtype(query,RR_NS) == JS_ERROR)
 
1165
                goto giveerror;
 
1166
              spot_data = mhash_get(bighash,query);
 
1167
              /* If NS record of lower-case found... */
 
1168
              if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1169
                if(found == 1) {
 
1170
                    if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1171
                       &(header.nscount),&(header.arcount),0,
 
1172
                       spot_data.point,0,0) == JS_ERROR)
 
1173
                        goto giveerror;
 
1174
                    }
 
1175
                else {
 
1176
                    where = spot_data.value;
 
1177
                    authoritative = where->authoritative;
 
1178
                    if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1179
                       &(header.nscount),&(header.arcount),1,
 
1180
                       spot_data.point,0,0) == JS_ERROR)
 
1181
                        goto giveerror;
 
1182
                    }
 
1183
                }
 
1184
              if(change_rtype(query,RR_SOA) == JS_ERROR)
 
1185
                goto giveerror;
 
1186
              spot_data = mhash_get(bighash,query);
 
1187
              /* If SOA record of lower-case found... */
 
1188
              if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1189
                if(found == 1) {
 
1190
                    if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1191
                       &(header.nscount),&(header.arcount),0,
 
1192
                       spot_data.point,0,0) == JS_ERROR)
 
1193
                        goto giveerror;
 
1194
                    }
 
1195
                else {
 
1196
                    where = spot_data.value;
 
1197
                    authoritative = where->authoritative;
 
1198
                    if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1199
                       &(header.nscount),&(header.arcount),1,
 
1200
                       spot_data.point,0,0) == JS_ERROR)
 
1201
                        goto giveerror;
 
1202
                    }
 
1203
                }
 
1204
              }
 
1205
            }
 
1206
        if(found == 0) { /* If not found, do star record search */
 
1207
                found = starwhitis_seek_any(query,RR_A,0,&header,&where,
 
1208
                                &authoritative,most,ns,ar);
 
1209
                found = starwhitis_seek_any(query,RR_MX,found,&header,
 
1210
                                &where,&authoritative,most,ns,ar);
 
1211
                if(rr_set == 15) {
 
1212
                        found = starwhitis_seek_any(query,RR_NS,found,
 
1213
                                        &header,&where,&authoritative,most,
 
1214
                                        ns,ar);
 
1215
                        found = starwhitis_seek_any(query,RR_SOA,found,
 
1216
                                        &header,&where,&authoritative,most,
 
1217
                                        ns,ar);
 
1218
                }
 
1219
                if(found == JS_ERROR) {
 
1220
                        goto giveerror;
 
1221
                }
 
1222
        }
 
1223
       }
 
1224
 
 
1225
    /* If nothing found, look for a CNAME record with the same name as
 
1226
       the query */
 
1227
    if(found == 0) {
 
1228
        if(change_rtype(query,RR_CNAME) == JS_ERROR)
 
1229
            goto giveerror;
 
1230
        spot_data = mhash_get(bighash,query);
 
1231
        /* If found, add the data to our records */
 
1232
        if(spot_data.value != 0 && spot_data.datatype == MARA_DNSRR) {
 
1233
            found = 1;
 
1234
            if(add_answer(spot_data.value,most,ns,ar,&(header.ancount),
 
1235
                         &(header.nscount),&(header.arcount),1,
 
1236
                         spot_data.point,0,0) == JS_ERROR)
 
1237
                goto giveerror;
 
1238
            }
 
1239
        }
 
1240
 
 
1241
    /* If nothing found, look for DDIP notation */
 
1242
    if(found == 0) {
 
1243
        if(change_rtype(query,RR_ANY) == JS_ERROR)
 
1244
            goto giveerror;
 
1245
        found = ddip_check(id,sock,ect,query);
 
1246
        if(found == JS_ERROR)
 
1247
            goto giveerror;
 
1248
        if(found == JS_SUCCESS) {
 
1249
            js_destroy(ar);
 
1250
            js_destroy(ns);
 
1251
            js_destroy(most);
 
1252
            return JS_SUCCESS;
 
1253
            }
 
1254
        }
 
1255
 
1029
1256
old_udpany_code_disabled:
1030
1257
 
1031
1258
    /* Return with exit code of 0 if no answer was found */
1074
1301
 
1075
1302
    /* Check to make sure the data fits in under 512 bytes */
1076
1303
    if(ar->unit_count > 512) {
1077
 
 
1078
 
        /* If this is an ipv4 connection and we didn't get a compress error */
1079
 
        if(ect->type == 4) {
1080
 
            struct sockaddr_in *dq;
1081
 
            uint32 ip_test;
1082
 
            dq = (struct sockaddr_in *)(ect->d);
1083
 
            ip_test = ntohl(dq->sin_addr.s_addr);
1084
 
            /* See if we are allowed to send a long packet up to
1085
 
             * 4096 bytes to this ip address */
1086
 
            if(check_ipv4_acl(ip_test,long_packet) == 1) {
1087
 
                if(ar->unit_count < 4096) {
1088
 
                    goto long_packet_ok;
1089
 
                    }
1090
 
                }
1091
 
            }
1092
 
 
1093
1304
        /* We handle truncation by truncating everything except the
1094
1305
           12-byte header */
1095
1306
        header.tc = 1;
1096
 
        header.ancount = 0;
1097
1307
        make_hdr(&header,ar);
1098
 
        /* Append the question, if there is one */
1099
 
        if(query != 0) {
1100
 
           js_append(query,ar);
1101
 
           js_adduint16(ar,1);
1102
 
           }
1103
1308
        }
1104
1309
 
1105
 
long_packet_ok:
1106
 
 
1107
1310
    /* Success! Put out the good data */
1108
1311
    if(ect == 0) {
1109
1312
        sendto(sock,ar->string,ar->unit_count,0,
3409
3612
/* We don't allow both recursive and ipv6 support, since the recursive
3410
3613
 * resolver is ipv4-only */
3411
3614
 
3412
 
#ifdef IPV6
 
3615
#ifdef AUTHONLY
3413
3616
/* Cygwin doesn't have ipv6 yet */
3414
3617
#ifndef __CYGWIN__
3415
3618
/* Bind to IPV6 UDP port 53. (or DNS_PORT if debugging MaraDNS on a system
3465
3668
    return JS_SUCCESS;
3466
3669
    }
3467
3670
#endif /* __CYGWIN__ */
3468
 
#endif /* IPV6 */
 
3671
#endif /* AUTHONLY */
3469
3672
 
3470
3673
/* Return a list of addresses we are bound to
3471
3674
 * Input: None
3501
3704
    int max_socket;
3502
3705
    struct timeval timeout;
3503
3706
    struct sockaddr_in *ipv4_client;
3504
 
#ifdef IPV6
 
3707
#ifdef AUTHONLY
3505
3708
/* Cugwin doesn't support ipv6 yet */
3506
3709
#ifndef __CYGWIN__
3507
3710
    struct sockaddr_in6 *ipv6_client;
3528
3731
            }
3529
3732
        counter++;
3530
3733
        }
3531
 
#ifdef IPV6
 
3734
#ifdef AUTHONLY
3532
3735
/* Cygwin doesn't have ipv6 yet */
3533
3736
#ifndef __CYGWIN__
3534
3737
    if(have_ipv6_address == 1) {
3583
3786
            }
3584
3787
        counter++;
3585
3788
        }
3586
 
#ifdef IPV6
 
3789
#ifdef AUTHONLY
3587
3790
/* Cygwin doesn't have ipv6 yet */
3588
3791
#ifndef __CYGWIN__
3589
3792
    if(have_ipv6_address == 1) {
3618
3821
            }
3619
3822
        }
3620
3823
#endif /* __CYGWIN__ */
3621
 
#endif /* IPV6 */
 
3824
#endif /* AUTHONLY */
3622
3825
 
3623
3826
    /* "JS_ERROR" means "nobody talked to us in the last second" */
3624
3827
    ect->type = 0;
3749
3952
    int recurse_number_ports = 4096;
3750
3953
#endif
3751
3954
 
 
3955
    /* First order of business: Initialize the hash */
 
3956
    if(mhash_set_add_constant(
 
3957
#ifdef MINGW32
 
3958
        "secret.txt"
 
3959
#else
 
3960
        "/dev/urandom"
 
3961
#endif
 
3962
                ) != 1) {
 
3963
        printf(
 
3964
#ifdef MINGW32
 
3965
        "Fatal error opening secret.txt"
 
3966
#else
 
3967
        "Fatal error opening /dev/urandom"
 
3968
#endif
 
3969
                        );
 
3970
                        return 32;
 
3971
        }
 
3972
 
3752
3973
    memset(&client,0,sizeof(client)); /* Initialize ya variables */
3753
3974
    clin = (struct sockaddr_in *)&client;
3754
3975
#ifdef AUTHONLY
4174
4395
    if(verbstr != 0) { js_destroy(verbstr); verbstr = 0; }
4175
4396
    verbstr = read_string_kvar("recursive_acl");
4176
4397
    if(verbstr != 0 && js_length(verbstr) > 0) {
4177
 
            harderror("No recursion in MaraDNS 2; use Deadwood");
 
4398
            harderror("No recursion when MaraDNS is compiled authonly");
4178
4399
    }
4179
4400
#endif /* AUTHONLY */
4180
4401
 
4287
4508
        js_destroy(ipv4_bind_address);
4288
4509
 
4289
4510
        ipv6_bind_address = read_string_kvar("ipv6_bind_address");
4290
 
#ifndef IPV6
 
4511
#ifndef AUTHONLY
4291
4512
        /* If there is an ipv6 bind address, have MaraDNS return an error */
4292
4513
        if(js_length(ipv6_bind_address) >= 1) {
4293
 
                harderror("maradns must be compiled as ipv6 to have ipv6 support\n"
4294
 
                "./configure --ipv6 ; make will compile maradns thusly\n"
4295
 
                );
 
4514
                harderror("maradns must be compiled as authonly to have ipv6 support\n"
 
4515
                "./configure --authonly ; make will compile maradns thusly\n"
 
4516
                "Note that this will disable recursion and caching");
4296
4517
        }
4297
4518
#else
4298
4519
/* Cygwin doesn't have ipv6 support yet */
4316
4537
                exit(1);
4317
4538
        }
4318
4539
#endif /* __CYGWIN__ */
4319
 
#endif /* IPV6 */
 
4540
#endif /* AUTHONLY */
4320
4541
 
4321
4542
#ifndef MINGW32
4322
4543
        /* Drop the elevated privileges */
4352
4573
    if(1 == 2) {
4353
4574
#endif /* __CYGWIN__ */
4354
4575
#ifndef ALLOW_NON_ROOT
4355
 
        harderror("Running MaraDNS 2.0 as a non-root server support disabled");
 
4576
        harderror("Running MaraDNS 1.2 as a non-root server support disabled");
4356
4577
#else
4357
4578
        /* Bind to port 53 as a non-root user */
4358
4579
        bind_address = read_string_kvar("bind_address");
4554
4775
                if(ect.type == 4) {
4555
4776
                    clin = (struct sockaddr_in *)(ect.d);
4556
4777
                }
4557
 
#ifdef IPV6
 
4778
#ifdef AUTHONLY
4558
4779
/* Cygwin doesn't have ipv6 support yet */
4559
4780
#ifndef __CYGWIN__
4560
4781
                else {
4568
4789
                if(ect.type == 4) {
4569
4790
                    debug_show_ip(ntohl(clin->sin_addr.s_addr));
4570
4791
                } else {
4571
 
#ifdef IPV6
 
4792
#ifdef AUTHONLY
4572
4793
/* Cygwin doesn't have ipv6 support yet */
4573
4794
#ifndef __CYGWIN__
4574
4795
                    debug_show_socket_ipv6(clin6);
4601
4822
            value = js_readuint16(uncomp,12+counter);
4602
4823
            if(js_substr(uncomp,incoming,12,counter) != JS_ERROR) {
4603
4824
                clin = (struct sockaddr_in *)(ect.d);
4604
 
#ifdef IPV6
 
4825
#ifdef AUTHONLY
4605
4826
/* Cygwin doesn't have ipv6 support yet */
4606
4827
#ifndef __CYGWIN__
4607
4828
                clin6 = (struct sockaddr_in6 *)(ect.d);
4615
4836
                if(ect.type == 4) {
4616
4837
                    debug_show_ip(ntohl(clin->sin_addr.s_addr));
4617
4838
                } else {
4618
 
#ifdef IPV6
 
4839
#ifdef AUTHONLY
4619
4840
/* Cygwin doesn't have ipv6 support yet */
4620
4841
#ifndef __CYGWIN__
4621
4842
                    debug_show_socket_ipv6(clin6);