## modules/pw/protocol_whois.c

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. PW_interact

    /***************************************
      $Revision: 1.14 $

      Protocol whois module (pw).  Whois protocol.

      Status: NOT REVUED, NOT TESTED

      ******************/ /******************
      Filename            : protocol_whois.c
      Author              : ottrey@ripe.net
      OSs Tested          : Solaris
      ******************/ /******************
      Copyright (c) 1999                              RIPE NCC

      All Rights Reserved

      Permission to use, copy, modify, and distribute this software and its
      documentation for any purpose and without fee is hereby granted,
      provided that the above copyright notice appear in all copies and that
      both that copyright notice and this permission notice appear in
      supporting documentation, and that the name of the author not be
      used in advertising or publicity pertaining to distribution of the
      software without specific, written prior permission.

      THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
      ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
      AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
      DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
      AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      ***************************************/
    #include <stdio.h>
    #include <glib.h>

    #include "NAME"

    #include "protocol_whois.h"
    #include "mysql_driver.h"
    #include "query_command.h"
    #include "query_instructions.h"
    #include "constants.h"
    /*
    #include "objects.h"
    */
    #include "access_control.h"
    #include "socket.h"

    /* PW_interact() */
    /*++++++++++++++++++++++++++++++++++++++
      Interact with the client.

      int sock Socket that client is connected to.

      More:
      +html+ <PRE>
      Authors:
            ottrey

      +html+ </PRE><DL COMPACT>
      +html+ <DT>Online References:
      +html+ <DD><UL>
      +html+ </UL></DL>

      ++++++++++++++++++++++++++++++++++++++*/
    void PW_interact(int sock) {
    /* [<][>][^][v][top][bottom][index][help] */
      char input[MAX_INPUT_SIZE];
      int connected = 1;
      int read_result;
      char *welcome=NULL;
      int input_length;
      int k_flag;
      char *str=NULL;
      char *str1=NULL;
      char *str2=NULL;
      char *hostaddress=NULL;

      Query_environ *qe=NULL;
      Query_command *qc=NULL;
      Query_instructions *qis=NULL;

      /* Initialize the query environment. */
      qe = QC_environ_new();

      /* Initialize the flag that instructs to keep the connection open. */
      k_flag = 0;

      /* Get the name of the client's address. */
      /* XXX I can't get this to work.  :-(
      */
      hostaddress = SK_getpeername(sock);
      printf("SK address: %s\n", hostaddress);

      while ((connected == 1) && (CO_get_whois_suspended() == 0)) {
        /* Read input */
        read_result = SK_gets(sock, input, MAX_INPUT_SIZE);
        if (read_result == -1) {
          connected = 0;
        }
        else if (read_result == -2) {
          printf("Thread received a control-c\n");
          connected = 0;
        }

        /* Default to process the whois query */
        input_length = strlen(input);
        if (input_length > 0) {

          printf("whois %s\n", input);
          /* Make a new query */
          QC_free(qc);
          qc = QC_new(input, sock, qe);

          /* Update the query environment */
          qe = QC_environ_update(qc, qe);

          /* Send the banner to the client. */
          SK_puts(sock, CVS_NAME);

          /* Send the environment aswell. */
          SK_puts(sock, "% Environment={");
          str1 = QC_environ_to_string(*qe);
          SK_puts(sock, str1);
          free(str1);
          SK_puts(sock, "}\n");

          /* Only do a query if there are keys. */
          if (qc->keys != NULL) {
            if (strcmp(qc->keys, "") != 0) {
              /* Perform the query */

              /* Log the query in the access control. */
              AC_log(hostaddress);

              /* Send the client info aswell. */
              SK_puts(sock, "% Host_info={");
              str2 = AC_hostinfo_to_string(hostaddress);
              SK_puts(sock, str2);
              free(str2);
              SK_puts(sock, "}\n");

              /* Finally send a blank line */
              SK_puts(sock, "\n");

              QI_free(qis);

              qis = QI_new(qc, sock);
              g_list_foreach(qc->qe.sources_list, QI_execute, qis);
            }
            else {
              if (qc->t != 0) {
                /* Perform a template query */
                /*
                str = OB_attribute_i_to_string(qc->t, 0);
                */
                if (str != NULL) {
                  SK_puts(sock, str);
                  SK_puts(sock, "\n");
                  free(str);
                }
                else {
                  SK_puts(sock, "Not an object.\n");
                  SK_puts(sock, USAGE);
                }
              }
              if (qc->v != 0) {
                /* Perform a verbose template query */
                /*
                str = OB_attribute_i_to_string(qc->v, 1);
                */
                if (str != NULL) {
                  SK_puts(sock, str);
                  free(str);
                }
                else {
                  SK_puts(sock, "Not an object.\n");
                  SK_puts(sock, USAGE);
                }
              }
            }
          }

          /* Put a newline at the end of the query. */
          SK_puts(sock, "\n");

          connected = qe->k;
        }
        else {
          /* An empty query (Ie return) was sent */
          /*
          printf("whois WHAT???\n");
          SK_puts(sock, "whois WHAT???\n");
          */
          connected = 0;
        }
      }

      /* Free the query_environ */
      QC_environ_free(qe);

      /* Free the hostaddress */
      free(hostaddress);

      SK_close(sock);

    } /* PW_interact() */

/* [<][>][^][v][top][bottom][index][help] */