Annotation of 43BSDReno/usr.sbin/named/tools/nstest/nstest.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1986, 1989 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted provided
                      6:  * that: (1) source distributions retain this entire copyright notice and
                      7:  * comment, and (2) distributions including binaries display the following
                      8:  * acknowledgement:  ``This product includes software developed by the
                      9:  * University of California, Berkeley and its contributors'' in the
                     10:  * documentation or other materials provided with the distribution and in
                     11:  * all advertising materials mentioning features or use of this software.
                     12:  * Neither the name of the University nor the names of its contributors may
                     13:  * be used to endorse or promote products derived from this software without
                     14:  * specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: char copyright[] =
                     22: "@(#) Copyright (c) 1986 Regents of the University of California.\n\
                     23:  All rights reserved.\n";
                     24: #endif /* not lint */
                     25: 
                     26: #ifndef lint
                     27: static char sccsid[] = "@(#)nstest.c   4.12 (Berkeley) 6/1/90";
                     28: #endif /* not lint */
                     29: 
                     30: #include <sys/types.h>
                     31: #include <sys/socket.h>
                     32: #include <netinet/in.h>
                     33: #include <stdio.h>
                     34: #include <arpa/nameser.h>
                     35: #include <resolv.h>
                     36: 
                     37: extern char *inet_ntoa();
                     38: char *progname;
                     39: FILE *log;
                     40: #define MAXDATA                256   /* really should get definition from named/db.h */
                     41: main(argc, argv)
                     42:        char **argv;
                     43: {
                     44:        register char *cp;
                     45:        struct hostent *hp;
                     46:        u_short port = htons(NAMESERVER_PORT);
                     47:        char buf[BUFSIZ];
                     48:        char packet[PACKETSZ];
                     49:        char answer[PACKETSZ];
                     50:        struct rrec NewRR;
                     51:        char OldRRData[MAXDATA];
                     52:        int n, dump_packet;
                     53: 
                     54:        NewRR.r_data = (char *) malloc(MAXDATA);
                     55:        NewRR.r_data = (char *) malloc(MAXDATA);
                     56:        progname = argv[0];
                     57:        dump_packet = 0;
                     58:        _res.options |= RES_DEBUG|RES_RECURSE;
                     59:        (void) res_init();
                     60:        while (argc > 1 && argv[1][0] == '-') {
                     61:                argc--;
                     62:                cp = *++argv;
                     63:                while (*++cp)
                     64:                        switch (*cp) {
                     65:                        case 'p':
                     66:                                if (--argc <= 0)
                     67:                                        usage();
                     68:                                port = htons(atoi(*++argv));
                     69:                                break;
                     70: 
                     71:                        case 'i':
                     72:                                _res.options |= RES_IGNTC;
                     73:                                break;
                     74: 
                     75:                        case 'v':
                     76:                                _res.options |= RES_USEVC|RES_STAYOPEN;
                     77:                                break;
                     78: 
                     79:                        case 'r':
                     80:                                _res.options &= ~RES_RECURSE;
                     81:                                break;
                     82: 
                     83:                        case 'd':
                     84:                                dump_packet++;
                     85:                                break;
                     86: 
                     87:                        default:
                     88:                                usage();
                     89:                        }
                     90:        }
                     91:        _res.nsaddr.sin_family = AF_INET;
                     92:        _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
                     93:        _res.nsaddr.sin_port = port;
                     94:        if (argc > 1) {
                     95:                _res.nsaddr.sin_addr.s_addr = inet_addr(argv[1]);
                     96:                if (_res.nsaddr.sin_addr.s_addr == (u_long) -1)
                     97:                        usage();
                     98:        }
                     99:        if (argc > 2) {
                    100:                log = fopen(argv[2],"w");
                    101:                if (log == NULL) perror(argv[2]);
                    102:        }
                    103:        for (;;) {
                    104:                printf("> ");
                    105:                fflush(stdout);
                    106:                if ((cp = (char *)gets(buf)) == NULL)
                    107:                        break;
                    108:                switch (*cp++) {
                    109:                case 'a':
                    110:                        n = res_mkquery(QUERY, cp, C_IN, T_A, (char *)0, 0,
                    111:                                NULL, packet, sizeof(packet));
                    112:                        break;
                    113: 
                    114:                case 'A':
                    115:                        n = ntohl(inet_addr(cp));
                    116:                        putlong(n, cp);
                    117:                        n = res_mkquery(IQUERY, "", C_IN, T_A, cp, sizeof(long),
                    118:                                NULL, packet, sizeof(packet));
                    119:                        break;
                    120: 
                    121:                case 'f':
                    122:                        n = res_mkquery(QUERY, cp, C_ANY, T_UINFO, (char *)0, 0,
                    123:                                NULL, packet, sizeof(packet));
                    124:                        break;
                    125: 
                    126:                case 'g':
                    127:                        n = res_mkquery(QUERY, cp, C_ANY, T_GID, (char *)0, 0,
                    128:                                NULL, packet, sizeof(packet));
                    129:                        break;
                    130: 
                    131:                case 'G':
                    132:                        *(int *)cp = htonl(atoi(cp));
                    133:                        n = res_mkquery(IQUERY, "", C_ANY, T_GID, cp,
                    134:                                sizeof(int), NULL, packet, sizeof(packet));
                    135:                        break;
                    136: 
                    137:                case 'c':
                    138:                        n = res_mkquery(QUERY, cp, C_IN, T_CNAME, (char *)0, 0,
                    139:                                NULL, packet, sizeof(packet));
                    140:                        break;
                    141: 
                    142:                case 'h':
                    143:                        n = res_mkquery(QUERY, cp, C_IN, T_HINFO, (char *)0, 0,
                    144:                                NULL, packet, sizeof(packet));
                    145:                        break;
                    146: 
                    147:                case 'm':
                    148:                        n = res_mkquery(QUERY, cp, C_IN, T_MX, (char *)0, 0,
                    149:                                NULL, packet, sizeof(packet));
                    150:                        break;
                    151: 
                    152:                case 'M':
                    153:                        n = res_mkquery(QUERY, cp, C_IN, T_MAILB, (char *)0, 0,
                    154:                                NULL, packet, sizeof(packet));
                    155:                        break;
                    156: 
                    157:                case 'n':
                    158:                        n = res_mkquery(QUERY, cp, C_IN, T_NS, (char *)0, 0,
                    159:                                NULL, packet, sizeof(packet));
                    160:                        break;
                    161: 
                    162:                case 'p':
                    163:                        n = res_mkquery(QUERY, cp, C_IN, T_PTR, (char *)0, 0,
                    164:                                NULL, packet, sizeof(packet));
                    165:                        break;
                    166: 
                    167:                case 's':
                    168:                        n = res_mkquery(QUERY, cp, C_IN, T_SOA, (char *)0, 0,
                    169:                                NULL, packet, sizeof(packet));
                    170:                        break;
                    171: 
                    172:                case 'T':
                    173:                        n = res_mkquery(QUERY, cp, C_IN, T_TXT, (char *)0, 0,
                    174:                                NULL, packet, sizeof(packet));
                    175:                        break;
                    176: 
                    177:                case 'u':
                    178:                        n = res_mkquery(QUERY, cp, C_ANY, T_UID, (char *)0, 0,
                    179:                                NULL, packet, sizeof(packet));
                    180:                        break;
                    181: 
                    182:                case 'U':
                    183:                        *(int *)cp = htonl(atoi(cp));
                    184:                        n = res_mkquery(IQUERY, "", C_ANY, T_UID, cp,
                    185:                                sizeof(int), NULL, packet, sizeof(packet));
                    186:                        break;
                    187: 
                    188:                case 'x':
                    189:                        n = res_mkquery(QUERY, cp, C_IN, T_AXFR, (char *)0, 0,
                    190:                                NULL, packet, sizeof(packet));
                    191:                        break;
                    192: 
                    193:                case 'w':
                    194:                        n = res_mkquery(QUERY, cp, C_IN, T_WKS, (char *)0, 0,
                    195:                                NULL, packet, sizeof(packet));
                    196:                        break;
                    197: 
                    198:                case 'b':
                    199:                        n = res_mkquery(QUERY, cp, C_IN, T_MB, (char *)0, 0,
                    200:                                NULL, packet, sizeof(packet));
                    201:                        break;
                    202: 
                    203:                case 'B':
                    204:                        n = res_mkquery(QUERY, cp, C_IN, T_MG, (char *)0, 0,
                    205:                                NULL, packet, sizeof(packet));
                    206:                        break;
                    207: 
                    208:                case 'i':
                    209:                        n = res_mkquery(QUERY, cp, C_IN, T_MINFO, (char *)0, 0,
                    210:                                NULL, packet, sizeof(packet));
                    211:                        break;
                    212: 
                    213:                case 'r':
                    214:                        n = res_mkquery(QUERY, cp, C_IN, T_MR, (char *)0, 0,
                    215:                                NULL, packet, sizeof(packet));
                    216:                        break;
                    217: 
                    218:                case '*':
                    219:                        n = res_mkquery(QUERY, cp, C_IN, T_ANY, (char *)0, 0,
                    220:                                NULL, packet, sizeof(packet));
                    221:                        break;
                    222: 
                    223: #ifdef ALLOW_UPDATES
                    224:                case '^':
                    225:                        {
                    226:                            char IType[10], TempStr[50];
                    227:                            int Type, oldnbytes, nbytes, i;
                    228: #ifdef ALLOW_T_UNSPEC
                    229:                            printf("Data type (a = T_A, u = T_UNSPEC): ");
                    230:                            gets(IType);
                    231:                            if (IType[0] == 'u') {
                    232:                                Type = T_UNSPEC;
                    233:                                printf("How many data bytes? ");
                    234:                                gets(TempStr); /* Throw away CR */
                    235:                                sscanf(TempStr, "%d", &nbytes);
                    236:                                for (i = 0; i < nbytes; i++) {
                    237:                                        (NewRR.r_data)[i] = (char) i;
                    238:                                }
                    239:                            } else {
                    240: #endif ALLOW_T_UNSPEC
                    241:                                Type = T_A;
                    242:                                nbytes = sizeof(u_long);
                    243:                                printf("Inet addr for new dname (e.g., 192.4.3.2): ");
                    244:                                gets(TempStr);
                    245:                                putlong(ntohl(inet_addr(TempStr)), NewRR.r_data);
                    246: #ifdef ALLOW_T_UNSPEC
                    247:                            }
                    248: #endif ALLOW_T_UNSPEC
                    249:                            NewRR.r_class = C_IN;
                    250:                            NewRR.r_type = Type;
                    251:                            NewRR.r_size = nbytes;
                    252:                            NewRR.r_ttl = 99999999;
                    253:                            printf("Add, modify, or modify all (a/m/M)? ");
                    254:                            gets(TempStr);
                    255:                            if (TempStr[0] == 'a') {
                    256:                                n = res_mkquery(UPDATEA, cp, C_IN, Type,
                    257:                                                OldRRData, nbytes,
                    258:                                                &NewRR, packet,
                    259:                                                sizeof(packet));
                    260:                            } else {
                    261:                                if (TempStr[0] == 'm') {
                    262:                                    printf("How many data bytes in old RR? ");
                    263:                                    gets(TempStr); /* Throw away CR */
                    264:                                    sscanf(TempStr, "%d", &oldnbytes);
                    265:                                    for (i = 0; i < oldnbytes; i++) {
                    266:                                            OldRRData[i] = (char) i;
                    267:                                    }
                    268:                                        n = res_mkquery(UPDATEM, cp, C_IN, Type,
                    269:                                                        OldRRData, oldnbytes,
                    270:                                                        &NewRR, packet,
                    271:                                                        sizeof(packet));
                    272:                                } else { /* Modify all */
                    273:                                        n = res_mkquery(UPDATEMA, cp,
                    274:                                                        C_IN, Type, NULL, 0,
                    275:                                                        &NewRR, packet,
                    276:                                                        sizeof(packet));
                    277: 
                    278:                                }
                    279:                            }
                    280:                        }
                    281:                        break;
                    282: 
                    283: #ifdef ALLOW_T_UNSPEC
                    284:                case 'D':
                    285:                        n = res_mkquery(UPDATEDA, cp, C_IN, T_UNSPEC, (char *)0,
                    286:                                        0, NULL, packet, sizeof(packet));
                    287:                        break;
                    288: 
                    289:                case 'd':
                    290:                        {
                    291:                                char TempStr[100];
                    292:                                int nbytes, i;
                    293:                                printf("How many data bytes in oldrr data? ");
                    294:                                gets(TempStr); /* Throw away CR */
                    295:                                sscanf(TempStr, "%d", &nbytes);
                    296:                                for (i = 0; i < nbytes; i++) {
                    297:                                        OldRRData[i] = (char) i;
                    298:                                }
                    299:                                n = res_mkquery(UPDATED, cp, C_IN, T_UNSPEC,
                    300:                                                OldRRData, nbytes, NULL, packet,
                    301:                                                sizeof(packet));
                    302:                        }
                    303:                        break;
                    304: #endif ALLOW_T_UNSPEC
                    305: #endif ALLOW_UPDATES
                    306: 
                    307:                default:
                    308:                        printf("a{host} - query  T_A\n");
                    309:                        printf("A{addr} - iquery T_A\n");
                    310:                        printf("b{user} - query  T_MB\n");
                    311:                        printf("B{user} - query  T_MG\n");
                    312:                        printf("f{host} - query  T_UINFO\n");
                    313:                        printf("g{host} - query  T_GID\n");
                    314:                        printf("G{gid}  - iquery T_GID\n");
                    315:                        printf("h{host} - query  T_HINFO\n");
                    316:                        printf("i{host} - query  T_MINFO\n");
                    317:                        printf("p{host} - query  T_PTR\n");
                    318:                        printf("m{host} - query  T_MX\n");
                    319:                        printf("M{host} - query  T_MAILB\n");
                    320:                        printf("n{host} - query  T_NS\n");
                    321:                        printf("r{host} - query  T_MR\n");
                    322:                        printf("s{host} - query  T_SOA\n");
                    323:                        printf("T{host} - query  T_TXT\n");
                    324:                        printf("u{host} - query  T_UID\n");
                    325:                        printf("U{uid}  - iquery T_UID\n");
                    326:                        printf("x{host} - query  T_AXFR\n");
                    327:                        printf("w{host} - query  T_WKS\n");
                    328:                        printf("c{host} - query  T_CNAME\n");
                    329:                        printf("*{host} - query  T_ANY\n");
                    330: #ifdef ALLOW_UPDATES
                    331:                        printf("^{host} - add/mod/moda    (T_A/T_UNSPEC)\n");
                    332: #ifdef ALLOW_T_UNSPEC
                    333:                        printf("D{host} - deletea T_UNSPEC\n");
                    334:                        printf("d{host} - delete T_UNSPEC\n");
                    335: #endif ALLOW_T_UNSPEC
                    336: #endif ALLOW_UPDATES
                    337:                        continue;
                    338:                }
                    339:                if (n < 0) {
                    340:                        printf("res_mkquery: buffer too small\n");
                    341:                        continue;
                    342:                }
                    343:                if (log) {
                    344:                        fprintf(log,"SEND QUERY\n");
                    345:                        fp_query(packet, log);
                    346:                }
                    347:                n = res_send(packet, n, answer, sizeof(answer));
                    348:                if (n < 0) {
                    349:                        printf("res_send: send error\n");
                    350:                        if (log) fprintf(log, "res_send: send error\n");
                    351:                }
                    352:                else {
                    353:                        if (dump_packet) {
                    354:                                int f;
                    355:                                f = creat("ns_packet.dump", 0644);
                    356:                                write(f, answer, n);
                    357:                                (void) close(f);
                    358:                        }
                    359:                        if (log) {
                    360:                                fprintf(log, "GOT ANSWER\n");
                    361:                                fp_query(answer, log);
                    362:                        }
                    363:                }
                    364:        }
                    365: }
                    366: 
                    367: usage()
                    368: {
                    369:        fprintf(stderr, "Usage: %s [-v] [-i] [-r] [-d] [-p port] hostaddr\n",
                    370:                progname);
                    371:        exit(1);
                    372: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.