DNS Query Logging in Bind

I wanted to turn on logging of DNS queries on a Solaris 2.7 system running BIND version 8.

BIND does not log DNS queries by default, so you need to enable logging. To do so when you are using BIND 8™ or BIND 9™, you can add the lines shown below to the top of the /etc/named.conf file:

You can determine which version of BIND a name server is running using the command nslookup -type=txt -class=chaos version.bind nameserver

$ nslookup -type=txt -class=chaos version.bind 192.168.1.1
Server:         192.168.1.1
Address:        192.168.1.1#53

VERSION.BIND    text = "BIND 8.1.2"

Additions to named.conf for logging:

logging {
    channel query_logging {
         file "/var/log/named_querylog"
         versions 3 size 100M;
         print-time yes;                 // timestamp log entries
      };

      category queries {
          query_logging;
      };
};

To have the change take effect, you need to kill the named process and restart it , e.g. kill `cat path_to/named.pid` followed by /usr/sbin/in.named to restart the service. You should then be able to view the log of DNS queries.

# cat /var/log/named_querylog
11-May-2009 17:00:34.885 XX /127.0.0.1/inbound.broadbandsupport.net/A
11-May-2009 17:00:36.097 XX /192.168.1.3/cisco.com/A
11-May-2009 17:00:39.883 XX /127.0.0.1/inbound.broadbandsupport.net/A
#

The syntax of the logging section that you add to named.conf is as follows:


     logging {
            [ channel channel_name {
              ( file path_name
                 [ versions ( number | unlimited ) ]
                 [ size size_spec ]
               | syslog ( kern | user | mail | daemon | auth | syslog | lpr |
                          news | uucp | cron | authpriv | ftp |
                          local0 | local1 | local2 | local3 |
                          local4 | local5 | local6 | local7 )
               | null );

              [ severity ( critical | error | warning | notice |
                           info  | debug [ level ] | dynamic ); ]
              [ print-category yes_or_no; ]
              [ print-severity yes_or_no; ]
              [ print-time yes_or_no; ]
            }; ]

            [ category category_name {
              channel_name; [ channel_name; ... ]
            }; ]
            ...
     };

The logging statement configures a wide variety of logging options for the name server. Its channel phrase associates output methods, format options and severity levels with a name that can then be used with the category phrase to select how various classes of messages are logged.

Only one logging statement is used to define as many channels and categories as are wanted. If there are multiple logging statements in a configuration, the first defined determines the logging, and warnings are issued for the others. If there is no logging statement, the default logging configuration will be:


      logging {
         category default { default_syslog; default_debug; };
         category panic { default_syslog; default_stderr; };
         category packet { default_debug; };
         category eventlib { default_debug; };
     };

The Channel Phrase:

All log output goes to one or more "channels"; you can make as many of them as you want.

Every channel definition must include a clause that says whether messages selected for the channel go to a file, to a particular syslog facility, or are discarded. It can optionally also limit the message severity level that will be accepted by the channel (default is "info"), and whether to include a named-generated time stamp, the category name and/or severity level (default is not to include any).

The word null as the destination option for the channel will cause all messages sent to it to be discarded; other options for the channel are meaningless.

The file clause can include limitations both on how large the file is allowed to become, and how many versions of the file will be saved each time the file is opened.

The size option for files is simply a hard ceiling on log growth. If the file ever exceeds the size, then named will just not write anything more to it until the file is reopened; exceeding the size does not automatically trigger a reopen. The default behavior is to not limit the size of the file.

If you use the version logfile option, then named will retain that many backup versions of the file by renaming them when opening. For example, if you choose to keep 3 old versions of the file "lamers.log" then just before it is opened lamers.log.1 is renamed to lamers.log.2, lamers.log.0 is renamed to lamers.log.1, and lamers.log is renamed to lamers.log.0. No rolled versions are kept by default. The unlimited keyword is synonymous with 99 in current BIND releases.

The argument for the syslog() clause is a syslog() facility as described in the syslog(3) manual page. How syslogd(1M) will handle messages sent to this facility is described in the syslog.conf(4) manual page. If you have a system which uses a very old version of syslog() that only uses two arguments to the openlog() function, then this clause is silently ignored.

The severity clause works like the "priorities" to syslog(), except that they can also be used if you are writing straight to a file rather than using syslog(). Messages which are not at least of the severity level given will not be selected for the channel; messages of higher severity levels will be accepted.

If you are using syslog(), then the syslog.conf priorities will also determine what eventually passes through. For example, defining a channel facility and severity as daemon and debug but only logging daemon.warning by way of syslog.conf will cause messages of severity info and notice to be dropped. If the situation were reversed, with named writing messages of only warning or higher, then syslogd would print all messages it received from the channel.

The server can supply extensive debugging information when it is in debugging mode. If the server's global debug level is greater than zero, then debugging mode will be active. The global debug level is set either by starting the server with the -d option followed by a positive integer, or sending the server the SIGUSR1 signal (for example, by using "ndc trace "). The global debug level can be set to zero, and debugging mode turned off, by sending the server the SIGUSR2 signal ("ndc notrace". All debugging messages in the server have a debug level, and higher debug levels give more more detailed output. Channels that specify a specific debug severity, for example:

   
   channel specific_debug_level {
         file "foo";
         severity debug 3;
     };

will get debugging output of level 3 or less any time the server is in debugging mode, regardless of the global debugging level. Channels with dynamic severity use the server's global level to determine what messages to print.

If print-time has been turned on, then the date and time will be logged. print-time may be specified for a syslog() channel, but is usually pointless since syslog() also prints the date and time. If print-category is requested, then the category of the message will be logged as well. Finally, if print-severity is on, then the severity level of the message will be logged. The print- options may be used in any combination, and will always be printed in the following order: time, category, severity. Here is an example where all three print- options are on:

28-Apr-1997 15:05:32.863 default: notice: Ready to answer queries.

There are four predefined channels that are used for default logging for in.named as follows. How they are used is described in the next section.


     channel default_syslog {
         syslog daemon;        # send to syslog's daemon facility
         severity info;        # only send priority info and higher
      };

     channel default_debug {
         file "named.run";     # write to named.run in the working directory
         severity dynamic;     # log at the server's current debug level
     };

     channel default_stderr {  # writes to stderr
         file "<stderr>";      # this is illustrative only;
                               # there's currently
                               # no way of specifying an internal file
                               # descriptor in the configuration language.
         severity info;        # only send priority info and higher
              };

     channel null {
         null;                 # toss anything sent to this channel
     };

Once a channel is defined, it cannot be redefined. Thus you cannot alter the built-in channels directly, but you can modify the default logging by pointing categories at channels you have defined.

The Category Phase

There are many categories, so you can send the logs you want to see wherever you want, without seeing logs you do not want. If you do not specify a list of channels for a category, then log messages in that category will be sent to the default category instead. If do not specify a default category, the following "default default" is used:

category default { default_syslog; default_debug; };

For example, if you want to log security events to a file, but you also want keep the default logging behavior, specify the following:


     channel my_security_channel {
         file "my_security_file";
         severity info;
      };
     category security { my_security_channel; default_syslog; default_debug; };

To discard all messages in a category, specify the null channel:


     category lame-servers { null; };
     category cname { null; };

The following categories are available:

     default   The catch-all. Many things still are  not  classi-
               fied  into  categories,  and they all end up here.
               Also, if you do  notspecify  any  channels  for  a
               category,  the  default category is  used instead.
               If you do not define  the  default  category,  the
               following definition is used:

     category default { default_syslog; default_debug; };

     config    High-level configuration file processing.

     parser    Low-level configuration file processing.

     queries   A short log message is generated for  every  query
               the server receives.

     lame-servers
               Messages like "Lame server on ..."

     statistics
               Statistics.

     panic     If the server has to shut itself down  due  to  an
               internal  problem, it will log the problem in this
               category  as  well  as  in  the  problem's  native
               category. If you do not define the panic category,
               the following definition is used:

     category panic { default_syslog; default_stderr; };

     update    Dynamic updates.

     ncache    Negative caching.

     xfer-in   Zone transfers the server is receiving.

     xfer-out  Zone transfers the server is sending.

     db        All database operations.

     eventlib  Debugging info from the  event  system.  Only  one
               channel  may  be  specified for this category, and
               it must be a file channel. If  you do  not  define
               the eventlib category, the following definition is
               used:

     category eventlib { default_debug; };

     packet    Dumps of packets received and sent. Only one chan-
               nel  may  be  specified  for this category, and it
               must be a file channel. If  you do not define  the
               packet category, the following definition is used:

     category packet { default_debug; };

     notify    The NOTIFY protocol.

     cname     Messages like "... points to a CNAME".

     security  Approved/unapproved requests.

     os        Operating system problems.

     insist    Internal consistency check failures.

     maintenance
               Periodic maintenance events.

     load      Zone loading messages.

     response-checks
               Messages arising from response checking,  such  as
               "Malformed                         response  ...",
               "wrong ans. name ...", "unrelated additional
                   info ...", "invalid RR  type  ...",  and  "bad
               referral ...".

References:

  1. Chaper 7. DNS Supported Log Format
    LogReport
  2. Determining/hiding BIND version number
    bandonhutchinson.com - Linux, Solaris, and general UNIX notes
  3. BIND logging
    bandonhutchinson.com - Linux, Solaris, and general UNIX notes
  4. How do I restart BIND??
    Date: May 8, 2003
    Dev Shed Forums - Open Source web development

Valid HTML 4.01 Transitional